Commit | Line | Data |
---|---|---|
6086d29d YS |
1 | // SPDX-License-Identifier: GPL-2.0-only |
2 | /* Copyright (c) 2020 Facebook */ | |
3 | #include <linux/bpf.h> | |
4 | #include <linux/fs.h> | |
5 | #include <linux/filter.h> | |
6 | #include <linux/kernel.h> | |
7 | ||
8 | struct bpf_iter_seq_map_info { | |
9 | u32 mid; | |
10 | }; | |
11 | ||
12 | static void *bpf_map_seq_start(struct seq_file *seq, loff_t *pos) | |
13 | { | |
14 | struct bpf_iter_seq_map_info *info = seq->private; | |
15 | struct bpf_map *map; | |
16 | ||
17 | map = bpf_map_get_curr_or_next(&info->mid); | |
18 | if (!map) | |
19 | return NULL; | |
20 | ||
21 | ++*pos; | |
22 | return map; | |
23 | } | |
24 | ||
25 | static void *bpf_map_seq_next(struct seq_file *seq, void *v, loff_t *pos) | |
26 | { | |
27 | struct bpf_iter_seq_map_info *info = seq->private; | |
28 | struct bpf_map *map; | |
29 | ||
30 | ++*pos; | |
31 | ++info->mid; | |
32 | bpf_map_put((struct bpf_map *)v); | |
33 | map = bpf_map_get_curr_or_next(&info->mid); | |
34 | if (!map) | |
35 | return NULL; | |
36 | ||
37 | return map; | |
38 | } | |
39 | ||
40 | struct bpf_iter__bpf_map { | |
41 | __bpf_md_ptr(struct bpf_iter_meta *, meta); | |
42 | __bpf_md_ptr(struct bpf_map *, map); | |
43 | }; | |
44 | ||
45 | DEFINE_BPF_ITER_FUNC(bpf_map, struct bpf_iter_meta *meta, struct bpf_map *map) | |
46 | ||
47 | static int __bpf_map_seq_show(struct seq_file *seq, void *v, bool in_stop) | |
48 | { | |
49 | struct bpf_iter__bpf_map ctx; | |
50 | struct bpf_iter_meta meta; | |
51 | struct bpf_prog *prog; | |
52 | int ret = 0; | |
53 | ||
54 | ctx.meta = &meta; | |
55 | ctx.map = v; | |
56 | meta.seq = seq; | |
57 | prog = bpf_iter_get_info(&meta, in_stop); | |
58 | if (prog) | |
59 | ret = bpf_iter_run_prog(prog, &ctx); | |
60 | ||
61 | return ret; | |
62 | } | |
63 | ||
64 | static int bpf_map_seq_show(struct seq_file *seq, void *v) | |
65 | { | |
66 | return __bpf_map_seq_show(seq, v, false); | |
67 | } | |
68 | ||
69 | static void bpf_map_seq_stop(struct seq_file *seq, void *v) | |
70 | { | |
71 | if (!v) | |
72 | (void)__bpf_map_seq_show(seq, v, true); | |
73 | else | |
74 | bpf_map_put((struct bpf_map *)v); | |
75 | } | |
76 | ||
77 | static const struct seq_operations bpf_map_seq_ops = { | |
78 | .start = bpf_map_seq_start, | |
79 | .next = bpf_map_seq_next, | |
80 | .stop = bpf_map_seq_stop, | |
81 | .show = bpf_map_seq_show, | |
82 | }; | |
83 | ||
15172a46 YS |
84 | static const struct bpf_iter_reg bpf_map_reg_info = { |
85 | .target = "bpf_map", | |
86 | .seq_ops = &bpf_map_seq_ops, | |
87 | .init_seq_private = NULL, | |
88 | .fini_seq_private = NULL, | |
89 | .seq_priv_size = sizeof(struct bpf_iter_seq_map_info), | |
90 | }; | |
91 | ||
6086d29d YS |
92 | static int __init bpf_map_iter_init(void) |
93 | { | |
15172a46 | 94 | return bpf_iter_reg_target(&bpf_map_reg_info); |
6086d29d YS |
95 | } |
96 | ||
97 | late_initcall(bpf_map_iter_init); |