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.

Acked-by: Stanislav Fomichev <s...@fomichev.me>
Signed-off-by: Bastien Curutchet (eBPF Foundation) 
<bastien.curutc...@bootlin.com>
---
 .../selftests/bpf/progs/xdp_redirect_multi_kern.c  | 41 +++++++++++++++-------
 1 file changed, 29 insertions(+), 12 deletions(-)

diff --git a/tools/testing/selftests/bpf/progs/xdp_redirect_multi_kern.c 
b/tools/testing/selftests/bpf/progs/xdp_redirect_multi_kern.c
index 
97b26a30b59a758f5a5f2152af509acef80031ce..bc2945ed8a8017021c2792671b4de9aa4928a3e4
 100644
--- a/tools/testing/selftests/bpf/progs/xdp_redirect_multi_kern.c
+++ b/tools/testing/selftests/bpf/progs/xdp_redirect_multi_kern.c
@@ -34,6 +34,14 @@ struct {
        __uint(max_entries, 128);
 } mac_map SEC(".maps");
 
+/* map to store redirect flags for each protocol*/
+struct {
+       __uint(type, BPF_MAP_TYPE_HASH);
+       __type(key, __u16);
+       __type(value, __u64);
+       __uint(max_entries, 16);
+} redirect_flags SEC(".maps");
+
 SEC("xdp")
 int xdp_redirect_map_multi_prog(struct xdp_md *ctx)
 {
@@ -41,25 +49,34 @@ int xdp_redirect_map_multi_prog(struct xdp_md *ctx)
        void *data = (void *)(long)ctx->data;
        int if_index = ctx->ingress_ifindex;
        struct ethhdr *eth = data;
+       __u64 *flags_from_map;
        __u16 h_proto;
        __u64 nh_off;
+       __u64 flags;
 
        nh_off = sizeof(*eth);
        if (data + nh_off > data_end)
                return XDP_DROP;
 
-       h_proto = eth->h_proto;
-
-       /* Using IPv4 for (BPF_F_BROADCAST | BPF_F_EXCLUDE_INGRESS) testing */
-       if (h_proto == bpf_htons(ETH_P_IP))
-               return bpf_redirect_map(&map_all, 0,
-                                       BPF_F_BROADCAST | 
BPF_F_EXCLUDE_INGRESS);
-       /* Using IPv6 for none flag testing */
-       else if (h_proto == bpf_htons(ETH_P_IPV6))
-               return bpf_redirect_map(&map_all, if_index, 0);
-       /* All others for BPF_F_BROADCAST testing */
-       else
-               return bpf_redirect_map(&map_all, 0, BPF_F_BROADCAST);
+       h_proto = bpf_htons(eth->h_proto);
+
+       flags_from_map = bpf_map_lookup_elem(&redirect_flags, &h_proto);
+
+       /* Default flags for IPv4 : (BPF_F_BROADCAST | BPF_F_EXCLUDE_INGRESS) */
+       if (h_proto == ETH_P_IP) {
+               flags = flags_from_map ? *flags_from_map : BPF_F_BROADCAST | 
BPF_F_EXCLUDE_INGRESS;
+               return bpf_redirect_map(&map_all, 0, flags);
+       }
+       /* Default flags for IPv6 : 0 */
+       if (h_proto == ETH_P_IPV6) {
+               flags = flags_from_map ? *flags_from_map : 0;
+               return bpf_redirect_map(&map_all, if_index, flags);
+       }
+       /* Default flags for others BPF_F_BROADCAST : 0 */
+       else {
+               flags = flags_from_map ? *flags_from_map : BPF_F_BROADCAST;
+               return bpf_redirect_map(&map_all, 0, flags);
+       }
 }
 
 /* The following 2 progs are for 2nd devmap prog testing */

-- 
2.48.1


Reply via email to