]> git.proxmox.com Git - ceph.git/blob - ceph/src/seastar/dpdk/examples/l2fwd-cat/l2fwd-cat.c
update sources to ceph Nautilus 14.2.1
[ceph.git] / ceph / src / seastar / dpdk / examples / l2fwd-cat / l2fwd-cat.c
1 /*-
2 * BSD LICENSE
3 *
4 * Copyright(c) 2016 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 #include "cat.h"
43
44 #define RX_RING_SIZE 128
45 #define TX_RING_SIZE 512
46
47 #define NUM_MBUFS 8191
48 #define MBUF_CACHE_SIZE 250
49 #define BURST_SIZE 32
50
51 static const struct rte_eth_conf port_conf_default = {
52 .rxmode = { .max_rx_pkt_len = ETHER_MAX_LEN }
53 };
54
55 /* l2fwd-cat.c: CAT enabled, basic DPDK skeleton forwarding example. */
56
57 /*
58 * Initializes a given port using global settings and with the RX buffers
59 * coming from the mbuf_pool passed as a parameter.
60 */
61 static inline int
62 port_init(uint8_t port, struct rte_mempool *mbuf_pool)
63 {
64 struct rte_eth_conf port_conf = port_conf_default;
65 const uint16_t rx_rings = 1, tx_rings = 1;
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 /* Allocate and set up 1 RX queue per Ethernet port. */
78 for (q = 0; q < rx_rings; q++) {
79 retval = rte_eth_rx_queue_setup(port, q, RX_RING_SIZE,
80 rte_eth_dev_socket_id(port), NULL, mbuf_pool);
81 if (retval < 0)
82 return retval;
83 }
84
85 /* Allocate and set up 1 TX queue per Ethernet port. */
86 for (q = 0; q < tx_rings; q++) {
87 retval = rte_eth_tx_queue_setup(port, q, TX_RING_SIZE,
88 rte_eth_dev_socket_id(port), NULL);
89 if (retval < 0)
90 return retval;
91 }
92
93 /* Start the Ethernet port. */
94 retval = rte_eth_dev_start(port);
95 if (retval < 0)
96 return retval;
97
98 /* Display the port MAC address. */
99 struct ether_addr addr;
100 rte_eth_macaddr_get(port, &addr);
101 printf("Port %u MAC: %02" PRIx8 " %02" PRIx8 " %02" PRIx8
102 " %02" PRIx8 " %02" PRIx8 " %02" PRIx8 "\n",
103 (unsigned)port,
104 addr.addr_bytes[0], addr.addr_bytes[1],
105 addr.addr_bytes[2], addr.addr_bytes[3],
106 addr.addr_bytes[4], addr.addr_bytes[5]);
107
108 /* Enable RX in promiscuous mode for the Ethernet device. */
109 rte_eth_promiscuous_enable(port);
110
111 return 0;
112 }
113
114 /*
115 * The lcore main. This is the main thread that does the work, reading from
116 * an input port and writing to an output port.
117 */
118 static __attribute__((noreturn)) void
119 lcore_main(void)
120 {
121 const uint8_t nb_ports = rte_eth_dev_count();
122 uint8_t port;
123
124 /*
125 * Check that the port is on the same NUMA node as the polling thread
126 * for best performance.
127 */
128 for (port = 0; port < nb_ports; port++)
129 if (rte_eth_dev_socket_id(port) > 0 &&
130 rte_eth_dev_socket_id(port) !=
131 (int)rte_socket_id())
132 printf("WARNING, port %u is on remote NUMA node to "
133 "polling thread.\n\tPerformance will "
134 "not be optimal.\n", port);
135
136 printf("\nCore %u forwarding packets. [Ctrl+C to quit]\n",
137 rte_lcore_id());
138
139 /* Run until the application is quit or killed. */
140 for (;;) {
141 /*
142 * Receive packets on a port and forward them on the paired
143 * port. The mapping is 0 -> 1, 1 -> 0, 2 -> 3, 3 -> 2, etc.
144 */
145 for (port = 0; port < nb_ports; port++) {
146
147 /* Get burst of RX packets, from first port of pair. */
148 struct rte_mbuf *bufs[BURST_SIZE];
149 const uint16_t nb_rx = rte_eth_rx_burst(port, 0,
150 bufs, BURST_SIZE);
151
152 if (unlikely(nb_rx == 0))
153 continue;
154
155 /* Send burst of TX packets, to second port of pair. */
156 const uint16_t nb_tx = rte_eth_tx_burst(port ^ 1, 0,
157 bufs, nb_rx);
158
159 /* Free any unsent packets. */
160 if (unlikely(nb_tx < nb_rx)) {
161 uint16_t buf;
162 for (buf = nb_tx; buf < nb_rx; buf++)
163 rte_pktmbuf_free(bufs[buf]);
164 }
165 }
166 }
167 }
168
169 /*
170 * The main function, which does initialization and calls the per-lcore
171 * functions.
172 */
173 int
174 main(int argc, char *argv[])
175 {
176 struct rte_mempool *mbuf_pool;
177 unsigned nb_ports;
178 uint8_t portid;
179
180 /* Initialize the Environment Abstraction Layer (EAL). */
181 int ret = rte_eal_init(argc, argv);
182 if (ret < 0)
183 rte_exit(EXIT_FAILURE, "Error with EAL initialization\n");
184
185 argc -= ret;
186 argv += ret;
187
188 /*
189 * Initialize the PQoS library and configure CAT.
190 * Please see l2fwd-cat documentation for more info.
191 */
192 ret = cat_init(argc, argv);
193 if (ret < 0)
194 rte_exit(EXIT_FAILURE, "PQOS: L3CA init failed!\n");
195
196 argc -= ret;
197 argv += ret;
198
199 /* Check that there is an even number of ports to send/receive on. */
200 nb_ports = rte_eth_dev_count();
201 if (nb_ports < 2 || (nb_ports & 1))
202 rte_exit(EXIT_FAILURE, "Error: number of ports must be even\n");
203
204 /* Creates a new mempool in memory to hold the mbufs. */
205 mbuf_pool = rte_pktmbuf_pool_create("MBUF_POOL", NUM_MBUFS * nb_ports,
206 MBUF_CACHE_SIZE, 0, RTE_MBUF_DEFAULT_BUF_SIZE, rte_socket_id());
207
208 if (mbuf_pool == NULL)
209 rte_exit(EXIT_FAILURE, "Cannot create mbuf pool\n");
210
211 /* Initialize all ports. */
212 for (portid = 0; portid < nb_ports; portid++)
213 if (port_init(portid, mbuf_pool) != 0)
214 rte_exit(EXIT_FAILURE, "Cannot init port %"PRIu8 "\n",
215 portid);
216
217 if (rte_lcore_count() > 1)
218 printf("\nWARNING: Too many lcores enabled. Only 1 used.\n");
219
220 /* Call lcore_main on the master core only. */
221 lcore_main();
222
223 return 0;
224 }