Commit dc8698ca authored by Andrii Nakryiko's avatar Andrii Nakryiko Committed by Alexei Starovoitov
Browse files

libbpf: Add support for BPF XDP link



Sync UAPI header and add support for using bpf_link-based XDP attachment.
Make xdp/ prog type set expected attach type. Kernel didn't enforce
attach_type for XDP programs before, so there is no backwards compatiblity
issues there.

Also fix section_names selftest to recognize that xdp prog types now have
expected attach type.

Signed-off-by: default avatarAndrii Nakryiko <andriin@fb.com>
Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
Link: https://lore.kernel.org/bpf/20200722064603.3350758-8-andriin@fb.com
parent c1931c97
Loading
Loading
Loading
Loading
+9 −1
Original line number Diff line number Diff line
@@ -230,6 +230,7 @@ enum bpf_attach_type {
	BPF_CGROUP_INET_SOCK_RELEASE,
	BPF_XDP_CPUMAP,
	BPF_SK_LOOKUP,
	BPF_XDP,
	__MAX_BPF_ATTACH_TYPE
};

@@ -242,6 +243,7 @@ enum bpf_link_type {
	BPF_LINK_TYPE_CGROUP = 3,
	BPF_LINK_TYPE_ITER = 4,
	BPF_LINK_TYPE_NETNS = 5,
	BPF_LINK_TYPE_XDP = 6,

	MAX_BPF_LINK_TYPE,
};
@@ -614,7 +616,10 @@ union bpf_attr {

	struct { /* struct used by BPF_LINK_CREATE command */
		__u32		prog_fd;	/* eBPF program to attach */
		union {
			__u32		target_fd;	/* object to attach to */
			__u32		target_ifindex; /* target ifindex */
		};
		__u32		attach_type;	/* attach type */
		__u32		flags;		/* extra flags */
	} link_create;
@@ -4064,6 +4069,9 @@ struct bpf_link_info {
			__u32 netns_ino;
			__u32 attach_type;
		} netns;
		struct {
			__u32 ifindex;
		} xdp;
	};
} __attribute__((aligned(8)));

+8 −1
Original line number Diff line number Diff line
@@ -6915,7 +6915,8 @@ static const struct bpf_sec_def section_defs[] = {
						BPF_XDP_DEVMAP),
	BPF_EAPROG_SEC("xdp_cpumap/",		BPF_PROG_TYPE_XDP,
						BPF_XDP_CPUMAP),
	BPF_PROG_SEC("xdp",			BPF_PROG_TYPE_XDP),
	BPF_EAPROG_SEC("xdp",			BPF_PROG_TYPE_XDP,
						BPF_XDP),
	BPF_PROG_SEC("perf_event",		BPF_PROG_TYPE_PERF_EVENT),
	BPF_PROG_SEC("lwt_in",			BPF_PROG_TYPE_LWT_IN),
	BPF_PROG_SEC("lwt_out",			BPF_PROG_TYPE_LWT_OUT),
@@ -8281,6 +8282,12 @@ bpf_program__attach_netns(struct bpf_program *prog, int netns_fd)
	return bpf_program__attach_fd(prog, netns_fd, "netns");
}

struct bpf_link *bpf_program__attach_xdp(struct bpf_program *prog, int ifindex)
{
	/* target_fd/target_ifindex use the same field in LINK_CREATE */
	return bpf_program__attach_fd(prog, ifindex, "xdp");
}

struct bpf_link *
bpf_program__attach_iter(struct bpf_program *prog,
			 const struct bpf_iter_attach_opts *opts)
+2 −0
Original line number Diff line number Diff line
@@ -257,6 +257,8 @@ LIBBPF_API struct bpf_link *
bpf_program__attach_cgroup(struct bpf_program *prog, int cgroup_fd);
LIBBPF_API struct bpf_link *
bpf_program__attach_netns(struct bpf_program *prog, int netns_fd);
LIBBPF_API struct bpf_link *
bpf_program__attach_xdp(struct bpf_program *prog, int ifindex);

struct bpf_map;

+1 −0
Original line number Diff line number Diff line
@@ -286,6 +286,7 @@ LIBBPF_0.1.0 {
		bpf_map__set_value_size;
		bpf_map__type;
		bpf_map__value_size;
		bpf_program__attach_xdp;
		bpf_program__autoload;
		bpf_program__is_sk_lookup;
		bpf_program__set_autoload;
+1 −1
Original line number Diff line number Diff line
@@ -35,7 +35,7 @@ static struct sec_name_test tests[] = {
		{-EINVAL, 0},
	},
	{"raw_tp/", {0, BPF_PROG_TYPE_RAW_TRACEPOINT, 0}, {-EINVAL, 0} },
	{"xdp", {0, BPF_PROG_TYPE_XDP, 0}, {-EINVAL, 0} },
	{"xdp", {0, BPF_PROG_TYPE_XDP, BPF_XDP}, {0, BPF_XDP} },
	{"perf_event", {0, BPF_PROG_TYPE_PERF_EVENT, 0}, {-EINVAL, 0} },
	{"lwt_in", {0, BPF_PROG_TYPE_LWT_IN, 0}, {-EINVAL, 0} },
	{"lwt_out", {0, BPF_PROG_TYPE_LWT_OUT, 0}, {-EINVAL, 0} },