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