The target can call bpf_iter_reg_target() to register itself. The needed information: target: target name seq_ops: the seq_file operations for the target init_seq_private target callback to initialize seq_priv during file open fini_seq_private target callback to clean up seq_priv during file release seq_priv_size: the private_data size needed by the seq_file operations The target name represents a target which provides a seq_ops for iterating objects. The target can provide two callback functions, init_seq_private and fini_seq_private, called during file open/release time. For example, /proc/net/{tcp6, ipv6_route, netlink, ...}, net name space needs to be setup properly during file open and released properly during file release. Function bpf_iter_unreg_target() is also implemented to unregister a particular target.
Unified · Split
| @@ -31,6 +31,7 @@ struct seq_file; · 30 unmodified lines | ||
| 31 | 31 | struct btf; |
| 32 | 32 | struct btf_type; |
| 33 | 33 | struct exception_table_entry; |
| 34 | +struct seq_operations; | |
| 34 | 35 | |
| 35 | 36 | extern struct idr btf_idr; |
| 36 | 37 | extern spinlock_t btf_idr_lock; |
| @@ -1126,6 +1127,20 @@ struct bpf_link *bpf_link_get_from_fd(u32 ufd); · 1089 unmodified lines | ||
| 1126 | 1127 | int bpf_obj_pin_user(u32 ufd, const char __user *pathname); |
| 1127 | 1128 | int bpf_obj_get_user(const char __user *pathname, int flags); |
| 1128 | 1129 | |
| 1130 | +typedef int (*bpf_iter_init_seq_priv_t)(void *private_data); | |
| 1131 | +typedef void (*bpf_iter_fini_seq_priv_t)(void *private_data); | |
| 1132 | + | |
| 1133 | +struct bpf_iter_reg { | |
| 1134 | + const char *target; | |
| 1135 | + const struct seq_operations *seq_ops; | |
| 1136 | + bpf_iter_init_seq_priv_t init_seq_private; | |
| 1137 | + bpf_iter_fini_seq_priv_t fini_seq_private; | |
| 1138 | + u32 seq_priv_size; | |
| 1139 | +}; | |
| 1140 | + | |
| 1141 | +int bpf_iter_reg_target(struct bpf_iter_reg *reg_info); | |
| 1142 | +void bpf_iter_unreg_target(const char *target); | |
| 1143 | + | |
| 1129 | 1144 | int bpf_percpu_hash_copy(struct bpf_map *map, void *key, void *value); |
| 1130 | 1145 | int bpf_percpu_array_copy(struct bpf_map *map, void *key, void *value); |
| 1131 | 1146 | int bpf_percpu_hash_update(struct bpf_map *map, void *key, void *value, |
| @@ -2,7 +2,7 @@ · 1 unmodified line | ||
| 2 | 2 | obj-y := core.o |
| 3 | 3 | CFLAGS_core.o += $(call cc-disable-warning, override-init) |
| 4 | 4 | |
| 5 | −obj-$(CONFIG_BPF_SYSCALL) += syscall.o verifier.o inode.o helpers.o tnum.o | |
| 5 | +obj-$(CONFIG_BPF_SYSCALL) += syscall.o verifier.o inode.o helpers.o tnum.o bpf_iter.o | |
| 6 | 6 | obj-$(CONFIG_BPF_SYSCALL) += hashtab.o arraymap.o percpu_freelist.o bpf_lru_list.o lpm_trie.o map_in_map.o |
| 7 | 7 | obj-$(CONFIG_BPF_SYSCALL) += local_storage.o queue_stack_maps.o |
| 8 | 8 | obj-$(CONFIG_BPF_SYSCALL) += disasm.o |
| @@ -0,0 +1,59 @@ | ||
| 1 | +// SPDX-License-Identifier: GPL-2.0-only | |
| 2 | +/* Copyright (c) 2020 Facebook */ | |
| 3 | + | |
| 4 | +#include <linux/fs.h> | |
| 5 | +#include <linux/filter.h> | |
| 6 | +#include <linux/bpf.h> | |
| 7 | + | |
| 8 | +struct bpf_iter_target_info { | |
| 9 | + struct list_head list; | |
| 10 | + const char *target; | |
| 11 | + const struct seq_operations *seq_ops; | |
| 12 | + bpf_iter_init_seq_priv_t init_seq_private; | |
| 13 | + bpf_iter_fini_seq_priv_t fini_seq_private; | |
| 14 | + u32 seq_priv_size; | |
| 15 | +}; | |
| 16 | + | |
| 17 | +static struct list_head targets = LIST_HEAD_INIT(targets); | |
| 18 | +static DEFINE_MUTEX(targets_mutex); | |
| 19 | + | |
| 20 | +int bpf_iter_reg_target(struct bpf_iter_reg *reg_info) | |
| 21 | +{ | |
| 22 | + struct bpf_iter_target_info *tinfo; | |
| 23 | + | |
| 24 | + tinfo = kmalloc(sizeof(*tinfo), GFP_KERNEL); | |
| 25 | + if (!tinfo) | |
| 26 | + return -ENOMEM; | |
| 27 | + | |
| 28 | + tinfo->target = reg_info->target; | |
| 29 | + tinfo->seq_ops = reg_info->seq_ops; | |
| 30 | + tinfo->init_seq_private = reg_info->init_seq_private; | |
| 31 | + tinfo->fini_seq_private = reg_info->fini_seq_private; | |
| 32 | + tinfo->seq_priv_size = reg_info->seq_priv_size; | |
| 33 | + INIT_LIST_HEAD(&tinfo->list); | |
| 34 | + | |
| 35 | + mutex_lock(&targets_mutex); | |
| 36 | + list_add(&tinfo->list, &targets); | |
| 37 | + mutex_unlock(&targets_mutex); | |
| 38 | + | |
| 39 | + return 0; | |
| 40 | +} | |
| 41 | + | |
| 42 | +void bpf_iter_unreg_target(const char *target) | |
| 43 | +{ | |
| 44 | + struct bpf_iter_target_info *tinfo; | |
| 45 | + bool found = false; | |
| 46 | + | |
| 47 | + mutex_lock(&targets_mutex); | |
| 48 | + list_for_each_entry(tinfo, &targets, list) { | |
| 49 | + if (!strcmp(target, tinfo->target)) { | |
| 50 | + list_del(&tinfo->list); | |
| 51 | + kfree(tinfo); | |
| 52 | + found = true; | |
| 53 | + break; | |
| 54 | + } | |
| 55 | + } | |
| 56 | + mutex_unlock(&targets_mutex); | |
| 57 | + | |
| 58 | + WARN_ON(found == false); | |
| 59 | +} | |