BPF testing
cover BPF testing
Hello,
This patch set introduces a very basic test which kicks the tires of the bpf system call. It doesn't actually load a eBPF program, I will create another test for that. However I have some concerns which I will discuss while doing that.
There are already extensive BPF tests in the kernel selftests. These appear to be quite complex and test a variety of functionality. They also are far less structured than LTP's modern tests and are tied to the kernel tree which makes using them in QA a pain. There are also some tests in the BCC project, which may test the kernel as a byproduct.
So there are a number of options which are not necessarily mutually exclusive:
1) Port (some of) the selftests to the LTP.
2) Port the LTP library to the selftests.
3) Focus the LTP's BPF tests on reproducing specific high impact bugs.
This patch set copies in the necessary headers so that we have zero external dependencies. I will also use raw byte code for the program test which is at least acceptable for trivial programs. So we do not need BCC or Clang/LLVM with eBPF support or matching kernel sources to generate offsets into internal structures.
For the time being atleast my preference would be for (3) while avoiding taking on any dependencies to ensure those tests are run by users mostly ignorant of BPF, but are still exposed to critical bugs in the BPF stack.
Richard Palethorpe (2):
Essential headers for BPF map creation
BPF: Sanity check creating and updating maps
include/lapi/bpf.h | 242 ++++++++++++++++++++++ include/lapi/syscalls/aarch64.in | 1 + include/lapi/syscalls/i386.in | 1 + include/lapi/syscalls/s390.in | 1 + include/lapi/syscalls/sparc.in | 1 + include/lapi/syscalls/x86_64.in | 1 + runtest/syscalls | 2 + testcases/kernel/syscalls/bpf/.gitignore | 1 + testcases/kernel/syscalls/bpf/Makefile | 10 + testcases/kernel/syscalls/bpf/bpf_map01.c | 138 ++++++++++++ 10 files changed, 398 insertions(+) create mode 100644 include/lapi/bpf.h create mode 100644 testcases/kernel/syscalls/bpf/.gitignore create mode 100644 testcases/kernel/syscalls/bpf/Makefile create mode 100644 testcases/kernel/syscalls/bpf/bpf_map01.c
Signature · 1 line
2.22.0
PATCH 1/2 Essential headers for BPF map creation
--- include/lapi/bpf.h | 242 +++++++++++++++++++++++++++++++ include/lapi/syscalls/aarch64.in | 1 + include/lapi/syscalls/i386.in | 1 + include/lapi/syscalls/s390.in | 1 + include/lapi/syscalls/sparc.in | 1 + include/lapi/syscalls/x86_64.in | 1 + 6 files changed, 247 insertions(+) create mode 100644 include/lapi/bpf.h
include/lapi/bpf.h new file +242 -0
@@ -0,0 +1,242 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* +* Copyright (c) 2019 Richard Palethorpe <[email protected]> +* +* Essential Extended Berkeley Packet Filter (eBPF) headers +* +* Mostly copied/adapted from linux/bpf.h and libbpf so that we can perform +* some eBPF testing without any external dependencies. +*/ + +#ifndef BPF_H +# define BPF_H + +#include <stdint.h> + +#include "lapi/syscalls.h" + +/* Start copy from linux/bpf.h */ +enum bpf_cmd { +BPF_MAP_CREATE , +BPF_MAP_LOOKUP_ELEM , +BPF_MAP_UPDATE_ELEM , +BPF_MAP_DELETE_ELEM , +BPF_MAP_GET_NEXT_KEY , +BPF_PROG_LOAD , +BPF_OBJ_PIN , +BPF_OBJ_GET , +BPF_PROG_ATTACH , +BPF_PROG_DETACH , +BPF_PROG_TEST_RUN , +BPF_PROG_GET_NEXT_ID , +BPF_MAP_GET_NEXT_ID , +BPF_PROG_GET_FD_BY_ID , +BPF_MAP_GET_FD_BY_ID , +BPF_OBJ_GET_INFO_BY_FD , +BPF_PROG_QUERY , +BPF_RAW_TRACEPOINT_OPEN , +BPF_BTF_LOAD , +BPF_BTF_GET_FD_BY_ID , +BPF_TASK_FD_QUERY , +BPF_MAP_LOOKUP_AND_DELETE_ELEM , +BPF_MAP_FREEZE , +}; + +enum bpf_map_type { +BPF_MAP_TYPE_UNSPEC , +BPF_MAP_TYPE_HASH , +BPF_MAP_TYPE_ARRAY , +BPF_MAP_TYPE_PROG_ARRAY , +BPF_MAP_TYPE_PERF_EVENT_ARRAY , +BPF_MAP_TYPE_PERCPU_HASH , +BPF_MAP_TYPE_PERCPU_ARRAY , +BPF_MAP_TYPE_STACK_TRACE , +BPF_MAP_TYPE_CGROUP_ARRAY , +BPF_MAP_TYPE_LRU_HASH , +BPF_MAP_TYPE_LRU_PERCPU_HASH , +BPF_MAP_TYPE_LPM_TRIE , +BPF_MAP_TYPE_ARRAY_OF_MAPS , +BPF_MAP_TYPE_HASH_OF_MAPS , +BPF_MAP_TYPE_DEVMAP , +BPF_MAP_TYPE_SOCKMAP , +BPF_MAP_TYPE_CPUMAP , +BPF_MAP_TYPE_XSKMAP , +BPF_MAP_TYPE_SOCKHASH , +BPF_MAP_TYPE_CGROUP_STORAGE , +BPF_MAP_TYPE_REUSEPORT_SOCKARRAY , +BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE , +BPF_MAP_TYPE_QUEUE , +BPF_MAP_TYPE_STACK , +BPF_MAP_TYPE_SK_STORAGE , +}; + +#define BPF_OBJ_NAME_LEN 16U + +#define BPF_ANY 0/* create new element or update existing */ +#define BPF_NOEXIST 1/* create new element if it didn't exist */ +#define BPF_EXIST 2/* update existing element */ +#define BPF_F_LOCK 4/* spin_lock-ed map_lookup/map_update */ + +#define aligned_uint64_t uint64_t __attribute__((aligned(8))) + +union bpf_attr { +struct {/* anonymous struct used by BPF_MAP_CREATE command */ +uint32_t map_type;/* one of enum bpf_map_type */ +uint32_t key_size;/* size of key in bytes */ +uint32_t value_size;/* size of value in bytes */ +uint32_t max_entries;/* max number of entries in a map */ +uint32_t map_flags;/* BPF_MAP_CREATE related +* flags defined above. +*/ +uint32_t inner_map_fd;/* fd pointing to the inner map */ +uint32_t numa_node;/* numa node (effective only if +* BPF_F_NUMA_NODE is set). +*/ +char map_name[BPF_OBJ_NAME_LEN ]; +uint32_t map_ifindex;/* ifindex of netdev to create on */ +uint32_t btf_fd;/* fd pointing to a BTF type data */ +uint32_t btf_key_type_id;/* BTF type_id of the key */ +uint32_t btf_value_type_id;/* BTF type_id of the value */ + }; + +struct {/* anonymous struct used by BPF_MAP_*_ELEM commands */ +uint32_t map_fd; +aligned_uint64_t key; +union { +aligned_uint64_t value; +aligned_uint64_t next_key; + }; +uint64_t flags; + }; + +struct {/* anonymous struct used by BPF_PROG_LOAD command */ +uint32_t prog_type;/* one of enum bpf_prog_type */ +uint32_t insn_cnt; +aligned_uint64_t insns; +aligned_uint64_t license; +uint32_t log_level;/* verbosity level of verifier */ +uint32_t log_size;/* size of user buffer */ +aligned_uint64_t log_buf;/* user supplied buffer */ +uint32_t kern_version;/* not used */ +uint32_t prog_flags; +char prog_name[BPF_OBJ_NAME_LEN ]; +uint32_t prog_ifindex;/* ifindex of netdev to prep for */ +/* For some prog types expected attach type must be known at +* load time to verify attach type specific parts of prog +* (context accesses, allowed helpers, etc). +*/ +uint32_t expected_attach_type; +uint32_t prog_btf_fd;/* fd pointing to BTF type data */ +uint32_t func_info_rec_size;/* userspace bpf_func_info size */ +aligned_uint64_t func_info;/* func info */ +uint32_t func_info_cnt;/* number of bpf_func_info records */ +uint32_t line_info_rec_size;/* userspace bpf_line_info size */ +aligned_uint64_t line_info;/* line info */ +uint32_t line_info_cnt;/* number of bpf_line_info records */ + }; + +struct {/* anonymous struct used by BPF_OBJ_* commands */ +aligned_uint64_t pathname; +uint32_t bpf_fd; +uint32_t file_flags; + }; + +struct {/* anonymous struct used by BPF_PROG_ATTACH/DETACH commands */ +uint32_t target_fd;/* container object to attach to */ +uint32_t attach_bpf_fd;/* eBPF program to attach */ +uint32_t attach_type; +uint32_t attach_flags; + }; + +struct {/* anonymous struct used by BPF_PROG_TEST_RUN command */ +uint32_t prog_fd; +uint32_t retval; +uint32_t data_size_in;/* input: len of data_in */ +uint32_t data_size_out;/* input/output: len of data_out +* returns ENOSPC if data_out +* is too small. +*/ +aligned_uint64_t data_in; +aligned_uint64_t data_out; +uint32_t repeat; +uint32_t duration; +uint32_t ctx_size_in;/* input: len of ctx_in */ +uint32_t ctx_size_out;/* input/output: len of ctx_out +* returns ENOSPC if ctx_out +* is too small. +*/ +aligned_uint64_t ctx_in; +aligned_uint64_t ctx_out; + } test; + +struct {/* anonymous struct used by BPF_*_GET_*_ID */ +union { +uint32_t start_id; +uint32_t prog_id; +uint32_t map_id; +uint32_t btf_id; + }; +uint32_t next_id; +uint32_t open_flags; + }; + +struct {/* anonymous struct used by BPF_OBJ_GET_INFO_BY_FD */ +uint32_t bpf_fd; +uint32_t info_len; +aligned_uint64_t info; + } info; + +struct {/* anonymous struct used by BPF_PROG_QUERY command */ +uint32_t target_fd;/* container object to query */ +uint32_t attach_type; +uint32_t query_flags; +uint32_t attach_flags; +aligned_uint64_t prog_ids; +uint32_t prog_cnt; + } query; + +struct { +uint64_t name; +uint32_t prog_fd; + } raw_tracepoint; + +struct {/* anonymous struct for BPF_BTF_LOAD */ +aligned_uint64_t btf; +aligned_uint64_t btf_log_buf; +uint32_t btf_size; +uint32_t btf_log_size; +uint32_t btf_log_level; + }; + +struct { +uint32_t pid;/* input: pid */ +uint32_t fd;/* input: fd */ +uint32_t flags;/* input: flags */ +uint32_t buf_len;/* input/output: buf len */ +aligned_uint64_t buf;/* input/output: +* tp_name for tracepoint +* symbol for kprobe +* filename for uprobe +*/ +uint32_t prog_id;/* output: prod_id */ +uint32_t fd_type;/* output: BPF_FD_TYPE_* */ +uint64_t probe_offset;/* output: probe_offset */ +uint64_t probe_addr;/* output: probe_addr */ + } task_fd_query; +} __attribute__((aligned (8 ))); + +/* End copy from linux/bpf.h */ + +/* Start copy from tools/lib/bpf */ +inline uint64_t ptr_to_u64 (const void *ptr) +{ +return (uint64_t ) (unsigned long ) ptr; +} + +inline int bpf (enum bpf_cmd cmd,union bpf_attr *attr,unsigned int size) +{ +return tst_syscall (__NR_bpf, cmd, attr, size); +} +/* End copy from tools/lib/bpf */ + +#endif /* BPF_H */
include/lapi/syscalls/aarch64.in +1 -0
@@ -258,6 +258,7 @@ process_vm_writev 271 kcmp 272 getrandom 278 memfd_create 279 +bpf 280 userfaultfd 282 membarrier 283 execveat 281
include/lapi/syscalls/i386.in +1 -0
@@ -340,6 +340,7 @@ sched_getattr 352 renameat2 354 getrandom 355 memfd_create 356 +bpf 357 execveat 358 userfaultfd 374 membarrier 375
include/lapi/syscalls/s390.in +1 -0
@@ -331,6 +331,7 @@ sched_getattr 346 renameat2 347 getrandom 349 memfd_create 350 +bpf 351 userfaultfd 355 membarrier 356 execveat 354
include/lapi/syscalls/sparc.in +1 -0
@@ -336,6 +336,7 @@ kcmp 341 renameat2 345 getrandom 347 memfd_create 348 +bpf 349 membarrier 351 userfaultfd 352 execveat 350
include/lapi/syscalls/x86_64.in +1 -0
@@ -307,6 +307,7 @@ sched_getattr 315 renameat2 316 getrandom 318 memfd_create 319 +bpf 321 execveat 322 userfaultfd 323 membarrier 324
Signature · 1 line
2.22.0
PATCH 1/2 Essential headers for BPF map creation
Hi Richie,
Quoted 1 line
Signed-off-by Richard Palethorpe <[email protected]>
Quoted 16 lines
--- include/lapi/bpf.h | 242 +++++++++++++++++++++++++++++++ include/lapi/syscalls/aarch64.in | 1 + include/lapi/syscalls/i386.in | 1 + include/lapi/syscalls/s390.in | 1 + include/lapi/syscalls/sparc.in | 1 + include/lapi/syscalls/x86_64.in | 1 + 6 files changed, 247 insertions(+) create mode 100644 include/lapi/bpf.hinclude/lapi/bpf.h new file +0
-0
include/lapi/bpf.h line 8
Quoted 8 lines
include/lapi/bpf.h new file +8
-0@@ -0,0 +1,242 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* +* Copyright (c) 2019 Richard Palethorpe <[email protected]> +* +* Essential Extended Berkeley Packet Filter (eBPF) headers +* +* Mostly copied/adapted from linux/bpf.h and libbpf so that we can perform +* some eBPF testing without any external dependencies.
Probably the only sane way. But it will be uncomfortable to keep the header updated. Simple copy of include/uapi/linux/bpf.h would be easier (but we'd require to have kernel headers anyway (<linux/types.h> and <linux/bpf_common.h>).
Kind regards,
Petr
PATCH 1/2 Essential headers for BPF map creation
Hello,
Quoted 25 lines · Petr Vorel
Hi Richie,
Quoted 1 line
Signed-off-by Richard Palethorpe <[email protected]>Reviewed-by Petr Vorel <[email protected]>Quoted 20 lines
--- include/lapi/bpf.h | 242 +++++++++++++++++++++++++++++++ include/lapi/syscalls/aarch64.in | 1 + include/lapi/syscalls/i386.in | 1 + include/lapi/syscalls/s390.in | 1 + include/lapi/syscalls/sparc.in | 1 + include/lapi/syscalls/x86_64.in | 1 + 6 files changed, 247 insertions(+) create mode 100644 include/lapi/bpf.hinclude/lapi/bpf.h new file +4
-0@@ -0,0 +1,242 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* +* Copyright (c) 2019 Richard Palethorpe <[email protected]> +*
Quoted 8 lines
include/lapi/bpf.h line 8
Quoted 4 lines
include/lapi/bpf.h new file +4
-0+* Essential Extended Berkeley Packet Filter (eBPF) headers +* +* Mostly copied/adapted from linux/bpf.h and libbpf so that we can perform +* some eBPF testing without any external dependencies. Probably the only sane way. But it will be uncomfortable to keep the header updated. Simple copy of
include/uapi/linux/bpf.hwould be easier (but we'd require to have kernel headers anyway (<linux/types.h> and <linux/bpf_common.h>).
It hasn't been a problem in the past, we just need to add the missing parts when adding a new test.
--
Thank you,
Richard.
PATCH 2/2 BPF: Sanity check creating and updating maps
In reply to Richard Palethorpe--- runtest/syscalls | 2 + testcases/kernel/syscalls/bpf/.gitignore | 1 + testcases/kernel/syscalls/bpf/Makefile | 10 ++ testcases/kernel/syscalls/bpf/bpf_map01.c | 138 ++++++++++++++++++++++ 4 files changed, 151 insertions(+) create mode 100644 testcases/kernel/syscalls/bpf/.gitignore create mode 100644 testcases/kernel/syscalls/bpf/Makefile create mode 100644 testcases/kernel/syscalls/bpf/bpf_map01.c
runtest/syscalls +2 -0
@@ -32,6 +32,8 @@ bind01 bind01 bind02 bind02 bind03 bind03 +bpf_map01 bpf_map01 + brk01 brk01 capget01 capget01
testcases/kernel/syscalls/bpf/.gitignore new file +1 -0
@@ -0,0 +1 @@ +bpf_map01
testcases/kernel/syscalls/bpf/Makefile new file +10 -0
@@ -0,0 +1,10 @@ +# SPDX-License-Identifier: GPL-2.0-or-later +# Copyright (c) 2019 Linux Test Project + +top_srcdir ?= ../../../.. + +include $(top_srcdir)/include/mk/testcases.mk + +CFLAGS += -D_GNU_SOURCE + +include $(top_srcdir)/include/mk/generic_leaf_target.mk
testcases/kernel/syscalls/bpf/bpf_map01.c new file +138 -0
138 lines not on this page · raw message
Signature · 1 line
2.22.0
PATCH 2/2 BPF: Sanity check creating and updating maps
Hi Richie,
Quoted 1 line
Signed-off-by Richard Palethorpe <[email protected]>
LGTM with minor comments.
...
Quoted 1 line
testcases/kernel/syscalls/bpf/bpf_map01.c +0
-0
...
Quoted 30 lines
testcases/kernel/syscalls/bpf/bpf_map01.c +30
-0+#include <limits.h> +#include <string.h> + +#include "config.h" +#include "tst_test.h" +#include "lapi/bpf.h" + +#define KEY_SZ 8 +#define VAL_SZ 1024 + +struct map_type { +uint32_t id; +char *name; +}; + +static const struct map_type map_types[] = { + {BPF_MAP_TYPE_HASH ,"hash" }, + {BPF_MAP_TYPE_ARRAY ,"array" } +}; + +static void *key; +static void *val0; +static void *val1; + +static void setup (void ) +{ + key =SAFE_MALLOC (KEY_SZ ); +memset (key,0 , (size_t )KEY_SZ ); + val0 =SAFE_MALLOC (VAL_SZ ); + val1 =SAFE_MALLOC (VAL_SZ );
testcases/kernel/syscalls/bpf/bpf_map01.c line 50
Quoted 10 lines
testcases/kernel/syscalls/bpf/bpf_map01.c +10
-0+memset (val1,0 , (size_t )VAL_SZ ); +} + +void run (unsigned int n) +{ +int fd, i; +union bpf_attr attr; +memset (&attr,0 ,sizeof (attr)); + attr.map_type = map_types[n].id; + attr.key_size = n ==0 ?KEY_SZ :4 ;
Out of curiosity why 4? As whole test is working for KEY_SZ >= 1 (but for second test it's needed to be 4).
testcases/kernel/syscalls/bpf/bpf_map01.c line 52
Quoted 2 lines
testcases/kernel/syscalls/bpf/bpf_map01.c +2
-0+ attr.value_size =VAL_SZ ; + attr.max_entries =1 ;
+ we usually use struct test_case + description at the test start, but I guess it's ok like this.
Quoted 21 lines
testcases/kernel/syscalls/bpf/bpf_map01.c +21
-0+ +if ((fd =bpf (BPF_MAP_CREATE , &attr,sizeof (attr))) ==-1 ) { +tst_brk (TFAIL |TERRNO ,"Failed to create %s map" , + map_types[n].name); + }else { +tst_res (TPASS ,"Created %s map" , map_types[n].name); + } + +if (n ==0 ) +memcpy (key,"12345678" ,KEY_SZ ); +else +memset (key,0 ,4 ); + +memset (&attr,0 ,sizeof (attr)); + attr.map_fd = fd; + attr.key =ptr_to_u64 (key); + attr.value =ptr_to_u64 (val1); + +TEST (bpf (BPF_MAP_LOOKUP_ELEM , &attr,sizeof (attr))); +if (n ==0 ) { +if (TST_RET !=-1 ||TST_ERR !=ENOENT ) {
testcases/kernel/syscalls/bpf/bpf_map01.c line 83
Quoted 10 lines
testcases/kernel/syscalls/bpf/bpf_map01.c +10
-0+tst_res (TFAIL |TTERRNO , +"Empty hash map lookup should fail with ENOENT" ); + }else { +tst_res (TPASS |TTERRNO ,"Empty hash map lookup" ); + } + }else if (TST_RET !=-1 ) { +for (i =0 ;;) { +if (*(char *) val1 !=0 ) { +tst_res (TFAIL , +"Preallocated array map val not zero" );
I guess here is missing break.
testcases/kernel/syscalls/bpf/bpf_map01.c line 92
Quoted 9 lines
testcases/kernel/syscalls/bpf/bpf_map01.c +9
-0+ }else if (++i >=VAL_SZ ) { +tst_res (TPASS , +"Preallocated array map lookup" ); +break ; + } + } + }else { +tst_res (TFAIL |TERRNO ,"Prellocated array map lookup" ); + }
...
Kind regards,
Petr
PATCH 2/2 BPF: Sanity check creating and updating maps
Quoted 44 lines · Petr Vorel
Hi Richie,
Quoted 1 line
Signed-off-by Richard Palethorpe <[email protected]>Reviewed-by Petr Vorel <[email protected]>LGTM with minor comments.
...
Quoted 1 line
testcases/kernel/syscalls/bpf/bpf_map01.c +0
-0...
Quoted 34 lines
testcases/kernel/syscalls/bpf/bpf_map01.c +34
-0+#include <limits.h> +#include <string.h> + +#include "config.h" +#include "tst_test.h" +#include "lapi/bpf.h" + +#define KEY_SZ 8 +#define VAL_SZ 1024 + +struct map_type { +uint32_t id; +char *name; +}; + +static const struct map_type map_types[] = { + {BPF_MAP_TYPE_HASH ,"hash" }, + {BPF_MAP_TYPE_ARRAY ,"array" } +}; + +static void *key; +static void *val0; +static void *val1; + +static void setup (void ) +{ + key =SAFE_MALLOC (KEY_SZ ); +memset (key,0 , (size_t )KEY_SZ ); + val0 =SAFE_MALLOC (VAL_SZ ); + val1 =SAFE_MALLOC (VAL_SZ ); +memset (val1,0 , (size_t )VAL_SZ ); +} + +void run (unsigned int n)
Quoted 8 lines
testcases/kernel/syscalls/bpf/bpf_map01.c line 50
Quoted 6 lines
testcases/kernel/syscalls/bpf/bpf_map01.c +6
-0+{ +int fd, i; +union bpf_attr attr; +memset (&attr,0 ,sizeof (attr)); + attr.map_type = map_types[n].id; + attr.key_size = n ==0 ?KEY_SZ :4 ;Out of curiosity why 4? As whole test is working for
KEY_SZ>= 1 (but for second test it's needed to be 4).
The second test is for an array map type which only supports 32bit integer indexes.
Signature · 2 lines
Thank you, Richard.
cover BPF testing
In reply to Richard PalethorpeHi Richie,
Quoted 6 lines
Hello,
This patch set introduces a very basic test which kicks the tires of the bpf system call. It doesn't actually load a eBPF program, I will create another test for that. However I have some concerns which I will discuss while doing that.
Good start, great.
Quoted 5 lines
There are already extensive BPF tests in the kernel selftests. These appear to be quite complex and test a variety of functionality. They also are far less structured than LTP's modern tests and are tied to the kernel tree which makes using them in QA a pain. There are also some tests in the BCC project, which may test the kernel as a byproduct.
Yep, this is true for other tests in kselftest tree.
Quoted 6 lines
So there are a number of options which are not necessarily mutually exclusive:
1) Port (some of) the selftests to the LTP.
2) Port the LTP library to the selftests.
3) Focus the LTP's BPF tests on reproducing specific high impact bugs.
Quoted 7 lines
This patch set copies in the necessary headers so that we have zero external dependencies.
I will also use raw byte code for the program test which is at least acceptable for trivial programs. So we do not need BCC or Clang/LLVM with eBPF support or matching kernel sources to generate offsets into internal structures.
+1
Quoted 3 lines
For the time being atleast my preference would be for (3) while avoiding taking on any dependencies to ensure those tests are run by users mostly ignorant of BPF, but are still exposed to critical bugs in the BPF stack.
+1
Kind regards,
Petr
cover BPF testing
In reply to Richard PalethorpeHi!
Quoted 11 lines
This patch set introduces a very basic test which kicks the tires of the bpf system call. It doesn't actually load a eBPF program, I will create another test for that. However I have some concerns which I will discuss while doing that.
There are already extensive BPF tests in the kernel selftests. These appear to be quite complex and test a variety of functionality. They also are far less structured than LTP's modern tests and are tied to the kernel tree which makes using them in QA a pain. There are also some tests in the BCC project, which may test the kernel as a byproduct.
Quoted 5 lines
So there are a number of options which are not necessarily mutually exclusive:
1) Port (some of) the selftests to the LTP.
2) Port the LTP library to the selftests.
3) Focus the LTP's BPF tests on reproducing specific high impact bugs.
The option 3 sounds good, just FYI there are CVEs some with POCs for BPF, just by googling "ebpf CVE" you got some:
https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2017-16995
https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2019-7308
Also cloudfare blog seems to be very relevant:
https://blog.cloudflare.com/ebpf-cant-count/
And there are some test stuffed in linux/samples/bpf/ as well.
Signature · 2 lines
Cyril Hrubis [email protected]
cover BPF testing
Hello,
Quoted 21 lines · Cyril Hrubis
Hi!
Quoted 11 lines
This patch set introduces a very basic test which kicks the tires of the bpf system call. It doesn't actually load a eBPF program, I will create another test for that. However I have some concerns which I will discuss while doing that.
There are already extensive BPF tests in the kernel selftests. These appear to be quite complex and test a variety of functionality. They also are far less structured than LTP's modern tests and are tied to the kernel tree which makes using them in QA a pain. There are also some tests in the BCC project, which may test the kernel as a byproduct.
Quoted 5 lines
So there are a number of options which are not necessarily mutually exclusive:
1) Port (some of) the selftests to the LTP.
2) Port the LTP library to the selftests.
3) Focus the LTP's BPF tests on reproducing specific high impact bugs.The option 3 sounds good, just FYI there are CVEs some with POCs for BPF, just by googling "ebpf CVE" you got some:
Quoted 8 lines
https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2017-16995
https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2019-7308Also cloudfare blog seems to be very relevant:
https://blog.cloudflare.com/ebpf-cant-count/
And there are some test stuffed in linux/samples/bpf/ as well.
Ah, something I didn't realise is that various types of eBPF program can be run without CAP_SYS_ADMIN because the man page is out of date. I will send a patch for that.
--
Thank you,
Richard.
PATCH v2 1/4 BPF: Essential headers for map creation
In reply to Richard Palethorpe---
V2:
* Added bpf_prog01 test
* Removed root check and instead signal TCONF if bpf fails with EPERM
* Added more headers, including for byte code and old distro's
include/lapi/bpf.h | 242 +++++++++++++++++++++++++++++++ include/lapi/syscalls/aarch64.in | 1 + include/lapi/syscalls/i386.in | 1 + include/lapi/syscalls/s390.in | 1 + include/lapi/syscalls/sparc.in | 1 + include/lapi/syscalls/x86_64.in | 1 + 6 files changed, 247 insertions(+) create mode 100644 include/lapi/bpf.h
include/lapi/bpf.h new file +242 -0
@@ -0,0 +1,242 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* +* Copyright (c) 2019 Richard Palethorpe <[email protected]> +* +* Essential Extended Berkeley Packet Filter (eBPF) headers +* +* Mostly copied/adapted from linux/bpf.h and libbpf so that we can perform +* some eBPF testing without any external dependencies. +*/ + +#ifndef BPF_H +# define BPF_H + +#include <stdint.h> + +#include "lapi/syscalls.h" + +/* Start copy from linux/bpf.h */ +enum bpf_cmd { +BPF_MAP_CREATE , +BPF_MAP_LOOKUP_ELEM , +BPF_MAP_UPDATE_ELEM , +BPF_MAP_DELETE_ELEM , +BPF_MAP_GET_NEXT_KEY , +BPF_PROG_LOAD , +BPF_OBJ_PIN , +BPF_OBJ_GET , +BPF_PROG_ATTACH , +BPF_PROG_DETACH , +BPF_PROG_TEST_RUN , +BPF_PROG_GET_NEXT_ID , +BPF_MAP_GET_NEXT_ID , +BPF_PROG_GET_FD_BY_ID , +BPF_MAP_GET_FD_BY_ID , +BPF_OBJ_GET_INFO_BY_FD , +BPF_PROG_QUERY , +BPF_RAW_TRACEPOINT_OPEN , +BPF_BTF_LOAD , +BPF_BTF_GET_FD_BY_ID , +BPF_TASK_FD_QUERY , +BPF_MAP_LOOKUP_AND_DELETE_ELEM , +BPF_MAP_FREEZE , +}; + +enum bpf_map_type { +BPF_MAP_TYPE_UNSPEC , +BPF_MAP_TYPE_HASH , +BPF_MAP_TYPE_ARRAY , +BPF_MAP_TYPE_PROG_ARRAY , +BPF_MAP_TYPE_PERF_EVENT_ARRAY , +BPF_MAP_TYPE_PERCPU_HASH , +BPF_MAP_TYPE_PERCPU_ARRAY , +BPF_MAP_TYPE_STACK_TRACE , +BPF_MAP_TYPE_CGROUP_ARRAY , +BPF_MAP_TYPE_LRU_HASH , +BPF_MAP_TYPE_LRU_PERCPU_HASH , +BPF_MAP_TYPE_LPM_TRIE , +BPF_MAP_TYPE_ARRAY_OF_MAPS , +BPF_MAP_TYPE_HASH_OF_MAPS , +BPF_MAP_TYPE_DEVMAP , +BPF_MAP_TYPE_SOCKMAP , +BPF_MAP_TYPE_CPUMAP , +BPF_MAP_TYPE_XSKMAP , +BPF_MAP_TYPE_SOCKHASH , +BPF_MAP_TYPE_CGROUP_STORAGE , +BPF_MAP_TYPE_REUSEPORT_SOCKARRAY , +BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE , +BPF_MAP_TYPE_QUEUE , +BPF_MAP_TYPE_STACK , +BPF_MAP_TYPE_SK_STORAGE , +}; + +#define BPF_OBJ_NAME_LEN 16U + +#define BPF_ANY 0/* create new element or update existing */ +#define BPF_NOEXIST 1/* create new element if it didn't exist */ +#define BPF_EXIST 2/* update existing element */ +#define BPF_F_LOCK 4/* spin_lock-ed map_lookup/map_update */ + +#define aligned_uint64_t uint64_t __attribute__((aligned(8))) + +union bpf_attr { +struct {/* anonymous struct used by BPF_MAP_CREATE command */ +uint32_t map_type;/* one of enum bpf_map_type */ +uint32_t key_size;/* size of key in bytes */ +uint32_t value_size;/* size of value in bytes */ +uint32_t max_entries;/* max number of entries in a map */ +uint32_t map_flags;/* BPF_MAP_CREATE related +* flags defined above. +*/ +uint32_t inner_map_fd;/* fd pointing to the inner map */ +uint32_t numa_node;/* numa node (effective only if +* BPF_F_NUMA_NODE is set). +*/ +char map_name[BPF_OBJ_NAME_LEN ]; +uint32_t map_ifindex;/* ifindex of netdev to create on */ +uint32_t btf_fd;/* fd pointing to a BTF type data */ +uint32_t btf_key_type_id;/* BTF type_id of the key */ +uint32_t btf_value_type_id;/* BTF type_id of the value */ + }; + +struct {/* anonymous struct used by BPF_MAP_*_ELEM commands */ +uint32_t map_fd; +aligned_uint64_t key; +union { +aligned_uint64_t value; +aligned_uint64_t next_key; + }; +uint64_t flags; + }; + +struct {/* anonymous struct used by BPF_PROG_LOAD command */ +uint32_t prog_type;/* one of enum bpf_prog_type */ +uint32_t insn_cnt; +aligned_uint64_t insns; +aligned_uint64_t license; +uint32_t log_level;/* verbosity level of verifier */ +uint32_t log_size;/* size of user buffer */ +aligned_uint64_t log_buf;/* user supplied buffer */ +uint32_t kern_version;/* not used */ +uint32_t prog_flags; +char prog_name[BPF_OBJ_NAME_LEN ]; +uint32_t prog_ifindex;/* ifindex of netdev to prep for */ +/* For some prog types expected attach type must be known at +* load time to verify attach type specific parts of prog +* (context accesses, allowed helpers, etc). +*/ +uint32_t expected_attach_type; +uint32_t prog_btf_fd;/* fd pointing to BTF type data */ +uint32_t func_info_rec_size;/* userspace bpf_func_info size */ +aligned_uint64_t func_info;/* func info */ +uint32_t func_info_cnt;/* number of bpf_func_info records */ +uint32_t line_info_rec_size;/* userspace bpf_line_info size */ +aligned_uint64_t line_info;/* line info */ +uint32_t line_info_cnt;/* number of bpf_line_info records */ + }; + +struct {/* anonymous struct used by BPF_OBJ_* commands */ +aligned_uint64_t pathname; +uint32_t bpf_fd; +uint32_t file_flags; + }; + +struct {/* anonymous struct used by BPF_PROG_ATTACH/DETACH commands */ +uint32_t target_fd;/* container object to attach to */ +uint32_t attach_bpf_fd;/* eBPF program to attach */ +uint32_t attach_type; +uint32_t attach_flags; + }; + +struct {/* anonymous struct used by BPF_PROG_TEST_RUN command */ +uint32_t prog_fd; +uint32_t retval; +uint32_t data_size_in;/* input: len of data_in */ +uint32_t data_size_out;/* input/output: len of data_out +* returns ENOSPC if data_out +* is too small. +*/ +aligned_uint64_t data_in; +aligned_uint64_t data_out; +uint32_t repeat; +uint32_t duration; +uint32_t ctx_size_in;/* input: len of ctx_in */ +uint32_t ctx_size_out;/* input/output: len of ctx_out +* returns ENOSPC if ctx_out +* is too small. +*/ +aligned_uint64_t ctx_in; +aligned_uint64_t ctx_out; + } test; + +struct {/* anonymous struct used by BPF_*_GET_*_ID */ +union { +uint32_t start_id; +uint32_t prog_id; +uint32_t map_id; +uint32_t btf_id; + }; +uint32_t next_id; +uint32_t open_flags; + }; + +struct {/* anonymous struct used by BPF_OBJ_GET_INFO_BY_FD */ +uint32_t bpf_fd; +uint32_t info_len; +aligned_uint64_t info; + } info; + +struct {/* anonymous struct used by BPF_PROG_QUERY command */ +uint32_t target_fd;/* container object to query */ +uint32_t attach_type; +uint32_t query_flags; +uint32_t attach_flags; +aligned_uint64_t prog_ids; +uint32_t prog_cnt; + } query; + +struct { +uint64_t name; +uint32_t prog_fd; + } raw_tracepoint; + +struct {/* anonymous struct for BPF_BTF_LOAD */ +aligned_uint64_t btf; +aligned_uint64_t btf_log_buf; +uint32_t btf_size; +uint32_t btf_log_size; +uint32_t btf_log_level; + }; + +struct { +uint32_t pid;/* input: pid */ +uint32_t fd;/* input: fd */ +uint32_t flags;/* input: flags */ +uint32_t buf_len;/* input/output: buf len */ +aligned_uint64_t buf;/* input/output: +* tp_name for tracepoint +* symbol for kprobe +* filename for uprobe +*/ +uint32_t prog_id;/* output: prod_id */ +uint32_t fd_type;/* output: BPF_FD_TYPE_* */ +uint64_t probe_offset;/* output: probe_offset */ +uint64_t probe_addr;/* output: probe_addr */ + } task_fd_query; +} __attribute__((aligned (8 ))); + +/* End copy from linux/bpf.h */ + +/* Start copy from tools/lib/bpf */ +inline uint64_t ptr_to_u64 (const void *ptr) +{ +return (uint64_t ) (unsigned long ) ptr; +} + +inline int bpf (enum bpf_cmd cmd,union bpf_attr *attr,unsigned int size) +{ +return tst_syscall (__NR_bpf, cmd, attr, size); +} +/* End copy from tools/lib/bpf */ + +#endif /* BPF_H */
include/lapi/syscalls/aarch64.in +1 -0
@@ -258,6 +258,7 @@ process_vm_writev 271 kcmp 272 getrandom 278 memfd_create 279 +bpf 280 userfaultfd 282 membarrier 283 execveat 281
include/lapi/syscalls/i386.in +1 -0
@@ -340,6 +340,7 @@ sched_getattr 352 renameat2 354 getrandom 355 memfd_create 356 +bpf 357 execveat 358 userfaultfd 374 membarrier 375
include/lapi/syscalls/s390.in +1 -0
@@ -331,6 +331,7 @@ sched_getattr 346 renameat2 347 getrandom 349 memfd_create 350 +bpf 351 userfaultfd 355 membarrier 356 execveat 354
include/lapi/syscalls/sparc.in +1 -0
@@ -336,6 +336,7 @@ kcmp 341 renameat2 345 getrandom 347 memfd_create 348 +bpf 349 membarrier 351 userfaultfd 352 execveat 350
include/lapi/syscalls/x86_64.in +1 -0
@@ -307,6 +307,7 @@ sched_getattr 315 renameat2 316 getrandom 318 memfd_create 319 +bpf 321 execveat 322 userfaultfd 323 membarrier 324
Signature · 1 line
2.22.0
PATCH v2 2/4 BPF: Sanity check creating and updating maps
--- runtest/syscalls | 2 + testcases/kernel/syscalls/bpf/.gitignore | 1 + testcases/kernel/syscalls/bpf/Makefile | 10 ++ testcases/kernel/syscalls/bpf/bpf_map01.c | 143 ++++++++++++++++++++++ 4 files changed, 156 insertions(+) create mode 100644 testcases/kernel/syscalls/bpf/.gitignore create mode 100644 testcases/kernel/syscalls/bpf/Makefile create mode 100644 testcases/kernel/syscalls/bpf/bpf_map01.c
runtest/syscalls +2 -0
@@ -32,6 +32,8 @@ bind01 bind01 bind02 bind02 bind03 bind03 +bpf_map01 bpf_map01 + brk01 brk01 capget01 capget01
testcases/kernel/syscalls/bpf/.gitignore new file +1 -0
@@ -0,0 +1 @@ +bpf_map01
testcases/kernel/syscalls/bpf/Makefile new file +10 -0
@@ -0,0 +1,10 @@ +# SPDX-License-Identifier: GPL-2.0-or-later +# Copyright (c) 2019 Linux Test Project + +top_srcdir ?= ../../../.. + +include $(top_srcdir)/include/mk/testcases.mk + +CFLAGS += -D_GNU_SOURCE + +include $(top_srcdir)/include/mk/generic_leaf_target.mk
testcases/kernel/syscalls/bpf/bpf_map01.c new file +143 -0
143 lines not on this page · raw message
Signature · 1 line
2.22.0
PATCH v2 3/4 BPF: Essential headers for a basic program
In reply to Richard Palethorpe--- include/lapi/bpf.h | 286 +++++++++++++++++++++++++++++++++++++++++- include/lapi/socket.h | 4 + 2 files changed, 289 insertions(+), 1 deletion(-)
include/lapi/bpf.h +285 -1
@@ -15,7 +15,57 @@#include "lapi/syscalls.h" -/* Start copy from linux/bpf.h */ +/* Start copy from linux/bpf_(common).h */ +#define BPF_CLASS (code) ((code) & 0x07) +#define BPF_LD 0x00 +#define BPF_ST 0x02 +#define BPF_JMP 0x05 + +#define BPF_SIZE (code) ((code) & 0x18) +#define BPF_DW 0x18/* double word (64-bit) */ + +#define BPF_MODE (code) ((code) & 0xe0) +#define BPF_IMM 0x00 +#define BPF_MEM 0x60 + +#define BPF_OP (code) ((code) & 0xf0) +#define BPF_ADD 0x00 + +#define BPF_JEQ 0x10 + +#define BPF_SRC (code) ((code) & 0x08) +#define BPF_K 0x00 +#define BPF_X 0x08 + +#define BPF_ALU64 0x07/* alu mode in double word width */ +#define BPF_MOV 0xb0/* mov reg to reg */ +#define BPF_CALL 0x80/* function call */ +#define BPF_EXIT 0x90/* function return */ + +/* Register numbers */ +enum { +BPF_REG_0 =0 , +BPF_REG_1 , +BPF_REG_2 , +BPF_REG_3 , +BPF_REG_4 , +BPF_REG_5 , +BPF_REG_6 , +BPF_REG_7 , +BPF_REG_8 , +BPF_REG_9 , +BPF_REG_10 , +MAX_BPF_REG , +}; + +struct bpf_insn { +uint8_t code;/* opcode */ +uint8_t dst_reg:4 ;/* dest register */ +uint8_t src_reg:4 ;/* source register */ +int16_t off;/* signed offset */ +int32_t imm;/* signed immediate constant */ +}; +enum bpf_cmd {BPF_MAP_CREATE ,BPF_MAP_LOOKUP_ELEM ,
@@ -70,6 +120,37 @@ enum bpf_map_type { BPF_MAP_TYPE_SK_STORAGE, }; +enum bpf_prog_type { +BPF_PROG_TYPE_UNSPEC , +BPF_PROG_TYPE_SOCKET_FILTER , +BPF_PROG_TYPE_KPROBE , +BPF_PROG_TYPE_SCHED_CLS , +BPF_PROG_TYPE_SCHED_ACT , +BPF_PROG_TYPE_TRACEPOINT , +BPF_PROG_TYPE_XDP , +BPF_PROG_TYPE_PERF_EVENT , +BPF_PROG_TYPE_CGROUP_SKB , +BPF_PROG_TYPE_CGROUP_SOCK , +BPF_PROG_TYPE_LWT_IN , +BPF_PROG_TYPE_LWT_OUT , +BPF_PROG_TYPE_LWT_XMIT , +BPF_PROG_TYPE_SOCK_OPS , +BPF_PROG_TYPE_SK_SKB , +BPF_PROG_TYPE_CGROUP_DEVICE , +BPF_PROG_TYPE_SK_MSG , +BPF_PROG_TYPE_RAW_TRACEPOINT , +BPF_PROG_TYPE_CGROUP_SOCK_ADDR , +BPF_PROG_TYPE_LWT_SEG6LOCAL , +BPF_PROG_TYPE_LIRC_MODE2 , +BPF_PROG_TYPE_SK_REUSEPORT , +BPF_PROG_TYPE_FLOW_DISSECTOR , +BPF_PROG_TYPE_CGROUP_SYSCTL , +BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE , +BPF_PROG_TYPE_CGROUP_SOCKOPT , +}; + +#define BPF_PSEUDO_MAP_FD 1 +#define BPF_OBJ_NAME_LEN 16U#define BPF_ANY 0/* create new element or update existing */
@@ -225,8 +306,211 @@ union bpf_attr { } task_fd_query; } __attribute__((aligned(8))); +#define __BPF_FUNC_MAPPER (FN ) \ + FN(unspec), \ + FN(map_lookup_elem), \ + FN(map_update_elem), \ + FN(map_delete_elem), \ + FN(probe_read), \ + FN(ktime_get_ns), \ + FN(trace_printk), \ + FN(get_prandom_u32), \ + FN(get_smp_processor_id), \ + FN(skb_store_bytes), \ + FN(l3_csum_replace), \ + FN(l4_csum_replace), \ + FN(tail_call), \ + FN(clone_redirect), \ + FN(get_current_pid_tgid), \ + FN(get_current_uid_gid), \ + FN(get_current_comm), \ + FN(get_cgroup_classid), \ + FN(skb_vlan_push), \ + FN(skb_vlan_pop), \ + FN(skb_get_tunnel_key), \ + FN(skb_set_tunnel_key), \ + FN(perf_event_read), \ + FN(redirect), \ + FN(get_route_realm), \ + FN(perf_event_output), \ + FN(skb_load_bytes), \ + FN(get_stackid), \ + FN(csum_diff), \ + FN(skb_get_tunnel_opt), \ + FN(skb_set_tunnel_opt), \ + FN(skb_change_proto), \ + FN(skb_change_type), \ + FN(skb_under_cgroup), \ + FN(get_hash_recalc), \ + FN(get_current_task), \ + FN(probe_write_user), \ + FN(current_task_under_cgroup), \ + FN(skb_change_tail), \ + FN(skb_pull_data), \ + FN(csum_update), \ + FN(set_hash_invalid), \ + FN(get_numa_node_id), \ + FN(skb_change_head), \ + FN(xdp_adjust_head), \ + FN(probe_read_str), \ + FN(get_socket_cookie), \ + FN(get_socket_uid), \ + FN(set_hash), \ + FN(setsockopt), \ + FN(skb_adjust_room), \ + FN(redirect_map), \ + FN(sk_redirect_map), \ + FN(sock_map_update), \ + FN(xdp_adjust_meta), \ + FN(perf_event_read_value), \ + FN(perf_prog_read_value), \ + FN(getsockopt), \ + FN(override_return), \ + FN(sock_ops_cb_flags_set), \ + FN(msg_redirect_map), \ + FN(msg_apply_bytes), \ + FN(msg_cork_bytes), \ + FN(msg_pull_data), \ + FN(bind), \ + FN(xdp_adjust_tail), \ + FN(skb_get_xfrm_state), \ + FN(get_stack), \ + FN(skb_load_bytes_relative), \ + FN(fib_lookup), \ + FN(sock_hash_update), \ + FN(msg_redirect_hash), \ + FN(sk_redirect_hash), \ + FN(lwt_push_encap), \ + FN(lwt_seg6_store_bytes), \ + FN(lwt_seg6_adjust_srh), \ + FN(lwt_seg6_action), \ + FN(rc_repeat), \ + FN(rc_keydown), \ + FN(skb_cgroup_id), \ + FN(get_current_cgroup_id), \ + FN(get_local_storage), \ + FN(sk_select_reuseport), \ + FN(skb_ancestor_cgroup_id), \ + FN(sk_lookup_tcp), \ + FN(sk_lookup_udp), \ + FN(sk_release), \ + FN(map_push_elem), \ + FN(map_pop_elem), \ + FN(map_peek_elem), \ + FN(msg_push_data), \ + FN(msg_pop_data), \ + FN(rc_pointer_rel), \ + FN(spin_lock), \ + FN(spin_unlock), \ + FN(sk_fullsock), \ + FN(tcp_sock), \ + FN(skb_ecn_set_ce), \ + FN(get_listener_sock), \ + FN(skc_lookup_tcp), \ + FN(tcp_check_syncookie), \ + FN(sysctl_get_name), \ + FN(sysctl_get_current_value), \ + FN(sysctl_get_new_value), \ + FN(sysctl_set_new_value), \ + FN(strtol), \ + FN(strtoul), \ + FN(sk_storage_get), \ + FN(sk_storage_delete), \ + FN(send_signal), + +/* integer value in 'imm' field of BPF_CALL instruction selects which helper +* function eBPF program intends to call +*/ +#define __BPF_ENUM_FN (x) BPF_FUNC_ ## x +enum bpf_func_id { + __BPF_FUNC_MAPPER(__BPF_ENUM_FN) + __BPF_FUNC_MAX_ID, +}; +#undef __BPF_ENUM_FN +/* End copy from linux/bpf.h */ +/* Start copy from tools/include/filter.h */ + +#define BPF_ALU64_IMM (OP ,DST ,IMM ) \ + ((struct bpf_insn) { \ + .code = BPF_ALU64 | BPF_OP(OP) | BPF_K, \ + .dst_reg = DST, \ + .src_reg = 0, \ + .off = 0, \ + .imm = IMM }) + +#define BPF_MOV64_REG (DST ,SRC ) \ + ((struct bpf_insn) { \ + .code = BPF_ALU64 | BPF_MOV | BPF_X, \ + .dst_reg = DST, \ + .src_reg = SRC, \ + .off = 0, \ + .imm = 0 }) + +#define BPF_LD_IMM64 (DST ,IMM ) \ + BPF_LD_IMM64_RAW(DST, 0, IMM) + +#define BPF_LD_IMM64_RAW(DST, SRC, IMM) \ + ((struct bpf_insn) { \ + .code = BPF_LD | BPF_DW | BPF_IMM, \ + .dst_reg = DST, \ + .src_reg = SRC, \ + .off = 0, \ + .imm = (uint32_t) (IMM) }), \ + ((struct bpf_insn) { \ + .code = 0, /* zero is reserved opcode */ \ + .dst_reg = 0, \ + .src_reg = 0, \ + .off = 0, \ + .imm = ((uint64_t) (IMM)) >> 32 }) + +/* pseudo BPF_LD_IMM64 insn used to refer to process-local map_fd */ +#define BPF_LD_MAP_FD (DST ,MAP_FD ) \ + BPF_LD_IMM64_RAW(DST, BPF_PSEUDO_MAP_FD, MAP_FD) + +#define BPF_ST_MEM (SIZE ,DST ,OFF ,IMM ) \ + ((struct bpf_insn) { \ + .code = BPF_ST | BPF_SIZE(SIZE) | BPF_MEM, \ + .dst_reg = DST, \ + .src_reg = 0, \ + .off = OFF, \ + .imm = IMM }) + +#define BPF_JMP_IMM (OP ,DST ,IMM ,OFF ) \ + ((struct bpf_insn) { \ + .code = BPF_JMP | BPF_OP(OP) | BPF_K, \ + .dst_reg = DST, \ + .src_reg = 0, \ + .off = OFF, \ + .imm = IMM }) + +#define BPF_MOV64_IMM (DST ,IMM ) \ + ((struct bpf_insn) { \ + .code = BPF_ALU64 | BPF_MOV | BPF_K, \ + .dst_reg = DST, \ + .src_reg = 0, \ + .off = 0, \ + .imm = IMM }) + +#define BPF_EMIT_CALL (FUNC ) \ + ((struct bpf_insn) { \ + .code = BPF_JMP | BPF_CALL, \ + .dst_reg = 0, \ + .src_reg = 0, \ + .off = 0, \ + .imm = ((FUNC) - BPF_FUNC_unspec) }) + +#define BPF_EXIT_INSN () \ + ((struct bpf_insn) { \ + .code = BPF_JMP | BPF_EXIT, \ + .dst_reg = 0, \ + .src_reg = 0, \ + .off = 0, \ + .imm = 0 }) + +/* End copy from tools/include/filter.h */ +/* Start copy from tools/lib/bpf */ inline uint64_t ptr_to_u64 (const void *ptr) {
include/lapi/socket.h +4 -0
@@ -37,6 +37,10 @@# define SO_BUSY_POLL 46#endif +#ifndef SO_ATTACH_BPF +# define SO_ATTACH_BPF 50 +#endif +#ifndef SO_ZEROCOPY # define SO_ZEROCOPY 60#endif
Signature · 1 line
2.22.0
PATCH v2 4/4 BPF: Sanity check creating a program
In reply to Richard Palethorpe--- runtest/syscalls | 1 + testcases/kernel/syscalls/bpf/.gitignore | 1 + testcases/kernel/syscalls/bpf/bpf_prog01.c | 138 +++++++++++++++++++++ 3 files changed, 140 insertions(+) create mode 100644 testcases/kernel/syscalls/bpf/bpf_prog01.c
runtest/syscalls +1 -0
@@ -33,6 +33,7 @@ bind02 bind02 bind03 bind03 bpf_map01 bpf_map01 +bpf_prog01 bpf_prog01 brk01 brk01
testcases/kernel/syscalls/bpf/.gitignore +1 -0
@@ -1 +1,2 @@ bpf_map01 +bpf_prog01
testcases/kernel/syscalls/bpf/bpf_prog01.c new file +138 -0
138 lines not on this page · raw message
Signature · 1 line
2.22.0