1*4418919fSjohnjiang /* SPDX-License-Identifier: BSD-3-Clause
2*4418919fSjohnjiang * Copyright(c) 2018 Intel Corporation
3*4418919fSjohnjiang */
4*4418919fSjohnjiang
5*4418919fSjohnjiang #include <stdio.h>
6*4418919fSjohnjiang #include <string.h>
7*4418919fSjohnjiang
8*4418919fSjohnjiang #include <rte_eth_ring.h>
9*4418919fSjohnjiang #include <rte_ethdev.h>
10*4418919fSjohnjiang #include <rte_mbuf.h>
11*4418919fSjohnjiang #include <rte_bus_vdev.h>
12*4418919fSjohnjiang #include "rte_lcore.h"
13*4418919fSjohnjiang #include "rte_mempool.h"
14*4418919fSjohnjiang #include "rte_ring.h"
15*4418919fSjohnjiang
16*4418919fSjohnjiang #include "sample_packet_forward.h"
17*4418919fSjohnjiang
18*4418919fSjohnjiang /* Sample test to create virtual rings and tx,rx portid from rings */
19*4418919fSjohnjiang int
test_ring_setup(struct rte_ring ** ring,uint16_t * portid)20*4418919fSjohnjiang test_ring_setup(struct rte_ring **ring, uint16_t *portid)
21*4418919fSjohnjiang {
22*4418919fSjohnjiang *ring = rte_ring_create("R0", RING_SIZE, rte_socket_id(),
23*4418919fSjohnjiang RING_F_SP_ENQ | RING_F_SC_DEQ);
24*4418919fSjohnjiang if (*ring == NULL) {
25*4418919fSjohnjiang printf("%s() line %u: rte_ring_create R0 failed",
26*4418919fSjohnjiang __func__, __LINE__);
27*4418919fSjohnjiang return -1;
28*4418919fSjohnjiang }
29*4418919fSjohnjiang *portid = rte_eth_from_rings("net_ringa", ring, NUM_QUEUES,
30*4418919fSjohnjiang ring, NUM_QUEUES, rte_socket_id());
31*4418919fSjohnjiang
32*4418919fSjohnjiang return 0;
33*4418919fSjohnjiang }
34*4418919fSjohnjiang
35*4418919fSjohnjiang /* Sample test to free the mempool */
36*4418919fSjohnjiang void
test_mp_free(struct rte_mempool * mp)37*4418919fSjohnjiang test_mp_free(struct rte_mempool *mp)
38*4418919fSjohnjiang {
39*4418919fSjohnjiang rte_mempool_free(mp);
40*4418919fSjohnjiang }
41*4418919fSjohnjiang
42*4418919fSjohnjiang /* Sample test to free the virtual rings */
43*4418919fSjohnjiang void
test_ring_free(struct rte_ring * rxtx)44*4418919fSjohnjiang test_ring_free(struct rte_ring *rxtx)
45*4418919fSjohnjiang {
46*4418919fSjohnjiang rte_ring_free(rxtx);
47*4418919fSjohnjiang }
48*4418919fSjohnjiang
49*4418919fSjohnjiang /* Sample test to release the vdev */
50*4418919fSjohnjiang void
test_vdev_uninit(const char * vdev)51*4418919fSjohnjiang test_vdev_uninit(const char *vdev)
52*4418919fSjohnjiang {
53*4418919fSjohnjiang rte_vdev_uninit(vdev);
54*4418919fSjohnjiang }
55*4418919fSjohnjiang
56*4418919fSjohnjiang /* sample test to allocate the mempool */
57*4418919fSjohnjiang int
test_get_mempool(struct rte_mempool ** mp,char * poolname)58*4418919fSjohnjiang test_get_mempool(struct rte_mempool **mp, char *poolname)
59*4418919fSjohnjiang {
60*4418919fSjohnjiang *mp = rte_pktmbuf_pool_create(poolname, NB_MBUF, 32, 0,
61*4418919fSjohnjiang RTE_MBUF_DEFAULT_BUF_SIZE, rte_socket_id());
62*4418919fSjohnjiang if (*mp == NULL)
63*4418919fSjohnjiang return -1;
64*4418919fSjohnjiang return 0;
65*4418919fSjohnjiang }
66*4418919fSjohnjiang
67*4418919fSjohnjiang /* sample test to allocate buffer for pkts */
68*4418919fSjohnjiang int
test_get_mbuf_from_pool(struct rte_mempool ** mp,struct rte_mbuf ** pbuf,char * poolname)69*4418919fSjohnjiang test_get_mbuf_from_pool(struct rte_mempool **mp, struct rte_mbuf **pbuf,
70*4418919fSjohnjiang char *poolname)
71*4418919fSjohnjiang {
72*4418919fSjohnjiang int ret = 0;
73*4418919fSjohnjiang
74*4418919fSjohnjiang ret = test_get_mempool(mp, poolname);
75*4418919fSjohnjiang if (ret < 0)
76*4418919fSjohnjiang return -1;
77*4418919fSjohnjiang if (rte_pktmbuf_alloc_bulk(*mp, pbuf, NUM_PACKETS) != 0) {
78*4418919fSjohnjiang printf("%s() line %u: rte_pktmbuf_alloc_bulk failed", __func__,
79*4418919fSjohnjiang __LINE__);
80*4418919fSjohnjiang return -1;
81*4418919fSjohnjiang }
82*4418919fSjohnjiang return 0;
83*4418919fSjohnjiang }
84*4418919fSjohnjiang
85*4418919fSjohnjiang /* sample test to deallocate the allocated buffers and mempool */
86*4418919fSjohnjiang void
test_put_mbuf_to_pool(struct rte_mempool * mp,struct rte_mbuf ** pbuf)87*4418919fSjohnjiang test_put_mbuf_to_pool(struct rte_mempool *mp, struct rte_mbuf **pbuf)
88*4418919fSjohnjiang {
89*4418919fSjohnjiang int itr = 0;
90*4418919fSjohnjiang
91*4418919fSjohnjiang for (itr = 0; itr < NUM_PACKETS; itr++)
92*4418919fSjohnjiang rte_pktmbuf_free(pbuf[itr]);
93*4418919fSjohnjiang rte_mempool_free(mp);
94*4418919fSjohnjiang }
95*4418919fSjohnjiang
96*4418919fSjohnjiang /* Sample test to forward packets using virtual portids */
97*4418919fSjohnjiang int
test_packet_forward(struct rte_mbuf ** pbuf,uint16_t portid,uint16_t queue_id)98*4418919fSjohnjiang test_packet_forward(struct rte_mbuf **pbuf, uint16_t portid, uint16_t queue_id)
99*4418919fSjohnjiang {
100*4418919fSjohnjiang /* send and receive packet and check for stats update */
101*4418919fSjohnjiang if (rte_eth_tx_burst(portid, queue_id, pbuf, NUM_PACKETS)
102*4418919fSjohnjiang < NUM_PACKETS) {
103*4418919fSjohnjiang printf("%s() line %u: Error sending packet to"
104*4418919fSjohnjiang " port %d\n", __func__, __LINE__, portid);
105*4418919fSjohnjiang return -1;
106*4418919fSjohnjiang }
107*4418919fSjohnjiang if (rte_eth_rx_burst(portid, queue_id, pbuf, NUM_PACKETS)
108*4418919fSjohnjiang < NUM_PACKETS) {
109*4418919fSjohnjiang printf("%s() line %u: Error receiving packet from"
110*4418919fSjohnjiang " port %d\n", __func__, __LINE__, portid);
111*4418919fSjohnjiang return -1;
112*4418919fSjohnjiang }
113*4418919fSjohnjiang return 0;
114*4418919fSjohnjiang }
115