blob: b55c55aea475fa51190da8e37ceb5012467dcbbd [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;
29 pool_get (app_workers, app_wrk);
30 clib_memset (app_wrk, 0, sizeof (*app_wrk));
31 app_wrk->wrk_index = app_wrk - app_workers;
32 app_wrk->app_index = app->app_index;
33 app_wrk->wrk_map_index = ~0;
34 app_wrk->connects_seg_manager = APP_INVALID_SEGMENT_MANAGER_INDEX;
Florin Corasc8e812f2020-05-14 05:32:18 +000035 clib_spinlock_init (&app_wrk->detached_seg_managers_lock);
Nathan Skrzypczakba65ca42019-05-16 16:35:40 +020036 APP_DBG ("New app %v worker %u", app->name, app_wrk->wrk_index);
Florin Coras623eb562019-02-03 19:28:34 -080037 return app_wrk;
38}
39
40app_worker_t *
41app_worker_get (u32 wrk_index)
42{
43 return pool_elt_at_index (app_workers, wrk_index);
44}
45
46app_worker_t *
47app_worker_get_if_valid (u32 wrk_index)
48{
49 if (pool_is_free_index (app_workers, wrk_index))
50 return 0;
51 return pool_elt_at_index (app_workers, wrk_index);
52}
53
54void
55app_worker_free (app_worker_t * app_wrk)
56{
57 application_t *app = application_get (app_wrk->app_index);
Florin Corasc1a42652019-02-08 18:27:29 -080058 vnet_unlisten_args_t _a, *a = &_a;
Florin Corasbf395972020-04-30 15:05:24 +000059 u64 handle, *handles = 0, *sm_indices = 0;
Florin Coras623eb562019-02-03 19:28:34 -080060 segment_manager_t *sm;
Florin Corasea727642021-05-07 19:39:43 -070061 session_handle_t *sh;
Florin Coras87d66332019-06-11 12:31:31 -070062 session_t *ls;
Florin Coras623eb562019-02-03 19:28:34 -080063 u32 sm_index;
Florin Corasea727642021-05-07 19:39:43 -070064 int i;
Florin Coras623eb562019-02-03 19:28:34 -080065
66 /*
67 * Listener cleanup
68 */
69
70 /* *INDENT-OFF* */
Florin Corasc9940fc2019-02-05 20:55:11 -080071 hash_foreach (handle, sm_index, app_wrk->listeners_table, ({
72 ls = listen_session_get_from_handle (handle);
Florin Coras87d66332019-06-11 12:31:31 -070073 vec_add1 (handles, app_listen_session_handle (ls));
Florin Corasbf395972020-04-30 15:05:24 +000074 vec_add1 (sm_indices, sm_index);
Florin Coras623eb562019-02-03 19:28:34 -080075 sm = segment_manager_get (sm_index);
Florin Coras623eb562019-02-03 19:28:34 -080076 }));
77 /* *INDENT-ON* */
78
79 for (i = 0; i < vec_len (handles); i++)
80 {
Florin Corasbf395972020-04-30 15:05:24 +000081 /* Cleanup listener */
Florin Coras623eb562019-02-03 19:28:34 -080082 a->app_index = app->app_index;
83 a->wrk_map_index = app_wrk->wrk_map_index;
84 a->handle = handles[i];
Florin Corasc1a42652019-02-08 18:27:29 -080085 (void) vnet_unlisten (a);
Florin Corasbf395972020-04-30 15:05:24 +000086
87 sm = segment_manager_get_if_valid (sm_indices[i]);
88 if (sm && !segment_manager_app_detached (sm))
89 {
90 sm->first_is_protected = 0;
91 segment_manager_init_free (sm);
92 }
Florin Coras623eb562019-02-03 19:28:34 -080093 }
Florin Corasd50ff7f2020-04-16 04:30:22 +000094 vec_reset_length (handles);
Florin Corasbf395972020-04-30 15:05:24 +000095 vec_free (sm_indices);
96 hash_free (app_wrk->listeners_table);
Florin Coras623eb562019-02-03 19:28:34 -080097
98 /*
99 * Connects segment manager cleanup
100 */
101
102 if (app_wrk->connects_seg_manager != APP_INVALID_SEGMENT_MANAGER_INDEX)
103 {
104 sm = segment_manager_get (app_wrk->connects_seg_manager);
105 sm->app_wrk_index = SEGMENT_MANAGER_INVALID_APP_INDEX;
Florin Coras565115e2019-02-20 19:48:31 -0800106 sm->first_is_protected = 0;
Florin Coras88001c62019-04-24 14:44:46 -0700107 segment_manager_init_free (sm);
Florin Coras623eb562019-02-03 19:28:34 -0800108 }
109
Florin Corasd50ff7f2020-04-16 04:30:22 +0000110 /*
111 * Half-open cleanup
112 */
113
Florin Corasea727642021-05-07 19:39:43 -0700114 pool_foreach (sh, app_wrk->half_open_table)
115 session_cleanup_half_open (*sh);
Florin Corasd50ff7f2020-04-16 04:30:22 +0000116
Florin Corasea727642021-05-07 19:39:43 -0700117 pool_free (app_wrk->half_open_table);
Florin Corasd50ff7f2020-04-16 04:30:22 +0000118
Florin Corasc8e812f2020-05-14 05:32:18 +0000119 /*
120 * Detached listener segment managers cleanup
121 */
122 for (i = 0; i < vec_len (app_wrk->detached_seg_managers); i++)
123 {
124 sm = segment_manager_get (app_wrk->detached_seg_managers[i]);
125 segment_manager_init_free (sm);
126 }
127 vec_free (app_wrk->detached_seg_managers);
128 clib_spinlock_free (&app_wrk->detached_seg_managers_lock);
129
Florin Coras623eb562019-02-03 19:28:34 -0800130 if (CLIB_DEBUG)
131 clib_memset (app_wrk, 0xfe, sizeof (*app_wrk));
Benoît Ganned4aeb842019-07-18 18:38:42 +0200132 pool_put (app_workers, app_wrk);
Florin Coras623eb562019-02-03 19:28:34 -0800133}
134
135application_t *
136app_worker_get_app (u32 wrk_index)
137{
138 app_worker_t *app_wrk;
139 app_wrk = app_worker_get_if_valid (wrk_index);
140 if (!app_wrk)
141 return 0;
142 return application_get_if_valid (app_wrk->app_index);
143}
144
145static segment_manager_t *
146app_worker_alloc_segment_manager (app_worker_t * app_wrk)
147{
Florin Coras94a6df02021-05-06 15:32:14 -0700148 segment_manager_t *sm;
Florin Coras623eb562019-02-03 19:28:34 -0800149
Florin Coras94a6df02021-05-06 15:32:14 -0700150 sm = segment_manager_alloc ();
Florin Coras623eb562019-02-03 19:28:34 -0800151 sm->app_wrk_index = app_wrk->wrk_index;
Florin Corasa107f402020-09-29 19:18:46 -0700152 segment_manager_init (sm);
Florin Coras623eb562019-02-03 19:28:34 -0800153 return sm;
154}
155
Florin Corasa27a46e2019-02-18 13:02:28 -0800156static int
157app_worker_alloc_session_fifos (segment_manager_t * sm, session_t * s)
158{
159 svm_fifo_t *rx_fifo = 0, *tx_fifo = 0;
Florin Corasa27a46e2019-02-18 13:02:28 -0800160 int rv;
161
Florin Coras62ddc032019-12-08 18:30:42 -0800162 if ((rv = segment_manager_alloc_session_fifos (sm, s->thread_index,
163 &rx_fifo, &tx_fifo)))
Florin Corasa27a46e2019-02-18 13:02:28 -0800164 return rv;
165
Florin Corasc547e912020-12-08 17:50:45 -0800166 rx_fifo->shr->master_session_index = s->session_index;
Florin Corasa27a46e2019-02-18 13:02:28 -0800167 rx_fifo->master_thread_index = s->thread_index;
168
Florin Corasc547e912020-12-08 17:50:45 -0800169 tx_fifo->shr->master_session_index = s->session_index;
Florin Corasa27a46e2019-02-18 13:02:28 -0800170 tx_fifo->master_thread_index = s->thread_index;
171
172 s->rx_fifo = rx_fifo;
173 s->tx_fifo = tx_fifo;
Florin Corasa27a46e2019-02-18 13:02:28 -0800174 return 0;
175}
176
Florin Coras623eb562019-02-03 19:28:34 -0800177int
Florin Corasd4295e62019-02-22 13:11:38 -0800178app_worker_init_listener (app_worker_t * app_wrk, session_t * ls)
Florin Coras623eb562019-02-03 19:28:34 -0800179{
180 segment_manager_t *sm;
181
182 /* Allocate segment manager. All sessions derived out of a listen session
183 * have fifos allocated by the same segment manager. */
184 if (!(sm = app_worker_alloc_segment_manager (app_wrk)))
Florin Coras00e01d32019-10-21 16:07:46 -0700185 return SESSION_E_ALLOC;
Florin Coras623eb562019-02-03 19:28:34 -0800186
Florin Corasc9940fc2019-02-05 20:55:11 -0800187 /* Keep track of the segment manager for the listener or this worker */
Florin Coras623eb562019-02-03 19:28:34 -0800188 hash_set (app_wrk->listeners_table, listen_session_get_handle (ls),
189 segment_manager_index (sm));
190
Florin Coras87b7e3d2020-03-27 15:06:07 +0000191 if (transport_connection_is_cless (session_get_transport (ls)))
Florin Coras623eb562019-02-03 19:28:34 -0800192 {
Florin Coras87b7e3d2020-03-27 15:06:07 +0000193 if (ls->rx_fifo)
Florin Coras00e01d32019-10-21 16:07:46 -0700194 return SESSION_E_NOSUPPORT;
195 return app_worker_alloc_session_fifos (sm, ls);
Florin Coras623eb562019-02-03 19:28:34 -0800196 }
197 return 0;
198}
199
200int
Florin Corasd4295e62019-02-22 13:11:38 -0800201app_worker_start_listen (app_worker_t * app_wrk,
202 app_listener_t * app_listener)
203{
204 session_t *ls;
Florin Coras00e01d32019-10-21 16:07:46 -0700205 int rv;
Florin Corasd4295e62019-02-22 13:11:38 -0800206
207 if (clib_bitmap_get (app_listener->workers, app_wrk->wrk_map_index))
Florin Coras00e01d32019-10-21 16:07:46 -0700208 return SESSION_E_ALREADY_LISTENING;
Florin Corasd4295e62019-02-22 13:11:38 -0800209
210 app_listener->workers = clib_bitmap_set (app_listener->workers,
211 app_wrk->wrk_map_index, 1);
212
213 if (app_listener->session_index != SESSION_INVALID_INDEX)
214 {
215 ls = session_get (app_listener->session_index, 0);
Florin Coras00e01d32019-10-21 16:07:46 -0700216 if ((rv = app_worker_init_listener (app_wrk, ls)))
217 return rv;
Florin Corasd4295e62019-02-22 13:11:38 -0800218 }
219
220 if (app_listener->local_index != SESSION_INVALID_INDEX)
221 {
222 ls = session_get (app_listener->local_index, 0);
Florin Coras00e01d32019-10-21 16:07:46 -0700223 if ((rv = app_worker_init_listener (app_wrk, ls)))
224 return rv;
Florin Corasd4295e62019-02-22 13:11:38 -0800225 }
226
227 return 0;
228}
229
Florin Coras2b81e3c2019-02-27 07:55:46 -0800230static void
Florin Corasc8e812f2020-05-14 05:32:18 +0000231app_worker_add_detached_sm (app_worker_t * app_wrk, u32 sm_index)
232{
233 vec_add1 (app_wrk->detached_seg_managers, sm_index);
234}
235
236void
237app_worker_del_detached_sm (app_worker_t * app_wrk, u32 sm_index)
238{
239 u32 i;
240
241 clib_spinlock_lock (&app_wrk->detached_seg_managers_lock);
242 for (i = 0; i < vec_len (app_wrk->detached_seg_managers); i++)
243 {
244 if (app_wrk->detached_seg_managers[i] == sm_index)
245 {
246 vec_del1 (app_wrk->detached_seg_managers, i);
247 break;
248 }
249 }
250 clib_spinlock_unlock (&app_wrk->detached_seg_managers_lock);
251}
252
253static void
Florin Coras2b81e3c2019-02-27 07:55:46 -0800254app_worker_stop_listen_session (app_worker_t * app_wrk, session_t * ls)
Florin Coras623eb562019-02-03 19:28:34 -0800255{
Florin Corasc9940fc2019-02-05 20:55:11 -0800256 session_handle_t handle;
Florin Coras623eb562019-02-03 19:28:34 -0800257 segment_manager_t *sm;
258 uword *sm_indexp;
liuyacan87d48ad2021-04-28 11:34:03 +0000259 session_state_t *states = 0;
Florin Coras2b81e3c2019-02-27 07:55:46 -0800260
261 handle = listen_session_get_handle (ls);
262 sm_indexp = hash_get (app_wrk->listeners_table, handle);
263 if (PREDICT_FALSE (!sm_indexp))
264 return;
265
Florin Coras1bd46162020-04-13 23:35:55 +0000266 /* Dealloc fifos, if any (dgram listeners) */
Florin Coras87b7e3d2020-03-27 15:06:07 +0000267 if (ls->rx_fifo)
268 {
269 segment_manager_dealloc_fifos (ls->rx_fifo, ls->tx_fifo);
270 ls->tx_fifo = ls->rx_fifo = 0;
271 }
272
Florin Coras1bd46162020-04-13 23:35:55 +0000273 /* Try to cleanup segment manager */
Florin Coras2b81e3c2019-02-27 07:55:46 -0800274 sm = segment_manager_get (*sm_indexp);
Florin Coras94a6df02021-05-06 15:32:14 -0700275 if (sm)
Florin Coras2b81e3c2019-02-27 07:55:46 -0800276 {
Florin Coras1bd46162020-04-13 23:35:55 +0000277 segment_manager_app_detach (sm);
278 if (!segment_manager_has_fifos (sm))
Florin Coras94a6df02021-05-06 15:32:14 -0700279 {
280 /* Empty segment manager, cleanup it up */
281 segment_manager_free (sm);
282 }
Florin Corasc8e812f2020-05-14 05:32:18 +0000283 else
284 {
Florin Coras94a6df02021-05-06 15:32:14 -0700285 /* Delete sessions in CREATED state */
286 vec_add1 (states, SESSION_STATE_CREATED);
287 segment_manager_del_sessions_filter (sm, states);
288 vec_free (states);
289
Florin Corasc8e812f2020-05-14 05:32:18 +0000290 /* Track segment manager in case app detaches and all the
291 * outstanding sessions need to be closed */
292 app_worker_add_detached_sm (app_wrk, *sm_indexp);
293 sm->flags |= SEG_MANAGER_F_DETACHED_LISTENER;
294 }
Florin Coras2b81e3c2019-02-27 07:55:46 -0800295 }
Florin Coras1bd46162020-04-13 23:35:55 +0000296
Florin Coras2b81e3c2019-02-27 07:55:46 -0800297 hash_unset (app_wrk->listeners_table, handle);
298}
299
300int
301app_worker_stop_listen (app_worker_t * app_wrk, app_listener_t * al)
302{
Florin Corasd4295e62019-02-22 13:11:38 -0800303 session_t *ls;
Florin Coras623eb562019-02-03 19:28:34 -0800304
Florin Corasc9940fc2019-02-05 20:55:11 -0800305 if (!clib_bitmap_get (al->workers, app_wrk->wrk_map_index))
306 return 0;
307
308 if (al->session_index != SESSION_INVALID_INDEX)
Florin Coras623eb562019-02-03 19:28:34 -0800309 {
Florin Corasc9940fc2019-02-05 20:55:11 -0800310 ls = listen_session_get (al->session_index);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800311 app_worker_stop_listen_session (app_wrk, ls);
Florin Coras623eb562019-02-03 19:28:34 -0800312 }
313
Florin Corasc9940fc2019-02-05 20:55:11 -0800314 if (al->local_index != SESSION_INVALID_INDEX)
Florin Coras623eb562019-02-03 19:28:34 -0800315 {
Florin Corasd4295e62019-02-22 13:11:38 -0800316 ls = listen_session_get (al->local_index);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800317 app_worker_stop_listen_session (app_wrk, ls);
Florin Coras623eb562019-02-03 19:28:34 -0800318 }
Florin Corasc9940fc2019-02-05 20:55:11 -0800319
320 clib_bitmap_set_no_check (al->workers, app_wrk->wrk_map_index, 0);
321 if (clib_bitmap_is_zero (al->workers))
322 app_listener_cleanup (al);
Florin Coras623eb562019-02-03 19:28:34 -0800323
324 return 0;
325}
326
327int
Florin Corasa27a46e2019-02-18 13:02:28 -0800328app_worker_init_accepted (session_t * s)
329{
330 app_worker_t *app_wrk;
331 segment_manager_t *sm;
332 session_t *listener;
Ryujiro Shibuyad8f48e22020-01-22 12:11:42 +0000333 application_t *app;
Florin Corasa27a46e2019-02-18 13:02:28 -0800334
Nathan Skrzypczak2f0f96b2019-06-13 10:14:28 +0200335 listener = listen_session_get_from_handle (s->listener_handle);
Florin Corasa27a46e2019-02-18 13:02:28 -0800336 app_wrk = application_listener_select_worker (listener);
337 s->app_wrk_index = app_wrk->wrk_index;
Florin Coras2b81e3c2019-02-27 07:55:46 -0800338
Ryujiro Shibuyad8f48e22020-01-22 12:11:42 +0000339 app = application_get (app_wrk->app_index);
340 if (app->cb_fns.fifo_tuning_callback)
341 s->flags |= SESSION_F_CUSTOM_FIFO_TUNING;
342
Florin Corasa27a46e2019-02-18 13:02:28 -0800343 sm = app_worker_get_listen_segment_manager (app_wrk, listener);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800344 if (app_worker_alloc_session_fifos (sm, s))
345 return -1;
346
347 return 0;
Florin Corasa27a46e2019-02-18 13:02:28 -0800348}
349
350int
351app_worker_accept_notify (app_worker_t * app_wrk, session_t * s)
352{
353 application_t *app = application_get (app_wrk->app_index);
354 return app->cb_fns.session_accept_callback (s);
355}
356
357int
358app_worker_init_connected (app_worker_t * app_wrk, session_t * s)
359{
360 application_t *app = application_get (app_wrk->app_index);
361 segment_manager_t *sm;
362
Ryujiro Shibuyad8f48e22020-01-22 12:11:42 +0000363 if (app->cb_fns.fifo_tuning_callback)
364 s->flags |= SESSION_F_CUSTOM_FIFO_TUNING;
365
Florin Corase6552402020-11-11 13:25:53 -0800366 /* Allocate fifos for session, unless the app is a builtin proxy */
367 if (application_is_builtin_proxy (app))
368 return 0;
369
370 sm = app_worker_get_connect_segment_manager (app_wrk);
371 return app_worker_alloc_session_fifos (sm, s);
Florin Corasa27a46e2019-02-18 13:02:28 -0800372}
373
374int
Florin Coras00e01d32019-10-21 16:07:46 -0700375app_worker_connect_notify (app_worker_t * app_wrk, session_t * s,
376 session_error_t err, u32 opaque)
Florin Corasa27a46e2019-02-18 13:02:28 -0800377{
378 application_t *app = application_get (app_wrk->app_index);
379 return app->cb_fns.session_connected_callback (app_wrk->wrk_index, opaque,
Florin Coras00e01d32019-10-21 16:07:46 -0700380 s, err);
Florin Corasa27a46e2019-02-18 13:02:28 -0800381}
382
383int
Florin Corasea727642021-05-07 19:39:43 -0700384app_worker_add_half_open (app_worker_t *app_wrk, session_handle_t sh)
Florin Corasd50ff7f2020-04-16 04:30:22 +0000385{
Florin Corasea727642021-05-07 19:39:43 -0700386 session_handle_t *shp;
387
Florin Corasd50ff7f2020-04-16 04:30:22 +0000388 ASSERT (vlib_get_thread_index () == 0);
Florin Corasea727642021-05-07 19:39:43 -0700389 pool_get (app_wrk->half_open_table, shp);
390 *shp = sh;
391
392 return (shp - app_wrk->half_open_table);
Florin Corasd50ff7f2020-04-16 04:30:22 +0000393}
394
395int
Florin Corasea727642021-05-07 19:39:43 -0700396app_worker_del_half_open (app_worker_t *app_wrk, u32 ho_index)
Florin Corasd50ff7f2020-04-16 04:30:22 +0000397{
398 ASSERT (vlib_get_thread_index () == 0);
Florin Corasea727642021-05-07 19:39:43 -0700399 pool_put_index (app_wrk->half_open_table, ho_index);
Florin Corasd50ff7f2020-04-16 04:30:22 +0000400 return 0;
401}
402
Florin Corasd50ff7f2020-04-16 04:30:22 +0000403int
Florin Corasbf7ce2c2019-03-06 14:44:42 -0800404app_worker_close_notify (app_worker_t * app_wrk, session_t * s)
405{
406 application_t *app = application_get (app_wrk->app_index);
407 app->cb_fns.session_disconnect_callback (s);
408 return 0;
409}
410
411int
Florin Coras692b9492019-07-12 15:01:53 -0700412app_worker_transport_closed_notify (app_worker_t * app_wrk, session_t * s)
413{
414 application_t *app = application_get (app_wrk->app_index);
415 if (app->cb_fns.session_transport_closed_callback)
416 app->cb_fns.session_transport_closed_callback (s);
417 return 0;
418}
419
420int
Florin Coras69b68ef2019-04-02 11:38:51 -0700421app_worker_reset_notify (app_worker_t * app_wrk, session_t * s)
422{
423 application_t *app = application_get (app_wrk->app_index);
424 app->cb_fns.session_reset_callback (s);
425 return 0;
426}
427
428int
Florin Coras70f26d52019-07-08 11:47:18 -0700429app_worker_cleanup_notify (app_worker_t * app_wrk, session_t * s,
430 session_cleanup_ntf_t ntf)
431{
432 application_t *app = application_get (app_wrk->app_index);
433 if (app->cb_fns.session_cleanup_callback)
434 app->cb_fns.session_cleanup_callback (s, ntf);
435 return 0;
436}
437
438int
Florin Corasbf7ce2c2019-03-06 14:44:42 -0800439app_worker_builtin_rx (app_worker_t * app_wrk, session_t * s)
440{
441 application_t *app = application_get (app_wrk->app_index);
442 app->cb_fns.builtin_app_rx_callback (s);
443 return 0;
444}
445
446int
Florin Coras0e573f52019-05-07 16:28:16 -0700447app_worker_builtin_tx (app_worker_t * app_wrk, session_t * s)
448{
449 application_t *app = application_get (app_wrk->app_index);
450
451 if (!app->cb_fns.builtin_app_tx_callback)
452 return 0;
453
454 app->cb_fns.builtin_app_tx_callback (s);
455 return 0;
456}
457
458int
Florin Coras49568af2019-07-31 16:46:24 -0700459app_worker_migrate_notify (app_worker_t * app_wrk, session_t * s,
460 session_handle_t new_sh)
461{
462 application_t *app = application_get (app_wrk->app_index);
463 app->cb_fns.session_migrate_callback (s, new_sh);
464 return 0;
465}
466
467int
Florin Coras623eb562019-02-03 19:28:34 -0800468app_worker_own_session (app_worker_t * app_wrk, session_t * s)
469{
470 segment_manager_t *sm;
471 svm_fifo_t *rxf, *txf;
472
473 if (s->session_state == SESSION_STATE_LISTENING)
474 return application_change_listener_owner (s, app_wrk);
475
476 s->app_wrk_index = app_wrk->wrk_index;
477
478 rxf = s->rx_fifo;
479 txf = s->tx_fifo;
480
481 if (!rxf || !txf)
482 return 0;
483
484 s->rx_fifo = 0;
485 s->tx_fifo = 0;
486
Florin Coras94a6df02021-05-06 15:32:14 -0700487 sm = app_worker_get_connect_segment_manager (app_wrk);
Florin Corasa27a46e2019-02-18 13:02:28 -0800488 if (app_worker_alloc_session_fifos (sm, s))
Florin Coras623eb562019-02-03 19:28:34 -0800489 return -1;
490
Sirshak Das28aa5392019-02-05 01:33:33 -0600491 if (!svm_fifo_is_empty_cons (rxf))
492 svm_fifo_clone (s->rx_fifo, rxf);
Florin Coras623eb562019-02-03 19:28:34 -0800493
Sirshak Das28aa5392019-02-05 01:33:33 -0600494 if (!svm_fifo_is_empty_cons (txf))
495 svm_fifo_clone (s->tx_fifo, txf);
496
Florin Coras19223e02019-03-03 14:56:05 -0800497 segment_manager_dealloc_fifos (rxf, txf);
Florin Coras623eb562019-02-03 19:28:34 -0800498
499 return 0;
500}
501
502int
Florin Coras89a9f612021-05-11 11:55:07 -0700503app_worker_connect_session (app_worker_t *app_wrk, session_endpoint_cfg_t *sep,
504 session_handle_t *rsh)
Florin Coras623eb562019-02-03 19:28:34 -0800505{
Florin Coras89a9f612021-05-11 11:55:07 -0700506 sep->app_wrk_index = app_wrk->wrk_index;
Florin Coras623eb562019-02-03 19:28:34 -0800507
Florin Coras89a9f612021-05-11 11:55:07 -0700508 return session_open (sep, rsh);
Florin Coras623eb562019-02-03 19:28:34 -0800509}
510
511int
Ryujiro Shibuyad8f48e22020-01-22 12:11:42 +0000512app_worker_session_fifo_tuning (app_worker_t * app_wrk, session_t * s,
513 svm_fifo_t * f,
514 session_ft_action_t act, u32 len)
515{
516 application_t *app = application_get (app_wrk->app_index);
517 return app->cb_fns.fifo_tuning_callback (s, f, act, len);
518}
519
Florin Coras623eb562019-02-03 19:28:34 -0800520segment_manager_t *
521app_worker_get_connect_segment_manager (app_worker_t * app)
522{
523 ASSERT (app->connects_seg_manager != (u32) ~ 0);
524 return segment_manager_get (app->connects_seg_manager);
525}
526
527segment_manager_t *
Florin Coras623eb562019-02-03 19:28:34 -0800528app_worker_get_listen_segment_manager (app_worker_t * app,
529 session_t * listener)
530{
531 uword *smp;
532 smp = hash_get (app->listeners_table, listen_session_get_handle (listener));
Dave Barach47d41ad2020-02-17 09:13:26 -0500533 ALWAYS_ASSERT (smp != 0);
Florin Coras623eb562019-02-03 19:28:34 -0800534 return segment_manager_get (*smp);
535}
536
537session_t *
Florin Corasc9940fc2019-02-05 20:55:11 -0800538app_worker_first_listener (app_worker_t * app_wrk, u8 fib_proto,
Florin Coras623eb562019-02-03 19:28:34 -0800539 u8 transport_proto)
540{
541 session_t *listener;
542 u64 handle;
543 u32 sm_index;
544 u8 sst;
545
546 sst = session_type_from_proto_and_ip (transport_proto,
547 fib_proto == FIB_PROTOCOL_IP4);
548
549 /* *INDENT-OFF* */
Florin Corasc9940fc2019-02-05 20:55:11 -0800550 hash_foreach (handle, sm_index, app_wrk->listeners_table, ({
Florin Coras623eb562019-02-03 19:28:34 -0800551 listener = listen_session_get_from_handle (handle);
552 if (listener->session_type == sst
Florin Corasd5c604d2019-03-18 09:06:35 -0700553 && !(listener->flags & SESSION_F_PROXY))
Florin Coras623eb562019-02-03 19:28:34 -0800554 return listener;
555 }));
556 /* *INDENT-ON* */
557
558 return 0;
559}
560
561session_t *
Florin Corasc9940fc2019-02-05 20:55:11 -0800562app_worker_proxy_listener (app_worker_t * app_wrk, u8 fib_proto,
Florin Coras623eb562019-02-03 19:28:34 -0800563 u8 transport_proto)
564{
565 session_t *listener;
566 u64 handle;
567 u32 sm_index;
568 u8 sst;
569
570 sst = session_type_from_proto_and_ip (transport_proto,
571 fib_proto == FIB_PROTOCOL_IP4);
572
573 /* *INDENT-OFF* */
Florin Corasc9940fc2019-02-05 20:55:11 -0800574 hash_foreach (handle, sm_index, app_wrk->listeners_table, ({
Florin Coras623eb562019-02-03 19:28:34 -0800575 listener = listen_session_get_from_handle (handle);
Florin Corasd5c604d2019-03-18 09:06:35 -0700576 if (listener->session_type == sst && (listener->flags & SESSION_F_PROXY))
Florin Coras623eb562019-02-03 19:28:34 -0800577 return listener;
578 }));
579 /* *INDENT-ON* */
580
581 return 0;
582}
583
584/**
585 * Send an API message to the external app, to map new segment
586 */
587int
Florin Coras2b81e3c2019-02-27 07:55:46 -0800588app_worker_add_segment_notify (app_worker_t * app_wrk, u64 segment_handle)
Florin Coras623eb562019-02-03 19:28:34 -0800589{
Florin Coras623eb562019-02-03 19:28:34 -0800590 application_t *app = application_get (app_wrk->app_index);
Florin Corasc4c4cf52019-08-24 18:17:34 -0700591
592 return app->cb_fns.add_segment_callback (app_wrk->wrk_index,
Florin Coras623eb562019-02-03 19:28:34 -0800593 segment_handle);
594}
595
Florin Coras2b81e3c2019-02-27 07:55:46 -0800596int
597app_worker_del_segment_notify (app_worker_t * app_wrk, u64 segment_handle)
598{
599 application_t *app = application_get (app_wrk->app_index);
Florin Corasc4c4cf52019-08-24 18:17:34 -0700600 return app->cb_fns.del_segment_callback (app_wrk->wrk_index,
Florin Coras2b81e3c2019-02-27 07:55:46 -0800601 segment_handle);
602}
603
Florin Coras31c99552019-03-01 13:00:58 -0800604static inline u8
Florin Coras623eb562019-02-03 19:28:34 -0800605app_worker_application_is_builtin (app_worker_t * app_wrk)
606{
607 return app_wrk->app_is_builtin;
608}
609
610static inline int
Florin Coras2b5fed82019-07-25 14:51:09 -0700611app_send_io_evt_rx (app_worker_t * app_wrk, session_t * s)
Florin Coras623eb562019-02-03 19:28:34 -0800612{
613 session_event_t *evt;
614 svm_msg_q_msg_t msg;
615 svm_msg_q_t *mq;
616
Florin Coras5c290292019-09-19 08:19:44 -0700617 if (app_worker_application_is_builtin (app_wrk))
618 return app_worker_builtin_rx (app_wrk, s);
619
Florin Coras653e43f2019-03-04 10:56:23 -0800620 if (svm_fifo_has_event (s->rx_fifo))
Florin Coras623eb562019-02-03 19:28:34 -0800621 return 0;
622
623 mq = app_wrk->event_queue;
Florin Coras2b5fed82019-07-25 14:51:09 -0700624 svm_msg_q_lock (mq);
625
626 if (PREDICT_FALSE (svm_msg_q_is_full (mq)))
627 {
628 clib_warning ("evt q full");
629 svm_msg_q_unlock (mq);
630 return -1;
631 }
Florin Coras623eb562019-02-03 19:28:34 -0800632
633 if (PREDICT_FALSE (svm_msg_q_ring_is_full (mq, SESSION_MQ_IO_EVT_RING)))
634 {
635 clib_warning ("evt q rings full");
Florin Coras2b5fed82019-07-25 14:51:09 -0700636 svm_msg_q_unlock (mq);
Florin Coras623eb562019-02-03 19:28:34 -0800637 return -1;
638 }
639
640 msg = svm_msg_q_alloc_msg_w_ring (mq, SESSION_MQ_IO_EVT_RING);
Florin Coras623eb562019-02-03 19:28:34 -0800641 evt = (session_event_t *) svm_msg_q_msg_data (mq, &msg);
Florin Corasc547e912020-12-08 17:50:45 -0800642 evt->session_index = s->rx_fifo->shr->client_session_index;
Florin Corasf6c43132019-03-01 12:41:21 -0800643 evt->event_type = SESSION_IO_EVT_RX;
Florin Coras623eb562019-02-03 19:28:34 -0800644
645 (void) svm_fifo_set_event (s->rx_fifo);
Florin Coras2b5fed82019-07-25 14:51:09 -0700646 svm_msg_q_add_and_unlock (mq, &msg);
Florin Coras623eb562019-02-03 19:28:34 -0800647
Florin Coras623eb562019-02-03 19:28:34 -0800648 return 0;
649}
650
651static inline int
Florin Coras2b5fed82019-07-25 14:51:09 -0700652app_send_io_evt_tx (app_worker_t * app_wrk, session_t * s)
Florin Coras623eb562019-02-03 19:28:34 -0800653{
654 svm_msg_q_t *mq;
655 session_event_t *evt;
656 svm_msg_q_msg_t msg;
657
658 if (app_worker_application_is_builtin (app_wrk))
Florin Coras0e573f52019-05-07 16:28:16 -0700659 return app_worker_builtin_tx (app_wrk, s);
Florin Coras623eb562019-02-03 19:28:34 -0800660
661 mq = app_wrk->event_queue;
Florin Coras2b5fed82019-07-25 14:51:09 -0700662 svm_msg_q_lock (mq);
663
664 if (PREDICT_FALSE (svm_msg_q_is_full (mq)))
665 {
666 clib_warning ("evt q full");
667 svm_msg_q_unlock (mq);
668 return -1;
669 }
Florin Coras623eb562019-02-03 19:28:34 -0800670
671 if (PREDICT_FALSE (svm_msg_q_ring_is_full (mq, SESSION_MQ_IO_EVT_RING)))
672 {
673 clib_warning ("evt q rings full");
Florin Coras2b5fed82019-07-25 14:51:09 -0700674 svm_msg_q_unlock (mq);
Florin Coras623eb562019-02-03 19:28:34 -0800675 return -1;
676 }
677
678 msg = svm_msg_q_alloc_msg_w_ring (mq, SESSION_MQ_IO_EVT_RING);
Florin Coras623eb562019-02-03 19:28:34 -0800679 evt = (session_event_t *) svm_msg_q_msg_data (mq, &msg);
Florin Corasf6c43132019-03-01 12:41:21 -0800680 evt->event_type = SESSION_IO_EVT_TX;
Florin Corasc547e912020-12-08 17:50:45 -0800681 evt->session_index = s->tx_fifo->shr->client_session_index;
Florin Coras623eb562019-02-03 19:28:34 -0800682
Florin Coras2b5fed82019-07-25 14:51:09 -0700683 svm_msg_q_add_and_unlock (mq, &msg);
684 return 0;
Florin Coras623eb562019-02-03 19:28:34 -0800685}
686
687/* *INDENT-OFF* */
688typedef int (app_send_evt_handler_fn) (app_worker_t *app,
Florin Coras2b5fed82019-07-25 14:51:09 -0700689 session_t *s);
Florin Coras653e43f2019-03-04 10:56:23 -0800690static app_send_evt_handler_fn * const app_send_evt_handler_fns[2] = {
Florin Coras623eb562019-02-03 19:28:34 -0800691 app_send_io_evt_rx,
Florin Coras623eb562019-02-03 19:28:34 -0800692 app_send_io_evt_tx,
693};
694/* *INDENT-ON* */
695
696/**
697 * Send event to application
698 *
Florin Coras623eb562019-02-03 19:28:34 -0800699 * Logic from queue perspective is blocking. However, if queue is full,
700 * we return.
701 */
702int
703app_worker_lock_and_send_event (app_worker_t * app, session_t * s,
704 u8 evt_type)
705{
Florin Coras2b5fed82019-07-25 14:51:09 -0700706 return app_send_evt_handler_fns[evt_type] (app, s);
Florin Coras623eb562019-02-03 19:28:34 -0800707}
708
Florin Coras623eb562019-02-03 19:28:34 -0800709u8 *
710format_app_worker_listener (u8 * s, va_list * args)
711{
712 app_worker_t *app_wrk = va_arg (*args, app_worker_t *);
713 u64 handle = va_arg (*args, u64);
714 u32 sm_index = va_arg (*args, u32);
715 int verbose = va_arg (*args, int);
716 session_t *listener;
717 const u8 *app_name;
718 u8 *str;
719
720 if (!app_wrk)
721 {
722 if (verbose)
Florin Coras91413ac2020-10-13 07:40:42 -0700723 s = format (s, "%-40s%-25s%-10s%-15s%-15s%-10s", "Connection", "App",
Florin Coras623eb562019-02-03 19:28:34 -0800724 "Wrk", "API Client", "ListenerID", "SegManager");
725 else
Florin Coras91413ac2020-10-13 07:40:42 -0700726 s = format (s, "%-40s%-25s%-10s", "Connection", "App", "Wrk");
Florin Coras623eb562019-02-03 19:28:34 -0800727
728 return s;
729 }
730
731 app_name = application_name_from_index (app_wrk->app_index);
732 listener = listen_session_get_from_handle (handle);
Florin Coras31c99552019-03-01 13:00:58 -0800733 str = format (0, "%U", format_session, listener, verbose);
Florin Coras623eb562019-02-03 19:28:34 -0800734
735 if (verbose)
736 {
Dave Barach3e07a4a2020-04-04 10:05:48 -0400737 u8 *buf;
738 buf = format (0, "%u(%u)", app_wrk->wrk_map_index, app_wrk->wrk_index);
Florin Coras91413ac2020-10-13 07:40:42 -0700739 s = format (s, "%-40v%-25v%-10v%-15u%-15u%-10u", str, app_name,
Florin Coras623eb562019-02-03 19:28:34 -0800740 buf, app_wrk->api_client_index, handle, sm_index);
Dave Barach3e07a4a2020-04-04 10:05:48 -0400741 vec_free (buf);
Florin Coras623eb562019-02-03 19:28:34 -0800742 }
743 else
jiangxiaoming6b410e62020-10-10 15:23:54 +0800744 s = format (s, "%-40v%-25v%=10u", str, app_name, app_wrk->wrk_map_index);
745
746 vec_free (str);
Florin Coras623eb562019-02-03 19:28:34 -0800747
748 return s;
749}
750
751u8 *
752format_app_worker (u8 * s, va_list * args)
753{
754 app_worker_t *app_wrk = va_arg (*args, app_worker_t *);
755 u32 indent = 1;
756
757 s = format (s, "%U wrk-index %u app-index %u map-index %u "
758 "api-client-index %d\n", format_white_space, indent,
759 app_wrk->wrk_index, app_wrk->app_index, app_wrk->wrk_map_index,
760 app_wrk->api_client_index);
761 return s;
762}
763
764void
765app_worker_format_connects (app_worker_t * app_wrk, int verbose)
766{
Florin Coras623eb562019-02-03 19:28:34 -0800767 segment_manager_t *sm;
Florin Coras623eb562019-02-03 19:28:34 -0800768
769 /* Header */
770 if (!app_wrk)
771 {
Florin Coras88001c62019-04-24 14:44:46 -0700772 segment_manager_format_sessions (0, verbose);
Florin Coras623eb562019-02-03 19:28:34 -0800773 return;
774 }
775
776 if (app_wrk->connects_seg_manager == (u32) ~ 0)
777 return;
778
Florin Coras623eb562019-02-03 19:28:34 -0800779 sm = segment_manager_get (app_wrk->connects_seg_manager);
Florin Coras88001c62019-04-24 14:44:46 -0700780 segment_manager_format_sessions (sm, verbose);
Florin Coras623eb562019-02-03 19:28:34 -0800781}
782
Florin Coras623eb562019-02-03 19:28:34 -0800783/*
784 * fd.io coding-style-patch-verification: ON
785 *
786 * Local Variables:
787 * eval: (c-set-style "gnu")
788 * End:
789 */