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"
50 #define BUF_SIZE RTE_MBUF_DEFAULT_DATAROOM
51 #define MBUF_DATA_SIZE (BUF_SIZE + RTE_PKTMBUF_HEADROOM)
53 #define MBUF_PER_POOL 8192
55 struct rte_eth_conf eth_conf
= {
65 .mq_mode
= ETH_MQ_TX_NONE
,
69 #define MAX_QUEUE_NUM 1
70 #define RX_QUEUE_NUM 1
71 #define TX_QUEUE_NUM 1
73 #define MAX_DESC_NUM 0x400
74 #define RX_DESC_NUM 0x100
75 #define TX_DESC_NUM 0x200
77 #define RX_SYNC_NUM 0x20
78 #define TX_SYNC_NUM 0x20
80 struct rte_netmap_port_conf port_conf
= {
81 .eth_conf
= ð_conf
,
82 .socket_id
= SOCKET_ID_ANY
,
83 .nr_tx_rings
= TX_QUEUE_NUM
,
84 .nr_rx_rings
= RX_QUEUE_NUM
,
85 .nr_tx_slots
= TX_DESC_NUM
,
86 .nr_rx_slots
= RX_DESC_NUM
,
87 .tx_burst
= TX_SYNC_NUM
,
88 .rx_burst
= RX_SYNC_NUM
,
91 struct rte_netmap_conf netmap_conf
= {
92 .socket_id
= SOCKET_ID_ANY
,
93 .max_bufsz
= BUF_SIZE
,
94 .max_rings
= MAX_QUEUE_NUM
,
95 .max_slots
= MAX_DESC_NUM
,
100 #define MAX_PORT_NUM 2
104 struct netmap_if
*nmif
;
105 struct netmap_ring
*rx_ring
;
106 struct netmap_ring
*tx_ring
;
113 struct netmap_port p
[MAX_PORT_NUM
];
118 usage(const char *prgname
)
120 fprintf(stderr
, "Usage: %s [EAL args] -- [OPTION]...\n"
121 "-h, --help \t Show this help message and exit\n"
122 "-i INTERFACE_A \t Interface (DPDK port number) to use\n"
123 "[ -i INTERFACE_B \t Interface (DPDK port number) to use ]\n",
128 parse_portid(const char *portid_str
)
133 id
= strtoul(portid_str
, &end
, 10);
135 if (end
== portid_str
|| *end
!= '\0' || id
> RTE_MAX_ETHPORTS
)
136 rte_exit(EXIT_FAILURE
, "Invalid port number\n");
142 parse_args(int argc
, char **argv
)
146 while ((opt
= getopt(argc
, argv
, "hi:")) != -1) {
150 rte_exit(EXIT_SUCCESS
, "exiting...");
153 if (ports
.num
>= RTE_DIM(ports
.p
)) {
155 rte_exit(EXIT_FAILURE
, "configs with %u "
156 "ports are not supported\n",
161 ports
.p
[ports
.num
].str
= optarg
;
162 ports
.p
[ports
.num
].id
= parse_portid(optarg
);
167 rte_exit(EXIT_FAILURE
, "invalid option: %c\n", opt
);
174 static void sigint_handler(__rte_unused
int sig
)
177 signal(SIGINT
, SIG_DFL
);
180 static void move(int n
, struct netmap_ring
*rx
, struct netmap_ring
*tx
)
185 tmp
= tx
->slot
[tx
->cur
].buf_idx
;
187 tx
->slot
[tx
->cur
].buf_idx
= rx
->slot
[rx
->cur
].buf_idx
;
188 tx
->slot
[tx
->cur
].len
= rx
->slot
[rx
->cur
].len
;
189 tx
->slot
[tx
->cur
].flags
|= NS_BUF_CHANGED
;
190 tx
->cur
= NETMAP_RING_NEXT(tx
, tx
->cur
);
193 rx
->slot
[rx
->cur
].buf_idx
= tmp
;
194 rx
->slot
[rx
->cur
].flags
|= NS_BUF_CHANGED
;
195 rx
->cur
= NETMAP_RING_NEXT(rx
, rx
->cur
);
201 netmap_port_open(uint32_t idx
)
204 struct netmap_port
*port
;
207 port
= ports
.p
+ idx
;
209 port
->fd
= rte_netmap_open("/dev/netmap", O_RDWR
);
211 snprintf(req
.nr_name
, sizeof(req
.nr_name
), "%s", port
->str
);
212 req
.nr_version
= NETMAP_API
;
215 err
= rte_netmap_ioctl(port
->fd
, NIOCGINFO
, &req
);
217 printf("[E] NIOCGINFO ioctl failed (error %d)\n", err
);
221 snprintf(req
.nr_name
, sizeof(req
.nr_name
), "%s", port
->str
);
222 req
.nr_version
= NETMAP_API
;
225 err
= rte_netmap_ioctl(port
->fd
, NIOCREGIF
, &req
);
227 printf("[E] NIOCREGIF ioctl failed (error %d)\n", err
);
231 /* mmap only once. */
232 if (ports
.mem
== NULL
)
233 ports
.mem
= rte_netmap_mmap(NULL
, req
.nr_memsize
,
234 PROT_WRITE
| PROT_READ
, MAP_PRIVATE
, port
->fd
, 0);
236 if (ports
.mem
== MAP_FAILED
) {
237 printf("[E] NETMAP mmap failed for fd: %d)\n", port
->fd
);
241 port
->nmif
= NETMAP_IF(ports
.mem
, req
.nr_offset
);
243 port
->tx_ring
= NETMAP_TXRING(port
->nmif
, 0);
244 port
->rx_ring
= NETMAP_RXRING(port
->nmif
, 0);
250 int main(int argc
, char *argv
[])
255 struct pollfd pollfd
[MAX_PORT_NUM
];
256 struct rte_mempool
*pool
;
257 struct netmap_ring
*rx_ring
, *tx_ring
;
259 ret
= rte_eal_init(argc
, argv
);
261 rte_exit(EXIT_FAILURE
, "Cannot initialize EAL\n");
266 parse_args(argc
, argv
);
269 rte_exit(EXIT_FAILURE
, "no ports specified\n");
271 if (rte_eth_dev_count() < 1)
272 rte_exit(EXIT_FAILURE
, "Not enough ethernet ports available\n");
274 pool
= rte_pktmbuf_pool_create("mbuf_pool", MBUF_PER_POOL
, 32, 0,
275 MBUF_DATA_SIZE
, rte_socket_id());
277 rte_exit(EXIT_FAILURE
, "Couldn't create mempool\n");
279 netmap_conf
.socket_id
= rte_socket_id();
280 err
= rte_netmap_init(&netmap_conf
);
283 rte_exit(EXIT_FAILURE
,
284 "Couldn't initialize librte_compat_netmap\n");
286 printf("librte_compat_netmap initialized\n");
288 port_conf
.pool
= pool
;
289 port_conf
.socket_id
= rte_socket_id();
291 for (i
= 0; i
!= ports
.num
; i
++) {
293 err
= rte_netmap_init_port(ports
.p
[i
].id
, &port_conf
);
295 rte_exit(EXIT_FAILURE
, "Couldn't setup port %hhu\n",
298 rte_eth_promiscuous_enable(ports
.p
[i
].id
);
301 for (i
= 0; i
!= ports
.num
; i
++) {
303 err
= netmap_port_open(i
);
305 rte_exit(EXIT_FAILURE
, "Couldn't set port %hhu "
306 "under NETMAP control\n",
310 printf("Port %hhu now in Netmap mode\n", ports
.p
[i
].id
);
313 memset(pollfd
, 0, sizeof(pollfd
));
315 for (i
= 0; i
!= ports
.num
; i
++) {
316 pollfd
[i
].fd
= ports
.p
[i
].fd
;
317 pollfd
[i
].events
= POLLIN
| POLLOUT
;
320 signal(SIGINT
, sigint_handler
);
322 pmsk
= ports
.num
- 1;
324 printf("Bridge up and running!\n");
329 pollfd
[0].revents
= 0;
330 pollfd
[1].revents
= 0;
332 ret
= rte_netmap_poll(pollfd
, ports
.num
, 0);
335 printf("[E] poll returned with error %d\n", ret
);
338 if (((pollfd
[0].revents
| pollfd
[1].revents
) & POLLERR
) != 0) {
339 printf("POLLERR!\n");
342 if ((pollfd
[0].revents
& POLLIN
) != 0 &&
343 (pollfd
[pmsk
].revents
& POLLOUT
) != 0) {
345 rx_ring
= ports
.p
[0].rx_ring
;
346 tx_ring
= ports
.p
[pmsk
].tx_ring
;
348 n_pkts
= RTE_MIN(rx_ring
->avail
, tx_ring
->avail
);
349 move(n_pkts
, rx_ring
, tx_ring
);
352 if (pmsk
!= 0 && (pollfd
[pmsk
].revents
& POLLIN
) != 0 &&
353 (pollfd
[0].revents
& POLLOUT
) != 0) {
355 rx_ring
= ports
.p
[pmsk
].rx_ring
;
356 tx_ring
= ports
.p
[0].tx_ring
;
358 n_pkts
= RTE_MIN(rx_ring
->avail
, tx_ring
->avail
);
359 move(n_pkts
, rx_ring
, tx_ring
);
363 printf("Bridge stopped!\n");
365 for (i
= 0; i
!= ports
.num
; i
++) {
366 err
= rte_netmap_ioctl(ports
.p
[i
].fd
, NIOCUNREGIF
, &req
);
368 printf("[E] NIOCUNREGIF ioctl failed (error %d)\n",
372 printf("Port %hhu unregistered from Netmap mode\n", ports
.p
[i
].id
);
374 rte_netmap_close(ports
.p
[i
].fd
);