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

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

File Facts

System
Linux kernel
Corpus path
tools/testing/selftests/bpf/prog_tests/xfrm_info.c
Extension
.c
Size
10614 bytes
Lines
348
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

if (!ASSERT_OK(ret, "bpf_tc_attach")) {
			bpf_tc_hook_destroy(hook);
			return ret;
		}
	}

	if (egr_fd >= 0) {
		hook->attach_point = BPF_TC_EGRESS;
		ret = bpf_tc_attach(hook, &opts2);
		if (!ASSERT_OK(ret, "bpf_tc_attach")) {
			bpf_tc_hook_destroy(hook);
			return ret;
		}
	}

	return 0;
}

static void cleanup(void)
{
	SYS_NOFAIL("test -f /var/run/netns/" NS0 " && ip netns delete " NS0);
	SYS_NOFAIL("test -f /var/run/netns/" NS1 " && ip netns delete " NS1);
	SYS_NOFAIL("test -f /var/run/netns/" NS2 " && ip netns delete " NS2);
}

static int config_underlay(void)
{
	SYS(fail, "ip netns add " NS0);
	SYS(fail, "ip netns add " NS1);
	SYS(fail, "ip netns add " NS2);

	/* NS0 <-> NS1 [veth01 <-> veth10] */
	SYS(fail, "ip link add veth01 netns " NS0 " type veth peer name veth10 netns " NS1);
	SYS(fail, "ip -net " NS0 " addr add " IP4_ADDR_VETH01 "/24 dev veth01");
	SYS(fail, "ip -net " NS0 " link set dev veth01 up");
	SYS(fail, "ip -net " NS1 " addr add " IP4_ADDR_VETH10 "/24 dev veth10");
	SYS(fail, "ip -net " NS1 " link set dev veth10 up");

	/* NS0 <-> NS2 [veth02 <-> veth20] */
	SYS(fail, "ip link add veth02 netns " NS0 " type veth peer name veth20 netns " NS2);
	SYS(fail, "ip -net " NS0 " addr add " IP4_ADDR_VETH02 "/24 dev veth02");
	SYS(fail, "ip -net " NS0 " link set dev veth02 up");
	SYS(fail, "ip -net " NS2 " addr add " IP4_ADDR_VETH20 "/24 dev veth20");
	SYS(fail, "ip -net " NS2 " link set dev veth20 up");

	return 0;
fail:
	return -1;
}

static int setup_xfrm_tunnel_ns(const char *ns, const char *ipv4_local,
				const char *ipv4_remote, int if_id)
{
	/* State: local -> remote */
	SYS(fail, "ip -net %s xfrm state add src %s dst %s spi 1 "
	    ESP_DUMMY_PARAMS "if_id %d", ns, ipv4_local, ipv4_remote, if_id);

	/* State: local <- remote */
	SYS(fail, "ip -net %s xfrm state add src %s dst %s spi 1 "
	    ESP_DUMMY_PARAMS "if_id %d", ns, ipv4_remote, ipv4_local, if_id);

	/* Policy: local -> remote */
	SYS(fail, "ip -net %s xfrm policy add dir out src 0.0.0.0/0 dst 0.0.0.0/0 "
	    "if_id %d tmpl src %s dst %s proto esp mode tunnel if_id %d", ns,
	    if_id, ipv4_local, ipv4_remote, if_id);

	/* Policy: local <- remote */
	SYS(fail, "ip -net %s xfrm policy add dir in src 0.0.0.0/0 dst 0.0.0.0/0 "
	    "if_id %d tmpl src %s dst %s proto esp mode tunnel if_id %d", ns,
	    if_id, ipv4_remote, ipv4_local, if_id);

	return 0;
fail:
	return -1;
}

static int setup_xfrm_tunnel(const char *ns_a, const char *ns_b,
			     const char *ipv4_a, const char *ipv4_b,
			     int if_id_a, int if_id_b)
{
	return setup_xfrm_tunnel_ns(ns_a, ipv4_a, ipv4_b, if_id_a) ||
		setup_xfrm_tunnel_ns(ns_b, ipv4_b, ipv4_a, if_id_b);
}

static struct rtattr *rtattr_add(struct nlmsghdr *nh, unsigned short type,
				 unsigned short len)
{
	struct rtattr *rta =
		(struct rtattr *)((uint8_t *)nh + RTA_ALIGN(nh->nlmsg_len));
	rta->rta_type = type;

Annotation

Implementation Notes