remove trailing whitespaces
[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 struct rte_eth_txconf tx_conf = {
71         .tx_thresh = {
72             .pthresh = 36,
73             .hthresh = 0,
74             .wthresh = 0,
75         },
76         .tx_free_thresh = 0,
77         .tx_rs_thresh = 0,
78         .txq_flags = (ETH_TXQ_FLAGS_NOMULTSEGS |
79                         ETH_TXQ_FLAGS_NOVLANOFFL |
80                         ETH_TXQ_FLAGS_NOXSUMSCTP |
81                         ETH_TXQ_FLAGS_NOXSUMUDP |
82                         ETH_TXQ_FLAGS_NOXSUMTCP)
83 };
84
85 struct rte_eth_rxconf rx_conf = {
86         .rx_thresh = {
87             .pthresh = 8,
88             .hthresh = 8,
89             .wthresh = 4,
90         },
91 };
92
93 #define MAX_QUEUE_NUM   1
94 #define RX_QUEUE_NUM    1
95 #define TX_QUEUE_NUM    1
96
97 #define MAX_DESC_NUM    0x400
98 #define RX_DESC_NUM     0x100
99 #define TX_DESC_NUM     0x200
100
101 #define RX_SYNC_NUM     0x20
102 #define TX_SYNC_NUM     0x20
103
104 struct rte_netmap_port_conf port_conf = {
105         .eth_conf = &eth_conf,
106         .tx_conf  = &tx_conf,
107         .rx_conf  = &rx_conf,
108         .socket_id = SOCKET_ID_ANY,
109         .nr_tx_rings = TX_QUEUE_NUM,
110         .nr_rx_rings = RX_QUEUE_NUM,
111         .nr_tx_slots = TX_DESC_NUM,
112         .nr_rx_slots = RX_DESC_NUM,
113         .tx_burst = TX_SYNC_NUM,
114         .rx_burst = RX_SYNC_NUM,
115 };
116
117 struct rte_netmap_conf netmap_conf = {
118         .socket_id = SOCKET_ID_ANY,
119         .max_bufsz = BUF_SIZE,
120         .max_rings = MAX_QUEUE_NUM,
121         .max_slots = MAX_DESC_NUM,
122 };
123
124 static int stop = 0;
125
126 #define MAX_PORT_NUM    2
127
128 struct netmap_port {
129         int fd;
130         struct netmap_if *nmif;
131         struct netmap_ring *rx_ring;
132         struct netmap_ring *tx_ring;
133         const char *str;
134         uint8_t id;
135 };
136
137 static struct {
138         uint32_t num;
139         struct netmap_port p[MAX_PORT_NUM];
140         void *mem;
141 } ports;
142
143 static void
144 usage(const char *prgname)
145 {
146         fprintf(stderr, "Usage: %s [EAL args] -- [OPTION]...\n"
147                 "-h, --help   \t Show this help message and exit\n"
148                 "-i INTERFACE_A   \t Interface (DPDK port number) to use\n"
149                 "[ -i INTERFACE_B   \t Interface (DPDK port number) to use ]\n",
150                 prgname);
151 }
152
153 static uint8_t
154 parse_portid(const char *portid_str)
155 {
156         char *end;
157         unsigned id;
158
159         id = strtoul(portid_str, &end, 10);
160
161         if (end == portid_str || *end != '\0' || id > RTE_MAX_ETHPORTS)
162                 rte_exit(EXIT_FAILURE, "Invalid port number\n");
163
164         return (uint8_t) id;
165 }
166
167 static int
168 parse_args(int argc, char **argv)
169 {
170         int opt;
171
172         while ((opt = getopt(argc, argv, "hi:")) != -1) {
173                 switch (opt) {
174                 case 'h':
175                         usage(argv[0]);
176                         rte_exit(EXIT_SUCCESS, "exiting...");
177                         break;
178                 case 'i':
179                         if (ports.num >= RTE_DIM(ports.p)) {
180                                 usage(argv[0]);
181                                 rte_exit(EXIT_FAILURE, "configs with %u "
182                                         "ports are not supported\n",
183                                         ports.num + 1);
184
185                         }
186
187                         ports.p[ports.num].str = optarg;
188                         ports.p[ports.num].id = parse_portid(optarg);
189                         ports.num++;
190                         break;
191                 default:
192                         usage(argv[0]);
193                         rte_exit(EXIT_FAILURE, "invalid option: %c\n", opt);
194                 }
195         }
196
197         return 0;
198 }
199
200 static void sigint_handler(__rte_unused int sig)
201 {
202         stop = 1;
203         signal(SIGINT, SIG_DFL);
204 }
205
206 static void move(int n, struct netmap_ring *rx, struct netmap_ring *tx)
207 {
208         uint32_t tmp;
209
210         while (n-- > 0) {
211                 tmp = tx->slot[tx->cur].buf_idx;
212
213                 tx->slot[tx->cur].buf_idx = rx->slot[rx->cur].buf_idx;
214                 tx->slot[tx->cur].len     = rx->slot[rx->cur].len;
215                 tx->slot[tx->cur].flags  |= NS_BUF_CHANGED;
216                 tx->cur = NETMAP_RING_NEXT(tx, tx->cur);
217                 tx->avail--;
218
219                 rx->slot[rx->cur].buf_idx = tmp;
220                 rx->slot[rx->cur].flags  |= NS_BUF_CHANGED;
221                 rx->cur = NETMAP_RING_NEXT(rx, rx->cur);
222                 rx->avail--;
223         }
224 }
225
226 static int
227 netmap_port_open(uint32_t idx)
228 {
229         int err;
230         struct netmap_port *port;
231         struct nmreq req;
232
233         port = ports.p + idx;
234
235         port->fd = rte_netmap_open("/dev/netmap", O_RDWR);
236
237         rte_snprintf(req.nr_name, sizeof(req.nr_name), "%s", port->str);
238         req.nr_version = NETMAP_API;
239         req.nr_ringid = 0;
240
241         err = rte_netmap_ioctl(port->fd, NIOCGINFO, &req);
242         if (err) {
243                 printf("[E] NIOCGINFO ioctl failed (error %d)\n", err);
244                 return (err);
245         }
246
247         rte_snprintf(req.nr_name, sizeof(req.nr_name), "%s", port->str);
248         req.nr_version = NETMAP_API;
249         req.nr_ringid = 0;
250
251         err = rte_netmap_ioctl(port->fd, NIOCREGIF, &req);
252         if (err) {
253                 printf("[E] NIOCREGIF ioctl failed (error %d)\n", err);
254                 return (err);
255         }
256
257         /* mmap only once. */
258         if (ports.mem == NULL)
259                 ports.mem = rte_netmap_mmap(NULL, req.nr_memsize,
260                         PROT_WRITE | PROT_READ, MAP_PRIVATE, port->fd, 0);
261
262         if (ports.mem == MAP_FAILED) {
263                 printf("[E] NETMAP mmap failed for fd: %d)\n", port->fd);
264                 return (-ENOMEM);
265         }
266
267         port->nmif = NETMAP_IF(ports.mem, req.nr_offset);
268
269         port->tx_ring = NETMAP_TXRING(port->nmif, 0);
270         port->rx_ring = NETMAP_RXRING(port->nmif, 0);
271
272         return (0);
273 }
274
275
276 int main(int argc, char *argv[])
277 {
278         int err, ret;
279         uint32_t i, pmsk;
280         struct nmreq req;
281         struct pollfd pollfd[MAX_PORT_NUM];
282         struct rte_mempool *pool;
283         struct netmap_ring *rx_ring, *tx_ring;
284
285         ret = rte_eal_init(argc, argv);
286         if (ret < 0)
287                 rte_exit(EXIT_FAILURE, "Cannot initialize EAL\n");
288
289         argc -= ret;
290         argv += ret;
291
292         parse_args(argc, argv);
293
294         if (ports.num == 0)
295                 rte_exit(EXIT_FAILURE, "no ports specified\n");
296
297         err = rte_eal_pci_probe();
298         if (err < 0)
299                 rte_exit(EXIT_FAILURE, "rte_eal_pci_probe(): error %d\n", err);
300
301         if (rte_eth_dev_count() < 1)
302                 rte_exit(EXIT_FAILURE, "Not enough ethernet ports available\n");
303
304         pool = rte_mempool_create("mbuf_pool", MBUF_PER_POOL, MBUF_SIZE, 32,
305                                 sizeof(struct rte_pktmbuf_pool_private),
306                                 rte_pktmbuf_pool_init, NULL,
307                                 rte_pktmbuf_init, NULL,
308                                 rte_socket_id(), 0);
309         if (pool == NULL)
310                 rte_exit(EXIT_FAILURE, "Couldn't create mempool\n");
311
312         netmap_conf.socket_id = rte_socket_id();
313         err = rte_netmap_init(&netmap_conf);
314
315         if (err < 0)
316                 rte_exit(EXIT_FAILURE,
317                         "Couldn't initialize librte_compat_netmap\n");
318         else
319                 printf("librte_compat_netmap initialized\n");
320
321         port_conf.pool = pool;
322         port_conf.socket_id = rte_socket_id();
323
324         for (i = 0; i != ports.num; i++) {
325
326                 err = rte_netmap_init_port(ports.p[i].id, &port_conf);
327                 if (err < 0)
328                         rte_exit(EXIT_FAILURE, "Couldn't setup port %hhu\n",
329                                 ports.p[i].id);
330
331                 rte_eth_promiscuous_enable(ports.p[i].id);
332         }
333
334         for (i = 0; i != ports.num; i++) {
335
336                 err = netmap_port_open(i);
337                 if (err) {
338                         rte_exit(EXIT_FAILURE, "Couldn't set port %hhu "
339                                 "under NETMAP control\n",
340                                 ports.p[i].id);
341                 }
342                 else
343                         printf("Port %hhu now in Netmap mode\n", ports.p[i].id);
344         }
345
346         memset(pollfd, 0, sizeof(pollfd));
347
348         for (i = 0; i != ports.num; i++) {
349                 pollfd[i].fd = ports.p[i].fd;
350                 pollfd[i].events = POLLIN | POLLOUT;
351         }
352
353         signal(SIGINT, sigint_handler);
354
355         pmsk = ports.num - 1;
356
357         printf("Bridge up and running!\n");
358
359         while (!stop) {
360                 uint32_t n_pkts;
361
362                 pollfd[0].revents = 0;
363                 pollfd[1].revents = 0;
364
365                 ret = rte_netmap_poll(pollfd, ports.num, 0);
366                 if (ret < 0) {
367                         stop = 1;
368                         printf("[E] poll returned with error %d\n", ret);
369                 }
370
371                 if (((pollfd[0].revents | pollfd[1].revents) & POLLERR) != 0) {
372                         printf("POLLERR!\n");
373                 }
374
375                 if ((pollfd[0].revents & POLLIN) != 0 &&
376                                 (pollfd[pmsk].revents & POLLOUT) != 0) {
377
378                         rx_ring = ports.p[0].rx_ring;
379                         tx_ring = ports.p[pmsk].tx_ring;
380
381                         n_pkts = RTE_MIN(rx_ring->avail, tx_ring->avail);
382                         move(n_pkts, rx_ring, tx_ring);
383                 }
384
385                 if (pmsk != 0 && (pollfd[pmsk].revents & POLLIN) != 0 &&
386                                 (pollfd[0].revents & POLLOUT) != 0) {
387
388                         rx_ring = ports.p[pmsk].rx_ring;
389                         tx_ring = ports.p[0].tx_ring;
390
391                         n_pkts = RTE_MIN(rx_ring->avail, tx_ring->avail);
392                         move(n_pkts, rx_ring, tx_ring);
393                 }
394         }
395
396         printf("Bridge stopped!\n");
397
398         for (i = 0; i != ports.num; i++) {
399                 err = rte_netmap_ioctl(ports.p[i].fd, NIOCUNREGIF, &req);
400                 if (err) {
401                         printf("[E] NIOCUNREGIF ioctl failed (error %d)\n",
402                                 err);
403                 }
404                 else
405                         printf("Port %hhu unregistered from Netmap mode\n", ports.p[i].id);
406
407                 rte_netmap_close(ports.p[i].fd);
408         }
409         return 0;
410 }