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 "state.h"
7 : :
8 : : #include <dirent.h>
9 : : #include <errno.h>
10 : : #include <stdio.h>
11 : : #include <stdlib.h>
12 : : #include <unistd.h>
13 : :
14 : : #include <bpfilter/bpf.h>
15 : : #include <bpfilter/chain.h>
16 : : #include <bpfilter/ctx.h>
17 : : #include <bpfilter/helper.h>
18 : : #include <bpfilter/logger.h>
19 : :
20 : : #include "cgen/handle.h"
21 : : #include "cgen/prog/map.h"
22 : : #include "core/lock.h"
23 : :
24 : : #define _BF_STATE_TMP_PIN_NAME "bf_ctx_tmp"
25 : :
26 : : static void _bf_state_free_dir(DIR **dir)
27 : : {
28 [ + - ]: 1253 : if (*dir)
29 : 1253 : closedir(*dir);
30 : : }
31 : :
32 : : #define _free_dir_ __attribute__((cleanup(_bf_state_free_dir)))
33 : :
34 : 1506 : int bf_state_pack(const struct bf_chain *chain, const struct bf_handle *handle,
35 : : bf_wpack_t *pack)
36 : : {
37 : : assert(chain);
38 : : assert(handle);
39 : : assert(pack);
40 : :
41 : 1506 : bf_wpack_open_object(pack, "chain");
42 : 1506 : bf_chain_pack(chain, pack);
43 : 1506 : bf_wpack_close_object(pack);
44 : :
45 : 1506 : bf_wpack_open_object(pack, "handle");
46 : 1506 : bf_handle_pack(handle, pack);
47 : 1506 : bf_wpack_close_object(pack);
48 : :
49 [ - + ]: 1506 : return bf_wpack_is_valid(pack) ? 0 : -EINVAL;
50 : : }
51 : :
52 : 1506 : int bf_state_persist(struct bf_lock *lock, const struct bf_chain *chain,
53 : : const struct bf_handle *handle)
54 : : {
55 : 1506 : _free_bf_wpack_ bf_wpack_t *pack = NULL;
56 : 1506 : _free_bf_map_ struct bf_map *map = NULL;
57 : : const void *data;
58 : : size_t data_len;
59 : 1506 : uint32_t key = 0;
60 : : int dir_fd;
61 : : int r;
62 : :
63 : : assert(lock);
64 : : assert(chain);
65 : : assert(handle);
66 : :
67 : 1506 : dir_fd = lock->chain_fd;
68 : :
69 : 1506 : r = bf_wpack_new(&pack);
70 [ - + ]: 1506 : if (r)
71 [ # # ]: 0 : return bf_err_r(r, "failed to create wpack for chain state");
72 : :
73 : 1506 : r = bf_state_pack(chain, handle, pack);
74 [ - + ]: 1506 : if (r)
75 [ # # ]: 0 : return bf_err_r(r, "failed to pack chain state");
76 : :
77 : 1506 : r = bf_wpack_get_data(pack, &data, &data_len);
78 [ - + ]: 1506 : if (r)
79 [ # # ]: 0 : return bf_err_r(r, "failed to get data from chain state wpack");
80 : :
81 : 1506 : r = bf_map_new(&map, BF_STATE_PIN_NAME, BF_MAP_TYPE_CTX, sizeof(uint32_t),
82 : : data_len, 1);
83 [ - + ]: 1506 : if (r)
84 [ # # ]: 0 : return bf_err_r(r, "failed to create context map");
85 : :
86 : 1506 : r = bf_map_set_elem(map, &key, (void *)data);
87 [ - + ]: 1506 : if (r)
88 [ # # ]: 0 : return bf_err_r(r, "failed to write context to map");
89 : :
90 : : // Remove stale temporary pin if present.
91 : 1506 : unlinkat(dir_fd, _BF_STATE_TMP_PIN_NAME, 0);
92 : :
93 : 1506 : r = bf_bpf_obj_pin(_BF_STATE_TMP_PIN_NAME, map->fd, dir_fd);
94 [ - + ]: 1506 : if (r)
95 [ # # ]: 0 : return bf_err_r(r, "failed to pin context map");
96 : :
97 : 1506 : r = renameat(dir_fd, _BF_STATE_TMP_PIN_NAME, dir_fd, BF_STATE_PIN_NAME);
98 [ - + ]: 1506 : if (r) {
99 : 0 : r = -errno;
100 : 0 : unlinkat(dir_fd, _BF_STATE_TMP_PIN_NAME, 0);
101 [ # # ]: 0 : return bf_err_r(r, "failed to atomically replace context map pin");
102 : : }
103 : :
104 : : return 0;
105 : : }
106 : :
107 : 5853 : static int _bf_state_unpack(struct bf_lock *lock, bf_rpack_node_t node,
108 : : struct bf_chain **chain, struct bf_handle **handle)
109 : : {
110 : 5853 : _free_bf_chain_ struct bf_chain *_chain = NULL;
111 : 5853 : _free_bf_handle_ struct bf_handle *_handle = NULL;
112 : : bf_rpack_node_t child;
113 : : int r;
114 : :
115 : 5853 : r = bf_rpack_kv_obj(node, "chain", &child);
116 [ - + ]: 5853 : if (r)
117 [ # # ]: 0 : return bf_rpack_key_err(r, "state.chain");
118 : :
119 : 5853 : r = bf_chain_new_from_pack(&_chain, child);
120 [ - + ]: 5853 : if (r)
121 [ # # ]: 0 : return bf_rpack_key_err(r, "state.chain");
122 : :
123 : 5853 : r = bf_rpack_kv_node(node, "handle", &child);
124 [ - + ]: 5853 : if (r)
125 [ # # ]: 0 : return bf_rpack_key_err(r, "state.handle");
126 : :
127 : 5853 : r = bf_handle_new_from_pack(&_handle, lock, child);
128 [ + - ]: 5853 : if (r)
129 : : return r;
130 : :
131 : 5853 : *chain = TAKE_PTR(_chain);
132 : 5853 : *handle = TAKE_PTR(_handle);
133 : :
134 : 5853 : return 0;
135 : : }
136 : :
137 : 5856 : int bf_state_restore(struct bf_lock *lock, struct bf_chain **chain,
138 : : struct bf_handle **handle)
139 : : {
140 : 5856 : _free_bf_rpack_ bf_rpack_t *pack = NULL;
141 : 5856 : _cleanup_close_ int map_fd = -1;
142 : : _cleanup_free_ void *data = NULL;
143 : : struct bpf_map_info info;
144 : 5856 : uint32_t key = 0;
145 : : int r;
146 : :
147 : : assert(chain);
148 : : assert(handle);
149 : : assert(lock);
150 : :
151 [ - + ]: 5856 : if (!bf_ctx_is_setup())
152 [ # # ]: 0 : return bf_err_r(-EINVAL, "context is not initialized");
153 : :
154 : 5856 : r = bf_bpf_obj_get(BF_STATE_PIN_NAME, lock->chain_fd, &map_fd);
155 [ + + ]: 5856 : if (r < 0) {
156 [ + - ]: 3 : return bf_err_r(r, "failed to open pinned context map of chain '%s'",
157 : : lock->chain_name);
158 : : }
159 : :
160 : 5853 : r = bf_bpf_map_get_info(map_fd, &info);
161 [ - + ]: 5853 : if (r) {
162 [ # # ]: 0 : return bf_err_r(r, "failed to get context map info of chain '%s'",
163 : : lock->chain_name);
164 : : }
165 : :
166 [ - + ]: 5853 : if (info.value_size == 0) {
167 [ # # ]: 0 : return bf_err_r(-EINVAL,
168 : : "invalid serialized context size for chain '%s'",
169 : : lock->chain_name);
170 : : }
171 : :
172 : 5853 : data = malloc(info.value_size);
173 [ + - ]: 5853 : if (!data)
174 : : return -ENOMEM;
175 : :
176 : 5853 : r = bf_bpf_map_lookup_elem(map_fd, &key, data);
177 [ - + ]: 5853 : if (r) {
178 [ # # ]: 0 : return bf_err_r(r, "failed to read context of chain '%s' from map",
179 : : lock->chain_name);
180 : : }
181 : :
182 : 5853 : r = bf_rpack_new(&pack, data, info.value_size);
183 [ - + ]: 5853 : if (r) {
184 [ # # ]: 0 : return bf_err_r(r, "failed to create rpack for state of chain '%s'",
185 : : lock->chain_name);
186 : : }
187 : :
188 : 5853 : r = _bf_state_unpack(lock, bf_rpack_root(pack), chain, handle);
189 [ - + ]: 5853 : if (r) {
190 [ # # ]: 0 : return bf_err_r(r, "failed to deserialize state of chain '%s'",
191 : : lock->chain_name);
192 : : }
193 : :
194 : : return 0;
195 : : }
196 : :
197 : 1253 : int bf_state_discover(struct bf_lock *lock, bf_list **chains, bf_list **handles)
198 : : {
199 : 1253 : _free_bf_list_ bf_list *_chains = NULL;
200 : 1253 : _free_bf_list_ bf_list *_handles = NULL;
201 : : _free_dir_ DIR *dir = NULL;
202 : : struct dirent *entry;
203 : : int iter_fd;
204 : : int r;
205 : :
206 : : assert(lock);
207 : : assert(chains);
208 : : assert(handles);
209 : :
210 [ - + ]: 1253 : if (!bf_ctx_is_setup())
211 [ # # ]: 0 : return bf_err_r(-EINVAL, "context is not initialized");
212 : :
213 : 1253 : r = bf_list_new(&_chains,
214 : 1253 : &bf_list_ops_default(bf_chain_free, bf_chain_pack));
215 [ - + ]: 1253 : if (r)
216 [ # # ]: 0 : return bf_err_r(r, "failed to allocate chain list");
217 : :
218 : 1253 : r = bf_list_new(&_handles,
219 : 1253 : &bf_list_ops_default(bf_handle_free, bf_handle_pack));
220 [ - + ]: 1253 : if (r)
221 [ # # ]: 0 : return bf_err_r(r, "failed to allocate handle list");
222 : :
223 : : /* fdopendir() takes ownership of the fd: dup so lock->pindir_fd remains
224 : : * valid for further uses. */
225 : 1253 : iter_fd = dup(lock->pindir_fd);
226 [ - + ]: 1253 : if (iter_fd < 0)
227 [ # # ]: 0 : return bf_err_r(-errno, "failed to dup pin directory fd");
228 : :
229 : 1253 : dir = fdopendir(iter_fd);
230 [ - + ]: 1253 : if (!dir) {
231 : 0 : r = -errno;
232 : 0 : close(iter_fd);
233 [ # # ]: 0 : return bf_err_r(r, "failed to open pin directory for iteration");
234 : : }
235 : :
236 : : while (true) {
237 : 4392 : _free_bf_chain_ struct bf_chain *chain = NULL;
238 : 4392 : _free_bf_handle_ struct bf_handle *handle = NULL;
239 : :
240 : 4392 : errno = 0;
241 : 4392 : entry = readdir(dir);
242 [ + + - + ]: 4392 : if (!entry && errno != 0) {
243 [ # # ]: 0 : bf_warn_r(errno, "readdir failed, returning partial results");
244 : : break;
245 : : }
246 : : if (!entry)
247 : : break;
248 : :
249 [ + + + + ]: 3139 : if (bf_streq(entry->d_name, ".") || bf_streq(entry->d_name, ".."))
250 : 2502 : continue;
251 : :
252 [ + + ]: 637 : if (entry->d_type != DT_DIR)
253 : 1 : continue;
254 : :
255 : : /* Skip in-flight staging directories owned by concurrent writers. */
256 [ - + ]: 636 : if (bf_strneq(entry->d_name, BF_LOCK_STAGING_PREFIX,
257 : : sizeof(BF_LOCK_STAGING_PREFIX) - 1))
258 : 0 : continue;
259 : :
260 : 636 : r = bf_lock_acquire_chain(lock, entry->d_name, BF_LOCK_READ, false);
261 [ - + ]: 636 : if (r) {
262 [ # # ]: 0 : bf_warn_r(r, "failed to acquire READ lock on chain '%s', skipping",
263 : : entry->d_name);
264 : 0 : continue;
265 : : }
266 : :
267 : 636 : r = bf_state_restore(lock, &chain, &handle);
268 [ + + ]: 636 : if (r) {
269 [ + - ]: 2 : bf_warn_r(r, "failed to restore chain '%s', skipping",
270 : : entry->d_name);
271 : 2 : bf_lock_release_chain(lock);
272 : 2 : continue;
273 : : }
274 : :
275 : 634 : bf_lock_release_chain(lock);
276 : :
277 : 634 : r = bf_list_push(_chains, (void **)&chain);
278 [ - + ]: 634 : if (r) {
279 [ # # ]: 0 : bf_warn_r(r, "failed to push chain '%s' to list, skipping",
280 : : entry->d_name);
281 : 0 : continue;
282 : : }
283 : :
284 : 634 : r = bf_list_push(_handles, (void **)&handle);
285 [ - + ]: 634 : if (r) {
286 : 0 : bf_list_delete(_chains, bf_list_get_tail(_chains));
287 [ # # ]: 0 : bf_warn_r(r, "failed to push handle '%s' to list, skipping",
288 : : entry->d_name);
289 : : }
290 : : }
291 : :
292 : 1253 : *chains = TAKE_PTR(_chains);
293 : 1253 : *handles = TAKE_PTR(_handles);
294 : :
295 : 1253 : return 0;
296 : : }
|