blob: 84682cdf930cfab818093c8cd167818b65048db7 [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 pool_put (app_workers, app_wrk);
113 if (CLIB_DEBUG)
114 clib_memset (app_wrk, 0xfe, sizeof (*app_wrk));
115}
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 Coras1219b2d2019-04-23 15:53:43 -0700153 if ((rv = segment_manager_alloc_session_fifos (sm, &rx_fifo, &tx_fifo)))
Florin Corasa27a46e2019-02-18 13:02:28 -0800154 return rv;
155
156 rx_fifo->master_session_index = s->session_index;
157 rx_fifo->master_thread_index = s->thread_index;
158
159 tx_fifo->master_session_index = s->session_index;
160 tx_fifo->master_thread_index = s->thread_index;
161
162 s->rx_fifo = rx_fifo;
163 s->tx_fifo = tx_fifo;
Florin Corasa27a46e2019-02-18 13:02:28 -0800164 return 0;
165}
166
Florin Coras623eb562019-02-03 19:28:34 -0800167int
Florin Corasd4295e62019-02-22 13:11:38 -0800168app_worker_init_listener (app_worker_t * app_wrk, session_t * ls)
Florin Coras623eb562019-02-03 19:28:34 -0800169{
170 segment_manager_t *sm;
171
172 /* Allocate segment manager. All sessions derived out of a listen session
173 * have fifos allocated by the same segment manager. */
174 if (!(sm = app_worker_alloc_segment_manager (app_wrk)))
175 return -1;
176
Florin Corasc9940fc2019-02-05 20:55:11 -0800177 /* Keep track of the segment manager for the listener or this worker */
Florin Coras623eb562019-02-03 19:28:34 -0800178 hash_set (app_wrk->listeners_table, listen_session_get_handle (ls),
179 segment_manager_index (sm));
180
Florin Corasc9940fc2019-02-05 20:55:11 -0800181 if (session_transport_service_type (ls) == TRANSPORT_SERVICE_CL)
Florin Coras623eb562019-02-03 19:28:34 -0800182 {
Florin Corasa27a46e2019-02-18 13:02:28 -0800183 if (!ls->rx_fifo && app_worker_alloc_session_fifos (sm, ls))
Florin Coras623eb562019-02-03 19:28:34 -0800184 return -1;
185 }
186 return 0;
187}
188
189int
Florin Corasd4295e62019-02-22 13:11:38 -0800190app_worker_start_listen (app_worker_t * app_wrk,
191 app_listener_t * app_listener)
192{
193 session_t *ls;
194
195 if (clib_bitmap_get (app_listener->workers, app_wrk->wrk_map_index))
196 return VNET_API_ERROR_ADDRESS_IN_USE;
197
198 app_listener->workers = clib_bitmap_set (app_listener->workers,
199 app_wrk->wrk_map_index, 1);
200
201 if (app_listener->session_index != SESSION_INVALID_INDEX)
202 {
203 ls = session_get (app_listener->session_index, 0);
204 if (app_worker_init_listener (app_wrk, ls))
205 return -1;
206 }
207
208 if (app_listener->local_index != SESSION_INVALID_INDEX)
209 {
210 ls = session_get (app_listener->local_index, 0);
211 if (app_worker_init_listener (app_wrk, ls))
212 return -1;
213 }
214
215 return 0;
216}
217
Florin Coras2b81e3c2019-02-27 07:55:46 -0800218static void
219app_worker_stop_listen_session (app_worker_t * app_wrk, session_t * ls)
Florin Coras623eb562019-02-03 19:28:34 -0800220{
Florin Corasc9940fc2019-02-05 20:55:11 -0800221 session_handle_t handle;
Florin Coras623eb562019-02-03 19:28:34 -0800222 segment_manager_t *sm;
223 uword *sm_indexp;
Florin Coras2b81e3c2019-02-27 07:55:46 -0800224
225 handle = listen_session_get_handle (ls);
226 sm_indexp = hash_get (app_wrk->listeners_table, handle);
227 if (PREDICT_FALSE (!sm_indexp))
228 return;
229
230 sm = segment_manager_get (*sm_indexp);
231 if (app_wrk->first_segment_manager == *sm_indexp)
232 {
233 /* Delete sessions but don't remove segment manager */
234 app_wrk->first_segment_manager_in_use = 0;
235 segment_manager_del_sessions (sm);
236 }
237 else
238 {
Florin Coras88001c62019-04-24 14:44:46 -0700239 segment_manager_init_free (sm);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800240 }
241 hash_unset (app_wrk->listeners_table, handle);
242}
243
244int
245app_worker_stop_listen (app_worker_t * app_wrk, app_listener_t * al)
246{
Florin Corasd4295e62019-02-22 13:11:38 -0800247 session_t *ls;
Florin Coras623eb562019-02-03 19:28:34 -0800248
Florin Corasc9940fc2019-02-05 20:55:11 -0800249 if (!clib_bitmap_get (al->workers, app_wrk->wrk_map_index))
250 return 0;
251
252 if (al->session_index != SESSION_INVALID_INDEX)
Florin Coras623eb562019-02-03 19:28:34 -0800253 {
Florin Corasc9940fc2019-02-05 20:55:11 -0800254 ls = listen_session_get (al->session_index);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800255 app_worker_stop_listen_session (app_wrk, ls);
Florin Coras623eb562019-02-03 19:28:34 -0800256 }
257
Florin Corasc9940fc2019-02-05 20:55:11 -0800258 if (al->local_index != SESSION_INVALID_INDEX)
Florin Coras623eb562019-02-03 19:28:34 -0800259 {
Florin Corasd4295e62019-02-22 13:11:38 -0800260 ls = listen_session_get (al->local_index);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800261 app_worker_stop_listen_session (app_wrk, ls);
Florin Coras623eb562019-02-03 19:28:34 -0800262 }
Florin Corasc9940fc2019-02-05 20:55:11 -0800263
264 clib_bitmap_set_no_check (al->workers, app_wrk->wrk_map_index, 0);
265 if (clib_bitmap_is_zero (al->workers))
266 app_listener_cleanup (al);
Florin Coras623eb562019-02-03 19:28:34 -0800267
268 return 0;
269}
270
271int
Florin Corasa27a46e2019-02-18 13:02:28 -0800272app_worker_init_accepted (session_t * s)
273{
274 app_worker_t *app_wrk;
275 segment_manager_t *sm;
276 session_t *listener;
277
Nathan Skrzypczak2f0f96b2019-06-13 10:14:28 +0200278 listener = listen_session_get_from_handle (s->listener_handle);
Florin Corasa27a46e2019-02-18 13:02:28 -0800279 app_wrk = application_listener_select_worker (listener);
280 s->app_wrk_index = app_wrk->wrk_index;
Florin Coras2b81e3c2019-02-27 07:55:46 -0800281
Florin Corasa27a46e2019-02-18 13:02:28 -0800282 sm = app_worker_get_listen_segment_manager (app_wrk, listener);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800283 if (app_worker_alloc_session_fifos (sm, s))
284 return -1;
285
286 return 0;
Florin Corasa27a46e2019-02-18 13:02:28 -0800287}
288
289int
290app_worker_accept_notify (app_worker_t * app_wrk, session_t * s)
291{
292 application_t *app = application_get (app_wrk->app_index);
293 return app->cb_fns.session_accept_callback (s);
294}
295
296int
297app_worker_init_connected (app_worker_t * app_wrk, session_t * s)
298{
299 application_t *app = application_get (app_wrk->app_index);
300 segment_manager_t *sm;
301
302 /* Allocate fifos for session, unless the app is a builtin proxy */
303 if (!application_is_builtin_proxy (app))
304 {
305 sm = app_worker_get_connect_segment_manager (app_wrk);
306 if (app_worker_alloc_session_fifos (sm, s))
307 return -1;
308 }
309 return 0;
310}
311
312int
313app_worker_connect_notify (app_worker_t * app_wrk, session_t * s, u32 opaque)
314{
315 application_t *app = application_get (app_wrk->app_index);
316 return app->cb_fns.session_connected_callback (app_wrk->wrk_index, opaque,
317 s, s == 0 /* is_fail */ );
318}
319
320int
Florin Corasbf7ce2c2019-03-06 14:44:42 -0800321app_worker_close_notify (app_worker_t * app_wrk, session_t * s)
322{
323 application_t *app = application_get (app_wrk->app_index);
324 app->cb_fns.session_disconnect_callback (s);
325 return 0;
326}
327
328int
Florin Coras69b68ef2019-04-02 11:38:51 -0700329app_worker_reset_notify (app_worker_t * app_wrk, session_t * s)
330{
331 application_t *app = application_get (app_wrk->app_index);
332 app->cb_fns.session_reset_callback (s);
333 return 0;
334}
335
336int
Florin Coras70f26d52019-07-08 11:47:18 -0700337app_worker_cleanup_notify (app_worker_t * app_wrk, session_t * s,
338 session_cleanup_ntf_t ntf)
339{
340 application_t *app = application_get (app_wrk->app_index);
341 if (app->cb_fns.session_cleanup_callback)
342 app->cb_fns.session_cleanup_callback (s, ntf);
343 return 0;
344}
345
346int
Florin Corasbf7ce2c2019-03-06 14:44:42 -0800347app_worker_builtin_rx (app_worker_t * app_wrk, session_t * s)
348{
349 application_t *app = application_get (app_wrk->app_index);
350 app->cb_fns.builtin_app_rx_callback (s);
351 return 0;
352}
353
354int
Florin Coras0e573f52019-05-07 16:28:16 -0700355app_worker_builtin_tx (app_worker_t * app_wrk, session_t * s)
356{
357 application_t *app = application_get (app_wrk->app_index);
358
359 if (!app->cb_fns.builtin_app_tx_callback)
360 return 0;
361
362 app->cb_fns.builtin_app_tx_callback (s);
363 return 0;
364}
365
366int
Florin Coras623eb562019-02-03 19:28:34 -0800367app_worker_own_session (app_worker_t * app_wrk, session_t * s)
368{
369 segment_manager_t *sm;
370 svm_fifo_t *rxf, *txf;
371
372 if (s->session_state == SESSION_STATE_LISTENING)
373 return application_change_listener_owner (s, app_wrk);
374
375 s->app_wrk_index = app_wrk->wrk_index;
376
377 rxf = s->rx_fifo;
378 txf = s->tx_fifo;
379
380 if (!rxf || !txf)
381 return 0;
382
383 s->rx_fifo = 0;
384 s->tx_fifo = 0;
385
386 sm = app_worker_get_or_alloc_connect_segment_manager (app_wrk);
Florin Corasa27a46e2019-02-18 13:02:28 -0800387 if (app_worker_alloc_session_fifos (sm, s))
Florin Coras623eb562019-02-03 19:28:34 -0800388 return -1;
389
Sirshak Das28aa5392019-02-05 01:33:33 -0600390 if (!svm_fifo_is_empty_cons (rxf))
391 svm_fifo_clone (s->rx_fifo, rxf);
Florin Coras623eb562019-02-03 19:28:34 -0800392
Sirshak Das28aa5392019-02-05 01:33:33 -0600393 if (!svm_fifo_is_empty_cons (txf))
394 svm_fifo_clone (s->tx_fifo, txf);
395
Florin Coras19223e02019-03-03 14:56:05 -0800396 segment_manager_dealloc_fifos (rxf, txf);
Florin Coras623eb562019-02-03 19:28:34 -0800397
398 return 0;
399}
400
401int
Florin Corasc9940fc2019-02-05 20:55:11 -0800402app_worker_connect_session (app_worker_t * app, session_endpoint_t * sep,
403 u32 api_context)
Florin Coras623eb562019-02-03 19:28:34 -0800404{
405 int rv;
406
407 /* Make sure we have a segment manager for connects */
408 app_worker_alloc_connects_segment_manager (app);
409
410 if ((rv = session_open (app->wrk_index, sep, api_context)))
411 return rv;
412
413 return 0;
414}
415
416int
417app_worker_alloc_connects_segment_manager (app_worker_t * app_wrk)
418{
419 segment_manager_t *sm;
420
421 if (app_wrk->connects_seg_manager == APP_INVALID_SEGMENT_MANAGER_INDEX)
422 {
423 sm = app_worker_alloc_segment_manager (app_wrk);
424 if (sm == 0)
425 return -1;
426 app_wrk->connects_seg_manager = segment_manager_index (sm);
427 }
428 return 0;
429}
430
431segment_manager_t *
432app_worker_get_connect_segment_manager (app_worker_t * app)
433{
434 ASSERT (app->connects_seg_manager != (u32) ~ 0);
435 return segment_manager_get (app->connects_seg_manager);
436}
437
438segment_manager_t *
439app_worker_get_or_alloc_connect_segment_manager (app_worker_t * app_wrk)
440{
441 if (app_wrk->connects_seg_manager == (u32) ~ 0)
442 app_worker_alloc_connects_segment_manager (app_wrk);
443 return segment_manager_get (app_wrk->connects_seg_manager);
444}
445
446segment_manager_t *
447app_worker_get_listen_segment_manager (app_worker_t * app,
448 session_t * listener)
449{
450 uword *smp;
451 smp = hash_get (app->listeners_table, listen_session_get_handle (listener));
452 ASSERT (smp != 0);
453 return segment_manager_get (*smp);
454}
455
456session_t *
Florin Corasc9940fc2019-02-05 20:55:11 -0800457app_worker_first_listener (app_worker_t * app_wrk, u8 fib_proto,
Florin Coras623eb562019-02-03 19:28:34 -0800458 u8 transport_proto)
459{
460 session_t *listener;
461 u64 handle;
462 u32 sm_index;
463 u8 sst;
464
465 sst = session_type_from_proto_and_ip (transport_proto,
466 fib_proto == FIB_PROTOCOL_IP4);
467
468 /* *INDENT-OFF* */
Florin Corasc9940fc2019-02-05 20:55:11 -0800469 hash_foreach (handle, sm_index, app_wrk->listeners_table, ({
Florin Coras623eb562019-02-03 19:28:34 -0800470 listener = listen_session_get_from_handle (handle);
471 if (listener->session_type == sst
Florin Corasd5c604d2019-03-18 09:06:35 -0700472 && !(listener->flags & SESSION_F_PROXY))
Florin Coras623eb562019-02-03 19:28:34 -0800473 return listener;
474 }));
475 /* *INDENT-ON* */
476
477 return 0;
478}
479
480session_t *
Florin Corasc9940fc2019-02-05 20:55:11 -0800481app_worker_proxy_listener (app_worker_t * app_wrk, u8 fib_proto,
Florin Coras623eb562019-02-03 19:28:34 -0800482 u8 transport_proto)
483{
484 session_t *listener;
485 u64 handle;
486 u32 sm_index;
487 u8 sst;
488
489 sst = session_type_from_proto_and_ip (transport_proto,
490 fib_proto == FIB_PROTOCOL_IP4);
491
492 /* *INDENT-OFF* */
Florin Corasc9940fc2019-02-05 20:55:11 -0800493 hash_foreach (handle, sm_index, app_wrk->listeners_table, ({
Florin Coras623eb562019-02-03 19:28:34 -0800494 listener = listen_session_get_from_handle (handle);
Florin Corasd5c604d2019-03-18 09:06:35 -0700495 if (listener->session_type == sst && (listener->flags & SESSION_F_PROXY))
Florin Coras623eb562019-02-03 19:28:34 -0800496 return listener;
497 }));
498 /* *INDENT-ON* */
499
500 return 0;
501}
502
503/**
504 * Send an API message to the external app, to map new segment
505 */
506int
Florin Coras2b81e3c2019-02-27 07:55:46 -0800507app_worker_add_segment_notify (app_worker_t * app_wrk, u64 segment_handle)
Florin Coras623eb562019-02-03 19:28:34 -0800508{
Florin Coras623eb562019-02-03 19:28:34 -0800509 application_t *app = application_get (app_wrk->app_index);
510 return app->cb_fns.add_segment_callback (app_wrk->api_client_index,
511 segment_handle);
512}
513
Florin Coras2b81e3c2019-02-27 07:55:46 -0800514int
515app_worker_del_segment_notify (app_worker_t * app_wrk, u64 segment_handle)
516{
517 application_t *app = application_get (app_wrk->app_index);
518 return app->cb_fns.del_segment_callback (app_wrk->api_client_index,
519 segment_handle);
520}
521
Florin Coras31c99552019-03-01 13:00:58 -0800522static inline u8
Florin Coras623eb562019-02-03 19:28:34 -0800523app_worker_application_is_builtin (app_worker_t * app_wrk)
524{
525 return app_wrk->app_is_builtin;
526}
527
528static inline int
529app_enqueue_evt (svm_msg_q_t * mq, svm_msg_q_msg_t * msg, u8 lock)
530{
531 if (PREDICT_FALSE (svm_msg_q_is_full (mq)))
532 {
533 clib_warning ("evt q full");
534 svm_msg_q_free_msg (mq, msg);
535 if (lock)
536 svm_msg_q_unlock (mq);
537 return -1;
538 }
539
540 if (lock)
541 {
542 svm_msg_q_add_and_unlock (mq, msg);
543 return 0;
544 }
545
546 /* Even when not locking the ring, we must wait for queue mutex */
547 if (svm_msg_q_add (mq, msg, SVM_Q_WAIT))
548 {
549 clib_warning ("msg q add returned");
550 return -1;
551 }
552 return 0;
553}
554
555static inline int
556app_send_io_evt_rx (app_worker_t * app_wrk, session_t * s, u8 lock)
557{
558 session_event_t *evt;
559 svm_msg_q_msg_t msg;
560 svm_msg_q_t *mq;
561
562 if (PREDICT_FALSE (s->session_state != SESSION_STATE_READY
563 && s->session_state != SESSION_STATE_LISTENING))
Florin Coras096f2f82019-04-02 10:17:48 -0700564 return 0;
Florin Coras623eb562019-02-03 19:28:34 -0800565
566 if (app_worker_application_is_builtin (app_wrk))
Florin Coras69b68ef2019-04-02 11:38:51 -0700567 return app_worker_builtin_rx (app_wrk, s);
Florin Coras623eb562019-02-03 19:28:34 -0800568
Florin Coras653e43f2019-03-04 10:56:23 -0800569 if (svm_fifo_has_event (s->rx_fifo))
Florin Coras623eb562019-02-03 19:28:34 -0800570 return 0;
571
572 mq = app_wrk->event_queue;
573 if (lock)
574 svm_msg_q_lock (mq);
575
576 if (PREDICT_FALSE (svm_msg_q_ring_is_full (mq, SESSION_MQ_IO_EVT_RING)))
577 {
578 clib_warning ("evt q rings full");
579 if (lock)
580 svm_msg_q_unlock (mq);
581 return -1;
582 }
583
584 msg = svm_msg_q_alloc_msg_w_ring (mq, SESSION_MQ_IO_EVT_RING);
585 ASSERT (!svm_msg_q_msg_is_invalid (&msg));
586
587 evt = (session_event_t *) svm_msg_q_msg_data (mq, &msg);
Florin Corasc0737e92019-03-04 14:19:39 -0800588 evt->session_index = s->rx_fifo->client_session_index;
Florin Corasf6c43132019-03-01 12:41:21 -0800589 evt->event_type = SESSION_IO_EVT_RX;
Florin Coras623eb562019-02-03 19:28:34 -0800590
591 (void) svm_fifo_set_event (s->rx_fifo);
592
593 if (app_enqueue_evt (mq, &msg, lock))
594 return -1;
595 return 0;
596}
597
598static inline int
599app_send_io_evt_tx (app_worker_t * app_wrk, session_t * s, u8 lock)
600{
601 svm_msg_q_t *mq;
602 session_event_t *evt;
603 svm_msg_q_msg_t msg;
604
605 if (app_worker_application_is_builtin (app_wrk))
Florin Coras0e573f52019-05-07 16:28:16 -0700606 return app_worker_builtin_tx (app_wrk, s);
Florin Coras623eb562019-02-03 19:28:34 -0800607
608 mq = app_wrk->event_queue;
609 if (lock)
610 svm_msg_q_lock (mq);
611
612 if (PREDICT_FALSE (svm_msg_q_ring_is_full (mq, SESSION_MQ_IO_EVT_RING)))
613 {
614 clib_warning ("evt q rings full");
615 if (lock)
616 svm_msg_q_unlock (mq);
617 return -1;
618 }
619
620 msg = svm_msg_q_alloc_msg_w_ring (mq, SESSION_MQ_IO_EVT_RING);
621 ASSERT (!svm_msg_q_msg_is_invalid (&msg));
622
623 evt = (session_event_t *) svm_msg_q_msg_data (mq, &msg);
Florin Corasf6c43132019-03-01 12:41:21 -0800624 evt->event_type = SESSION_IO_EVT_TX;
Florin Corasc0737e92019-03-04 14:19:39 -0800625 evt->session_index = s->tx_fifo->client_session_index;
Florin Coras623eb562019-02-03 19:28:34 -0800626
627 return app_enqueue_evt (mq, &msg, lock);
628}
629
630/* *INDENT-OFF* */
631typedef int (app_send_evt_handler_fn) (app_worker_t *app,
632 session_t *s,
633 u8 lock);
Florin Coras653e43f2019-03-04 10:56:23 -0800634static app_send_evt_handler_fn * const app_send_evt_handler_fns[2] = {
Florin Coras623eb562019-02-03 19:28:34 -0800635 app_send_io_evt_rx,
Florin Coras623eb562019-02-03 19:28:34 -0800636 app_send_io_evt_tx,
637};
638/* *INDENT-ON* */
639
640/**
641 * Send event to application
642 *
643 * Logic from queue perspective is non-blocking. If there's
644 * not enough space to enqueue a message, we return.
645 */
646int
647app_worker_send_event (app_worker_t * app, session_t * s, u8 evt_type)
648{
Florin Corasf6c43132019-03-01 12:41:21 -0800649 ASSERT (app && evt_type <= SESSION_IO_EVT_TX);
Florin Coras623eb562019-02-03 19:28:34 -0800650 return app_send_evt_handler_fns[evt_type] (app, s, 0 /* lock */ );
651}
652
653/**
654 * Send event to application
655 *
656 * Logic from queue perspective is blocking. However, if queue is full,
657 * we return.
658 */
659int
660app_worker_lock_and_send_event (app_worker_t * app, session_t * s,
661 u8 evt_type)
662{
663 return app_send_evt_handler_fns[evt_type] (app, s, 1 /* lock */ );
664}
665
Florin Coras623eb562019-02-03 19:28:34 -0800666u8 *
667format_app_worker_listener (u8 * s, va_list * args)
668{
669 app_worker_t *app_wrk = va_arg (*args, app_worker_t *);
670 u64 handle = va_arg (*args, u64);
671 u32 sm_index = va_arg (*args, u32);
672 int verbose = va_arg (*args, int);
673 session_t *listener;
674 const u8 *app_name;
675 u8 *str;
676
677 if (!app_wrk)
678 {
679 if (verbose)
680 s = format (s, "%-40s%-25s%=10s%-15s%-15s%-10s", "Connection", "App",
681 "Wrk", "API Client", "ListenerID", "SegManager");
682 else
683 s = format (s, "%-40s%-25s%=10s", "Connection", "App", "Wrk");
684
685 return s;
686 }
687
688 app_name = application_name_from_index (app_wrk->app_index);
689 listener = listen_session_get_from_handle (handle);
Florin Coras31c99552019-03-01 13:00:58 -0800690 str = format (0, "%U", format_session, listener, verbose);
Florin Coras623eb562019-02-03 19:28:34 -0800691
692 if (verbose)
693 {
694 char buf[32];
695 sprintf (buf, "%u(%u)", app_wrk->wrk_map_index, app_wrk->wrk_index);
696 s = format (s, "%-40s%-25s%=10s%-15u%-15u%-10u", str, app_name,
697 buf, app_wrk->api_client_index, handle, sm_index);
698 }
699 else
700 s = format (s, "%-40s%-25s%=10u", str, app_name, app_wrk->wrk_map_index);
701
702 return s;
703}
704
705u8 *
706format_app_worker (u8 * s, va_list * args)
707{
708 app_worker_t *app_wrk = va_arg (*args, app_worker_t *);
709 u32 indent = 1;
710
711 s = format (s, "%U wrk-index %u app-index %u map-index %u "
712 "api-client-index %d\n", format_white_space, indent,
713 app_wrk->wrk_index, app_wrk->app_index, app_wrk->wrk_map_index,
714 app_wrk->api_client_index);
715 return s;
716}
717
718void
719app_worker_format_connects (app_worker_t * app_wrk, int verbose)
720{
Florin Coras623eb562019-02-03 19:28:34 -0800721 segment_manager_t *sm;
Florin Coras623eb562019-02-03 19:28:34 -0800722
723 /* Header */
724 if (!app_wrk)
725 {
Florin Coras88001c62019-04-24 14:44:46 -0700726 segment_manager_format_sessions (0, verbose);
Florin Coras623eb562019-02-03 19:28:34 -0800727 return;
728 }
729
730 if (app_wrk->connects_seg_manager == (u32) ~ 0)
731 return;
732
Florin Coras623eb562019-02-03 19:28:34 -0800733 sm = segment_manager_get (app_wrk->connects_seg_manager);
Florin Coras88001c62019-04-24 14:44:46 -0700734 segment_manager_format_sessions (sm, verbose);
Florin Coras623eb562019-02-03 19:28:34 -0800735}
736
Florin Coras623eb562019-02-03 19:28:34 -0800737/*
738 * fd.io coding-style-patch-verification: ON
739 *
740 * Local Variables:
741 * eval: (c-set-style "gnu")
742 * End:
743 */