blob: 7e03171b1159937aa4253278f3c7abc456030ff6 [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 Coras87d66332019-06-11 12:31:31 -070061 session_t *ls;
Florin Coras623eb562019-02-03 19:28:34 -080062 u32 sm_index;
Florin Coras701c0992020-04-19 19:20:19 +000063 int i, j;
Florin Coras623eb562019-02-03 19:28:34 -080064
65 /*
66 * Listener cleanup
67 */
68
69 /* *INDENT-OFF* */
Florin Corasc9940fc2019-02-05 20:55:11 -080070 hash_foreach (handle, sm_index, app_wrk->listeners_table, ({
71 ls = listen_session_get_from_handle (handle);
Florin Coras87d66332019-06-11 12:31:31 -070072 vec_add1 (handles, app_listen_session_handle (ls));
Florin Corasbf395972020-04-30 15:05:24 +000073 vec_add1 (sm_indices, sm_index);
Florin Coras623eb562019-02-03 19:28:34 -080074 sm = segment_manager_get (sm_index);
Florin Coras623eb562019-02-03 19:28:34 -080075 }));
76 /* *INDENT-ON* */
77
78 for (i = 0; i < vec_len (handles); i++)
79 {
Florin Corasbf395972020-04-30 15:05:24 +000080 /* Cleanup listener */
Florin Coras623eb562019-02-03 19:28:34 -080081 a->app_index = app->app_index;
82 a->wrk_map_index = app_wrk->wrk_map_index;
83 a->handle = handles[i];
Florin Corasc1a42652019-02-08 18:27:29 -080084 (void) vnet_unlisten (a);
Florin Corasbf395972020-04-30 15:05:24 +000085
86 sm = segment_manager_get_if_valid (sm_indices[i]);
87 if (sm && !segment_manager_app_detached (sm))
88 {
89 sm->first_is_protected = 0;
90 segment_manager_init_free (sm);
91 }
Florin Coras623eb562019-02-03 19:28:34 -080092 }
Florin Corasd50ff7f2020-04-16 04:30:22 +000093 vec_reset_length (handles);
Florin Corasbf395972020-04-30 15:05:24 +000094 vec_free (sm_indices);
95 hash_free (app_wrk->listeners_table);
Florin Coras623eb562019-02-03 19:28:34 -080096
97 /*
98 * Connects segment manager cleanup
99 */
100
101 if (app_wrk->connects_seg_manager != APP_INVALID_SEGMENT_MANAGER_INDEX)
102 {
103 sm = segment_manager_get (app_wrk->connects_seg_manager);
104 sm->app_wrk_index = SEGMENT_MANAGER_INVALID_APP_INDEX;
Florin Coras565115e2019-02-20 19:48:31 -0800105 sm->first_is_protected = 0;
Florin Coras88001c62019-04-24 14:44:46 -0700106 segment_manager_init_free (sm);
Florin Coras623eb562019-02-03 19:28:34 -0800107 }
108
Florin Corasd50ff7f2020-04-16 04:30:22 +0000109 /*
110 * Half-open cleanup
111 */
112
113 for (i = 0; i < vec_len (app_wrk->half_open_table); i++)
114 {
115 if (!app_wrk->half_open_table[i])
116 continue;
117
118 /* *INDENT-OFF* */
119 hash_foreach (handle, sm_index, app_wrk->half_open_table[i], ({
120 vec_add1 (handles, handle);
121 }));
122 /* *INDENT-ON* */
123
Florin Coras701c0992020-04-19 19:20:19 +0000124 for (j = 0; j < vec_len (handles); j++)
125 session_cleanup_half_open (i, handles[j]);
Florin Corasd50ff7f2020-04-16 04:30:22 +0000126
127 hash_free (app_wrk->half_open_table[i]);
128 vec_reset_length (handles);
129 }
130
131 vec_free (app_wrk->half_open_table);
132 vec_free (handles);
133
Florin Corasc8e812f2020-05-14 05:32:18 +0000134 /*
135 * Detached listener segment managers cleanup
136 */
137 for (i = 0; i < vec_len (app_wrk->detached_seg_managers); i++)
138 {
139 sm = segment_manager_get (app_wrk->detached_seg_managers[i]);
140 segment_manager_init_free (sm);
141 }
142 vec_free (app_wrk->detached_seg_managers);
143 clib_spinlock_free (&app_wrk->detached_seg_managers_lock);
144
Florin Coras623eb562019-02-03 19:28:34 -0800145 if (CLIB_DEBUG)
146 clib_memset (app_wrk, 0xfe, sizeof (*app_wrk));
Benoît Ganned4aeb842019-07-18 18:38:42 +0200147 pool_put (app_workers, app_wrk);
Florin Coras623eb562019-02-03 19:28:34 -0800148}
149
150application_t *
151app_worker_get_app (u32 wrk_index)
152{
153 app_worker_t *app_wrk;
154 app_wrk = app_worker_get_if_valid (wrk_index);
155 if (!app_wrk)
156 return 0;
157 return application_get_if_valid (app_wrk->app_index);
158}
159
160static segment_manager_t *
161app_worker_alloc_segment_manager (app_worker_t * app_wrk)
162{
Florin Coras94a6df02021-05-06 15:32:14 -0700163 segment_manager_t *sm;
Florin Coras623eb562019-02-03 19:28:34 -0800164
Florin Coras94a6df02021-05-06 15:32:14 -0700165 sm = segment_manager_alloc ();
Florin Coras623eb562019-02-03 19:28:34 -0800166 sm->app_wrk_index = app_wrk->wrk_index;
Florin Corasa107f402020-09-29 19:18:46 -0700167 segment_manager_init (sm);
Florin Coras623eb562019-02-03 19:28:34 -0800168 return sm;
169}
170
Florin Corasa27a46e2019-02-18 13:02:28 -0800171static int
172app_worker_alloc_session_fifos (segment_manager_t * sm, session_t * s)
173{
174 svm_fifo_t *rx_fifo = 0, *tx_fifo = 0;
Florin Corasa27a46e2019-02-18 13:02:28 -0800175 int rv;
176
Florin Coras62ddc032019-12-08 18:30:42 -0800177 if ((rv = segment_manager_alloc_session_fifos (sm, s->thread_index,
178 &rx_fifo, &tx_fifo)))
Florin Corasa27a46e2019-02-18 13:02:28 -0800179 return rv;
180
Florin Corasc547e912020-12-08 17:50:45 -0800181 rx_fifo->shr->master_session_index = s->session_index;
Florin Corasa27a46e2019-02-18 13:02:28 -0800182 rx_fifo->master_thread_index = s->thread_index;
183
Florin Corasc547e912020-12-08 17:50:45 -0800184 tx_fifo->shr->master_session_index = s->session_index;
Florin Corasa27a46e2019-02-18 13:02:28 -0800185 tx_fifo->master_thread_index = s->thread_index;
186
187 s->rx_fifo = rx_fifo;
188 s->tx_fifo = tx_fifo;
Florin Corasa27a46e2019-02-18 13:02:28 -0800189 return 0;
190}
191
Florin Coras623eb562019-02-03 19:28:34 -0800192int
Florin Corasd4295e62019-02-22 13:11:38 -0800193app_worker_init_listener (app_worker_t * app_wrk, session_t * ls)
Florin Coras623eb562019-02-03 19:28:34 -0800194{
195 segment_manager_t *sm;
196
197 /* Allocate segment manager. All sessions derived out of a listen session
198 * have fifos allocated by the same segment manager. */
199 if (!(sm = app_worker_alloc_segment_manager (app_wrk)))
Florin Coras00e01d32019-10-21 16:07:46 -0700200 return SESSION_E_ALLOC;
Florin Coras623eb562019-02-03 19:28:34 -0800201
Florin Corasc9940fc2019-02-05 20:55:11 -0800202 /* Keep track of the segment manager for the listener or this worker */
Florin Coras623eb562019-02-03 19:28:34 -0800203 hash_set (app_wrk->listeners_table, listen_session_get_handle (ls),
204 segment_manager_index (sm));
205
Florin Coras87b7e3d2020-03-27 15:06:07 +0000206 if (transport_connection_is_cless (session_get_transport (ls)))
Florin Coras623eb562019-02-03 19:28:34 -0800207 {
Florin Coras87b7e3d2020-03-27 15:06:07 +0000208 if (ls->rx_fifo)
Florin Coras00e01d32019-10-21 16:07:46 -0700209 return SESSION_E_NOSUPPORT;
210 return app_worker_alloc_session_fifos (sm, ls);
Florin Coras623eb562019-02-03 19:28:34 -0800211 }
212 return 0;
213}
214
215int
Florin Corasd4295e62019-02-22 13:11:38 -0800216app_worker_start_listen (app_worker_t * app_wrk,
217 app_listener_t * app_listener)
218{
219 session_t *ls;
Florin Coras00e01d32019-10-21 16:07:46 -0700220 int rv;
Florin Corasd4295e62019-02-22 13:11:38 -0800221
222 if (clib_bitmap_get (app_listener->workers, app_wrk->wrk_map_index))
Florin Coras00e01d32019-10-21 16:07:46 -0700223 return SESSION_E_ALREADY_LISTENING;
Florin Corasd4295e62019-02-22 13:11:38 -0800224
225 app_listener->workers = clib_bitmap_set (app_listener->workers,
226 app_wrk->wrk_map_index, 1);
227
228 if (app_listener->session_index != SESSION_INVALID_INDEX)
229 {
230 ls = session_get (app_listener->session_index, 0);
Florin Coras00e01d32019-10-21 16:07:46 -0700231 if ((rv = app_worker_init_listener (app_wrk, ls)))
232 return rv;
Florin Corasd4295e62019-02-22 13:11:38 -0800233 }
234
235 if (app_listener->local_index != SESSION_INVALID_INDEX)
236 {
237 ls = session_get (app_listener->local_index, 0);
Florin Coras00e01d32019-10-21 16:07:46 -0700238 if ((rv = app_worker_init_listener (app_wrk, ls)))
239 return rv;
Florin Corasd4295e62019-02-22 13:11:38 -0800240 }
241
242 return 0;
243}
244
Florin Coras2b81e3c2019-02-27 07:55:46 -0800245static void
Florin Corasc8e812f2020-05-14 05:32:18 +0000246app_worker_add_detached_sm (app_worker_t * app_wrk, u32 sm_index)
247{
248 vec_add1 (app_wrk->detached_seg_managers, sm_index);
249}
250
251void
252app_worker_del_detached_sm (app_worker_t * app_wrk, u32 sm_index)
253{
254 u32 i;
255
256 clib_spinlock_lock (&app_wrk->detached_seg_managers_lock);
257 for (i = 0; i < vec_len (app_wrk->detached_seg_managers); i++)
258 {
259 if (app_wrk->detached_seg_managers[i] == sm_index)
260 {
261 vec_del1 (app_wrk->detached_seg_managers, i);
262 break;
263 }
264 }
265 clib_spinlock_unlock (&app_wrk->detached_seg_managers_lock);
266}
267
268static void
Florin Coras2b81e3c2019-02-27 07:55:46 -0800269app_worker_stop_listen_session (app_worker_t * app_wrk, session_t * ls)
Florin Coras623eb562019-02-03 19:28:34 -0800270{
Florin Corasc9940fc2019-02-05 20:55:11 -0800271 session_handle_t handle;
Florin Coras623eb562019-02-03 19:28:34 -0800272 segment_manager_t *sm;
273 uword *sm_indexp;
liuyacan87d48ad2021-04-28 11:34:03 +0000274 session_state_t *states = 0;
Florin Coras2b81e3c2019-02-27 07:55:46 -0800275
276 handle = listen_session_get_handle (ls);
277 sm_indexp = hash_get (app_wrk->listeners_table, handle);
278 if (PREDICT_FALSE (!sm_indexp))
279 return;
280
Florin Coras1bd46162020-04-13 23:35:55 +0000281 /* Dealloc fifos, if any (dgram listeners) */
Florin Coras87b7e3d2020-03-27 15:06:07 +0000282 if (ls->rx_fifo)
283 {
284 segment_manager_dealloc_fifos (ls->rx_fifo, ls->tx_fifo);
285 ls->tx_fifo = ls->rx_fifo = 0;
286 }
287
Florin Coras1bd46162020-04-13 23:35:55 +0000288 /* Try to cleanup segment manager */
Florin Coras2b81e3c2019-02-27 07:55:46 -0800289 sm = segment_manager_get (*sm_indexp);
Florin Coras94a6df02021-05-06 15:32:14 -0700290 if (sm)
Florin Coras2b81e3c2019-02-27 07:55:46 -0800291 {
Florin Coras1bd46162020-04-13 23:35:55 +0000292 segment_manager_app_detach (sm);
293 if (!segment_manager_has_fifos (sm))
Florin Coras94a6df02021-05-06 15:32:14 -0700294 {
295 /* Empty segment manager, cleanup it up */
296 segment_manager_free (sm);
297 }
Florin Corasc8e812f2020-05-14 05:32:18 +0000298 else
299 {
Florin Coras94a6df02021-05-06 15:32:14 -0700300 /* Delete sessions in CREATED state */
301 vec_add1 (states, SESSION_STATE_CREATED);
302 segment_manager_del_sessions_filter (sm, states);
303 vec_free (states);
304
Florin Corasc8e812f2020-05-14 05:32:18 +0000305 /* Track segment manager in case app detaches and all the
306 * outstanding sessions need to be closed */
307 app_worker_add_detached_sm (app_wrk, *sm_indexp);
308 sm->flags |= SEG_MANAGER_F_DETACHED_LISTENER;
309 }
Florin Coras2b81e3c2019-02-27 07:55:46 -0800310 }
Florin Coras1bd46162020-04-13 23:35:55 +0000311
Florin Coras2b81e3c2019-02-27 07:55:46 -0800312 hash_unset (app_wrk->listeners_table, handle);
313}
314
315int
316app_worker_stop_listen (app_worker_t * app_wrk, app_listener_t * al)
317{
Florin Corasd4295e62019-02-22 13:11:38 -0800318 session_t *ls;
Florin Coras623eb562019-02-03 19:28:34 -0800319
Florin Corasc9940fc2019-02-05 20:55:11 -0800320 if (!clib_bitmap_get (al->workers, app_wrk->wrk_map_index))
321 return 0;
322
323 if (al->session_index != SESSION_INVALID_INDEX)
Florin Coras623eb562019-02-03 19:28:34 -0800324 {
Florin Corasc9940fc2019-02-05 20:55:11 -0800325 ls = listen_session_get (al->session_index);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800326 app_worker_stop_listen_session (app_wrk, ls);
Florin Coras623eb562019-02-03 19:28:34 -0800327 }
328
Florin Corasc9940fc2019-02-05 20:55:11 -0800329 if (al->local_index != SESSION_INVALID_INDEX)
Florin Coras623eb562019-02-03 19:28:34 -0800330 {
Florin Corasd4295e62019-02-22 13:11:38 -0800331 ls = listen_session_get (al->local_index);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800332 app_worker_stop_listen_session (app_wrk, ls);
Florin Coras623eb562019-02-03 19:28:34 -0800333 }
Florin Corasc9940fc2019-02-05 20:55:11 -0800334
335 clib_bitmap_set_no_check (al->workers, app_wrk->wrk_map_index, 0);
336 if (clib_bitmap_is_zero (al->workers))
337 app_listener_cleanup (al);
Florin Coras623eb562019-02-03 19:28:34 -0800338
339 return 0;
340}
341
342int
Florin Corasa27a46e2019-02-18 13:02:28 -0800343app_worker_init_accepted (session_t * s)
344{
345 app_worker_t *app_wrk;
346 segment_manager_t *sm;
347 session_t *listener;
Ryujiro Shibuyad8f48e22020-01-22 12:11:42 +0000348 application_t *app;
Florin Corasa27a46e2019-02-18 13:02:28 -0800349
Nathan Skrzypczak2f0f96b2019-06-13 10:14:28 +0200350 listener = listen_session_get_from_handle (s->listener_handle);
Florin Corasa27a46e2019-02-18 13:02:28 -0800351 app_wrk = application_listener_select_worker (listener);
352 s->app_wrk_index = app_wrk->wrk_index;
Florin Coras2b81e3c2019-02-27 07:55:46 -0800353
Ryujiro Shibuyad8f48e22020-01-22 12:11:42 +0000354 app = application_get (app_wrk->app_index);
355 if (app->cb_fns.fifo_tuning_callback)
356 s->flags |= SESSION_F_CUSTOM_FIFO_TUNING;
357
Florin Corasa27a46e2019-02-18 13:02:28 -0800358 sm = app_worker_get_listen_segment_manager (app_wrk, listener);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800359 if (app_worker_alloc_session_fifos (sm, s))
360 return -1;
361
362 return 0;
Florin Corasa27a46e2019-02-18 13:02:28 -0800363}
364
365int
366app_worker_accept_notify (app_worker_t * app_wrk, session_t * s)
367{
368 application_t *app = application_get (app_wrk->app_index);
369 return app->cb_fns.session_accept_callback (s);
370}
371
372int
373app_worker_init_connected (app_worker_t * app_wrk, session_t * s)
374{
375 application_t *app = application_get (app_wrk->app_index);
376 segment_manager_t *sm;
377
Ryujiro Shibuyad8f48e22020-01-22 12:11:42 +0000378 if (app->cb_fns.fifo_tuning_callback)
379 s->flags |= SESSION_F_CUSTOM_FIFO_TUNING;
380
Florin Corase6552402020-11-11 13:25:53 -0800381 /* Allocate fifos for session, unless the app is a builtin proxy */
382 if (application_is_builtin_proxy (app))
383 return 0;
384
385 sm = app_worker_get_connect_segment_manager (app_wrk);
386 return app_worker_alloc_session_fifos (sm, s);
Florin Corasa27a46e2019-02-18 13:02:28 -0800387}
388
389int
Florin Coras00e01d32019-10-21 16:07:46 -0700390app_worker_connect_notify (app_worker_t * app_wrk, session_t * s,
391 session_error_t err, u32 opaque)
Florin Corasa27a46e2019-02-18 13:02:28 -0800392{
393 application_t *app = application_get (app_wrk->app_index);
394 return app->cb_fns.session_connected_callback (app_wrk->wrk_index, opaque,
Florin Coras00e01d32019-10-21 16:07:46 -0700395 s, err);
Florin Corasa27a46e2019-02-18 13:02:28 -0800396}
397
398int
Florin Corasd50ff7f2020-04-16 04:30:22 +0000399app_worker_add_half_open (app_worker_t * app_wrk, transport_proto_t tp,
400 session_handle_t ho_handle,
401 session_handle_t wrk_handle)
402{
403 ASSERT (vlib_get_thread_index () == 0);
404 vec_validate (app_wrk->half_open_table, tp);
405 hash_set (app_wrk->half_open_table[tp], ho_handle, wrk_handle);
406 return 0;
407}
408
409int
410app_worker_del_half_open (app_worker_t * app_wrk, transport_proto_t tp,
411 session_handle_t ho_handle)
412{
413 ASSERT (vlib_get_thread_index () == 0);
414 hash_unset (app_wrk->half_open_table[tp], ho_handle);
415 return 0;
416}
417
418u64
419app_worker_lookup_half_open (app_worker_t * app_wrk, transport_proto_t tp,
420 session_handle_t ho_handle)
421{
422 u64 *ho_wrk_handlep;
423
424 /* No locking because all updates are done from main thread */
425 ho_wrk_handlep = hash_get (app_wrk->half_open_table[tp], ho_handle);
426 if (!ho_wrk_handlep)
427 return SESSION_INVALID_HANDLE;
428
429 return *ho_wrk_handlep;
430}
431
432int
Florin Corasbf7ce2c2019-03-06 14:44:42 -0800433app_worker_close_notify (app_worker_t * app_wrk, session_t * s)
434{
435 application_t *app = application_get (app_wrk->app_index);
436 app->cb_fns.session_disconnect_callback (s);
437 return 0;
438}
439
440int
Florin Coras692b9492019-07-12 15:01:53 -0700441app_worker_transport_closed_notify (app_worker_t * app_wrk, session_t * s)
442{
443 application_t *app = application_get (app_wrk->app_index);
444 if (app->cb_fns.session_transport_closed_callback)
445 app->cb_fns.session_transport_closed_callback (s);
446 return 0;
447}
448
449int
Florin Coras69b68ef2019-04-02 11:38:51 -0700450app_worker_reset_notify (app_worker_t * app_wrk, session_t * s)
451{
452 application_t *app = application_get (app_wrk->app_index);
453 app->cb_fns.session_reset_callback (s);
454 return 0;
455}
456
457int
Florin Coras70f26d52019-07-08 11:47:18 -0700458app_worker_cleanup_notify (app_worker_t * app_wrk, session_t * s,
459 session_cleanup_ntf_t ntf)
460{
461 application_t *app = application_get (app_wrk->app_index);
462 if (app->cb_fns.session_cleanup_callback)
463 app->cb_fns.session_cleanup_callback (s, ntf);
464 return 0;
465}
466
467int
Florin Corasbf7ce2c2019-03-06 14:44:42 -0800468app_worker_builtin_rx (app_worker_t * app_wrk, session_t * s)
469{
470 application_t *app = application_get (app_wrk->app_index);
471 app->cb_fns.builtin_app_rx_callback (s);
472 return 0;
473}
474
475int
Florin Coras0e573f52019-05-07 16:28:16 -0700476app_worker_builtin_tx (app_worker_t * app_wrk, session_t * s)
477{
478 application_t *app = application_get (app_wrk->app_index);
479
480 if (!app->cb_fns.builtin_app_tx_callback)
481 return 0;
482
483 app->cb_fns.builtin_app_tx_callback (s);
484 return 0;
485}
486
487int
Florin Coras49568af2019-07-31 16:46:24 -0700488app_worker_migrate_notify (app_worker_t * app_wrk, session_t * s,
489 session_handle_t new_sh)
490{
491 application_t *app = application_get (app_wrk->app_index);
492 app->cb_fns.session_migrate_callback (s, new_sh);
493 return 0;
494}
495
496int
Florin Coras623eb562019-02-03 19:28:34 -0800497app_worker_own_session (app_worker_t * app_wrk, session_t * s)
498{
499 segment_manager_t *sm;
500 svm_fifo_t *rxf, *txf;
501
502 if (s->session_state == SESSION_STATE_LISTENING)
503 return application_change_listener_owner (s, app_wrk);
504
505 s->app_wrk_index = app_wrk->wrk_index;
506
507 rxf = s->rx_fifo;
508 txf = s->tx_fifo;
509
510 if (!rxf || !txf)
511 return 0;
512
513 s->rx_fifo = 0;
514 s->tx_fifo = 0;
515
Florin Coras94a6df02021-05-06 15:32:14 -0700516 sm = app_worker_get_connect_segment_manager (app_wrk);
Florin Corasa27a46e2019-02-18 13:02:28 -0800517 if (app_worker_alloc_session_fifos (sm, s))
Florin Coras623eb562019-02-03 19:28:34 -0800518 return -1;
519
Sirshak Das28aa5392019-02-05 01:33:33 -0600520 if (!svm_fifo_is_empty_cons (rxf))
521 svm_fifo_clone (s->rx_fifo, rxf);
Florin Coras623eb562019-02-03 19:28:34 -0800522
Sirshak Das28aa5392019-02-05 01:33:33 -0600523 if (!svm_fifo_is_empty_cons (txf))
524 svm_fifo_clone (s->tx_fifo, txf);
525
Florin Coras19223e02019-03-03 14:56:05 -0800526 segment_manager_dealloc_fifos (rxf, txf);
Florin Coras623eb562019-02-03 19:28:34 -0800527
528 return 0;
529}
530
531int
Florin Coras72db6642020-04-15 01:30:46 +0000532app_worker_connect_session (app_worker_t * app_wrk, session_endpoint_t * sep,
Florin Corasc9940fc2019-02-05 20:55:11 -0800533 u32 api_context)
Florin Coras623eb562019-02-03 19:28:34 -0800534{
535 int rv;
536
Florin Coras72db6642020-04-15 01:30:46 +0000537 if ((rv = session_open (app_wrk->wrk_index, sep, api_context)))
Florin Coras623eb562019-02-03 19:28:34 -0800538 return rv;
539
540 return 0;
541}
542
543int
Ryujiro Shibuyad8f48e22020-01-22 12:11:42 +0000544app_worker_session_fifo_tuning (app_worker_t * app_wrk, session_t * s,
545 svm_fifo_t * f,
546 session_ft_action_t act, u32 len)
547{
548 application_t *app = application_get (app_wrk->app_index);
549 return app->cb_fns.fifo_tuning_callback (s, f, act, len);
550}
551
Florin Coras623eb562019-02-03 19:28:34 -0800552segment_manager_t *
553app_worker_get_connect_segment_manager (app_worker_t * app)
554{
555 ASSERT (app->connects_seg_manager != (u32) ~ 0);
556 return segment_manager_get (app->connects_seg_manager);
557}
558
559segment_manager_t *
Florin Coras623eb562019-02-03 19:28:34 -0800560app_worker_get_listen_segment_manager (app_worker_t * app,
561 session_t * listener)
562{
563 uword *smp;
564 smp = hash_get (app->listeners_table, listen_session_get_handle (listener));
Dave Barach47d41ad2020-02-17 09:13:26 -0500565 ALWAYS_ASSERT (smp != 0);
Florin Coras623eb562019-02-03 19:28:34 -0800566 return segment_manager_get (*smp);
567}
568
569session_t *
Florin Corasc9940fc2019-02-05 20:55:11 -0800570app_worker_first_listener (app_worker_t * app_wrk, u8 fib_proto,
Florin Coras623eb562019-02-03 19:28:34 -0800571 u8 transport_proto)
572{
573 session_t *listener;
574 u64 handle;
575 u32 sm_index;
576 u8 sst;
577
578 sst = session_type_from_proto_and_ip (transport_proto,
579 fib_proto == FIB_PROTOCOL_IP4);
580
581 /* *INDENT-OFF* */
Florin Corasc9940fc2019-02-05 20:55:11 -0800582 hash_foreach (handle, sm_index, app_wrk->listeners_table, ({
Florin Coras623eb562019-02-03 19:28:34 -0800583 listener = listen_session_get_from_handle (handle);
584 if (listener->session_type == sst
Florin Corasd5c604d2019-03-18 09:06:35 -0700585 && !(listener->flags & SESSION_F_PROXY))
Florin Coras623eb562019-02-03 19:28:34 -0800586 return listener;
587 }));
588 /* *INDENT-ON* */
589
590 return 0;
591}
592
593session_t *
Florin Corasc9940fc2019-02-05 20:55:11 -0800594app_worker_proxy_listener (app_worker_t * app_wrk, u8 fib_proto,
Florin Coras623eb562019-02-03 19:28:34 -0800595 u8 transport_proto)
596{
597 session_t *listener;
598 u64 handle;
599 u32 sm_index;
600 u8 sst;
601
602 sst = session_type_from_proto_and_ip (transport_proto,
603 fib_proto == FIB_PROTOCOL_IP4);
604
605 /* *INDENT-OFF* */
Florin Corasc9940fc2019-02-05 20:55:11 -0800606 hash_foreach (handle, sm_index, app_wrk->listeners_table, ({
Florin Coras623eb562019-02-03 19:28:34 -0800607 listener = listen_session_get_from_handle (handle);
Florin Corasd5c604d2019-03-18 09:06:35 -0700608 if (listener->session_type == sst && (listener->flags & SESSION_F_PROXY))
Florin Coras623eb562019-02-03 19:28:34 -0800609 return listener;
610 }));
611 /* *INDENT-ON* */
612
613 return 0;
614}
615
616/**
617 * Send an API message to the external app, to map new segment
618 */
619int
Florin Coras2b81e3c2019-02-27 07:55:46 -0800620app_worker_add_segment_notify (app_worker_t * app_wrk, u64 segment_handle)
Florin Coras623eb562019-02-03 19:28:34 -0800621{
Florin Coras623eb562019-02-03 19:28:34 -0800622 application_t *app = application_get (app_wrk->app_index);
Florin Corasc4c4cf52019-08-24 18:17:34 -0700623
624 return app->cb_fns.add_segment_callback (app_wrk->wrk_index,
Florin Coras623eb562019-02-03 19:28:34 -0800625 segment_handle);
626}
627
Florin Coras2b81e3c2019-02-27 07:55:46 -0800628int
629app_worker_del_segment_notify (app_worker_t * app_wrk, u64 segment_handle)
630{
631 application_t *app = application_get (app_wrk->app_index);
Florin Corasc4c4cf52019-08-24 18:17:34 -0700632 return app->cb_fns.del_segment_callback (app_wrk->wrk_index,
Florin Coras2b81e3c2019-02-27 07:55:46 -0800633 segment_handle);
634}
635
Florin Coras31c99552019-03-01 13:00:58 -0800636static inline u8
Florin Coras623eb562019-02-03 19:28:34 -0800637app_worker_application_is_builtin (app_worker_t * app_wrk)
638{
639 return app_wrk->app_is_builtin;
640}
641
642static inline int
Florin Coras2b5fed82019-07-25 14:51:09 -0700643app_send_io_evt_rx (app_worker_t * app_wrk, session_t * s)
Florin Coras623eb562019-02-03 19:28:34 -0800644{
645 session_event_t *evt;
646 svm_msg_q_msg_t msg;
647 svm_msg_q_t *mq;
648
Florin Coras5c290292019-09-19 08:19:44 -0700649 if (app_worker_application_is_builtin (app_wrk))
650 return app_worker_builtin_rx (app_wrk, s);
651
Florin Coras653e43f2019-03-04 10:56:23 -0800652 if (svm_fifo_has_event (s->rx_fifo))
Florin Coras623eb562019-02-03 19:28:34 -0800653 return 0;
654
655 mq = app_wrk->event_queue;
Florin Coras2b5fed82019-07-25 14:51:09 -0700656 svm_msg_q_lock (mq);
657
658 if (PREDICT_FALSE (svm_msg_q_is_full (mq)))
659 {
660 clib_warning ("evt q full");
661 svm_msg_q_unlock (mq);
662 return -1;
663 }
Florin Coras623eb562019-02-03 19:28:34 -0800664
665 if (PREDICT_FALSE (svm_msg_q_ring_is_full (mq, SESSION_MQ_IO_EVT_RING)))
666 {
667 clib_warning ("evt q rings full");
Florin Coras2b5fed82019-07-25 14:51:09 -0700668 svm_msg_q_unlock (mq);
Florin Coras623eb562019-02-03 19:28:34 -0800669 return -1;
670 }
671
672 msg = svm_msg_q_alloc_msg_w_ring (mq, SESSION_MQ_IO_EVT_RING);
Florin Coras623eb562019-02-03 19:28:34 -0800673 evt = (session_event_t *) svm_msg_q_msg_data (mq, &msg);
Florin Corasc547e912020-12-08 17:50:45 -0800674 evt->session_index = s->rx_fifo->shr->client_session_index;
Florin Corasf6c43132019-03-01 12:41:21 -0800675 evt->event_type = SESSION_IO_EVT_RX;
Florin Coras623eb562019-02-03 19:28:34 -0800676
677 (void) svm_fifo_set_event (s->rx_fifo);
Florin Coras2b5fed82019-07-25 14:51:09 -0700678 svm_msg_q_add_and_unlock (mq, &msg);
Florin Coras623eb562019-02-03 19:28:34 -0800679
Florin Coras623eb562019-02-03 19:28:34 -0800680 return 0;
681}
682
683static inline int
Florin Coras2b5fed82019-07-25 14:51:09 -0700684app_send_io_evt_tx (app_worker_t * app_wrk, session_t * s)
Florin Coras623eb562019-02-03 19:28:34 -0800685{
686 svm_msg_q_t *mq;
687 session_event_t *evt;
688 svm_msg_q_msg_t msg;
689
690 if (app_worker_application_is_builtin (app_wrk))
Florin Coras0e573f52019-05-07 16:28:16 -0700691 return app_worker_builtin_tx (app_wrk, s);
Florin Coras623eb562019-02-03 19:28:34 -0800692
693 mq = app_wrk->event_queue;
Florin Coras2b5fed82019-07-25 14:51:09 -0700694 svm_msg_q_lock (mq);
695
696 if (PREDICT_FALSE (svm_msg_q_is_full (mq)))
697 {
698 clib_warning ("evt q full");
699 svm_msg_q_unlock (mq);
700 return -1;
701 }
Florin Coras623eb562019-02-03 19:28:34 -0800702
703 if (PREDICT_FALSE (svm_msg_q_ring_is_full (mq, SESSION_MQ_IO_EVT_RING)))
704 {
705 clib_warning ("evt q rings full");
Florin Coras2b5fed82019-07-25 14:51:09 -0700706 svm_msg_q_unlock (mq);
Florin Coras623eb562019-02-03 19:28:34 -0800707 return -1;
708 }
709
710 msg = svm_msg_q_alloc_msg_w_ring (mq, SESSION_MQ_IO_EVT_RING);
Florin Coras623eb562019-02-03 19:28:34 -0800711 evt = (session_event_t *) svm_msg_q_msg_data (mq, &msg);
Florin Corasf6c43132019-03-01 12:41:21 -0800712 evt->event_type = SESSION_IO_EVT_TX;
Florin Corasc547e912020-12-08 17:50:45 -0800713 evt->session_index = s->tx_fifo->shr->client_session_index;
Florin Coras623eb562019-02-03 19:28:34 -0800714
Florin Coras2b5fed82019-07-25 14:51:09 -0700715 svm_msg_q_add_and_unlock (mq, &msg);
716 return 0;
Florin Coras623eb562019-02-03 19:28:34 -0800717}
718
719/* *INDENT-OFF* */
720typedef int (app_send_evt_handler_fn) (app_worker_t *app,
Florin Coras2b5fed82019-07-25 14:51:09 -0700721 session_t *s);
Florin Coras653e43f2019-03-04 10:56:23 -0800722static app_send_evt_handler_fn * const app_send_evt_handler_fns[2] = {
Florin Coras623eb562019-02-03 19:28:34 -0800723 app_send_io_evt_rx,
Florin Coras623eb562019-02-03 19:28:34 -0800724 app_send_io_evt_tx,
725};
726/* *INDENT-ON* */
727
728/**
729 * Send event to application
730 *
Florin Coras623eb562019-02-03 19:28:34 -0800731 * Logic from queue perspective is blocking. However, if queue is full,
732 * we return.
733 */
734int
735app_worker_lock_and_send_event (app_worker_t * app, session_t * s,
736 u8 evt_type)
737{
Florin Coras2b5fed82019-07-25 14:51:09 -0700738 return app_send_evt_handler_fns[evt_type] (app, s);
Florin Coras623eb562019-02-03 19:28:34 -0800739}
740
Florin Coras623eb562019-02-03 19:28:34 -0800741u8 *
742format_app_worker_listener (u8 * s, va_list * args)
743{
744 app_worker_t *app_wrk = va_arg (*args, app_worker_t *);
745 u64 handle = va_arg (*args, u64);
746 u32 sm_index = va_arg (*args, u32);
747 int verbose = va_arg (*args, int);
748 session_t *listener;
749 const u8 *app_name;
750 u8 *str;
751
752 if (!app_wrk)
753 {
754 if (verbose)
Florin Coras91413ac2020-10-13 07:40:42 -0700755 s = format (s, "%-40s%-25s%-10s%-15s%-15s%-10s", "Connection", "App",
Florin Coras623eb562019-02-03 19:28:34 -0800756 "Wrk", "API Client", "ListenerID", "SegManager");
757 else
Florin Coras91413ac2020-10-13 07:40:42 -0700758 s = format (s, "%-40s%-25s%-10s", "Connection", "App", "Wrk");
Florin Coras623eb562019-02-03 19:28:34 -0800759
760 return s;
761 }
762
763 app_name = application_name_from_index (app_wrk->app_index);
764 listener = listen_session_get_from_handle (handle);
Florin Coras31c99552019-03-01 13:00:58 -0800765 str = format (0, "%U", format_session, listener, verbose);
Florin Coras623eb562019-02-03 19:28:34 -0800766
767 if (verbose)
768 {
Dave Barach3e07a4a2020-04-04 10:05:48 -0400769 u8 *buf;
770 buf = format (0, "%u(%u)", app_wrk->wrk_map_index, app_wrk->wrk_index);
Florin Coras91413ac2020-10-13 07:40:42 -0700771 s = format (s, "%-40v%-25v%-10v%-15u%-15u%-10u", str, app_name,
Florin Coras623eb562019-02-03 19:28:34 -0800772 buf, app_wrk->api_client_index, handle, sm_index);
Dave Barach3e07a4a2020-04-04 10:05:48 -0400773 vec_free (buf);
Florin Coras623eb562019-02-03 19:28:34 -0800774 }
775 else
jiangxiaoming6b410e62020-10-10 15:23:54 +0800776 s = format (s, "%-40v%-25v%=10u", str, app_name, app_wrk->wrk_map_index);
777
778 vec_free (str);
Florin Coras623eb562019-02-03 19:28:34 -0800779
780 return s;
781}
782
783u8 *
784format_app_worker (u8 * s, va_list * args)
785{
786 app_worker_t *app_wrk = va_arg (*args, app_worker_t *);
787 u32 indent = 1;
788
789 s = format (s, "%U wrk-index %u app-index %u map-index %u "
790 "api-client-index %d\n", format_white_space, indent,
791 app_wrk->wrk_index, app_wrk->app_index, app_wrk->wrk_map_index,
792 app_wrk->api_client_index);
793 return s;
794}
795
796void
797app_worker_format_connects (app_worker_t * app_wrk, int verbose)
798{
Florin Coras623eb562019-02-03 19:28:34 -0800799 segment_manager_t *sm;
Florin Coras623eb562019-02-03 19:28:34 -0800800
801 /* Header */
802 if (!app_wrk)
803 {
Florin Coras88001c62019-04-24 14:44:46 -0700804 segment_manager_format_sessions (0, verbose);
Florin Coras623eb562019-02-03 19:28:34 -0800805 return;
806 }
807
808 if (app_wrk->connects_seg_manager == (u32) ~ 0)
809 return;
810
Florin Coras623eb562019-02-03 19:28:34 -0800811 sm = segment_manager_get (app_wrk->connects_seg_manager);
Florin Coras88001c62019-04-24 14:44:46 -0700812 segment_manager_format_sessions (sm, verbose);
Florin Coras623eb562019-02-03 19:28:34 -0800813}
814
Florin Coras623eb562019-02-03 19:28:34 -0800815/*
816 * fd.io coding-style-patch-verification: ON
817 *
818 * Local Variables:
819 * eval: (c-set-style "gnu")
820 * End:
821 */