blob: 41e8beb8abcd7f9634e56b5fce4d229021137344 [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);
158 s->app_wrk_index = SESSION_INVALID_INDEX;
159 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);
188 s->app_wrk_index = SESSION_INVALID_INDEX;
189 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);
228 if (app->cb_fns.session_transport_closed_callback)
229 app->cb_fns.session_transport_closed_callback (s);
230 break;
231 case SESSION_CTRL_EVT_CLEANUP:
232 s = session_get (evt->as_u64[0] & 0xffffffff, thread_index);
233 if (app->cb_fns.session_cleanup_callback)
234 app->cb_fns.session_cleanup_callback (s, evt->as_u64[0] >> 32);
235 if (evt->as_u64[0] >> 32 != SESSION_CLEANUP_SESSION)
236 break;
237 uword_to_pointer (evt->as_u64[1], void (*) (session_t * s)) (s);
238 break;
239 case SESSION_CTRL_EVT_HALF_CLEANUP:
240 s = ho_session_get (evt->session_index);
241 ASSERT (session_vlib_thread_is_cl_thread ());
242 if (app->cb_fns.half_open_cleanup_callback)
243 app->cb_fns.half_open_cleanup_callback (s);
244 pool_put_index (app_wrk->half_open_table, s->ho_index);
245 session_free (s);
246 break;
247 case SESSION_CTRL_EVT_APP_ADD_SEGMENT:
248 app->cb_fns.add_segment_callback (app_wrk->wrk_index,
249 evt->as_u64[1]);
250 break;
251 case SESSION_CTRL_EVT_APP_DEL_SEGMENT:
252 app->cb_fns.del_segment_callback (app_wrk->wrk_index,
253 evt->as_u64[1]);
254 break;
255 default:
256 clib_warning ("unexpected event: %u", evt->event_type);
257 ASSERT (0);
258 break;
259 }
260 clib_fifo_advance_head (app_wrk->wrk_evts[thread_index], 1);
261 }
262
263 if (!is_builtin)
264 {
265 svm_msg_q_unlock (mq);
266 if (mq_is_cong && i == n_evts)
267 app_worker_unset_wrk_mq_congested (app_wrk, thread_index);
268 }
269
270 return 0;
271}
272
273int
274app_wrk_flush_wrk_events (app_worker_t *app_wrk, u32 thread_index)
275{
276 if (app_worker_application_is_builtin (app_wrk))
277 return app_worker_flush_events_inline (app_wrk, thread_index,
278 1 /* is_builtin */);
279 else
280 return app_worker_flush_events_inline (app_wrk, thread_index,
281 0 /* is_builtin */);
282}
283
284static inline int
285session_wrk_flush_events (session_worker_t *wrk)
286{
287 app_worker_t *app_wrk;
288 uword app_wrk_index;
289 u32 thread_index;
290
291 thread_index = wrk->vm->thread_index;
292 app_wrk_index = clib_bitmap_first_set (wrk->app_wrks_pending_ntf);
293
294 while (app_wrk_index != ~0)
295 {
296 app_wrk = app_worker_get_if_valid (app_wrk_index);
297 /* app_wrk events are flushed on free, so should be valid here */
298 ASSERT (app_wrk != 0);
299 app_wrk_flush_wrk_events (app_wrk, thread_index);
300
301 if (!clib_fifo_elts (app_wrk->wrk_evts[thread_index]))
302 clib_bitmap_set (wrk->app_wrks_pending_ntf, app_wrk->wrk_index, 0);
303
304 app_wrk_index =
305 clib_bitmap_next_set (wrk->app_wrks_pending_ntf, app_wrk_index + 1);
306 }
307
308 if (!clib_bitmap_is_zero (wrk->app_wrks_pending_ntf))
309 vlib_node_set_interrupt_pending (wrk->vm, session_input_node.index);
310
311 return 0;
312}
313
314VLIB_NODE_FN (session_input_node)
315(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
316{
317 u32 thread_index = vm->thread_index;
318 session_worker_t *wrk;
319
320 wrk = session_main_get_worker (thread_index);
321 session_wrk_flush_events (wrk);
322
323 return 0;
324}
325
326VLIB_REGISTER_NODE (session_input_node) = {
327 .name = "session-input",
328 .type = VLIB_NODE_TYPE_INPUT,
329 .state = VLIB_NODE_STATE_DISABLED,
330};
331
332/*
333 * fd.io coding-style-patch-verification: ON
334 *
335 * Local Variables:
336 * eval: (c-set-style "gnu")
337 * End:
338 */