Florin Coras | 0242d30 | 2022-12-22 15:03:44 -0800 | [diff] [blame] | 1 | /* SPDX-License-Identifier: Apache-2.0 |
| 2 | * Copyright(c) 2023 Cisco Systems, Inc. |
| 3 | */ |
| 4 | |
| 5 | #include <vnet/session/session.h> |
| 6 | #include <vnet/session/application.h> |
| 7 | |
| 8 | static inline int |
| 9 | mq_try_lock (svm_msg_q_t *mq) |
| 10 | { |
| 11 | int rv, n_try = 0; |
| 12 | |
| 13 | while (n_try < 100) |
| 14 | { |
| 15 | rv = svm_msg_q_try_lock (mq); |
| 16 | if (!rv) |
| 17 | return 0; |
| 18 | n_try += 1; |
| 19 | usleep (1); |
| 20 | } |
| 21 | |
| 22 | return -1; |
| 23 | } |
| 24 | |
| 25 | always_inline u8 |
| 26 | mq_event_ring_index (session_evt_type_t et) |
| 27 | { |
| 28 | return (et >= SESSION_CTRL_EVT_RPC ? SESSION_MQ_CTRL_EVT_RING : |
| 29 | SESSION_MQ_IO_EVT_RING); |
| 30 | } |
| 31 | |
| 32 | void |
| 33 | app_worker_del_all_events (app_worker_t *app_wrk) |
| 34 | { |
| 35 | session_worker_t *wrk; |
| 36 | session_event_t *evt; |
| 37 | u32 thread_index; |
| 38 | session_t *s; |
| 39 | |
| 40 | for (thread_index = 0; thread_index < vec_len (app_wrk->wrk_evts); |
| 41 | thread_index++) |
| 42 | { |
| 43 | while (clib_fifo_elts (app_wrk->wrk_evts[thread_index])) |
| 44 | { |
| 45 | clib_fifo_sub2 (app_wrk->wrk_evts[thread_index], evt); |
| 46 | switch (evt->event_type) |
| 47 | { |
| 48 | case SESSION_CTRL_EVT_MIGRATED: |
| 49 | s = session_get (evt->session_index, thread_index); |
| 50 | transport_cleanup (session_get_transport_proto (s), |
| 51 | s->connection_index, s->thread_index); |
| 52 | session_free (s); |
| 53 | break; |
| 54 | case SESSION_CTRL_EVT_CLEANUP: |
| 55 | s = session_get (evt->as_u64[0] & 0xffffffff, thread_index); |
| 56 | if (evt->as_u64[0] >> 32 != SESSION_CLEANUP_SESSION) |
| 57 | break; |
| 58 | uword_to_pointer (evt->as_u64[1], void (*) (session_t * s)) (s); |
| 59 | break; |
| 60 | case SESSION_CTRL_EVT_HALF_CLEANUP: |
| 61 | s = ho_session_get (evt->session_index); |
| 62 | pool_put_index (app_wrk->half_open_table, s->ho_index); |
| 63 | session_free (s); |
| 64 | break; |
| 65 | default: |
| 66 | break; |
| 67 | } |
| 68 | } |
| 69 | wrk = session_main_get_worker (thread_index); |
| 70 | clib_bitmap_set (wrk->app_wrks_pending_ntf, app_wrk->wrk_index, 0); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | always_inline int |
| 75 | app_worker_flush_events_inline (app_worker_t *app_wrk, u32 thread_index, |
| 76 | u8 is_builtin) |
| 77 | { |
| 78 | application_t *app = application_get (app_wrk->app_index); |
| 79 | svm_msg_q_t *mq = app_wrk->event_queue; |
| 80 | session_event_t *evt; |
| 81 | u32 n_evts = 128, i; |
| 82 | u8 ring_index, mq_is_cong; |
| 83 | session_t *s; |
| 84 | |
| 85 | n_evts = clib_min (n_evts, clib_fifo_elts (app_wrk->wrk_evts[thread_index])); |
| 86 | |
| 87 | if (!is_builtin) |
| 88 | { |
| 89 | mq_is_cong = app_worker_mq_is_congested (app_wrk); |
| 90 | if (mq_try_lock (mq)) |
| 91 | { |
| 92 | app_worker_set_mq_wrk_congested (app_wrk, thread_index); |
| 93 | return 0; |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | for (i = 0; i < n_evts; i++) |
| 98 | { |
| 99 | evt = clib_fifo_head (app_wrk->wrk_evts[thread_index]); |
| 100 | if (!is_builtin) |
| 101 | { |
| 102 | ring_index = mq_event_ring_index (evt->event_type); |
| 103 | if (svm_msg_q_or_ring_is_full (mq, ring_index)) |
| 104 | { |
| 105 | app_worker_set_mq_wrk_congested (app_wrk, thread_index); |
| 106 | break; |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | switch (evt->event_type) |
| 111 | { |
| 112 | case SESSION_IO_EVT_RX: |
| 113 | s = session_get (evt->session_index, thread_index); |
| 114 | s->flags &= ~SESSION_F_RX_EVT; |
| 115 | /* Application didn't confirm accept yet */ |
| 116 | if (PREDICT_FALSE (s->session_state == SESSION_STATE_ACCEPTING)) |
| 117 | break; |
| 118 | app->cb_fns.builtin_app_rx_callback (s); |
| 119 | break; |
| 120 | /* Handle sessions that might not be on current thread */ |
| 121 | case SESSION_IO_EVT_BUILTIN_RX: |
| 122 | s = session_get_from_handle_if_valid (evt->session_handle); |
| 123 | if (!s || s->session_state == SESSION_STATE_ACCEPTING) |
| 124 | break; |
Florin Coras | 48cd559 | 2023-09-06 19:38:44 -0700 | [diff] [blame] | 125 | s->flags &= ~SESSION_F_RX_EVT; |
Florin Coras | 0242d30 | 2022-12-22 15:03:44 -0800 | [diff] [blame] | 126 | app->cb_fns.builtin_app_rx_callback (s); |
| 127 | break; |
| 128 | case SESSION_IO_EVT_TX: |
| 129 | s = session_get (evt->session_index, thread_index); |
| 130 | app->cb_fns.builtin_app_tx_callback (s); |
| 131 | break; |
| 132 | case SESSION_IO_EVT_TX_MAIN: |
| 133 | s = session_get_from_handle_if_valid (evt->session_handle); |
| 134 | if (!s) |
| 135 | break; |
| 136 | app->cb_fns.builtin_app_tx_callback (s); |
| 137 | break; |
| 138 | case SESSION_CTRL_EVT_BOUND: |
| 139 | /* No app cb function currently */ |
| 140 | if (is_builtin) |
| 141 | break; |
| 142 | mq_send_session_bound_cb (app_wrk->wrk_index, evt->as_u64[1] >> 32, |
| 143 | evt->session_handle, |
| 144 | evt->as_u64[1] & 0xffffffff); |
| 145 | break; |
| 146 | case SESSION_CTRL_EVT_ACCEPTED: |
| 147 | s = session_get (evt->session_index, thread_index); |
| 148 | app->cb_fns.session_accept_callback (s); |
| 149 | break; |
| 150 | case SESSION_CTRL_EVT_CONNECTED: |
| 151 | if (!(evt->as_u64[1] & 0xffffffff)) |
| 152 | s = session_get (evt->session_index, thread_index); |
| 153 | else |
| 154 | s = 0; |
| 155 | app->cb_fns.session_connected_callback (app_wrk->wrk_index, |
| 156 | evt->as_u64[1] >> 32, s, |
| 157 | evt->as_u64[1] & 0xffffffff); |
| 158 | break; |
| 159 | case SESSION_CTRL_EVT_DISCONNECTED: |
| 160 | s = session_get (evt->session_index, thread_index); |
| 161 | app->cb_fns.session_disconnect_callback (s); |
| 162 | break; |
| 163 | case SESSION_CTRL_EVT_RESET: |
| 164 | s = session_get (evt->session_index, thread_index); |
| 165 | app->cb_fns.session_reset_callback (s); |
| 166 | break; |
| 167 | case SESSION_CTRL_EVT_UNLISTEN_REPLY: |
| 168 | if (is_builtin) |
| 169 | break; |
| 170 | mq_send_unlisten_reply (app_wrk, evt->session_handle, |
| 171 | evt->as_u64[1] >> 32, |
| 172 | evt->as_u64[1] & 0xffffffff); |
| 173 | break; |
| 174 | case SESSION_CTRL_EVT_MIGRATED: |
| 175 | s = session_get (evt->session_index, thread_index); |
| 176 | app->cb_fns.session_migrate_callback (s, evt->as_u64[1]); |
| 177 | transport_cleanup (session_get_transport_proto (s), |
| 178 | s->connection_index, s->thread_index); |
| 179 | session_free (s); |
| 180 | /* Notify app that it has data on the new session */ |
| 181 | s = session_get_from_handle (evt->as_u64[1]); |
| 182 | session_send_io_evt_to_thread (s->rx_fifo, |
| 183 | SESSION_IO_EVT_BUILTIN_RX); |
| 184 | break; |
| 185 | case SESSION_CTRL_EVT_TRANSPORT_CLOSED: |
| 186 | s = session_get (evt->session_index, thread_index); |
| 187 | if (app->cb_fns.session_transport_closed_callback) |
| 188 | app->cb_fns.session_transport_closed_callback (s); |
| 189 | break; |
| 190 | case SESSION_CTRL_EVT_CLEANUP: |
| 191 | s = session_get (evt->as_u64[0] & 0xffffffff, thread_index); |
| 192 | if (app->cb_fns.session_cleanup_callback) |
| 193 | app->cb_fns.session_cleanup_callback (s, evt->as_u64[0] >> 32); |
| 194 | if (evt->as_u64[0] >> 32 != SESSION_CLEANUP_SESSION) |
| 195 | break; |
| 196 | uword_to_pointer (evt->as_u64[1], void (*) (session_t * s)) (s); |
| 197 | break; |
| 198 | case SESSION_CTRL_EVT_HALF_CLEANUP: |
| 199 | s = ho_session_get (evt->session_index); |
| 200 | ASSERT (session_vlib_thread_is_cl_thread ()); |
| 201 | if (app->cb_fns.half_open_cleanup_callback) |
| 202 | app->cb_fns.half_open_cleanup_callback (s); |
| 203 | pool_put_index (app_wrk->half_open_table, s->ho_index); |
| 204 | session_free (s); |
| 205 | break; |
| 206 | case SESSION_CTRL_EVT_APP_ADD_SEGMENT: |
| 207 | app->cb_fns.add_segment_callback (app_wrk->wrk_index, |
| 208 | evt->as_u64[1]); |
| 209 | break; |
| 210 | case SESSION_CTRL_EVT_APP_DEL_SEGMENT: |
| 211 | app->cb_fns.del_segment_callback (app_wrk->wrk_index, |
| 212 | evt->as_u64[1]); |
| 213 | break; |
| 214 | default: |
| 215 | clib_warning ("unexpected event: %u", evt->event_type); |
| 216 | ASSERT (0); |
| 217 | break; |
| 218 | } |
| 219 | clib_fifo_advance_head (app_wrk->wrk_evts[thread_index], 1); |
| 220 | } |
| 221 | |
| 222 | if (!is_builtin) |
| 223 | { |
| 224 | svm_msg_q_unlock (mq); |
| 225 | if (mq_is_cong && i == n_evts) |
| 226 | app_worker_unset_wrk_mq_congested (app_wrk, thread_index); |
| 227 | } |
| 228 | |
| 229 | return 0; |
| 230 | } |
| 231 | |
| 232 | int |
| 233 | app_wrk_flush_wrk_events (app_worker_t *app_wrk, u32 thread_index) |
| 234 | { |
| 235 | if (app_worker_application_is_builtin (app_wrk)) |
| 236 | return app_worker_flush_events_inline (app_wrk, thread_index, |
| 237 | 1 /* is_builtin */); |
| 238 | else |
| 239 | return app_worker_flush_events_inline (app_wrk, thread_index, |
| 240 | 0 /* is_builtin */); |
| 241 | } |
| 242 | |
| 243 | static inline int |
| 244 | session_wrk_flush_events (session_worker_t *wrk) |
| 245 | { |
| 246 | app_worker_t *app_wrk; |
| 247 | uword app_wrk_index; |
| 248 | u32 thread_index; |
| 249 | |
| 250 | thread_index = wrk->vm->thread_index; |
| 251 | app_wrk_index = clib_bitmap_first_set (wrk->app_wrks_pending_ntf); |
| 252 | |
| 253 | while (app_wrk_index != ~0) |
| 254 | { |
| 255 | app_wrk = app_worker_get_if_valid (app_wrk_index); |
| 256 | /* app_wrk events are flushed on free, so should be valid here */ |
| 257 | ASSERT (app_wrk != 0); |
| 258 | app_wrk_flush_wrk_events (app_wrk, thread_index); |
| 259 | |
| 260 | if (!clib_fifo_elts (app_wrk->wrk_evts[thread_index])) |
| 261 | clib_bitmap_set (wrk->app_wrks_pending_ntf, app_wrk->wrk_index, 0); |
| 262 | |
| 263 | app_wrk_index = |
| 264 | clib_bitmap_next_set (wrk->app_wrks_pending_ntf, app_wrk_index + 1); |
| 265 | } |
| 266 | |
| 267 | if (!clib_bitmap_is_zero (wrk->app_wrks_pending_ntf)) |
| 268 | vlib_node_set_interrupt_pending (wrk->vm, session_input_node.index); |
| 269 | |
| 270 | return 0; |
| 271 | } |
| 272 | |
| 273 | VLIB_NODE_FN (session_input_node) |
| 274 | (vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame) |
| 275 | { |
| 276 | u32 thread_index = vm->thread_index; |
| 277 | session_worker_t *wrk; |
| 278 | |
| 279 | wrk = session_main_get_worker (thread_index); |
| 280 | session_wrk_flush_events (wrk); |
| 281 | |
| 282 | return 0; |
| 283 | } |
| 284 | |
| 285 | VLIB_REGISTER_NODE (session_input_node) = { |
| 286 | .name = "session-input", |
| 287 | .type = VLIB_NODE_TYPE_INPUT, |
| 288 | .state = VLIB_NODE_STATE_DISABLED, |
| 289 | }; |
| 290 | |
| 291 | /* |
| 292 | * fd.io coding-style-patch-verification: ON |
| 293 | * |
| 294 | * Local Variables: |
| 295 | * eval: (c-set-style "gnu") |
| 296 | * End: |
| 297 | */ |