blob: 1793998f1e01944d1f42a797cf4dd8ee91c288ea [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;
35 app_wrk->first_segment_manager = APP_INVALID_SEGMENT_MANAGER_INDEX;
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 Coras623eb562019-02-03 19:28:34 -080059 u64 handle, *handles = 0;
60 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;
63 int i;
64
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 Coras623eb562019-02-03 19:28:34 -080073 sm = segment_manager_get (sm_index);
74 sm->app_wrk_index = SEGMENT_MANAGER_INVALID_APP_INDEX;
75 }));
76 /* *INDENT-ON* */
77
78 for (i = 0; i < vec_len (handles); i++)
79 {
80 a->app_index = app->app_index;
81 a->wrk_map_index = app_wrk->wrk_map_index;
82 a->handle = handles[i];
83 /* seg manager is removed when unbind completes */
Florin Corasc1a42652019-02-08 18:27:29 -080084 (void) vnet_unlisten (a);
Florin Coras623eb562019-02-03 19:28:34 -080085 }
86
87 /*
88 * Connects segment manager cleanup
89 */
90
91 if (app_wrk->connects_seg_manager != APP_INVALID_SEGMENT_MANAGER_INDEX)
92 {
93 sm = segment_manager_get (app_wrk->connects_seg_manager);
94 sm->app_wrk_index = SEGMENT_MANAGER_INVALID_APP_INDEX;
Florin Coras565115e2019-02-20 19:48:31 -080095 sm->first_is_protected = 0;
Florin Coras88001c62019-04-24 14:44:46 -070096 segment_manager_init_free (sm);
Florin Coras623eb562019-02-03 19:28:34 -080097 }
98
99 /* If first segment manager is used by a listener */
100 if (app_wrk->first_segment_manager != APP_INVALID_SEGMENT_MANAGER_INDEX
101 && app_wrk->first_segment_manager != app_wrk->connects_seg_manager)
102 {
103 sm = segment_manager_get (app_wrk->first_segment_manager);
104 sm->first_is_protected = 0;
105 sm->app_wrk_index = SEGMENT_MANAGER_INVALID_APP_INDEX;
106 /* .. and has no fifos, e.g. it might be used for redirected sessions,
107 * remove it */
108 if (!segment_manager_has_fifos (sm))
Florin Coras88001c62019-04-24 14:44:46 -0700109 segment_manager_free (sm);
Florin Coras623eb562019-02-03 19:28:34 -0800110 }
111
Florin Coras623eb562019-02-03 19:28:34 -0800112 if (CLIB_DEBUG)
113 clib_memset (app_wrk, 0xfe, sizeof (*app_wrk));
BenoƮt Ganned4aeb842019-07-18 18:38:42 +0200114 pool_put (app_workers, app_wrk);
Florin Coras623eb562019-02-03 19:28:34 -0800115}
116
117application_t *
118app_worker_get_app (u32 wrk_index)
119{
120 app_worker_t *app_wrk;
121 app_wrk = app_worker_get_if_valid (wrk_index);
122 if (!app_wrk)
123 return 0;
124 return application_get_if_valid (app_wrk->app_index);
125}
126
127static segment_manager_t *
128app_worker_alloc_segment_manager (app_worker_t * app_wrk)
129{
130 segment_manager_t *sm = 0;
131
132 /* If the first segment manager is not in use, don't allocate a new one */
133 if (app_wrk->first_segment_manager != APP_INVALID_SEGMENT_MANAGER_INDEX
134 && app_wrk->first_segment_manager_in_use == 0)
135 {
136 sm = segment_manager_get (app_wrk->first_segment_manager);
137 app_wrk->first_segment_manager_in_use = 1;
138 return sm;
139 }
140
Florin Coras88001c62019-04-24 14:44:46 -0700141 sm = segment_manager_alloc ();
Florin Coras623eb562019-02-03 19:28:34 -0800142 sm->app_wrk_index = app_wrk->wrk_index;
143
144 return sm;
145}
146
Florin Corasa27a46e2019-02-18 13:02:28 -0800147static int
148app_worker_alloc_session_fifos (segment_manager_t * sm, session_t * s)
149{
150 svm_fifo_t *rx_fifo = 0, *tx_fifo = 0;
Florin Corasa27a46e2019-02-18 13:02:28 -0800151 int rv;
152
Florin Coras62ddc032019-12-08 18:30:42 -0800153 if ((rv = segment_manager_alloc_session_fifos (sm, s->thread_index,
154 &rx_fifo, &tx_fifo)))
Florin Corasa27a46e2019-02-18 13:02:28 -0800155 return rv;
156
157 rx_fifo->master_session_index = s->session_index;
158 rx_fifo->master_thread_index = s->thread_index;
159
160 tx_fifo->master_session_index = s->session_index;
161 tx_fifo->master_thread_index = s->thread_index;
162
163 s->rx_fifo = rx_fifo;
164 s->tx_fifo = tx_fifo;
Florin Corasa27a46e2019-02-18 13:02:28 -0800165 return 0;
166}
167
Florin Coras623eb562019-02-03 19:28:34 -0800168int
Florin Corasd4295e62019-02-22 13:11:38 -0800169app_worker_init_listener (app_worker_t * app_wrk, session_t * ls)
Florin Coras623eb562019-02-03 19:28:34 -0800170{
171 segment_manager_t *sm;
172
173 /* Allocate segment manager. All sessions derived out of a listen session
174 * have fifos allocated by the same segment manager. */
175 if (!(sm = app_worker_alloc_segment_manager (app_wrk)))
176 return -1;
177
Florin Corasc9940fc2019-02-05 20:55:11 -0800178 /* Keep track of the segment manager for the listener or this worker */
Florin Coras623eb562019-02-03 19:28:34 -0800179 hash_set (app_wrk->listeners_table, listen_session_get_handle (ls),
180 segment_manager_index (sm));
181
Florin Corasc9940fc2019-02-05 20:55:11 -0800182 if (session_transport_service_type (ls) == TRANSPORT_SERVICE_CL)
Florin Coras623eb562019-02-03 19:28:34 -0800183 {
Florin Corasa27a46e2019-02-18 13:02:28 -0800184 if (!ls->rx_fifo && app_worker_alloc_session_fifos (sm, ls))
Florin Coras623eb562019-02-03 19:28:34 -0800185 return -1;
186 }
187 return 0;
188}
189
190int
Florin Corasd4295e62019-02-22 13:11:38 -0800191app_worker_start_listen (app_worker_t * app_wrk,
192 app_listener_t * app_listener)
193{
194 session_t *ls;
195
196 if (clib_bitmap_get (app_listener->workers, app_wrk->wrk_map_index))
197 return VNET_API_ERROR_ADDRESS_IN_USE;
198
199 app_listener->workers = clib_bitmap_set (app_listener->workers,
200 app_wrk->wrk_map_index, 1);
201
202 if (app_listener->session_index != SESSION_INVALID_INDEX)
203 {
204 ls = session_get (app_listener->session_index, 0);
205 if (app_worker_init_listener (app_wrk, ls))
206 return -1;
207 }
208
209 if (app_listener->local_index != SESSION_INVALID_INDEX)
210 {
211 ls = session_get (app_listener->local_index, 0);
212 if (app_worker_init_listener (app_wrk, ls))
213 return -1;
214 }
215
216 return 0;
217}
218
Florin Coras2b81e3c2019-02-27 07:55:46 -0800219static void
220app_worker_stop_listen_session (app_worker_t * app_wrk, session_t * ls)
Florin Coras623eb562019-02-03 19:28:34 -0800221{
Florin Corasc9940fc2019-02-05 20:55:11 -0800222 session_handle_t handle;
Florin Coras623eb562019-02-03 19:28:34 -0800223 segment_manager_t *sm;
224 uword *sm_indexp;
Florin Coras2b81e3c2019-02-27 07:55:46 -0800225
226 handle = listen_session_get_handle (ls);
227 sm_indexp = hash_get (app_wrk->listeners_table, handle);
228 if (PREDICT_FALSE (!sm_indexp))
229 return;
230
231 sm = segment_manager_get (*sm_indexp);
232 if (app_wrk->first_segment_manager == *sm_indexp)
233 {
234 /* Delete sessions but don't remove segment manager */
235 app_wrk->first_segment_manager_in_use = 0;
236 segment_manager_del_sessions (sm);
237 }
238 else
239 {
Florin Coras88001c62019-04-24 14:44:46 -0700240 segment_manager_init_free (sm);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800241 }
242 hash_unset (app_wrk->listeners_table, handle);
243}
244
245int
246app_worker_stop_listen (app_worker_t * app_wrk, app_listener_t * al)
247{
Florin Corasd4295e62019-02-22 13:11:38 -0800248 session_t *ls;
Florin Coras623eb562019-02-03 19:28:34 -0800249
Florin Corasc9940fc2019-02-05 20:55:11 -0800250 if (!clib_bitmap_get (al->workers, app_wrk->wrk_map_index))
251 return 0;
252
253 if (al->session_index != SESSION_INVALID_INDEX)
Florin Coras623eb562019-02-03 19:28:34 -0800254 {
Florin Corasc9940fc2019-02-05 20:55:11 -0800255 ls = listen_session_get (al->session_index);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800256 app_worker_stop_listen_session (app_wrk, ls);
Florin Coras623eb562019-02-03 19:28:34 -0800257 }
258
Florin Corasc9940fc2019-02-05 20:55:11 -0800259 if (al->local_index != SESSION_INVALID_INDEX)
Florin Coras623eb562019-02-03 19:28:34 -0800260 {
Florin Corasd4295e62019-02-22 13:11:38 -0800261 ls = listen_session_get (al->local_index);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800262 app_worker_stop_listen_session (app_wrk, ls);
Florin Coras623eb562019-02-03 19:28:34 -0800263 }
Florin Corasc9940fc2019-02-05 20:55:11 -0800264
265 clib_bitmap_set_no_check (al->workers, app_wrk->wrk_map_index, 0);
266 if (clib_bitmap_is_zero (al->workers))
267 app_listener_cleanup (al);
Florin Coras623eb562019-02-03 19:28:34 -0800268
269 return 0;
270}
271
272int
Florin Corasa27a46e2019-02-18 13:02:28 -0800273app_worker_init_accepted (session_t * s)
274{
275 app_worker_t *app_wrk;
276 segment_manager_t *sm;
277 session_t *listener;
Ryujiro Shibuyad8f48e22020-01-22 12:11:42 +0000278 application_t *app;
Florin Corasa27a46e2019-02-18 13:02:28 -0800279
Nathan Skrzypczak2f0f96b2019-06-13 10:14:28 +0200280 listener = listen_session_get_from_handle (s->listener_handle);
Florin Corasa27a46e2019-02-18 13:02:28 -0800281 app_wrk = application_listener_select_worker (listener);
282 s->app_wrk_index = app_wrk->wrk_index;
Florin Coras2b81e3c2019-02-27 07:55:46 -0800283
Ryujiro Shibuyad8f48e22020-01-22 12:11:42 +0000284 app = application_get (app_wrk->app_index);
285 if (app->cb_fns.fifo_tuning_callback)
286 s->flags |= SESSION_F_CUSTOM_FIFO_TUNING;
287
Florin Corasa27a46e2019-02-18 13:02:28 -0800288 sm = app_worker_get_listen_segment_manager (app_wrk, listener);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800289 if (app_worker_alloc_session_fifos (sm, s))
290 return -1;
291
292 return 0;
Florin Corasa27a46e2019-02-18 13:02:28 -0800293}
294
295int
296app_worker_accept_notify (app_worker_t * app_wrk, session_t * s)
297{
298 application_t *app = application_get (app_wrk->app_index);
299 return app->cb_fns.session_accept_callback (s);
300}
301
302int
303app_worker_init_connected (app_worker_t * app_wrk, session_t * s)
304{
305 application_t *app = application_get (app_wrk->app_index);
306 segment_manager_t *sm;
307
308 /* Allocate fifos for session, unless the app is a builtin proxy */
309 if (!application_is_builtin_proxy (app))
310 {
311 sm = app_worker_get_connect_segment_manager (app_wrk);
312 if (app_worker_alloc_session_fifos (sm, s))
313 return -1;
314 }
Ryujiro Shibuyad8f48e22020-01-22 12:11:42 +0000315
316 if (app->cb_fns.fifo_tuning_callback)
317 s->flags |= SESSION_F_CUSTOM_FIFO_TUNING;
318
Florin Corasa27a46e2019-02-18 13:02:28 -0800319 return 0;
320}
321
322int
323app_worker_connect_notify (app_worker_t * app_wrk, session_t * s, u32 opaque)
324{
325 application_t *app = application_get (app_wrk->app_index);
326 return app->cb_fns.session_connected_callback (app_wrk->wrk_index, opaque,
327 s, s == 0 /* is_fail */ );
328}
329
330int
Florin Corasbf7ce2c2019-03-06 14:44:42 -0800331app_worker_close_notify (app_worker_t * app_wrk, session_t * s)
332{
333 application_t *app = application_get (app_wrk->app_index);
334 app->cb_fns.session_disconnect_callback (s);
335 return 0;
336}
337
338int
Florin Coras692b9492019-07-12 15:01:53 -0700339app_worker_transport_closed_notify (app_worker_t * app_wrk, session_t * s)
340{
341 application_t *app = application_get (app_wrk->app_index);
342 if (app->cb_fns.session_transport_closed_callback)
343 app->cb_fns.session_transport_closed_callback (s);
344 return 0;
345}
346
347int
Florin Coras69b68ef2019-04-02 11:38:51 -0700348app_worker_reset_notify (app_worker_t * app_wrk, session_t * s)
349{
350 application_t *app = application_get (app_wrk->app_index);
351 app->cb_fns.session_reset_callback (s);
352 return 0;
353}
354
355int
Florin Coras70f26d52019-07-08 11:47:18 -0700356app_worker_cleanup_notify (app_worker_t * app_wrk, session_t * s,
357 session_cleanup_ntf_t ntf)
358{
359 application_t *app = application_get (app_wrk->app_index);
360 if (app->cb_fns.session_cleanup_callback)
361 app->cb_fns.session_cleanup_callback (s, ntf);
362 return 0;
363}
364
365int
Florin Corasbf7ce2c2019-03-06 14:44:42 -0800366app_worker_builtin_rx (app_worker_t * app_wrk, session_t * s)
367{
368 application_t *app = application_get (app_wrk->app_index);
369 app->cb_fns.builtin_app_rx_callback (s);
370 return 0;
371}
372
373int
Florin Coras0e573f52019-05-07 16:28:16 -0700374app_worker_builtin_tx (app_worker_t * app_wrk, session_t * s)
375{
376 application_t *app = application_get (app_wrk->app_index);
377
378 if (!app->cb_fns.builtin_app_tx_callback)
379 return 0;
380
381 app->cb_fns.builtin_app_tx_callback (s);
382 return 0;
383}
384
385int
Florin Coras49568af2019-07-31 16:46:24 -0700386app_worker_migrate_notify (app_worker_t * app_wrk, session_t * s,
387 session_handle_t new_sh)
388{
389 application_t *app = application_get (app_wrk->app_index);
390 app->cb_fns.session_migrate_callback (s, new_sh);
391 return 0;
392}
393
394int
Florin Coras623eb562019-02-03 19:28:34 -0800395app_worker_own_session (app_worker_t * app_wrk, session_t * s)
396{
397 segment_manager_t *sm;
398 svm_fifo_t *rxf, *txf;
399
400 if (s->session_state == SESSION_STATE_LISTENING)
401 return application_change_listener_owner (s, app_wrk);
402
403 s->app_wrk_index = app_wrk->wrk_index;
404
405 rxf = s->rx_fifo;
406 txf = s->tx_fifo;
407
408 if (!rxf || !txf)
409 return 0;
410
411 s->rx_fifo = 0;
412 s->tx_fifo = 0;
413
414 sm = app_worker_get_or_alloc_connect_segment_manager (app_wrk);
Florin Corasa27a46e2019-02-18 13:02:28 -0800415 if (app_worker_alloc_session_fifos (sm, s))
Florin Coras623eb562019-02-03 19:28:34 -0800416 return -1;
417
Sirshak Das28aa5392019-02-05 01:33:33 -0600418 if (!svm_fifo_is_empty_cons (rxf))
419 svm_fifo_clone (s->rx_fifo, rxf);
Florin Coras623eb562019-02-03 19:28:34 -0800420
Sirshak Das28aa5392019-02-05 01:33:33 -0600421 if (!svm_fifo_is_empty_cons (txf))
422 svm_fifo_clone (s->tx_fifo, txf);
423
Florin Coras19223e02019-03-03 14:56:05 -0800424 segment_manager_dealloc_fifos (rxf, txf);
Florin Coras623eb562019-02-03 19:28:34 -0800425
426 return 0;
427}
428
429int
Florin Corasc9940fc2019-02-05 20:55:11 -0800430app_worker_connect_session (app_worker_t * app, session_endpoint_t * sep,
431 u32 api_context)
Florin Coras623eb562019-02-03 19:28:34 -0800432{
433 int rv;
434
435 /* Make sure we have a segment manager for connects */
436 app_worker_alloc_connects_segment_manager (app);
437
438 if ((rv = session_open (app->wrk_index, sep, api_context)))
439 return rv;
440
441 return 0;
442}
443
444int
Ryujiro Shibuyad8f48e22020-01-22 12:11:42 +0000445app_worker_session_fifo_tuning (app_worker_t * app_wrk, session_t * s,
446 svm_fifo_t * f,
447 session_ft_action_t act, u32 len)
448{
449 application_t *app = application_get (app_wrk->app_index);
450 return app->cb_fns.fifo_tuning_callback (s, f, act, len);
451}
452
453int
Florin Coras623eb562019-02-03 19:28:34 -0800454app_worker_alloc_connects_segment_manager (app_worker_t * app_wrk)
455{
456 segment_manager_t *sm;
457
458 if (app_wrk->connects_seg_manager == APP_INVALID_SEGMENT_MANAGER_INDEX)
459 {
460 sm = app_worker_alloc_segment_manager (app_wrk);
461 if (sm == 0)
462 return -1;
463 app_wrk->connects_seg_manager = segment_manager_index (sm);
464 }
465 return 0;
466}
467
468segment_manager_t *
469app_worker_get_connect_segment_manager (app_worker_t * app)
470{
471 ASSERT (app->connects_seg_manager != (u32) ~ 0);
472 return segment_manager_get (app->connects_seg_manager);
473}
474
475segment_manager_t *
476app_worker_get_or_alloc_connect_segment_manager (app_worker_t * app_wrk)
477{
478 if (app_wrk->connects_seg_manager == (u32) ~ 0)
479 app_worker_alloc_connects_segment_manager (app_wrk);
480 return segment_manager_get (app_wrk->connects_seg_manager);
481}
482
483segment_manager_t *
484app_worker_get_listen_segment_manager (app_worker_t * app,
485 session_t * listener)
486{
487 uword *smp;
488 smp = hash_get (app->listeners_table, listen_session_get_handle (listener));
Dave Barach47d41ad2020-02-17 09:13:26 -0500489 ALWAYS_ASSERT (smp != 0);
Florin Coras623eb562019-02-03 19:28:34 -0800490 return segment_manager_get (*smp);
491}
492
493session_t *
Florin Corasc9940fc2019-02-05 20:55:11 -0800494app_worker_first_listener (app_worker_t * app_wrk, u8 fib_proto,
Florin Coras623eb562019-02-03 19:28:34 -0800495 u8 transport_proto)
496{
497 session_t *listener;
498 u64 handle;
499 u32 sm_index;
500 u8 sst;
501
502 sst = session_type_from_proto_and_ip (transport_proto,
503 fib_proto == FIB_PROTOCOL_IP4);
504
505 /* *INDENT-OFF* */
Florin Corasc9940fc2019-02-05 20:55:11 -0800506 hash_foreach (handle, sm_index, app_wrk->listeners_table, ({
Florin Coras623eb562019-02-03 19:28:34 -0800507 listener = listen_session_get_from_handle (handle);
508 if (listener->session_type == sst
Florin Corasd5c604d2019-03-18 09:06:35 -0700509 && !(listener->flags & SESSION_F_PROXY))
Florin Coras623eb562019-02-03 19:28:34 -0800510 return listener;
511 }));
512 /* *INDENT-ON* */
513
514 return 0;
515}
516
517session_t *
Florin Corasc9940fc2019-02-05 20:55:11 -0800518app_worker_proxy_listener (app_worker_t * app_wrk, u8 fib_proto,
Florin Coras623eb562019-02-03 19:28:34 -0800519 u8 transport_proto)
520{
521 session_t *listener;
522 u64 handle;
523 u32 sm_index;
524 u8 sst;
525
526 sst = session_type_from_proto_and_ip (transport_proto,
527 fib_proto == FIB_PROTOCOL_IP4);
528
529 /* *INDENT-OFF* */
Florin Corasc9940fc2019-02-05 20:55:11 -0800530 hash_foreach (handle, sm_index, app_wrk->listeners_table, ({
Florin Coras623eb562019-02-03 19:28:34 -0800531 listener = listen_session_get_from_handle (handle);
Florin Corasd5c604d2019-03-18 09:06:35 -0700532 if (listener->session_type == sst && (listener->flags & SESSION_F_PROXY))
Florin Coras623eb562019-02-03 19:28:34 -0800533 return listener;
534 }));
535 /* *INDENT-ON* */
536
537 return 0;
538}
539
540/**
541 * Send an API message to the external app, to map new segment
542 */
543int
Florin Coras2b81e3c2019-02-27 07:55:46 -0800544app_worker_add_segment_notify (app_worker_t * app_wrk, u64 segment_handle)
Florin Coras623eb562019-02-03 19:28:34 -0800545{
Florin Coras623eb562019-02-03 19:28:34 -0800546 application_t *app = application_get (app_wrk->app_index);
Florin Corasc4c4cf52019-08-24 18:17:34 -0700547
548 return app->cb_fns.add_segment_callback (app_wrk->wrk_index,
Florin Coras623eb562019-02-03 19:28:34 -0800549 segment_handle);
550}
551
Florin Coras2b81e3c2019-02-27 07:55:46 -0800552int
553app_worker_del_segment_notify (app_worker_t * app_wrk, u64 segment_handle)
554{
555 application_t *app = application_get (app_wrk->app_index);
Florin Corasc4c4cf52019-08-24 18:17:34 -0700556 return app->cb_fns.del_segment_callback (app_wrk->wrk_index,
Florin Coras2b81e3c2019-02-27 07:55:46 -0800557 segment_handle);
558}
559
Florin Coras31c99552019-03-01 13:00:58 -0800560static inline u8
Florin Coras623eb562019-02-03 19:28:34 -0800561app_worker_application_is_builtin (app_worker_t * app_wrk)
562{
563 return app_wrk->app_is_builtin;
564}
565
566static inline int
Florin Coras2b5fed82019-07-25 14:51:09 -0700567app_send_io_evt_rx (app_worker_t * app_wrk, session_t * s)
Florin Coras623eb562019-02-03 19:28:34 -0800568{
569 session_event_t *evt;
570 svm_msg_q_msg_t msg;
571 svm_msg_q_t *mq;
572
Florin Coras5c290292019-09-19 08:19:44 -0700573 if (app_worker_application_is_builtin (app_wrk))
574 return app_worker_builtin_rx (app_wrk, s);
575
576 /* Make sure the session is in established state within external apps.
577 * Should be removed once we confirm closes to apps */
Florin Coras623eb562019-02-03 19:28:34 -0800578 if (PREDICT_FALSE (s->session_state != SESSION_STATE_READY
579 && s->session_state != SESSION_STATE_LISTENING))
Florin Coras096f2f82019-04-02 10:17:48 -0700580 return 0;
Florin Coras623eb562019-02-03 19:28:34 -0800581
Florin Coras653e43f2019-03-04 10:56:23 -0800582 if (svm_fifo_has_event (s->rx_fifo))
Florin Coras623eb562019-02-03 19:28:34 -0800583 return 0;
584
585 mq = app_wrk->event_queue;
Florin Coras2b5fed82019-07-25 14:51:09 -0700586 svm_msg_q_lock (mq);
587
588 if (PREDICT_FALSE (svm_msg_q_is_full (mq)))
589 {
590 clib_warning ("evt q full");
591 svm_msg_q_unlock (mq);
592 return -1;
593 }
Florin Coras623eb562019-02-03 19:28:34 -0800594
595 if (PREDICT_FALSE (svm_msg_q_ring_is_full (mq, SESSION_MQ_IO_EVT_RING)))
596 {
597 clib_warning ("evt q rings full");
Florin Coras2b5fed82019-07-25 14:51:09 -0700598 svm_msg_q_unlock (mq);
Florin Coras623eb562019-02-03 19:28:34 -0800599 return -1;
600 }
601
602 msg = svm_msg_q_alloc_msg_w_ring (mq, SESSION_MQ_IO_EVT_RING);
Florin Coras623eb562019-02-03 19:28:34 -0800603 evt = (session_event_t *) svm_msg_q_msg_data (mq, &msg);
Florin Corasc0737e92019-03-04 14:19:39 -0800604 evt->session_index = s->rx_fifo->client_session_index;
Florin Corasf6c43132019-03-01 12:41:21 -0800605 evt->event_type = SESSION_IO_EVT_RX;
Florin Coras623eb562019-02-03 19:28:34 -0800606
607 (void) svm_fifo_set_event (s->rx_fifo);
Florin Coras2b5fed82019-07-25 14:51:09 -0700608 svm_msg_q_add_and_unlock (mq, &msg);
Florin Coras623eb562019-02-03 19:28:34 -0800609
Florin Coras623eb562019-02-03 19:28:34 -0800610 return 0;
611}
612
613static inline int
Florin Coras2b5fed82019-07-25 14:51:09 -0700614app_send_io_evt_tx (app_worker_t * app_wrk, session_t * s)
Florin Coras623eb562019-02-03 19:28:34 -0800615{
616 svm_msg_q_t *mq;
617 session_event_t *evt;
618 svm_msg_q_msg_t msg;
619
620 if (app_worker_application_is_builtin (app_wrk))
Florin Coras0e573f52019-05-07 16:28:16 -0700621 return app_worker_builtin_tx (app_wrk, s);
Florin Coras623eb562019-02-03 19:28:34 -0800622
623 mq = app_wrk->event_queue;
Florin Coras2b5fed82019-07-25 14:51:09 -0700624 svm_msg_q_lock (mq);
625
626 if (PREDICT_FALSE (svm_msg_q_is_full (mq)))
627 {
628 clib_warning ("evt q full");
629 svm_msg_q_unlock (mq);
630 return -1;
631 }
Florin Coras623eb562019-02-03 19:28:34 -0800632
633 if (PREDICT_FALSE (svm_msg_q_ring_is_full (mq, SESSION_MQ_IO_EVT_RING)))
634 {
635 clib_warning ("evt q rings full");
Florin Coras2b5fed82019-07-25 14:51:09 -0700636 svm_msg_q_unlock (mq);
Florin Coras623eb562019-02-03 19:28:34 -0800637 return -1;
638 }
639
640 msg = svm_msg_q_alloc_msg_w_ring (mq, SESSION_MQ_IO_EVT_RING);
Florin Coras623eb562019-02-03 19:28:34 -0800641 evt = (session_event_t *) svm_msg_q_msg_data (mq, &msg);
Florin Corasf6c43132019-03-01 12:41:21 -0800642 evt->event_type = SESSION_IO_EVT_TX;
Florin Corasc0737e92019-03-04 14:19:39 -0800643 evt->session_index = s->tx_fifo->client_session_index;
Florin Coras623eb562019-02-03 19:28:34 -0800644
Florin Coras2b5fed82019-07-25 14:51:09 -0700645 svm_msg_q_add_and_unlock (mq, &msg);
646 return 0;
Florin Coras623eb562019-02-03 19:28:34 -0800647}
648
649/* *INDENT-OFF* */
650typedef int (app_send_evt_handler_fn) (app_worker_t *app,
Florin Coras2b5fed82019-07-25 14:51:09 -0700651 session_t *s);
Florin Coras653e43f2019-03-04 10:56:23 -0800652static app_send_evt_handler_fn * const app_send_evt_handler_fns[2] = {
Florin Coras623eb562019-02-03 19:28:34 -0800653 app_send_io_evt_rx,
Florin Coras623eb562019-02-03 19:28:34 -0800654 app_send_io_evt_tx,
655};
656/* *INDENT-ON* */
657
658/**
659 * Send event to application
660 *
Florin Coras623eb562019-02-03 19:28:34 -0800661 * Logic from queue perspective is blocking. However, if queue is full,
662 * we return.
663 */
664int
665app_worker_lock_and_send_event (app_worker_t * app, session_t * s,
666 u8 evt_type)
667{
Florin Coras2b5fed82019-07-25 14:51:09 -0700668 return app_send_evt_handler_fns[evt_type] (app, s);
Florin Coras623eb562019-02-03 19:28:34 -0800669}
670
Florin Coras623eb562019-02-03 19:28:34 -0800671u8 *
672format_app_worker_listener (u8 * s, va_list * args)
673{
674 app_worker_t *app_wrk = va_arg (*args, app_worker_t *);
675 u64 handle = va_arg (*args, u64);
676 u32 sm_index = va_arg (*args, u32);
677 int verbose = va_arg (*args, int);
678 session_t *listener;
679 const u8 *app_name;
680 u8 *str;
681
682 if (!app_wrk)
683 {
684 if (verbose)
685 s = format (s, "%-40s%-25s%=10s%-15s%-15s%-10s", "Connection", "App",
686 "Wrk", "API Client", "ListenerID", "SegManager");
687 else
688 s = format (s, "%-40s%-25s%=10s", "Connection", "App", "Wrk");
689
690 return s;
691 }
692
693 app_name = application_name_from_index (app_wrk->app_index);
694 listener = listen_session_get_from_handle (handle);
Florin Coras31c99552019-03-01 13:00:58 -0800695 str = format (0, "%U", format_session, listener, verbose);
Florin Coras623eb562019-02-03 19:28:34 -0800696
697 if (verbose)
698 {
699 char buf[32];
700 sprintf (buf, "%u(%u)", app_wrk->wrk_map_index, app_wrk->wrk_index);
701 s = format (s, "%-40s%-25s%=10s%-15u%-15u%-10u", str, app_name,
702 buf, app_wrk->api_client_index, handle, sm_index);
703 }
704 else
705 s = format (s, "%-40s%-25s%=10u", str, app_name, app_wrk->wrk_map_index);
706
707 return s;
708}
709
710u8 *
711format_app_worker (u8 * s, va_list * args)
712{
713 app_worker_t *app_wrk = va_arg (*args, app_worker_t *);
714 u32 indent = 1;
715
716 s = format (s, "%U wrk-index %u app-index %u map-index %u "
717 "api-client-index %d\n", format_white_space, indent,
718 app_wrk->wrk_index, app_wrk->app_index, app_wrk->wrk_map_index,
719 app_wrk->api_client_index);
720 return s;
721}
722
723void
724app_worker_format_connects (app_worker_t * app_wrk, int verbose)
725{
Florin Coras623eb562019-02-03 19:28:34 -0800726 segment_manager_t *sm;
Florin Coras623eb562019-02-03 19:28:34 -0800727
728 /* Header */
729 if (!app_wrk)
730 {
Florin Coras88001c62019-04-24 14:44:46 -0700731 segment_manager_format_sessions (0, verbose);
Florin Coras623eb562019-02-03 19:28:34 -0800732 return;
733 }
734
735 if (app_wrk->connects_seg_manager == (u32) ~ 0)
736 return;
737
Florin Coras623eb562019-02-03 19:28:34 -0800738 sm = segment_manager_get (app_wrk->connects_seg_manager);
Florin Coras88001c62019-04-24 14:44:46 -0700739 segment_manager_format_sessions (sm, verbose);
Florin Coras623eb562019-02-03 19:28:34 -0800740}
741
Florin Coras623eb562019-02-03 19:28:34 -0800742/*
743 * fd.io coding-style-patch-verification: ON
744 *
745 * Local Variables:
746 * eval: (c-set-style "gnu")
747 * End:
748 */