bpf: Extend bpf_{prog,map}_info
Subsystems (7)
cover bpf: Extend bpf_{prog,map}_info
This patch series adds more fields to bpf_prog_info and bpf_map_info. Please see individual patch for details.
Martin KaFai Lau (5):
bpf: Add name, load_time, uid and map_ids to bpf_prog_info
bpf: Add map_name to bpf_map_info
bpf: libbpf: Provide basic API support to specify BPF obj name
bpf: Swap the order of checking prog_info and map_info
bpf: Test new fields in bpf_attr and bpf_{prog,map}_info
include/linux/bpf.h | 3 + include/uapi/linux/bpf.h | 10 ++ kernel/bpf/syscall.c | 58 ++++++++- samples/bpf/bpf_load.c | 2 + samples/bpf/map_perf_test_user.c | 1 + tools/include/uapi/linux/bpf.h | 10 ++ tools/lib/bpf/bpf.c | 57 ++++++-- tools/lib/bpf/bpf.h | 23 +++- tools/lib/bpf/libbpf.c | 109 ++++++++++++---- tools/testing/selftests/bpf/test_progs.c | 195 +++++++++++++++++++++++----- tools/testing/selftests/bpf/test_verifier.c | 2 +- 11 files changed, 385 insertions(+), 85 deletions(-)
Signature · 1 line
2.9.5
PATCH 1/5 bpf: Add name, load_time, uid and map_ids to bpf_prog_info
The patch adds name and load_time to struct bpf_prog_aux. They are also exported to bpf_prog_info.
The bpf_prog's name is passed by userspace during BPF_PROG_LOAD. The kernel only stores the first (BPF_PROG_NAME_LEN - 1) bytes and the name stored in the kernel is always \0 terminated.
The kernel will reject name that contains characters other than isalnum() and '_'. It will also reject name that is not null terminated.
The existing 'user->uid' of the bpf_prog_aux is also exported to the bpf_prog_info as created_by_uid.
The existing 'used_maps' of the bpf_prog_aux is exported to the newly added members 'nr_map_ids' and 'map_ids' of the bpf_prog_info. On the input, nr_map_ids tells how big the userspace's map_ids buffer is. On the output, nr_map_ids tells the exact user_map_cnt and it will only copy up to the userspace's map_ids buffer is allowed.
--- include/linux/bpf.h | 2 ++ include/uapi/linux/bpf.h | 8 ++++++++ kernel/bpf/syscall.c | 51 +++++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 60 insertions(+), 1 deletion(-)
include/linux/bpf.h +2 -0
@@ -187,6 +187,8 @@ struct bpf_prog_aux {struct bpf_map **used_maps;struct bpf_prog *prog;struct user_struct *user; +u64 load_time;/* ns since boottime */ +u8 name[BPF_OBJ_NAME_LEN ];union {struct work_struct work;struct rcu_head rcu;
include/uapi/linux/bpf.h +8 -0
@@ -175,6 +175,8 @@ enum bpf_attach_type {/* Specify numa node during map creation */ #define BPF_F_NUMA_NODE (1U << 2) +#define BPF_OBJ_NAME_LEN 16U +union bpf_attr {struct {/* anonymous struct used by BPF_MAP_CREATE command */ __u32 map_type;/* one of enum bpf_map_type */
@@ -210,6 +212,7 @@ union bpf_attr {__aligned_u64 log_buf;/* user supplied buffer */ __u32 kern_version;/* checked when prog_type=kprobe */ __u32 prog_flags; +__u8 prog_name[BPF_OBJ_NAME_LEN ]; };struct {/* anonymous struct used by BPF_OBJ_* commands */
@@ -812,6 +815,11 @@ struct bpf_prog_info {__u32 xlated_prog_len;__aligned_u64 jited_prog_insns;__aligned_u64 xlated_prog_insns; +__u64 load_time;/* ns since boottime */ +__u32 created_by_uid; +__u32 nr_map_ids; +__aligned_u64 map_ids; +__u8 name[BPF_OBJ_NAME_LEN ]; } __attribute__((aligned(8)));struct bpf_map_info {
kernel/bpf/syscall.c +50 -1
83 lines not on this page · raw message
Signature · 1 line
2.9.5
PATCH 2/5 bpf: Add map_name to bpf_map_info
In reply to Martin KaFai LauThis patch allows userspace to specify a name for a map during BPF_MAP_CREATE.
The map's name can later be exported to user space via BPF_OBJ_GET_INFO_BY_FD.
--- include/linux/bpf.h | 1 + include/uapi/linux/bpf.h | 2 ++ kernel/bpf/syscall.c | 7 ++++++- 3 files changed, 9 insertions(+), 1 deletion(-)
include/linux/bpf.h +1 -0
@@ -56,6 +56,7 @@ struct bpf_map {struct work_struct work;atomic_t usercnt;struct bpf_map *inner_map_meta; +u8 name[BPF_OBJ_NAME_LEN ]; };/* function argument constraints */
include/uapi/linux/bpf.h +2 -0
@@ -190,6 +190,7 @@ union bpf_attr {__u32 numa_node;/* numa node (effective only if * BPF_F_NUMA_NODE is set). */ +__u8 map_name[BPF_OBJ_NAME_LEN ]; };struct {/* anonymous struct used by BPF_MAP_*_ELEM commands */
@@ -829,6 +830,7 @@ struct bpf_map_info {__u32 value_size;__u32 max_entries;__u32 map_flags; +__u8 name[BPF_OBJ_NAME_LEN ]; } __attribute__((aligned(8)));/* User bpf_sock_ops struct to access socket values and specify request ops
kernel/bpf/syscall.c +6 -1
@@ -339,7 +339,7 @@ static int bpf_obj_name_cpy(char *dst, const char *src)return 0 ; } -#define BPF_MAP_CREATE_LAST_FIELD numa_node +#define BPF_MAP_CREATE_LAST_FIELD map_name/* called via syscall */ static int map_create (union bpf_attr *attr) {
@@ -361,6 +361,10 @@ static int map_create(union bpf_attr *attr)if (IS_ERR (map))return PTR_ERR (map); + err =bpf_obj_name_cpy (map->name, attr->map_name); +if (err) + goto free_map_nouncharge; +atomic_set (&map->refcnt,1 );atomic_set (&map->usercnt,1 );
@@ -1462,6 +1466,7 @@ static int bpf_map_get_info_by_fd(struct bpf_map *map, info.value_size = map->value_size; info.max_entries = map->max_entries; info.map_flags = map->map_flags; +memcpy (info.name, map->name,sizeof (map->name));if (copy_to_user (uinfo, &info, info_len) ||put_user (info_len, &uattr->info.info_len))
Signature · 1 line
2.9.5
Re: [PATCH net-next 2/5] bpf: Add map_name to bpf_map_info
Hi Martin!
include/linux/bpf.h struct bpf_map { · line 59
Quoted 9 lines · Martin KaFai Lau
include/linux/bpf.h +1
-0@@ -56,6 +56,7 @@ struct bpf_map {struct work_struct work;atomic_t usercnt;struct bpf_map *inner_map_meta; +u8 name[BPF_OBJ_NAME_LEN ];
Any reason not to use plain char? I was looking at adding names to bpftool and:
map.c: In function ‘show_map_close’:
map.c:386:13: warning: pointer targets in passing argument 1 of ‘strlen’ differ in signedness [-Wpointer-sign]
if (strlen(info->name))
^~~~
In file included from map.c:43:0:
/usr/include/string.h:399:15: note: expected ‘const char *’ but argument is of type ‘__u8 * {aka unsigned char *}’
extern size_t strlen (const char *__s)
^~~~~~Quoted 3 lines
include/linux/bpf.h +0
-0};/* function argument constraints */
Re: [PATCH net-next 2/5] bpf: Add map_name to bpf_map_info
Quoted 15 lines · Jakub Kicinski
Hi Martin!
include/linux/bpf.h struct bpf_map { · line 59
Quoted 9 lines · Martin KaFai Lau
include/linux/bpf.h +1
-0@@ -56,6 +56,7 @@ struct bpf_map {struct work_struct work;atomic_t usercnt;struct bpf_map *inner_map_meta; +u8 name[BPF_OBJ_NAME_LEN ];Any reason not to use plain char? I was looking at adding names to bpftool and:
Happy to have early user :)
It was mostly due to my early idea on treating the name as a blob without much char checking, which was then trashed but did not reflect that here.
I will make a followup patch later.
Quoted 13 lines
map.c: In function ‘show_map_close’:
map.c:386:13: warning: pointer targets in passing argument 1 of ‘strlen’ differ in signedness [-Wpointer-sign]
if (strlen(info->name))^~~~ In file included from map.c:43:0: /usr/include/string.h:399:15: note: expected ‘const char *’ but argument is of type ‘__u8 * {aka unsigned char *}’ extern size_t strlen (const char *__s) ^~~~~~Quoted 3 lines
include/linux/bpf.h +0
-0};/* function argument constraints */
Re: [PATCH net-next 2/5] bpf: Add map_name to bpf_map_info
Quoted 16 lines · Martin KaFai Lau
Quoted 15 lines · Jakub Kicinski
Hi Martin!
include/linux/bpf.h struct bpf_map { · line 59
Quoted 9 lines · Martin KaFai Lau
include/linux/bpf.h +1
-0@@ -56,6 +56,7 @@ struct bpf_map {struct work_struct work;atomic_t usercnt;struct bpf_map *inner_map_meta; +u8 name[BPF_OBJ_NAME_LEN ];Any reason not to use plain char? I was looking at adding names to bpftool and:
Quoted 1 line
Happy to have early user :)
The newly exposed info is so useful I couldn't resist :)
Quoted 5 lines
It was mostly due to my early idea on treating the name as a blob without much char checking, which was then trashed but did not reflect that here.
I will make a followup patch later.
Awesome, thanks!
PATCH 3/5 bpf: libbpf: Provide basic API support to specify BPF obj name
In reply to Martin KaFai LauThis patch extends the libbpf to provide API support to allow specifying BPF object name.
In tools/lib/bpf/libbpf, the C symbol of the function and the map is used. Regarding section name, all maps are under the same section named "maps". Hence, section name is not a good choice for map's name. To be consistent with map, bpf_prog also follows and uses its function symbol as the prog's name.
This patch adds logic to collect function's symbols in libbpf. There is existing codes to collect the map's symbols and no change is needed.
The bpf_load_program_name() and bpf_map_create_name() are added to take the name argument. For the other bpf_map_create_xxx() variants, a name argument is directly added to them.
In samples/bpf, bpf_load.c in particular, the symbol is also used as the map's name and the map symbols has already been collected in the existing code. For bpf_prog, bpf_load.c does not collect the function symbol name. We can consider to collect them later if there is a need to continue supporting the bpf_load.c.
--- samples/bpf/bpf_load.c | 2 + samples/bpf/map_perf_test_user.c | 1 + tools/include/uapi/linux/bpf.h | 10 +++ tools/lib/bpf/bpf.c | 57 +++++++++++---- tools/lib/bpf/bpf.h | 23 ++++-- tools/lib/bpf/libbpf.c | 109 +++++++++++++++++++++------- tools/testing/selftests/bpf/test_verifier.c | 2 +- 7 files changed, 157 insertions(+), 47 deletions(-)
samples/bpf/bpf_load.c +2 -0
@@ -221,6 +221,7 @@ static int load_maps(struct bpf_map_data *maps, int nr_maps,int inner_map_fd = map_fd[maps[i].def.inner_map_idx]; map_fd[i] = bpf_create_map_in_map_node(maps[i].def.type, + maps[i].name, maps[i].def.key_size, inner_map_fd, maps[i].def.max_entries,
@@ -228,6 +229,7 @@ static int load_maps(struct bpf_map_data *maps, int nr_maps, numa_node); }else { map_fd[i] = bpf_create_map_node(maps[i].def.type, + maps[i].name, maps[i].def.key_size, maps[i].def.value_size, maps[i].def.max_entries,
samples/bpf/map_perf_test_user.c +1 -0
@@ -137,6 +137,7 @@ static void do_test_lru(enum test_type test, int cpu) inner_lru_map_fds[cpu] = bpf_create_map_node(BPF_MAP_TYPE_LRU_HASH, + test_map_names[INNER_LRU_HASH_PREALLOC], sizeof(uint32_t), sizeof(long), inner_lru_hash_size, 0,
tools/include/uapi/linux/bpf.h +10 -0
@@ -175,6 +175,8 @@ enum bpf_attach_type {/* Specify numa node during map creation */ #define BPF_F_NUMA_NODE (1U << 2) +#define BPF_OBJ_NAME_LEN 16U +union bpf_attr {struct {/* anonymous struct used by BPF_MAP_CREATE command */ __u32 map_type;/* one of enum bpf_map_type */
@@ -188,6 +190,7 @@ union bpf_attr {__u32 numa_node;/* numa node (effective only if * BPF_F_NUMA_NODE is set). */ +__u8 map_name[BPF_OBJ_NAME_LEN ]; };struct {/* anonymous struct used by BPF_MAP_*_ELEM commands */
@@ -210,6 +213,7 @@ union bpf_attr {__aligned_u64 log_buf;/* user supplied buffer */ __u32 kern_version;/* checked when prog_type=kprobe */ __u32 prog_flags; +__u8 prog_name[BPF_OBJ_NAME_LEN ]; };struct {/* anonymous struct used by BPF_OBJ_* commands */
@@ -812,6 +816,11 @@ struct bpf_prog_info {__u32 xlated_prog_len;__aligned_u64 jited_prog_insns;__aligned_u64 xlated_prog_insns; +__u64 load_time;/* ns since boottime */ +__u32 created_by_uid; +__u32 nr_map_ids; +__aligned_u64 map_ids; +__u8 name[BPF_OBJ_NAME_LEN ]; } __attribute__((aligned(8)));struct bpf_map_info {
@@ -821,6 +830,7 @@ struct bpf_map_info {__u32 value_size;__u32 max_entries;__u32 map_flags; +__u8 name[BPF_OBJ_NAME_LEN ]; } __attribute__((aligned(8)));/* User bpf_sock_ops struct to access socket values and specify request ops
tools/lib/bpf/bpf.c +44 -13
117 lines not on this page · raw message
tools/lib/bpf/bpf.h +16 -7
@@ -24,19 +24,28 @@#include <linux/bpf.h> #include <stddef.h> -int bpf_create_map_node (enum bpf_map_type map_type,int key_size, -int value_size,int max_entries,__u32 map_flags, -int node); +int bpf_create_map_node (enum bpf_map_type map_type,const char *name, +int key_size,int value_size,int max_entries, +__u32 map_flags,int node); +int bpf_create_map_name (enum bpf_map_type map_type,const char *name, +int key_size,int value_size,int max_entries, +__u32 map_flags);int bpf_create_map (enum bpf_map_type map_type,int key_size,int value_size,int max_entries,__u32 map_flags); -int bpf_create_map_in_map_node (enum bpf_map_type map_type,int key_size, -int inner_map_fd,int max_entries, +int bpf_create_map_in_map_node (enum bpf_map_type map_type,const char *name, +int key_size,int inner_map_fd,int max_entries,__u32 map_flags,int node); -int bpf_create_map_in_map (enum bpf_map_type map_type,int key_size, -int inner_map_fd,int max_entries,__u32 map_flags); +int bpf_create_map_in_map (enum bpf_map_type map_type,const char *name, +int key_size,int inner_map_fd,int max_entries, +__u32 map_flags);/* Recommend log buffer size */ #define BPF_LOG_BUF_SIZE 65536 +int bpf_load_program_name (enum bpf_prog_type type,const char *name, +const struct bpf_insn *insns, +size_t insns_cnt,const char *license, +__u32 kern_version,char *log_buf, +size_t log_buf_sz); int bpf_load_program(enum bpf_prog_type type, const struct bpf_insn *insns, size_t insns_cnt, const char *license, __u32 kern_version, char *log_buf,
tools/lib/bpf/libbpf.c +83 -26
205 lines not on this page · raw message
tools/testing/selftests/bpf/test_verifier.c +1 -1
@@ -6939,7 +6939,7 @@ static int create_map_in_map(void)return inner_map_fd; } - outer_map_fd =bpf_create_map_in_map (BPF_MAP_TYPE_ARRAY_OF_MAPS , + outer_map_fd =bpf_create_map_in_map (BPF_MAP_TYPE_ARRAY_OF_MAPS ,NULL ,sizeof (int ), inner_map_fd,1 ,0 );if (outer_map_fd <0 ) printf("Failed to create array of maps '%s'!\n",
Signature · 1 line
2.9.5
PATCH 4/5 bpf: Swap the order of checking prog_info and map_info
In reply to Martin KaFai LauThis patch swaps the checking order. It now checks the map_info first and then prog_info. It is a prep work for adding test to the newly added fields (the map_ids of prog_info field in particular).
--- tools/testing/selftests/bpf/test_progs.c | 58 +++++++++++++++++--------------- 1 file changed, 30 insertions(+), 28 deletions(-)
tools/testing/selftests/bpf/test_progs.c +30 -28
@@ -316,6 +316,36 @@ static void test_bpf_obj_id(void) error_cnt++;assert (!err); +/* Insert a magic value to the map */ + map_fds[i] =bpf_find_map (__func__, objs[i],"test_map_id" ); +assert (map_fds[i] >=0 ); + err =bpf_map_update_elem (map_fds[i], &array_key, + &array_magic_value,0 ); +assert (!err); + +/* Check getting map info */ + info_len =sizeof (struct bpf_map_info ) *2 ; +bzero (&map_infos[i], info_len); + err =bpf_obj_get_info_by_fd (map_fds[i], &map_infos[i], + &info_len); +if (CHECK (err || + map_infos[i].type !=BPF_MAP_TYPE_ARRAY || + map_infos[i].key_size !=sizeof (__u32) || + map_infos[i].value_size !=sizeof (__u64) || + map_infos[i].max_entries !=1 || + map_infos[i].map_flags !=0 || + info_len !=sizeof (struct bpf_map_info ), +"get-map-info(fd)" , +"err %d errno %d type %d(%d) info_len %u(%lu) key_size %u value_size %u max_entries %u map_flags %X\n" , + err, errno, + map_infos[i].type,BPF_MAP_TYPE_ARRAY , + info_len,sizeof (struct bpf_map_info ), + map_infos[i].key_size, + map_infos[i].value_size, + map_infos[i].max_entries, + map_infos[i].map_flags)) + goto done; +/* Check getting prog info */ info_len =sizeof (struct bpf_prog_info ) *2 ;bzero (&prog_infos[i], info_len);
@@ -347,34 +377,6 @@ static void test_bpf_obj_id(void) !!memcmp(xlated_insns, zeros, sizeof(zeros)))) goto done; - map_fds[i] =bpf_find_map (__func__, objs[i],"test_map_id" ); -assert (map_fds[i] >=0 ); - err =bpf_map_update_elem (map_fds[i], &array_key, - &array_magic_value,0 ); -assert (!err); - -/* Check getting map info */ - info_len =sizeof (struct bpf_map_info ) *2 ; -bzero (&map_infos[i], info_len); - err =bpf_obj_get_info_by_fd (map_fds[i], &map_infos[i], - &info_len); -if (CHECK (err || - map_infos[i].type !=BPF_MAP_TYPE_ARRAY || - map_infos[i].key_size !=sizeof (__u32) || - map_infos[i].value_size !=sizeof (__u64) || - map_infos[i].max_entries !=1 || - map_infos[i].map_flags !=0 || - info_len !=sizeof (struct bpf_map_info ), -"get-map-info(fd)" , -"err %d errno %d type %d(%d) info_len %u(%lu) key_size %u value_size %u max_entries %u map_flags %X\n" , - err, errno, - map_infos[i].type,BPF_MAP_TYPE_ARRAY , - info_len,sizeof (struct bpf_map_info ), - map_infos[i].key_size, - map_infos[i].value_size, - map_infos[i].max_entries, - map_infos[i].map_flags)) - goto done; }/* Check bpf_prog_get_next_id() */
Signature · 1 line
2.9.5
PATCH 5/5 bpf: Test new fields in bpf_attr and bpf_{prog,map}_info
In reply to Martin KaFai LauThis patch tests newly added fields of the bpf_attr, bpf_prog_info and bpf_map_info.
--- tools/testing/selftests/bpf/test_progs.c | 143 ++++++++++++++++++++++++++++--- 1 file changed, 132 insertions(+), 11 deletions(-)
tools/testing/selftests/bpf/test_progs.c +132 -11
@@ -10,6 +10,7 @@#include <string.h> #include <assert.h> #include <stdlib.h> +#include <time.h> #include <linux/types.h> typedef __u16 __sum16 ;
@@ -19,6 +20,8 @@ typedef __u16 __sum16;#include <linux/ip.h> #include <linux/ipv6.h> #include <linux/tcp.h> +#include <linux/filter.h> +#include <linux/unistd.h> #include <sys/wait.h> #include <sys/resource.h>
@@ -273,16 +276,26 @@ static void test_bpf_obj_id(void)const int nr_iters =2 ;const char *file ="./test_obj_id.o" ;const char *jit_sysctl ="/proc/sys/net/core/bpf_jit_enable" ; +const char *expected_prog_name ="test_obj_id" ; +const char *expected_map_name ="test_map_id" ; +const __u64 nsec_per_sec =1000000000 ;struct bpf_object *objs[nr_iters];int prog_fds[nr_iters], map_fds[nr_iters];/* +1 to test for the info_len returned by kernel */ struct bpf_prog_info prog_infos[nr_iters +1 ];struct bpf_map_info map_infos[nr_iters +1 ]; +/* Each prog only uses one map. +1 to test nr_map_ids +* returned by kernel. +*/ +__u32 map_ids[nr_iters +1 ];char jited_insns[128 ], xlated_insns[128 ], zeros[128 ];__u32 i, next_id, info_len, nr_id_found, duration =0 ; +struct timespec real_time_ts, boot_time_ts;int sysctl_fd, jit_enabled =0 , err =0 ;__u64 array_value; +uid_t my_uid =getuid (); +time_t now, load_time; sysctl_fd =open (jit_sysctl,0 ,O_RDONLY );if (sysctl_fd !=-1 ) {
@@ -307,6 +320,7 @@ static void test_bpf_obj_id(void)/* Check bpf_obj_get_info_by_fd() */ bzero (zeros,sizeof (zeros));for (i =0 ; i < nr_iters; i++) { + now =time (NULL ); err =bpf_prog_load (file,BPF_PROG_TYPE_SOCKET_FILTER , &objs[i], &prog_fds[i]);/* test_obj_id.o is a dumb prog. It should never fail
@@ -334,16 +348,18 @@ static void test_bpf_obj_id(void) map_infos[i].value_size !=sizeof (__u64) || map_infos[i].max_entries !=1 || map_infos[i].map_flags !=0 || - info_len !=sizeof (struct bpf_map_info ), + info_len !=sizeof (struct bpf_map_info ) || +strcmp ((char *)map_infos[i].name, expected_map_name),"get-map-info(fd)" , -"err %d errno %d type %d(%d) info_len %u(%lu) key_size %u value_size %u max_entries %u map_flags %X\n" , +"err %d errno %d type %d(%d) info_len %u(%lu) key_size %u value_size %u max_entries %u map_flags %X name %s(%s)\n" , err, errno, map_infos[i].type,BPF_MAP_TYPE_ARRAY , info_len,sizeof (struct bpf_map_info ), map_infos[i].key_size, map_infos[i].value_size, map_infos[i].max_entries, - map_infos[i].map_flags)) + map_infos[i].map_flags, + map_infos[i].name, expected_map_name)) goto done;/* Check getting prog info */
@@ -355,8 +371,16 @@ static void test_bpf_obj_id(void) prog_infos[i].jited_prog_len =sizeof (jited_insns); prog_infos[i].xlated_prog_insns =ptr_to_u64 (xlated_insns); prog_infos[i].xlated_prog_len =sizeof (xlated_insns); + prog_infos[i].map_ids =ptr_to_u64 (map_ids + i); + prog_infos[i].nr_map_ids =2 ; + err =clock_gettime (CLOCK_REALTIME , &real_time_ts); +assert (!err); + err =clock_gettime (CLOCK_BOOTTIME , &boot_time_ts); +assert (!err); err =bpf_obj_get_info_by_fd (prog_fds[i], &prog_infos[i], &info_len); + load_time = (real_time_ts.tv_sec - boot_time_ts.tv_sec) + + (prog_infos[i].load_time / nsec_per_sec); if (CHECK(err || prog_infos[i].type != BPF_PROG_TYPE_SOCKET_FILTER || info_len != sizeof(struct bpf_prog_info) ||
@@ -364,9 +388,14 @@ static void test_bpf_obj_id(void) (jit_enabled && !memcmp (jited_insns, zeros,sizeof (zeros))) || !prog_infos[i].xlated_prog_len || - !memcmp (xlated_insns, zeros,sizeof (zeros)), + !memcmp (xlated_insns, zeros,sizeof (zeros)) || + load_time < now -60 || load_time > now +60 || + prog_infos[i].created_by_uid != my_uid || + prog_infos[i].nr_map_ids !=1 || + *(int *)prog_infos[i].map_ids != map_infos[i].id || +strcmp ((char *)prog_infos[i].name, expected_prog_name),"get-prog-info(fd)" , -"err %d errno %d i %d type %d(%d) info_len %u(%lu) jit_enabled %d jited_prog_len %u xlated_prog_len %u jited_prog %d xlated_prog %d\n" , +"err %d errno %d i %d type %d(%d) info_len %u(%lu) jit_enabled %d jited_prog_len %u xlated_prog_len %u jited_prog %d xlated_prog %d load_time %lu(%lu) uid %u(%u) nr_map_ids %u(%u) map_id %u(%u) name %s(%s)\n" , err, errno, i, prog_infos[i].type,BPF_PROG_TYPE_SOCKET_FILTER , info_len, sizeof(struct bpf_prog_info),
@@ -374,9 +403,13 @@ static void test_bpf_obj_id(void) prog_infos[i].jited_prog_len, prog_infos[i].xlated_prog_len, !!memcmp (jited_insns, zeros,sizeof (zeros)), - !!memcmp(xlated_insns, zeros, sizeof(zeros)))) + !!memcmp (xlated_insns, zeros,sizeof (zeros)), + load_time, now, + prog_infos[i].created_by_uid, my_uid, + prog_infos[i].nr_map_ids,1 , + *(int *)prog_infos[i].map_ids, map_infos[i].id, + prog_infos[i].name, expected_prog_name)) goto done; - }/* Check bpf_prog_get_next_id() */
@@ -384,6 +417,7 @@ static void test_bpf_obj_id(void) next_id =0 ;while (!bpf_prog_get_next_id (next_id, &next_id)) {struct bpf_prog_info prog_info = {}; +__u32 saved_map_id;int prog_fd; info_len =sizeof (prog_info);
@@ -406,16 +440,33 @@ static void test_bpf_obj_id(void) nr_id_found++; +/* Negative test: +* prog_info.nr_map_ids = 1 +* prog_info.map_ids = NULL +*/ + prog_info.nr_map_ids =1 ; + err =bpf_obj_get_info_by_fd (prog_fd, &prog_info, &info_len); +if (CHECK (!err || errno !=EFAULT , +"get-prog-fd-bad-nr-map-ids" ,"err %d errno %d(%d)" , + err, errno,EFAULT )) +break ; +bzero (&prog_info,sizeof (prog_info)); + info_len =sizeof (prog_info); + + saved_map_id = *(int *)(prog_infos[i].map_ids); + prog_info.map_ids = prog_infos[i].map_ids; + prog_info.nr_map_ids =2 ; err =bpf_obj_get_info_by_fd (prog_fd, &prog_info, &info_len); prog_infos[i].jited_prog_insns =0 ; prog_infos[i].xlated_prog_insns =0 ;CHECK (err || info_len !=sizeof (struct bpf_prog_info ) || -memcmp (&prog_info, &prog_infos[i], info_len), +memcmp (&prog_info, &prog_infos[i], info_len) || + *(int *)prog_info.map_ids != saved_map_id,"get-prog-info(next_id->fd)" , -"err %d errno %d info_len %u(%lu) memcmp %d\n" , +"err %d errno %d info_len %u(%lu) memcmp %d map_id %u(%u)\n" , err, errno, info_len,sizeof (struct bpf_prog_info ), -memcmp (&prog_info, &prog_infos[i], info_len)); - +memcmp (&prog_info, &prog_infos[i], info_len), + *(int *)prog_info.map_ids, saved_map_id);close (prog_fd); } CHECK(nr_id_found != nr_iters,
@@ -497,6 +548,75 @@ static void test_pkt_md_access(void)bpf_object__close (obj); } +static void test_obj_name (void ) +{ +struct { +const char *name; +int success; +int expected_errno; + } tests[] = { + {"" ,1 ,0 }, + {"_123456789ABCDE" ,1 ,0 }, + {"_123456789ABCDEF" ,0 ,EINVAL }, + {"_123456789ABCD\n" ,0 ,EINVAL }, + }; +struct bpf_insn prog[] = { +BPF_ALU64_IMM (BPF_MOV ,BPF_REG_0 ,0 ), +BPF_EXIT_INSN (), + }; +__u32 duration =0 ; +int i; + +for (i =0 ; i <sizeof (tests) /sizeof (tests[0 ]); i++) { +size_t name_len =strlen (tests[i].name) +1 ; +union bpf_attr attr; +size_t ncopy; +int fd; + +/* test different attr.prog_name during BPF_PROG_LOAD */ + ncopy = name_len <sizeof (attr.prog_name) ? + name_len :sizeof (attr.prog_name); +bzero (&attr,sizeof (attr)); + attr.prog_type =BPF_PROG_TYPE_SCHED_CLS ; + attr.insn_cnt =2 ; + attr.insns =ptr_to_u64 (prog); + attr.license =ptr_to_u64 ("" ); +memcpy (attr.prog_name, tests[i].name, ncopy); + + fd =syscall (__NR_bpf,BPF_PROG_LOAD , &attr,sizeof (attr)); +CHECK ((tests[i].success && fd <0 ) || + (!tests[i].success && fd !=-1 ) || + (!tests[i].success && errno != tests[i].expected_errno), +"check-bpf-prog-name" , +"fd %d(%d) errno %d(%d)\n" , + fd, tests[i].success, errno, tests[i].expected_errno); + +if (fd !=-1 ) +close (fd); + +/* test different attr.map_name during BPF_MAP_CREATE */ + ncopy = name_len <sizeof (attr.map_name) ? + name_len :sizeof (attr.map_name); +bzero (&attr,sizeof (attr)); + attr.map_type =BPF_MAP_TYPE_ARRAY ; + attr.key_size =4 ; + attr.value_size =4 ; + attr.max_entries =1 ; + attr.map_flags =0 ; +memcpy (attr.map_name, tests[i].name, ncopy); + fd =syscall (__NR_bpf,BPF_MAP_CREATE , &attr,sizeof (attr)); +CHECK ((tests[i].success && fd <0 ) || + (!tests[i].success && fd !=-1 ) || + (!tests[i].success && errno != tests[i].expected_errno), +"check-bpf-map-name" , +"fd %d(%d) errno %d(%d)\n" , + fd, tests[i].success, errno, tests[i].expected_errno); + +if (fd !=-1 ) +close (fd); + } +} +int main (void ) {struct rlimit rinf = {RLIM_INFINITY ,RLIM_INFINITY };
@@ -509,6 +629,7 @@ int main(void)test_tcp_estats ();test_bpf_obj_id ();test_pkt_md_access (); +test_obj_name ();printf ("Summary: %d PASSED, %d FAILED\n" , pass_cnt, error_cnt);return error_cnt ?EXIT_FAILURE :EXIT_SUCCESS ;
Signature · 1 line
2.9.5
David Miller
maintainerFrom: Martin KaFai Lau <[email protected]>
Date: Wed, 27 Sep 2017 14:37:51 -0700
Quoted 2 lines
This patch series adds more fields to
bpf_prog_infoandbpf_map_info. Please see individual patch for details.
Great to see progress in the area of eBPF introspection.
Series applied, thanks.