-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbridge.c
452 lines (405 loc) · 10.2 KB
/
bridge.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
/*
* (C) 2011-2014 Luigi Rizzo, Matteo Landi
*
* BSD license
*
* A netmap client to bridge two network interfaces
* (or one interface and the host stack).
*
* $FreeBSD$
*/
#include <stdio.h>
#define NETMAP_WITH_LIBS
#include <net/netmap_user.h>
#include <sys/poll.h>
int verbose = 0;
static int do_abort = 0;
static int zerocopy = 1; /* enable zerocopy if possible */
static void *pkt;
static void
sigint_h(int sig)
{
(void)sig; /* UNUSED */
do_abort = 1;
signal(SIGINT, SIG_DFL);
}
static void
hexdump(char *desc, void *buf, int len) {
int i;
unsigned char buff[17];
unsigned char *pc = (unsigned char *) buf;
if (desc != NULL) {
printf("%s:\n", desc);
}
for (i = 0; i < len; i++) {
if ((i % 16) == 0) {
if (i != 0) {
printf(" %s\n", buff);
}
printf(" %04x ", i);;
}
printf(" %02x", pc[i]);
if ((pc[i] < 0x20) || (pc[i] > 0x7e)) {
buff[i % 16] = '.';
} else {
buff[i % 16] = pc[i];
}
buff[(i % 16) + 1] = '\0';
}
while ((i % 16) != 0) {
printf(" ");
i++;
}
printf(" %s\n", buff);
}
static void
print_packet(char *buf, int len, int tx) {
bzero(pkt, sizeof(pkt));
memcpy(pkt, (void*)buf, len);
if (tx) {
printf("------TX(copied)------\n");
} else {
printf("------RX------\n");
}
hexdump(NULL, pkt, len);
}
/*
* how many packets on this set of queues ?
*/
int
pkt_queued(struct nm_desc *d, int tx)
{
u_int i, tot = 0;
if (tx) {
for (i = d->first_tx_ring; i <= d->last_tx_ring; i++) {
tot += nm_ring_space(NETMAP_TXRING(d->nifp, i));
}
} else {
for (i = d->first_rx_ring; i <= d->last_rx_ring; i++) {
tot += nm_ring_space(NETMAP_RXRING(d->nifp, i));
}
}
return tot;
}
#define BUF_OFFSET(buf, x, t) (*((t *) (buf + x)))
static int
filter(char* buf, int len) {
//ICMP
if (len >= 34
&& (BUF_OFFSET(buf, 12, uint16_t) == 0x0008)
&& (BUF_OFFSET(buf, 23, uint8_t) == 0x1)
) {
int request = BUF_OFFSET(buf, 34, uint8_t);
printf("ICMP %s \n", request ? "request" : "reply" );
return 1;
}
// ARP
if (len >= 14 &&
BUF_OFFSET(buf, 12, uint16_t) == 0x0608) {
char *request = (BUF_OFFSET(buf, 21, uint8_t) == 0x1 ? "request" : "reply");
printf("ARP %s \n", request);
return 1;
}
printf("Unknown protocol\n");
return 0;
}
/*
* move up to 'limit' pkts from rxring to txring swapping buffers.
*/
static int
process_rings(struct netmap_ring *rxring, struct netmap_ring *txring,
u_int limit, const char *msg, const char *if_src, const char *if_dst)
{
u_int j, k, m = 0;
/* print a warning if any of the ring flags is set (e.g. NM_REINIT) */
if (rxring->flags || txring->flags)
D("%s rxflags %x txflags %x",
msg, rxring->flags, txring->flags);
j = rxring->cur; /* RX */
k = txring->cur; /* TX */
m = nm_ring_space(rxring);
if (m < limit)
limit = m;
m = nm_ring_space(txring);
if (m < limit)
limit = m;
m = limit;
while (limit-- > 0) {
struct netmap_slot *rs = &rxring->slot[j];
struct netmap_slot *ts = &txring->slot[k];
/* swap packets */
if (ts->buf_idx < 2 || rs->buf_idx < 2) {
RD(5, "wrong index rx[%d] = %d -> tx[%d] = %d",
j, rs->buf_idx, k, ts->buf_idx);
sleep(2);
}
/* copy the packet length. */
if (rs->len > rxring->nr_buf_size) {
RD(5, "wrong len %d rx[%d] -> tx[%d]", rs->len, j, k);
rs->len = 0;
} else if (verbose > 1) {
D("%s send len %d rx[%d] -> tx[%d]", msg, rs->len, j, k);
}
ts->len = rs->len;
if (zerocopy) {
uint32_t pkt = ts->buf_idx;
ts->buf_idx = rs->buf_idx;
rs->buf_idx = pkt;
/* report the buffer change. */
ts->flags |= NS_BUF_CHANGED;
rs->flags |= NS_BUF_CHANGED;
/* copy the NS_MOREFRAG */
rs->flags = (rs->flags & ~NS_MOREFRAG) | (ts->flags & NS_MOREFRAG);
printf("%s -> %s -- ", if_src, if_dst);
char *txbuf = NETMAP_BUF(txring, ts->buf_idx);
filter(txbuf, ts->len);
if (verbose > 0) {
print_packet(txbuf, ts->len, 1);
}
} else {
printf("%s -> %s -- ", if_src, if_dst);
char *rxbuf = NETMAP_BUF(rxring, rs->buf_idx);
filter(rxbuf, rs->len);
if (verbose > 0)
print_packet(rxbuf, rs->len, 0);
char *txbuf = NETMAP_BUF(txring, ts->buf_idx);
nm_pkt_copy(rxbuf, txbuf, ts->len);
if (verbose > 0)
print_packet(txbuf, ts->len, 1);
}
j = nm_ring_next(rxring, j);
k = nm_ring_next(txring, k);
}
rxring->head = rxring->cur = j;
txring->head = txring->cur = k;
if (verbose && m > 0)
D("%s sent %d packets to %p", msg, m, txring);
return (m);
}
/* move packts from src to destination */
static int
move(struct nm_desc *src, struct nm_desc *dst, u_int limit, const char *if_src, const char* if_dst)
{
struct netmap_ring *txring, *rxring;
u_int m = 0, si = src->first_rx_ring, di = dst->first_tx_ring;
const char *msg = (src->req.nr_flags == NR_REG_SW) ?
"host->net" : "net->host";
while (si <= src->last_rx_ring && di <= dst->last_tx_ring) {
rxring = NETMAP_RXRING(src->nifp, si);
txring = NETMAP_TXRING(dst->nifp, di);
ND("txring %p rxring %p", txring, rxring);
if (nm_ring_empty(rxring)) {
si++;
continue;
}
if (nm_ring_empty(txring)) {
di++;
continue;
}
m += process_rings(rxring, txring, limit, msg, if_src, if_dst);
}
return (m);
}
static void
usage(void)
{
fprintf(stderr,
"netmap bridge program: forward packets between two "
"network interfaces\n"
" usage(1): bridge [-v] [-i ifa] [-i ifb] [-b burst] "
"[-w wait_time] [-L]\n"
" usage(2): bridge [-v] [-w wait_time] [-L] "
"[ifa [ifb [burst]]]\n"
"\n"
" ifa and ifb are specified using the nm_open() syntax.\n"
" When ifb is missing (or is equal to ifa), bridge will\n"
" forward between between ifa and the host stack if -L\n"
" is not specified, otherwise loopback traffic on ifa.\n"
"\n"
" example: bridge -w 10 -i netmap:eth3 -i netmap:eth1\n"
);
exit(1);
}
/*
* bridge [-v] if1 [if2]
*
* If only one name, or the two interfaces are the same,
* bridges userland and the adapter. Otherwise bridge
* two intefaces.
*/
int
main(int argc, char **argv)
{
struct pollfd pollfd[2];
int ch;
u_int burst = 1024, wait_link = 4;
struct nm_desc *pa = NULL, *pb = NULL;
char *ifa = NULL, *ifb = NULL;
char ifabuf[64] = { 0 };
int loopback = 0;
fprintf(stderr, "%s built %s %s\n\n", argv[0], __DATE__, __TIME__);
while ((ch = getopt(argc, argv, "hb:ci:vw:L")) != -1) {
switch (ch) {
default:
D("bad option %c %s", ch, optarg);
/* fallthrough */
case 'h':
usage();
break;
case 'b': /* burst */
burst = atoi(optarg);
break;
case 'i': /* interface */
if (ifa == NULL)
ifa = optarg;
else if (ifb == NULL)
ifb = optarg;
else
D("%s ignored, already have 2 interfaces",
optarg);
break;
case 'c':
zerocopy = 0; /* do not zerocopy */
break;
case 'v':
verbose++;
break;
case 'w':
wait_link = atoi(optarg);
break;
case 'L':
loopback = 1;
break;
}
}
argc -= optind;
argv += optind;
if (argc > 0)
ifa = argv[0];
if (argc > 1)
ifb = argv[1];
if (argc > 2)
burst = atoi(argv[2]);
if (!ifb)
ifb = ifa;
if (!ifa) {
D("missing interface");
usage();
}
if (burst < 1 || burst > 8192) {
D("invalid burst %d, set to 1024", burst);
burst = 1024;
}
if (wait_link > 100) {
D("invalid wait_link %d, set to 4", wait_link);
wait_link = 4;
}
if (!strcmp(ifa, ifb)) {
if (!loopback) {
D("same interface, endpoint 0 goes to host");
snprintf(ifabuf, sizeof(ifabuf) - 1, "%s^", ifa);
ifa = ifabuf;
} else {
D("same interface, loopbacking traffic");
}
} else {
/* two different interfaces. Take all rings on if1 */
}
pa = nm_open(ifa, NULL, 0, NULL);
if (pa == NULL) {
D("cannot open %s", ifa);
return (1);
}
/* try to reuse the mmap() of the first interface, if possible */
pb = nm_open(ifb, NULL, NM_OPEN_NO_MMAP, pa);
if (pb == NULL) {
D("cannot open %s", ifb);
nm_close(pa);
return (1);
}
zerocopy = zerocopy && (pa->mem == pb->mem);
D("------- zerocopy %ssupported", zerocopy ? "" : "NOT ");
pkt = malloc(1500);
/* setup poll(2) array */
memset(pollfd, 0, sizeof(pollfd));
pollfd[0].fd = pa->fd;
pollfd[1].fd = pb->fd;
D("Wait %d secs for link to come up...", wait_link);
sleep(wait_link);
D("Ready to go, %s 0x%x/%d <-> %s 0x%x/%d.",
pa->req.nr_name, pa->first_rx_ring, pa->req.nr_rx_rings,
pb->req.nr_name, pb->first_rx_ring, pb->req.nr_rx_rings);
/* main loop */
signal(SIGINT, sigint_h);
while (!do_abort) {
int n0, n1, ret;
pollfd[0].events = pollfd[1].events = 0;
pollfd[0].revents = pollfd[1].revents = 0;
n0 = pkt_queued(pa, 0);
n1 = pkt_queued(pb, 0);
#if defined(_WIN32) || defined(BUSYWAIT)
if (n0) {
ioctl(pollfd[1].fd, NIOCTXSYNC, NULL);
pollfd[1].revents = POLLOUT;
} else {
ioctl(pollfd[0].fd, NIOCRXSYNC, NULL);
}
if (n1) {
ioctl(pollfd[0].fd, NIOCTXSYNC, NULL);
pollfd[0].revents = POLLOUT;
} else {
ioctl(pollfd[1].fd, NIOCRXSYNC, NULL);
}
ret = 1;
#else
if (n0)
pollfd[1].events |= POLLOUT;
else
pollfd[0].events |= POLLIN;
if (n1)
pollfd[0].events |= POLLOUT;
else
pollfd[1].events |= POLLIN;
/* poll() also cause kernel to txsync/rxsync the NICs */
ret = poll(pollfd, 2, 2500);
#endif /* defined(_WIN32) || defined(BUSYWAIT) */
if (ret <= 0 || verbose)
D("poll %s [0(%s)] ev %x %x rx %d@%d tx %d,"
" [1(%s)] ev %x %x rx %d@%d tx %d",
ret <= 0 ? "timeout" : "ok",
ifa,
pollfd[0].events,
pollfd[0].revents,
pkt_queued(pa, 0),
NETMAP_RXRING(pa->nifp, pa->cur_rx_ring)->cur,
pkt_queued(pa, 1),
ifb,
pollfd[1].events,
pollfd[1].revents,
pkt_queued(pb, 0),
NETMAP_RXRING(pb->nifp, pb->cur_rx_ring)->cur,
pkt_queued(pb, 1)
);
if (ret < 0)
continue;
if (pollfd[0].revents & POLLERR) {
struct netmap_ring *rx = NETMAP_RXRING(pa->nifp, pa->cur_rx_ring);
D("error on fd0, rx [%d,%d,%d)",
rx->head, rx->cur, rx->tail);
}
if (pollfd[1].revents & POLLERR) {
struct netmap_ring *rx = NETMAP_RXRING(pb->nifp, pb->cur_rx_ring);
D("error on fd1, rx [%d,%d,%d)",
rx->head, rx->cur, rx->tail);
}
if (pollfd[0].revents & POLLOUT)
move(pb, pa, burst, ifb, ifa); /* move from b to a */
if (pollfd[1].revents & POLLOUT)
move(pa, pb, burst, ifa, ifb); /* move from a to b */
/* We don't need ioctl(NIOCTXSYNC) on the two file descriptors here,
* kernel will txsync on next poll(). */
}
nm_close(pb);
nm_close(pa);
return (0);
}