api: add mp-safe/barrier-sync indication to elogs
[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   if (vec_len (vm->pending_rpc_requests))
107     {
108       vm->queue_signal_pending = 1;
109       vm->api_queue_nonempty = 1;
110       vlib_process_signal_event (vm, vl_api_clnt_node.index,
111                                  /* event_type */ QUEUE_SIGNAL_EVENT,
112                                  /* event_data */ 0);
113     }
114 }
115
116 /*
117  * vl_api_memclnt_create_internal
118  */
119 u32
120 vl_api_memclnt_create_internal (char *name, svm_queue_t * q)
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));
139   regp->registration_type = REGISTRATION_TYPE_SHMEM;
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);
149   return vl_msg_api_handle_from_index_and_epoch
150     (regp->vl_api_registration_pool_index,
151      am->shmem_hdr->application_restarts);
152 }
153
154 /*
155  * vl_api_memclnt_create_t_handler
156  */
157 void
158 vl_api_memclnt_create_t_handler (vl_api_memclnt_create_t * mp)
159 {
160   vl_api_registration_t **regpp;
161   vl_api_registration_t *regp;
162   vl_api_memclnt_create_reply_t *rp;
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));
206   regp->registration_type = REGISTRATION_TYPE_SHMEM;
207   regp->vl_api_registration_pool_index = regpp - am->vl_clients;
208   regp->vlib_rp = svm;
209   regp->shmem_hdr = am->shmem_hdr;
210   regp->clib_file_index = am->shmem_hdr->clib_file_index;
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
217   if (am->serialized_message_table_in_shmem == 0)
218     am->serialized_message_table_in_shmem =
219       vl_api_serialize_message_table (am, 0);
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;
232   rp->index = vl_msg_api_handle_from_index_and_epoch
233     (regp->vl_api_registration_pool_index,
234      am->shmem_hdr->application_restarts);
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
243 vl_api_call_reaper_functions (u32 client_index)
244 {
245   clib_error_t *error = 0;
246   _vl_msg_api_function_list_elt_t *i;
247
248   i = api_main.reaper_function_registrations;
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
263 vl_api_memclnt_delete_t_handler (vl_api_memclnt_delete_t * mp)
264 {
265   vl_api_registration_t **regpp;
266   vl_api_registration_t *regp;
267   vl_api_memclnt_delete_reply_t *rp;
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     {
283       clib_warning
284         ("Stale clnt delete index %d old epoch %d cur epoch %d",
285          client_index, epoch,
286          (am->shmem_hdr->application_restarts & VL_API_EPOCH_MASK));
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,
316                             regp->vl_api_registration_pool_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                */
336               pool_put_index (am->vl_clients,
337                               regp->vl_api_registration_pool_index);
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 */
344               vec_reset_length (vl_api_queue_cursizes);
345               private_registration = 1;
346               break;
347             }
348         }
349
350       /* No dangling references, please */
351       *regpp = 0;
352
353       if (private_registration == 0)
354         {
355           pool_put_index (am->vl_clients,
356                           regp->vl_api_registration_pool_index);
357           pthread_mutex_lock (&svm->mutex);
358           oldheap = svm_push_data_heap (svm);
359           if (mp->do_cleanup)
360             svm_queue_free (regp->vl_input_queue);
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
386   vl_api_memclnt_keepalive_reply_t_handler
387   (vl_api_memclnt_keepalive_reply_t * mp)
388 {
389   vl_api_registration_t *regp;
390   vlib_main_t *vm = vlib_get_main ();
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
407 vl_api_memclnt_keepalive_t_handler (vl_api_memclnt_keepalive_t * mp)
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 #define foreach_vlib_api_msg                            \
424 _(MEMCLNT_CREATE, memclnt_create)                       \
425 _(MEMCLNT_DELETE, memclnt_delete)                       \
426 _(MEMCLNT_KEEPALIVE, memclnt_keepalive)                 \
427 _(MEMCLNT_KEEPALIVE_REPLY, memclnt_keepalive_reply)     \
428
429 /*
430  * memory_api_init
431  */
432 int
433 vl_mem_api_init (const char *region_name)
434 {
435   int rv;
436   api_main_t *am = &api_main;
437   vl_msg_api_msg_config_t cfg;
438   vl_msg_api_msg_config_t *c = &cfg;
439   vl_shmem_hdr_t *shm;
440   vlib_main_t *vm = vlib_get_main ();
441
442   clib_memset (c, 0, sizeof (*c));
443
444   if ((rv = vl_map_shmem (region_name, 1 /* is_vlib */ )) < 0)
445     return rv;
446
447 #define _(N,n) do {                                             \
448     c->id = VL_API_##N;                                         \
449     c->name = #n;                                               \
450     c->handler = vl_api_##n##_t_handler;                        \
451     c->cleanup = vl_noop_handler;                               \
452     c->endian = vl_api_##n##_t_endian;                          \
453     c->print = vl_api_##n##_t_print;                            \
454     c->size = sizeof(vl_api_##n##_t);                           \
455     c->traced = 1; /* trace, so these msgs print */             \
456     c->replay = 0; /* don't replay client create/delete msgs */ \
457     c->message_bounce = 0; /* don't bounce this message */      \
458     vl_msg_api_config(c);} while (0);
459
460   foreach_vlib_api_msg;
461 #undef _
462
463   /*
464    * special-case freeing of memclnt_delete messages, so we can
465    * simply munmap pairwise / private API segments...
466    */
467   am->message_bounce[VL_API_MEMCLNT_DELETE] = 1;
468   am->is_mp_safe[VL_API_MEMCLNT_KEEPALIVE_REPLY] = 1;
469   am->is_mp_safe[VL_API_MEMCLNT_KEEPALIVE] = 1;
470
471   vlib_set_queue_signal_callback (vm, memclnt_queue_callback);
472
473   shm = am->shmem_hdr;
474   ASSERT (shm && shm->vl_input_queue);
475
476   /* Make a note so we can always find the primary region easily */
477   am->vlib_primary_rp = am->vlib_rp;
478
479   return 0;
480 }
481
482 clib_error_t *
483 map_api_segment_init (vlib_main_t * vm)
484 {
485   api_main_t *am = &api_main;
486   int rv;
487
488   if ((rv = vl_mem_api_init (am->region_name)) < 0)
489     {
490       return clib_error_return (0, "vl_mem_api_init (%s) failed",
491                                 am->region_name);
492     }
493   return 0;
494 }
495
496 static void
497 send_memclnt_keepalive (vl_api_registration_t * regp, f64 now)
498 {
499   vl_api_memclnt_keepalive_t *mp;
500   svm_queue_t *q;
501   api_main_t *am = &api_main;
502   svm_region_t *save_vlib_rp = am->vlib_rp;
503   vl_shmem_hdr_t *save_shmem_hdr = am->shmem_hdr;
504
505   q = regp->vl_input_queue;
506
507   /*
508    * If the queue head is moving, assume that the client is processing
509    * messages and skip the ping. This heuristic may fail if the queue
510    * is in the same position as last time, net of wrapping; in which
511    * case, the client will receive a keepalive.
512    */
513   if (regp->last_queue_head != q->head)
514     {
515       regp->last_heard = now;
516       regp->unanswered_pings = 0;
517       regp->last_queue_head = q->head;
518       return;
519     }
520
521   /*
522    * push/pop shared memory segment, so this routine
523    * will work with "normal" as well as "private segment"
524    * memory clients..
525    */
526
527   am->vlib_rp = regp->vlib_rp;
528   am->shmem_hdr = regp->shmem_hdr;
529
530   mp = vl_msg_api_alloc (sizeof (*mp));
531   clib_memset (mp, 0, sizeof (*mp));
532   mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_MEMCLNT_KEEPALIVE);
533   mp->context = mp->client_index =
534     vl_msg_api_handle_from_index_and_epoch
535     (regp->vl_api_registration_pool_index,
536      am->shmem_hdr->application_restarts);
537
538   regp->unanswered_pings++;
539
540   /* Failure-to-send due to a stuffed queue is absolutely expected */
541   if (svm_queue_add (q, (u8 *) & mp, 1 /* nowait */ ))
542     vl_msg_api_free (mp);
543
544   am->vlib_rp = save_vlib_rp;
545   am->shmem_hdr = save_shmem_hdr;
546 }
547
548 static void
549 vl_mem_send_client_keepalive_w_reg (api_main_t * am, f64 now,
550                                     vl_api_registration_t ** regpp,
551                                     u32 ** dead_indices,
552                                     u32 ** confused_indices)
553 {
554   vl_api_registration_t *regp = *regpp;
555   if (regp)
556     {
557       /* If we haven't heard from this client recently... */
558       if (regp->last_heard < (now - 10.0))
559         {
560           if (regp->unanswered_pings == 2)
561             {
562               svm_queue_t *q;
563               q = regp->vl_input_queue;
564               if (kill (q->consumer_pid, 0) >= 0)
565                 {
566                   clib_warning ("REAPER: lazy binary API client '%s'",
567                                 regp->name);
568                   regp->unanswered_pings = 0;
569                   regp->last_heard = now;
570                 }
571               else
572                 {
573                   clib_warning ("REAPER: binary API client '%s' died",
574                                 regp->name);
575                   vec_add1 (*dead_indices, regpp - am->vl_clients);
576                 }
577             }
578           else
579             send_memclnt_keepalive (regp, now);
580         }
581       else
582         regp->unanswered_pings = 0;
583     }
584   else
585     {
586       clib_warning ("NULL client registration index %d",
587                     regpp - am->vl_clients);
588       vec_add1 (*confused_indices, regpp - am->vl_clients);
589     }
590 }
591
592 void
593 vl_mem_api_dead_client_scan (api_main_t * am, vl_shmem_hdr_t * shm, f64 now)
594 {
595   vl_api_registration_t **regpp;
596   static u32 *dead_indices;
597   static u32 *confused_indices;
598
599   vec_reset_length (dead_indices);
600   vec_reset_length (confused_indices);
601
602   /* *INDENT-OFF* */
603   pool_foreach (regpp, am->vl_clients, ({
604       vl_mem_send_client_keepalive_w_reg (am, now, regpp, &dead_indices,
605                                           &confused_indices);
606   }));
607   /* *INDENT-ON* */
608
609   /* This should "never happen," but if it does, fix it... */
610   if (PREDICT_FALSE (vec_len (confused_indices) > 0))
611     {
612       int i;
613       for (i = 0; i < vec_len (confused_indices); i++)
614         {
615           pool_put_index (am->vl_clients, confused_indices[i]);
616         }
617     }
618
619   if (PREDICT_FALSE (vec_len (dead_indices) > 0))
620     {
621       int i;
622       svm_region_t *svm;
623       void *oldheap;
624
625       /* Allow the application to clean up its registrations */
626       for (i = 0; i < vec_len (dead_indices); i++)
627         {
628           regpp = pool_elt_at_index (am->vl_clients, dead_indices[i]);
629           if (regpp)
630             {
631               u32 handle;
632
633               handle = vl_msg_api_handle_from_index_and_epoch
634                 (dead_indices[i], shm->application_restarts);
635               (void) vl_api_call_reaper_functions (handle);
636             }
637         }
638
639       svm = am->vlib_rp;
640       pthread_mutex_lock (&svm->mutex);
641       oldheap = svm_push_data_heap (svm);
642
643       for (i = 0; i < vec_len (dead_indices); i++)
644         {
645           regpp = pool_elt_at_index (am->vl_clients, dead_indices[i]);
646           if (regpp)
647             {
648               /* Is this a pairwise SVM segment? */
649               if ((*regpp)->vlib_rp != svm)
650                 {
651                   int i;
652                   svm_region_t *dead_rp = (*regpp)->vlib_rp;
653                   /* Note: account for the memfd header page */
654                   uword virtual_base = dead_rp->virtual_base - MMAP_PAGESIZE;
655                   uword virtual_size = dead_rp->virtual_size + MMAP_PAGESIZE;
656
657                   /* For horizontal scaling, add a hash table... */
658                   for (i = 0; i < vec_len (am->vlib_private_rps); i++)
659                     if (am->vlib_private_rps[i] == dead_rp)
660                       {
661                         vec_delete (am->vlib_private_rps, 1, i);
662                         goto found;
663                       }
664                   clib_warning ("private rp %llx AWOL", dead_rp);
665
666                 found:
667                   /* Kill it, accounting for the memfd header page */
668                   if (munmap ((void *) virtual_base, virtual_size) < 0)
669                     clib_unix_warning ("munmap");
670                   /* Reset the queue-length-address cache */
671                   vec_reset_length (vl_api_queue_cursizes);
672                 }
673               else
674                 {
675                   /* Poison the old registration */
676                   clib_memset (*regpp, 0xF3, sizeof (**regpp));
677                   clib_mem_free (*regpp);
678                 }
679               /* no dangling references, please */
680               *regpp = 0;
681             }
682           else
683             {
684               svm_pop_heap (oldheap);
685               clib_warning ("Duplicate free, client index %d",
686                             regpp - am->vl_clients);
687               oldheap = svm_push_data_heap (svm);
688             }
689         }
690
691       svm_client_scan_this_region_nolock (am->vlib_rp);
692
693       pthread_mutex_unlock (&svm->mutex);
694       svm_pop_heap (oldheap);
695       for (i = 0; i < vec_len (dead_indices); i++)
696         pool_put_index (am->vl_clients, dead_indices[i]);
697     }
698 }
699
700 static inline int
701 void_mem_api_handle_msg_i (api_main_t * am, vlib_main_t * vm,
702                            vlib_node_runtime_t * node, svm_queue_t * q)
703 {
704   uword mp;
705   if (!svm_queue_sub2 (q, (u8 *) & mp))
706     {
707       vl_msg_api_handler_with_vm_node (am, (void *) mp, vm, node);
708       return 0;
709     }
710   return -1;
711 }
712
713 int
714 vl_mem_api_handle_msg_main (vlib_main_t * vm, vlib_node_runtime_t * node)
715 {
716   api_main_t *am = &api_main;
717   return void_mem_api_handle_msg_i (am, vm, node,
718                                     am->shmem_hdr->vl_input_queue);
719 }
720
721 int
722 vl_mem_api_handle_rpc (vlib_main_t * vm, vlib_node_runtime_t * node)
723 {
724   api_main_t *am = &api_main;
725   int i;
726   uword *tmp, mp;
727
728   /*
729    * Swap pending and processing vectors, then process the RPCs
730    * Avoid deadlock conditions by construction.
731    */
732   clib_spinlock_lock_if_init (&vm->pending_rpc_lock);
733   tmp = vm->processing_rpc_requests;
734   vec_reset_length (tmp);
735   vm->processing_rpc_requests = vm->pending_rpc_requests;
736   vm->pending_rpc_requests = tmp;
737   clib_spinlock_unlock_if_init (&vm->pending_rpc_lock);
738
739   /*
740    * RPCs are used to reflect function calls to thread 0
741    * when the underlying code is not thread-safe.
742    *
743    * Grabbing the thread barrier across a set of RPCs
744    * greatly increases efficiency, and avoids
745    * running afoul of the barrier sync holddown timer.
746    * The barrier sync code supports recursive locking.
747    *
748    * We really need to rewrite RPC-based code...
749    */
750   if (PREDICT_TRUE (vec_len (vm->processing_rpc_requests)))
751     {
752       vl_msg_api_barrier_sync ();
753       for (i = 0; i < vec_len (vm->processing_rpc_requests); i++)
754         {
755           mp = vm->processing_rpc_requests[i];
756           vl_msg_api_handler_with_vm_node (am, (void *) mp, vm, node);
757         }
758       vl_msg_api_barrier_release ();
759     }
760
761   return 0;
762 }
763
764 int
765 vl_mem_api_handle_msg_private (vlib_main_t * vm, vlib_node_runtime_t * node,
766                                u32 reg_index)
767 {
768   api_main_t *am = &api_main;
769   vl_shmem_hdr_t *save_shmem_hdr = am->shmem_hdr;
770   svm_region_t *vlib_rp, *save_vlib_rp = am->vlib_rp;
771   svm_queue_t *q;
772   int rv;
773
774   vlib_rp = am->vlib_rp = am->vlib_private_rps[reg_index];
775
776   am->shmem_hdr = (void *) vlib_rp->user_ctx;
777   q = am->shmem_hdr->vl_input_queue;
778
779   rv = void_mem_api_handle_msg_i (am, vm, node, q);
780
781   am->shmem_hdr = save_shmem_hdr;
782   am->vlib_rp = save_vlib_rp;
783
784   return rv;
785 }
786
787 vl_api_registration_t *
788 vl_mem_api_client_index_to_registration (u32 handle)
789 {
790   vl_api_registration_t **regpp;
791   vl_api_registration_t *regp;
792   api_main_t *am = &api_main;
793   vl_shmem_hdr_t *shmem_hdr;
794   u32 index;
795
796   index = vl_msg_api_handle_get_index (handle);
797   regpp = am->vl_clients + index;
798
799   if (pool_is_free (am->vl_clients, regpp))
800     {
801       vl_msg_api_increment_missing_client_counter ();
802       return 0;
803     }
804   regp = *regpp;
805
806   shmem_hdr = (vl_shmem_hdr_t *) regp->shmem_hdr;
807   if (!vl_msg_api_handle_is_valid (handle, shmem_hdr->application_restarts))
808     {
809       vl_msg_api_increment_missing_client_counter ();
810       return 0;
811     }
812
813   return (regp);
814 }
815
816 svm_queue_t *
817 vl_api_client_index_to_input_queue (u32 index)
818 {
819   vl_api_registration_t *regp;
820   api_main_t *am = &api_main;
821
822   /* Special case: vlib trying to send itself a message */
823   if (index == (u32) ~ 0)
824     return (am->shmem_hdr->vl_input_queue);
825
826   regp = vl_mem_api_client_index_to_registration (index);
827   if (!regp)
828     return 0;
829   return (regp->vl_input_queue);
830 }
831
832 static clib_error_t *
833 setup_memclnt_exit (vlib_main_t * vm)
834 {
835   atexit (vl_unmap_shmem);
836   return 0;
837 }
838
839 VLIB_INIT_FUNCTION (setup_memclnt_exit);
840
841 u8 *
842 format_api_message_rings (u8 * s, va_list * args)
843 {
844   api_main_t *am = va_arg (*args, api_main_t *);
845   vl_shmem_hdr_t *shmem_hdr = va_arg (*args, vl_shmem_hdr_t *);
846   int main_segment = va_arg (*args, int);
847   ring_alloc_t *ap;
848   int i;
849
850   if (shmem_hdr == 0)
851     return format (s, "%8s %8s %8s %8s %8s\n",
852                    "Owner", "Size", "Nitems", "Hits", "Misses");
853
854   ap = shmem_hdr->vl_rings;
855
856   for (i = 0; i < vec_len (shmem_hdr->vl_rings); i++)
857     {
858       s = format (s, "%8s %8d %8d %8d %8d\n",
859                   "vlib", ap->size, ap->nitems, ap->hits, ap->misses);
860       ap++;
861     }
862
863   ap = shmem_hdr->client_rings;
864
865   for (i = 0; i < vec_len (shmem_hdr->client_rings); i++)
866     {
867       s = format (s, "%8s %8d %8d %8d %8d\n",
868                   "clnt", ap->size, ap->nitems, ap->hits, ap->misses);
869       ap++;
870     }
871
872   if (main_segment)
873     {
874       s = format (s, "%d ring miss fallback allocations\n", am->ring_misses);
875       s = format
876         (s,
877          "%d application restarts, %d reclaimed msgs, %d garbage collects\n",
878          shmem_hdr->application_restarts, shmem_hdr->restart_reclaims,
879          shmem_hdr->garbage_collects);
880     }
881   return s;
882 }
883
884 static clib_error_t *
885 vl_api_ring_command (vlib_main_t * vm,
886                      unformat_input_t * input, vlib_cli_command_t * cli_cmd)
887 {
888   int i;
889   vl_shmem_hdr_t *shmem_hdr;
890   api_main_t *am = &api_main;
891
892   /* First, dump the primary region rings.. */
893
894   if (am->vlib_primary_rp == 0 || am->vlib_primary_rp->user_ctx == 0)
895     {
896       vlib_cli_output (vm, "Shared memory segment not initialized...\n");
897       return 0;
898     }
899
900   shmem_hdr = (void *) am->vlib_primary_rp->user_ctx;
901
902   vlib_cli_output (vm, "Main API segment rings:");
903
904   vlib_cli_output (vm, "%U", format_api_message_rings, am,
905                    0 /* print header */ , 0 /* notused */ );
906
907   vlib_cli_output (vm, "%U", format_api_message_rings, am,
908                    shmem_hdr, 1 /* main segment */ );
909
910   for (i = 0; i < vec_len (am->vlib_private_rps); i++)
911     {
912       svm_region_t *vlib_rp = am->vlib_private_rps[i];
913       shmem_hdr = (void *) vlib_rp->user_ctx;
914       vl_api_registration_t **regpp;
915       vl_api_registration_t *regp = 0;
916
917       /* For horizontal scaling, add a hash table... */
918       /* *INDENT-OFF* */
919       pool_foreach (regpp, am->vl_clients,
920       ({
921         regp = *regpp;
922         if (regp && regp->vlib_rp == vlib_rp)
923           {
924             vlib_cli_output (vm, "%s segment rings:", regp->name);
925             goto found;
926           }
927       }));
928       vlib_cli_output (vm, "regp %llx not found?", regp);
929       continue;
930       /* *INDENT-ON* */
931     found:
932       vlib_cli_output (vm, "%U", format_api_message_rings, am,
933                        0 /* print header */ , 0 /* notused */ );
934       vlib_cli_output (vm, "%U", format_api_message_rings, am,
935                        shmem_hdr, 0 /* main segment */ );
936     }
937
938   return 0;
939 }
940
941 /*?
942  * Display binary api message allocation ring statistics
943 ?*/
944 /* *INDENT-OFF* */
945 VLIB_CLI_COMMAND (cli_show_api_ring_command, static) =
946 {
947   .path = "show api ring-stats",
948   .short_help = "Message ring statistics",
949   .function = vl_api_ring_command,
950 };
951 /* *INDENT-ON* */
952
953 clib_error_t *
954 vlibmemory_init (vlib_main_t * vm)
955 {
956   api_main_t *am = &api_main;
957   svm_map_region_args_t _a, *a = &_a;
958   u8 *remove_path1, *remove_path2;
959   void vlibsocket_reference (void);
960
961   vlibsocket_reference ();
962
963   /*
964    * By popular request / to avoid support fires, remove any old api segment
965    * files Right Here.
966    */
967   if (am->root_path == 0)
968     {
969       remove_path1 = format (0, "/dev/shm/global_vm%c", 0);
970       remove_path2 = format (0, "/dev/shm/vpe-api%c", 0);
971     }
972   else
973     {
974       remove_path1 = format (0, "/dev/shm/%s-global_vm%c", am->root_path, 0);
975       remove_path2 = format (0, "/dev/shm/%s-vpe-api%c", am->root_path, 0);
976     }
977
978   (void) unlink ((char *) remove_path1);
979   (void) unlink ((char *) remove_path2);
980
981   vec_free (remove_path1);
982   vec_free (remove_path2);
983
984   clib_memset (a, 0, sizeof (*a));
985   a->root_path = am->root_path;
986   a->name = SVM_GLOBAL_REGION_NAME;
987   a->baseva = (am->global_baseva != 0) ?
988     am->global_baseva : +svm_get_global_region_base_va ();
989   a->size = (am->global_size != 0) ? am->global_size : SVM_GLOBAL_REGION_SIZE;
990   a->flags = SVM_FLAGS_NODATA;
991   a->uid = am->api_uid;
992   a->gid = am->api_gid;
993   a->pvt_heap_size =
994     (am->global_pvt_heap_size !=
995      0) ? am->global_pvt_heap_size : SVM_PVT_MHEAP_SIZE;
996
997   svm_region_init_args (a);
998
999   return 0;
1000 }
1001
1002 void
1003 vl_set_memory_region_name (const char *name)
1004 {
1005   api_main_t *am = &api_main;
1006   am->region_name = name;
1007 }
1008
1009 /*
1010  * fd.io coding-style-patch-verification: ON
1011  *
1012  * Local Variables:
1013  * eval: (c-set-style "gnu")
1014  * End:
1015  */