2 * (C) 2011-2014 Luigi Rizzo, Matteo Landi
6 * A netmap application to bridge two network interfaces,
7 * or one interface and the host stack.
12 #include <libnetmap.h>
16 #include <sys/ioctl.h>
24 static int verbose = 0;
26 static int do_abort = 0;
27 static int zerocopy = 1; /* enable zerocopy if possible */
32 (void)sig; /* UNUSED */
34 signal(SIGINT, SIG_DFL);
39 * How many slots do we (user application) have on this
43 rx_slots_avail(struct nmport_d *d)
47 for (i = d->first_rx_ring; i <= d->last_rx_ring; i++) {
48 tot += nm_ring_space(NETMAP_RXRING(d->nifp, i));
55 tx_slots_avail(struct nmport_d *d)
59 for (i = d->first_tx_ring; i <= d->last_tx_ring; i++) {
60 tot += nm_ring_space(NETMAP_TXRING(d->nifp, i));
67 * Move up to 'limit' pkts from rxring to txring, swapping buffers
68 * if zerocopy is possible. Otherwise fall back on packet copying.
71 rings_move(struct netmap_ring *rxring, struct netmap_ring *txring,
72 u_int limit, const char *msg)
76 /* print a warning if any of the ring flags is set (e.g. NM_REINIT) */
77 if (rxring->flags || txring->flags)
78 D("%s rxflags %x txflags %x",
79 msg, rxring->flags, txring->flags);
80 j = rxring->head; /* RX */
81 k = txring->head; /* TX */
82 m = nm_ring_space(rxring);
85 m = nm_ring_space(txring);
90 struct netmap_slot *rs = &rxring->slot[j];
91 struct netmap_slot *ts = &txring->slot[k];
93 if (ts->buf_idx < 2 || rs->buf_idx < 2) {
94 RD(2, "wrong index rxr[%d] = %d -> txr[%d] = %d",
95 j, rs->buf_idx, k, ts->buf_idx);
98 /* Copy the packet length. */
99 if (rs->len > rxring->nr_buf_size) {
100 RD(2, "%s: invalid len %u, rxr[%d] -> txr[%d]",
103 } else if (verbose > 1) {
104 D("%s: fwd len %u, rx[%d] -> tx[%d]",
109 uint32_t pkt = ts->buf_idx;
110 ts->buf_idx = rs->buf_idx;
112 /* report the buffer change. */
113 ts->flags |= NS_BUF_CHANGED;
114 rs->flags |= NS_BUF_CHANGED;
116 char *rxbuf = NETMAP_BUF(rxring, rs->buf_idx);
117 char *txbuf = NETMAP_BUF(txring, ts->buf_idx);
118 nm_pkt_copy(rxbuf, txbuf, ts->len);
121 * Copy the NS_MOREFRAG from rs to ts, leaving any
122 * other flags unchanged.
124 ts->flags = (ts->flags & ~NS_MOREFRAG) | (rs->flags & NS_MOREFRAG);
125 j = nm_ring_next(rxring, j);
126 k = nm_ring_next(txring, k);
128 rxring->head = rxring->cur = j;
129 txring->head = txring->cur = k;
130 if (verbose && m > 0)
131 D("%s fwd %d packets: rxring %u --> txring %u",
132 msg, m, rxring->ringid, txring->ringid);
137 /* Move packets from source port to destination port. */
139 ports_move(struct nmport_d *src, struct nmport_d *dst, u_int limit,
142 struct netmap_ring *txring, *rxring;
143 u_int m = 0, si = src->first_rx_ring, di = dst->first_tx_ring;
145 while (si <= src->last_rx_ring && di <= dst->last_tx_ring) {
146 rxring = NETMAP_RXRING(src->nifp, si);
147 txring = NETMAP_TXRING(dst->nifp, di);
148 if (nm_ring_empty(rxring)) {
152 if (nm_ring_empty(txring)) {
156 m += rings_move(rxring, txring, limit, msg);
167 "netmap bridge program: forward packets between two "
169 " usage(1): bridge [-v] [-i ifa] [-i ifb] [-b burst] "
170 "[-w wait_time] [-L]\n"
171 " usage(2): bridge [-v] [-w wait_time] [-L] "
172 "[ifa [ifb [burst]]]\n"
174 " ifa and ifb are specified using the nm_open() syntax.\n"
175 " When ifb is missing (or is equal to ifa), bridge will\n"
176 " forward between between ifa and the host stack if -L\n"
177 " is not specified, otherwise loopback traffic on ifa.\n"
179 " example: bridge -w 10 -i netmap:eth3 -i netmap:eth1\n"
181 " If ifa and ifb are two interfaces, they must be in\n"
182 " promiscuous mode. Otherwise, if bridging with the \n"
183 " host stack, the interface must have the offloads \n"
190 * bridge [-v] if1 [if2]
192 * If only one name, or the two interfaces are the same,
193 * bridges userland and the adapter. Otherwise bridge
197 main(int argc, char **argv)
199 char msg_a2b[256], msg_b2a[256];
200 struct pollfd pollfd[2];
201 u_int burst = 1024, wait_link = 4;
202 struct nmport_d *pa = NULL, *pb = NULL;
203 char *ifa = NULL, *ifb = NULL;
204 char ifabuf[64] = { 0 };
205 int pa_sw_rings, pb_sw_rings;
209 while ((ch = getopt(argc, argv, "hb:ci:vw:L")) != -1) {
212 D("bad option %c %s", ch, optarg);
217 case 'b': /* burst */
218 burst = atoi(optarg);
220 case 'i': /* interface */
223 else if (ifb == NULL)
226 D("%s ignored, already have 2 interfaces",
230 zerocopy = 0; /* do not zerocopy */
236 wait_link = atoi(optarg);
253 burst = atoi(argv[2]);
257 D("missing interface");
260 if (burst < 1 || burst > 8192) {
261 D("invalid burst %d, set to 1024", burst);
264 if (wait_link > 100) {
265 D("invalid wait_link %d, set to 4", wait_link);
268 if (!strcmp(ifa, ifb)) {
270 D("same interface, endpoint 0 goes to host");
271 snprintf(ifabuf, sizeof(ifabuf) - 1, "%s^", ifa);
274 D("same interface, loopbacking traffic");
277 /* two different interfaces. Take all rings on if1 */
279 pa = nmport_open(ifa);
281 D("cannot open %s", ifa);
284 /* try to reuse the mmap() of the first interface, if possible */
285 pb = nmport_open(ifb);
287 D("cannot open %s", ifb);
291 zerocopy = zerocopy && (pa->mem == pb->mem);
292 D("------- zerocopy %ssupported", zerocopy ? "" : "NOT ");
294 /* setup poll(2) array */
295 memset(pollfd, 0, sizeof(pollfd));
296 pollfd[0].fd = pa->fd;
297 pollfd[1].fd = pb->fd;
299 D("Wait %d secs for link to come up...", wait_link);
301 D("Ready to go, %s 0x%x/%d <-> %s 0x%x/%d.",
302 pa->hdr.nr_name, pa->first_rx_ring, pa->reg.nr_rx_rings,
303 pb->hdr.nr_name, pb->first_rx_ring, pb->reg.nr_rx_rings);
305 pa_sw_rings = (pa->reg.nr_mode == NR_REG_SW ||
306 pa->reg.nr_mode == NR_REG_ONE_SW);
307 pb_sw_rings = (pb->reg.nr_mode == NR_REG_SW ||
308 pb->reg.nr_mode == NR_REG_ONE_SW);
310 snprintf(msg_a2b, sizeof(msg_a2b), "%s:%s --> %s:%s",
311 pa->hdr.nr_name, pa_sw_rings ? "host" : "nic",
312 pb->hdr.nr_name, pb_sw_rings ? "host" : "nic");
314 snprintf(msg_b2a, sizeof(msg_b2a), "%s:%s --> %s:%s",
315 pb->hdr.nr_name, pb_sw_rings ? "host" : "nic",
316 pa->hdr.nr_name, pa_sw_rings ? "host" : "nic");
319 signal(SIGINT, sigint_h);
322 pollfd[0].events = pollfd[1].events = 0;
323 pollfd[0].revents = pollfd[1].revents = 0;
324 n0 = rx_slots_avail(pa);
325 n1 = rx_slots_avail(pb);
328 pollfd[1].revents = POLLOUT;
330 ioctl(pollfd[0].fd, NIOCRXSYNC, NULL);
333 pollfd[0].revents = POLLOUT;
335 ioctl(pollfd[1].fd, NIOCRXSYNC, NULL);
338 #else /* !defined(BUSYWAIT) */
340 pollfd[1].events |= POLLOUT;
342 pollfd[0].events |= POLLIN;
344 pollfd[0].events |= POLLOUT;
346 pollfd[1].events |= POLLIN;
348 /* poll() also cause kernel to txsync/rxsync the NICs */
349 ret = poll(pollfd, 2, 2500);
350 #endif /* !defined(BUSYWAIT) */
351 if (ret <= 0 || verbose)
352 D("poll %s [0] ev %x %x rx %d@%d tx %d,"
353 " [1] ev %x %x rx %d@%d tx %d",
354 ret <= 0 ? "timeout" : "ok",
358 NETMAP_RXRING(pa->nifp, pa->cur_rx_ring)->head,
363 NETMAP_RXRING(pb->nifp, pb->cur_rx_ring)->head,
368 if (pollfd[0].revents & POLLERR) {
369 struct netmap_ring *rx = NETMAP_RXRING(pa->nifp, pa->cur_rx_ring);
370 D("error on fd0, rx [%d,%d,%d)",
371 rx->head, rx->cur, rx->tail);
373 if (pollfd[1].revents & POLLERR) {
374 struct netmap_ring *rx = NETMAP_RXRING(pb->nifp, pb->cur_rx_ring);
375 D("error on fd1, rx [%d,%d,%d)",
376 rx->head, rx->cur, rx->tail);
378 if (pollfd[0].revents & POLLOUT) {
379 ports_move(pb, pa, burst, msg_b2a);
381 ioctl(pollfd[0].fd, NIOCTXSYNC, NULL);
385 if (pollfd[1].revents & POLLOUT) {
386 ports_move(pa, pb, burst, msg_a2b);
388 ioctl(pollfd[1].fd, NIOCTXSYNC, NULL);
393 * We don't need ioctl(NIOCTXSYNC) on the two file descriptors.
394 * here. The kernel will txsync on next poll().