Skip to content

Commit d60d81a

Browse files
jsitnickiAlexei Starovoitov
authored andcommitted
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: Jakub Sitnicki <[email protected]> Signed-off-by: Alexei Starovoitov <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent 0c047ec commit d60d81a

File tree

3 files changed

+21
-5
lines changed

3 files changed

+21
-5
lines changed

tools/lib/bpf/libbpf.c

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7896,8 +7896,9 @@ static struct bpf_link *attach_iter(const struct bpf_sec_def *sec,
78967896
return bpf_program__attach_iter(prog, NULL);
78977897
}
78987898

7899-
struct bpf_link *
7900-
bpf_program__attach_cgroup(struct bpf_program *prog, int cgroup_fd)
7899+
static struct bpf_link *
7900+
bpf_program__attach_fd(struct bpf_program *prog, int target_fd,
7901+
const char *target_name)
79017902
{
79027903
enum bpf_attach_type attach_type;
79037904
char errmsg[STRERR_BUFSIZE];
@@ -7917,19 +7918,31 @@ bpf_program__attach_cgroup(struct bpf_program *prog, int cgroup_fd)
79177918
link->detach = &bpf_link__detach_fd;
79187919

79197920
attach_type = bpf_program__get_expected_attach_type(prog);
7920-
link_fd = bpf_link_create(prog_fd, cgroup_fd, attach_type, NULL);
7921+
link_fd = bpf_link_create(prog_fd, target_fd, attach_type, NULL);
79217922
if (link_fd < 0) {
79227923
link_fd = -errno;
79237924
free(link);
7924-
pr_warn("program '%s': failed to attach to cgroup: %s\n",
7925-
bpf_program__title(prog, false),
7925+
pr_warn("program '%s': failed to attach to %s: %s\n",
7926+
bpf_program__title(prog, false), target_name,
79267927
libbpf_strerror_r(link_fd, errmsg, sizeof(errmsg)));
79277928
return ERR_PTR(link_fd);
79287929
}
79297930
link->fd = link_fd;
79307931
return link;
79317932
}
79327933

7934+
struct bpf_link *
7935+
bpf_program__attach_cgroup(struct bpf_program *prog, int cgroup_fd)
7936+
{
7937+
return bpf_program__attach_fd(prog, cgroup_fd, "cgroup");
7938+
}
7939+
7940+
struct bpf_link *
7941+
bpf_program__attach_netns(struct bpf_program *prog, int netns_fd)
7942+
{
7943+
return bpf_program__attach_fd(prog, netns_fd, "netns");
7944+
}
7945+
79337946
struct bpf_link *
79347947
bpf_program__attach_iter(struct bpf_program *prog,
79357948
const struct bpf_iter_attach_opts *opts)

tools/lib/bpf/libbpf.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,8 @@ LIBBPF_API struct bpf_link *
253253
bpf_program__attach_lsm(struct bpf_program *prog);
254254
LIBBPF_API struct bpf_link *
255255
bpf_program__attach_cgroup(struct bpf_program *prog, int cgroup_fd);
256+
LIBBPF_API struct bpf_link *
257+
bpf_program__attach_netns(struct bpf_program *prog, int netns_fd);
256258

257259
struct bpf_map;
258260

tools/lib/bpf/libbpf.map

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,7 @@ LIBBPF_0.0.9 {
262262
bpf_link_get_fd_by_id;
263263
bpf_link_get_next_id;
264264
bpf_program__attach_iter;
265+
bpf_program__attach_netns;
265266
perf_buffer__consume;
266267
ring_buffer__add;
267268
ring_buffer__consume;

0 commit comments

Comments
 (0)