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