Skip to content

Commit cd035d5

Browse files
committed
packet03-redirecting: remove refs to tx_port map from assignment 04
Looking up the tx_port map for a 1:1 mapping in packet 03 assignment 04 is pointless. If bpf_fib_lookup returns a successful lookup, it is much simplier and more obvious to directly call bpf_redirect with the information from the fib without the map lookup. Signed-off-by: Ray Kinsella <[email protected]>
1 parent dbbbbdc commit cd035d5

File tree

5 files changed

+19
-30
lines changed

5 files changed

+19
-30
lines changed

packet-solutions/xdp_prog_kern_03.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ int xdp_router_func(struct xdp_md *ctx)
304304

305305
memcpy(eth->h_dest, fib_params.dmac, ETH_ALEN);
306306
memcpy(eth->h_source, fib_params.smac, ETH_ALEN);
307-
action = bpf_redirect_map(&tx_port, fib_params.ifindex, 0);
307+
action = bpf_redirect(fib_params.ifindex, 0);
308308
break;
309309
case BPF_FIB_LKUP_RET_BLACKHOLE: /* dest is blackholed; can be dropped */
310310
case BPF_FIB_LKUP_RET_UNREACHABLE: /* dest is unreachable; can be dropped */

packet-solutions/xdp_prog_user.c

-4
Original file line numberDiff line numberDiff line change
@@ -172,10 +172,6 @@ int main(int argc, char **argv)
172172
fprintf(stderr, "can't write iface params\n");
173173
return 1;
174174
}
175-
} else {
176-
/* setup 1-1 mapping for the dynamic router */
177-
for (i = 1; i < 256; ++i)
178-
bpf_map_update_elem(map_fd, &i, &i, 0);
179175
}
180176

181177
return EXIT_OK;

packet03-redirecting/README.org

+16-21
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,14 @@ an option to forward packets to egress ports of other interfaces (if the
4646
corresponding driver supports this feature). This can be done using the
4747
=bpf_redirect= or =bpf_redirect_map= helpers. These helpers will return the
4848
=XDP_REDIRECT= value and this is what should the program return. The
49-
=bpf_redirect= helper actually shouldn't be used in production as it is slow
50-
and can't be configured from user space. However, we will use it in the
51-
Assignment 2 for the sake of simplicity. To use the =bpf_redirect_map= helper
52-
we need to setup a special map of type =BPF_MAP_TYPE_DEVMAP= which maps virtual
53-
ports to actual network devices. See [[https://lwn.net/Articles/728146][this
54-
patch series]] which implemented the XDP redirect functionality. An example of
55-
usage can be found in the
49+
=bpf_redirect= helper takes the interface index of the redirect port as
50+
parameter and may be used with other helpers such as =bpf_fib_lookup=. We will
51+
use it in Assignment 2 in a simpler way, and again in Assignment 4 with a kernel
52+
fib lookup. To use the =bpf_redirect_map= helper we need to setup a special map
53+
of type =BPF_MAP_TYPE_DEVMAP= which maps virtual ports to actual network
54+
devices. See [[https://lwn.net/Articles/728146][this patch series]] which
55+
implemented the XDP redirect functionality. An example of usage can be found in
56+
the
5657
[[https://github.com/torvalds/linux/blob/master/samples/bpf/xdp_redirect_map_kern.c][xdp_redirect_map_kern.c]]
5758
file and the corresponding loader.
5859

@@ -171,9 +172,9 @@ XDP_REDIRECT 176 pkts ( 4 pps) 20 Kbytes ( 0 Mbits/
171172
** Assignment 3: Extend to a bidirectional router
172173

173174
In the previous assignment we were able to pass packets from one interface to
174-
the other. However, we were using the wrong =bpf_redirect= helper and needed to
175-
hardcode interface number and MAC addresses. This is not useful and we will use
176-
better techniques in this Assignment.
175+
the other. However, we needed to hard code the interface number and MAC
176+
addresses. This is not useful and we will use better techniques in this
177+
Assignment.
177178

178179
This Assignment will show how to use the =bpf_redirect_map= function. Besides
179180
that, to make the program more useful we will use a map which will contain a
@@ -246,13 +247,11 @@ and return =XDP_PASS= when packets destined to outer interfaces, but this
246247
doesn't cover all cases and wouldn't it be better to dynamically lookup
247248
where each packet should go?
248249

249-
This assignment teaches to use the =bpf_fib_lookup= helper. This function lets
250-
XDP and TC programs to access the kernel routing table and will return an
251-
ifindex of interface to forward packet to. As was noted the =bpf_redirect_map=
252-
function accepts virtual ports, so to use ifindexes you will need to update the
253-
=xdp_prog_user.c= program to setup 1-1 mapping between virtual ports and
254-
network devices where each virtual port corresponds to a device with the same
255-
ifindex.
250+
This assignment teaches how to use the =bpf_fib_lookup= helper. This function
251+
lets XDP and TC programs access the kernel routing table and will return the
252+
ifindex of interface to forward packet to, along with source and destination
253+
mac addresses. After updating the Ethernet header, we can then redirect the
254+
packet to this interface with the =bpf_redirect= function.
256255

257256
The Assignment, for the most part, reproduces the
258257
[[https://github.com/torvalds/linux/blob/master/samples/bpf/xdp_fwd_kern.c][xdp_fwd_kern.c]]
@@ -286,10 +285,6 @@ $ t exec -n uno -- ./xdp_loader -d veth0 -F --progsec xdp_pass
286285
$ t exec -n dos -- ./xdp_loader -d veth0 -F --progsec xdp_pass
287286
$ t exec -n tres -- ./xdp_loader -d veth0 -F --progsec xdp_pass
288287

289-
$ sudo ./xdp_prog_user -d uno
290-
$ sudo ./xdp_prog_user -d dos
291-
$ sudo ./xdp_prog_user -d tres
292-
293288
$ sudo ./xdp_stats -d uno
294289
$ sudo ./xdp_stats -d dos
295290
$ sudo ./xdp_stats -d tres

packet03-redirecting/xdp_prog_kern.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -242,10 +242,10 @@ int xdp_router_func(struct xdp_md *ctx)
242242
ip6h->hop_limit--;
243243

244244
/* Assignment 4: fill in the eth destination and source
245-
* addresses and call the bpf_redirect_map function */
245+
* addresses and call the bpf_redirect function */
246246
/* memcpy(eth->h_dest, ???, ETH_ALEN); */
247247
/* memcpy(eth->h_source, ???, ETH_ALEN); */
248-
/* action = bpf_redirect_map(&tx_port, ???, 0); */
248+
/* action = bpf_redirect(???, 0); */
249249
break;
250250
case BPF_FIB_LKUP_RET_BLACKHOLE: /* dest is blackholed; can be dropped */
251251
case BPF_FIB_LKUP_RET_UNREACHABLE: /* dest is unreachable; can be dropped */

packet03-redirecting/xdp_prog_user.c

-2
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,6 @@ int main(int argc, char **argv)
143143
fprintf(stderr, "can't write iface params\n");
144144
return 1;
145145
}
146-
} else {
147-
/* Assignment 4: setup 1-1 mapping for the dynamic router */
148146
}
149147

150148
return EXIT_OK;

0 commit comments

Comments
 (0)