blob: 1927f2f78d3329c6e65f76c3909c68ac18b1a54f [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);
Florin Coras20c24232021-11-22 21:19:01 -080036 clib_spinlock_init (&app_wrk->postponed_mq_msgs_lock);
Nathan Skrzypczakba65ca42019-05-16 16:35:40 +020037 APP_DBG ("New app %v worker %u", app->name, app_wrk->wrk_index);
Florin Coras623eb562019-02-03 19:28:34 -080038 return app_wrk;
39}
40
41app_worker_t *
42app_worker_get (u32 wrk_index)
43{
44 return pool_elt_at_index (app_workers, wrk_index);
45}
46
47app_worker_t *
48app_worker_get_if_valid (u32 wrk_index)
49{
50 if (pool_is_free_index (app_workers, wrk_index))
51 return 0;
52 return pool_elt_at_index (app_workers, wrk_index);
53}
54
55void
56app_worker_free (app_worker_t * app_wrk)
57{
58 application_t *app = application_get (app_wrk->app_index);
Florin Corasc1a42652019-02-08 18:27:29 -080059 vnet_unlisten_args_t _a, *a = &_a;
Florin Corasbf395972020-04-30 15:05:24 +000060 u64 handle, *handles = 0, *sm_indices = 0;
Florin Coras623eb562019-02-03 19:28:34 -080061 segment_manager_t *sm;
Florin Corasea727642021-05-07 19:39:43 -070062 session_handle_t *sh;
Florin Coras87d66332019-06-11 12:31:31 -070063 session_t *ls;
Florin Coras623eb562019-02-03 19:28:34 -080064 u32 sm_index;
Florin Corasea727642021-05-07 19:39:43 -070065 int i;
Florin Coras623eb562019-02-03 19:28:34 -080066
67 /*
68 * Listener cleanup
69 */
70
71 /* *INDENT-OFF* */
Florin Corasc9940fc2019-02-05 20:55:11 -080072 hash_foreach (handle, sm_index, app_wrk->listeners_table, ({
73 ls = listen_session_get_from_handle (handle);
Florin Coras87d66332019-06-11 12:31:31 -070074 vec_add1 (handles, app_listen_session_handle (ls));
Florin Corasbf395972020-04-30 15:05:24 +000075 vec_add1 (sm_indices, sm_index);
Florin Coras623eb562019-02-03 19:28:34 -080076 sm = segment_manager_get (sm_index);
Florin Coras623eb562019-02-03 19:28:34 -080077 }));
78 /* *INDENT-ON* */
79
80 for (i = 0; i < vec_len (handles); i++)
81 {
Florin Corasbf395972020-04-30 15:05:24 +000082 /* Cleanup listener */
Florin Coras623eb562019-02-03 19:28:34 -080083 a->app_index = app->app_index;
84 a->wrk_map_index = app_wrk->wrk_map_index;
85 a->handle = handles[i];
Florin Corasc1a42652019-02-08 18:27:29 -080086 (void) vnet_unlisten (a);
Florin Corasbf395972020-04-30 15:05:24 +000087
88 sm = segment_manager_get_if_valid (sm_indices[i]);
89 if (sm && !segment_manager_app_detached (sm))
90 {
91 sm->first_is_protected = 0;
92 segment_manager_init_free (sm);
93 }
Florin Coras623eb562019-02-03 19:28:34 -080094 }
Florin Corasd50ff7f2020-04-16 04:30:22 +000095 vec_reset_length (handles);
Florin Corasbf395972020-04-30 15:05:24 +000096 vec_free (sm_indices);
97 hash_free (app_wrk->listeners_table);
Florin Coras623eb562019-02-03 19:28:34 -080098
99 /*
100 * Connects segment manager cleanup
101 */
102
103 if (app_wrk->connects_seg_manager != APP_INVALID_SEGMENT_MANAGER_INDEX)
104 {
105 sm = segment_manager_get (app_wrk->connects_seg_manager);
106 sm->app_wrk_index = SEGMENT_MANAGER_INVALID_APP_INDEX;
Florin Coras565115e2019-02-20 19:48:31 -0800107 sm->first_is_protected = 0;
Florin Coras88001c62019-04-24 14:44:46 -0700108 segment_manager_init_free (sm);
Florin Coras623eb562019-02-03 19:28:34 -0800109 }
110
Florin Corasd50ff7f2020-04-16 04:30:22 +0000111 /*
112 * Half-open cleanup
113 */
114
Florin Corasea727642021-05-07 19:39:43 -0700115 pool_foreach (sh, app_wrk->half_open_table)
116 session_cleanup_half_open (*sh);
Florin Corasd50ff7f2020-04-16 04:30:22 +0000117
Florin Corasea727642021-05-07 19:39:43 -0700118 pool_free (app_wrk->half_open_table);
Florin Corasd50ff7f2020-04-16 04:30:22 +0000119
Florin Corasc8e812f2020-05-14 05:32:18 +0000120 /*
121 * Detached listener segment managers cleanup
122 */
123 for (i = 0; i < vec_len (app_wrk->detached_seg_managers); i++)
124 {
125 sm = segment_manager_get (app_wrk->detached_seg_managers[i]);
126 segment_manager_init_free (sm);
127 }
128 vec_free (app_wrk->detached_seg_managers);
129 clib_spinlock_free (&app_wrk->detached_seg_managers_lock);
Florin Coras20c24232021-11-22 21:19:01 -0800130 clib_spinlock_free (&app_wrk->postponed_mq_msgs_lock);
Florin Corasc8e812f2020-05-14 05:32:18 +0000131
Florin Coras623eb562019-02-03 19:28:34 -0800132 if (CLIB_DEBUG)
133 clib_memset (app_wrk, 0xfe, sizeof (*app_wrk));
Benoît Ganned4aeb842019-07-18 18:38:42 +0200134 pool_put (app_workers, app_wrk);
Florin Coras623eb562019-02-03 19:28:34 -0800135}
136
137application_t *
138app_worker_get_app (u32 wrk_index)
139{
140 app_worker_t *app_wrk;
141 app_wrk = app_worker_get_if_valid (wrk_index);
142 if (!app_wrk)
143 return 0;
144 return application_get_if_valid (app_wrk->app_index);
145}
146
147static segment_manager_t *
148app_worker_alloc_segment_manager (app_worker_t * app_wrk)
149{
Florin Coras94a6df02021-05-06 15:32:14 -0700150 segment_manager_t *sm;
Florin Coras623eb562019-02-03 19:28:34 -0800151
Florin Coras94a6df02021-05-06 15:32:14 -0700152 sm = segment_manager_alloc ();
Florin Coras623eb562019-02-03 19:28:34 -0800153 sm->app_wrk_index = app_wrk->wrk_index;
Florin Corasa107f402020-09-29 19:18:46 -0700154 segment_manager_init (sm);
Florin Coras623eb562019-02-03 19:28:34 -0800155 return sm;
156}
157
Florin Corasa27a46e2019-02-18 13:02:28 -0800158static int
159app_worker_alloc_session_fifos (segment_manager_t * sm, session_t * s)
160{
161 svm_fifo_t *rx_fifo = 0, *tx_fifo = 0;
Florin Corasa27a46e2019-02-18 13:02:28 -0800162 int rv;
163
Florin Coras62ddc032019-12-08 18:30:42 -0800164 if ((rv = segment_manager_alloc_session_fifos (sm, s->thread_index,
165 &rx_fifo, &tx_fifo)))
Florin Corasa27a46e2019-02-18 13:02:28 -0800166 return rv;
167
Florin Corasc547e912020-12-08 17:50:45 -0800168 rx_fifo->shr->master_session_index = s->session_index;
Florin Corasa27a46e2019-02-18 13:02:28 -0800169 rx_fifo->master_thread_index = s->thread_index;
170
Florin Corasc547e912020-12-08 17:50:45 -0800171 tx_fifo->shr->master_session_index = s->session_index;
Florin Corasa27a46e2019-02-18 13:02:28 -0800172 tx_fifo->master_thread_index = s->thread_index;
173
174 s->rx_fifo = rx_fifo;
175 s->tx_fifo = tx_fifo;
Florin Corasa27a46e2019-02-18 13:02:28 -0800176 return 0;
177}
178
Florin Coras623eb562019-02-03 19:28:34 -0800179int
Florin Corasd4295e62019-02-22 13:11:38 -0800180app_worker_init_listener (app_worker_t * app_wrk, session_t * ls)
Florin Coras623eb562019-02-03 19:28:34 -0800181{
182 segment_manager_t *sm;
183
184 /* Allocate segment manager. All sessions derived out of a listen session
185 * have fifos allocated by the same segment manager. */
186 if (!(sm = app_worker_alloc_segment_manager (app_wrk)))
Florin Coras00e01d32019-10-21 16:07:46 -0700187 return SESSION_E_ALLOC;
Florin Coras623eb562019-02-03 19:28:34 -0800188
Florin Coras1a4aaf12021-11-27 10:30:03 -0800189 /* Once the first segment is mapped, don't remove it until unlisten */
190 sm->first_is_protected = 1;
191
Florin Corasc9940fc2019-02-05 20:55:11 -0800192 /* Keep track of the segment manager for the listener or this worker */
Florin Coras623eb562019-02-03 19:28:34 -0800193 hash_set (app_wrk->listeners_table, listen_session_get_handle (ls),
194 segment_manager_index (sm));
195
Florin Coras87b7e3d2020-03-27 15:06:07 +0000196 if (transport_connection_is_cless (session_get_transport (ls)))
Florin Coras623eb562019-02-03 19:28:34 -0800197 {
Florin Coras87b7e3d2020-03-27 15:06:07 +0000198 if (ls->rx_fifo)
Florin Coras00e01d32019-10-21 16:07:46 -0700199 return SESSION_E_NOSUPPORT;
200 return app_worker_alloc_session_fifos (sm, ls);
Florin Coras623eb562019-02-03 19:28:34 -0800201 }
202 return 0;
203}
204
205int
Florin Corasd4295e62019-02-22 13:11:38 -0800206app_worker_start_listen (app_worker_t * app_wrk,
207 app_listener_t * app_listener)
208{
209 session_t *ls;
Florin Coras00e01d32019-10-21 16:07:46 -0700210 int rv;
Florin Corasd4295e62019-02-22 13:11:38 -0800211
212 if (clib_bitmap_get (app_listener->workers, app_wrk->wrk_map_index))
Florin Coras00e01d32019-10-21 16:07:46 -0700213 return SESSION_E_ALREADY_LISTENING;
Florin Corasd4295e62019-02-22 13:11:38 -0800214
215 app_listener->workers = clib_bitmap_set (app_listener->workers,
216 app_wrk->wrk_map_index, 1);
217
218 if (app_listener->session_index != SESSION_INVALID_INDEX)
219 {
220 ls = session_get (app_listener->session_index, 0);
Florin Coras00e01d32019-10-21 16:07:46 -0700221 if ((rv = app_worker_init_listener (app_wrk, ls)))
222 return rv;
Florin Corasd4295e62019-02-22 13:11:38 -0800223 }
224
225 if (app_listener->local_index != SESSION_INVALID_INDEX)
226 {
227 ls = session_get (app_listener->local_index, 0);
Florin Coras00e01d32019-10-21 16:07:46 -0700228 if ((rv = app_worker_init_listener (app_wrk, ls)))
229 return rv;
Florin Corasd4295e62019-02-22 13:11:38 -0800230 }
231
232 return 0;
233}
234
Florin Coras2b81e3c2019-02-27 07:55:46 -0800235static void
Florin Corasc8e812f2020-05-14 05:32:18 +0000236app_worker_add_detached_sm (app_worker_t * app_wrk, u32 sm_index)
237{
238 vec_add1 (app_wrk->detached_seg_managers, sm_index);
239}
240
241void
242app_worker_del_detached_sm (app_worker_t * app_wrk, u32 sm_index)
243{
244 u32 i;
245
246 clib_spinlock_lock (&app_wrk->detached_seg_managers_lock);
247 for (i = 0; i < vec_len (app_wrk->detached_seg_managers); i++)
248 {
249 if (app_wrk->detached_seg_managers[i] == sm_index)
250 {
251 vec_del1 (app_wrk->detached_seg_managers, i);
252 break;
253 }
254 }
255 clib_spinlock_unlock (&app_wrk->detached_seg_managers_lock);
256}
257
258static void
Florin Coras2b81e3c2019-02-27 07:55:46 -0800259app_worker_stop_listen_session (app_worker_t * app_wrk, session_t * ls)
Florin Coras623eb562019-02-03 19:28:34 -0800260{
Florin Corasc9940fc2019-02-05 20:55:11 -0800261 session_handle_t handle;
Florin Coras623eb562019-02-03 19:28:34 -0800262 segment_manager_t *sm;
263 uword *sm_indexp;
liuyacan87d48ad2021-04-28 11:34:03 +0000264 session_state_t *states = 0;
Florin Coras2b81e3c2019-02-27 07:55:46 -0800265
266 handle = listen_session_get_handle (ls);
267 sm_indexp = hash_get (app_wrk->listeners_table, handle);
268 if (PREDICT_FALSE (!sm_indexp))
269 return;
270
Florin Coras1bd46162020-04-13 23:35:55 +0000271 /* Dealloc fifos, if any (dgram listeners) */
Florin Coras87b7e3d2020-03-27 15:06:07 +0000272 if (ls->rx_fifo)
273 {
274 segment_manager_dealloc_fifos (ls->rx_fifo, ls->tx_fifo);
275 ls->tx_fifo = ls->rx_fifo = 0;
276 }
277
Florin Coras1bd46162020-04-13 23:35:55 +0000278 /* Try to cleanup segment manager */
Florin Coras2b81e3c2019-02-27 07:55:46 -0800279 sm = segment_manager_get (*sm_indexp);
Florin Coras94a6df02021-05-06 15:32:14 -0700280 if (sm)
Florin Coras2b81e3c2019-02-27 07:55:46 -0800281 {
Florin Coras1a4aaf12021-11-27 10:30:03 -0800282 sm->first_is_protected = 0;
Florin Coras1bd46162020-04-13 23:35:55 +0000283 segment_manager_app_detach (sm);
284 if (!segment_manager_has_fifos (sm))
Florin Coras94a6df02021-05-06 15:32:14 -0700285 {
286 /* Empty segment manager, cleanup it up */
287 segment_manager_free (sm);
288 }
Florin Corasc8e812f2020-05-14 05:32:18 +0000289 else
290 {
Florin Coras94a6df02021-05-06 15:32:14 -0700291 /* Delete sessions in CREATED state */
292 vec_add1 (states, SESSION_STATE_CREATED);
293 segment_manager_del_sessions_filter (sm, states);
294 vec_free (states);
295
Florin Corasc8e812f2020-05-14 05:32:18 +0000296 /* Track segment manager in case app detaches and all the
297 * outstanding sessions need to be closed */
298 app_worker_add_detached_sm (app_wrk, *sm_indexp);
299 sm->flags |= SEG_MANAGER_F_DETACHED_LISTENER;
300 }
Florin Coras2b81e3c2019-02-27 07:55:46 -0800301 }
Florin Coras1bd46162020-04-13 23:35:55 +0000302
Florin Coras2b81e3c2019-02-27 07:55:46 -0800303 hash_unset (app_wrk->listeners_table, handle);
304}
305
306int
307app_worker_stop_listen (app_worker_t * app_wrk, app_listener_t * al)
308{
Florin Corasd4295e62019-02-22 13:11:38 -0800309 session_t *ls;
Florin Coras623eb562019-02-03 19:28:34 -0800310
Florin Corasc9940fc2019-02-05 20:55:11 -0800311 if (!clib_bitmap_get (al->workers, app_wrk->wrk_map_index))
312 return 0;
313
314 if (al->session_index != SESSION_INVALID_INDEX)
Florin Coras623eb562019-02-03 19:28:34 -0800315 {
Florin Corasc9940fc2019-02-05 20:55:11 -0800316 ls = listen_session_get (al->session_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 }
319
Florin Corasc9940fc2019-02-05 20:55:11 -0800320 if (al->local_index != SESSION_INVALID_INDEX)
Florin Coras623eb562019-02-03 19:28:34 -0800321 {
Florin Corasd4295e62019-02-22 13:11:38 -0800322 ls = listen_session_get (al->local_index);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800323 app_worker_stop_listen_session (app_wrk, ls);
Florin Coras623eb562019-02-03 19:28:34 -0800324 }
Florin Corasc9940fc2019-02-05 20:55:11 -0800325
326 clib_bitmap_set_no_check (al->workers, app_wrk->wrk_map_index, 0);
327 if (clib_bitmap_is_zero (al->workers))
328 app_listener_cleanup (al);
Florin Coras623eb562019-02-03 19:28:34 -0800329
330 return 0;
331}
332
333int
Florin Corasa27a46e2019-02-18 13:02:28 -0800334app_worker_init_accepted (session_t * s)
335{
336 app_worker_t *app_wrk;
337 segment_manager_t *sm;
338 session_t *listener;
Ryujiro Shibuyad8f48e22020-01-22 12:11:42 +0000339 application_t *app;
Florin Corasa27a46e2019-02-18 13:02:28 -0800340
Nathan Skrzypczak2f0f96b2019-06-13 10:14:28 +0200341 listener = listen_session_get_from_handle (s->listener_handle);
Florin Corasa27a46e2019-02-18 13:02:28 -0800342 app_wrk = application_listener_select_worker (listener);
Florin Coras20c24232021-11-22 21:19:01 -0800343 if (PREDICT_FALSE (app_wrk->mq_congested))
344 return -1;
Florin Coras2b81e3c2019-02-27 07:55:46 -0800345
Florin Coras20c24232021-11-22 21:19:01 -0800346 s->app_wrk_index = app_wrk->wrk_index;
Ryujiro Shibuyad8f48e22020-01-22 12:11:42 +0000347 app = application_get (app_wrk->app_index);
348 if (app->cb_fns.fifo_tuning_callback)
349 s->flags |= SESSION_F_CUSTOM_FIFO_TUNING;
350
Florin Corasa27a46e2019-02-18 13:02:28 -0800351 sm = app_worker_get_listen_segment_manager (app_wrk, listener);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800352 if (app_worker_alloc_session_fifos (sm, s))
353 return -1;
354
355 return 0;
Florin Corasa27a46e2019-02-18 13:02:28 -0800356}
357
358int
359app_worker_accept_notify (app_worker_t * app_wrk, session_t * s)
360{
361 application_t *app = application_get (app_wrk->app_index);
362 return app->cb_fns.session_accept_callback (s);
363}
364
365int
366app_worker_init_connected (app_worker_t * app_wrk, session_t * s)
367{
368 application_t *app = application_get (app_wrk->app_index);
369 segment_manager_t *sm;
370
Ryujiro Shibuyad8f48e22020-01-22 12:11:42 +0000371 if (app->cb_fns.fifo_tuning_callback)
372 s->flags |= SESSION_F_CUSTOM_FIFO_TUNING;
373
Florin Corase6552402020-11-11 13:25:53 -0800374 /* Allocate fifos for session, unless the app is a builtin proxy */
375 if (application_is_builtin_proxy (app))
376 return 0;
377
378 sm = app_worker_get_connect_segment_manager (app_wrk);
379 return app_worker_alloc_session_fifos (sm, s);
Florin Corasa27a46e2019-02-18 13:02:28 -0800380}
381
382int
Florin Coras00e01d32019-10-21 16:07:46 -0700383app_worker_connect_notify (app_worker_t * app_wrk, session_t * s,
384 session_error_t err, u32 opaque)
Florin Corasa27a46e2019-02-18 13:02:28 -0800385{
386 application_t *app = application_get (app_wrk->app_index);
387 return app->cb_fns.session_connected_callback (app_wrk->wrk_index, opaque,
Florin Coras00e01d32019-10-21 16:07:46 -0700388 s, err);
Florin Corasa27a46e2019-02-18 13:02:28 -0800389}
390
391int
Florin Corasea727642021-05-07 19:39:43 -0700392app_worker_add_half_open (app_worker_t *app_wrk, session_handle_t sh)
Florin Corasd50ff7f2020-04-16 04:30:22 +0000393{
Florin Corasea727642021-05-07 19:39:43 -0700394 session_handle_t *shp;
395
Florin Coras309f7aa2022-03-18 08:33:08 -0700396 ASSERT (session_vlib_thread_is_cl_thread ());
Florin Corasea727642021-05-07 19:39:43 -0700397 pool_get (app_wrk->half_open_table, shp);
398 *shp = sh;
399
400 return (shp - app_wrk->half_open_table);
Florin Corasd50ff7f2020-04-16 04:30:22 +0000401}
402
403int
Florin Coras2c876f92021-05-10 21:12:27 -0700404app_worker_del_half_open (app_worker_t *app_wrk, session_t *s)
Florin Corasd50ff7f2020-04-16 04:30:22 +0000405{
Florin Coras2c876f92021-05-10 21:12:27 -0700406 application_t *app = application_get (app_wrk->app_index);
Florin Coras309f7aa2022-03-18 08:33:08 -0700407 ASSERT (session_vlib_thread_is_cl_thread ());
Florin Coras2c876f92021-05-10 21:12:27 -0700408 pool_put_index (app_wrk->half_open_table, s->ho_index);
409 if (app->cb_fns.half_open_cleanup_callback)
410 app->cb_fns.half_open_cleanup_callback (s);
Florin Corasd50ff7f2020-04-16 04:30:22 +0000411 return 0;
412}
413
Florin Corasd50ff7f2020-04-16 04:30:22 +0000414int
Florin Corasbf7ce2c2019-03-06 14:44:42 -0800415app_worker_close_notify (app_worker_t * app_wrk, session_t * s)
416{
417 application_t *app = application_get (app_wrk->app_index);
418 app->cb_fns.session_disconnect_callback (s);
419 return 0;
420}
421
422int
Florin Coras692b9492019-07-12 15:01:53 -0700423app_worker_transport_closed_notify (app_worker_t * app_wrk, session_t * s)
424{
425 application_t *app = application_get (app_wrk->app_index);
426 if (app->cb_fns.session_transport_closed_callback)
427 app->cb_fns.session_transport_closed_callback (s);
428 return 0;
429}
430
431int
Florin Coras69b68ef2019-04-02 11:38:51 -0700432app_worker_reset_notify (app_worker_t * app_wrk, session_t * s)
433{
434 application_t *app = application_get (app_wrk->app_index);
435 app->cb_fns.session_reset_callback (s);
436 return 0;
437}
438
439int
Florin Coras70f26d52019-07-08 11:47:18 -0700440app_worker_cleanup_notify (app_worker_t * app_wrk, session_t * s,
441 session_cleanup_ntf_t ntf)
442{
443 application_t *app = application_get (app_wrk->app_index);
444 if (app->cb_fns.session_cleanup_callback)
445 app->cb_fns.session_cleanup_callback (s, ntf);
446 return 0;
447}
448
449int
Florin Corasbf7ce2c2019-03-06 14:44:42 -0800450app_worker_builtin_rx (app_worker_t * app_wrk, session_t * s)
451{
452 application_t *app = application_get (app_wrk->app_index);
453 app->cb_fns.builtin_app_rx_callback (s);
454 return 0;
455}
456
457int
Florin Coras0e573f52019-05-07 16:28:16 -0700458app_worker_builtin_tx (app_worker_t * app_wrk, session_t * s)
459{
460 application_t *app = application_get (app_wrk->app_index);
461
462 if (!app->cb_fns.builtin_app_tx_callback)
463 return 0;
464
465 app->cb_fns.builtin_app_tx_callback (s);
466 return 0;
467}
468
469int
Florin Coras49568af2019-07-31 16:46:24 -0700470app_worker_migrate_notify (app_worker_t * app_wrk, session_t * s,
471 session_handle_t new_sh)
472{
473 application_t *app = application_get (app_wrk->app_index);
474 app->cb_fns.session_migrate_callback (s, new_sh);
475 return 0;
476}
477
478int
Florin Coras623eb562019-02-03 19:28:34 -0800479app_worker_own_session (app_worker_t * app_wrk, session_t * s)
480{
481 segment_manager_t *sm;
482 svm_fifo_t *rxf, *txf;
Filip Tehlarac3c8dc2023-03-14 08:50:28 +0100483 int rv;
Florin Coras623eb562019-02-03 19:28:34 -0800484
485 if (s->session_state == SESSION_STATE_LISTENING)
486 return application_change_listener_owner (s, app_wrk);
487
488 s->app_wrk_index = app_wrk->wrk_index;
489
490 rxf = s->rx_fifo;
491 txf = s->tx_fifo;
492
493 if (!rxf || !txf)
494 return 0;
495
496 s->rx_fifo = 0;
497 s->tx_fifo = 0;
498
Florin Coras94a6df02021-05-06 15:32:14 -0700499 sm = app_worker_get_connect_segment_manager (app_wrk);
Filip Tehlarac3c8dc2023-03-14 08:50:28 +0100500 if ((rv = app_worker_alloc_session_fifos (sm, s)))
501 return rv;
Florin Coras623eb562019-02-03 19:28:34 -0800502
Sirshak Das28aa5392019-02-05 01:33:33 -0600503 if (!svm_fifo_is_empty_cons (rxf))
504 svm_fifo_clone (s->rx_fifo, rxf);
Florin Coras623eb562019-02-03 19:28:34 -0800505
Sirshak Das28aa5392019-02-05 01:33:33 -0600506 if (!svm_fifo_is_empty_cons (txf))
507 svm_fifo_clone (s->tx_fifo, txf);
508
Florin Coras19223e02019-03-03 14:56:05 -0800509 segment_manager_dealloc_fifos (rxf, txf);
Florin Coras623eb562019-02-03 19:28:34 -0800510
511 return 0;
512}
513
514int
Florin Coras89a9f612021-05-11 11:55:07 -0700515app_worker_connect_session (app_worker_t *app_wrk, session_endpoint_cfg_t *sep,
516 session_handle_t *rsh)
Florin Coras623eb562019-02-03 19:28:34 -0800517{
Florin Coras20c24232021-11-22 21:19:01 -0800518 if (PREDICT_FALSE (app_wrk->mq_congested))
519 return SESSION_E_REFUSED;
520
Florin Coras89a9f612021-05-11 11:55:07 -0700521 sep->app_wrk_index = app_wrk->wrk_index;
Florin Coras623eb562019-02-03 19:28:34 -0800522
Florin Coras89a9f612021-05-11 11:55:07 -0700523 return session_open (sep, rsh);
Florin Coras623eb562019-02-03 19:28:34 -0800524}
525
526int
Ryujiro Shibuyad8f48e22020-01-22 12:11:42 +0000527app_worker_session_fifo_tuning (app_worker_t * app_wrk, session_t * s,
528 svm_fifo_t * f,
529 session_ft_action_t act, u32 len)
530{
531 application_t *app = application_get (app_wrk->app_index);
532 return app->cb_fns.fifo_tuning_callback (s, f, act, len);
533}
534
Florin Coras623eb562019-02-03 19:28:34 -0800535segment_manager_t *
536app_worker_get_connect_segment_manager (app_worker_t * app)
537{
538 ASSERT (app->connects_seg_manager != (u32) ~ 0);
539 return segment_manager_get (app->connects_seg_manager);
540}
541
542segment_manager_t *
Florin Coras623eb562019-02-03 19:28:34 -0800543app_worker_get_listen_segment_manager (app_worker_t * app,
544 session_t * listener)
545{
546 uword *smp;
547 smp = hash_get (app->listeners_table, listen_session_get_handle (listener));
Dave Barach47d41ad2020-02-17 09:13:26 -0500548 ALWAYS_ASSERT (smp != 0);
Florin Coras623eb562019-02-03 19:28:34 -0800549 return segment_manager_get (*smp);
550}
551
552session_t *
Florin Corasc9940fc2019-02-05 20:55:11 -0800553app_worker_first_listener (app_worker_t * app_wrk, u8 fib_proto,
Florin Coras623eb562019-02-03 19:28:34 -0800554 u8 transport_proto)
555{
556 session_t *listener;
557 u64 handle;
558 u32 sm_index;
559 u8 sst;
560
561 sst = session_type_from_proto_and_ip (transport_proto,
562 fib_proto == FIB_PROTOCOL_IP4);
563
564 /* *INDENT-OFF* */
Florin Corasc9940fc2019-02-05 20:55:11 -0800565 hash_foreach (handle, sm_index, app_wrk->listeners_table, ({
Florin Coras623eb562019-02-03 19:28:34 -0800566 listener = listen_session_get_from_handle (handle);
567 if (listener->session_type == sst
Florin Corasd5c604d2019-03-18 09:06:35 -0700568 && !(listener->flags & SESSION_F_PROXY))
Florin Coras623eb562019-02-03 19:28:34 -0800569 return listener;
570 }));
571 /* *INDENT-ON* */
572
573 return 0;
574}
575
576session_t *
Florin Corasc9940fc2019-02-05 20:55:11 -0800577app_worker_proxy_listener (app_worker_t * app_wrk, u8 fib_proto,
Florin Coras623eb562019-02-03 19:28:34 -0800578 u8 transport_proto)
579{
580 session_t *listener;
581 u64 handle;
582 u32 sm_index;
583 u8 sst;
584
585 sst = session_type_from_proto_and_ip (transport_proto,
586 fib_proto == FIB_PROTOCOL_IP4);
587
588 /* *INDENT-OFF* */
Florin Corasc9940fc2019-02-05 20:55:11 -0800589 hash_foreach (handle, sm_index, app_wrk->listeners_table, ({
Florin Coras623eb562019-02-03 19:28:34 -0800590 listener = listen_session_get_from_handle (handle);
Florin Corasd5c604d2019-03-18 09:06:35 -0700591 if (listener->session_type == sst && (listener->flags & SESSION_F_PROXY))
Florin Coras623eb562019-02-03 19:28:34 -0800592 return listener;
593 }));
594 /* *INDENT-ON* */
595
596 return 0;
597}
598
599/**
600 * Send an API message to the external app, to map new segment
601 */
602int
Florin Coras2b81e3c2019-02-27 07:55:46 -0800603app_worker_add_segment_notify (app_worker_t * app_wrk, u64 segment_handle)
Florin Coras623eb562019-02-03 19:28:34 -0800604{
Florin Coras623eb562019-02-03 19:28:34 -0800605 application_t *app = application_get (app_wrk->app_index);
Florin Corasc4c4cf52019-08-24 18:17:34 -0700606
607 return app->cb_fns.add_segment_callback (app_wrk->wrk_index,
Florin Coras623eb562019-02-03 19:28:34 -0800608 segment_handle);
609}
610
Florin Coras2b81e3c2019-02-27 07:55:46 -0800611int
612app_worker_del_segment_notify (app_worker_t * app_wrk, u64 segment_handle)
613{
614 application_t *app = application_get (app_wrk->app_index);
Florin Corasc4c4cf52019-08-24 18:17:34 -0700615 return app->cb_fns.del_segment_callback (app_wrk->wrk_index,
Florin Coras2b81e3c2019-02-27 07:55:46 -0800616 segment_handle);
617}
618
Florin Coras31c99552019-03-01 13:00:58 -0800619static inline u8
Florin Coras623eb562019-02-03 19:28:34 -0800620app_worker_application_is_builtin (app_worker_t * app_wrk)
621{
622 return app_wrk->app_is_builtin;
623}
624
Florin Coras20c24232021-11-22 21:19:01 -0800625static int
626app_wrk_send_fd (app_worker_t *app_wrk, int fd)
627{
628 if (!appns_sapi_enabled ())
629 {
630 vl_api_registration_t *reg;
631 clib_error_t *error;
632
633 reg =
634 vl_mem_api_client_index_to_registration (app_wrk->api_client_index);
635 if (!reg)
636 {
637 clib_warning ("no api registration for client: %u",
638 app_wrk->api_client_index);
639 return -1;
640 }
641
642 if (vl_api_registration_file_index (reg) == VL_API_INVALID_FI)
643 return -1;
644
645 error = vl_api_send_fd_msg (reg, &fd, 1);
646 if (error)
647 {
648 clib_error_report (error);
649 return -1;
650 }
651
652 return 0;
653 }
654
655 app_sapi_msg_t smsg = { 0 };
656 app_namespace_t *app_ns;
657 clib_error_t *error;
658 application_t *app;
659 clib_socket_t *cs;
660 u32 cs_index;
661
662 app = application_get (app_wrk->app_index);
663 app_ns = app_namespace_get (app->ns_index);
664 cs_index = appns_sapi_handle_sock_index (app_wrk->api_client_index);
665 cs = appns_sapi_get_socket (app_ns, cs_index);
666 if (PREDICT_FALSE (!cs))
667 return -1;
668
669 /* There's no payload for the message only the type */
670 smsg.type = APP_SAPI_MSG_TYPE_SEND_FDS;
671 error = clib_socket_sendmsg (cs, &smsg, sizeof (smsg), &fd, 1);
672 if (error)
673 {
674 clib_error_report (error);
675 return -1;
676 }
677
678 return 0;
679}
680
681static int
682mq_try_lock_and_alloc_msg (svm_msg_q_t *mq, session_mq_rings_e ring,
683 svm_msg_q_msg_t *msg)
684{
685 int rv, n_try = 0;
686
Radha krishna Saragadamadd76372022-07-18 19:23:06 +0530687 while (n_try < 75)
Florin Coras20c24232021-11-22 21:19:01 -0800688 {
689 rv = svm_msg_q_lock_and_alloc_msg_w_ring (mq, ring, SVM_Q_NOWAIT, msg);
690 if (!rv)
691 return 0;
692 /*
693 * Break the loop if mq is full, usually this is because the
694 * app has crashed or is hanging on somewhere.
695 */
696 if (rv != -1)
697 break;
698 n_try += 1;
699 usleep (1);
700 }
701
702 return -1;
703}
704
705typedef union app_wrk_mq_rpc_args_
706{
707 struct
708 {
709 u32 thread_index;
710 u32 app_wrk_index;
711 };
712 uword as_uword;
713} app_wrk_mq_rpc_ags_t;
714
715static int
716app_wrk_handle_mq_postponed_msgs (void *arg)
717{
718 svm_msg_q_msg_t _mq_msg, *mq_msg = &_mq_msg;
719 app_wrk_postponed_msg_t *pm;
720 app_wrk_mq_rpc_ags_t args;
721 u32 max_msg, n_msg = 0;
722 app_worker_t *app_wrk;
723 session_event_t *evt;
724 svm_msg_q_t *mq;
725
726 args.as_uword = pointer_to_uword (arg);
727 app_wrk = app_worker_get_if_valid (args.app_wrk_index);
728 if (!app_wrk)
729 return 0;
730
731 mq = app_wrk->event_queue;
732
733 clib_spinlock_lock (&app_wrk->postponed_mq_msgs_lock);
734
735 max_msg = clib_min (32, clib_fifo_elts (app_wrk->postponed_mq_msgs));
736
737 while (n_msg < max_msg)
738 {
739 pm = clib_fifo_head (app_wrk->postponed_mq_msgs);
740 if (mq_try_lock_and_alloc_msg (mq, pm->ring, mq_msg))
741 break;
742
743 evt = svm_msg_q_msg_data (mq, mq_msg);
744 clib_memset (evt, 0, sizeof (*evt));
745 evt->event_type = pm->event_type;
746 clib_memcpy_fast (evt->data, pm->data, pm->len);
747
748 if (pm->fd != -1)
749 app_wrk_send_fd (app_wrk, pm->fd);
750
751 svm_msg_q_add_and_unlock (mq, mq_msg);
752
753 clib_fifo_advance_head (app_wrk->postponed_mq_msgs, 1);
754 n_msg += 1;
755 }
756
757 if (!clib_fifo_elts (app_wrk->postponed_mq_msgs))
758 {
759 app_wrk->mq_congested = 0;
760 }
761 else
762 {
763 session_send_rpc_evt_to_thread_force (
764 args.thread_index, app_wrk_handle_mq_postponed_msgs,
765 uword_to_pointer (args.as_uword, void *));
766 }
767
768 clib_spinlock_unlock (&app_wrk->postponed_mq_msgs_lock);
769
770 return 0;
771}
772
773static void
774app_wrk_add_mq_postponed_msg (app_worker_t *app_wrk, session_mq_rings_e ring,
775 u8 evt_type, void *msg, u32 msg_len, int fd)
776{
777 app_wrk_postponed_msg_t *pm;
778
779 clib_spinlock_lock (&app_wrk->postponed_mq_msgs_lock);
780
781 app_wrk->mq_congested = 1;
782
783 clib_fifo_add2 (app_wrk->postponed_mq_msgs, pm);
784 clib_memcpy_fast (pm->data, msg, msg_len);
785 pm->event_type = evt_type;
786 pm->ring = ring;
787 pm->len = msg_len;
788 pm->fd = fd;
789
790 if (clib_fifo_elts (app_wrk->postponed_mq_msgs) == 1)
791 {
792 app_wrk_mq_rpc_ags_t args = { .thread_index = vlib_get_thread_index (),
793 .app_wrk_index = app_wrk->wrk_index };
794
795 session_send_rpc_evt_to_thread_force (
796 args.thread_index, app_wrk_handle_mq_postponed_msgs,
797 uword_to_pointer (args.as_uword, void *));
798 }
799
800 clib_spinlock_unlock (&app_wrk->postponed_mq_msgs_lock);
801}
802
803always_inline void
804app_wrk_send_ctrl_evt_inline (app_worker_t *app_wrk, u8 evt_type, void *msg,
805 u32 msg_len, int fd)
806{
807 svm_msg_q_msg_t _mq_msg, *mq_msg = &_mq_msg;
808 svm_msg_q_t *mq = app_wrk->event_queue;
809 session_event_t *evt;
810 int rv;
811
812 if (PREDICT_FALSE (app_wrk->mq_congested))
813 goto handle_congestion;
814
815 rv = mq_try_lock_and_alloc_msg (mq, SESSION_MQ_CTRL_EVT_RING, mq_msg);
816 if (PREDICT_FALSE (rv))
817 goto handle_congestion;
818
819 evt = svm_msg_q_msg_data (mq, mq_msg);
820 clib_memset (evt, 0, sizeof (*evt));
821 evt->event_type = evt_type;
822 clib_memcpy_fast (evt->data, msg, msg_len);
823
824 if (fd != -1)
825 app_wrk_send_fd (app_wrk, fd);
826
827 svm_msg_q_add_and_unlock (mq, mq_msg);
828
829 return;
830
831handle_congestion:
832
833 app_wrk_add_mq_postponed_msg (app_wrk, SESSION_MQ_CTRL_EVT_RING, evt_type,
834 msg, msg_len, fd);
835}
836
837void
838app_wrk_send_ctrl_evt_fd (app_worker_t *app_wrk, u8 evt_type, void *msg,
839 u32 msg_len, int fd)
840{
841 app_wrk_send_ctrl_evt_inline (app_wrk, evt_type, msg, msg_len, fd);
842}
843
844void
845app_wrk_send_ctrl_evt (app_worker_t *app_wrk, u8 evt_type, void *msg,
846 u32 msg_len)
847{
848 app_wrk_send_ctrl_evt_inline (app_wrk, evt_type, msg, msg_len, -1);
849}
850
Florin Coras623eb562019-02-03 19:28:34 -0800851static inline int
Florin Coras2b5fed82019-07-25 14:51:09 -0700852app_send_io_evt_rx (app_worker_t * app_wrk, session_t * s)
Florin Coras623eb562019-02-03 19:28:34 -0800853{
Florin Coras20c24232021-11-22 21:19:01 -0800854 svm_msg_q_msg_t _mq_msg = { 0 }, *mq_msg = &_mq_msg;
Florin Coras623eb562019-02-03 19:28:34 -0800855 session_event_t *evt;
Florin Coras623eb562019-02-03 19:28:34 -0800856 svm_msg_q_t *mq;
Florin Coras20c24232021-11-22 21:19:01 -0800857 u32 app_session;
858 int rv;
Florin Coras623eb562019-02-03 19:28:34 -0800859
Florin Coras5c290292019-09-19 08:19:44 -0700860 if (app_worker_application_is_builtin (app_wrk))
861 return app_worker_builtin_rx (app_wrk, s);
862
Florin Coras653e43f2019-03-04 10:56:23 -0800863 if (svm_fifo_has_event (s->rx_fifo))
Florin Coras623eb562019-02-03 19:28:34 -0800864 return 0;
865
Florin Coras20c24232021-11-22 21:19:01 -0800866 app_session = s->rx_fifo->shr->client_session_index;
Florin Coras623eb562019-02-03 19:28:34 -0800867 mq = app_wrk->event_queue;
Florin Coras2b5fed82019-07-25 14:51:09 -0700868
Florin Coras20c24232021-11-22 21:19:01 -0800869 if (PREDICT_FALSE (app_wrk->mq_congested))
870 goto handle_congestion;
Florin Coras623eb562019-02-03 19:28:34 -0800871
Florin Coras20c24232021-11-22 21:19:01 -0800872 rv = mq_try_lock_and_alloc_msg (mq, SESSION_MQ_IO_EVT_RING, mq_msg);
Florin Coras623eb562019-02-03 19:28:34 -0800873
Florin Coras20c24232021-11-22 21:19:01 -0800874 if (PREDICT_FALSE (rv))
875 goto handle_congestion;
876
877 evt = svm_msg_q_msg_data (mq, mq_msg);
Florin Corasf6c43132019-03-01 12:41:21 -0800878 evt->event_type = SESSION_IO_EVT_RX;
Florin Coras20c24232021-11-22 21:19:01 -0800879 evt->session_index = app_session;
Florin Coras623eb562019-02-03 19:28:34 -0800880
881 (void) svm_fifo_set_event (s->rx_fifo);
Florin Coras20c24232021-11-22 21:19:01 -0800882
883 svm_msg_q_add_and_unlock (mq, mq_msg);
Florin Coras623eb562019-02-03 19:28:34 -0800884
Florin Coras623eb562019-02-03 19:28:34 -0800885 return 0;
Florin Coras20c24232021-11-22 21:19:01 -0800886
887handle_congestion:
888
889 app_wrk_add_mq_postponed_msg (app_wrk, SESSION_MQ_IO_EVT_RING,
890 SESSION_IO_EVT_RX, &app_session,
891 sizeof (app_session), -1);
892 return -1;
Florin Coras623eb562019-02-03 19:28:34 -0800893}
894
895static inline int
Florin Coras2b5fed82019-07-25 14:51:09 -0700896app_send_io_evt_tx (app_worker_t * app_wrk, session_t * s)
Florin Coras623eb562019-02-03 19:28:34 -0800897{
Florin Coras20c24232021-11-22 21:19:01 -0800898 svm_msg_q_msg_t _mq_msg = { 0 }, *mq_msg = &_mq_msg;
Florin Coras623eb562019-02-03 19:28:34 -0800899 session_event_t *evt;
Florin Coras20c24232021-11-22 21:19:01 -0800900 svm_msg_q_t *mq;
901 u32 app_session;
902 int rv;
Florin Coras623eb562019-02-03 19:28:34 -0800903
904 if (app_worker_application_is_builtin (app_wrk))
Florin Coras0e573f52019-05-07 16:28:16 -0700905 return app_worker_builtin_tx (app_wrk, s);
Florin Coras623eb562019-02-03 19:28:34 -0800906
Florin Coras20c24232021-11-22 21:19:01 -0800907 app_session = s->tx_fifo->shr->client_session_index;
Florin Coras623eb562019-02-03 19:28:34 -0800908 mq = app_wrk->event_queue;
Florin Coras2b5fed82019-07-25 14:51:09 -0700909
Florin Coras20c24232021-11-22 21:19:01 -0800910 if (PREDICT_FALSE (app_wrk->mq_congested))
911 goto handle_congestion;
Florin Coras623eb562019-02-03 19:28:34 -0800912
Florin Coras20c24232021-11-22 21:19:01 -0800913 rv = mq_try_lock_and_alloc_msg (mq, SESSION_MQ_IO_EVT_RING, mq_msg);
Florin Coras623eb562019-02-03 19:28:34 -0800914
Florin Coras20c24232021-11-22 21:19:01 -0800915 if (PREDICT_FALSE (rv))
916 goto handle_congestion;
917
918 evt = svm_msg_q_msg_data (mq, mq_msg);
Florin Corasf6c43132019-03-01 12:41:21 -0800919 evt->event_type = SESSION_IO_EVT_TX;
Florin Coras20c24232021-11-22 21:19:01 -0800920 evt->session_index = app_session;
Florin Coras623eb562019-02-03 19:28:34 -0800921
Florin Coras20c24232021-11-22 21:19:01 -0800922 svm_msg_q_add_and_unlock (mq, mq_msg);
923
Florin Coras2b5fed82019-07-25 14:51:09 -0700924 return 0;
Florin Coras20c24232021-11-22 21:19:01 -0800925
926handle_congestion:
927
928 app_wrk_add_mq_postponed_msg (app_wrk, SESSION_MQ_IO_EVT_RING,
929 SESSION_IO_EVT_TX, &app_session,
930 sizeof (app_session), -1);
931 return -1;
Florin Coras623eb562019-02-03 19:28:34 -0800932}
933
934/* *INDENT-OFF* */
935typedef int (app_send_evt_handler_fn) (app_worker_t *app,
Florin Coras2b5fed82019-07-25 14:51:09 -0700936 session_t *s);
Florin Coras653e43f2019-03-04 10:56:23 -0800937static app_send_evt_handler_fn * const app_send_evt_handler_fns[2] = {
Florin Coras623eb562019-02-03 19:28:34 -0800938 app_send_io_evt_rx,
Florin Coras623eb562019-02-03 19:28:34 -0800939 app_send_io_evt_tx,
940};
941/* *INDENT-ON* */
942
943/**
944 * Send event to application
945 *
Florin Coras623eb562019-02-03 19:28:34 -0800946 * Logic from queue perspective is blocking. However, if queue is full,
947 * we return.
948 */
949int
950app_worker_lock_and_send_event (app_worker_t * app, session_t * s,
951 u8 evt_type)
952{
Florin Coras2b5fed82019-07-25 14:51:09 -0700953 return app_send_evt_handler_fns[evt_type] (app, s);
Florin Coras623eb562019-02-03 19:28:34 -0800954}
955
Florin Coras623eb562019-02-03 19:28:34 -0800956u8 *
957format_app_worker_listener (u8 * s, va_list * args)
958{
959 app_worker_t *app_wrk = va_arg (*args, app_worker_t *);
960 u64 handle = va_arg (*args, u64);
961 u32 sm_index = va_arg (*args, u32);
962 int verbose = va_arg (*args, int);
963 session_t *listener;
964 const u8 *app_name;
965 u8 *str;
966
967 if (!app_wrk)
968 {
969 if (verbose)
Xiaoming Jiang806709f2021-06-23 09:07:57 +0000970 s = format (s, "%-" SESSION_CLI_ID_LEN "s%-25s%-10s%-15s%-15s%-10s",
971 "Connection", "App", "Wrk", "API Client", "ListenerID",
972 "SegManager");
Florin Coras623eb562019-02-03 19:28:34 -0800973 else
Xiaoming Jiang806709f2021-06-23 09:07:57 +0000974 s = format (s, "%-" SESSION_CLI_ID_LEN "s%-25s%-10s", "Connection",
975 "App", "Wrk");
Florin Coras623eb562019-02-03 19:28:34 -0800976
977 return s;
978 }
979
980 app_name = application_name_from_index (app_wrk->app_index);
981 listener = listen_session_get_from_handle (handle);
Florin Coras31c99552019-03-01 13:00:58 -0800982 str = format (0, "%U", format_session, listener, verbose);
Florin Coras623eb562019-02-03 19:28:34 -0800983
984 if (verbose)
985 {
Dave Barach3e07a4a2020-04-04 10:05:48 -0400986 u8 *buf;
987 buf = format (0, "%u(%u)", app_wrk->wrk_map_index, app_wrk->wrk_index);
Xiaoming Jiang806709f2021-06-23 09:07:57 +0000988 s = format (s, "%-" SESSION_CLI_ID_LEN "v%-25v%-10v%-15u%-15u%-10u", str,
989 app_name, buf, app_wrk->api_client_index, handle, sm_index);
Dave Barach3e07a4a2020-04-04 10:05:48 -0400990 vec_free (buf);
Florin Coras623eb562019-02-03 19:28:34 -0800991 }
992 else
Xiaoming Jiang806709f2021-06-23 09:07:57 +0000993 s = format (s, "%-" SESSION_CLI_ID_LEN "v%-25v%=10u", str, app_name,
994 app_wrk->wrk_map_index);
jiangxiaoming6b410e62020-10-10 15:23:54 +0800995
996 vec_free (str);
Florin Coras623eb562019-02-03 19:28:34 -0800997
998 return s;
999}
1000
1001u8 *
1002format_app_worker (u8 * s, va_list * args)
1003{
1004 app_worker_t *app_wrk = va_arg (*args, app_worker_t *);
1005 u32 indent = 1;
1006
Florin Coras20c24232021-11-22 21:19:01 -08001007 s = format (s,
1008 "%U wrk-index %u app-index %u map-index %u "
1009 "api-client-index %d mq-cong %u\n",
1010 format_white_space, indent, app_wrk->wrk_index,
1011 app_wrk->app_index, app_wrk->wrk_map_index,
1012 app_wrk->api_client_index, app_wrk->mq_congested);
Florin Coras623eb562019-02-03 19:28:34 -08001013 return s;
1014}
1015
1016void
1017app_worker_format_connects (app_worker_t * app_wrk, int verbose)
1018{
Florin Coras623eb562019-02-03 19:28:34 -08001019 segment_manager_t *sm;
Florin Coras623eb562019-02-03 19:28:34 -08001020
1021 /* Header */
1022 if (!app_wrk)
1023 {
Florin Coras88001c62019-04-24 14:44:46 -07001024 segment_manager_format_sessions (0, verbose);
Florin Coras623eb562019-02-03 19:28:34 -08001025 return;
1026 }
1027
1028 if (app_wrk->connects_seg_manager == (u32) ~ 0)
1029 return;
1030
Florin Coras623eb562019-02-03 19:28:34 -08001031 sm = segment_manager_get (app_wrk->connects_seg_manager);
Florin Coras88001c62019-04-24 14:44:46 -07001032 segment_manager_format_sessions (sm, verbose);
Florin Coras623eb562019-02-03 19:28:34 -08001033}
1034
Florin Coras623eb562019-02-03 19:28:34 -08001035/*
1036 * fd.io coding-style-patch-verification: ON
1037 *
1038 * Local Variables:
1039 * eval: (c-set-style "gnu")
1040 * End:
1041 */