Skip to content

Commit 09c8bb1

Browse files
bastien-curutchetMartin KaFai Lau
authored andcommitted
selftests/bpf: Optionally select broadcasting flags
Broadcasting flags are hardcoded for each kind for protocol. Create a redirect_flags map that allows to select the broadcasting flags to use in the bpf_redirect_map(). The protocol ID is used as a key. Set the old hardcoded values as default if the map isn't filled by the BPF caller. Signed-off-by: Bastien Curutchet (eBPF Foundation) <[email protected]> Signed-off-by: Martin KaFai Lau <[email protected]> Acked-by: Stanislav Fomichev <[email protected]> Link: https://patch.msgid.link/[email protected]
1 parent 19a9484 commit 09c8bb1

File tree

1 file changed

+29
-12
lines changed

1 file changed

+29
-12
lines changed

tools/testing/selftests/bpf/progs/xdp_redirect_multi_kern.c

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,32 +34,49 @@ struct {
3434
__uint(max_entries, 128);
3535
} mac_map SEC(".maps");
3636

37+
/* map to store redirect flags for each protocol*/
38+
struct {
39+
__uint(type, BPF_MAP_TYPE_HASH);
40+
__type(key, __u16);
41+
__type(value, __u64);
42+
__uint(max_entries, 16);
43+
} redirect_flags SEC(".maps");
44+
3745
SEC("xdp")
3846
int xdp_redirect_map_multi_prog(struct xdp_md *ctx)
3947
{
4048
void *data_end = (void *)(long)ctx->data_end;
4149
void *data = (void *)(long)ctx->data;
4250
int if_index = ctx->ingress_ifindex;
4351
struct ethhdr *eth = data;
52+
__u64 *flags_from_map;
4453
__u16 h_proto;
4554
__u64 nh_off;
55+
__u64 flags;
4656

4757
nh_off = sizeof(*eth);
4858
if (data + nh_off > data_end)
4959
return XDP_DROP;
5060

51-
h_proto = eth->h_proto;
52-
53-
/* Using IPv4 for (BPF_F_BROADCAST | BPF_F_EXCLUDE_INGRESS) testing */
54-
if (h_proto == bpf_htons(ETH_P_IP))
55-
return bpf_redirect_map(&map_all, 0,
56-
BPF_F_BROADCAST | BPF_F_EXCLUDE_INGRESS);
57-
/* Using IPv6 for none flag testing */
58-
else if (h_proto == bpf_htons(ETH_P_IPV6))
59-
return bpf_redirect_map(&map_all, if_index, 0);
60-
/* All others for BPF_F_BROADCAST testing */
61-
else
62-
return bpf_redirect_map(&map_all, 0, BPF_F_BROADCAST);
61+
h_proto = bpf_htons(eth->h_proto);
62+
63+
flags_from_map = bpf_map_lookup_elem(&redirect_flags, &h_proto);
64+
65+
/* Default flags for IPv4 : (BPF_F_BROADCAST | BPF_F_EXCLUDE_INGRESS) */
66+
if (h_proto == ETH_P_IP) {
67+
flags = flags_from_map ? *flags_from_map : BPF_F_BROADCAST | BPF_F_EXCLUDE_INGRESS;
68+
return bpf_redirect_map(&map_all, 0, flags);
69+
}
70+
/* Default flags for IPv6 : 0 */
71+
if (h_proto == ETH_P_IPV6) {
72+
flags = flags_from_map ? *flags_from_map : 0;
73+
return bpf_redirect_map(&map_all, if_index, flags);
74+
}
75+
/* Default flags for others BPF_F_BROADCAST : 0 */
76+
else {
77+
flags = flags_from_map ? *flags_from_map : BPF_F_BROADCAST;
78+
return bpf_redirect_map(&map_all, 0, flags);
79+
}
6380
}
6481

6582
/* The following 2 progs are for 2nd devmap prog testing */

0 commit comments

Comments
 (0)