LCOV - code coverage report
Current view: top level - libbpfilter - api.c (source / functions) Coverage Total Hit
Test: coverage.lcov Lines: 83.6 % 336 281
Test Date: 2026-11-26 17:15:32 Functions: 100.0 % 16 16
Branches: 53.0 % 296 157

             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 <stdlib.h>
       7                 :             : #include <string.h>
       8                 :             : #include <unistd.h>
       9                 :             : 
      10                 :             : #include <bpfilter/bpfilter.h>
      11                 :             : #include <bpfilter/ctx.h>
      12                 :             : 
      13                 :             : #include "bpfilter/chain.h"
      14                 :             : #include "bpfilter/core/list.h"
      15                 :             : #include "bpfilter/counter.h"
      16                 :             : #include "bpfilter/helper.h"
      17                 :             : #include "bpfilter/hook.h"
      18                 :             : #include "bpfilter/logger.h"
      19                 :             : #include "bpfilter/set.h"
      20                 :             : #include "cgen/codegen.h"
      21                 :             : #include "cgen/handle.h"
      22                 :             : #include "cgen/prog/link.h"
      23                 :             : #include "cgen/prog/map.h"
      24                 :             : #include "core/lock.h"
      25                 :             : #include "state.h"
      26                 :             : 
      27                 :             : #define _BF_PROG_NAME "bf_prog"
      28                 :             : 
      29                 :         192 : static int copy_hookopts(struct bf_hookopts **dest,
      30                 :             :                          const struct bf_hookopts *src)
      31                 :             : {
      32                 :             :     struct bf_hookopts *copy;
      33                 :             : 
      34                 :         192 :     copy = bf_memdup(src, sizeof(*src));
      35         [ +  - ]:         192 :     if (!copy)
      36                 :             :         return -ENOMEM;
      37                 :             : 
      38         [ +  + ]:         192 :     if (src->cgpath) {
      39                 :          87 :         copy->cgpath = strdup(src->cgpath);
      40         [ -  + ]:          87 :         if (!copy->cgpath) {
      41                 :           0 :             free(copy);
      42                 :           0 :             return -ENOMEM;
      43                 :             :         }
      44                 :             :     }
      45                 :             : 
      46                 :         192 :     *dest = copy;
      47                 :             : 
      48                 :         192 :     return 0;
      49                 :             : }
      50                 :             : 
      51                 :        1416 : static void _bf_chain_unload(struct bf_handle *handle, struct bf_lock *lock)
      52                 :             : {
      53                 :             :     // The lock cleanup removes an empty chain directory under a WRITE lock.
      54                 :        1416 :     unlinkat(lock->chain_fd, BF_STATE_PIN_NAME, 0);
      55                 :        1416 :     bf_handle_unpin(handle, lock);
      56                 :        1416 :     bf_handle_unload(handle);
      57                 :        1416 : }
      58                 :             : 
      59                 :             : /**
      60                 :             :  * @brief Unload every chain currently pinned in the pin directory.
      61                 :             :  *
      62                 :             :  * The caller must already hold a `BF_LOCK_WRITE` lock on the pin directory
      63                 :             :  * (typically via `bf_lock_init(BF_LOCK_WRITE)`). For each chain entry, a
      64                 :             :  * per-chain `BF_LOCK_WRITE` lock is acquired (so the chain dir is removed
      65                 :             :  * by `bf_lock_release_chain` after unload).
      66                 :             :  */
      67                 :        1243 : static int _bf_ruleset_flush(struct bf_lock *lock)
      68                 :             : {
      69                 :        1243 :     _free_bf_list_ bf_list *chains = NULL;
      70                 :        1243 :     _free_bf_list_ bf_list *handles = NULL;
      71                 :             :     struct bf_list_node *handle_node;
      72                 :             :     int r;
      73                 :             : 
      74                 :             :     assert(lock);
      75                 :             : 
      76                 :        1243 :     r = bf_state_discover(lock, &chains, &handles);
      77         [ -  + ]:        1243 :     if (r)
      78         [ #  # ]:           0 :         return bf_err_r(r, "failed to discover chains during flush");
      79                 :             : 
      80         [ +  + ]:        1243 :     handle_node = bf_list_get_head(handles);
      81   [ +  +  +  +  :        3740 :     bf_list_foreach (chains, chain_node) {
                   +  + ]
      82                 :             :         struct bf_chain *chain = bf_list_node_get_data(chain_node);
      83                 :             :         struct bf_handle *handle = bf_list_node_get_data(handle_node);
      84                 :             : 
      85                 :             :         handle_node = bf_list_node_next(handle_node);
      86                 :         627 :         r = bf_lock_acquire_chain(lock, chain->name, BF_LOCK_WRITE, false);
      87         [ -  + ]:         627 :         if (r) {
      88         [ #  # ]:           0 :             bf_warn_r(
      89                 :             :                 r,
      90                 :             :                 "failed to acquire WRITE lock on chain '%s' during flush, skipping",
      91                 :             :                 chain->name);
      92                 :           0 :             continue;
      93                 :             :         }
      94                 :             : 
      95                 :         627 :         _bf_chain_unload(handle, lock);
      96                 :         627 :         bf_lock_release_chain(lock);
      97                 :             :     }
      98                 :             : 
      99                 :             :     return 0;
     100                 :             : }
     101                 :             : 
     102                 :           8 : int bf_ruleset_get(bf_list *chains, bf_list *hookopts)
     103                 :             : {
     104                 :           8 :     _clean_bf_lock_ struct bf_lock lock = bf_lock_default();
     105                 :           8 :     _free_bf_list_ bf_list *restored_chains = NULL;
     106                 :           8 :     _free_bf_list_ bf_list *handles = NULL;
     107                 :           8 :     _clean_bf_list_ bf_list _chains = bf_list_default_from(*chains);
     108                 :           8 :     _clean_bf_list_ bf_list _hookopts = bf_list_default_from(*hookopts);
     109                 :             :     struct bf_list_node *handle_node;
     110                 :             :     int r;
     111                 :             : 
     112                 :           8 :     r = bf_lock_init(&lock, BF_LOCK_READ);
     113         [ -  + ]:           8 :     if (r)
     114         [ #  # ]:           0 :         return bf_err_r(r, "failed to acquire READ lock for ruleset get");
     115                 :             : 
     116                 :           8 :     r = bf_state_discover(&lock, &restored_chains, &handles);
     117         [ -  + ]:           8 :     if (r)
     118         [ #  # ]:           0 :         return bf_err_r(r, "failed to discover chains");
     119                 :             : 
     120         [ +  + ]:           8 :     handle_node = bf_list_get_head(handles);
     121   [ +  +  +  +  :          30 :     bf_list_foreach (restored_chains, chain_node) {
                   +  + ]
     122                 :          14 :         _free_bf_chain_ struct bf_chain *chain =
     123                 :             :             bf_list_node_take_data(chain_node);
     124                 :             :         struct bf_handle *handle = bf_list_node_get_data(handle_node);
     125                 :           7 :         _free_bf_hookopts_ struct bf_hookopts *hookopts_copy = NULL;
     126                 :             : 
     127                 :             :         handle_node = bf_list_node_next(handle_node);
     128                 :           7 :         r = bf_handle_load_counters(handle, chain);
     129         [ -  + ]:           7 :         if (r)
     130         [ #  # ]:           0 :             return bf_err_r(r, "failed to read counters for '%s'", chain->name);
     131                 :             : 
     132                 :           7 :         r = bf_list_push(&_chains, (void **)&chain);
     133         [ +  - ]:           7 :         if (r)
     134                 :             :             return r;
     135                 :             : 
     136   [ +  +  +  - ]:           7 :         if (handle->link && handle->link->hookopts) {
     137                 :           6 :             r = copy_hookopts(&hookopts_copy, handle->link->hookopts);
     138         [ -  + ]:           6 :             if (r)
     139         [ #  # ]:           0 :                 return bf_err_r(r, "failed to copy hookopts");
     140                 :             :         }
     141                 :           7 :         r = bf_list_add_tail(&_hookopts, hookopts_copy);
     142         [ +  - ]:           7 :         if (r)
     143                 :             :             return r;
     144                 :           7 :         TAKE_PTR(hookopts_copy);
     145                 :             :     }
     146                 :             : 
     147                 :           8 :     *chains = bf_list_move(&_chains);
     148                 :           8 :     *hookopts = bf_list_move(&_hookopts);
     149                 :             : 
     150                 :           8 :     return 0;
     151                 :             : }
     152                 :             : 
     153                 :          10 : int bf_ruleset_set(bf_list *chains, bf_list *hookopts)
     154                 :             : {
     155         [ +  + ]:          19 :     _clean_bf_lock_ struct bf_lock lock = bf_lock_default();
     156                 :             :     struct bf_list_node *chain_node = bf_list_get_head(chains);
     157                 :             :     struct bf_list_node *hookopts_node = bf_list_get_head(hookopts);
     158                 :             :     int r;
     159                 :             : 
     160         [ +  + ]:          10 :     if (bf_list_size(chains) != bf_list_size(hookopts))
     161                 :             :         return -EINVAL;
     162                 :             : 
     163                 :           9 :     r = bf_lock_init(&lock, BF_LOCK_WRITE);
     164         [ -  + ]:           9 :     if (r)
     165         [ #  # ]:           0 :         return bf_err_r(r, "failed to acquire WRITE lock for ruleset set");
     166                 :             : 
     167                 :           9 :     r = _bf_ruleset_flush(&lock);
     168         [ +  - ]:           9 :     if (r)
     169                 :             :         return r;
     170                 :             : 
     171         [ +  + ]:          20 :     while (chain_node && hookopts_node) {
     172                 :          13 :         _free_bf_handle_ struct bf_handle *handle = NULL;
     173                 :          13 :         _free_bf_codegen_ struct bf_codegen *codegen = NULL;
     174         [ +  + ]:          26 :         _free_bf_hookopts_ struct bf_hookopts *hookopts_copy = NULL;
     175                 :             :         struct bf_chain *chain = bf_list_node_get_data(chain_node);
     176                 :             :         const struct bf_hookopts *node_hookopts =
     177                 :             :             bf_list_node_get_data(hookopts_node);
     178                 :             : 
     179         [ +  + ]:          13 :         if (node_hookopts) {
     180                 :          11 :             r = copy_hookopts(&hookopts_copy, node_hookopts);
     181         [ -  + ]:          11 :             if (r)
     182                 :           0 :                 goto err_load;
     183                 :             :         }
     184                 :             : 
     185                 :          13 :         r = bf_handle_new(&handle, _BF_PROG_NAME);
     186         [ -  + ]:          13 :         if (r)
     187                 :           0 :             goto err_load;
     188                 :             : 
     189                 :          13 :         r = bf_lock_acquire_chain(&lock, chain->name, BF_LOCK_WRITE, true);
     190         [ -  + ]:          13 :         if (r) {
     191         [ #  # ]:           0 :             bf_err_r(r, "failed to acquire WRITE lock on chain '%s'",
     192                 :             :                      chain->name);
     193                 :           0 :             goto err_load;
     194                 :             :         }
     195                 :             : 
     196                 :          13 :         r = bf_codegen_new(&codegen, chain, handle);
     197         [ -  + ]:          13 :         if (r)
     198                 :           0 :             goto err_release;
     199                 :             : 
     200                 :          13 :         r = bf_codegen_generate(codegen);
     201         [ -  + ]:          13 :         if (r)
     202                 :           0 :             goto err_release;
     203                 :             : 
     204                 :          13 :         r = bf_codegen_load(codegen);
     205         [ -  + ]:          13 :         if (r)
     206                 :           0 :             goto err_release;
     207                 :             : 
     208         [ +  + ]:          13 :         if (hookopts_copy) {
     209                 :          11 :             r = bf_handle_attach(handle, chain->hook, &hookopts_copy);
     210         [ +  + ]:          11 :             if (r)
     211                 :           2 :                 goto err_release;
     212                 :             :         }
     213                 :             : 
     214                 :          11 :         r = bf_handle_pin(handle, &lock);
     215         [ -  + ]:          11 :         if (r)
     216                 :           0 :             goto err_release;
     217                 :             : 
     218                 :          11 :         r = bf_state_persist(&lock, chain, handle);
     219         [ -  + ]:          11 :         if (r) {
     220                 :           0 :             bf_handle_unpin(handle, &lock);
     221                 :           0 :             goto err_release;
     222                 :             :         }
     223                 :             : 
     224                 :          11 :         bf_lock_release_chain(&lock);
     225                 :             :         chain_node = bf_list_node_next(chain_node);
     226                 :             :         hookopts_node = bf_list_node_next(hookopts_node);
     227                 :             :         continue;
     228                 :             : 
     229                 :           2 : err_release:
     230         [ +  - ]:           2 :         bf_err_r(r, "failed to set chain '%s'", chain->name);
     231                 :           2 :         bf_lock_release_chain(&lock);
     232                 :           2 :         goto err_load;
     233                 :             :     }
     234                 :             : 
     235                 :             :     return 0;
     236                 :             : 
     237                 :             : err_load:
     238                 :           2 :     _bf_ruleset_flush(&lock);
     239                 :           2 :     return r;
     240                 :             : }
     241                 :             : 
     242                 :        1232 : int bf_ruleset_flush(void)
     243                 :             : {
     244                 :        1232 :     _clean_bf_lock_ struct bf_lock lock = bf_lock_default();
     245                 :             :     int r;
     246                 :             : 
     247                 :        1232 :     r = bf_lock_init(&lock, BF_LOCK_WRITE);
     248         [ -  + ]:        1232 :     if (r)
     249         [ #  # ]:           0 :         return bf_err_r(r, "failed to acquire WRITE lock for ruleset flush");
     250                 :             : 
     251                 :        1232 :     return _bf_ruleset_flush(&lock);
     252                 :             : }
     253                 :             : 
     254                 :        1420 : int bf_chain_set(struct bf_chain *chain, struct bf_hookopts *hookopts)
     255                 :             : {
     256                 :        1420 :     _clean_bf_lock_ struct bf_lock lock = bf_lock_default();
     257                 :        1420 :     _free_bf_chain_ struct bf_chain *old_chain = NULL;
     258                 :        1420 :     _free_bf_handle_ struct bf_handle *old_handle = NULL;
     259                 :        1420 :     _free_bf_handle_ struct bf_handle *handle = NULL;
     260                 :        1420 :     _free_bf_codegen_ struct bf_codegen *codegen = NULL;
     261                 :        1420 :     _free_bf_hookopts_ struct bf_hookopts *hookopts_copy = NULL;
     262                 :             :     int r;
     263                 :             : 
     264                 :             :     assert(chain);
     265                 :             : 
     266                 :             :     /* bf_chain_set is a namespace mutator: the previous chain (if any) is
     267                 :             :      * destroyed and a fresh one is published under the same name. Take
     268                 :             :      * pindir WRITE for the whole operation so the flush-then-load
     269                 :             :      * sequence is atomic w.r.t. every other libbpfilter caller. */
     270                 :        1420 :     r = bf_lock_init(&lock, BF_LOCK_WRITE);
     271         [ +  - ]:        1420 :     if (r)
     272                 :             :         return r;
     273                 :             : 
     274                 :             :     /* Unload any pre-existing chain under this name, and remove its pindir
     275                 :             :      * entry so stage-and-rename can publish the new one. */
     276                 :        1420 :     r = bf_lock_acquire_chain(&lock, chain->name, BF_LOCK_WRITE, false);
     277         [ +  + ]:        1420 :     if (r == 0) {
     278                 :         745 :         r = bf_state_restore(&lock, &old_chain, &old_handle);
     279         [ -  + ]:         745 :         if (r && r != -ENOENT) {
     280                 :           0 :             bf_lock_release_chain(&lock);
     281                 :           0 :             return r;
     282                 :             :         }
     283         [ +  - ]:         745 :         if (old_handle)
     284                 :         745 :             _bf_chain_unload(old_handle, &lock);
     285                 :             : 
     286                 :             :         /* Release drops the chain flock and (because we held WRITE)
     287                 :             :          * removes the now-empty chain dir. */
     288                 :         745 :         bf_lock_release_chain(&lock);
     289         [ +  - ]:         675 :     } else if (r != -ENOENT) {
     290                 :             :         return r;
     291                 :             :     }
     292                 :             : 
     293         [ +  + ]:        1420 :     if (hookopts) {
     294                 :         132 :         r = copy_hookopts(&hookopts_copy, hookopts);
     295         [ +  - ]:         132 :         if (r)
     296                 :             :             return r;
     297                 :             :     }
     298                 :             : 
     299                 :        1420 :     r = bf_handle_new(&handle, _BF_PROG_NAME);
     300         [ +  - ]:        1420 :     if (r)
     301                 :             :         return r;
     302                 :             : 
     303                 :             :     /* Create the new chain dir via stage-and-rename (I3). */
     304                 :        1420 :     r = bf_lock_acquire_chain(&lock, chain->name, BF_LOCK_WRITE, true);
     305         [ +  - ]:        1420 :     if (r)
     306                 :             :         return r;
     307                 :             : 
     308                 :        1420 :     r = bf_codegen_new(&codegen, chain, handle);
     309         [ +  + ]:        1420 :     if (r)
     310                 :             :         return r;
     311                 :             : 
     312                 :        1419 :     r = bf_codegen_generate(codegen);
     313         [ +  + ]:        1419 :     if (r)
     314         [ +  - ]:           3 :         return bf_err_r(r, "failed to generate bytecode");
     315                 :             : 
     316                 :        1416 :     r = bf_codegen_load(codegen);
     317         [ -  + ]:        1416 :     if (r)
     318         [ #  # ]:           0 :         return bf_err_r(r, "failed to load the chain");
     319                 :             : 
     320         [ +  + ]:        1416 :     if (hookopts_copy) {
     321                 :         129 :         r = bf_handle_attach(handle, chain->hook, &hookopts_copy);
     322         [ -  + ]:         129 :         if (r)
     323         [ #  # ]:           0 :             return bf_err_r(r, "failed to load and attach the chain");
     324                 :             :     }
     325                 :             : 
     326                 :        1416 :     r = bf_handle_pin(handle, &lock);
     327         [ +  + ]:        1416 :     if (r)
     328                 :             :         return r;
     329                 :             : 
     330                 :        1415 :     r = bf_state_persist(&lock, chain, handle);
     331         [ -  + ]:        1415 :     if (r) {
     332                 :           0 :         bf_handle_unpin(handle, &lock);
     333         [ #  # ]:           0 :         return bf_err_r(r, "failed to persist chain '%s'", chain->name);
     334                 :             :     }
     335                 :             : 
     336                 :             :     return 0;
     337                 :             : }
     338                 :             : 
     339                 :        1261 : int bf_chain_get(const char *name, struct bf_chain **chain,
     340                 :             :                  struct bf_hookopts **hookopts)
     341                 :             : {
     342                 :        1261 :     _clean_bf_lock_ struct bf_lock lock = bf_lock_default();
     343                 :        1261 :     _free_bf_chain_ struct bf_chain *_chain = NULL;
     344                 :        1261 :     _free_bf_handle_ struct bf_handle *handle = NULL;
     345                 :        1261 :     _free_bf_hookopts_ struct bf_hookopts *_hookopts = NULL;
     346                 :             :     int r;
     347                 :             : 
     348                 :             :     assert(name);
     349                 :             :     assert(chain);
     350                 :             :     assert(hookopts);
     351                 :             : 
     352                 :        1261 :     r = bf_lock_init_for_chain(&lock, name, BF_LOCK_READ, BF_LOCK_READ, false);
     353         [ +  + ]:        1261 :     if (r)
     354                 :             :         return r;
     355                 :             : 
     356                 :        1259 :     r = bf_state_restore(&lock, &_chain, &handle);
     357         [ +  - ]:        1259 :     if (r)
     358                 :             :         return r;
     359                 :             : 
     360                 :        1259 :     r = bf_handle_load_counters(handle, _chain);
     361         [ -  + ]:        1259 :     if (r)
     362         [ #  # ]:           0 :         return bf_err_r(r, "failed to load counters for '%s'", _chain->name);
     363                 :             : 
     364   [ +  +  +  - ]:        1259 :     if (handle->link && handle->link->hookopts) {
     365                 :          29 :         r = copy_hookopts(&_hookopts, handle->link->hookopts);
     366         [ +  - ]:          29 :         if (r)
     367                 :             :             return r;
     368                 :             :     }
     369                 :             : 
     370                 :        1259 :     *chain = TAKE_PTR(_chain);
     371                 :        1259 :     *hookopts = TAKE_PTR(_hookopts);
     372                 :             : 
     373                 :        1259 :     return 0;
     374                 :             : }
     375                 :             : 
     376                 :        3119 : int bf_chain_prog_fd(const char *name)
     377                 :             : {
     378                 :        3119 :     _clean_bf_lock_ struct bf_lock lock = bf_lock_default();
     379                 :        3119 :     _free_bf_chain_ struct bf_chain *chain = NULL;
     380                 :        3119 :     _free_bf_handle_ struct bf_handle *handle = NULL;
     381                 :             :     int r;
     382                 :             : 
     383                 :             :     assert(name);
     384                 :             : 
     385                 :        3119 :     r = bf_lock_init_for_chain(&lock, name, BF_LOCK_READ, BF_LOCK_READ, false);
     386         [ +  + ]:        3119 :     if (r)
     387                 :             :         return r;
     388                 :             : 
     389                 :        3118 :     r = bf_state_restore(&lock, &chain, &handle);
     390         [ +  - ]:        3118 :     if (r)
     391                 :             :         return r;
     392                 :             : 
     393         [ -  + ]:        3118 :     if (handle->prog_fd == -1)
     394         [ #  # ]:           0 :         return bf_err_r(-ENODEV, "chain '%s' has no loaded program", name);
     395                 :             : 
     396                 :        3118 :     return dup(handle->prog_fd);
     397                 :             : }
     398                 :             : 
     399                 :          10 : int bf_chain_logs_fd(const char *name)
     400                 :             : {
     401                 :          10 :     _clean_bf_lock_ struct bf_lock lock = bf_lock_default();
     402                 :          10 :     _free_bf_chain_ struct bf_chain *chain = NULL;
     403                 :          10 :     _free_bf_handle_ struct bf_handle *handle = NULL;
     404                 :             :     int r;
     405                 :             : 
     406                 :             :     assert(name);
     407                 :             : 
     408                 :          10 :     r = bf_lock_init_for_chain(&lock, name, BF_LOCK_READ, BF_LOCK_READ, false);
     409         [ +  + ]:          10 :     if (r)
     410                 :             :         return r;
     411                 :             : 
     412                 :           9 :     r = bf_state_restore(&lock, &chain, &handle);
     413         [ +  - ]:           9 :     if (r)
     414                 :             :         return r;
     415                 :             : 
     416         [ -  + ]:           9 :     if (!handle->lmap)
     417         [ #  # ]:           0 :         return bf_err_r(-ENOENT, "chain '%s' has no logs buffer", name);
     418                 :             : 
     419                 :           9 :     return dup(handle->lmap->fd);
     420                 :             : }
     421                 :             : 
     422                 :          17 : int bf_chain_load(struct bf_chain *chain)
     423                 :             : {
     424                 :          17 :     _clean_bf_lock_ struct bf_lock lock = bf_lock_default();
     425                 :          17 :     _free_bf_handle_ struct bf_handle *handle = NULL;
     426                 :          17 :     _free_bf_codegen_ struct bf_codegen *codegen = NULL;
     427                 :             :     int r;
     428                 :             : 
     429                 :             :     assert(chain);
     430                 :             : 
     431                 :             :     /* chain_load is a namespace mutator: pindir WRITE + chain WRITE on the
     432                 :             :      * staged inode. Stage-and-rename (I3) returns `-EEXIST` if another
     433                 :             :      * creator already claimed the name, which replaces the former
     434                 :             :      * check-then-create sequence atomically. */
     435                 :          17 :     r = bf_lock_init_for_chain(&lock, chain->name, BF_LOCK_WRITE, BF_LOCK_WRITE,
     436                 :             :                                true);
     437         [ +  - ]:          17 :     if (r)
     438                 :             :         return r;
     439                 :             : 
     440                 :          17 :     r = bf_handle_new(&handle, _BF_PROG_NAME);
     441         [ +  - ]:          17 :     if (r)
     442                 :             :         return r;
     443                 :             : 
     444                 :          17 :     r = bf_codegen_new(&codegen, chain, handle);
     445         [ +  - ]:          17 :     if (r)
     446                 :             :         return r;
     447                 :             : 
     448                 :          17 :     r = bf_codegen_generate(codegen);
     449         [ -  + ]:          17 :     if (r)
     450         [ #  # ]:           0 :         return bf_err_r(r, "failed to generate bytecode");
     451                 :             : 
     452                 :          17 :     r = bf_codegen_load(codegen);
     453         [ -  + ]:          17 :     if (r)
     454         [ #  # ]:           0 :         return bf_err_r(r, "failed to load the chain");
     455                 :             : 
     456                 :          17 :     r = bf_handle_pin(handle, &lock);
     457         [ +  + ]:          17 :     if (r)
     458                 :             :         return r;
     459                 :             : 
     460                 :          16 :     r = bf_state_persist(&lock, chain, handle);
     461         [ -  + ]:          16 :     if (r) {
     462                 :           0 :         bf_handle_unpin(handle, &lock);
     463         [ #  # ]:           0 :         return bf_err_r(r, "failed to persist chain '%s'", chain->name);
     464                 :             :     }
     465                 :             : 
     466         [ +  - ]:          16 :     bf_info("chain '%s' loaded", chain->name);
     467                 :             : 
     468         [ -  + ]:          16 :     if (bf_log_get_level() == BF_LOG_DBG) {
     469                 :           0 :         bf_chain_dump(chain, EMPTY_PREFIX);
     470                 :           0 :         bf_handle_dump(handle, EMPTY_PREFIX);
     471                 :             :     }
     472                 :             : 
     473                 :             :     return 0;
     474                 :             : }
     475                 :             : 
     476                 :          16 : int bf_chain_attach(const char *name, const struct bf_hookopts *hookopts)
     477                 :             : {
     478                 :          16 :     _clean_bf_lock_ struct bf_lock lock = bf_lock_default();
     479                 :          16 :     _free_bf_chain_ struct bf_chain *chain = NULL;
     480                 :          16 :     _free_bf_handle_ struct bf_handle *handle = NULL;
     481                 :          16 :     _free_bf_hookopts_ struct bf_hookopts *hookopts_copy = NULL;
     482                 :             :     int r;
     483                 :             : 
     484                 :             :     assert(name);
     485                 :             :     assert(hookopts);
     486                 :             : 
     487                 :          16 :     r = bf_lock_init_for_chain(&lock, name, BF_LOCK_READ, BF_LOCK_WRITE, false);
     488         [ +  + ]:          16 :     if (r)
     489                 :             :         return r;
     490                 :             : 
     491                 :          15 :     r = bf_state_restore(&lock, &chain, &handle);
     492         [ +  - ]:          15 :     if (r)
     493                 :             :         return r;
     494                 :             : 
     495         [ -  + ]:          15 :     if (handle->link)
     496         [ #  # ]:           0 :         return bf_err_r(-EBUSY, "chain '%s' is already linked to a hook", name);
     497                 :             : 
     498                 :          15 :     r = bf_hookopts_validate(hookopts, chain->hook);
     499         [ +  + ]:          15 :     if (r)
     500         [ +  - ]:           1 :         return bf_err_r(r, "failed to validate hook options");
     501                 :             : 
     502                 :          14 :     r = copy_hookopts(&hookopts_copy, hookopts);
     503         [ +  - ]:          14 :     if (r)
     504                 :             :         return r;
     505                 :             : 
     506         [ +  - ]:          14 :     bf_info("attaching %s to %s", chain->name, bf_hook_to_str(chain->hook));
     507                 :          14 :     bf_hookopts_dump(hookopts_copy, EMPTY_PREFIX);
     508                 :             : 
     509                 :          14 :     r = bf_handle_attach(handle, chain->hook, &hookopts_copy);
     510         [ +  + ]:          14 :     if (r)
     511         [ +  - ]:           2 :         return bf_err_r(r, "failed to attach chain '%s'", name);
     512                 :             : 
     513                 :          12 :     r = bf_link_pin(handle->link, &lock);
     514         [ -  + ]:          12 :     if (r) {
     515                 :           0 :         bf_handle_detach(handle);
     516                 :           0 :         return r;
     517                 :             :     }
     518                 :             : 
     519                 :          12 :     r = bf_state_persist(&lock, chain, handle);
     520         [ -  + ]:          12 :     if (r) {
     521                 :           0 :         bf_link_unpin(handle->link, &lock);
     522                 :           0 :         bf_handle_detach(handle);
     523         [ #  # ]:           0 :         return bf_err_r(r, "failed to persist chain '%s'", name);
     524                 :             :     }
     525                 :             : 
     526                 :             :     return 0;
     527                 :             : }
     528                 :             : 
     529                 :             : /* The caller holds the chain WRITE lock and owns both chains and old_handle. */
     530                 :          27 : static int _bf_chain_update(struct bf_lock *lock,
     531                 :             :                             const struct bf_chain *old_chain,
     532                 :             :                             struct bf_handle *old_handle,
     533                 :             :                             struct bf_chain *chain, bool preserve_counters)
     534                 :             : {
     535                 :          27 :     _free_bf_handle_ struct bf_handle *handle = NULL;
     536                 :          27 :     _free_bf_codegen_ struct bf_codegen *codegen = NULL;
     537                 :             :     int r;
     538                 :             : 
     539                 :             :     assert(lock);
     540                 :             :     assert(old_chain);
     541                 :             :     assert(old_handle);
     542                 :             :     assert(chain);
     543                 :             : 
     544                 :          27 :     r = bf_handle_new(&handle, _BF_PROG_NAME);
     545         [ +  - ]:          27 :     if (r)
     546                 :             :         return r;
     547                 :             : 
     548                 :          27 :     r = bf_codegen_new(&codegen, chain, handle);
     549         [ +  + ]:          27 :     if (r)
     550         [ +  - ]:           1 :         return bf_err_r(r, "failed to create a generation context");
     551                 :             : 
     552                 :          26 :     r = bf_codegen_generate(codegen);
     553         [ -  + ]:          26 :     if (r)
     554         [ #  # ]:           0 :         return bf_err_r(r, "failed to generate replacement bytecode");
     555                 :             : 
     556   [ +  +  +  + ]:          26 :     if (preserve_counters && old_handle->lmap) {
     557                 :           3 :         r = bf_map_dup(&handle->lmap, old_handle->lmap);
     558         [ -  + ]:           3 :         if (r)
     559         [ #  # ]:           0 :             return bf_err_r(r, "failed to reuse the log map");
     560                 :             :     }
     561                 :             : 
     562                 :          26 :     r = bf_codegen_load(codegen);
     563         [ -  + ]:          26 :     if (r)
     564         [ #  # ]:           0 :         return bf_err_r(r, "failed to load new program");
     565                 :             : 
     566         [ +  + ]:          26 :     if (preserve_counters) {
     567         [ -  + ]:          20 :         if (bf_list_size(&old_chain->rules) != bf_list_size(&chain->rules))
     568         [ #  # ]:           0 :             return bf_err_r(-EINVAL,
     569                 :             :                             "rule count mismatch for counter transfer");
     570                 :             : 
     571                 :          20 :         r = bf_handle_copy_counters(old_handle, handle,
     572                 :             :                                     bf_list_size(&chain->rules));
     573         [ -  + ]:          20 :         if (r)
     574         [ #  # ]:           0 :             return bf_err_r(r, "failed to transfer counters");
     575                 :             :     }
     576                 :             : 
     577                 :             :     // Publication starts here. Preserve the existing recovery behavior.
     578                 :          26 :     bf_handle_unpin(old_handle, lock);
     579                 :             : 
     580         [ +  + ]:          26 :     if (old_handle->link) {
     581                 :          19 :         r = bf_link_update(old_handle->link, handle->prog_fd);
     582         [ -  + ]:          19 :         if (r) {
     583         [ #  # ]:           0 :             bf_err_r(r, "failed to update link with new program");
     584         [ #  # ]:           0 :             if (bf_handle_pin(old_handle, lock) < 0)
     585         [ #  # ]:           0 :                 bf_err("failed to repin old handle, ignoring");
     586                 :             :             return r;
     587                 :             :         }
     588                 :             : 
     589                 :          19 :         bf_swap(handle->link, old_handle->link);
     590                 :             :     }
     591                 :             : 
     592                 :          26 :     r = bf_handle_pin(handle, lock);
     593         [ -  + ]:          26 :     if (r)
     594         [ #  # ]:           0 :         return bf_err_r(r, "failed to pin new handle");
     595                 :             : 
     596                 :             :     // Keep the intermediate old-configuration persistence step unchanged.
     597                 :          26 :     r = bf_state_persist(lock, old_chain, handle);
     598         [ -  + ]:          26 :     if (r) {
     599                 :           0 :         bf_handle_unpin(handle, lock);
     600         [ #  # ]:           0 :         return bf_err_r(r, "failed to persist chain '%s'", old_chain->name);
     601                 :             :     }
     602                 :             : 
     603                 :          26 :     r = bf_state_persist(lock, chain, handle);
     604         [ -  + ]:          26 :     if (r) {
     605                 :           0 :         bf_handle_unpin(handle, lock);
     606         [ #  # ]:           0 :         return bf_err_r(r, "failed to persist chain '%s'", chain->name);
     607                 :             :     }
     608                 :             : 
     609                 :             :     return 0;
     610                 :             : }
     611                 :             : 
     612                 :          11 : int bf_chain_update(const struct bf_chain *chain)
     613                 :             : {
     614                 :          11 :     _clean_bf_lock_ struct bf_lock lock = bf_lock_default();
     615                 :          11 :     _free_bf_chain_ struct bf_chain *old_chain = NULL;
     616                 :          11 :     _free_bf_handle_ struct bf_handle *old_handle = NULL;
     617                 :             :     int r;
     618                 :             : 
     619                 :             :     assert(chain);
     620                 :             : 
     621                 :          11 :     r = bf_lock_init_for_chain(&lock, chain->name, BF_LOCK_READ, BF_LOCK_WRITE,
     622                 :             :                                false);
     623         [ +  + ]:          11 :     if (r)
     624                 :             :         return r;
     625                 :             : 
     626                 :           9 :     r = bf_state_restore(&lock, &old_chain, &old_handle);
     627         [ +  - ]:           9 :     if (r)
     628                 :             :         return r;
     629                 :             : 
     630         [ +  + ]:           9 :     if (chain->hook != old_chain->hook) {
     631         [ +  - ]:           2 :         return bf_err_r(
     632                 :             :             -EINVAL,
     633                 :             :             "can't update chain '%s': hook %s doesn't match the existing hook %s",
     634                 :             :             chain->name, bf_hook_to_str(chain->hook),
     635                 :             :             bf_hook_to_str(old_chain->hook));
     636                 :             :     }
     637                 :             : 
     638                 :             :     // Code generation updates the chain's derived state in place.
     639                 :           7 :     return _bf_chain_update(&lock, old_chain, old_handle,
     640                 :             :                             (struct bf_chain *)chain, false);
     641                 :             : }
     642                 :             : 
     643                 :          21 : int bf_chain_update_set(const char *name, const struct bf_set *to_add,
     644                 :             :                         const struct bf_set *to_remove)
     645                 :             : {
     646                 :          21 :     _clean_bf_lock_ struct bf_lock lock = bf_lock_default();
     647                 :          21 :     _free_bf_chain_ struct bf_chain *old_chain = NULL;
     648                 :          21 :     _free_bf_chain_ struct bf_chain *new_chain = NULL;
     649                 :          21 :     _free_bf_handle_ struct bf_handle *old_handle = NULL;
     650                 :             :     int r;
     651                 :             : 
     652                 :             :     assert(name);
     653                 :             :     assert(to_add);
     654                 :             :     assert(to_remove);
     655                 :             : 
     656         [ +  + ]:          21 :     if (!bf_streq(to_add->name, to_remove->name))
     657         [ +  - ]:           1 :         return bf_err_r(-EINVAL, "to_add->name must match to_remove->name");
     658                 :             : 
     659                 :          20 :     r = bf_lock_init_for_chain(&lock, name, BF_LOCK_READ, BF_LOCK_WRITE, false);
     660         [ +  - ]:          20 :     if (r)
     661                 :             :         return r;
     662                 :             : 
     663                 :          20 :     r = bf_state_restore(&lock, &old_chain, &old_handle);
     664         [ +  - ]:          20 :     if (r)
     665                 :             :         return r;
     666                 :             : 
     667                 :             :     // Publication still needs the old configuration for its first persist.
     668                 :          20 :     r = bf_chain_new_from_copy(&new_chain, old_chain);
     669         [ +  - ]:          20 :     if (r)
     670                 :             :         return r;
     671                 :             : 
     672                 :          20 :     r = bf_chain_apply_set_delta(new_chain, to_add->name, to_add, to_remove);
     673         [ +  - ]:          20 :     if (r)
     674                 :             :         return r;
     675                 :             : 
     676                 :          20 :     return _bf_chain_update(&lock, old_chain, old_handle, new_chain, true);
     677                 :             : }
     678                 :             : 
     679                 :          45 : int bf_chain_flush(const char *name)
     680                 :             : {
     681                 :          45 :     _clean_bf_lock_ struct bf_lock lock = bf_lock_default();
     682                 :          45 :     _free_bf_chain_ struct bf_chain *chain = NULL;
     683                 :          45 :     _free_bf_handle_ struct bf_handle *handle = NULL;
     684                 :             :     int r;
     685                 :             : 
     686                 :             :     assert(name);
     687                 :             : 
     688                 :             :     /* chain_flush removes the chain dir from the pindir namespace, so it
     689                 :             :      * needs pindir WRITE (I2). */
     690                 :          45 :     r = bf_lock_init_for_chain(&lock, name, BF_LOCK_WRITE, BF_LOCK_WRITE,
     691                 :             :                                false);
     692         [ +  + ]:          45 :     if (r)
     693                 :             :         return r;
     694                 :             : 
     695                 :          44 :     r = bf_state_restore(&lock, &chain, &handle);
     696         [ +  - ]:          44 :     if (r)
     697                 :             :         return r;
     698                 :             : 
     699                 :          44 :     _bf_chain_unload(handle, &lock);
     700                 :             : 
     701                 :          44 :     return 0;
     702                 :             : }
        

Generated by: LCOV version 2.0-1