Skip to content

Commit

Permalink
selftests/bpf: Extend netkit tests to validate set {head,tail}room
Browse files Browse the repository at this point in the history
Extend the netkit selftests to specify and validate the {head,tail}room
on the netdevice:

  # ./vmtest.sh -- ./test_progs -t netkit
  [...]
  ./test_progs -t netkit
  [    1.174147] bpf_testmod: loading out-of-tree module taints kernel.
  [    1.174585] bpf_testmod: module verification failed: signature and/or required key missing - tainting kernel
  [    1.422307] tsc: Refined TSC clocksource calibration: 3407.983 MHz
  [    1.424511] clocksource: tsc: mask: 0xffffffffffffffff max_cycles: 0x311fc3e5084, max_idle_ns: 440795359833 ns
  [    1.428092] clocksource: Switched to clocksource tsc
  torvalds#363     tc_netkit_basic:OK
  torvalds#364     tc_netkit_device:OK
  torvalds#365     tc_netkit_multi_links:OK
  torvalds#366     tc_netkit_multi_opts:OK
  torvalds#367     tc_netkit_neigh_links:OK
  torvalds#368     tc_netkit_pkt_type:OK
  torvalds#369     tc_netkit_scrub:OK
  Summary: 7/0 PASSED, 0 SKIPPED, 0 FAILED

Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Acked-by: Nikolay Aleksandrov <razor@blackwall.org>
Link: https://lore.kernel.org/bpf/20241220234658.490686-3-daniel@iogearbox.net
  • Loading branch information
borkmann committed Jan 6, 2025
1 parent cc529a3 commit 058268e
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 18 deletions.
49 changes: 31 additions & 18 deletions tools/testing/selftests/bpf/prog_tests/tc_netkit.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,16 @@
#include "netlink_helpers.h"
#include "tc_helpers.h"

#define NETKIT_HEADROOM 32
#define NETKIT_TAILROOM 8

#define MARK 42
#define PRIO 0xeb9f
#define ICMP_ECHO 8

#define FLAG_ADJUST_ROOM (1 << 0)
#define FLAG_SAME_NETNS (1 << 1)

struct icmphdr {
__u8 type;
__u8 code;
Expand All @@ -35,7 +41,7 @@ struct iplink_req {
};

static int create_netkit(int mode, int policy, int peer_policy, int *ifindex,
bool same_netns, int scrub, int peer_scrub)
int scrub, int peer_scrub, __u32 flags)
{
struct rtnl_handle rth = { .fd = -1 };
struct iplink_req req = {};
Expand Down Expand Up @@ -63,6 +69,10 @@ static int create_netkit(int mode, int policy, int peer_policy, int *ifindex,
addattr32(&req.n, sizeof(req), IFLA_NETKIT_SCRUB, scrub);
addattr32(&req.n, sizeof(req), IFLA_NETKIT_PEER_SCRUB, peer_scrub);
addattr32(&req.n, sizeof(req), IFLA_NETKIT_MODE, mode);
if (flags & FLAG_ADJUST_ROOM) {
addattr16(&req.n, sizeof(req), IFLA_NETKIT_HEADROOM, NETKIT_HEADROOM);
addattr16(&req.n, sizeof(req), IFLA_NETKIT_TAILROOM, NETKIT_TAILROOM);
}
addattr_nest_end(&req.n, data);
addattr_nest_end(&req.n, linkinfo);

Expand All @@ -87,7 +97,7 @@ static int create_netkit(int mode, int policy, int peer_policy, int *ifindex,
" addr ee:ff:bb:cc:aa:dd"),
"set hwaddress");
}
if (same_netns) {
if (flags & FLAG_SAME_NETNS) {
ASSERT_OK(system("ip link set dev " netkit_peer " up"),
"up peer");
ASSERT_OK(system("ip addr add dev " netkit_peer " 10.0.0.2/24"),
Expand Down Expand Up @@ -184,8 +194,8 @@ void serial_test_tc_netkit_basic(void)
int err, ifindex;

err = create_netkit(NETKIT_L2, NETKIT_PASS, NETKIT_PASS,
&ifindex, false, NETKIT_SCRUB_DEFAULT,
NETKIT_SCRUB_DEFAULT);
&ifindex, NETKIT_SCRUB_DEFAULT,
NETKIT_SCRUB_DEFAULT, 0);
if (err)
return;

Expand Down Expand Up @@ -299,8 +309,8 @@ static void serial_test_tc_netkit_multi_links_target(int mode, int target)
int err, ifindex;

err = create_netkit(mode, NETKIT_PASS, NETKIT_PASS,
&ifindex, false, NETKIT_SCRUB_DEFAULT,
NETKIT_SCRUB_DEFAULT);
&ifindex, NETKIT_SCRUB_DEFAULT,
NETKIT_SCRUB_DEFAULT, 0);
if (err)
return;

Expand Down Expand Up @@ -428,8 +438,8 @@ static void serial_test_tc_netkit_multi_opts_target(int mode, int target)
int err, ifindex;

err = create_netkit(mode, NETKIT_PASS, NETKIT_PASS,
&ifindex, false, NETKIT_SCRUB_DEFAULT,
NETKIT_SCRUB_DEFAULT);
&ifindex, NETKIT_SCRUB_DEFAULT,
NETKIT_SCRUB_DEFAULT, 0);
if (err)
return;

