blob: 9e4b147ccf079150fb2162778b924fa912a9cb3f [file] [log] [blame]
Florin Coras623eb562019-02-03 19:28:34 -08001/*
2 * Copyright (c) 2019 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16#include <vnet/session/application.h>
17#include <vnet/session/application_interface.h>
Florin Coras54a51fd2019-02-07 15:34:52 -080018#include <vnet/session/session.h>
Florin Coras623eb562019-02-03 19:28:34 -080019
20/**
21 * Pool of workers associated to apps
22 */
23static app_worker_t *app_workers;
24
Florin Coras623eb562019-02-03 19:28:34 -080025app_worker_t *
26app_worker_alloc (application_t * app)
27{
28 app_worker_t *app_wrk;
Florin Coras0242d302022-12-22 15:03:44 -080029
Florin Coras623eb562019-02-03 19:28:34 -080030 pool_get (app_workers, app_wrk);
31 clib_memset (app_wrk, 0, sizeof (*app_wrk));
32 app_wrk->wrk_index = app_wrk - app_workers;
33 app_wrk->app_index = app->app_index;
34 app_wrk->wrk_map_index = ~0;
35 app_wrk->connects_seg_manager = APP_INVALID_SEGMENT_MANAGER_INDEX;
Florin Corasc8e812f2020-05-14 05:32:18 +000036 clib_spinlock_init (&app_wrk->detached_seg_managers_lock);
Florin Coras0242d302022-12-22 15:03:44 -080037 vec_validate (app_wrk->wrk_evts, vlib_num_workers ());
38 vec_validate (app_wrk->wrk_mq_congested, vlib_num_workers ());
Nathan Skrzypczakba65ca42019-05-16 16:35:40 +020039 APP_DBG ("New app %v worker %u", app->name, app_wrk->wrk_index);
Florin Coras623eb562019-02-03 19:28:34 -080040 return app_wrk;
41}
42
43app_worker_t *
44app_worker_get (u32 wrk_index)
45{
46 return pool_elt_at_index (app_workers, wrk_index);
47}
48
49app_worker_t *
50app_worker_get_if_valid (u32 wrk_index)
51{
52 if (pool_is_free_index (app_workers, wrk_index))
53 return 0;
54 return pool_elt_at_index (app_workers, wrk_index);
55}
56
57void
58app_worker_free (app_worker_t * app_wrk)
59{
60 application_t *app = application_get (app_wrk->app_index);
Florin Corasc1a42652019-02-08 18:27:29 -080061 vnet_unlisten_args_t _a, *a = &_a;
Florin Corasbf395972020-04-30 15:05:24 +000062 u64 handle, *handles = 0, *sm_indices = 0;
Florin Coras623eb562019-02-03 19:28:34 -080063 segment_manager_t *sm;
Florin Corasea727642021-05-07 19:39:43 -070064 session_handle_t *sh;
Florin Coras87d66332019-06-11 12:31:31 -070065 session_t *ls;
Florin Coras623eb562019-02-03 19:28:34 -080066 u32 sm_index;
Florin Corasea727642021-05-07 19:39:43 -070067 int i;
Florin Coras623eb562019-02-03 19:28:34 -080068
69 /*
Florin Coras0242d302022-12-22 15:03:44 -080070 * Cleanup vpp wrk events
71 */
72 app_worker_del_all_events (app_wrk);
73 for (i = 0; i < vec_len (app_wrk->wrk_evts); i++)
74 clib_fifo_free (app_wrk->wrk_evts[i]);
75
76 vec_free (app_wrk->wrk_evts);
77 vec_free (app_wrk->wrk_mq_congested);
78
79 /*
Florin Coras623eb562019-02-03 19:28:34 -080080 * Listener cleanup
81 */
82
Florin Corasc9940fc2019-02-05 20:55:11 -080083 hash_foreach (handle, sm_index, app_wrk->listeners_table, ({
84 ls = listen_session_get_from_handle (handle);
Florin Coras87d66332019-06-11 12:31:31 -070085 vec_add1 (handles, app_listen_session_handle (ls));
Florin Corasbf395972020-04-30 15:05:24 +000086 vec_add1 (sm_indices, sm_index);
Florin Coras623eb562019-02-03 19:28:34 -080087 sm = segment_manager_get (sm_index);
Florin Coras623eb562019-02-03 19:28:34 -080088 }));
Florin Coras623eb562019-02-03 19:28:34 -080089
90 for (i = 0; i < vec_len (handles); i++)
91 {
Florin Corasbf395972020-04-30 15:05:24 +000092 /* Cleanup listener */
Florin Coras623eb562019-02-03 19:28:34 -080093 a->app_index = app->app_index;
94 a->wrk_map_index = app_wrk->wrk_map_index;
95 a->handle = handles[i];
Florin Corasc1a42652019-02-08 18:27:29 -080096 (void) vnet_unlisten (a);
Florin Corasbf395972020-04-30 15:05:24 +000097
98 sm = segment_manager_get_if_valid (sm_indices[i]);
99 if (sm && !segment_manager_app_detached (sm))
100 {
101 sm->first_is_protected = 0;
102 segment_manager_init_free (sm);
103 }
Florin Coras623eb562019-02-03 19:28:34 -0800104 }
Florin Corasd50ff7f2020-04-16 04:30:22 +0000105 vec_reset_length (handles);
Florin Corasbf395972020-04-30 15:05:24 +0000106 vec_free (sm_indices);
107 hash_free (app_wrk->listeners_table);
Florin Coras623eb562019-02-03 19:28:34 -0800108
109 /*
110 * Connects segment manager cleanup
111 */
112
113 if (app_wrk->connects_seg_manager != APP_INVALID_SEGMENT_MANAGER_INDEX)
114 {
115 sm = segment_manager_get (app_wrk->connects_seg_manager);
116 sm->app_wrk_index = SEGMENT_MANAGER_INVALID_APP_INDEX;
Florin Coras565115e2019-02-20 19:48:31 -0800117 sm->first_is_protected = 0;
Florin Coras88001c62019-04-24 14:44:46 -0700118 segment_manager_init_free (sm);
Florin Coras623eb562019-02-03 19:28:34 -0800119 }
120
Florin Corasd50ff7f2020-04-16 04:30:22 +0000121 /*
122 * Half-open cleanup
123 */
124
Florin Corasea727642021-05-07 19:39:43 -0700125 pool_foreach (sh, app_wrk->half_open_table)
126 session_cleanup_half_open (*sh);
Florin Corasd50ff7f2020-04-16 04:30:22 +0000127
Florin Corasea727642021-05-07 19:39:43 -0700128 pool_free (app_wrk->half_open_table);
Florin Corasd50ff7f2020-04-16 04:30:22 +0000129
Florin Corasc8e812f2020-05-14 05:32:18 +0000130 /*
131 * Detached listener segment managers cleanup
132 */
133 for (i = 0; i < vec_len (app_wrk->detached_seg_managers); i++)
134 {
135 sm = segment_manager_get (app_wrk->detached_seg_managers[i]);
136 segment_manager_init_free (sm);
137 }
138 vec_free (app_wrk->detached_seg_managers);
139 clib_spinlock_free (&app_wrk->detached_seg_managers_lock);
140
Florin Coras623eb562019-02-03 19:28:34 -0800141 if (CLIB_DEBUG)
142 clib_memset (app_wrk, 0xfe, sizeof (*app_wrk));
Benoît Ganned4aeb842019-07-18 18:38:42 +0200143 pool_put (app_workers, app_wrk);
Florin Coras623eb562019-02-03 19:28:34 -0800144}
145
146application_t *
147app_worker_get_app (u32 wrk_index)
148{
149 app_worker_t *app_wrk;
150 app_wrk = app_worker_get_if_valid (wrk_index);
151 if (!app_wrk)
152 return 0;
153 return application_get_if_valid (app_wrk->app_index);
154}
155
156static segment_manager_t *
157app_worker_alloc_segment_manager (app_worker_t * app_wrk)
158{
Florin Coras94a6df02021-05-06 15:32:14 -0700159 segment_manager_t *sm;
Florin Coras623eb562019-02-03 19:28:34 -0800160
Florin Coras94a6df02021-05-06 15:32:14 -0700161 sm = segment_manager_alloc ();
Florin Coras623eb562019-02-03 19:28:34 -0800162 sm->app_wrk_index = app_wrk->wrk_index;
Florin Corasa107f402020-09-29 19:18:46 -0700163 segment_manager_init (sm);
Florin Coras623eb562019-02-03 19:28:34 -0800164 return sm;
165}
166
Florin Corasa27a46e2019-02-18 13:02:28 -0800167static int
168app_worker_alloc_session_fifos (segment_manager_t * sm, session_t * s)
169{
170 svm_fifo_t *rx_fifo = 0, *tx_fifo = 0;
Florin Corasa27a46e2019-02-18 13:02:28 -0800171 int rv;
172
Florin Coras62ddc032019-12-08 18:30:42 -0800173 if ((rv = segment_manager_alloc_session_fifos (sm, s->thread_index,
174 &rx_fifo, &tx_fifo)))
Florin Corasa27a46e2019-02-18 13:02:28 -0800175 return rv;
176
Florin Corasc547e912020-12-08 17:50:45 -0800177 rx_fifo->shr->master_session_index = s->session_index;
Florin Corasa27a46e2019-02-18 13:02:28 -0800178 rx_fifo->master_thread_index = s->thread_index;
179
Florin Corasc547e912020-12-08 17:50:45 -0800180 tx_fifo->shr->master_session_index = s->session_index;
Florin Corasa27a46e2019-02-18 13:02:28 -0800181 tx_fifo->master_thread_index = s->thread_index;
182
183 s->rx_fifo = rx_fifo;
184 s->tx_fifo = tx_fifo;
Florin Corasa27a46e2019-02-18 13:02:28 -0800185 return 0;
186}
187
Florin Coras623eb562019-02-03 19:28:34 -0800188int
Florin Corasd4295e62019-02-22 13:11:38 -0800189app_worker_init_listener (app_worker_t * app_wrk, session_t * ls)
Florin Coras623eb562019-02-03 19:28:34 -0800190{
191 segment_manager_t *sm;
192
193 /* Allocate segment manager. All sessions derived out of a listen session
194 * have fifos allocated by the same segment manager. */
195 if (!(sm = app_worker_alloc_segment_manager (app_wrk)))
Florin Coras00e01d32019-10-21 16:07:46 -0700196 return SESSION_E_ALLOC;
Florin Coras623eb562019-02-03 19:28:34 -0800197
Florin Coras1a4aaf12021-11-27 10:30:03 -0800198 /* Once the first segment is mapped, don't remove it until unlisten */
199 sm->first_is_protected = 1;
200
Florin Corasc9940fc2019-02-05 20:55:11 -0800201 /* Keep track of the segment manager for the listener or this worker */
Florin Coras623eb562019-02-03 19:28:34 -0800202 hash_set (app_wrk->listeners_table, listen_session_get_handle (ls),
203 segment_manager_index (sm));
204
Florin Coras87b7e3d2020-03-27 15:06:07 +0000205 if (transport_connection_is_cless (session_get_transport (ls)))
Florin Coras623eb562019-02-03 19:28:34 -0800206 {
Florin Coras87b7e3d2020-03-27 15:06:07 +0000207 if (ls->rx_fifo)
Florin Coras00e01d32019-10-21 16:07:46 -0700208 return SESSION_E_NOSUPPORT;
209 return app_worker_alloc_session_fifos (sm, ls);
Florin Coras623eb562019-02-03 19:28:34 -0800210 }
211 return 0;
212}
213
Filip Tehlar0028e6f2023-06-28 10:47:32 +0200214session_error_t
215app_worker_start_listen (app_worker_t *app_wrk, app_listener_t *app_listener)
Florin Corasd4295e62019-02-22 13:11:38 -0800216{
217 session_t *ls;
Florin Coras00e01d32019-10-21 16:07:46 -0700218 int rv;
Florin Corasd4295e62019-02-22 13:11:38 -0800219
220 if (clib_bitmap_get (app_listener->workers, app_wrk->wrk_map_index))
Florin Coras00e01d32019-10-21 16:07:46 -0700221 return SESSION_E_ALREADY_LISTENING;
Florin Corasd4295e62019-02-22 13:11:38 -0800222
223 app_listener->workers = clib_bitmap_set (app_listener->workers,
224 app_wrk->wrk_map_index, 1);
225
226 if (app_listener->session_index != SESSION_INVALID_INDEX)
227 {
228 ls = session_get (app_listener->session_index, 0);
Florin Coras00e01d32019-10-21 16:07:46 -0700229 if ((rv = app_worker_init_listener (app_wrk, ls)))
230 return rv;
Florin Corasd4295e62019-02-22 13:11:38 -0800231 }
232
233 if (app_listener->local_index != SESSION_INVALID_INDEX)
234 {
235 ls = session_get (app_listener->local_index, 0);
Florin Coras00e01d32019-10-21 16:07:46 -0700236 if ((rv = app_worker_init_listener (app_wrk, ls)))
237 return rv;
Florin Corasd4295e62019-02-22 13:11:38 -0800238 }
239
240 return 0;
241}
242
Florin Coras2b81e3c2019-02-27 07:55:46 -0800243static void
Florin Corasc8e812f2020-05-14 05:32:18 +0000244app_worker_add_detached_sm (app_worker_t * app_wrk, u32 sm_index)
245{
246 vec_add1 (app_wrk->detached_seg_managers, sm_index);
247}
248
249void
250app_worker_del_detached_sm (app_worker_t * app_wrk, u32 sm_index)
251{
252 u32 i;
253
254 clib_spinlock_lock (&app_wrk->detached_seg_managers_lock);
255 for (i = 0; i < vec_len (app_wrk->detached_seg_managers); i++)
256 {
257 if (app_wrk->detached_seg_managers[i] == sm_index)
258 {
259 vec_del1 (app_wrk->detached_seg_managers, i);
260 break;
261 }
262 }
263 clib_spinlock_unlock (&app_wrk->detached_seg_managers_lock);
264}
265
266static void
Florin Coras2b81e3c2019-02-27 07:55:46 -0800267app_worker_stop_listen_session (app_worker_t * app_wrk, session_t * ls)
Florin Coras623eb562019-02-03 19:28:34 -0800268{
Florin Corasc9940fc2019-02-05 20:55:11 -0800269 session_handle_t handle;
Florin Coras623eb562019-02-03 19:28:34 -0800270 segment_manager_t *sm;
271 uword *sm_indexp;
liuyacan87d48ad2021-04-28 11:34:03 +0000272 session_state_t *states = 0;
Florin Coras2b81e3c2019-02-27 07:55:46 -0800273
274 handle = listen_session_get_handle (ls);
275 sm_indexp = hash_get (app_wrk->listeners_table, handle);
276 if (PREDICT_FALSE (!sm_indexp))
277 return;
278
Florin Coras1bd46162020-04-13 23:35:55 +0000279 /* Dealloc fifos, if any (dgram listeners) */
Florin Coras87b7e3d2020-03-27 15:06:07 +0000280 if (ls->rx_fifo)
281 {
282 segment_manager_dealloc_fifos (ls->rx_fifo, ls->tx_fifo);
283 ls->tx_fifo = ls->rx_fifo = 0;
284 }
285
Florin Coras1bd46162020-04-13 23:35:55 +0000286 /* Try to cleanup segment manager */
Florin Coras2b81e3c2019-02-27 07:55:46 -0800287 sm = segment_manager_get (*sm_indexp);
Florin Coras94a6df02021-05-06 15:32:14 -0700288 if (sm)
Florin Coras2b81e3c2019-02-27 07:55:46 -0800289 {
Florin Coras1a4aaf12021-11-27 10:30:03 -0800290 sm->first_is_protected = 0;
Florin Coras1bd46162020-04-13 23:35:55 +0000291 segment_manager_app_detach (sm);
292 if (!segment_manager_has_fifos (sm))
Florin Coras94a6df02021-05-06 15:32:14 -0700293 {
294 /* Empty segment manager, cleanup it up */
295 segment_manager_free (sm);
296 }
Florin Corasc8e812f2020-05-14 05:32:18 +0000297 else
298 {
Florin Coras94a6df02021-05-06 15:32:14 -0700299 /* Delete sessions in CREATED state */
300 vec_add1 (states, SESSION_STATE_CREATED);
301 segment_manager_del_sessions_filter (sm, states);
302 vec_free (states);
303
Florin Corasc8e812f2020-05-14 05:32:18 +0000304 /* Track segment manager in case app detaches and all the
305 * outstanding sessions need to be closed */
306 app_worker_add_detached_sm (app_wrk, *sm_indexp);
307 sm->flags |= SEG_MANAGER_F_DETACHED_LISTENER;
308 }
Florin Coras2b81e3c2019-02-27 07:55:46 -0800309 }
Florin Coras1bd46162020-04-13 23:35:55 +0000310
Florin Coras2b81e3c2019-02-27 07:55:46 -0800311 hash_unset (app_wrk->listeners_table, handle);
312}
313
314int
315app_worker_stop_listen (app_worker_t * app_wrk, app_listener_t * al)
316{
Florin Corasd4295e62019-02-22 13:11:38 -0800317 session_t *ls;
Florin Coras623eb562019-02-03 19:28:34 -0800318
Florin Corasc9940fc2019-02-05 20:55:11 -0800319 if (!clib_bitmap_get (al->workers, app_wrk->wrk_map_index))
320 return 0;
321
322 if (al->session_index != SESSION_INVALID_INDEX)
Florin Coras623eb562019-02-03 19:28:34 -0800323 {
Florin Corasc9940fc2019-02-05 20:55:11 -0800324 ls = listen_session_get (al->session_index);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800325 app_worker_stop_listen_session (app_wrk, ls);
Florin Coras623eb562019-02-03 19:28:34 -0800326 }
327
Florin Corasc9940fc2019-02-05 20:55:11 -0800328 if (al->local_index != SESSION_INVALID_INDEX)
Florin Coras623eb562019-02-03 19:28:34 -0800329 {
Florin Corasd4295e62019-02-22 13:11:38 -0800330 ls = listen_session_get (al->local_index);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800331 app_worker_stop_listen_session (app_wrk, ls);
Florin Coras623eb562019-02-03 19:28:34 -0800332 }
Florin Corasc9940fc2019-02-05 20:55:11 -0800333
334 clib_bitmap_set_no_check (al->workers, app_wrk->wrk_map_index, 0);
335 if (clib_bitmap_is_zero (al->workers))
336 app_listener_cleanup (al);
Florin Coras623eb562019-02-03 19:28:34 -0800337
338 return 0;
339}
340
341int
Florin Corasa27a46e2019-02-18 13:02:28 -0800342app_worker_init_accepted (session_t * s)
343{
344 app_worker_t *app_wrk;
345 segment_manager_t *sm;
346 session_t *listener;
Ryujiro Shibuyad8f48e22020-01-22 12:11:42 +0000347 application_t *app;
Florin Corasa27a46e2019-02-18 13:02:28 -0800348
Nathan Skrzypczak2f0f96b2019-06-13 10:14:28 +0200349 listener = listen_session_get_from_handle (s->listener_handle);
Florin Corasa27a46e2019-02-18 13:02:28 -0800350 app_wrk = application_listener_select_worker (listener);
Florin Coras0242d302022-12-22 15:03:44 -0800351 if (PREDICT_FALSE (app_worker_mq_is_congested (app_wrk)))
Florin Coras20c24232021-11-22 21:19:01 -0800352 return -1;
Florin Coras2b81e3c2019-02-27 07:55:46 -0800353
Florin Coras20c24232021-11-22 21:19:01 -0800354 s->app_wrk_index = app_wrk->wrk_index;
Ryujiro Shibuyad8f48e22020-01-22 12:11:42 +0000355 app = application_get (app_wrk->app_index);
356 if (app->cb_fns.fifo_tuning_callback)
357 s->flags |= SESSION_F_CUSTOM_FIFO_TUNING;
358
Florin Corasa27a46e2019-02-18 13:02:28 -0800359 sm = app_worker_get_listen_segment_manager (app_wrk, listener);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800360 if (app_worker_alloc_session_fifos (sm, s))
361 return -1;
362
363 return 0;
Florin Corasa27a46e2019-02-18 13:02:28 -0800364}
365
366int
Florin Coras0242d302022-12-22 15:03:44 -0800367app_worker_listened_notify (app_worker_t *app_wrk, session_handle_t alsh,
368 u32 opaque, session_error_t err)
369{
Florin Coras09c0e8f2023-08-11 14:40:13 -0700370 session_event_t evt = { .event_type = SESSION_CTRL_EVT_BOUND,
371 .as_u64[0] = alsh,
372 .as_u64[1] = (u64) opaque << 32 | err };
Florin Coras0242d302022-12-22 15:03:44 -0800373
374 app_worker_add_event_custom (app_wrk, 0 /* thread index */, &evt);
375
376 return 0;
377}
378
379int
380app_worker_unlisten_reply (app_worker_t *app_wrk, session_handle_t sh,
381 u32 opaque, session_error_t err)
382{
Florin Coras09c0e8f2023-08-11 14:40:13 -0700383 session_event_t evt = { .event_type = SESSION_CTRL_EVT_UNLISTEN_REPLY,
384 .as_u64[0] = sh,
385 .as_u64[1] = (u64) opaque << 32 | (u32) err };
Florin Coras0242d302022-12-22 15:03:44 -0800386
387 app_worker_add_event_custom (app_wrk, 0 /* thread index */, &evt);
388 return 0;
389}
390
391int
Florin Corasa27a46e2019-02-18 13:02:28 -0800392app_worker_accept_notify (app_worker_t * app_wrk, session_t * s)
393{
Florin Coras0242d302022-12-22 15:03:44 -0800394 app_worker_add_event (app_wrk, s, SESSION_CTRL_EVT_ACCEPTED);
395 return 0;
Florin Corasa27a46e2019-02-18 13:02:28 -0800396}
397
398int
399app_worker_init_connected (app_worker_t * app_wrk, session_t * s)
400{
401 application_t *app = application_get (app_wrk->app_index);
402 segment_manager_t *sm;
403
Ryujiro Shibuyad8f48e22020-01-22 12:11:42 +0000404 if (app->cb_fns.fifo_tuning_callback)
405 s->flags |= SESSION_F_CUSTOM_FIFO_TUNING;
406
Florin Corase6552402020-11-11 13:25:53 -0800407 /* Allocate fifos for session, unless the app is a builtin proxy */
408 if (application_is_builtin_proxy (app))
409 return 0;
410
411 sm = app_worker_get_connect_segment_manager (app_wrk);
412 return app_worker_alloc_session_fifos (sm, s);
Florin Corasa27a46e2019-02-18 13:02:28 -0800413}
414
415int
Florin Coras00e01d32019-10-21 16:07:46 -0700416app_worker_connect_notify (app_worker_t * app_wrk, session_t * s,
417 session_error_t err, u32 opaque)
Florin Corasa27a46e2019-02-18 13:02:28 -0800418{
Florin Coras09c0e8f2023-08-11 14:40:13 -0700419 session_event_t evt = { .event_type = SESSION_CTRL_EVT_CONNECTED,
420 .as_u64[0] = s ? s->session_index : ~0,
421 .as_u64[1] = (u64) opaque << 32 | (u32) err };
422 u32 thread_index = s ? s->thread_index : vlib_get_thread_index ();
Florin Coras0242d302022-12-22 15:03:44 -0800423
424 app_worker_add_event_custom (app_wrk, thread_index, &evt);
425 return 0;
Florin Corasa27a46e2019-02-18 13:02:28 -0800426}
427
428int
Florin Corasea727642021-05-07 19:39:43 -0700429app_worker_add_half_open (app_worker_t *app_wrk, session_handle_t sh)
Florin Corasd50ff7f2020-04-16 04:30:22 +0000430{
Florin Corasea727642021-05-07 19:39:43 -0700431 session_handle_t *shp;
432
Florin Coras309f7aa2022-03-18 08:33:08 -0700433 ASSERT (session_vlib_thread_is_cl_thread ());
Florin Corasea727642021-05-07 19:39:43 -0700434 pool_get (app_wrk->half_open_table, shp);
435 *shp = sh;
436
437 return (shp - app_wrk->half_open_table);
Florin Corasd50ff7f2020-04-16 04:30:22 +0000438}
439
440int
Florin Coras2c876f92021-05-10 21:12:27 -0700441app_worker_del_half_open (app_worker_t *app_wrk, session_t *s)
Florin Corasd50ff7f2020-04-16 04:30:22 +0000442{
Florin Coras0242d302022-12-22 15:03:44 -0800443 app_worker_add_event (app_wrk, s, SESSION_CTRL_EVT_HALF_CLEANUP);
Florin Corasd50ff7f2020-04-16 04:30:22 +0000444 return 0;
445}
446
Florin Corasd50ff7f2020-04-16 04:30:22 +0000447int
Florin Corasbf7ce2c2019-03-06 14:44:42 -0800448app_worker_close_notify (app_worker_t * app_wrk, session_t * s)
449{
Florin Coras0242d302022-12-22 15:03:44 -0800450 app_worker_add_event (app_wrk, s, SESSION_CTRL_EVT_DISCONNECTED);
Florin Corasbf7ce2c2019-03-06 14:44:42 -0800451 return 0;
452}
453
454int
Florin Coras692b9492019-07-12 15:01:53 -0700455app_worker_transport_closed_notify (app_worker_t * app_wrk, session_t * s)
456{
Florin Coras0242d302022-12-22 15:03:44 -0800457 app_worker_add_event (app_wrk, s, SESSION_CTRL_EVT_TRANSPORT_CLOSED);
Florin Coras692b9492019-07-12 15:01:53 -0700458 return 0;
459}
460
461int
Florin Coras69b68ef2019-04-02 11:38:51 -0700462app_worker_reset_notify (app_worker_t * app_wrk, session_t * s)
463{
Florin Coras0242d302022-12-22 15:03:44 -0800464 app_worker_add_event (app_wrk, s, SESSION_CTRL_EVT_RESET);
Florin Coras69b68ef2019-04-02 11:38:51 -0700465 return 0;
466}
467
468int
Florin Coras70f26d52019-07-08 11:47:18 -0700469app_worker_cleanup_notify (app_worker_t * app_wrk, session_t * s,
470 session_cleanup_ntf_t ntf)
471{
Florin Coras09c0e8f2023-08-11 14:40:13 -0700472 session_event_t evt = { .event_type = SESSION_CTRL_EVT_CLEANUP,
473 .as_u64[0] = (u64) ntf << 32 | s->session_index,
474 .as_u64[1] = pointer_to_uword (session_cleanup) };
Florin Coras0242d302022-12-22 15:03:44 -0800475
476 app_worker_add_event_custom (app_wrk, s->thread_index, &evt);
477
Florin Coras70f26d52019-07-08 11:47:18 -0700478 return 0;
479}
480
481int
Florin Coras0242d302022-12-22 15:03:44 -0800482app_worker_cleanup_notify_custom (app_worker_t *app_wrk, session_t *s,
483 session_cleanup_ntf_t ntf,
484 void (*cleanup_cb) (session_t *s))
Florin Corasbf7ce2c2019-03-06 14:44:42 -0800485{
Florin Coras09c0e8f2023-08-11 14:40:13 -0700486 session_event_t evt = { .event_type = SESSION_CTRL_EVT_CLEANUP,
487 .as_u64[0] = (u64) ntf << 32 | s->session_index,
488 .as_u64[1] = pointer_to_uword (cleanup_cb) };
Florin Coras0242d302022-12-22 15:03:44 -0800489
490 app_worker_add_event_custom (app_wrk, s->thread_index, &evt);
491
Florin Corasbf7ce2c2019-03-06 14:44:42 -0800492 return 0;
493}
494
495int
Florin Coras0242d302022-12-22 15:03:44 -0800496app_worker_rx_notify (app_worker_t *app_wrk, session_t *s)
Florin Coras0e573f52019-05-07 16:28:16 -0700497{
Florin Coras0242d302022-12-22 15:03:44 -0800498 app_worker_add_event (app_wrk, s, SESSION_IO_EVT_RX);
Florin Coras0e573f52019-05-07 16:28:16 -0700499 return 0;
500}
501
502int
Florin Coras49568af2019-07-31 16:46:24 -0700503app_worker_migrate_notify (app_worker_t * app_wrk, session_t * s,
504 session_handle_t new_sh)
505{
Florin Coras09c0e8f2023-08-11 14:40:13 -0700506 session_event_t evt = { .event_type = SESSION_CTRL_EVT_MIGRATED,
507 .as_u64[0] = s->session_index,
508 .as_u64[1] = new_sh };
Florin Coras0242d302022-12-22 15:03:44 -0800509
510 app_worker_add_event_custom (app_wrk, s->thread_index, &evt);
Florin Coras49568af2019-07-31 16:46:24 -0700511 return 0;
512}
513
514int
Florin Coras623eb562019-02-03 19:28:34 -0800515app_worker_own_session (app_worker_t * app_wrk, session_t * s)
516{
517 segment_manager_t *sm;
518 svm_fifo_t *rxf, *txf;
Filip Tehlarac3c8dc2023-03-14 08:50:28 +0100519 int rv;
Florin Coras623eb562019-02-03 19:28:34 -0800520
521 if (s->session_state == SESSION_STATE_LISTENING)
522 return application_change_listener_owner (s, app_wrk);
523
524 s->app_wrk_index = app_wrk->wrk_index;
525
526 rxf = s->rx_fifo;
527 txf = s->tx_fifo;
528
529 if (!rxf || !txf)
530 return 0;
531
532 s->rx_fifo = 0;
533 s->tx_fifo = 0;
534
Florin Coras94a6df02021-05-06 15:32:14 -0700535 sm = app_worker_get_connect_segment_manager (app_wrk);
Filip Tehlarac3c8dc2023-03-14 08:50:28 +0100536 if ((rv = app_worker_alloc_session_fifos (sm, s)))
537 return rv;
Florin Coras623eb562019-02-03 19:28:34 -0800538
Sirshak Das28aa5392019-02-05 01:33:33 -0600539 if (!svm_fifo_is_empty_cons (rxf))
540 svm_fifo_clone (s->rx_fifo, rxf);
Florin Coras623eb562019-02-03 19:28:34 -0800541
Sirshak Das28aa5392019-02-05 01:33:33 -0600542 if (!svm_fifo_is_empty_cons (txf))
543 svm_fifo_clone (s->tx_fifo, txf);
544
Florin Coras19223e02019-03-03 14:56:05 -0800545 segment_manager_dealloc_fifos (rxf, txf);
Florin Coras623eb562019-02-03 19:28:34 -0800546
547 return 0;
548}
549
550int
Florin Coras89a9f612021-05-11 11:55:07 -0700551app_worker_connect_session (app_worker_t *app_wrk, session_endpoint_cfg_t *sep,
552 session_handle_t *rsh)
Florin Coras623eb562019-02-03 19:28:34 -0800553{
Florin Coras0242d302022-12-22 15:03:44 -0800554 if (PREDICT_FALSE (app_worker_mq_is_congested (app_wrk)))
Florin Coras20c24232021-11-22 21:19:01 -0800555 return SESSION_E_REFUSED;
556
Florin Coras89a9f612021-05-11 11:55:07 -0700557 sep->app_wrk_index = app_wrk->wrk_index;
Florin Coras623eb562019-02-03 19:28:34 -0800558
Florin Coras89a9f612021-05-11 11:55:07 -0700559 return session_open (sep, rsh);
Florin Coras623eb562019-02-03 19:28:34 -0800560}
561
562int
Ryujiro Shibuyad8f48e22020-01-22 12:11:42 +0000563app_worker_session_fifo_tuning (app_worker_t * app_wrk, session_t * s,
564 svm_fifo_t * f,
565 session_ft_action_t act, u32 len)
566{
567 application_t *app = application_get (app_wrk->app_index);
568 return app->cb_fns.fifo_tuning_callback (s, f, act, len);
569}
570
Florin Coras623eb562019-02-03 19:28:34 -0800571segment_manager_t *
572app_worker_get_connect_segment_manager (app_worker_t * app)
573{
574 ASSERT (app->connects_seg_manager != (u32) ~ 0);
575 return segment_manager_get (app->connects_seg_manager);
576}
577
578segment_manager_t *
Florin Coras623eb562019-02-03 19:28:34 -0800579app_worker_get_listen_segment_manager (app_worker_t * app,
580 session_t * listener)
581{
582 uword *smp;
583 smp = hash_get (app->listeners_table, listen_session_get_handle (listener));
Dave Barach47d41ad2020-02-17 09:13:26 -0500584 ALWAYS_ASSERT (smp != 0);
Florin Coras623eb562019-02-03 19:28:34 -0800585 return segment_manager_get (*smp);
586}
587
588session_t *
Florin Corasc9940fc2019-02-05 20:55:11 -0800589app_worker_first_listener (app_worker_t * app_wrk, u8 fib_proto,
Florin Coras623eb562019-02-03 19:28:34 -0800590 u8 transport_proto)
591{
592 session_t *listener;
593 u64 handle;
594 u32 sm_index;
595 u8 sst;
596
597 sst = session_type_from_proto_and_ip (transport_proto,
598 fib_proto == FIB_PROTOCOL_IP4);
599
600 /* *INDENT-OFF* */
Florin Corasc9940fc2019-02-05 20:55:11 -0800601 hash_foreach (handle, sm_index, app_wrk->listeners_table, ({
Florin Coras623eb562019-02-03 19:28:34 -0800602 listener = listen_session_get_from_handle (handle);
603 if (listener->session_type == sst
Florin Corasd5c604d2019-03-18 09:06:35 -0700604 && !(listener->flags & SESSION_F_PROXY))
Florin Coras623eb562019-02-03 19:28:34 -0800605 return listener;
606 }));
607 /* *INDENT-ON* */
608
609 return 0;
610}
611
612session_t *
Florin Corasc9940fc2019-02-05 20:55:11 -0800613app_worker_proxy_listener (app_worker_t * app_wrk, u8 fib_proto,
Florin Coras623eb562019-02-03 19:28:34 -0800614 u8 transport_proto)
615{
616 session_t *listener;
617 u64 handle;
618 u32 sm_index;
619 u8 sst;
620
621 sst = session_type_from_proto_and_ip (transport_proto,
622 fib_proto == FIB_PROTOCOL_IP4);
623
624 /* *INDENT-OFF* */
Florin Corasc9940fc2019-02-05 20:55:11 -0800625 hash_foreach (handle, sm_index, app_wrk->listeners_table, ({
Florin Coras623eb562019-02-03 19:28:34 -0800626 listener = listen_session_get_from_handle (handle);
Florin Corasd5c604d2019-03-18 09:06:35 -0700627 if (listener->session_type == sst && (listener->flags & SESSION_F_PROXY))
Florin Coras623eb562019-02-03 19:28:34 -0800628 return listener;
629 }));
630 /* *INDENT-ON* */
631
632 return 0;
633}
634
635/**
636 * Send an API message to the external app, to map new segment
637 */
638int
Florin Coras2b81e3c2019-02-27 07:55:46 -0800639app_worker_add_segment_notify (app_worker_t * app_wrk, u64 segment_handle)
Florin Coras623eb562019-02-03 19:28:34 -0800640{
Florin Coras09c0e8f2023-08-11 14:40:13 -0700641 session_event_t evt = { .event_type = SESSION_CTRL_EVT_APP_ADD_SEGMENT,
642 .as_u64[1] = segment_handle };
Florin Coras0242d302022-12-22 15:03:44 -0800643
644 app_worker_add_event_custom (app_wrk, vlib_get_thread_index (), &evt);
645
646 return 0;
Florin Coras623eb562019-02-03 19:28:34 -0800647}
648
Florin Coras2b81e3c2019-02-27 07:55:46 -0800649int
650app_worker_del_segment_notify (app_worker_t * app_wrk, u64 segment_handle)
651{
Florin Coras09c0e8f2023-08-11 14:40:13 -0700652 session_event_t evt = { .event_type = SESSION_CTRL_EVT_APP_DEL_SEGMENT,
653 .as_u64[1] = segment_handle };
Florin Coras0242d302022-12-22 15:03:44 -0800654
655 app_worker_add_event_custom (app_wrk, vlib_get_thread_index (), &evt);
656
657 return 0;
Florin Coras623eb562019-02-03 19:28:34 -0800658}
659
Florin Coras20c24232021-11-22 21:19:01 -0800660static int
661app_wrk_send_fd (app_worker_t *app_wrk, int fd)
662{
663 if (!appns_sapi_enabled ())
664 {
665 vl_api_registration_t *reg;
666 clib_error_t *error;
667
668 reg =
669 vl_mem_api_client_index_to_registration (app_wrk->api_client_index);
670 if (!reg)
671 {
672 clib_warning ("no api registration for client: %u",
673 app_wrk->api_client_index);
674 return -1;
675 }
676
677 if (vl_api_registration_file_index (reg) == VL_API_INVALID_FI)
678 return -1;
679
680 error = vl_api_send_fd_msg (reg, &fd, 1);
681 if (error)
682 {
683 clib_error_report (error);
684 return -1;
685 }
686
687 return 0;
688 }
689
690 app_sapi_msg_t smsg = { 0 };
691 app_namespace_t *app_ns;
692 clib_error_t *error;
693 application_t *app;
694 clib_socket_t *cs;
695 u32 cs_index;
696
697 app = application_get (app_wrk->app_index);
698 app_ns = app_namespace_get (app->ns_index);
699 cs_index = appns_sapi_handle_sock_index (app_wrk->api_client_index);
700 cs = appns_sapi_get_socket (app_ns, cs_index);
701 if (PREDICT_FALSE (!cs))
702 return -1;
703
704 /* There's no payload for the message only the type */
705 smsg.type = APP_SAPI_MSG_TYPE_SEND_FDS;
706 error = clib_socket_sendmsg (cs, &smsg, sizeof (smsg), &fd, 1);
707 if (error)
708 {
709 clib_error_report (error);
710 return -1;
711 }
712
713 return 0;
714}
715
Florin Coras0242d302022-12-22 15:03:44 -0800716void
717app_worker_add_event (app_worker_t *app_wrk, session_t *s,
718 session_evt_type_t evt_type)
Florin Coras20c24232021-11-22 21:19:01 -0800719{
Florin Coras20c24232021-11-22 21:19:01 -0800720 session_event_t *evt;
Florin Coras20c24232021-11-22 21:19:01 -0800721
Florin Coras0242d302022-12-22 15:03:44 -0800722 ASSERT (s->thread_index == vlib_get_thread_index ());
723 clib_fifo_add2 (app_wrk->wrk_evts[s->thread_index], evt);
724 evt->session_index = s->session_index;
725 evt->event_type = evt_type;
726 evt->postponed = 0;
Florin Coras20c24232021-11-22 21:19:01 -0800727
Florin Coras0242d302022-12-22 15:03:44 -0800728 /* First event for this app_wrk. Schedule it for handling in session input */
729 if (clib_fifo_elts (app_wrk->wrk_evts[s->thread_index]) == 1)
Florin Coras20c24232021-11-22 21:19:01 -0800730 {
Florin Coras0242d302022-12-22 15:03:44 -0800731 session_worker_t *wrk = session_main_get_worker (s->thread_index);
732 session_wrk_program_app_wrk_evts (wrk, app_wrk->wrk_index);
Florin Coras20c24232021-11-22 21:19:01 -0800733 }
Florin Coras20c24232021-11-22 21:19:01 -0800734}
735
Florin Coras0242d302022-12-22 15:03:44 -0800736void
737app_worker_add_event_custom (app_worker_t *app_wrk, u32 thread_index,
738 session_event_t *evt)
Florin Coras20c24232021-11-22 21:19:01 -0800739{
Florin Coras0242d302022-12-22 15:03:44 -0800740 clib_fifo_add1 (app_wrk->wrk_evts[thread_index], *evt);
Florin Coras20c24232021-11-22 21:19:01 -0800741
Florin Coras0242d302022-12-22 15:03:44 -0800742 /* First event for this app_wrk. Schedule it for handling in session input */
743 if (clib_fifo_elts (app_wrk->wrk_evts[thread_index]) == 1)
Florin Coras20c24232021-11-22 21:19:01 -0800744 {
Florin Coras0242d302022-12-22 15:03:44 -0800745 session_worker_t *wrk = session_main_get_worker (thread_index);
746 session_wrk_program_app_wrk_evts (wrk, app_wrk->wrk_index);
Florin Coras20c24232021-11-22 21:19:01 -0800747 }
Florin Coras20c24232021-11-22 21:19:01 -0800748}
749
750always_inline void
751app_wrk_send_ctrl_evt_inline (app_worker_t *app_wrk, u8 evt_type, void *msg,
752 u32 msg_len, int fd)
753{
754 svm_msg_q_msg_t _mq_msg, *mq_msg = &_mq_msg;
755 svm_msg_q_t *mq = app_wrk->event_queue;
756 session_event_t *evt;
Florin Coras20c24232021-11-22 21:19:01 -0800757
Florin Coras0242d302022-12-22 15:03:44 -0800758 ASSERT (!svm_msg_q_or_ring_is_full (mq, SESSION_MQ_CTRL_EVT_RING));
759 *mq_msg = svm_msg_q_alloc_msg_w_ring (mq, SESSION_MQ_CTRL_EVT_RING);
Florin Coras20c24232021-11-22 21:19:01 -0800760
761 evt = svm_msg_q_msg_data (mq, mq_msg);
762 clib_memset (evt, 0, sizeof (*evt));
763 evt->event_type = evt_type;
764 clib_memcpy_fast (evt->data, msg, msg_len);
765
766 if (fd != -1)
767 app_wrk_send_fd (app_wrk, fd);
768
Florin Coras0242d302022-12-22 15:03:44 -0800769 svm_msg_q_add_raw (mq, mq_msg);
Florin Coras20c24232021-11-22 21:19:01 -0800770}
771
772void
773app_wrk_send_ctrl_evt_fd (app_worker_t *app_wrk, u8 evt_type, void *msg,
774 u32 msg_len, int fd)
775{
776 app_wrk_send_ctrl_evt_inline (app_wrk, evt_type, msg, msg_len, fd);
777}
778
779void
780app_wrk_send_ctrl_evt (app_worker_t *app_wrk, u8 evt_type, void *msg,
781 u32 msg_len)
782{
783 app_wrk_send_ctrl_evt_inline (app_wrk, evt_type, msg, msg_len, -1);
784}
785
Florin Coras0242d302022-12-22 15:03:44 -0800786u8
787app_worker_mq_wrk_is_congested (app_worker_t *app_wrk, u32 thread_index)
Florin Coras623eb562019-02-03 19:28:34 -0800788{
Florin Coras0242d302022-12-22 15:03:44 -0800789 return app_wrk->wrk_mq_congested[thread_index] > 0;
Florin Coras623eb562019-02-03 19:28:34 -0800790}
791
Florin Coras0242d302022-12-22 15:03:44 -0800792void
793app_worker_set_mq_wrk_congested (app_worker_t *app_wrk, u32 thread_index)
Florin Coras623eb562019-02-03 19:28:34 -0800794{
Florin Coras0242d302022-12-22 15:03:44 -0800795 clib_atomic_fetch_add_relax (&app_wrk->mq_congested, 1);
796 ASSERT (thread_index == vlib_get_thread_index ());
797 app_wrk->wrk_mq_congested[thread_index] = 1;
Florin Coras623eb562019-02-03 19:28:34 -0800798}
799
Florin Coras0242d302022-12-22 15:03:44 -0800800void
801app_worker_unset_wrk_mq_congested (app_worker_t *app_wrk, u32 thread_index)
Florin Coras623eb562019-02-03 19:28:34 -0800802{
Florin Coras0242d302022-12-22 15:03:44 -0800803 clib_atomic_fetch_sub_relax (&app_wrk->mq_congested, 1);
804 ASSERT (thread_index == vlib_get_thread_index ());
805 app_wrk->wrk_mq_congested[thread_index] = 0;
Florin Coras623eb562019-02-03 19:28:34 -0800806}
807
Florin Coras623eb562019-02-03 19:28:34 -0800808u8 *
809format_app_worker_listener (u8 * s, va_list * args)
810{
811 app_worker_t *app_wrk = va_arg (*args, app_worker_t *);
812 u64 handle = va_arg (*args, u64);
813 u32 sm_index = va_arg (*args, u32);
814 int verbose = va_arg (*args, int);
815 session_t *listener;
816 const u8 *app_name;
817 u8 *str;
818
819 if (!app_wrk)
820 {
821 if (verbose)
Xiaoming Jiang806709f2021-06-23 09:07:57 +0000822 s = format (s, "%-" SESSION_CLI_ID_LEN "s%-25s%-10s%-15s%-15s%-10s",
823 "Connection", "App", "Wrk", "API Client", "ListenerID",
824 "SegManager");
Florin Coras623eb562019-02-03 19:28:34 -0800825 else
Xiaoming Jiang806709f2021-06-23 09:07:57 +0000826 s = format (s, "%-" SESSION_CLI_ID_LEN "s%-25s%-10s", "Connection",
827 "App", "Wrk");
Florin Coras623eb562019-02-03 19:28:34 -0800828
829 return s;
830 }
831
832 app_name = application_name_from_index (app_wrk->app_index);
833 listener = listen_session_get_from_handle (handle);
Florin Coras31c99552019-03-01 13:00:58 -0800834 str = format (0, "%U", format_session, listener, verbose);
Florin Coras623eb562019-02-03 19:28:34 -0800835
836 if (verbose)
837 {
Dave Barach3e07a4a2020-04-04 10:05:48 -0400838 u8 *buf;
839 buf = format (0, "%u(%u)", app_wrk->wrk_map_index, app_wrk->wrk_index);
Xiaoming Jiang806709f2021-06-23 09:07:57 +0000840 s = format (s, "%-" SESSION_CLI_ID_LEN "v%-25v%-10v%-15u%-15u%-10u", str,
841 app_name, buf, app_wrk->api_client_index, handle, sm_index);
Dave Barach3e07a4a2020-04-04 10:05:48 -0400842 vec_free (buf);
Florin Coras623eb562019-02-03 19:28:34 -0800843 }
844 else
Xiaoming Jiang806709f2021-06-23 09:07:57 +0000845 s = format (s, "%-" SESSION_CLI_ID_LEN "v%-25v%=10u", str, app_name,
846 app_wrk->wrk_map_index);
jiangxiaoming6b410e62020-10-10 15:23:54 +0800847
848 vec_free (str);
Florin Coras623eb562019-02-03 19:28:34 -0800849
850 return s;
851}
852
853u8 *
854format_app_worker (u8 * s, va_list * args)
855{
856 app_worker_t *app_wrk = va_arg (*args, app_worker_t *);
857 u32 indent = 1;
858
Florin Coras20c24232021-11-22 21:19:01 -0800859 s = format (s,
860 "%U wrk-index %u app-index %u map-index %u "
861 "api-client-index %d mq-cong %u\n",
862 format_white_space, indent, app_wrk->wrk_index,
863 app_wrk->app_index, app_wrk->wrk_map_index,
864 app_wrk->api_client_index, app_wrk->mq_congested);
Florin Coras623eb562019-02-03 19:28:34 -0800865 return s;
866}
867
868void
869app_worker_format_connects (app_worker_t * app_wrk, int verbose)
870{
Florin Coras623eb562019-02-03 19:28:34 -0800871 segment_manager_t *sm;
Florin Coras623eb562019-02-03 19:28:34 -0800872
873 /* Header */
874 if (!app_wrk)
875 {
Florin Coras88001c62019-04-24 14:44:46 -0700876 segment_manager_format_sessions (0, verbose);
Florin Coras623eb562019-02-03 19:28:34 -0800877 return;
878 }
879
880 if (app_wrk->connects_seg_manager == (u32) ~ 0)
881 return;
882
Florin Coras623eb562019-02-03 19:28:34 -0800883 sm = segment_manager_get (app_wrk->connects_seg_manager);
Florin Coras88001c62019-04-24 14:44:46 -0700884 segment_manager_format_sessions (sm, verbose);
Florin Coras623eb562019-02-03 19:28:34 -0800885}
886
Florin Coras623eb562019-02-03 19:28:34 -0800887/*
888 * fd.io coding-style-patch-verification: ON
889 *
890 * Local Variables:
891 * eval: (c-set-style "gnu")
892 * End:
893 */