b4d50de893c1c90843de03d4dd5f62dcd29ae363
[deb_dpdk.git] / examples / skeleton / basicfwd.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2015 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #include <stdint.h>
35 #include <inttypes.h>
36 #include <rte_eal.h>
37 #include <rte_ethdev.h>
38 #include <rte_cycles.h>
39 #include <rte_lcore.h>
40 #include <rte_mbuf.h>
41
42 #define RX_RING_SIZE 128
43 #define TX_RING_SIZE 512
44
45 #define NUM_MBUFS 8191
46 #define MBUF_CACHE_SIZE 250
47 #define BURST_SIZE 32
48
49 static const struct rte_eth_conf port_conf_default = {
50         .rxmode = { .max_rx_pkt_len = ETHER_MAX_LEN }
51 };
52
53 /* basicfwd.c: Basic DPDK skeleton forwarding example. */
54
55 /*
56  * Initializes a given port using global settings and with the RX buffers
57  * coming from the mbuf_pool passed as a parameter.
58  */
59 static inline int
60 port_init(uint8_t port, struct rte_mempool *mbuf_pool)
61 {
62         struct rte_eth_conf port_conf = port_conf_default;
63         const uint16_t rx_rings = 1, tx_rings = 1;
64         uint16_t nb_rxd = RX_RING_SIZE;
65         uint16_t nb_txd = TX_RING_SIZE;
66         int retval;
67         uint16_t q;
68
69         if (port >= rte_eth_dev_count())
70                 return -1;
71
72         /* Configure the Ethernet device. */
73         retval = rte_eth_dev_configure(port, rx_rings, tx_rings, &port_conf);
74         if (retval != 0)
75                 return retval;
76
77         retval = rte_eth_dev_adjust_nb_rx_tx_desc(port, &nb_rxd, &nb_txd);
78         if (retval != 0)
79                 return retval;
80
81         /* Allocate and set up 1 RX queue per Ethernet port. */
82         for (q = 0; q < rx_rings; q++) {
83                 retval = rte_eth_rx_queue_setup(port, q, nb_rxd,
84                                 rte_eth_dev_socket_id(port), NULL, mbuf_pool);
85                 if (retval < 0)
86                         return retval;
87         }
88
89         /* Allocate and set up 1 TX queue per Ethernet port. */
90         for (q = 0; q < tx_rings; q++) {
91                 retval = rte_eth_tx_queue_setup(port, q, nb_txd,
92                                 rte_eth_dev_socket_id(port), NULL);
93                 if (retval < 0)
94                         return retval;
95         }
96
97         /* Start the Ethernet port. */
98         retval = rte_eth_dev_start(port);
99         if (retval < 0)
100                 return retval;
101
102         /* Display the port MAC address. */
103         struct ether_addr addr;
104         rte_eth_macaddr_get(port, &addr);
105         printf("Port %u MAC: %02" PRIx8 " %02" PRIx8 " %02" PRIx8
106                            " %02" PRIx8 " %02" PRIx8 " %02" PRIx8 "\n",
107                         (unsigned)port,
108                         addr.addr_bytes[0], addr.addr_bytes[1],
109                         addr.addr_bytes[2], addr.addr_bytes[3],
110                         addr.addr_bytes[4], addr.addr_bytes[5]);
111
112         /* Enable RX in promiscuous mode for the Ethernet device. */
113         rte_eth_promiscuous_enable(port);
114
115         return 0;
116 }
117
118 /*
119  * The lcore main. This is the main thread that does the work, reading from
120  * an input port and writing to an output port.
121  */
122 static __attribute__((noreturn)) void
123 lcore_main(void)
124 {
125         const uint8_t nb_ports = rte_eth_dev_count();
126         uint8_t port;
127
128         /*
129          * Check that the port is on the same NUMA node as the polling thread
130          * for best performance.
131          */
132         for (port = 0; port < nb_ports; port++)
133                 if (rte_eth_dev_socket_id(port) > 0 &&
134                                 rte_eth_dev_socket_id(port) !=
135                                                 (int)rte_socket_id())
136                         printf("WARNING, port %u is on remote NUMA node to "
137                                         "polling thread.\n\tPerformance will "
138                                         "not be optimal.\n", port);
139
140         printf("\nCore %u forwarding packets. [Ctrl+C to quit]\n",
141                         rte_lcore_id());
142
143         /* Run until the application is quit or killed. */
144         for (;;) {
145                 /*
146                  * Receive packets on a port and forward them on the paired
147                  * port. The mapping is 0 -> 1, 1 -> 0, 2 -> 3, 3 -> 2, etc.
148                  */
149                 for (port = 0; port < nb_ports; port++) {
150
151                         /* Get burst of RX packets, from first port of pair. */
152                         struct rte_mbuf *bufs[BURST_SIZE];
153                         const uint16_t nb_rx = rte_eth_rx_burst(port, 0,
154                                         bufs, BURST_SIZE);
155
156                         if (unlikely(nb_rx == 0))
157                                 continue;
158
159                         /* Send burst of TX packets, to second port of pair. */
160                         const uint16_t nb_tx = rte_eth_tx_burst(port ^ 1, 0,
161                                         bufs, nb_rx);
162
163                         /* Free any unsent packets. */
164                         if (unlikely(nb_tx < nb_rx)) {
165                                 uint16_t buf;
166                                 for (buf = nb_tx; buf < nb_rx; buf++)
167                                         rte_pktmbuf_free(bufs[buf]);
168                         }
169                 }
170         }
171 }
172
173 /*
174  * The main function, which does initialization and calls the per-lcore
175  * functions.
176  */
177 int
178 main(int argc, char *argv[])
179 {
180         struct rte_mempool *mbuf_pool;
181         unsigned nb_ports;
182         uint8_t portid;
183
184         /* Initialize the Environment Abstraction Layer (EAL). */
185         int ret = rte_eal_init(argc, argv);
186         if (ret < 0)
187                 rte_exit(EXIT_FAILURE, "Error with EAL initialization\n");
188
189         argc -= ret;
190         argv += ret;
191
192         /* Check that there is an even number of ports to send/receive on. */
193         nb_ports = rte_eth_dev_count();
194         if (nb_ports < 2 || (nb_ports & 1))
195                 rte_exit(EXIT_FAILURE, "Error: number of ports must be even\n");
196
197         /* Creates a new mempool in memory to hold the mbufs. */
198         mbuf_pool = rte_pktmbuf_pool_create("MBUF_POOL", NUM_MBUFS * nb_ports,
199                 MBUF_CACHE_SIZE, 0, RTE_MBUF_DEFAULT_BUF_SIZE, rte_socket_id());
200
201         if (mbuf_pool == NULL)
202                 rte_exit(EXIT_FAILURE, "Cannot create mbuf pool\n");
203
204         /* Initialize all ports. */
205         for (portid = 0; portid < nb_ports; portid++)
206                 if (port_init(portid, mbuf_pool) != 0)
207                         rte_exit(EXIT_FAILURE, "Cannot init port %"PRIu8 "\n",
208                                         portid);
209
210         if (rte_lcore_count() > 1)
211                 printf("\nWARNING: Too many lcores enabled. Only 1 used.\n");
212
213         /* Call lcore_main on the master core only. */
214         lcore_main();
215
216         return 0;
217 }