]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - tools/tools/netmap/bridge.c
- Copy stable/9 to releng/9.2 as part of the 9.2-RELEASE cycle.
[FreeBSD/releng/9.2.git] / tools / tools / netmap / bridge.c
1 /*
2  * (C) 2011 Luigi Rizzo, Matteo Landi
3  *
4  * BSD license
5  *
6  * A netmap client to bridge two network interfaces
7  * (or one interface and the host stack).
8  *
9  * $FreeBSD$
10  */
11
12 #include "nm_util.h"
13
14
15 int verbose = 0;
16
17 char *version = "$Id: bridge.c 12016 2013-01-23 17:24:22Z luigi $";
18
19 static int do_abort = 0;
20
21 static void
22 sigint_h(int sig)
23 {
24         (void)sig;      /* UNUSED */
25         do_abort = 1;
26         signal(SIGINT, SIG_DFL);
27 }
28
29
30 /*
31  * move up to 'limit' pkts from rxring to txring swapping buffers.
32  */
33 static int
34 process_rings(struct netmap_ring *rxring, struct netmap_ring *txring,
35               u_int limit, const char *msg)
36 {
37         u_int j, k, m = 0;
38
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;
49         m = limit;
50         while (limit-- > 0) {
51                 struct netmap_slot *rs = &rxring->slot[j];
52                 struct netmap_slot *ts = &txring->slot[k];
53                 uint32_t pkt;
54
55                 /* swap packets */
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);
59                         sleep(2);
60                 }
61                 pkt = ts->buf_idx;
62                 ts->buf_idx = rs->buf_idx;
63                 rs->buf_idx = pkt;
64
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);
68                 else if (verbose > 1)
69                         D("%s send len %d rx[%d] -> tx[%d]", msg, rs->len, j, k);
70                 ts->len = rs->len;
71
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);
77         }
78         rxring->avail -= m;
79         txring->avail -= m;
80         rxring->cur = j;
81         txring->cur = k;
82         if (verbose && m > 0)
83                 D("%s sent %d packets to %p", msg, m, txring);
84
85         return (m);
86 }
87
88 /* move packts from src to destination */
89 static int
90 move(struct my_ring *src, struct my_ring *dst, u_int limit)
91 {
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";
96
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) {
102                         si++;
103                         continue;
104                 }
105                 if (txring->avail == 0) {
106                         di++;
107                         continue;
108                 }
109                 m += process_rings(rxring, txring, limit, msg);
110         }
111
112         return (m);
113 }
114
115 /*
116  * how many packets on this set of queues ?
117  */
118 static int
119 pkt_queued(struct my_ring *me, int tx)
120 {
121         u_int i, tot = 0;
122
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);
127                 tot += ring->avail;
128         }
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 ?
133                                 "host":"net",
134                         tot, NETMAP_TXRING(me->nifp, me->begin)->cur);
135         return tot;
136 }
137
138 static void
139 usage(void)
140 {
141         fprintf(stderr,
142             "usage: bridge [-v] [-i ifa] [-i ifb] [-b burst] [-w wait_time] [iface]\n");
143         exit(1);
144 }
145
146 /*
147  * bridge [-v] if1 [if2]
148  *
149  * If only one name, or the two interfaces are the same,
150  * bridges userland and the adapter. Otherwise bridge
151  * two intefaces.
152  */
153 int
154 main(int argc, char **argv)
155 {
156         struct pollfd pollfd[2];
157         int i, ch;
158         u_int burst = 1024, wait_link = 4;
159         struct my_ring me[2];
160         char *ifa = NULL, *ifb = NULL;
161
162         fprintf(stderr, "%s %s built %s %s\n",
163                 argv[0], version, __DATE__, __TIME__);
164
165         bzero(me, sizeof(me));
166
167         while ( (ch = getopt(argc, argv, "b:i:vw:")) != -1) {
168                 switch (ch) {
169                 default:
170                         D("bad option %c %s", ch, optarg);
171                         usage();
172                         break;
173                 case 'b':       /* burst */
174                         burst = atoi(optarg);
175                         break;
176                 case 'i':       /* interface */
177                         if (ifa == NULL)
178                                 ifa = optarg;
179                         else if (ifb == NULL)
180                                 ifb = optarg;
181                         else
182                                 D("%s ignored, already have 2 interfaces",
183                                         optarg);
184                         break;
185                 case 'v':
186                         verbose++;
187                         break;
188                 case 'w':
189                         wait_link = atoi(optarg);
190                         break;
191                 }
192
193         }
194
195         argc -= optind;
196         argv += optind;
197
198         if (argc > 1)
199                 ifa = argv[1];
200         if (argc > 2)
201                 ifb = argv[2];
202         if (argc > 3)
203                 burst = atoi(argv[3]);
204         if (!ifb)
205                 ifb = ifa;
206         if (!ifa) {
207                 D("missing interface");
208                 usage();
209         }
210         if (burst < 1 || burst > 8192) {
211                 D("invalid burst %d, set to 1024", burst);
212                 burst = 1024;
213         }
214         if (wait_link > 100) {
215                 D("invalid wait_link %d, set to 4", wait_link);
216                 wait_link = 4;
217         }
218         /* setup netmap interface #1. */
219         me[0].ifname = ifa;
220         me[1].ifname = ifb;
221         if (!strcmp(ifa, ifb)) {
222                 D("same interface, endpoint 0 goes to host");
223                 i = NETMAP_SW_RING;
224         } else {
225                 /* two different interfaces. Take all rings on if1 */
226                 i = 0;  // all hw rings
227         }
228         if (netmap_open(me, i, 1))
229                 return (1);
230         me[1].mem = me[0].mem; /* copy the pointer, so only one mmap */
231         if (netmap_open(me+1, 0, 1))
232                 return (1);
233
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);
239         }
240
241         D("Wait %d secs for link to come up...", wait_link);
242         sleep(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);
246
247         /* main loop */
248         signal(SIGINT, sigint_h);
249         while (!do_abort) {
250                 int n0, n1, ret;
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);
255                 if (n0)
256                         pollfd[1].events |= POLLOUT;
257                 else
258                         pollfd[0].events |= POLLIN;
259                 if (n1)
260                         pollfd[0].events |= POLLOUT;
261                 else
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",
268                                 pollfd[0].events,
269                                 pollfd[0].revents,
270                                 pkt_queued(me, 0),
271                                 me[0].rx->cur,
272                                 pkt_queued(me, 1),
273                                 pollfd[1].events,
274                                 pollfd[1].revents,
275                                 pkt_queued(me+1, 0),
276                                 me[1].rx->cur,
277                                 pkt_queued(me+1, 1)
278                         );
279                 if (ret < 0)
280                         continue;
281                 if (pollfd[0].revents & POLLERR) {
282                         D("error on fd0, rxcur %d@%d",
283                                 me[0].rx->avail, me[0].rx->cur);
284                 }
285                 if (pollfd[1].revents & POLLERR) {
286                         D("error on fd1, rxcur %d@%d",
287                                 me[1].rx->avail, me[1].rx->cur);
288                 }
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);
293                 }
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);
298                 }
299         }
300         D("exiting");
301         netmap_close(me + 1);
302         netmap_close(me + 0);
303
304         return (0);
305 }