blob: 91e4b90d167612a301132e37288bc6bbc3aee425 [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;
148 mq_send_session_bound_cb (app_wrk->wrk_index, evt->as_u64[1] >> 32,
149 evt->session_handle,
150 evt->as_u64[1] & 0xffffffff);
151 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 {
165 session_set_state (s, old_state);
166 app_worker_close_notify (app_wrk, s);
167 }
Florin Coras431b4892023-10-07 14:22:52 -0700168 }
Florin Coras0242d302022-12-22 15:03:44 -0800169 break;
170 case SESSION_CTRL_EVT_CONNECTED:
171 if (!(evt->as_u64[1] & 0xffffffff))
Florin Coras9ffec142023-10-06 16:45:04 -0700172 {
173 s = session_get (evt->session_index, thread_index);
Florin Coras84c9ee32023-10-09 18:25:39 -0700174 old_state = s->session_state;
Florin Coras9ffec142023-10-06 16:45:04 -0700175 }
Florin Coras0242d302022-12-22 15:03:44 -0800176 else
177 s = 0;
Florin Coras9ffec142023-10-06 16:45:04 -0700178 rv = app->cb_fns.session_connected_callback (
179 app_wrk->wrk_index, evt->as_u64[1] >> 32, s,
180 evt->as_u64[1] & 0xffffffff);
181 if (!s)
182 break;
183 if (rv)
184 {
185 session_close (s);
186 s->app_wrk_index = SESSION_INVALID_INDEX;
187 break;
188 }
Florin Coras84c9ee32023-10-09 18:25:39 -0700189 if (old_state >= SESSION_STATE_TRANSPORT_CLOSING)
190 {
191 session_set_state (s, old_state);
192 app_worker_close_notify (app_wrk, s);
193 }
Florin Coras0242d302022-12-22 15:03:44 -0800194 break;
195 case SESSION_CTRL_EVT_DISCONNECTED:
196 s = session_get (evt->session_index, thread_index);
197 app->cb_fns.session_disconnect_callback (s);
198 break;
199 case SESSION_CTRL_EVT_RESET:
200 s = session_get (evt->session_index, thread_index);
201 app->cb_fns.session_reset_callback (s);
202 break;
203 case SESSION_CTRL_EVT_UNLISTEN_REPLY:
204 if (is_builtin)
205 break;
206 mq_send_unlisten_reply (app_wrk, evt->session_handle,
207 evt->as_u64[1] >> 32,
208 evt->as_u64[1] & 0xffffffff);
209 break;
210 case SESSION_CTRL_EVT_MIGRATED:
211 s = session_get (evt->session_index, thread_index);
212 app->cb_fns.session_migrate_callback (s, evt->as_u64[1]);
213 transport_cleanup (session_get_transport_proto (s),
214 s->connection_index, s->thread_index);
215 session_free (s);
216 /* Notify app that it has data on the new session */
217 s = session_get_from_handle (evt->as_u64[1]);
218 session_send_io_evt_to_thread (s->rx_fifo,
219 SESSION_IO_EVT_BUILTIN_RX);
220 break;
221 case SESSION_CTRL_EVT_TRANSPORT_CLOSED:
222 s = session_get (evt->session_index, thread_index);
223 if (app->cb_fns.session_transport_closed_callback)
224 app->cb_fns.session_transport_closed_callback (s);
225 break;
226 case SESSION_CTRL_EVT_CLEANUP:
227 s = session_get (evt->as_u64[0] & 0xffffffff, thread_index);
228 if (app->cb_fns.session_cleanup_callback)
229 app->cb_fns.session_cleanup_callback (s, evt->as_u64[0] >> 32);
230 if (evt->as_u64[0] >> 32 != SESSION_CLEANUP_SESSION)
231 break;
232 uword_to_pointer (evt->as_u64[1], void (*) (session_t * s)) (s);
233 break;
234 case SESSION_CTRL_EVT_HALF_CLEANUP:
235 s = ho_session_get (evt->session_index);
236 ASSERT (session_vlib_thread_is_cl_thread ());
237 if (app->cb_fns.half_open_cleanup_callback)
238 app->cb_fns.half_open_cleanup_callback (s);
239 pool_put_index (app_wrk->half_open_table, s->ho_index);
240 session_free (s);
241 break;
242 case SESSION_CTRL_EVT_APP_ADD_SEGMENT:
243 app->cb_fns.add_segment_callback (app_wrk->wrk_index,
244 evt->as_u64[1]);
245 break;
246 case SESSION_CTRL_EVT_APP_DEL_SEGMENT:
247 app->cb_fns.del_segment_callback (app_wrk->wrk_index,
248 evt->as_u64[1]);
249 break;
250 default:
251 clib_warning ("unexpected event: %u", evt->event_type);
252 ASSERT (0);
253 break;
254 }
255 clib_fifo_advance_head (app_wrk->wrk_evts[thread_index], 1);
256 }
257
258 if (!is_builtin)
259 {
260 svm_msg_q_unlock (mq);
261 if (mq_is_cong && i == n_evts)
262 app_worker_unset_wrk_mq_congested (app_wrk, thread_index);
263 }
264
265 return 0;
266}
267
268int
269app_wrk_flush_wrk_events (app_worker_t *app_wrk, u32 thread_index)
270{
271 if (app_worker_application_is_builtin (app_wrk))
272 return app_worker_flush_events_inline (app_wrk, thread_index,
273 1 /* is_builtin */);
274 else
275 return app_worker_flush_events_inline (app_wrk, thread_index,
276 0 /* is_builtin */);
277}
278
279static inline int
280session_wrk_flush_events (session_worker_t *wrk)
281{
282 app_worker_t *app_wrk;
283 uword app_wrk_index;
284 u32 thread_index;
285
286 thread_index = wrk->vm->thread_index;
287 app_wrk_index = clib_bitmap_first_set (wrk->app_wrks_pending_ntf);
288
289 while (app_wrk_index != ~0)
290 {
291 app_wrk = app_worker_get_if_valid (app_wrk_index);
292 /* app_wrk events are flushed on free, so should be valid here */
293 ASSERT (app_wrk != 0);
294 app_wrk_flush_wrk_events (app_wrk, thread_index);
295
296 if (!clib_fifo_elts (app_wrk->wrk_evts[thread_index]))
297 clib_bitmap_set (wrk->app_wrks_pending_ntf, app_wrk->wrk_index, 0);
298
299 app_wrk_index =
300 clib_bitmap_next_set (wrk->app_wrks_pending_ntf, app_wrk_index + 1);
301 }
302
303 if (!clib_bitmap_is_zero (wrk->app_wrks_pending_ntf))
304 vlib_node_set_interrupt_pending (wrk->vm, session_input_node.index);
305
306 return 0;
307}
308
309VLIB_NODE_FN (session_input_node)
310(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
311{
312 u32 thread_index = vm->thread_index;
313 session_worker_t *wrk;
314
315 wrk = session_main_get_worker (thread_index);
316 session_wrk_flush_events (wrk);
317
318 return 0;
319}
320
321VLIB_REGISTER_NODE (session_input_node) = {
322 .name = "session-input",
323 .type = VLIB_NODE_TYPE_INPUT,
324 .state = VLIB_NODE_STATE_DISABLED,
325};
326
327/*
328 * fd.io coding-style-patch-verification: ON
329 *
330 * Local Variables:
331 * eval: (c-set-style "gnu")
332 * End:
333 */