blob: 42d71819b0a71b09c66c340519e3e7e5d81c65a9 [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 Coras0242d302022-12-22 15:03:44 -0800116 /* Application didn't confirm accept yet */
Florin Coras431b4892023-10-07 14:22:52 -0700117 if (PREDICT_FALSE (s->session_state == SESSION_STATE_ACCEPTING ||
118 s->session_state == SESSION_STATE_CONNECTING))
Florin Coras0242d302022-12-22 15:03:44 -0800119 break;
Florin Coras431b4892023-10-07 14:22:52 -0700120 s->flags &= ~SESSION_F_RX_EVT;
Florin Coras0242d302022-12-22 15:03:44 -0800121 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 Coras431b4892023-10-07 14:22:52 -0700126 if (!s || s->session_state == SESSION_STATE_ACCEPTING ||
127 s->session_state == SESSION_STATE_CONNECTING)
Florin Coras0242d302022-12-22 15:03:44 -0800128 break;
Florin Coras48cd5592023-09-06 19:38:44 -0700129 s->flags &= ~SESSION_F_RX_EVT;
Florin Coras0242d302022-12-22 15:03:44 -0800130 app->cb_fns.builtin_app_rx_callback (s);
131 break;
132 case SESSION_IO_EVT_TX:
133 s = session_get (evt->session_index, thread_index);
134 app->cb_fns.builtin_app_tx_callback (s);
135 break;
136 case SESSION_IO_EVT_TX_MAIN:
137 s = session_get_from_handle_if_valid (evt->session_handle);
138 if (!s)
139 break;
140 app->cb_fns.builtin_app_tx_callback (s);
141 break;
142 case SESSION_CTRL_EVT_BOUND:
143 /* No app cb function currently */
144 if (is_builtin)
145 break;
146 mq_send_session_bound_cb (app_wrk->wrk_index, evt->as_u64[1] >> 32,
147 evt->session_handle,
148 evt->as_u64[1] & 0xffffffff);
149 break;
150 case SESSION_CTRL_EVT_ACCEPTED:
151 s = session_get (evt->session_index, thread_index);
Florin Coras84c9ee32023-10-09 18:25:39 -0700152 old_state = s->session_state;
Florin Coras9ffec142023-10-06 16:45:04 -0700153 if (app->cb_fns.session_accept_callback (s))
154 {
155 session_close (s);
156 s->app_wrk_index = SESSION_INVALID_INDEX;
157 break;
158 }
Florin Coras431b4892023-10-07 14:22:52 -0700159 if (is_builtin)
160 {
Florin Coras84c9ee32023-10-09 18:25:39 -0700161 if (old_state >= SESSION_STATE_TRANSPORT_CLOSING)
162 {
163 session_set_state (s, old_state);
164 app_worker_close_notify (app_wrk, s);
165 }
Florin Coras431b4892023-10-07 14:22:52 -0700166 }
Florin Coras0242d302022-12-22 15:03:44 -0800167 break;
168 case SESSION_CTRL_EVT_CONNECTED:
169 if (!(evt->as_u64[1] & 0xffffffff))
Florin Coras9ffec142023-10-06 16:45:04 -0700170 {
171 s = session_get (evt->session_index, thread_index);
Florin Coras84c9ee32023-10-09 18:25:39 -0700172 old_state = s->session_state;
Florin Coras9ffec142023-10-06 16:45:04 -0700173 }
Florin Coras0242d302022-12-22 15:03:44 -0800174 else
175 s = 0;
Florin Coras9ffec142023-10-06 16:45:04 -0700176 rv = app->cb_fns.session_connected_callback (
177 app_wrk->wrk_index, evt->as_u64[1] >> 32, s,
178 evt->as_u64[1] & 0xffffffff);
179 if (!s)
180 break;
181 if (rv)
182 {
183 session_close (s);
184 s->app_wrk_index = SESSION_INVALID_INDEX;
185 break;
186 }
Florin Coras84c9ee32023-10-09 18:25:39 -0700187 if (old_state >= SESSION_STATE_TRANSPORT_CLOSING)
188 {
189 session_set_state (s, old_state);
190 app_worker_close_notify (app_wrk, s);
191 }
Florin Coras0242d302022-12-22 15:03:44 -0800192 break;
193 case SESSION_CTRL_EVT_DISCONNECTED:
194 s = session_get (evt->session_index, thread_index);
195 app->cb_fns.session_disconnect_callback (s);
196 break;
197 case SESSION_CTRL_EVT_RESET:
198 s = session_get (evt->session_index, thread_index);
199 app->cb_fns.session_reset_callback (s);
200 break;
201 case SESSION_CTRL_EVT_UNLISTEN_REPLY:
202 if (is_builtin)
203 break;
204 mq_send_unlisten_reply (app_wrk, evt->session_handle,
205 evt->as_u64[1] >> 32,
206 evt->as_u64[1] & 0xffffffff);
207 break;
208 case SESSION_CTRL_EVT_MIGRATED:
209 s = session_get (evt->session_index, thread_index);
210 app->cb_fns.session_migrate_callback (s, evt->as_u64[1]);
211 transport_cleanup (session_get_transport_proto (s),
212 s->connection_index, s->thread_index);
213 session_free (s);
214 /* Notify app that it has data on the new session */
215 s = session_get_from_handle (evt->as_u64[1]);
216 session_send_io_evt_to_thread (s->rx_fifo,
217 SESSION_IO_EVT_BUILTIN_RX);
218 break;
219 case SESSION_CTRL_EVT_TRANSPORT_CLOSED:
220 s = session_get (evt->session_index, thread_index);
221 if (app->cb_fns.session_transport_closed_callback)
222 app->cb_fns.session_transport_closed_callback (s);
223 break;
224 case SESSION_CTRL_EVT_CLEANUP:
225 s = session_get (evt->as_u64[0] & 0xffffffff, thread_index);
226 if (app->cb_fns.session_cleanup_callback)
227 app->cb_fns.session_cleanup_callback (s, evt->as_u64[0] >> 32);
228 if (evt->as_u64[0] >> 32 != SESSION_CLEANUP_SESSION)
229 break;
230 uword_to_pointer (evt->as_u64[1], void (*) (session_t * s)) (s);
231 break;
232 case SESSION_CTRL_EVT_HALF_CLEANUP:
233 s = ho_session_get (evt->session_index);
234 ASSERT (session_vlib_thread_is_cl_thread ());
235 if (app->cb_fns.half_open_cleanup_callback)
236 app->cb_fns.half_open_cleanup_callback (s);
237 pool_put_index (app_wrk->half_open_table, s->ho_index);
238 session_free (s);
239 break;
240 case SESSION_CTRL_EVT_APP_ADD_SEGMENT:
241 app->cb_fns.add_segment_callback (app_wrk->wrk_index,
242 evt->as_u64[1]);
243 break;
244 case SESSION_CTRL_EVT_APP_DEL_SEGMENT:
245 app->cb_fns.del_segment_callback (app_wrk->wrk_index,
246 evt->as_u64[1]);
247 break;
248 default:
249 clib_warning ("unexpected event: %u", evt->event_type);
250 ASSERT (0);
251 break;
252 }
253 clib_fifo_advance_head (app_wrk->wrk_evts[thread_index], 1);
254 }
255
256 if (!is_builtin)
257 {
258 svm_msg_q_unlock (mq);
259 if (mq_is_cong && i == n_evts)
260 app_worker_unset_wrk_mq_congested (app_wrk, thread_index);
261 }
262
263 return 0;
264}
265
266int
267app_wrk_flush_wrk_events (app_worker_t *app_wrk, u32 thread_index)
268{
269 if (app_worker_application_is_builtin (app_wrk))
270 return app_worker_flush_events_inline (app_wrk, thread_index,
271 1 /* is_builtin */);
272 else
273 return app_worker_flush_events_inline (app_wrk, thread_index,
274 0 /* is_builtin */);
275}
276
277static inline int
278session_wrk_flush_events (session_worker_t *wrk)
279{
280 app_worker_t *app_wrk;
281 uword app_wrk_index;
282 u32 thread_index;
283
284 thread_index = wrk->vm->thread_index;
285 app_wrk_index = clib_bitmap_first_set (wrk->app_wrks_pending_ntf);
286
287 while (app_wrk_index != ~0)
288 {
289 app_wrk = app_worker_get_if_valid (app_wrk_index);
290 /* app_wrk events are flushed on free, so should be valid here */
291 ASSERT (app_wrk != 0);
292 app_wrk_flush_wrk_events (app_wrk, thread_index);
293
294 if (!clib_fifo_elts (app_wrk->wrk_evts[thread_index]))
295 clib_bitmap_set (wrk->app_wrks_pending_ntf, app_wrk->wrk_index, 0);
296
297 app_wrk_index =
298 clib_bitmap_next_set (wrk->app_wrks_pending_ntf, app_wrk_index + 1);
299 }
300
301 if (!clib_bitmap_is_zero (wrk->app_wrks_pending_ntf))
302 vlib_node_set_interrupt_pending (wrk->vm, session_input_node.index);
303
304 return 0;
305}
306
307VLIB_NODE_FN (session_input_node)
308(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
309{
310 u32 thread_index = vm->thread_index;
311 session_worker_t *wrk;
312
313 wrk = session_main_get_worker (thread_index);
314 session_wrk_flush_events (wrk);
315
316 return 0;
317}
318
319VLIB_REGISTER_NODE (session_input_node) = {
320 .name = "session-input",
321 .type = VLIB_NODE_TYPE_INPUT,
322 .state = VLIB_NODE_STATE_DISABLED,
323};
324
325/*
326 * fd.io coding-style-patch-verification: ON
327 *
328 * Local Variables:
329 * eval: (c-set-style "gnu")
330 * End:
331 */