Use __attribute__((weak)) references where necessary
[vpp.git] / src / vlibmemory / memory_api.c
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 */
27 #include <vlibmemory/vl_memory_api_h.h>
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
33 #include <vlibmemory/vl_memory_api_h.h>
34 #undef vl_printfun
35
36 /* instantiate all the endian swap functions we know about */
37 #define vl_endianfun
38 #include <vlibmemory/vl_memory_api_h.h>
39 #undef vl_endianfun
40
41 static inline void *
42 vl_api_memclnt_create_t_print (vl_api_memclnt_create_t * a, void *handle)
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 *
53 vl_api_memclnt_delete_t_print (vl_api_memclnt_delete_t * a, void *handle)
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
64 memclnt_queue_callback (vlib_main_t * vm)
65 {
66   int i;
67   api_main_t *am = &api_main;
68
69   if (PREDICT_FALSE (vec_len (vl_api_queue_cursizes) !=
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;
100           vlib_process_signal_event (vm, vl_api_clnt_node.index,
101                                      /* event_type */ QUEUE_SIGNAL_EVENT,
102                                      /* event_data */ 0);
103           break;
104         }
105     }
106 }
107
108 /*
109  * vl_api_memclnt_create_internal
110  */
111 u32
112 vl_api_memclnt_create_internal (char *name, svm_queue_t * q)
113 {
114   vl_api_registration_t **regpp;
115   vl_api_registration_t *regp;
116   svm_region_t *svm;
117   void *oldheap;
118   api_main_t *am = &api_main;
119
120   ASSERT (vlib_get_thread_index () == 0);
121   pool_get (am->vl_clients, regpp);
122
123   svm = am->vlib_rp;
124
125   pthread_mutex_lock (&svm->mutex);
126   oldheap = svm_push_data_heap (svm);
127   *regpp = clib_mem_alloc (sizeof (vl_api_registration_t));
128
129   regp = *regpp;
130   memset (regp, 0, sizeof (*regp));
131   regp->registration_type = REGISTRATION_TYPE_SHMEM;
132   regp->vl_api_registration_pool_index = regpp - am->vl_clients;
133   regp->vlib_rp = svm;
134   regp->shmem_hdr = am->shmem_hdr;
135
136   regp->vl_input_queue = q;
137   regp->name = format (0, "%s%c", name, 0);
138
139   pthread_mutex_unlock (&svm->mutex);
140   svm_pop_heap (oldheap);
141   return vl_msg_api_handle_from_index_and_epoch
142     (regp->vl_api_registration_pool_index,
143      am->shmem_hdr->application_restarts);
144 }
145
146 /*
147  * vl_api_memclnt_create_t_handler
148  */
149 void
150 vl_api_memclnt_create_t_handler (vl_api_memclnt_create_t * mp)
151 {
152   vl_api_registration_t **regpp;
153   vl_api_registration_t *regp;
154   vl_api_memclnt_create_reply_t *rp;
155   svm_region_t *svm;
156   svm_queue_t *q;
157   int rv = 0;
158   void *oldheap;
159   api_main_t *am = &api_main;
160
161   /*
162    * This is tortured. Maintain a vlib-address-space private
163    * pool of client registrations. We use the shared-memory virtual
164    * address of client structure as a handle, to allow direct
165    * manipulation of context quota vbls from the client library.
166    *
167    * This scheme causes trouble w/ API message trace replay, since
168    * some random VA from clib_mem_alloc() certainly won't
169    * occur in the Linux sim. The (very) few places
170    * that care need to use the pool index.
171    *
172    * Putting the registration object(s) into a pool in shared memory and
173    * using the pool index as a handle seems like a great idea.
174    * Unfortunately, each and every reference to that pool would need
175    * to be protected by a mutex:
176    *
177    *     Client                      VLIB
178    *     ------                      ----
179    *     convert pool index to
180    *     pointer.
181    *     <deschedule>
182    *                                 expand pool
183    *                                 <deschedule>
184    *     kaboom!
185    */
186
187   pool_get (am->vl_clients, regpp);
188
189   svm = am->vlib_rp;
190
191   pthread_mutex_lock (&svm->mutex);
192   oldheap = svm_push_data_heap (svm);
193   *regpp = clib_mem_alloc (sizeof (vl_api_registration_t));
194
195   regp = *regpp;
196   memset (regp, 0, sizeof (*regp));
197   regp->registration_type = REGISTRATION_TYPE_SHMEM;
198   regp->vl_api_registration_pool_index = regpp - am->vl_clients;
199   regp->vlib_rp = svm;
200   regp->shmem_hdr = am->shmem_hdr;
201   regp->clib_file_index = am->shmem_hdr->clib_file_index;
202
203   q = regp->vl_input_queue = (svm_queue_t *) (uword) mp->input_queue;
204
205   regp->name = format (0, "%s", mp->name);
206   vec_add1 (regp->name, 0);
207
208   if (am->serialized_message_table_in_shmem == 0)
209     am->serialized_message_table_in_shmem =
210       vl_api_serialize_message_table (am, 0);
211
212   pthread_mutex_unlock (&svm->mutex);
213   svm_pop_heap (oldheap);
214
215   rp = vl_msg_api_alloc (sizeof (*rp));
216   rp->_vl_msg_id = ntohs (VL_API_MEMCLNT_CREATE_REPLY);
217   rp->handle = (uword) regp;
218   rp->index = vl_msg_api_handle_from_index_and_epoch
219     (regp->vl_api_registration_pool_index,
220      am->shmem_hdr->application_restarts);
221   rp->context = mp->context;
222   rp->response = ntohl (rv);
223   rp->message_table =
224     pointer_to_uword (am->serialized_message_table_in_shmem);
225
226   vl_msg_api_send_shmem (q, (u8 *) & rp);
227 }
228
229 int
230 vl_api_call_reaper_functions (u32 client_index)
231 {
232   clib_error_t *error = 0;
233   _vl_msg_api_function_list_elt_t *i;
234
235   i = api_main.reaper_function_registrations;
236   while (i)
237     {
238       error = i->f (client_index);
239       if (error)
240         clib_error_report (error);
241       i = i->next_init_function;
242     }
243   return 0;
244 }
245
246 /*
247  * vl_api_memclnt_delete_t_handler
248  */
249 void
250 vl_api_memclnt_delete_t_handler (vl_api_memclnt_delete_t * mp)
251 {
252   vl_api_registration_t **regpp;
253   vl_api_registration_t *regp;
254   vl_api_memclnt_delete_reply_t *rp;
255   svm_region_t *svm;
256   void *oldheap;
257   api_main_t *am = &api_main;
258   u32 handle, client_index, epoch;
259
260   handle = mp->index;
261
262   if (vl_api_call_reaper_functions (handle))
263     return;
264
265   epoch = vl_msg_api_handle_get_epoch (handle);
266   client_index = vl_msg_api_handle_get_index (handle);
267
268   if (epoch != (am->shmem_hdr->application_restarts & VL_API_EPOCH_MASK))
269     {
270       clib_warning
271         ("Stale clnt delete index %d old epoch %d cur epoch %d",
272          client_index, epoch,
273          (am->shmem_hdr->application_restarts & VL_API_EPOCH_MASK));
274       return;
275     }
276
277   regpp = pool_elt_at_index (am->vl_clients, client_index);
278
279   if (!pool_is_free (am->vl_clients, regpp))
280     {
281       int i;
282       regp = *regpp;
283       svm = am->vlib_rp;
284       int private_registration = 0;
285
286       /*
287        * Note: the API message handling path will set am->vlib_rp
288        * as appropriate for pairwise / private memory segments
289        */
290       rp = vl_msg_api_alloc (sizeof (*rp));
291       rp->_vl_msg_id = ntohs (VL_API_MEMCLNT_DELETE_REPLY);
292       rp->handle = mp->handle;
293       rp->response = 1;
294
295       vl_msg_api_send_shmem (regp->vl_input_queue, (u8 *) & rp);
296
297       if (client_index != regp->vl_api_registration_pool_index)
298         {
299           clib_warning ("mismatch client_index %d pool_index %d",
300                         client_index, regp->vl_api_registration_pool_index);
301           vl_msg_api_free (rp);
302           return;
303         }
304
305       /* For horizontal scaling, add a hash table... */
306       for (i = 0; i < vec_len (am->vlib_private_rps); i++)
307         {
308           /* Is this a pairwise / private API segment? */
309           if (am->vlib_private_rps[i] == svm)
310             {
311               /* Note: account for the memfd header page */
312               u64 virtual_base = svm->virtual_base - MMAP_PAGESIZE;
313               u64 virtual_size = svm->virtual_size + MMAP_PAGESIZE;
314
315               /*
316                * Kill the registration pool element before we make
317                * the index vanish forever
318                */
319               pool_put_index (am->vl_clients,
320                               regp->vl_api_registration_pool_index);
321
322               vec_delete (am->vlib_private_rps, 1, i);
323               /* Kill it, accounting for the memfd header page */
324               if (munmap ((void *) virtual_base, virtual_size) < 0)
325                 clib_unix_warning ("munmap");
326               /* Reset the queue-length-address cache */
327               vec_reset_length (vl_api_queue_cursizes);
328               private_registration = 1;
329               break;
330             }
331         }
332
333       /* No dangling references, please */
334       *regpp = 0;
335
336       if (private_registration == 0)
337         {
338           pool_put_index (am->vl_clients,
339                           regp->vl_api_registration_pool_index);
340           pthread_mutex_lock (&svm->mutex);
341           oldheap = svm_push_data_heap (svm);
342           vec_free (regp->name);
343           /* Poison the old registration */
344           memset (regp, 0xF1, sizeof (*regp));
345           clib_mem_free (regp);
346           pthread_mutex_unlock (&svm->mutex);
347           svm_pop_heap (oldheap);
348           /*
349            * These messages must be freed manually, since they're set up
350            * as "bounce" messages. In the private_registration == 1 case,
351            * we kill the shared-memory segment which contains the message
352            * with munmap.
353            */
354           vl_msg_api_free (mp);
355         }
356     }
357   else
358     {
359       clib_warning ("unknown client ID %d", mp->index);
360     }
361 }
362
363 /**
364  * client answered a ping, stave off the grim reaper...
365  */
366 void
367   vl_api_memclnt_keepalive_reply_t_handler
368   (vl_api_memclnt_keepalive_reply_t * mp)
369 {
370   vl_api_registration_t *regp;
371   vlib_main_t *vm = vlib_get_main ();
372
373   regp = vl_api_client_index_to_registration (mp->context);
374   if (regp)
375     {
376       regp->last_heard = vlib_time_now (vm);
377       regp->unanswered_pings = 0;
378     }
379   else
380     clib_warning ("BUG: anonymous memclnt_keepalive_reply");
381 }
382
383 /**
384  * We can send ourselves these messages if someone uses the
385  * builtin binary api test tool...
386  */
387 static void
388 vl_api_memclnt_keepalive_t_handler (vl_api_memclnt_keepalive_t * mp)
389 {
390   vl_api_memclnt_keepalive_reply_t *rmp;
391   api_main_t *am;
392   vl_shmem_hdr_t *shmem_hdr;
393
394   am = &api_main;
395   shmem_hdr = am->shmem_hdr;
396
397   rmp = vl_msg_api_alloc_as_if_client (sizeof (*rmp));
398   memset (rmp, 0, sizeof (*rmp));
399   rmp->_vl_msg_id = ntohs (VL_API_MEMCLNT_KEEPALIVE_REPLY);
400   rmp->context = mp->context;
401   vl_msg_api_send_shmem (shmem_hdr->vl_input_queue, (u8 *) & rmp);
402 }
403
404 #define foreach_vlib_api_msg                            \
405 _(MEMCLNT_CREATE, memclnt_create)                       \
406 _(MEMCLNT_DELETE, memclnt_delete)                       \
407 _(MEMCLNT_KEEPALIVE, memclnt_keepalive)                 \
408 _(MEMCLNT_KEEPALIVE_REPLY, memclnt_keepalive_reply)     \
409
410 /*
411  * memory_api_init
412  */
413 int
414 vl_mem_api_init (const char *region_name)
415 {
416   int rv;
417   api_main_t *am = &api_main;
418   vl_msg_api_msg_config_t cfg;
419   vl_msg_api_msg_config_t *c = &cfg;
420   vl_shmem_hdr_t *shm;
421   vlib_main_t *vm = vlib_get_main ();
422
423   memset (c, 0, sizeof (*c));
424
425   if ((rv = vl_map_shmem (region_name, 1 /* is_vlib */ )) < 0)
426     return rv;
427
428 #define _(N,n) do {                                             \
429     c->id = VL_API_##N;                                         \
430     c->name = #n;                                               \
431     c->handler = vl_api_##n##_t_handler;                        \
432     c->cleanup = vl_noop_handler;                               \
433     c->endian = vl_api_##n##_t_endian;                          \
434     c->print = vl_api_##n##_t_print;                            \
435     c->size = sizeof(vl_api_##n##_t);                           \
436     c->traced = 1; /* trace, so these msgs print */             \
437     c->replay = 0; /* don't replay client create/delete msgs */ \
438     c->message_bounce = 0; /* don't bounce this message */      \
439     vl_msg_api_config(c);} while (0);
440
441   foreach_vlib_api_msg;
442 #undef _
443
444   /*
445    * special-case freeing of memclnt_delete messages, so we can
446    * simply munmap pairwise / private API segments...
447    */
448   am->message_bounce[VL_API_MEMCLNT_DELETE] = 1;
449   am->is_mp_safe[VL_API_MEMCLNT_KEEPALIVE_REPLY] = 1;
450
451   vlib_set_queue_signal_callback (vm, memclnt_queue_callback);
452
453   shm = am->shmem_hdr;
454   ASSERT (shm && shm->vl_input_queue);
455
456   /* Make a note so we can always find the primary region easily */
457   am->vlib_primary_rp = am->vlib_rp;
458
459   return 0;
460 }
461
462 clib_error_t *
463 map_api_segment_init (vlib_main_t * vm)
464 {
465   api_main_t *am = &api_main;
466   int rv;
467
468   if ((rv = vl_mem_api_init (am->region_name)) < 0)
469     {
470       return clib_error_return (0, "vl_mem_api_init (%s) failed",
471                                 am->region_name);
472     }
473   return 0;
474 }
475
476 static void
477 send_memclnt_keepalive (vl_api_registration_t * regp, f64 now)
478 {
479   vl_api_memclnt_keepalive_t *mp;
480   svm_queue_t *q;
481   api_main_t *am = &api_main;
482   svm_region_t *save_vlib_rp = am->vlib_rp;
483   vl_shmem_hdr_t *save_shmem_hdr = am->shmem_hdr;
484
485   q = regp->vl_input_queue;
486
487   /*
488    * If the queue head is moving, assume that the client is processing
489    * messages and skip the ping. This heuristic may fail if the queue
490    * is in the same position as last time, net of wrapping; in which
491    * case, the client will receive a keepalive.
492    */
493   if (regp->last_queue_head != q->head)
494     {
495       regp->last_heard = now;
496       regp->unanswered_pings = 0;
497       regp->last_queue_head = q->head;
498       return;
499     }
500
501   /*
502    * push/pop shared memory segment, so this routine
503    * will work with "normal" as well as "private segment"
504    * memory clients..
505    */
506
507   am->vlib_rp = regp->vlib_rp;
508   am->shmem_hdr = regp->shmem_hdr;
509
510   mp = vl_msg_api_alloc (sizeof (*mp));
511   memset (mp, 0, sizeof (*mp));
512   mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_MEMCLNT_KEEPALIVE);
513   mp->context = mp->client_index =
514     vl_msg_api_handle_from_index_and_epoch
515     (regp->vl_api_registration_pool_index,
516      am->shmem_hdr->application_restarts);
517
518   regp->unanswered_pings++;
519
520   /* Failure-to-send due to a stuffed queue is absolutely expected */
521   if (svm_queue_add (q, (u8 *) & mp, 1 /* nowait */ ))
522     vl_msg_api_free (mp);
523
524   am->vlib_rp = save_vlib_rp;
525   am->shmem_hdr = save_shmem_hdr;
526 }
527
528 static void
529 vl_mem_send_client_keepalive_w_reg (api_main_t * am, f64 now,
530                                     vl_api_registration_t ** regpp,
531                                     u32 ** dead_indices,
532                                     u32 ** confused_indices)
533 {
534   vl_api_registration_t *regp = *regpp;
535   if (regp)
536     {
537       /* If we haven't heard from this client recently... */
538       if (regp->last_heard < (now - 10.0))
539         {
540           if (regp->unanswered_pings == 2)
541             {
542               svm_queue_t *q;
543               q = regp->vl_input_queue;
544               if (kill (q->consumer_pid, 0) >= 0)
545                 {
546                   clib_warning ("REAPER: lazy binary API client '%s'",
547                                 regp->name);
548                   regp->unanswered_pings = 0;
549                   regp->last_heard = now;
550                 }
551               else
552                 {
553                   clib_warning ("REAPER: binary API client '%s' died",
554                                 regp->name);
555                   vec_add1 (*dead_indices, regpp - am->vl_clients);
556                 }
557             }
558           else
559             send_memclnt_keepalive (regp, now);
560         }
561       else
562         regp->unanswered_pings = 0;
563     }
564   else
565     {
566       clib_warning ("NULL client registration index %d",
567                     regpp - am->vl_clients);
568       vec_add1 (*confused_indices, regpp - am->vl_clients);
569     }
570 }
571
572 void
573 vl_mem_api_dead_client_scan (api_main_t * am, vl_shmem_hdr_t * shm, f64 now)
574 {
575   vl_api_registration_t **regpp;
576   static u32 *dead_indices;
577   static u32 *confused_indices;
578
579   vec_reset_length (dead_indices);
580   vec_reset_length (confused_indices);
581
582   /* *INDENT-OFF* */
583   pool_foreach (regpp, am->vl_clients, ({
584       vl_mem_send_client_keepalive_w_reg (am, now, regpp, &dead_indices,
585                                           &confused_indices);
586   }));
587   /* *INDENT-ON* */
588
589   /* This should "never happen," but if it does, fix it... */
590   if (PREDICT_FALSE (vec_len (confused_indices) > 0))
591     {
592       int i;
593       for (i = 0; i < vec_len (confused_indices); i++)
594         {
595           pool_put_index (am->vl_clients, confused_indices[i]);
596         }
597     }
598
599   if (PREDICT_FALSE (vec_len (dead_indices) > 0))
600     {
601       int i;
602       svm_region_t *svm;
603       void *oldheap;
604
605       /* Allow the application to clean up its registrations */
606       for (i = 0; i < vec_len (dead_indices); i++)
607         {
608           regpp = pool_elt_at_index (am->vl_clients, dead_indices[i]);
609           if (regpp)
610             {
611               u32 handle;
612
613               handle = vl_msg_api_handle_from_index_and_epoch
614                 (dead_indices[i], shm->application_restarts);
615               (void) vl_api_call_reaper_functions (handle);
616             }
617         }
618
619       svm = am->vlib_rp;
620       pthread_mutex_lock (&svm->mutex);
621       oldheap = svm_push_data_heap (svm);
622
623       for (i = 0; i < vec_len (dead_indices); i++)
624         {
625           regpp = pool_elt_at_index (am->vl_clients, dead_indices[i]);
626           if (regpp)
627             {
628               /* Is this a pairwise SVM segment? */
629               if ((*regpp)->vlib_rp != svm)
630                 {
631                   int i;
632                   svm_region_t *dead_rp = (*regpp)->vlib_rp;
633                   /* Note: account for the memfd header page */
634                   u64 virtual_base = dead_rp->virtual_base - MMAP_PAGESIZE;
635                   u64 virtual_size = dead_rp->virtual_size + MMAP_PAGESIZE;
636
637                   /* For horizontal scaling, add a hash table... */
638                   for (i = 0; i < vec_len (am->vlib_private_rps); i++)
639                     if (am->vlib_private_rps[i] == dead_rp)
640                       {
641                         vec_delete (am->vlib_private_rps, 1, i);
642                         goto found;
643                       }
644                   clib_warning ("private rp %llx AWOL", dead_rp);
645
646                 found:
647                   /* Kill it, accounting for the memfd header page */
648                   if (munmap ((void *) virtual_base, virtual_size) < 0)
649                     clib_unix_warning ("munmap");
650                   /* Reset the queue-length-address cache */
651                   vec_reset_length (vl_api_queue_cursizes);
652                 }
653               else
654                 {
655                   /* Poison the old registration */
656                   memset (*regpp, 0xF3, sizeof (**regpp));
657                   clib_mem_free (*regpp);
658                 }
659               /* no dangling references, please */
660               *regpp = 0;
661             }
662           else
663             {
664               svm_pop_heap (oldheap);
665               clib_warning ("Duplicate free, client index %d",
666                             regpp - am->vl_clients);
667               oldheap = svm_push_data_heap (svm);
668             }
669         }
670
671       svm_client_scan_this_region_nolock (am->vlib_rp);
672
673       pthread_mutex_unlock (&svm->mutex);
674       svm_pop_heap (oldheap);
675       for (i = 0; i < vec_len (dead_indices); i++)
676         pool_put_index (am->vl_clients, dead_indices[i]);
677     }
678 }
679
680 static inline int
681 void_mem_api_handle_msg_i (api_main_t * am, vlib_main_t * vm,
682                            vlib_node_runtime_t * node, svm_queue_t * q)
683 {
684   uword mp;
685   if (!svm_queue_sub2 (q, (u8 *) & mp))
686     {
687       vl_msg_api_handler_with_vm_node (am, (void *) mp, vm, node);
688       return 0;
689     }
690   return -1;
691 }
692
693 int
694 vl_mem_api_handle_msg_main (vlib_main_t * vm, vlib_node_runtime_t * node)
695 {
696   api_main_t *am = &api_main;
697   return void_mem_api_handle_msg_i (am, vm, node,
698                                     am->shmem_hdr->vl_input_queue);
699 }
700
701 int
702 vl_mem_api_handle_msg_private (vlib_main_t * vm, vlib_node_runtime_t * node,
703                                u32 reg_index)
704 {
705   api_main_t *am = &api_main;
706   vl_shmem_hdr_t *save_shmem_hdr = am->shmem_hdr;
707   svm_region_t *vlib_rp, *save_vlib_rp = am->vlib_rp;
708   svm_queue_t *q;
709   int rv;
710
711   vlib_rp = am->vlib_rp = am->vlib_private_rps[reg_index];
712
713   am->shmem_hdr = (void *) vlib_rp->user_ctx;
714   q = am->shmem_hdr->vl_input_queue;
715
716   rv = void_mem_api_handle_msg_i (am, vm, node, q);
717
718   am->shmem_hdr = save_shmem_hdr;
719   am->vlib_rp = save_vlib_rp;
720
721   return rv;
722 }
723
724 vl_api_registration_t *
725 vl_mem_api_client_index_to_registration (u32 handle)
726 {
727   vl_api_registration_t **regpp;
728   vl_api_registration_t *regp;
729   api_main_t *am = &api_main;
730   vl_shmem_hdr_t *shmem_hdr;
731   u32 index;
732
733   index = vl_msg_api_handle_get_index (handle);
734   regpp = am->vl_clients + index;
735
736   if (pool_is_free (am->vl_clients, regpp))
737     {
738       vl_msg_api_increment_missing_client_counter ();
739       return 0;
740     }
741   regp = *regpp;
742
743   shmem_hdr = (vl_shmem_hdr_t *) regp->shmem_hdr;
744   if (!vl_msg_api_handle_is_valid (handle, shmem_hdr->application_restarts))
745     {
746       vl_msg_api_increment_missing_client_counter ();
747       return 0;
748     }
749
750   return (regp);
751 }
752
753 svm_queue_t *
754 vl_api_client_index_to_input_queue (u32 index)
755 {
756   vl_api_registration_t *regp;
757   api_main_t *am = &api_main;
758
759   /* Special case: vlib trying to send itself a message */
760   if (index == (u32) ~ 0)
761     return (am->shmem_hdr->vl_input_queue);
762
763   regp = vl_mem_api_client_index_to_registration (index);
764   if (!regp)
765     return 0;
766   return (regp->vl_input_queue);
767 }
768
769 static clib_error_t *
770 setup_memclnt_exit (vlib_main_t * vm)
771 {
772   atexit (vl_unmap_shmem);
773   return 0;
774 }
775
776 VLIB_INIT_FUNCTION (setup_memclnt_exit);
777
778 u8 *
779 format_api_message_rings (u8 * s, va_list * args)
780 {
781   api_main_t *am = va_arg (*args, api_main_t *);
782   vl_shmem_hdr_t *shmem_hdr = va_arg (*args, vl_shmem_hdr_t *);
783   int main_segment = va_arg (*args, int);
784   ring_alloc_t *ap;
785   int i;
786
787   if (shmem_hdr == 0)
788     return format (s, "%8s %8s %8s %8s %8s\n",
789                    "Owner", "Size", "Nitems", "Hits", "Misses");
790
791   ap = shmem_hdr->vl_rings;
792
793   for (i = 0; i < vec_len (shmem_hdr->vl_rings); i++)
794     {
795       s = format (s, "%8s %8d %8d %8d %8d\n",
796                   "vlib", ap->size, ap->nitems, ap->hits, ap->misses);
797       ap++;
798     }
799
800   ap = shmem_hdr->client_rings;
801
802   for (i = 0; i < vec_len (shmem_hdr->client_rings); i++)
803     {
804       s = format (s, "%8s %8d %8d %8d %8d\n",
805                   "clnt", ap->size, ap->nitems, ap->hits, ap->misses);
806       ap++;
807     }
808
809   if (main_segment)
810     {
811       s = format (s, "%d ring miss fallback allocations\n", am->ring_misses);
812       s = format
813         (s,
814          "%d application restarts, %d reclaimed msgs, %d garbage collects\n",
815          shmem_hdr->application_restarts, shmem_hdr->restart_reclaims,
816          shmem_hdr->garbage_collects);
817     }
818   return s;
819 }
820
821 static clib_error_t *
822 vl_api_ring_command (vlib_main_t * vm,
823                      unformat_input_t * input, vlib_cli_command_t * cli_cmd)
824 {
825   int i;
826   vl_shmem_hdr_t *shmem_hdr;
827   api_main_t *am = &api_main;
828
829   /* First, dump the primary region rings.. */
830
831   if (am->vlib_primary_rp == 0 || am->vlib_primary_rp->user_ctx == 0)
832     {
833       vlib_cli_output (vm, "Shared memory segment not initialized...\n");
834       return 0;
835     }
836
837   shmem_hdr = (void *) am->vlib_primary_rp->user_ctx;
838
839   vlib_cli_output (vm, "Main API segment rings:");
840
841   vlib_cli_output (vm, "%U", format_api_message_rings, am,
842                    0 /* print header */ , 0 /* notused */ );
843
844   vlib_cli_output (vm, "%U", format_api_message_rings, am,
845                    shmem_hdr, 1 /* main segment */ );
846
847   for (i = 0; i < vec_len (am->vlib_private_rps); i++)
848     {
849       svm_region_t *vlib_rp = am->vlib_private_rps[i];
850       shmem_hdr = (void *) vlib_rp->user_ctx;
851       vl_api_registration_t **regpp;
852       vl_api_registration_t *regp = 0;
853
854       /* For horizontal scaling, add a hash table... */
855       /* *INDENT-OFF* */
856       pool_foreach (regpp, am->vl_clients,
857       ({
858         regp = *regpp;
859         if (regp && regp->vlib_rp == vlib_rp)
860           {
861             vlib_cli_output (vm, "%s segment rings:", regp->name);
862             goto found;
863           }
864       }));
865       vlib_cli_output (vm, "regp %llx not found?", regp);
866       continue;
867       /* *INDENT-ON* */
868     found:
869       vlib_cli_output (vm, "%U", format_api_message_rings, am,
870                        0 /* print header */ , 0 /* notused */ );
871       vlib_cli_output (vm, "%U", format_api_message_rings, am,
872                        shmem_hdr, 0 /* main segment */ );
873     }
874
875   return 0;
876 }
877
878 /*?
879  * Display binary api message allocation ring statistics
880 ?*/
881 /* *INDENT-OFF* */
882 VLIB_CLI_COMMAND (cli_show_api_ring_command, static) =
883 {
884   .path = "show api ring-stats",
885   .short_help = "Message ring statistics",
886   .function = vl_api_ring_command,
887 };
888 /* *INDENT-ON* */
889
890 clib_error_t *
891 vlibmemory_init (vlib_main_t * vm)
892 {
893   api_main_t *am = &api_main;
894   svm_map_region_args_t _a, *a = &_a;
895   clib_error_t *error;
896
897   memset (a, 0, sizeof (*a));
898   a->root_path = am->root_path;
899   a->name = SVM_GLOBAL_REGION_NAME;
900   a->baseva = (am->global_baseva != 0) ?
901     am->global_baseva : +svm_get_global_region_base_va ();
902   a->size = (am->global_size != 0) ? am->global_size : SVM_GLOBAL_REGION_SIZE;
903   a->flags = SVM_FLAGS_NODATA;
904   a->uid = am->api_uid;
905   a->gid = am->api_gid;
906   a->pvt_heap_size =
907     (am->global_pvt_heap_size !=
908      0) ? am->global_pvt_heap_size : SVM_PVT_MHEAP_SIZE;
909
910   svm_region_init_args (a);
911
912   error = vlib_call_init_function (vm, vlibsocket_init);
913
914   return error;
915 }
916
917 void
918 vl_set_memory_region_name (const char *name)
919 {
920   api_main_t *am = &api_main;
921   am->region_name = name;
922 }
923
924 /*
925  * fd.io coding-style-patch-verification: ON
926  *
927  * Local Variables:
928  * eval: (c-set-style "gnu")
929  * End:
930  */