bpf: bidirectional VLAN support for bpf_fib_lookup()
Subsystems (6)
v5 cover bpf: bidirectional VLAN support for bpf_fib_lookup()
This series adds VLAN awareness to bpf_fib_lookup() in both directions. BPF_FIB_LOOKUP_VLAN resolves a VLAN egress to its underlying real device plus the VLAN tag (XDP programs need this because VLAN devices have no XDP xmit), and BPF_FIB_LOOKUP_VLAN_INPUT runs the lookup as if a tagged frame had arrived on the matching VLAN subinterface, for iif policy routing and VRF table selection.
The independent l3mdev/VRF flow-init fix, patch 1 in v1 and v2, was split out and merged to bpf separately.
An unreducible VLAN egress (a QinQ egress, or a parent in another namespace) returns BPF_FIB_LKUP_RET_VLAN_FAILURE rather than a best-effort SUCCESS, so an XDP program cannot mistake it for a physical egress and silently blackhole the frame at xdp_do_flush(). The code is appended after BPF_FIB_LKUP_RET_NO_SRC_ADDR (nothing renumbered, tools/ mirror updated) and is returned only when BPF_FIB_LOOKUP_VLAN is set, so no existing caller can observe it. On that failure params->ifindex is left at the input; a program that wants the VLAN device's own ifindex re-issues without the flag.
Changes v4 -> v5 (Toke's review,
https://lore.kernel.org/bpf/[email protected]/):
- Patch 1: BPF_FIB_LOOKUP_VLAN only makes sense for XDP, which cannot redirect to a VLAN device; a tc program can redirect to the VLAN device directly. So bpf_skb_fib_lookup() now rejects the flag with -EINVAL, and the fwd_dev out-parameter added in v4 is dropped: with the flag gone from the skb path there is no swap to preserve, so the deferred mtu check returns to the original dev_get_by_index_rcu(net, params->ifindex). The VLAN_FAILURE rewind moves into bpf_fib_set_fwd_params() via an input ifindex parameter, so each lookup ends in a plain
"return bpf_fib_set_fwd_params(...)". The early params->ifindex = dev->ifindex that NO_NEIGH and NO_SRC_ADDR report stays where d1c362e1dd68a ("bpf: Always return target ifindex in bpf_fib_lookup") put it. Dropping fwd_dev also removes the i386 W=1 unused-variable warning the kernel test robot reported, since net is used again.
- Patch 2: no code change; add Toke's Reviewed-by.
- Patch 3: the BPF_FIB_LOOKUP_VLAN cases assert the tc helper returns -EINVAL and check the egress result on the XDP path, including dmac and (for tot_len cases) the route mtu_result; the cross-netns egress case runs through bpf_xdp_fib_lookup(); the obsolete skb-mtu-after-swap arm is dropped.
Changes v3 -> v4:
- Patch 1: return BPF_FIB_LKUP_RET_VLAN_FAILURE for an unreducible VLAN egress, leaving params->ifindex at the input, per Toke's v3 review.
- Patch 3: QinQ-egress and cross-namespace-egress arms expect VLAN_FAILURE; an escape-hatch arm re-issues without the flag; and a live-frames arm asserts a reducible egress is delivered and a QinQ egress is passed to the stack.
Taking the tag as lookup input follows the approach David Ahern suggested in the 2021 fwmark discussion:
https://lore.kernel.org/bpf/[email protected]/
v4: https://lore.kernel.org/all/[email protected]/
v3: https://lore.kernel.org/all/[email protected]/
v2: https://lore.kernel.org/all/[email protected]/
v1: https://lore.kernel.org/all/[email protected]/
Avinash Duduskar (3):
bpf: Add BPF_FIB_LOOKUP_VLAN flag to bpf_fib_lookup() helper
bpf: Add BPF_FIB_LOOKUP_VLAN_INPUT flag to bpf_fib_lookup() helper selftests/bpf: Add bpf_fib_lookup() VLAN flag tests
include/uapi/linux/bpf.h | 50 +- net/core/filter.c | 97 ++- tools/include/uapi/linux/bpf.h | 50 +- .../selftests/bpf/prog_tests/fib_lookup.c | 717 +++++++++++++++++- .../testing/selftests/bpf/progs/fib_lookup.c | 36 + 5 files changed, 936 insertions(+), 14 deletions(-)
base-commit: a975094bf98ca97be9146f9d3b5681a6f9cf5ce3
Signature · 1 line
2.54.0
PATCH v5 1/3 bpf: Add BPF_FIB_LOOKUP_VLAN flag to bpf_fib_lookup() helper
bpf_fib_lookup() returns the FIB-resolved egress ifindex straight from the fib result. When the egress is a VLAN device, the returned ifindex is the VLAN netdev's, which has no XDP xmit handler; XDP programs that want to forward the frame (e.g. xdp-forward) must instead target the underlying physical device and push the VLAN tag themselves. Today the program has no way to learn either the underlying ifindex or the VLAN tag without maintaining its own VLAN-to-ifindex map in userspace and refreshing it on netlink events.
Add BPF_FIB_LOOKUP_VLAN. When the caller sets this flag and the fib result is a VLAN device whose immediate parent is a real (non-VLAN) device in the same network namespace, populate the existing output fields params->h_vlan_proto and params->h_vlan_TCI from the VLAN device and replace params->ifindex with the parent's ifindex. params->h_vlan_TCI carries the VID only, with PCP and DEI bits zero; a consumer wanting to set egress priority writes PCP itself. params->smac is the VLAN device's own address, which can differ from the parent's.
Only the immediate parent is resolved, via vlan_dev_priv(dev)->real_dev and not vlan_dev_real_dev(), which walks to the bottom of a stack. When the immediate parent is not a real device in the same namespace, the lookup returns BPF_FIB_LKUP_RET_VLAN_FAILURE and leaves params->ifindex at the input. This covers a stacked VLAN (QinQ), where the immediate parent is itself a VLAN device and one h_vlan_proto/h_vlan_TCI pair cannot describe two tags, and a parent in another network namespace (a VLAN device can be moved while its parent stays), whose ifindex would be meaningless in the caller's namespace. A program that wants the VLAN device's own ifindex re-issues the lookup without BPF_FIB_LOOKUP_VLAN, so the unreducible case stays distinct from a physical egress. That distinction matters for XDP: a program cannot xmit on a VLAN device, so a success carrying the VLAN ifindex would make it redirect to a device with no ndo_xdp_xmit and drop the frame at xdp_do_flush(). The swap and the vlan fields are written only on the reduce path; other output fields keep their existing behaviour, so a frag-needed result still reports the route mtu in params->mtu_result.
BPF_FIB_LOOKUP_VLAN is only useful to XDP, which cannot redirect to a VLAN device. A tc program can redirect to the VLAN device directly, so bpf_skb_fib_lookup() rejects the flag with -EINVAL; bpf_xdp_fib_lookup() accepts it. When the flag is not set, behaviour is unchanged:h_vlan_proto and h_vlan_TCI are zeroed and ifindex is left at the FIB result.
The new block is compiled only under CONFIG_VLAN_8021Q since vlan_dev_priv() is not defined otherwise; without that config is_vlan_dev() is constant false and the flag is accepted but never acts. That is safe because no VLAN device can exist there, so every egress is already physical.
This lets an XDP redirect target the physical device and learn the tag to push in a single lookup, which xdp-forward's optional VLAN mode (xdp-project/xdp-tools#504) wants from the kernel side.
The helper's input semantics are unchanged; the reverse direction (supplying a tag as lookup input) is added in the following patch.
--- include/uapi/linux/bpf.h | 31 ++++++++++++++++++++++++++++++- net/core/filter.c | 33 +++++++++++++++++++++++++++++---- tools/include/uapi/linux/bpf.h | 31 ++++++++++++++++++++++++++++++- 3 files changed, 89 insertions(+), 6 deletions(-)
include/uapi/linux/bpf.h +30 -1
@@ -3532,6 +3532,29 @@ union bpf_attr {* Use the mark present in *params*->mark for the fib lookup. * This option should not be used with BPF_FIB_LOOKUP_DIRECT, * as it only has meaning for full lookups. +* **BPF_FIB_LOOKUP_VLAN** +* If the fib lookup resolves to a VLAN device whose +* parent is a real (non-VLAN) device, set +* *params*->h_vlan_proto and *params*->h_vlan_TCI from +* the VLAN device and replace *params*->ifindex with the +* parent's ifindex. *params*->h_vlan_TCI carries the VID +* only, with PCP and DEI bits zero; a consumer wanting to +* set egress priority writes PCP itself. *params*->smac is +* the VLAN device's own address, which can differ from the +* parent's. Only the immediate parent is resolved; if it +* is itself a VLAN device (QinQ) or in another namespace, +* the egress cannot be reduced to a physical device plus +* one tag and the lookup returns +* **BPF_FIB_LKUP_RET_VLAN_FAILURE** with *params*->ifindex +* left at the input. Re-issue without +* **BPF_FIB_LOOKUP_VLAN** to obtain the VLAN device's own +* ifindex. The swap and the vlan fields +* are written only on success; other output fields keep +* the helper's existing behaviour, so a frag-needed result +* still reports the route mtu in *params*->mtu_result. +* This flag is only valid for XDP programs; tc programs +* receive -EINVAL since they can redirect to the VLAN +* device directly. * * *ctx* is either **struct xdp_md** for XDP programs or * **struct sk_buff** tc cls_act programs.
@@ -7327,6 +7350,7 @@ enum {BPF_FIB_LOOKUP_TBID = (1U <<3 ),BPF_FIB_LOOKUP_SRC = (1U <<4 ),BPF_FIB_LOOKUP_MARK = (1U <<5 ), + BPF_FIB_LOOKUP_VLAN = (1U << 6), }; enum {
@@ -7340,6 +7364,7 @@ enum {BPF_FIB_LKUP_RET_NO_NEIGH ,/* no neighbor entry for nh */ BPF_FIB_LKUP_RET_FRAG_NEEDED ,/* fragmentation required to fwd */ BPF_FIB_LKUP_RET_NO_SRC_ADDR ,/* failed to derive IP src addr */ + BPF_FIB_LKUP_RET_VLAN_FAILURE, /* VLAN egress, parent unresolvable */ }; struct bpf_fib_lookup {
@@ -7393,7 +7418,11 @@ struct bpf_fib_lookup {union {struct { -/* output */ +/* +* output with BPF_FIB_LOOKUP_VLAN: set from the +* resolved egress VLAN device (see the flag); zeroed +* on other successful lookups. +*/ __be16 h_vlan_proto;__be16 h_vlan_TCI; };
net/core/filter.c +29 -4
@@ -6201,10 +6201,29 @@ static const struct bpf_func_proto bpf_skb_get_xfrm_state_proto = {#endif #if IS_ENABLED (CONFIG_INET ) ||IS_ENABLED (CONFIG_IPV6 ) -static int bpf_fib_set_fwd_params (struct bpf_fib_lookup *params,u32 mtu) +static int bpf_fib_set_fwd_params (struct net_device *dev, +struct bpf_fib_lookup *params, +u32 flags,u32 mtu,u32 in_ifindex) { params->h_vlan_TCI =0 ; params->h_vlan_proto =0 ; + +#if IS_ENABLED (CONFIG_VLAN_8021Q ) +if ((flags &BPF_FIB_LOOKUP_VLAN ) &&is_vlan_dev (dev)) { +struct net_device *real_dev =vlan_dev_priv (dev)->real_dev; + +if (!is_vlan_dev (real_dev) && +net_eq (dev_net (real_dev),dev_net (dev))) { + params->h_vlan_proto =vlan_dev_vlan_proto (dev); + params->h_vlan_TCI =htons (vlan_dev_vlan_id (dev)); + params->ifindex = real_dev->ifindex; + }else { + params->ifindex = in_ifindex; +return BPF_FIB_LKUP_RET_VLAN_FAILURE ; + } + } +#endif +if (mtu) params->mtu_result = mtu;/* union with tot_len */
@@ -6216,6 +6235,7 @@ static int bpf_fib_set_fwd_params(struct bpf_fib_lookup *params, u32 mtu)static int bpf_ipv4_fib_lookup (struct net *net,struct bpf_fib_lookup *params,u32 flags,bool check_mtu) { +u32 in_ifindex = params->ifindex;struct neighbour *neigh =NULL ;struct fib_nh_common *nhc;struct in_device *in_dev;
@@ -6347,7 +6367,7 @@ static int bpf_ipv4_fib_lookup(struct net *net, struct bpf_fib_lookup *params,memcpy (params->smac, dev->dev_addr,ETH_ALEN ); set_fwd_params: - return bpf_fib_set_fwd_params(params, mtu); + return bpf_fib_set_fwd_params(dev, params, flags, mtu, in_ifindex); }#endif
@@ -6357,6 +6377,7 @@ static int bpf_ipv6_fib_lookup(struct net *net, struct bpf_fib_lookup *params, {struct in6_addr *src = (struct in6_addr *) params->ipv6_src;struct in6_addr *dst = (struct in6_addr *) params->ipv6_dst; +u32 in_ifindex = params->ifindex;struct fib6_result res = {};struct neighbour *neigh;struct net_device *dev;
@@ -6486,13 +6507,14 @@ static int bpf_ipv6_fib_lookup(struct net *net, struct bpf_fib_lookup *params,memcpy (params->smac, dev->dev_addr,ETH_ALEN ); set_fwd_params: -return bpf_fib_set_fwd_params (params, mtu); +return bpf_fib_set_fwd_params (dev, params, flags, mtu, in_ifindex); }#endif #define BPF_FIB_LOOKUP_MASK (BPF_FIB_LOOKUP_DIRECT | BPF_FIB_LOOKUP_OUTPUT | \ BPF_FIB_LOOKUP_SKIP_NEIGH | BPF_FIB_LOOKUP_TBID | \ - BPF_FIB_LOOKUP_SRC | BPF_FIB_LOOKUP_MARK) + BPF_FIB_LOOKUP_SRC | BPF_FIB_LOOKUP_MARK | \ + BPF_FIB_LOOKUP_VLAN) BPF_CALL_4(bpf_xdp_fib_lookup, struct xdp_buff *, ctx, struct bpf_fib_lookup *, params, int, plen, u32, flags)
@@ -6541,6 +6563,9 @@ BPF_CALL_4(bpf_skb_fib_lookup, struct sk_buff *, skb,if (flags & ~BPF_FIB_LOOKUP_MASK )return -EINVAL ; +if (flags &BPF_FIB_LOOKUP_VLAN ) +return -EINVAL ; +if (params->tot_len) check_mtu = true;
tools/include/uapi/linux/bpf.h +30 -1
@@ -3532,6 +3532,29 @@ union bpf_attr {* Use the mark present in *params*->mark for the fib lookup. * This option should not be used with BPF_FIB_LOOKUP_DIRECT, * as it only has meaning for full lookups. +* **BPF_FIB_LOOKUP_VLAN** +* If the fib lookup resolves to a VLAN device whose +* parent is a real (non-VLAN) device, set +* *params*->h_vlan_proto and *params*->h_vlan_TCI from +* the VLAN device and replace *params*->ifindex with the +* parent's ifindex. *params*->h_vlan_TCI carries the VID +* only, with PCP and DEI bits zero; a consumer wanting to +* set egress priority writes PCP itself. *params*->smac is +* the VLAN device's own address, which can differ from the +* parent's. Only the immediate parent is resolved; if it +* is itself a VLAN device (QinQ) or in another namespace, +* the egress cannot be reduced to a physical device plus +* one tag and the lookup returns +* **BPF_FIB_LKUP_RET_VLAN_FAILURE** with *params*->ifindex +* left at the input. Re-issue without +* **BPF_FIB_LOOKUP_VLAN** to obtain the VLAN device's own +* ifindex. The swap and the vlan fields +* are written only on success; other output fields keep +* the helper's existing behaviour, so a frag-needed result +* still reports the route mtu in *params*->mtu_result. +* This flag is only valid for XDP programs; tc programs +* receive -EINVAL since they can redirect to the VLAN +* device directly. * * *ctx* is either **struct xdp_md** for XDP programs or * **struct sk_buff** tc cls_act programs.
@@ -7327,6 +7350,7 @@ enum {BPF_FIB_LOOKUP_TBID = (1U <<3 ),BPF_FIB_LOOKUP_SRC = (1U <<4 ),BPF_FIB_LOOKUP_MARK = (1U <<5 ), + BPF_FIB_LOOKUP_VLAN = (1U << 6), }; enum {
@@ -7340,6 +7364,7 @@ enum {BPF_FIB_LKUP_RET_NO_NEIGH ,/* no neighbor entry for nh */ BPF_FIB_LKUP_RET_FRAG_NEEDED ,/* fragmentation required to fwd */ BPF_FIB_LKUP_RET_NO_SRC_ADDR ,/* failed to derive IP src addr */ + BPF_FIB_LKUP_RET_VLAN_FAILURE, /* VLAN egress, parent unresolvable */ }; struct bpf_fib_lookup {
@@ -7393,7 +7418,11 @@ struct bpf_fib_lookup {union {struct { -/* output */ +/* +* output with BPF_FIB_LOOKUP_VLAN: set from the +* resolved egress VLAN device (see the flag); zeroed +* on other successful lookups. +*/ __be16 h_vlan_proto;__be16 h_vlan_TCI; };
Signature · 1 line
2.54.0
Re: [PATCH bpf-next v5 1/3] bpf: Add BPF_FIB_LOOKUP_VLAN flag to bpf_fib_lookup() helper
Quoted 55 lines · Avinash Duduskar
bpf_fib_lookup()returns the FIB-resolved egress ifindex straight from the fib result. When the egress is a VLAN device, the returned ifindex is the VLAN netdev's, which has no XDP xmit handler; XDP programs that want to forward the frame (e.g. xdp-forward) must instead target the underlying physical device and push the VLAN tag themselves. Today the program has no way to learn either the underlying ifindex or the VLAN tag without maintaining its own VLAN-to-ifindex map in userspace and refreshing it on netlink events.Add
BPF_FIB_LOOKUP_VLAN. When the caller sets this flag and the fib result is a VLAN device whose immediate parent is a real (non-VLAN) device in the same network namespace, populate the existing output fieldsparams->h_vlan_protoandparams->h_vlan_TCIfrom the VLAN device and replaceparams->ifindexwith the parent's ifindex.params->h_vlan_TCIcarries the VID only, with PCP and DEI bits zero; a consumer wanting to set egress priority writes PCP itself.params->smacis the VLAN device's own address, which can differ from the parent's.Only the immediate parent is resolved, via vlan_dev_priv(dev)->real_dev and not
vlan_dev_real_dev(), which walks to the bottom of a stack. When the immediate parent is not a real device in the same namespace, the lookup returnsBPF_FIB_LKUP_RET_VLAN_FAILUREand leavesparams->ifindexat the input. This covers a stacked VLAN (QinQ), where the immediate parent is itself a VLAN device and one h_vlan_proto/h_vlan_TCI pair cannot describe two tags, and a parent in another network namespace (a VLAN device can be moved while its parent stays), whose ifindex would be meaningless in the caller's namespace. A program that wants the VLAN device's own ifindex re-issues the lookup withoutBPF_FIB_LOOKUP_VLAN, so the unreducible case stays distinct from a physical egress. That distinction matters for XDP: a program cannot xmit on a VLAN device, so a success carrying the VLAN ifindex would make it redirect to a device with nondo_xdp_xmitand drop the frame atxdp_do_flush(). The swap and the vlan fields are written only on the reduce path; other output fields keep their existing behaviour, so a frag-needed result still reports the route mtu inparams->mtu_result.
BPF_FIB_LOOKUP_VLANis only useful to XDP, which cannot redirect to a VLAN device. A tc program can redirect to the VLAN device directly, sobpf_skb_fib_lookup()rejects the flag with-EINVAL;bpf_xdp_fib_lookup()accepts it. When the flag is not set, behaviour is unchanged:h_vlan_protoandh_vlan_TCIare zeroed and ifindex is left at the FIB result.The new block is compiled only under
CONFIG_VLAN_8021Qsincevlan_dev_priv()is not defined otherwise; without that configis_vlan_dev()is constant false and the flag is accepted but never acts. That is safe because no VLAN device can exist there, so every egress is already physical.This lets an XDP redirect target the physical device and learn the tag to push in a single lookup, which xdp-forward's optional VLAN mode (xdp-project/xdp-tools#504) wants from the kernel side.
Quoted 5 lines
The helper's input semantics are unchanged; the reverse direction (supplying a tag as lookup input) is added in the following patch.
Suggested-by Toke Høiland-Jørgensen <[email protected]>Signed-off-by Avinash Duduskar <[email protected]>
Yes, this is way nicer - thanks! One nit below, otherwise LGTM:
[..]
net/core/filter.c BPF_CALL_4(bpf_skb_fib_lookup, struct sk_buff *, skb, · line 6568
Quoted 3 lines
net/core/filter.c +3
-0+if (flags &BPF_FIB_LOOKUP_VLAN ) +return -EINVAL ; +
This is fine, but we should probably reject the input flag as well in the next patch (for symmetry).
-Toke
Re: [PATCH bpf-next v5 1/3] bpf: Add BPF_FIB_LOOKUP_VLAN flag to bpf_fib_lookup() helper
Quoted 6 lines
net/core/filter.c BPF_CALL_4(bpf_skb_fib_lookup, struct sk_buff *, skb, · line 6568
Quoted 3 lines
net/core/filter.c +3
-0+if (flags &BPF_FIB_LOOKUP_VLAN ) +return -EINVAL ; +This is fine, but we should probably reject the input flag as well in the next patch (for symmetry).
I dug into this and I don't think the two are symmetric. The egress reject is right for exactly the reason you gave: in tc you can redirect to the VLAN device directly, so reducing the egress to the physical parent is only needed for XDP. But that is a transmit argument, and VLAN_INPUT never touches the egress or redirect side. It only sets the lookup's ingress (flowi_iif), which picks the iif policy rule and the VRF table, and there is no XDP-only constraint there for the symmetry to mirror.
tc also has a real user for it. In __netif_receive_skb_core() the tcx ingress hook runs before vlan_do_receive() demuxes the frame, so a clsact program on the physical port sees a tagged frame with skb->dev still the physical device and the tag in skb->vlan_tci. That is exactly the physical-ifindex-plus-tag input VLAN_INPUT takes, and it wants the subinterface-scoped answer. The 2/3 selftest already runs the VLAN_INPUT cases on the tc path, including the VRF-table-selection ones, and they pass, so this isn't a theoretical tc path.
So I would keep VLAN_INPUT allowed on both. If you would rather hold a uniform "both VLAN flags are XDP-only" line under the same restrict-now-relax-later rule as the TBID and OUTPUT combos, I will add the reject, but unlike the egress case it removes a working tc path rather than one tc cannot use. Happy either way, just let me know.
Thanks for the review.
-Avinash
Re: [PATCH bpf-next v5 1/3] bpf: Add BPF_FIB_LOOKUP_VLAN flag to bpf_fib_lookup() helper
Quoted 25 lines · Avinash Duduskar
Quoted 6 lines
net/core/filter.c BPF_CALL_4(bpf_skb_fib_lookup, struct sk_buff *, skb, · line 6568
Quoted 3 lines
net/core/filter.c +3
-0+if (flags &BPF_FIB_LOOKUP_VLAN ) +return -EINVAL ; +This is fine, but we should probably reject the input flag as well in the next patch (for symmetry).
I dug into this and I don't think the two are symmetric. The egress reject is right for exactly the reason you gave: in tc you can redirect to the VLAN device directly, so reducing the egress to the physical parent is only needed for XDP. But that is a transmit argument, and
VLAN_INPUTnever touches the egress or redirect side. It only sets the lookup's ingress (flowi_iif), which picks the iif policy rule and the VRF table, and there is no XDP-only constraint there for the symmetry to mirror.tc also has a real user for it. In
__netif_receive_skb_core()the tcx ingress hook runs beforevlan_do_receive()demuxes the frame, so a clsact program on the physical port sees a tagged frame withskb->devstill the physical device and the tag inskb->vlan_tci. That is exactly the physical-ifindex-plus-tag inputVLAN_INPUTtakes, and it wants the subinterface-scoped answer. The 2/3 selftest already runs theVLAN_INPUTcases on the tc path, including the VRF-table-selection ones, and they pass, so this isn't a theoretical tc path.
Quoted 5 lines
So I would keep
VLAN_INPUTallowed on both. If you would rather hold a uniform "both VLAN flags are XDP-only" line under the same restrict-now-relax-later rule as the TBID and OUTPUT combos, I will add the reject, but unlike the egress case it removes a working tc path rather than one tc cannot use. Happy either way, just let me know.
Hmm, OK, I'm fine with keeping it available for TC then.
-Toke
Re: [PATCH bpf-next v5 1/3] bpf: Add BPF_FIB_LOOKUP_VLAN flag to bpf_fib_lookup() helper
In reply to Avinash Duduskarinclude/uapi/linux/bpf.h union bpf_attr { · line 3535
Quoted 9 lines · Avinash Duduskar
include/uapi/linux/bpf.h +1
-0@@ -3532,6 +3532,29 @@ union bpf_attr {* Use the mark present in *params*->mark for the fib lookup. * This option should not be used with BPF_FIB_LOOKUP_DIRECT, * as it only has meaning for full lookups. +* **BPF_FIB_LOOKUP_VLAN**
This flag should not be needed. Patches for vlan support were never submitted (I have them in some old branch). Since the vlan params are initialized to 0, no new flag should be needed. Besides, these are output parameters.
Quoted 22 lines
include/uapi/linux/bpf.h +22
-0+* If the fib lookup resolves to a VLAN device whose +* parent is a real (non-VLAN) device, set +* *params*->h_vlan_proto and *params*->h_vlan_TCI from +* the VLAN device and replace *params*->ifindex with the +* parent's ifindex. *params*->h_vlan_TCI carries the VID +* only, with PCP and DEI bits zero; a consumer wanting to +* set egress priority writes PCP itself. *params*->smac is +* the VLAN device's own address, which can differ from the +* parent's. Only the immediate parent is resolved; if it +* is itself a VLAN device (QinQ) or in another namespace, +* the egress cannot be reduced to a physical device plus +* one tag and the lookup returns +* **BPF_FIB_LKUP_RET_VLAN_FAILURE** with *params*->ifindex +* left at the input. Re-issue without +* **BPF_FIB_LOOKUP_VLAN** to obtain the VLAN device's own +* ifindex. The swap and the vlan fields +* are written only on success; other output fields keep +* the helper's existing behaviour, so a frag-needed result +* still reports the route mtu in *params*->mtu_result. +* This flag is only valid for XDP programs; tc programs +* receive -EINVAL since they can redirect to the VLAN +* device directly.
Re: [PATCH bpf-next v5 1/3] bpf: Add BPF_FIB_LOOKUP_VLAN flag to bpf_fib_lookup() helper
Quoted 15 lines · David Ahern
include/uapi/linux/bpf.h union bpf_attr { · line 3535
Quoted 9 lines · Avinash Duduskar
include/uapi/linux/bpf.h +1
-0@@ -3532,6 +3532,29 @@ union bpf_attr {* Use the mark present in *params*->mark for the fib lookup. * This option should not be used with BPF_FIB_LOOKUP_DIRECT, * as it only has meaning for full lookups. +* **BPF_FIB_LOOKUP_VLAN** This flag should not be needed. Patches for vlan support were never submitted (I have them in some old branch). Since the vlan params are initialized to 0, no new flag should be needed. Besides, these are output parameters.
There's no enforcement from the kernel side of the parameters being zero, though? So we do need the flag for feature detection; unless we expect applications to do that out of band? But then we'd need a mechanism to do that which could be... the presence of the flag in the ENUM (and thus in BTF)? :)
-Toke
Re: [PATCH bpf-next v5 1/3] bpf: Add BPF_FIB_LOOKUP_VLAN flag to bpf_fib_lookup() helper
Quoted 18 lines · Toke Høiland-Jørgensen
Quoted 15 lines · David Ahern
include/uapi/linux/bpf.h union bpf_attr { · line 3535
Quoted 9 lines · Avinash Duduskar
include/uapi/linux/bpf.h +1
-0@@ -3532,6 +3532,29 @@ union bpf_attr {* Use the mark present in *params*->mark for the fib lookup. * This option should not be used with BPF_FIB_LOOKUP_DIRECT, * as it only has meaning for full lookups. +* **BPF_FIB_LOOKUP_VLAN** This flag should not be needed. Patches for vlan support were never submitted (I have them in some old branch). Since the vlan params are initialized to 0, no new flag should be needed. Besides, these are output parameters.
Quoted 6 lines
There's no enforcement from the kernel side of the parameters being zero, though? So we do need the flag for feature detection; unless we expect applications to do that out of band? But then we'd need a mechanism to do that which could be... the presence of the flag in the ENUM (and thus in BTF)? :)
This is output direction - return from the fib lookup. It does not make sense to require a flag to get lookup output. vlan proto of 0 is not valid, so it is a clear indication that the vlan output parameters were not set during the lookup.
Re: [PATCH bpf-next v5 1/3] bpf: Add BPF_FIB_LOOKUP_VLAN flag to bpf_fib_lookup() helper
Quoted 19 lines · David Ahern
Quoted 18 lines · Toke Høiland-Jørgensen
Quoted 15 lines · David Ahern
include/uapi/linux/bpf.h union bpf_attr { · line 3535
Quoted 9 lines · Avinash Duduskar
include/uapi/linux/bpf.h +1
-0@@ -3532,6 +3532,29 @@ union bpf_attr {* Use the mark present in *params*->mark for the fib lookup. * This option should not be used with BPF_FIB_LOOKUP_DIRECT, * as it only has meaning for full lookups. +* **BPF_FIB_LOOKUP_VLAN** This flag should not be needed. Patches for vlan support were never submitted (I have them in some old branch). Since the vlan params are initialized to 0, no new flag should be needed. Besides, these are output parameters.
Quoted 8 lines
Quoted 6 lines
There's no enforcement from the kernel side of the parameters being zero, though? So we do need the flag for feature detection; unless we expect applications to do that out of band? But then we'd need a mechanism to do that which could be... the presence of the flag in the ENUM (and thus in BTF)? :)
This is output direction - return from the fib lookup.
Ah, right, silly me.
Quoted 3 lines
It does not make sense to require a flag to get lookup output. vlan proto of 0 is not valid, so it is a clear indication that the vlan output parameters were not set during the lookup.
Okay, so we could just unconditionally set the VLAN fields, but if we start rewriting the ifindex that would be a change of the existing behaviour that could break existing applications, no?
Specifically, if an XDP application has a table of the interfaces it forwards between, today they'd get a VLAN interface ifindex, which would not be in that table, and the application would return XDP_PASS. Whereas if we change the ifindex and populate the VLAN tag, suddenly the interface would be in the table, but because the application doesn't read the returned VLAN tag, it will end up sending packets out without tagging them, leading to broken forwarding.
So if we don't want the flag, we'd need some other mechanism to resolve the parent ifindex, AFAICT? Maybe a xdp_get_parent_ifindex() kfunc, say? That could also be made generic for other stacked interface types, I suppose.
WDYT?
-Toke
Re: [PATCH bpf-next v5 1/3] bpf: Add BPF_FIB_LOOKUP_VLAN flag to bpf_fib_lookup() helper
Quoted 7 lines · Toke Høiland-Jørgensen
Quoted 3 lines
It does not make sense to require a flag to get lookup output. vlan proto of 0 is not valid, so it is a clear indication that the vlan output parameters were not set during the lookup.
Okay, so we could just unconditionally set the VLAN fields, but if we start rewriting the ifindex that would be a change of the existing behaviour that could break existing applications, no?
Consistently dealing with upper devices is one of the reasons I never sent patches for vlan support.
xdp support is at the driver layer for real (physical) devices. The fib lookup is going to return the vlan device index - a virtual device. Support for xdp should not be propagated to virtual devices; it goes against the intent of xdp. Any trip down this path will have to decide how to handle vlan-in-vlan use cases. Where is the line drawn for fast networking?
Quoted 8 lines
Specifically, if an XDP application has a table of the interfaces it forwards between, today they'd get a VLAN interface ifindex, which would not be in that table, and the application would return
XDP_PASS. Whereas if we change the ifindex and populate the VLAN tag, suddenly the interface would be in the table, but because the application doesn't read the returned VLAN tag, it will end up sending packets out without tagging them, leading to broken forwarding.
I have not followed developments over the past few years. Does XDP have support for vlan acceleration in the Tx path now? You really want that to deal with vlans and not replicating s/w processing in ebpf.
Quoted 7 lines
So if we don't want the flag, we'd need some other mechanism to resolve the parent ifindex, AFAICT? Maybe a
xdp_get_parent_ifindex()kfunc, say? That could also be made generic for other stacked interface types, I suppose.WDYT?
dealing with stacked devices is hard :-)
What is the return is a bond device or a vlan on a bond device?
Re: [PATCH bpf-next v5 1/3] bpf: Add BPF_FIB_LOOKUP_VLAN flag to bpf_fib_lookup() helper
Quoted 12 lines · David Ahern
Quoted 7 lines · Toke Høiland-Jørgensen
Quoted 3 lines
It does not make sense to require a flag to get lookup output. vlan proto of 0 is not valid, so it is a clear indication that the vlan output parameters were not set during the lookup.
Okay, so we could just unconditionally set the VLAN fields, but if we start rewriting the ifindex that would be a change of the existing behaviour that could break existing applications, no?
Consistently dealing with upper devices is one of the reasons I never sent patches for vlan support.
Quoted 6 lines
xdp support is at the driver layer for real (physical) devices. The fib lookup is going to return the vlan device index - a virtual device. Support for xdp should not be propagated to virtual devices; it goes against the intent of xdp. Any trip down this path will have to decide how to handle vlan-in-vlan use cases. Where is the line drawn for fast networking?
Right, which is why we need building blocks that makes it possible for XDP programs to do the right thing in the BPF code :)
A helper that resolves the parent could be used for stacked VLANs as well (just calling the helper multiple times).
Quoted 11 lines
Quoted 7 lines
Specifically, if an XDP application has a table of the interfaces it forwards between, today they'd get a VLAN interface ifindex, which would not be in that table, and the application would return
XDP_PASS. Whereas if we change the ifindex and populate the VLAN tag, suddenly the interface would be in the table, but because the application doesn't read the returned VLAN tag, it will end up sending packets out without tagging them, leading to broken forwarding.I have not followed developments over the past few years. Does XDP have support for vlan acceleration in the Tx path now? You really want that to deal with vlans and not replicating s/w processing in ebpf.
It does not, no. There's TX metadata for AF_XDP, but VLAN support is not in there (see include/uapi/linux/if_xdp.h).
Doesn't mean software VLAN handling can't be useful, though; there are use cases other than the very high end where XDP can speed things up even if it has to write a VLAN tag or two...
Quoted 10 lines
Quoted 6 lines
So if we don't want the flag, we'd need some other mechanism to resolve the parent ifindex, AFAICT? Maybe a
xdp_get_parent_ifindex()kfunc, say? That could also be made generic for other stacked interface types, I suppose.WDYT?
dealing with stacked devices is hard :-)
What is the return is a bond device or a vlan on a bond device?
Well, bond devices have XDP support, so you can just redirect to those :)
But yeah, each type of stacked device would need to pass different information through to the XDP program, and the program would need to support those. Building a single XDP program that supports all of them will require quite a bit of code, and would probably not perform super well. But most deployments have distinct subsets of features they need, so this does not have to be a blocker, IMO?
-Toke
Re: [PATCH bpf-next v5 1/3] bpf: Add BPF_FIB_LOOKUP_VLAN flag to bpf_fib_lookup() helper
Quoted 57 lines · Toke Høiland-Jørgensen
Quoted 12 lines · David Ahern
Quoted 7 lines · Toke Høiland-Jørgensen
Quoted 3 lines
It does not make sense to require a flag to get lookup output. vlan proto of 0 is not valid, so it is a clear indication that the vlan output parameters were not set during the lookup.
Okay, so we could just unconditionally set the VLAN fields, but if we start rewriting the ifindex that would be a change of the existing behaviour that could break existing applications, no?
Consistently dealing with upper devices is one of the reasons I never sent patches for vlan support.
Quoted 6 lines
xdp support is at the driver layer for real (physical) devices. The fib lookup is going to return the vlan device index - a virtual device. Support for xdp should not be propagated to virtual devices; it goes against the intent of xdp. Any trip down this path will have to decide how to handle vlan-in-vlan use cases. Where is the line drawn for fast networking?
Right, which is why we need building blocks that makes it possible for XDP programs to do the right thing in the BPF code :)
A helper that resolves the parent could be used for stacked VLANs as well (just calling the helper multiple times).
Quoted 11 lines
Quoted 7 lines
Specifically, if an XDP application has a table of the interfaces it forwards between, today they'd get a VLAN interface ifindex, which would not be in that table, and the application would return
XDP_PASS. Whereas if we change the ifindex and populate the VLAN tag, suddenly the interface would be in the table, but because the application doesn't read the returned VLAN tag, it will end up sending packets out without tagging them, leading to broken forwarding.I have not followed developments over the past few years. Does XDP have support for vlan acceleration in the Tx path now? You really want that to deal with vlans and not replicating s/w processing in ebpf.
It does not, no. There's TX metadata for
AF_XDP, but VLAN support is not in there (seeinclude/uapi/linux/if_xdp.h).Doesn't mean software VLAN handling can't be useful, though; there are use cases other than the very high end where XDP can speed things up even if it has to write a VLAN tag or two...
Quoted 10 lines
Quoted 6 lines
So if we don't want the flag, we'd need some other mechanism to resolve the parent ifindex, AFAICT? Maybe a
xdp_get_parent_ifindex()kfunc, say? That could also be made generic for other stacked interface types, I suppose.WDYT?
dealing with stacked devices is hard :-)
What is the return is a bond device or a vlan on a bond device?
Quoted 9 lines
Well, bond devices have XDP support, so you can just redirect to those :)
But yeah, each type of stacked device would need to pass different information through to the XDP program, and the program would need to support those. Building a single XDP program that supports all of them will require quite a bit of code, and would probably not perform super well. But most deployments have distinct subsets of features they need, so this does not have to be a blocker, IMO?
Seems to me the fib_lookup for xdp needs to return the bottom device, not the vlan device, for forwarding to work. That's why I added the fields to the struct. That allows the program to push the vlan header if required. My preference (dream?) was that Tx path had support to tell the redirect the vlan and h/w added it on send.
But really, once stacked devices come into play, I just wanted to make sure thought is given to different use cases. As you know the lookup struct if hard bound to 64B and it is trying to cover a lot of use cases.
Re: [PATCH bpf-next v5 1/3] bpf: Add BPF_FIB_LOOKUP_VLAN flag to bpf_fib_lookup() helper
Quoted 68 lines · David Ahern
Quoted 57 lines · Toke Høiland-Jørgensen
Quoted 12 lines · David Ahern
Quoted 7 lines · Toke Høiland-Jørgensen
Quoted 3 lines
It does not make sense to require a flag to get lookup output. vlan proto of 0 is not valid, so it is a clear indication that the vlan output parameters were not set during the lookup.
Okay, so we could just unconditionally set the VLAN fields, but if we start rewriting the ifindex that would be a change of the existing behaviour that could break existing applications, no?
Consistently dealing with upper devices is one of the reasons I never sent patches for vlan support.
Quoted 6 lines
xdp support is at the driver layer for real (physical) devices. The fib lookup is going to return the vlan device index - a virtual device. Support for xdp should not be propagated to virtual devices; it goes against the intent of xdp. Any trip down this path will have to decide how to handle vlan-in-vlan use cases. Where is the line drawn for fast networking?
Right, which is why we need building blocks that makes it possible for XDP programs to do the right thing in the BPF code :)
A helper that resolves the parent could be used for stacked VLANs as well (just calling the helper multiple times).
Quoted 11 lines
Quoted 7 lines
Specifically, if an XDP application has a table of the interfaces it forwards between, today they'd get a VLAN interface ifindex, which would not be in that table, and the application would return
XDP_PASS. Whereas if we change the ifindex and populate the VLAN tag, suddenly the interface would be in the table, but because the application doesn't read the returned VLAN tag, it will end up sending packets out without tagging them, leading to broken forwarding.I have not followed developments over the past few years. Does XDP have support for vlan acceleration in the Tx path now? You really want that to deal with vlans and not replicating s/w processing in ebpf.
It does not, no. There's TX metadata for
AF_XDP, but VLAN support is not in there (seeinclude/uapi/linux/if_xdp.h).Doesn't mean software VLAN handling can't be useful, though; there are use cases other than the very high end where XDP can speed things up even if it has to write a VLAN tag or two...
Quoted 10 lines
Quoted 6 lines
So if we don't want the flag, we'd need some other mechanism to resolve the parent ifindex, AFAICT? Maybe a
xdp_get_parent_ifindex()kfunc, say? That could also be made generic for other stacked interface types, I suppose.WDYT?
dealing with stacked devices is hard :-)
What is the return is a bond device or a vlan on a bond device?
Quoted 9 lines
Well, bond devices have XDP support, so you can just redirect to those :)
But yeah, each type of stacked device would need to pass different information through to the XDP program, and the program would need to support those. Building a single XDP program that supports all of them will require quite a bit of code, and would probably not perform super well. But most deployments have distinct subsets of features they need, so this does not have to be a blocker, IMO?
Quoted 5 lines
Seems to me the
fib_lookupfor xdp needs to return the bottom device, not the vlan device, for forwarding to work. That's why I added the fields to the struct. That allows the program to push the vlan header if required. My preference (dream?) was that Tx path had support to tell the redirect the vlan and h/w added it on send.
Sure, returning the bottom device index with the VLAN tag makes sense, and that's basically what this series does (but bails out on stacked VLANs). However, that's not what the helper does today, which is why the flag is there, to opt-in to the new behaviour. I don't think we can just change the ifindex without breaking existing applications (as noted up-thread).
Quoted 3 lines
But really, once stacked devices come into play, I just wanted to make sure thought is given to different use cases. As you know the lookup struct if hard bound to 64B and it is trying to cover a lot of use cases.
Agreed, I don't think we can handle stacked devices in this helper. But we could split it out into a new one. Something like:
struct lower_device_info {
enum device_type type;
struct {
__be16 h_vlan_proto;
__be16 h_vlan_TCI;
} vlan;
/* add other types here */
};int xdp_get_lower_device(int ifindex, struct lower_device_info *info);
called like:
int xdp_program(struct xdp_md *ctx)
{
struct lower_device_info dev_info = {};
int ifindex, ret;
ifindex = find_destination(ctx); /* does fib lookup, or something else */
while ((ret = xdp_get_lower_device_info(ifindex, &dev_info)) > 0) {
if (dev_info.type == VLAN) {
push_vlan_tag(ctx, &dev_info.vlan);
ifindex = ret;
} else {
return XDP_PASS; /* we only handle VLAN devices */
}
}
return bpf_redirect(ifindex, 0);
}With a helper like this, we obviously don't strictly speaking need to change the fib lookup helper at all. However, for the single-tagged VLAN case, I think supporting it directly in the fib lookup could still have value, as an optimisation: it saves an extra call for resolving the ifindex, and the fields are already there. So I think my preference would be to merge this series as-is, and then follow up with a new kfunc to handle the stacked case. But we could also just drop this series and go straight to the new kfunc.
WDYT?
-Toke
Re: [PATCH bpf-next v5 1/3] bpf: Add BPF_FIB_LOOKUP_VLAN flag to bpf_fib_lookup() helper
Quoted 7 lines · Toke Høiland-Jørgensen
Quoted 5 lines · David Ahern
Seems to me the
fib_lookupfor xdp needs to return the bottom device, not the vlan device, for forwarding to work. That's why I added the fields to the struct. That allows the program to push the vlan header if required. My preference (dream?) was that Tx path had support to tell the redirect the vlan and h/w added it on send.
Quoted 6 lines
Sure, returning the bottom device index with the VLAN tag makes sense, and that's basically what this series does (but bails out on stacked VLANs). However, that's not what the helper does today, which is why the flag is there, to opt-in to the new behaviour. I don't think we can just change the ifindex without breaking existing applications (as noted up-thread).
I do not see it as breaking existing programs which is why I chimed in on the thread.
Quoted 50 lines
Quoted 4 lines
But really, once stacked devices come into play, I just wanted to make sure thought is given to different use cases. As you know the lookup struct if hard bound to 64B and it is trying to cover a lot of use cases.
Agreed, I don't think we can handle stacked devices in this helper. But we could split it out into a new one. Something like:
struct lower_device_info { enum device_type type; struct { __be16 h_vlan_proto; __be16 h_vlan_TCI; } vlan; /* add other types here */ };int
xdp_get_lower_device(int ifindex, struct lower_device_info *info);called like:
int
xdp_program(struct xdp_md *ctx){ struct lower_device_info dev_info = {}; int ifindex, ret; ifindex = find_destination(ctx); /* does fib lookup, or something else */ while ((ret = xdp_get_lower_device_info(ifindex, &dev_info)) > 0) { if (dev_info.type == VLAN) { push_vlan_tag(ctx, &dev_info.vlan); ifindex = ret; } else { return XDP_PASS; /* we only handle VLAN devices */ } } return bpf_redirect(ifindex, 0); }With a helper like this, we obviously don't strictly speaking need to change the fib lookup helper at all. However, for the single-tagged VLAN case, I think supporting it directly in the fib lookup could still have value, as an optimisation: it saves an extra call for resolving the ifindex, and the fields are already there. So I think my preference would be to merge this series as-is, and then follow up with a new kfunc to handle the stacked case. But we could also just drop this series and go straight to the new kfunc.
Quoted 1 line
WDYT?
no preference. I only chimed in because of the added flag to the uapi which I do not see as needed. If the consensus is that it is in fact needed, all good then.
Re: [PATCH bpf-next v5 1/3] bpf: Add BPF_FIB_LOOKUP_VLAN flag to bpf_fib_lookup() helper
Quoted 68 lines · David Ahern
Quoted 7 lines · Toke Høiland-Jørgensen
Quoted 5 lines · David Ahern
Seems to me the
fib_lookupfor xdp needs to return the bottom device, not the vlan device, for forwarding to work. That's why I added the fields to the struct. That allows the program to push the vlan header if required. My preference (dream?) was that Tx path had support to tell the redirect the vlan and h/w added it on send.Quoted 6 lines
Sure, returning the bottom device index with the VLAN tag makes sense, and that's basically what this series does (but bails out on stacked VLANs). However, that's not what the helper does today, which is why the flag is there, to opt-in to the new behaviour. I don't think we can just change the ifindex without breaking existing applications (as noted up-thread).
I do not see it as breaking existing programs which is why I chimed in on the thread.
Quoted 50 lines
Quoted 4 lines
But really, once stacked devices come into play, I just wanted to make sure thought is given to different use cases. As you know the lookup struct if hard bound to 64B and it is trying to cover a lot of use cases.
Agreed, I don't think we can handle stacked devices in this helper. But we could split it out into a new one. Something like:
struct lower_device_info { enum device_type type; struct { __be16 h_vlan_proto; __be16 h_vlan_TCI; } vlan; /* add other types here */ };int
xdp_get_lower_device(int ifindex, struct lower_device_info *info);called like:
int
xdp_program(struct xdp_md *ctx){ struct lower_device_info dev_info = {}; int ifindex, ret; ifindex = find_destination(ctx); /* does fib lookup, or something else */ while ((ret = xdp_get_lower_device_info(ifindex, &dev_info)) > 0) { if (dev_info.type == VLAN) { push_vlan_tag(ctx, &dev_info.vlan); ifindex = ret; } else { return XDP_PASS; /* we only handle VLAN devices */ } } return bpf_redirect(ifindex, 0); }With a helper like this, we obviously don't strictly speaking need to change the fib lookup helper at all. However, for the single-tagged VLAN case, I think supporting it directly in the fib lookup could still have value, as an optimisation: it saves an extra call for resolving the ifindex, and the fields are already there. So I think my preference would be to merge this series as-is, and then follow up with a new kfunc to handle the stacked case. But we could also just drop this series and go straight to the new kfunc.
Quoted 5 lines
Quoted 1 line
WDYT?
no preference. I only chimed in because of the added flag to the uapi which I do not see as needed. If the consensus is that it is in fact needed, all good then.
Alright, cool - care to provide an ACK, then? :)
-Toke
David Ahern
Acked-byRe: [PATCH bpf-next v5 1/3] bpf: Add BPF_FIB_LOOKUP_VLAN flag to bpf_fib_lookup() helper
Quoted 70 lines · Toke Høiland-Jørgensen
Quoted 68 lines · David Ahern
Quoted 7 lines · Toke Høiland-Jørgensen
Quoted 5 lines · David Ahern
Seems to me the
fib_lookupfor xdp needs to return the bottom device, not the vlan device, for forwarding to work. That's why I added the fields to the struct. That allows the program to push the vlan header if required. My preference (dream?) was that Tx path had support to tell the redirect the vlan and h/w added it on send.Quoted 6 lines
Sure, returning the bottom device index with the VLAN tag makes sense, and that's basically what this series does (but bails out on stacked VLANs). However, that's not what the helper does today, which is why the flag is there, to opt-in to the new behaviour. I don't think we can just change the ifindex without breaking existing applications (as noted up-thread).
I do not see it as breaking existing programs which is why I chimed in on the thread.
Quoted 50 lines
Quoted 4 lines
But really, once stacked devices come into play, I just wanted to make sure thought is given to different use cases. As you know the lookup struct if hard bound to 64B and it is trying to cover a lot of use cases.
Agreed, I don't think we can handle stacked devices in this helper. But we could split it out into a new one. Something like:
struct lower_device_info { enum device_type type; struct { __be16 h_vlan_proto; __be16 h_vlan_TCI; } vlan; /* add other types here */ };int
xdp_get_lower_device(int ifindex, struct lower_device_info *info);called like:
int
xdp_program(struct xdp_md *ctx){ struct lower_device_info dev_info = {}; int ifindex, ret; ifindex = find_destination(ctx); /* does fib lookup, or something else */ while ((ret = xdp_get_lower_device_info(ifindex, &dev_info)) > 0) { if (dev_info.type == VLAN) { push_vlan_tag(ctx, &dev_info.vlan); ifindex = ret; } else { return XDP_PASS; /* we only handle VLAN devices */ } } return bpf_redirect(ifindex, 0); }With a helper like this, we obviously don't strictly speaking need to change the fib lookup helper at all. However, for the single-tagged VLAN case, I think supporting it directly in the fib lookup could still have value, as an optimisation: it saves an extra call for resolving the ifindex, and the fields are already there. So I think my preference would be to merge this series as-is, and then follow up with a new kfunc to handle the stacked case. But we could also just drop this series and go straight to the new kfunc.
Quoted 8 lines
Quoted 5 lines
Quoted 1 line
WDYT?
no preference. I only chimed in because of the added flag to the uapi which I do not see as needed. If the consensus is that it is in fact needed, all good then.
Alright, cool - care to provide an ACK, then? :)
PATCH v5 2/3 bpf: Add BPF_FIB_LOOKUP_VLAN_INPUT flag to bpf_fib_lookup() helper
In reply to Avinash DuduskarBPF_FIB_LOOKUP_VLAN resolves a VLAN egress. The reverse is also
useful: an XDP program receiving a VLAN-tagged frame on a physical device wants the lookup to behave as if the packet had arrived on the corresponding VLAN subinterface, so iif-based policy routing and VRF table selection use the right ingress.
Add BPF_FIB_LOOKUP_VLAN_INPUT. When set, params->h_vlan_proto and params->h_vlan_TCI are read as an input VLAN tag and the matching VLAN device of params->ifindex is resolved with __vlan_find_dev_deep_rcu(). The device must be up and in the same network namespace as params->ifindex (a VLAN device can be moved to another netns while registered on its parent; receive would deliver into that other namespace, which a lookup here cannot represent). If params->ifindex is itself a VLAN device, its inner (QinQ) subinterface is matched. For a bond or team, a tag on a port matches no device and returns NOT_FWDED; pass the master's ifindex.
The lookup then runs with the resolved device as the ingress; params->ifindex itself is not modified on the input side. When the resolved device is enslaved to a VRF, both the full lookup (via the l3mdev rule) and BPF_FIB_LOOKUP_DIRECT (via l3mdev_fib_table_rcu()) select the VRF's table from the resolved ingress. That follows from feeding the resolved device to the flow as the ingress (fl4.flowi4_iif = dev->ifindex), which is what makes l3mdev resolve the VRF master from the subinterface rather than from params->ifindex.
The two failure classes get different treatment on purpose. A h_vlan_proto other than 802.1Q/802.1ad is API misuse and returns -EINVAL, since it would otherwise reach the WARN in vlan_proto_idx() with a program-controlled value. An unmatched VID, a device that is down, or one in another namespace is a data outcome and returns BPF_FIB_LKUP_RET_NOT_FWDED, matching the DIRECT path when fib_get_table() finds no table and mirroring real ingress, where the receive path drops such frames. A VID of 0 (a priority tag) is looked up literally and normally fails the same way; receive instead processes such frames untagged, so callers should not set the flag for priority tags. Proceeding on the physical device for any of these would be fail-open for the policy-routing cases above.
The h_vlan fields share a union with tbid, so the flag cannot be combined with BPF_FIB_LOOKUP_TBID. It describes ingress, so it also cannot be combined with BPF_FIB_LOOKUP_OUTPUT. Both combinations return -EINVAL; restricting now keeps a later relaxation backward compatible. Combining with BPF_FIB_LOOKUP_VLAN is allowed: the tag is consumed on the ingress side and the egress tag is written on success.
Under !CONFIG_VLAN_8021Q the __vlan_find_dev_deep_rcu() stub returns NULL, so every lookup with the flag returns NOT_FWDED, which is correct since no VLAN device can exist.
--- include/uapi/linux/bpf.h | 21 ++++++++++- net/core/filter.c | 66 +++++++++++++++++++++++++++++++--- tools/include/uapi/linux/bpf.h | 21 ++++++++++- 3 files changed, 101 insertions(+), 7 deletions(-)
include/uapi/linux/bpf.h +20 -1
@@ -3555,6 +3555,22 @@ union bpf_attr {* This flag is only valid for XDP programs; tc programs * receive -EINVAL since they can redirect to the VLAN * device directly. +* **BPF_FIB_LOOKUP_VLAN_INPUT** +* Treat *params*->h_vlan_proto and *params*->h_vlan_TCI +* as an input VLAN tag and run the lookup as if ingress +* had happened on the VLAN subinterface carrying that tag +* on *params*->ifindex. The VID is the low 12 bits of +* *params*->h_vlan_TCI; *params*->h_vlan_proto must be +* ETH_P_8021Q or ETH_P_8021AD in network byte order, else +* **-EINVAL**. If *params*->ifindex is itself a VLAN +* device, its inner (QinQ) subinterface is matched; for a +* bond or team, pass the master's ifindex. An unmatched +* tag, a down device, or one in another namespace returns +* **BPF_FIB_LKUP_RET_NOT_FWDED**, mirroring real ingress. +* A VID of 0 is looked up literally, so do not set this +* flag for priority-tagged frames. Cannot be combined with +* **BPF_FIB_LOOKUP_TBID** or **BPF_FIB_LOOKUP_OUTPUT** +* (returns **-EINVAL**). * * *ctx* is either **struct xdp_md** for XDP programs or * **struct sk_buff** tc cls_act programs.
@@ -7351,6 +7367,7 @@ enum {BPF_FIB_LOOKUP_SRC = (1U <<4 ),BPF_FIB_LOOKUP_MARK = (1U <<5 ),BPF_FIB_LOOKUP_VLAN = (1U <<6 ), + BPF_FIB_LOOKUP_VLAN_INPUT = (1U << 7), }; enum {
@@ -7421,7 +7438,9 @@ struct bpf_fib_lookup {/* * output with BPF_FIB_LOOKUP_VLAN: set from the * resolved egress VLAN device (see the flag); zeroed -* on other successful lookups. +* on other successful lookups. input with +* BPF_FIB_LOOKUP_VLAN_INPUT: the VLAN tag to scope +* the lookup by. */ __be16 h_vlan_proto;__be16 h_vlan_TCI;
net/core/filter.c +61 -5
114 lines not on this page · raw message
tools/include/uapi/linux/bpf.h +20 -1
@@ -3555,6 +3555,22 @@ union bpf_attr {* This flag is only valid for XDP programs; tc programs * receive -EINVAL since they can redirect to the VLAN * device directly. +* **BPF_FIB_LOOKUP_VLAN_INPUT** +* Treat *params*->h_vlan_proto and *params*->h_vlan_TCI +* as an input VLAN tag and run the lookup as if ingress +* had happened on the VLAN subinterface carrying that tag +* on *params*->ifindex. The VID is the low 12 bits of +* *params*->h_vlan_TCI; *params*->h_vlan_proto must be +* ETH_P_8021Q or ETH_P_8021AD in network byte order, else +* **-EINVAL**. If *params*->ifindex is itself a VLAN +* device, its inner (QinQ) subinterface is matched; for a +* bond or team, pass the master's ifindex. An unmatched +* tag, a down device, or one in another namespace returns +* **BPF_FIB_LKUP_RET_NOT_FWDED**, mirroring real ingress. +* A VID of 0 is looked up literally, so do not set this +* flag for priority-tagged frames. Cannot be combined with +* **BPF_FIB_LOOKUP_TBID** or **BPF_FIB_LOOKUP_OUTPUT** +* (returns **-EINVAL**). * * *ctx* is either **struct xdp_md** for XDP programs or * **struct sk_buff** tc cls_act programs.
@@ -7351,6 +7367,7 @@ enum {BPF_FIB_LOOKUP_SRC = (1U <<4 ),BPF_FIB_LOOKUP_MARK = (1U <<5 ),BPF_FIB_LOOKUP_VLAN = (1U <<6 ), + BPF_FIB_LOOKUP_VLAN_INPUT = (1U << 7), }; enum {
@@ -7421,7 +7438,9 @@ struct bpf_fib_lookup {/* * output with BPF_FIB_LOOKUP_VLAN: set from the * resolved egress VLAN device (see the flag); zeroed -* on other successful lookups. +* on other successful lookups. input with +* BPF_FIB_LOOKUP_VLAN_INPUT: the VLAN tag to scope +* the lookup by. */ __be16 h_vlan_proto;__be16 h_vlan_TCI;
Signature · 1 line
2.54.0
PATCH v5 3/3 selftests/bpf: Add bpf_fib_lookup() VLAN flag tests
In reply to Avinash DuduskarCover both new VLAN flags in the fib_lookup test. BPF_FIB_LOOKUP_VLAN reduces a VLAN egress to its physical parent plus the tag, and BPF_FIB_LOOKUP_VLAN_INPUT scopes the lookup to a VLAN subinterface.
BPF_FIB_LOOKUP_VLAN is XDP-only, since VLAN devices have no XDP xmit; the tc helper rejects it with -EINVAL, which the table runner asserts for every flag arm, and the egress result is checked through bpf_xdp_fib_lookup(). Non-VLAN cases run through both helpers and assert the path-independent results match; the XDP loop also checks dmac and, for the tot_len cases, the route mtu_result, so the VLAN-egress dmac and frag-needed coverage stays even though the tc path no longer reaches it.
The egress arms pin the reduction (parent ifindex plus tag, including via a neighbour on the VLAN device, in OUTPUT mode, over a bond, and through a DIRECT|TBID table) and the failure contract: a stacked-VLAN (QinQ) egress returns BPF_FIB_LKUP_RET_VLAN_FAILURE with params->ifindex left at the input. That is distinct from a no-neighbour return, which reports the egress ifindex; only VLAN_FAILURE rewinds params->ifindex, and a guard arm whose input and egress devices differ pins the distinction. The VLAN_FAILURE arms are IPv4; the IPv6 path reaches it through the same shared code, so an IPv6 arm would only re-test that.
The input arms use an iif rule that routes one destination to two gateways, so the asserted gateway reveals which device the lookup used as ingress, including VRF table selection through the l3mdev rule and l3mdev_fib_table_rcu(). A cross-netns subtest moves a VLAN device into a second netns while it stays registered on its parent and checks both directions fail closed at the boundary.
A live-frames subtest (test_fib_lookup_vlan_redirect, with BPF_F_TEST_XDP_LIVE_FRAMES) drives real frames through the native xdp_do_redirect() / xdp_do_flush() path: a reducible egress is redirected to the parent and delivered to its peer, while a QinQ egress is passed to the stack, since redirecting to the VLAN device would drop the frame at flush (no ndo_xdp_xmit).
The remaining per-case assertions -- resolution semantics, the -EINVAL and NOT_FWDED error arms, and the SRC/SKIP_NEIGH combinations -- are in the test table.
--- .../selftests/bpf/prog_tests/fib_lookup.c | 717 +++++++++++++++++- .../testing/selftests/bpf/progs/fib_lookup.c | 36 + 2 files changed, 749 insertions(+), 4 deletions(-)
tools/testing/selftests/bpf/prog_tests/fib_lookup.c +713 -4
@@ -2,6 +2,7 @@/* Copyright (c) 2023 Meta Platforms, Inc. and affiliates. */ #include <linux/rtnetlink.h> +#include <linux/if_ether.h> #include <sys/types.h> #include <net/if.h>
@@ -23,6 +24,7 @@#define IPV4_TBID_ADDR "172.0.0.254"#define IPV4_TBID_NET "172.0.0.0"#define IPV4_TBID_DST "172.0.0.2" +#define IPV4_TBID_NONEIGH_DST "172.0.0.5"#define IPV6_TBID_ADDR "fd00::FFFF"#define IPV6_TBID_NET "fd00::"#define IPV6_TBID_DST "fd00::2"
@@ -37,6 +39,41 @@#define IPV6_LOCAL "fd01::3"#define IPV6_GW1 "fd01::1"#define IPV6_GW2 "fd01::2" +#define VLAN_ID 100 +#define VLAN_IFACE "veth1.100" +#define VLAN_ID_DOWN 102 +#define VLAN_IFACE_DOWN "veth1.102" +#define QINQ_OUTER_IFACE "veth1.200" +#define QINQ_INNER_IFACE "veth1.200.300" +#define VLAN_TABLE "300" +#define IPV4_VLAN_IFACE_ADDR "10.5.0.254" +#define IPV4_VLAN_EGRESS_DST "10.5.0.2" +#define IPV4_QINQ_DST "10.7.0.2" +#define IPV4_VLAN_DST "10.6.0.2" +#define IPV4_VLAN_GW "10.5.0.1" +#define IPV6_VLAN_IFACE_ADDR "fd02::254" +#define IPV6_VLAN_EGRESS_DST "fd02::2" +#define IPV6_VLAN_DST "fd03::2" +#define IPV6_VLAN_GW "fd02::1" +#define VLAN_VID_UNUSED 999 +#define VRF_IFACE "vrf-blue" +#define VRF_TABLE "1000" +#define VRF_VLAN_ID 101 +#define VRF_VLAN_IFACE "veth1.101" +#define IPV4_VRF_IFACE_ADDR "10.8.0.254" +#define IPV4_VRF_GW "10.8.0.1" +#define IPV4_VRF_DST "10.9.0.2" +#define TBID_VLAN_ID 50 +#define TBID_VLAN_IFACE "veth2.50" +#define IPV4_TBID_VLAN_DST "172.2.0.2" +#define IPV4_BOND_VLAN_DST "10.11.0.2" +#define IPV4_VLAN_MTU_DST "10.5.9.2" +#define QINQ_AD_VLAN_ID 200 +#define QINQ_INNER_VLAN_ID 300 +#define BOND_IFACE "bond99" +#define BOND_PORT "veth3" +#define BOND_PORT_PEER "veth4" +#define BOND_VLAN_ID 500#define DMAC "11:11:11:11:11:11"#define DMAC_INIT { 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, }#define DMAC2 "01:01:01:01:01:01"
@@ -52,6 +89,17 @@ struct fib_lookup_test {__u32 tbid;__u8 dmac[6 ];__u32 mark; +/* +* input tag with BPF_FIB_LOOKUP_VLAN_INPUT; expected output tag +* with BPF_FIB_LOOKUP_VLAN (checked when check_vlan is set) +*/ +__u16 vlan_proto; +__u16 vlan_id; +bool check_vlan; +const char *expected_dev;/* expected params->ifindex after lookup */ +const char *iif;/* override the default veth1 input device */ +__u16 tot_len;/* triggers the in-lookup mtu check when set */ +__u16 expected_mtu;/* expected mtu_result (union with tot_len) */ };static const struct fib_lookup_test tests[] = {
@@ -79,6 +127,17 @@ static const struct fib_lookup_test tests[] = { .daddr = IPV4_TBID_DST, .expected_ret = BPF_FIB_LKUP_RET_SUCCESS, .lookup_flags = BPF_FIB_LOOKUP_DIRECT | BPF_FIB_LOOKUP_TBID, .tbid = 100, .dmac = DMAC_INIT2, }, +/* +* An error that returns after the egress device is resolved must +* report the egress ifindex, not the input. This routes from input +* veth1 via veth2 (table 100) to a dst with no neighbour, so +* input != egress, pinning NO_NEIGH to the egress device. +*/ + { .desc = "IPv4 NO_NEIGH reports the egress ifindex, not the input", + .daddr = IPV4_TBID_NONEIGH_DST, + .expected_ret = BPF_FIB_LKUP_RET_NO_NEIGH, + .lookup_flags = BPF_FIB_LOOKUP_DIRECT | BPF_FIB_LOOKUP_TBID, .tbid = 100, + .expected_dev = "veth2", }, { .desc = "IPv6 TBID lookup failure", .daddr = IPV6_TBID_DST, .expected_ret = BPF_FIB_LKUP_RET_NOT_FWDED, .lookup_flags = BPF_FIB_LOOKUP_DIRECT | BPF_FIB_LOOKUP_TBID,
@@ -142,6 +201,218 @@ static const struct fib_lookup_test tests[] = { .expected_dst = IPV6_GW1, .lookup_flags = BPF_FIB_LOOKUP_SKIP_NEIGH, .mark = MARK, }, +/* vlan egress resolution */ +/* +* Invariant the VLAN-egress arms jointly enforce: a +* BPF_FIB_LOOKUP_VLAN SUCCESS always carries a physical, +* xmit-capable ifindex -- no SUCCESS ever returns a VLAN-device +* ifindex. Reducible arms pin ifindex == the physical parent; the +* QinQ and foreign-netns arms pin VLAN_FAILURE with params->ifindex +* left at the input, so a regression to best-effort (SUCCESS + the +* VLAN ifindex) fails one. +*/ + { .desc = "IPv4 VLAN egress, no flag", + .daddr = IPV4_VLAN_EGRESS_DST, .expected_ret = BPF_FIB_LKUP_RET_SUCCESS, + .lookup_flags = BPF_FIB_LOOKUP_SKIP_NEIGH, + .expected_dev = VLAN_IFACE, .check_vlan = true, }, + { .desc = "IPv4 VLAN egress, single VLAN", + .daddr = IPV4_VLAN_EGRESS_DST, .expected_ret = BPF_FIB_LKUP_RET_SUCCESS, + .lookup_flags = BPF_FIB_LOOKUP_VLAN | BPF_FIB_LOOKUP_SKIP_NEIGH, + .expected_dev = "veth1", .check_vlan = true, + .vlan_proto = ETH_P_8021Q, .vlan_id = VLAN_ID, }, + /* + * skb path without tot_len: mtu_result is the VLAN device's mtu + * (1400), not the parent's (1500) + */ + { .desc = "IPv4 VLAN egress, skb-path mtu is the VLAN device's without the flag", + .daddr = IPV4_VLAN_EGRESS_DST, .expected_ret = BPF_FIB_LKUP_RET_SUCCESS, + .lookup_flags = BPF_FIB_LOOKUP_SKIP_NEIGH, + .expected_dev = VLAN_IFACE, .check_vlan = true, .expected_mtu = 1400, }, + { .desc = "IPv4 VLAN egress, flag set but egress is not a VLAN", + .daddr = IPV4_NUD_FAILED_ADDR, .expected_ret = BPF_FIB_LKUP_RET_SUCCESS, + .lookup_flags = BPF_FIB_LOOKUP_VLAN | BPF_FIB_LOOKUP_SKIP_NEIGH, + .expected_dev = "veth1", .check_vlan = true, }, + { .desc = "IPv4 VLAN egress, QinQ not reducible (VLAN_FAILURE)", + .daddr = IPV4_QINQ_DST, + .expected_ret = BPF_FIB_LKUP_RET_VLAN_FAILURE, + .lookup_flags = BPF_FIB_LOOKUP_VLAN | BPF_FIB_LOOKUP_SKIP_NEIGH, + .expected_dev = "veth1", .check_vlan = true, }, + { .desc = "IPv4 QinQ egress without the flag (escape hatch)", + .daddr = IPV4_QINQ_DST, .expected_ret = BPF_FIB_LKUP_RET_SUCCESS, + .lookup_flags = BPF_FIB_LOOKUP_SKIP_NEIGH, + .expected_dev = QINQ_INNER_IFACE, }, + { .desc = "IPv6 VLAN egress, single VLAN", + .daddr = IPV6_VLAN_EGRESS_DST, .expected_ret = BPF_FIB_LKUP_RET_SUCCESS, + .lookup_flags = BPF_FIB_LOOKUP_VLAN | BPF_FIB_LOOKUP_SKIP_NEIGH, + .expected_dev = "veth1", .check_vlan = true, + .vlan_proto = ETH_P_8021Q, .vlan_id = VLAN_ID, }, + { .desc = "IPv4 VLAN egress, neighbour on the VLAN device", + .daddr = IPV4_VLAN_EGRESS_DST, .expected_ret = BPF_FIB_LKUP_RET_SUCCESS, + .lookup_flags = BPF_FIB_LOOKUP_VLAN, + .expected_dev = "veth1", .check_vlan = true, + .vlan_proto = ETH_P_8021Q, .vlan_id = VLAN_ID, .dmac = DMAC_INIT, }, + { .desc = "IPv4 VLAN egress in OUTPUT mode", + .daddr = IPV4_VLAN_EGRESS_DST, .expected_ret = BPF_FIB_LKUP_RET_SUCCESS, + .iif = VLAN_IFACE, + .lookup_flags = BPF_FIB_LOOKUP_OUTPUT | BPF_FIB_LOOKUP_VLAN | + BPF_FIB_LOOKUP_SKIP_NEIGH, + .expected_dev = "veth1", .check_vlan = true, + .vlan_proto = ETH_P_8021Q, .vlan_id = VLAN_ID, }, + { .desc = "IPv4 VLAN egress over a bond", + .daddr = IPV4_BOND_VLAN_DST, .expected_ret = BPF_FIB_LKUP_RET_SUCCESS, + .lookup_flags = BPF_FIB_LOOKUP_VLAN | BPF_FIB_LOOKUP_SKIP_NEIGH, + .expected_dev = BOND_IFACE, .check_vlan = true, + .vlan_proto = ETH_P_8021Q, .vlan_id = BOND_VLAN_ID, }, + { .desc = "IPv4 VLAN egress via TBID table", + .daddr = IPV4_TBID_VLAN_DST, .expected_ret = BPF_FIB_LKUP_RET_SUCCESS, + .lookup_flags = BPF_FIB_LOOKUP_DIRECT | BPF_FIB_LOOKUP_TBID | + BPF_FIB_LOOKUP_VLAN | BPF_FIB_LOOKUP_SKIP_NEIGH, + .tbid = 100, + .expected_dev = "veth2", .check_vlan = true, + .vlan_proto = ETH_P_8021Q, .vlan_id = TBID_VLAN_ID, }, + { .desc = "IPv4 VLAN egress, success writes mtu_result with the swap", + .daddr = IPV4_VLAN_MTU_DST, .expected_ret = BPF_FIB_LKUP_RET_SUCCESS, + .tot_len = 500, .expected_mtu = 1000, + .lookup_flags = BPF_FIB_LOOKUP_VLAN | BPF_FIB_LOOKUP_SKIP_NEIGH, + .expected_dev = "veth1", .check_vlan = true, + .vlan_proto = ETH_P_8021Q, .vlan_id = VLAN_ID, }, + { .desc = "IPv4 VLAN egress, FRAG_NEEDED reports mtu, swap unwritten", + .daddr = IPV4_VLAN_MTU_DST, .expected_ret = BPF_FIB_LKUP_RET_FRAG_NEEDED, + .tot_len = 1400, .expected_mtu = 1000, + .lookup_flags = BPF_FIB_LOOKUP_VLAN | BPF_FIB_LOOKUP_SKIP_NEIGH, + .expected_dev = "veth1", .check_vlan = true, }, + /* vlan tag as lookup input */ + { .desc = "IPv4 VLAN input, no flag", + .daddr = IPV4_VLAN_DST, .expected_ret = BPF_FIB_LKUP_RET_SUCCESS, + .expected_dst = IPV4_GW1, + .lookup_flags = BPF_FIB_LOOKUP_SKIP_NEIGH, }, + { .desc = "IPv4 VLAN input, tag selects subinterface route", + .daddr = IPV4_VLAN_DST, .expected_ret = BPF_FIB_LKUP_RET_SUCCESS, + .expected_dst = IPV4_VLAN_GW, .expected_dev = VLAN_IFACE, + .lookup_flags = BPF_FIB_LOOKUP_VLAN_INPUT | BPF_FIB_LOOKUP_SKIP_NEIGH, + .vlan_proto = ETH_P_8021Q, .vlan_id = VLAN_ID, }, + { .desc = "IPv6 VLAN input, tag selects subinterface route", + .daddr = IPV6_VLAN_DST, .expected_ret = BPF_FIB_LKUP_RET_SUCCESS, + .expected_dst = IPV6_VLAN_GW, .expected_dev = VLAN_IFACE, + .lookup_flags = BPF_FIB_LOOKUP_VLAN_INPUT | BPF_FIB_LOOKUP_SKIP_NEIGH, + .vlan_proto = ETH_P_8021Q, .vlan_id = VLAN_ID, }, + { .desc = "IPv4 VLAN input and egress combined", + .daddr = IPV4_VLAN_DST, .expected_ret = BPF_FIB_LKUP_RET_SUCCESS, + .expected_dst = IPV4_VLAN_GW, .expected_dev = "veth1", + .check_vlan = true, + .lookup_flags = BPF_FIB_LOOKUP_VLAN_INPUT | BPF_FIB_LOOKUP_VLAN | + BPF_FIB_LOOKUP_SKIP_NEIGH, + .vlan_proto = ETH_P_8021Q, .vlan_id = VLAN_ID, }, + { .desc = "IPv4 VLAN input, neighbour resolved on the route", + .daddr = IPV4_VLAN_DST, .expected_ret = BPF_FIB_LKUP_RET_SUCCESS, + .expected_dst = IPV4_VLAN_GW, .expected_dev = VLAN_IFACE, + .lookup_flags = BPF_FIB_LOOKUP_VLAN_INPUT, + .vlan_proto = ETH_P_8021Q, .vlan_id = VLAN_ID, .dmac = DMAC_INIT2, }, + { .desc = "IPv4 VLAN input, source address from the subinterface", + .daddr = IPV4_VLAN_DST, .expected_ret = BPF_FIB_LKUP_RET_SUCCESS, + .expected_src = IPV4_VLAN_IFACE_ADDR, + .lookup_flags = BPF_FIB_LOOKUP_VLAN_INPUT | BPF_FIB_LOOKUP_SRC | + BPF_FIB_LOOKUP_SKIP_NEIGH, + .vlan_proto = ETH_P_8021Q, .vlan_id = VLAN_ID, }, + /* + * VRF: the resolved subinterface is enslaved, so the l3mdev rule + * (full lookup) and l3mdev_fib_table_rcu() (DIRECT) must select + * the VRF table from the resolved ingress + */ + { .desc = "IPv4 VLAN input, VRF subinterface, no flag", + .daddr = IPV4_VRF_DST, .expected_ret = BPF_FIB_LKUP_RET_SUCCESS, + .expected_dst = IPV4_GW1, + .lookup_flags = BPF_FIB_LOOKUP_SKIP_NEIGH, }, + { .desc = "IPv4 VLAN input, tag selects VRF table", + .daddr = IPV4_VRF_DST, .expected_ret = BPF_FIB_LKUP_RET_SUCCESS, + .expected_dst = IPV4_VRF_GW, .expected_dev = VRF_VLAN_IFACE, + .lookup_flags = BPF_FIB_LOOKUP_VLAN_INPUT | BPF_FIB_LOOKUP_SKIP_NEIGH, + .vlan_proto = ETH_P_8021Q, .vlan_id = VRF_VLAN_ID, }, + { .desc = "IPv4 VLAN input, DIRECT uses VRF table from resolved ingress", + .daddr = IPV4_VRF_DST, .expected_ret = BPF_FIB_LKUP_RET_SUCCESS, + .expected_dst = IPV4_VRF_GW, .expected_dev = VRF_VLAN_IFACE, + .lookup_flags = BPF_FIB_LOOKUP_VLAN_INPUT | BPF_FIB_LOOKUP_DIRECT | + BPF_FIB_LOOKUP_SKIP_NEIGH, + .vlan_proto = ETH_P_8021Q, .vlan_id = VRF_VLAN_ID, }, + /* + * failure arms also assert params is left untouched: ifindex still + * names the physical device and the input tag bytes survive + */ + { .desc = "IPv4 VLAN input, invalid proto", + .daddr = IPV4_VLAN_DST, .expected_ret = -EINVAL, + .expected_dev = "veth1", .check_vlan = true, + .lookup_flags = BPF_FIB_LOOKUP_VLAN_INPUT | BPF_FIB_LOOKUP_SKIP_NEIGH, + .vlan_proto = 0x1234, .vlan_id = VLAN_ID, }, + { .desc = "IPv4 VLAN input, unmatched VID", + .daddr = IPV4_VLAN_DST, .expected_ret = BPF_FIB_LKUP_RET_NOT_FWDED, + .expected_dev = "veth1", .check_vlan = true, + .lookup_flags = BPF_FIB_LOOKUP_VLAN_INPUT | BPF_FIB_LOOKUP_SKIP_NEIGH, + .vlan_proto = ETH_P_8021Q, .vlan_id = VLAN_VID_UNUSED, }, + { .desc = "IPv4 VLAN input, subinterface down", + .daddr = IPV4_VLAN_DST, .expected_ret = BPF_FIB_LKUP_RET_NOT_FWDED, + .expected_dev = "veth1", .check_vlan = true, + .lookup_flags = BPF_FIB_LOOKUP_VLAN_INPUT | BPF_FIB_LOOKUP_SKIP_NEIGH, + .vlan_proto = ETH_P_8021Q, .vlan_id = VLAN_ID_DOWN, }, + /* + * the resolver runs before the forwarding check, so on devices + * with forwarding off FWD_DISABLED (not NOT_FWDED) proves the tag + * resolved to that device and the lookup used it as ingress + */ + { .desc = "IPv4 VLAN input, 802.1ad tag", + .daddr = IPV4_VLAN_DST, .expected_ret = BPF_FIB_LKUP_RET_FWD_DISABLED, + .lookup_flags = BPF_FIB_LOOKUP_VLAN_INPUT | BPF_FIB_LOOKUP_SKIP_NEIGH, + .vlan_proto = ETH_P_8021AD, .vlan_id = QINQ_AD_VLAN_ID, }, + { .desc = "IPv4 VLAN input, PCP and DEI bits ignored in TCI", + .daddr = IPV4_VLAN_DST, .expected_ret = BPF_FIB_LKUP_RET_SUCCESS, + .expected_dst = IPV4_VLAN_GW, + .lookup_flags = BPF_FIB_LOOKUP_VLAN_INPUT | BPF_FIB_LOOKUP_SKIP_NEIGH, + .vlan_proto = ETH_P_8021Q, .vlan_id = 0xe000 | VLAN_ID, }, + { .desc = "IPv4 VLAN input, inner QinQ device from VLAN ifindex", + .daddr = IPV4_VLAN_DST, .expected_ret = BPF_FIB_LKUP_RET_FWD_DISABLED, + .iif = QINQ_OUTER_IFACE, + .lookup_flags = BPF_FIB_LOOKUP_VLAN_INPUT | BPF_FIB_LOOKUP_SKIP_NEIGH, + .vlan_proto = ETH_P_8021Q, .vlan_id = QINQ_INNER_VLAN_ID, }, + /* + * bonding: the VLANs live on the master, as on receive, where the + * frame is steered to the master before VLAN processing; a port + * ifindex does not match (ports carry vid state but no VLAN devs) + */ + { .desc = "IPv4 VLAN input, tag on bond master resolves", + .daddr = IPV4_VLAN_DST, .expected_ret = BPF_FIB_LKUP_RET_FWD_DISABLED, + .iif = BOND_IFACE, + .lookup_flags = BPF_FIB_LOOKUP_VLAN_INPUT | BPF_FIB_LOOKUP_SKIP_NEIGH, + .vlan_proto = ETH_P_8021Q, .vlan_id = BOND_VLAN_ID, }, + { .desc = "IPv4 VLAN input, tag on bond port does not match", + .daddr = IPV4_VLAN_DST, .expected_ret = BPF_FIB_LKUP_RET_NOT_FWDED, + .iif = BOND_PORT, .expected_dev = BOND_PORT, .check_vlan = true, + .lookup_flags = BPF_FIB_LOOKUP_VLAN_INPUT | BPF_FIB_LOOKUP_SKIP_NEIGH, + .vlan_proto = ETH_P_8021Q, .vlan_id = BOND_VLAN_ID, }, + { .desc = "IPv6 VLAN input, invalid proto", + .daddr = IPV6_VLAN_DST, .expected_ret = -EINVAL, + .expected_dev = "veth1", .check_vlan = true, + .lookup_flags = BPF_FIB_LOOKUP_VLAN_INPUT | BPF_FIB_LOOKUP_SKIP_NEIGH, + .vlan_proto = 0x1234, .vlan_id = VLAN_ID, }, + { .desc = "IPv4 VLAN input, VID 0 priority tag fails closed", + .daddr = IPV4_VLAN_DST, .expected_ret = BPF_FIB_LKUP_RET_NOT_FWDED, + .expected_dev = "veth1", .check_vlan = true, + .lookup_flags = BPF_FIB_LOOKUP_VLAN_INPUT | BPF_FIB_LOOKUP_SKIP_NEIGH, + .vlan_proto = ETH_P_8021Q, .vlan_id = 0, }, + { .desc = "IPv6 VLAN input, unmatched VID", + .daddr = IPV6_VLAN_DST, .expected_ret = BPF_FIB_LKUP_RET_NOT_FWDED, + .expected_dev = "veth1", .check_vlan = true, + .lookup_flags = BPF_FIB_LOOKUP_VLAN_INPUT | BPF_FIB_LOOKUP_SKIP_NEIGH, + .vlan_proto = ETH_P_8021Q, .vlan_id = VLAN_VID_UNUSED, }, + { .desc = "unknown flag bit rejected", + .daddr = IPV4_VLAN_DST, .expected_ret = -EINVAL, + .lookup_flags = (1 << 14) | BPF_FIB_LOOKUP_SKIP_NEIGH, }, + { .desc = "IPv4 VLAN input rejected with TBID", + .daddr = IPV4_VLAN_DST, .expected_ret = -EINVAL, + .lookup_flags = BPF_FIB_LOOKUP_VLAN_INPUT | BPF_FIB_LOOKUP_TBID, + .vlan_proto = ETH_P_8021Q, .vlan_id = VLAN_ID, }, + { .desc = "IPv4 VLAN input rejected with OUTPUT", + .daddr = IPV4_VLAN_DST, .expected_ret = -EINVAL, + .lookup_flags = BPF_FIB_LOOKUP_VLAN_INPUT | BPF_FIB_LOOKUP_OUTPUT, + .vlan_proto = ETH_P_8021Q, .vlan_id = VLAN_ID, }, }; static int setup_netns(void)
@@ -204,6 +475,110 @@ static int setup_netns(void)SYS (fail,"ip rule add prio 2 fwmark %d lookup %s" ,MARK ,MARK_TABLE );SYS (fail,"ip -6 rule add prio 2 fwmark %d lookup %s" ,MARK ,MARK_TABLE ); +/* +* Setup for vlan tests: a subinterface for egress resolution and +* tag-as-input, a QinQ stack, and an iif rule so the input tests +* observe which device the lookup used as ingress. +*/ +SYS (fail,"ip link add link veth1 name %s type vlan id %d" , +VLAN_IFACE ,VLAN_ID ); +SYS (fail,"ip link set dev %s up" ,VLAN_IFACE ); +/* +* lower than the veth1 parent (1500): the skb-path mtu check uses the +* FIB result (VLAN) device, so mtu_result is this value with or +* without the egress swap, which two arms below pin +*/ +SYS (fail,"ip link set dev %s mtu 1400" ,VLAN_IFACE ); +SYS (fail,"ip addr add %s/24 dev %s" ,IPV4_VLAN_IFACE_ADDR ,VLAN_IFACE ); +SYS (fail,"ip addr add %s/64 dev %s nodad" ,IPV6_VLAN_IFACE_ADDR ,VLAN_IFACE ); + +/* +* stays down: the input flag must treat its tag the way real +* ingress treats a frame arriving on a down VLAN device (drop) +*/ +SYS (fail,"ip link add link veth1 name %s type vlan id %d" , +VLAN_IFACE_DOWN ,VLAN_ID_DOWN ); + + err =write_sysctl ("/proc/sys/net/ipv4/conf/" VLAN_IFACE "/forwarding" ,"1" ); +if (!ASSERT_OK (err,"write_sysctl(net.ipv4.conf." VLAN_IFACE ".forwarding)" )) + goto fail; + + err =write_sysctl ("/proc/sys/net/ipv6/conf/" VLAN_IFACE "/forwarding" ,"1" ); +if (!ASSERT_OK (err,"write_sysctl(net.ipv6.conf." VLAN_IFACE ".forwarding)" )) + goto fail; + +SYS (fail,"ip link add link veth1 name %s type vlan proto 802.1ad id 200" , +QINQ_OUTER_IFACE ); +SYS (fail,"ip link add link %s name %s type vlan id 300" , +QINQ_OUTER_IFACE ,QINQ_INNER_IFACE ); +SYS (fail,"ip link set dev %s up" ,QINQ_OUTER_IFACE ); +SYS (fail,"ip link set dev %s up" ,QINQ_INNER_IFACE ); +SYS (fail,"ip route add %s/32 dev %s" ,IPV4_QINQ_DST ,QINQ_INNER_IFACE ); + +SYS (fail,"ip route add %s/32 via %s" ,IPV4_VLAN_DST ,IPV4_GW1 ); +SYS (fail,"ip route add table %s %s/32 via %s" , +VLAN_TABLE ,IPV4_VLAN_DST ,IPV4_VLAN_GW ); +SYS (fail,"ip rule add prio 3 iif %s lookup %s" ,VLAN_IFACE ,VLAN_TABLE ); +SYS (fail,"ip -6 route add %s/128 via %s" ,IPV6_VLAN_DST ,IPV6_GW1 ); +SYS (fail,"ip -6 route add table %s %s/128 via %s" , +VLAN_TABLE ,IPV6_VLAN_DST ,IPV6_VLAN_GW ); +SYS (fail,"ip -6 rule add prio 3 iif %s lookup %s" ,VLAN_IFACE ,VLAN_TABLE ); + +/* +* a bond with one port and a VLAN on the bond: VLANs on a bond +* live on the master, so resolution succeeds for the master's +* ifindex and fails closed for a port's, matching receive, which +* steers the frame to the master before VLAN processing +*/ +SYS (fail,"ip link add %s type bond" ,BOND_IFACE ); +SYS (fail,"ip link add %s type veth peer name %s" ,BOND_PORT ,BOND_PORT_PEER ); +SYS (fail,"ip link set %s master %s" ,BOND_PORT ,BOND_IFACE ); +SYS (fail,"ip link set dev %s up" ,BOND_IFACE ); +SYS (fail,"ip link set dev %s up" ,BOND_PORT ); +SYS (fail,"ip link add link %s name %s.%d type vlan id %d" , +BOND_IFACE ,BOND_IFACE ,BOND_VLAN_ID ,BOND_VLAN_ID ); +SYS (fail,"ip link set dev %s.%d up" ,BOND_IFACE ,BOND_VLAN_ID ); +SYS (fail,"ip route add %s/32 dev %s.%d" , +IPV4_BOND_VLAN_DST ,BOND_IFACE ,BOND_VLAN_ID ); + +/* +* a VRF with its own dedicated subinterface (the iif rules above +* must not see it), for the table-selection-by-ingress cases +*/ +SYS (fail,"ip link add %s type vrf table %s" ,VRF_IFACE ,VRF_TABLE ); +SYS (fail,"ip link set dev %s up" ,VRF_IFACE ); +SYS (fail,"ip link add link veth1 name %s type vlan id %d" , +VRF_VLAN_IFACE ,VRF_VLAN_ID ); +SYS (fail,"ip link set %s master %s" ,VRF_VLAN_IFACE ,VRF_IFACE ); +SYS (fail,"ip link set dev %s up" ,VRF_VLAN_IFACE ); +SYS (fail,"ip addr add %s/24 dev %s" ,IPV4_VRF_IFACE_ADDR ,VRF_VLAN_IFACE ); + err =write_sysctl ("/proc/sys/net/ipv4/conf/" VRF_VLAN_IFACE "/forwarding" ,"1" ); +if (!ASSERT_OK (err,"write_sysctl(net.ipv4.conf." VRF_VLAN_IFACE ".forwarding)" )) + goto fail; +SYS (fail,"ip route add %s/32 via %s" ,IPV4_VRF_DST ,IPV4_GW1 ); +SYS (fail,"ip route add table %s %s/32 via %s" , +VRF_TABLE ,IPV4_VRF_DST ,IPV4_VRF_GW ); + +/* neighbours on the VLAN subinterface for the non-SKIP_NEIGH cases */ + err =write_sysctl ("/proc/sys/net/ipv4/neigh/" VLAN_IFACE "/gc_stale_time" ,"900" ); +if (!ASSERT_OK (err,"write_sysctl(net.ipv4.neigh." VLAN_IFACE ".gc_stale_time)" )) + goto fail; +SYS (fail,"ip neigh add %s dev %s lladdr %s nud stale" , +IPV4_VLAN_EGRESS_DST ,VLAN_IFACE ,DMAC ); +SYS (fail,"ip neigh add %s dev %s lladdr %s nud stale" , +IPV4_VLAN_GW ,VLAN_IFACE ,DMAC2 ); + +/* a VLAN on veth2 with a route in the tbid test table */ +SYS (fail,"ip link add link veth2 name %s type vlan id %d" , +TBID_VLAN_IFACE ,TBID_VLAN_ID ); +SYS (fail,"ip link set dev %s up" ,TBID_VLAN_IFACE ); +SYS (fail,"ip route add table 100 %s/32 dev %s" , +IPV4_TBID_VLAN_DST ,TBID_VLAN_IFACE ); + +/* a locked-mtu route via the subinterface for the FRAG_NEEDED case */ +SYS (fail,"ip route add %s/32 dev %s mtu lock 1000" , +IPV4_VLAN_MTU_DST ,VLAN_IFACE ); +return 0 ; fail:return -1 ;
@@ -218,9 +593,16 @@ static int set_lookup_params(struct bpf_fib_lookup *params,memset (params,0 ,sizeof (*params)); params->l4_protocol =IPPROTO_TCP ; - params->ifindex = ifindex; + params->ifindex = test->iif ?if_nametoindex (test->iif) : ifindex; params->tbid = test->tbid; params->mark = test->mark; + params->tot_len = test->tot_len; + +/* h_vlan_proto/h_vlan_TCI union with tbid */ +if (test->lookup_flags &BPF_FIB_LOOKUP_VLAN_INPUT ) { + params->h_vlan_proto =htons (test->vlan_proto); + params->h_vlan_TCI =htons (test->vlan_id); + }if (inet_pton (AF_INET6 , test->daddr, params->ipv6_dst) ==1 ) { params->family =AF_INET6 ;
@@ -298,7 +680,7 @@ void test_fib_lookup(void)struct nstoken *nstoken =NULL ;struct __sk_buff skb = { };struct fib_lookup *skel; -int prog_fd, err, ret, i; +int prog_fd, xdp_fd, err, ret, i;/* The test does not use the skb->data, so * use pkt_v6 for both v6 and v4 test.
@@ -309,11 +691,16 @@ void test_fib_lookup(void) .ctx_in = &skb, .ctx_size_in =sizeof (skb), ); +LIBBPF_OPTS (bpf_test_run_opts, xdp_opts, + .data_in = &pkt_v6, + .data_size_in = sizeof(pkt_v6), + ); skel =fib_lookup__open_and_load ();if (!ASSERT_OK_PTR (skel,"skel open_and_load" ))return ; prog_fd =bpf_program__fd (skel->progs.fib_lookup); + xdp_fd =bpf_program__fd (skel->progs.fib_lookup_xdp);SYS (fail,"ip netns add %s" ,NS_TEST );
@@ -343,6 +730,15 @@ void test_fib_lookup(void)if (!ASSERT_OK (err,"bpf_prog_test_run_opts" ))continue ; +/* BPF_FIB_LOOKUP_VLAN is XDP-only; the tc helper rejects it. +* These cases are exercised on the XDP path below. +*/ +if (tests[i].lookup_flags &BPF_FIB_LOOKUP_VLAN ) { +ASSERT_EQ (skel->bss->fib_lookup_ret, -EINVAL , +"tc rejects BPF_FIB_LOOKUP_VLAN" ); +continue ; + } +ASSERT_EQ (skel->bss->fib_lookup_ret, tests[i].expected_ret,"fib_lookup_ret" );
@@ -352,6 +748,21 @@ void test_fib_lookup(void)if (tests[i].expected_dst)assert_dst_ip (fib_params, tests[i].expected_dst); +if (tests[i].expected_dev) +ASSERT_EQ (fib_params->ifindex, +if_nametoindex (tests[i].expected_dev),"ifindex" ); + +if (tests[i].expected_mtu) +ASSERT_EQ (fib_params->mtu_result, tests[i].expected_mtu, +"mtu_result" ); + +if (tests[i].check_vlan) { +ASSERT_EQ (fib_params->h_vlan_proto, +htons (tests[i].vlan_proto),"h_vlan_proto" ); +ASSERT_EQ (fib_params->h_vlan_TCI, +htons (tests[i].vlan_id),"h_vlan_TCI" ); + } + ret =memcmp (tests[i].dmac, fib_params->dmac,sizeof (tests[i].dmac));if (!ASSERT_EQ (ret,0 ,"dmac not match" )) {char expected[18 ], actual[18 ];
@@ -361,15 +772,313 @@ void test_fib_lookup(void)printf ("dmac expected %s actual %s " , expected, actual); } -// ensure tbid is zero'd out after fib lookup. -if (tests[i].lookup_flags &BPF_FIB_LOOKUP_DIRECT ) { +/* +* ensure tbid is zero'd out after fib lookup. With +* BPF_FIB_LOOKUP_VLAN the union holds the packed vlan +* fields instead, so skip the check for those. +*/ +if ((tests[i].lookup_flags &BPF_FIB_LOOKUP_DIRECT ) && + !(tests[i].lookup_flags &BPF_FIB_LOOKUP_VLAN )) {if (!ASSERT_EQ (skel->bss->fib_params.tbid,0 ,"expected fib_params.tbid to be zero" )) goto fail; } } +/* +* Re-run the cases through bpf_xdp_fib_lookup(). test_run uses the +* current netns' loopback for ctx->rxq->dev, so dev_net() is NS_TEST +* and the lookup runs against its FIB. The path-independent results +* (return code, swapped ifindex, vlan tag, gateway) must match the skb +* path; the no-tot_len mtu_result is skb-specific and not rechecked. +*/ +for (i =0 ; i <ARRAY_SIZE (tests); i++) { +if (set_lookup_params (fib_params, &tests[i], skb.ifindex)) +continue ; + + skel->bss->fib_lookup_ret =-1 ; + skel->bss->lookup_flags = tests[i].lookup_flags; + + err =bpf_prog_test_run_opts (xdp_fd, &xdp_opts); +if (!ASSERT_OK (err,"xdp test_run" )) +continue ; + +if (!ASSERT_EQ (skel->bss->fib_lookup_ret, tests[i].expected_ret, +"xdp fib_lookup_ret" )) +printf ("(xdp) %s\n" , tests[i].desc); + +if (tests[i].expected_dev) +ASSERT_EQ (fib_params->ifindex, +if_nametoindex (tests[i].expected_dev), +"xdp ifindex" ); + +if (tests[i].expected_dst) +assert_dst_ip (fib_params, tests[i].expected_dst); + +if (tests[i].check_vlan) { +ASSERT_EQ (fib_params->h_vlan_proto, +htons (tests[i].vlan_proto),"xdp h_vlan_proto" ); +ASSERT_EQ (fib_params->h_vlan_TCI, +htons (tests[i].vlan_id),"xdp h_vlan_TCI" ); + } + + ret =memcmp (tests[i].dmac, fib_params->dmac,sizeof (tests[i].dmac)); +ASSERT_EQ (ret,0 ,"xdp dmac" ); + +/* +* mtu_result from a tot_len lookup is the route mtu and is +* path-independent; the no-tot_len arm reads dev->mtu and is +* skb-only, so gate on tot_len +*/ +if (tests[i].expected_mtu && tests[i].tot_len) +ASSERT_EQ (fib_params->mtu_result, tests[i].expected_mtu, +"xdp mtu_result" ); + } + +fail: +if (nstoken) +close_netns (nstoken); +SYS_NOFAIL ("ip netns del " NS_TEST ); +fib_lookup__destroy (skel); +} + +#define NS_VLAN_A "fib_lookup_vlan_ns_a" +#define NS_VLAN_B "fib_lookup_vlan_ns_b" + +/* +* A VLAN device can be moved to another netns while staying registered +* on its parent. Neither direction may then cross the boundary: the +* egress flag must not publish the foreign parent's ifindex, and the +* input flag must fail closed rather than use a foreign ingress. +*/ +void test_fib_lookup_vlan_netns (void ) +{ +struct bpf_fib_lookup *fib_params; +struct nstoken *nstoken =NULL ; +struct __sk_buff skb = { }; +struct fib_lookup *skel =NULL ; +int prog_fd, xdp_fd, err, parent_idx, vlan_idx; + +LIBBPF_OPTS (bpf_test_run_opts, run_opts, + .data_in = &pkt_v6, + .data_size_in = sizeof(pkt_v6), + .ctx_in = &skb, + .ctx_size_in = sizeof(skb), + ); +LIBBPF_OPTS (bpf_test_run_opts, xdp_opts, + .data_in = &pkt_v6, + .data_size_in = sizeof(pkt_v6), + ); + + skel =fib_lookup__open_and_load (); +if (!ASSERT_OK_PTR (skel,"skel open_and_load" )) +return ; + prog_fd =bpf_program__fd (skel->progs.fib_lookup); + xdp_fd =bpf_program__fd (skel->progs.fib_lookup_xdp); + fib_params = &skel->bss->fib_params; + +SYS (fail,"ip netns add %s" ,NS_VLAN_A ); +SYS (fail,"ip netns add %s" ,NS_VLAN_B ); + + nstoken =open_netns (NS_VLAN_A ); +if (!ASSERT_OK_PTR (nstoken,"open_netns(a)" )) + goto fail; + +SYS (fail,"ip link add veth7 type veth peer name veth8" ); +SYS (fail,"ip link set dev veth7 up" ); +SYS (fail,"ip link add link veth7 name veth7.66 type vlan id 66" ); +SYS (fail,"ip link set veth7.66 netns %s" ,NS_VLAN_B ); + + parent_idx =if_nametoindex ("veth7" ); +if (!ASSERT_NEQ (parent_idx,0 ,"if_nametoindex(veth7)" )) + goto fail; + +/* +* input: the moved device is still in veth7's VLAN group, but it +* lives in another netns, so the lookup must fail closed +*/ + skb.ifindex = parent_idx; +memset (fib_params,0 ,sizeof (*fib_params)); + fib_params->family =AF_INET ; + fib_params->l4_protocol =IPPROTO_TCP ; + fib_params->ifindex = parent_idx; + fib_params->h_vlan_proto =htons (ETH_P_8021Q ); + fib_params->h_vlan_TCI =htons (66 ); +if (!ASSERT_EQ (inet_pton (AF_INET ,"10.66.0.2" , &fib_params->ipv4_dst), +1 ,"inet_pton(dst)" )) + goto fail; + + skel->bss->fib_lookup_ret =-1 ; + skel->bss->lookup_flags =BPF_FIB_LOOKUP_VLAN_INPUT | +BPF_FIB_LOOKUP_SKIP_NEIGH ; + err =bpf_prog_test_run_opts (prog_fd, &run_opts); +if (!ASSERT_OK (err,"test_run(input)" )) + goto fail; +ASSERT_EQ (skel->bss->fib_lookup_ret,BPF_FIB_LKUP_RET_NOT_FWDED , +"input across netns fails closed" ); +ASSERT_EQ (fib_params->ifindex, parent_idx,"ifindex untouched" ); +ASSERT_EQ (fib_params->h_vlan_TCI,htons (66 ),"tag untouched" ); + +close_netns (nstoken); + nstoken =open_netns (NS_VLAN_B ); +if (!ASSERT_OK_PTR (nstoken,"open_netns(b)" )) + goto fail; + +/* +* egress: the fib result is the VLAN device here, but its parent +* is in the other netns, so the swap must not happen +*/ +SYS (fail,"ip link set dev veth7.66 up" ); +SYS (fail,"ip addr add 10.66.0.1/24 dev veth7.66" ); + err =write_sysctl ("/proc/sys/net/ipv4/conf/veth7.66/forwarding" ,"1" ); +if (!ASSERT_OK (err,"write_sysctl(forwarding)" )) + goto fail; + + vlan_idx =if_nametoindex ("veth7.66" ); +if (!ASSERT_NEQ (vlan_idx,0 ,"if_nametoindex(veth7.66)" )) + goto fail; + + skb.ifindex = vlan_idx; +memset (fib_params,0 ,sizeof (*fib_params)); + fib_params->family =AF_INET ; + fib_params->l4_protocol =IPPROTO_TCP ; + fib_params->ifindex = vlan_idx; +if (!ASSERT_EQ (inet_pton (AF_INET ,"10.66.0.2" , &fib_params->ipv4_dst), +1 ,"inet_pton(dst)" ) || + !ASSERT_EQ (inet_pton (AF_INET ,"10.66.0.1" , &fib_params->ipv4_src), +1 ,"inet_pton(src)" )) + goto fail; + + skel->bss->fib_lookup_ret =-1 ; + skel->bss->lookup_flags =BPF_FIB_LOOKUP_VLAN | +BPF_FIB_LOOKUP_SKIP_NEIGH ; + err =bpf_prog_test_run_opts (xdp_fd, &xdp_opts); +if (!ASSERT_OK (err,"test_run(egress)" )) + goto fail; +ASSERT_EQ (skel->bss->fib_lookup_ret,BPF_FIB_LKUP_RET_VLAN_FAILURE , +"egress returns VLAN_FAILURE" ); +ASSERT_EQ (fib_params->ifindex, vlan_idx, +"foreign parent not published" ); +ASSERT_EQ (fib_params->h_vlan_TCI,0 ,"vlan fields zero" ); + +fail: +if (nstoken) +close_netns (nstoken); +SYS_NOFAIL ("ip netns del " NS_VLAN_A ); +SYS_NOFAIL ("ip netns del " NS_VLAN_B ); +fib_lookup__destroy (skel); +} + +#define REDIRECT_NPKTS 1000 + +/* +* The egress flag exists so an XDP program can redirect to the physical +* parent. A redirect that lands on a VLAN device is dropped at +* xdp_do_flush(), because a VLAN device has no ndo_xdp_xmit. Drive real +* frames with BPF_F_TEST_XDP_LIVE_FRAMES, which runs the native +* xdp_do_redirect() + xdp_do_flush() path: a reducible VLAN egress +* resolves to veth1 and is delivered to its peer veth2, while a QinQ +* egress returns VLAN_FAILURE and is passed to the stack instead of +* redirected to a device that would silently drop it. +*/ +void test_fib_lookup_vlan_redirect (void ) +{ +int redirect_fd, err, veth1_idx, veth2_idx =-1 ; +struct bpf_fib_lookup *fib_params; +struct nstoken *nstoken =NULL ; +struct fib_lookup *skel =NULL ; +bool xdp_attached = false; + +LIBBPF_OPTS (bpf_test_run_opts, lf_opts, + .data_in = &pkt_v4, + .data_size_in = sizeof(pkt_v4), + .flags = BPF_F_TEST_XDP_LIVE_FRAMES, + .repeat = REDIRECT_NPKTS, + ); + + skel =fib_lookup__open_and_load (); +if (!ASSERT_OK_PTR (skel,"skel open_and_load" )) +return ; + redirect_fd =bpf_program__fd (skel->progs.fib_lookup_redirect); + fib_params = &skel->bss->fib_params; + +SYS (fail,"ip netns add %s" ,NS_TEST ); + nstoken =open_netns (NS_TEST ); +if (!ASSERT_OK_PTR (nstoken,"open_netns" )) + goto fail; +if (setup_netns ()) + goto fail; + + veth1_idx =if_nametoindex ("veth1" ); + veth2_idx =if_nametoindex ("veth2" ); +if (!ASSERT_NEQ (veth1_idx,0 ,"if_nametoindex(veth1)" ) || + !ASSERT_NEQ (veth2_idx,0 ,"if_nametoindex(veth2)" )) + goto fail; + +/* +* A redirect to veth1 is delivered to its peer veth2. veth_xdp_xmit() +* only accepts the frame if veth2's NAPI is up, which on veth means +* veth2 carries an XDP program; xdp_count tallies what arrives. +*/ + err =bpf_xdp_attach (veth2_idx,bpf_program__fd (skel->progs.xdp_count), +XDP_FLAGS_DRV_MODE ,NULL ); +if (!ASSERT_OK (err,"attach xdp_count on veth2" )) + goto fail; + xdp_attached = true; + +/* reducible VLAN egress: resolves to the physical parent veth1 */ +memset (fib_params,0 ,sizeof (*fib_params)); + fib_params->family =AF_INET ; + fib_params->l4_protocol =IPPROTO_TCP ; + fib_params->ifindex = veth1_idx; +if (!ASSERT_EQ (inet_pton (AF_INET ,IPV4_IFACE_ADDR , &fib_params->ipv4_src), +1 ,"inet_pton(src)" ) || + !ASSERT_EQ (inet_pton (AF_INET ,IPV4_VLAN_EGRESS_DST , &fib_params->ipv4_dst), +1 ,"inet_pton(reducible dst)" )) + goto fail; + skel->bss->lookup_flags =BPF_FIB_LOOKUP_VLAN |BPF_FIB_LOOKUP_SKIP_NEIGH ; + skel->bss->redirected =0 ; + skel->bss->passed =0 ; + skel->bss->delivered =0 ; + + err =bpf_prog_test_run_opts (redirect_fd, &lf_opts); +if (!ASSERT_OK (err,"test_run(reducible egress)" )) + goto fail; +ASSERT_EQ (skel->bss->redirected,REDIRECT_NPKTS ,"reducible egress redirected" ); +ASSERT_EQ (skel->bss->passed,0 ,"reducible egress not passed" ); +ASSERT_GT (skel->bss->delivered,0 ,"reducible egress delivered to veth2" ); + +/* +* QinQ egress: not reducible, so the lookup returns VLAN_FAILURE and +* the program passes the frame instead of redirecting to the inner +* VLAN device. redirected == 0 is the assertion that matters: the +* program did not redirect to a device that would drop the frame at +* xdp_do_flush(). veth2's delivered count is not checked here, since +* a passed frame can still reach veth2 through the stack's forwarding +* path, which is unrelated to the redirect under test. +*/ +memset (fib_params,0 ,sizeof (*fib_params)); + fib_params->family =AF_INET ; + fib_params->l4_protocol =IPPROTO_TCP ; + fib_params->ifindex = veth1_idx; +if (!ASSERT_EQ (inet_pton (AF_INET ,IPV4_IFACE_ADDR , &fib_params->ipv4_src), +1 ,"inet_pton(src)" ) || + !ASSERT_EQ (inet_pton (AF_INET ,IPV4_QINQ_DST , &fib_params->ipv4_dst), +1 ,"inet_pton(qinq dst)" )) + goto fail; + skel->bss->lookup_flags =BPF_FIB_LOOKUP_VLAN |BPF_FIB_LOOKUP_SKIP_NEIGH ; + skel->bss->redirected =0 ; + skel->bss->passed =0 ; + + err =bpf_prog_test_run_opts (redirect_fd, &lf_opts); +if (!ASSERT_OK (err,"test_run(qinq egress)" )) + goto fail; +ASSERT_EQ (skel->bss->passed,REDIRECT_NPKTS ,"qinq egress passed" ); +ASSERT_EQ (skel->bss->redirected,0 ,"qinq egress not redirected" ); + fail: +if (xdp_attached) +bpf_xdp_detach (veth2_idx,XDP_FLAGS_DRV_MODE ,NULL );if (nstoken)close_netns (nstoken);SYS_NOFAIL ("ip netns del " NS_TEST );
tools/testing/selftests/bpf/progs/fib_lookup.c +36 -0
@@ -19,4 +19,40 @@ int fib_lookup(struct __sk_buff *skb)return TC_ACT_SHOT ; } +SEC("xdp") +int fib_lookup_xdp (struct xdp_md *ctx) +{ + fib_lookup_ret =bpf_fib_lookup (ctx, &fib_params,sizeof (fib_params), + lookup_flags); + +return XDP_DROP ; +} + +int redirected =0 ; +int passed =0 ; +int delivered =0 ; + +SEC("xdp") +int fib_lookup_redirect (struct xdp_md *ctx) +{ +struct bpf_fib_lookup params = fib_params; +long ret; + + ret =bpf_fib_lookup (ctx, ¶ms,sizeof (params), lookup_flags); +if (ret ==BPF_FIB_LKUP_RET_SUCCESS ) { + redirected++; +return bpf_redirect (params.ifindex,0 ); + } + + passed++; +return XDP_PASS ; +} + +SEC("xdp") +int xdp_count (struct xdp_md *ctx) +{ + delivered++; +return XDP_DROP ; +} + char _license[] SEC("license") = "GPL";
Signature · 1 line
2.54.0
[email protected]
AI reviewRe: [PATCH bpf-next v5 3/3] selftests/bpf: Add bpf_fib_lookup() VLAN flag tests
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [Low] Multi-line comment does not follow the modern BPF kernel style. - [Low] Dead write to `skb.ifindex`…
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Low] Multi-line comment does not follow the modern BPF kernel style.
- [Low] Dead write to skb.ifindex before running the XDP test in test_fib_lookup_vlan_netns. --
commit 8850d806ed94f65fe16bb923b209b37fa59c65ae
Author: Avinash Duduskar <[email protected]>
selftests/bpf: Add bpf_fib_lookup() VLAN flag tests
This commit introduces selftests for the bpf_fib_lookup() VLAN flags. It covers both BPF_FIB_LOOKUP_VLAN for egress and BPF_FIB_LOOKUP_VLAN_INPUT for scoping the lookup to a VLAN subinterface.
Quoted 4 lines
tools/testing/selftests/bpf/prog_tests/fib_lookup.c +0
-0
[ ... ]
tools/testing/selftests/bpf/prog_tests/fib_lookup.c void test_fib_lookup(void) · line 735
Quoted 7 lines
tools/testing/selftests/bpf/prog_tests/fib_lookup.c +3
-0@@ -343,6 +730,15 @@ void test_fib_lookup(void)if (!ASSERT_OK (err,"bpf_prog_test_run_opts" ))continue ; +/* BPF_FIB_LOOKUP_VLAN is XDP-only; the tc helper rejects it. +* These cases are exercised on the XDP path below. +*/
[Severity: Low]
This isn't a bug, but this multi-line comment does not follow the preferred kernel comment style for the BPF subsystem, which places the opening /* on its own line:
/*
* BPF_FIB_LOOKUP_VLAN is XDP-only; the tc helper rejects it.
* These cases are exercised on the XDP path below. */
Quoted 5 lines
tools/testing/selftests/bpf/prog_tests/fib_lookup.c +5
-0+if (tests[i].lookup_flags &BPF_FIB_LOOKUP_VLAN ) { +ASSERT_EQ (skel->bss->fib_lookup_ret, -EINVAL , +"tc rejects BPF_FIB_LOOKUP_VLAN" ); +continue ; + }
[ ... ]
Quoted 5 lines
tools/testing/selftests/bpf/prog_tests/fib_lookup.c +5
-0+/* +* egress: the fib result is the VLAN device here, but its parent +* is in the other netns, so the swap must not happen +*/ +SYS (fail,"ip link set dev veth7.66 up" );
tools/testing/selftests/bpf/prog_tests/fib_lookup.c void test_fib_lookup(void) · line 941
Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/fib_lookup.c +10
-0+SYS (fail,"ip addr add 10.66.0.1/24 dev veth7.66" ); + err =write_sysctl ("/proc/sys/net/ipv4/conf/veth7.66/forwarding" ,"1" ); +if (!ASSERT_OK (err,"write_sysctl(forwarding)" )) + goto fail; + + vlan_idx =if_nametoindex ("veth7.66" ); +if (!ASSERT_NEQ (vlan_idx,0 ,"if_nametoindex(veth7.66)" )) + goto fail; + + skb.ifindex = vlan_idx;
[Severity: Low]
Is this assignment to skb.ifindex necessary? The subsequent test run uses xdp_opts, which doesn't specify a context (unlike run_opts which passes skb as ctx_in), so it appears the skb is ignored here.
Quoted 16 lines
tools/testing/selftests/bpf/prog_tests/fib_lookup.c +16
-0+memset (fib_params,0 ,sizeof (*fib_params)); + fib_params->family =AF_INET ; + fib_params->l4_protocol =IPPROTO_TCP ; + fib_params->ifindex = vlan_idx; +if (!ASSERT_EQ (inet_pton (AF_INET ,"10.66.0.2" , &fib_params->ipv4_dst), +1 ,"inet_pton(dst)" ) || + !ASSERT_EQ (inet_pton (AF_INET ,"10.66.0.1" , &fib_params->ipv4_src), +1 ,"inet_pton(src)" )) + goto fail; + + skel->bss->fib_lookup_ret =-1 ; + skel->bss->lookup_flags =BPF_FIB_LOOKUP_VLAN | +BPF_FIB_LOOKUP_SKIP_NEIGH ; + err =bpf_prog_test_run_opts (xdp_fd, &xdp_opts); +if (!ASSERT_OK (err,"test_run(egress)" )) + goto fail;
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3