Skip to content

Commit

Permalink
Update bpf syscall decoding
Browse files Browse the repository at this point in the history
* bpf.c: Add decoding for BPF_OBJ_PIN, BPF_OBJ_GET, BPF_PROG_ATTACH,
and BPF_PROG_DETACH.
* xlat/bpf_attach_type.in: New file.
* xlat/bpf_commands.in: Update list of BPF_* command constants.
* xlat/bpf_map_types.in: Update list of BPF_MAP_TYPE_* constants.
* xlat/bpf_prog_types.in: Update list of BPF_PROG_TYPE_* constants.
* tests/bpf.c: New tests.
  • Loading branch information
qmonnet committed Jan 25, 2017
1 parent 3fac547 commit 5232624
Show file tree
Hide file tree
Showing 6 changed files with 154 additions and 0 deletions.
94 changes: 94 additions & 0 deletions bpf.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#include "xlat/bpf_map_types.h"
#include "xlat/bpf_prog_types.h"
#include "xlat/bpf_map_update_elem_flags.h"
#include "xlat/bpf_attach_type.h"

static int
bpf_map_create(struct tcb *const tcp, const kernel_ulong_t addr,
Expand Down Expand Up @@ -177,6 +178,87 @@ bpf_prog_load(struct tcb *const tcp, const kernel_ulong_t addr,
return RVAL_DECODED | RVAL_FD;
}

static int
bpf_obj_manage(struct tcb *const tcp, const kernel_ulong_t addr,
unsigned int size)
{
struct {
uint64_t ATTRIBUTE_ALIGNED(8) pathname;
uint32_t bpf_fd;
} attr = {};

if (!size) {
printaddr(addr);
return RVAL_DECODED | RVAL_FD;
}
if (size > sizeof(attr))
size = sizeof(attr);
if (umoven_or_printaddr(tcp, addr, size, &attr))
return RVAL_DECODED | RVAL_FD;

tprintf("{pathname=");
printpath(tcp, attr.pathname);
tprints(", bpf_fd=");
printfd(tcp, attr.bpf_fd);
tprintf("}");

return RVAL_DECODED | RVAL_FD;
}

static int
bpf_prog_attach(struct tcb *const tcp, const kernel_ulong_t addr,
unsigned int size)
{
struct {
uint32_t target_fd, attach_bpf_fd, attach_type;
} attr = {};

if (!size) {
printaddr(addr);
return RVAL_DECODED;
}
if (size > sizeof(attr))
size = sizeof(attr);
if (umoven_or_printaddr(tcp, addr, size, &attr))
return RVAL_DECODED;

tprintf("{target_fd=");
printfd(tcp, attr.target_fd);
tprintf(", attach_bpf_fd=");
printfd(tcp, attr.attach_bpf_fd);
tprintf(", attach_type=");
printxval64(bpf_attach_type, attr.attach_type, "BPF_???");
tprintf("}");

return RVAL_DECODED;
}

static int
bpf_prog_detach(struct tcb *const tcp, const kernel_ulong_t addr,
unsigned int size)
{
struct {
uint32_t target_fd, attach_bpf_fd, attach_type;
} attr = {};

if (!size) {
printaddr(addr);
return RVAL_DECODED;
}
if (size > sizeof(attr))
size = sizeof(attr);
if (umoven_or_printaddr(tcp, addr, size, &attr))
return RVAL_DECODED;

tprintf("{target_fd=");
printfd(tcp, attr.target_fd);
tprintf(", attach_type=");
printxval64(bpf_attach_type, attr.attach_type, "BPF_???");
tprintf("}");

return RVAL_DECODED;
}

SYS_FUNC(bpf)
{
const unsigned int cmd = tcp->u_arg[0];
Expand Down Expand Up @@ -208,6 +290,18 @@ SYS_FUNC(bpf)
case BPF_PROG_LOAD:
rc = bpf_prog_load(tcp, addr, size);
break;
case BPF_OBJ_PIN:
rc = bpf_obj_manage(tcp, addr, size);
break;
case BPF_OBJ_GET:
rc = bpf_obj_manage(tcp, addr, size);
break;
case BPF_PROG_ATTACH:
rc = bpf_prog_attach(tcp, addr, size);
break;
case BPF_PROG_DETACH:
rc = bpf_prog_detach(tcp, addr, size);
break;
default:
printaddr(addr);
break;
Expand Down
45 changes: 45 additions & 0 deletions tests/bpf.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,27 @@ prog_load(void)
return syscall(__NR_bpf, BPF_PROG_LOAD, &attr, sizeof(attr));
}

static int
obj_manage(int cmd)
{
union bpf_attr attr = {
.pathname = (uint64_t)(void *) "/sys/fs/bpf/foo/bar",
.bpf_fd = -1
};
return syscall(__NR_bpf, cmd, &attr, sizeof(attr));
}

static int
prog_cgroup(int cmd)
{
union bpf_attr attr = {
.target_fd = -1,
.attach_bpf_fd = -1,
.attach_type = BPF_CGROUP_INET_INGRESS
};
return syscall(__NR_bpf, cmd, &attr, sizeof(attr));
}

int
main(void)
{
Expand Down Expand Up @@ -119,6 +140,30 @@ main(void)
"kern_version=0\\}, %u\\) += -1 .*\n",
insns, log_buf, (unsigned) sizeof(union bpf_attr));

if (!obj_manage(BPF_OBJ_PIN))
perror_msg_and_skip("BPF_OBJ_PIN");
printf("bpf\\(BPF_OBJ_PIN, "
"\\{pathname=\"/sys/fs/bpf/foo/bar\", bpf_fd=-1\\}, %u\\) += -1 .*\n",
(unsigned) sizeof(union bpf_attr));

if (!obj_manage(BPF_OBJ_GET))
perror_msg_and_skip("BPF_OBJ_GET");
printf("bpf\\(BPF_OBJ_GET, "
"\\{pathname=\"/sys/fs/bpf/foo/bar\", bpf_fd=-1\\}, %u\\) += -1 .*\n",
(unsigned) sizeof(union bpf_attr));

if (!prog_cgroup(BPF_PROG_ATTACH))
perror_msg_and_skip("BPF_PROG_ATTACH");
printf("bpf\\(BPF_PROG_ATTACH, {target_fd=-1, attach_bpf_fd=-1, "
"attach_type=BPF_CGROUP_INET_INGRESS\\}, %u\\) += -1 .*\n",
(unsigned) sizeof(union bpf_attr));

if (!prog_cgroup(BPF_PROG_DETACH))
perror_msg_and_skip("BPF_PROG_DETACH");
printf("bpf\\(BPF_PROG_DETACH, \\{target_fd=-1, "
"attach_type=BPF_CGROUP_INET_INGRESS\\}, %u\\) += -1 .*\n",
(unsigned) sizeof(union bpf_attr));

return 0;
}

