|
| 1 | +// SPDX-License-Identifier: GPL-2.0 |
| 2 | + |
| 3 | +/* |
| 4 | + * Network topology: |
| 5 | + * ----------- ----------- |
| 6 | + * | NS1 | | NS2 | |
| 7 | + * | veth0 -|--------|- veth0 | |
| 8 | + * ----------- ----------- |
| 9 | + * |
| 10 | + */ |
| 11 | + |
| 12 | +#define _GNU_SOURCE |
| 13 | +#include <net/if.h> |
| 14 | +#include <uapi/linux/if_link.h> |
| 15 | + |
| 16 | +#include "network_helpers.h" |
| 17 | +#include "test_progs.h" |
| 18 | +#include "test_xdp_vlan.skel.h" |
| 19 | + |
| 20 | + |
| 21 | +#define VETH_NAME "veth0" |
| 22 | +#define NS_MAX_SIZE 32 |
| 23 | +#define NS1_NAME "ns-xdp-vlan-1-" |
| 24 | +#define NS2_NAME "ns-xdp-vlan-2-" |
| 25 | +#define NS1_IP_ADDR "100.64.10.1" |
| 26 | +#define NS2_IP_ADDR "100.64.10.2" |
| 27 | +#define VLAN_ID 4011 |
| 28 | + |
| 29 | +static int setup_network(char *ns1, char *ns2) |
| 30 | +{ |
| 31 | + if (!ASSERT_OK(append_tid(ns1, NS_MAX_SIZE), "create ns1 name")) |
| 32 | + goto fail; |
| 33 | + if (!ASSERT_OK(append_tid(ns2, NS_MAX_SIZE), "create ns2 name")) |
| 34 | + goto fail; |
| 35 | + |
| 36 | + SYS(fail, "ip netns add %s", ns1); |
| 37 | + SYS(fail, "ip netns add %s", ns2); |
| 38 | + SYS(fail, "ip -n %s link add %s type veth peer name %s netns %s", |
| 39 | + ns1, VETH_NAME, VETH_NAME, ns2); |
| 40 | + |
| 41 | + /* NOTICE: XDP require VLAN header inside packet payload |
| 42 | + * - Thus, disable VLAN offloading driver features |
| 43 | + */ |
| 44 | + SYS(fail, "ip netns exec %s ethtool -K %s rxvlan off txvlan off", ns1, VETH_NAME); |
| 45 | + SYS(fail, "ip netns exec %s ethtool -K %s rxvlan off txvlan off", ns2, VETH_NAME); |
| 46 | + |
| 47 | + /* NS1 configuration */ |
| 48 | + SYS(fail, "ip -n %s addr add %s/24 dev %s", ns1, NS1_IP_ADDR, VETH_NAME); |
| 49 | + SYS(fail, "ip -n %s link set %s up", ns1, VETH_NAME); |
| 50 | + |
| 51 | + /* NS2 configuration */ |
| 52 | + SYS(fail, "ip -n %s link add link %s name %s.%d type vlan id %d", |
| 53 | + ns2, VETH_NAME, VETH_NAME, VLAN_ID, VLAN_ID); |
| 54 | + SYS(fail, "ip -n %s addr add %s/24 dev %s.%d", ns2, NS2_IP_ADDR, VETH_NAME, VLAN_ID); |
| 55 | + SYS(fail, "ip -n %s link set %s up", ns2, VETH_NAME); |
| 56 | + SYS(fail, "ip -n %s link set %s.%d up", ns2, VETH_NAME, VLAN_ID); |
| 57 | + |
| 58 | + /* At this point ping should fail because VLAN tags are only used by NS2 */ |
| 59 | + return !SYS_NOFAIL("ip netns exec %s ping -W 1 -c1 %s", ns2, NS1_IP_ADDR); |
| 60 | + |
| 61 | +fail: |
| 62 | + return -1; |
| 63 | +} |
| 64 | + |
| 65 | +static void cleanup_network(const char *ns1, const char *ns2) |
| 66 | +{ |
| 67 | + SYS_NOFAIL("ip netns del %s", ns1); |
| 68 | + SYS_NOFAIL("ip netns del %s", ns2); |
| 69 | +} |
| 70 | + |
| 71 | +static void xdp_vlan(struct bpf_program *xdp, struct bpf_program *tc, u32 flags) |
| 72 | +{ |
| 73 | + LIBBPF_OPTS(bpf_tc_hook, tc_hook, .attach_point = BPF_TC_EGRESS); |
| 74 | + LIBBPF_OPTS(bpf_tc_opts, tc_opts, .handle = 1, .priority = 1); |
| 75 | + char ns1[NS_MAX_SIZE] = NS1_NAME; |
| 76 | + char ns2[NS_MAX_SIZE] = NS2_NAME; |
| 77 | + struct nstoken *nstoken = NULL; |
| 78 | + int interface; |
| 79 | + int ret; |
| 80 | + |
| 81 | + if (!ASSERT_OK(setup_network(ns1, ns2), "setup network")) |
| 82 | + goto cleanup; |
| 83 | + |
| 84 | + nstoken = open_netns(ns1); |
| 85 | + if (!ASSERT_OK_PTR(nstoken, "open NS1")) |
| 86 | + goto cleanup; |
| 87 | + |
| 88 | + interface = if_nametoindex(VETH_NAME); |
| 89 | + if (!ASSERT_NEQ(interface, 0, "get interface index")) |
| 90 | + goto cleanup; |
| 91 | + |
| 92 | + ret = bpf_xdp_attach(interface, bpf_program__fd(xdp), flags, NULL); |
| 93 | + if (!ASSERT_OK(ret, "attach xdp_vlan_change")) |
| 94 | + goto cleanup; |
| 95 | + |
| 96 | + tc_hook.ifindex = interface; |
| 97 | + ret = bpf_tc_hook_create(&tc_hook); |
| 98 | + if (!ASSERT_OK(ret, "bpf_tc_hook_create")) |
| 99 | + goto detach_xdp; |
| 100 | + |
| 101 | + /* Now we'll use BPF programs to pop/push the VLAN tags */ |
| 102 | + tc_opts.prog_fd = bpf_program__fd(tc); |
| 103 | + ret = bpf_tc_attach(&tc_hook, &tc_opts); |
| 104 | + if (!ASSERT_OK(ret, "bpf_tc_attach")) |
| 105 | + goto detach_xdp; |
| 106 | + |
| 107 | + close_netns(nstoken); |
| 108 | + nstoken = NULL; |
| 109 | + |
| 110 | + /* Now the namespaces can reach each-other, test with pings */ |
| 111 | + SYS(detach_tc, "ip netns exec %s ping -i 0.2 -W 2 -c 2 %s > /dev/null", ns1, NS2_IP_ADDR); |
| 112 | + SYS(detach_tc, "ip netns exec %s ping -i 0.2 -W 2 -c 2 %s > /dev/null", ns2, NS1_IP_ADDR); |
| 113 | + |
| 114 | + |
| 115 | +detach_tc: |
| 116 | + bpf_tc_detach(&tc_hook, &tc_opts); |
| 117 | +detach_xdp: |
| 118 | + bpf_xdp_detach(interface, flags, NULL); |
| 119 | +cleanup: |
| 120 | + close_netns(nstoken); |
| 121 | + cleanup_network(ns1, ns2); |
| 122 | +} |
| 123 | + |
| 124 | +/* First test: Remove VLAN by setting VLAN ID 0, using "xdp_vlan_change" |
| 125 | + * egress use TC to add back VLAN tag 4011 |
| 126 | + */ |
| 127 | +void test_xdp_vlan_change(void) |
| 128 | +{ |
| 129 | + struct test_xdp_vlan *skel; |
| 130 | + |
| 131 | + skel = test_xdp_vlan__open_and_load(); |
| 132 | + if (!ASSERT_OK_PTR(skel, "xdp_vlan__open_and_load")) |
| 133 | + return; |
| 134 | + |
| 135 | + if (test__start_subtest("0")) |
| 136 | + xdp_vlan(skel->progs.xdp_vlan_change, skel->progs.tc_vlan_push, 0); |
| 137 | + |
| 138 | + if (test__start_subtest("DRV_MODE")) |
| 139 | + xdp_vlan(skel->progs.xdp_vlan_change, skel->progs.tc_vlan_push, |
| 140 | + XDP_FLAGS_DRV_MODE); |
| 141 | + |
| 142 | + if (test__start_subtest("SKB_MODE")) |
| 143 | + xdp_vlan(skel->progs.xdp_vlan_change, skel->progs.tc_vlan_push, |
| 144 | + XDP_FLAGS_SKB_MODE); |
| 145 | + |
| 146 | + test_xdp_vlan__destroy(skel); |
| 147 | +} |
| 148 | + |
| 149 | +/* Second test: XDP prog fully remove vlan header |
| 150 | + * |
| 151 | + * Catch kernel bug for generic-XDP, that doesn't allow us to |
| 152 | + * remove a VLAN header, because skb->protocol still contain VLAN |
| 153 | + * ETH_P_8021Q indication, and this cause overwriting of our changes. |
| 154 | + */ |
| 155 | +void test_xdp_vlan_remove(void) |
| 156 | +{ |
| 157 | + struct test_xdp_vlan *skel; |
| 158 | + |
| 159 | + skel = test_xdp_vlan__open_and_load(); |
| 160 | + if (!ASSERT_OK_PTR(skel, "xdp_vlan__open_and_load")) |
| 161 | + return; |
| 162 | + |
| 163 | + if (test__start_subtest("0")) |
| 164 | + xdp_vlan(skel->progs.xdp_vlan_remove_outer2, skel->progs.tc_vlan_push, 0); |
| 165 | + |
| 166 | + if (test__start_subtest("DRV_MODE")) |
| 167 | + xdp_vlan(skel->progs.xdp_vlan_remove_outer2, skel->progs.tc_vlan_push, |
| 168 | + XDP_FLAGS_DRV_MODE); |
| 169 | + |
| 170 | + if (test__start_subtest("SKB_MODE")) |
| 171 | + xdp_vlan(skel->progs.xdp_vlan_remove_outer2, skel->progs.tc_vlan_push, |
| 172 | + XDP_FLAGS_SKB_MODE); |
| 173 | + |
| 174 | + test_xdp_vlan__destroy(skel); |
| 175 | +} |
0 commit comments