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
107107 /* BTF name and id of struct allocated by map_alloc */
108108 const char * const map_btf_name;
109109 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;
110113 };
111114
112115 struct bpf_map_memory {
@@ -1207,12 +1210,18 @@ int bpf_obj_get_user(const char __user *pathname, int flags); · 1094 unmodified lines
12071210 int __init bpf_iter_ ## target(args) { return 0; }
12081211
12091212 struct bpf_iter_aux_info {
1213+ struct bpf_map *map;
12101214 };
12111215
1216+typedef int (*bpf_iter_check_target_t)(struct bpf_prog *prog,
1217+ struct bpf_iter_aux_info *aux);
1218+
12121219 #define BPF_ITER_CTX_ARG_MAX 2
12131220 struct bpf_iter_reg {
12141221 const char *target;
1222+ bpf_iter_check_target_t check_target;
12151223 u32 ctx_arg_info_size;
1224+ enum bpf_iter_link_info req_linfo;
12161225 struct bpf_ctx_arg_aux ctx_arg_info[BPF_ITER_CTX_ARG_MAX];
12171226 const struct bpf_iter_seq_info *seq_info;
12181227 };
@@ -1223,6 +1232,13 @@ struct bpf_iter_meta { · 4 unmodified lines
12231232 u64 seq_num;
12241233 };
12251234
1235+struct 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+
12261242 int bpf_iter_reg_target(const struct bpf_iter_reg *reg_info);
12271243 void bpf_iter_unreg_target(const struct bpf_iter_reg *reg_info);
12281244 bool 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