FD.io VPP  v19.04.4-rc0-5-ge88582fac
Vector Packet Processing
memory_api.c
Go to the documentation of this file.
1 /*
2  *------------------------------------------------------------------
3  * Copyright (c) 2018 Cisco and/or its affiliates.
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *------------------------------------------------------------------
16  */
17 #include <signal.h>
18 
19 #include <vlib/vlib.h>
20 #include <vlibapi/api.h>
21 #include <vlibmemory/api.h>
22 #include <vlibmemory/memory_api.h>
23 
24 #include <vlibmemory/vl_memory_msg_enum.h> /* enumerate all vlib messages */
25 
26 #define vl_typedefs /* define message structures */
28 #undef vl_typedefs
29 
30 /* instantiate all the print functions we know about */
31 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
32 #define vl_printfun
34 #undef vl_printfun
35 
36 /* instantiate all the endian swap functions we know about */
37 #define vl_endianfun
39 #undef vl_endianfun
40 
41 static inline void *
43 {
44  vl_print (handle, "vl_api_memclnt_create_t:\n");
45  vl_print (handle, "name: %s\n", a->name);
46  vl_print (handle, "input_queue: 0x%wx\n", a->input_queue);
47  vl_print (handle, "context: %u\n", (unsigned) a->context);
48  vl_print (handle, "ctx_quota: %ld\n", (long) a->ctx_quota);
49  return handle;
50 }
51 
52 static inline void *
54 {
55  vl_print (handle, "vl_api_memclnt_delete_t:\n");
56  vl_print (handle, "index: %u\n", (unsigned) a->index);
57  vl_print (handle, "handle: 0x%wx\n", a->handle);
58  return handle;
59 }
60 
61 volatile int **vl_api_queue_cursizes;
62 
63 static void
65 {
66  int i;
67  api_main_t *am = &api_main;
68 
70  1 + vec_len (am->vlib_private_rps)))
71  {
72  vl_shmem_hdr_t *shmem_hdr = am->shmem_hdr;
73  svm_queue_t *q;
74 
75  if (shmem_hdr == 0)
76  return;
77 
78  q = shmem_hdr->vl_input_queue;
79  if (q == 0)
80  return;
81 
82  vec_add1 (vl_api_queue_cursizes, &q->cursize);
83 
84  for (i = 0; i < vec_len (am->vlib_private_rps); i++)
85  {
86  svm_region_t *vlib_rp = am->vlib_private_rps[i];
87 
88  shmem_hdr = (void *) vlib_rp->user_ctx;
89  q = shmem_hdr->vl_input_queue;
90  vec_add1 (vl_api_queue_cursizes, &q->cursize);
91  }
92  }
93 
94  for (i = 0; i < vec_len (vl_api_queue_cursizes); i++)
95  {
96  if (*vl_api_queue_cursizes[i])
97  {
98  vm->queue_signal_pending = 1;
99  vm->api_queue_nonempty = 1;
101  /* event_type */ QUEUE_SIGNAL_EVENT,
102  /* event_data */ 0);
103  break;
104  }
105  }
106  if (vec_len (vm->pending_rpc_requests))
107  {
108  vm->queue_signal_pending = 1;
109  vm->api_queue_nonempty = 1;
111  /* event_type */ QUEUE_SIGNAL_EVENT,
112  /* event_data */ 0);
113  }
114 }
115 
116 /*
117  * vl_api_memclnt_create_internal
118  */
119 u32
121 {
122  vl_api_registration_t **regpp;
123  vl_api_registration_t *regp;
124  svm_region_t *svm;
125  void *oldheap;
126  api_main_t *am = &api_main;
127 
128  ASSERT (vlib_get_thread_index () == 0);
129  pool_get (am->vl_clients, regpp);
130 
131  svm = am->vlib_rp;
132 
133  pthread_mutex_lock (&svm->mutex);
134  oldheap = svm_push_data_heap (svm);
135  *regpp = clib_mem_alloc (sizeof (vl_api_registration_t));
136 
137  regp = *regpp;
138  clib_memset (regp, 0, sizeof (*regp));
140  regp->vl_api_registration_pool_index = regpp - am->vl_clients;
141  regp->vlib_rp = svm;
142  regp->shmem_hdr = am->shmem_hdr;
143 
144  regp->vl_input_queue = q;
145  regp->name = format (0, "%s%c", name, 0);
146 
147  pthread_mutex_unlock (&svm->mutex);
148  svm_pop_heap (oldheap);
152 }
153 
154 /*
155  * vl_api_memclnt_create_t_handler
156  */
157 void
159 {
160  vl_api_registration_t **regpp;
161  vl_api_registration_t *regp;
163  svm_region_t *svm;
164  svm_queue_t *q;
165  int rv = 0;
166  void *oldheap;
167  api_main_t *am = &api_main;
168  u8 *msg_table;
169 
170  /*
171  * This is tortured. Maintain a vlib-address-space private
172  * pool of client registrations. We use the shared-memory virtual
173  * address of client structure as a handle, to allow direct
174  * manipulation of context quota vbls from the client library.
175  *
176  * This scheme causes trouble w/ API message trace replay, since
177  * some random VA from clib_mem_alloc() certainly won't
178  * occur in the Linux sim. The (very) few places
179  * that care need to use the pool index.
180  *
181  * Putting the registration object(s) into a pool in shared memory and
182  * using the pool index as a handle seems like a great idea.
183  * Unfortunately, each and every reference to that pool would need
184  * to be protected by a mutex:
185  *
186  * Client VLIB
187  * ------ ----
188  * convert pool index to
189  * pointer.
190  * <deschedule>
191  * expand pool
192  * <deschedule>
193  * kaboom!
194  */
195 
196  pool_get (am->vl_clients, regpp);
197 
198  svm = am->vlib_rp;
199 
200  pthread_mutex_lock (&svm->mutex);
201  oldheap = svm_push_data_heap (svm);
202  *regpp = clib_mem_alloc (sizeof (vl_api_registration_t));
203 
204  regp = *regpp;
205  clib_memset (regp, 0, sizeof (*regp));
207  regp->vl_api_registration_pool_index = regpp - am->vl_clients;
208  regp->vlib_rp = svm;
209  regp->shmem_hdr = am->shmem_hdr;
211 
212  q = regp->vl_input_queue = (svm_queue_t *) (uword) mp->input_queue;
213 
214  regp->name = format (0, "%s", mp->name);
215  vec_add1 (regp->name, 0);
216 
220 
221  if (am->vlib_rp != am->vlib_primary_rp)
222  msg_table = vl_api_serialize_message_table (am, 0);
223  else
224  msg_table = am->serialized_message_table_in_shmem;
225 
226  pthread_mutex_unlock (&svm->mutex);
227  svm_pop_heap (oldheap);
228 
229  rp = vl_msg_api_alloc (sizeof (*rp));
230  rp->_vl_msg_id = ntohs (VL_API_MEMCLNT_CREATE_REPLY);
231  rp->handle = (uword) regp;
235  rp->context = mp->context;
236  rp->response = ntohl (rv);
237  rp->message_table = pointer_to_uword (msg_table);
238 
239  vl_msg_api_send_shmem (q, (u8 *) & rp);
240 }
241 
242 int
244 {
245  clib_error_t *error = 0;
246  _vl_msg_api_function_list_elt_t *i;
247 
249  while (i)
250  {
251  error = i->f (client_index);
252  if (error)
253  clib_error_report (error);
254  i = i->next_init_function;
255  }
256  return 0;
257 }
258 
259 /*
260  * vl_api_memclnt_delete_t_handler
261  */
262 void
264 {
265  vl_api_registration_t **regpp;
266  vl_api_registration_t *regp;
268  svm_region_t *svm;
269  void *oldheap;
270  api_main_t *am = &api_main;
271  u32 handle, client_index, epoch;
272 
273  handle = mp->index;
274 
275  if (vl_api_call_reaper_functions (handle))
276  return;
277 
278  epoch = vl_msg_api_handle_get_epoch (handle);
279  client_index = vl_msg_api_handle_get_index (handle);
280 
281  if (epoch != (am->shmem_hdr->application_restarts & VL_API_EPOCH_MASK))
282  {
284  ("Stale clnt delete index %d old epoch %d cur epoch %d",
285  client_index, epoch,
287  return;
288  }
289 
290  regpp = pool_elt_at_index (am->vl_clients, client_index);
291 
292  if (!pool_is_free (am->vl_clients, regpp))
293  {
294  int i;
295  regp = *regpp;
296  svm = am->vlib_rp;
297  int private_registration = 0;
298 
299  /* Send reply unless client asked us to do the cleanup */
300  if (!mp->do_cleanup)
301  {
302  /*
303  * Note: the API message handling path will set am->vlib_rp
304  * as appropriate for pairwise / private memory segments
305  */
306  rp = vl_msg_api_alloc (sizeof (*rp));
307  rp->_vl_msg_id = ntohs (VL_API_MEMCLNT_DELETE_REPLY);
308  rp->handle = mp->handle;
309  rp->response = 1;
310 
311  vl_msg_api_send_shmem (regp->vl_input_queue, (u8 *) & rp);
312  if (client_index != regp->vl_api_registration_pool_index)
313  {
314  clib_warning ("mismatch client_index %d pool_index %d",
315  client_index,
317  vl_msg_api_free (rp);
318  return;
319  }
320  }
321 
322  /* For horizontal scaling, add a hash table... */
323  for (i = 0; i < vec_len (am->vlib_private_rps); i++)
324  {
325  /* Is this a pairwise / private API segment? */
326  if (am->vlib_private_rps[i] == svm)
327  {
328  /* Note: account for the memfd header page */
329  uword virtual_base = svm->virtual_base - MMAP_PAGESIZE;
330  uword virtual_size = svm->virtual_size + MMAP_PAGESIZE;
331 
332  /*
333  * Kill the registration pool element before we make
334  * the index vanish forever
335  */
338 
339  vec_delete (am->vlib_private_rps, 1, i);
340  /* Kill it, accounting for the memfd header page */
341  if (munmap ((void *) virtual_base, virtual_size) < 0)
342  clib_unix_warning ("munmap");
343  /* Reset the queue-length-address cache */
345  private_registration = 1;
346  break;
347  }
348  }
349 
350  /* No dangling references, please */
351  *regpp = 0;
352 
353  if (private_registration == 0)
354  {
357  pthread_mutex_lock (&svm->mutex);
358  oldheap = svm_push_data_heap (svm);
359  if (mp->do_cleanup)
361  vec_free (regp->name);
362  /* Poison the old registration */
363  clib_memset (regp, 0xF1, sizeof (*regp));
364  clib_mem_free (regp);
365  pthread_mutex_unlock (&svm->mutex);
366  svm_pop_heap (oldheap);
367  /*
368  * These messages must be freed manually, since they're set up
369  * as "bounce" messages. In the private_registration == 1 case,
370  * we kill the shared-memory segment which contains the message
371  * with munmap.
372  */
373  vl_msg_api_free (mp);
374  }
375  }
376  else
377  {
378  clib_warning ("unknown client ID %d", mp->index);
379  }
380 }
381 
382 /**
383  * client answered a ping, stave off the grim reaper...
384  */
385 void
387  (vl_api_memclnt_keepalive_reply_t * mp)
388 {
389  vl_api_registration_t *regp;
391 
392  regp = vl_api_client_index_to_registration (mp->context);
393  if (regp)
394  {
395  regp->last_heard = vlib_time_now (vm);
396  regp->unanswered_pings = 0;
397  }
398  else
399  clib_warning ("BUG: anonymous memclnt_keepalive_reply");
400 }
401 
402 /**
403  * We can send ourselves these messages if someone uses the
404  * builtin binary api test tool...
405  */
406 static void
408 {
409  vl_api_memclnt_keepalive_reply_t *rmp;
410  api_main_t *am;
411  vl_shmem_hdr_t *shmem_hdr;
412 
413  am = &api_main;
414  shmem_hdr = am->shmem_hdr;
415 
416  rmp = vl_msg_api_alloc_as_if_client (sizeof (*rmp));
417  clib_memset (rmp, 0, sizeof (*rmp));
418  rmp->_vl_msg_id = ntohs (VL_API_MEMCLNT_KEEPALIVE_REPLY);
419  rmp->context = mp->context;
420  vl_msg_api_send_shmem (shmem_hdr->vl_input_queue, (u8 *) & rmp);
421 }
422 
423 /*
424  * To avoid filling the API trace buffer with boring messages,
425  * don't trace memclnt_keepalive[_reply] msgs
426  */
427 
428 #define foreach_vlib_api_msg \
429 _(MEMCLNT_CREATE, memclnt_create, 1) \
430 _(MEMCLNT_DELETE, memclnt_delete, 1) \
431 _(MEMCLNT_KEEPALIVE, memclnt_keepalive, 0) \
432 _(MEMCLNT_KEEPALIVE_REPLY, memclnt_keepalive_reply, 0)
433 
434 /*
435  * memory_api_init
436  */
437 int
438 vl_mem_api_init (const char *region_name)
439 {
440  int rv;
441  api_main_t *am = &api_main;
443  vl_msg_api_msg_config_t *c = &cfg;
444  vl_shmem_hdr_t *shm;
446 
447  clib_memset (c, 0, sizeof (*c));
448 
449  if ((rv = vl_map_shmem (region_name, 1 /* is_vlib */ )) < 0)
450  return rv;
451 
452 #define _(N,n,t) do { \
453  c->id = VL_API_##N; \
454  c->name = #n; \
455  c->handler = vl_api_##n##_t_handler; \
456  c->cleanup = vl_noop_handler; \
457  c->endian = vl_api_##n##_t_endian; \
458  c->print = vl_api_##n##_t_print; \
459  c->size = sizeof(vl_api_##n##_t); \
460  c->traced = t; /* trace, so these msgs print */ \
461  c->replay = 0; /* don't replay client create/delete msgs */ \
462  c->message_bounce = 0; /* don't bounce this message */ \
463  vl_msg_api_config(c);} while (0);
464 
466 #undef _
467 
468  /*
469  * special-case freeing of memclnt_delete messages, so we can
470  * simply munmap pairwise / private API segments...
471  */
472  am->message_bounce[VL_API_MEMCLNT_DELETE] = 1;
473  am->is_mp_safe[VL_API_MEMCLNT_KEEPALIVE_REPLY] = 1;
474 
476 
477  shm = am->shmem_hdr;
478  ASSERT (shm && shm->vl_input_queue);
479 
480  /* Make a note so we can always find the primary region easily */
481  am->vlib_primary_rp = am->vlib_rp;
482 
483  return 0;
484 }
485 
486 clib_error_t *
488 {
489  api_main_t *am = &api_main;
490  int rv;
491 
492  if ((rv = vl_mem_api_init (am->region_name)) < 0)
493  {
494  return clib_error_return (0, "vl_mem_api_init (%s) failed",
495  am->region_name);
496  }
497  return 0;
498 }
499 
500 static void
502 {
504  svm_queue_t *q;
505  api_main_t *am = &api_main;
506  svm_region_t *save_vlib_rp = am->vlib_rp;
507  vl_shmem_hdr_t *save_shmem_hdr = am->shmem_hdr;
508 
509  q = regp->vl_input_queue;
510 
511  /*
512  * If the queue head is moving, assume that the client is processing
513  * messages and skip the ping. This heuristic may fail if the queue
514  * is in the same position as last time, net of wrapping; in which
515  * case, the client will receive a keepalive.
516  */
517  if (regp->last_queue_head != q->head)
518  {
519  regp->last_heard = now;
520  regp->unanswered_pings = 0;
521  regp->last_queue_head = q->head;
522  return;
523  }
524 
525  /*
526  * push/pop shared memory segment, so this routine
527  * will work with "normal" as well as "private segment"
528  * memory clients..
529  */
530 
531  am->vlib_rp = regp->vlib_rp;
532  am->shmem_hdr = regp->shmem_hdr;
533 
534  mp = vl_msg_api_alloc (sizeof (*mp));
535  clib_memset (mp, 0, sizeof (*mp));
536  mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_MEMCLNT_KEEPALIVE);
537  mp->context = mp->client_index =
541 
542  regp->unanswered_pings++;
543 
544  /* Failure-to-send due to a stuffed queue is absolutely expected */
545  if (svm_queue_add (q, (u8 *) & mp, 1 /* nowait */ ))
546  vl_msg_api_free (mp);
547 
548  am->vlib_rp = save_vlib_rp;
549  am->shmem_hdr = save_shmem_hdr;
550 }
551 
552 static void
554  vl_api_registration_t ** regpp,
555  u32 ** dead_indices,
556  u32 ** confused_indices)
557 {
558  vl_api_registration_t *regp = *regpp;
559  if (regp)
560  {
561  /* If we haven't heard from this client recently... */
562  if (regp->last_heard < (now - 10.0))
563  {
564  if (regp->unanswered_pings == 2)
565  {
566  svm_queue_t *q;
567  q = regp->vl_input_queue;
568  if (kill (q->consumer_pid, 0) >= 0)
569  {
570  clib_warning ("REAPER: lazy binary API client '%s'",
571  regp->name);
572  regp->unanswered_pings = 0;
573  regp->last_heard = now;
574  }
575  else
576  {
577  clib_warning ("REAPER: binary API client '%s' died",
578  regp->name);
579  vec_add1 (*dead_indices, regpp - am->vl_clients);
580  }
581  }
582  else
583  send_memclnt_keepalive (regp, now);
584  }
585  else
586  regp->unanswered_pings = 0;
587  }
588  else
589  {
590  clib_warning ("NULL client registration index %d",
591  regpp - am->vl_clients);
592  vec_add1 (*confused_indices, regpp - am->vl_clients);
593  }
594 }
595 
596 void
598 {
599  vl_api_registration_t **regpp;
600  static u32 *dead_indices;
601  static u32 *confused_indices;
602 
603  vec_reset_length (dead_indices);
604  vec_reset_length (confused_indices);
605 
606  /* *INDENT-OFF* */
607  pool_foreach (regpp, am->vl_clients, ({
608  vl_mem_send_client_keepalive_w_reg (am, now, regpp, &dead_indices,
609  &confused_indices);
610  }));
611  /* *INDENT-ON* */
612 
613  /* This should "never happen," but if it does, fix it... */
614  if (PREDICT_FALSE (vec_len (confused_indices) > 0))
615  {
616  int i;
617  for (i = 0; i < vec_len (confused_indices); i++)
618  {
619  pool_put_index (am->vl_clients, confused_indices[i]);
620  }
621  }
622 
623  if (PREDICT_FALSE (vec_len (dead_indices) > 0))
624  {
625  int i;
626  svm_region_t *svm;
627  void *oldheap;
628 
629  /* Allow the application to clean up its registrations */
630  for (i = 0; i < vec_len (dead_indices); i++)
631  {
632  regpp = pool_elt_at_index (am->vl_clients, dead_indices[i]);
633  if (regpp)
634  {
635  u32 handle;
636 
638  (dead_indices[i], shm->application_restarts);
639  (void) vl_api_call_reaper_functions (handle);
640  }
641  }
642 
643  svm = am->vlib_rp;
644  pthread_mutex_lock (&svm->mutex);
645  oldheap = svm_push_data_heap (svm);
646 
647  for (i = 0; i < vec_len (dead_indices); i++)
648  {
649  regpp = pool_elt_at_index (am->vl_clients, dead_indices[i]);
650  if (regpp)
651  {
652  /* Is this a pairwise SVM segment? */
653  if ((*regpp)->vlib_rp != svm)
654  {
655  int i;
656  svm_region_t *dead_rp = (*regpp)->vlib_rp;
657  /* Note: account for the memfd header page */
658  uword virtual_base = dead_rp->virtual_base - MMAP_PAGESIZE;
659  uword virtual_size = dead_rp->virtual_size + MMAP_PAGESIZE;
660 
661  /* For horizontal scaling, add a hash table... */
662  for (i = 0; i < vec_len (am->vlib_private_rps); i++)
663  if (am->vlib_private_rps[i] == dead_rp)
664  {
665  vec_delete (am->vlib_private_rps, 1, i);
666  goto found;
667  }
668  clib_warning ("private rp %llx AWOL", dead_rp);
669 
670  found:
671  /* Kill it, accounting for the memfd header page */
672  if (munmap ((void *) virtual_base, virtual_size) < 0)
673  clib_unix_warning ("munmap");
674  /* Reset the queue-length-address cache */
676  }
677  else
678  {
679  /* Poison the old registration */
680  clib_memset (*regpp, 0xF3, sizeof (**regpp));
681  clib_mem_free (*regpp);
682  }
683  /* no dangling references, please */
684  *regpp = 0;
685  }
686  else
687  {
688  svm_pop_heap (oldheap);
689  clib_warning ("Duplicate free, client index %d",
690  regpp - am->vl_clients);
691  oldheap = svm_push_data_heap (svm);
692  }
693  }
694 
696 
697  pthread_mutex_unlock (&svm->mutex);
698  svm_pop_heap (oldheap);
699  for (i = 0; i < vec_len (dead_indices); i++)
700  pool_put_index (am->vl_clients, dead_indices[i]);
701  }
702 }
703 
704 static inline int
706  vlib_node_runtime_t * node, svm_queue_t * q)
707 {
708  uword mp;
709  if (!svm_queue_sub2 (q, (u8 *) & mp))
710  {
711  vl_msg_api_handler_with_vm_node (am, (void *) mp, vm, node);
712  return 0;
713  }
714  return -1;
715 }
716 
717 int
719 {
720  api_main_t *am = &api_main;
721  return void_mem_api_handle_msg_i (am, vm, node,
723 }
724 
725 int
727 {
728  api_main_t *am = &api_main;
729  int i;
730  uword *tmp, mp;
731 
732  /*
733  * Swap pending and processing vectors, then process the RPCs
734  * Avoid deadlock conditions by construction.
735  */
737  tmp = vm->processing_rpc_requests;
738  vec_reset_length (tmp);
740  vm->pending_rpc_requests = tmp;
742 
743  /*
744  * RPCs are used to reflect function calls to thread 0
745  * when the underlying code is not thread-safe.
746  *
747  * Grabbing the thread barrier across a set of RPCs
748  * greatly increases efficiency, and avoids
749  * running afoul of the barrier sync holddown timer.
750  * The barrier sync code supports recursive locking.
751  *
752  * We really need to rewrite RPC-based code...
753  */
755  {
757  for (i = 0; i < vec_len (vm->processing_rpc_requests); i++)
758  {
759  mp = vm->processing_rpc_requests[i];
760  vl_msg_api_handler_with_vm_node (am, (void *) mp, vm, node);
761  }
763  }
764 
765  return 0;
766 }
767 
768 int
770  u32 reg_index)
771 {
772  api_main_t *am = &api_main;
773  vl_shmem_hdr_t *save_shmem_hdr = am->shmem_hdr;
774  svm_region_t *vlib_rp, *save_vlib_rp = am->vlib_rp;
775  svm_queue_t *q;
776  int rv;
777 
778  vlib_rp = am->vlib_rp = am->vlib_private_rps[reg_index];
779 
780  am->shmem_hdr = (void *) vlib_rp->user_ctx;
781  q = am->shmem_hdr->vl_input_queue;
782 
783  rv = void_mem_api_handle_msg_i (am, vm, node, q);
784 
785  am->shmem_hdr = save_shmem_hdr;
786  am->vlib_rp = save_vlib_rp;
787 
788  return rv;
789 }
790 
793 {
794  vl_api_registration_t **regpp;
795  vl_api_registration_t *regp;
796  api_main_t *am = &api_main;
797  vl_shmem_hdr_t *shmem_hdr;
798  u32 index;
799 
800  index = vl_msg_api_handle_get_index (handle);
801  regpp = am->vl_clients + index;
802 
803  if (pool_is_free (am->vl_clients, regpp))
804  {
806  return 0;
807  }
808  regp = *regpp;
809 
810  shmem_hdr = (vl_shmem_hdr_t *) regp->shmem_hdr;
811  if (!vl_msg_api_handle_is_valid (handle, shmem_hdr->application_restarts))
812  {
814  return 0;
815  }
816 
817  return (regp);
818 }
819 
820 svm_queue_t *
822 {
823  vl_api_registration_t *regp;
824  api_main_t *am = &api_main;
825 
826  /* Special case: vlib trying to send itself a message */
827  if (index == (u32) ~ 0)
828  return (am->shmem_hdr->vl_input_queue);
829 
831  if (!regp)
832  return 0;
833  return (regp->vl_input_queue);
834 }
835 
836 static clib_error_t *
838 {
839  atexit (vl_unmap_shmem);
840  return 0;
841 }
842 
844 
845 u8 *
846 format_api_message_rings (u8 * s, va_list * args)
847 {
848  api_main_t *am = va_arg (*args, api_main_t *);
849  vl_shmem_hdr_t *shmem_hdr = va_arg (*args, vl_shmem_hdr_t *);
850  int main_segment = va_arg (*args, int);
851  ring_alloc_t *ap;
852  int i;
853 
854  if (shmem_hdr == 0)
855  return format (s, "%8s %8s %8s %8s %8s\n",
856  "Owner", "Size", "Nitems", "Hits", "Misses");
857 
858  ap = shmem_hdr->vl_rings;
859 
860  for (i = 0; i < vec_len (shmem_hdr->vl_rings); i++)
861  {
862  s = format (s, "%8s %8d %8d %8d %8d\n",
863  "vlib", ap->size, ap->nitems, ap->hits, ap->misses);
864  ap++;
865  }
866 
867  ap = shmem_hdr->client_rings;
868 
869  for (i = 0; i < vec_len (shmem_hdr->client_rings); i++)
870  {
871  s = format (s, "%8s %8d %8d %8d %8d\n",
872  "clnt", ap->size, ap->nitems, ap->hits, ap->misses);
873  ap++;
874  }
875 
876  if (main_segment)
877  {
878  s = format (s, "%d ring miss fallback allocations\n", am->ring_misses);
879  s = format
880  (s,
881  "%d application restarts, %d reclaimed msgs, %d garbage collects\n",
882  shmem_hdr->application_restarts, shmem_hdr->restart_reclaims,
883  shmem_hdr->garbage_collects);
884  }
885  return s;
886 }
887 
888 static clib_error_t *
890  unformat_input_t * input, vlib_cli_command_t * cli_cmd)
891 {
892  int i;
893  vl_shmem_hdr_t *shmem_hdr;
894  api_main_t *am = &api_main;
895 
896  /* First, dump the primary region rings.. */
897 
898  if (am->vlib_primary_rp == 0 || am->vlib_primary_rp->user_ctx == 0)
899  {
900  vlib_cli_output (vm, "Shared memory segment not initialized...\n");
901  return 0;
902  }
903 
904  shmem_hdr = (void *) am->vlib_primary_rp->user_ctx;
905 
906  vlib_cli_output (vm, "Main API segment rings:");
907 
909  0 /* print header */ , 0 /* notused */ );
910 
912  shmem_hdr, 1 /* main segment */ );
913 
914  for (i = 0; i < vec_len (am->vlib_private_rps); i++)
915  {
916  svm_region_t *vlib_rp = am->vlib_private_rps[i];
917  shmem_hdr = (void *) vlib_rp->user_ctx;
918  vl_api_registration_t **regpp;
919  vl_api_registration_t *regp = 0;
920 
921  /* For horizontal scaling, add a hash table... */
922  /* *INDENT-OFF* */
923  pool_foreach (regpp, am->vl_clients,
924  ({
925  regp = *regpp;
926  if (regp && regp->vlib_rp == vlib_rp)
927  {
928  vlib_cli_output (vm, "%s segment rings:", regp->name);
929  goto found;
930  }
931  }));
932  vlib_cli_output (vm, "regp %llx not found?", regp);
933  continue;
934  /* *INDENT-ON* */
935  found:
937  0 /* print header */ , 0 /* notused */ );
939  shmem_hdr, 0 /* main segment */ );
940  }
941 
942  return 0;
943 }
944 
945 /*?
946  * Display binary api message allocation ring statistics
947 ?*/
948 /* *INDENT-OFF* */
949 VLIB_CLI_COMMAND (cli_show_api_ring_command, static) =
950 {
951  .path = "show api ring-stats",
952  .short_help = "Message ring statistics",
953  .function = vl_api_ring_command,
954 };
955 /* *INDENT-ON* */
956 
957 clib_error_t *
959 {
960  api_main_t *am = &api_main;
961  svm_map_region_args_t _a, *a = &_a;
962  clib_error_t *error;
963  u8 *remove_path1, *remove_path2;
964 
965  /*
966  * By popular request / to avoid support fires, remove any old api segment
967  * files Right Here.
968  */
969  if (am->root_path == 0)
970  {
971  remove_path1 = format (0, "/dev/shm/global_vm%c", 0);
972  remove_path2 = format (0, "/dev/shm/vpe-api%c", 0);
973  }
974  else
975  {
976  remove_path1 = format (0, "/dev/shm/%s-global_vm%c", am->root_path, 0);
977  remove_path2 = format (0, "/dev/shm/%s-vpe-api%c", am->root_path, 0);
978  }
979 
980  (void) unlink ((char *) remove_path1);
981  (void) unlink ((char *) remove_path2);
982 
983  vec_free (remove_path1);
984  vec_free (remove_path2);
985 
986  clib_memset (a, 0, sizeof (*a));
987  a->root_path = am->root_path;
989  a->baseva = (am->global_baseva != 0) ?
991  a->size = (am->global_size != 0) ? am->global_size : SVM_GLOBAL_REGION_SIZE;
992  a->flags = SVM_FLAGS_NODATA;
993  a->uid = am->api_uid;
994  a->gid = am->api_gid;
995  a->pvt_heap_size =
996  (am->global_pvt_heap_size !=
998 
1000 
1002 
1003  return error;
1004 }
1005 
1006 void
1008 {
1009  api_main_t *am = &api_main;
1010  am->region_name = name;
1011 }
1012 
1013 /*
1014  * fd.io coding-style-patch-verification: ON
1015  *
1016  * Local Variables:
1017  * eval: (c-set-style "gnu")
1018  * End:
1019  */
int vl_mem_api_handle_msg_private(vlib_main_t *vm, vlib_node_runtime_t *node, u32 reg_index)
Definition: memory_api.c:769
int vl_mem_api_handle_msg_main(vlib_main_t *vm, vlib_node_runtime_t *node)
Definition: memory_api.c:718
uword * pending_rpc_requests
Definition: main.h:243
static u32 vl_msg_api_handle_get_index(u32 index)
Definition: memory_api.h:48
void vl_api_memclnt_create_t_handler(vl_api_memclnt_create_t *mp)
Definition: memory_api.c:158
u8 * name
Client name.
Definition: api_common.h:52
#define SVM_GLOBAL_REGION_NAME
Definition: svm_common.h:97
const char * root_path
Definition: svm_common.h:67
static void * vl_api_memclnt_create_t_print(vl_api_memclnt_create_t *a, void *handle)
Definition: memory_api.c:42
static void vl_api_memclnt_keepalive_t_handler(vl_api_memclnt_keepalive_t *mp)
We can send ourselves these messages if someone uses the builtin binary api test tool...
Definition: memory_api.c:407
static void svm_pop_heap(void *oldheap)
Definition: svm.h:94
clib_spinlock_t pending_rpc_lock
Definition: main.h:245
#define vl_print(handle,...)
Definition: memory_api.c:31
int svm_queue_add(svm_queue_t *q, u8 *elem, int nowait)
Definition: queue.c:239
u32 vl_api_memclnt_create_internal(char *name, svm_queue_t *q)
Definition: memory_api.c:120
a
Definition: bitmap.h:538
#define SVM_FLAGS_NODATA
Definition: svm_common.h:29
static void send_memclnt_keepalive(vl_api_registration_t *regp, f64 now)
Definition: memory_api.c:501
int vl_map_shmem(const char *region_name, int is_vlib)
uword * processing_rpc_requests
Definition: main.h:244
volatile int ** vl_api_queue_cursizes
Definition: memory_api.c:61
u32 application_restarts
Definition: memory_shared.h:95
#define PREDICT_TRUE(x)
Definition: clib.h:112
clib_memset(h->entries, 0, sizeof(h->entries[0]) *entries)
static f64 vlib_time_now(vlib_main_t *vm)
Definition: main.h:255
void vl_unmap_shmem(void)
int svm_queue_sub2(svm_queue_t *q, u8 *elem)
Definition: queue.c:413
u8 * message_bounce
Don&#39;t automatically free message buffer vetor.
Definition: api_common.h:222
static_always_inline void clib_spinlock_unlock_if_init(clib_spinlock_t *p)
Definition: lock.h:98
Message configuration definition.
Definition: api_common.h:119
svm_region_t * vlib_primary_rp
Primary api segment descriptor.
Definition: api_common.h:258
#define vec_add1(V, E)
Add 1 element to end of vector (unspecified alignment).
Definition: vec.h:522
uword virtual_base
Definition: svm_common.h:42
#define SVM_PVT_MHEAP_SIZE
Definition: svm_common.h:32
int i
int api_uid
uid for the api shared memory region
Definition: api_common.h:283
ring_alloc_t * client_rings
Definition: memory_shared.h:92
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:424
#define pool_is_free(P, E)
Use free bitmap to query whether given element is free.
Definition: pool.h:275
#define pool_get(P, E)
Allocate an object E from a pool P (unspecified alignment).
Definition: pool.h:236
void * vl_msg_api_alloc(int nbytes)
int api_gid
gid for the api shared memory region
Definition: api_common.h:286
unsigned char u8
Definition: types.h:56
#define vec_reset_length(v)
Reset vector length to zero NULL-pointer tolerant.
double f64
Definition: types.h:142
_vl_msg_api_function_list_elt_t * reaper_function_registrations
List of API client reaper functions.
Definition: api_common.h:340
u8 * format_api_message_rings(u8 *s, va_list *args)
Definition: memory_api.c:846
u32 clib_file_index
Socket only: file index.
Definition: api_common.h:65
#define pool_foreach(VAR, POOL, BODY)
Iterate through pool.
Definition: pool.h:493
void vl_msg_api_barrier_sync(void)
Definition: api_shared.c:383
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:163
u32 ring_misses
Number of times that the ring allocator failed.
Definition: api_common.h:231
static void * svm_push_data_heap(svm_region_t *rp)
Definition: svm.h:86
vl_api_registration_t ** vl_clients
vlib/vpp only: vector of client registrations
Definition: api_common.h:268
vl_api_registration_t * vl_mem_api_client_index_to_registration(u32 handle)
Definition: memory_api.c:792
volatile void * user_ctx
Definition: svm_common.h:47
static clib_error_t * vl_api_ring_command(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cli_cmd)
Definition: memory_api.c:889
static u32 vl_msg_api_handle_from_index_and_epoch(u32 index, u32 epoch)
Definition: memory_api.h:54
void vl_api_memclnt_keepalive_reply_t_handler(vl_api_memclnt_keepalive_reply_t *mp)
client answered a ping, stave off the grim reaper...
Definition: memory_api.c:387
void vl_mem_api_dead_client_scan(api_main_t *am, vl_shmem_hdr_t *shm, f64 now)
Definition: memory_api.c:597
vlib_node_registration_t vl_api_clnt_node
(constructor) VLIB_REGISTER_NODE (vl_api_clnt_node)
Definition: vlib_api.c:438
const char * root_path
Chroot path to the shared memory API files.
Definition: api_common.h:331
#define clib_error_return(e, args...)
Definition: error.h:99
svm_region_t * vlib_rp
Current binary api segment descriptor.
Definition: api_common.h:255
struct vl_shmem_hdr_ * shmem_hdr
Binary API shared-memory segment header pointer.
Definition: api_common.h:265
unsigned int u32
Definition: types.h:88
#define vlib_call_init_function(vm, x)
Definition: init.h:260
void vl_msg_api_send_shmem(svm_queue_t *q, u8 *elem)
static void vlib_set_queue_signal_callback(vlib_main_t *vm, void(*fp)(vlib_main_t *))
Definition: main.h:385
vl_registration_type_t registration_type
type
Definition: api_common.h:47
char * name
Definition: main.h:117
#define pool_elt_at_index(p, i)
Returns pointer to element at given index.
Definition: pool.h:514
#define foreach_vlib_api_msg
Definition: memory_api.c:428
static void vlib_process_signal_event(vlib_main_t *vm, uword node_index, uword type_opaque, uword data)
Definition: node_funcs.h:934
svm_queue_t * vl_input_queue
shared memory only: pointer to client input queue
Definition: api_common.h:60
struct _unformat_input_t unformat_input_t
svm_region_t ** vlib_private_rps
Vector of all mapped shared-VM segments.
Definition: api_common.h:261
#define PREDICT_FALSE(x)
Definition: clib.h:111
uword virtual_size
Definition: svm_common.h:43
void svm_region_init_args(svm_map_region_args_t *a)
Definition: svm.c:916
#define SVM_GLOBAL_REGION_SIZE
Definition: svm_common.h:96
void vl_msg_api_handler_with_vm_node(api_main_t *am, void *the_msg, vlib_main_t *vm, vlib_node_runtime_t *node)
Definition: api_shared.c:463
u8 name[64]
Definition: memclnt.api:152
clib_error_t * vlibmemory_init(vlib_main_t *vm)
Definition: memory_api.c:958
volatile u32 queue_signal_pending
Definition: main.h:210
int vl_mem_api_handle_rpc(vlib_main_t *vm, vlib_node_runtime_t *node)
Definition: memory_api.c:726
clib_error_t * map_api_segment_init(vlib_main_t *vm)
Definition: memory_api.c:487
API main structure, used by both vpp and binary API clients.
Definition: api_common.h:202
u64 global_size
size of the global VM region
Definition: api_common.h:292
An API client registration, only in vpp/vlib.
Definition: api_common.h:45
Shared memory connection.
Definition: api_common.h:37
ring_alloc_t * vl_rings
Definition: memory_shared.h:89
svmdb_client_t * c
static_always_inline uword vlib_get_thread_index(void)
Definition: threads.h:212
vlib_main_t * vm
Definition: buffer.c:312
svm_queue_t * vl_api_client_index_to_input_queue(u32 index)
Definition: memory_api.c:821
u64 global_baseva
base virtual address for global VM region
Definition: api_common.h:289
static clib_error_t * setup_memclnt_exit(vlib_main_t *vm)
Definition: memory_api.c:837
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:341
#define clib_warning(format, args...)
Definition: error.h:59
static void * vl_api_memclnt_delete_t_print(vl_api_memclnt_delete_t *a, void *handle)
Definition: memory_api.c:53
u8 * vl_api_serialize_message_table(api_main_t *am, u8 *vector)
Definition: vlib_api.c:72
static int void_mem_api_handle_msg_i(api_main_t *am, vlib_main_t *vm, vlib_node_runtime_t *node, svm_queue_t *q)
Definition: memory_api.c:705
clib_error_t * vlibsocket_init(vlib_main_t *vm)
Definition: socket_api.c:794
static vl_api_registration_t * vl_api_client_index_to_registration(u32 index)
Definition: api.h:56
svm_queue_t * vl_input_queue
Definition: memory_shared.h:84
void svm_queue_free(svm_queue_t *q)
Definition: queue.c:89
#define VLIB_CLI_COMMAND(x,...)
Definition: cli.h:155
static u32 vl_msg_api_handle_get_epoch(u32 index)
Definition: memory_api.h:42
#define pool_put_index(p, i)
Free pool element with given index.
Definition: pool.h:311
#define ASSERT(truth)
int vl_mem_api_init(const char *region_name)
Definition: memory_api.c:438
#define vec_delete(V, N, M)
Delete N elements starting at element M.
Definition: vec.h:784
static u8 vl_msg_api_handle_is_valid(u32 handle, u32 restarts)
Definition: memory_api.h:64
static void vl_mem_send_client_keepalive_w_reg(api_main_t *am, f64 now, vl_api_registration_t **regpp, u32 **dead_indices, u32 **confused_indices)
Definition: memory_api.c:553
static void clib_mem_free(void *p)
Definition: mem.h:205
void vl_set_memory_region_name(const char *name)
Definition: memory_api.c:1007
u64 svm_get_global_region_base_va()
Definition: svm.c:62
u8 * serialized_message_table_in_shmem
vlib/vpp only: serialized (message, name, crc) table
Definition: api_common.h:271
#define clib_error_report(e)
Definition: error.h:113
u64 global_pvt_heap_size
size of the global VM private mheap
Definition: api_common.h:298
void vl_api_memclnt_delete_t_handler(vl_api_memclnt_delete_t *mp)
Definition: memory_api.c:263
static void * clib_mem_alloc(uword size)
Definition: mem.h:132
static uword pointer_to_uword(const void *p)
Definition: types.h:131
static vlib_main_t * vlib_get_main(void)
Definition: global_funcs.h:23
void vl_msg_api_free(void *)
#define MMAP_PAGESIZE
Definition: ssvm.h:42
#define VL_API_EPOCH_MASK
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
const char * name
Definition: svm_common.h:68
static void memclnt_queue_callback(vlib_main_t *vm)
Definition: memory_api.c:64
u64 uword
Definition: types.h:112
#define clib_unix_warning(format, args...)
Definition: error.h:68
u32 vl_api_registration_pool_index
Index in VLIB&#39;s brain (not shared memory).
Definition: api_common.h:50
void svm_client_scan_this_region_nolock(svm_region_t *rp)
Definition: svm.c:1262
volatile u32 api_queue_nonempty
Definition: main.h:211
struct _svm_queue svm_queue_t
u8 * is_mp_safe
Message is mp safe vector.
Definition: api_common.h:225
void vl_msg_api_increment_missing_client_counter(void)
Definition: api_shared.c:44
int vl_api_call_reaper_functions(u32 client_index)
Definition: memory_api.c:243
void * vl_msg_api_alloc_as_if_client(int nbytes)
const char * region_name
Shared VM binary API region name.
Definition: api_common.h:328
void vl_msg_api_barrier_release(void)
Definition: api_shared.c:388
api_main_t api_main
Definition: api_shared.c:35
void vlib_cli_output(vlib_main_t *vm, char *fmt,...)
Definition: cli.c:762
static_always_inline void clib_spinlock_lock_if_init(clib_spinlock_t *p)
Definition: lock.h:82
svm_region_t * vlib_rp
Definition: api_common.h:61
pthread_mutex_t mutex
Definition: svm_common.h:37