blob: 844e78f7fa965c12ac9aecca7d9eaf92b6620a5d [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 Corasd50ff7f2020-04-16 04:30:22 +0000396 ASSERT (vlib_get_thread_index () == 0);
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 Corasbab6c522021-05-13 08:36:56 -0700407 ASSERT (vlib_get_thread_index () <= 1);
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;
483
484 if (s->session_state == SESSION_STATE_LISTENING)
485 return application_change_listener_owner (s, app_wrk);
486
487 s->app_wrk_index = app_wrk->wrk_index;
488
489 rxf = s->rx_fifo;
490 txf = s->tx_fifo;
491
492 if (!rxf || !txf)
493 return 0;
494
495 s->rx_fifo = 0;
496 s->tx_fifo = 0;
497
Florin Coras94a6df02021-05-06 15:32:14 -0700498 sm = app_worker_get_connect_segment_manager (app_wrk);
Florin Corasa27a46e2019-02-18 13:02:28 -0800499 if (app_worker_alloc_session_fifos (sm, s))
Florin Coras623eb562019-02-03 19:28:34 -0800500 return -1;
501
Sirshak Das28aa5392019-02-05 01:33:33 -0600502 if (!svm_fifo_is_empty_cons (rxf))
503 svm_fifo_clone (s->rx_fifo, rxf);
Florin Coras623eb562019-02-03 19:28:34 -0800504
Sirshak Das28aa5392019-02-05 01:33:33 -0600505 if (!svm_fifo_is_empty_cons (txf))
506 svm_fifo_clone (s->tx_fifo, txf);
507
Florin Coras19223e02019-03-03 14:56:05 -0800508 segment_manager_dealloc_fifos (rxf, txf);
Florin Coras623eb562019-02-03 19:28:34 -0800509
510 return 0;
511}
512
513int
Florin Coras89a9f612021-05-11 11:55:07 -0700514app_worker_connect_session (app_worker_t *app_wrk, session_endpoint_cfg_t *sep,
515 session_handle_t *rsh)
Florin Coras623eb562019-02-03 19:28:34 -0800516{
Florin Coras20c24232021-11-22 21:19:01 -0800517 if (PREDICT_FALSE (app_wrk->mq_congested))
518 return SESSION_E_REFUSED;
519
Florin Coras89a9f612021-05-11 11:55:07 -0700520 sep->app_wrk_index = app_wrk->wrk_index;
Florin Coras623eb562019-02-03 19:28:34 -0800521
Florin Coras89a9f612021-05-11 11:55:07 -0700522 return session_open (sep, rsh);
Florin Coras623eb562019-02-03 19:28:34 -0800523}
524
525int
Ryujiro Shibuyad8f48e22020-01-22 12:11:42 +0000526app_worker_session_fifo_tuning (app_worker_t * app_wrk, session_t * s,
527 svm_fifo_t * f,
528 session_ft_action_t act, u32 len)
529{
530 application_t *app = application_get (app_wrk->app_index);
531 return app->cb_fns.fifo_tuning_callback (s, f, act, len);
532}
533
Florin Coras623eb562019-02-03 19:28:34 -0800534segment_manager_t *
535app_worker_get_connect_segment_manager (app_worker_t * app)
536{
537 ASSERT (app->connects_seg_manager != (u32) ~ 0);
538 return segment_manager_get (app->connects_seg_manager);
539}
540
541segment_manager_t *
Florin Coras623eb562019-02-03 19:28:34 -0800542app_worker_get_listen_segment_manager (app_worker_t * app,
543 session_t * listener)
544{
545 uword *smp;
546 smp = hash_get (app->listeners_table, listen_session_get_handle (listener));
Dave Barach47d41ad2020-02-17 09:13:26 -0500547 ALWAYS_ASSERT (smp != 0);
Florin Coras623eb562019-02-03 19:28:34 -0800548 return segment_manager_get (*smp);
549}
550
551session_t *
Florin Corasc9940fc2019-02-05 20:55:11 -0800552app_worker_first_listener (app_worker_t * app_wrk, u8 fib_proto,
Florin Coras623eb562019-02-03 19:28:34 -0800553 u8 transport_proto)
554{
555 session_t *listener;
556 u64 handle;
557 u32 sm_index;
558 u8 sst;
559
560 sst = session_type_from_proto_and_ip (transport_proto,
561 fib_proto == FIB_PROTOCOL_IP4);
562
563 /* *INDENT-OFF* */
Florin Corasc9940fc2019-02-05 20:55:11 -0800564 hash_foreach (handle, sm_index, app_wrk->listeners_table, ({
Florin Coras623eb562019-02-03 19:28:34 -0800565 listener = listen_session_get_from_handle (handle);
566 if (listener->session_type == sst
Florin Corasd5c604d2019-03-18 09:06:35 -0700567 && !(listener->flags & SESSION_F_PROXY))
Florin Coras623eb562019-02-03 19:28:34 -0800568 return listener;
569 }));
570 /* *INDENT-ON* */
571
572 return 0;
573}
574
575session_t *
Florin Corasc9940fc2019-02-05 20:55:11 -0800576app_worker_proxy_listener (app_worker_t * app_wrk, u8 fib_proto,
Florin Coras623eb562019-02-03 19:28:34 -0800577 u8 transport_proto)
578{
579 session_t *listener;
580 u64 handle;
581 u32 sm_index;
582 u8 sst;
583
584 sst = session_type_from_proto_and_ip (transport_proto,
585 fib_proto == FIB_PROTOCOL_IP4);
586
587 /* *INDENT-OFF* */
Florin Corasc9940fc2019-02-05 20:55:11 -0800588 hash_foreach (handle, sm_index, app_wrk->listeners_table, ({
Florin Coras623eb562019-02-03 19:28:34 -0800589 listener = listen_session_get_from_handle (handle);
Florin Corasd5c604d2019-03-18 09:06:35 -0700590 if (listener->session_type == sst && (listener->flags & SESSION_F_PROXY))
Florin Coras623eb562019-02-03 19:28:34 -0800591 return listener;
592 }));
593 /* *INDENT-ON* */
594
595 return 0;
596}
597
598/**
599 * Send an API message to the external app, to map new segment
600 */
601int
Florin Coras2b81e3c2019-02-27 07:55:46 -0800602app_worker_add_segment_notify (app_worker_t * app_wrk, u64 segment_handle)
Florin Coras623eb562019-02-03 19:28:34 -0800603{
Florin Coras623eb562019-02-03 19:28:34 -0800604 application_t *app = application_get (app_wrk->app_index);
Florin Corasc4c4cf52019-08-24 18:17:34 -0700605
606 return app->cb_fns.add_segment_callback (app_wrk->wrk_index,
Florin Coras623eb562019-02-03 19:28:34 -0800607 segment_handle);
608}
609
Florin Coras2b81e3c2019-02-27 07:55:46 -0800610int
611app_worker_del_segment_notify (app_worker_t * app_wrk, u64 segment_handle)
612{
613 application_t *app = application_get (app_wrk->app_index);
Florin Corasc4c4cf52019-08-24 18:17:34 -0700614 return app->cb_fns.del_segment_callback (app_wrk->wrk_index,
Florin Coras2b81e3c2019-02-27 07:55:46 -0800615 segment_handle);
616}
617
Florin Coras31c99552019-03-01 13:00:58 -0800618static inline u8
Florin Coras623eb562019-02-03 19:28:34 -0800619app_worker_application_is_builtin (app_worker_t * app_wrk)
620{
621 return app_wrk->app_is_builtin;
622}
623
Florin Coras20c24232021-11-22 21:19:01 -0800624static int
625app_wrk_send_fd (app_worker_t *app_wrk, int fd)
626{
627 if (!appns_sapi_enabled ())
628 {
629 vl_api_registration_t *reg;
630 clib_error_t *error;
631
632 reg =
633 vl_mem_api_client_index_to_registration (app_wrk->api_client_index);
634 if (!reg)
635 {
636 clib_warning ("no api registration for client: %u",
637 app_wrk->api_client_index);
638 return -1;
639 }
640
641 if (vl_api_registration_file_index (reg) == VL_API_INVALID_FI)
642 return -1;
643
644 error = vl_api_send_fd_msg (reg, &fd, 1);
645 if (error)
646 {
647 clib_error_report (error);
648 return -1;
649 }
650
651 return 0;
652 }
653
654 app_sapi_msg_t smsg = { 0 };
655 app_namespace_t *app_ns;
656 clib_error_t *error;
657 application_t *app;
658 clib_socket_t *cs;
659 u32 cs_index;
660
661 app = application_get (app_wrk->app_index);
662 app_ns = app_namespace_get (app->ns_index);
663 cs_index = appns_sapi_handle_sock_index (app_wrk->api_client_index);
664 cs = appns_sapi_get_socket (app_ns, cs_index);
665 if (PREDICT_FALSE (!cs))
666 return -1;
667
668 /* There's no payload for the message only the type */
669 smsg.type = APP_SAPI_MSG_TYPE_SEND_FDS;
670 error = clib_socket_sendmsg (cs, &smsg, sizeof (smsg), &fd, 1);
671 if (error)
672 {
673 clib_error_report (error);
674 return -1;
675 }
676
677 return 0;
678}
679
680static int
681mq_try_lock_and_alloc_msg (svm_msg_q_t *mq, session_mq_rings_e ring,
682 svm_msg_q_msg_t *msg)
683{
684 int rv, n_try = 0;
685
Radha krishna Saragadamadd76372022-07-18 19:23:06 +0530686 while (n_try < 75)
Florin Coras20c24232021-11-22 21:19:01 -0800687 {
688 rv = svm_msg_q_lock_and_alloc_msg_w_ring (mq, ring, SVM_Q_NOWAIT, msg);
689 if (!rv)
690 return 0;
691 /*
692 * Break the loop if mq is full, usually this is because the
693 * app has crashed or is hanging on somewhere.
694 */
695 if (rv != -1)
696 break;
697 n_try += 1;
698 usleep (1);
699 }
700
701 return -1;
702}
703
704typedef union app_wrk_mq_rpc_args_
705{
706 struct
707 {
708 u32 thread_index;
709 u32 app_wrk_index;
710 };
711 uword as_uword;
712} app_wrk_mq_rpc_ags_t;
713
714static int
715app_wrk_handle_mq_postponed_msgs (void *arg)
716{
717 svm_msg_q_msg_t _mq_msg, *mq_msg = &_mq_msg;
718 app_wrk_postponed_msg_t *pm;
719 app_wrk_mq_rpc_ags_t args;
720 u32 max_msg, n_msg = 0;
721 app_worker_t *app_wrk;
722 session_event_t *evt;
723 svm_msg_q_t *mq;
724
725 args.as_uword = pointer_to_uword (arg);
726 app_wrk = app_worker_get_if_valid (args.app_wrk_index);
727 if (!app_wrk)
728 return 0;
729
730 mq = app_wrk->event_queue;
731
732 clib_spinlock_lock (&app_wrk->postponed_mq_msgs_lock);
733
734 max_msg = clib_min (32, clib_fifo_elts (app_wrk->postponed_mq_msgs));
735
736 while (n_msg < max_msg)
737 {
738 pm = clib_fifo_head (app_wrk->postponed_mq_msgs);
739 if (mq_try_lock_and_alloc_msg (mq, pm->ring, mq_msg))
740 break;
741
742 evt = svm_msg_q_msg_data (mq, mq_msg);
743 clib_memset (evt, 0, sizeof (*evt));
744 evt->event_type = pm->event_type;
745 clib_memcpy_fast (evt->data, pm->data, pm->len);
746
747 if (pm->fd != -1)
748 app_wrk_send_fd (app_wrk, pm->fd);
749
750 svm_msg_q_add_and_unlock (mq, mq_msg);
751
752 clib_fifo_advance_head (app_wrk->postponed_mq_msgs, 1);
753 n_msg += 1;
754 }
755
756 if (!clib_fifo_elts (app_wrk->postponed_mq_msgs))
757 {
758 app_wrk->mq_congested = 0;
759 }
760 else
761 {
762 session_send_rpc_evt_to_thread_force (
763 args.thread_index, app_wrk_handle_mq_postponed_msgs,
764 uword_to_pointer (args.as_uword, void *));
765 }
766
767 clib_spinlock_unlock (&app_wrk->postponed_mq_msgs_lock);
768
769 return 0;
770}
771
772static void
773app_wrk_add_mq_postponed_msg (app_worker_t *app_wrk, session_mq_rings_e ring,
774 u8 evt_type, void *msg, u32 msg_len, int fd)
775{
776 app_wrk_postponed_msg_t *pm;
777
778 clib_spinlock_lock (&app_wrk->postponed_mq_msgs_lock);
779
780 app_wrk->mq_congested = 1;
781
782 clib_fifo_add2 (app_wrk->postponed_mq_msgs, pm);
783 clib_memcpy_fast (pm->data, msg, msg_len);
784 pm->event_type = evt_type;
785 pm->ring = ring;
786 pm->len = msg_len;
787 pm->fd = fd;
788
789 if (clib_fifo_elts (app_wrk->postponed_mq_msgs) == 1)
790 {
791 app_wrk_mq_rpc_ags_t args = { .thread_index = vlib_get_thread_index (),
792 .app_wrk_index = app_wrk->wrk_index };
793
794 session_send_rpc_evt_to_thread_force (
795 args.thread_index, app_wrk_handle_mq_postponed_msgs,
796 uword_to_pointer (args.as_uword, void *));
797 }
798
799 clib_spinlock_unlock (&app_wrk->postponed_mq_msgs_lock);
800}
801
802always_inline void
803app_wrk_send_ctrl_evt_inline (app_worker_t *app_wrk, u8 evt_type, void *msg,
804 u32 msg_len, int fd)
805{
806 svm_msg_q_msg_t _mq_msg, *mq_msg = &_mq_msg;
807 svm_msg_q_t *mq = app_wrk->event_queue;
808 session_event_t *evt;
809 int rv;
810
811 if (PREDICT_FALSE (app_wrk->mq_congested))
812 goto handle_congestion;
813
814 rv = mq_try_lock_and_alloc_msg (mq, SESSION_MQ_CTRL_EVT_RING, mq_msg);
815 if (PREDICT_FALSE (rv))
816 goto handle_congestion;
817
818 evt = svm_msg_q_msg_data (mq, mq_msg);
819 clib_memset (evt, 0, sizeof (*evt));
820 evt->event_type = evt_type;
821 clib_memcpy_fast (evt->data, msg, msg_len);
822
823 if (fd != -1)
824 app_wrk_send_fd (app_wrk, fd);
825
826 svm_msg_q_add_and_unlock (mq, mq_msg);
827
828 return;
829
830handle_congestion:
831
832 app_wrk_add_mq_postponed_msg (app_wrk, SESSION_MQ_CTRL_EVT_RING, evt_type,
833 msg, msg_len, fd);
834}
835
836void
837app_wrk_send_ctrl_evt_fd (app_worker_t *app_wrk, u8 evt_type, void *msg,
838 u32 msg_len, int fd)
839{
840 app_wrk_send_ctrl_evt_inline (app_wrk, evt_type, msg, msg_len, fd);
841}
842
843void
844app_wrk_send_ctrl_evt (app_worker_t *app_wrk, u8 evt_type, void *msg,
845 u32 msg_len)
846{
847 app_wrk_send_ctrl_evt_inline (app_wrk, evt_type, msg, msg_len, -1);
848}
849
Florin Coras623eb562019-02-03 19:28:34 -0800850static inline int
Florin Coras2b5fed82019-07-25 14:51:09 -0700851app_send_io_evt_rx (app_worker_t * app_wrk, session_t * s)
Florin Coras623eb562019-02-03 19:28:34 -0800852{
Florin Coras20c24232021-11-22 21:19:01 -0800853 svm_msg_q_msg_t _mq_msg = { 0 }, *mq_msg = &_mq_msg;
Florin Coras623eb562019-02-03 19:28:34 -0800854 session_event_t *evt;
Florin Coras623eb562019-02-03 19:28:34 -0800855 svm_msg_q_t *mq;
Florin Coras20c24232021-11-22 21:19:01 -0800856 u32 app_session;
857 int rv;
Florin Coras623eb562019-02-03 19:28:34 -0800858
Florin Coras5c290292019-09-19 08:19:44 -0700859 if (app_worker_application_is_builtin (app_wrk))
860 return app_worker_builtin_rx (app_wrk, s);
861
Florin Coras653e43f2019-03-04 10:56:23 -0800862 if (svm_fifo_has_event (s->rx_fifo))
Florin Coras623eb562019-02-03 19:28:34 -0800863 return 0;
864
Florin Coras20c24232021-11-22 21:19:01 -0800865 app_session = s->rx_fifo->shr->client_session_index;
Florin Coras623eb562019-02-03 19:28:34 -0800866 mq = app_wrk->event_queue;
Florin Coras2b5fed82019-07-25 14:51:09 -0700867
Florin Coras20c24232021-11-22 21:19:01 -0800868 if (PREDICT_FALSE (app_wrk->mq_congested))
869 goto handle_congestion;
Florin Coras623eb562019-02-03 19:28:34 -0800870
Florin Coras20c24232021-11-22 21:19:01 -0800871 rv = mq_try_lock_and_alloc_msg (mq, SESSION_MQ_IO_EVT_RING, mq_msg);
Florin Coras623eb562019-02-03 19:28:34 -0800872
Florin Coras20c24232021-11-22 21:19:01 -0800873 if (PREDICT_FALSE (rv))
874 goto handle_congestion;
875
876 evt = svm_msg_q_msg_data (mq, mq_msg);
Florin Corasf6c43132019-03-01 12:41:21 -0800877 evt->event_type = SESSION_IO_EVT_RX;
Florin Coras20c24232021-11-22 21:19:01 -0800878 evt->session_index = app_session;
Florin Coras623eb562019-02-03 19:28:34 -0800879
880 (void) svm_fifo_set_event (s->rx_fifo);
Florin Coras20c24232021-11-22 21:19:01 -0800881
882 svm_msg_q_add_and_unlock (mq, mq_msg);
Florin Coras623eb562019-02-03 19:28:34 -0800883
Florin Coras623eb562019-02-03 19:28:34 -0800884 return 0;
Florin Coras20c24232021-11-22 21:19:01 -0800885
886handle_congestion:
887
888 app_wrk_add_mq_postponed_msg (app_wrk, SESSION_MQ_IO_EVT_RING,
889 SESSION_IO_EVT_RX, &app_session,
890 sizeof (app_session), -1);
891 return -1;
Florin Coras623eb562019-02-03 19:28:34 -0800892}
893
894static inline int
Florin Coras2b5fed82019-07-25 14:51:09 -0700895app_send_io_evt_tx (app_worker_t * app_wrk, session_t * s)
Florin Coras623eb562019-02-03 19:28:34 -0800896{
Florin Coras20c24232021-11-22 21:19:01 -0800897 svm_msg_q_msg_t _mq_msg = { 0 }, *mq_msg = &_mq_msg;
Florin Coras623eb562019-02-03 19:28:34 -0800898 session_event_t *evt;
Florin Coras20c24232021-11-22 21:19:01 -0800899 svm_msg_q_t *mq;
900 u32 app_session;
901 int rv;
Florin Coras623eb562019-02-03 19:28:34 -0800902
903 if (app_worker_application_is_builtin (app_wrk))
Florin Coras0e573f52019-05-07 16:28:16 -0700904 return app_worker_builtin_tx (app_wrk, s);
Florin Coras623eb562019-02-03 19:28:34 -0800905
Florin Coras20c24232021-11-22 21:19:01 -0800906 app_session = s->tx_fifo->shr->client_session_index;
Florin Coras623eb562019-02-03 19:28:34 -0800907 mq = app_wrk->event_queue;
Florin Coras2b5fed82019-07-25 14:51:09 -0700908
Florin Coras20c24232021-11-22 21:19:01 -0800909 if (PREDICT_FALSE (app_wrk->mq_congested))
910 goto handle_congestion;
Florin Coras623eb562019-02-03 19:28:34 -0800911
Florin Coras20c24232021-11-22 21:19:01 -0800912 rv = mq_try_lock_and_alloc_msg (mq, SESSION_MQ_IO_EVT_RING, mq_msg);
Florin Coras623eb562019-02-03 19:28:34 -0800913
Florin Coras20c24232021-11-22 21:19:01 -0800914 if (PREDICT_FALSE (rv))
915 goto handle_congestion;
916
917 evt = svm_msg_q_msg_data (mq, mq_msg);
Florin Corasf6c43132019-03-01 12:41:21 -0800918 evt->event_type = SESSION_IO_EVT_TX;
Florin Coras20c24232021-11-22 21:19:01 -0800919 evt->session_index = app_session;
Florin Coras623eb562019-02-03 19:28:34 -0800920
Florin Coras20c24232021-11-22 21:19:01 -0800921 svm_msg_q_add_and_unlock (mq, mq_msg);
922
Florin Coras2b5fed82019-07-25 14:51:09 -0700923 return 0;
Florin Coras20c24232021-11-22 21:19:01 -0800924
925handle_congestion:
926
927 app_wrk_add_mq_postponed_msg (app_wrk, SESSION_MQ_IO_EVT_RING,
928 SESSION_IO_EVT_TX, &app_session,
929 sizeof (app_session), -1);
930 return -1;
Florin Coras623eb562019-02-03 19:28:34 -0800931}
932
933/* *INDENT-OFF* */
934typedef int (app_send_evt_handler_fn) (app_worker_t *app,
Florin Coras2b5fed82019-07-25 14:51:09 -0700935 session_t *s);
Florin Coras653e43f2019-03-04 10:56:23 -0800936static app_send_evt_handler_fn * const app_send_evt_handler_fns[2] = {
Florin Coras623eb562019-02-03 19:28:34 -0800937 app_send_io_evt_rx,
Florin Coras623eb562019-02-03 19:28:34 -0800938 app_send_io_evt_tx,
939};
940/* *INDENT-ON* */
941
942/**
943 * Send event to application
944 *
Florin Coras623eb562019-02-03 19:28:34 -0800945 * Logic from queue perspective is blocking. However, if queue is full,
946 * we return.
947 */
948int
949app_worker_lock_and_send_event (app_worker_t * app, session_t * s,
950 u8 evt_type)
951{
Florin Coras2b5fed82019-07-25 14:51:09 -0700952 return app_send_evt_handler_fns[evt_type] (app, s);
Florin Coras623eb562019-02-03 19:28:34 -0800953}
954
Florin Coras623eb562019-02-03 19:28:34 -0800955u8 *
956format_app_worker_listener (u8 * s, va_list * args)
957{
958 app_worker_t *app_wrk = va_arg (*args, app_worker_t *);
959 u64 handle = va_arg (*args, u64);
960 u32 sm_index = va_arg (*args, u32);
961 int verbose = va_arg (*args, int);
962 session_t *listener;
963 const u8 *app_name;
964 u8 *str;
965
966 if (!app_wrk)
967 {
968 if (verbose)
Xiaoming Jiang806709f2021-06-23 09:07:57 +0000969 s = format (s, "%-" SESSION_CLI_ID_LEN "s%-25s%-10s%-15s%-15s%-10s",
970 "Connection", "App", "Wrk", "API Client", "ListenerID",
971 "SegManager");
Florin Coras623eb562019-02-03 19:28:34 -0800972 else
Xiaoming Jiang806709f2021-06-23 09:07:57 +0000973 s = format (s, "%-" SESSION_CLI_ID_LEN "s%-25s%-10s", "Connection",
974 "App", "Wrk");
Florin Coras623eb562019-02-03 19:28:34 -0800975
976 return s;
977 }
978
979 app_name = application_name_from_index (app_wrk->app_index);
980 listener = listen_session_get_from_handle (handle);
Florin Coras31c99552019-03-01 13:00:58 -0800981 str = format (0, "%U", format_session, listener, verbose);
Florin Coras623eb562019-02-03 19:28:34 -0800982
983 if (verbose)
984 {
Dave Barach3e07a4a2020-04-04 10:05:48 -0400985 u8 *buf;
986 buf = format (0, "%u(%u)", app_wrk->wrk_map_index, app_wrk->wrk_index);
Xiaoming Jiang806709f2021-06-23 09:07:57 +0000987 s = format (s, "%-" SESSION_CLI_ID_LEN "v%-25v%-10v%-15u%-15u%-10u", str,
988 app_name, buf, app_wrk->api_client_index, handle, sm_index);
Dave Barach3e07a4a2020-04-04 10:05:48 -0400989 vec_free (buf);
Florin Coras623eb562019-02-03 19:28:34 -0800990 }
991 else
Xiaoming Jiang806709f2021-06-23 09:07:57 +0000992 s = format (s, "%-" SESSION_CLI_ID_LEN "v%-25v%=10u", str, app_name,
993 app_wrk->wrk_map_index);
jiangxiaoming6b410e62020-10-10 15:23:54 +0800994
995 vec_free (str);
Florin Coras623eb562019-02-03 19:28:34 -0800996
997 return s;
998}
999
1000u8 *
1001format_app_worker (u8 * s, va_list * args)
1002{
1003 app_worker_t *app_wrk = va_arg (*args, app_worker_t *);
1004 u32 indent = 1;
1005
Florin Coras20c24232021-11-22 21:19:01 -08001006 s = format (s,
1007 "%U wrk-index %u app-index %u map-index %u "
1008 "api-client-index %d mq-cong %u\n",
1009 format_white_space, indent, app_wrk->wrk_index,
1010 app_wrk->app_index, app_wrk->wrk_map_index,
1011 app_wrk->api_client_index, app_wrk->mq_congested);
Florin Coras623eb562019-02-03 19:28:34 -08001012 return s;
1013}
1014
1015void
1016app_worker_format_connects (app_worker_t * app_wrk, int verbose)
1017{
Florin Coras623eb562019-02-03 19:28:34 -08001018 segment_manager_t *sm;
Florin Coras623eb562019-02-03 19:28:34 -08001019
1020 /* Header */
1021 if (!app_wrk)
1022 {
Florin Coras88001c62019-04-24 14:44:46 -07001023 segment_manager_format_sessions (0, verbose);
Florin Coras623eb562019-02-03 19:28:34 -08001024 return;
1025 }
1026
1027 if (app_wrk->connects_seg_manager == (u32) ~ 0)
1028 return;
1029
Florin Coras623eb562019-02-03 19:28:34 -08001030 sm = segment_manager_get (app_wrk->connects_seg_manager);
Florin Coras88001c62019-04-24 14:44:46 -07001031 segment_manager_format_sessions (sm, verbose);
Florin Coras623eb562019-02-03 19:28:34 -08001032}
1033
Florin Coras623eb562019-02-03 19:28:34 -08001034/*
1035 * fd.io coding-style-patch-verification: ON
1036 *
1037 * Local Variables:
1038 * eval: (c-set-style "gnu")
1039 * End:
1040 */