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

Source file repositories/reference/linux-study-clean/tools/testing/selftests/bpf/prog_tests/xdp_vlan.c

File Facts

System
Linux kernel
Corpus path
tools/testing/selftests/bpf/prog_tests/xdp_vlan.c
Extension
.c
Size
5137 bytes
Lines
176
Domain
Support Tooling And Documentation
Bucket
tools
Inferred role
Support Tooling And Documentation: implementation source
Status
source implementation candidate

Why This File Exists

Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.

Dependency Surface

Detected Declarations

Annotated Snippet

// SPDX-License-Identifier: GPL-2.0

/*
 * Network topology:
 *  -----------        -----------
 *  |  NS1    |        |   NS2   |
 *  | veth0  -|--------|- veth0  |
 *  -----------        -----------
 *
 */

#define _GNU_SOURCE
#include <net/if.h>
#include <uapi/linux/if_link.h>

#include "network_helpers.h"
#include "test_progs.h"
#include "test_xdp_vlan.skel.h"


#define VETH_NAME	"veth0"
#define NS_MAX_SIZE	32
#define NS1_NAME	"ns-xdp-vlan-1-"
#define NS2_NAME	"ns-xdp-vlan-2-"
#define NS1_IP_ADDR	"100.64.10.1"
#define NS2_IP_ADDR	"100.64.10.2"
#define VLAN_ID		4011

static int setup_network(char *ns1, char *ns2)
{
	if (!ASSERT_OK(append_tid(ns1, NS_MAX_SIZE), "create ns1 name"))
		goto fail;
	if (!ASSERT_OK(append_tid(ns2, NS_MAX_SIZE), "create ns2 name"))
		goto fail;

	SYS(fail, "ip netns add %s", ns1);
	SYS(fail, "ip netns add %s", ns2);
	SYS(fail, "ip -n %s link add %s type veth peer name %s netns %s",
	    ns1, VETH_NAME, VETH_NAME, ns2);

	/* NOTICE: XDP require VLAN header inside packet payload
	 *  - Thus, disable VLAN offloading driver features
	 */
	SYS(fail, "ip netns exec %s ethtool -K %s rxvlan off txvlan off", ns1, VETH_NAME);
	SYS(fail, "ip netns exec %s ethtool -K %s rxvlan off txvlan off", ns2, VETH_NAME);

	/* NS1 configuration */
	SYS(fail, "ip -n %s addr add %s/24 dev %s", ns1, NS1_IP_ADDR, VETH_NAME);
	SYS(fail, "ip -n %s link set %s up", ns1, VETH_NAME);

	/* NS2 configuration */
	SYS(fail, "ip -n %s link add link %s name %s.%d type vlan id %d",
	    ns2, VETH_NAME, VETH_NAME, VLAN_ID, VLAN_ID);
	SYS(fail, "ip -n %s addr add %s/24 dev %s.%d", ns2, NS2_IP_ADDR, VETH_NAME, VLAN_ID);
	SYS(fail, "ip -n %s link set %s up", ns2, VETH_NAME);
	SYS(fail, "ip -n %s link set %s.%d up", ns2, VETH_NAME, VLAN_ID);

	/* At this point ping should fail because VLAN tags are only used by NS2 */
	return !SYS_NOFAIL("ip netns exec %s ping -W 1 -c1 %s", ns2, NS1_IP_ADDR);

fail:
	return -1;
}

static void cleanup_network(const char *ns1, const char *ns2)
{
	SYS_NOFAIL("ip netns del %s", ns1);
	SYS_NOFAIL("ip netns del %s", ns2);
}

static void xdp_vlan(struct bpf_program *xdp, struct bpf_program *tc, u32 flags)
{
	LIBBPF_OPTS(bpf_tc_hook, tc_hook, .attach_point = BPF_TC_EGRESS);
	LIBBPF_OPTS(bpf_tc_opts, tc_opts, .handle = 1, .priority = 1);
	char ns1[NS_MAX_SIZE] = NS1_NAME;
	char ns2[NS_MAX_SIZE] = NS2_NAME;
	struct nstoken *nstoken = NULL;
	int interface;
	int ret;

	if (!ASSERT_OK(setup_network(ns1, ns2), "setup network"))
		goto cleanup;

	nstoken = open_netns(ns1);
	if (!ASSERT_OK_PTR(nstoken, "open NS1"))
		goto cleanup;

	interface = if_nametoindex(VETH_NAME);
	if (!ASSERT_NEQ(interface, 0, "get interface index"))
		goto cleanup;

Annotation

Implementation Notes