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 struct rte_eth_txconf tx_conf = {
78 .txq_flags = (ETH_TXQ_FLAGS_NOMULTSEGS |
79 ETH_TXQ_FLAGS_NOVLANOFFL |
80 ETH_TXQ_FLAGS_NOXSUMSCTP |
81 ETH_TXQ_FLAGS_NOXSUMUDP |
82 ETH_TXQ_FLAGS_NOXSUMTCP)
85 struct rte_eth_rxconf rx_conf = {
93 #define MAX_QUEUE_NUM 1
94 #define RX_QUEUE_NUM 1
95 #define TX_QUEUE_NUM 1
97 #define MAX_DESC_NUM 0x400
98 #define RX_DESC_NUM 0x100
99 #define TX_DESC_NUM 0x200
101 #define RX_SYNC_NUM 0x20
102 #define TX_SYNC_NUM 0x20
104 struct rte_netmap_port_conf port_conf = {
105 .eth_conf = ð_conf,
108 .socket_id = SOCKET_ID_ANY,
109 .nr_tx_rings = TX_QUEUE_NUM,
110 .nr_rx_rings = RX_QUEUE_NUM,
111 .nr_tx_slots = TX_DESC_NUM,
112 .nr_rx_slots = RX_DESC_NUM,
113 .tx_burst = TX_SYNC_NUM,
114 .rx_burst = RX_SYNC_NUM,
117 struct rte_netmap_conf netmap_conf = {
118 .socket_id = SOCKET_ID_ANY,
119 .max_bufsz = BUF_SIZE,
120 .max_rings = MAX_QUEUE_NUM,
121 .max_slots = MAX_DESC_NUM,
126 #define MAX_PORT_NUM 2
130 struct netmap_if *nmif;
131 struct netmap_ring *rx_ring;
132 struct netmap_ring *tx_ring;
139 struct netmap_port p[MAX_PORT_NUM];
144 usage(const char *prgname)
146 fprintf(stderr, "Usage: %s [EAL args] -- [OPTION]...\n"
147 "-h, --help \t Show this help message and exit\n"
148 "-i INTERFACE_A \t Interface (DPDK port number) to use\n"
149 "[ -i INTERFACE_B \t Interface (DPDK port number) to use ]\n",
154 parse_portid(const char *portid_str)
159 id = strtoul(portid_str, &end, 10);
161 if (end == portid_str || *end != '\0' || id > RTE_MAX_ETHPORTS)
162 rte_exit(EXIT_FAILURE, "Invalid port number\n");
168 parse_args(int argc, char **argv)
172 while ((opt = getopt(argc, argv, "hi:")) != -1) {
176 rte_exit(EXIT_SUCCESS, "exiting...");
179 if (ports.num >= RTE_DIM(ports.p)) {
181 rte_exit(EXIT_FAILURE, "configs with %u "
182 "ports are not supported\n",
187 ports.p[ports.num].str = optarg;
188 ports.p[ports.num].id = parse_portid(optarg);
193 rte_exit(EXIT_FAILURE, "invalid option: %c\n", opt);
200 static void sigint_handler(__rte_unused int sig)
203 signal(SIGINT, SIG_DFL);
206 static void move(int n, struct netmap_ring *rx, struct netmap_ring *tx)
211 tmp = tx->slot[tx->cur].buf_idx;
213 tx->slot[tx->cur].buf_idx = rx->slot[rx->cur].buf_idx;
214 tx->slot[tx->cur].len = rx->slot[rx->cur].len;
215 tx->slot[tx->cur].flags |= NS_BUF_CHANGED;
216 tx->cur = NETMAP_RING_NEXT(tx, tx->cur);
219 rx->slot[rx->cur].buf_idx = tmp;
220 rx->slot[rx->cur].flags |= NS_BUF_CHANGED;
221 rx->cur = NETMAP_RING_NEXT(rx, rx->cur);
227 netmap_port_open(uint32_t idx)
230 struct netmap_port *port;
233 port = ports.p + idx;
235 port->fd = rte_netmap_open("/dev/netmap", O_RDWR);
237 rte_snprintf(req.nr_name, sizeof(req.nr_name), "%s", port->str);
238 req.nr_version = NETMAP_API;
241 err = rte_netmap_ioctl(port->fd, NIOCGINFO, &req);
243 printf("[E] NIOCGINFO ioctl failed (error %d)\n", err);
247 rte_snprintf(req.nr_name, sizeof(req.nr_name), "%s", port->str);
248 req.nr_version = NETMAP_API;
251 err = rte_netmap_ioctl(port->fd, NIOCREGIF, &req);
253 printf("[E] NIOCREGIF ioctl failed (error %d)\n", err);
257 /* mmap only once. */
258 if (ports.mem == NULL)
259 ports.mem = rte_netmap_mmap(NULL, req.nr_memsize,
260 PROT_WRITE | PROT_READ, MAP_PRIVATE, port->fd, 0);
262 if (ports.mem == MAP_FAILED) {
263 printf("[E] NETMAP mmap failed for fd: %d)\n", port->fd);
267 port->nmif = NETMAP_IF(ports.mem, req.nr_offset);
269 port->tx_ring = NETMAP_TXRING(port->nmif, 0);
270 port->rx_ring = NETMAP_RXRING(port->nmif, 0);
276 int main(int argc, char *argv[])
281 struct pollfd pollfd[MAX_PORT_NUM];
282 struct rte_mempool *pool;
283 struct netmap_ring *rx_ring, *tx_ring;
285 ret = rte_eal_init(argc, argv);
287 rte_exit(EXIT_FAILURE, "Cannot initialize EAL\n");
292 parse_args(argc, argv);
295 rte_exit(EXIT_FAILURE, "no ports specified\n");
297 err = rte_pmd_init_all();
299 rte_exit(EXIT_FAILURE, "rte_pmd_init_all(): error %d\n", err);
301 err = rte_eal_pci_probe();
303 rte_exit(EXIT_FAILURE, "rte_eal_pci_probe(): error %d\n", err);
305 if (rte_eth_dev_count() < 1)
306 rte_exit(EXIT_FAILURE, "Not enough ethernet ports available\n");
308 pool = rte_mempool_create("mbuf_pool", MBUF_PER_POOL, MBUF_SIZE, 32,
309 sizeof(struct rte_pktmbuf_pool_private),
310 rte_pktmbuf_pool_init, NULL,
311 rte_pktmbuf_init, NULL,
314 rte_exit(EXIT_FAILURE, "Couldn't create mempool\n");
316 netmap_conf.socket_id = rte_socket_id();
317 err = rte_netmap_init(&netmap_conf);
320 rte_exit(EXIT_FAILURE,
321 "Couldn't initialize librte_compat_netmap\n");
323 printf("librte_compat_netmap initialized\n");
325 port_conf.pool = pool;
326 port_conf.socket_id = rte_socket_id();
328 for (i = 0; i != ports.num; i++) {
330 err = rte_netmap_init_port(ports.p[i].id, &port_conf);
332 rte_exit(EXIT_FAILURE, "Couldn't setup port %hhu\n",
335 rte_eth_promiscuous_enable(ports.p[i].id);
338 for (i = 0; i != ports.num; i++) {
340 err = netmap_port_open(i);
342 rte_exit(EXIT_FAILURE, "Couldn't set port %hhu "
343 "under NETMAP control\n",
347 printf("Port %hhu now in Netmap mode\n", ports.p[i].id);
350 memset(pollfd, 0, sizeof(pollfd));
352 for (i = 0; i != ports.num; i++) {
353 pollfd[i].fd = ports.p[i].fd;
354 pollfd[i].events = POLLIN | POLLOUT;
357 signal(SIGINT, sigint_handler);
359 pmsk = ports.num - 1;
361 printf("Bridge up and running!\n");
366 pollfd[0].revents = 0;
367 pollfd[1].revents = 0;
369 ret = rte_netmap_poll(pollfd, ports.num, 0);
372 printf("[E] poll returned with error %d\n", ret);
375 if (((pollfd[0].revents | pollfd[1].revents) & POLLERR) != 0) {
376 printf("POLLERR!\n");
379 if ((pollfd[0].revents & POLLIN) != 0 &&
380 (pollfd[pmsk].revents & POLLOUT) != 0) {
382 rx_ring = ports.p[0].rx_ring;
383 tx_ring = ports.p[pmsk].tx_ring;
385 n_pkts = RTE_MIN(rx_ring->avail, tx_ring->avail);
386 move(n_pkts, rx_ring, tx_ring);
389 if (pmsk != 0 && (pollfd[pmsk].revents & POLLIN) != 0 &&
390 (pollfd[0].revents & POLLOUT) != 0) {
392 rx_ring = ports.p[pmsk].rx_ring;
393 tx_ring = ports.p[0].tx_ring;
395 n_pkts = RTE_MIN(rx_ring->avail, tx_ring->avail);
396 move(n_pkts, rx_ring, tx_ring);
400 printf("Bridge stopped!\n");
402 for (i = 0; i != ports.num; i++) {
403 err = rte_netmap_ioctl(ports.p[i].fd, NIOCUNREGIF, &req);
405 printf("[E] NIOCUNREGIF ioctl failed (error %d)\n",
409 printf("Port %hhu unregistered from Netmap mode\n", ports.p[i].id);
411 rte_netmap_close(ports.p[i].fd);