Skip to content

Commit 064c349

Browse files
Keelan10mergify[bot]
authored andcommitted
bgpd: Free Memory for SRv6 Functions and Locator Chunks
Implement proper memory cleanup for SRv6 functions and locator chunks to prevent potential memory leaks. The list callback deletion functions have been set. The ASan leak log for reference: ``` *********************************************************************************** Address Sanitizer Error detected in bgp_srv6l3vpn_to_bgp_vrf.test_bgp_srv6l3vpn_to_bgp_vrf/r2.asan.bgpd.4180 ================================================================= ==4180==ERROR: LeakSanitizer: detected memory leaks Direct leak of 544 byte(s) in 2 object(s) allocated from: #0 0x7f8d176a0d28 in __interceptor_calloc (/usr/lib/x86_64-linux-gnu/libasan.so.4+0xded28) sonic-net#1 0x7f8d1709f238 in qcalloc lib/memory.c:105 sonic-net#2 0x55d5dba6ee75 in sid_register bgpd/bgp_mplsvpn.c:591 sonic-net#3 0x55d5dba6ee75 in alloc_new_sid bgpd/bgp_mplsvpn.c:712 sonic-net#4 0x55d5dba6f3ce in ensure_vrf_tovpn_sid_per_af bgpd/bgp_mplsvpn.c:758 sonic-net#5 0x55d5dba6fb94 in ensure_vrf_tovpn_sid bgpd/bgp_mplsvpn.c:849 sonic-net#6 0x55d5dba7f975 in vpn_leak_postchange bgpd/bgp_mplsvpn.h:299 sonic-net#7 0x55d5dba7f975 in vpn_leak_postchange_all bgpd/bgp_mplsvpn.c:3704 sonic-net#8 0x55d5dbbb6c66 in bgp_zebra_process_srv6_locator_chunk bgpd/bgp_zebra.c:3164 sonic-net#9 0x7f8d1716f08a in zclient_read lib/zclient.c:4459 sonic-net#10 0x7f8d1713f034 in event_call lib/event.c:1974 sonic-net#11 0x7f8d1708242b in frr_run lib/libfrr.c:1214 sonic-net#12 0x55d5db99d19d in main bgpd/bgp_main.c:510 sonic-net#13 0x7f8d160c5c86 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x21c86) Direct leak of 296 byte(s) in 1 object(s) allocated from: #0 0x7f8d176a0d28 in __interceptor_calloc (/usr/lib/x86_64-linux-gnu/libasan.so.4+0xded28) sonic-net#1 0x7f8d1709f238 in qcalloc lib/memory.c:105 sonic-net#2 0x7f8d170b1d5f in srv6_locator_chunk_alloc lib/srv6.c:135 sonic-net#3 0x55d5dbbb6a19 in bgp_zebra_process_srv6_locator_chunk bgpd/bgp_zebra.c:3144 sonic-net#4 0x7f8d1716f08a in zclient_read lib/zclient.c:4459 sonic-net#5 0x7f8d1713f034 in event_call lib/event.c:1974 sonic-net#6 0x7f8d1708242b in frr_run lib/libfrr.c:1214 sonic-net#7 0x55d5db99d19d in main bgpd/bgp_main.c:510 sonic-net#8 0x7f8d160c5c86 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x21c86) *********************************************************************************** ``` Signed-off-by: Keelan Cannoo <[email protected]> (cherry picked from commit 8e7044b)
1 parent a027be0 commit 064c349

File tree

7 files changed

+15
-4
lines changed

7 files changed

+15
-4
lines changed

bgpd/bgp_mplsvpn.c

+6-1
Original file line numberDiff line numberDiff line change
@@ -596,6 +596,11 @@ static void sid_register(struct bgp *bgp, const struct in6_addr *sid,
596596
listnode_add(bgp->srv6_functions, func);
597597
}
598598

