summaryrefslogtreecommitdiff
path: root/tools/testing/selftests/bpf/prog_tests/xdp_devmap_attach.c
blob: d19dbd668f6a4185987b33bff9f30ef2e2d5bd87 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// SPDX-License-Identifier: GPL-2.0
#include <uapi/linux/bpf.h>
#include <linux/if_link.h>
#include <test_progs.h>

#include "test_xdp_devmap_helpers.skel.h"
#include "test_xdp_with_devmap_helpers.skel.h"

#define IFINDEX_LO 1

struct bpf_devmap_val {
	u32 ifindex;   /* device index */
	union {
		int fd;  /* prog fd on map write */
		u32 id;  /* prog id on map read */
	} bpf_prog;
};

void test_xdp_with_devmap_helpers(void)
{
	struct test_xdp_with_devmap_helpers *skel;
	struct bpf_prog_info info = {};
	struct bpf_devmap_val val = {
		.ifindex = IFINDEX_LO,
	};
	__u32 len = sizeof(info);
	__u32 duration = 0, idx = 0;
	int err, dm_fd, map_fd;


	skel = test_xdp_with_devmap_helpers__open_and_load();
	if (CHECK_FAIL(!skel)) {
		perror("test_xdp_with_devmap_helpers__open_and_load");
		return;
	}

	/* can not attach program with DEVMAPs that allow programs
	 * as xdp generic
	 */
	dm_fd = bpf_program__fd(skel->progs.xdp_redir_prog);
	err = bpf_set_link_xdp_fd(IFINDEX_LO, dm_fd, XDP_FLAGS_SKB_MODE);
	CHECK(err == 0, "Generic attach of program with 8-byte devmap",
	      "should have failed\n");

	dm_fd = bpf_program__fd(skel->progs.xdp_dummy_dm);
	map_fd = bpf_map__fd(skel->maps.dm_ports);
	err = bpf_obj_get_info_by_fd(dm_fd, &info, &len);
	if (CHECK_FAIL(err))
		goto out_close;

	val.bpf_prog.fd = dm_fd;
	err = bpf_map_update_elem(map_fd, &idx, &val, 0);
	CHECK(err, "Add program to devmap entry",
	      "err %d errno %d\n", err, errno);

	err = bpf_map_lookup_elem(map_fd, &idx, &val);
	CHECK(err, "Read devmap entry", "err %d errno %d\n", err, errno);
	CHECK(info.id != val.bpf_prog.id, "Expected program id in devmap entry",
	      "expected %u read %u\n", info.id, val.bpf_prog.id);

	/* can not attach BPF_XDP_DEVMAP program to a device */
	err = bpf_set_link_xdp_fd(IFINDEX_LO, dm_fd, XDP_FLAGS_SKB_MODE);
	CHECK(err == 0, "Attach of BPF_XDP_DEVMAP program",
	      "should have failed\n");

	val.ifindex = 1;
	val.bpf_prog.fd = bpf_program__fd(skel->progs.xdp_dummy_prog);
	err = bpf_map_update_elem(map_fd, &idx, &val, 0);
	CHECK(err == 0, "Add non-BPF_XDP_DEVMAP program to devmap entry",
	      "should have failed\n");

out_close:
	test_xdp_with_devmap_helpers__destroy(skel);
}

void test_neg_xdp_devmap_helpers(void)
{
	struct test_xdp_devmap_helpers *skel;
	__u32 duration = 0;

	skel = test_xdp_devmap_helpers__open_and_load();
	if (CHECK(skel,
		  "Load of XDP program accessing egress ifindex without attach type",
		  "should have failed\n")) {
		test_xdp_devmap_helpers__destroy(skel);
	}
}


void test_xdp_devmap_attach(void)
{
	if (test__start_subtest("DEVMAP with programs in entries"))
		test_xdp_with_devmap_helpers();

	if (test__start_subtest("Verifier check of DEVMAP programs"))
		test_neg_xdp_devmap_helpers();
}