blob: 662c65ba05b1609c9edfd1c223d1943669674fc0 [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;
Florin Coras623eb562019-02-03 19:28:34 -080036 APP_DBG ("New app %v worker %u", app_get_name (app), app_wrk->wrk_index);
37 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;
61 u32 sm_index;
62 int i;
Florin Corasc9940fc2019-02-05 20:55:11 -080063 app_listener_t *al;
64 session_t *ls;
Florin Coras623eb562019-02-03 19:28:34 -080065
66 /*
67 * Listener cleanup
68 */
69
70 /* *INDENT-OFF* */
Florin Corasc9940fc2019-02-05 20:55:11 -080071 hash_foreach (handle, sm_index, app_wrk->listeners_table, ({
72 ls = listen_session_get_from_handle (handle);
73 al = app_listener_get (app, ls->al_index);
74 vec_add1 (handles, app_listener_handle (al));
Florin Coras623eb562019-02-03 19:28:34 -080075 sm = segment_manager_get (sm_index);
76 sm->app_wrk_index = SEGMENT_MANAGER_INVALID_APP_INDEX;
77 }));
78 /* *INDENT-ON* */
79
80 for (i = 0; i < vec_len (handles); i++)
81 {
82 a->app_index = app->app_index;
83 a->wrk_map_index = app_wrk->wrk_map_index;
84 a->handle = handles[i];
85 /* seg manager is removed when unbind completes */
Florin Corasc1a42652019-02-08 18:27:29 -080086 (void) vnet_unlisten (a);
Florin Coras623eb562019-02-03 19:28:34 -080087 }
88
89 /*
90 * Connects segment manager cleanup
91 */
92
93 if (app_wrk->connects_seg_manager != APP_INVALID_SEGMENT_MANAGER_INDEX)
94 {
95 sm = segment_manager_get (app_wrk->connects_seg_manager);
96 sm->app_wrk_index = SEGMENT_MANAGER_INVALID_APP_INDEX;
Florin Coras565115e2019-02-20 19:48:31 -080097 sm->first_is_protected = 0;
Florin Coras623eb562019-02-03 19:28:34 -080098 segment_manager_init_del (sm);
99 }
100
101 /* If first segment manager is used by a listener */
102 if (app_wrk->first_segment_manager != APP_INVALID_SEGMENT_MANAGER_INDEX
103 && app_wrk->first_segment_manager != app_wrk->connects_seg_manager)
104 {
105 sm = segment_manager_get (app_wrk->first_segment_manager);
106 sm->first_is_protected = 0;
107 sm->app_wrk_index = SEGMENT_MANAGER_INVALID_APP_INDEX;
108 /* .. and has no fifos, e.g. it might be used for redirected sessions,
109 * remove it */
110 if (!segment_manager_has_fifos (sm))
111 segment_manager_del (sm);
112 }
113
Florin Coras623eb562019-02-03 19:28:34 -0800114 pool_put (app_workers, app_wrk);
115 if (CLIB_DEBUG)
116 clib_memset (app_wrk, 0xfe, sizeof (*app_wrk));
117}
118
119application_t *
120app_worker_get_app (u32 wrk_index)
121{
122 app_worker_t *app_wrk;
123 app_wrk = app_worker_get_if_valid (wrk_index);
124 if (!app_wrk)
125 return 0;
126 return application_get_if_valid (app_wrk->app_index);
127}
128
129static segment_manager_t *
130app_worker_alloc_segment_manager (app_worker_t * app_wrk)
131{
132 segment_manager_t *sm = 0;
133
134 /* If the first segment manager is not in use, don't allocate a new one */
135 if (app_wrk->first_segment_manager != APP_INVALID_SEGMENT_MANAGER_INDEX
136 && app_wrk->first_segment_manager_in_use == 0)
137 {
138 sm = segment_manager_get (app_wrk->first_segment_manager);
139 app_wrk->first_segment_manager_in_use = 1;
140 return sm;
141 }
142
143 sm = segment_manager_new ();
144 sm->app_wrk_index = app_wrk->wrk_index;
145
146 return sm;
147}
148
Florin Corasa27a46e2019-02-18 13:02:28 -0800149static int
150app_worker_alloc_session_fifos (segment_manager_t * sm, session_t * s)
151{
152 svm_fifo_t *rx_fifo = 0, *tx_fifo = 0;
153 u32 fifo_segment_index;
154 int rv;
155
156 if ((rv = segment_manager_alloc_session_fifos (sm, &rx_fifo, &tx_fifo,
157 &fifo_segment_index)))
158 return rv;
159
160 rx_fifo->master_session_index = s->session_index;
161 rx_fifo->master_thread_index = s->thread_index;
162
163 tx_fifo->master_session_index = s->session_index;
164 tx_fifo->master_thread_index = s->thread_index;
165
166 s->rx_fifo = rx_fifo;
167 s->tx_fifo = tx_fifo;
168 s->svm_segment_index = fifo_segment_index;
169 return 0;
170}
171
Florin Coras623eb562019-02-03 19:28:34 -0800172int
Florin Corasd4295e62019-02-22 13:11:38 -0800173app_worker_init_listener (app_worker_t * app_wrk, session_t * ls)
Florin Coras623eb562019-02-03 19:28:34 -0800174{
175 segment_manager_t *sm;
176
177 /* Allocate segment manager. All sessions derived out of a listen session
178 * have fifos allocated by the same segment manager. */
179 if (!(sm = app_worker_alloc_segment_manager (app_wrk)))
180 return -1;
181
Florin Corasc9940fc2019-02-05 20:55:11 -0800182 /* Keep track of the segment manager for the listener or this worker */
Florin Coras623eb562019-02-03 19:28:34 -0800183 hash_set (app_wrk->listeners_table, listen_session_get_handle (ls),
184 segment_manager_index (sm));
185
Florin Corasc9940fc2019-02-05 20:55:11 -0800186 if (session_transport_service_type (ls) == TRANSPORT_SERVICE_CL)
Florin Coras623eb562019-02-03 19:28:34 -0800187 {
Florin Corasa27a46e2019-02-18 13:02:28 -0800188 if (!ls->rx_fifo && app_worker_alloc_session_fifos (sm, ls))
Florin Coras623eb562019-02-03 19:28:34 -0800189 return -1;
190 }
191 return 0;
192}
193
194int
Florin Corasd4295e62019-02-22 13:11:38 -0800195app_worker_start_listen (app_worker_t * app_wrk,
196 app_listener_t * app_listener)
197{
198 session_t *ls;
199
200 if (clib_bitmap_get (app_listener->workers, app_wrk->wrk_map_index))
201 return VNET_API_ERROR_ADDRESS_IN_USE;
202
203 app_listener->workers = clib_bitmap_set (app_listener->workers,
204 app_wrk->wrk_map_index, 1);
205
206 if (app_listener->session_index != SESSION_INVALID_INDEX)
207 {
208 ls = session_get (app_listener->session_index, 0);
209 if (app_worker_init_listener (app_wrk, ls))
210 return -1;
211 }
212
213 if (app_listener->local_index != SESSION_INVALID_INDEX)
214 {
215 ls = session_get (app_listener->local_index, 0);
216 if (app_worker_init_listener (app_wrk, ls))
217 return -1;
218 }
219
220 return 0;
221}
222
Florin Coras2b81e3c2019-02-27 07:55:46 -0800223static void
224app_worker_stop_listen_session (app_worker_t * app_wrk, session_t * ls)
Florin Coras623eb562019-02-03 19:28:34 -0800225{
Florin Corasc9940fc2019-02-05 20:55:11 -0800226 session_handle_t handle;
Florin Coras623eb562019-02-03 19:28:34 -0800227 segment_manager_t *sm;
228 uword *sm_indexp;
Florin Coras2b81e3c2019-02-27 07:55:46 -0800229
230 handle = listen_session_get_handle (ls);
231 sm_indexp = hash_get (app_wrk->listeners_table, handle);
232 if (PREDICT_FALSE (!sm_indexp))
233 return;
234
235 sm = segment_manager_get (*sm_indexp);
236 if (app_wrk->first_segment_manager == *sm_indexp)
237 {
238 /* Delete sessions but don't remove segment manager */
239 app_wrk->first_segment_manager_in_use = 0;
240 segment_manager_del_sessions (sm);
241 }
242 else
243 {
244 segment_manager_init_del (sm);
245 }
246 hash_unset (app_wrk->listeners_table, handle);
247}
248
249int
250app_worker_stop_listen (app_worker_t * app_wrk, app_listener_t * al)
251{
Florin Corasd4295e62019-02-22 13:11:38 -0800252 session_t *ls;
Florin Coras623eb562019-02-03 19:28:34 -0800253
Florin Corasc9940fc2019-02-05 20:55:11 -0800254 if (!clib_bitmap_get (al->workers, app_wrk->wrk_map_index))
255 return 0;
256
257 if (al->session_index != SESSION_INVALID_INDEX)
Florin Coras623eb562019-02-03 19:28:34 -0800258 {
Florin Corasc9940fc2019-02-05 20:55:11 -0800259 ls = listen_session_get (al->session_index);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800260 app_worker_stop_listen_session (app_wrk, ls);
Florin Coras623eb562019-02-03 19:28:34 -0800261 }
262
Florin Corasc9940fc2019-02-05 20:55:11 -0800263 if (al->local_index != SESSION_INVALID_INDEX)
Florin Coras623eb562019-02-03 19:28:34 -0800264 {
Florin Corasd4295e62019-02-22 13:11:38 -0800265 ls = listen_session_get (al->local_index);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800266 app_worker_stop_listen_session (app_wrk, ls);
Florin Coras623eb562019-02-03 19:28:34 -0800267 }
Florin Corasc9940fc2019-02-05 20:55:11 -0800268
269 clib_bitmap_set_no_check (al->workers, app_wrk->wrk_map_index, 0);
270 if (clib_bitmap_is_zero (al->workers))
271 app_listener_cleanup (al);
Florin Coras623eb562019-02-03 19:28:34 -0800272
273 return 0;
274}
275
276int
Florin Corasa27a46e2019-02-18 13:02:28 -0800277app_worker_init_accepted (session_t * s)
278{
279 app_worker_t *app_wrk;
280 segment_manager_t *sm;
281 session_t *listener;
282
283 listener = listen_session_get (s->listener_index);
284 app_wrk = application_listener_select_worker (listener);
285 s->app_wrk_index = app_wrk->wrk_index;
Florin Coras2b81e3c2019-02-27 07:55:46 -0800286
Florin Corasa27a46e2019-02-18 13:02:28 -0800287 sm = app_worker_get_listen_segment_manager (app_wrk, listener);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800288 if (app_worker_alloc_session_fifos (sm, s))
289 return -1;
290
291 return 0;
Florin Corasa27a46e2019-02-18 13:02:28 -0800292}
293
294int
295app_worker_accept_notify (app_worker_t * app_wrk, session_t * s)
296{
297 application_t *app = application_get (app_wrk->app_index);
298 return app->cb_fns.session_accept_callback (s);
299}
300
301int
302app_worker_init_connected (app_worker_t * app_wrk, session_t * s)
303{
304 application_t *app = application_get (app_wrk->app_index);
305 segment_manager_t *sm;
306
307 /* Allocate fifos for session, unless the app is a builtin proxy */
308 if (!application_is_builtin_proxy (app))
309 {
310 sm = app_worker_get_connect_segment_manager (app_wrk);
311 if (app_worker_alloc_session_fifos (sm, s))
312 return -1;
313 }
314 return 0;
315}
316
317int
318app_worker_connect_notify (app_worker_t * app_wrk, session_t * s, u32 opaque)
319{
320 application_t *app = application_get (app_wrk->app_index);
321 return app->cb_fns.session_connected_callback (app_wrk->wrk_index, opaque,
322 s, s == 0 /* is_fail */ );
323}
324
325int
Florin Coras623eb562019-02-03 19:28:34 -0800326app_worker_own_session (app_worker_t * app_wrk, session_t * s)
327{
328 segment_manager_t *sm;
329 svm_fifo_t *rxf, *txf;
330
331 if (s->session_state == SESSION_STATE_LISTENING)
332 return application_change_listener_owner (s, app_wrk);
333
334 s->app_wrk_index = app_wrk->wrk_index;
335
336 rxf = s->rx_fifo;
337 txf = s->tx_fifo;
338
339 if (!rxf || !txf)
340 return 0;
341
342 s->rx_fifo = 0;
343 s->tx_fifo = 0;
344
345 sm = app_worker_get_or_alloc_connect_segment_manager (app_wrk);
Florin Corasa27a46e2019-02-18 13:02:28 -0800346 if (app_worker_alloc_session_fifos (sm, s))
Florin Coras623eb562019-02-03 19:28:34 -0800347 return -1;
348
349 if (!svm_fifo_is_empty (rxf))
350 {
351 clib_memcpy_fast (s->rx_fifo->data, rxf->data, rxf->nitems);
352 s->rx_fifo->head = rxf->head;
353 s->rx_fifo->tail = rxf->tail;
354 s->rx_fifo->cursize = rxf->cursize;
355 }
356
357 if (!svm_fifo_is_empty (txf))
358 {
359 clib_memcpy_fast (s->tx_fifo->data, txf->data, txf->nitems);
360 s->tx_fifo->head = txf->head;
361 s->tx_fifo->tail = txf->tail;
362 s->tx_fifo->cursize = txf->cursize;
363 }
364
365 segment_manager_dealloc_fifos (rxf->segment_index, rxf, txf);
366
367 return 0;
368}
369
370int
Florin Corasc9940fc2019-02-05 20:55:11 -0800371app_worker_connect_session (app_worker_t * app, session_endpoint_t * sep,
372 u32 api_context)
Florin Coras623eb562019-02-03 19:28:34 -0800373{
374 int rv;
375
376 /* Make sure we have a segment manager for connects */
377 app_worker_alloc_connects_segment_manager (app);
378
379 if ((rv = session_open (app->wrk_index, sep, api_context)))
380 return rv;
381
382 return 0;
383}
384
385int
386app_worker_alloc_connects_segment_manager (app_worker_t * app_wrk)
387{
388 segment_manager_t *sm;
389
390 if (app_wrk->connects_seg_manager == APP_INVALID_SEGMENT_MANAGER_INDEX)
391 {
392 sm = app_worker_alloc_segment_manager (app_wrk);
393 if (sm == 0)
394 return -1;
395 app_wrk->connects_seg_manager = segment_manager_index (sm);
396 }
397 return 0;
398}
399
400segment_manager_t *
401app_worker_get_connect_segment_manager (app_worker_t * app)
402{
403 ASSERT (app->connects_seg_manager != (u32) ~ 0);
404 return segment_manager_get (app->connects_seg_manager);
405}
406
407segment_manager_t *
408app_worker_get_or_alloc_connect_segment_manager (app_worker_t * app_wrk)
409{
410 if (app_wrk->connects_seg_manager == (u32) ~ 0)
411 app_worker_alloc_connects_segment_manager (app_wrk);
412 return segment_manager_get (app_wrk->connects_seg_manager);
413}
414
415segment_manager_t *
416app_worker_get_listen_segment_manager (app_worker_t * app,
417 session_t * listener)
418{
419 uword *smp;
420 smp = hash_get (app->listeners_table, listen_session_get_handle (listener));
421 ASSERT (smp != 0);
422 return segment_manager_get (*smp);
423}
424
425session_t *
Florin Corasc9940fc2019-02-05 20:55:11 -0800426app_worker_first_listener (app_worker_t * app_wrk, u8 fib_proto,
Florin Coras623eb562019-02-03 19:28:34 -0800427 u8 transport_proto)
428{
429 session_t *listener;
430 u64 handle;
431 u32 sm_index;
432 u8 sst;
433
434 sst = session_type_from_proto_and_ip (transport_proto,
435 fib_proto == FIB_PROTOCOL_IP4);
436
437 /* *INDENT-OFF* */
Florin Corasc9940fc2019-02-05 20:55:11 -0800438 hash_foreach (handle, sm_index, app_wrk->listeners_table, ({
Florin Coras623eb562019-02-03 19:28:34 -0800439 listener = listen_session_get_from_handle (handle);
440 if (listener->session_type == sst
441 && listener->enqueue_epoch != SESSION_PROXY_LISTENER_INDEX)
442 return listener;
443 }));
444 /* *INDENT-ON* */
445
446 return 0;
447}
448
449session_t *
Florin Corasc9940fc2019-02-05 20:55:11 -0800450app_worker_proxy_listener (app_worker_t * app_wrk, u8 fib_proto,
Florin Coras623eb562019-02-03 19:28:34 -0800451 u8 transport_proto)
452{
453 session_t *listener;
454 u64 handle;
455 u32 sm_index;
456 u8 sst;
457
458 sst = session_type_from_proto_and_ip (transport_proto,
459 fib_proto == FIB_PROTOCOL_IP4);
460
461 /* *INDENT-OFF* */
Florin Corasc9940fc2019-02-05 20:55:11 -0800462 hash_foreach (handle, sm_index, app_wrk->listeners_table, ({
Florin Coras623eb562019-02-03 19:28:34 -0800463 listener = listen_session_get_from_handle (handle);
464 if (listener->session_type == sst
465 && listener->enqueue_epoch == SESSION_PROXY_LISTENER_INDEX)
466 return listener;
467 }));
468 /* *INDENT-ON* */
469
470 return 0;
471}
472
473/**
474 * Send an API message to the external app, to map new segment
475 */
476int
Florin Coras2b81e3c2019-02-27 07:55:46 -0800477app_worker_add_segment_notify (app_worker_t * app_wrk, u64 segment_handle)
Florin Coras623eb562019-02-03 19:28:34 -0800478{
Florin Coras623eb562019-02-03 19:28:34 -0800479 application_t *app = application_get (app_wrk->app_index);
480 return app->cb_fns.add_segment_callback (app_wrk->api_client_index,
481 segment_handle);
482}
483
Florin Coras2b81e3c2019-02-27 07:55:46 -0800484int
485app_worker_del_segment_notify (app_worker_t * app_wrk, u64 segment_handle)
486{
487 application_t *app = application_get (app_wrk->app_index);
488 return app->cb_fns.del_segment_callback (app_wrk->api_client_index,
489 segment_handle);
490}
491
Florin Coras31c99552019-03-01 13:00:58 -0800492static inline u8
Florin Coras623eb562019-02-03 19:28:34 -0800493app_worker_application_is_builtin (app_worker_t * app_wrk)
494{
495 return app_wrk->app_is_builtin;
496}
497
498static inline int
499app_enqueue_evt (svm_msg_q_t * mq, svm_msg_q_msg_t * msg, u8 lock)
500{
501 if (PREDICT_FALSE (svm_msg_q_is_full (mq)))
502 {
503 clib_warning ("evt q full");
504 svm_msg_q_free_msg (mq, msg);
505 if (lock)
506 svm_msg_q_unlock (mq);
507 return -1;
508 }
509
510 if (lock)
511 {
512 svm_msg_q_add_and_unlock (mq, msg);
513 return 0;
514 }
515
516 /* Even when not locking the ring, we must wait for queue mutex */
517 if (svm_msg_q_add (mq, msg, SVM_Q_WAIT))
518 {
519 clib_warning ("msg q add returned");
520 return -1;
521 }
522 return 0;
523}
524
525static inline int
526app_send_io_evt_rx (app_worker_t * app_wrk, session_t * s, u8 lock)
527{
528 session_event_t *evt;
529 svm_msg_q_msg_t msg;
530 svm_msg_q_t *mq;
531
532 if (PREDICT_FALSE (s->session_state != SESSION_STATE_READY
533 && s->session_state != SESSION_STATE_LISTENING))
534 {
535 /* Session is closed so app will never clean up. Flush rx fifo */
536 if (s->session_state == SESSION_STATE_CLOSED)
537 svm_fifo_dequeue_drop_all (s->rx_fifo);
538 return 0;
539 }
540
541 if (app_worker_application_is_builtin (app_wrk))
542 {
543 application_t *app = application_get (app_wrk->app_index);
544 return app->cb_fns.builtin_app_rx_callback (s);
545 }
546
547 if (svm_fifo_has_event (s->rx_fifo) || svm_fifo_is_empty (s->rx_fifo))
548 return 0;
549
550 mq = app_wrk->event_queue;
551 if (lock)
552 svm_msg_q_lock (mq);
553
554 if (PREDICT_FALSE (svm_msg_q_ring_is_full (mq, SESSION_MQ_IO_EVT_RING)))
555 {
556 clib_warning ("evt q rings full");
557 if (lock)
558 svm_msg_q_unlock (mq);
559 return -1;
560 }
561
562 msg = svm_msg_q_alloc_msg_w_ring (mq, SESSION_MQ_IO_EVT_RING);
563 ASSERT (!svm_msg_q_msg_is_invalid (&msg));
564
565 evt = (session_event_t *) svm_msg_q_msg_data (mq, &msg);
566 evt->fifo = s->rx_fifo;
Florin Corasf6c43132019-03-01 12:41:21 -0800567 evt->event_type = SESSION_IO_EVT_RX;
Florin Coras623eb562019-02-03 19:28:34 -0800568
569 (void) svm_fifo_set_event (s->rx_fifo);
570
571 if (app_enqueue_evt (mq, &msg, lock))
572 return -1;
573 return 0;
574}
575
576static inline int
577app_send_io_evt_tx (app_worker_t * app_wrk, session_t * s, u8 lock)
578{
579 svm_msg_q_t *mq;
580 session_event_t *evt;
581 svm_msg_q_msg_t msg;
582
583 if (app_worker_application_is_builtin (app_wrk))
584 return 0;
585
586 mq = app_wrk->event_queue;
587 if (lock)
588 svm_msg_q_lock (mq);
589
590 if (PREDICT_FALSE (svm_msg_q_ring_is_full (mq, SESSION_MQ_IO_EVT_RING)))
591 {
592 clib_warning ("evt q rings full");
593 if (lock)
594 svm_msg_q_unlock (mq);
595 return -1;
596 }
597
598 msg = svm_msg_q_alloc_msg_w_ring (mq, SESSION_MQ_IO_EVT_RING);
599 ASSERT (!svm_msg_q_msg_is_invalid (&msg));
600
601 evt = (session_event_t *) svm_msg_q_msg_data (mq, &msg);
Florin Corasf6c43132019-03-01 12:41:21 -0800602 evt->event_type = SESSION_IO_EVT_TX;
Florin Coras623eb562019-02-03 19:28:34 -0800603 evt->fifo = s->tx_fifo;
604
605 return app_enqueue_evt (mq, &msg, lock);
606}
607
608/* *INDENT-OFF* */
609typedef int (app_send_evt_handler_fn) (app_worker_t *app,
610 session_t *s,
611 u8 lock);
612static app_send_evt_handler_fn * const app_send_evt_handler_fns[3] = {
613 app_send_io_evt_rx,
614 0,
615 app_send_io_evt_tx,
616};
617/* *INDENT-ON* */
618
619/**
620 * Send event to application
621 *
622 * Logic from queue perspective is non-blocking. If there's
623 * not enough space to enqueue a message, we return.
624 */
625int
626app_worker_send_event (app_worker_t * app, session_t * s, u8 evt_type)
627{
Florin Corasf6c43132019-03-01 12:41:21 -0800628 ASSERT (app && evt_type <= SESSION_IO_EVT_TX);
Florin Coras623eb562019-02-03 19:28:34 -0800629 return app_send_evt_handler_fns[evt_type] (app, s, 0 /* lock */ );
630}
631
632/**
633 * Send event to application
634 *
635 * Logic from queue perspective is blocking. However, if queue is full,
636 * we return.
637 */
638int
639app_worker_lock_and_send_event (app_worker_t * app, session_t * s,
640 u8 evt_type)
641{
642 return app_send_evt_handler_fns[evt_type] (app, s, 1 /* lock */ );
643}
644
Florin Coras623eb562019-02-03 19:28:34 -0800645u8 *
646format_app_worker_listener (u8 * s, va_list * args)
647{
648 app_worker_t *app_wrk = va_arg (*args, app_worker_t *);
649 u64 handle = va_arg (*args, u64);
650 u32 sm_index = va_arg (*args, u32);
651 int verbose = va_arg (*args, int);
652 session_t *listener;
653 const u8 *app_name;
654 u8 *str;
655
656 if (!app_wrk)
657 {
658 if (verbose)
659 s = format (s, "%-40s%-25s%=10s%-15s%-15s%-10s", "Connection", "App",
660 "Wrk", "API Client", "ListenerID", "SegManager");
661 else
662 s = format (s, "%-40s%-25s%=10s", "Connection", "App", "Wrk");
663
664 return s;
665 }
666
667 app_name = application_name_from_index (app_wrk->app_index);
668 listener = listen_session_get_from_handle (handle);
Florin Coras31c99552019-03-01 13:00:58 -0800669 str = format (0, "%U", format_session, listener, verbose);
Florin Coras623eb562019-02-03 19:28:34 -0800670
671 if (verbose)
672 {
673 char buf[32];
674 sprintf (buf, "%u(%u)", app_wrk->wrk_map_index, app_wrk->wrk_index);
675 s = format (s, "%-40s%-25s%=10s%-15u%-15u%-10u", str, app_name,
676 buf, app_wrk->api_client_index, handle, sm_index);
677 }
678 else
679 s = format (s, "%-40s%-25s%=10u", str, app_name, app_wrk->wrk_map_index);
680
681 return s;
682}
683
684u8 *
685format_app_worker (u8 * s, va_list * args)
686{
687 app_worker_t *app_wrk = va_arg (*args, app_worker_t *);
688 u32 indent = 1;
689
690 s = format (s, "%U wrk-index %u app-index %u map-index %u "
691 "api-client-index %d\n", format_white_space, indent,
692 app_wrk->wrk_index, app_wrk->app_index, app_wrk->wrk_map_index,
693 app_wrk->api_client_index);
694 return s;
695}
696
697void
698app_worker_format_connects (app_worker_t * app_wrk, int verbose)
699{
700 svm_fifo_segment_private_t *fifo_segment;
701 vlib_main_t *vm = vlib_get_main ();
702 segment_manager_t *sm;
703 const u8 *app_name;
704 u8 *s = 0;
705
706 /* Header */
707 if (!app_wrk)
708 {
709 if (verbose)
710 vlib_cli_output (vm, "%-40s%-20s%-15s%-10s", "Connection", "App",
711 "API Client", "SegManager");
712 else
713 vlib_cli_output (vm, "%-40s%-20s", "Connection", "App");
714 return;
715 }
716
717 if (app_wrk->connects_seg_manager == (u32) ~ 0)
718 return;
719
720 app_name = application_name_from_index (app_wrk->app_index);
721
722 /* Across all fifo segments */
723 sm = segment_manager_get (app_wrk->connects_seg_manager);
724
725 /* *INDENT-OFF* */
726 segment_manager_foreach_segment_w_lock (fifo_segment, sm, ({
727 svm_fifo_t *fifo;
728 u8 *str;
729
730 fifo = svm_fifo_segment_get_fifo_list (fifo_segment);
731 while (fifo)
732 {
733 u32 session_index, thread_index;
734 session_t *session;
735
736 session_index = fifo->master_session_index;
737 thread_index = fifo->master_thread_index;
738
739 session = session_get (session_index, thread_index);
Florin Coras31c99552019-03-01 13:00:58 -0800740 str = format (0, "%U", format_session, session, verbose);
Florin Coras623eb562019-02-03 19:28:34 -0800741
742 if (verbose)
743 s = format (s, "%-40s%-20s%-15u%-10u", str, app_name,
744 app_wrk->api_client_index, app_wrk->connects_seg_manager);
745 else
746 s = format (s, "%-40s%-20s", str, app_name);
747
748 vlib_cli_output (vm, "%v", s);
749 vec_reset_length (s);
750 vec_free (str);
751
752 fifo = fifo->next;
753 }
754 vec_free (s);
755 }));
756 /* *INDENT-ON* */
757}
758
Florin Coras623eb562019-02-03 19:28:34 -0800759/*
760 * fd.io coding-style-patch-verification: ON
761 *
762 * Local Variables:
763 * eval: (c-set-style "gnu")
764 * End:
765 */