18
18
#include <net/netmap.h>
19
19
#define NETMAP_WITH_LIBS
20
20
#include <net/netmap_user.h>
21
- #include <netinet/ether.h>
21
+ #include <sys/socket.h>
22
+ #include <netinet/in.h>
23
+ #include <netinet/if_ether.h>
22
24
#include <netinet/ip.h>
23
25
#include <netinet/udp.h>
24
26
#include <netinet/tcp.h>
25
27
26
- static int stop = 0 ;
28
+ static int stop = 0 ;
27
29
static unsigned long long fwdback = 0 ;
28
- static unsigned long long fwda = 0 ;
29
- static unsigned long long fwdb = 0 ;
30
- static unsigned long long tot = 0 ;
30
+ static unsigned long long fwda = 0 ;
31
+ static unsigned long long fwdb = 0 ;
32
+ static unsigned long long tot = 0 ;
31
33
32
34
static void
33
35
sigint_handler (int signum )
@@ -40,13 +42,13 @@ rx_ready(struct nm_desc *nmd)
40
42
{
41
43
unsigned int ri ;
42
44
43
- for (ri = nmd -> first_rx_ring ; ri <= nmd -> last_rx_ring ; ri ++ ) {
44
- struct netmap_ring * ring ;
45
+ for (ri = nmd -> first_rx_ring ; ri <= nmd -> last_rx_ring ; ri ++ ) {
46
+ struct netmap_ring * ring ;
45
47
46
- ring = NETMAP_RXRING (nmd -> nifp , ri );
47
- if (nm_ring_space (ring )) {
48
- return 1 ; /* there is something to read */
49
- }
48
+ ring = NETMAP_RXRING (nmd -> nifp , ri );
49
+ if (nm_ring_space (ring )) {
50
+ return 1 ; /* there is something to read */
51
+ }
50
52
}
51
53
52
54
return 0 ;
@@ -56,23 +58,23 @@ static inline int
56
58
pkt_get_udp_port (const char * buf )
57
59
{
58
60
struct ether_header * ethh ;
59
- struct iphdr * iph ;
61
+ struct ip * iph ;
60
62
struct udphdr * udph ;
61
63
62
64
ethh = (struct ether_header * )buf ;
63
65
if (ethh -> ether_type != htons (ETHERTYPE_IP )) {
64
66
/* Filter out non-IP traffic. */
65
67
return 0 ;
66
68
}
67
- iph = (struct iphdr * )(ethh + 1 );
68
- if (iph -> protocol != IPPROTO_UDP ) {
69
+ iph = (struct ip * )(ethh + 1 );
70
+ if (iph -> ip_p != IPPROTO_UDP ) {
69
71
/* Filter out non-UDP traffic. */
70
72
return 0 ;
71
73
}
72
74
udph = (struct udphdr * )(iph + 1 );
73
75
74
76
/* Return destination port. */
75
- return ntohs (udph -> dest );
77
+ return ntohs (udph -> uh_dport );
76
78
}
77
79
78
80
#ifdef SOLUTION
@@ -148,8 +150,8 @@ forward_pkts(struct nm_desc *src, struct nm_desc *dst)
148
150
149
151
rxring = NETMAP_RXRING (src -> nifp , si );
150
152
txring = NETMAP_TXRING (dst -> nifp , di );
151
- nrx = nm_ring_space (rxring );
152
- ntx = nm_ring_space (txring );
153
+ nrx = nm_ring_space (rxring );
154
+ ntx = nm_ring_space (txring );
153
155
if (nrx == 0 ) {
154
156
si ++ ;
155
157
continue ;
@@ -159,25 +161,25 @@ forward_pkts(struct nm_desc *src, struct nm_desc *dst)
159
161
continue ;
160
162
}
161
163
162
- rxhead = rxring -> head ;
163
- txhead = txring -> head ;
164
- for (; nrx > 0 && ntx > 0 ;
165
- nrx -- , rxhead = nm_ring_next (rxring , rxhead ), tot ++ ) {
164
+ rxhead = rxring -> head ;
165
+ txhead = txring -> head ;
166
+ for (; nrx > 0 && ntx > 0 ;
167
+ nrx -- , rxhead = nm_ring_next (rxring , rxhead ), tot ++ ) {
166
168
struct netmap_slot * rs = & rxring -> slot [rxhead ];
167
169
struct netmap_slot * ts = & txring -> slot [txhead ];
168
- char * rxbuf = NETMAP_BUF (rxring , rs -> buf_idx );
169
- char * txbuf = NETMAP_BUF (txring , ts -> buf_idx );
170
+ char * rxbuf = NETMAP_BUF (rxring , rs -> buf_idx );
171
+ char * txbuf = NETMAP_BUF (txring , ts -> buf_idx );
170
172
171
173
ts -> len = rs -> len ;
172
174
memcpy (txbuf , rxbuf , ts -> len );
173
175
txhead = nm_ring_next (txring , txhead );
174
- ntx -- ;
175
- fwdback ++ ;
176
- tot ++ ;
176
+ ntx -- ;
177
+ fwdback ++ ;
178
+ tot ++ ;
177
179
}
178
180
/* Update state of netmap ring. */
179
- rxring -> head = rxring -> cur = rxhead ;
180
- txring -> head = txring -> cur = txhead ;
181
+ rxring -> head = rxring -> cur = rxhead ;
182
+ txring -> head = txring -> cur = txhead ;
181
183
}
182
184
}
183
185
@@ -192,11 +194,11 @@ main_loop(const char *netmap_port_one, const char *netmap_port_two,
192
194
nmd_one = nm_open (netmap_port_one , NULL , 0 , NULL );
193
195
if (nmd_one == NULL ) {
194
196
if (!errno ) {
195
- printf ("Failed to nm_open(%s): not a netmap port\n" ,
196
- netmap_port_one );
197
+ printf ("Failed to nm_open(%s): not a netmap port\n" ,
198
+ netmap_port_one );
197
199
} else {
198
- printf ("Failed to nm_open(%s): %s\n" , netmap_port_one ,
199
- strerror (errno ));
200
+ printf ("Failed to nm_open(%s): %s\n" , netmap_port_one ,
201
+ strerror (errno ));
200
202
}
201
203
return -1 ;
202
204
}
@@ -217,7 +219,7 @@ main_loop(const char *netmap_port_one, const char *netmap_port_two,
217
219
if (nmd_three == NULL ) {
218
220
if (!errno ) {
219
221
printf ("Failed to nm_open(%s): not a netmap port\n" ,
220
- netmap_port_three );
222
+ netmap_port_three );
221
223
} else {
222
224
printf ("Failed to nm_open(%s): %s\n" , netmap_port_three ,
223
225
strerror (errno ));
@@ -294,61 +296,62 @@ usage(char **argv)
294
296
{
295
297
printf ("usage: %s [-h] [-i NETMAP_PORT_ONE] "
296
298
"[-i NETMAP_PORT_TWO] [-i NETMAP_PORT_THREE] "
297
- "[-p UDP_PORT_A] [-p UDP_PORT_B]\n" , argv [0 ]);
299
+ "[-p UDP_PORT_A] [-p UDP_PORT_B]\n" ,
300
+ argv [0 ]);
298
301
exit (EXIT_SUCCESS );
299
302
}
300
303
301
304
int
302
305
main (int argc , char * * argv )
303
306
{
304
- const char * netmap_port_one = NULL ;
305
- const char * netmap_port_two = NULL ;
307
+ const char * netmap_port_one = NULL ;
308
+ const char * netmap_port_two = NULL ;
306
309
const char * netmap_port_three = NULL ;
307
310
int udp_port ;
308
- int udp_port_a = 8000 ;
309
- int udp_port_b = 8001 ;
311
+ int udp_port_a = 8000 ;
312
+ int udp_port_b = 8001 ;
310
313
int udp_port_args = 0 ;
311
314
struct sigaction sa ;
312
315
int opt ;
313
316
int ret ;
314
317
315
318
while ((opt = getopt (argc , argv , "hi:p:" )) != -1 ) {
316
319
switch (opt ) {
317
- case 'h' :
320
+ case 'h' :
321
+ usage (argv );
322
+ return 0 ;
323
+
324
+ case 'i' :
325
+ if (netmap_port_one == NULL ) {
326
+ netmap_port_one = optarg ;
327
+ } else if (netmap_port_two == NULL ) {
328
+ netmap_port_two = optarg ;
329
+ } else if (netmap_port_three == NULL ) {
330
+ netmap_port_three = optarg ;
331
+ }
332
+ break ;
333
+
334
+ case 'p' :
335
+ udp_port = atoi (optarg );
336
+ if (udp_port <= 0 || udp_port >= 65535 ) {
337
+ printf (" invalid UDP port %s\n" , optarg );
318
338
usage (argv );
319
- return 0 ;
320
-
321
- case 'i' :
322
- if (netmap_port_one == NULL ) {
323
- netmap_port_one = optarg ;
324
- } else if (netmap_port_two == NULL ) {
325
- netmap_port_two = optarg ;
326
- } else if (netmap_port_three == NULL ) {
327
- netmap_port_three = optarg ;
328
- }
339
+ }
340
+ switch (udp_port_args ) {
341
+ case 0 :
342
+ udp_port_a = udp_port ;
329
343
break ;
330
-
331
- case 'p' :
332
- udp_port = atoi (optarg );
333
- if (udp_port <= 0 || udp_port >= 65535 ) {
334
- printf (" invalid UDP port %s\n" , optarg );
335
- usage (argv );
336
- }
337
- switch (udp_port_args ) {
338
- case 0 :
339
- udp_port_a = udp_port ;
340
- break ;
341
- case 1 :
342
- udp_port_b = udp_port ;
343
- break ;
344
- }
345
- udp_port_args ++ ;
344
+ case 1 :
345
+ udp_port_b = udp_port ;
346
346
break ;
347
+ }
348
+ udp_port_args ++ ;
349
+ break ;
347
350
348
- default :
349
- printf (" unrecognized option '-%c'\n" , opt );
350
- usage (argv );
351
- return -1 ;
351
+ default :
352
+ printf (" unrecognized option '-%c'\n" , opt );
353
+ usage (argv );
354
+ return -1 ;
352
355
}
353
356
}
354
357
@@ -366,7 +369,7 @@ main(int argc, char **argv)
366
369
sa .sa_handler = sigint_handler ;
367
370
sigemptyset (& sa .sa_mask );
368
371
sa .sa_flags = SA_RESTART ;
369
- ret = sigaction (SIGINT , & sa , NULL );
372
+ ret = sigaction (SIGINT , & sa , NULL );
370
373
if (ret ) {
371
374
perror ("sigaction(SIGINT)" );
372
375
exit (EXIT_FAILURE );
@@ -379,8 +382,10 @@ main(int argc, char **argv)
379
382
printf ("UDP port A: %d\n" , udp_port_a );
380
383
printf ("UDP port B: %d\n" , udp_port_b );
381
384
382
- main_loop (netmap_port_one , netmap_port_two , netmap_port_three ,
383
- udp_port_a , udp_port_b );
385
+ main_loop (netmap_port_one , netmap_port_two , netmap_port_three , udp_port_a ,
386
+ udp_port_b );
387
+
388
+ (void ) pkt_get_udp_port ;
384
389
385
390
return 0 ;
386
391
}
0 commit comments