+static int
+app_wrk_send_fd (app_worker_t *app_wrk, int fd)
+{
+ if (!appns_sapi_enabled ())
+ {
+ vl_api_registration_t *reg;
+ clib_error_t *error;
+
+ reg =
+ vl_mem_api_client_index_to_registration (app_wrk->api_client_index);
+ if (!reg)
+ {
+ clib_warning ("no api registration for client: %u",
+ app_wrk->api_client_index);
+ return -1;
+ }
+
+ if (vl_api_registration_file_index (reg) == VL_API_INVALID_FI)
+ return -1;
+
+ error = vl_api_send_fd_msg (reg, &fd, 1);
+ if (error)
+ {
+ clib_error_report (error);
+ return -1;
+ }
+
+ return 0;
+ }
+
+ app_sapi_msg_t smsg = { 0 };
+ app_namespace_t *app_ns;
+ clib_error_t *error;
+ application_t *app;
+ clib_socket_t *cs;
+ u32 cs_index;
+
+ app = application_get (app_wrk->app_index);
+ app_ns = app_namespace_get (app->ns_index);
+ cs_index = appns_sapi_handle_sock_index (app_wrk->api_client_index);
+ cs = appns_sapi_get_socket (app_ns, cs_index);
+ if (PREDICT_FALSE (!cs))
+ return -1;
+
+ /* There's no payload for the message only the type */
+ smsg.type = APP_SAPI_MSG_TYPE_SEND_FDS;
+ error = clib_socket_sendmsg (cs, &smsg, sizeof (smsg), &fd, 1);
+ if (error)
+ {
+ clib_error_report (error);
+ return -1;
+ }
+
+ return 0;
+}
+
+static int
+mq_try_lock_and_alloc_msg (svm_msg_q_t *mq, session_mq_rings_e ring,
+ svm_msg_q_msg_t *msg)
+{
+ int rv, n_try = 0;
+
+ while (n_try < 75)
+ {
+ rv = svm_msg_q_lock_and_alloc_msg_w_ring (mq, ring, SVM_Q_NOWAIT, msg);
+ if (!rv)
+ return 0;
+ /*
+ * Break the loop if mq is full, usually this is because the
+ * app has crashed or is hanging on somewhere.
+ */
+ if (rv != -1)
+ break;
+ n_try += 1;
+ usleep (1);
+ }
+
+ return -1;
+}
+
+typedef union app_wrk_mq_rpc_args_
+{
+ struct
+ {
+ u32 thread_index;
+ u32 app_wrk_index;
+ };
+ uword as_uword;
+} app_wrk_mq_rpc_ags_t;
+
+static int
+app_wrk_handle_mq_postponed_msgs (void *arg)
+{
+ svm_msg_q_msg_t _mq_msg, *mq_msg = &_mq_msg;
+ app_wrk_postponed_msg_t *pm;
+ app_wrk_mq_rpc_ags_t args;
+ u32 max_msg, n_msg = 0;
+ app_worker_t *app_wrk;
+ session_event_t *evt;
+ svm_msg_q_t *mq;
+
+ args.as_uword = pointer_to_uword (arg);
+ app_wrk = app_worker_get_if_valid (args.app_wrk_index);
+ if (!app_wrk)
+ return 0;
+
+ mq = app_wrk->event_queue;
+
+ clib_spinlock_lock (&app_wrk->postponed_mq_msgs_lock);
+
+ max_msg = clib_min (32, clib_fifo_elts (app_wrk->postponed_mq_msgs));
+
+ while (n_msg < max_msg)
+ {
+ pm = clib_fifo_head (app_wrk->postponed_mq_msgs);
+ if (mq_try_lock_and_alloc_msg (mq, pm->ring, mq_msg))
+ break;
+
+ evt = svm_msg_q_msg_data (mq, mq_msg);
+ clib_memset (evt, 0, sizeof (*evt));
+ evt->event_type = pm->event_type;
+ clib_memcpy_fast (evt->data, pm->data, pm->len);
+
+ if (pm->fd != -1)
+ app_wrk_send_fd (app_wrk, pm->fd);
+
+ svm_msg_q_add_and_unlock (mq, mq_msg);
+
+ clib_fifo_advance_head (app_wrk->postponed_mq_msgs, 1);
+ n_msg += 1;
+ }
+
+ if (!clib_fifo_elts (app_wrk->postponed_mq_msgs))
+ {
+ app_wrk->mq_congested = 0;
+ }
+ else
+ {
+ session_send_rpc_evt_to_thread_force (
+ args.thread_index, app_wrk_handle_mq_postponed_msgs,
+ uword_to_pointer (args.as_uword, void *));
+ }
+
+ clib_spinlock_unlock (&app_wrk->postponed_mq_msgs_lock);
+
+ return 0;
+}
+
+static void
+app_wrk_add_mq_postponed_msg (app_worker_t *app_wrk, session_mq_rings_e ring,
+ u8 evt_type, void *msg, u32 msg_len, int fd)
+{
+ app_wrk_postponed_msg_t *pm;
+
+ clib_spinlock_lock (&app_wrk->postponed_mq_msgs_lock);
+
+ app_wrk->mq_congested = 1;
+
+ clib_fifo_add2 (app_wrk->postponed_mq_msgs, pm);
+ clib_memcpy_fast (pm->data, msg, msg_len);
+ pm->event_type = evt_type;
+ pm->ring = ring;
+ pm->len = msg_len;
+ pm->fd = fd;
+
+ if (clib_fifo_elts (app_wrk->postponed_mq_msgs) == 1)
+ {
+ app_wrk_mq_rpc_ags_t args = { .thread_index = vlib_get_thread_index (),
+ .app_wrk_index = app_wrk->wrk_index };
+
+ session_send_rpc_evt_to_thread_force (
+ args.thread_index, app_wrk_handle_mq_postponed_msgs,
+ uword_to_pointer (args.as_uword, void *));
+ }
+
+ clib_spinlock_unlock (&app_wrk->postponed_mq_msgs_lock);
+}
+
+always_inline void
+app_wrk_send_ctrl_evt_inline (app_worker_t *app_wrk, u8 evt_type, void *msg,
+ u32 msg_len, int fd)
+{
+ svm_msg_q_msg_t _mq_msg, *mq_msg = &_mq_msg;
+ svm_msg_q_t *mq = app_wrk->event_queue;
+ session_event_t *evt;
+ int rv;
+
+ if (PREDICT_FALSE (app_wrk->mq_congested))
+ goto handle_congestion;
+
+ rv = mq_try_lock_and_alloc_msg (mq, SESSION_MQ_CTRL_EVT_RING, mq_msg);
+ if (PREDICT_FALSE (rv))
+ goto handle_congestion;
+
+ evt = svm_msg_q_msg_data (mq, mq_msg);
+ clib_memset (evt, 0, sizeof (*evt));
+ evt->event_type = evt_type;
+ clib_memcpy_fast (evt->data, msg, msg_len);
+
+ if (fd != -1)
+ app_wrk_send_fd (app_wrk, fd);
+
+ svm_msg_q_add_and_unlock (mq, mq_msg);
+
+ return;
+
+handle_congestion:
+
+ app_wrk_add_mq_postponed_msg (app_wrk, SESSION_MQ_CTRL_EVT_RING, evt_type,
+ msg, msg_len, fd);
+}
+
+void
+app_wrk_send_ctrl_evt_fd (app_worker_t *app_wrk, u8 evt_type, void *msg,
+ u32 msg_len, int fd)
+{
+ app_wrk_send_ctrl_evt_inline (app_wrk, evt_type, msg, msg_len, fd);
+}
+
+void
+app_wrk_send_ctrl_evt (app_worker_t *app_wrk, u8 evt_type, void *msg,
+ u32 msg_len)
+{
+ app_wrk_send_ctrl_evt_inline (app_wrk, evt_type, msg, msg_len, -1);
+}
+