Skip to content

Commit 95348e4

Browse files
borkmannAlexei Starovoitov
authored and
Alexei Starovoitov
committed
selftests/bpf: Add netkit test for pkt_type
Add a test case to assert that the skb->pkt_type which was set from the BPF program is retained from the netkit xmit side to the peer's device at tcx ingress location. # ./vmtest.sh -- ./test_progs -t netkit [...] ./test_progs -t netkit [ 1.140780] bpf_testmod: loading out-of-tree module taints kernel. [ 1.141127] bpf_testmod: module verification failed: signature and/or required key missing - tainting kernel [ 1.284601] tsc: Refined TSC clocksource calibration: 3408.006 MHz [ 1.286672] clocksource: tsc: mask: 0xffffffffffffffff max_cycles: 0x311fd9b189d, max_idle_ns: 440795225691 ns [ 1.290384] clocksource: Switched to clocksource tsc #345 tc_netkit_basic:OK #346 tc_netkit_device:OK #347 tc_netkit_multi_links:OK #348 tc_netkit_multi_opts:OK #349 tc_netkit_neigh_links:OK #350 tc_netkit_pkt_type:OK Summary: 6/0 PASSED, 0 SKIPPED, 0 FAILED Signed-off-by: Daniel Borkmann <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent 998ffeb commit 95348e4

File tree

2 files changed

+118
-1
lines changed

2 files changed

+118
-1
lines changed

tools/testing/selftests/bpf/prog_tests/tc_netkit.c

+84
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,16 @@ static int create_netkit(int mode, int policy, int peer_policy, int *ifindex,
9999
return err;
100100
}
101101

102+
static void move_netkit(void)
103+
{
104+
ASSERT_OK(system("ip link set " netkit_peer " netns foo"),
105+
"move peer");
106+
ASSERT_OK(system("ip netns exec foo ip link set dev "
107+
netkit_peer " up"), "up peer");
108+
ASSERT_OK(system("ip netns exec foo ip addr add dev "
109+
netkit_peer " 10.0.0.2/24"), "addr peer");
110+
}
111+
102112
static void destroy_netkit(void)
103113
{
104114
ASSERT_OK(system("ip link del dev " netkit_name), "del primary");
@@ -695,3 +705,77 @@ void serial_test_tc_netkit_neigh_links(void)
695705
serial_test_tc_netkit_neigh_links_target(NETKIT_L2, BPF_NETKIT_PRIMARY);
696706
serial_test_tc_netkit_neigh_links_target(NETKIT_L3, BPF_NETKIT_PRIMARY);
697707
}
708+
709+
static void serial_test_tc_netkit_pkt_type_mode(int mode)
710+
{
711+
LIBBPF_OPTS(bpf_netkit_opts, optl_nk);
712+
LIBBPF_OPTS(bpf_tcx_opts, optl_tcx);
713+
int err, ifindex, ifindex2;
714+
struct test_tc_link *skel;
715+
struct bpf_link *link;
716+
717+
err = create_netkit(mode, NETKIT_PASS, NETKIT_PASS,
718+
&ifindex, true);
719+
if (err)
720+
return;
721+
722+
ifindex2 = if_nametoindex(netkit_peer);
723+
ASSERT_NEQ(ifindex, ifindex2, "ifindex_1_2");
724+
725+
skel = test_tc_link__open();
726+
if (!ASSERT_OK_PTR(skel, "skel_open"))
727+
goto cleanup;
728+
729+
ASSERT_EQ(bpf_program__set_expected_attach_type(skel->progs.tc1,
730+
BPF_NETKIT_PRIMARY), 0, "tc1_attach_type");
731+
ASSERT_EQ(bpf_program__set_expected_attach_type(skel->progs.tc7,
732+
BPF_TCX_INGRESS), 0, "tc7_attach_type");
733+
734+
err = test_tc_link__load(skel);
735+
if (!ASSERT_OK(err, "skel_load"))
736+
goto cleanup;
737+
738+
assert_mprog_count_ifindex(ifindex, BPF_NETKIT_PRIMARY, 0);
739+
assert_mprog_count_ifindex(ifindex2, BPF_TCX_INGRESS, 0);
740+
741+
link = bpf_program__attach_netkit(skel->progs.tc1, ifindex, &optl_nk);
742+
if (!ASSERT_OK_PTR(link, "link_attach"))
743+
goto cleanup;
744+
745+
skel->links.tc1 = link;
746+
747+
assert_mprog_count_ifindex(ifindex, BPF_NETKIT_PRIMARY, 1);
748+
assert_mprog_count_ifindex(ifindex2, BPF_TCX_INGRESS, 0);
749+
750+
link = bpf_program__attach_tcx(skel->progs.tc7, ifindex2, &optl_tcx);
751+
if (!ASSERT_OK_PTR(link, "link_attach"))
752+
goto cleanup;
753+
754+
skel->links.tc7 = link;
755+
756+
assert_mprog_count_ifindex(ifindex, BPF_NETKIT_PRIMARY, 1);
757+
assert_mprog_count_ifindex(ifindex2, BPF_TCX_INGRESS, 1);
758+
759+
move_netkit();
760+
761+
tc_skel_reset_all_seen(skel);
762+
skel->bss->set_type = true;
763+
ASSERT_EQ(send_icmp(), 0, "icmp_pkt");
764+
765+
ASSERT_EQ(skel->bss->seen_tc1, true, "seen_tc1");
766+
ASSERT_EQ(skel->bss->seen_tc7, true, "seen_tc7");
767+
768+
ASSERT_EQ(skel->bss->seen_host, true, "seen_host");
769+
ASSERT_EQ(skel->bss->seen_mcast, true, "seen_mcast");
770+
cleanup:
771+
test_tc_link__destroy(skel);
772+
773+
assert_mprog_count_ifindex(ifindex, BPF_NETKIT_PRIMARY, 0);
774+
destroy_netkit();
775+
}
776+
777+
void serial_test_tc_netkit_pkt_type(void)
778+
{
779+
serial_test_tc_netkit_pkt_type_mode(NETKIT_L2);
780+
serial_test_tc_netkit_pkt_type_mode(NETKIT_L3);
781+
}

