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