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