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