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