Skip to content

Commit 4d174dd

Browse files
committed
daemon: replace file-based context persistence with bpffs discovery
Remove the data.bin save/load machinery (bf_ctx_save, bf_ctx_load, bf_ctx_is_empty, _bf_save, _bf_load) and the post-request serialization in _bf_process_request. Instead, bf_ctx_setup now calls _bf_ctx_discover on non-transient startups, which iterates {bpffs}/bpfilter/ subdirectories and restores chains from their pinned BPF context maps. bf_cgen_new_from_pack is made static as it is now only used internally by bf_cgen_new_from_dir_fd. In bf_cgen_update, the persist and chain swap are reordered so the new handle and chain are serialized correctly.
1 parent 3bd565f commit 4d174dd

File tree

6 files changed

+158
-338
lines changed

6 files changed

+158
-338
lines changed

doc/usage/daemon.rst

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,6 @@ It is possible to customize the daemon's behavior using the following command-li
1717
- ``--usage``: print a short usage message.
1818
- ``-?``, ``--help``: print the help message.
1919

20-
21-
Runtime data
22-
------------
23-
24-
``bpfilter`` runtime data is located in two different directories:
25-
26-
- ``/run/bpfilter``: runtime context. Contains the socket used to communicate with the daemon, and the serialized data (except in ``--transient`` mode).
27-
- ``/sys/fs/bpf/bpfilter``: directory used to pin the BPF objects (except in ``--transient`` mode) so they persist across restarts of the daemon.
28-
29-
.. warning::
30-
If ``bpfilter`` fails to restore its state after restarting, its data can be cleanup up by removing both those directories. Doing so will remove all your filtering rules.
31-
3220
Namespaces
3321
----------
3422

src/bpfilter/cgen/cgen.c

Lines changed: 45 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,46 @@ static int _bf_cgen_persist(const struct bf_cgen *cgen, int dir_fd)
118118
return 0;
119119
}
120120

121+
static int _bf_cgen_new_from_pack(struct bf_cgen **cgen, bf_rpack_node_t node)
122+
{
123+
_free_bf_cgen_ struct bf_cgen *_cgen = NULL;
124+
_cleanup_close_ int dir_fd = -1;
125+
bf_rpack_node_t child;
126+
int r;
127+
128+
assert(cgen);
129+
130+
_cgen = calloc(1, sizeof(*_cgen));
131+
if (!_cgen)
132+
return -ENOMEM;
133+
134+
r = bf_rpack_kv_obj(node, "chain", &child);
135+
if (r)
136+
return bf_rpack_key_err(r, "bf_cgen.chain");
137+
138+
r = bf_chain_new_from_pack(&_cgen->chain, child);
139+
if (r)
140+
return bf_rpack_key_err(r, "bf_cgen.chain");
141+
142+
r = bf_rpack_kv_node(node, "handle", &child);
143+
if (r)
144+
return bf_rpack_key_err(r, "bf_cgen.handle");
145+
146+
dir_fd = _bf_cgen_get_chain_pindir_fd(_cgen->chain->name);
147+
if (dir_fd < 0) {
148+
return bf_err_r(dir_fd, "failed to open chain pin directory for '%s'",
149+
_cgen->chain->name);
150+
}
151+
152+
r = bf_handle_new_from_pack(&_cgen->handle, dir_fd, child);
153+
if (r)
154+
return r;
155+
156+
*cgen = TAKE_PTR(_cgen);
157+
158+
return 0;
159+
}
160+
121161
int bf_cgen_new_from_dir_fd(struct bf_cgen **cgen, int dir_fd)
122162
{
123163
_free_bf_rpack_ bf_rpack_t *pack = NULL;
@@ -150,7 +190,7 @@ int bf_cgen_new_from_dir_fd(struct bf_cgen **cgen, int dir_fd)
150190
if (r)
151191
return bf_err_r(r, "failed to create rpack for bf_cgen");
152192

153-
r = bf_cgen_new_from_pack(cgen, bf_rpack_root(pack));
193+
r = _bf_cgen_new_from_pack(cgen, bf_rpack_root(pack));
154194
if (r)
155195
return bf_err_r(r, "failed to deserialize cgen from context map");
156196

@@ -180,45 +220,6 @@ int bf_cgen_new(struct bf_cgen **cgen, struct bf_chain **chain)
180220
return 0;
181221
}
182222

