blob: 54377604803bc1372744ab2f3513267cb9ad30c1 [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 Coras1a4aaf12021-11-27 10:30:03 -0800187 /* Once the first segment is mapped, don't remove it until unlisten */
188 sm->first_is_protected = 1;
189
Florin Corasc9940fc2019-02-05 20:55:11 -0800190 /* Keep track of the segment manager for the listener or this worker */
Florin Coras623eb562019-02-03 19:28:34 -0800191 hash_set (app_wrk->listeners_table, listen_session_get_handle (ls),
192 segment_manager_index (sm));
193
Florin Coras87b7e3d2020-03-27 15:06:07 +0000194 if (transport_connection_is_cless (session_get_transport (ls)))
Florin Coras623eb562019-02-03 19:28:34 -0800195 {
Florin Coras87b7e3d2020-03-27 15:06:07 +0000196 if (ls->rx_fifo)
Florin Coras00e01d32019-10-21 16:07:46 -0700197 return SESSION_E_NOSUPPORT;
198 return app_worker_alloc_session_fifos (sm, ls);
Florin Coras623eb562019-02-03 19:28:34 -0800199 }
200 return 0;
201}
202
203int
Florin Corasd4295e62019-02-22 13:11:38 -0800204app_worker_start_listen (app_worker_t * app_wrk,
205 app_listener_t * app_listener)
206{
207 session_t *ls;
Florin Coras00e01d32019-10-21 16:07:46 -0700208 int rv;
Florin Corasd4295e62019-02-22 13:11:38 -0800209
210 if (clib_bitmap_get (app_listener->workers, app_wrk->wrk_map_index))
Florin Coras00e01d32019-10-21 16:07:46 -0700211 return SESSION_E_ALREADY_LISTENING;
Florin Corasd4295e62019-02-22 13:11:38 -0800212
213 app_listener->workers = clib_bitmap_set (app_listener->workers,
214 app_wrk->wrk_map_index, 1);
215
216 if (app_listener->session_index != SESSION_INVALID_INDEX)
217 {
218 ls = session_get (app_listener->session_index, 0);
Florin Coras00e01d32019-10-21 16:07:46 -0700219 if ((rv = app_worker_init_listener (app_wrk, ls)))
220 return rv;
Florin Corasd4295e62019-02-22 13:11:38 -0800221 }
222
223 if (app_listener->local_index != SESSION_INVALID_INDEX)
224 {
225 ls = session_get (app_listener->local_index, 0);
Florin Coras00e01d32019-10-21 16:07:46 -0700226 if ((rv = app_worker_init_listener (app_wrk, ls)))
227 return rv;
Florin Corasd4295e62019-02-22 13:11:38 -0800228 }
229
230 return 0;
231}
232
Florin Coras2b81e3c2019-02-27 07:55:46 -0800233static void
Florin Corasc8e812f2020-05-14 05:32:18 +0000234app_worker_add_detached_sm (app_worker_t * app_wrk, u32 sm_index)
235{
236 vec_add1 (app_wrk->detached_seg_managers, sm_index);
237}
238
239void
240app_worker_del_detached_sm (app_worker_t * app_wrk, u32 sm_index)
241{
242 u32 i;
243
244 clib_spinlock_lock (&app_wrk->detached_seg_managers_lock);
245 for (i = 0; i < vec_len (app_wrk->detached_seg_managers); i++)
246 {
247 if (app_wrk->detached_seg_managers[i] == sm_index)
248 {
249 vec_del1 (app_wrk->detached_seg_managers, i);
250 break;
251 }
252 }
253 clib_spinlock_unlock (&app_wrk->detached_seg_managers_lock);
254}
255
256static void
Florin Coras2b81e3c2019-02-27 07:55:46 -0800257app_worker_stop_listen_session (app_worker_t * app_wrk, session_t * ls)
Florin Coras623eb562019-02-03 19:28:34 -0800258{
Florin Corasc9940fc2019-02-05 20:55:11 -0800259 session_handle_t handle;
Florin Coras623eb562019-02-03 19:28:34 -0800260 segment_manager_t *sm;
261 uword *sm_indexp;
liuyacan87d48ad2021-04-28 11:34:03 +0000262 session_state_t *states = 0;
Florin Coras2b81e3c2019-02-27 07:55:46 -0800263
264 handle = listen_session_get_handle (ls);
265 sm_indexp = hash_get (app_wrk->listeners_table, handle);
266 if (PREDICT_FALSE (!sm_indexp))
267 return;
268
Florin Coras1bd46162020-04-13 23:35:55 +0000269 /* Dealloc fifos, if any (dgram listeners) */
Florin Coras87b7e3d2020-03-27 15:06:07 +0000270 if (ls->rx_fifo)
271 {
272 segment_manager_dealloc_fifos (ls->rx_fifo, ls->tx_fifo);
273 ls->tx_fifo = ls->rx_fifo = 0;
274 }
275
Florin Coras1bd46162020-04-13 23:35:55 +0000276 /* Try to cleanup segment manager */
Florin Coras2b81e3c2019-02-27 07:55:46 -0800277 sm = segment_manager_get (*sm_indexp);
Florin Coras94a6df02021-05-06 15:32:14 -0700278 if (sm)
Florin Coras2b81e3c2019-02-27 07:55:46 -0800279 {
Florin Coras1a4aaf12021-11-27 10:30:03 -0800280 sm->first_is_protected = 0;
Florin Coras1bd46162020-04-13 23:35:55 +0000281 segment_manager_app_detach (sm);
282 if (!segment_manager_has_fifos (sm))
Florin Coras94a6df02021-05-06 15:32:14 -0700283 {
284 /* Empty segment manager, cleanup it up */
285 segment_manager_free (sm);
286 }
Florin Corasc8e812f2020-05-14 05:32:18 +0000287 else
288 {
Florin Coras94a6df02021-05-06 15:32:14 -0700289 /* Delete sessions in CREATED state */
290 vec_add1 (states, SESSION_STATE_CREATED);
291 segment_manager_del_sessions_filter (sm, states);
292 vec_free (states);
293
Florin Corasc8e812f2020-05-14 05:32:18 +0000294 /* Track segment manager in case app detaches and all the
295 * outstanding sessions need to be closed */
296 app_worker_add_detached_sm (app_wrk, *sm_indexp);
297 sm->flags |= SEG_MANAGER_F_DETACHED_LISTENER;
298 }
Florin Coras2b81e3c2019-02-27 07:55:46 -0800299 }
Florin Coras1bd46162020-04-13 23:35:55 +0000300
Florin Coras2b81e3c2019-02-27 07:55:46 -0800301 hash_unset (app_wrk->listeners_table, handle);
302}
303
304int
305app_worker_stop_listen (app_worker_t * app_wrk, app_listener_t * al)
306{
Florin Corasd4295e62019-02-22 13:11:38 -0800307 session_t *ls;
Florin Coras623eb562019-02-03 19:28:34 -0800308
Florin Corasc9940fc2019-02-05 20:55:11 -0800309 if (!clib_bitmap_get (al->workers, app_wrk->wrk_map_index))
310 return 0;
311
312 if (al->session_index != SESSION_INVALID_INDEX)
Florin Coras623eb562019-02-03 19:28:34 -0800313 {
Florin Corasc9940fc2019-02-05 20:55:11 -0800314 ls = listen_session_get (al->session_index);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800315 app_worker_stop_listen_session (app_wrk, ls);
Florin Coras623eb562019-02-03 19:28:34 -0800316 }
317
Florin Corasc9940fc2019-02-05 20:55:11 -0800318 if (al->local_index != SESSION_INVALID_INDEX)
Florin Coras623eb562019-02-03 19:28:34 -0800319 {
Florin Corasd4295e62019-02-22 13:11:38 -0800320 ls = listen_session_get (al->local_index);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800321 app_worker_stop_listen_session (app_wrk, ls);
Florin Coras623eb562019-02-03 19:28:34 -0800322 }
Florin Corasc9940fc2019-02-05 20:55:11 -0800323
324 clib_bitmap_set_no_check (al->workers, app_wrk->wrk_map_index, 0);
325 if (clib_bitmap_is_zero (al->workers))
326 app_listener_cleanup (al);
Florin Coras623eb562019-02-03 19:28:34 -0800327
328 return 0;
329}
330
331int
Florin Corasa27a46e2019-02-18 13:02:28 -0800332app_worker_init_accepted (session_t * s)
333{
334 app_worker_t *app_wrk;
335 segment_manager_t *sm;
336 session_t *listener;
Ryujiro Shibuyad8f48e22020-01-22 12:11:42 +0000337 application_t *app;
Florin Corasa27a46e2019-02-18 13:02:28 -0800338
Nathan Skrzypczak2f0f96b2019-06-13 10:14:28 +0200339 listener = listen_session_get_from_handle (s->listener_handle);
Florin Corasa27a46e2019-02-18 13:02:28 -0800340 app_wrk = application_listener_select_worker (listener);
341 s->app_wrk_index = app_wrk->wrk_index;
Florin Coras2b81e3c2019-02-27 07:55:46 -0800342
Ryujiro Shibuyad8f48e22020-01-22 12:11:42 +0000343 app = application_get (app_wrk->app_index);
344 if (app->cb_fns.fifo_tuning_callback)
345 s->flags |= SESSION_F_CUSTOM_FIFO_TUNING;
346
Florin Corasa27a46e2019-02-18 13:02:28 -0800347 sm = app_worker_get_listen_segment_manager (app_wrk, listener);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800348 if (app_worker_alloc_session_fifos (sm, s))
349 return -1;
350
351 return 0;
Florin Corasa27a46e2019-02-18 13:02:28 -0800352}
353
354int
355app_worker_accept_notify (app_worker_t * app_wrk, session_t * s)
356{
357 application_t *app = application_get (app_wrk->app_index);
358 return app->cb_fns.session_accept_callback (s);
359}
360
361int
362app_worker_init_connected (app_worker_t * app_wrk, session_t * s)
363{
364 application_t *app = application_get (app_wrk->app_index);
365 segment_manager_t *sm;
366
Ryujiro Shibuyad8f48e22020-01-22 12:11:42 +0000367 if (app->cb_fns.fifo_tuning_callback)
368 s->flags |= SESSION_F_CUSTOM_FIFO_TUNING;
369
Florin Corase6552402020-11-11 13:25:53 -0800370 /* Allocate fifos for session, unless the app is a builtin proxy */
371 if (application_is_builtin_proxy (app))
372 return 0;
373
374 sm = app_worker_get_connect_segment_manager (app_wrk);
375 return app_worker_alloc_session_fifos (sm, s);
Florin Corasa27a46e2019-02-18 13:02:28 -0800376}
377
378int
Florin Coras00e01d32019-10-21 16:07:46 -0700379app_worker_connect_notify (app_worker_t * app_wrk, session_t * s,
380 session_error_t err, u32 opaque)
Florin Corasa27a46e2019-02-18 13:02:28 -0800381{
382 application_t *app = application_get (app_wrk->app_index);
383 return app->cb_fns.session_connected_callback (app_wrk->wrk_index, opaque,
Florin Coras00e01d32019-10-21 16:07:46 -0700384 s, err);
Florin Corasa27a46e2019-02-18 13:02:28 -0800385}
386
387int
Florin Corasea727642021-05-07 19:39:43 -0700388app_worker_add_half_open (app_worker_t *app_wrk, session_handle_t sh)
Florin Corasd50ff7f2020-04-16 04:30:22 +0000389{
Florin Corasea727642021-05-07 19:39:43 -0700390 session_handle_t *shp;
391
Florin Corasd50ff7f2020-04-16 04:30:22 +0000392 ASSERT (vlib_get_thread_index () == 0);
Florin Corasea727642021-05-07 19:39:43 -0700393 pool_get (app_wrk->half_open_table, shp);
394 *shp = sh;
395
396 return (shp - app_wrk->half_open_table);
Florin Corasd50ff7f2020-04-16 04:30:22 +0000397}
398
399int
Florin Coras2c876f92021-05-10 21:12:27 -0700400app_worker_del_half_open (app_worker_t *app_wrk, session_t *s)
Florin Corasd50ff7f2020-04-16 04:30:22 +0000401{
Florin Coras2c876f92021-05-10 21:12:27 -0700402 application_t *app = application_get (app_wrk->app_index);
Florin Corasbab6c522021-05-13 08:36:56 -0700403 ASSERT (vlib_get_thread_index () <= 1);
Florin Coras2c876f92021-05-10 21:12:27 -0700404 pool_put_index (app_wrk->half_open_table, s->ho_index);
405 if (app->cb_fns.half_open_cleanup_callback)
406 app->cb_fns.half_open_cleanup_callback (s);
Florin Corasd50ff7f2020-04-16 04:30:22 +0000407 return 0;
408}
409
Florin Corasd50ff7f2020-04-16 04:30:22 +0000410int
Florin Corasbf7ce2c2019-03-06 14:44:42 -0800411app_worker_close_notify (app_worker_t * app_wrk, session_t * s)
412{
413 application_t *app = application_get (app_wrk->app_index);
414 app->cb_fns.session_disconnect_callback (s);
415 return 0;
416}
417
418int
Florin Coras692b9492019-07-12 15:01:53 -0700419app_worker_transport_closed_notify (app_worker_t * app_wrk, session_t * s)
420{
421 application_t *app = application_get (app_wrk->app_index);
422 if (app->cb_fns.session_transport_closed_callback)
423 app->cb_fns.session_transport_closed_callback (s);
424 return 0;
425}
426
427int
Florin Coras69b68ef2019-04-02 11:38:51 -0700428app_worker_reset_notify (app_worker_t * app_wrk, session_t * s)
429{
430 application_t *app = application_get (app_wrk->app_index);
431 app->cb_fns.session_reset_callback (s);
432 return 0;
433}
434
435int
Florin Coras70f26d52019-07-08 11:47:18 -0700436app_worker_cleanup_notify (app_worker_t * app_wrk, session_t * s,
437 session_cleanup_ntf_t ntf)
438{
439 application_t *app = application_get (app_wrk->app_index);
440 if (app->cb_fns.session_cleanup_callback)
441 app->cb_fns.session_cleanup_callback (s, ntf);
442 return 0;
443}
444
445int
Florin Corasbf7ce2c2019-03-06 14:44:42 -0800446app_worker_builtin_rx (app_worker_t * app_wrk, session_t * s)
447{
448 application_t *app = application_get (app_wrk->app_index);
449 app->cb_fns.builtin_app_rx_callback (s);
450 return 0;
451}
452
453int
Florin Coras0e573f52019-05-07 16:28:16 -0700454app_worker_builtin_tx (app_worker_t * app_wrk, session_t * s)
455{
456 application_t *app = application_get (app_wrk->app_index);
457
458 if (!app->cb_fns.builtin_app_tx_callback)
459 return 0;
460
461 app->cb_fns.builtin_app_tx_callback (s);
462 return 0;
463}
464
465int
Florin Coras49568af2019-07-31 16:46:24 -0700466app_worker_migrate_notify (app_worker_t * app_wrk, session_t * s,
467 session_handle_t new_sh)
468{
469 application_t *app = application_get (app_wrk->app_index);
470 app->cb_fns.session_migrate_callback (s, new_sh);
471 return 0;
472}
473
474int
Florin Coras623eb562019-02-03 19:28:34 -0800475app_worker_own_session (app_worker_t * app_wrk, session_t * s)
476{
477 segment_manager_t *sm;
478 svm_fifo_t *rxf, *txf;
479
480 if (s->session_state == SESSION_STATE_LISTENING)
481 return application_change_listener_owner (s, app_wrk);
482
483 s->app_wrk_index = app_wrk->wrk_index;
484
485 rxf = s->rx_fifo;
486 txf = s->tx_fifo;
487
488 if (!rxf || !txf)
489 return 0;
490
491 s->rx_fifo = 0;
492 s->tx_fifo = 0;
493
Florin Coras94a6df02021-05-06 15:32:14 -0700494 sm = app_worker_get_connect_segment_manager (app_wrk);
Florin Corasa27a46e2019-02-18 13:02:28 -0800495 if (app_worker_alloc_session_fifos (sm, s))
Florin Coras623eb562019-02-03 19:28:34 -0800496 return -1;
497
Sirshak Das28aa5392019-02-05 01:33:33 -0600498 if (!svm_fifo_is_empty_cons (rxf))
499 svm_fifo_clone (s->rx_fifo, rxf);
Florin Coras623eb562019-02-03 19:28:34 -0800500
Sirshak Das28aa5392019-02-05 01:33:33 -0600501 if (!svm_fifo_is_empty_cons (txf))
502 svm_fifo_clone (s->tx_fifo, txf);
503
Florin Coras19223e02019-03-03 14:56:05 -0800504 segment_manager_dealloc_fifos (rxf, txf);
Florin Coras623eb562019-02-03 19:28:34 -0800505
506 return 0;
507}
508
509int
Florin Coras89a9f612021-05-11 11:55:07 -0700510app_worker_connect_session (app_worker_t *app_wrk, session_endpoint_cfg_t *sep,
511 session_handle_t *rsh)
Florin Coras623eb562019-02-03 19:28:34 -0800512{
Florin Coras89a9f612021-05-11 11:55:07 -0700513 sep->app_wrk_index = app_wrk->wrk_index;
Florin Coras623eb562019-02-03 19:28:34 -0800514
Florin Coras89a9f612021-05-11 11:55:07 -0700515 return session_open (sep, rsh);
Florin Coras623eb562019-02-03 19:28:34 -0800516}
517
518int
Ryujiro Shibuyad8f48e22020-01-22 12:11:42 +0000519app_worker_session_fifo_tuning (app_worker_t * app_wrk, session_t * s,
520 svm_fifo_t * f,
521 session_ft_action_t act, u32 len)
522{
523 application_t *app = application_get (app_wrk->app_index);
524 return app->cb_fns.fifo_tuning_callback (s, f, act, len);
525}
526
Florin Coras623eb562019-02-03 19:28:34 -0800527segment_manager_t *
528app_worker_get_connect_segment_manager (app_worker_t * app)
529{
530 ASSERT (app->connects_seg_manager != (u32) ~ 0);
531 return segment_manager_get (app->connects_seg_manager);
532}
533
534segment_manager_t *
Florin Coras623eb562019-02-03 19:28:34 -0800535app_worker_get_listen_segment_manager (app_worker_t * app,
536 session_t * listener)
537{
538 uword *smp;
539 smp = hash_get (app->listeners_table, listen_session_get_handle (listener));
Dave Barach47d41ad2020-02-17 09:13:26 -0500540 ALWAYS_ASSERT (smp != 0);
Florin Coras623eb562019-02-03 19:28:34 -0800541 return segment_manager_get (*smp);
542}
543
544session_t *
Florin Corasc9940fc2019-02-05 20:55:11 -0800545app_worker_first_listener (app_worker_t * app_wrk, u8 fib_proto,
Florin Coras623eb562019-02-03 19:28:34 -0800546 u8 transport_proto)
547{
548 session_t *listener;
549 u64 handle;
550 u32 sm_index;
551 u8 sst;
552
553 sst = session_type_from_proto_and_ip (transport_proto,
554 fib_proto == FIB_PROTOCOL_IP4);
555
556 /* *INDENT-OFF* */
Florin Corasc9940fc2019-02-05 20:55:11 -0800557 hash_foreach (handle, sm_index, app_wrk->listeners_table, ({
Florin Coras623eb562019-02-03 19:28:34 -0800558 listener = listen_session_get_from_handle (handle);
559 if (listener->session_type == sst
Florin Corasd5c604d2019-03-18 09:06:35 -0700560 && !(listener->flags & SESSION_F_PROXY))
Florin Coras623eb562019-02-03 19:28:34 -0800561 return listener;
562 }));
563 /* *INDENT-ON* */
564
565 return 0;
566}
567
568session_t *
Florin Corasc9940fc2019-02-05 20:55:11 -0800569app_worker_proxy_listener (app_worker_t * app_wrk, u8 fib_proto,
Florin Coras623eb562019-02-03 19:28:34 -0800570 u8 transport_proto)
571{
572 session_t *listener;
573 u64 handle;
574 u32 sm_index;
575 u8 sst;
576
577 sst = session_type_from_proto_and_ip (transport_proto,
578 fib_proto == FIB_PROTOCOL_IP4);
579
580 /* *INDENT-OFF* */
Florin Corasc9940fc2019-02-05 20:55:11 -0800581 hash_foreach (handle, sm_index, app_wrk->listeners_table, ({
Florin Coras623eb562019-02-03 19:28:34 -0800582 listener = listen_session_get_from_handle (handle);
Florin Corasd5c604d2019-03-18 09:06:35 -0700583 if (listener->session_type == sst && (listener->flags & SESSION_F_PROXY))
Florin Coras623eb562019-02-03 19:28:34 -0800584 return listener;
585 }));
586 /* *INDENT-ON* */
587
588 return 0;
589}
590
591/**
592 * Send an API message to the external app, to map new segment
593 */
594int
Florin Coras2b81e3c2019-02-27 07:55:46 -0800595app_worker_add_segment_notify (app_worker_t * app_wrk, u64 segment_handle)
Florin Coras623eb562019-02-03 19:28:34 -0800596{
Florin Coras623eb562019-02-03 19:28:34 -0800597 application_t *app = application_get (app_wrk->app_index);
Florin Corasc4c4cf52019-08-24 18:17:34 -0700598
599 return app->cb_fns.add_segment_callback (app_wrk->wrk_index,
Florin Coras623eb562019-02-03 19:28:34 -0800600 segment_handle);
601}
602
Florin Coras2b81e3c2019-02-27 07:55:46 -0800603int
604app_worker_del_segment_notify (app_worker_t * app_wrk, u64 segment_handle)
605{
606 application_t *app = application_get (app_wrk->app_index);
Florin Corasc4c4cf52019-08-24 18:17:34 -0700607 return app->cb_fns.del_segment_callback (app_wrk->wrk_index,
Florin Coras2b81e3c2019-02-27 07:55:46 -0800608 segment_handle);
609}
610
Florin Coras31c99552019-03-01 13:00:58 -0800611static inline u8
Florin Coras623eb562019-02-03 19:28:34 -0800612app_worker_application_is_builtin (app_worker_t * app_wrk)
613{
614 return app_wrk->app_is_builtin;
615}
616
617static inline int
Florin Coras2b5fed82019-07-25 14:51:09 -0700618app_send_io_evt_rx (app_worker_t * app_wrk, session_t * s)
Florin Coras623eb562019-02-03 19:28:34 -0800619{
620 session_event_t *evt;
621 svm_msg_q_msg_t msg;
622 svm_msg_q_t *mq;
623
Florin Coras5c290292019-09-19 08:19:44 -0700624 if (app_worker_application_is_builtin (app_wrk))
625 return app_worker_builtin_rx (app_wrk, s);
626
Florin Coras653e43f2019-03-04 10:56:23 -0800627 if (svm_fifo_has_event (s->rx_fifo))
Florin Coras623eb562019-02-03 19:28:34 -0800628 return 0;
629
630 mq = app_wrk->event_queue;
Florin Coras2b5fed82019-07-25 14:51:09 -0700631 svm_msg_q_lock (mq);
632
633 if (PREDICT_FALSE (svm_msg_q_is_full (mq)))
634 {
635 clib_warning ("evt q full");
636 svm_msg_q_unlock (mq);
637 return -1;
638 }
Florin Coras623eb562019-02-03 19:28:34 -0800639
640 if (PREDICT_FALSE (svm_msg_q_ring_is_full (mq, SESSION_MQ_IO_EVT_RING)))
641 {
642 clib_warning ("evt q rings full");
Florin Coras2b5fed82019-07-25 14:51:09 -0700643 svm_msg_q_unlock (mq);
Florin Coras623eb562019-02-03 19:28:34 -0800644 return -1;
645 }
646
647 msg = svm_msg_q_alloc_msg_w_ring (mq, SESSION_MQ_IO_EVT_RING);
Florin Coras623eb562019-02-03 19:28:34 -0800648 evt = (session_event_t *) svm_msg_q_msg_data (mq, &msg);
Florin Corasc547e912020-12-08 17:50:45 -0800649 evt->session_index = s->rx_fifo->shr->client_session_index;
Florin Corasf6c43132019-03-01 12:41:21 -0800650 evt->event_type = SESSION_IO_EVT_RX;
Florin Coras623eb562019-02-03 19:28:34 -0800651
652 (void) svm_fifo_set_event (s->rx_fifo);
Florin Coras2b5fed82019-07-25 14:51:09 -0700653 svm_msg_q_add_and_unlock (mq, &msg);
Florin Coras623eb562019-02-03 19:28:34 -0800654
Florin Coras623eb562019-02-03 19:28:34 -0800655 return 0;
656}
657
658static inline int
Florin Coras2b5fed82019-07-25 14:51:09 -0700659app_send_io_evt_tx (app_worker_t * app_wrk, session_t * s)
Florin Coras623eb562019-02-03 19:28:34 -0800660{
661 svm_msg_q_t *mq;
662 session_event_t *evt;
663 svm_msg_q_msg_t msg;
664
665 if (app_worker_application_is_builtin (app_wrk))
Florin Coras0e573f52019-05-07 16:28:16 -0700666 return app_worker_builtin_tx (app_wrk, s);
Florin Coras623eb562019-02-03 19:28:34 -0800667
668 mq = app_wrk->event_queue;
Florin Coras2b5fed82019-07-25 14:51:09 -0700669 svm_msg_q_lock (mq);
670
671 if (PREDICT_FALSE (svm_msg_q_is_full (mq)))
672 {
673 clib_warning ("evt q full");
674 svm_msg_q_unlock (mq);
675 return -1;
676 }
Florin Coras623eb562019-02-03 19:28:34 -0800677
678 if (PREDICT_FALSE (svm_msg_q_ring_is_full (mq, SESSION_MQ_IO_EVT_RING)))
679 {
680 clib_warning ("evt q rings full");
Florin Coras2b5fed82019-07-25 14:51:09 -0700681 svm_msg_q_unlock (mq);
Florin Coras623eb562019-02-03 19:28:34 -0800682 return -1;
683 }
684
685 msg = svm_msg_q_alloc_msg_w_ring (mq, SESSION_MQ_IO_EVT_RING);
Florin Coras623eb562019-02-03 19:28:34 -0800686 evt = (session_event_t *) svm_msg_q_msg_data (mq, &msg);
Florin Corasf6c43132019-03-01 12:41:21 -0800687 evt->event_type = SESSION_IO_EVT_TX;
Florin Corasc547e912020-12-08 17:50:45 -0800688 evt->session_index = s->tx_fifo->shr->client_session_index;
Florin Coras623eb562019-02-03 19:28:34 -0800689
Florin Coras2b5fed82019-07-25 14:51:09 -0700690 svm_msg_q_add_and_unlock (mq, &msg);
691 return 0;
Florin Coras623eb562019-02-03 19:28:34 -0800692}
693
694/* *INDENT-OFF* */
695typedef int (app_send_evt_handler_fn) (app_worker_t *app,
Florin Coras2b5fed82019-07-25 14:51:09 -0700696 session_t *s);
Florin Coras653e43f2019-03-04 10:56:23 -0800697static app_send_evt_handler_fn * const app_send_evt_handler_fns[2] = {
Florin Coras623eb562019-02-03 19:28:34 -0800698 app_send_io_evt_rx,
Florin Coras623eb562019-02-03 19:28:34 -0800699 app_send_io_evt_tx,
700};
701/* *INDENT-ON* */
702
703/**
704 * Send event to application
705 *
Florin Coras623eb562019-02-03 19:28:34 -0800706 * Logic from queue perspective is blocking. However, if queue is full,
707 * we return.
708 */
709int
710app_worker_lock_and_send_event (app_worker_t * app, session_t * s,
711 u8 evt_type)
712{
Florin Coras2b5fed82019-07-25 14:51:09 -0700713 return app_send_evt_handler_fns[evt_type] (app, s);
Florin Coras623eb562019-02-03 19:28:34 -0800714}
715
Florin Coras623eb562019-02-03 19:28:34 -0800716u8 *
717format_app_worker_listener (u8 * s, va_list * args)
718{
719 app_worker_t *app_wrk = va_arg (*args, app_worker_t *);
720 u64 handle = va_arg (*args, u64);
721 u32 sm_index = va_arg (*args, u32);
722 int verbose = va_arg (*args, int);
723 session_t *listener;
724 const u8 *app_name;
725 u8 *str;
726
727 if (!app_wrk)
728 {
729 if (verbose)
Xiaoming Jiang806709f2021-06-23 09:07:57 +0000730 s = format (s, "%-" SESSION_CLI_ID_LEN "s%-25s%-10s%-15s%-15s%-10s",
731 "Connection", "App", "Wrk", "API Client", "ListenerID",
732 "SegManager");
Florin Coras623eb562019-02-03 19:28:34 -0800733 else
Xiaoming Jiang806709f2021-06-23 09:07:57 +0000734 s = format (s, "%-" SESSION_CLI_ID_LEN "s%-25s%-10s", "Connection",
735 "App", "Wrk");
Florin Coras623eb562019-02-03 19:28:34 -0800736
737 return s;
738 }
739
740 app_name = application_name_from_index (app_wrk->app_index);
741 listener = listen_session_get_from_handle (handle);
Florin Coras31c99552019-03-01 13:00:58 -0800742 str = format (0, "%U", format_session, listener, verbose);
Florin Coras623eb562019-02-03 19:28:34 -0800743
744 if (verbose)
745 {
Dave Barach3e07a4a2020-04-04 10:05:48 -0400746 u8 *buf;
747 buf = format (0, "%u(%u)", app_wrk->wrk_map_index, app_wrk->wrk_index);
Xiaoming Jiang806709f2021-06-23 09:07:57 +0000748 s = format (s, "%-" SESSION_CLI_ID_LEN "v%-25v%-10v%-15u%-15u%-10u", str,
749 app_name, buf, app_wrk->api_client_index, handle, sm_index);
Dave Barach3e07a4a2020-04-04 10:05:48 -0400750 vec_free (buf);
Florin Coras623eb562019-02-03 19:28:34 -0800751 }
752 else
Xiaoming Jiang806709f2021-06-23 09:07:57 +0000753 s = format (s, "%-" SESSION_CLI_ID_LEN "v%-25v%=10u", str, app_name,
754 app_wrk->wrk_map_index);
jiangxiaoming6b410e62020-10-10 15:23:54 +0800755
756 vec_free (str);
Florin Coras623eb562019-02-03 19:28:34 -0800757
758 return s;
759}
760
761u8 *
762format_app_worker (u8 * s, va_list * args)
763{
764 app_worker_t *app_wrk = va_arg (*args, app_worker_t *);
765 u32 indent = 1;
766
767 s = format (s, "%U wrk-index %u app-index %u map-index %u "
768 "api-client-index %d\n", format_white_space, indent,
769 app_wrk->wrk_index, app_wrk->app_index, app_wrk->wrk_map_index,
770 app_wrk->api_client_index);
771 return s;
772}
773
774void
775app_worker_format_connects (app_worker_t * app_wrk, int verbose)
776{
Florin Coras623eb562019-02-03 19:28:34 -0800777 segment_manager_t *sm;
Florin Coras623eb562019-02-03 19:28:34 -0800778
779 /* Header */
780 if (!app_wrk)
781 {
Florin Coras88001c62019-04-24 14:44:46 -0700782 segment_manager_format_sessions (0, verbose);
Florin Coras623eb562019-02-03 19:28:34 -0800783 return;
784 }
785
786 if (app_wrk->connects_seg_manager == (u32) ~ 0)
787 return;
788
Florin Coras623eb562019-02-03 19:28:34 -0800789 sm = segment_manager_get (app_wrk->connects_seg_manager);
Florin Coras88001c62019-04-24 14:44:46 -0700790 segment_manager_format_sessions (sm, verbose);
Florin Coras623eb562019-02-03 19:28:34 -0800791}
792
Florin Coras623eb562019-02-03 19:28:34 -0800793/*
794 * fd.io coding-style-patch-verification: ON
795 *
796 * Local Variables:
797 * eval: (c-set-style "gnu")
798 * End:
799 */