2 * (C) 2011-2014 Luigi Rizzo, Matteo Landi
6 * A netmap client to bridge two network interfaces
7 * (or one interface and the host stack).
13 #define NETMAP_WITH_LIBS
14 #include <net/netmap_user.h>
19 static int do_abort = 0;
20 static int zerocopy = 1; /* enable zerocopy if possible */
25 (void)sig; /* UNUSED */
27 signal(SIGINT, SIG_DFL);
32 * how many packets on this set of queues ?
35 pkt_queued(struct nm_desc *d, int tx)
40 for (i = d->first_tx_ring; i <= d->last_tx_ring; i++) {
41 tot += nm_ring_space(NETMAP_TXRING(d->nifp, i));
44 for (i = d->first_rx_ring; i <= d->last_rx_ring; i++) {
45 tot += nm_ring_space(NETMAP_RXRING(d->nifp, i));
52 * move up to 'limit' pkts from rxring to txring swapping buffers.
55 process_rings(struct netmap_ring *rxring, struct netmap_ring *txring,
56 u_int limit, const char *msg)
60 /* print a warning if any of the ring flags is set (e.g. NM_REINIT) */
61 if (rxring->flags || txring->flags)
62 D("%s rxflags %x txflags %x",
63 msg, rxring->flags, txring->flags);
64 j = rxring->cur; /* RX */
65 k = txring->cur; /* TX */
66 m = nm_ring_space(rxring);
69 m = nm_ring_space(txring);
74 struct netmap_slot *rs = &rxring->slot[j];
75 struct netmap_slot *ts = &txring->slot[k];
78 if (ts->buf_idx < 2 || rs->buf_idx < 2) {
79 D("wrong index rx[%d] = %d -> tx[%d] = %d",
80 j, rs->buf_idx, k, ts->buf_idx);
83 /* copy the packet length. */
85 D("wrong len %d rx[%d] -> tx[%d]", rs->len, j, k);
87 } else if (verbose > 1) {
88 D("%s send len %d rx[%d] -> tx[%d]", msg, rs->len, j, k);
92 uint32_t pkt = ts->buf_idx;
93 ts->buf_idx = rs->buf_idx;
95 /* report the buffer change. */
96 ts->flags |= NS_BUF_CHANGED;
97 rs->flags |= NS_BUF_CHANGED;
99 char *rxbuf = NETMAP_BUF(rxring, rs->buf_idx);
100 char *txbuf = NETMAP_BUF(txring, ts->buf_idx);
101 nm_pkt_copy(rxbuf, txbuf, ts->len);
103 j = nm_ring_next(rxring, j);
104 k = nm_ring_next(txring, k);
106 rxring->head = rxring->cur = j;
107 txring->head = txring->cur = k;
108 if (verbose && m > 0)
109 D("%s sent %d packets to %p", msg, m, txring);
114 /* move packts from src to destination */
116 move(struct nm_desc *src, struct nm_desc *dst, u_int limit)
118 struct netmap_ring *txring, *rxring;
119 u_int m = 0, si = src->first_rx_ring, di = dst->first_tx_ring;
120 const char *msg = (src->req.nr_ringid & NETMAP_SW_RING) ?
121 "host->net" : "net->host";
123 while (si <= src->last_rx_ring && di <= dst->last_tx_ring) {
124 rxring = NETMAP_RXRING(src->nifp, si);
125 txring = NETMAP_TXRING(dst->nifp, di);
126 ND("txring %p rxring %p", txring, rxring);
127 if (nm_ring_empty(rxring)) {
131 if (nm_ring_empty(txring)) {
135 m += process_rings(rxring, txring, limit, msg);
146 "usage: bridge [-v] [-i ifa] [-i ifb] [-b burst] [-w wait_time] [ifa [ifb [burst]]]\n");
151 * bridge [-v] if1 [if2]
153 * If only one name, or the two interfaces are the same,
154 * bridges userland and the adapter. Otherwise bridge
158 main(int argc, char **argv)
160 struct pollfd pollfd[2];
162 u_int burst = 1024, wait_link = 4;
163 struct nm_desc *pa = NULL, *pb = NULL;
164 char *ifa = NULL, *ifb = NULL;
165 char ifabuf[64] = { 0 };
167 fprintf(stderr, "%s built %s %s\n",
168 argv[0], __DATE__, __TIME__);
170 while ( (ch = getopt(argc, argv, "b:ci:vw:")) != -1) {
173 D("bad option %c %s", ch, optarg);
176 case 'b': /* burst */
177 burst = atoi(optarg);
179 case 'i': /* interface */
182 else if (ifb == NULL)
185 D("%s ignored, already have 2 interfaces",
189 zerocopy = 0; /* do not zerocopy */
195 wait_link = atoi(optarg);
209 burst = atoi(argv[2]);
213 D("missing interface");
216 if (burst < 1 || burst > 8192) {
217 D("invalid burst %d, set to 1024", burst);
220 if (wait_link > 100) {
221 D("invalid wait_link %d, set to 4", wait_link);
224 if (!strcmp(ifa, ifb)) {
225 D("same interface, endpoint 0 goes to host");
226 snprintf(ifabuf, sizeof(ifabuf) - 1, "%s^", ifa);
229 /* two different interfaces. Take all rings on if1 */
231 pa = nm_open(ifa, NULL, 0, NULL);
233 D("cannot open %s", ifa);
236 /* try to reuse the mmap() of the first interface, if possible */
237 pb = nm_open(ifb, NULL, NM_OPEN_NO_MMAP, pa);
239 D("cannot open %s", ifb);
243 zerocopy = zerocopy && (pa->mem == pb->mem);
244 D("------- zerocopy %ssupported", zerocopy ? "" : "NOT ");
246 /* setup poll(2) variables. */
247 memset(pollfd, 0, sizeof(pollfd));
248 pollfd[0].fd = pa->fd;
249 pollfd[1].fd = pb->fd;
251 D("Wait %d secs for link to come up...", wait_link);
253 D("Ready to go, %s 0x%x/%d <-> %s 0x%x/%d.",
254 pa->req.nr_name, pa->first_rx_ring, pa->req.nr_rx_rings,
255 pb->req.nr_name, pb->first_rx_ring, pb->req.nr_rx_rings);
258 signal(SIGINT, sigint_h);
261 pollfd[0].events = pollfd[1].events = 0;
262 pollfd[0].revents = pollfd[1].revents = 0;
263 n0 = pkt_queued(pa, 0);
264 n1 = pkt_queued(pb, 0);
265 #if defined(_WIN32) || defined(BUSYWAIT)
267 ioctl(pollfd[1].fd, NIOCTXSYNC, NULL);
268 pollfd[1].revents = POLLOUT;
271 ioctl(pollfd[0].fd, NIOCRXSYNC, NULL);
274 ioctl(pollfd[0].fd, NIOCTXSYNC, NULL);
275 pollfd[0].revents = POLLOUT;
278 ioctl(pollfd[1].fd, NIOCRXSYNC, NULL);
283 pollfd[1].events |= POLLOUT;
285 pollfd[0].events |= POLLIN;
287 pollfd[0].events |= POLLOUT;
289 pollfd[1].events |= POLLIN;
290 ret = poll(pollfd, 2, 2500);
291 #endif //defined(_WIN32) || defined(BUSYWAIT)
292 if (ret <= 0 || verbose)
293 D("poll %s [0] ev %x %x rx %d@%d tx %d,"
294 " [1] ev %x %x rx %d@%d tx %d",
295 ret <= 0 ? "timeout" : "ok",
299 NETMAP_RXRING(pa->nifp, pa->cur_rx_ring)->cur,
304 NETMAP_RXRING(pb->nifp, pb->cur_rx_ring)->cur,
309 if (pollfd[0].revents & POLLERR) {
310 struct netmap_ring *rx = NETMAP_RXRING(pa->nifp, pa->cur_rx_ring);
311 D("error on fd0, rx [%d,%d,%d)",
312 rx->head, rx->cur, rx->tail);
314 if (pollfd[1].revents & POLLERR) {
315 struct netmap_ring *rx = NETMAP_RXRING(pb->nifp, pb->cur_rx_ring);
316 D("error on fd1, rx [%d,%d,%d)",
317 rx->head, rx->cur, rx->tail);
319 if (pollfd[0].revents & POLLOUT) {
321 // XXX we don't need the ioctl */
322 // ioctl(me[0].fd, NIOCTXSYNC, NULL);
324 if (pollfd[1].revents & POLLOUT) {
326 // XXX we don't need the ioctl */
327 // ioctl(me[1].fd, NIOCTXSYNC, NULL);