Expand Down
3 changes: 3 additions & 0 deletions xlat/bpf_attach_type.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
BPF_CGROUP_INET_INGRESS 0
BPF_CGROUP_INET_EGRESS 1
BPF_CGROUP_INET_SOCK_CREATE 2
4 changes: 4 additions & 0 deletions xlat/bpf_commands.in
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,7 @@ BPF_MAP_UPDATE_ELEM 2
BPF_MAP_DELETE_ELEM 3
BPF_MAP_GET_NEXT_KEY 4
BPF_PROG_LOAD 5
BPF_OBJ_PIN 6
BPF_OBJ_GET 7
BPF_PROG_ATTACH 8
BPF_PROG_DETACH 9
3 changes: 3 additions & 0 deletions xlat/bpf_map_types.in
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,6 @@ BPF_MAP_TYPE_PERCPU_HASH 5
BPF_MAP_TYPE_PERCPU_ARRAY 6
BPF_MAP_TYPE_STACK_TRACE 7
BPF_MAP_TYPE_CGROUP_ARRAY 8
BPF_MAP_TYPE_LRU_HASH 9
BPF_MAP_TYPE_LRU_PERCPU_HASH 10
BPF_MAP_TYPE_LPM_TRIE 11
5 changes: 5 additions & 0 deletions xlat/bpf_prog_types.in
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,8 @@ BPF_PROG_TYPE_SCHED_ACT 4
BPF_PROG_TYPE_TRACEPOINT 5
BPF_PROG_TYPE_XDP 6
BPF_PROG_TYPE_PERF_EVENT 7
BPF_PROG_TYPE_CGROUP_SKB 8
BPF_PROG_TYPE_CGROUP_SOCK 9
BPF_PROG_TYPE_LWT_IN 10
BPF_PROG_TYPE_LWT_OUT 11
BPF_PROG_TYPE_LWT_XMIT 12

0 comments on commit 5232624

Please sign in to comment.