tools/testing/selftests/bpf/progs/test_tc_link.c

+34-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44

55
#include <linux/bpf.h>
66
#include <linux/if_ether.h>
7-
7+
#include <linux/stddef.h>
8+
#include <linux/if_packet.h>
89
#include <bpf/bpf_endian.h>
910
#include <bpf/bpf_helpers.h>
1011

@@ -16,7 +17,13 @@ bool seen_tc3;
1617
bool seen_tc4;
1718
bool seen_tc5;
1819
bool seen_tc6;
20+
bool seen_tc7;
21+
22+
bool set_type;
23+
1924
bool seen_eth;
25+
bool seen_host;
26+
bool seen_mcast;
2027

2128
SEC("tc/ingress")
2229
int tc1(struct __sk_buff *skb)
@@ -28,8 +35,16 @@ int tc1(struct __sk_buff *skb)
2835
if (bpf_skb_load_bytes(skb, 0, &eth, sizeof(eth)))
2936
goto out;
3037
seen_eth = eth.h_proto == bpf_htons(ETH_P_IP);
38+
seen_host = skb->pkt_type == PACKET_HOST;
39+
if (seen_host && set_type) {
40+
eth.h_dest[0] = 4;
41+
if (bpf_skb_store_bytes(skb, 0, &eth, sizeof(eth), 0))
42+
goto fail;
43+
bpf_skb_change_type(skb, PACKET_MULTICAST);
44+
}
3145
out:
3246
seen_tc1 = true;
47+
fail:
3348
return TCX_NEXT;
3449
}
3550

@@ -67,3 +82,21 @@ int tc6(struct __sk_buff *skb)
6782
seen_tc6 = true;
6883
return TCX_PASS;
6984
}
85+
86+
SEC("tc/ingress")
87+
int tc7(struct __sk_buff *skb)
88+
{
89+
struct ethhdr eth = {};
90+
91+
if (skb->protocol != __bpf_constant_htons(ETH_P_IP))
92+
goto out;
93+
if (bpf_skb_load_bytes(skb, 0, &eth, sizeof(eth)))
94+
goto out;
95+
if (eth.h_dest[0] == 4 && set_type) {
96+
seen_mcast = skb->pkt_type == PACKET_MULTICAST;
97+
bpf_skb_change_type(skb, PACKET_HOST);
98+
}
99+
out:
100+
seen_tc7 = true;
101+
return TCX_PASS;
102+
}

0 commit comments

Comments
 (0)