2 * (C) 2011 Luigi Rizzo, Matteo Landi
6 * A netmap client to bridge two network interfaces
7 * (or one interface and the host stack).
17 char *version = "$Id: bridge.c 12016 2013-01-23 17:24:22Z luigi $";
19 static int do_abort = 0;
24 (void)sig; /* UNUSED */
26 signal(SIGINT, SIG_DFL);
31 * move up to 'limit' pkts from rxring to txring swapping buffers.
34 process_rings(struct netmap_ring *rxring, struct netmap_ring *txring,
35 u_int limit, const char *msg)
39 /* print a warning if any of the ring flags is set (e.g. NM_REINIT) */
40 if (rxring->flags || txring->flags)
41 D("%s rxflags %x txflags %x",
42 msg, rxring->flags, txring->flags);
43 j = rxring->cur; /* RX */
44 k = txring->cur; /* TX */
45 if (rxring->avail < limit)
46 limit = rxring->avail;
47 if (txring->avail < limit)
48 limit = txring->avail;
51 struct netmap_slot *rs = &rxring->slot[j];
52 struct netmap_slot *ts = &txring->slot[k];
56 if (ts->buf_idx < 2 || rs->buf_idx < 2) {
57 D("wrong index rx[%d] = %d -> tx[%d] = %d",
58 j, rs->buf_idx, k, ts->buf_idx);
62 ts->buf_idx = rs->buf_idx;
65 /* copy the packet length. */
66 if (rs->len < 14 || rs->len > 2048)
67 D("wrong len %d rx[%d] -> tx[%d]", rs->len, j, k);
69 D("%s send len %d rx[%d] -> tx[%d]", msg, rs->len, j, k);
72 /* report the buffer change. */
73 ts->flags |= NS_BUF_CHANGED;
74 rs->flags |= NS_BUF_CHANGED;
75 j = NETMAP_RING_NEXT(rxring, j);
76 k = NETMAP_RING_NEXT(txring, k);
83 D("%s sent %d packets to %p", msg, m, txring);
88 /* move packts from src to destination */
90 move(struct my_ring *src, struct my_ring *dst, u_int limit)
92 struct netmap_ring *txring, *rxring;
93 u_int m = 0, si = src->begin, di = dst->begin;
94 const char *msg = (src->queueid & NETMAP_SW_RING) ?
95 "host->net" : "net->host";
97 while (si < src->end && di < dst->end) {
98 rxring = NETMAP_RXRING(src->nifp, si);
99 txring = NETMAP_TXRING(dst->nifp, di);
100 ND("txring %p rxring %p", txring, rxring);
101 if (rxring->avail == 0) {
105 if (txring->avail == 0) {
109 m += process_rings(rxring, txring, limit, msg);
116 * how many packets on this set of queues ?
119 pkt_queued(struct my_ring *me, int tx)
123 ND("me %p begin %d end %d", me, me->begin, me->end);
124 for (i = me->begin; i < me->end; i++) {
125 struct netmap_ring *ring = tx ?
126 NETMAP_TXRING(me->nifp, i) : NETMAP_RXRING(me->nifp, i);
129 if (0 && verbose && tot && !tx)
130 D("ring %s %s %s has %d avail at %d",
131 me->ifname, tx ? "tx": "rx",
132 me->end >= me->nifp->ni_tx_rings ? // XXX who comes first ?
134 tot, NETMAP_TXRING(me->nifp, me->begin)->cur);
142 "usage: bridge [-v] [-i ifa] [-i ifb] [-b burst] [-w wait_time] [iface]\n");
147 * bridge [-v] if1 [if2]
149 * If only one name, or the two interfaces are the same,
150 * bridges userland and the adapter. Otherwise bridge
154 main(int argc, char **argv)
156 struct pollfd pollfd[2];
158 u_int burst = 1024, wait_link = 4;
159 struct my_ring me[2];
160 char *ifa = NULL, *ifb = NULL;
162 fprintf(stderr, "%s %s built %s %s\n",
163 argv[0], version, __DATE__, __TIME__);
165 bzero(me, sizeof(me));
167 while ( (ch = getopt(argc, argv, "b:i:vw:")) != -1) {
170 D("bad option %c %s", ch, optarg);
173 case 'b': /* burst */
174 burst = atoi(optarg);
176 case 'i': /* interface */
179 else if (ifb == NULL)
182 D("%s ignored, already have 2 interfaces",
189 wait_link = atoi(optarg);
203 burst = atoi(argv[3]);
207 D("missing interface");
210 if (burst < 1 || burst > 8192) {
211 D("invalid burst %d, set to 1024", burst);
214 if (wait_link > 100) {
215 D("invalid wait_link %d, set to 4", wait_link);
218 /* setup netmap interface #1. */
221 if (!strcmp(ifa, ifb)) {
222 D("same interface, endpoint 0 goes to host");
225 /* two different interfaces. Take all rings on if1 */
226 i = 0; // all hw rings
228 if (netmap_open(me, i, 1))
230 me[1].mem = me[0].mem; /* copy the pointer, so only one mmap */
231 if (netmap_open(me+1, 0, 1))
234 /* setup poll(2) variables. */
235 memset(pollfd, 0, sizeof(pollfd));
236 for (i = 0; i < 2; i++) {
237 pollfd[i].fd = me[i].fd;
238 pollfd[i].events = (POLLIN);
241 D("Wait %d secs for link to come up...", wait_link);
243 D("Ready to go, %s 0x%x/%d <-> %s 0x%x/%d.",
244 me[0].ifname, me[0].queueid, me[0].nifp->ni_rx_rings,
245 me[1].ifname, me[1].queueid, me[1].nifp->ni_rx_rings);
248 signal(SIGINT, sigint_h);
251 pollfd[0].events = pollfd[1].events = 0;
252 pollfd[0].revents = pollfd[1].revents = 0;
253 n0 = pkt_queued(me, 0);
254 n1 = pkt_queued(me + 1, 0);
256 pollfd[1].events |= POLLOUT;
258 pollfd[0].events |= POLLIN;
260 pollfd[0].events |= POLLOUT;
262 pollfd[1].events |= POLLIN;
263 ret = poll(pollfd, 2, 2500);
264 if (ret <= 0 || verbose)
265 D("poll %s [0] ev %x %x rx %d@%d tx %d,"
266 " [1] ev %x %x rx %d@%d tx %d",
267 ret <= 0 ? "timeout" : "ok",
281 if (pollfd[0].revents & POLLERR) {
282 D("error on fd0, rxcur %d@%d",
283 me[0].rx->avail, me[0].rx->cur);
285 if (pollfd[1].revents & POLLERR) {
286 D("error on fd1, rxcur %d@%d",
287 me[1].rx->avail, me[1].rx->cur);
289 if (pollfd[0].revents & POLLOUT) {
290 move(me + 1, me, burst);
291 // XXX we don't need the ioctl */
292 // ioctl(me[0].fd, NIOCTXSYNC, NULL);
294 if (pollfd[1].revents & POLLOUT) {
295 move(me, me + 1, burst);
296 // XXX we don't need the ioctl */
297 // ioctl(me[1].fd, NIOCTXSYNC, NULL);
301 netmap_close(me + 1);
302 netmap_close(me + 0);