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