183-
int bf_cgen_new_from_pack(struct bf_cgen **cgen, bf_rpack_node_t node)
184-
{
185-
_free_bf_cgen_ struct bf_cgen *_cgen = NULL;
186-
_cleanup_close_ int dir_fd = -1;
187-
bf_rpack_node_t child;
188-
int r;
189-
190-
assert(cgen);
191-
192-
_cgen = calloc(1, sizeof(*_cgen));
193-
if (!_cgen)
194-
return -ENOMEM;
195-
196-
r = bf_rpack_kv_obj(node, "chain", &child);
197-
if (r)
198-
return bf_rpack_key_err(r, "bf_cgen.chain");
199-
200-
r = bf_chain_new_from_pack(&_cgen->chain, child);
201-
if (r)
202-
return bf_rpack_key_err(r, "bf_cgen.chain");
203-
204-
r = bf_rpack_kv_node(node, "handle", &child);
205-
if (r)
206-
return bf_rpack_key_err(r, "bf_cgen.handle");
207-
208-
if ((dir_fd = _bf_cgen_get_chain_pindir_fd(_cgen->chain->name)) < 0) {
209-
return bf_err_r(dir_fd, "failed to open chain pin directory for '%s'",
210-
_cgen->chain->name);
211-
}
212-
213-
r = bf_handle_new_from_pack(&_cgen->handle, dir_fd, child);
214-
if (r)
215-
return r;
216-
217-
*cgen = TAKE_PTR(_cgen);
218-
219-
return 0;
220-
}
221-
222223
void bf_cgen_free(struct bf_cgen **cgen)
223224
{
224225
_cleanup_close_ int pin_fd = -1;
@@ -570,21 +571,21 @@ int bf_cgen_update(struct bf_cgen *cgen, struct bf_chain **new_chain,
570571
bf_swap(new_handle->link, old_handle->link);
571572
}
572573

574+
bf_swap(cgen->handle, new_handle);
575+
573576
if (persist) {
574-
r = bf_handle_pin(new_handle, pindir_fd);
577+
r = bf_handle_pin(cgen->handle, pindir_fd);
575578
if (r)
576579
return bf_err_r(r, "failed to pin new handle, ignoring");
577580

578581
r = _bf_cgen_persist(cgen, pindir_fd);
579582
if (r) {
580-
bf_handle_unpin(new_handle, pindir_fd);
583+
bf_handle_unpin(cgen->handle, pindir_fd);
581584
return bf_err_r(r, "failed to persist cgen for '%s'",
582585
cgen->chain->name);
583586
}
584587
}
585588

586-
bf_swap(cgen->handle, new_handle);
587-
588589
bf_chain_free(&cgen->chain);
589590
cgen->chain = TAKE_PTR(*new_chain);
590591

src/bpfilter/cgen/cgen.h

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -49,17 +49,6 @@ struct bf_cgen
4949
*/
5050
int bf_cgen_new(struct bf_cgen **cgen, struct bf_chain **chain);
5151

52-
/**
53-
* @brief Allocate and initialize a new codegen from serialized data.
54-
*
55-
* @param cgen Codegen object to allocate and initialize from the serialized
56-
* data. The caller will own the object. On failure, `*cgen` is
57-
* unchanged. Can't be NULL.
58-
* @param node Node containing the serialized codegen.
59-
* @return 0 on success, or a negative errno value on failure.
60-
*/
61-
int bf_cgen_new_from_pack(struct bf_cgen **cgen, bf_rpack_node_t node);
62-
6352
/**
6453
* @brief Allocate and initialize a codegen from a pinned context map.
6554
*

0 commit comments

Comments
 (0)