Skip to content

Commit 670dc10

Browse files
committed
solutions: apply codelab fixes
1 parent a0b90f4 commit 670dc10

File tree

5 files changed

+191
-172
lines changed

5 files changed

+191
-172
lines changed

solutions/Makefile

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
CC=gcc
21
CFLAGS=-Wall -g -Werror -DSOLUTION
32
PROGS=sink forward swap fe
43

@@ -10,4 +9,4 @@ swap: swap.o
109
fe: fe.o
1110

1211
clean:
13-
-rm *.o $(PROGS)
12+
-rm -f *.o $(PROGS)

solutions/fe.c

Lines changed: 77 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,18 @@
1818
#include <net/netmap.h>
1919
#define NETMAP_WITH_LIBS
2020
#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>
2224
#include <netinet/ip.h>
2325
#include <netinet/udp.h>
2426
#include <netinet/tcp.h>
2527

26-
static int stop = 0;
28+
static int stop = 0;
2729
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;
3133

3234
static void
3335
sigint_handler(int signum)
@@ -40,13 +42,13 @@ rx_ready(struct nm_desc *nmd)
4042
{
4143
unsigned int ri;
4244

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;
4547

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+
}
5052
}
5153

5254
return 0;
@@ -56,23 +58,23 @@ static inline int
5658
pkt_get_udp_port(const char *buf)
5759
{
5860
struct ether_header *ethh;
59-
struct iphdr *iph;
61+
struct ip *iph;
6062
struct udphdr *udph;
6163

6264
ethh = (struct ether_header *)buf;
6365
if (ethh->ether_type != htons(ETHERTYPE_IP)) {
6466
/* Filter out non-IP traffic. */
6567
return 0;
6668
}
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) {
6971
/* Filter out non-UDP traffic. */
7072
return 0;
7173
}
7274
udph = (struct udphdr *)(iph + 1);
7375

7476
/* Return destination port. */
75-
return ntohs(udph->dest);
77+
return ntohs(udph->uh_dport);
7678
}
7779

7880
#ifdef SOLUTION
@@ -148,8 +150,8 @@ forward_pkts(struct nm_desc *src, struct nm_desc *dst)
148150

149151
rxring = NETMAP_RXRING(src->nifp, si);
150152
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);
153155
if (nrx == 0) {
154156
si++;
155157
continue;
@@ -159,25 +161,25 @@ forward_pkts(struct nm_desc *src, struct nm_desc *dst)
159161
continue;
160162
}
161163

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++) {
166168
struct netmap_slot *rs = &rxring->slot[rxhead];
167169
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);
170172

171173
ts->len = rs->len;
172174
memcpy(txbuf, rxbuf, ts->len);
173175
txhead = nm_ring_next(txring, txhead);
174-
ntx --;
175-
fwdback ++;
176-
tot ++;
176+
ntx--;
177+
fwdback++;
178+
tot++;
177179
}
178180
/* 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;
181183
}
182184
}
183185

@@ -192,11 +194,11 @@ main_loop(const char *netmap_port_one, const char *netmap_port_two,
192194
nmd_one = nm_open(netmap_port_one, NULL, 0, NULL);
193195
if (nmd_one == NULL) {
194196
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);
197199
} 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));
200202
}
201203
return -1;
202204
}
@@ -217,7 +219,7 @@ main_loop(const char *netmap_port_one, const char *netmap_port_two,
217219
if (nmd_three == NULL) {
218220
if (!errno) {
219221
printf("Failed to nm_open(%s): not a netmap port\n",
220-
netmap_port_three);
222+
netmap_port_three);
221223
} else {
222224
printf("Failed to nm_open(%s): %s\n", netmap_port_three,
223225
strerror(errno));
@@ -294,61 +296,62 @@ usage(char **argv)
294296
{
295297
printf("usage: %s [-h] [-i NETMAP_PORT_ONE] "
296298
"[-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]);
298301
exit(EXIT_SUCCESS);
299302
}
300303

301304
int
302305
main(int argc, char **argv)
303306
{
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;
306309
const char *netmap_port_three = NULL;
307310
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;
310313
int udp_port_args = 0;
311314
struct sigaction sa;
312315
int opt;
313316
int ret;
314317

315318
while ((opt = getopt(argc, argv, "hi:p:")) != -1) {
316319
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);
318338
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;
329343
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;
346346
break;
347+
}
348+
udp_port_args++;
349+
break;
347350

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;
352355
}
353356
}
354357

@@ -366,7 +369,7 @@ main(int argc, char **argv)
366369
sa.sa_handler = sigint_handler;
367370
sigemptyset(&sa.sa_mask);
368371
sa.sa_flags = SA_RESTART;
369-
ret = sigaction(SIGINT, &sa, NULL);
372+
ret = sigaction(SIGINT, &sa, NULL);
370373
if (ret) {
371374
perror("sigaction(SIGINT)");
372375
exit(EXIT_FAILURE);
@@ -379,8 +382,10 @@ main(int argc, char **argv)
379382
printf("UDP port A: %d\n", udp_port_a);
380383
printf("UDP port B: %d\n", udp_port_b);
381384

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;
384389

385390
return 0;
386391
}

0 commit comments

Comments
 (0)