bpf: Add table ID to bpf_fib_lookup BPF helper
authorLouis DeLosSantos <louis.delos.devel@gmail.com>
Wed, 31 May 2023 19:38:48 +0000 (15:38 -0400)
committerDaniel Borkmann <daniel@iogearbox.net>
Thu, 1 Jun 2023 17:58:44 +0000 (19:58 +0200)
Add ability to specify routing table ID to the `bpf_fib_lookup` BPF
helper.

A new field `tbid` is added to `struct bpf_fib_lookup` used as
parameters to the `bpf_fib_lookup` BPF helper.

When the helper is called with the `BPF_FIB_LOOKUP_DIRECT` and
`BPF_FIB_LOOKUP_TBID` flags the `tbid` field in `struct bpf_fib_lookup`
will be used as the table ID for the fib lookup.

If the `tbid` does not exist the fib lookup will fail with
`BPF_FIB_LKUP_RET_NOT_FWDED`.

The `tbid` field becomes a union over the vlan related output fields
in `struct bpf_fib_lookup` and will be zeroed immediately after usage.

This functionality is useful in containerized environments.

For instance, if a CNI wants to dictate the next-hop for traffic leaving
a container it can create a container-specific routing table and perform
a fib lookup against this table in a "host-net-namespace-side" TC program.

This functionality also allows `ip rule` like functionality at the TC
layer, allowing an eBPF program to pick a routing table based on some
aspect of the sk_buff.

As a concrete use case, this feature will be used in Cilium's SRv6 L3VPN
datapath.

When egress traffic leaves a Pod an eBPF program attached by Cilium will
determine which VRF the egress traffic should target, and then perform a
FIB lookup in a specific table representing this VRF's FIB.

Signed-off-by: Louis DeLosSantos <louis.delos.devel@gmail.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Link: https://lore.kernel.org/bpf/20230505-bpf-add-tbid-fib-lookup-v2-1-0a31c22c748c@gmail.com
include/uapi/linux/bpf.h
net/core/filter.c
tools/include/uapi/linux/bpf.h

