blob: 9bab65730ef0bc6c1a0369dccc3adf275a74f928 [file] [log] [blame]
Dave Barach371e4e12016-07-08 09:38:52 -04001/*
Ed Warnickecb9cada2015-12-08 15:45:58 -07002 *------------------------------------------------------------------
3 * memclnt_shared.c - API message handling, common code for both clients
4 * and the vlib process itself.
Dave Barach371e4e12016-07-08 09:38:52 -04005 *
Ed Warnickecb9cada2015-12-08 15:45:58 -07006 *
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>
Ole Troan6855f6c2016-04-09 03:16:30 +020024#include <stddef.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070025#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
Dave Barach371e4e12016-07-08 09:38:52 -040036#include <vlibmemory/vl_memory_msg_enum.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070037
Dave Barach371e4e12016-07-08 09:38:52 -040038#define vl_typedefs
Ed Warnickecb9cada2015-12-08 15:45:58 -070039#include <vlibmemory/vl_memory_api_h.h>
40#undef vl_typedefs
41
Dave Barach371e4e12016-07-08 09:38:52 -040042static inline void *
Dave Barach77378332016-10-13 17:35:09 -040043vl_msg_api_alloc_internal (int nbytes, int pool, int may_return_null)
Ed Warnickecb9cada2015-12-08 15:45:58 -070044{
Dave Barach371e4e12016-07-08 09:38:52 -040045 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;
Ed Warnickecb9cada2015-12-08 15:45:58 -070052
Dave Barach371e4e12016-07-08 09:38:52 -040053 shmem_hdr = am->shmem_hdr;
Ed Warnickecb9cada2015-12-08 15:45:58 -070054
Dave Barach371e4e12016-07-08 09:38:52 -040055 if (shmem_hdr == 0)
56 {
57 clib_warning ("shared memory header NULL");
58 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -070059 }
60
Dave Barach371e4e12016-07-08 09:38:52 -040061 /* account for the msgbuf_t header */
62 nbytes += sizeof (msgbuf_t);
Ed Warnickecb9cada2015-12-08 15:45:58 -070063
Dave Barach371e4e12016-07-08 09:38:52 -040064 if (shmem_hdr->vl_rings == 0)
65 {
66 clib_warning ("vl_rings NULL");
Dave Barach77378332016-10-13 17:35:09 -040067 ASSERT (0);
68 abort ();
Ed Warnickecb9cada2015-12-08 15:45:58 -070069 }
70
Dave Barach371e4e12016-07-08 09:38:52 -040071 if (shmem_hdr->client_rings == 0)
72 {
73 clib_warning ("client_rings NULL");
Dave Barach77378332016-10-13 17:35:09 -040074 ASSERT (0);
75 abort ();
Ed Warnickecb9cada2015-12-08 15:45:58 -070076 }
77
Dave Barach371e4e12016-07-08 09:38:52 -040078 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 }
Ed Warnickecb9cada2015-12-08 15:45:58 -070086
Dave Barach371e4e12016-07-08 09:38:52 -040087 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 {
Dave Barach842b9c52017-01-09 15:54:00 -050098 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)
Dave Barach10d8cc62017-05-30 09:30:07 -0400107 {
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 }
Dave Barach842b9c52017-01-09 15:54:00 -0500118 shmem_hdr->garbage_collects++;
119 goto collected;
120 }
121 }
122
123
Dave Barach371e4e12016-07-08 09:38:52 -0400124 /* yes, loser; try next larger pool */
125 ap[i].misses++;
126 if (pool == 0)
127 pthread_mutex_unlock (&q->mutex);
128 continue;
129 }
Dave Barach842b9c52017-01-09 15:54:00 -0500130 collected:
131
Dave Barach371e4e12016-07-08 09:38:52 -0400132 /* 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;
Dave Barach842b9c52017-01-09 15:54:00 -0500139 rv->gc_mark_timestamp = 0;
Dave Barach371e4e12016-07-08 09:38:52 -0400140 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;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700147 }
148
Dave Barach371e4e12016-07-08 09:38:52 -0400149 /*
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++;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700154
Dave Barach371e4e12016-07-08 09:38:52 -0400155 pthread_mutex_lock (&am->vlib_rp->mutex);
156 oldheap = svm_push_data_heap (am->vlib_rp);
Dave Barach77378332016-10-13 17:35:09 -0400157 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
Dave Barach371e4e12016-07-08 09:38:52 -0400170 rv->q = 0;
171 svm_pop_heap (oldheap);
172 pthread_mutex_unlock (&am->vlib_rp->mutex);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700173
Dave Barach371e4e12016-07-08 09:38:52 -0400174out:
175 rv->data_len = htonl (nbytes - sizeof (msgbuf_t));
176 return (rv->data);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700177}
178
Dave Barach371e4e12016-07-08 09:38:52 -0400179void *
180vl_msg_api_alloc (int nbytes)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700181{
Dave Barach371e4e12016-07-08 09:38:52 -0400182 int pool;
183 api_main_t *am = &api_main;
184 vl_shmem_hdr_t *shmem_hdr = am->shmem_hdr;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700185
Dave Barach371e4e12016-07-08 09:38:52 -0400186 /*
187 * Clients use pool-0, vlib proc uses pool 1
188 */
189 pool = (am->our_pid == shmem_hdr->vl_pid);
Dave Barach77378332016-10-13 17:35:09 -0400190 return vl_msg_api_alloc_internal (nbytes, pool, 0 /* may_return_null */ );
191}
192
193void *
194vl_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 */ );
Ed Warnickecb9cada2015-12-08 15:45:58 -0700202}
203
Dave Barach371e4e12016-07-08 09:38:52 -0400204void *
205vl_msg_api_alloc_as_if_client (int nbytes)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700206{
Dave Barach77378332016-10-13 17:35:09 -0400207 return vl_msg_api_alloc_internal (nbytes, 0, 0 /* may_return_null */ );
208}
209
210void *
211vl_msg_api_alloc_as_if_client_or_null (int nbytes)
212{
213 return vl_msg_api_alloc_internal (nbytes, 0, 1 /* may_return_null */ );
Ed Warnickecb9cada2015-12-08 15:45:58 -0700214}
215
Dave Barach371e4e12016-07-08 09:38:52 -0400216void
217vl_msg_api_free (void *a)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700218{
Dave Barach371e4e12016-07-08 09:38:52 -0400219 msgbuf_t *rv;
220 void *oldheap;
221 api_main_t *am = &api_main;
Ole Troan6855f6c2016-04-09 03:16:30 +0200222
Dave Barach371e4e12016-07-08 09:38:52 -0400223 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;
Dave Barach842b9c52017-01-09 15:54:00 -0500233 rv->gc_mark_timestamp = 0;
Dave Barach371e4e12016-07-08 09:38:52 -0400234 return;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700235 }
236
Dave Barach371e4e12016-07-08 09:38:52 -0400237 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);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700242}
243
Dave Barach371e4e12016-07-08 09:38:52 -0400244static void
245vl_msg_api_free_nolock (void *a)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700246{
Dave Barach371e4e12016-07-08 09:38:52 -0400247 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;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700261 }
262
Dave Barach371e4e12016-07-08 09:38:52 -0400263 oldheap = svm_push_data_heap (am->vlib_rp);
264 clib_mem_free (rv);
265 svm_pop_heap (oldheap);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700266}
267
Dave Barach371e4e12016-07-08 09:38:52 -0400268void
Neale Rannse72be392017-04-26 13:59:20 -0700269vl_set_memory_root_path (const char *name)
Dave Barach309bef22016-01-22 16:09:52 -0500270{
Dave Barach371e4e12016-07-08 09:38:52 -0400271 api_main_t *am = &api_main;
Dave Barach309bef22016-01-22 16:09:52 -0500272
Dave Barach371e4e12016-07-08 09:38:52 -0400273 am->root_path = name;
Dave Barach309bef22016-01-22 16:09:52 -0500274}
275
Dave Barach371e4e12016-07-08 09:38:52 -0400276void
277vl_set_memory_uid (int uid)
Dave Barach16c75df2016-05-31 14:05:46 -0400278{
Dave Barach371e4e12016-07-08 09:38:52 -0400279 api_main_t *am = &api_main;
Dave Barach16c75df2016-05-31 14:05:46 -0400280
Dave Barach371e4e12016-07-08 09:38:52 -0400281 am->api_uid = uid;
Dave Barach16c75df2016-05-31 14:05:46 -0400282}
283
Dave Barach371e4e12016-07-08 09:38:52 -0400284void
285vl_set_memory_gid (int gid)
Dave Barach16c75df2016-05-31 14:05:46 -0400286{
Dave Barach371e4e12016-07-08 09:38:52 -0400287 api_main_t *am = &api_main;
Dave Barach16c75df2016-05-31 14:05:46 -0400288
Dave Barach371e4e12016-07-08 09:38:52 -0400289 am->api_gid = gid;
Dave Barach16c75df2016-05-31 14:05:46 -0400290}
291
Dave Barachb3d93da2016-08-03 14:34:38 -0400292void
293vl_set_global_memory_baseva (u64 baseva)
294{
295 api_main_t *am = &api_main;
296
297 am->global_baseva = baseva;
298}
299
300void
301vl_set_global_memory_size (u64 size)
302{
303 api_main_t *am = &api_main;
304
305 am->global_size = size;
306}
307
308void
309vl_set_api_memory_size (u64 size)
310{
311 api_main_t *am = &api_main;
312
313 am->api_size = size;
314}
315
316void
317vl_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
324void
325vl_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
Dave Barach371e4e12016-07-08 09:38:52 -0400332int
Neale Rannse72be392017-04-26 13:59:20 -0700333vl_map_shmem (const char *region_name, int is_vlib)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700334{
Dave Barach371e4e12016-07-08 09:38:52 -0400335 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;
341 struct timespec ts, tsrem;
Dave Barach10d8cc62017-05-30 09:30:07 -0400342 u32 vlib_input_queue_length;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700343
Jan Srnicek5beec812017-03-24 10:18:11 +0100344 memset (a, 0, sizeof (*a));
345
346 if (strstr (region_name, "-vpe-api"))
347 {
348 char root_path[strlen (region_name)];
349 strncpy (root_path, region_name, strlen (region_name) - 8);
350 a->root_path = root_path;
351 am->root_path = root_path;
352 }
353
Dave Barach371e4e12016-07-08 09:38:52 -0400354 if (is_vlib == 0)
355 svm_region_init_chroot (am->root_path);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700356
Jan Srnicek5beec812017-03-24 10:18:11 +0100357 if (a->root_path != NULL)
358 {
359 a->name = "/vpe-api";
360 }
361 else
362 a->name = region_name;
Dave Barachc3799992016-08-15 11:12:27 -0400363 a->size = am->api_size ? am->api_size : (16 << 20);
Dave Barach371e4e12016-07-08 09:38:52 -0400364 a->flags = SVM_FLAGS_MHEAP;
365 a->uid = am->api_uid;
366 a->gid = am->api_gid;
Dave Barachb3d93da2016-08-03 14:34:38 -0400367 a->pvt_heap_size = am->api_pvt_heap_size;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700368
Dave Barach371e4e12016-07-08 09:38:52 -0400369 vlib_rp = svm_region_find_or_create (a);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700370
Dave Barach371e4e12016-07-08 09:38:52 -0400371 if (vlib_rp == 0)
372 return (-2);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700373
Dave Barach371e4e12016-07-08 09:38:52 -0400374 pthread_mutex_lock (&vlib_rp->mutex);
375 /* Has someone else set up the shared-memory variable table? */
376 if (vlib_rp->user_ctx)
377 {
378 am->shmem_hdr = (void *) vlib_rp->user_ctx;
379 am->our_pid = getpid ();
380 if (is_vlib)
381 {
382 unix_shared_memory_queue_t *q;
383 uword old_msg;
384 /*
385 * application restart. Reset cached pids, API message
386 * rings, list of clients; otherwise, various things
387 * fail. (e.g. queue non-empty notification)
388 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700389
Dave Barach371e4e12016-07-08 09:38:52 -0400390 /* ghosts keep the region from disappearing properly */
391 svm_client_scan_this_region_nolock (vlib_rp);
392 am->shmem_hdr->application_restarts++;
393 q = am->shmem_hdr->vl_input_queue;
394 am->shmem_hdr->vl_pid = getpid ();
395 q->consumer_pid = am->shmem_hdr->vl_pid;
396 /* Drain the input queue, freeing msgs */
397 for (i = 0; i < 10; i++)
398 {
399 if (pthread_mutex_trylock (&q->mutex) == 0)
400 {
401 pthread_mutex_unlock (&q->mutex);
402 goto mutex_ok;
403 }
404 ts.tv_sec = 0;
405 ts.tv_nsec = 10000 * 1000; /* 10 ms */
406 while (nanosleep (&ts, &tsrem) < 0)
407 ts = tsrem;
408 }
409 /* Mutex buggered, "fix" it */
410 memset (&q->mutex, 0, sizeof (q->mutex));
411 clib_warning ("forcibly release main input queue mutex");
412
413 mutex_ok:
414 am->vlib_rp = vlib_rp;
415 while (unix_shared_memory_queue_sub (q,
416 (u8 *) & old_msg,
417 1 /* nowait */ )
418 != -2 /* queue underflow */ )
419 {
420 vl_msg_api_free_nolock ((void *) old_msg);
421 am->shmem_hdr->restart_reclaims++;
422 }
423 pthread_mutex_unlock (&vlib_rp->mutex);
424 root_rp = svm_get_root_rp ();
425 ASSERT (root_rp);
426 /* Clean up the root region client list */
427 pthread_mutex_lock (&root_rp->mutex);
428 svm_client_scan_this_region_nolock (root_rp);
429 pthread_mutex_unlock (&root_rp->mutex);
430 }
431 else
432 {
433 pthread_mutex_unlock (&vlib_rp->mutex);
434 }
435 am->vlib_rp = vlib_rp;
436 vec_add1 (am->mapped_shmem_regions, vlib_rp);
437 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700438 }
Dave Barach371e4e12016-07-08 09:38:52 -0400439 /* Clients simply have to wait... */
440 if (!is_vlib)
441 {
442 pthread_mutex_unlock (&vlib_rp->mutex);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700443
Dave Barach371e4e12016-07-08 09:38:52 -0400444 /* Wait up to 100 seconds... */
445 for (i = 0; i < 10000; i++)
446 {
447 ts.tv_sec = 0;
448 ts.tv_nsec = 10000 * 1000; /* 10 ms */
449 while (nanosleep (&ts, &tsrem) < 0)
450 ts = tsrem;
451 if (vlib_rp->user_ctx)
452 goto ready;
453 }
454 /* Clean up and leave... */
455 svm_region_unmap (vlib_rp);
456 clib_warning ("region init fail");
457 return (-2);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700458
459 ready:
Dave Barach371e4e12016-07-08 09:38:52 -0400460 am->shmem_hdr = (void *) vlib_rp->user_ctx;
461 am->our_pid = getpid ();
462 am->vlib_rp = vlib_rp;
463 vec_add1 (am->mapped_shmem_regions, vlib_rp);
464 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700465 }
466
Dave Barach371e4e12016-07-08 09:38:52 -0400467 /* Nope, it's our problem... */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700468
Dave Barach371e4e12016-07-08 09:38:52 -0400469 oldheap = svm_push_data_heap (vlib_rp);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700470
Dave Barach371e4e12016-07-08 09:38:52 -0400471 vec_validate (shmem_hdr, 0);
472 shmem_hdr->version = VL_SHM_VERSION;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700473
Dave Barach371e4e12016-07-08 09:38:52 -0400474 /* vlib main input queue */
Dave Barach10d8cc62017-05-30 09:30:07 -0400475 vlib_input_queue_length = 1024;
476 if (am->vlib_input_queue_length)
477 vlib_input_queue_length = am->vlib_input_queue_length;
478
Dave Barach371e4e12016-07-08 09:38:52 -0400479 shmem_hdr->vl_input_queue =
Dave Barach10d8cc62017-05-30 09:30:07 -0400480 unix_shared_memory_queue_init (vlib_input_queue_length, sizeof (uword),
481 getpid (), am->vlib_signal);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700482
Dave Barach371e4e12016-07-08 09:38:52 -0400483 /* Set up the msg ring allocator */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700484#define _(sz,n) \
485 do { \
486 ring_alloc_t _rp; \
487 _rp.rp = unix_shared_memory_queue_init ((n), (sz), 0, 0); \
488 _rp.size = (sz); \
489 _rp.nitems = n; \
490 _rp.hits = 0; \
491 _rp.misses = 0; \
492 vec_add1(shmem_hdr->vl_rings, _rp); \
493 } while (0);
494
Dave Barach371e4e12016-07-08 09:38:52 -0400495 foreach_vl_aring_size;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700496#undef _
497
498#define _(sz,n) \
499 do { \
500 ring_alloc_t _rp; \
501 _rp.rp = unix_shared_memory_queue_init ((n), (sz), 0, 0); \
502 _rp.size = (sz); \
503 _rp.nitems = n; \
504 _rp.hits = 0; \
505 _rp.misses = 0; \
506 vec_add1(shmem_hdr->client_rings, _rp); \
507 } while (0);
508
Dave Barach371e4e12016-07-08 09:38:52 -0400509 foreach_clnt_aring_size;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700510#undef _
511
Dave Barach371e4e12016-07-08 09:38:52 -0400512 am->shmem_hdr = shmem_hdr;
513 am->vlib_rp = vlib_rp;
514 am->our_pid = getpid ();
515 if (is_vlib)
516 am->shmem_hdr->vl_pid = am->our_pid;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700517
Dave Barach371e4e12016-07-08 09:38:52 -0400518 svm_pop_heap (oldheap);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700519
Dave Barach371e4e12016-07-08 09:38:52 -0400520 /*
521 * After absolutely everything that a client might see is set up,
522 * declare the shmem region valid
523 */
524 vlib_rp->user_ctx = shmem_hdr;
525
526 pthread_mutex_unlock (&vlib_rp->mutex);
527 vec_add1 (am->mapped_shmem_regions, vlib_rp);
528 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700529}
530
Dave Barach371e4e12016-07-08 09:38:52 -0400531void
532vl_register_mapped_shmem_region (svm_region_t * rp)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700533{
Dave Barach371e4e12016-07-08 09:38:52 -0400534 api_main_t *am = &api_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700535
Dave Barach371e4e12016-07-08 09:38:52 -0400536 vec_add1 (am->mapped_shmem_regions, rp);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700537}
538
Dave Barach371e4e12016-07-08 09:38:52 -0400539void
540vl_unmap_shmem (void)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700541{
Dave Barach371e4e12016-07-08 09:38:52 -0400542 svm_region_t *rp;
543 int i;
544 api_main_t *am = &api_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700545
Dave Barach371e4e12016-07-08 09:38:52 -0400546 if (!svm_get_root_rp ())
547 return;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700548
Dave Barach371e4e12016-07-08 09:38:52 -0400549 for (i = 0; i < vec_len (am->mapped_shmem_regions); i++)
550 {
551 rp = am->mapped_shmem_regions[i];
552 svm_region_unmap (rp);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700553 }
554
Dave Barach371e4e12016-07-08 09:38:52 -0400555 vec_free (am->mapped_shmem_regions);
556 am->shmem_hdr = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700557
Dave Barach371e4e12016-07-08 09:38:52 -0400558 svm_region_exit ();
559 /* $$$ more careful cleanup, valgrind run... */
560 vec_free (am->msg_handlers);
561 vec_free (am->msg_endian_handlers);
562 vec_free (am->msg_print_handlers);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700563}
564
Dave Barach371e4e12016-07-08 09:38:52 -0400565void
566vl_msg_api_send_shmem (unix_shared_memory_queue_t * q, u8 * elem)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700567{
Dave Barach371e4e12016-07-08 09:38:52 -0400568 api_main_t *am = &api_main;
569 uword *trace = (uword *) elem;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700570
Dave Barach371e4e12016-07-08 09:38:52 -0400571 if (am->tx_trace && am->tx_trace->enabled)
572 vl_msg_api_trace (am, am->tx_trace, (void *) trace[0]);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700573
Dave Barach371e4e12016-07-08 09:38:52 -0400574 (void) unix_shared_memory_queue_add (q, elem, 0 /* nowait */ );
Ed Warnickecb9cada2015-12-08 15:45:58 -0700575}
576
Dave Barach371e4e12016-07-08 09:38:52 -0400577void
578vl_msg_api_send_shmem_nolock (unix_shared_memory_queue_t * q, u8 * elem)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700579{
Dave Barach371e4e12016-07-08 09:38:52 -0400580 api_main_t *am = &api_main;
581 uword *trace = (uword *) elem;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700582
Dave Barach371e4e12016-07-08 09:38:52 -0400583 if (am->tx_trace && am->tx_trace->enabled)
584 vl_msg_api_trace (am, am->tx_trace, (void *) trace[0]);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700585
Dave Barach371e4e12016-07-08 09:38:52 -0400586 (void) unix_shared_memory_queue_add_nolock (q, elem);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700587}
588
Dave Barach557d1282016-11-10 14:22:49 -0500589u32
590vl_api_get_msg_index (u8 * name_and_crc)
Dave Barach371e4e12016-07-08 09:38:52 -0400591{
Dave Barach557d1282016-11-10 14:22:49 -0500592 api_main_t *am = &api_main;
593 uword *p;
Dave Barach371e4e12016-07-08 09:38:52 -0400594
Dave Barach557d1282016-11-10 14:22:49 -0500595 if (am->msg_index_by_name_and_crc)
596 {
597 p = hash_get_mem (am->msg_index_by_name_and_crc, name_and_crc);
598 if (p)
599 return p[0];
600 }
601 return ~0;
Dave Barach371e4e12016-07-08 09:38:52 -0400602}
603
Dave Barach371e4e12016-07-08 09:38:52 -0400604static inline vl_api_registration_t *
605vl_api_client_index_to_registration_internal (u32 handle)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700606{
Dave Barach371e4e12016-07-08 09:38:52 -0400607 vl_api_registration_t **regpp;
608 vl_api_registration_t *regp;
609 api_main_t *am = &api_main;
610 u32 index;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700611
Dave Barach371e4e12016-07-08 09:38:52 -0400612 index = vl_msg_api_handle_get_index (handle);
613 if ((am->shmem_hdr->application_restarts & VL_API_EPOCH_MASK)
614 != vl_msg_api_handle_get_epoch (handle))
615 {
616 vl_msg_api_increment_missing_client_counter ();
617 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700618 }
619
Dave Barach371e4e12016-07-08 09:38:52 -0400620 regpp = am->vl_clients + index;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700621
Dave Barach371e4e12016-07-08 09:38:52 -0400622 if (pool_is_free (am->vl_clients, regpp))
623 {
624 vl_msg_api_increment_missing_client_counter ();
625 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700626 }
Dave Barach371e4e12016-07-08 09:38:52 -0400627 regp = *regpp;
628 return (regp);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700629}
630
Dave Barach371e4e12016-07-08 09:38:52 -0400631vl_api_registration_t *
632vl_api_client_index_to_registration (u32 index)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700633{
Dave Barach371e4e12016-07-08 09:38:52 -0400634 return (vl_api_client_index_to_registration_internal (index));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700635}
636
Dave Barach371e4e12016-07-08 09:38:52 -0400637unix_shared_memory_queue_t *
638vl_api_client_index_to_input_queue (u32 index)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700639{
Dave Barach371e4e12016-07-08 09:38:52 -0400640 vl_api_registration_t *regp;
Dave Barachfc262a02016-12-14 14:49:55 -0500641 api_main_t *am = &api_main;
642
643 /* Special case: vlib trying to send itself a message */
644 if (index == (u32) ~ 0)
645 return (am->shmem_hdr->vl_input_queue);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700646
Dave Barach371e4e12016-07-08 09:38:52 -0400647 regp = vl_api_client_index_to_registration_internal (index);
648 if (!regp)
649 return 0;
650 return (regp->vl_input_queue);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700651}
652
Dave Barach371e4e12016-07-08 09:38:52 -0400653/*
654 * fd.io coding-style-patch-verification: ON
655 *
656 * Local Variables:
657 * eval: (c-set-style "gnu")
658 * End:
659 */