History

bpf: Implement bpf iterator for map elements

The bpf iterator for map elements are implemented. The bpf program will receive four parameters: bpf_iter_meta *meta: the meta data bpf_map *map: the bpf_map whose elements are traversed void *key: the key of one element void *value: the value of the same element Here, meta and map pointers are always valid, and key has register type PTR_TO_RDONLY_BUF_OR_NULL and value has register type PTR_TO_RDWR_BUF_OR_NULL. The kernel will track the access range of key and value during verification time. Later, these values will be compared against the values in the actual map to ensure all accesses are within range. A new field iter_seq_info is added to bpf_map_ops which is used to add map type specific information, i.e., seq_ops, init/fini seq_file func and seq_file private data size. Subsequent patches will have actual implementation for bpf_map_ops->iter_seq_info. In user space, BPF_ITER_LINK_MAP_FD needs to be specified in prog attr->link_create.flags, which indicates that attr->link_create.target_fd is a map_fd. The reason for such an explicit flag is for possible future cases where one bpf iterator may allow more than one possible customization, e.g., pid and cgroup id for task_file. Current kernel internal implementation only allows the target to register at most one required bpf_iter_link_info. To support the above case, optional bpf_iter_link_info's are needed, the target can be extended to register such link infos, and user provided link_info needs to match one of target supported ones.

Yonghong Song committed 6 years ago

Commit a5cbe051 parent afbf21d5 files · +128 −17Browse files

Showing the changes to include/linux (1 of 5 files). Show all

Unified · Split

include/linux/bpf.h+16−0
@@ -107,6 +107,9 @@ struct bpf_map_ops { · 106 unmodified lines
107 /* BTF name and id of struct allocated by map_alloc */107 /* BTF name and id of struct allocated by map_alloc */
108 const char * const map_btf_name;108 const char * const map_btf_name;
109 int *map_btf_id;109 int *map_btf_id;
110
111 /* bpf_iter info used to open a seq_file */
112 const struct bpf_iter_seq_info *iter_seq_info;
110};113};
111 114
112struct bpf_map_memory {115struct bpf_map_memory {
@@ -1207,12 +1210,18 @@ int bpf_obj_get_user(const char __user *pathname, int flags); · 1094 unmodified lines
1207 int __init bpf_iter_ ## target(args) { return 0; }1210 int __init bpf_iter_ ## target(args) { return 0; }
1208 1211
1209struct bpf_iter_aux_info {1212struct bpf_iter_aux_info {
1213 struct bpf_map *map;
1210};1214};
1211 1215
1216typedef int (*bpf_iter_check_target_t)(struct bpf_prog *prog,
1217 struct bpf_iter_aux_info *aux);
1218
1212#define BPF_ITER_CTX_ARG_MAX 21219#define BPF_ITER_CTX_ARG_MAX 2
1213struct bpf_iter_reg {1220struct bpf_iter_reg {
1214 const char *target;1221 const char *target;
1222 bpf_iter_check_target_t check_target;
1215 u32 ctx_arg_info_size;1223 u32 ctx_arg_info_size;
1224 enum bpf_iter_link_info req_linfo;
1216 struct bpf_ctx_arg_aux ctx_arg_info[BPF_ITER_CTX_ARG_MAX];1225 struct bpf_ctx_arg_aux ctx_arg_info[BPF_ITER_CTX_ARG_MAX];
1217 const struct bpf_iter_seq_info *seq_info;1226 const struct bpf_iter_seq_info *seq_info;
1218};1227};
@@ -1223,6 +1232,13 @@ struct bpf_iter_meta { · 4 unmodified lines
1223 u64 seq_num;1232 u64 seq_num;
1224};1233};
1225 1234
1235struct bpf_iter__bpf_map_elem {
1236 __bpf_md_ptr(struct bpf_iter_meta *, meta);
1237 __bpf_md_ptr(struct bpf_map *, map);
1238 __bpf_md_ptr(void *, key);
1239 __bpf_md_ptr(void *, value);
1240};
1241
1226int bpf_iter_reg_target(const struct bpf_iter_reg *reg_info);1242int bpf_iter_reg_target(const struct bpf_iter_reg *reg_info);
1227void bpf_iter_unreg_target(const struct bpf_iter_reg *reg_info);1243void bpf_iter_unreg_target(const struct bpf_iter_reg *reg_info);
1228bool bpf_iter_prog_supported(struct bpf_prog *prog);1244bool bpf_iter_prog_supported(struct bpf_prog *prog);

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t