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