FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
netmap-tutorial/codelab/fe.c at master · dnwick/netmap-tutorial · GitHub
dnwick
/
netmap-tutorial
Public
forked from
netmap-unipi/netmap-tutorial
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
netmap-tutorial
/
codelab
/
fe.c
Copy path
More file actions
More file actions
Latest commit
History
History
History
286 lines (251 loc) · 7.57 KB
Breadcrumbs
netmap-tutorial
/
codelab
/
fe.c
Copy path
File metadata and controls
286 lines (251 loc) · 7.57 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
/*
* This program reads UDP packets from the first netmap port and
* selectively forwards them to the second or third port, depending
* on the UDP destination port. The user can specify two UDP ports A and
* B by command line: packets with destination port A will be forwarded
* to the second netmap port; packets with destination port B will be
* forwarded to the third netmap port; all the other packets are
* dropped.
*/
#include
<stdio.h>
#include
<stdlib.h>
#include
<unistd.h>
#include
<signal.h>
#include
<errno.h>
#include
<poll.h>
#include
<net/if.h>
#include
<stdint.h>
#include
<net/netmap.h>
#define
NETMAP_WITH_LIBS
#include
<net/netmap_user.h>
#include
<sys/socket.h>
#include
<netinet/in.h>
#include
<netinet/if_ether.h>
#include
<netinet/ip.h>
#include
<netinet/udp.h>
#include
<netinet/tcp.h>
static
int
stop
=
0
;
static
unsigned long long
fwdback
=
0
;
static
unsigned long long
fwda
=
0
;
static
unsigned long long
fwdb
=
0
;
static
unsigned long long
tot
=
0
;
static
void
sigint_handler
(
int
signum
)
{
stop
=
1
;
}
static
int
rx_ready
(
struct
nm_desc
*
nmd
)
{
unsigned
int
ri
;
for
(
ri
=
nmd
->
first_rx_ring
;
ri
<=
nmd
->
last_rx_ring
;
ri
++
) {
struct
netmap_ring
*
ring
;
ring
=
NETMAP_RXRING
(
nmd
->
nifp
,
ri
);
if
(
nm_ring_space
(
ring
)) {
return
1
;
/* there is something to read */
}
}
return
0
;
}
static
inline
int
pkt_get_udp_port
(
const
char
*
buf
)
{
struct
ether_header
*
ethh
;
struct
ip
*
iph
;
struct
udphdr
*
udph
;
ethh
=
(
struct
ether_header
*
)
buf
;
if
(
ethh
->
ether_type
!=
htons
(
ETHERTYPE_IP
)) {
/* Filter out non-IP traffic. */
return
0
;
}
iph
=
(
struct
ip
*
)(
ethh
+
1
);
if
(
iph
->
ip_p
!=
IPPROTO_UDP
) {
/* Filter out non-UDP traffic. */
return
0
;
}
udph
=
(
struct
udphdr
*
)(
iph
+
1
);
/* Return destination port. */
return
ntohs
(
udph
->
uh_dport
);
}
static
void
forward_pkts
(
struct
nm_desc
*
src
,
struct
nm_desc
*
dst
)
{
unsigned
int
si
=
src
->
first_rx_ring
;
unsigned
int
di
=
dst
->
first_tx_ring
;
while
(
si
<=
src
->
last_rx_ring
&&
di
<=
dst
->
last_tx_ring
) {
struct
netmap_ring
*
txring
;
struct
netmap_ring
*
rxring
;
unsigned
int
rxhead
,
txhead
;
int
nrx
,
ntx
;
rxring
=
NETMAP_RXRING
(
src
->
nifp
,
si
);
txring
=
NETMAP_TXRING
(
dst
->
nifp
,
di
);
nrx
=
nm_ring_space
(
rxring
);
ntx
=
nm_ring_space
(
txring
);
if
(
nrx
==
0
) {
si
++
;
continue
;
}
if
(
ntx
==
0
) {
di
++
;
continue
;
}
rxhead
=
rxring
->
head
;
txhead
=
txring
->
head
;
for
(;
nrx
>
0
&&
ntx
>
0
;
nrx
--
,
rxhead
=
nm_ring_next
(
rxring
,
rxhead
),
tot
++
) {
struct
netmap_slot
*
rs
=
&
rxring
->
slot
[
rxhead
];
struct
netmap_slot
*
ts
=
&
txring
->
slot
[
txhead
];
char
*
rxbuf
=
NETMAP_BUF
(
rxring
,
rs
->
buf_idx
);
char
*
txbuf
=
NETMAP_BUF
(
txring
,
ts
->
buf_idx
);
ts
->
len
=
rs
->
len
;
memcpy
(
txbuf
,
rxbuf
,
ts
->
len
);
txhead
=
nm_ring_next
(
txring
,
txhead
);
ntx
--
;
fwdback
++
;
tot
++
;
}
/* Update state of netmap ring. */
rxring
->
head
=
rxring
->
cur
=
rxhead
;
txring
->
head
=
txring
->
cur
=
txhead
;
}
}
static
int
main_loop
(
const
char
*
netmap_port_one
,
const
char
*
netmap_port_two
,
const
char
*
netmap_port_three
,
int
udp_port_a
,
int
udp_port_b
)
{
struct
nm_desc
*
nmd_one
;
struct
nm_desc
*
nmd_two
;
struct
nm_desc
*
nmd_three
;
nmd_one
=
nm_open
(
netmap_port_one
,
NULL
,
0
,
NULL
);
if
(
nmd_one
==
NULL
) {
if
(!
errno
) {
printf
(
"Failed to nm_open(%s): not a netmap port\n"
,
netmap_port_one
);
}
else
{
printf
(
"Failed to nm_open(%s): %s\n"
,
netmap_port_one
,
strerror
(
errno
));
}
return
-1
;
}
nmd_two
=
nm_open
(
netmap_port_two
,
NULL
,
0
,
NULL
);
if
(
nmd_two
==
NULL
) {
if
(!
errno
) {
printf
(
"Failed to nm_open(%s): not a netmap port\n"
,
netmap_port_two
);
}
else
{
printf
(
"Failed to nm_open(%s): %s\n"
,
netmap_port_two
,
strerror
(
errno
));
}
return
-1
;
}
nmd_three
=
nm_open
(
netmap_port_three
,
NULL
,
0
,
NULL
);
if
(
nmd_three
==
NULL
) {
if
(!
errno
) {
printf
(
"Failed to nm_open(%s): not a netmap port\n"
,
netmap_port_three
);
}
else
{
printf
(
"Failed to nm_open(%s): %s\n"
,
netmap_port_three
,
strerror
(
errno
));
}
return
-1
;
}
while
(!
stop
) {
/* Forward traffic from ports two and three back to port one. */
forward_pkts
(
nmd_two
,
nmd_one
);
forward_pkts
(
nmd_three
,
nmd_one
);
}
nm_close
(
nmd_one
);
nm_close
(
nmd_two
);
nm_close
(
nmd_three
);
printf
(
"Total processed packets: %llu\n"
,
tot
);
printf
(
"Forwarded to port one : %llu\n"
,
fwdback
);
printf
(
"Forwarded to port two : %llu\n"
,
fwda
);
printf
(
"Forwarded to port three: %llu\n"
,
fwdb
);
return
0
;
}
static
void
usage
(
char
*
*
argv
)
{
printf
(
"usage: %s [-h] [-i NETMAP_PORT_ONE] "
"[-i NETMAP_PORT_TWO] [-i NETMAP_PORT_THREE] "
"[-p UDP_PORT_A] [-p UDP_PORT_B]\n"
,
argv
[
0
]);
exit
(
EXIT_SUCCESS
);
}
int
main
(
int
argc
,
char
*
*
argv
)
{
const
char
*
netmap_port_one
=
NULL
;
const
char
*
netmap_port_two
=
NULL
;
const
char
*
netmap_port_three
=
NULL
;
int
udp_port
;
int
udp_port_a
=
8000
;
int
udp_port_b
=
8001
;
int
udp_port_args
=
0
;
struct
sigaction
sa
;
int
opt
;
int
ret
;
while
((
opt
=
getopt
(
argc
,
argv
,
"hi:p:"
))
!=
-1
) {
switch
(
opt
) {
case
'h'
:
usage
(
argv
);
return
0
;
case
'i'
:
if
(
netmap_port_one
==
NULL
) {
netmap_port_one
=
optarg
;
}
else
if
(
netmap_port_two
==
NULL
) {
netmap_port_two
=
optarg
;
}
else
if
(
netmap_port_three
==
NULL
) {
netmap_port_three
=
optarg
;
}
break
;
case
'p'
:
udp_port
=
atoi
(
optarg
);
if
(
udp_port
<=
0
||
udp_port
>=
65535
) {
printf
(
" invalid UDP port %s\n"
,
optarg
);
usage
(
argv
);
}
switch
(
udp_port_args
) {
case
0
:
udp_port_a
=
udp_port
;
break
;
case
1
:
udp_port_b
=
udp_port
;
break
;
}
udp_port_args
++
;
break
;
default
:
printf
(
" unrecognized option '-%c'\n"
,
opt
);
usage
(
argv
);
return
-1
;
}
}
if
(
netmap_port_one
==
NULL
) {
printf
(
" missing netmap port #1\n"
);
usage
(
argv
);
}
if
(
netmap_port_two
==
NULL
) {
printf
(
" missing netmap port #2\n"
);
usage
(
argv
);
}
/* Register Ctrl-C handler. */
sa
.
sa_handler
=
sigint_handler
;
sigemptyset
(
&
sa
.
sa_mask
);
sa
.
sa_flags
=
SA_RESTART
;
ret
=
sigaction
(
SIGINT
,
&
sa
,
NULL
);
if
(
ret
) {
perror
(
"sigaction(SIGINT)"
);
exit
(
EXIT_FAILURE
);
}
(
void
)
rx_ready
;
printf
(
"Port one : %s\n"
,
netmap_port_one
);
printf
(
"Port two : %s\n"
,
netmap_port_two
);
printf
(
"Port three: %s\n"
,
netmap_port_three
);
printf
(
"UDP port A: %d\n"
,
udp_port_a
);
printf
(
"UDP port B: %d\n"
,
udp_port_b
);
main_loop
(
netmap_port_one
,
netmap_port_two
,
netmap_port_three
,
udp_port_a
,
udp_port_b
);
(
void
)
pkt_get_udp_port
;
return
0
;
}
Back
|
FazBrowse Home
|
New Git URL