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