Commit d60d81ac authored by Jakub Sitnicki's avatar Jakub Sitnicki Committed by Alexei Starovoitov

libbpf: Add support for bpf_link-based netns attachment

Add bpf_program__attach_nets(), which uses LINK_CREATE subcommand to create
an FD-based kernel bpf_link, for attach types tied to network namespace,
that is BPF_FLOW_DISSECTOR for the moment.
Signed-off-by: default avatarJakub Sitnicki <jakub@cloudflare.com>
Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
Link: https://lore.kernel.org/bpf/20200531082846.2117903-7-jakub@cloudflare.com
parent 0c047ecb
...@@ -7896,8 +7896,9 @@ static struct bpf_link *attach_iter(const struct bpf_sec_def *sec, ...@@ -7896,8 +7896,9 @@ static struct bpf_link *attach_iter(const struct bpf_sec_def *sec,
return bpf_program__attach_iter(prog, NULL); return bpf_program__attach_iter(prog, NULL);
} }
struct bpf_link * static struct bpf_link *
bpf_program__attach_cgroup(struct bpf_program *prog, int cgroup_fd) bpf_program__attach_fd(struct bpf_program *prog, int target_fd,
const char *target_name)
{ {
enum bpf_attach_type attach_type; enum bpf_attach_type attach_type;
char errmsg[STRERR_BUFSIZE]; char errmsg[STRERR_BUFSIZE];
...@@ -7917,12 +7918,12 @@ bpf_program__attach_cgroup(struct bpf_program *prog, int cgroup_fd) ...@@ -7917,12 +7918,12 @@ bpf_program__attach_cgroup(struct bpf_program *prog, int cgroup_fd)
link->detach = &bpf_link__detach_fd; link->detach = &bpf_link__detach_fd;
attach_type = bpf_program__get_expected_attach_type(prog); attach_type = bpf_program__get_expected_attach_type(prog);
link_fd = bpf_link_create(prog_fd, cgroup_fd, attach_type, NULL); link_fd = bpf_link_create(prog_fd, target_fd, attach_type, NULL);
if (link_fd < 0) { if (link_fd < 0) {
link_fd = -errno; link_fd = -errno;
free(link); free(link);
pr_warn("program '%s': failed to attach to cgroup: %s\n", pr_warn("program '%s': failed to attach to %s: %s\n",
bpf_program__title(prog, false), bpf_program__title(prog, false), target_name,
libbpf_strerror_r(link_fd, errmsg, sizeof(errmsg))); libbpf_strerror_r(link_fd, errmsg, sizeof(errmsg)));
return ERR_PTR(link_fd); return ERR_PTR(link_fd);
} }
...@@ -7930,6 +7931,18 @@ bpf_program__attach_cgroup(struct bpf_program *prog, int cgroup_fd) ...@@ -7930,6 +7931,18 @@ bpf_program__attach_cgroup(struct bpf_program *prog, int cgroup_fd)
return link; return link;
} }
struct bpf_link *
bpf_program__attach_cgroup(struct bpf_program *prog, int cgroup_fd)
{
return bpf_program__attach_fd(prog, cgroup_fd, "cgroup");
}
struct bpf_link *
bpf_program__attach_netns(struct bpf_program *prog, int netns_fd)
{
return bpf_program__attach_fd(prog, netns_fd, "netns");
}
struct bpf_link * struct bpf_link *
bpf_program__attach_iter(struct bpf_program *prog, bpf_program__attach_iter(struct bpf_program *prog,
const struct bpf_iter_attach_opts *opts) const struct bpf_iter_attach_opts *opts)
......
...@@ -253,6 +253,8 @@ LIBBPF_API struct bpf_link * ...@@ -253,6 +253,8 @@ LIBBPF_API struct bpf_link *
bpf_program__attach_lsm(struct bpf_program *prog); bpf_program__attach_lsm(struct bpf_program *prog);
LIBBPF_API struct bpf_link * LIBBPF_API struct bpf_link *
bpf_program__attach_cgroup(struct bpf_program *prog, int cgroup_fd); 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);
struct bpf_map; struct bpf_map;
......
...@@ -262,6 +262,7 @@ LIBBPF_0.0.9 { ...@@ -262,6 +262,7 @@ LIBBPF_0.0.9 {
bpf_link_get_fd_by_id; bpf_link_get_fd_by_id;
bpf_link_get_next_id; bpf_link_get_next_id;
bpf_program__attach_iter; bpf_program__attach_iter;
bpf_program__attach_netns;
perf_buffer__consume; perf_buffer__consume;
ring_buffer__add; ring_buffer__add;
ring_buffer__consume; ring_buffer__consume;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment