4 * Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
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.
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.
43 #include <rte_ethdev.h>
45 #include <rte_mempool.h>
46 #include <rte_string_fns.h>
47 #include "compat_netmap.h"
51 #define MBUF_SIZE (BUF_SIZE + sizeof(struct rte_mbuf) + \
54 #define MBUF_PER_POOL 8192
56 struct rte_eth_conf eth_conf = {
66 .mq_mode = ETH_MQ_TX_NONE,
70 #define MAX_QUEUE_NUM 1
71 #define RX_QUEUE_NUM 1
72 #define TX_QUEUE_NUM 1
74 #define MAX_DESC_NUM 0x400
75 #define RX_DESC_NUM 0x100
76 #define TX_DESC_NUM 0x200
78 #define RX_SYNC_NUM 0x20
79 #define TX_SYNC_NUM 0x20
81 struct rte_netmap_port_conf port_conf = {
82 .eth_conf = ð_conf,
83 .socket_id = SOCKET_ID_ANY,
84 .nr_tx_rings = TX_QUEUE_NUM,
85 .nr_rx_rings = RX_QUEUE_NUM,
86 .nr_tx_slots = TX_DESC_NUM,
87 .nr_rx_slots = RX_DESC_NUM,
88 .tx_burst = TX_SYNC_NUM,
89 .rx_burst = RX_SYNC_NUM,
92 struct rte_netmap_conf netmap_conf = {
93 .socket_id = SOCKET_ID_ANY,
94 .max_bufsz = BUF_SIZE,
95 .max_rings = MAX_QUEUE_NUM,
96 .max_slots = MAX_DESC_NUM,
101 #define MAX_PORT_NUM 2
105 struct netmap_if *nmif;
106 struct netmap_ring *rx_ring;
107 struct netmap_ring *tx_ring;
114 struct netmap_port p[MAX_PORT_NUM];
119 usage(const char *prgname)
121 fprintf(stderr, "Usage: %s [EAL args] -- [OPTION]...\n"
122 "-h, --help \t Show this help message and exit\n"
123 "-i INTERFACE_A \t Interface (DPDK port number) to use\n"
124 "[ -i INTERFACE_B \t Interface (DPDK port number) to use ]\n",
129 parse_portid(const char *portid_str)
134 id = strtoul(portid_str, &end, 10);
136 if (end == portid_str || *end != '\0' || id > RTE_MAX_ETHPORTS)
137 rte_exit(EXIT_FAILURE, "Invalid port number\n");
143 parse_args(int argc, char **argv)
147 while ((opt = getopt(argc, argv, "hi:")) != -1) {
151 rte_exit(EXIT_SUCCESS, "exiting...");
154 if (ports.num >= RTE_DIM(ports.p)) {
156 rte_exit(EXIT_FAILURE, "configs with %u "
157 "ports are not supported\n",
162 ports.p[ports.num].str = optarg;
163 ports.p[ports.num].id = parse_portid(optarg);
168 rte_exit(EXIT_FAILURE, "invalid option: %c\n", opt);
175 static void sigint_handler(__rte_unused int sig)
178 signal(SIGINT, SIG_DFL);
181 static void move(int n, struct netmap_ring *rx, struct netmap_ring *tx)
186 tmp = tx->slot[tx->cur].buf_idx;
188 tx->slot[tx->cur].buf_idx = rx->slot[rx->cur].buf_idx;
189 tx->slot[tx->cur].len = rx->slot[rx->cur].len;
190 tx->slot[tx->cur].flags |= NS_BUF_CHANGED;
191 tx->cur = NETMAP_RING_NEXT(tx, tx->cur);
194 rx->slot[rx->cur].buf_idx = tmp;
195 rx->slot[rx->cur].flags |= NS_BUF_CHANGED;
196 rx->cur = NETMAP_RING_NEXT(rx, rx->cur);
202 netmap_port_open(uint32_t idx)
205 struct netmap_port *port;
208 port = ports.p + idx;
210 port->fd = rte_netmap_open("/dev/netmap", O_RDWR);
212 snprintf(req.nr_name, sizeof(req.nr_name), "%s", port->str);
213 req.nr_version = NETMAP_API;
216 err = rte_netmap_ioctl(port->fd, NIOCGINFO, &req);
218 printf("[E] NIOCGINFO ioctl failed (error %d)\n", err);
222 snprintf(req.nr_name, sizeof(req.nr_name), "%s", port->str);
223 req.nr_version = NETMAP_API;
226 err = rte_netmap_ioctl(port->fd, NIOCREGIF, &req);
228 printf("[E] NIOCREGIF ioctl failed (error %d)\n", err);
232 /* mmap only once. */
233 if (ports.mem == NULL)
234 ports.mem = rte_netmap_mmap(NULL, req.nr_memsize,
235 PROT_WRITE | PROT_READ, MAP_PRIVATE, port->fd, 0);
237 if (ports.mem == MAP_FAILED) {
238 printf("[E] NETMAP mmap failed for fd: %d)\n", port->fd);
242 port->nmif = NETMAP_IF(ports.mem, req.nr_offset);
244 port->tx_ring = NETMAP_TXRING(port->nmif, 0);
245 port->rx_ring = NETMAP_RXRING(port->nmif, 0);
251 int main(int argc, char *argv[])
256 struct pollfd pollfd[MAX_PORT_NUM];
257 struct rte_mempool *pool;
258 struct netmap_ring *rx_ring, *tx_ring;
260 ret = rte_eal_init(argc, argv);
262 rte_exit(EXIT_FAILURE, "Cannot initialize EAL\n");
267 parse_args(argc, argv);
270 rte_exit(EXIT_FAILURE, "no ports specified\n");
272 if (rte_eth_dev_count() < 1)
273 rte_exit(EXIT_FAILURE, "Not enough ethernet ports available\n");
275 pool = rte_mempool_create("mbuf_pool", MBUF_PER_POOL, MBUF_SIZE, 32,
276 sizeof(struct rte_pktmbuf_pool_private),
277 rte_pktmbuf_pool_init, NULL,
278 rte_pktmbuf_init, NULL,
281 rte_exit(EXIT_FAILURE, "Couldn't create mempool\n");
283 netmap_conf.socket_id = rte_socket_id();
284 err = rte_netmap_init(&netmap_conf);
287 rte_exit(EXIT_FAILURE,
288 "Couldn't initialize librte_compat_netmap\n");
290 printf("librte_compat_netmap initialized\n");
292 port_conf.pool = pool;
293 port_conf.socket_id = rte_socket_id();
295 for (i = 0; i != ports.num; i++) {
297 err = rte_netmap_init_port(ports.p[i].id, &port_conf);
299 rte_exit(EXIT_FAILURE, "Couldn't setup port %hhu\n",
302 rte_eth_promiscuous_enable(ports.p[i].id);
305 for (i = 0; i != ports.num; i++) {
307 err = netmap_port_open(i);
309 rte_exit(EXIT_FAILURE, "Couldn't set port %hhu "
310 "under NETMAP control\n",
314 printf("Port %hhu now in Netmap mode\n", ports.p[i].id);
317 memset(pollfd, 0, sizeof(pollfd));
319 for (i = 0; i != ports.num; i++) {
320 pollfd[i].fd = ports.p[i].fd;
321 pollfd[i].events = POLLIN | POLLOUT;
324 signal(SIGINT, sigint_handler);
326 pmsk = ports.num - 1;
328 printf("Bridge up and running!\n");
333 pollfd[0].revents = 0;
334 pollfd[1].revents = 0;
336 ret = rte_netmap_poll(pollfd, ports.num, 0);
339 printf("[E] poll returned with error %d\n", ret);
342 if (((pollfd[0].revents | pollfd[1].revents) & POLLERR) != 0) {
343 printf("POLLERR!\n");
346 if ((pollfd[0].revents & POLLIN) != 0 &&
347 (pollfd[pmsk].revents & POLLOUT) != 0) {
349 rx_ring = ports.p[0].rx_ring;
350 tx_ring = ports.p[pmsk].tx_ring;
352 n_pkts = RTE_MIN(rx_ring->avail, tx_ring->avail);
353 move(n_pkts, rx_ring, tx_ring);
356 if (pmsk != 0 && (pollfd[pmsk].revents & POLLIN) != 0 &&
357 (pollfd[0].revents & POLLOUT) != 0) {
359 rx_ring = ports.p[pmsk].rx_ring;
360 tx_ring = ports.p[0].tx_ring;
362 n_pkts = RTE_MIN(rx_ring->avail, tx_ring->avail);
363 move(n_pkts, rx_ring, tx_ring);
367 printf("Bridge stopped!\n");
369 for (i = 0; i != ports.num; i++) {
370 err = rte_netmap_ioctl(ports.p[i].fd, NIOCUNREGIF, &req);
372 printf("[E] NIOCUNREGIF ioctl failed (error %d)\n",
376 printf("Port %hhu unregistered from Netmap mode\n", ports.p[i].id);
378 rte_netmap_close(ports.p[i].fd);