23
23
#include <netinet/udp.h>
24
24
#include <netinet/tcp.h>
25
25
26
- static int stop = 0 ;
26
+ static int stop = 0 ;
27
27
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 ;
28
+ static unsigned long long fwda = 0 ;
29
+ static unsigned long long fwdb = 0 ;
30
+ static unsigned long long tot = 0 ;
31
31
32
32
static void
33
33
sigint_handler (int signum )
@@ -40,13 +40,13 @@ rx_ready(struct nm_desc *nmd)
40
40
{
41
41
unsigned int ri ;
42
42
43
- for (ri = nmd -> first_rx_ring ; ri <= nmd -> last_rx_ring ; ri ++ ) {
44
- struct netmap_ring * ring ;
43
+ for (ri = nmd -> first_rx_ring ; ri <= nmd -> last_rx_ring ; ri ++ ) {
44
+ struct netmap_ring * ring ;
45
45
46
- ring = NETMAP_RXRING (nmd -> nifp , ri );
47
- if (nm_ring_space (ring )) {
48
- return 1 ; /* there is something to read */
49
- }
46
+ ring = NETMAP_RXRING (nmd -> nifp , ri );
47
+ if (nm_ring_space (ring )) {
48
+ return 1 ; /* there is something to read */
49
+ }
50
50
}
51
51
52
52
return 0 ;
@@ -75,7 +75,6 @@ pkt_get_udp_port(const char *buf)
75
75
return ntohs (udph -> dest );
76
76
}
77
77
78
-
79
78
static void
80
79
forward_pkts (struct nm_desc * src , struct nm_desc * dst )
81
80
{
@@ -90,8 +89,8 @@ forward_pkts(struct nm_desc *src, struct nm_desc *dst)
90
89
91
90
rxring = NETMAP_RXRING (src -> nifp , si );
92
91
txring = NETMAP_TXRING (dst -> nifp , di );
93
- nrx = nm_ring_space (rxring );
94
- ntx = nm_ring_space (txring );
92
+ nrx = nm_ring_space (rxring );
93
+ ntx = nm_ring_space (txring );
95
94
if (nrx == 0 ) {
96
95
si ++ ;
97
96
continue ;
@@ -101,25 +100,25 @@ forward_pkts(struct nm_desc *src, struct nm_desc *dst)
101
100
continue ;
102
101
}
103
102
104
- rxhead = rxring -> head ;
105
- txhead = txring -> head ;
106
- for (; nrx > 0 && ntx > 0 ;
107
- nrx -- , rxhead = nm_ring_next (rxring , rxhead ), tot ++ ) {
103
+ rxhead = rxring -> head ;
104
+ txhead = txring -> head ;
105
+ for (; nrx > 0 && ntx > 0 ;
106
+ nrx -- , rxhead = nm_ring_next (rxring , rxhead ), tot ++ ) {
108
107
struct netmap_slot * rs = & rxring -> slot [rxhead ];
109
108
struct netmap_slot * ts = & txring -> slot [txhead ];
110
- char * rxbuf = NETMAP_BUF (rxring , rs -> buf_idx );
111
- char * txbuf = NETMAP_BUF (txring , ts -> buf_idx );
109
+ char * rxbuf = NETMAP_BUF (rxring , rs -> buf_idx );
110
+ char * txbuf = NETMAP_BUF (txring , ts -> buf_idx );
112
111
113
112
ts -> len = rs -> len ;
114
113
memcpy (txbuf , rxbuf , ts -> len );
115
114
txhead = nm_ring_next (txring , txhead );
116
- ntx -- ;
117
- fwdback ++ ;
118
- tot ++ ;
115
+ ntx -- ;
116
+ fwdback ++ ;
117
+ tot ++ ;
119
118
}
120
119
/* Update state of netmap ring. */
121
- rxring -> head = rxring -> cur = rxhead ;
122
- txring -> head = txring -> cur = txhead ;
120
+ rxring -> head = rxring -> cur = rxhead ;
121
+ txring -> head = txring -> cur = txhead ;
123
122
}
124
123
}
125
124
@@ -134,11 +133,11 @@ main_loop(const char *netmap_port_one, const char *netmap_port_two,
134
133
nmd_one = nm_open (netmap_port_one , NULL , 0 , NULL );
135
134
if (nmd_one == NULL ) {
136
135
if (!errno ) {
137
- printf ("Failed to nm_open(%s): not a netmap port\n" ,
138
- netmap_port_one );
136
+ printf ("Failed to nm_open(%s): not a netmap port\n" ,
137
+ netmap_port_one );
139
138
} else {
140
- printf ("Failed to nm_open(%s): %s\n" , netmap_port_one ,
141
- strerror (errno ));
139
+ printf ("Failed to nm_open(%s): %s\n" , netmap_port_one ,
140
+ strerror (errno ));
142
141
}
143
142
return -1 ;
144
143
}
@@ -159,7 +158,7 @@ main_loop(const char *netmap_port_one, const char *netmap_port_two,
159
158
if (nmd_three == NULL ) {
160
159
if (!errno ) {
161
160
printf ("Failed to nm_open(%s): not a netmap port\n" ,
162
- netmap_port_three );
161
+ netmap_port_three );
163
162
} else {
164
163
printf ("Failed to nm_open(%s): %s\n" , netmap_port_three ,
165
164
strerror (errno ));
@@ -168,7 +167,6 @@ main_loop(const char *netmap_port_one, const char *netmap_port_two,
168
167
}
169
168
170
169
while (!stop ) {
171
-
172
170
/* Forward traffic from ports two and three back to port one. */
173
171
forward_pkts (nmd_two , nmd_one );
174
172
forward_pkts (nmd_three , nmd_one );
@@ -191,61 +189,62 @@ usage(char **argv)
191
189
{
192
190
printf ("usage: %s [-h] [-i NETMAP_PORT_ONE] "
193
191
"[-i NETMAP_PORT_TWO] [-i NETMAP_PORT_THREE] "
194
- "[-p UDP_PORT_A] [-p UDP_PORT_B]\n" , argv [0 ]);
192
+ "[-p UDP_PORT_A] [-p UDP_PORT_B]\n" ,
193
+ argv [0 ]);
195
194
exit (EXIT_SUCCESS );
196
195
}
197
196
198
197
int
199
198
main (int argc , char * * argv )
200
199
{
201
- const char * netmap_port_one = NULL ;
202
- const char * netmap_port_two = NULL ;
200
+ const char * netmap_port_one = NULL ;
201
+ const char * netmap_port_two = NULL ;
203
202
const char * netmap_port_three = NULL ;
204
203
int udp_port ;
205
- int udp_port_a = 8000 ;
206
- int udp_port_b = 8001 ;
204
+ int udp_port_a = 8000 ;
205
+ int udp_port_b = 8001 ;
207
206
int udp_port_args = 0 ;
208
207
struct sigaction sa ;
209
208
int opt ;
210
209
int ret ;
211
210
212
211
while ((opt = getopt (argc , argv , "hi:p:" )) != -1 ) {
213
212
switch (opt ) {
214
- case 'h' :
213
+ case 'h' :
214
+ usage (argv );
215
+ return 0 ;
216
+
217
+ case 'i' :
218
+ if (netmap_port_one == NULL ) {
219
+ netmap_port_one = optarg ;
220
+ } else if (netmap_port_two == NULL ) {
221
+ netmap_port_two = optarg ;
222
+ } else if (netmap_port_three == NULL ) {
223
+ netmap_port_three = optarg ;
224
+ }
225
+ break ;
226
+
227
+ case 'p' :
228
+ udp_port = atoi (optarg );
229
+ if (udp_port <= 0 || udp_port >= 65535 ) {
230
+ printf (" invalid UDP port %s\n" , optarg );
215
231
usage (argv );
216
- return 0 ;
217
-
218
- case 'i' :
219
- if (netmap_port_one == NULL ) {
220
- netmap_port_one = optarg ;
221
- } else if (netmap_port_two == NULL ) {
222
- netmap_port_two = optarg ;
223
- } else if (netmap_port_three == NULL ) {
224
- netmap_port_three = optarg ;
225
- }
232
+ }
233
+ switch (udp_port_args ) {
234
+ case 0 :
235
+ udp_port_a = udp_port ;
226
236
break ;
227
-
228
- case 'p' :
229
- udp_port = atoi (optarg );
230
- if (udp_port <= 0 || udp_port >= 65535 ) {
231
- printf (" invalid UDP port %s\n" , optarg );
232
- usage (argv );
233
- }
234
- switch (udp_port_args ) {
235
- case 0 :
236
- udp_port_a = udp_port ;
237
- break ;
238
- case 1 :
239
- udp_port_b = udp_port ;
240
- break ;
241
- }
242
- udp_port_args ++ ;
237
+ case 1 :
238
+ udp_port_b = udp_port ;
243
239
break ;
240
+ }
241
+ udp_port_args ++ ;
242
+ break ;
244
243
245
- default :
246
- printf (" unrecognized option '-%c'\n" , opt );
247
- usage (argv );
248
- return -1 ;
244
+ default :
245
+ printf (" unrecognized option '-%c'\n" , opt );
246
+ usage (argv );
247
+ return -1 ;
249
248
}
250
249
}
251
250
@@ -263,7 +262,7 @@ main(int argc, char **argv)
263
262
sa .sa_handler = sigint_handler ;
264
263
sigemptyset (& sa .sa_mask );
265
264
sa .sa_flags = SA_RESTART ;
266
- ret = sigaction (SIGINT , & sa , NULL );
265
+ ret = sigaction (SIGINT , & sa , NULL );
267
266
if (ret ) {
268
267
perror ("sigaction(SIGINT)" );
269
268
exit (EXIT_FAILURE );
@@ -276,8 +275,8 @@ main(int argc, char **argv)
276
275
printf ("UDP port A: %d\n" , udp_port_a );
277
276
printf ("UDP port B: %d\n" , udp_port_b );
278
277
279
- main_loop (netmap_port_one , netmap_port_two , netmap_port_three ,
280
- udp_port_a , udp_port_b );
278
+ main_loop (netmap_port_one , netmap_port_two , netmap_port_three , udp_port_a ,
279
+ udp_port_b );
281
280
282
281
return 0 ;
283
282
}
0 commit comments