xref: /dpdk/examples/skeleton/basicfwd.c (revision 98a1648109b8dbaa4e6b821c17d1f6bd86d33a9a)
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2014 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_SIZE (1600 + sizeof(struct rte_mbuf) + RTE_PKTMBUF_HEADROOM)
47 #define MBUF_CACHE_SIZE 250
48 #define BURST_SIZE 32
49 
50 static const struct rte_eth_conf port_conf_default = {
51 	.rxmode = { .max_rx_pkt_len = ETHER_MAX_LEN, },
52 };
53 
54 /*
55  * Initialises a given port using global settings and with the rx buffers
56  * coming from the mbuf_pool passed as parameter
57  */
58 static inline int
59 port_init(uint8_t port, struct rte_mempool *mbuf_pool)
60 {
61 	struct rte_eth_conf port_conf = port_conf_default;
62 	const uint16_t rx_rings = 1, tx_rings = 1;
63 	int retval;
64 	uint16_t q;
65 
66 	if (port >= rte_eth_dev_count())
67 		return -1;
68 
69 	retval = rte_eth_dev_configure(port, rx_rings, tx_rings, &port_conf);
70 	if (retval != 0)
71 		return retval;
72 
73 	for (q = 0; q < rx_rings; q++) {
74 		retval = rte_eth_rx_queue_setup(port, q, RX_RING_SIZE,
75 				rte_eth_dev_socket_id(port), NULL, mbuf_pool);
76 		if (retval < 0)
77 			return retval;
78 	}
79 
80 	for (q = 0; q < tx_rings; q++) {
81 		retval = rte_eth_tx_queue_setup(port, q, TX_RING_SIZE,
82 				rte_eth_dev_socket_id(port), NULL);
83 		if (retval < 0)
84 			return retval;
85 	}
86 
87 	retval  = rte_eth_dev_start(port);
88 	if (retval < 0)
89 		return retval;
90 
91 	struct ether_addr addr;
92 	rte_eth_macaddr_get(port, &addr);
93 	printf("Port %u MAC: %02"PRIx8" %02"PRIx8" %02"PRIx8
94 			" %02"PRIx8" %02"PRIx8" %02"PRIx8"\n",
95 			(unsigned)port,
96 			addr.addr_bytes[0], addr.addr_bytes[1],
97 			addr.addr_bytes[2], addr.addr_bytes[3],
98 			addr.addr_bytes[4], addr.addr_bytes[5]);
99 
100 	rte_eth_promiscuous_enable(port);
101 
102 	return 0;
103 }
104 
105 /*
106  * Main thread that does the work, reading from INPUT_PORT
107  * and writing to OUTPUT_PORT
108  */
109 static  __attribute__((noreturn)) void
110 lcore_main(void)
111 {
112 	const uint8_t nb_ports = rte_eth_dev_count();
113 	uint8_t port;
114 	for (port = 0; port < nb_ports; port++)
115 		if (rte_eth_dev_socket_id(port) > 0 &&
116 				rte_eth_dev_socket_id(port) !=
117 						(int)rte_socket_id())
118 			printf("WARNING, port %u is on remote NUMA node to "
119 					"polling thread.\n\tPerformance will "
120 					"not be optimal.\n", port);
121 
122 	printf("\nCore %u forwarding packets. [Ctrl+C to quit]\n",
123 			rte_lcore_id());
124 	for (;;) {
125 		for (port = 0; port < nb_ports; port++) {
126 			struct rte_mbuf *bufs[BURST_SIZE];
127 			const uint16_t nb_rx = rte_eth_rx_burst(port, 0,
128 					bufs, BURST_SIZE);
129 			if (unlikely(nb_rx == 0))
130 				continue;
131 			const uint16_t nb_tx = rte_eth_tx_burst(port ^ 1, 0,
132 					bufs, nb_rx);
133 			if (unlikely(nb_tx < nb_rx)) {
134 				uint16_t buf;
135 				for (buf = nb_tx; buf < nb_rx; buf++)
136 					rte_pktmbuf_free(bufs[buf]);
137 			}
138 		}
139 	}
140 }
141 
142 /* Main function, does initialisation and calls the per-lcore functions */
143 int
144 main(int argc, char *argv[])
145 {
146 	struct rte_mempool *mbuf_pool;
147 	unsigned nb_ports;
148 	uint8_t portid;
149 
150 	/* init EAL */
151 	int ret = rte_eal_init(argc, argv);
152 	if (ret < 0)
153 		rte_exit(EXIT_FAILURE, "Error with EAL initialization\n");
154 	argc -= ret;
155 	argv += ret;
156 
157 	nb_ports = rte_eth_dev_count();
158 	if (nb_ports < 2 || (nb_ports & 1))
159 		rte_exit(EXIT_FAILURE, "Error: number of ports must be even\n");
160 
161 	mbuf_pool = rte_mempool_create("MBUF_POOL", NUM_MBUFS * nb_ports,
162 				       MBUF_SIZE, MBUF_CACHE_SIZE,
163 				       sizeof(struct rte_pktmbuf_pool_private),
164 				       rte_pktmbuf_pool_init, NULL,
165 				       rte_pktmbuf_init, NULL,
166 				       rte_socket_id(), 0);
167 	if (mbuf_pool == NULL)
168 		rte_exit(EXIT_FAILURE, "Cannot create mbuf pool\n");
169 
170 	/* initialize all ports */
171 	for (portid = 0; portid < nb_ports; portid++)
172 		if (port_init(portid, mbuf_pool) != 0)
173 			rte_exit(EXIT_FAILURE, "Cannot init port %"PRIu8"\n",
174 					portid);
175 
176 	if (rte_lcore_count() > 1)
177 		printf("\nWARNING: Too much enabled lcores - App uses only 1 lcore\n");
178 
179 	/* call lcore_main on master core only */
180 	lcore_main();
181 	return 0;
182 }
183