a74a2cd91ae245c0270f0bf44aae4447bb970fdd
[dpdk.git] / examples / netmap_compat / bridge / bridge.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2014 Intel Corporation
3  */
4
5 #include <fcntl.h>
6 #include <getopt.h>
7 #include <inttypes.h>
8 #include <signal.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <sys/mman.h>
12
13 #include <rte_eal.h>
14 #include <rte_ethdev.h>
15 #include <rte_mbuf.h>
16 #include <rte_mempool.h>
17 #include <rte_string_fns.h>
18 #include "compat_netmap.h"
19
20
21 #define BUF_SIZE        RTE_MBUF_DEFAULT_DATAROOM
22 #define MBUF_DATA_SIZE  (BUF_SIZE + RTE_PKTMBUF_HEADROOM)
23
24 #define MBUF_PER_POOL   8192
25
26 struct rte_eth_conf eth_conf = {
27         .rxmode = {
28                 .split_hdr_size = 0,
29                 .header_split   = 0,
30                 .hw_ip_checksum = 0,
31                 .hw_vlan_filter = 0,
32                 .jumbo_frame    = 0,
33                 .hw_strip_crc   = 1,
34         },
35         .txmode = {
36                 .mq_mode = ETH_MQ_TX_NONE,
37         },
38 };
39
40 #define MAX_QUEUE_NUM   1
41 #define RX_QUEUE_NUM    1
42 #define TX_QUEUE_NUM    1
43
44 #define MAX_DESC_NUM    0x400
45 #define RX_DESC_NUM     0x100
46 #define TX_DESC_NUM     0x200
47
48 #define RX_SYNC_NUM     0x20
49 #define TX_SYNC_NUM     0x20
50
51 struct rte_netmap_port_conf port_conf = {
52         .eth_conf = &eth_conf,
53         .socket_id = SOCKET_ID_ANY,
54         .nr_tx_rings = TX_QUEUE_NUM,
55         .nr_rx_rings = RX_QUEUE_NUM,
56         .nr_tx_slots = TX_DESC_NUM,
57         .nr_rx_slots = RX_DESC_NUM,
58         .tx_burst = TX_SYNC_NUM,
59         .rx_burst = RX_SYNC_NUM,
60 };
61
62 struct rte_netmap_conf netmap_conf = {
63         .socket_id = SOCKET_ID_ANY,
64         .max_bufsz = BUF_SIZE,
65         .max_rings = MAX_QUEUE_NUM,
66         .max_slots = MAX_DESC_NUM,
67 };
68
69 static int stop = 0;
70
71 #define MAX_PORT_NUM    2
72
73 struct netmap_port {
74         int fd;
75         struct netmap_if *nmif;
76         struct netmap_ring *rx_ring;
77         struct netmap_ring *tx_ring;
78         const char *str;
79         uint8_t id;
80 };
81
82 static struct {
83         uint32_t num;
84         struct netmap_port p[MAX_PORT_NUM];
85         void *mem;
86 } ports;
87
88 static void
89 usage(const char *prgname)
90 {
91         fprintf(stderr, "Usage: %s [EAL args] -- [OPTION]...\n"
92                 "-h, --help   \t Show this help message and exit\n"
93                 "-i INTERFACE_A   \t Interface (DPDK port number) to use\n"
94                 "[ -i INTERFACE_B   \t Interface (DPDK port number) to use ]\n",
95                 prgname);
96 }
97
98 static uint8_t
99 parse_portid(const char *portid_str)
100 {
101         char *end;
102         unsigned id;
103
104         id = strtoul(portid_str, &end, 10);
105
106         if (end == portid_str || *end != '\0' || id > RTE_MAX_ETHPORTS)
107                 rte_exit(EXIT_FAILURE, "Invalid port number\n");
108
109         return (uint8_t) id;
110 }
111
112 static int
113 parse_args(int argc, char **argv)
114 {
115         int opt;
116
117         while ((opt = getopt(argc, argv, "hi:")) != -1) {
118                 switch (opt) {
119                 case 'h':
120                         usage(argv[0]);
121                         rte_exit(EXIT_SUCCESS, "exiting...");
122                         break;
123                 case 'i':
124                         if (ports.num >= RTE_DIM(ports.p)) {
125                                 usage(argv[0]);
126                                 rte_exit(EXIT_FAILURE, "configs with %u "
127                                         "ports are not supported\n",
128                                         ports.num + 1);
129
130                         }
131
132                         ports.p[ports.num].str = optarg;
133                         ports.p[ports.num].id = parse_portid(optarg);
134                         ports.num++;
135                         break;
136                 default:
137                         usage(argv[0]);
138                         rte_exit(EXIT_FAILURE, "invalid option: %c\n", opt);
139                 }
140         }
141
142         return 0;
143 }
144
145 static void sigint_handler(__rte_unused int sig)
146 {
147         stop = 1;
148         signal(SIGINT, SIG_DFL);
149 }
150
151 static void move(int n, struct netmap_ring *rx, struct netmap_ring *tx)
152 {
153         uint32_t tmp;
154
155         while (n-- > 0) {
156                 tmp = tx->slot[tx->cur].buf_idx;
157
158                 tx->slot[tx->cur].buf_idx = rx->slot[rx->cur].buf_idx;
159                 tx->slot[tx->cur].len     = rx->slot[rx->cur].len;
160                 tx->slot[tx->cur].flags  |= NS_BUF_CHANGED;
161                 tx->cur = NETMAP_RING_NEXT(tx, tx->cur);
162                 tx->avail--;
163
164                 rx->slot[rx->cur].buf_idx = tmp;
165                 rx->slot[rx->cur].flags  |= NS_BUF_CHANGED;
166                 rx->cur = NETMAP_RING_NEXT(rx, rx->cur);
167                 rx->avail--;
168         }
169 }
170
171 static int
172 netmap_port_open(uint32_t idx)
173 {
174         int err;
175         struct netmap_port *port;
176         struct nmreq req;
177
178         port = ports.p + idx;
179
180         port->fd = rte_netmap_open("/dev/netmap", O_RDWR);
181
182         snprintf(req.nr_name, sizeof(req.nr_name), "%s", port->str);
183         req.nr_version = NETMAP_API;
184         req.nr_ringid = 0;
185
186         err = rte_netmap_ioctl(port->fd, NIOCGINFO, &req);
187         if (err) {
188                 printf("[E] NIOCGINFO ioctl failed (error %d)\n", err);
189                 return err;
190         }
191
192         snprintf(req.nr_name, sizeof(req.nr_name), "%s", port->str);
193         req.nr_version = NETMAP_API;
194         req.nr_ringid = 0;
195
196         err = rte_netmap_ioctl(port->fd, NIOCREGIF, &req);
197         if (err) {
198                 printf("[E] NIOCREGIF ioctl failed (error %d)\n", err);
199                 return err;
200         }
201
202         /* mmap only once. */
203         if (ports.mem == NULL)
204                 ports.mem = rte_netmap_mmap(NULL, req.nr_memsize,
205                         PROT_WRITE | PROT_READ, MAP_PRIVATE, port->fd, 0);
206
207         if (ports.mem == MAP_FAILED) {
208                 printf("[E] NETMAP mmap failed for fd: %d)\n", port->fd);
209                 return -ENOMEM;
210         }
211
212         port->nmif = NETMAP_IF(ports.mem, req.nr_offset);
213
214         port->tx_ring = NETMAP_TXRING(port->nmif, 0);
215         port->rx_ring = NETMAP_RXRING(port->nmif, 0);
216
217         return 0;
218 }
219
220
221 int main(int argc, char *argv[])
222 {
223         int err, ret;
224         uint32_t i, pmsk;
225         struct nmreq req;
226         struct pollfd pollfd[MAX_PORT_NUM];
227         struct rte_mempool *pool;
228         struct netmap_ring *rx_ring, *tx_ring;
229
230         ret = rte_eal_init(argc, argv);
231         if (ret < 0)
232                 rte_exit(EXIT_FAILURE, "Cannot initialize EAL\n");
233
234         argc -= ret;
235         argv += ret;
236
237         parse_args(argc, argv);
238
239         if (ports.num == 0)
240                 rte_exit(EXIT_FAILURE, "no ports specified\n");
241
242         if (rte_eth_dev_count() < 1)
243                 rte_exit(EXIT_FAILURE, "Not enough ethernet ports available\n");
244
245         pool = rte_pktmbuf_pool_create("mbuf_pool", MBUF_PER_POOL, 32, 0,
246                 MBUF_DATA_SIZE, rte_socket_id());
247         if (pool == NULL)
248                 rte_exit(EXIT_FAILURE, "Couldn't create mempool\n");
249
250         netmap_conf.socket_id = rte_socket_id();
251         err = rte_netmap_init(&netmap_conf);
252
253         if (err < 0)
254                 rte_exit(EXIT_FAILURE,
255                         "Couldn't initialize librte_compat_netmap\n");
256         else
257                 printf("librte_compat_netmap initialized\n");
258
259         port_conf.pool = pool;
260         port_conf.socket_id = rte_socket_id();
261
262         for (i = 0; i != ports.num; i++) {
263
264                 err = rte_netmap_init_port(ports.p[i].id, &port_conf);
265                 if (err < 0)
266                         rte_exit(EXIT_FAILURE, "Couldn't setup port %hhu\n",
267                                 ports.p[i].id);
268
269                 rte_eth_promiscuous_enable(ports.p[i].id);
270         }
271
272         for (i = 0; i != ports.num; i++) {
273
274                 err = netmap_port_open(i);
275                 if (err) {
276                         rte_exit(EXIT_FAILURE, "Couldn't set port %hhu "
277                                 "under NETMAP control\n",
278                                 ports.p[i].id);
279                 }
280                 else
281                         printf("Port %hhu now in Netmap mode\n", ports.p[i].id);
282         }
283
284         memset(pollfd, 0, sizeof(pollfd));
285
286         for (i = 0; i != ports.num; i++) {
287                 pollfd[i].fd = ports.p[i].fd;
288                 pollfd[i].events = POLLIN | POLLOUT;
289         }
290
291         signal(SIGINT, sigint_handler);
292
293         pmsk = ports.num - 1;
294
295         printf("Bridge up and running!\n");
296
297         while (!stop) {
298                 uint32_t n_pkts;
299
300                 pollfd[0].revents = 0;
301                 pollfd[1].revents = 0;
302
303                 ret = rte_netmap_poll(pollfd, ports.num, 0);
304                 if (ret < 0) {
305                         stop = 1;
306                         printf("[E] poll returned with error %d\n", ret);
307                 }
308
309                 if (((pollfd[0].revents | pollfd[1].revents) & POLLERR) != 0) {
310                         printf("POLLERR!\n");
311                 }
312
313                 if ((pollfd[0].revents & POLLIN) != 0 &&
314                                 (pollfd[pmsk].revents & POLLOUT) != 0) {
315
316                         rx_ring = ports.p[0].rx_ring;
317                         tx_ring = ports.p[pmsk].tx_ring;
318
319                         n_pkts = RTE_MIN(rx_ring->avail, tx_ring->avail);
320                         move(n_pkts, rx_ring, tx_ring);
321                 }
322
323                 if (pmsk != 0 && (pollfd[pmsk].revents & POLLIN) != 0 &&
324                                 (pollfd[0].revents & POLLOUT) != 0) {
325
326                         rx_ring = ports.p[pmsk].rx_ring;
327                         tx_ring = ports.p[0].tx_ring;
328
329                         n_pkts = RTE_MIN(rx_ring->avail, tx_ring->avail);
330                         move(n_pkts, rx_ring, tx_ring);
331                 }
332         }
333
334         printf("Bridge stopped!\n");
335
336         for (i = 0; i != ports.num; i++) {
337                 err = rte_netmap_ioctl(ports.p[i].fd, NIOCUNREGIF, &req);
338                 if (err) {
339                         printf("[E] NIOCUNREGIF ioctl failed (error %d)\n",
340                                 err);
341                 }
342                 else
343                         printf("Port %hhu unregistered from Netmap mode\n", ports.p[i].id);
344
345                 rte_netmap_close(ports.p[i].fd);
346         }
347         return 0;
348 }