8c6469080d7a612c0749a7cc2bd45a1b70048032
[vpp.git] / src / vlibmemory / memory_shared.c
1 /*
2  *------------------------------------------------------------------
3  * memclnt_shared.c - API message handling, common code for both clients
4  * and the vlib process itself.
5  *
6  *
7  * Copyright (c) 2009 Cisco and/or its affiliates.
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at:
11  *
12  *     http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *------------------------------------------------------------------
20  */
21
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <stddef.h>
25 #include <string.h>
26 #include <unistd.h>
27 #include <signal.h>
28 #include <vppinfra/format.h>
29 #include <vppinfra/byte_order.h>
30 #include <vppinfra/error.h>
31 #include <vlib/vlib.h>
32 #include <vlib/unix/unix.h>
33 #include <vlibmemory/api.h>
34 #include <vlibmemory/unix_shared_memory_queue.h>
35
36 #include <vlibmemory/vl_memory_msg_enum.h>
37
38 #define vl_typedefs
39 #include <vlibmemory/vl_memory_api_h.h>
40 #undef vl_typedefs
41
42 static inline void *
43 vl_msg_api_alloc_internal (int nbytes, int pool, int may_return_null)
44 {
45   int i;
46   msgbuf_t *rv;
47   ring_alloc_t *ap;
48   unix_shared_memory_queue_t *q;
49   void *oldheap;
50   vl_shmem_hdr_t *shmem_hdr;
51   api_main_t *am = &api_main;
52
53   shmem_hdr = am->shmem_hdr;
54
55   if (shmem_hdr == 0)
56     {
57       clib_warning ("shared memory header NULL");
58       return 0;
59     }
60
61   /* account for the msgbuf_t header */
62   nbytes += sizeof (msgbuf_t);
63
64   if (shmem_hdr->vl_rings == 0)
65     {
66       clib_warning ("vl_rings NULL");
67       ASSERT (0);
68       abort ();
69     }
70
71   if (shmem_hdr->client_rings == 0)
72     {
73       clib_warning ("client_rings NULL");
74       ASSERT (0);
75       abort ();
76     }
77
78   ap = pool ? shmem_hdr->vl_rings : shmem_hdr->client_rings;
79   for (i = 0; i < vec_len (ap); i++)
80     {
81       /* Too big? */
82       if (nbytes > ap[i].size)
83         {
84           continue;
85         }
86
87       q = ap[i].rp;
88       if (pool == 0)
89         {
90           pthread_mutex_lock (&q->mutex);
91         }
92       rv = (msgbuf_t *) (&q->data[0] + q->head * q->elsize);
93       /*
94        * Is this item still in use?
95        */
96       if (rv->q)
97         {
98           u32 now = (u32) time (0);
99
100           if (PREDICT_TRUE (rv->gc_mark_timestamp == 0))
101             rv->gc_mark_timestamp = now;
102           else
103             {
104               if (now - rv->gc_mark_timestamp > 10)
105                 {
106                   if (CLIB_DEBUG > 0)
107                     {
108                       u16 *msg_idp, msg_id;
109                       clib_warning
110                         ("garbage collect pool %d ring %d index %d", pool, i,
111                          q->head);
112                       msg_idp = (u16 *) (rv->data);
113                       msg_id = clib_net_to_host_u16 (*msg_idp);
114                       if (msg_id < vec_len (api_main.msg_names))
115                         clib_warning ("msg id %d name %s", (u32) msg_id,
116                                       api_main.msg_names[msg_id]);
117                     }
118                   shmem_hdr->garbage_collects++;
119                   goto collected;
120                 }
121             }
122
123
124           /* yes, loser; try next larger pool */
125           ap[i].misses++;
126           if (pool == 0)
127             pthread_mutex_unlock (&q->mutex);
128           continue;
129         }
130     collected:
131
132       /* OK, we have a winner */
133       ap[i].hits++;
134       /*
135        * Remember the source queue, although we
136        * don't need to know the queue to free the item.
137        */
138       rv->q = q;
139       rv->gc_mark_timestamp = 0;
140       q->head++;
141       if (q->head == q->maxsize)
142         q->head = 0;
143
144       if (pool == 0)
145         pthread_mutex_unlock (&q->mutex);
146       goto out;
147     }
148
149   /*
150    * Request too big, or head element of all size-compatible rings
151    * still in use. Fall back to shared-memory malloc.
152    */
153   am->ring_misses++;
154
155   pthread_mutex_lock (&am->vlib_rp->mutex);
156   oldheap = svm_push_data_heap (am->vlib_rp);
157   if (may_return_null)
158     {
159       rv = clib_mem_alloc_or_null (nbytes);
160       if (PREDICT_FALSE (rv == 0))
161         {
162           svm_pop_heap (oldheap);
163           pthread_mutex_unlock (&am->vlib_rp->mutex);
164           return 0;
165         }
166     }
167   else
168     rv = clib_mem_alloc (nbytes);
169
170   rv->q = 0;
171   svm_pop_heap (oldheap);
172   pthread_mutex_unlock (&am->vlib_rp->mutex);
173
174 out:
175   rv->data_len = htonl (nbytes - sizeof (msgbuf_t));
176   return (rv->data);
177 }
178
179 void *
180 vl_msg_api_alloc (int nbytes)
181 {
182   int pool;
183   api_main_t *am = &api_main;
184   vl_shmem_hdr_t *shmem_hdr = am->shmem_hdr;
185
186   /*
187    * Clients use pool-0, vlib proc uses pool 1
188    */
189   pool = (am->our_pid == shmem_hdr->vl_pid);
190   return vl_msg_api_alloc_internal (nbytes, pool, 0 /* may_return_null */ );
191 }
192
193 void *
194 vl_msg_api_alloc_or_null (int nbytes)
195 {
196   int pool;
197   api_main_t *am = &api_main;
198   vl_shmem_hdr_t *shmem_hdr = am->shmem_hdr;
199
200   pool = (am->our_pid == shmem_hdr->vl_pid);
201   return vl_msg_api_alloc_internal (nbytes, pool, 1 /* may_return_null */ );
202 }
203
204 void *
205 vl_msg_api_alloc_as_if_client (int nbytes)
206 {
207   return vl_msg_api_alloc_internal (nbytes, 0, 0 /* may_return_null */ );
208 }
209
210 void *
211 vl_msg_api_alloc_as_if_client_or_null (int nbytes)
212 {
213   return vl_msg_api_alloc_internal (nbytes, 0, 1 /* may_return_null */ );
214 }
215
216 void
217 vl_msg_api_free (void *a)
218 {
219   msgbuf_t *rv;
220   void *oldheap;
221   api_main_t *am = &api_main;
222
223   rv = (msgbuf_t *) (((u8 *) a) - offsetof (msgbuf_t, data));
224
225   /*
226    * Here's the beauty of the scheme.  Only one proc/thread has
227    * control of a given message buffer. To free a buffer, we just clear the
228    * queue field, and leave. No locks, no hits, no errors...
229    */
230   if (rv->q)
231     {
232       rv->q = 0;
233       rv->gc_mark_timestamp = 0;
234       return;
235     }
236
237   pthread_mutex_lock (&am->vlib_rp->mutex);
238   oldheap = svm_push_data_heap (am->vlib_rp);
239   clib_mem_free (rv);
240   svm_pop_heap (oldheap);
241   pthread_mutex_unlock (&am->vlib_rp->mutex);
242 }
243
244 static void
245 vl_msg_api_free_nolock (void *a)
246 {
247   msgbuf_t *rv;
248   void *oldheap;
249   api_main_t *am = &api_main;
250
251   rv = (msgbuf_t *) (((u8 *) a) - offsetof (msgbuf_t, data));
252   /*
253    * Here's the beauty of the scheme.  Only one proc/thread has
254    * control of a given message buffer. To free a buffer, we just clear the
255    * queue field, and leave. No locks, no hits, no errors...
256    */
257   if (rv->q)
258     {
259       rv->q = 0;
260       return;
261     }
262
263   oldheap = svm_push_data_heap (am->vlib_rp);
264   clib_mem_free (rv);
265   svm_pop_heap (oldheap);
266 }
267
268 void
269 vl_set_memory_root_path (const char *name)
270 {
271   api_main_t *am = &api_main;
272
273   am->root_path = name;
274 }
275
276 void
277 vl_set_memory_uid (int uid)
278 {
279   api_main_t *am = &api_main;
280
281   am->api_uid = uid;
282 }
283
284 void
285 vl_set_memory_gid (int gid)
286 {
287   api_main_t *am = &api_main;
288
289   am->api_gid = gid;
290 }
291
292 void
293 vl_set_global_memory_baseva (u64 baseva)
294 {
295   api_main_t *am = &api_main;
296
297   am->global_baseva = baseva;
298 }
299
300 void
301 vl_set_global_memory_size (u64 size)
302 {
303   api_main_t *am = &api_main;
304
305   am->global_size = size;
306 }
307
308 void
309 vl_set_api_memory_size (u64 size)
310 {
311   api_main_t *am = &api_main;
312
313   am->api_size = size;
314 }
315
316 void
317 vl_set_global_pvt_heap_size (u64 size)
318 {
319   api_main_t *am = &api_main;
320
321   am->global_pvt_heap_size = size;
322 }
323
324 void
325 vl_set_api_pvt_heap_size (u64 size)
326 {
327   api_main_t *am = &api_main;
328
329   am->api_pvt_heap_size = size;
330 }
331
332 int
333 vl_map_shmem (const char *region_name, int is_vlib)
334 {
335   svm_map_region_args_t _a, *a = &_a;
336   svm_region_t *vlib_rp, *root_rp;
337   void *oldheap;
338   vl_shmem_hdr_t *shmem_hdr = 0;
339   api_main_t *am = &api_main;
340   int i, rv;
341   struct timespec ts, tsrem;
342   u32 vlib_input_queue_length;
343   char *vpe_api_region_suffix = "-vpe-api";
344
345   memset (a, 0, sizeof (*a));
346
347   if (strstr (region_name, vpe_api_region_suffix))
348     {
349       u8 *root_path = format (0, "%s", region_name);
350       _vec_len (root_path) = (vec_len (root_path) -
351                               strlen (vpe_api_region_suffix));
352       vec_terminate_c_string (root_path);
353       a->root_path = (const char *) root_path;
354       am->root_path = (const char *) root_path;
355     }
356
357   if (is_vlib == 0)
358     {
359       rv = svm_region_init_chroot (am->root_path);
360       if (rv)
361         return rv;
362     }
363
364   if (a->root_path != NULL)
365     {
366       a->name = "/vpe-api";
367     }
368   else
369     a->name = region_name;
370   a->size = am->api_size ? am->api_size : (16 << 20);
371   a->flags = SVM_FLAGS_MHEAP;
372   a->uid = am->api_uid;
373   a->gid = am->api_gid;
374   a->pvt_heap_size = am->api_pvt_heap_size;
375
376   vlib_rp = svm_region_find_or_create (a);
377
378   if (vlib_rp == 0)
379     return (-2);
380
381   pthread_mutex_lock (&vlib_rp->mutex);
382   /* Has someone else set up the shared-memory variable table? */
383   if (vlib_rp->user_ctx)
384     {
385       am->shmem_hdr = (void *) vlib_rp->user_ctx;
386       am->our_pid = getpid ();
387       if (is_vlib)
388         {
389           unix_shared_memory_queue_t *q;
390           uword old_msg;
391           /*
392            * application restart. Reset cached pids, API message
393            * rings, list of clients; otherwise, various things
394            * fail. (e.g. queue non-empty notification)
395            */
396
397           /* ghosts keep the region from disappearing properly */
398           svm_client_scan_this_region_nolock (vlib_rp);
399           am->shmem_hdr->application_restarts++;
400           q = am->shmem_hdr->vl_input_queue;
401           am->shmem_hdr->vl_pid = getpid ();
402           q->consumer_pid = am->shmem_hdr->vl_pid;
403           /* Drain the input queue, freeing msgs */
404           for (i = 0; i < 10; i++)
405             {
406               if (pthread_mutex_trylock (&q->mutex) == 0)
407                 {
408                   pthread_mutex_unlock (&q->mutex);
409                   goto mutex_ok;
410                 }
411               ts.tv_sec = 0;
412               ts.tv_nsec = 10000 * 1000;        /* 10 ms */
413               while (nanosleep (&ts, &tsrem) < 0)
414                 ts = tsrem;
415             }
416           /* Mutex buggered, "fix" it */
417           memset (&q->mutex, 0, sizeof (q->mutex));
418           clib_warning ("forcibly release main input queue mutex");
419
420         mutex_ok:
421           am->vlib_rp = vlib_rp;
422           while (unix_shared_memory_queue_sub (q,
423                                                (u8 *) & old_msg,
424                                                1 /* nowait */ )
425                  != -2 /* queue underflow */ )
426             {
427               vl_msg_api_free_nolock ((void *) old_msg);
428               am->shmem_hdr->restart_reclaims++;
429             }
430           pthread_mutex_unlock (&vlib_rp->mutex);
431           root_rp = svm_get_root_rp ();
432           ASSERT (root_rp);
433           /* Clean up the root region client list */
434           pthread_mutex_lock (&root_rp->mutex);
435           svm_client_scan_this_region_nolock (root_rp);
436           pthread_mutex_unlock (&root_rp->mutex);
437         }
438       else
439         {
440           pthread_mutex_unlock (&vlib_rp->mutex);
441         }
442       am->vlib_rp = vlib_rp;
443       vec_add1 (am->mapped_shmem_regions, vlib_rp);
444       return 0;
445     }
446   /* Clients simply have to wait... */
447   if (!is_vlib)
448     {
449       pthread_mutex_unlock (&vlib_rp->mutex);
450
451       /* Wait up to 100 seconds... */
452       for (i = 0; i < 10000; i++)
453         {
454           ts.tv_sec = 0;
455           ts.tv_nsec = 10000 * 1000;    /* 10 ms */
456           while (nanosleep (&ts, &tsrem) < 0)
457             ts = tsrem;
458           if (vlib_rp->user_ctx)
459             goto ready;
460         }
461       /* Clean up and leave... */
462       svm_region_unmap (vlib_rp);
463       clib_warning ("region init fail");
464       return (-2);
465
466     ready:
467       am->shmem_hdr = (void *) vlib_rp->user_ctx;
468       am->our_pid = getpid ();
469       am->vlib_rp = vlib_rp;
470       vec_add1 (am->mapped_shmem_regions, vlib_rp);
471       return 0;
472     }
473
474   /* Nope, it's our problem... */
475
476   oldheap = svm_push_data_heap (vlib_rp);
477
478   vec_validate (shmem_hdr, 0);
479   shmem_hdr->version = VL_SHM_VERSION;
480
481   /* vlib main input queue */
482   vlib_input_queue_length = 1024;
483   if (am->vlib_input_queue_length)
484     vlib_input_queue_length = am->vlib_input_queue_length;
485
486   shmem_hdr->vl_input_queue =
487     unix_shared_memory_queue_init (vlib_input_queue_length, sizeof (uword),
488                                    getpid (), am->vlib_signal);
489
490   /* Set up the msg ring allocator */
491 #define _(sz,n)                                                 \
492     do {                                                        \
493         ring_alloc_t _rp;                                       \
494         _rp.rp = unix_shared_memory_queue_init ((n), (sz), 0, 0); \
495         _rp.size = (sz);                                        \
496         _rp.nitems = n;                                         \
497         _rp.hits = 0;                                           \
498         _rp.misses = 0;                                         \
499         vec_add1(shmem_hdr->vl_rings, _rp);                     \
500     } while (0);
501
502   foreach_vl_aring_size;
503 #undef _
504
505 #define _(sz,n)                                                 \
506     do {                                                        \
507         ring_alloc_t _rp;                                       \
508         _rp.rp = unix_shared_memory_queue_init ((n), (sz), 0, 0); \
509         _rp.size = (sz);                                        \
510         _rp.nitems = n;                                         \
511         _rp.hits = 0;                                           \
512         _rp.misses = 0;                                         \
513         vec_add1(shmem_hdr->client_rings, _rp);                 \
514     } while (0);
515
516   foreach_clnt_aring_size;
517 #undef _
518
519   am->shmem_hdr = shmem_hdr;
520   am->vlib_rp = vlib_rp;
521   am->our_pid = getpid ();
522   if (is_vlib)
523     am->shmem_hdr->vl_pid = am->our_pid;
524
525   svm_pop_heap (oldheap);
526
527   /*
528    * After absolutely everything that a client might see is set up,
529    * declare the shmem region valid
530    */
531   vlib_rp->user_ctx = shmem_hdr;
532
533   pthread_mutex_unlock (&vlib_rp->mutex);
534   vec_add1 (am->mapped_shmem_regions, vlib_rp);
535   return 0;
536 }
537
538 void
539 vl_register_mapped_shmem_region (svm_region_t * rp)
540 {
541   api_main_t *am = &api_main;
542
543   vec_add1 (am->mapped_shmem_regions, rp);
544 }
545
546 void
547 vl_unmap_shmem (void)
548 {
549   svm_region_t *rp;
550   int i;
551   api_main_t *am = &api_main;
552
553   if (!svm_get_root_rp ())
554     return;
555
556   for (i = 0; i < vec_len (am->mapped_shmem_regions); i++)
557     {
558       rp = am->mapped_shmem_regions[i];
559       svm_region_unmap (rp);
560     }
561
562   vec_free (am->mapped_shmem_regions);
563   am->shmem_hdr = 0;
564
565   svm_region_exit ();
566   /* $$$ more careful cleanup, valgrind run... */
567   vec_free (am->msg_handlers);
568   vec_free (am->msg_endian_handlers);
569   vec_free (am->msg_print_handlers);
570 }
571
572 void
573 vl_msg_api_send_shmem (unix_shared_memory_queue_t * q, u8 * elem)
574 {
575   api_main_t *am = &api_main;
576   uword *trace = (uword *) elem;
577
578   if (am->tx_trace && am->tx_trace->enabled)
579     vl_msg_api_trace (am, am->tx_trace, (void *) trace[0]);
580
581   (void) unix_shared_memory_queue_add (q, elem, 0 /* nowait */ );
582 }
583
584 void
585 vl_msg_api_send_shmem_nolock (unix_shared_memory_queue_t * q, u8 * elem)
586 {
587   api_main_t *am = &api_main;
588   uword *trace = (uword *) elem;
589
590   if (am->tx_trace && am->tx_trace->enabled)
591     vl_msg_api_trace (am, am->tx_trace, (void *) trace[0]);
592
593   (void) unix_shared_memory_queue_add_nolock (q, elem);
594 }
595
596 u32
597 vl_api_get_msg_index (u8 * name_and_crc)
598 {
599   api_main_t *am = &api_main;
600   uword *p;
601
602   if (am->msg_index_by_name_and_crc)
603     {
604       p = hash_get_mem (am->msg_index_by_name_and_crc, name_and_crc);
605       if (p)
606         return p[0];
607     }
608   return ~0;
609 }
610
611 static inline vl_api_registration_t *
612 vl_api_client_index_to_registration_internal (u32 handle)
613 {
614   vl_api_registration_t **regpp;
615   vl_api_registration_t *regp;
616   api_main_t *am = &api_main;
617   u32 index;
618
619   index = vl_msg_api_handle_get_index (handle);
620   if ((am->shmem_hdr->application_restarts & VL_API_EPOCH_MASK)
621       != vl_msg_api_handle_get_epoch (handle))
622     {
623       vl_msg_api_increment_missing_client_counter ();
624       return 0;
625     }
626
627   regpp = am->vl_clients + index;
628
629   if (pool_is_free (am->vl_clients, regpp))
630     {
631       vl_msg_api_increment_missing_client_counter ();
632       return 0;
633     }
634   regp = *regpp;
635   return (regp);
636 }
637
638 vl_api_registration_t *
639 vl_api_client_index_to_registration (u32 index)
640 {
641   return (vl_api_client_index_to_registration_internal (index));
642 }
643
644 unix_shared_memory_queue_t *
645 vl_api_client_index_to_input_queue (u32 index)
646 {
647   vl_api_registration_t *regp;
648   api_main_t *am = &api_main;
649
650   /* Special case: vlib trying to send itself a message */
651   if (index == (u32) ~ 0)
652     return (am->shmem_hdr->vl_input_queue);
653
654   regp = vl_api_client_index_to_registration_internal (index);
655   if (!regp)
656     return 0;
657   return (regp->vl_input_queue);
658 }
659
660 /*
661  * fd.io coding-style-patch-verification: ON
662  *
663  * Local Variables:
664  * eval: (c-set-style "gnu")
665  * End:
666  */