Unlink /dev/shm files on application detach.
[vpp.git] / src / vnet / session / application.c
1 /*
2  * Copyright (c) 2017 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>
18 #include <vnet/session/session.h>
19
20 /**
21  * Pool from which we allocate all applications
22  */
23 static application_t *app_pool;
24
25 /**
26  * Hash table of apps by api client index
27  */
28 static uword *app_by_api_client_index;
29
30 /**
31  * Default application event queue size
32  */
33 static u32 default_app_evt_queue_size = 128;
34
35 int
36 application_api_queue_is_full (application_t * app)
37 {
38   unix_shared_memory_queue_t *q;
39
40   /* builtin servers are always OK */
41   if (app->api_client_index == ~0)
42     return 0;
43
44   q = vl_api_client_index_to_input_queue (app->api_client_index);
45   if (!q)
46     return 1;
47
48   if (q->cursize == q->maxsize)
49     return 1;
50   return 0;
51 }
52
53 static void
54 application_table_add (application_t * app)
55 {
56   hash_set (app_by_api_client_index, app->api_client_index, app->index);
57 }
58
59 static void
60 application_table_del (application_t * app)
61 {
62   hash_unset (app_by_api_client_index, app->api_client_index);
63 }
64
65 application_t *
66 application_lookup (u32 api_client_index)
67 {
68   uword *p;
69   p = hash_get (app_by_api_client_index, api_client_index);
70   if (p)
71     return application_get (p[0]);
72
73   return 0;
74 }
75
76 application_t *
77 application_new ()
78 {
79   application_t *app;
80   pool_get (app_pool, app);
81   memset (app, 0, sizeof (*app));
82   app->index = application_get_index (app);
83   app->connects_seg_manager = ~0;
84   app->first_segment_manager = ~0;
85   if (CLIB_DEBUG > 1)
86     clib_warning ("[%d] New app (%d)", getpid (), app->index);
87   return app;
88 }
89
90 void
91 application_del (application_t * app)
92 {
93   segment_manager_t *sm;
94   u64 handle;
95   u32 index, *handles = 0;
96   int i;
97   vnet_unbind_args_t _a, *a = &_a;
98
99   /*
100    * The app event queue allocated in first segment is cleared with
101    * the segment manager. No need to explicitly free it.
102    */
103   if (CLIB_DEBUG > 1)
104     clib_warning ("[%d] Delete app (%d)", getpid (), app->index);
105
106   /*
107    * Cleanup segment managers
108    */
109   if (app->connects_seg_manager != (u32) ~ 0)
110     {
111       sm = segment_manager_get (app->connects_seg_manager);
112       segment_manager_del (sm);
113     }
114
115   /* *INDENT-OFF* */
116   hash_foreach (handle, index, app->listeners_table,
117   ({
118     vec_add1 (handles, handle);
119   }));
120   /* *INDENT-ON* */
121
122   /* Actual listener cleanup */
123   for (i = 0; i < vec_len (handles); i++)
124     {
125       a->app_index = app->index;
126       a->handle = handles[i];
127       /* seg manager is removed when unbind completes */
128       vnet_unbind (a);
129     }
130
131   if (app->first_segment_manager != ~0)
132     {
133       sm = segment_manager_get (app->first_segment_manager);
134       segment_manager_first_segment_maybe_del (sm);
135     }
136
137   application_table_del (app);
138   pool_put (app_pool, app);
139 }
140
141 static void
142 application_verify_cb_fns (session_cb_vft_t * cb_fns)
143 {
144   if (cb_fns->session_accept_callback == 0)
145     clib_warning ("No accept callback function provided");
146   if (cb_fns->session_connected_callback == 0)
147     clib_warning ("No session connected callback function provided");
148   if (cb_fns->session_disconnect_callback == 0)
149     clib_warning ("No session disconnect callback function provided");
150   if (cb_fns->session_reset_callback == 0)
151     clib_warning ("No session reset callback function provided");
152 }
153
154 int
155 application_init (application_t * app, u32 api_client_index, u64 * options,
156                   session_cb_vft_t * cb_fns)
157 {
158   segment_manager_t *sm;
159   segment_manager_properties_t *props;
160   u32 app_evt_queue_size, first_seg_size;
161   int rv;
162
163   app_evt_queue_size = options[APP_EVT_QUEUE_SIZE] > 0 ?
164     options[APP_EVT_QUEUE_SIZE] : default_app_evt_queue_size;
165
166   /* Setup segment manager */
167   sm = segment_manager_new ();
168   sm->app_index = app->index;
169   props = &app->sm_properties;
170   props->add_segment_size = options[SESSION_OPTIONS_ADD_SEGMENT_SIZE];
171   props->rx_fifo_size = options[SESSION_OPTIONS_RX_FIFO_SIZE];
172   props->tx_fifo_size = options[SESSION_OPTIONS_TX_FIFO_SIZE];
173   props->add_segment = props->add_segment_size != 0;
174   props->preallocated_fifo_pairs = options[APP_OPTIONS_PREALLOC_FIFO_PAIRS];
175   props->use_private_segment = options[APP_OPTIONS_FLAGS]
176     & APP_OPTIONS_FLAGS_BUILTIN_APP;
177
178   first_seg_size = options[SESSION_OPTIONS_SEGMENT_SIZE];
179   if ((rv = segment_manager_init (sm, props, first_seg_size)))
180     return rv;
181
182   app->first_segment_manager = segment_manager_index (sm);
183   app->api_client_index = api_client_index;
184   app->flags = options[APP_OPTIONS_FLAGS];
185   app->cb_fns = *cb_fns;
186
187   /* Allocate app event queue in the first shared-memory segment */
188   app->event_queue = segment_manager_alloc_queue (sm, app_evt_queue_size);
189
190   /* Check that the obvious things are properly set up */
191   application_verify_cb_fns (cb_fns);
192
193   /* Add app to lookup by api_client_index table */
194   application_table_add (app);
195
196   return 0;
197 }
198
199 application_t *
200 application_get (u32 index)
201 {
202   return pool_elt_at_index (app_pool, index);
203 }
204
205 application_t *
206 application_get_if_valid (u32 index)
207 {
208   if (pool_is_free_index (app_pool, index))
209     return 0;
210
211   return pool_elt_at_index (app_pool, index);
212 }
213
214 u32
215 application_get_index (application_t * app)
216 {
217   return app - app_pool;
218 }
219
220 static segment_manager_t *
221 application_alloc_segment_manager (application_t * app)
222 {
223   segment_manager_t *sm = 0;
224
225   if (app->first_segment_manager != (u32) ~ 0)
226     {
227       sm = segment_manager_get (app->first_segment_manager);
228       return sm;
229     }
230
231   sm = segment_manager_new ();
232   if (segment_manager_init (sm, &app->sm_properties, 0))
233     return 0;
234   return sm;
235 }
236
237 /**
238  * Start listening local transport endpoint for requested transport.
239  *
240  * Creates a 'dummy' stream session with state LISTENING to be used in session
241  * lookups, prior to establishing connection. Requests transport to build
242  * it's own specific listening connection.
243  */
244 int
245 application_start_listen (application_t * srv, session_type_t session_type,
246                           transport_endpoint_t * tep, u64 * res)
247 {
248   segment_manager_t *sm;
249   stream_session_t *s;
250   u64 handle;
251
252   s = listen_session_new (session_type);
253   s->app_index = srv->index;
254
255   if (stream_session_listen (s, tep))
256     goto err;
257
258   /* Allocate segment manager. All sessions derived out of a listen session
259    * have fifos allocated by the same segment manager. */
260   sm = application_alloc_segment_manager (srv);
261   if (sm == 0)
262     goto err;
263
264   /* Add to app's listener table. Useful to find all child listeners
265    * when app goes down, although, just for unbinding this is not needed */
266   handle = listen_session_get_handle (s);
267   hash_set (srv->listeners_table, handle, segment_manager_index (sm));
268
269   *res = handle;
270   return 0;
271
272 err:
273   listen_session_del (s);
274   return -1;
275 }
276
277 /**
278  * Stop listening on session associated to handle
279  */
280 int
281 application_stop_listen (application_t * srv, u64 handle)
282 {
283   stream_session_t *listener;
284   uword *indexp;
285   segment_manager_t *sm;
286
287   if (srv && hash_get (srv->listeners_table, handle) == 0)
288     {
289       clib_warning ("app doesn't own handle %llu!", handle);
290       return -1;
291     }
292
293   listener = listen_session_get_from_handle (handle);
294   stream_session_stop_listen (listener);
295
296   indexp = hash_get (srv->listeners_table, handle);
297   ASSERT (indexp);
298
299   sm = segment_manager_get (*indexp);
300   segment_manager_del (sm);
301   srv->first_segment_manager = ~0;
302   hash_unset (srv->listeners_table, handle);
303   listen_session_del (listener);
304
305   return 0;
306 }
307
308 int
309 application_open_session (application_t * app, session_type_t sst,
310                           transport_endpoint_t * tep, u32 api_context)
311 {
312   segment_manager_t *sm;
313   transport_connection_t *tc = 0;
314   int rv;
315
316   /* Make sure we have a segment manager for connects */
317   if (app->connects_seg_manager == (u32) ~ 0)
318     {
319       sm = application_alloc_segment_manager (app);
320       if (sm == 0)
321         return -1;
322       app->connects_seg_manager = segment_manager_index (sm);
323     }
324
325   if ((rv = stream_session_open (app->index, sst, tep, &tc)))
326     return rv;
327
328   /* Store api_context for when the reply comes. Not the nicest thing
329    * but better allocating a separate half-open pool.  */
330   tc->s_index = api_context;
331
332   return 0;
333 }
334
335 segment_manager_t *
336 application_get_connect_segment_manager (application_t * app)
337 {
338   ASSERT (app->connects_seg_manager != (u32) ~ 0);
339   return segment_manager_get (app->connects_seg_manager);
340 }
341
342 segment_manager_t *
343 application_get_listen_segment_manager (application_t * app,
344                                         stream_session_t * s)
345 {
346   uword *smp;
347   smp = hash_get (app->listeners_table, listen_session_get_handle (s));
348   ASSERT (smp != 0);
349   return segment_manager_get (*smp);
350 }
351
352 static u8 *
353 app_get_name_from_reg_index (application_t * app)
354 {
355   u8 *app_name;
356
357   vl_api_registration_t *regp;
358   regp = vl_api_client_index_to_registration (app->api_client_index);
359   if (!regp)
360     app_name = format (0, "builtin-%d%c", app->index, 0);
361   else
362     app_name = format (0, "%s%c", regp->name, 0);
363
364   return app_name;
365 }
366
367 u8 *
368 format_application_listener (u8 * s, va_list * args)
369 {
370   application_t *app = va_arg (*args, application_t *);
371   u64 handle = va_arg (*args, u64);
372   u32 index = va_arg (*args, u32);
373   int verbose = va_arg (*args, int);
374   stream_session_t *listener;
375   u8 *app_name, *str;
376
377   if (app == 0)
378     {
379       if (verbose)
380         s = format (s, "%-40s%-20s%-15s%-15s%-10s", "Connection", "App",
381                     "API Client", "ListenerID", "SegManager");
382       else
383         s = format (s, "%-40s%-20s", "Connection", "App");
384
385       return s;
386     }
387
388   app_name = app_get_name_from_reg_index (app);
389   listener = listen_session_get_from_handle (handle);
390   str = format (0, "%U", format_stream_session, listener, verbose);
391
392   if (verbose)
393     {
394       s = format (s, "%-40s%-20s%-15u%-15u%-10u", str, app_name,
395                   app->api_client_index, handle, index);
396     }
397   else
398     s = format (s, "%-40s%-20s", str, app_name);
399
400   vec_free (app_name);
401   return s;
402 }
403
404 void
405 application_format_connects (application_t * app, int verbose)
406 {
407   vlib_main_t *vm = vlib_get_main ();
408   segment_manager_t *sm;
409   u8 *app_name, *s = 0;
410   int j;
411
412   /* Header */
413   if (app == 0)
414     {
415       if (verbose)
416         vlib_cli_output (vm, "%-40s%-20s%-15s%-10s", "Connection", "App",
417                          "API Client", "SegManager");
418       else
419         vlib_cli_output (vm, "%-40s%-20s", "Connection", "App");
420       return;
421     }
422
423   /* make sure */
424   if (app->connects_seg_manager == (u32) ~ 0)
425     return;
426
427   app_name = app_get_name_from_reg_index (app);
428
429   /* Across all fifo segments */
430   sm = segment_manager_get (app->connects_seg_manager);
431   for (j = 0; j < vec_len (sm->segment_indices); j++)
432     {
433       svm_fifo_segment_private_t *fifo_segment;
434       svm_fifo_t *fifo;
435       u8 *str;
436
437       fifo_segment = svm_fifo_get_segment (sm->segment_indices[j]);
438       fifo = svm_fifo_segment_get_fifo_list (fifo_segment);
439       while (fifo)
440         {
441           u32 session_index, thread_index;
442           stream_session_t *session;
443
444           session_index = fifo->master_session_index;
445           thread_index = fifo->master_thread_index;
446
447           session = stream_session_get (session_index, thread_index);
448           str = format (0, "%U", format_stream_session, session, verbose);
449
450           if (verbose)
451             s = format (s, "%-40s%-20s%-15u%-10u", str, app_name,
452                         app->api_client_index, app->connects_seg_manager);
453           else
454             s = format (s, "%-40s%-20s", str, app_name);
455
456           vlib_cli_output (vm, "%v", s);
457           vec_reset_length (s);
458           vec_free (str);
459
460           fifo = fifo->next;
461         }
462       vec_free (s);
463     }
464
465   vec_free (app_name);
466 }
467
468 u8 *
469 format_application (u8 * s, va_list * args)
470 {
471   application_t *app = va_arg (*args, application_t *);
472   CLIB_UNUSED (int verbose) = va_arg (*args, int);
473   u8 *app_name;
474
475   if (app == 0)
476     {
477       if (verbose)
478         s = format (s, "%-10s%-20s%-15s%-15s%-15s%-15s", "Index", "Name",
479                     "API Client", "Add seg size", "Rx fifo size",
480                     "Tx fifo size");
481       else
482         s = format (s, "%-10s%-20s%-20s", "Index", "Name", "API Client");
483       return s;
484     }
485
486   app_name = app_get_name_from_reg_index (app);
487   if (verbose)
488     s = format (s, "%-10d%-20s%-15d%-15d%-15d%-15d", app->index, app_name,
489                 app->api_client_index, app->sm_properties.add_segment_size,
490                 app->sm_properties.rx_fifo_size,
491                 app->sm_properties.tx_fifo_size);
492   else
493     s = format (s, "%-10d%-20s%-20d", app->index, app_name,
494                 app->api_client_index);
495   return s;
496 }
497
498 static clib_error_t *
499 show_app_command_fn (vlib_main_t * vm, unformat_input_t * input,
500                      vlib_cli_command_t * cmd)
501 {
502   application_t *app;
503   int do_server = 0;
504   int do_client = 0;
505   int verbose = 0;
506
507   if (!session_manager_is_enabled ())
508     {
509       clib_error_return (0, "session layer is not enabled");
510     }
511
512   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
513     {
514       if (unformat (input, "server"))
515         do_server = 1;
516       else if (unformat (input, "client"))
517         do_client = 1;
518       else if (unformat (input, "verbose"))
519         verbose = 1;
520       else
521         break;
522     }
523
524   if (do_server)
525     {
526       u64 handle;
527       u32 index;
528       if (pool_elts (app_pool))
529         {
530           vlib_cli_output (vm, "%U", format_application_listener,
531                            0 /* header */ , 0, 0,
532                            verbose);
533           /* *INDENT-OFF* */
534           pool_foreach (app, app_pool,
535           ({
536             /* App's listener sessions */
537             if (hash_elts (app->listeners_table) == 0)
538               continue;
539             hash_foreach (handle, index, app->listeners_table,
540             ({
541               vlib_cli_output (vm, "%U", format_application_listener, app,
542                                        handle, index, verbose);
543             }));
544           }));
545           /* *INDENT-ON* */
546         }
547       else
548         vlib_cli_output (vm, "No active server bindings");
549     }
550
551   if (do_client)
552     {
553       if (pool_elts (app_pool))
554         {
555           application_format_connects (0, verbose);
556
557           /* *INDENT-OFF* */
558           pool_foreach (app, app_pool,
559           ({
560             if (app->connects_seg_manager == (u32)~0)
561               continue;
562             application_format_connects (app, verbose);
563           }));
564           /* *INDENT-ON* */
565         }
566       else
567         vlib_cli_output (vm, "No active client bindings");
568     }
569
570   /* Print app related info */
571   if (!do_server && !do_client)
572     {
573       vlib_cli_output (vm, "%U", format_application, 0, verbose);
574       pool_foreach (app, app_pool, (
575                                      {
576                                      vlib_cli_output (vm, "%U",
577                                                       format_application, app,
578                                                       verbose);
579                                      }
580                     ));
581     }
582
583   return 0;
584 }
585
586 /* *INDENT-OFF* */
587 VLIB_CLI_COMMAND (show_app_command, static) =
588 {
589   .path = "show app",
590   .short_help = "show app [server|client] [verbose]",
591   .function = show_app_command_fn,
592 };
593 /* *INDENT-ON* */
594
595 /*
596  * fd.io coding-style-patch-verification: ON
597  *
598  * Local Variables:
599  * eval: (c-set-style "gnu")
600  * End:
601  */