api: multiple connections per process
[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
29 #include <vppinfra/format.h>
30 #include <vppinfra/byte_order.h>
31 #include <vppinfra/error.h>
32 #include <vppinfra/elog.h>
33 #include <svm/queue.h>
34 #include <vlib/vlib.h>
35 #include <vlib/unix/unix.h>
36 #include <vlibmemory/memory_api.h>
37 #include <vlibmemory/vl_memory_msg_enum.h>
38 #include <vlibapi/api_common.h>
39
40 #define vl_typedefs
41 #include <vlibmemory/vl_memory_api_h.h>
42 #undef vl_typedefs
43
44 #define DEBUG_MESSAGE_BUFFER_OVERRUN 0
45
46 CLIB_NOSANITIZE_ADDR static inline void *
47 vl_msg_api_alloc_internal (svm_region_t * vlib_rp, int nbytes, int pool,
48                            int may_return_null)
49 {
50   int i;
51   msgbuf_t *rv;
52   ring_alloc_t *ap;
53   svm_queue_t *q;
54   void *oldheap;
55   vl_shmem_hdr_t *shmem_hdr;
56   api_main_t *am = vlibapi_get_main ();
57
58   shmem_hdr = (void *) vlib_rp->user_ctx;
59
60 #if DEBUG_MESSAGE_BUFFER_OVERRUN > 0
61   nbytes += 4;
62 #endif
63
64   ASSERT (pool == 0 || vlib_get_thread_index () == 0);
65
66   if (shmem_hdr == 0)
67     {
68       clib_warning ("shared memory header NULL");
69       return 0;
70     }
71
72   /* account for the msgbuf_t header */
73   nbytes += sizeof (msgbuf_t);
74
75   if (shmem_hdr->vl_rings == 0)
76     {
77       clib_warning ("vl_rings NULL");
78       ASSERT (0);
79       abort ();
80     }
81
82   if (shmem_hdr->client_rings == 0)
83     {
84       clib_warning ("client_rings NULL");
85       ASSERT (0);
86       abort ();
87     }
88
89   ap = pool ? shmem_hdr->vl_rings : shmem_hdr->client_rings;
90   for (i = 0; i < vec_len (ap); i++)
91     {
92       /* Too big? */
93       if (nbytes > ap[i].size)
94         {
95           continue;
96         }
97
98       q = ap[i].rp;
99       if (pool == 0)
100         {
101           pthread_mutex_lock (&q->mutex);
102         }
103       rv = (msgbuf_t *) (&q->data[0] + q->head * q->elsize);
104       /*
105        * Is this item still in use?
106        */
107       if (rv->q)
108         {
109           u32 now = (u32) time (0);
110
111           if (PREDICT_TRUE (rv->gc_mark_timestamp == 0))
112             rv->gc_mark_timestamp = now;
113           else
114             {
115               if (now - rv->gc_mark_timestamp > 10)
116                 {
117                   if (CLIB_DEBUG > 0)
118                     {
119                       u16 *msg_idp, msg_id;
120                       clib_warning
121                         ("garbage collect pool %d ring %d index %d", pool, i,
122                          q->head);
123                       msg_idp = (u16 *) (rv->data);
124                       msg_id = clib_net_to_host_u16 (*msg_idp);
125                       if (msg_id < vec_len (vlibapi_get_main ()->msg_names))
126                         clib_warning ("msg id %d name %s", (u32) msg_id,
127                                       vlibapi_get_main ()->msg_names[msg_id]);
128                     }
129                   shmem_hdr->garbage_collects++;
130                   goto collected;
131                 }
132             }
133
134
135           /* yes, loser; try next larger pool */
136           ap[i].misses++;
137           if (pool == 0)
138             pthread_mutex_unlock (&q->mutex);
139           continue;
140         }
141     collected:
142
143       /* OK, we have a winner */
144       ap[i].hits++;
145       /*
146        * Remember the source queue, although we
147        * don't need to know the queue to free the item.
148        */
149       rv->q = q;
150       rv->gc_mark_timestamp = 0;
151       q->head++;
152       if (q->head == q->maxsize)
153         q->head = 0;
154
155       if (pool == 0)
156         pthread_mutex_unlock (&q->mutex);
157       goto out;
158     }
159
160   /*
161    * Request too big, or head element of all size-compatible rings
162    * still in use. Fall back to shared-memory malloc.
163    */
164   am->ring_misses++;
165
166   pthread_mutex_lock (&vlib_rp->mutex);
167   oldheap = svm_push_data_heap (vlib_rp);
168   if (may_return_null)
169     {
170       rv = clib_mem_alloc_or_null (nbytes);
171       if (PREDICT_FALSE (rv == 0))
172         {
173           svm_pop_heap (oldheap);
174           pthread_mutex_unlock (&vlib_rp->mutex);
175           return 0;
176         }
177     }
178   else
179     rv = clib_mem_alloc (nbytes);
180
181   rv->q = 0;
182   rv->gc_mark_timestamp = 0;
183   svm_pop_heap (oldheap);
184   pthread_mutex_unlock (&vlib_rp->mutex);
185
186 out:
187 #if DEBUG_MESSAGE_BUFFER_OVERRUN > 0
188   {
189     nbytes -= 4;
190     u32 *overrun;
191     overrun = (u32 *) (rv->data + nbytes - sizeof (msgbuf_t));
192     *overrun = 0x1badbabe;
193   }
194 #endif
195   rv->data_len = htonl (nbytes - sizeof (msgbuf_t));
196
197   VL_MSG_API_UNPOISON (rv->data);
198   return (rv->data);
199 }
200
201 void *
202 vl_msg_api_alloc (int nbytes)
203 {
204   int pool;
205   api_main_t *am = vlibapi_get_main ();
206   vl_shmem_hdr_t *shmem_hdr = am->shmem_hdr;
207
208   /*
209    * Clients use pool-0, vlib proc uses pool 1
210    */
211   pool = (am->our_pid == shmem_hdr->vl_pid);
212   return vl_msg_api_alloc_internal (am->vlib_rp, nbytes, pool,
213                                     0 /* may_return_null */ );
214 }
215
216 void *
217 vl_msg_api_alloc_zero (int nbytes)
218 {
219   void *ret;
220
221   ret = vl_msg_api_alloc (nbytes);
222   clib_memset (ret, 0, nbytes);
223   return ret;
224 }
225
226 void *
227 vl_msg_api_alloc_or_null (int nbytes)
228 {
229   int pool;
230   api_main_t *am = vlibapi_get_main ();
231   vl_shmem_hdr_t *shmem_hdr = am->shmem_hdr;
232
233   pool = (am->our_pid == shmem_hdr->vl_pid);
234   return vl_msg_api_alloc_internal (am->vlib_rp, nbytes, pool,
235                                     1 /* may_return_null */ );
236 }
237
238 void *
239 vl_msg_api_alloc_as_if_client (int nbytes)
240 {
241   api_main_t *am = vlibapi_get_main ();
242   return vl_msg_api_alloc_internal (am->vlib_rp, nbytes, 0,
243                                     0 /* may_return_null */ );
244 }
245
246 void *
247 vl_msg_api_alloc_zero_as_if_client (int nbytes)
248 {
249   void *ret;
250
251   ret = vl_msg_api_alloc_as_if_client (nbytes);
252   clib_memset (ret, 0, nbytes);
253   return ret;
254 }
255
256 void *
257 vl_msg_api_alloc_as_if_client_or_null (int nbytes)
258 {
259   api_main_t *am = vlibapi_get_main ();
260   return vl_msg_api_alloc_internal (am->vlib_rp, nbytes, 0,
261                                     1 /* may_return_null */ );
262 }
263
264 void *
265 vl_mem_api_alloc_as_if_client_w_reg (vl_api_registration_t * reg, int nbytes)
266 {
267   return vl_msg_api_alloc_internal (reg->vlib_rp, nbytes, 0,
268                                     0 /* may_return_null */ );
269 }
270
271 void
272 vl_msg_api_free_w_region (svm_region_t * vlib_rp, void *a)
273 {
274   msgbuf_t *rv;
275   void *oldheap;
276
277   rv = (msgbuf_t *) (((u8 *) a) - offsetof (msgbuf_t, data));
278
279   /*
280    * Here's the beauty of the scheme.  Only one proc/thread has
281    * control of a given message buffer. To free a buffer, we just clear the
282    * queue field, and leave. No locks, no hits, no errors...
283    */
284   if (rv->q)
285     {
286       rv->q = 0;
287       rv->gc_mark_timestamp = 0;
288 #if DEBUG_MESSAGE_BUFFER_OVERRUN > 0
289       {
290         u32 *overrun;
291         overrun = (u32 *) (rv->data + ntohl (rv->data_len));
292         ASSERT (*overrun == 0x1badbabe);
293       }
294 #endif
295       VL_MSG_API_POISON (rv->data);
296       return;
297     }
298
299   pthread_mutex_lock (&vlib_rp->mutex);
300   oldheap = svm_push_data_heap (vlib_rp);
301
302 #if DEBUG_MESSAGE_BUFFER_OVERRUN > 0
303   {
304     u32 *overrun;
305     overrun = (u32 *) (rv->data + ntohl (rv->data_len));
306     ASSERT (*overrun == 0x1badbabe);
307   }
308 #endif
309
310   clib_mem_free (rv);
311   svm_pop_heap (oldheap);
312   pthread_mutex_unlock (&vlib_rp->mutex);
313 }
314
315 void
316 vl_msg_api_free (void *a)
317 {
318   api_main_t *am = vlibapi_get_main ();
319   vl_msg_api_free_w_region (am->vlib_rp, a);
320 }
321
322 static void
323 vl_msg_api_free_nolock (void *a)
324 {
325   msgbuf_t *rv;
326   void *oldheap;
327   api_main_t *am = vlibapi_get_main ();
328
329   rv = (msgbuf_t *) (((u8 *) a) - offsetof (msgbuf_t, data));
330   /*
331    * Here's the beauty of the scheme.  Only one proc/thread has
332    * control of a given message buffer. To free a buffer, we just clear the
333    * queue field, and leave. No locks, no hits, no errors...
334    */
335   if (rv->q)
336     {
337       rv->q = 0;
338       VL_MSG_API_POISON (rv->data);
339       return;
340     }
341
342   oldheap = svm_push_data_heap (am->vlib_rp);
343   clib_mem_free (rv);
344   svm_pop_heap (oldheap);
345 }
346
347 void
348 vl_set_memory_root_path (const char *name)
349 {
350   api_main_t *am = vlibapi_get_main ();
351
352   am->root_path = name;
353 }
354
355 void
356 vl_set_memory_uid (int uid)
357 {
358   api_main_t *am = vlibapi_get_main ();
359
360   am->api_uid = uid;
361 }
362
363 void
364 vl_set_memory_gid (int gid)
365 {
366   api_main_t *am = vlibapi_get_main ();
367
368   am->api_gid = gid;
369 }
370
371 void
372 vl_set_global_memory_baseva (u64 baseva)
373 {
374   api_main_t *am = vlibapi_get_main ();
375
376   am->global_baseva = baseva;
377 }
378
379 void
380 vl_set_global_memory_size (u64 size)
381 {
382   api_main_t *am = vlibapi_get_main ();
383
384   am->global_size = size;
385 }
386
387 void
388 vl_set_api_memory_size (u64 size)
389 {
390   api_main_t *am = vlibapi_get_main ();
391
392   am->api_size = size;
393 }
394
395 void
396 vl_set_global_pvt_heap_size (u64 size)
397 {
398   api_main_t *am = vlibapi_get_main ();
399
400   am->global_pvt_heap_size = size;
401 }
402
403 void
404 vl_set_api_pvt_heap_size (u64 size)
405 {
406   api_main_t *am = vlibapi_get_main ();
407
408   am->api_pvt_heap_size = size;
409 }
410
411 static void
412 vl_api_default_mem_config (vl_shmem_hdr_t * shmem_hdr)
413 {
414   api_main_t *am = vlibapi_get_main ();
415   u32 vlib_input_queue_length;
416
417   /* vlib main input queue */
418   vlib_input_queue_length = 1024;
419   if (am->vlib_input_queue_length)
420     vlib_input_queue_length = am->vlib_input_queue_length;
421
422   shmem_hdr->vl_input_queue =
423     svm_queue_alloc_and_init (vlib_input_queue_length, sizeof (uword),
424                               getpid ());
425
426 #define _(sz,n)                                                 \
427     do {                                                        \
428         ring_alloc_t _rp;                                       \
429         _rp.rp = svm_queue_alloc_and_init ((n), (sz), 0);       \
430         _rp.size = (sz);                                        \
431         _rp.nitems = n;                                         \
432         _rp.hits = 0;                                           \
433         _rp.misses = 0;                                         \
434         vec_add1(shmem_hdr->vl_rings, _rp);                     \
435     } while (0);
436
437   foreach_vl_aring_size;
438 #undef _
439
440 #define _(sz,n)                                                 \
441     do {                                                        \
442         ring_alloc_t _rp;                                       \
443         _rp.rp = svm_queue_alloc_and_init ((n), (sz), 0);       \
444         _rp.size = (sz);                                        \
445         _rp.nitems = n;                                         \
446         _rp.hits = 0;                                           \
447         _rp.misses = 0;                                         \
448         vec_add1(shmem_hdr->client_rings, _rp);                 \
449     } while (0);
450
451   foreach_clnt_aring_size;
452 #undef _
453 }
454
455 void
456 vl_api_mem_config (vl_shmem_hdr_t * hdr, vl_api_shm_elem_config_t * config)
457 {
458   vl_api_shm_elem_config_t *c;
459   ring_alloc_t *rp;
460   u32 size;
461
462   if (!config)
463     {
464       vl_api_default_mem_config (hdr);
465       return;
466     }
467
468   vec_foreach (c, config)
469   {
470     switch (c->type)
471       {
472       case VL_API_QUEUE:
473         hdr->vl_input_queue = svm_queue_alloc_and_init (c->count, c->size,
474                                                         getpid ());
475         continue;
476       case VL_API_VLIB_RING:
477         vec_add2 (hdr->vl_rings, rp, 1);
478         break;
479       case VL_API_CLIENT_RING:
480         vec_add2 (hdr->client_rings, rp, 1);
481         break;
482       default:
483         clib_warning ("unknown config type: %d", c->type);
484         continue;
485       }
486
487     size = sizeof (ring_alloc_t) + c->size;
488     rp->rp = svm_queue_alloc_and_init (c->count, size, 0);
489     rp->size = size;
490     rp->nitems = c->count;
491     rp->hits = 0;
492     rp->misses = 0;
493   }
494 }
495
496 void
497 vl_init_shmem (svm_region_t * vlib_rp, vl_api_shm_elem_config_t * config,
498                int is_vlib, int is_private_region)
499 {
500   api_main_t *am = vlibapi_get_main ();
501   vl_shmem_hdr_t *shmem_hdr = 0;
502   void *oldheap;
503   ASSERT (vlib_rp);
504
505   /* $$$$ need private region config parameters */
506
507   oldheap = svm_push_data_heap (vlib_rp);
508
509   vec_validate (shmem_hdr, 0);
510   shmem_hdr->version = VL_SHM_VERSION;
511   shmem_hdr->clib_file_index = VL_API_INVALID_FI;
512
513   /* Set up the queue and msg ring allocator */
514   vl_api_mem_config (shmem_hdr, config);
515
516   if (is_private_region == 0)
517     {
518       am->shmem_hdr = shmem_hdr;
519       am->vlib_rp = vlib_rp;
520       am->our_pid = getpid ();
521       if (is_vlib)
522         am->shmem_hdr->vl_pid = am->our_pid;
523     }
524   else
525     shmem_hdr->vl_pid = am->our_pid;
526
527   svm_pop_heap (oldheap);
528
529   /*
530    * After absolutely everything that a client might see is set up,
531    * declare the shmem region valid
532    */
533   vlib_rp->user_ctx = shmem_hdr;
534
535   pthread_mutex_unlock (&vlib_rp->mutex);
536 }
537
538 int
539 vl_map_shmem (const char *region_name, int is_vlib)
540 {
541   svm_map_region_args_t _a, *a = &_a;
542   svm_region_t *vlib_rp, *root_rp;
543   api_main_t *am = vlibapi_get_main ();
544   int i;
545   struct timespec ts, tsrem;
546   char *vpe_api_region_suffix = "-vpe-api";
547
548   clib_memset (a, 0, sizeof (*a));
549
550   if (strstr (region_name, vpe_api_region_suffix))
551     {
552       u8 *root_path = format (0, "%s", region_name);
553       _vec_len (root_path) = (vec_len (root_path) -
554                               strlen (vpe_api_region_suffix));
555       vec_terminate_c_string (root_path);
556       a->root_path = (const char *) root_path;
557       am->root_path = (const char *) root_path;
558     }
559
560   if (is_vlib == 0)
561     {
562       int tfd;
563       u8 *api_name;
564       /*
565        * Clients wait for vpp to set up the root / API regioins
566        */
567       if (am->root_path)
568         api_name = format (0, "/dev/shm/%s-%s%c", am->root_path,
569                            region_name + 1, 0);
570       else
571         api_name = format (0, "/dev/shm%s%c", region_name, 0);
572
573       /* Wait up to 100 seconds... */
574       for (i = 0; i < 10000; i++)
575         {
576           ts.tv_sec = 0;
577           ts.tv_nsec = 10000 * 1000;    /* 10 ms */
578           while (nanosleep (&ts, &tsrem) < 0)
579             ts = tsrem;
580           tfd = open ((char *) api_name, O_RDWR);
581           if (tfd >= 0)
582             break;
583         }
584       vec_free (api_name);
585       if (tfd < 0)
586         {
587           clib_warning ("region init fail");
588           return -2;
589         }
590       close (tfd);
591       svm_region_init_chroot_uid_gid (am->root_path, getuid (), getgid ());
592     }
593
594   if (a->root_path != NULL)
595     {
596       a->name = "/vpe-api";
597     }
598   else
599     a->name = region_name;
600   a->size = am->api_size ? am->api_size : (16 << 20);
601   a->flags = SVM_FLAGS_MHEAP;
602   a->uid = am->api_uid;
603   a->gid = am->api_gid;
604   a->pvt_heap_size = am->api_pvt_heap_size;
605
606   vlib_rp = svm_region_find_or_create (a);
607
608   if (vlib_rp == 0)
609     return (-2);
610
611   pthread_mutex_lock (&vlib_rp->mutex);
612   /* Has someone else set up the shared-memory variable table? */
613   if (vlib_rp->user_ctx)
614     {
615       am->shmem_hdr = (void *) vlib_rp->user_ctx;
616       am->our_pid = getpid ();
617       if (is_vlib)
618         {
619           svm_queue_t *q;
620           uword old_msg;
621           /*
622            * application restart. Reset cached pids, API message
623            * rings, list of clients; otherwise, various things
624            * fail. (e.g. queue non-empty notification)
625            */
626
627           /* ghosts keep the region from disappearing properly */
628           svm_client_scan_this_region_nolock (vlib_rp);
629           am->shmem_hdr->application_restarts++;
630           q = am->shmem_hdr->vl_input_queue;
631           am->shmem_hdr->vl_pid = getpid ();
632           q->consumer_pid = am->shmem_hdr->vl_pid;
633           /* Drain the input queue, freeing msgs */
634           for (i = 0; i < 10; i++)
635             {
636               if (pthread_mutex_trylock (&q->mutex) == 0)
637                 {
638                   pthread_mutex_unlock (&q->mutex);
639                   goto mutex_ok;
640                 }
641               ts.tv_sec = 0;
642               ts.tv_nsec = 10000 * 1000;        /* 10 ms */
643               while (nanosleep (&ts, &tsrem) < 0)
644                 ts = tsrem;
645             }
646           /* Mutex buggered, "fix" it */
647           clib_memset (&q->mutex, 0, sizeof (q->mutex));
648           clib_warning ("forcibly release main input queue mutex");
649
650         mutex_ok:
651           am->vlib_rp = vlib_rp;
652           while (svm_queue_sub (q, (u8 *) & old_msg, SVM_Q_NOWAIT, 0)
653                  != -2 /* queue underflow */ )
654             {
655               vl_msg_api_free_nolock ((void *) old_msg);
656               am->shmem_hdr->restart_reclaims++;
657             }
658           pthread_mutex_unlock (&vlib_rp->mutex);
659           root_rp = svm_get_root_rp ();
660           ASSERT (root_rp);
661           /* Clean up the root region client list */
662           pthread_mutex_lock (&root_rp->mutex);
663           svm_client_scan_this_region_nolock (root_rp);
664           pthread_mutex_unlock (&root_rp->mutex);
665         }
666       else
667         {
668           pthread_mutex_unlock (&vlib_rp->mutex);
669         }
670       am->vlib_rp = vlib_rp;
671       vec_add1 (am->mapped_shmem_regions, vlib_rp);
672       return 0;
673     }
674   /* Clients simply have to wait... */
675   if (!is_vlib)
676     {
677       pthread_mutex_unlock (&vlib_rp->mutex);
678
679       /* Wait up to 100 seconds... */
680       for (i = 0; i < 10000; i++)
681         {
682           ts.tv_sec = 0;
683           ts.tv_nsec = 10000 * 1000;    /* 10 ms */
684           while (nanosleep (&ts, &tsrem) < 0)
685             ts = tsrem;
686           if (vlib_rp->user_ctx)
687             goto ready;
688         }
689       /* Clean up and leave... */
690       svm_region_unmap (vlib_rp);
691       clib_warning ("region init fail");
692       return (-2);
693
694     ready:
695       am->shmem_hdr = (void *) vlib_rp->user_ctx;
696       am->our_pid = getpid ();
697       am->vlib_rp = vlib_rp;
698       vec_add1 (am->mapped_shmem_regions, vlib_rp);
699       return 0;
700     }
701
702   /* Nope, it's our problem... */
703   vl_init_shmem (vlib_rp, 0 /* default config */ , 1 /* is vlib */ ,
704                  0 /* is_private_region */ );
705
706   vec_add1 (am->mapped_shmem_regions, vlib_rp);
707   return 0;
708 }
709
710 void
711 vl_register_mapped_shmem_region (svm_region_t * rp)
712 {
713   api_main_t *am = vlibapi_get_main ();
714
715   vec_add1 (am->mapped_shmem_regions, rp);
716 }
717
718 static void
719 vl_unmap_shmem_internal (u8 is_client)
720 {
721   svm_region_t *rp;
722   int i;
723   api_main_t *am = vlibapi_get_main ();
724
725   if (!svm_get_root_rp ())
726     return;
727
728   for (i = 0; i < vec_len (am->mapped_shmem_regions); i++)
729     {
730       rp = am->mapped_shmem_regions[i];
731       is_client ? svm_region_unmap_client (rp) : svm_region_unmap (rp);
732     }
733
734   vec_free (am->mapped_shmem_regions);
735   am->shmem_hdr = 0;
736
737   is_client ? svm_region_exit_client () : svm_region_exit ();
738
739   /* $$$ more careful cleanup, valgrind run... */
740   vec_free (am->msg_handlers);
741   vec_free (am->msg_endian_handlers);
742   vec_free (am->msg_print_handlers);
743 }
744
745 void
746 vl_unmap_shmem (void)
747 {
748   vl_unmap_shmem_internal (0);
749 }
750
751 void
752 vl_unmap_shmem_client (void)
753 {
754   vl_unmap_shmem_internal (1);
755 }
756
757 void
758 vl_msg_api_send_shmem (svm_queue_t * q, u8 * elem)
759 {
760   api_main_t *am = vlibapi_get_main ();
761   void *msg = (void *) *(uword *) elem;
762
763   if (am->tx_trace && am->tx_trace->enabled)
764     vl_msg_api_trace (am, am->tx_trace, msg);
765
766   /*
767    * Announce a probable binary API client bug:
768    * some client's input queue is stuffed.
769    * The situation may be recoverable, or not.
770    */
771   if (PREDICT_FALSE
772       (am->vl_clients /* vpp side */  && (q->cursize == q->maxsize)))
773     {
774       if (PREDICT_FALSE (am->elog_trace_api_messages))
775         {
776           /* *INDENT-OFF* */
777           ELOG_TYPE_DECLARE (e) =
778             {
779               .format = "api-client-queue-stuffed: %x%x",
780               .format_args = "i4i4",
781             };
782           /* *INDENT-ON* */
783           struct
784           {
785             u32 hi, low;
786           } *ed;
787           ed = ELOG_DATA (am->elog_main, e);
788           ed->hi = (uword) q >> 32;
789           ed->low = (uword) q & 0xFFFFFFFF;
790           clib_warning ("WARNING: client input queue at %llx is stuffed...",
791                         q);
792         }
793     }
794   VL_MSG_API_POISON (msg);
795   (void) svm_queue_add (q, elem, 0 /* nowait */ );
796 }
797
798 int
799 vl_mem_api_can_send (svm_queue_t * q)
800 {
801   return (q->cursize < q->maxsize);
802 }
803
804 void
805 vl_msg_api_send_shmem_nolock (svm_queue_t * q, u8 * elem)
806 {
807   api_main_t *am = vlibapi_get_main ();
808   void *msg = (void *) *(uword *) elem;
809
810   if (am->tx_trace && am->tx_trace->enabled)
811     vl_msg_api_trace (am, am->tx_trace, msg);
812
813   (void) svm_queue_add_nolock (q, elem);
814   VL_MSG_API_POISON (msg);
815 }
816
817 /*
818  * fd.io coding-style-patch-verification: ON
819  *
820  * Local Variables:
821  * eval: (c-set-style "gnu")
822  * End:
823  */