Branch data Line data Source code
1 : : /* SPDX-License-Identifier: GPL-2.0-only */
2 : : /*
3 : : * Copyright (c) 2023 Meta Platforms, Inc. and affiliates.
4 : : */
5 : :
6 : : #include "cgen/handle.h"
7 : :
8 : : #include <assert.h>
9 : : #include <bpf/libbpf.h>
10 : : #include <errno.h>
11 : : #include <stdlib.h>
12 : : #include <string.h>
13 : : #include <unistd.h>
14 : :
15 : : #include <bpfilter/bpf.h>
16 : : #include <bpfilter/chain.h>
17 : : #include <bpfilter/core/list.h>
18 : : #include <bpfilter/counter.h>
19 : : #include <bpfilter/dump.h>
20 : : #include <bpfilter/helper.h>
21 : : #include <bpfilter/hook.h>
22 : : #include <bpfilter/logger.h>
23 : : #include <bpfilter/pack.h>
24 : : #include <bpfilter/rule.h>
25 : :
26 : : #include "cgen/prog/link.h"
27 : : #include "cgen/prog/map.h"
28 : : #include "core/lock.h"
29 : :
30 : : #define _BF_LINK_NAME "bf_link"
31 : :
32 : 7330 : int bf_handle_new(struct bf_handle **handle, const char *prog_name)
33 : : {
34 : 7330 : _free_bf_handle_ struct bf_handle *_handle = NULL;
35 : :
36 : : assert(handle);
37 : : assert(prog_name);
38 : :
39 : 7330 : _handle = calloc(1, sizeof(*_handle));
40 [ + - ]: 7330 : if (!_handle)
41 : : return -ENOMEM;
42 : :
43 : 7330 : (void)snprintf(_handle->prog_name, BPF_OBJ_NAME_LEN, "%s", prog_name);
44 : 7330 : _handle->prog_fd = -1;
45 : 7330 : _handle->sets = bf_list_default(bf_map_free, bf_map_pack);
46 : :
47 : 7330 : *handle = TAKE_PTR(_handle);
48 : :
49 : 7330 : return 0;
50 : : }
51 : :
52 : 5853 : int bf_handle_new_from_pack(struct bf_handle **handle, struct bf_lock *lock,
53 : : bf_rpack_node_t node)
54 : : {
55 : 5853 : _free_bf_handle_ struct bf_handle *_handle = NULL;
56 : 5853 : _cleanup_free_ char *name = NULL;
57 : : bf_rpack_node_t child, array_node;
58 : : int dir_fd;
59 : : int r;
60 : :
61 : : assert(handle);
62 : : assert(lock);
63 : :
64 : 5853 : dir_fd = lock->chain_fd;
65 : :
66 : 5853 : r = bf_rpack_kv_str(node, "prog_name", &name);
67 [ - + ]: 5853 : if (r)
68 [ # # ]: 0 : return bf_rpack_key_err(r, "bf_handle.name");
69 : :
70 : 5853 : r = bf_handle_new(&_handle, name);
71 [ + - ]: 5853 : if (r)
72 : : return r;
73 : :
74 : 5853 : r = bf_bpf_obj_get(_handle->prog_name, dir_fd, &_handle->prog_fd);
75 [ - + ]: 5853 : if (r < 0)
76 [ # # ]: 0 : return bf_err_r(r, "failed to restore bf_handle.prog_fd from pin");
77 : :
78 : 5853 : r = bf_rpack_kv_node(node, "link", &child);
79 [ - + ]: 5853 : if (r)
80 [ # # ]: 0 : return bf_rpack_key_err(r, "bf_handle.link");
81 [ + + ]: 5853 : if (!bf_rpack_is_nil(child)) {
82 : 196 : r = bf_link_new_from_pack(&_handle->link, dir_fd, child);
83 [ - + ]: 196 : if (r)
84 [ # # ]: 0 : return bf_rpack_key_err(r, "bf_handle.link");
85 : : }
86 : :
87 : 5853 : r = bf_rpack_kv_node(node, "cmap", &child);
88 [ - + ]: 5853 : if (r)
89 [ # # ]: 0 : return bf_rpack_key_err(r, "bf_handle.cmap");
90 [ + - ]: 5853 : if (!bf_rpack_is_nil(child)) {
91 : 5853 : r = bf_map_new_from_pack(&_handle->cmap, dir_fd, child);
92 [ - + ]: 5853 : if (r)
93 [ # # ]: 0 : return bf_rpack_key_err(r, "bf_handle.cmap");
94 : : }
95 : :
96 : 5853 : r = bf_rpack_kv_node(node, "pmap", &child);
97 [ - + ]: 5853 : if (r)
98 [ # # ]: 0 : return bf_rpack_key_err(r, "bf_handle.pmap");
99 [ + - ]: 5853 : if (!bf_rpack_is_nil(child)) {
100 : 5853 : r = bf_map_new_from_pack(&_handle->pmap, dir_fd, child);
101 [ - + ]: 5853 : if (r)
102 [ # # ]: 0 : return bf_rpack_key_err(r, "bf_handle.pmap");
103 : : }
104 : :
105 : 5853 : r = bf_rpack_kv_node(node, "lmap", &child);
106 [ - + ]: 5853 : if (r)
107 [ # # ]: 0 : return bf_rpack_key_err(r, "bf_handle.lmap");
108 [ + + ]: 5853 : if (!bf_rpack_is_nil(child)) {
109 : 65 : r = bf_map_new_from_pack(&_handle->lmap, dir_fd, child);
110 [ - + ]: 65 : if (r)
111 [ # # ]: 0 : return bf_rpack_key_err(r, "bf_handle.lmap");
112 : : }
113 : :
114 : 5853 : r = bf_rpack_kv_node(node, "smap", &child);
115 [ - + ]: 5853 : if (r)
116 [ # # ]: 0 : return bf_rpack_key_err(r, "bf_handle.smap");
117 [ + + ]: 5853 : if (!bf_rpack_is_nil(child)) {
118 : 6 : r = bf_map_new_from_pack(&_handle->smap, dir_fd, child);
119 [ - + ]: 6 : if (r)
120 [ # # ]: 0 : return bf_rpack_key_err(r, "bf_handle.smap");
121 : : }
122 : :
123 : 5853 : r = bf_rpack_kv_array(node, "sets", &child);
124 [ - + ]: 5853 : if (r)
125 [ # # ]: 0 : return bf_rpack_key_err(r, "bf_handle.sets");
126 [ + + - + : 8237 : bf_rpack_array_foreach (child, array_node) {
+ + ]
127 : 0 : _free_bf_map_ struct bf_map *map = NULL;
128 : :
129 [ - + ]: 1192 : if (bf_rpack_is_nil(array_node)) {
130 : 0 : r = bf_list_add_tail(&_handle->sets, NULL);
131 [ # # ]: 0 : if (r)
132 : : return r;
133 : : continue;
134 : : }
135 : :
136 [ + - + - : 1192 : r = bf_list_emplace(&_handle->sets, bf_map_new_from_pack, map, dir_fd,
- - - - ]
137 : : array_node);
138 : : if (r)
139 [ # # ]: 0 : return bf_err_r(r, "failed to unpack bf_map into bf_handle.sets");
140 : : }
141 : :
142 : 5853 : *handle = TAKE_PTR(_handle);
143 : :
144 : 5853 : return 0;
145 : : }
146 : :
147 : 31443 : void bf_handle_free(struct bf_handle **handle)
148 : : {
149 : : assert(handle);
150 : :
151 [ + + ]: 31443 : if (!*handle)
152 : : return;
153 : :
154 : 7330 : closep(&(*handle)->prog_fd);
155 : :
156 : 7330 : bf_link_free(&(*handle)->link);
157 : 7330 : bf_map_free(&(*handle)->cmap);
158 : 7330 : bf_map_free(&(*handle)->pmap);
159 : 7330 : bf_map_free(&(*handle)->lmap);
160 : 7330 : bf_map_free(&(*handle)->smap);
161 : 7330 : bf_list_clean(&(*handle)->sets);
162 : :
163 : 7330 : free(*handle);
164 : 7330 : *handle = NULL;
165 : : }
166 : :
167 : 1506 : int bf_handle_pack(const struct bf_handle *handle, bf_wpack_t *pack)
168 : : {
169 : : assert(handle);
170 : : assert(pack);
171 : :
172 : 1506 : bf_wpack_kv_str(pack, "prog_name", handle->prog_name);
173 : :
174 [ + + ]: 1506 : if (handle->link) {
175 : 188 : bf_wpack_open_object(pack, "link");
176 : 188 : bf_link_pack(handle->link, pack);
177 : 188 : bf_wpack_close_object(pack);
178 : : } else {
179 : : bf_wpack_kv_nil(pack, "link");
180 : : }
181 : :
182 [ + - ]: 1506 : if (handle->cmap) {
183 : 1506 : bf_wpack_open_object(pack, "cmap");
184 : 1506 : bf_map_pack(handle->cmap, pack);
185 : 1506 : bf_wpack_close_object(pack);
186 : : } else {
187 : : bf_wpack_kv_nil(pack, "cmap");
188 : : }
189 : :
190 [ + - ]: 1506 : if (handle->pmap) {
191 : 1506 : bf_wpack_open_object(pack, "pmap");
192 : 1506 : bf_map_pack(handle->pmap, pack);
193 : 1506 : bf_wpack_close_object(pack);
194 : : } else {
195 : : bf_wpack_kv_nil(pack, "pmap");
196 : : }
197 : :
198 [ + + ]: 1506 : if (handle->lmap) {
199 : 49 : bf_wpack_open_object(pack, "lmap");
200 : 49 : bf_map_pack(handle->lmap, pack);
201 : 49 : bf_wpack_close_object(pack);
202 : : } else {
203 : : bf_wpack_kv_nil(pack, "lmap");
204 : : }
205 : :
206 [ + + ]: 1506 : if (handle->smap) {
207 : 3 : bf_wpack_open_object(pack, "smap");
208 : 3 : bf_map_pack(handle->smap, pack);
209 : 3 : bf_wpack_close_object(pack);
210 : : } else {
211 : : bf_wpack_kv_nil(pack, "smap");
212 : : }
213 : :
214 : 1506 : bf_wpack_kv_list(pack, "sets", &handle->sets);
215 : :
216 [ - + ]: 1506 : return bf_wpack_is_valid(pack) ? 0 : -EINVAL;
217 : : }
218 : :
219 : 0 : void bf_handle_dump(const struct bf_handle *handle, prefix_t *prefix)
220 : : {
221 : : assert(handle);
222 : : assert(prefix);
223 : :
224 [ # # ]: 0 : DUMP(prefix, "struct bf_handle at %p", handle);
225 : :
226 : 0 : bf_dump_prefix_push(prefix);
227 : :
228 [ # # ]: 0 : DUMP(prefix, "prog_name: %s", handle->prog_name);
229 [ # # ]: 0 : DUMP(prefix, "prog_fd: %d", handle->prog_fd);
230 : :
231 [ # # ]: 0 : if (handle->link) {
232 [ # # ]: 0 : DUMP(prefix, "link: struct bf_link *");
233 : 0 : bf_dump_prefix_push(prefix);
234 : 0 : bf_link_dump(handle->link, bf_dump_prefix_last(prefix));
235 : 0 : bf_dump_prefix_pop(prefix);
236 : : } else {
237 [ # # ]: 0 : DUMP(prefix, "link: struct bf_link * (NULL)");
238 : : }
239 : :
240 [ # # ]: 0 : if (handle->cmap) {
241 [ # # ]: 0 : DUMP(prefix, "cmap: struct bf_map *");
242 : 0 : bf_dump_prefix_push(prefix);
243 : 0 : bf_map_dump(handle->cmap, bf_dump_prefix_last(prefix));
244 : 0 : bf_dump_prefix_pop(prefix);
245 : : } else {
246 [ # # ]: 0 : DUMP(prefix, "cmap: struct bf_map * (NULL)");
247 : : }
248 : :
249 [ # # ]: 0 : if (handle->pmap) {
250 [ # # ]: 0 : DUMP(prefix, "pmap: struct bf_map *");
251 : 0 : bf_dump_prefix_push(prefix);
252 : 0 : bf_map_dump(handle->pmap, bf_dump_prefix_last(prefix));
253 : 0 : bf_dump_prefix_pop(prefix);
254 : : } else {
255 [ # # ]: 0 : DUMP(prefix, "pmap: struct bf_map * (NULL)");
256 : : }
257 : :
258 [ # # ]: 0 : if (handle->lmap) {
259 [ # # ]: 0 : DUMP(prefix, "lmap: struct bf_map *");
260 : 0 : bf_dump_prefix_push(prefix);
261 : 0 : bf_map_dump(handle->lmap, bf_dump_prefix_last(prefix));
262 : 0 : bf_dump_prefix_pop(prefix);
263 : : } else {
264 [ # # ]: 0 : DUMP(prefix, "lmap: struct bf_map * (NULL)");
265 : : }
266 : :
267 [ # # ]: 0 : if (handle->smap) {
268 [ # # ]: 0 : DUMP(prefix, "smap: struct bf_map *");
269 : 0 : bf_dump_prefix_push(prefix);
270 : 0 : bf_map_dump(handle->smap, bf_dump_prefix_last(prefix));
271 : 0 : bf_dump_prefix_pop(prefix);
272 : : } else {
273 [ # # ]: 0 : DUMP(prefix, "smap: struct bf_map * (NULL)");
274 : : }
275 : :
276 [ # # ]: 0 : DUMP(bf_dump_prefix_last(prefix), "sets: bf_list<bf_map>[%lu]",
277 : : bf_list_size(&handle->sets));
278 : 0 : bf_dump_prefix_push(prefix);
279 [ # # # # : 0 : bf_list_foreach (&handle->sets, map_node) {
# # ]
280 : : struct bf_map *map = bf_list_node_get_data(map_node);
281 : :
282 [ # # ]: 0 : if (bf_list_is_tail(&handle->sets, map_node))
283 : 0 : bf_dump_prefix_last(prefix);
284 : :
285 [ # # ]: 0 : if (!map) {
286 [ # # ]: 0 : DUMP(prefix, "struct bf_map * (NULL)");
287 : 0 : continue;
288 : : }
289 : :
290 : 0 : bf_map_dump(map, prefix);
291 : : }
292 : 0 : bf_dump_prefix_pop(prefix);
293 : :
294 : 0 : bf_dump_prefix_pop(prefix);
295 : 0 : }
296 : :
297 : 1470 : int bf_handle_pin(struct bf_handle *handle, struct bf_lock *lock)
298 : : {
299 : : int dir_fd;
300 : : int r;
301 : :
302 : : assert(handle);
303 : : assert(lock);
304 : :
305 : 1470 : dir_fd = lock->chain_fd;
306 : :
307 : 1470 : r = bf_bpf_obj_pin(handle->prog_name, handle->prog_fd, dir_fd);
308 [ + + ]: 1470 : if (r) {
309 [ + - ]: 2 : bf_err_r(r, "failed to pin BPF program");
310 : 2 : goto err_unpin_all;
311 : : }
312 : :
313 [ + - ]: 1468 : if (handle->cmap) {
314 : 1468 : r = bf_map_pin(handle->cmap, dir_fd);
315 [ - + ]: 1468 : if (r) {
316 [ # # ]: 0 : bf_err_r(r, "failed to pin BPF counters map");
317 : 0 : goto err_unpin_all;
318 : : }
319 : : }
320 : :
321 [ + - ]: 1468 : if (handle->pmap) {
322 : 1468 : r = bf_map_pin(handle->pmap, dir_fd);
323 [ - + ]: 1468 : if (r) {
324 [ # # ]: 0 : bf_err_r(r, "failed to pin BPF printer map");
325 : 0 : goto err_unpin_all;
326 : : }
327 : : }
328 : :
329 [ + + ]: 1468 : if (handle->lmap) {
330 : 42 : r = bf_map_pin(handle->lmap, dir_fd);
331 [ - + ]: 42 : if (r) {
332 [ # # ]: 0 : bf_err_r(r, "failed to pin BPF log map");
333 : 0 : goto err_unpin_all;
334 : : }
335 : : }
336 : :
337 [ + + ]: 1468 : if (handle->smap) {
338 : 3 : r = bf_map_pin(handle->smap, dir_fd);
339 [ - + ]: 3 : if (r) {
340 [ # # ]: 0 : bf_err_r(r, "failed to pin BPF state map");
341 : 0 : goto err_unpin_all;
342 : : }
343 : : }
344 : :
345 [ + + + + : 3662 : bf_list_foreach (&handle->sets, set_node) {
+ + ]
346 : : struct bf_map *map = bf_list_node_get_data(set_node);
347 : :
348 [ - + ]: 363 : if (!map)
349 : 0 : continue;
350 : :
351 : 363 : r = bf_map_pin(map, dir_fd);
352 [ - + ]: 363 : if (r) {
353 [ # # ]: 0 : bf_err_r(r, "failed to pin BPF set map");
354 : 0 : goto err_unpin_all;
355 : : }
356 : : }
357 : :
358 [ + + ]: 1468 : if (handle->link) {
359 : 157 : r = bf_link_pin(handle->link, lock);
360 [ - + ]: 157 : if (r) {
361 [ # # ]: 0 : bf_err_r(r, "failed to pin BPF link");
362 : 0 : goto err_unpin_all;
363 : : }
364 : : }
365 : :
366 : : return 0;
367 : :
368 : 2 : err_unpin_all:
369 : 2 : bf_handle_unpin(handle, lock);
370 : 2 : return r;
371 : : }
372 : :
373 : 1444 : void bf_handle_unpin(struct bf_handle *handle, struct bf_lock *lock)
374 : : {
375 : : int dir_fd;
376 : :
377 : : assert(handle);
378 : : assert(lock);
379 : :
380 : 1444 : dir_fd = lock->chain_fd;
381 : :
382 [ + - ]: 1444 : if (handle->cmap)
383 : 1444 : bf_map_unpin(handle->cmap, dir_fd);
384 [ + - ]: 1444 : if (handle->pmap)
385 : 1444 : bf_map_unpin(handle->pmap, dir_fd);
386 [ + + ]: 1444 : if (handle->lmap)
387 : 32 : bf_map_unpin(handle->lmap, dir_fd);
388 [ + + ]: 1444 : if (handle->smap)
389 : 3 : bf_map_unpin(handle->smap, dir_fd);
390 : :
391 [ + + - + : 3458 : bf_list_foreach (&handle->sets, set_node) {
+ + ]
392 : : struct bf_map *map = bf_list_node_get_data(set_node);
393 : :
394 [ - + ]: 285 : if (!map)
395 : 0 : continue;
396 : :
397 : 285 : bf_map_unpin(map, dir_fd);
398 : : }
399 : :
400 [ + + ]: 1444 : if (handle->link)
401 : 160 : bf_link_unpin(handle->link, lock);
402 : :
403 : 1444 : unlinkat(dir_fd, handle->prog_name, 0);
404 : 1444 : }
405 : :
406 : 5139 : int bf_handle_get_counter(const struct bf_handle *handle, uint32_t counter_idx,
407 : : struct bf_counter *counter)
408 : : {
409 : : _cleanup_free_ struct bf_counter *percpu = NULL;
410 : : int num_cpus;
411 : : int r;
412 : :
413 : : assert(handle);
414 : : assert(counter);
415 : :
416 [ - + ]: 5139 : if (!handle->cmap)
417 [ # # ]: 0 : return bf_err_r(-ENOENT, "handle has no counters map");
418 : :
419 : 5139 : num_cpus = libbpf_num_possible_cpus();
420 [ - + ]: 5139 : if (num_cpus < 0)
421 [ # # ]: 0 : return bf_err_r(num_cpus, "failed to get number of possible CPUs");
422 : :
423 : 5139 : percpu = calloc(num_cpus, sizeof(*percpu));
424 [ + - ]: 5139 : if (!percpu)
425 : : return -ENOMEM;
426 : :
427 : 5139 : r = bf_bpf_map_lookup_elem(handle->cmap->fd, &counter_idx, percpu);
428 [ - + ]: 5139 : if (r < 0)
429 [ # # ]: 0 : return bf_err_r(r, "failed to lookup counters map");
430 : :
431 : 5139 : counter->count = 0;
432 : 5139 : counter->size = 0;
433 [ + + ]: 46251 : for (int i = 0; i < num_cpus; i++) {
434 : 41112 : counter->count += percpu[i].count;
435 : 41112 : counter->size += percpu[i].size;
436 : : }
437 : :
438 : : return 0;
439 : : }
440 : :
441 : 15 : int bf_handle_set_counter(struct bf_handle *handle, uint32_t counter_idx,
442 : : const struct bf_counter *counter)
443 : : {
444 : : _cleanup_free_ struct bf_counter *percpu = NULL;
445 : : int num_cpus;
446 : : int r;
447 : :
448 : : assert(handle);
449 : : assert(counter);
450 : :
451 [ - + ]: 15 : if (!handle->cmap)
452 [ # # ]: 0 : return bf_err_r(-ENOENT, "handle has no counters map");
453 : :
454 : 15 : num_cpus = libbpf_num_possible_cpus();
455 [ - + ]: 15 : if (num_cpus < 0)
456 [ # # ]: 0 : return bf_err_r(num_cpus, "failed to get number of possible CPUs");
457 : :
458 : 15 : percpu = calloc(num_cpus, sizeof(*percpu));
459 [ + - ]: 15 : if (!percpu)
460 : : return -ENOMEM;
461 : :
462 : 15 : percpu[0] = *counter;
463 : :
464 : 15 : r = bf_bpf_map_update_elem(handle->cmap->fd, &counter_idx, percpu, 0);
465 [ - + ]: 15 : if (r < 0)
466 [ # # ]: 0 : return bf_err_r(r, "failed to update counters map");
467 : :
468 : : return 0;
469 : : }
470 : :
471 : 154 : int bf_handle_attach(struct bf_handle *handle, enum bf_hook hook,
472 : : struct bf_hookopts **hookopts)
473 : : {
474 : : int r;
475 : :
476 : : assert(handle);
477 : : assert(hookopts);
478 : :
479 [ - + ]: 154 : if (handle->link)
480 [ # # ]: 0 : return bf_err_r(-EEXIST, "program is already attached");
481 : :
482 : 154 : r = bf_link_new(&handle->link, _BF_LINK_NAME, hook, hookopts,
483 : : handle->prog_fd);
484 [ + + ]: 154 : if (r)
485 [ + - ]: 4 : return bf_err_r(r, "failed to attach bf_link");
486 : :
487 : : return 0;
488 : : }
489 : :
490 : 1416 : void bf_handle_detach(struct bf_handle *handle)
491 : : {
492 : : assert(handle);
493 : :
494 [ + + ]: 1416 : if (handle->link) {
495 : 141 : (void)bf_bpf_link_detach(handle->link->fd);
496 [ + + ]: 141 : if (handle->link->fd_extra >= 0)
497 : 7 : (void)bf_bpf_link_detach(handle->link->fd_extra);
498 : : }
499 : :
500 : 1416 : bf_link_free(&handle->link);
501 : 1416 : }
502 : :
503 : 1416 : void bf_handle_unload(struct bf_handle *handle)
504 : : {
505 : : assert(handle);
506 : :
507 : 1416 : closep(&handle->prog_fd);
508 : :
509 : : /* Explicitly detach the BPF link before closing it. Relying solely on
510 : : * close() is not sufficient when the link is pinned (the pin holds a
511 : : * kernel reference that keeps the link alive). Using BPF_LINK_DETACH
512 : : * ensures the program is removed from the hook immediately. */
513 : 1416 : bf_handle_detach(handle);
514 : :
515 : 1416 : bf_link_free(&handle->link);
516 : 1416 : bf_map_free(&handle->cmap);
517 : 1416 : bf_map_free(&handle->pmap);
518 : 1416 : bf_map_free(&handle->lmap);
519 : 1416 : bf_map_free(&handle->smap);
520 : 1416 : bf_list_clean(&handle->sets);
521 : 1416 : }
522 : :
523 [ + + ]: 1266 : int bf_handle_load_counters(const struct bf_handle *handle,
524 : : struct bf_chain *chain)
525 : : {
526 : : size_t n_rules;
527 : : int r;
528 : :
529 : : assert(handle);
530 : : assert(chain);
531 : :
532 : : n_rules = bf_list_size(&chain->rules);
533 [ + + + + : 5078 : bf_list_foreach (&chain->rules, rule_node) {
+ + ]
534 : : struct bf_rule *rule = bf_list_node_get_data(rule_node);
535 : :
536 [ + + ]: 1273 : if (!rule->has_counters)
537 : 14 : continue;
538 : :
539 : 1259 : r = bf_handle_get_counter(handle, rule->index, &rule->counters);
540 [ - + ]: 1259 : if (r)
541 [ # # ]: 0 : return bf_err_r(r, "failed to load counter for rule %u",
542 : : rule->index);
543 : : }
544 : :
545 : 1266 : r = bf_handle_get_counter(handle, n_rules, &chain->policy_counters);
546 [ - + ]: 1266 : if (r) {
547 [ # # ]: 0 : return bf_err_r(r, "failed to load policy counters for '%s'",
548 : : chain->name);
549 : : }
550 : :
551 : 1266 : r = bf_handle_get_counter(handle, n_rules + 1, &chain->error_counters);
552 [ - + ]: 1266 : if (r) {
553 [ # # ]: 0 : return bf_err_r(r, "failed to load error counters for '%s'",
554 : : chain->name);
555 : : }
556 : :
557 : 1266 : r = bf_handle_get_counter(handle, n_rules + 2,
558 : : &chain->parser_drop_counters);
559 [ - + ]: 1266 : if (r) {
560 [ # # ]: 0 : return bf_err_r(r, "failed to load parser-drop counters for '%s'",
561 : : chain->name);
562 : : }
563 : :
564 : : return 0;
565 : : }
566 : :
567 : 20 : int bf_handle_copy_counters(const struct bf_handle *src, struct bf_handle *dst,
568 : : size_t n_rules)
569 : : {
570 : : int r;
571 : :
572 : : assert(src);
573 : : assert(dst);
574 : :
575 [ + - - + ]: 20 : if (!src->cmap || !dst->cmap)
576 [ # # ]: 0 : return bf_err_r(-ENOENT, "missing counter map for counter transfer");
577 : :
578 : : // The policy, error, and parser-drop counters follow the rule counters.
579 [ + + ]: 102 : for (uint32_t i = 0; i < n_rules + 3; ++i) {
580 : : struct bf_counter counter;
581 : :
582 : 82 : r = bf_handle_get_counter(src, i, &counter);
583 [ - + ]: 82 : if (r)
584 [ # # ]: 0 : return bf_err_r(r, "failed to read counter %u", i);
585 : :
586 [ + + + - ]: 82 : if (!counter.count && !counter.size)
587 : 67 : continue;
588 : :
589 : 15 : r = bf_handle_set_counter(dst, i, &counter);
590 [ - + ]: 15 : if (r)
591 [ # # ]: 0 : return bf_err_r(r, "failed to write counter %u", i);
592 : : }
593 : :
594 : : return 0;
595 : : }
|