@@ -278,6 +278,19 @@ static int get_map_fd_info(unsigned int id, int *map_fd, struct bpf_map_info *in
278
278
return ret ;
279
279
}
280
280
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
+
281
294
static int free_outter_map_entry (struct op_context * ctx , void * outter_key )
282
295
{
283
296
int key = * (int * )outter_key ;
@@ -337,13 +350,16 @@ static int copy_sfield_to_map(struct op_context *ctx, int o_index, const Protobu
337
350
* (uintptr_t * )value = (size_t )o_index ;
338
351
ret = bpf_map_update_elem (ctx -> curr_fd , ctx -> key , ctx -> value , BPF_ANY );
339
352
if (ret ) {
353
+ LOG_ERR ("copy_sfield_to_map bpf_map_update_elem failed, ret:%d ERRNO:%d\n" , ret , errno );
340
354
free_outter_map_entry (ctx , & o_index );
341
355
return ret ;
342
356
}
343
357
344
358
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 );
346
361
return inner_fd ;
362
+ }
347
363
348
364
strcpy_s (ctx -> inner_map_object , ctx -> inner_info -> value_size , save_value );
349
365
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
363
379
* (uintptr_t * )value = (size_t )o_index ;
364
380
ret = bpf_map_update_elem (ctx -> curr_fd , ctx -> key , ctx -> value , BPF_ANY );
365
381
if (ret ) {
382
+ LOG_ERR ("copy_msg_field_to_map bpf_map_update_elem failed, ret:%d ERRNO:%d\n" , ret , errno );
366
383
free_outter_map_entry (ctx , & o_index );
367
384
return ret ;
368
385
}
369
386
370
387
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 );
372
390
return inner_fd ;
391
+ }
373
392
374
393
memcpy_s (& new_ctx , sizeof (new_ctx ), ctx , sizeof (* ctx ));
375
394
@@ -476,6 +495,7 @@ static int repeat_field_handle(struct op_context *ctx, const ProtobufCFieldDescr
476
495
* (uintptr_t * )value = (size_t )outter_key ;
477
496
ret = bpf_map_update_elem (ctx -> curr_fd , ctx -> key , ctx -> value , BPF_ANY );
478
497
if (ret ) {
498
+ LOG_ERR ("repeat_field_handle bpf_map_update_elem failed, ret:%d ERRNO:%d\n" , ret , errno );
479
499
free_outter_map_entry (ctx , & outter_key );
480
500
return ret ;
481
501
}
@@ -1593,12 +1613,12 @@ void inner_map_batch_delete()
1593
1613
return ;
1594
1614
}
1595
1615
1596
- void deserial_uninit (bool persist )
1616
+ int deserial_uninit (bool persist )
1597
1617
{
1618
+ int ret = 0 ;
1619
+ remove (MAP_IN_MAP_MNG_PERSIST_FILE_PATH );
1598
1620
if (persist )
1599
- inner_map_mng_persist ();
1600
- else
1601
- remove (MAP_IN_MAP_MNG_PERSIST_FILE_PATH );
1621
+ ret = inner_map_mng_persist ();
1602
1622
1603
1623
for (int i = 1 ; i <= g_inner_map_mng .max_allocated_idx ; i ++ ) {
1604
1624
g_inner_map_mng .inner_maps [i ].allocated = 0 ;
@@ -1616,7 +1636,7 @@ void deserial_uninit(bool persist)
1616
1636
1617
1637
close (g_inner_map_mng .inner_fd );
1618
1638
close (g_inner_map_mng .outter_fd );
1619
- return ;
1639
+ return ret ;
1620
1640
}
1621
1641
1622
1642
int inner_map_scaleup ()
@@ -1709,7 +1729,7 @@ int inner_map_elastic_scaling()
1709
1729
1710
1730
int inner_map_mng_persist ()
1711
1731
{
1712
- int i , size ;
1732
+ int i , size , map_fd ;
1713
1733
FILE * f = NULL ;
1714
1734
struct persist_info * p = NULL ;
1715
1735
@@ -1727,10 +1747,26 @@ int inner_map_mng_persist()
1727
1747
p -> allocated_cnt = g_inner_map_mng .allocated_cnt ;
1728
1748
p -> used_cnt = g_inner_map_mng .used_cnt ;
1729
1749
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.*/
1730
1760
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
+ }
1734
1770
}
1735
1771
1736
1772
f = fopen (MAP_IN_MAP_MNG_PERSIST_FILE_PATH , "wb" );
@@ -1746,20 +1782,35 @@ int inner_map_mng_persist()
1746
1782
return 0 ;
1747
1783
}
1748
1784
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 )
1750
1786
{
1751
1787
memcpy (g_inner_map_mng .inner_maps , stat , sizeof (struct inner_map_stat ) * (p -> max_allocated_idx + 1 ));
1752
1788
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
+
1753
1803
g_inner_map_mng .used_cnt = p -> used_cnt ;
1754
1804
g_inner_map_mng .allocated_cnt = p -> allocated_cnt ;
1755
1805
g_inner_map_mng .max_allocated_idx = p -> max_allocated_idx ;
1756
- return ;
1806
+ return 0 ;
1757
1807
}
1758
1808
1759
1809
int inner_map_restore (bool restore )
1760
1810
{
1761
1811
int size ;
1762
1812
int read_size ;
1813
+ int ret ;
1763
1814
FILE * f = NULL ;
1764
1815
struct persist_info p ;
1765
1816
struct inner_map_stat * stat = NULL ;
@@ -1796,10 +1847,10 @@ int inner_map_restore(bool restore)
1796
1847
return 0 ;
1797
1848
}
1798
1849
1799
- inner_map_mng_restore_by_persist_stat (& p , stat );
1850
+ ret = inner_map_mng_restore_by_persist_stat (& p , stat );
1800
1851
free (stat );
1801
1852
fclose (f );
1802
- return 0 ;
1853
+ return ret ;
1803
1854
}
1804
1855
1805
1856
int deserial_init (bool restore )
0 commit comments