blob: e7a81c343f41c80ecb09d6e35b7afc9cf78b17f4 [file] [log] [blame]
Florin Coras0242d302022-12-22 15:03:44 -08001/* 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
8static inline int
9mq_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
25always_inline u8
26mq_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
32void
33app_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
74always_inline int
75app_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;
Florin Coras84c9ee32023-10-09 18:25:39 -070080 u8 ring_index, mq_is_cong;
81 session_state_t old_state;
Florin Coras0242d302022-12-22 15:03:44 -080082 session_event_t *evt;
83 u32 n_evts = 128, i;
Florin Coras0242d302022-12-22 15:03:44 -080084 session_t *s;
Florin Coras9ffec142023-10-06 16:45:04 -070085 int rv;
Florin Coras0242d302022-12-22 15:03:44 -080086
87 n_evts = clib_min (n_evts, clib_fifo_elts (app_wrk->wrk_evts[thread_index]));
88
89 if (!is_builtin)
90 {
91 mq_is_cong = app_worker_mq_is_congested (app_wrk);
92 if (mq_try_lock (mq))
93 {
94 app_worker_set_mq_wrk_congested (app_wrk, thread_index);
95 return 0;
96 }
97 }
98
99 for (i = 0; i < n_evts; i++)
100 {
101 evt = clib_fifo_head (app_wrk->wrk_evts[thread_index]);
102 if (!is_builtin)
103 {
104 ring_index = mq_event_ring_index (evt->event_type);
105 if (svm_msg_q_or_ring_is_full (mq, ring_index))
106 {
107 app_worker_set_mq_wrk_congested (app_wrk, thread_index);
108 break;
109 }
110 }
111
112 switch (evt->event_type)
113 {
114 case SESSION_IO_EVT_RX:
115 s = session_get (evt->session_index, thread_index);
Florin Corascfe64622023-11-16 21:16:13 -0800116 s->flags &= ~SESSION_F_RX_EVT;
Florin Coras0242d302022-12-22 15:03:44 -0800117 /* Application didn't confirm accept yet */
Florin Coras431b4892023-10-07 14:22:52 -0700118 if (PREDICT_FALSE (s->session_state == SESSION_STATE_ACCEPTING ||
119 s->session_state == SESSION_STATE_CONNECTING))
Florin Coras0242d302022-12-22 15:03:44 -0800120 break;
121 app->cb_fns.builtin_app_rx_callback (s);
122 break;
123 /* Handle sessions that might not be on current thread */
124 case SESSION_IO_EVT_BUILTIN_RX:
125 s = session_get_from_handle_if_valid (evt->session_handle);
Florin Corascfe64622023-11-16 21:16:13 -0800126 if (!s)
Florin Coras0242d302022-12-22 15:03:44 -0800127 break;
Florin Coras48cd5592023-09-06 19:38:44 -0700128 s->flags &= ~SESSION_F_RX_EVT;
Florin Corascfe64622023-11-16 21:16:13 -0800129 if (PREDICT_FALSE (s->session_state == SESSION_STATE_ACCEPTING ||
130 s->session_state == SESSION_STATE_CONNECTING))
131 break;
Florin Coras0242d302022-12-22 15:03:44 -0800132 app->cb_fns.builtin_app_rx_callback (s);
133 break;
134 case SESSION_IO_EVT_TX:
135 s = session_get (evt->session_index, thread_index);
136 app->cb_fns.builtin_app_tx_callback (s);
137 break;
138 case SESSION_IO_EVT_TX_MAIN:
139 s = session_get_from_handle_if_valid (evt->session_handle);
140 if (!s)
141 break;
142 app->cb_fns.builtin_app_tx_callback (s);
143 break;
144 case SESSION_CTRL_EVT_BOUND:
145 /* No app cb function currently */
146 if (is_builtin)
147 break;
Florin Coras4f3da9c2024-02-29 18:19:30 -0800148 app->cb_fns.session_listened_callback (
149 app_wrk->wrk_index, evt->as_u64[1] >> 32, evt->session_handle,
150 evt->as_u64[1] & 0xffffffff);
Florin Coras0242d302022-12-22 15:03:44 -0800151 break;
152 case SESSION_CTRL_EVT_ACCEPTED:
153 s = session_get (evt->session_index, thread_index);
Florin Coras84c9ee32023-10-09 18:25:39 -0700154 old_state = s->session_state;
Florin Coras9ffec142023-10-06 16:45:04 -0700155 if (app->cb_fns.session_accept_callback (s))
156 {
157 session_close (s);
Florin Corasd1364402024-04-04 22:09:58 -0700158 s->app_wrk_index = APP_INVALID_INDEX;
Florin Coras9ffec142023-10-06 16:45:04 -0700159 break;
160 }
Florin Coras431b4892023-10-07 14:22:52 -0700161 if (is_builtin)
162 {
Florin Coras84c9ee32023-10-09 18:25:39 -0700163 if (old_state >= SESSION_STATE_TRANSPORT_CLOSING)
164 {
Florin Coras7c90be52024-02-26 14:52:34 -0800165 session_set_state (s,
166 clib_max (old_state, s->session_state));
167 if (!(s->flags & SESSION_F_APP_CLOSED))
168 app->cb_fns.session_disconnect_callback (s);
Florin Coras84c9ee32023-10-09 18:25:39 -0700169 }
Florin Coras431b4892023-10-07 14:22:52 -0700170 }
Florin Coras0242d302022-12-22 15:03:44 -0800171 break;
172 case SESSION_CTRL_EVT_CONNECTED:
173 if (!(evt->as_u64[1] & 0xffffffff))
Florin Coras9ffec142023-10-06 16:45:04 -0700174 {
175 s = session_get (evt->session_index, thread_index);
Florin Coras84c9ee32023-10-09 18:25:39 -0700176 old_state = s->session_state;
Florin Coras9ffec142023-10-06 16:45:04 -0700177 }
Florin Coras0242d302022-12-22 15:03:44 -0800178 else
179 s = 0;
Florin Coras9ffec142023-10-06 16:45:04 -0700180 rv = app->cb_fns.session_connected_callback (
181 app_wrk->wrk_index, evt->as_u64[1] >> 32, s,
182 evt->as_u64[1] & 0xffffffff);
183 if (!s)
184 break;
185 if (rv)
186 {
187 session_close (s);
Florin Corasd1364402024-04-04 22:09:58 -0700188 s->app_wrk_index = APP_INVALID_INDEX;
Florin Coras9ffec142023-10-06 16:45:04 -0700189 break;
190 }
Florin Coras84c9ee32023-10-09 18:25:39 -0700191 if (old_state >= SESSION_STATE_TRANSPORT_CLOSING)
192 {
Florin Coras7c90be52024-02-26 14:52:34 -0800193 session_set_state (s, clib_max (old_state, s->session_state));
194 if (!(s->flags & SESSION_F_APP_CLOSED))
195 app->cb_fns.session_disconnect_callback (s);
Florin Coras84c9ee32023-10-09 18:25:39 -0700196 }
Florin Coras0242d302022-12-22 15:03:44 -0800197 break;
198 case SESSION_CTRL_EVT_DISCONNECTED:
199 s = session_get (evt->session_index, thread_index);
Florin Coras77680ae2024-01-22 17:04:36 -0800200 if (!(s->flags & SESSION_F_APP_CLOSED))
Florin Coras66abf322024-01-11 11:56:04 -0500201 app->cb_fns.session_disconnect_callback (s);
Florin Coras0242d302022-12-22 15:03:44 -0800202 break;
203 case SESSION_CTRL_EVT_RESET:
204 s = session_get (evt->session_index, thread_index);
Florin Coras77680ae2024-01-22 17:04:36 -0800205 if (!(s->flags & SESSION_F_APP_CLOSED))
Florin Coras66abf322024-01-11 11:56:04 -0500206 app->cb_fns.session_reset_callback (s);
Florin Coras0242d302022-12-22 15:03:44 -0800207 break;
208 case SESSION_CTRL_EVT_UNLISTEN_REPLY:
209 if (is_builtin)
210 break;
Florin Coras4f3da9c2024-02-29 18:19:30 -0800211 app->cb_fns.session_unlistened_callback (
212 app_wrk->wrk_index, evt->session_handle, evt->as_u64[1] >> 32,
213 evt->as_u64[1] & 0xffffffff);
Florin Coras0242d302022-12-22 15:03:44 -0800214 break;
215 case SESSION_CTRL_EVT_MIGRATED:
216 s = session_get (evt->session_index, thread_index);
217 app->cb_fns.session_migrate_callback (s, evt->as_u64[1]);
218 transport_cleanup (session_get_transport_proto (s),
219 s->connection_index, s->thread_index);
220 session_free (s);
221 /* Notify app that it has data on the new session */
222 s = session_get_from_handle (evt->as_u64[1]);
223 session_send_io_evt_to_thread (s->rx_fifo,
224 SESSION_IO_EVT_BUILTIN_RX);
225 break;
226 case SESSION_CTRL_EVT_TRANSPORT_CLOSED:
227 s = session_get (evt->session_index, thread_index);
Florin Coras461aaa82024-04-01 19:51:22 -0700228 /* Notification enqueued before session was refused by app */
229 if (PREDICT_FALSE (s->app_wrk_index == APP_INVALID_INDEX))
230 break;
Florin Coras0242d302022-12-22 15:03:44 -0800231 if (app->cb_fns.session_transport_closed_callback)
232 app->cb_fns.session_transport_closed_callback (s);
233 break;
234 case SESSION_CTRL_EVT_CLEANUP:
235 s = session_get (evt->as_u64[0] & 0xffffffff, thread_index);
Florin Coras461aaa82024-04-01 19:51:22 -0700236 /* Notification enqueued before session was refused by app */
Florin Corasd1364402024-04-04 22:09:58 -0700237 if (PREDICT_TRUE (s->app_wrk_index != APP_INVALID_INDEX))
238 {
239 if (app->cb_fns.session_cleanup_callback)
240 app->cb_fns.session_cleanup_callback (s, evt->as_u64[0] >> 32);
241 }
Florin Coras0242d302022-12-22 15:03:44 -0800242 if (evt->as_u64[0] >> 32 != SESSION_CLEANUP_SESSION)
243 break;
244 uword_to_pointer (evt->as_u64[1], void (*) (session_t * s)) (s);
245 break;
246 case SESSION_CTRL_EVT_HALF_CLEANUP:
247 s = ho_session_get (evt->session_index);
248 ASSERT (session_vlib_thread_is_cl_thread ());
249 if (app->cb_fns.half_open_cleanup_callback)
250 app->cb_fns.half_open_cleanup_callback (s);
251 pool_put_index (app_wrk->half_open_table, s->ho_index);
252 session_free (s);
253 break;
254 case SESSION_CTRL_EVT_APP_ADD_SEGMENT:
255 app->cb_fns.add_segment_callback (app_wrk->wrk_index,
256 evt->as_u64[1]);
257 break;
258 case SESSION_CTRL_EVT_APP_DEL_SEGMENT:
259 app->cb_fns.del_segment_callback (app_wrk->wrk_index,
260 evt->as_u64[1]);
261 break;
262 default:
263 clib_warning ("unexpected event: %u", evt->event_type);
264 ASSERT (0);
265 break;
266 }
267 clib_fifo_advance_head (app_wrk->wrk_evts[thread_index], 1);
268 }
269
270 if (!is_builtin)
271 {
272 svm_msg_q_unlock (mq);
273 if (mq_is_cong && i == n_evts)
274 app_worker_unset_wrk_mq_congested (app_wrk, thread_index);
275 }
276
277 return 0;
278}
279
280int
281app_wrk_flush_wrk_events (app_worker_t *app_wrk, u32 thread_index)
282{
283 if (app_worker_application_is_builtin (app_wrk))
284 return app_worker_flush_events_inline (app_wrk, thread_index,
285 1 /* is_builtin */);
286 else
287 return app_worker_flush_events_inline (app_wrk, thread_index,
288 0 /* is_builtin */);
289}
290
291static inline int
292session_wrk_flush_events (session_worker_t *wrk)
293{
294 app_worker_t *app_wrk;
295 uword app_wrk_index;
296 u32 thread_index;
297
298 thread_index = wrk->vm->thread_index;
299 app_wrk_index = clib_bitmap_first_set (wrk->app_wrks_pending_ntf);
300
301 while (app_wrk_index != ~0)
302 {
303 app_wrk = app_worker_get_if_valid (app_wrk_index);
304 /* app_wrk events are flushed on free, so should be valid here */
305 ASSERT (app_wrk != 0);
306 app_wrk_flush_wrk_events (app_wrk, thread_index);
307
308 if (!clib_fifo_elts (app_wrk->wrk_evts[thread_index]))
309 clib_bitmap_set (wrk->app_wrks_pending_ntf, app_wrk->wrk_index, 0);
310
311 app_wrk_index =
312 clib_bitmap_next_set (wrk->app_wrks_pending_ntf, app_wrk_index + 1);
313 }
314
315 if (!clib_bitmap_is_zero (wrk->app_wrks_pending_ntf))
316 vlib_node_set_interrupt_pending (wrk->vm, session_input_node.index);
317
318 return 0;
319}
320
321VLIB_NODE_FN (session_input_node)
322(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
323{
324 u32 thread_index = vm->thread_index;
325 session_worker_t *wrk;
326
327 wrk = session_main_get_worker (thread_index);
328 session_wrk_flush_events (wrk);
329
330 return 0;
331}
332
333VLIB_REGISTER_NODE (session_input_node) = {
334 .name = "session-input",
335 .type = VLIB_NODE_TYPE_INPUT,
336 .state = VLIB_NODE_STATE_DISABLED,
337};
338
339/*
340 * fd.io coding-style-patch-verification: ON
341 *
342 * Local Variables:
343 * eval: (c-set-style "gnu")
344 * End:
345 */