Skip to content

Commit ed77f2d

Browse files
orospivecera
authored andcommitted
xdp: Add VLAN tag hint
JIRA: https://issues.redhat.com/browse/RHEL-31890 Conflicts: - adjusted conflict that was resolved upstream by commit 753c860 ("Merge tag 'for-netdev' of https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next") - xmo_rx_vlan_tag is placed in netdevice.h instead of xdp.h due to missing 680ee04 ("net: invert the netdevice.h vs xdp.h dependency") Upstream commit(s): commit e679533 Author: Larysa Zaremba <larysa.zaremba@intel.com> Date: Tue Dec 5 22:08:38 2023 +0100 xdp: Add VLAN tag hint Implement functionality that enables drivers to expose VLAN tag to XDP code. VLAN tag is represented by 2 variables: - protocol ID, which is passed to bpf code in BE - VLAN TCI, in host byte order Acked-by: Stanislav Fomichev <sdf@google.com> Signed-off-by: Larysa Zaremba <larysa.zaremba@intel.com> Acked-by: Jesper Dangaard Brouer <hawk@kernel.org> Link: https://lore.kernel.org/r/20231205210847.28460-10-larysa.zaremba@intel.com Signed-off-by: Alexei Starovoitov <ast@kernel.org> Signed-off-by: Petr Oros <poros@redhat.com>
1 parent 434aaea commit ed77f2d

File tree

8 files changed

+57
-10
lines changed

8 files changed

+57
-10
lines changed

Documentation/netlink/specs/netdev.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ definitions:
5454
name: hash
5555
doc:
5656
Device is capable of exposing receive packet hash via bpf_xdp_metadata_rx_hash().
57+
-
58+
name: vlan-tag
59+
doc:
60+
Device is capable of exposing receive packet VLAN tag via bpf_xdp_metadata_rx_vlan_tag().
5761
-
5862
type: flags
5963
name: xsk-flags

Documentation/networking/xdp-rx-metadata.rst

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,13 @@ Currently, the following kfuncs are supported. In the future, as more
2020
metadata is supported, this set will grow:
2121

2222
.. kernel-doc:: net/core/xdp.c
23-
:identifiers: bpf_xdp_metadata_rx_timestamp bpf_xdp_metadata_rx_hash
23+
:identifiers: bpf_xdp_metadata_rx_timestamp
24+
25+
.. kernel-doc:: net/core/xdp.c
26+
:identifiers: bpf_xdp_metadata_rx_hash
27+
28+
.. kernel-doc:: net/core/xdp.c
29+
:identifiers: bpf_xdp_metadata_rx_vlan_tag
2430

2531
An XDP program can use these kfuncs to read the metadata into stack
2632
variables for its own consumption. Or, to pass the metadata on to other

include/linux/netdevice.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1610,6 +1610,8 @@ struct xdp_metadata_ops {
16101610
int (*xmo_rx_timestamp)(const struct xdp_md *ctx, u64 *timestamp);
16111611
int (*xmo_rx_hash)(const struct xdp_md *ctx, u32 *hash,
16121612
enum xdp_rss_hash_type *rss_type);
1613+
int (*xmo_rx_vlan_tag)(const struct xdp_md *ctx, __be16 *vlan_proto,
1614+
u16 *vlan_tci);
16131615
};
16141616

16151617
/**

include/net/xdp.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,10 @@ void xdp_attachment_setup(struct xdp_attachment_info *info,
406406
NETDEV_XDP_RX_METADATA_HASH, \
407407
bpf_xdp_metadata_rx_hash, \
408408
xmo_rx_hash) \
409+
XDP_METADATA_KFUNC(XDP_METADATA_KFUNC_RX_VLAN_TAG, \
410+
NETDEV_XDP_RX_METADATA_VLAN_TAG, \
411+
bpf_xdp_metadata_rx_vlan_tag, \
412+
xmo_rx_vlan_tag) \
409413

410414
enum xdp_rx_metadata {
411415
#define XDP_METADATA_KFUNC(name, _, __, ___) name,

include/uapi/linux/netdev.h

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,13 @@ enum netdev_xdp_act {
4444
* timestamp via bpf_xdp_metadata_rx_timestamp().
4545
* @NETDEV_XDP_RX_METADATA_HASH: Device is capable of exposing receive packet
4646
* hash via bpf_xdp_metadata_rx_hash().
47+
* @NETDEV_XDP_RX_METADATA_VLAN_TAG: Device is capable of exposing receive
48+
* packet VLAN tag via bpf_xdp_metadata_rx_vlan_tag().
4749
*/
4850
enum netdev_xdp_rx_metadata {
4951
NETDEV_XDP_RX_METADATA_TIMESTAMP = 1,
5052
NETDEV_XDP_RX_METADATA_HASH = 2,
51-
52-
/* private: */
53-
NETDEV_XDP_RX_METADATA_MASK = 3,
53+
NETDEV_XDP_RX_METADATA_VLAN_TAG = 4,
5454
};
5555

