blob: 134fcd52c84d1b0dcc1ebebd069d37f398d9a2fa [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 {
98 /* yes, loser; try next larger pool */
99 ap[i].misses++;
100 if (pool == 0)
101 pthread_mutex_unlock (&q->mutex);
102 continue;
103 }
104 /* OK, we have a winner */
105 ap[i].hits++;
106 /*
107 * Remember the source queue, although we
108 * don't need to know the queue to free the item.
109 */
110 rv->q = q;
111 q->head++;
112 if (q->head == q->maxsize)
113 q->head = 0;
114
115 if (pool == 0)
116 pthread_mutex_unlock (&q->mutex);
117 goto out;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700118 }
119
Dave Barach371e4e12016-07-08 09:38:52 -0400120 /*
121 * Request too big, or head element of all size-compatible rings
122 * still in use. Fall back to shared-memory malloc.
123 */
124 am->ring_misses++;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700125
Dave Barach371e4e12016-07-08 09:38:52 -0400126 pthread_mutex_lock (&am->vlib_rp->mutex);
127 oldheap = svm_push_data_heap (am->vlib_rp);
Dave Barach77378332016-10-13 17:35:09 -0400128 if (may_return_null)
129 {
130 rv = clib_mem_alloc_or_null (nbytes);
131 if (PREDICT_FALSE (rv == 0))
132 {
133 svm_pop_heap (oldheap);
134 pthread_mutex_unlock (&am->vlib_rp->mutex);
135 return 0;
136 }
137 }
138 else
139 rv = clib_mem_alloc (nbytes);
140
Dave Barach371e4e12016-07-08 09:38:52 -0400141 rv->q = 0;
142 svm_pop_heap (oldheap);
143 pthread_mutex_unlock (&am->vlib_rp->mutex);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700144
Dave Barach371e4e12016-07-08 09:38:52 -0400145out:
146 rv->data_len = htonl (nbytes - sizeof (msgbuf_t));
147 return (rv->data);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700148}
149
Dave Barach371e4e12016-07-08 09:38:52 -0400150void *
151vl_msg_api_alloc (int nbytes)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700152{
Dave Barach371e4e12016-07-08 09:38:52 -0400153 int pool;
154 api_main_t *am = &api_main;
155 vl_shmem_hdr_t *shmem_hdr = am->shmem_hdr;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700156
Dave Barach371e4e12016-07-08 09:38:52 -0400157 /*
158 * Clients use pool-0, vlib proc uses pool 1
159 */
160 pool = (am->our_pid == shmem_hdr->vl_pid);
Dave Barach77378332016-10-13 17:35:09 -0400161 return vl_msg_api_alloc_internal (nbytes, pool, 0 /* may_return_null */ );
162}
163
164void *
165vl_msg_api_alloc_or_null (int nbytes)
166{
167 int pool;
168 api_main_t *am = &api_main;
169 vl_shmem_hdr_t *shmem_hdr = am->shmem_hdr;
170
171 pool = (am->our_pid == shmem_hdr->vl_pid);
172 return vl_msg_api_alloc_internal (nbytes, pool, 1 /* may_return_null */ );
Ed Warnickecb9cada2015-12-08 15:45:58 -0700173}
174
Dave Barach371e4e12016-07-08 09:38:52 -0400175void *
176vl_msg_api_alloc_as_if_client (int nbytes)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700177{
Dave Barach77378332016-10-13 17:35:09 -0400178 return vl_msg_api_alloc_internal (nbytes, 0, 0 /* may_return_null */ );
179}
180
181void *
182vl_msg_api_alloc_as_if_client_or_null (int nbytes)
183{
184 return vl_msg_api_alloc_internal (nbytes, 0, 1 /* may_return_null */ );
Ed Warnickecb9cada2015-12-08 15:45:58 -0700185}
186
Dave Barach371e4e12016-07-08 09:38:52 -0400187void
188vl_msg_api_free (void *a)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700189{
Dave Barach371e4e12016-07-08 09:38:52 -0400190 msgbuf_t *rv;
191 void *oldheap;
192 api_main_t *am = &api_main;
Ole Troan6855f6c2016-04-09 03:16:30 +0200193
Dave Barach371e4e12016-07-08 09:38:52 -0400194 rv = (msgbuf_t *) (((u8 *) a) - offsetof (msgbuf_t, data));
195
196 /*
197 * Here's the beauty of the scheme. Only one proc/thread has
198 * control of a given message buffer. To free a buffer, we just clear the
199 * queue field, and leave. No locks, no hits, no errors...
200 */
201 if (rv->q)
202 {
203 rv->q = 0;
204 return;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700205 }
206
Dave Barach371e4e12016-07-08 09:38:52 -0400207 pthread_mutex_lock (&am->vlib_rp->mutex);
208 oldheap = svm_push_data_heap (am->vlib_rp);
209 clib_mem_free (rv);
210 svm_pop_heap (oldheap);
211 pthread_mutex_unlock (&am->vlib_rp->mutex);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700212}
213
Dave Barach371e4e12016-07-08 09:38:52 -0400214static void
215vl_msg_api_free_nolock (void *a)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700216{
Dave Barach371e4e12016-07-08 09:38:52 -0400217 msgbuf_t *rv;
218 void *oldheap;
219 api_main_t *am = &api_main;
220
221 rv = (msgbuf_t *) (((u8 *) a) - offsetof (msgbuf_t, data));
222 /*
223 * Here's the beauty of the scheme. Only one proc/thread has
224 * control of a given message buffer. To free a buffer, we just clear the
225 * queue field, and leave. No locks, no hits, no errors...
226 */
227 if (rv->q)
228 {
229 rv->q = 0;
230 return;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700231 }
232
Dave Barach371e4e12016-07-08 09:38:52 -0400233 oldheap = svm_push_data_heap (am->vlib_rp);
234 clib_mem_free (rv);
235 svm_pop_heap (oldheap);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700236}
237
Dave Barach371e4e12016-07-08 09:38:52 -0400238void
239vl_set_memory_root_path (char *name)
Dave Barach309bef22016-01-22 16:09:52 -0500240{
Dave Barach371e4e12016-07-08 09:38:52 -0400241 api_main_t *am = &api_main;
Dave Barach309bef22016-01-22 16:09:52 -0500242
Dave Barach371e4e12016-07-08 09:38:52 -0400243 am->root_path = name;
Dave Barach309bef22016-01-22 16:09:52 -0500244}
245
Dave Barach371e4e12016-07-08 09:38:52 -0400246void
247vl_set_memory_uid (int uid)
Dave Barach16c75df2016-05-31 14:05:46 -0400248{
Dave Barach371e4e12016-07-08 09:38:52 -0400249 api_main_t *am = &api_main;
Dave Barach16c75df2016-05-31 14:05:46 -0400250
Dave Barach371e4e12016-07-08 09:38:52 -0400251 am->api_uid = uid;
Dave Barach16c75df2016-05-31 14:05:46 -0400252}
253
Dave Barach371e4e12016-07-08 09:38:52 -0400254void
255vl_set_memory_gid (int gid)
Dave Barach16c75df2016-05-31 14:05:46 -0400256{
Dave Barach371e4e12016-07-08 09:38:52 -0400257 api_main_t *am = &api_main;
Dave Barach16c75df2016-05-31 14:05:46 -0400258
Dave Barach371e4e12016-07-08 09:38:52 -0400259 am->api_gid = gid;
Dave Barach16c75df2016-05-31 14:05:46 -0400260}
261
Dave Barachb3d93da2016-08-03 14:34:38 -0400262void
263vl_set_global_memory_baseva (u64 baseva)
264{
265 api_main_t *am = &api_main;
266
267 am->global_baseva = baseva;
268}
269
270void
271vl_set_global_memory_size (u64 size)
272{
273 api_main_t *am = &api_main;
274
275 am->global_size = size;
276}
277
278void
279vl_set_api_memory_size (u64 size)
280{
281 api_main_t *am = &api_main;
282
283 am->api_size = size;
284}
285
286void
287vl_set_global_pvt_heap_size (u64 size)
288{
289 api_main_t *am = &api_main;
290
291 am->global_pvt_heap_size = size;
292}
293
294void
295vl_set_api_pvt_heap_size (u64 size)
296{
297 api_main_t *am = &api_main;
298
299 am->api_pvt_heap_size = size;
300}
301
Dave Barach371e4e12016-07-08 09:38:52 -0400302int
303vl_map_shmem (char *region_name, int is_vlib)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700304{
Dave Barach371e4e12016-07-08 09:38:52 -0400305 svm_map_region_args_t _a, *a = &_a;
306 svm_region_t *vlib_rp, *root_rp;
307 void *oldheap;
308 vl_shmem_hdr_t *shmem_hdr = 0;
309 api_main_t *am = &api_main;
310 int i;
311 struct timespec ts, tsrem;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700312
Dave Barach371e4e12016-07-08 09:38:52 -0400313 if (is_vlib == 0)
314 svm_region_init_chroot (am->root_path);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700315
Dave Barach371e4e12016-07-08 09:38:52 -0400316 memset (a, 0, sizeof (*a));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700317
Dave Barach371e4e12016-07-08 09:38:52 -0400318 a->name = region_name;
Dave Barachc3799992016-08-15 11:12:27 -0400319 a->size = am->api_size ? am->api_size : (16 << 20);
Dave Barach371e4e12016-07-08 09:38:52 -0400320 a->flags = SVM_FLAGS_MHEAP;
321 a->uid = am->api_uid;
322 a->gid = am->api_gid;
Dave Barachb3d93da2016-08-03 14:34:38 -0400323 a->pvt_heap_size = am->api_pvt_heap_size;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700324
Dave Barach371e4e12016-07-08 09:38:52 -0400325 vlib_rp = svm_region_find_or_create (a);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700326
Dave Barach371e4e12016-07-08 09:38:52 -0400327 if (vlib_rp == 0)
328 return (-2);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700329
Dave Barach371e4e12016-07-08 09:38:52 -0400330 pthread_mutex_lock (&vlib_rp->mutex);
331 /* Has someone else set up the shared-memory variable table? */
332 if (vlib_rp->user_ctx)
333 {
334 am->shmem_hdr = (void *) vlib_rp->user_ctx;
335 am->our_pid = getpid ();
336 if (is_vlib)
337 {
338 unix_shared_memory_queue_t *q;
339 uword old_msg;
340 /*
341 * application restart. Reset cached pids, API message
342 * rings, list of clients; otherwise, various things
343 * fail. (e.g. queue non-empty notification)
344 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700345
Dave Barach371e4e12016-07-08 09:38:52 -0400346 /* ghosts keep the region from disappearing properly */
347 svm_client_scan_this_region_nolock (vlib_rp);
348 am->shmem_hdr->application_restarts++;
349 q = am->shmem_hdr->vl_input_queue;
350 am->shmem_hdr->vl_pid = getpid ();
351 q->consumer_pid = am->shmem_hdr->vl_pid;
352 /* Drain the input queue, freeing msgs */
353 for (i = 0; i < 10; i++)
354 {
355 if (pthread_mutex_trylock (&q->mutex) == 0)
356 {
357 pthread_mutex_unlock (&q->mutex);
358 goto mutex_ok;
359 }
360 ts.tv_sec = 0;
361 ts.tv_nsec = 10000 * 1000; /* 10 ms */
362 while (nanosleep (&ts, &tsrem) < 0)
363 ts = tsrem;
364 }
365 /* Mutex buggered, "fix" it */
366 memset (&q->mutex, 0, sizeof (q->mutex));
367 clib_warning ("forcibly release main input queue mutex");
368
369 mutex_ok:
370 am->vlib_rp = vlib_rp;
371 while (unix_shared_memory_queue_sub (q,
372 (u8 *) & old_msg,
373 1 /* nowait */ )
374 != -2 /* queue underflow */ )
375 {
376 vl_msg_api_free_nolock ((void *) old_msg);
377 am->shmem_hdr->restart_reclaims++;
378 }
379 pthread_mutex_unlock (&vlib_rp->mutex);
380 root_rp = svm_get_root_rp ();
381 ASSERT (root_rp);
382 /* Clean up the root region client list */
383 pthread_mutex_lock (&root_rp->mutex);
384 svm_client_scan_this_region_nolock (root_rp);
385 pthread_mutex_unlock (&root_rp->mutex);
386 }
387 else
388 {
389 pthread_mutex_unlock (&vlib_rp->mutex);
390 }
391 am->vlib_rp = vlib_rp;
392 vec_add1 (am->mapped_shmem_regions, vlib_rp);
393 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700394 }
Dave Barach371e4e12016-07-08 09:38:52 -0400395 /* Clients simply have to wait... */
396 if (!is_vlib)
397 {
398 pthread_mutex_unlock (&vlib_rp->mutex);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700399
Dave Barach371e4e12016-07-08 09:38:52 -0400400 /* Wait up to 100 seconds... */
401 for (i = 0; i < 10000; i++)
402 {
403 ts.tv_sec = 0;
404 ts.tv_nsec = 10000 * 1000; /* 10 ms */
405 while (nanosleep (&ts, &tsrem) < 0)
406 ts = tsrem;
407 if (vlib_rp->user_ctx)
408 goto ready;
409 }
410 /* Clean up and leave... */
411 svm_region_unmap (vlib_rp);
412 clib_warning ("region init fail");
413 return (-2);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700414
415 ready:
Dave Barach371e4e12016-07-08 09:38:52 -0400416 am->shmem_hdr = (void *) vlib_rp->user_ctx;
417 am->our_pid = getpid ();
418 am->vlib_rp = vlib_rp;
419 vec_add1 (am->mapped_shmem_regions, vlib_rp);
420 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700421 }
422
Dave Barach371e4e12016-07-08 09:38:52 -0400423 /* Nope, it's our problem... */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700424
Dave Barach371e4e12016-07-08 09:38:52 -0400425 oldheap = svm_push_data_heap (vlib_rp);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700426
Dave Barach371e4e12016-07-08 09:38:52 -0400427 vec_validate (shmem_hdr, 0);
428 shmem_hdr->version = VL_SHM_VERSION;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700429
Dave Barach371e4e12016-07-08 09:38:52 -0400430 /* vlib main input queue */
431 shmem_hdr->vl_input_queue =
432 unix_shared_memory_queue_init (1024, sizeof (uword), getpid (),
433 am->vlib_signal);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700434
Dave Barach371e4e12016-07-08 09:38:52 -0400435 /* Set up the msg ring allocator */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700436#define _(sz,n) \
437 do { \
438 ring_alloc_t _rp; \
439 _rp.rp = unix_shared_memory_queue_init ((n), (sz), 0, 0); \
440 _rp.size = (sz); \
441 _rp.nitems = n; \
442 _rp.hits = 0; \
443 _rp.misses = 0; \
444 vec_add1(shmem_hdr->vl_rings, _rp); \
445 } while (0);
446
Dave Barach371e4e12016-07-08 09:38:52 -0400447 foreach_vl_aring_size;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700448#undef _
449
450#define _(sz,n) \
451 do { \
452 ring_alloc_t _rp; \
453 _rp.rp = unix_shared_memory_queue_init ((n), (sz), 0, 0); \
454 _rp.size = (sz); \
455 _rp.nitems = n; \
456 _rp.hits = 0; \
457 _rp.misses = 0; \
458 vec_add1(shmem_hdr->client_rings, _rp); \
459 } while (0);
460
Dave Barach371e4e12016-07-08 09:38:52 -0400461 foreach_clnt_aring_size;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700462#undef _
463
Dave Barach371e4e12016-07-08 09:38:52 -0400464 am->shmem_hdr = shmem_hdr;
465 am->vlib_rp = vlib_rp;
466 am->our_pid = getpid ();
467 if (is_vlib)
468 am->shmem_hdr->vl_pid = am->our_pid;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700469
Dave Barach371e4e12016-07-08 09:38:52 -0400470 svm_pop_heap (oldheap);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700471
Dave Barach371e4e12016-07-08 09:38:52 -0400472 /*
473 * After absolutely everything that a client might see is set up,
474 * declare the shmem region valid
475 */
476 vlib_rp->user_ctx = shmem_hdr;
477
478 pthread_mutex_unlock (&vlib_rp->mutex);
479 vec_add1 (am->mapped_shmem_regions, vlib_rp);
480 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700481}
482
Dave Barach371e4e12016-07-08 09:38:52 -0400483void
484vl_register_mapped_shmem_region (svm_region_t * rp)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700485{
Dave Barach371e4e12016-07-08 09:38:52 -0400486 api_main_t *am = &api_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700487
Dave Barach371e4e12016-07-08 09:38:52 -0400488 vec_add1 (am->mapped_shmem_regions, rp);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700489}
490
Dave Barach371e4e12016-07-08 09:38:52 -0400491void
492vl_unmap_shmem (void)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700493{
Dave Barach371e4e12016-07-08 09:38:52 -0400494 svm_region_t *rp;
495 int i;
496 api_main_t *am = &api_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700497
Dave Barach371e4e12016-07-08 09:38:52 -0400498 if (!svm_get_root_rp ())
499 return;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700500
Dave Barach371e4e12016-07-08 09:38:52 -0400501 for (i = 0; i < vec_len (am->mapped_shmem_regions); i++)
502 {
503 rp = am->mapped_shmem_regions[i];
504 svm_region_unmap (rp);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700505 }
506
Dave Barach371e4e12016-07-08 09:38:52 -0400507 vec_free (am->mapped_shmem_regions);
508 am->shmem_hdr = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700509
Dave Barach371e4e12016-07-08 09:38:52 -0400510 svm_region_exit ();
511 /* $$$ more careful cleanup, valgrind run... */
512 vec_free (am->msg_handlers);
513 vec_free (am->msg_endian_handlers);
514 vec_free (am->msg_print_handlers);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700515}
516
Dave Barach371e4e12016-07-08 09:38:52 -0400517void
518vl_msg_api_send_shmem (unix_shared_memory_queue_t * q, u8 * elem)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700519{
Dave Barach371e4e12016-07-08 09:38:52 -0400520 api_main_t *am = &api_main;
521 uword *trace = (uword *) elem;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700522
Dave Barach371e4e12016-07-08 09:38:52 -0400523 if (am->tx_trace && am->tx_trace->enabled)
524 vl_msg_api_trace (am, am->tx_trace, (void *) trace[0]);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700525
Dave Barach371e4e12016-07-08 09:38:52 -0400526 (void) unix_shared_memory_queue_add (q, elem, 0 /* nowait */ );
Ed Warnickecb9cada2015-12-08 15:45:58 -0700527}
528
Dave Barach371e4e12016-07-08 09:38:52 -0400529void
530vl_msg_api_send_shmem_nolock (unix_shared_memory_queue_t * q, u8 * elem)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700531{
Dave Barach371e4e12016-07-08 09:38:52 -0400532 api_main_t *am = &api_main;
533 uword *trace = (uword *) elem;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700534
Dave Barach371e4e12016-07-08 09:38:52 -0400535 if (am->tx_trace && am->tx_trace->enabled)
536 vl_msg_api_trace (am, am->tx_trace, (void *) trace[0]);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700537
Dave Barach371e4e12016-07-08 09:38:52 -0400538 (void) unix_shared_memory_queue_add_nolock (q, elem);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700539}
540
Dave Barach371e4e12016-07-08 09:38:52 -0400541static void
542vl_api_memclnt_create_reply_t_handler (vl_api_memclnt_create_reply_t * mp)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700543{
Dave Barach371e4e12016-07-08 09:38:52 -0400544 api_main_t *am = &api_main;
545 int rv;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700546
Dave Barach371e4e12016-07-08 09:38:52 -0400547 am->my_client_index = mp->index;
548 am->my_registration = (vl_api_registration_t *) (uword) mp->handle;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700549
Dave Barach371e4e12016-07-08 09:38:52 -0400550 rv = ntohl (mp->response);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700551
Dave Barach371e4e12016-07-08 09:38:52 -0400552 if (rv < 0)
553 clib_warning ("WARNING: API mismatch detected");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700554}
555
Dave Barach371e4e12016-07-08 09:38:52 -0400556void vl_client_add_api_signatures (vl_api_memclnt_create_t * mp)
557 __attribute__ ((weak));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700558
Dave Barach371e4e12016-07-08 09:38:52 -0400559void
560vl_client_add_api_signatures (vl_api_memclnt_create_t * mp)
561{
562 int i;
563
564 for (i = 0; i < ARRAY_LEN (mp->api_versions); i++)
565 mp->api_versions[i] = 0;
566}
567
568int
569vl_client_connect (char *name, int ctx_quota, int input_queue_size)
570{
571 svm_region_t *svm;
572 vl_api_memclnt_create_t *mp;
573 vl_api_memclnt_create_reply_t *rp;
574 unix_shared_memory_queue_t *vl_input_queue;
575 vl_shmem_hdr_t *shmem_hdr;
576 int rv = 0;
577 void *oldheap;
578 api_main_t *am = &api_main;
579
580 if (am->my_registration)
581 {
582 clib_warning ("client %s already connected...", name);
583 return -1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700584 }
585
Dave Barach371e4e12016-07-08 09:38:52 -0400586 if (am->vlib_rp == 0)
587 {
588 clib_warning ("am->vlib_rp NULL");
589 return -1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700590 }
591
Dave Barach371e4e12016-07-08 09:38:52 -0400592 svm = am->vlib_rp;
593 shmem_hdr = am->shmem_hdr;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700594
Dave Barach371e4e12016-07-08 09:38:52 -0400595 if (shmem_hdr == 0 || shmem_hdr->vl_input_queue == 0)
596 {
597 clib_warning ("shmem_hdr / input queue NULL");
598 return -1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700599 }
600
Dave Barach371e4e12016-07-08 09:38:52 -0400601 pthread_mutex_lock (&svm->mutex);
602 oldheap = svm_push_data_heap (svm);
603 vl_input_queue =
604 unix_shared_memory_queue_init (input_queue_size, sizeof (uword),
605 getpid (), 0);
606 pthread_mutex_unlock (&svm->mutex);
607 svm_pop_heap (oldheap);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700608
Dave Barach371e4e12016-07-08 09:38:52 -0400609 am->my_client_index = ~0;
610 am->my_registration = 0;
611 am->vl_input_queue = vl_input_queue;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700612
Dave Barach371e4e12016-07-08 09:38:52 -0400613 mp = vl_msg_api_alloc (sizeof (vl_api_memclnt_create_t));
614 memset (mp, 0, sizeof (*mp));
615 mp->_vl_msg_id = ntohs (VL_API_MEMCLNT_CREATE);
616 mp->ctx_quota = ctx_quota;
617 mp->input_queue = (uword) vl_input_queue;
618 strncpy ((char *) mp->name, name, sizeof (mp->name) - 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700619
Dave Barach371e4e12016-07-08 09:38:52 -0400620 vl_client_add_api_signatures (mp);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700621
Dave Barach371e4e12016-07-08 09:38:52 -0400622 vl_msg_api_send_shmem (shmem_hdr->vl_input_queue, (u8 *) & mp);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700623
Dave Barach371e4e12016-07-08 09:38:52 -0400624 while (1)
625 {
626 int qstatus;
627 struct timespec ts, tsrem;
628 int i;
629
630 /* Wait up to 10 seconds */
631 for (i = 0; i < 1000; i++)
632 {
633 qstatus = unix_shared_memory_queue_sub (vl_input_queue, (u8 *) & rp,
634 1 /* nowait */ );
635 if (qstatus == 0)
636 goto read_one_msg;
637 ts.tv_sec = 0;
638 ts.tv_nsec = 10000 * 1000; /* 10 ms */
639 while (nanosleep (&ts, &tsrem) < 0)
640 ts = tsrem;
641 }
642 /* Timeout... */
643 clib_warning ("memclnt_create_reply timeout");
644 return -1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700645
646 read_one_msg:
Dave Barach371e4e12016-07-08 09:38:52 -0400647 if (ntohs (rp->_vl_msg_id) != VL_API_MEMCLNT_CREATE_REPLY)
648 {
649 clib_warning ("unexpected reply: id %d", ntohs (rp->_vl_msg_id));
650 continue;
651 }
652 rv = clib_net_to_host_u32 (rp->response);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700653
Dave Barach371e4e12016-07-08 09:38:52 -0400654 vl_msg_api_handler ((void *) rp);
655 break;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700656 }
Dave Barach371e4e12016-07-08 09:38:52 -0400657 return (rv);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700658}
659
Dave Barach371e4e12016-07-08 09:38:52 -0400660static void
661vl_api_memclnt_delete_reply_t_handler (vl_api_memclnt_delete_reply_t * mp)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700662{
Dave Barach371e4e12016-07-08 09:38:52 -0400663 void *oldheap;
664 api_main_t *am = &api_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700665
Dave Barach371e4e12016-07-08 09:38:52 -0400666 pthread_mutex_lock (&am->vlib_rp->mutex);
667 oldheap = svm_push_data_heap (am->vlib_rp);
668 unix_shared_memory_queue_free (am->vl_input_queue);
669 pthread_mutex_unlock (&am->vlib_rp->mutex);
670 svm_pop_heap (oldheap);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700671
Dave Barach371e4e12016-07-08 09:38:52 -0400672 am->my_client_index = ~0;
673 am->my_registration = 0;
674 am->vl_input_queue = 0;
675}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700676
Dave Barach371e4e12016-07-08 09:38:52 -0400677void
678vl_client_disconnect (void)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700679{
Dave Barach371e4e12016-07-08 09:38:52 -0400680 vl_api_memclnt_delete_t *mp;
681 vl_api_memclnt_delete_reply_t *rp;
682 unix_shared_memory_queue_t *vl_input_queue;
683 vl_shmem_hdr_t *shmem_hdr;
684 time_t begin;
685 api_main_t *am = &api_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700686
Dave Barach371e4e12016-07-08 09:38:52 -0400687 ASSERT (am->vlib_rp);
688 shmem_hdr = am->shmem_hdr;
689 ASSERT (shmem_hdr && shmem_hdr->vl_input_queue);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700690
Dave Barach371e4e12016-07-08 09:38:52 -0400691 vl_input_queue = am->vl_input_queue;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700692
Dave Barach371e4e12016-07-08 09:38:52 -0400693 mp = vl_msg_api_alloc (sizeof (vl_api_memclnt_delete_t));
694 memset (mp, 0, sizeof (*mp));
695 mp->_vl_msg_id = ntohs (VL_API_MEMCLNT_DELETE);
696 mp->index = am->my_client_index;
697 mp->handle = (uword) am->my_registration;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700698
Dave Barach371e4e12016-07-08 09:38:52 -0400699 vl_msg_api_send_shmem (shmem_hdr->vl_input_queue, (u8 *) & mp);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700700
Dave Barach371e4e12016-07-08 09:38:52 -0400701 /*
702 * Have to be careful here, in case the client is disconnecting
703 * because e.g. the vlib process died, or is unresponsive.
704 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700705
Dave Barach371e4e12016-07-08 09:38:52 -0400706 begin = time (0);
707 while (1)
708 {
709 time_t now;
710
711 now = time (0);
712
713 if (now >= (begin + 2))
714 {
715 clib_warning ("peer unresponsive, give up");
716 am->my_client_index = ~0;
717 am->my_registration = 0;
718 am->shmem_hdr = 0;
719 break;
720 }
721 if (unix_shared_memory_queue_sub (vl_input_queue, (u8 *) & rp, 1) < 0)
722 continue;
723
724 /* drain the queue */
725 if (ntohs (rp->_vl_msg_id) != VL_API_MEMCLNT_DELETE_REPLY)
726 {
727 vl_msg_api_handler ((void *) rp);
728 continue;
729 }
730 vl_msg_api_handler ((void *) rp);
731 break;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700732 }
733}
734
Dave Barach371e4e12016-07-08 09:38:52 -0400735static inline vl_api_registration_t *
736vl_api_client_index_to_registration_internal (u32 handle)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700737{
Dave Barach371e4e12016-07-08 09:38:52 -0400738 vl_api_registration_t **regpp;
739 vl_api_registration_t *regp;
740 api_main_t *am = &api_main;
741 u32 index;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700742
Dave Barach371e4e12016-07-08 09:38:52 -0400743 index = vl_msg_api_handle_get_index (handle);
744 if ((am->shmem_hdr->application_restarts & VL_API_EPOCH_MASK)
745 != vl_msg_api_handle_get_epoch (handle))
746 {
747 vl_msg_api_increment_missing_client_counter ();
748 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700749 }
750
Dave Barach371e4e12016-07-08 09:38:52 -0400751 regpp = am->vl_clients + index;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700752
Dave Barach371e4e12016-07-08 09:38:52 -0400753 if (pool_is_free (am->vl_clients, regpp))
754 {
755 vl_msg_api_increment_missing_client_counter ();
756 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700757 }
Dave Barach371e4e12016-07-08 09:38:52 -0400758 regp = *regpp;
759 return (regp);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700760}
761
Dave Barach371e4e12016-07-08 09:38:52 -0400762vl_api_registration_t *
763vl_api_client_index_to_registration (u32 index)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700764{
Dave Barach371e4e12016-07-08 09:38:52 -0400765 return (vl_api_client_index_to_registration_internal (index));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700766}
767
Dave Barach371e4e12016-07-08 09:38:52 -0400768unix_shared_memory_queue_t *
769vl_api_client_index_to_input_queue (u32 index)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700770{
Dave Barach371e4e12016-07-08 09:38:52 -0400771 vl_api_registration_t *regp;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700772
Dave Barach371e4e12016-07-08 09:38:52 -0400773 regp = vl_api_client_index_to_registration_internal (index);
774 if (!regp)
775 return 0;
776 return (regp->vl_input_queue);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700777}
778
779#define foreach_api_client_msg \
780_(MEMCLNT_CREATE_REPLY, memclnt_create_reply) \
781_(MEMCLNT_DELETE_REPLY, memclnt_delete_reply)
782
Dave Barach371e4e12016-07-08 09:38:52 -0400783int
784vl_client_api_map (char *region_name)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700785{
Dave Barach371e4e12016-07-08 09:38:52 -0400786 int rv;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700787
Dave Barach371e4e12016-07-08 09:38:52 -0400788 if ((rv = vl_map_shmem (region_name, 0 /* is_vlib */ )) < 0)
789 {
790 return rv;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700791 }
792
793#define _(N,n) \
794 vl_msg_api_set_handlers(VL_API_##N, 0 /* name */, \
795 vl_api_##n##_t_handler, \
796 0/* cleanup */, 0/* endian */, 0/* print */, \
Dave Barach371e4e12016-07-08 09:38:52 -0400797 sizeof(vl_api_##n##_t), 1);
798 foreach_api_client_msg;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700799#undef _
Dave Barach371e4e12016-07-08 09:38:52 -0400800 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700801}
802
Dave Barach371e4e12016-07-08 09:38:52 -0400803void
804vl_client_api_unmap (void)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700805{
Dave Barach371e4e12016-07-08 09:38:52 -0400806 vl_unmap_shmem ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700807}
Dave Barach371e4e12016-07-08 09:38:52 -0400808
809/*
810 * fd.io coding-style-patch-verification: ON
811 *
812 * Local Variables:
813 * eval: (c-set-style "gnu")
814 * End:
815 */