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 = {
29 .ignore_offload_bitfield = 1,
30 .offloads = DEV_RX_OFFLOAD_CRC_STRIP,
33 .mq_mode = ETH_MQ_TX_NONE,
37 #define MAX_QUEUE_NUM 1
38 #define RX_QUEUE_NUM 1
39 #define TX_QUEUE_NUM 1
41 #define MAX_DESC_NUM 0x400
42 #define RX_DESC_NUM 0x100
43 #define TX_DESC_NUM 0x200
45 #define RX_SYNC_NUM 0x20
46 #define TX_SYNC_NUM 0x20
48 struct rte_netmap_port_conf port_conf = {
49 .eth_conf = ð_conf,
50 .socket_id = SOCKET_ID_ANY,
51 .nr_tx_rings = TX_QUEUE_NUM,
52 .nr_rx_rings = RX_QUEUE_NUM,
53 .nr_tx_slots = TX_DESC_NUM,
54 .nr_rx_slots = RX_DESC_NUM,
55 .tx_burst = TX_SYNC_NUM,
56 .rx_burst = RX_SYNC_NUM,
59 struct rte_netmap_conf netmap_conf = {
60 .socket_id = SOCKET_ID_ANY,
61 .max_bufsz = BUF_SIZE,
62 .max_rings = MAX_QUEUE_NUM,
63 .max_slots = MAX_DESC_NUM,
68 #define MAX_PORT_NUM 2
72 struct netmap_if *nmif;
73 struct netmap_ring *rx_ring;
74 struct netmap_ring *tx_ring;
81 struct netmap_port p[MAX_PORT_NUM];
86 usage(const char *prgname)
88 fprintf(stderr, "Usage: %s [EAL args] -- [OPTION]...\n"
89 "-h, --help \t Show this help message and exit\n"
90 "-i INTERFACE_A \t Interface (DPDK port number) to use\n"
91 "[ -i INTERFACE_B \t Interface (DPDK port number) to use ]\n",
96 parse_portid(const char *portid_str)
101 id = strtoul(portid_str, &end, 10);
103 if (end == portid_str || *end != '\0' || id > RTE_MAX_ETHPORTS)
104 rte_exit(EXIT_FAILURE, "Invalid port number\n");
110 parse_args(int argc, char **argv)
114 while ((opt = getopt(argc, argv, "hi:")) != -1) {
118 rte_exit(EXIT_SUCCESS, "exiting...");
121 if (ports.num >= RTE_DIM(ports.p)) {
123 rte_exit(EXIT_FAILURE, "configs with %u "
124 "ports are not supported\n",
129 ports.p[ports.num].str = optarg;
130 ports.p[ports.num].id = parse_portid(optarg);
135 rte_exit(EXIT_FAILURE, "invalid option: %c\n", opt);
142 static void sigint_handler(__rte_unused int sig)
145 signal(SIGINT, SIG_DFL);
148 static void move(int n, struct netmap_ring *rx, struct netmap_ring *tx)
153 tmp = tx->slot[tx->cur].buf_idx;
155 tx->slot[tx->cur].buf_idx = rx->slot[rx->cur].buf_idx;
156 tx->slot[tx->cur].len = rx->slot[rx->cur].len;
157 tx->slot[tx->cur].flags |= NS_BUF_CHANGED;
158 tx->cur = NETMAP_RING_NEXT(tx, tx->cur);
161 rx->slot[rx->cur].buf_idx = tmp;
162 rx->slot[rx->cur].flags |= NS_BUF_CHANGED;
163 rx->cur = NETMAP_RING_NEXT(rx, rx->cur);
169 netmap_port_open(uint32_t idx)
172 struct netmap_port *port;
175 port = ports.p + idx;
177 port->fd = rte_netmap_open("/dev/netmap", O_RDWR);
179 snprintf(req.nr_name, sizeof(req.nr_name), "%s", port->str);
180 req.nr_version = NETMAP_API;
183 err = rte_netmap_ioctl(port->fd, NIOCGINFO, &req);
185 printf("[E] NIOCGINFO ioctl failed (error %d)\n", err);
189 snprintf(req.nr_name, sizeof(req.nr_name), "%s", port->str);
190 req.nr_version = NETMAP_API;
193 err = rte_netmap_ioctl(port->fd, NIOCREGIF, &req);
195 printf("[E] NIOCREGIF ioctl failed (error %d)\n", err);
199 /* mmap only once. */
200 if (ports.mem == NULL)
201 ports.mem = rte_netmap_mmap(NULL, req.nr_memsize,
202 PROT_WRITE | PROT_READ, MAP_PRIVATE, port->fd, 0);
204 if (ports.mem == MAP_FAILED) {
205 printf("[E] NETMAP mmap failed for fd: %d)\n", port->fd);
209 port->nmif = NETMAP_IF(ports.mem, req.nr_offset);
211 port->tx_ring = NETMAP_TXRING(port->nmif, 0);
212 port->rx_ring = NETMAP_RXRING(port->nmif, 0);
218 int main(int argc, char *argv[])
223 struct pollfd pollfd[MAX_PORT_NUM];
224 struct rte_mempool *pool;
225 struct netmap_ring *rx_ring, *tx_ring;
227 ret = rte_eal_init(argc, argv);
229 rte_exit(EXIT_FAILURE, "Cannot initialize EAL\n");
234 parse_args(argc, argv);
237 rte_exit(EXIT_FAILURE, "no ports specified\n");
239 if (rte_eth_dev_count_avail() < 1)
240 rte_exit(EXIT_FAILURE, "Not enough ethernet ports available\n");
242 pool = rte_pktmbuf_pool_create("mbuf_pool", MBUF_PER_POOL, 32, 0,
243 MBUF_DATA_SIZE, rte_socket_id());
245 rte_exit(EXIT_FAILURE, "Couldn't create mempool\n");
247 netmap_conf.socket_id = rte_socket_id();
248 err = rte_netmap_init(&netmap_conf);
251 rte_exit(EXIT_FAILURE,
252 "Couldn't initialize librte_compat_netmap\n");
254 printf("librte_compat_netmap initialized\n");
256 port_conf.pool = pool;
257 port_conf.socket_id = rte_socket_id();
259 for (i = 0; i != ports.num; i++) {
261 err = rte_netmap_init_port(ports.p[i].id, &port_conf);
263 rte_exit(EXIT_FAILURE, "Couldn't setup port %hhu\n",
266 rte_eth_promiscuous_enable(ports.p[i].id);
269 for (i = 0; i != ports.num; i++) {
271 err = netmap_port_open(i);
273 rte_exit(EXIT_FAILURE, "Couldn't set port %hhu "
274 "under NETMAP control\n",
278 printf("Port %hhu now in Netmap mode\n", ports.p[i].id);
281 memset(pollfd, 0, sizeof(pollfd));
283 for (i = 0; i != ports.num; i++) {
284 pollfd[i].fd = ports.p[i].fd;
285 pollfd[i].events = POLLIN | POLLOUT;
288 signal(SIGINT, sigint_handler);
290 pmsk = ports.num - 1;
292 printf("Bridge up and running!\n");
297 pollfd[0].revents = 0;
298 pollfd[1].revents = 0;
300 ret = rte_netmap_poll(pollfd, ports.num, 0);
303 printf("[E] poll returned with error %d\n", ret);
306 if (((pollfd[0].revents | pollfd[1].revents) & POLLERR) != 0) {
307 printf("POLLERR!\n");
310 if ((pollfd[0].revents & POLLIN) != 0 &&
311 (pollfd[pmsk].revents & POLLOUT) != 0) {
313 rx_ring = ports.p[0].rx_ring;
314 tx_ring = ports.p[pmsk].tx_ring;
316 n_pkts = RTE_MIN(rx_ring->avail, tx_ring->avail);
317 move(n_pkts, rx_ring, tx_ring);
320 if (pmsk != 0 && (pollfd[pmsk].revents & POLLIN) != 0 &&
321 (pollfd[0].revents & POLLOUT) != 0) {
323 rx_ring = ports.p[pmsk].rx_ring;
324 tx_ring = ports.p[0].tx_ring;
326 n_pkts = RTE_MIN(rx_ring->avail, tx_ring->avail);
327 move(n_pkts, rx_ring, tx_ring);
331 printf("Bridge stopped!\n");
333 for (i = 0; i != ports.num; i++) {
334 err = rte_netmap_ioctl(ports.p[i].fd, NIOCUNREGIF, &req);
336 printf("[E] NIOCUNREGIF ioctl failed (error %d)\n",
340 printf("Port %hhu unregistered from Netmap mode\n", ports.p[i].id);
342 rte_netmap_close(ports.p[i].fd);