1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
14 #include <rte_ethdev.h>
16 #include <rte_mempool.h>
17 #include <rte_string_fns.h>
18 #include "compat_netmap.h"
21 #define BUF_SIZE RTE_MBUF_DEFAULT_DATAROOM
22 #define MBUF_DATA_SIZE (BUF_SIZE + RTE_PKTMBUF_HEADROOM)
24 #define MBUF_PER_POOL 8192
26 struct rte_eth_conf eth_conf = {
36 .mq_mode = ETH_MQ_TX_NONE,
40 #define MAX_QUEUE_NUM 1
41 #define RX_QUEUE_NUM 1
42 #define TX_QUEUE_NUM 1
44 #define MAX_DESC_NUM 0x400
45 #define RX_DESC_NUM 0x100
46 #define TX_DESC_NUM 0x200
48 #define RX_SYNC_NUM 0x20
49 #define TX_SYNC_NUM 0x20
51 struct rte_netmap_port_conf port_conf = {
52 .eth_conf = ð_conf,
53 .socket_id = SOCKET_ID_ANY,
54 .nr_tx_rings = TX_QUEUE_NUM,
55 .nr_rx_rings = RX_QUEUE_NUM,
56 .nr_tx_slots = TX_DESC_NUM,
57 .nr_rx_slots = RX_DESC_NUM,
58 .tx_burst = TX_SYNC_NUM,
59 .rx_burst = RX_SYNC_NUM,
62 struct rte_netmap_conf netmap_conf = {
63 .socket_id = SOCKET_ID_ANY,
64 .max_bufsz = BUF_SIZE,
65 .max_rings = MAX_QUEUE_NUM,
66 .max_slots = MAX_DESC_NUM,
71 #define MAX_PORT_NUM 2
75 struct netmap_if *nmif;
76 struct netmap_ring *rx_ring;
77 struct netmap_ring *tx_ring;
84 struct netmap_port p[MAX_PORT_NUM];
89 usage(const char *prgname)
91 fprintf(stderr, "Usage: %s [EAL args] -- [OPTION]...\n"
92 "-h, --help \t Show this help message and exit\n"
93 "-i INTERFACE_A \t Interface (DPDK port number) to use\n"
94 "[ -i INTERFACE_B \t Interface (DPDK port number) to use ]\n",
99 parse_portid(const char *portid_str)
104 id = strtoul(portid_str, &end, 10);
106 if (end == portid_str || *end != '\0' || id > RTE_MAX_ETHPORTS)
107 rte_exit(EXIT_FAILURE, "Invalid port number\n");
113 parse_args(int argc, char **argv)
117 while ((opt = getopt(argc, argv, "hi:")) != -1) {
121 rte_exit(EXIT_SUCCESS, "exiting...");
124 if (ports.num >= RTE_DIM(ports.p)) {
126 rte_exit(EXIT_FAILURE, "configs with %u "
127 "ports are not supported\n",
132 ports.p[ports.num].str = optarg;
133 ports.p[ports.num].id = parse_portid(optarg);
138 rte_exit(EXIT_FAILURE, "invalid option: %c\n", opt);
145 static void sigint_handler(__rte_unused int sig)
148 signal(SIGINT, SIG_DFL);
151 static void move(int n, struct netmap_ring *rx, struct netmap_ring *tx)
156 tmp = tx->slot[tx->cur].buf_idx;
158 tx->slot[tx->cur].buf_idx = rx->slot[rx->cur].buf_idx;
159 tx->slot[tx->cur].len = rx->slot[rx->cur].len;
160 tx->slot[tx->cur].flags |= NS_BUF_CHANGED;
161 tx->cur = NETMAP_RING_NEXT(tx, tx->cur);
164 rx->slot[rx->cur].buf_idx = tmp;
165 rx->slot[rx->cur].flags |= NS_BUF_CHANGED;
166 rx->cur = NETMAP_RING_NEXT(rx, rx->cur);
172 netmap_port_open(uint32_t idx)
175 struct netmap_port *port;
178 port = ports.p + idx;
180 port->fd = rte_netmap_open("/dev/netmap", O_RDWR);
182 snprintf(req.nr_name, sizeof(req.nr_name), "%s", port->str);
183 req.nr_version = NETMAP_API;
186 err = rte_netmap_ioctl(port->fd, NIOCGINFO, &req);
188 printf("[E] NIOCGINFO ioctl failed (error %d)\n", err);
192 snprintf(req.nr_name, sizeof(req.nr_name), "%s", port->str);
193 req.nr_version = NETMAP_API;
196 err = rte_netmap_ioctl(port->fd, NIOCREGIF, &req);
198 printf("[E] NIOCREGIF ioctl failed (error %d)\n", err);
202 /* mmap only once. */
203 if (ports.mem == NULL)
204 ports.mem = rte_netmap_mmap(NULL, req.nr_memsize,
205 PROT_WRITE | PROT_READ, MAP_PRIVATE, port->fd, 0);
207 if (ports.mem == MAP_FAILED) {
208 printf("[E] NETMAP mmap failed for fd: %d)\n", port->fd);
212 port->nmif = NETMAP_IF(ports.mem, req.nr_offset);
214 port->tx_ring = NETMAP_TXRING(port->nmif, 0);
215 port->rx_ring = NETMAP_RXRING(port->nmif, 0);
221 int main(int argc, char *argv[])
226 struct pollfd pollfd[MAX_PORT_NUM];
227 struct rte_mempool *pool;
228 struct netmap_ring *rx_ring, *tx_ring;
230 ret = rte_eal_init(argc, argv);
232 rte_exit(EXIT_FAILURE, "Cannot initialize EAL\n");
237 parse_args(argc, argv);
240 rte_exit(EXIT_FAILURE, "no ports specified\n");
242 if (rte_eth_dev_count() < 1)
243 rte_exit(EXIT_FAILURE, "Not enough ethernet ports available\n");
245 pool = rte_pktmbuf_pool_create("mbuf_pool", MBUF_PER_POOL, 32, 0,
246 MBUF_DATA_SIZE, rte_socket_id());
248 rte_exit(EXIT_FAILURE, "Couldn't create mempool\n");
250 netmap_conf.socket_id = rte_socket_id();
251 err = rte_netmap_init(&netmap_conf);
254 rte_exit(EXIT_FAILURE,
255 "Couldn't initialize librte_compat_netmap\n");
257 printf("librte_compat_netmap initialized\n");
259 port_conf.pool = pool;
260 port_conf.socket_id = rte_socket_id();
262 for (i = 0; i != ports.num; i++) {
264 err = rte_netmap_init_port(ports.p[i].id, &port_conf);
266 rte_exit(EXIT_FAILURE, "Couldn't setup port %hhu\n",
269 rte_eth_promiscuous_enable(ports.p[i].id);
272 for (i = 0; i != ports.num; i++) {
274 err = netmap_port_open(i);
276 rte_exit(EXIT_FAILURE, "Couldn't set port %hhu "
277 "under NETMAP control\n",
281 printf("Port %hhu now in Netmap mode\n", ports.p[i].id);
284 memset(pollfd, 0, sizeof(pollfd));
286 for (i = 0; i != ports.num; i++) {
287 pollfd[i].fd = ports.p[i].fd;
288 pollfd[i].events = POLLIN | POLLOUT;
291 signal(SIGINT, sigint_handler);
293 pmsk = ports.num - 1;
295 printf("Bridge up and running!\n");
300 pollfd[0].revents = 0;
301 pollfd[1].revents = 0;
303 ret = rte_netmap_poll(pollfd, ports.num, 0);
306 printf("[E] poll returned with error %d\n", ret);
309 if (((pollfd[0].revents | pollfd[1].revents) & POLLERR) != 0) {
310 printf("POLLERR!\n");
313 if ((pollfd[0].revents & POLLIN) != 0 &&
314 (pollfd[pmsk].revents & POLLOUT) != 0) {
316 rx_ring = ports.p[0].rx_ring;
317 tx_ring = ports.p[pmsk].tx_ring;
319 n_pkts = RTE_MIN(rx_ring->avail, tx_ring->avail);
320 move(n_pkts, rx_ring, tx_ring);
323 if (pmsk != 0 && (pollfd[pmsk].revents & POLLIN) != 0 &&
324 (pollfd[0].revents & POLLOUT) != 0) {
326 rx_ring = ports.p[pmsk].rx_ring;
327 tx_ring = ports.p[0].tx_ring;
329 n_pkts = RTE_MIN(rx_ring->avail, tx_ring->avail);
330 move(n_pkts, rx_ring, tx_ring);
334 printf("Bridge stopped!\n");
336 for (i = 0; i != ports.num; i++) {
337 err = rte_netmap_ioctl(ports.p[i].fd, NIOCUNREGIF, &req);
339 printf("[E] NIOCUNREGIF ioctl failed (error %d)\n",
343 printf("Port %hhu unregistered from Netmap mode\n", ports.p[i].id);
345 rte_netmap_close(ports.p[i].fd);