Skip to content

Commit 1ce7c3a

Browse files
authored
Merge pull request #980 from lec-bit/deserialzation
g_inner_map_mng.inner_map support persist in restart
2 parents 865f832 + 1902392 commit 1ce7c3a

File tree

2 files changed

+67
-16
lines changed

2 files changed

+67
-16
lines changed

bpf/deserialization_to_bpf_map/deserialization_to_bpf_map.c

Lines changed: 66 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,19 @@ static int get_map_fd_info(unsigned int id, int *map_fd, struct bpf_map_info *in
278278
return ret;
279279
}
280280

281+
static int bpf_get_map_id_by_fd(int map_fd)
282+
{
283+
struct bpf_map_info info = {};
284+
__u32 info_len = sizeof(info);
285+
286+
int ret = bpf_obj_get_info_by_fd(map_fd, &info, &info_len);
287+
if (ret < 0) {
288+
LOG_ERR("bpf_obj_get_info_by_fd failed, map_fd:%d ret:%d ERRNO:%d\n", map_fd, ret, errno);
289+
return ret;
290+
}
291+
return info.id;
292+
}
293+
281294
static int free_outter_map_entry(struct op_context *ctx, void *outter_key)
282295
{
283296
int key = *(int *)outter_key;
@@ -337,13 +350,16 @@ static int copy_sfield_to_map(struct op_context *ctx, int o_index, const Protobu
337350
*(uintptr_t *)value = (size_t)o_index;
338351
ret = bpf_map_update_elem(ctx->curr_fd, ctx->key, ctx->value, BPF_ANY);
339352
if (ret) {
353+
LOG_ERR("copy_sfield_to_map bpf_map_update_elem failed, ret:%d ERRNO:%d\n", ret, errno);
340354
free_outter_map_entry(ctx, &o_index);
341355
return ret;
342356
}
343357

344358
inner_fd = outter_key_to_inner_fd(ctx, o_index);
345-
if (inner_fd < 0)
359+
if (inner_fd < 0) {
360+
LOG_ERR("copy_sfield_to_map outter_key_to_inner_fd failed, inner_fd:%d ERRNO:%d\n", inner_fd, errno);
346361
return inner_fd;
362+
}
347363

348364
strcpy_s(ctx->inner_map_object, ctx->inner_info->value_size, save_value);
349365
ret = bpf_map_update_elem(inner_fd, &key, ctx->inner_map_object, BPF_ANY);
@@ -363,13 +379,16 @@ static int copy_msg_field_to_map(struct op_context *ctx, int o_index, const Prot
363379
*(uintptr_t *)value = (size_t)o_index;
364380
ret = bpf_map_update_elem(ctx->curr_fd, ctx->key, ctx->value, BPF_ANY);
365381
if (ret) {
382+
LOG_ERR("copy_msg_field_to_map bpf_map_update_elem failed, ret:%d ERRNO:%d\n", ret, errno);
366383
free_outter_map_entry(ctx, &o_index);
367384
return ret;
368385
}
369386

370387
inner_fd = outter_key_to_inner_fd(ctx, o_index);
371-
if (inner_fd < 0)
388+
if (inner_fd < 0) {
389+
LOG_ERR("copy_msg_field_to_map outter_key_to_inner_fd failed, inner_fd:%d ERRNO:%d\n", inner_fd, errno);
372390
return inner_fd;
391+
}
373392

374393
memcpy_s(&new_ctx, sizeof(new_ctx), ctx, sizeof(*ctx));
375394

@@ -476,6 +495,7 @@ static int repeat_field_handle(struct op_context *ctx, const ProtobufCFieldDescr
476495
*(uintptr_t *)value = (size_t)outter_key;
477496
ret = bpf_map_update_elem(ctx->curr_fd, ctx->key, ctx->value, BPF_ANY);
478497
if (ret) {
498+
LOG_ERR("repeat_field_handle bpf_map_update_elem failed, ret:%d ERRNO:%d\n", ret, errno);
479499
free_outter_map_entry(ctx, &outter_key);
480500
return ret;
481501
}
@@ -1593,12 +1613,12 @@ void inner_map_batch_delete()
15931613
return;
15941614
}
15951615

1596-
void deserial_uninit(bool persist)
1616+
int deserial_uninit(bool persist)
15971617
{
1618+
int ret = 0;
1619+
remove(MAP_IN_MAP_MNG_PERSIST_FILE_PATH);
15981620
if (persist)
1599-
inner_map_mng_persist();
1600-
else
1601-
remove(MAP_IN_MAP_MNG_PERSIST_FILE_PATH);
1621+
ret = inner_map_mng_persist();
16021622

16031623
for (int i = 1; i <= g_inner_map_mng.max_allocated_idx; i++) {
16041624
g_inner_map_mng.inner_maps[i].allocated = 0;
@@ -1616,7 +1636,7 @@ void deserial_uninit(bool persist)
16161636

16171637
close(g_inner_map_mng.inner_fd);
16181638
close(g_inner_map_mng.outter_fd);
1619-
return;
1639+
return ret;
16201640
}
16211641

16221642
int inner_map_scaleup()
@@ -1709,7 +1729,7 @@ int inner_map_elastic_scaling()
17091729

17101730
int inner_map_mng_persist()
17111731
{
1712-
int i, size;
1732+
int i, size, map_fd;
17131733
FILE *f = NULL;
17141734
struct persist_info *p = NULL;
17151735

@@ -1727,10 +1747,26 @@ int inner_map_mng_persist()
17271747
p->allocated_cnt = g_inner_map_mng.allocated_cnt;
17281748
p->used_cnt = g_inner_map_mng.used_cnt;
17291749
p->max_allocated_idx = g_inner_map_mng.max_allocated_idx;
1750+
1751+
/* Since map_fd cannot be used universally in different processes,
1752+
map_fd needs to be converted into map_id and stored, and then
1753+
converted into map_fd in the corresponding process when the
1754+
configuration is restored.
1755+
When storing map_fd into outer_map, the kernel update interface will
1756+
perform special processing on maps of types BPF_MAP_TYPE_ARRAY_OF_MAPS
1757+
and BPF_MAP_TYPE_HASH_OF_MAPS. The input parameter is map_fd, but the
1758+
kernel will convert it into a bpf_map pointer for storage, so it will
1759+
not be affected.*/
17301760
for (i = 0; i <= g_inner_map_mng.max_allocated_idx; i++) {
1731-
p->inner_map_stat[i].map_fd = g_inner_map_mng.inner_maps[i].map_fd;
1732-
p->inner_map_stat[i].used = g_inner_map_mng.inner_maps[i].used;
1733-
p->inner_map_stat[i].allocated = g_inner_map_mng.inner_maps[i].allocated;
1761+
if (g_inner_map_mng.inner_maps[i].allocated) {
1762+
map_fd = bpf_get_map_id_by_fd(g_inner_map_mng.inner_maps[i].map_fd);
1763+
if (map_fd < 0) {
1764+
return map_fd;
1765+
}
1766+
p->inner_map_stat[i].map_fd = map_fd;
1767+
p->inner_map_stat[i].used = g_inner_map_mng.inner_maps[i].used;
1768+
p->inner_map_stat[i].allocated = g_inner_map_mng.inner_maps[i].allocated;
1769+
}
17341770
}
17351771

17361772
f = fopen(MAP_IN_MAP_MNG_PERSIST_FILE_PATH, "wb");
@@ -1746,20 +1782,35 @@ int inner_map_mng_persist()
17461782
return 0;
17471783
}
17481784

1749-
void inner_map_mng_restore_by_persist_stat(struct persist_info *p, struct inner_map_stat *stat)
1785+
int inner_map_mng_restore_by_persist_stat(struct persist_info *p, struct inner_map_stat *stat)
17501786
{
17511787
memcpy(g_inner_map_mng.inner_maps, stat, sizeof(struct inner_map_stat) * (p->max_allocated_idx + 1));
17521788

1789+
// What is recorded in g_inner_map_mng.inner_maps[i].map_fd is map_id.
1790+
// Use map_id to get the fd of the inner_map in this process and refresh
1791+
// it to inner_maps for use.
1792+
for (int i = 0; i < p->max_allocated_idx; i++) {
1793+
if (g_inner_map_mng.inner_maps[i].allocated) {
1794+
int map_fd = bpf_map_get_fd_by_id(g_inner_map_mng.inner_maps[i].map_fd);
1795+
if (map_fd < 0) {
1796+
LOG_ERR("restore_by_persist_stat bpf_map_get_fd_by_id failed, i:[%d] map_fd:[%d]\n", i, map_fd);
1797+
return map_fd;
1798+
}
1799+
g_inner_map_mng.inner_maps[i].map_fd = map_fd;
1800+
}
1801+
}
1802+
17531803
g_inner_map_mng.used_cnt = p->used_cnt;
17541804
g_inner_map_mng.allocated_cnt = p->allocated_cnt;
17551805
g_inner_map_mng.max_allocated_idx = p->max_allocated_idx;
1756-
return;
1806+
return 0;
17571807
}
17581808

17591809
int inner_map_restore(bool restore)
17601810
{
17611811
int size;
17621812
int read_size;
1813+
int ret;
17631814
FILE *f = NULL;
17641815
struct persist_info p;
17651816
struct inner_map_stat *stat = NULL;
@@ -1796,10 +1847,10 @@ int inner_map_restore(bool restore)
17961847
return 0;
17971848
}
17981849

1799-
inner_map_mng_restore_by_persist_stat(&p, stat);
1850+
ret = inner_map_mng_restore_by_persist_stat(&p, stat);
18001851
free(stat);
18011852
fclose(f);
1802-
return 0;
1853+
return ret;
18031854
}
18041855

18051856
int deserial_init(bool restore)

bpf/deserialization_to_bpf_map/deserialization_to_bpf_map.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ void deserial_free_elem_list(struct element_list_node *head);
3333
int deserial_delete_elem(void *key, const void *msg_desciptor);
3434

3535
int deserial_init(bool restore);
36-
void deserial_uninit(bool persist);
36+
int deserial_uninit(bool persist);
3737
int inner_map_mng_persist();
3838

3939
#endif /* __DESERIALIZATION_TO_BPF_MAP_H__ */

0 commit comments

Comments
 (0)