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