index 9273c654743ce519b14fc6daeb237ec450797bf4..a7b5e91dd768e7d5b716272488295f828aa9aa1c 100644 (file)
@@ -3177,6 +3177,10 @@ union bpf_attr {
  *             **BPF_FIB_LOOKUP_DIRECT**
  *                     Do a direct table lookup vs full lookup using FIB
  *                     rules.
+ *             **BPF_FIB_LOOKUP_TBID**
+ *                     Used with BPF_FIB_LOOKUP_DIRECT.
+ *                     Use the routing table ID present in *params*->tbid
+ *                     for the fib lookup.
  *             **BPF_FIB_LOOKUP_OUTPUT**
  *                     Perform lookup from an egress perspective (default is
  *                     ingress).
@@ -6831,6 +6835,7 @@ enum {
        BPF_FIB_LOOKUP_DIRECT  = (1U << 0),
        BPF_FIB_LOOKUP_OUTPUT  = (1U << 1),
        BPF_FIB_LOOKUP_SKIP_NEIGH = (1U << 2),
+       BPF_FIB_LOOKUP_TBID    = (1U << 3),
 };
 
 enum {
@@ -6891,9 +6896,19 @@ struct bpf_fib_lookup {
                __u32           ipv6_dst[4];  /* in6_addr; network order */
        };
 
-       /* output */
-       __be16  h_vlan_proto;
-       __be16  h_vlan_TCI;
+       union {
+               struct {
+                       /* output */
+                       __be16  h_vlan_proto;
+                       __be16  h_vlan_TCI;
+               };
+               /* input: when accompanied with the
+                * 'BPF_FIB_LOOKUP_DIRECT | BPF_FIB_LOOKUP_TBID` flags, a
+                * specific routing table to use for the fib lookup.
+                */
+               __u32   tbid;
+       };
+
        __u8    smac[6];     /* ETH_ALEN */
        __u8    dmac[6];     /* ETH_ALEN */
 };
index 968139f4a1acca6b8c8a8cb118af53f1cd94370b..d25d52854c21d26ca554a80c912cb3a2e9d38f83 100644 (file)
@@ -5803,6 +5803,12 @@ static int bpf_ipv4_fib_lookup(struct net *net, struct bpf_fib_lookup *params,
                u32 tbid = l3mdev_fib_table_rcu(dev) ? : RT_TABLE_MAIN;
                struct fib_table *tb;
 
+               if (flags & BPF_FIB_LOOKUP_TBID) {
+                       tbid = params->tbid;
+                       /* zero out for vlan output */
+                       params->tbid = 0;
+               }
+
                tb = fib_get_table(net, tbid);
                if (unlikely(!tb))
                        return BPF_FIB_LKUP_RET_NOT_FWDED;
@@ -5936,6 +5942,12 @@ static int bpf_ipv6_fib_lookup(struct net *net, struct bpf_fib_lookup *params,
                u32 tbid = l3mdev_fib_table_rcu(dev) ? : RT_TABLE_MAIN;
                struct fib6_table *tb;
 
+               if (flags & BPF_FIB_LOOKUP_TBID) {
+                       tbid = params->tbid;
+                       /* zero out for vlan output */
+                       params->tbid = 0;
+               }
+
                tb = ipv6_stub->fib6_get_table(net, tbid);
                if (unlikely(!tb))
                        return BPF_FIB_LKUP_RET_NOT_FWDED;
@@ -6008,7 +6020,7 @@ set_fwd_params:
 #endif
 
 #define BPF_FIB_LOOKUP_MASK (BPF_FIB_LOOKUP_DIRECT | BPF_FIB_LOOKUP_OUTPUT | \
-                            BPF_FIB_LOOKUP_SKIP_NEIGH)
+                            BPF_FIB_LOOKUP_SKIP_NEIGH | BPF_FIB_LOOKUP_TBID)
 
 BPF_CALL_4(bpf_xdp_fib_lookup, struct xdp_buff *, ctx,
           struct bpf_fib_lookup *, params, int, plen, u32, flags)
index 9273c654743ce519b14fc6daeb237ec450797bf4..a7b5e91dd768e7d5b716272488295f828aa9aa1c 100644 (file)
@@ -3177,6 +3177,10 @@ union bpf_attr {
  *             **BPF_FIB_LOOKUP_DIRECT**
  *                     Do a direct table lookup vs full lookup using FIB
  *                     rules.
+ *             **BPF_FIB_LOOKUP_TBID**
+ *                     Used with BPF_FIB_LOOKUP_DIRECT.
+ *                     Use the routing table ID present in *params*->tbid
+ *                     for the fib lookup.
  *             **BPF_FIB_LOOKUP_OUTPUT**
  *                     Perform lookup from an egress perspective (default is
  *                     ingress).
@@ -6831,6 +6835,7 @@ enum {
        BPF_FIB_LOOKUP_DIRECT  = (1U << 0),
        BPF_FIB_LOOKUP_OUTPUT  = (1U << 1),
        BPF_FIB_LOOKUP_SKIP_NEIGH = (1U << 2),
+       BPF_FIB_LOOKUP_TBID    = (1U << 3),
 };
 
 enum {
@@ -6891,9 +6896,19 @@ struct bpf_fib_lookup {
                __u32           ipv6_dst[4];  /* in6_addr; network order */
        };
 
-       /* output */
-       __be16  h_vlan_proto;
-       __be16  h_vlan_TCI;
+       union {
+               struct {
+                       /* output */
+                       __be16  h_vlan_proto;
+                       __be16  h_vlan_TCI;
+               };
+               /* input: when accompanied with the
+                * 'BPF_FIB_LOOKUP_DIRECT | BPF_FIB_LOOKUP_TBID` flags, a
+                * specific routing table to use for the fib lookup.
+                */
+               __u32   tbid;
+       };
+
        __u8    smac[6];     /* ETH_ALEN */
        __u8    dmac[6];     /* ETH_ALEN */
 };