Expand Down Expand Up @@ -543,8 +553,8 @@ void serial_test_tc_netkit_device(void)
int err, ifindex, ifindex2;

err = create_netkit(NETKIT_L3, NETKIT_PASS, NETKIT_PASS,
&ifindex, true, NETKIT_SCRUB_DEFAULT,
NETKIT_SCRUB_DEFAULT);
&ifindex, NETKIT_SCRUB_DEFAULT,
NETKIT_SCRUB_DEFAULT, FLAG_SAME_NETNS);
if (err)
return;

Expand Down Expand Up @@ -655,8 +665,8 @@ static void serial_test_tc_netkit_neigh_links_target(int mode, int target)
int err, ifindex;

err = create_netkit(mode, NETKIT_PASS, NETKIT_PASS,
&ifindex, false, NETKIT_SCRUB_DEFAULT,
NETKIT_SCRUB_DEFAULT);
&ifindex, NETKIT_SCRUB_DEFAULT,
NETKIT_SCRUB_DEFAULT, 0);
if (err)
return;

Expand Down Expand Up @@ -733,8 +743,8 @@ static void serial_test_tc_netkit_pkt_type_mode(int mode)
struct bpf_link *link;

err = create_netkit(mode, NETKIT_PASS, NETKIT_PASS,
&ifindex, true, NETKIT_SCRUB_DEFAULT,
NETKIT_SCRUB_DEFAULT);
&ifindex, NETKIT_SCRUB_DEFAULT,
NETKIT_SCRUB_DEFAULT, FLAG_SAME_NETNS);
if (err)
return;

Expand Down Expand Up @@ -799,15 +809,16 @@ void serial_test_tc_netkit_pkt_type(void)
serial_test_tc_netkit_pkt_type_mode(NETKIT_L3);
}

static void serial_test_tc_netkit_scrub_type(int scrub)
static void serial_test_tc_netkit_scrub_type(int scrub, bool room)
{
LIBBPF_OPTS(bpf_netkit_opts, optl);
struct test_tc_link *skel;
struct bpf_link *link;
int err, ifindex;

err = create_netkit(NETKIT_L2, NETKIT_PASS, NETKIT_PASS,
&ifindex, false, scrub, scrub);
&ifindex, scrub, scrub,
room ? FLAG_ADJUST_ROOM : 0);
if (err)
return;

Expand Down Expand Up @@ -842,6 +853,8 @@ static void serial_test_tc_netkit_scrub_type(int scrub)
ASSERT_EQ(skel->bss->seen_tc8, true, "seen_tc8");
ASSERT_EQ(skel->bss->mark, scrub == NETKIT_SCRUB_NONE ? MARK : 0, "mark");
ASSERT_EQ(skel->bss->prio, scrub == NETKIT_SCRUB_NONE ? PRIO : 0, "prio");
ASSERT_EQ(skel->bss->headroom, room ? NETKIT_HEADROOM : 0, "headroom");
ASSERT_EQ(skel->bss->tailroom, room ? NETKIT_TAILROOM : 0, "tailroom");
cleanup:
test_tc_link__destroy(skel);

Expand All @@ -852,6 +865,6 @@ static void serial_test_tc_netkit_scrub_type(int scrub)

void serial_test_tc_netkit_scrub(void)
{
serial_test_tc_netkit_scrub_type(NETKIT_SCRUB_DEFAULT);
serial_test_tc_netkit_scrub_type(NETKIT_SCRUB_NONE);
serial_test_tc_netkit_scrub_type(NETKIT_SCRUB_DEFAULT, false);
serial_test_tc_netkit_scrub_type(NETKIT_SCRUB_NONE, true);
}
15 changes: 15 additions & 0 deletions tools/testing/selftests/bpf/progs/test_tc_link.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <linux/if_packet.h>
#include <bpf/bpf_endian.h>
#include <bpf/bpf_helpers.h>
#include <bpf/bpf_core_read.h>

char LICENSE[] SEC("license") = "GPL";

Expand All @@ -27,6 +28,7 @@ bool seen_host;
bool seen_mcast;

int mark, prio;
unsigned short headroom, tailroom;

SEC("tc/ingress")
int tc1(struct __sk_buff *skb)
Expand Down Expand Up @@ -104,11 +106,24 @@ int tc7(struct __sk_buff *skb)
return TCX_PASS;
}

struct sk_buff {
struct net_device *dev;
};

struct net_device {
unsigned short needed_headroom;
unsigned short needed_tailroom;
};

SEC("tc/egress")
int tc8(struct __sk_buff *skb)
{
struct net_device *dev = BPF_CORE_READ((struct sk_buff *)skb, dev);

seen_tc8 = true;
mark = skb->mark;
prio = skb->priority;
headroom = BPF_CORE_READ(dev, needed_headroom);
tailroom = BPF_CORE_READ(dev, needed_tailroom);
return TCX_PASS;
}

0 comments on commit 058268e

Please sign in to comment.