Commit 4dba3e78 authored by Lorenzo Bianconi's avatar Lorenzo Bianconi Committed by Alexei Starovoitov

selftests/bpf: introduce XDP compliance test tool

Introduce xdp_features tool in order to test XDP features supported by
the NIC and match them against advertised ones.
In order to test supported/advertised XDP features, xdp_features must
run on the Device Under Test (DUT) and on a Tester device.
xdp_features opens a control TCP channel between DUT and Tester devices
to send control commands from Tester to the DUT and a UDP data channel
where the Tester sends UDP 'echo' packets and the DUT is expected to
reply back with the same packet. DUT installs multiple XDP programs on the
NIC to test XDP capabilities and reports back to the Tester some XDP stats.
Currently xdp_features supports the following XDP features:
- XDP_DROP
- XDP_ABORTED
- XDP_PASS
- XDP_TX
- XDP_REDIRECT
- XDP_NDO_XMIT
Co-developed-by: default avatarKumar Kartikeya Dwivedi <memxor@gmail.com>
Signed-off-by: default avatarKumar Kartikeya Dwivedi <memxor@gmail.com>
Signed-off-by: default avatarLorenzo Bianconi <lorenzo@kernel.org>
Acked-by: default avatarStanislav Fomichev <sdf@google.com>
Link: https://lore.kernel.org/r/7c1af8e7e6ef0614cf32fa9e6bdaa2d8d605f859.1675245258.git.lorenzo@kernel.orgSigned-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
parent 84050074
...@@ -48,3 +48,4 @@ xskxceiver ...@@ -48,3 +48,4 @@ xskxceiver
xdp_redirect_multi xdp_redirect_multi
xdp_synproxy xdp_synproxy
xdp_hw_metadata xdp_hw_metadata
xdp_features
...@@ -73,7 +73,8 @@ TEST_PROGS := test_kmod.sh \ ...@@ -73,7 +73,8 @@ TEST_PROGS := test_kmod.sh \
test_bpftool.sh \ test_bpftool.sh \
test_bpftool_metadata.sh \ test_bpftool_metadata.sh \
test_doc_build.sh \ test_doc_build.sh \
test_xsk.sh test_xsk.sh \
test_xdp_features.sh
TEST_PROGS_EXTENDED := with_addr.sh \ TEST_PROGS_EXTENDED := with_addr.sh \
with_tunnels.sh ima_setup.sh verify_sig_setup.sh \ with_tunnels.sh ima_setup.sh verify_sig_setup.sh \
...@@ -83,7 +84,8 @@ TEST_PROGS_EXTENDED := with_addr.sh \ ...@@ -83,7 +84,8 @@ TEST_PROGS_EXTENDED := with_addr.sh \
TEST_GEN_PROGS_EXTENDED = test_sock_addr test_skb_cgroup_id_user \ TEST_GEN_PROGS_EXTENDED = test_sock_addr test_skb_cgroup_id_user \
flow_dissector_load test_flow_dissector test_tcp_check_syncookie_user \ flow_dissector_load test_flow_dissector test_tcp_check_syncookie_user \
test_lirc_mode2_user xdping test_cpp runqslower bench bpf_testmod.ko \ test_lirc_mode2_user xdping test_cpp runqslower bench bpf_testmod.ko \
xskxceiver xdp_redirect_multi xdp_synproxy veristat xdp_hw_metadata xskxceiver xdp_redirect_multi xdp_synproxy veristat xdp_hw_metadata \
xdp_features
TEST_CUSTOM_PROGS = $(OUTPUT)/urandom_read $(OUTPUT)/sign-file TEST_CUSTOM_PROGS = $(OUTPUT)/urandom_read $(OUTPUT)/sign-file
TEST_GEN_FILES += liburandom_read.so TEST_GEN_FILES += liburandom_read.so
...@@ -385,6 +387,7 @@ test_subskeleton_lib.skel.h-deps := test_subskeleton_lib2.bpf.o test_subskeleton ...@@ -385,6 +387,7 @@ test_subskeleton_lib.skel.h-deps := test_subskeleton_lib2.bpf.o test_subskeleton
test_usdt.skel.h-deps := test_usdt.bpf.o test_usdt_multispec.bpf.o test_usdt.skel.h-deps := test_usdt.bpf.o test_usdt_multispec.bpf.o
xsk_xdp_progs.skel.h-deps := xsk_xdp_progs.bpf.o xsk_xdp_progs.skel.h-deps := xsk_xdp_progs.bpf.o
xdp_hw_metadata.skel.h-deps := xdp_hw_metadata.bpf.o xdp_hw_metadata.skel.h-deps := xdp_hw_metadata.bpf.o
xdp_features.skel.h-deps := xdp_features.bpf.o
LINKED_BPF_SRCS := $(patsubst %.bpf.o,%.c,$(foreach skel,$(LINKED_SKELS),$($(skel)-deps))) LINKED_BPF_SRCS := $(patsubst %.bpf.o,%.c,$(foreach skel,$(LINKED_SKELS),$($(skel)-deps)))
...@@ -587,6 +590,10 @@ $(OUTPUT)/xdp_hw_metadata: xdp_hw_metadata.c $(OUTPUT)/network_helpers.o $(OUTPU ...@@ -587,6 +590,10 @@ $(OUTPUT)/xdp_hw_metadata: xdp_hw_metadata.c $(OUTPUT)/network_helpers.o $(OUTPU
$(call msg,BINARY,,$@) $(call msg,BINARY,,$@)
$(Q)$(CC) $(CFLAGS) $(filter %.a %.o %.c,$^) $(LDLIBS) -o $@ $(Q)$(CC) $(CFLAGS) $(filter %.a %.o %.c,$^) $(LDLIBS) -o $@
$(OUTPUT)/xdp_features: xdp_features.c $(OUTPUT)/network_helpers.o $(OUTPUT)/xdp_features.skel.h | $(OUTPUT)
$(call msg,BINARY,,$@)
$(Q)$(CC) $(CFLAGS) $(filter %.a %.o %.c,$^) $(LDLIBS) -o $@
# Make sure we are able to include and link libbpf against c++. # Make sure we are able to include and link libbpf against c++.
$(OUTPUT)/test_cpp: test_cpp.cpp $(OUTPUT)/test_core_extern.skel.h $(BPFOBJ) $(OUTPUT)/test_cpp: test_cpp.cpp $(OUTPUT)/test_core_extern.skel.h $(BPFOBJ)
$(call msg,CXX,,$@) $(call msg,CXX,,$@)
......
// SPDX-License-Identifier: GPL-2.0
#include <stdbool.h>
#include <linux/bpf.h>
#include <linux/netdev.h>
#include <bpf/bpf_helpers.h>
#include <bpf/bpf_endian.h>
#include <bpf/bpf_tracing.h>
#include <linux/if_ether.h>
#include <linux/ip.h>
#include <linux/ipv6.h>
#include <linux/in.h>
#include <linux/in6.h>
#include <linux/udp.h>
#include <asm-generic/errno-base.h>
#include "xdp_features.h"
#define ipv6_addr_equal(a, b) ((a).s6_addr32[0] == (b).s6_addr32[0] && \
(a).s6_addr32[1] == (b).s6_addr32[1] && \
(a).s6_addr32[2] == (b).s6_addr32[2] && \
(a).s6_addr32[3] == (b).s6_addr32[3])
struct net_device;
struct bpf_prog;
struct xdp_cpumap_stats {
unsigned int redirect;
unsigned int pass;
unsigned int drop;
};
struct {
__uint(type, BPF_MAP_TYPE_ARRAY);
__type(key, __u32);
__type(value, __u32);
__uint(max_entries, 1);
} stats SEC(".maps");
struct {
__uint(type, BPF_MAP_TYPE_ARRAY);
__type(key, __u32);
__type(value, __u32);
__uint(max_entries, 1);
} dut_stats SEC(".maps");
struct {
__uint(type, BPF_MAP_TYPE_CPUMAP);
__uint(key_size, sizeof(__u32));
__uint(value_size, sizeof(struct bpf_cpumap_val));
__uint(max_entries, 1);
} cpu_map SEC(".maps");
struct {
__uint(type, BPF_MAP_TYPE_DEVMAP);
__uint(key_size, sizeof(__u32));
__uint(value_size, sizeof(struct bpf_devmap_val));
__uint(max_entries, 1);
} dev_map SEC(".maps");
const volatile struct in6_addr tester_addr;
const volatile struct in6_addr dut_addr;
static __always_inline int
xdp_process_echo_packet(struct xdp_md *xdp, bool dut)
{
void *data_end = (void *)(long)xdp->data_end;
void *data = (void *)(long)xdp->data;
struct ethhdr *eh = data;
struct tlv_hdr *tlv;
struct udphdr *uh;
__be16 port;
__u8 *cmd;
if (eh + 1 > (struct ethhdr *)data_end)
return -EINVAL;
if (eh->h_proto == bpf_htons(ETH_P_IP)) {
struct iphdr *ih = (struct iphdr *)(eh + 1);
__be32 saddr = dut ? tester_addr.s6_addr32[3]
: dut_addr.s6_addr32[3];
__be32 daddr = dut ? dut_addr.s6_addr32[3]
: tester_addr.s6_addr32[3];
ih = (struct iphdr *)(eh + 1);
if (ih + 1 > (struct iphdr *)data_end)
return -EINVAL;
if (saddr != ih->saddr)
return -EINVAL;
if (daddr != ih->daddr)
return -EINVAL;
if (ih->protocol != IPPROTO_UDP)
return -EINVAL;
uh = (struct udphdr *)(ih + 1);
} else if (eh->h_proto == bpf_htons(ETH_P_IPV6)) {
struct in6_addr saddr = dut ? tester_addr : dut_addr;
struct in6_addr daddr = dut ? dut_addr : tester_addr;
struct ipv6hdr *ih6 = (struct ipv6hdr *)(eh + 1);
if (ih6 + 1 > (struct ipv6hdr *)data_end)
return -EINVAL;
if (!ipv6_addr_equal(saddr, ih6->saddr))
return -EINVAL;
if (!ipv6_addr_equal(daddr, ih6->daddr))
return -EINVAL;
if (ih6->nexthdr != IPPROTO_UDP)
return -EINVAL;
uh = (struct udphdr *)(ih6 + 1);
} else {
return -EINVAL;
}
if (uh + 1 > (struct udphdr *)data_end)
return -EINVAL;
port = dut ? uh->dest : uh->source;
if (port != bpf_htons(DUT_ECHO_PORT))
return -EINVAL;
tlv = (struct tlv_hdr *)(uh + 1);
if (tlv + 1 > data_end)
return -EINVAL;
return bpf_htons(tlv->type) == CMD_ECHO ? 0 : -EINVAL;
}
static __always_inline int
xdp_update_stats(struct xdp_md *xdp, bool tx, bool dut)
{
__u32 *val, key = 0;
if (xdp_process_echo_packet(xdp, tx))
return -EINVAL;
if (dut)
val = bpf_map_lookup_elem(&dut_stats, &key);
else
val = bpf_map_lookup_elem(&stats, &key);
if (val)
__sync_add_and_fetch(val, 1);
return 0;
}
/* Tester */
SEC("xdp")
int xdp_tester_check_tx(struct xdp_md *xdp)
{
xdp_update_stats(xdp, true, false);
return XDP_PASS;
}
SEC("xdp")
int xdp_tester_check_rx(struct xdp_md *xdp)
{
xdp_update_stats(xdp, false, false);
return XDP_PASS;
}
/* DUT */
SEC("xdp")
int xdp_do_pass(struct xdp_md *xdp)
{
xdp_update_stats(xdp, true, true);
return XDP_PASS;
}
SEC("xdp")
int xdp_do_drop(struct xdp_md *xdp)
{
if (xdp_update_stats(xdp, true, true))
return XDP_PASS;
return XDP_DROP;
}
SEC("xdp")
int xdp_do_aborted(struct xdp_md *xdp)
{
if (xdp_process_echo_packet(xdp, true))
return XDP_PASS;
return XDP_ABORTED;
}
SEC("xdp")
int xdp_do_tx(struct xdp_md *xdp)
{
void *data = (void *)(long)xdp->data;
struct ethhdr *eh = data;
__u8 tmp_mac[ETH_ALEN];
if (xdp_update_stats(xdp, true, true))
return XDP_PASS;
__builtin_memcpy(tmp_mac, eh->h_source, ETH_ALEN);
__builtin_memcpy(eh->h_source, eh->h_dest, ETH_ALEN);
__builtin_memcpy(eh->h_dest, tmp_mac, ETH_ALEN);
return XDP_TX;
}
SEC("xdp")
int xdp_do_redirect(struct xdp_md *xdp)
{
if (xdp_process_echo_packet(xdp, true))
return XDP_PASS;
return bpf_redirect_map(&cpu_map, 0, 0);
}
SEC("tp_btf/xdp_exception")
int BPF_PROG(xdp_exception, const struct net_device *dev,
const struct bpf_prog *xdp, __u32 act)
{
__u32 *val, key = 0;
val = bpf_map_lookup_elem(&dut_stats, &key);
if (val)
__sync_add_and_fetch(val, 1);
return 0;
}
SEC("tp_btf/xdp_cpumap_kthread")
int BPF_PROG(tp_xdp_cpumap_kthread, int map_id, unsigned int processed,
unsigned int drops, int sched, struct xdp_cpumap_stats *xdp_stats)
{
__u32 *val, key = 0;
val = bpf_map_lookup_elem(&dut_stats, &key);
if (val)
__sync_add_and_fetch(val, 1);
return 0;
}
SEC("xdp/cpumap")
int xdp_do_redirect_cpumap(struct xdp_md *xdp)
{
void *data = (void *)(long)xdp->data;
struct ethhdr *eh = data;
__u8 tmp_mac[ETH_ALEN];
if (xdp_process_echo_packet(xdp, true))
return XDP_PASS;
__builtin_memcpy(tmp_mac, eh->h_source, ETH_ALEN);
__builtin_memcpy(eh->h_source, eh->h_dest, ETH_ALEN);
__builtin_memcpy(eh->h_dest, tmp_mac, ETH_ALEN);
return bpf_redirect_map(&dev_map, 0, 0);
}
char _license[] SEC("license") = "GPL";
#!/bin/bash
# SPDX-License-Identifier: GPL-2.0
readonly NS="ns1-$(mktemp -u XXXXXX)"
readonly V0_IP4=10.10.0.11
readonly V1_IP4=10.10.0.1
readonly V0_IP6=2001:db8::11
readonly V1_IP6=2001:db8::1
ret=1
setup() {
{
ip netns add ${NS}
ip link add v1 type veth peer name v0 netns ${NS}
ip link set v1 up
ip addr add $V1_IP4/24 dev v1
ip addr add $V1_IP6/64 nodad dev v1
ip -n ${NS} link set dev v0 up
ip -n ${NS} addr add $V0_IP4/24 dev v0
ip -n ${NS} addr add $V0_IP6/64 nodad dev v0
# Enable XDP mode and disable checksum offload
ethtool -K v1 gro on
ethtool -K v1 tx-checksumming off
ip netns exec ${NS} ethtool -K v0 gro on
ip netns exec ${NS} ethtool -K v0 tx-checksumming off
} > /dev/null 2>&1
}
cleanup() {
ip link del v1 2> /dev/null
ip netns del ${NS} 2> /dev/null
[ "$(pidof xdp_features)" = "" ] || kill $(pidof xdp_features) 2> /dev/null
}
wait_for_dut_server() {
while sleep 1; do
ss -tlp | grep -q xdp_features
[ $? -eq 0 ] && break
done
}
test_xdp_features() {
setup
## XDP_PASS
./xdp_features -f XDP_PASS -D $V1_IP6 -T $V0_IP6 v1 &
wait_for_dut_server
ip netns exec ${NS} ./xdp_features -t -f XDP_PASS \
-D $V1_IP6 -C $V1_IP6 \
-T $V0_IP6 v0
[ $? -ne 0 ] && exit
## XDP_DROP
./xdp_features -f XDP_DROP -D ::ffff:$V1_IP4 -T ::ffff:$V0_IP4 v1 &
wait_for_dut_server
ip netns exec ${NS} ./xdp_features -t -f XDP_DROP \
-D ::ffff:$V1_IP4 \
-C ::ffff:$V1_IP4 \
-T ::ffff:$V0_IP4 v0
[ $? -ne 0 ] && exit
## XDP_ABORTED
./xdp_features -f XDP_ABORTED -D $V1_IP6 -T $V0_IP6 v1 &
wait_for_dut_server
ip netns exec ${NS} ./xdp_features -t -f XDP_ABORTED \
-D $V1_IP6 -C $V1_IP6 \
-T $V0_IP6 v0
[ $? -ne 0 ] && exit
## XDP_TX
./xdp_features -f XDP_TX -D ::ffff:$V1_IP4 -T ::ffff:$V0_IP4 v1 &
wait_for_dut_server
ip netns exec ${NS} ./xdp_features -t -f XDP_TX \
-D ::ffff:$V1_IP4 \
-C ::ffff:$V1_IP4 \
-T ::ffff:$V0_IP4 v0
[ $? -ne 0 ] && exit
## XDP_REDIRECT
./xdp_features -f XDP_REDIRECT -D $V1_IP6 -T $V0_IP6 v1 &
wait_for_dut_server
ip netns exec ${NS} ./xdp_features -t -f XDP_REDIRECT \
-D $V1_IP6 -C $V1_IP6 \
-T $V0_IP6 v0
[ $? -ne 0 ] && exit
## XDP_NDO_XMIT
./xdp_features -f XDP_NDO_XMIT -D ::ffff:$V1_IP4 -T ::ffff:$V0_IP4 v1 &
wait_for_dut_server
ip netns exec ${NS} ./xdp_features -t -f XDP_NDO_XMIT \
-D ::ffff:$V1_IP4 \
-C ::ffff:$V1_IP4 \
-T ::ffff:$V0_IP4 v0
ret=$?
cleanup
}
set -e
trap cleanup 2 3 6 9
test_xdp_features
exit $ret
This diff is collapsed.
/* SPDX-License-Identifier: GPL-2.0 */
/* test commands */
enum test_commands {
CMD_STOP, /* CMD */
CMD_START, /* CMD */
CMD_ECHO, /* CMD */
CMD_ACK, /* CMD + data */
CMD_GET_XDP_CAP, /* CMD */
CMD_GET_STATS, /* CMD */
};
#define DUT_CTRL_PORT 12345
#define DUT_ECHO_PORT 12346
struct tlv_hdr {
__be16 type;
__be16 len;
__u8 data[];
};
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment