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