Add a drv-net test for the skb core extension area. An XDP program
stores an RX hash, a VLAN tag and a checksum status. A tc program on
ingress reads the skb fields, and the test compares them with the
stored values.

The stored hash and VLAN tag are values that the NIC does not report:
hash 0x12345678 and an 802.1ad tag with VID 0xabc. The checksum status
is tested as CHECKSUM_NONE and as CHECKSUM_UNNECESSARY with csum_level
1. The NIC reports only one of them, so a driver that ignores the
stored values fails at least one case.

The test runs only on devices with the skb-ext-pass XDP feature. GRO is
off, because it can change ip_summed before the tc program runs. Like
test_xdp_rss_hash, the test sets its config map by name with
bpf_map_set().

Signed-off-by: Tianyi Gao <[email protected]>
---
veth has the skb-ext-pass feature, but in drv-net the test packet reaches
veth as an skb. The stored values are then lost, so the test fails on
veth. NIPA runs drv-net on netdevsim, where the test skips, and on real
NICs.
---
 .../drivers/net/hw/lib/py/__init__.py         |   3 +-
 .../selftests/drivers/net/hw/xdp_metadata.py  | 118 ++++++++++++-
 .../drivers/net/hw/xdp_skb_ext.bpf.c          | 166 ++++++++++++++++++
 3 files changed, 285 insertions(+), 2 deletions(-)
 create mode 100644 tools/testing/selftests/drivers/net/hw/xdp_skb_ext.bpf.c

diff --git a/tools/testing/selftests/drivers/net/hw/lib/py/__init__.py 
b/tools/testing/selftests/drivers/net/hw/lib/py/__init__.py
index 4077f5f1a1be..8a6702abcdbe 100644
--- a/tools/testing/selftests/drivers/net/hw/lib/py/__init__.py
+++ b/tools/testing/selftests/drivers/net/hw/lib/py/__init__.py
@@ -24,7 +24,7 @@ try:
     from net.lib.py import CmdExitFailure
     from net.lib.py import bkg, cmd, bpftool, bpftrace, defer, ethtool, \
         fd_read_timeout, ip, rand_port, rand_ports, wait_port_listen, \
-        wait_file, ctl_file_write, tool
+        wait_file, ctl_file_write, tool, tc
     from net.lib.py import bpf_map_set, bpf_map_dump, bpf_prog_map_ids
     from net.lib.py import KsftSkipEx, KsftFailEx, KsftXfailEx
     from net.lib.py import ksft_disruptive, ksft_exit, ksft_pr, ksft_run, \
@@ -42,6 +42,7 @@ try:
                "bkg", "cmd", "bpftool", "bpftrace", "defer", "ethtool",
                "fd_read_timeout", "ip", "rand_port", "rand_ports",
                "wait_port_listen", "wait_file", "ctl_file_write", "tool",
+               "tc",
                "bpf_map_set", "bpf_map_dump", "bpf_prog_map_ids",
                "KsftSkipEx", "KsftFailEx", "KsftXfailEx",
                "ksft_disruptive", "ksft_exit", "ksft_pr", "ksft_run",
diff --git a/tools/testing/selftests/drivers/net/hw/xdp_metadata.py 
b/tools/testing/selftests/drivers/net/hw/xdp_metadata.py
index 33a1985356d9..88b242964cff 100644
--- a/tools/testing/selftests/drivers/net/hw/xdp_metadata.py
+++ b/tools/testing/selftests/drivers/net/hw/xdp_metadata.py
@@ -7,13 +7,18 @@ Tests for XDP metadata kfuncs (e.g. bpf_xdp_metadata_rx_hash).
 These tests load device-bound XDP programs from xdp_metadata.bpf.o
 that call metadata kfuncs, send traffic, and verify the extracted
 metadata via BPF maps.
