1 /* SPDX-License-Identifier: GPL-2.0 */
2 /* Copyright (c) 2023 Isovalent */
8 /* bpf_mprog framework:
10 * bpf_mprog is a generic layer for multi-program attachment. In-kernel users
11 * of the bpf_mprog don't need to care about the dependency resolution
12 * internals, they can just consume it with few API calls. Currently available
13 * dependency directives are BPF_F_{BEFORE,AFTER} which enable insertion of
14 * a BPF program or BPF link relative to an existing BPF program or BPF link
15 * inside the multi-program array as well as prepend and append behavior if
16 * no relative object was specified, see corresponding selftests for concrete
17 * examples (e.g. tc_links and tc_opts test cases of test_progs).
19 * Usage of bpf_mprog_{attach,detach,query}() core APIs with pseudo code:
23 * struct bpf_mprog_entry *entry, *entry_new;
26 * // bpf_mprog user-side lock
27 * // fetch active @entry from attach location
29 * ret = bpf_mprog_attach(entry, &entry_new, [...]);
31 * if (entry != entry_new) {
32 * // swap @entry to @entry_new at attach location
33 * // ensure there are no inflight users of @entry:
36 * bpf_mprog_commit(entry);
38 * // error path, bail out, propagate @ret
40 * // bpf_mprog user-side unlock
44 * struct bpf_mprog_entry *entry, *entry_new;
47 * // bpf_mprog user-side lock
48 * // fetch active @entry from attach location
50 * ret = bpf_mprog_detach(entry, &entry_new, [...]);
52 * // all (*) marked is optional and depends on the use-case
53 * // whether bpf_mprog_bundle should be freed or not
54 * if (!bpf_mprog_total(entry_new)) (*)
55 * entry_new = NULL (*)
56 * // swap @entry to @entry_new at attach location
57 * // ensure there are no inflight users of @entry:
59 * bpf_mprog_commit(entry);
61 * // free bpf_mprog_bundle (*)
63 * // error path, bail out, propagate @ret
65 * // bpf_mprog user-side unlock
69 * struct bpf_mprog_entry *entry;
72 * // bpf_mprog user-side lock
73 * // fetch active @entry from attach location
75 * ret = bpf_mprog_query(attr, uattr, entry);
76 * // bpf_mprog user-side unlock
80 * struct bpf_mprog_entry *entry;
81 * struct bpf_mprog_fp *fp;
82 * struct bpf_prog *prog;
86 * // fetch active @entry from attach location
88 * bpf_mprog_foreach_prog(entry, fp, prog) {
89 * ret = bpf_prog_run(prog, [...]);
90 * // process @ret from program
95 * bpf_mprog locking considerations:
97 * bpf_mprog_{attach,detach,query}() must be protected by an external lock
98 * (like RTNL in case of tcx).
100 * bpf_mprog_entry pointer can be an __rcu annotated pointer (in case of tcx
101 * the netdevice has tcx_ingress and tcx_egress __rcu pointer) which gets
102 * updated via rcu_assign_pointer() pointing to the active bpf_mprog_entry of
103 * the bpf_mprog_bundle.
105 * Fast path accesses the active bpf_mprog_entry within RCU critical section
106 * (in case of tcx it runs in NAPI which provides RCU protection there,
107 * other users might need explicit rcu_read_lock()). The bpf_mprog_commit()
108 * assumes that for the old bpf_mprog_entry there are no inflight users
111 * The READ_ONCE()/WRITE_ONCE() pairing for bpf_mprog_fp's prog access is for
112 * the replacement case where we don't swap the bpf_mprog_entry.
115 #define bpf_mprog_foreach_tuple(entry, fp, cp, t) \
116 for (fp = &entry->fp_items[0], cp = &entry->parent->cp_items[0];\
118 t.prog = READ_ONCE(fp->prog); \
124 #define bpf_mprog_foreach_prog(entry, fp, p) \
125 for (fp = &entry->fp_items[0]; \
126 (p = READ_ONCE(fp->prog)); \
129 #define BPF_MPROG_MAX 64
131 struct bpf_mprog_fp
{
132 struct bpf_prog
*prog
;
135 struct bpf_mprog_cp
{
136 struct bpf_link
*link
;
139 struct bpf_mprog_entry
{
140 struct bpf_mprog_fp fp_items
[BPF_MPROG_MAX
];
141 struct bpf_mprog_bundle
*parent
;
144 struct bpf_mprog_bundle
{
145 struct bpf_mprog_entry a
;
146 struct bpf_mprog_entry b
;
147 struct bpf_mprog_cp cp_items
[BPF_MPROG_MAX
];
148 struct bpf_prog
*ref
;
154 struct bpf_prog
*prog
;
155 struct bpf_link
*link
;
158 static inline struct bpf_mprog_entry
*
159 bpf_mprog_peer(const struct bpf_mprog_entry
*entry
)
161 if (entry
== &entry
->parent
->a
)
162 return &entry
->parent
->b
;
164 return &entry
->parent
->a
;
167 static inline void bpf_mprog_bundle_init(struct bpf_mprog_bundle
*bundle
)
169 BUILD_BUG_ON(sizeof(bundle
->a
.fp_items
[0]) > sizeof(u64
));
170 BUILD_BUG_ON(ARRAY_SIZE(bundle
->a
.fp_items
) !=
171 ARRAY_SIZE(bundle
->cp_items
));
173 memset(bundle
, 0, sizeof(*bundle
));
174 atomic64_set(&bundle
->revision
, 1);
175 bundle
->a
.parent
= bundle
;
176 bundle
->b
.parent
= bundle
;
179 static inline void bpf_mprog_inc(struct bpf_mprog_entry
*entry
)
181 entry
->parent
->count
++;
184 static inline void bpf_mprog_dec(struct bpf_mprog_entry
*entry
)
186 entry
->parent
->count
--;
189 static inline int bpf_mprog_max(void)
191 return ARRAY_SIZE(((struct bpf_mprog_entry
*)NULL
)->fp_items
) - 1;
194 static inline int bpf_mprog_total(struct bpf_mprog_entry
*entry
)
196 int total
= entry
->parent
->count
;
198 WARN_ON_ONCE(total
> bpf_mprog_max());
202 static inline bool bpf_mprog_exists(struct bpf_mprog_entry
*entry
,
203 struct bpf_prog
*prog
)
205 const struct bpf_mprog_fp
*fp
;
206 const struct bpf_prog
*tmp
;
208 bpf_mprog_foreach_prog(entry
, fp
, tmp
) {
215 static inline void bpf_mprog_mark_for_release(struct bpf_mprog_entry
*entry
,
216 struct bpf_tuple
*tuple
)
218 WARN_ON_ONCE(entry
->parent
->ref
);
220 entry
->parent
->ref
= tuple
->prog
;
223 static inline void bpf_mprog_complete_release(struct bpf_mprog_entry
*entry
)
225 /* In the non-link case prog deletions can only drop the reference
226 * to the prog after the bpf_mprog_entry got swapped and the
227 * bpf_mprog ensured that there are no inflight users anymore.
229 * Paired with bpf_mprog_mark_for_release().
231 if (entry
->parent
->ref
) {
232 bpf_prog_put(entry
->parent
->ref
);
233 entry
->parent
->ref
= NULL
;
237 static inline void bpf_mprog_revision_new(struct bpf_mprog_entry
*entry
)
239 atomic64_inc(&entry
->parent
->revision
);
242 static inline void bpf_mprog_commit(struct bpf_mprog_entry
*entry
)
244 bpf_mprog_complete_release(entry
);
245 bpf_mprog_revision_new(entry
);
248 static inline u64
bpf_mprog_revision(struct bpf_mprog_entry
*entry
)
250 return atomic64_read(&entry
->parent
->revision
);
253 static inline void bpf_mprog_entry_copy(struct bpf_mprog_entry
*dst
,
254 struct bpf_mprog_entry
*src
)
256 memcpy(dst
->fp_items
, src
->fp_items
, sizeof(src
->fp_items
));
259 static inline void bpf_mprog_entry_clear(struct bpf_mprog_entry
*dst
)
261 memset(dst
->fp_items
, 0, sizeof(dst
->fp_items
));
264 static inline void bpf_mprog_clear_all(struct bpf_mprog_entry
*entry
,
265 struct bpf_mprog_entry
**entry_new
)
267 struct bpf_mprog_entry
*peer
;
269 peer
= bpf_mprog_peer(entry
);
270 bpf_mprog_entry_clear(peer
);
271 peer
->parent
->count
= 0;
275 static inline void bpf_mprog_entry_grow(struct bpf_mprog_entry
*entry
, int idx
)
277 int total
= bpf_mprog_total(entry
);
279 memmove(entry
->fp_items
+ idx
+ 1,
280 entry
->fp_items
+ idx
,
281 (total
- idx
) * sizeof(struct bpf_mprog_fp
));
283 memmove(entry
->parent
->cp_items
+ idx
+ 1,
284 entry
->parent
->cp_items
+ idx
,
285 (total
- idx
) * sizeof(struct bpf_mprog_cp
));
288 static inline void bpf_mprog_entry_shrink(struct bpf_mprog_entry
*entry
, int idx
)
290 /* Total array size is needed in this case to enure the NULL
291 * entry is copied at the end.
293 int total
= ARRAY_SIZE(entry
->fp_items
);
295 memmove(entry
->fp_items
+ idx
,
296 entry
->fp_items
+ idx
+ 1,
297 (total
- idx
- 1) * sizeof(struct bpf_mprog_fp
));
299 memmove(entry
->parent
->cp_items
+ idx
,
300 entry
->parent
->cp_items
+ idx
+ 1,
301 (total
- idx
- 1) * sizeof(struct bpf_mprog_cp
));
304 static inline void bpf_mprog_read(struct bpf_mprog_entry
*entry
, u32 idx
,
305 struct bpf_mprog_fp
**fp
,
306 struct bpf_mprog_cp
**cp
)
308 *fp
= &entry
->fp_items
[idx
];
309 *cp
= &entry
->parent
->cp_items
[idx
];
312 static inline void bpf_mprog_write(struct bpf_mprog_fp
*fp
,
313 struct bpf_mprog_cp
*cp
,
314 struct bpf_tuple
*tuple
)
316 WRITE_ONCE(fp
->prog
, tuple
->prog
);
317 cp
->link
= tuple
->link
;
320 int bpf_mprog_attach(struct bpf_mprog_entry
*entry
,
321 struct bpf_mprog_entry
**entry_new
,
322 struct bpf_prog
*prog_new
, struct bpf_link
*link
,
323 struct bpf_prog
*prog_old
,
324 u32 flags
, u32 id_or_fd
, u64 revision
);
326 int bpf_mprog_detach(struct bpf_mprog_entry
*entry
,
327 struct bpf_mprog_entry
**entry_new
,
328 struct bpf_prog
*prog
, struct bpf_link
*link
,
329 u32 flags
, u32 id_or_fd
, u64 revision
);
331 int bpf_mprog_query(const union bpf_attr
*attr
, union bpf_attr __user
*uattr
,
332 struct bpf_mprog_entry
*entry
);
334 static inline bool bpf_mprog_supported(enum bpf_prog_type type
)
337 case BPF_PROG_TYPE_SCHED_CLS
:
343 #endif /* __BPF_MPROG_H */