Skip to content

Commit ea127e0

Browse files
AsphalttKernel Patches Daemon
authored andcommitted
libbpf: Add support for extended bpf syscall
To support the extended 'bpf()' syscall introduced in the previous commit, introduce the following internal APIs: * 'sys_bpf_ext()' * 'sys_bpf_ext_fd()' They wrap the raw 'syscall()' interface to support passing extended attributes. * 'probe_sys_bpf_ext()' Check whether current kernel supports the extended attributes. Signed-off-by: Leon Hwang <[email protected]>
1 parent 0146580 commit ea127e0

File tree

3 files changed

+44
-0
lines changed

3 files changed

+44
-0
lines changed

tools/lib/bpf/bpf.c

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,39 @@ static inline __u64 ptr_to_u64(const void *ptr)
6969
return (__u64) (unsigned long) ptr;
7070
}
7171

72+
static inline int sys_bpf_ext(enum bpf_cmd cmd, union bpf_attr *attr,
73+
unsigned int size,
74+
struct bpf_common_attr *common_attrs,
75+
unsigned int size_common)
76+
{
77+
cmd = common_attrs ? cmd | BPF_COMMON_ATTRS : cmd & ~BPF_COMMON_ATTRS;
78+
return syscall(__NR_bpf, cmd, attr, size, common_attrs, size_common);
79+
}
80+
81+
static inline int sys_bpf_ext_fd(enum bpf_cmd cmd, union bpf_attr *attr,
82+
unsigned int size,
83+
struct bpf_common_attr *common_attrs,
84+
unsigned int size_common)
85+
{
86+
int fd;
87+
88+
fd = sys_bpf_ext(cmd, attr, size, common_attrs, size_common);
89+
return ensure_good_fd(fd);
90+
}
91+
92+
int probe_sys_bpf_ext(void)
93+
{
94+
const size_t attr_sz = offsetofend(union bpf_attr, prog_token_fd);
95+
union bpf_attr attr;
96+
int fd;
97+
98+
memset(&attr, 0, attr_sz);
99+
fd = syscall(__NR_bpf, BPF_PROG_LOAD | BPF_COMMON_ATTRS, &attr, attr_sz, NULL,
100+
sizeof(struct bpf_common_attr));
101+
fd = errno == EFAULT ? syscall(__NR_memfd_create, "fd", 0) : fd;
102+
return ensure_good_fd(fd);
103+
}
104+
72105
static inline int sys_bpf(enum bpf_cmd cmd, union bpf_attr *attr,
73106
unsigned int size)
74107
{

tools/lib/bpf/features.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -507,6 +507,11 @@ static int probe_kern_arg_ctx_tag(int token_fd)
507507
return probe_fd(prog_fd);
508508
}
509509

510+
static int probe_kern_extended_syscall(int token_fd)
511+
{
512+
return probe_fd(probe_sys_bpf_ext());
513+
}
514+
510515
typedef int (*feature_probe_fn)(int /* token_fd */);
511516

512517
static struct kern_feature_cache feature_cache;
@@ -582,6 +587,9 @@ static struct kern_feature_desc {
582587
[FEAT_BTF_QMARK_DATASEC] = {
583588
"BTF DATASEC names starting from '?'", probe_kern_btf_qmark_datasec,
584589
},
590+
[FEAT_EXTENDED_SYSCALL] = {
591+
"Kernel supports extended syscall", probe_kern_extended_syscall,
592+
},
585593
};
586594

587595
bool feat_supported(struct kern_feature_cache *cache, enum kern_feature_id feat_id)

tools/lib/bpf/libbpf_internal.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,8 @@ enum kern_feature_id {
380380
FEAT_ARG_CTX_TAG,
381381
/* Kernel supports '?' at the front of datasec names */
382382
FEAT_BTF_QMARK_DATASEC,
383+
/* Kernel supports extended syscall */
384+
FEAT_EXTENDED_SYSCALL,
383385
__FEAT_CNT,
384386
};
385387

@@ -740,4 +742,5 @@ int probe_fd(int fd);
740742
#define SHA256_DWORD_SIZE SHA256_DIGEST_LENGTH / sizeof(__u64)
741743

742744
void libbpf_sha256(const void *data, size_t len, __u8 out[SHA256_DIGEST_LENGTH]);
745+
int probe_sys_bpf_ext(void);
743746
#endif /* __LIBBPF_LIBBPF_INTERNAL_H */

0 commit comments

Comments
 (0)