599+
void srv6_function_free(struct bgp_srv6_function *func)
600+
{
601+
XFREE(MTYPE_BGP_SRV6_FUNCTION, func);
602+
}
603+
599604
void sid_unregister(struct bgp *bgp, const struct in6_addr *sid)
600605
{
601606
struct listnode *node, *nnode;
@@ -604,7 +609,7 @@ void sid_unregister(struct bgp *bgp, const struct in6_addr *sid)
604609
for (ALL_LIST_ELEMENTS(bgp->srv6_functions, node, nnode, func))
605610
if (sid_same(&func->sid, sid)) {
606611
listnode_delete(bgp->srv6_functions, func);
607-
XFREE(MTYPE_BGP_SRV6_FUNCTION, func);
612+
srv6_function_free(func);
608613
}
609614
}
610615

bgpd/bgp_vty.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ static int bgp_srv6_locator_unset(struct bgp *bgp)
310310
/* refresh functions */
311311
for (ALL_LIST_ELEMENTS(bgp->srv6_functions, node, nnode, func)) {
312312
listnode_delete(bgp->srv6_functions, func);
313-
XFREE(MTYPE_BGP_SRV6_FUNCTION, func);
313+
srv6_function_free(func);
314314
}
315315

316316
/* refresh tovpn_sid */

bgpd/bgp_zebra.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -3235,7 +3235,7 @@ static int bgp_zebra_process_srv6_locator_delete(ZAPI_CALLBACK_ARGS)
32353235
if (prefix_match((struct prefix *)&loc.prefix,
32363236
(struct prefix *)&tmp_prefi)) {
32373237
listnode_delete(bgp->srv6_functions, func);
3238-
XFREE(MTYPE_BGP_SRV6_FUNCTION, func);
3238+
srv6_function_free(func);
32393239
}
32403240
}
32413241

bgpd/bgpd.c

+2
Original file line numberDiff line numberDiff line change
@@ -1428,7 +1428,9 @@ static void bgp_srv6_init(struct bgp *bgp)
14281428
bgp->srv6_enabled = false;
14291429
memset(bgp->srv6_locator_name, 0, sizeof(bgp->srv6_locator_name));
14301430
bgp->srv6_locator_chunks = list_new();
1431+
bgp->srv6_locator_chunks->del = srv6_locator_chunk_list_free;
14311432
bgp->srv6_functions = list_new();
1433+
bgp->srv6_functions->del = (void (*)(void *))srv6_function_free;
14321434
}
14331435

14341436
static void bgp_srv6_cleanup(struct bgp *bgp)

bgpd/bgpd.h

+3
Original file line numberDiff line numberDiff line change
@@ -2721,6 +2721,9 @@ extern bool bgp_path_attribute_discard(struct peer *peer, char *buf,
27212721
size_t size);
27222722
extern bool bgp_path_attribute_treat_as_withdraw(struct peer *peer, char *buf,
27232723
size_t size);
2724+
2725+
extern void srv6_function_free(struct bgp_srv6_function *func);
2726+
27242727
#ifdef _FRR_ATTRIBUTE_PRINTFRR
27252728
/* clang-format off */
27262729
#pragma FRR printfrr_ext "%pBP" (struct peer *)

lib/srv6.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ const char *seg6local_context2str(char *str, size_t size,
108108
}
109109
}
110110

111-
static void srv6_locator_chunk_list_free(void *data)
111+
void srv6_locator_chunk_list_free(void *data)
112112
{
113113
struct srv6_locator_chunk *chunk = data;
114114

lib/srv6.h

+1
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,7 @@ int snprintf_seg6_segs(char *str,
252252
extern struct srv6_locator *srv6_locator_alloc(const char *name);
253253
extern struct srv6_locator_chunk *srv6_locator_chunk_alloc(void);
254254
extern void srv6_locator_free(struct srv6_locator *locator);
255+
extern void srv6_locator_chunk_list_free(void *data);
255256
extern void srv6_locator_chunk_free(struct srv6_locator_chunk **chunk);
256257
json_object *srv6_locator_chunk_json(const struct srv6_locator_chunk *chunk);
257258
json_object *srv6_locator_json(const struct srv6_locator *loc);

0 commit comments

Comments
 (0)