From 583d4c94dc69f624a44a8dfa5c82165f3ca27271 Mon Sep 17 00:00:00 2001 From: Gabriel Oginski Date: Thu, 6 Oct 2022 06:58:45 +0000 Subject: wireguard: add atomic mutex The initiate handshake process can be called a numbers times for each peers, then the main VPP thread called by Wireguard starting to allocate memory. This behaviour can lead to out of memory when VPP has a lot of Wireguard tunnels concurrently. This fix add mutex to send only once handshake initiate at time for each peers. Type: fix Signed-off-by: Gabriel Oginski Change-Id: I13b4b2d47021753926d42a38ccadb36a411c5b79 --- src/plugins/wireguard/wireguard_send.c | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) (limited to 'src/plugins/wireguard/wireguard_send.c') diff --git a/src/plugins/wireguard/wireguard_send.c b/src/plugins/wireguard/wireguard_send.c index 72fa11034bf..41b2e7706a1 100644 --- a/src/plugins/wireguard/wireguard_send.c +++ b/src/plugins/wireguard/wireguard_send.c @@ -214,8 +214,11 @@ wg_send_handshake_thread_fn (void *arg) wg_main_t *wmp = &wg_main; wg_peer_t *peer = wg_peer_get (a->peer_idx); + bool handshake; wg_send_handshake (wmp->vlib_main, peer, a->is_retry); + handshake = false; + __atomic_store_n (&peer->handshake_is_sent, handshake, __ATOMIC_RELEASE); return 0; } @@ -227,8 +230,18 @@ wg_send_handshake_from_mt (u32 peer_idx, bool is_retry) .is_retry = is_retry, }; - vl_api_rpc_call_main_thread (wg_send_handshake_thread_fn, - (u8 *) & a, sizeof (a)); + wg_peer_t *peer = wg_peer_get (peer_idx); + + bool handshake = + __atomic_load_n (&peer->handshake_is_sent, __ATOMIC_ACQUIRE); + + if (handshake == false) + { + handshake = true; + __atomic_store_n (&peer->handshake_is_sent, handshake, __ATOMIC_RELEASE); + vl_api_rpc_call_main_thread (wg_send_handshake_thread_fn, (u8 *) &a, + sizeof (a)); + } } bool -- cgit 1.2.3-korg