wireguard: fix sending peer events from worker threads 02/38502/3
authorAlexander Chernavin <[email protected]>
Thu, 16 Mar 2023 09:48:45 +0000 (09:48 +0000)
committerFan Zhang <[email protected]>
Mon, 20 Mar 2023 16:41:14 +0000 (16:41 +0000)
Type: fix

API clients can register for peer events (e.g. to be notified when
connection is established). In a multi-worker setup, peer events might
be triggered from a worker thread. In order to send a peer event to the
clients, an API message needs to be allocated and populated.

API messages allocation is only allowed from the main thread. Currently,
the code does not handle the case when a peer event is trying to be sent
from a worker thread. In debug builds, when this happens, it causes
SIGABRT in vl_msg_api_alloc_internal() because assertion "pool == 0 ||
vlib_get_thread_index () == 0" fails. In production builds, when this
happens, it might cause unexplained behavior.

There is a test that is supposed to catch this but all multi-worker
Wireguard tests are currently disabled. This problem is likely to be one
of the reasons they were disabled.

With this fix, when a peer event is triggered from a worker thread,
allocate and send corresponding API message from the main thread using
RPC.

Signed-off-by: Alexander Chernavin <[email protected]>
Change-Id: Ib3fe19f8070563b35732afd16c017411c089437e

src/plugins/wireguard/wireguard_api.c

index c5334e0..e736efc 100644 (file)
@@ -346,7 +346,7 @@ vl_api_want_wireguard_peer_events_t_handler (
   REPLY_MACRO (VL_API_WANT_WIREGUARD_PEER_EVENTS_REPLY);
 }
 
-void
+static void
 wg_api_send_peer_event (vl_api_registration_t *rp, index_t peer_index,
                        wg_peer_flags flags)
 {
@@ -360,10 +360,16 @@ wg_api_send_peer_event (vl_api_registration_t *rp, index_t peer_index,
   vl_api_send_msg (rp, (u8 *) mp);
 }
 
-void
-wg_api_peer_event (index_t peeri, wg_peer_flags flags)
+typedef struct
 {
-  wg_peer_t *peer = wg_peer_get (peeri);
+  index_t peeri;
+  wg_peer_flags flags;
+} wg_api_peer_event_args_t;
+
+static void
+wg_api_peer_event_cb (wg_api_peer_event_args_t *args)
+{
+  wg_peer_t *peer = wg_peer_get (args->peeri);
   vpe_client_registration_t *api_client;
   vl_api_registration_t *rp;
 
@@ -372,11 +378,23 @@ wg_api_peer_event (index_t peeri, wg_peer_flags flags)
       rp = vl_api_client_index_to_registration (api_client->client_index);
       if (rp)
        {
-         wg_api_send_peer_event (rp, peeri, flags);
+         wg_api_send_peer_event (rp, args->peeri, args->flags);
        }
     };
 }
 
+void
+wg_api_peer_event (index_t peeri, wg_peer_flags flags)
+{
+  wg_api_peer_event_args_t args = {
+    .peeri = peeri,
+    .flags = flags,
+  };
+
+  vl_api_rpc_call_main_thread (wg_api_peer_event_cb, (u8 *) &args,
+                              sizeof (args));
+}
+
 static void
 vl_api_wg_set_async_mode_t_handler (vl_api_wg_set_async_mode_t *mp)
 {