5656
/**
@@ -63,9 +63,6 @@ enum netdev_xdp_rx_metadata {
6363
enum netdev_xsk_flags {
6464
NETDEV_XSK_FLAGS_TX_TIMESTAMP = 1,
6565
NETDEV_XSK_FLAGS_TX_CHECKSUM = 2,
66-
67-
/* private: */
68-
NETDEV_XSK_FLAGS_MASK = 3,
6966
};
7067

7168
enum {

net/core/xdp.c

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -737,6 +737,39 @@ __bpf_kfunc int bpf_xdp_metadata_rx_hash(const struct xdp_md *ctx, u32 *hash,
737737
return -EOPNOTSUPP;
738738
}
739739

740+
/**
741+
* bpf_xdp_metadata_rx_vlan_tag - Get XDP packet outermost VLAN tag
742+
* @ctx: XDP context pointer.
743+
* @vlan_proto: Destination pointer for VLAN Tag protocol identifier (TPID).
744+
* @vlan_tci: Destination pointer for VLAN TCI (VID + DEI + PCP)
745+
*
746+
* In case of success, ``vlan_proto`` contains *Tag protocol identifier (TPID)*,
747+
* usually ``ETH_P_8021Q`` or ``ETH_P_8021AD``, but some networks can use
748+
* custom TPIDs. ``vlan_proto`` is stored in **network byte order (BE)**
749+
* and should be used as follows:
750+
* ``if (vlan_proto == bpf_htons(ETH_P_8021Q)) do_something();``
751+
*
752+
* ``vlan_tci`` contains the remaining 16 bits of a VLAN tag.
753+
* Driver is expected to provide those in **host byte order (usually LE)**,
754+
* so the bpf program should not perform byte conversion.
755+
* According to 802.1Q standard, *VLAN TCI (Tag control information)*
756+
* is a bit field that contains:
757+
* *VLAN identifier (VID)* that can be read with ``vlan_tci & 0xfff``,
758+
* *Drop eligible indicator (DEI)* - 1 bit,
759+
* *Priority code point (PCP)* - 3 bits.
760+
* For detailed meaning of DEI and PCP, please refer to other sources.
761+
*
762+
* Return:
763+
* * Returns 0 on success or ``-errno`` on error.
764+
* * ``-EOPNOTSUPP`` : device driver doesn't implement kfunc
765+
* * ``-ENODATA`` : VLAN tag was not stripped or is not available
766+
*/
767+
__bpf_kfunc int bpf_xdp_metadata_rx_vlan_tag(const struct xdp_md *ctx,
768+
__be16 *vlan_proto, u16 *vlan_tci)
769+
{
770+
return -EOPNOTSUPP;
771+
}
772+
740773
__bpf_kfunc_end_defs();
741774

742775
BTF_SET8_START(xdp_metadata_kfunc_ids)

tools/include/uapi/linux/netdev.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,13 @@ enum netdev_xdp_act {
4444
* timestamp via bpf_xdp_metadata_rx_timestamp().
4545
* @NETDEV_XDP_RX_METADATA_HASH: Device is capable of exposing receive packet
4646
* hash via bpf_xdp_metadata_rx_hash().
47+
* @NETDEV_XDP_RX_METADATA_VLAN_TAG: Device is capable of exposing receive
48+
* packet VLAN tag via bpf_xdp_metadata_rx_vlan_tag().
4749
*/
4850
enum netdev_xdp_rx_metadata {
4951
NETDEV_XDP_RX_METADATA_TIMESTAMP = 1,
5052
NETDEV_XDP_RX_METADATA_HASH = 2,
51-
52-
/* private: */
53-
NETDEV_XDP_RX_METADATA_MASK = 3,
53+
NETDEV_XDP_RX_METADATA_VLAN_TAG = 4,
5454
};
5555

5656
/**

tools/net/ynl/generated/netdev-user.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ const char *netdev_xdp_act_str(enum netdev_xdp_act value)
5353
static const char * const netdev_xdp_rx_metadata_strmap[] = {
5454
[0] = "timestamp",
5555
[1] = "hash",
56+
[2] = "vlan-tag",
5657
};
5758

5859
const char *netdev_xdp_rx_metadata_str(enum netdev_xdp_rx_metadata value)

0 commit comments

Comments
 (0)