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 .offloads = DEV_RX_OFFLOAD_CRC_STRIP,
32 .mq_mode = ETH_MQ_TX_NONE,
36 #define MAX_QUEUE_NUM 1
37 #define RX_QUEUE_NUM 1
38 #define TX_QUEUE_NUM 1
40 #define MAX_DESC_NUM 0x400
41 #define RX_DESC_NUM 0x100
42 #define TX_DESC_NUM 0x200
44 #define RX_SYNC_NUM 0x20
45 #define TX_SYNC_NUM 0x20
47 struct rte_netmap_port_conf port_conf = {
48 .eth_conf = ð_conf,
49 .socket_id = SOCKET_ID_ANY,
50 .nr_tx_rings = TX_QUEUE_NUM,
51 .nr_rx_rings = RX_QUEUE_NUM,
52 .nr_tx_slots = TX_DESC_NUM,
53 .nr_rx_slots = RX_DESC_NUM,
54 .tx_burst = TX_SYNC_NUM,
55 .rx_burst = RX_SYNC_NUM,
58 struct rte_netmap_conf netmap_conf = {
59 .socket_id = SOCKET_ID_ANY,
60 .max_bufsz = BUF_SIZE,
61 .max_rings = MAX_QUEUE_NUM,
62 .max_slots = MAX_DESC_NUM,
67 #define MAX_PORT_NUM 2
71 struct netmap_if *nmif;
72 struct netmap_ring *rx_ring;
73 struct netmap_ring *tx_ring;
80 struct netmap_port p[MAX_PORT_NUM];
85 usage(const char *prgname)
87 fprintf(stderr, "Usage: %s [EAL args] -- [OPTION]...\n"
88 "-h, --help \t Show this help message and exit\n"
89 "-i INTERFACE_A \t Interface (DPDK port number) to use\n"
90 "[ -i INTERFACE_B \t Interface (DPDK port number) to use ]\n",
95 parse_portid(const char *portid_str)
100 id = strtoul(portid_str, &end, 10);
102 if (end == portid_str || *end != '\0' || id > RTE_MAX_ETHPORTS)
103 rte_exit(EXIT_FAILURE, "Invalid port number\n");
109 parse_args(int argc, char **argv)
113 while ((opt = getopt(argc, argv, "hi:")) != -1) {
117 rte_exit(EXIT_SUCCESS, "exiting...");
120 if (ports.num >= RTE_DIM(ports.p)) {
122 rte_exit(EXIT_FAILURE, "configs with %u "
123 "ports are not supported\n",
128 ports.p[ports.num].str = optarg;
129 ports.p[ports.num].id = parse_portid(optarg);
134 rte_exit(EXIT_FAILURE, "invalid option: %c\n", opt);
141 static void sigint_handler(__rte_unused int sig)
144 signal(SIGINT, SIG_DFL);
147 static void move(int n, struct netmap_ring *rx, struct netmap_ring *tx)
152 tmp = tx->slot[tx->cur].buf_idx;
154 tx->slot[tx->cur].buf_idx = rx->slot[rx->cur].buf_idx;
155 tx->slot[tx->cur].len = rx->slot[rx->cur].len;
156 tx->slot[tx->cur].flags |= NS_BUF_CHANGED;
157 tx->cur = NETMAP_RING_NEXT(tx, tx->cur);
160 rx->slot[rx->cur].buf_idx = tmp;
161 rx->slot[rx->cur].flags |= NS_BUF_CHANGED;
162 rx->cur = NETMAP_RING_NEXT(rx, rx->cur);
168 netmap_port_open(uint32_t idx)
171 struct netmap_port *port;
174 port = ports.p + idx;
176 port->fd = rte_netmap_open("/dev/netmap", O_RDWR);
178 snprintf(req.nr_name, sizeof(req.nr_name), "%s", port->str);
179 req.nr_version = NETMAP_API;
182 err = rte_netmap_ioctl(port->fd, NIOCGINFO, &req);
184 printf("[E] NIOCGINFO ioctl failed (error %d)\n", err);
188 snprintf(req.nr_name, sizeof(req.nr_name), "%s", port->str);
189 req.nr_version = NETMAP_API;
192 err = rte_netmap_ioctl(port->fd, NIOCREGIF, &req);
194 printf("[E] NIOCREGIF ioctl failed (error %d)\n", err);
198 /* mmap only once. */
199 if (ports.mem == NULL)
200 ports.mem = rte_netmap_mmap(NULL, req.nr_memsize,
201 PROT_WRITE | PROT_READ, MAP_PRIVATE, port->fd, 0);
203 if (ports.mem == MAP_FAILED) {
204 printf("[E] NETMAP mmap failed for fd: %d)\n", port->fd);
208 port->nmif = NETMAP_IF(ports.mem, req.nr_offset);
210 port->tx_ring = NETMAP_TXRING(port->nmif, 0);
211 port->rx_ring = NETMAP_RXRING(port->nmif, 0);
217 int main(int argc, char *argv[])
222 struct pollfd pollfd[MAX_PORT_NUM];
223 struct rte_mempool *pool;
224 struct netmap_ring *rx_ring, *tx_ring;
226 ret = rte_eal_init(argc, argv);
228 rte_exit(EXIT_FAILURE, "Cannot initialize EAL\n");
233 parse_args(argc, argv);
236 rte_exit(EXIT_FAILURE, "no ports specified\n");
238 if (rte_eth_dev_count_avail() < 1)
239 rte_exit(EXIT_FAILURE, "Not enough ethernet ports available\n");
241 pool = rte_pktmbuf_pool_create("mbuf_pool", MBUF_PER_POOL, 32, 0,
242 MBUF_DATA_SIZE, rte_socket_id());
244 rte_exit(EXIT_FAILURE, "Couldn't create mempool\n");
246 netmap_conf.socket_id = rte_socket_id();
247 err = rte_netmap_init(&netmap_conf);
250 rte_exit(EXIT_FAILURE,
251 "Couldn't initialize librte_compat_netmap\n");
253 printf("librte_compat_netmap initialized\n");
255 port_conf.pool = pool;
256 port_conf.socket_id = rte_socket_id();
258 for (i = 0; i != ports.num; i++) {
260 err = rte_netmap_init_port(ports.p[i].id, &port_conf);
262 rte_exit(EXIT_FAILURE, "Couldn't setup port %hhu\n",
265 rte_eth_promiscuous_enable(ports.p[i].id);
268 for (i = 0; i != ports.num; i++) {
270 err = netmap_port_open(i);
272 rte_exit(EXIT_FAILURE, "Couldn't set port %hhu "
273 "under NETMAP control\n",
277 printf("Port %hhu now in Netmap mode\n", ports.p[i].id);
280 memset(pollfd, 0, sizeof(pollfd));
282 for (i = 0; i != ports.num; i++) {
283 pollfd[i].fd = ports.p[i].fd;
284 pollfd[i].events = POLLIN | POLLOUT;
287 signal(SIGINT, sigint_handler);
289 pmsk = ports.num - 1;
291 printf("Bridge up and running!\n");
296 pollfd[0].revents = 0;
297 pollfd[1].revents = 0;
299 ret = rte_netmap_poll(pollfd, ports.num, 0);
302 printf("[E] poll returned with error %d\n", ret);
305 if (((pollfd[0].revents | pollfd[1].revents) & POLLERR) != 0) {
306 printf("POLLERR!\n");
309 if ((pollfd[0].revents & POLLIN) != 0 &&
310 (pollfd[pmsk].revents & POLLOUT) != 0) {
312 rx_ring = ports.p[0].rx_ring;
313 tx_ring = ports.p[pmsk].tx_ring;
315 n_pkts = RTE_MIN(rx_ring->avail, tx_ring->avail);
316 move(n_pkts, rx_ring, tx_ring);
319 if (pmsk != 0 && (pollfd[pmsk].revents & POLLIN) != 0 &&
320 (pollfd[0].revents & POLLOUT) != 0) {
322 rx_ring = ports.p[pmsk].rx_ring;
323 tx_ring = ports.p[0].tx_ring;
325 n_pkts = RTE_MIN(rx_ring->avail, tx_ring->avail);
326 move(n_pkts, rx_ring, tx_ring);
330 printf("Bridge stopped!\n");
332 for (i = 0; i != ports.num; i++) {
333 err = rte_netmap_ioctl(ports.p[i].fd, NIOCUNREGIF, &req);
335 printf("[E] NIOCUNREGIF ioctl failed (error %d)\n",
339 printf("Port %hhu unregistered from Netmap mode\n", ports.p[i].id);
341 rte_netmap_close(ports.p[i].fd);