+
+The skb core extension test loads xdp_skb_ext.bpf.o: an XDP program
+stores RX metadata and a tc program checks it on the skb.
 """
+import socket
+
 from lib.py import ksft_run, ksft_eq, ksft_exit, ksft_ge, ksft_ne, ksft_pr
 from lib.py import KsftNamedVariant, ksft_variants
 from lib.py import CmdExitFailure, KsftSkipEx, NetDrvEpEnv
 from lib.py import NetdevFamily
 from lib.py import bkg, cmd, rand_port, wait_port_listen
-from lib.py import ip, bpftool, defer
+from lib.py import ip, bpftool, defer, ethtool, tc, set_ethtool_feat
 from lib.py import bpf_map_set, bpf_map_dump, bpf_prog_map_ids
 
 
@@ -130,6 +135,116 @@ def test_xdp_rss_hash(cfg, proto):
             f"RSS hash type should include L4 for {proto.upper()} traffic")
 
 
+# BPF map keys matching the enums in xdp_skb_ext.bpf.c
+_CFG_PORT = 0
+_CFG_HASH = 1
+_CFG_VLAN_PROTO = 2
+_CFG_VLAN_TCI = 3
+_CFG_IP_SUMMED = 4
+_CFG_CSUM_LEVEL = 5
+
+_RES_PKT_CNT = 0
+_RES_STORE_ERR = 1
+_RES_HASH = 2
+_RES_L4_HASH = 3
+_RES_VLAN_PROTO = 4
+_RES_VLAN_TCI = 5
+_RES_IP_SUMMED = 6
+_RES_CSUM_LEVEL = 7
+
+ETH_P_8021AD = 0x88a8
+VLAN_PRIO_SHIFT = 13
+
+# Magic numbers the NIC does not report
+TEST_RX_HASH = 0x12345678
+TEST_VLAN_PCP = 5
+TEST_VLAN_VID = 0xabc
+TEST_VLAN_TCI = TEST_VLAN_PCP << VLAN_PRIO_SHIFT | TEST_VLAN_VID
+
+XDP_CHECKSUM_NONE = 1 << 0
+XDP_CHECKSUM_UNNECESSARY = 1 << 1
+CHECKSUM_NONE = 0
+CHECKSUM_UNNECESSARY = 1
+
+
+def _load_skb_ext_progs(cfg):
+    """Attach the store program to XDP and the check program to tc ingress.
+
+    Returns:
+        A dict mapping map name to map ID.
+    """
+    abs_path = cfg.test_dir / "xdp_skb_ext.bpf.o"
+    pin_dir = "/sys/fs/bpf/xdp_skb_ext_test"
+
+    cmd(f"rm -rf {pin_dir}", shell=True, fail=False)
+    cmd(f"mkdir -p {pin_dir}", shell=True)
+    defer(cmd, f"rm -rf {pin_dir}", shell=True, fail=False)
+
+    bpftool(f"prog loadall {abs_path} {pin_dir}")
+    ip(f"link set dev {cfg.ifname} xdpdrv pinned {pin_dir}/xdp_store")
+    defer(ip, f"link set dev {cfg.ifname} xdpdrv off")
+
+    qdiscs = tc(f"qdisc show dev {cfg.ifname}", json=True)
+    if not any(q["kind"] == "clsact" for q in qdiscs):
+        tc(f"qdisc add dev {cfg.ifname} clsact")
+        defer(tc, f"qdisc del dev {cfg.ifname} clsact")
+
+    tc(f"filter add dev {cfg.ifname} ingress bpf "
+       f"object-pinned {pin_dir}/tc_check direct-action")
+    prog_id = bpftool(f"prog show pinned {pin_dir}/tc_check", json=True)["id"]
+    for f in tc(f"filter show dev {cfg.ifname} ingress", json=True):
+        if f.get("options", {}).get("prog", {}).get("id") == prog_id:
+            defer(tc, f"filter del dev {cfg.ifname} ingress pref {f['pref']}")
+
+    return bpf_prog_map_ids(prog_id)
+
+
+@ksft_variants([
+    KsftNamedVariant("csum_none", XDP_CHECKSUM_NONE, 0, CHECKSUM_NONE),
+    KsftNamedVariant("csum_unnecessary", XDP_CHECKSUM_UNNECESSARY, 1,
+                     CHECKSUM_UNNECESSARY),
+])
+def test_xdp_skb_ext(cfg, xdp_csum, csum_level, ip_summed):
+    """Test that the skb gets the RX metadata stored on XDP_PASS.
+
+    The XDP program stores a hash, a VLAN tag and a checksum status that
+    differ from what the NIC reports. The tc program reads them from the
+    skb.
+    """
+    dev_info = cfg.netnl.dev_get({"ifindex": cfg.ifindex})
+    if "skb-ext-pass" not in dev_info.get("xdp-features", []):
+        raise KsftSkipEx("device does not support skb-ext-pass")
+
+    maps = _load_skb_ext_progs(cfg)
+
+    # GRO can change ip_summed before tc ingress
+    feat = ethtool(f"-k {cfg.ifname}", json=True)[0]
+    set_ethtool_feat(cfg.ifname, feat, {"generic-receive-offload": False})
+
+    cfg_map = "map_store_cfg"
+    bpf_map_set(cfg_map, _CFG_HASH, TEST_RX_HASH)
+    bpf_map_set(cfg_map, _CFG_VLAN_PROTO, ETH_P_8021AD)
+    bpf_map_set(cfg_map, _CFG_VLAN_TCI, TEST_VLAN_TCI)
+    bpf_map_set(cfg_map, _CFG_IP_SUMMED, xdp_csum)
+    bpf_map_set(cfg_map, _CFG_CSUM_LEVEL, csum_level)
+    # The port goes last: it enables the store for the test packets
+    port = rand_port(socket.SOCK_DGRAM)
+    bpf_map_set(cfg_map, _CFG_PORT, port)
+
+    _send_probe(cfg, port, proto="udp")
+
+    res = bpf_map_dump(maps["map_store_res"])
+    ksft_ge(res.get(_RES_PKT_CNT, 0), 1, "tc saw the test packet")
+    ksft_eq(res.get(_RES_STORE_ERR, 0), 0, "store kfunc errors")
+
+    ksft_eq(res.get(_RES_HASH, 0), TEST_RX_HASH, "skb->hash")
+    ksft_eq(res.get(_RES_L4_HASH, 0), 1, "skb->l4_hash")
+    ksft_eq(res.get(_RES_VLAN_PROTO, 0), ETH_P_8021AD, "skb->vlan_proto")
+    ksft_eq(res.get(_RES_VLAN_TCI, 0), TEST_VLAN_TCI, "skb->vlan_tci")
+    ksft_eq(res.get(_RES_IP_SUMMED, 0), ip_summed, "skb->ip_summed")
+    ksft_eq(res.get(_RES_CSUM_LEVEL, 0), csum_level, "skb->csum_level")
+
+
 def main():
     """Run XDP metadata kfunc tests against a real device."""
     with NetDrvEpEnv(__file__) as cfg:
@@ -137,6 +252,7 @@ def main():
         ksft_run(
             [
                 test_xdp_rss_hash,
+                test_xdp_skb_ext,
             ],
             args=(cfg,))
     ksft_exit()
diff --git a/tools/testing/selftests/drivers/net/hw/xdp_skb_ext.bpf.c 
b/tools/testing/selftests/drivers/net/hw/xdp_skb_ext.bpf.c
new file mode 100644
index 000000000000..b186bc1f7947
--- /dev/null
+++ b/tools/testing/selftests/drivers/net/hw/xdp_skb_ext.bpf.c
@@ -0,0 +1,166 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include <stdbool.h>
+#include <linux/bpf.h>
+#include <linux/if_ether.h>
+#include <linux/in.h>
+#include <linux/ip.h>
+#include <linux/ipv6.h>
+#include <linux/pkt_cls.h>
+#include <linux/udp.h>
+#include <bpf/bpf_core_read.h>
+#include <bpf/bpf_endian.h>
+#include <bpf/bpf_helpers.h>
+
+enum {
+       CFG_PORT,
+       CFG_HASH,
+       CFG_VLAN_PROTO,
+       CFG_VLAN_TCI,
+       CFG_IP_SUMMED,
+       CFG_CSUM_LEVEL,
+       CFG_MAX,
+};
+
+struct {
+       __uint(type, BPF_MAP_TYPE_ARRAY);
+       __uint(max_entries, CFG_MAX);
+       __type(key, __u32);
+       __type(value, __u32);
+} map_store_cfg SEC(".maps");
+
+enum {
+       RES_PKT_CNT,
+       RES_STORE_ERR,
+       RES_HASH,
+       RES_L4_HASH,
+       RES_VLAN_PROTO,
+       RES_VLAN_TCI,
+       RES_IP_SUMMED,
+       RES_CSUM_LEVEL,
+       RES_MAX,
+};
+
+struct {
+       __uint(type, BPF_MAP_TYPE_ARRAY);
+       __uint(max_entries, RES_MAX);
+       __type(key, __u32);
+       __type(value, __u32);
+} map_store_res SEC(".maps");
+
+/* Mirrors of include/net/xdp.h, not part of UAPI */
+enum xdp_rss_hash_type {
+       XDP_RSS_L4 = 1U << 3,
+};
+
+enum xdp_checksum {
+       XDP_CHECKSUM_NONE = 1U << 0,
+       XDP_CHECKSUM_UNNECESSARY = 1U << 1,
+};
+
+struct sk_buff {
+       __u8 ip_summed:2;
+       __u8 csum_level:2;
+       __u8 l4_hash:1;
+} __attribute__((preserve_access_index));
+
+extern int bpf_xdp_skb_ext_store_rx_hash(struct xdp_md *, __u32,
+                                        enum xdp_rss_hash_type) __ksym;
+extern int bpf_xdp_skb_ext_store_rx_vlan(struct xdp_md *, __be16,
+                                        __u16) __ksym;
+extern int bpf_xdp_skb_ext_store_rx_csum(struct xdp_md *, enum xdp_checksum,
+                                        __u8) __ksym;
+extern void *bpf_cast_to_kern_ctx(void *obj) __ksym;
+
+static __u32 cfg_get(__u32 key)
+{
+       __u32 *val = bpf_map_lookup_elem(&map_store_cfg, &key);
+
+       return val ? *val : 0;
+}
+
+static void res_set(__u32 key, __u32 val)
+{
+       bpf_map_update_elem(&map_store_res, &key, &val, BPF_ANY);
+}
+
+static void res_inc(__u32 key)
+{
+       __u32 *cnt = bpf_map_lookup_elem(&map_store_res, &key);
+
+       if (cnt)
+               __sync_fetch_and_add(cnt, 1);
+}
+
+static bool is_test_pkt(void *data, void *data_end)
+{
+       struct ethhdr *eth = data;
+       struct udphdr *udp;
+
+       if ((void *)(eth + 1) > data_end)
+               return false;
+
+       if (eth->h_proto == bpf_htons(ETH_P_IP)) {
+               struct iphdr *iph = (void *)(eth + 1);
+
+               if ((void *)(iph + 1) > data_end ||
+                   iph->protocol != IPPROTO_UDP)
+                       return false;
+               udp = (void *)(iph + 1);
+       } else if (eth->h_proto == bpf_htons(ETH_P_IPV6)) {
+               struct ipv6hdr *ip6h = (void *)(eth + 1);
+
+               if ((void *)(ip6h + 1) > data_end ||
+                   ip6h->nexthdr != IPPROTO_UDP)
+                       return false;
+               udp = (void *)(ip6h + 1);
+       } else {
+               return false;
+       }
+
+       if ((void *)(udp + 1) > data_end)
+               return false;
+
+       return bpf_ntohs(udp->dest) == cfg_get(CFG_PORT);
+}
+
+SEC("xdp")
+int xdp_store(struct xdp_md *ctx)
+{
+       if (!is_test_pkt((void *)(long)ctx->data, (void *)(long)ctx->data_end))
+               return XDP_PASS;
+
+       if (bpf_xdp_skb_ext_store_rx_hash(ctx, cfg_get(CFG_HASH), XDP_RSS_L4) ||
+           bpf_xdp_skb_ext_store_rx_vlan(ctx,
+                                         bpf_htons(cfg_get(CFG_VLAN_PROTO)),
+                                         cfg_get(CFG_VLAN_TCI)) ||
+           bpf_xdp_skb_ext_store_rx_csum(ctx, cfg_get(CFG_IP_SUMMED),
+                                         cfg_get(CFG_CSUM_LEVEL)))
+               res_inc(RES_STORE_ERR);
+
+       return XDP_PASS;
+}
+
+SEC("tc")
+int tc_check(struct __sk_buff *skb)
+{
+       struct sk_buff *kskb;
+
+       if (!is_test_pkt((void *)(long)skb->data, (void *)(long)skb->data_end))
+               return TC_ACT_UNSPEC;
+
+       kskb = bpf_cast_to_kern_ctx(skb);
+       res_set(RES_HASH, skb->hash);
+       res_set(RES_L4_HASH, BPF_CORE_READ_BITFIELD(kskb, l4_hash));
+       if (skb->vlan_present) {
+               res_set(RES_VLAN_PROTO, bpf_ntohs(skb->vlan_proto));
+               res_set(RES_VLAN_TCI, skb->vlan_tci);
+       }
+       res_set(RES_IP_SUMMED, BPF_CORE_READ_BITFIELD(kskb, ip_summed));
+       res_set(RES_CSUM_LEVEL, BPF_CORE_READ_BITFIELD(kskb, csum_level));
+       res_inc(RES_PKT_CNT);
+
+       return TC_ACT_UNSPEC;
+}
+
+char _license[] SEC("license") = "GPL";
-- 
2.56.0

Reply via email to