mirror of
				https://github.com/torvalds/linux.git
				synced 2025-11-04 10:40:15 +02:00 
			
		
		
		
	Merge branch 'bpf-extend-info'
Martin KaFai Lau says:
====================
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.
====================
Signed-off-by: David S. Miller <davem@davemloft.net>
			
			
This commit is contained in:
		
						commit
						fadad670a8
					
				
					 11 changed files with 387 additions and 87 deletions
				
			
		| 
						 | 
					@ -56,6 +56,7 @@ struct bpf_map {
 | 
				
			||||||
	struct work_struct work;
 | 
						struct work_struct work;
 | 
				
			||||||
	atomic_t usercnt;
 | 
						atomic_t usercnt;
 | 
				
			||||||
	struct bpf_map *inner_map_meta;
 | 
						struct bpf_map *inner_map_meta;
 | 
				
			||||||
 | 
						u8 name[BPF_OBJ_NAME_LEN];
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/* function argument constraints */
 | 
					/* function argument constraints */
 | 
				
			||||||
| 
						 | 
					@ -187,6 +188,8 @@ struct bpf_prog_aux {
 | 
				
			||||||
	struct bpf_map **used_maps;
 | 
						struct bpf_map **used_maps;
 | 
				
			||||||
	struct bpf_prog *prog;
 | 
						struct bpf_prog *prog;
 | 
				
			||||||
	struct user_struct *user;
 | 
						struct user_struct *user;
 | 
				
			||||||
 | 
						u64 load_time; /* ns since boottime */
 | 
				
			||||||
 | 
						u8 name[BPF_OBJ_NAME_LEN];
 | 
				
			||||||
	union {
 | 
						union {
 | 
				
			||||||
		struct work_struct work;
 | 
							struct work_struct work;
 | 
				
			||||||
		struct rcu_head	rcu;
 | 
							struct rcu_head	rcu;
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -175,6 +175,8 @@ enum bpf_attach_type {
 | 
				
			||||||
/* Specify numa node during map creation */
 | 
					/* Specify numa node during map creation */
 | 
				
			||||||
#define BPF_F_NUMA_NODE		(1U << 2)
 | 
					#define BPF_F_NUMA_NODE		(1U << 2)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#define BPF_OBJ_NAME_LEN 16U
 | 
				
			||||||
 | 
					
 | 
				
			||||||
union bpf_attr {
 | 
					union bpf_attr {
 | 
				
			||||||
	struct { /* anonymous struct used by BPF_MAP_CREATE command */
 | 
						struct { /* anonymous struct used by BPF_MAP_CREATE command */
 | 
				
			||||||
		__u32	map_type;	/* one of enum bpf_map_type */
 | 
							__u32	map_type;	/* one of enum bpf_map_type */
 | 
				
			||||||
| 
						 | 
					@ -188,6 +190,7 @@ union bpf_attr {
 | 
				
			||||||
		__u32	numa_node;	/* numa node (effective only if
 | 
							__u32	numa_node;	/* numa node (effective only if
 | 
				
			||||||
					 * BPF_F_NUMA_NODE is set).
 | 
										 * BPF_F_NUMA_NODE is set).
 | 
				
			||||||
					 */
 | 
										 */
 | 
				
			||||||
 | 
							__u8	map_name[BPF_OBJ_NAME_LEN];
 | 
				
			||||||
	};
 | 
						};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	struct { /* anonymous struct used by BPF_MAP_*_ELEM commands */
 | 
						struct { /* anonymous struct used by BPF_MAP_*_ELEM commands */
 | 
				
			||||||
| 
						 | 
					@ -210,6 +213,7 @@ union bpf_attr {
 | 
				
			||||||
		__aligned_u64	log_buf;	/* user supplied buffer */
 | 
							__aligned_u64	log_buf;	/* user supplied buffer */
 | 
				
			||||||
		__u32		kern_version;	/* checked when prog_type=kprobe */
 | 
							__u32		kern_version;	/* checked when prog_type=kprobe */
 | 
				
			||||||
		__u32		prog_flags;
 | 
							__u32		prog_flags;
 | 
				
			||||||
 | 
							__u8		prog_name[BPF_OBJ_NAME_LEN];
 | 
				
			||||||
	};
 | 
						};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	struct { /* anonymous struct used by BPF_OBJ_* commands */
 | 
						struct { /* anonymous struct used by BPF_OBJ_* commands */
 | 
				
			||||||
| 
						 | 
					@ -812,6 +816,11 @@ struct bpf_prog_info {
 | 
				
			||||||
	__u32 xlated_prog_len;
 | 
						__u32 xlated_prog_len;
 | 
				
			||||||
	__aligned_u64 jited_prog_insns;
 | 
						__aligned_u64 jited_prog_insns;
 | 
				
			||||||
	__aligned_u64 xlated_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)));
 | 
					} __attribute__((aligned(8)));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
struct bpf_map_info {
 | 
					struct bpf_map_info {
 | 
				
			||||||
| 
						 | 
					@ -821,6 +830,7 @@ struct bpf_map_info {
 | 
				
			||||||
	__u32 value_size;
 | 
						__u32 value_size;
 | 
				
			||||||
	__u32 max_entries;
 | 
						__u32 max_entries;
 | 
				
			||||||
	__u32 map_flags;
 | 
						__u32 map_flags;
 | 
				
			||||||
 | 
						__u8  name[BPF_OBJ_NAME_LEN];
 | 
				
			||||||
} __attribute__((aligned(8)));
 | 
					} __attribute__((aligned(8)));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/* User bpf_sock_ops struct to access socket values and specify request ops
 | 
					/* User bpf_sock_ops struct to access socket values and specify request ops
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -23,6 +23,9 @@
 | 
				
			||||||
#include <linux/version.h>
 | 
					#include <linux/version.h>
 | 
				
			||||||
#include <linux/kernel.h>
 | 
					#include <linux/kernel.h>
 | 
				
			||||||
#include <linux/idr.h>
 | 
					#include <linux/idr.h>
 | 
				
			||||||
 | 
					#include <linux/cred.h>
 | 
				
			||||||
 | 
					#include <linux/timekeeping.h>
 | 
				
			||||||
 | 
					#include <linux/ctype.h>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#define IS_FD_ARRAY(map) ((map)->map_type == BPF_MAP_TYPE_PROG_ARRAY || \
 | 
					#define IS_FD_ARRAY(map) ((map)->map_type == BPF_MAP_TYPE_PROG_ARRAY || \
 | 
				
			||||||
			   (map)->map_type == BPF_MAP_TYPE_PERF_EVENT_ARRAY || \
 | 
								   (map)->map_type == BPF_MAP_TYPE_PERF_EVENT_ARRAY || \
 | 
				
			||||||
| 
						 | 
					@ -312,7 +315,31 @@ int bpf_map_new_fd(struct bpf_map *map)
 | 
				
			||||||
		   offsetof(union bpf_attr, CMD##_LAST_FIELD) - \
 | 
							   offsetof(union bpf_attr, CMD##_LAST_FIELD) - \
 | 
				
			||||||
		   sizeof(attr->CMD##_LAST_FIELD)) != NULL
 | 
							   sizeof(attr->CMD##_LAST_FIELD)) != NULL
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#define BPF_MAP_CREATE_LAST_FIELD numa_node
 | 
					/* dst and src must have at least BPF_OBJ_NAME_LEN number of bytes.
 | 
				
			||||||
 | 
					 * Return 0 on success and < 0 on error.
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					static int bpf_obj_name_cpy(char *dst, const char *src)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						const char *end = src + BPF_OBJ_NAME_LEN;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						/* Copy all isalnum() and '_' char */
 | 
				
			||||||
 | 
						while (src < end && *src) {
 | 
				
			||||||
 | 
							if (!isalnum(*src) && *src != '_')
 | 
				
			||||||
 | 
								return -EINVAL;
 | 
				
			||||||
 | 
							*dst++ = *src++;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						/* No '\0' found in BPF_OBJ_NAME_LEN number of bytes */
 | 
				
			||||||
 | 
						if (src == end)
 | 
				
			||||||
 | 
							return -EINVAL;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						/* '\0' terminates dst */
 | 
				
			||||||
 | 
						*dst = 0;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						return 0;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#define BPF_MAP_CREATE_LAST_FIELD map_name
 | 
				
			||||||
/* called via syscall */
 | 
					/* called via syscall */
 | 
				
			||||||
static int map_create(union bpf_attr *attr)
 | 
					static int map_create(union bpf_attr *attr)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
| 
						 | 
					@ -334,6 +361,10 @@ static int map_create(union bpf_attr *attr)
 | 
				
			||||||
	if (IS_ERR(map))
 | 
						if (IS_ERR(map))
 | 
				
			||||||
		return PTR_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->refcnt, 1);
 | 
				
			||||||
	atomic_set(&map->usercnt, 1);
 | 
						atomic_set(&map->usercnt, 1);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -973,7 +1004,7 @@ struct bpf_prog *bpf_prog_get_type(u32 ufd, enum bpf_prog_type type)
 | 
				
			||||||
EXPORT_SYMBOL_GPL(bpf_prog_get_type);
 | 
					EXPORT_SYMBOL_GPL(bpf_prog_get_type);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/* last field in 'union bpf_attr' used by this command */
 | 
					/* last field in 'union bpf_attr' used by this command */
 | 
				
			||||||
#define	BPF_PROG_LOAD_LAST_FIELD prog_flags
 | 
					#define	BPF_PROG_LOAD_LAST_FIELD prog_name
 | 
				
			||||||
 | 
					
 | 
				
			||||||
static int bpf_prog_load(union bpf_attr *attr)
 | 
					static int bpf_prog_load(union bpf_attr *attr)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
| 
						 | 
					@ -1037,6 +1068,11 @@ static int bpf_prog_load(union bpf_attr *attr)
 | 
				
			||||||
	if (err < 0)
 | 
						if (err < 0)
 | 
				
			||||||
		goto free_prog;
 | 
							goto free_prog;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						prog->aux->load_time = ktime_get_boot_ns();
 | 
				
			||||||
 | 
						err = bpf_obj_name_cpy(prog->aux->name, attr->prog_name);
 | 
				
			||||||
 | 
						if (err)
 | 
				
			||||||
 | 
							goto free_prog;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	/* run eBPF verifier */
 | 
						/* run eBPF verifier */
 | 
				
			||||||
	err = bpf_check(&prog, attr);
 | 
						err = bpf_check(&prog, attr);
 | 
				
			||||||
	if (err < 0)
 | 
						if (err < 0)
 | 
				
			||||||
| 
						 | 
					@ -1358,8 +1394,25 @@ static int bpf_prog_get_info_by_fd(struct bpf_prog *prog,
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	info.type = prog->type;
 | 
						info.type = prog->type;
 | 
				
			||||||
	info.id = prog->aux->id;
 | 
						info.id = prog->aux->id;
 | 
				
			||||||
 | 
						info.load_time = prog->aux->load_time;
 | 
				
			||||||
 | 
						info.created_by_uid = from_kuid_munged(current_user_ns(),
 | 
				
			||||||
 | 
										       prog->aux->user->uid);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	memcpy(info.tag, prog->tag, sizeof(prog->tag));
 | 
						memcpy(info.tag, prog->tag, sizeof(prog->tag));
 | 
				
			||||||
 | 
						memcpy(info.name, prog->aux->name, sizeof(prog->aux->name));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						ulen = info.nr_map_ids;
 | 
				
			||||||
 | 
						info.nr_map_ids = prog->aux->used_map_cnt;
 | 
				
			||||||
 | 
						ulen = min_t(u32, info.nr_map_ids, ulen);
 | 
				
			||||||
 | 
						if (ulen) {
 | 
				
			||||||
 | 
							u32 *user_map_ids = (u32 *)info.map_ids;
 | 
				
			||||||
 | 
							u32 i;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							for (i = 0; i < ulen; i++)
 | 
				
			||||||
 | 
								if (put_user(prog->aux->used_maps[i]->id,
 | 
				
			||||||
 | 
									     &user_map_ids[i]))
 | 
				
			||||||
 | 
									return -EFAULT;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if (!capable(CAP_SYS_ADMIN)) {
 | 
						if (!capable(CAP_SYS_ADMIN)) {
 | 
				
			||||||
		info.jited_prog_len = 0;
 | 
							info.jited_prog_len = 0;
 | 
				
			||||||
| 
						 | 
					@ -1413,6 +1466,7 @@ static int bpf_map_get_info_by_fd(struct bpf_map *map,
 | 
				
			||||||
	info.value_size = map->value_size;
 | 
						info.value_size = map->value_size;
 | 
				
			||||||
	info.max_entries = map->max_entries;
 | 
						info.max_entries = map->max_entries;
 | 
				
			||||||
	info.map_flags = map->map_flags;
 | 
						info.map_flags = map->map_flags;
 | 
				
			||||||
 | 
						memcpy(info.name, map->name, sizeof(map->name));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if (copy_to_user(uinfo, &info, info_len) ||
 | 
						if (copy_to_user(uinfo, &info, info_len) ||
 | 
				
			||||||
	    put_user(info_len, &uattr->info.info_len))
 | 
						    put_user(info_len, &uattr->info.info_len))
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -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];
 | 
								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,
 | 
								map_fd[i] = bpf_create_map_in_map_node(maps[i].def.type,
 | 
				
			||||||
 | 
												maps[i].name,
 | 
				
			||||||
							maps[i].def.key_size,
 | 
												maps[i].def.key_size,
 | 
				
			||||||
							inner_map_fd,
 | 
												inner_map_fd,
 | 
				
			||||||
							maps[i].def.max_entries,
 | 
												maps[i].def.max_entries,
 | 
				
			||||||
| 
						 | 
					@ -228,6 +229,7 @@ static int load_maps(struct bpf_map_data *maps, int nr_maps,
 | 
				
			||||||
							numa_node);
 | 
												numa_node);
 | 
				
			||||||
		} else {
 | 
							} else {
 | 
				
			||||||
			map_fd[i] = bpf_create_map_node(maps[i].def.type,
 | 
								map_fd[i] = bpf_create_map_node(maps[i].def.type,
 | 
				
			||||||
 | 
												maps[i].name,
 | 
				
			||||||
							maps[i].def.key_size,
 | 
												maps[i].def.key_size,
 | 
				
			||||||
							maps[i].def.value_size,
 | 
												maps[i].def.value_size,
 | 
				
			||||||
							maps[i].def.max_entries,
 | 
												maps[i].def.max_entries,
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -137,6 +137,7 @@ static void do_test_lru(enum test_type test, int cpu)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
			inner_lru_map_fds[cpu] =
 | 
								inner_lru_map_fds[cpu] =
 | 
				
			||||||
				bpf_create_map_node(BPF_MAP_TYPE_LRU_HASH,
 | 
									bpf_create_map_node(BPF_MAP_TYPE_LRU_HASH,
 | 
				
			||||||
 | 
											    test_map_names[INNER_LRU_HASH_PREALLOC],
 | 
				
			||||||
						    sizeof(uint32_t),
 | 
											    sizeof(uint32_t),
 | 
				
			||||||
						    sizeof(long),
 | 
											    sizeof(long),
 | 
				
			||||||
						    inner_lru_hash_size, 0,
 | 
											    inner_lru_hash_size, 0,
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -175,6 +175,8 @@ enum bpf_attach_type {
 | 
				
			||||||
/* Specify numa node during map creation */
 | 
					/* Specify numa node during map creation */
 | 
				
			||||||
#define BPF_F_NUMA_NODE		(1U << 2)
 | 
					#define BPF_F_NUMA_NODE		(1U << 2)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#define BPF_OBJ_NAME_LEN 16U
 | 
				
			||||||
 | 
					
 | 
				
			||||||
union bpf_attr {
 | 
					union bpf_attr {
 | 
				
			||||||
	struct { /* anonymous struct used by BPF_MAP_CREATE command */
 | 
						struct { /* anonymous struct used by BPF_MAP_CREATE command */
 | 
				
			||||||
		__u32	map_type;	/* one of enum bpf_map_type */
 | 
							__u32	map_type;	/* one of enum bpf_map_type */
 | 
				
			||||||
| 
						 | 
					@ -188,6 +190,7 @@ union bpf_attr {
 | 
				
			||||||
		__u32	numa_node;	/* numa node (effective only if
 | 
							__u32	numa_node;	/* numa node (effective only if
 | 
				
			||||||
					 * BPF_F_NUMA_NODE is set).
 | 
										 * BPF_F_NUMA_NODE is set).
 | 
				
			||||||
					 */
 | 
										 */
 | 
				
			||||||
 | 
							__u8	map_name[BPF_OBJ_NAME_LEN];
 | 
				
			||||||
	};
 | 
						};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	struct { /* anonymous struct used by BPF_MAP_*_ELEM commands */
 | 
						struct { /* anonymous struct used by BPF_MAP_*_ELEM commands */
 | 
				
			||||||
| 
						 | 
					@ -210,6 +213,7 @@ union bpf_attr {
 | 
				
			||||||
		__aligned_u64	log_buf;	/* user supplied buffer */
 | 
							__aligned_u64	log_buf;	/* user supplied buffer */
 | 
				
			||||||
		__u32		kern_version;	/* checked when prog_type=kprobe */
 | 
							__u32		kern_version;	/* checked when prog_type=kprobe */
 | 
				
			||||||
		__u32		prog_flags;
 | 
							__u32		prog_flags;
 | 
				
			||||||
 | 
							__u8		prog_name[BPF_OBJ_NAME_LEN];
 | 
				
			||||||
	};
 | 
						};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	struct { /* anonymous struct used by BPF_OBJ_* commands */
 | 
						struct { /* anonymous struct used by BPF_OBJ_* commands */
 | 
				
			||||||
| 
						 | 
					@ -812,6 +816,11 @@ struct bpf_prog_info {
 | 
				
			||||||
	__u32 xlated_prog_len;
 | 
						__u32 xlated_prog_len;
 | 
				
			||||||
	__aligned_u64 jited_prog_insns;
 | 
						__aligned_u64 jited_prog_insns;
 | 
				
			||||||
	__aligned_u64 xlated_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)));
 | 
					} __attribute__((aligned(8)));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
struct bpf_map_info {
 | 
					struct bpf_map_info {
 | 
				
			||||||
| 
						 | 
					@ -821,6 +830,7 @@ struct bpf_map_info {
 | 
				
			||||||
	__u32 value_size;
 | 
						__u32 value_size;
 | 
				
			||||||
	__u32 max_entries;
 | 
						__u32 max_entries;
 | 
				
			||||||
	__u32 map_flags;
 | 
						__u32 map_flags;
 | 
				
			||||||
 | 
						__u8  name[BPF_OBJ_NAME_LEN];
 | 
				
			||||||
} __attribute__((aligned(8)));
 | 
					} __attribute__((aligned(8)));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/* User bpf_sock_ops struct to access socket values and specify request ops
 | 
					/* User bpf_sock_ops struct to access socket values and specify request ops
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -46,6 +46,8 @@
 | 
				
			||||||
# endif
 | 
					# endif
 | 
				
			||||||
#endif
 | 
					#endif
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#define min(x, y) ((x) < (y) ? (x) : (y))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
static inline __u64 ptr_to_u64(const void *ptr)
 | 
					static inline __u64 ptr_to_u64(const void *ptr)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
	return (__u64) (unsigned long) ptr;
 | 
						return (__u64) (unsigned long) ptr;
 | 
				
			||||||
| 
						 | 
					@ -57,10 +59,11 @@ static inline int sys_bpf(enum bpf_cmd cmd, union bpf_attr *attr,
 | 
				
			||||||
	return syscall(__NR_bpf, cmd, attr, size);
 | 
						return syscall(__NR_bpf, cmd, attr, size);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
int bpf_create_map_node(enum bpf_map_type map_type, int key_size,
 | 
					int bpf_create_map_node(enum bpf_map_type map_type, const char *name,
 | 
				
			||||||
			int value_size, int max_entries, __u32 map_flags,
 | 
								int key_size, int value_size, int max_entries,
 | 
				
			||||||
			int node)
 | 
								__u32 map_flags, int node)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
 | 
						__u32 name_len = name ? strlen(name) : 0;
 | 
				
			||||||
	union bpf_attr attr;
 | 
						union bpf_attr attr;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	memset(&attr, '\0', sizeof(attr));
 | 
						memset(&attr, '\0', sizeof(attr));
 | 
				
			||||||
| 
						 | 
					@ -70,6 +73,8 @@ int bpf_create_map_node(enum bpf_map_type map_type, int key_size,
 | 
				
			||||||
	attr.value_size = value_size;
 | 
						attr.value_size = value_size;
 | 
				
			||||||
	attr.max_entries = max_entries;
 | 
						attr.max_entries = max_entries;
 | 
				
			||||||
	attr.map_flags = map_flags;
 | 
						attr.map_flags = map_flags;
 | 
				
			||||||
 | 
						memcpy(attr.map_name, name, min(name_len, BPF_OBJ_NAME_LEN - 1));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if (node >= 0) {
 | 
						if (node >= 0) {
 | 
				
			||||||
		attr.map_flags |= BPF_F_NUMA_NODE;
 | 
							attr.map_flags |= BPF_F_NUMA_NODE;
 | 
				
			||||||
		attr.numa_node = node;
 | 
							attr.numa_node = node;
 | 
				
			||||||
| 
						 | 
					@ -81,14 +86,23 @@ int bpf_create_map_node(enum bpf_map_type map_type, int key_size,
 | 
				
			||||||
int bpf_create_map(enum bpf_map_type map_type, int key_size,
 | 
					int bpf_create_map(enum bpf_map_type map_type, int key_size,
 | 
				
			||||||
		   int value_size, int max_entries, __u32 map_flags)
 | 
							   int value_size, int max_entries, __u32 map_flags)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
	return bpf_create_map_node(map_type, key_size, value_size,
 | 
						return bpf_create_map_node(map_type, NULL, key_size, value_size,
 | 
				
			||||||
				   max_entries, map_flags, -1);
 | 
									   max_entries, map_flags, -1);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
int bpf_create_map_in_map_node(enum bpf_map_type map_type, int key_size,
 | 
					int bpf_create_map_name(enum bpf_map_type map_type, const char *name,
 | 
				
			||||||
			       int inner_map_fd, int max_entries,
 | 
								int key_size, int value_size, int max_entries,
 | 
				
			||||||
 | 
								__u32 map_flags)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						return bpf_create_map_node(map_type, name, key_size, value_size,
 | 
				
			||||||
 | 
									   max_entries, map_flags, -1);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					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)
 | 
								       __u32 map_flags, int node)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
 | 
						__u32 name_len = name ? strlen(name) : 0;
 | 
				
			||||||
	union bpf_attr attr;
 | 
						union bpf_attr attr;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	memset(&attr, '\0', sizeof(attr));
 | 
						memset(&attr, '\0', sizeof(attr));
 | 
				
			||||||
| 
						 | 
					@ -99,6 +113,8 @@ int bpf_create_map_in_map_node(enum bpf_map_type map_type, int key_size,
 | 
				
			||||||
	attr.inner_map_fd = inner_map_fd;
 | 
						attr.inner_map_fd = inner_map_fd;
 | 
				
			||||||
	attr.max_entries = max_entries;
 | 
						attr.max_entries = max_entries;
 | 
				
			||||||
	attr.map_flags = map_flags;
 | 
						attr.map_flags = map_flags;
 | 
				
			||||||
 | 
						memcpy(attr.map_name, name, min(name_len, BPF_OBJ_NAME_LEN - 1));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if (node >= 0) {
 | 
						if (node >= 0) {
 | 
				
			||||||
		attr.map_flags |= BPF_F_NUMA_NODE;
 | 
							attr.map_flags |= BPF_F_NUMA_NODE;
 | 
				
			||||||
		attr.numa_node = node;
 | 
							attr.numa_node = node;
 | 
				
			||||||
| 
						 | 
					@ -107,19 +123,24 @@ int bpf_create_map_in_map_node(enum bpf_map_type map_type, int key_size,
 | 
				
			||||||
	return sys_bpf(BPF_MAP_CREATE, &attr, sizeof(attr));
 | 
						return sys_bpf(BPF_MAP_CREATE, &attr, sizeof(attr));
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
int bpf_create_map_in_map(enum bpf_map_type map_type, int key_size,
 | 
					int bpf_create_map_in_map(enum bpf_map_type map_type, const char *name,
 | 
				
			||||||
			  int inner_map_fd, int max_entries, __u32 map_flags)
 | 
								  int key_size, int inner_map_fd, int max_entries,
 | 
				
			||||||
 | 
								  __u32 map_flags)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
	return bpf_create_map_in_map_node(map_type, key_size, inner_map_fd,
 | 
						return bpf_create_map_in_map_node(map_type, name, key_size,
 | 
				
			||||||
					  max_entries, map_flags, -1);
 | 
										  inner_map_fd, max_entries, map_flags,
 | 
				
			||||||
 | 
										  -1);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
int bpf_load_program(enum bpf_prog_type type, const struct bpf_insn *insns,
 | 
					int bpf_load_program_name(enum bpf_prog_type type, const char *name,
 | 
				
			||||||
		     size_t insns_cnt, const char *license,
 | 
								  const struct bpf_insn *insns,
 | 
				
			||||||
		     __u32 kern_version, char *log_buf, size_t log_buf_sz)
 | 
								  size_t insns_cnt, const char *license,
 | 
				
			||||||
 | 
								  __u32 kern_version, char *log_buf,
 | 
				
			||||||
 | 
								  size_t log_buf_sz)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
	int fd;
 | 
						int fd;
 | 
				
			||||||
	union bpf_attr attr;
 | 
						union bpf_attr attr;
 | 
				
			||||||
 | 
						__u32 name_len = name ? strlen(name) : 0;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	bzero(&attr, sizeof(attr));
 | 
						bzero(&attr, sizeof(attr));
 | 
				
			||||||
	attr.prog_type = type;
 | 
						attr.prog_type = type;
 | 
				
			||||||
| 
						 | 
					@ -130,6 +151,7 @@ int bpf_load_program(enum bpf_prog_type type, const struct bpf_insn *insns,
 | 
				
			||||||
	attr.log_size = 0;
 | 
						attr.log_size = 0;
 | 
				
			||||||
	attr.log_level = 0;
 | 
						attr.log_level = 0;
 | 
				
			||||||
	attr.kern_version = kern_version;
 | 
						attr.kern_version = kern_version;
 | 
				
			||||||
 | 
						memcpy(attr.prog_name, name, min(name_len, BPF_OBJ_NAME_LEN - 1));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	fd = sys_bpf(BPF_PROG_LOAD, &attr, sizeof(attr));
 | 
						fd = sys_bpf(BPF_PROG_LOAD, &attr, sizeof(attr));
 | 
				
			||||||
	if (fd >= 0 || !log_buf || !log_buf_sz)
 | 
						if (fd >= 0 || !log_buf || !log_buf_sz)
 | 
				
			||||||
| 
						 | 
					@ -143,6 +165,15 @@ int bpf_load_program(enum bpf_prog_type type, const struct bpf_insn *insns,
 | 
				
			||||||
	return sys_bpf(BPF_PROG_LOAD, &attr, sizeof(attr));
 | 
						return sys_bpf(BPF_PROG_LOAD, &attr, sizeof(attr));
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					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,
 | 
				
			||||||
 | 
							     size_t log_buf_sz)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						return bpf_load_program_name(type, NULL, insns, insns_cnt, license,
 | 
				
			||||||
 | 
									     kern_version, log_buf, log_buf_sz);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
int bpf_verify_program(enum bpf_prog_type type, const struct bpf_insn *insns,
 | 
					int bpf_verify_program(enum bpf_prog_type type, const struct bpf_insn *insns,
 | 
				
			||||||
		       size_t insns_cnt, int strict_alignment,
 | 
							       size_t insns_cnt, int strict_alignment,
 | 
				
			||||||
		       const char *license, __u32 kern_version,
 | 
							       const char *license, __u32 kern_version,
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -24,19 +24,28 @@
 | 
				
			||||||
#include <linux/bpf.h>
 | 
					#include <linux/bpf.h>
 | 
				
			||||||
#include <stddef.h>
 | 
					#include <stddef.h>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
int bpf_create_map_node(enum bpf_map_type map_type, int key_size,
 | 
					int bpf_create_map_node(enum bpf_map_type map_type, const char *name,
 | 
				
			||||||
			int value_size, int max_entries, __u32 map_flags,
 | 
								int key_size, int value_size, int max_entries,
 | 
				
			||||||
			int node);
 | 
								__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 bpf_create_map(enum bpf_map_type map_type, int key_size, int value_size,
 | 
				
			||||||
		   int max_entries, __u32 map_flags);
 | 
							   int max_entries, __u32 map_flags);
 | 
				
			||||||
int bpf_create_map_in_map_node(enum bpf_map_type map_type, int key_size,
 | 
					int bpf_create_map_in_map_node(enum bpf_map_type map_type, const char *name,
 | 
				
			||||||
			       int inner_map_fd, int max_entries,
 | 
								       int key_size, int inner_map_fd, int max_entries,
 | 
				
			||||||
			       __u32 map_flags, int node);
 | 
								       __u32 map_flags, int node);
 | 
				
			||||||
int bpf_create_map_in_map(enum bpf_map_type map_type, int key_size,
 | 
					int bpf_create_map_in_map(enum bpf_map_type map_type, const char *name,
 | 
				
			||||||
			  int inner_map_fd, int max_entries, __u32 map_flags);
 | 
								  int key_size, int inner_map_fd, int max_entries,
 | 
				
			||||||
 | 
								  __u32 map_flags);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/* Recommend log buffer size */
 | 
					/* Recommend log buffer size */
 | 
				
			||||||
#define BPF_LOG_BUF_SIZE 65536
 | 
					#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,
 | 
					int bpf_load_program(enum bpf_prog_type type, const struct bpf_insn *insns,
 | 
				
			||||||
		     size_t insns_cnt, const char *license,
 | 
							     size_t insns_cnt, const char *license,
 | 
				
			||||||
		     __u32 kern_version, char *log_buf,
 | 
							     __u32 kern_version, char *log_buf,
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -171,6 +171,7 @@ int libbpf_strerror(int err, char *buf, size_t size)
 | 
				
			||||||
struct bpf_program {
 | 
					struct bpf_program {
 | 
				
			||||||
	/* Index in elf obj file, for relocation use. */
 | 
						/* Index in elf obj file, for relocation use. */
 | 
				
			||||||
	int idx;
 | 
						int idx;
 | 
				
			||||||
 | 
						char *name;
 | 
				
			||||||
	char *section_name;
 | 
						char *section_name;
 | 
				
			||||||
	struct bpf_insn *insns;
 | 
						struct bpf_insn *insns;
 | 
				
			||||||
	size_t insns_cnt;
 | 
						size_t insns_cnt;
 | 
				
			||||||
| 
						 | 
					@ -283,6 +284,7 @@ static void bpf_program__exit(struct bpf_program *prog)
 | 
				
			||||||
	prog->clear_priv = NULL;
 | 
						prog->clear_priv = NULL;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	bpf_program__unload(prog);
 | 
						bpf_program__unload(prog);
 | 
				
			||||||
 | 
						zfree(&prog->name);
 | 
				
			||||||
	zfree(&prog->section_name);
 | 
						zfree(&prog->section_name);
 | 
				
			||||||
	zfree(&prog->insns);
 | 
						zfree(&prog->insns);
 | 
				
			||||||
	zfree(&prog->reloc_desc);
 | 
						zfree(&prog->reloc_desc);
 | 
				
			||||||
| 
						 | 
					@ -293,26 +295,27 @@ static void bpf_program__exit(struct bpf_program *prog)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
static int
 | 
					static int
 | 
				
			||||||
bpf_program__init(void *data, size_t size, char *name, int idx,
 | 
					bpf_program__init(void *data, size_t size, char *section_name, int idx,
 | 
				
			||||||
		    struct bpf_program *prog)
 | 
							  struct bpf_program *prog)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
	if (size < sizeof(struct bpf_insn)) {
 | 
						if (size < sizeof(struct bpf_insn)) {
 | 
				
			||||||
		pr_warning("corrupted section '%s'\n", name);
 | 
							pr_warning("corrupted section '%s'\n", section_name);
 | 
				
			||||||
		return -EINVAL;
 | 
							return -EINVAL;
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	bzero(prog, sizeof(*prog));
 | 
						bzero(prog, sizeof(*prog));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	prog->section_name = strdup(name);
 | 
						prog->section_name = strdup(section_name);
 | 
				
			||||||
	if (!prog->section_name) {
 | 
						if (!prog->section_name) {
 | 
				
			||||||
		pr_warning("failed to alloc name for prog %s\n",
 | 
							pr_warning("failed to alloc name for prog under section %s\n",
 | 
				
			||||||
			   name);
 | 
								   section_name);
 | 
				
			||||||
		goto errout;
 | 
							goto errout;
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	prog->insns = malloc(size);
 | 
						prog->insns = malloc(size);
 | 
				
			||||||
	if (!prog->insns) {
 | 
						if (!prog->insns) {
 | 
				
			||||||
		pr_warning("failed to alloc insns for %s\n", name);
 | 
							pr_warning("failed to alloc insns for prog under section %s\n",
 | 
				
			||||||
 | 
								   section_name);
 | 
				
			||||||
		goto errout;
 | 
							goto errout;
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	prog->insns_cnt = size / sizeof(struct bpf_insn);
 | 
						prog->insns_cnt = size / sizeof(struct bpf_insn);
 | 
				
			||||||
| 
						 | 
					@ -331,12 +334,12 @@ bpf_program__init(void *data, size_t size, char *name, int idx,
 | 
				
			||||||
 | 
					
 | 
				
			||||||
static int
 | 
					static int
 | 
				
			||||||
bpf_object__add_program(struct bpf_object *obj, void *data, size_t size,
 | 
					bpf_object__add_program(struct bpf_object *obj, void *data, size_t size,
 | 
				
			||||||
			char *name, int idx)
 | 
								char *section_name, int idx)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
	struct bpf_program prog, *progs;
 | 
						struct bpf_program prog, *progs;
 | 
				
			||||||
	int nr_progs, err;
 | 
						int nr_progs, err;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	err = bpf_program__init(data, size, name, idx, &prog);
 | 
						err = bpf_program__init(data, size, section_name, idx, &prog);
 | 
				
			||||||
	if (err)
 | 
						if (err)
 | 
				
			||||||
		return err;
 | 
							return err;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -350,8 +353,8 @@ bpf_object__add_program(struct bpf_object *obj, void *data, size_t size,
 | 
				
			||||||
		 * is still valid, so don't need special treat for
 | 
							 * is still valid, so don't need special treat for
 | 
				
			||||||
		 * bpf_close_object().
 | 
							 * bpf_close_object().
 | 
				
			||||||
		 */
 | 
							 */
 | 
				
			||||||
		pr_warning("failed to alloc a new program '%s'\n",
 | 
							pr_warning("failed to alloc a new program under section '%s'\n",
 | 
				
			||||||
			   name);
 | 
								   section_name);
 | 
				
			||||||
		bpf_program__exit(&prog);
 | 
							bpf_program__exit(&prog);
 | 
				
			||||||
		return -ENOMEM;
 | 
							return -ENOMEM;
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
| 
						 | 
					@ -364,6 +367,54 @@ bpf_object__add_program(struct bpf_object *obj, void *data, size_t size,
 | 
				
			||||||
	return 0;
 | 
						return 0;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					static int
 | 
				
			||||||
 | 
					bpf_object__init_prog_names(struct bpf_object *obj)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						Elf_Data *symbols = obj->efile.symbols;
 | 
				
			||||||
 | 
						struct bpf_program *prog;
 | 
				
			||||||
 | 
						size_t pi, si;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						for (pi = 0; pi < obj->nr_programs; pi++) {
 | 
				
			||||||
 | 
							char *name = NULL;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							prog = &obj->programs[pi];
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							for (si = 0; si < symbols->d_size / sizeof(GElf_Sym) && !name;
 | 
				
			||||||
 | 
							     si++) {
 | 
				
			||||||
 | 
								GElf_Sym sym;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								if (!gelf_getsym(symbols, si, &sym))
 | 
				
			||||||
 | 
									continue;
 | 
				
			||||||
 | 
								if (sym.st_shndx != prog->idx)
 | 
				
			||||||
 | 
									continue;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								name = elf_strptr(obj->efile.elf,
 | 
				
			||||||
 | 
										  obj->efile.strtabidx,
 | 
				
			||||||
 | 
										  sym.st_name);
 | 
				
			||||||
 | 
								if (!name) {
 | 
				
			||||||
 | 
									pr_warning("failed to get sym name string for prog %s\n",
 | 
				
			||||||
 | 
										   prog->section_name);
 | 
				
			||||||
 | 
									return -LIBBPF_ERRNO__LIBELF;
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							if (!name) {
 | 
				
			||||||
 | 
								pr_warning("failed to find sym for prog %s\n",
 | 
				
			||||||
 | 
									   prog->section_name);
 | 
				
			||||||
 | 
								return -EINVAL;
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							prog->name = strdup(name);
 | 
				
			||||||
 | 
							if (!prog->name) {
 | 
				
			||||||
 | 
								pr_warning("failed to allocate memory for prog sym %s\n",
 | 
				
			||||||
 | 
									   name);
 | 
				
			||||||
 | 
								return -ENOMEM;
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						return 0;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
static struct bpf_object *bpf_object__new(const char *path,
 | 
					static struct bpf_object *bpf_object__new(const char *path,
 | 
				
			||||||
					  void *obj_buf,
 | 
										  void *obj_buf,
 | 
				
			||||||
					  size_t obj_buf_sz)
 | 
										  size_t obj_buf_sz)
 | 
				
			||||||
| 
						 | 
					@ -766,8 +817,12 @@ static int bpf_object__elf_collect(struct bpf_object *obj)
 | 
				
			||||||
		pr_warning("Corrupted ELF file: index of strtab invalid\n");
 | 
							pr_warning("Corrupted ELF file: index of strtab invalid\n");
 | 
				
			||||||
		return LIBBPF_ERRNO__FORMAT;
 | 
							return LIBBPF_ERRNO__FORMAT;
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	if (obj->efile.maps_shndx >= 0)
 | 
						if (obj->efile.maps_shndx >= 0) {
 | 
				
			||||||
		err = bpf_object__init_maps(obj);
 | 
							err = bpf_object__init_maps(obj);
 | 
				
			||||||
 | 
							if (err)
 | 
				
			||||||
 | 
								goto out;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						err = bpf_object__init_prog_names(obj);
 | 
				
			||||||
out:
 | 
					out:
 | 
				
			||||||
	return err;
 | 
						return err;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					@ -870,11 +925,12 @@ bpf_object__create_maps(struct bpf_object *obj)
 | 
				
			||||||
		struct bpf_map_def *def = &obj->maps[i].def;
 | 
							struct bpf_map_def *def = &obj->maps[i].def;
 | 
				
			||||||
		int *pfd = &obj->maps[i].fd;
 | 
							int *pfd = &obj->maps[i].fd;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		*pfd = bpf_create_map(def->type,
 | 
							*pfd = bpf_create_map_name(def->type,
 | 
				
			||||||
				      def->key_size,
 | 
										   obj->maps[i].name,
 | 
				
			||||||
				      def->value_size,
 | 
										   def->key_size,
 | 
				
			||||||
				      def->max_entries,
 | 
										   def->value_size,
 | 
				
			||||||
				      0);
 | 
										   def->max_entries,
 | 
				
			||||||
 | 
										   0);
 | 
				
			||||||
		if (*pfd < 0) {
 | 
							if (*pfd < 0) {
 | 
				
			||||||
			size_t j;
 | 
								size_t j;
 | 
				
			||||||
			int err = *pfd;
 | 
								int err = *pfd;
 | 
				
			||||||
| 
						 | 
					@ -982,7 +1038,7 @@ static int bpf_object__collect_reloc(struct bpf_object *obj)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
static int
 | 
					static int
 | 
				
			||||||
load_program(enum bpf_prog_type type, struct bpf_insn *insns,
 | 
					load_program(enum bpf_prog_type type, const char *name, struct bpf_insn *insns,
 | 
				
			||||||
	     int insns_cnt, char *license, u32 kern_version, int *pfd)
 | 
						     int insns_cnt, char *license, u32 kern_version, int *pfd)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
	int ret;
 | 
						int ret;
 | 
				
			||||||
| 
						 | 
					@ -995,8 +1051,8 @@ load_program(enum bpf_prog_type type, struct bpf_insn *insns,
 | 
				
			||||||
	if (!log_buf)
 | 
						if (!log_buf)
 | 
				
			||||||
		pr_warning("Alloc log buffer for bpf loader error, continue without log\n");
 | 
							pr_warning("Alloc log buffer for bpf loader error, continue without log\n");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	ret = bpf_load_program(type, insns, insns_cnt, license,
 | 
						ret = bpf_load_program_name(type, name, insns, insns_cnt, license,
 | 
				
			||||||
			       kern_version, log_buf, BPF_LOG_BUF_SIZE);
 | 
									    kern_version, log_buf, BPF_LOG_BUF_SIZE);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if (ret >= 0) {
 | 
						if (ret >= 0) {
 | 
				
			||||||
		*pfd = ret;
 | 
							*pfd = ret;
 | 
				
			||||||
| 
						 | 
					@ -1021,9 +1077,9 @@ load_program(enum bpf_prog_type type, struct bpf_insn *insns,
 | 
				
			||||||
		if (type != BPF_PROG_TYPE_KPROBE) {
 | 
							if (type != BPF_PROG_TYPE_KPROBE) {
 | 
				
			||||||
			int fd;
 | 
								int fd;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
			fd = bpf_load_program(BPF_PROG_TYPE_KPROBE, insns,
 | 
								fd = bpf_load_program_name(BPF_PROG_TYPE_KPROBE, name,
 | 
				
			||||||
					      insns_cnt, license, kern_version,
 | 
											   insns, insns_cnt, license,
 | 
				
			||||||
					      NULL, 0);
 | 
											   kern_version, NULL, 0);
 | 
				
			||||||
			if (fd >= 0) {
 | 
								if (fd >= 0) {
 | 
				
			||||||
				close(fd);
 | 
									close(fd);
 | 
				
			||||||
				ret = -LIBBPF_ERRNO__PROGTYPE;
 | 
									ret = -LIBBPF_ERRNO__PROGTYPE;
 | 
				
			||||||
| 
						 | 
					@ -1067,8 +1123,8 @@ bpf_program__load(struct bpf_program *prog,
 | 
				
			||||||
			pr_warning("Program '%s' is inconsistent: nr(%d) != 1\n",
 | 
								pr_warning("Program '%s' is inconsistent: nr(%d) != 1\n",
 | 
				
			||||||
				   prog->section_name, prog->instances.nr);
 | 
									   prog->section_name, prog->instances.nr);
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		err = load_program(prog->type, prog->insns, prog->insns_cnt,
 | 
							err = load_program(prog->type, prog->name, prog->insns,
 | 
				
			||||||
				   license, kern_version, &fd);
 | 
									   prog->insns_cnt, license, kern_version, &fd);
 | 
				
			||||||
		if (!err)
 | 
							if (!err)
 | 
				
			||||||
			prog->instances.fds[0] = fd;
 | 
								prog->instances.fds[0] = fd;
 | 
				
			||||||
		goto out;
 | 
							goto out;
 | 
				
			||||||
| 
						 | 
					@ -1096,7 +1152,8 @@ bpf_program__load(struct bpf_program *prog,
 | 
				
			||||||
			continue;
 | 
								continue;
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		err = load_program(prog->type, result.new_insn_ptr,
 | 
							err = load_program(prog->type, prog->name,
 | 
				
			||||||
 | 
									   result.new_insn_ptr,
 | 
				
			||||||
				   result.new_insn_cnt,
 | 
									   result.new_insn_cnt,
 | 
				
			||||||
				   license, kern_version, &fd);
 | 
									   license, kern_version, &fd);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -10,6 +10,7 @@
 | 
				
			||||||
#include <string.h>
 | 
					#include <string.h>
 | 
				
			||||||
#include <assert.h>
 | 
					#include <assert.h>
 | 
				
			||||||
#include <stdlib.h>
 | 
					#include <stdlib.h>
 | 
				
			||||||
 | 
					#include <time.h>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#include <linux/types.h>
 | 
					#include <linux/types.h>
 | 
				
			||||||
typedef __u16 __sum16;
 | 
					typedef __u16 __sum16;
 | 
				
			||||||
| 
						 | 
					@ -19,6 +20,8 @@ typedef __u16 __sum16;
 | 
				
			||||||
#include <linux/ip.h>
 | 
					#include <linux/ip.h>
 | 
				
			||||||
#include <linux/ipv6.h>
 | 
					#include <linux/ipv6.h>
 | 
				
			||||||
#include <linux/tcp.h>
 | 
					#include <linux/tcp.h>
 | 
				
			||||||
 | 
					#include <linux/filter.h>
 | 
				
			||||||
 | 
					#include <linux/unistd.h>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#include <sys/wait.h>
 | 
					#include <sys/wait.h>
 | 
				
			||||||
#include <sys/resource.h>
 | 
					#include <sys/resource.h>
 | 
				
			||||||
| 
						 | 
					@ -273,16 +276,26 @@ static void test_bpf_obj_id(void)
 | 
				
			||||||
	const int nr_iters = 2;
 | 
						const int nr_iters = 2;
 | 
				
			||||||
	const char *file = "./test_obj_id.o";
 | 
						const char *file = "./test_obj_id.o";
 | 
				
			||||||
	const char *jit_sysctl = "/proc/sys/net/core/bpf_jit_enable";
 | 
						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];
 | 
						struct bpf_object *objs[nr_iters];
 | 
				
			||||||
	int prog_fds[nr_iters], map_fds[nr_iters];
 | 
						int prog_fds[nr_iters], map_fds[nr_iters];
 | 
				
			||||||
	/* +1 to test for the info_len returned by kernel */
 | 
						/* +1 to test for the info_len returned by kernel */
 | 
				
			||||||
	struct bpf_prog_info prog_infos[nr_iters + 1];
 | 
						struct bpf_prog_info prog_infos[nr_iters + 1];
 | 
				
			||||||
	struct bpf_map_info map_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];
 | 
						char jited_insns[128], xlated_insns[128], zeros[128];
 | 
				
			||||||
	__u32 i, next_id, info_len, nr_id_found, duration = 0;
 | 
						__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;
 | 
						int sysctl_fd, jit_enabled = 0, err = 0;
 | 
				
			||||||
	__u64 array_value;
 | 
						__u64 array_value;
 | 
				
			||||||
 | 
						uid_t my_uid = getuid();
 | 
				
			||||||
 | 
						time_t now, load_time;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	sysctl_fd = open(jit_sysctl, 0, O_RDONLY);
 | 
						sysctl_fd = open(jit_sysctl, 0, O_RDONLY);
 | 
				
			||||||
	if (sysctl_fd != -1) {
 | 
						if (sysctl_fd != -1) {
 | 
				
			||||||
| 
						 | 
					@ -307,6 +320,7 @@ static void test_bpf_obj_id(void)
 | 
				
			||||||
	/* Check bpf_obj_get_info_by_fd() */
 | 
						/* Check bpf_obj_get_info_by_fd() */
 | 
				
			||||||
	bzero(zeros, sizeof(zeros));
 | 
						bzero(zeros, sizeof(zeros));
 | 
				
			||||||
	for (i = 0; i < nr_iters; i++) {
 | 
						for (i = 0; i < nr_iters; i++) {
 | 
				
			||||||
 | 
							now = time(NULL);
 | 
				
			||||||
		err = bpf_prog_load(file, BPF_PROG_TYPE_SOCKET_FILTER,
 | 
							err = bpf_prog_load(file, BPF_PROG_TYPE_SOCKET_FILTER,
 | 
				
			||||||
				    &objs[i], &prog_fds[i]);
 | 
									    &objs[i], &prog_fds[i]);
 | 
				
			||||||
		/* test_obj_id.o is a dumb prog. It should never fail
 | 
							/* test_obj_id.o is a dumb prog. It should never fail
 | 
				
			||||||
| 
						 | 
					@ -316,37 +330,7 @@ static void test_bpf_obj_id(void)
 | 
				
			||||||
			error_cnt++;
 | 
								error_cnt++;
 | 
				
			||||||
		assert(!err);
 | 
							assert(!err);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		/* Check getting prog info */
 | 
							/* Insert a magic value to the map */
 | 
				
			||||||
		info_len = sizeof(struct bpf_prog_info) * 2;
 | 
					 | 
				
			||||||
		bzero(&prog_infos[i], info_len);
 | 
					 | 
				
			||||||
		bzero(jited_insns, sizeof(jited_insns));
 | 
					 | 
				
			||||||
		bzero(xlated_insns, sizeof(xlated_insns));
 | 
					 | 
				
			||||||
		prog_infos[i].jited_prog_insns = ptr_to_u64(jited_insns);
 | 
					 | 
				
			||||||
		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);
 | 
					 | 
				
			||||||
		err = bpf_obj_get_info_by_fd(prog_fds[i], &prog_infos[i],
 | 
					 | 
				
			||||||
					     &info_len);
 | 
					 | 
				
			||||||
		if (CHECK(err ||
 | 
					 | 
				
			||||||
			  prog_infos[i].type != BPF_PROG_TYPE_SOCKET_FILTER ||
 | 
					 | 
				
			||||||
			  info_len != sizeof(struct bpf_prog_info) ||
 | 
					 | 
				
			||||||
			  (jit_enabled && !prog_infos[i].jited_prog_len) ||
 | 
					 | 
				
			||||||
			  (jit_enabled &&
 | 
					 | 
				
			||||||
			   !memcmp(jited_insns, zeros, sizeof(zeros))) ||
 | 
					 | 
				
			||||||
			  !prog_infos[i].xlated_prog_len ||
 | 
					 | 
				
			||||||
			  !memcmp(xlated_insns, zeros, sizeof(zeros)),
 | 
					 | 
				
			||||||
			  "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, errno, i,
 | 
					 | 
				
			||||||
			  prog_infos[i].type, BPF_PROG_TYPE_SOCKET_FILTER,
 | 
					 | 
				
			||||||
			  info_len, sizeof(struct bpf_prog_info),
 | 
					 | 
				
			||||||
			  jit_enabled,
 | 
					 | 
				
			||||||
			  prog_infos[i].jited_prog_len,
 | 
					 | 
				
			||||||
			  prog_infos[i].xlated_prog_len,
 | 
					 | 
				
			||||||
			  !!memcmp(jited_insns, zeros, sizeof(zeros)),
 | 
					 | 
				
			||||||
			  !!memcmp(xlated_insns, zeros, sizeof(zeros))))
 | 
					 | 
				
			||||||
			goto done;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
		map_fds[i] = bpf_find_map(__func__, objs[i], "test_map_id");
 | 
							map_fds[i] = bpf_find_map(__func__, objs[i], "test_map_id");
 | 
				
			||||||
		assert(map_fds[i] >= 0);
 | 
							assert(map_fds[i] >= 0);
 | 
				
			||||||
		err = bpf_map_update_elem(map_fds[i], &array_key,
 | 
							err = bpf_map_update_elem(map_fds[i], &array_key,
 | 
				
			||||||
| 
						 | 
					@ -364,16 +348,67 @@ static void test_bpf_obj_id(void)
 | 
				
			||||||
			  map_infos[i].value_size != sizeof(__u64) ||
 | 
								  map_infos[i].value_size != sizeof(__u64) ||
 | 
				
			||||||
			  map_infos[i].max_entries != 1 ||
 | 
								  map_infos[i].max_entries != 1 ||
 | 
				
			||||||
			  map_infos[i].map_flags != 0 ||
 | 
								  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)",
 | 
								  "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,
 | 
								  err, errno,
 | 
				
			||||||
			  map_infos[i].type, BPF_MAP_TYPE_ARRAY,
 | 
								  map_infos[i].type, BPF_MAP_TYPE_ARRAY,
 | 
				
			||||||
			  info_len, sizeof(struct bpf_map_info),
 | 
								  info_len, sizeof(struct bpf_map_info),
 | 
				
			||||||
			  map_infos[i].key_size,
 | 
								  map_infos[i].key_size,
 | 
				
			||||||
			  map_infos[i].value_size,
 | 
								  map_infos[i].value_size,
 | 
				
			||||||
			  map_infos[i].max_entries,
 | 
								  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 */
 | 
				
			||||||
 | 
							info_len = sizeof(struct bpf_prog_info) * 2;
 | 
				
			||||||
 | 
							bzero(&prog_infos[i], info_len);
 | 
				
			||||||
 | 
							bzero(jited_insns, sizeof(jited_insns));
 | 
				
			||||||
 | 
							bzero(xlated_insns, sizeof(xlated_insns));
 | 
				
			||||||
 | 
							prog_infos[i].jited_prog_insns = ptr_to_u64(jited_insns);
 | 
				
			||||||
 | 
							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) ||
 | 
				
			||||||
 | 
								  (jit_enabled && !prog_infos[i].jited_prog_len) ||
 | 
				
			||||||
 | 
								  (jit_enabled &&
 | 
				
			||||||
 | 
								   !memcmp(jited_insns, zeros, sizeof(zeros))) ||
 | 
				
			||||||
 | 
								  !prog_infos[i].xlated_prog_len ||
 | 
				
			||||||
 | 
								  !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 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),
 | 
				
			||||||
 | 
								  jit_enabled,
 | 
				
			||||||
 | 
								  prog_infos[i].jited_prog_len,
 | 
				
			||||||
 | 
								  prog_infos[i].xlated_prog_len,
 | 
				
			||||||
 | 
								  !!memcmp(jited_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;
 | 
								goto done;
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -382,6 +417,7 @@ static void test_bpf_obj_id(void)
 | 
				
			||||||
	next_id = 0;
 | 
						next_id = 0;
 | 
				
			||||||
	while (!bpf_prog_get_next_id(next_id, &next_id)) {
 | 
						while (!bpf_prog_get_next_id(next_id, &next_id)) {
 | 
				
			||||||
		struct bpf_prog_info prog_info = {};
 | 
							struct bpf_prog_info prog_info = {};
 | 
				
			||||||
 | 
							__u32 saved_map_id;
 | 
				
			||||||
		int prog_fd;
 | 
							int prog_fd;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		info_len = sizeof(prog_info);
 | 
							info_len = sizeof(prog_info);
 | 
				
			||||||
| 
						 | 
					@ -404,16 +440,33 @@ static void test_bpf_obj_id(void)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		nr_id_found++;
 | 
							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);
 | 
							err = bpf_obj_get_info_by_fd(prog_fd, &prog_info, &info_len);
 | 
				
			||||||
		prog_infos[i].jited_prog_insns = 0;
 | 
							prog_infos[i].jited_prog_insns = 0;
 | 
				
			||||||
		prog_infos[i].xlated_prog_insns = 0;
 | 
							prog_infos[i].xlated_prog_insns = 0;
 | 
				
			||||||
		CHECK(err || info_len != sizeof(struct bpf_prog_info) ||
 | 
							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)",
 | 
							      "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),
 | 
							      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);
 | 
							close(prog_fd);
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	CHECK(nr_id_found != nr_iters,
 | 
						CHECK(nr_id_found != nr_iters,
 | 
				
			||||||
| 
						 | 
					@ -495,6 +548,75 @@ static void test_pkt_md_access(void)
 | 
				
			||||||
	bpf_object__close(obj);
 | 
						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)
 | 
					int main(void)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
	struct rlimit rinf = { RLIM_INFINITY, RLIM_INFINITY };
 | 
						struct rlimit rinf = { RLIM_INFINITY, RLIM_INFINITY };
 | 
				
			||||||
| 
						 | 
					@ -507,6 +629,7 @@ int main(void)
 | 
				
			||||||
	test_tcp_estats();
 | 
						test_tcp_estats();
 | 
				
			||||||
	test_bpf_obj_id();
 | 
						test_bpf_obj_id();
 | 
				
			||||||
	test_pkt_md_access();
 | 
						test_pkt_md_access();
 | 
				
			||||||
 | 
						test_obj_name();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	printf("Summary: %d PASSED, %d FAILED\n", pass_cnt, error_cnt);
 | 
						printf("Summary: %d PASSED, %d FAILED\n", pass_cnt, error_cnt);
 | 
				
			||||||
	return error_cnt ? EXIT_FAILURE : EXIT_SUCCESS;
 | 
						return error_cnt ? EXIT_FAILURE : EXIT_SUCCESS;
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -6939,7 +6939,7 @@ static int create_map_in_map(void)
 | 
				
			||||||
		return inner_map_fd;
 | 
							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);
 | 
										     sizeof(int), inner_map_fd, 1, 0);
 | 
				
			||||||
	if (outer_map_fd < 0)
 | 
						if (outer_map_fd < 0)
 | 
				
			||||||
		printf("Failed to create array of maps '%s'!\n",
 | 
							printf("Failed to create array of maps '%s'!\n",
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in a new issue