Skip to content

Commit

Permalink
tools lib bpf: Add bpf_prog_{attach,detach}
Browse files Browse the repository at this point in the history
Commit d8c5b17 ("samples: bpf: add userspace example for attaching
eBPF programs to cgroups") added these functions to samples/libbpf, but
during this merge all of the samples libbpf functionality is shifting to
tools/lib/bpf. Shift these functions there.

Signed-off-by: Joe Stringer <joe@ovn.org>
---
Arnaldo, this is a new patch you didn't previously review which I've
prepared due to the conflict with net-next. I figured it's better to try
to get samples/bpf properly switched over this window rather than defer the
problem and end up having to deal with another merge problem next time
around. I hope that is fine for you.

It's a simple copy/paste/delete with a minor change for sys_bpf() vs
syscall().
  • Loading branch information
joestringer committed Dec 15, 2016
1 parent 5c40f54 commit 353e6f2
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 24 deletions.
21 changes: 0 additions & 21 deletions samples/bpf/libbpf.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,6 @@
#include <arpa/inet.h>
#include "libbpf.h"

int bpf_prog_attach(int prog_fd, int target_fd, enum bpf_attach_type type)
{
union bpf_attr attr = {
.target_fd = target_fd,
.attach_bpf_fd = prog_fd,
.attach_type = type,
};

return syscall(__NR_bpf, BPF_PROG_ATTACH, &attr, sizeof(attr));
}

int bpf_prog_detach(int target_fd, enum bpf_attach_type type)
{
union bpf_attr attr = {
.target_fd = target_fd,
.attach_type = type,
};

return syscall(__NR_bpf, BPF_PROG_DETACH, &attr, sizeof(attr));
}

int open_raw_sock(const char *name)
{
struct sockaddr_ll sll;
Expand Down
3 changes: 0 additions & 3 deletions samples/bpf/libbpf.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@

struct bpf_insn;

int bpf_prog_attach(int prog_fd, int attachable_fd, enum bpf_attach_type type);
int bpf_prog_detach(int attachable_fd, enum bpf_attach_type type);

/* ALU ops on registers, bpf_add|sub|...: dst_reg += src_reg */

#define BPF_ALU64_REG(OP, DST, SRC) \
Expand Down
21 changes: 21 additions & 0 deletions tools/lib/bpf/bpf.c
Original file line number Diff line number Diff line change
Expand Up @@ -167,3 +167,24 @@ int bpf_obj_get(const char *pathname)

return sys_bpf(BPF_OBJ_GET, &attr, sizeof(attr));
}

int bpf_prog_attach(int prog_fd, int target_fd, enum bpf_attach_type type)
{
union bpf_attr attr = {
.target_fd = target_fd,
.attach_bpf_fd = prog_fd,
.attach_type = type,
};

return sys_bpf(BPF_PROG_ATTACH, &attr, sizeof(attr));
}

int bpf_prog_detach(int target_fd, enum bpf_attach_type type)
{
union bpf_attr attr = {
.target_fd = target_fd,
.attach_type = type,
};

return sys_bpf(BPF_PROG_DETACH, &attr, sizeof(attr));
}
3 changes: 3 additions & 0 deletions tools/lib/bpf/bpf.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,8 @@ int bpf_map_delete_elem(int fd, void *key);
int bpf_map_get_next_key(int fd, void *key, void *next_key);
int bpf_obj_pin(int fd, const char *pathname);
int bpf_obj_get(const char *pathname);
int bpf_prog_attach(int prog_fd, int attachable_fd, enum bpf_attach_type type);
int bpf_prog_detach(int attachable_fd, enum bpf_attach_type type);


#endif

0 comments on commit 353e6f2

Please sign in to comment.