blob: 0b232adf89923cf2319ffee62db5dfa4d6e940a8 [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>
Florin Corase86a8ed2018-01-05 03:20:25 -080028
Ed Warnickecb9cada2015-12-08 15:45:58 -070029#include <vppinfra/format.h>
30#include <vppinfra/byte_order.h>
31#include <vppinfra/error.h>
Dave Barachb09f4d02019-07-15 16:00:03 -040032#include <vppinfra/elog.h>
Florin Corase86a8ed2018-01-05 03:20:25 -080033#include <svm/queue.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070034#include <vlib/vlib.h>
35#include <vlib/unix/unix.h>
Florin Corase86a8ed2018-01-05 03:20:25 -080036#include <vlibmemory/memory_api.h>
Dave Barach371e4e12016-07-08 09:38:52 -040037#include <vlibmemory/vl_memory_msg_enum.h>
Dave Barach39d69112019-11-27 11:42:13 -050038#include <vlibapi/api_common.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070039
Dave Barach371e4e12016-07-08 09:38:52 -040040#define vl_typedefs
Ed Warnickecb9cada2015-12-08 15:45:58 -070041#include <vlibmemory/vl_memory_api_h.h>
42#undef vl_typedefs
43
Dave Barach59b25652017-09-10 15:04:27 -040044#define DEBUG_MESSAGE_BUFFER_OVERRUN 0
45
Benoît Ganne9fb6d402019-04-15 15:28:21 +020046CLIB_NOSANITIZE_ADDR static inline void *
Florin Coras8d820852019-11-27 09:15:25 -080047vl_msg_api_alloc_internal (svm_region_t * vlib_rp, int nbytes, int pool,
48 int may_return_null)
Ed Warnickecb9cada2015-12-08 15:45:58 -070049{
Dave Barach371e4e12016-07-08 09:38:52 -040050 int i;
51 msgbuf_t *rv;
52 ring_alloc_t *ap;
Florin Corase86a8ed2018-01-05 03:20:25 -080053 svm_queue_t *q;
Dave Barach371e4e12016-07-08 09:38:52 -040054 void *oldheap;
55 vl_shmem_hdr_t *shmem_hdr;
Dave Barach39d69112019-11-27 11:42:13 -050056 api_main_t *am = vlibapi_get_main ();
Ed Warnickecb9cada2015-12-08 15:45:58 -070057
Florin Coras8d820852019-11-27 09:15:25 -080058 shmem_hdr = (void *) vlib_rp->user_ctx;
Ed Warnickecb9cada2015-12-08 15:45:58 -070059
Dave Barach59b25652017-09-10 15:04:27 -040060#if DEBUG_MESSAGE_BUFFER_OVERRUN > 0
61 nbytes += 4;
62#endif
63
Florin Coras7a2e1bd2017-11-30 16:07:39 -050064 ASSERT (pool == 0 || vlib_get_thread_index () == 0);
65
Dave Barach371e4e12016-07-08 09:38:52 -040066 if (shmem_hdr == 0)
67 {
68 clib_warning ("shared memory header NULL");
69 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -070070 }
71
Dave Barach371e4e12016-07-08 09:38:52 -040072 /* account for the msgbuf_t header */
73 nbytes += sizeof (msgbuf_t);
Ed Warnickecb9cada2015-12-08 15:45:58 -070074
Dave Barach371e4e12016-07-08 09:38:52 -040075 if (shmem_hdr->vl_rings == 0)
76 {
77 clib_warning ("vl_rings NULL");
Dave Barach77378332016-10-13 17:35:09 -040078 ASSERT (0);
79 abort ();
Ed Warnickecb9cada2015-12-08 15:45:58 -070080 }
81
Dave Barach371e4e12016-07-08 09:38:52 -040082 if (shmem_hdr->client_rings == 0)
83 {
84 clib_warning ("client_rings NULL");
Dave Barach77378332016-10-13 17:35:09 -040085 ASSERT (0);
86 abort ();
Ed Warnickecb9cada2015-12-08 15:45:58 -070087 }
88
Dave Barach371e4e12016-07-08 09:38:52 -040089 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 }
Ed Warnickecb9cada2015-12-08 15:45:58 -070097
Dave Barach371e4e12016-07-08 09:38:52 -040098 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 {
Dave Barach842b9c52017-01-09 15:54:00 -0500109 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)
Dave Barach10d8cc62017-05-30 09:30:07 -0400118 {
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);
Dave Barach39d69112019-11-27 11:42:13 -0500125 if (msg_id < vec_len (vlibapi_get_main ()->msg_names))
Dave Barach10d8cc62017-05-30 09:30:07 -0400126 clib_warning ("msg id %d name %s", (u32) msg_id,
Dave Barach39d69112019-11-27 11:42:13 -0500127 vlibapi_get_main ()->msg_names[msg_id]);
Dave Barach10d8cc62017-05-30 09:30:07 -0400128 }
Dave Barach842b9c52017-01-09 15:54:00 -0500129 shmem_hdr->garbage_collects++;
130 goto collected;
131 }
132 }
133
134
Dave Barach371e4e12016-07-08 09:38:52 -0400135 /* yes, loser; try next larger pool */
136 ap[i].misses++;
137 if (pool == 0)
138 pthread_mutex_unlock (&q->mutex);
139 continue;
140 }
Dave Barach842b9c52017-01-09 15:54:00 -0500141 collected:
142
Dave Barach371e4e12016-07-08 09:38:52 -0400143 /* 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;
Dave Barach842b9c52017-01-09 15:54:00 -0500150 rv->gc_mark_timestamp = 0;
Dave Barach371e4e12016-07-08 09:38:52 -0400151 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;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700158 }
159
Dave Barach371e4e12016-07-08 09:38:52 -0400160 /*
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++;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700165
Florin Coras8d820852019-11-27 09:15:25 -0800166 pthread_mutex_lock (&vlib_rp->mutex);
167 oldheap = svm_push_data_heap (vlib_rp);
Dave Barach77378332016-10-13 17:35:09 -0400168 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);
Florin Coras8d820852019-11-27 09:15:25 -0800174 pthread_mutex_unlock (&vlib_rp->mutex);
Dave Barach77378332016-10-13 17:35:09 -0400175 return 0;
176 }
177 }
178 else
179 rv = clib_mem_alloc (nbytes);
180
Dave Barach371e4e12016-07-08 09:38:52 -0400181 rv->q = 0;
Brian Nesbitt194212c2019-01-22 14:47:53 +0000182 rv->gc_mark_timestamp = 0;
Dave Barach371e4e12016-07-08 09:38:52 -0400183 svm_pop_heap (oldheap);
Florin Coras8d820852019-11-27 09:15:25 -0800184 pthread_mutex_unlock (&vlib_rp->mutex);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700185
Dave Barach371e4e12016-07-08 09:38:52 -0400186out:
Dave Barach59b25652017-09-10 15:04:27 -0400187#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
Dave Barach371e4e12016-07-08 09:38:52 -0400195 rv->data_len = htonl (nbytes - sizeof (msgbuf_t));
Dave Barach59b25652017-09-10 15:04:27 -0400196
Benoît Ganne9fb6d402019-04-15 15:28:21 +0200197 VL_MSG_API_UNPOISON (rv->data);
Dave Barach371e4e12016-07-08 09:38:52 -0400198 return (rv->data);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700199}
200
Dave Barach371e4e12016-07-08 09:38:52 -0400201void *
202vl_msg_api_alloc (int nbytes)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700203{
Dave Barach371e4e12016-07-08 09:38:52 -0400204 int pool;
Dave Barach39d69112019-11-27 11:42:13 -0500205 api_main_t *am = vlibapi_get_main ();
Dave Barach371e4e12016-07-08 09:38:52 -0400206 vl_shmem_hdr_t *shmem_hdr = am->shmem_hdr;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700207
Dave Barach371e4e12016-07-08 09:38:52 -0400208 /*
209 * Clients use pool-0, vlib proc uses pool 1
210 */
211 pool = (am->our_pid == shmem_hdr->vl_pid);
Florin Coras8d820852019-11-27 09:15:25 -0800212 return vl_msg_api_alloc_internal (am->vlib_rp, nbytes, pool,
213 0 /* may_return_null */ );
Dave Barach77378332016-10-13 17:35:09 -0400214}
215
216void *
Vratko Polakfc4828c2019-07-02 11:07:24 +0200217vl_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
226void *
Dave Barach77378332016-10-13 17:35:09 -0400227vl_msg_api_alloc_or_null (int nbytes)
228{
229 int pool;
Dave Barach39d69112019-11-27 11:42:13 -0500230 api_main_t *am = vlibapi_get_main ();
Dave Barach77378332016-10-13 17:35:09 -0400231 vl_shmem_hdr_t *shmem_hdr = am->shmem_hdr;
232
233 pool = (am->our_pid == shmem_hdr->vl_pid);
Florin Coras8d820852019-11-27 09:15:25 -0800234 return vl_msg_api_alloc_internal (am->vlib_rp, nbytes, pool,
235 1 /* may_return_null */ );
Ed Warnickecb9cada2015-12-08 15:45:58 -0700236}
237
Dave Barach371e4e12016-07-08 09:38:52 -0400238void *
239vl_msg_api_alloc_as_if_client (int nbytes)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700240{
Dave Barach39d69112019-11-27 11:42:13 -0500241 api_main_t *am = vlibapi_get_main ();
242 return vl_msg_api_alloc_internal (am->vlib_rp, nbytes, 0,
Florin Coras8d820852019-11-27 09:15:25 -0800243 0 /* may_return_null */ );
Dave Barach77378332016-10-13 17:35:09 -0400244}
245
246void *
Vratko Polakfc4828c2019-07-02 11:07:24 +0200247vl_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
256void *
Dave Barach77378332016-10-13 17:35:09 -0400257vl_msg_api_alloc_as_if_client_or_null (int nbytes)
258{
Dave Barach39d69112019-11-27 11:42:13 -0500259 api_main_t *am = vlibapi_get_main ();
260 return vl_msg_api_alloc_internal (am->vlib_rp, nbytes, 0,
Florin Coras8d820852019-11-27 09:15:25 -0800261 1 /* may_return_null */ );
Ed Warnickecb9cada2015-12-08 15:45:58 -0700262}
263
Florin Corasb384b542018-01-15 01:08:33 -0800264void *
265vl_mem_api_alloc_as_if_client_w_reg (vl_api_registration_t * reg, int nbytes)
266{
Florin Coras8d820852019-11-27 09:15:25 -0800267 return vl_msg_api_alloc_internal (reg->vlib_rp, nbytes, 0,
268 0 /* may_return_null */ );
Florin Corasb384b542018-01-15 01:08:33 -0800269}
270
Dave Barach371e4e12016-07-08 09:38:52 -0400271void
Florin Coras8d820852019-11-27 09:15:25 -0800272vl_msg_api_free_w_region (svm_region_t * vlib_rp, void *a)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700273{
Dave Barach371e4e12016-07-08 09:38:52 -0400274 msgbuf_t *rv;
275 void *oldheap;
Ole Troan6855f6c2016-04-09 03:16:30 +0200276
Dave Barach371e4e12016-07-08 09:38:52 -0400277 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;
Dave Barach842b9c52017-01-09 15:54:00 -0500287 rv->gc_mark_timestamp = 0;
Dave Barach59b25652017-09-10 15:04:27 -0400288#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
Benoît Ganne9fb6d402019-04-15 15:28:21 +0200295 VL_MSG_API_POISON (rv->data);
Dave Barach371e4e12016-07-08 09:38:52 -0400296 return;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700297 }
298
Florin Coras8d820852019-11-27 09:15:25 -0800299 pthread_mutex_lock (&vlib_rp->mutex);
300 oldheap = svm_push_data_heap (vlib_rp);
Dave Barach59b25652017-09-10 15:04:27 -0400301
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
Dave Barach371e4e12016-07-08 09:38:52 -0400310 clib_mem_free (rv);
311 svm_pop_heap (oldheap);
Florin Coras8d820852019-11-27 09:15:25 -0800312 pthread_mutex_unlock (&vlib_rp->mutex);
313}
314
315void
316vl_msg_api_free (void *a)
317{
Dave Barach39d69112019-11-27 11:42:13 -0500318 api_main_t *am = vlibapi_get_main ();
319 vl_msg_api_free_w_region (am->vlib_rp, a);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700320}
321
Dave Barach371e4e12016-07-08 09:38:52 -0400322static void
323vl_msg_api_free_nolock (void *a)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700324{
Dave Barach371e4e12016-07-08 09:38:52 -0400325 msgbuf_t *rv;
326 void *oldheap;
Dave Barach39d69112019-11-27 11:42:13 -0500327 api_main_t *am = vlibapi_get_main ();
Dave Barach371e4e12016-07-08 09:38:52 -0400328
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;
Benoît Ganne9fb6d402019-04-15 15:28:21 +0200338 VL_MSG_API_POISON (rv->data);
Dave Barach371e4e12016-07-08 09:38:52 -0400339 return;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700340 }
341
Dave Barach371e4e12016-07-08 09:38:52 -0400342 oldheap = svm_push_data_heap (am->vlib_rp);
343 clib_mem_free (rv);
344 svm_pop_heap (oldheap);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700345}
346
Dave Barach371e4e12016-07-08 09:38:52 -0400347void
Neale Rannse72be392017-04-26 13:59:20 -0700348vl_set_memory_root_path (const char *name)
Dave Barach309bef22016-01-22 16:09:52 -0500349{
Dave Barach39d69112019-11-27 11:42:13 -0500350 api_main_t *am = vlibapi_get_main ();
Dave Barach309bef22016-01-22 16:09:52 -0500351
Dave Barach371e4e12016-07-08 09:38:52 -0400352 am->root_path = name;
Dave Barach309bef22016-01-22 16:09:52 -0500353}
354
Dave Barach371e4e12016-07-08 09:38:52 -0400355void
356vl_set_memory_uid (int uid)
Dave Barach16c75df2016-05-31 14:05:46 -0400357{
Dave Barach39d69112019-11-27 11:42:13 -0500358 api_main_t *am = vlibapi_get_main ();
Dave Barach16c75df2016-05-31 14:05:46 -0400359
Dave Barach371e4e12016-07-08 09:38:52 -0400360 am->api_uid = uid;
Dave Barach16c75df2016-05-31 14:05:46 -0400361}
362
Dave Barach371e4e12016-07-08 09:38:52 -0400363void
364vl_set_memory_gid (int gid)
Dave Barach16c75df2016-05-31 14:05:46 -0400365{
Dave Barach39d69112019-11-27 11:42:13 -0500366 api_main_t *am = vlibapi_get_main ();
Dave Barach16c75df2016-05-31 14:05:46 -0400367
Dave Barach371e4e12016-07-08 09:38:52 -0400368 am->api_gid = gid;
Dave Barach16c75df2016-05-31 14:05:46 -0400369}
370
Dave Barachb3d93da2016-08-03 14:34:38 -0400371void
372vl_set_global_memory_baseva (u64 baseva)
373{
Dave Barach39d69112019-11-27 11:42:13 -0500374 api_main_t *am = vlibapi_get_main ();
Dave Barachb3d93da2016-08-03 14:34:38 -0400375
376 am->global_baseva = baseva;
377}
378
379void
380vl_set_global_memory_size (u64 size)
381{
Dave Barach39d69112019-11-27 11:42:13 -0500382 api_main_t *am = vlibapi_get_main ();
Dave Barachb3d93da2016-08-03 14:34:38 -0400383
384 am->global_size = size;
385}
386
387void
388vl_set_api_memory_size (u64 size)
389{
Dave Barach39d69112019-11-27 11:42:13 -0500390 api_main_t *am = vlibapi_get_main ();
Dave Barachb3d93da2016-08-03 14:34:38 -0400391
392 am->api_size = size;
393}
394
395void
396vl_set_global_pvt_heap_size (u64 size)
397{
Dave Barach39d69112019-11-27 11:42:13 -0500398 api_main_t *am = vlibapi_get_main ();
Dave Barachb3d93da2016-08-03 14:34:38 -0400399
400 am->global_pvt_heap_size = size;
401}
402
403void
404vl_set_api_pvt_heap_size (u64 size)
405{
Dave Barach39d69112019-11-27 11:42:13 -0500406 api_main_t *am = vlibapi_get_main ();
Dave Barachb3d93da2016-08-03 14:34:38 -0400407
408 am->api_pvt_heap_size = size;
409}
410
Florin Coras90a63982017-12-19 04:50:01 -0800411static void
412vl_api_default_mem_config (vl_shmem_hdr_t * shmem_hdr)
Dave Barach59b25652017-09-10 15:04:27 -0400413{
Dave Barach39d69112019-11-27 11:42:13 -0500414 api_main_t *am = vlibapi_get_main ();
Dave Barach59b25652017-09-10 15:04:27 -0400415 u32 vlib_input_queue_length;
Dave Barach59b25652017-09-10 15:04:27 -0400416
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 =
Florin Corasc470e222018-08-01 07:53:18 -0700423 svm_queue_alloc_and_init (vlib_input_queue_length, sizeof (uword),
424 getpid ());
Dave Barach59b25652017-09-10 15:04:27 -0400425
Dave Barach59b25652017-09-10 15:04:27 -0400426#define _(sz,n) \
427 do { \
428 ring_alloc_t _rp; \
Florin Corasc470e222018-08-01 07:53:18 -0700429 _rp.rp = svm_queue_alloc_and_init ((n), (sz), 0); \
Dave Barach59b25652017-09-10 15:04:27 -0400430 _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; \
Florin Corasc470e222018-08-01 07:53:18 -0700443 _rp.rp = svm_queue_alloc_and_init ((n), (sz), 0); \
Dave Barach59b25652017-09-10 15:04:27 -0400444 _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 _
Florin Coras90a63982017-12-19 04:50:01 -0800453}
454
455void
456vl_api_mem_config (vl_shmem_hdr_t * hdr, vl_api_shm_elem_config_t * config)
457{
Florin Coras90a63982017-12-19 04:50:01 -0800458 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:
Florin Corasc470e222018-08-01 07:53:18 -0700473 hdr->vl_input_queue = svm_queue_alloc_and_init (c->count, c->size,
474 getpid ());
Florin Coras90a63982017-12-19 04:50:01 -0800475 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;
Florin Corasc470e222018-08-01 07:53:18 -0700488 rp->rp = svm_queue_alloc_and_init (c->count, size, 0);
Florin Coras90a63982017-12-19 04:50:01 -0800489 rp->size = size;
490 rp->nitems = c->count;
491 rp->hits = 0;
492 rp->misses = 0;
493 }
494}
495
496void
497vl_init_shmem (svm_region_t * vlib_rp, vl_api_shm_elem_config_t * config,
498 int is_vlib, int is_private_region)
499{
Dave Barach39d69112019-11-27 11:42:13 -0500500 api_main_t *am = vlibapi_get_main ();
Florin Coras90a63982017-12-19 04:50:01 -0800501 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;
Florin Corasb384b542018-01-15 01:08:33 -0800511 shmem_hdr->clib_file_index = VL_API_INVALID_FI;
Florin Coras90a63982017-12-19 04:50:01 -0800512
513 /* Set up the queue and msg ring allocator */
514 vl_api_mem_config (shmem_hdr, config);
Dave Barach59b25652017-09-10 15:04:27 -0400515
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
Dave Barach371e4e12016-07-08 09:38:52 -0400538int
Neale Rannse72be392017-04-26 13:59:20 -0700539vl_map_shmem (const char *region_name, int is_vlib)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700540{
Dave Barach371e4e12016-07-08 09:38:52 -0400541 svm_map_region_args_t _a, *a = &_a;
542 svm_region_t *vlib_rp, *root_rp;
Dave Barach39d69112019-11-27 11:42:13 -0500543 api_main_t *am = vlibapi_get_main ();
Neale Ranns30307af2019-02-19 00:41:22 -0800544 int i;
Dave Barach371e4e12016-07-08 09:38:52 -0400545 struct timespec ts, tsrem;
Dave Wallacecfc997e2017-08-22 18:32:34 -0400546 char *vpe_api_region_suffix = "-vpe-api";
Ed Warnickecb9cada2015-12-08 15:45:58 -0700547
Dave Barachb7b92992018-10-17 10:38:51 -0400548 clib_memset (a, 0, sizeof (*a));
Jan Srnicek5beec812017-03-24 10:18:11 +0100549
Dave Wallacecfc997e2017-08-22 18:32:34 -0400550 if (strstr (region_name, vpe_api_region_suffix))
Jan Srnicek5beec812017-03-24 10:18:11 +0100551 {
Dave Wallacecfc997e2017-08-22 18:32:34 -0400552 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;
Jan Srnicek5beec812017-03-24 10:18:11 +0100558 }
559
Dave Barach371e4e12016-07-08 09:38:52 -0400560 if (is_vlib == 0)
Ole Troan3cdc25f2017-08-17 11:07:33 +0200561 {
Dave Barach22af4472018-12-27 13:57:41 -0500562 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);
Steven Luong89053472019-03-05 20:07:31 -0800581 if (tfd >= 0)
Dave Barach22af4472018-12-27 13:57:41 -0500582 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);
Neale Ranns30307af2019-02-19 00:41:22 -0800591 svm_region_init_chroot_uid_gid (am->root_path, getuid (), getgid ());
Ole Troan3cdc25f2017-08-17 11:07:33 +0200592 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700593
Jan Srnicek5beec812017-03-24 10:18:11 +0100594 if (a->root_path != NULL)
595 {
596 a->name = "/vpe-api";
597 }
598 else
599 a->name = region_name;
Dave Barachc3799992016-08-15 11:12:27 -0400600 a->size = am->api_size ? am->api_size : (16 << 20);
Dave Barach371e4e12016-07-08 09:38:52 -0400601 a->flags = SVM_FLAGS_MHEAP;
602 a->uid = am->api_uid;
603 a->gid = am->api_gid;
Dave Barachb3d93da2016-08-03 14:34:38 -0400604 a->pvt_heap_size = am->api_pvt_heap_size;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700605
Dave Barach371e4e12016-07-08 09:38:52 -0400606 vlib_rp = svm_region_find_or_create (a);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700607
Dave Barach371e4e12016-07-08 09:38:52 -0400608 if (vlib_rp == 0)
609 return (-2);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700610
Dave Barach371e4e12016-07-08 09:38:52 -0400611 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 {
Florin Corase86a8ed2018-01-05 03:20:25 -0800619 svm_queue_t *q;
Dave Barach371e4e12016-07-08 09:38:52 -0400620 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 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700626
Dave Barach371e4e12016-07-08 09:38:52 -0400627 /* 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 */
Dave Barachb7b92992018-10-17 10:38:51 -0400647 clib_memset (&q->mutex, 0, sizeof (q->mutex));
Dave Barach371e4e12016-07-08 09:38:52 -0400648 clib_warning ("forcibly release main input queue mutex");
649
650 mutex_ok:
651 am->vlib_rp = vlib_rp;
Mohsin Kazmi3fca5672018-01-04 18:57:26 +0100652 while (svm_queue_sub (q, (u8 *) & old_msg, SVM_Q_NOWAIT, 0)
Dave Barach371e4e12016-07-08 09:38:52 -0400653 != -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;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700673 }
Dave Barach371e4e12016-07-08 09:38:52 -0400674 /* Clients simply have to wait... */
675 if (!is_vlib)
676 {
677 pthread_mutex_unlock (&vlib_rp->mutex);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700678
Dave Barach371e4e12016-07-08 09:38:52 -0400679 /* 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);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700693
694 ready:
Dave Barach371e4e12016-07-08 09:38:52 -0400695 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;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700700 }
701
Dave Barach371e4e12016-07-08 09:38:52 -0400702 /* Nope, it's our problem... */
Florin Coras90a63982017-12-19 04:50:01 -0800703 vl_init_shmem (vlib_rp, 0 /* default config */ , 1 /* is vlib */ ,
704 0 /* is_private_region */ );
Ed Warnickecb9cada2015-12-08 15:45:58 -0700705
Dave Barach371e4e12016-07-08 09:38:52 -0400706 vec_add1 (am->mapped_shmem_regions, vlib_rp);
707 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700708}
709
Dave Barach371e4e12016-07-08 09:38:52 -0400710void
711vl_register_mapped_shmem_region (svm_region_t * rp)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700712{
Dave Barach39d69112019-11-27 11:42:13 -0500713 api_main_t *am = vlibapi_get_main ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700714
Dave Barach371e4e12016-07-08 09:38:52 -0400715 vec_add1 (am->mapped_shmem_regions, rp);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700716}
717
Florin Corasd6c30d92018-01-29 05:11:24 -0800718static void
719vl_unmap_shmem_internal (u8 is_client)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700720{
Dave Barach371e4e12016-07-08 09:38:52 -0400721 svm_region_t *rp;
722 int i;
Dave Barach39d69112019-11-27 11:42:13 -0500723 api_main_t *am = vlibapi_get_main ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700724
Dave Barach371e4e12016-07-08 09:38:52 -0400725 if (!svm_get_root_rp ())
726 return;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700727
Dave Barach371e4e12016-07-08 09:38:52 -0400728 for (i = 0; i < vec_len (am->mapped_shmem_regions); i++)
729 {
730 rp = am->mapped_shmem_regions[i];
Florin Corasd6c30d92018-01-29 05:11:24 -0800731 is_client ? svm_region_unmap_client (rp) : svm_region_unmap (rp);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700732 }
733
Dave Barach371e4e12016-07-08 09:38:52 -0400734 vec_free (am->mapped_shmem_regions);
735 am->shmem_hdr = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700736
Florin Corasd6c30d92018-01-29 05:11:24 -0800737 is_client ? svm_region_exit_client () : svm_region_exit ();
738
Dave Barach371e4e12016-07-08 09:38:52 -0400739 /* $$$ 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);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700743}
744
Dave Barach371e4e12016-07-08 09:38:52 -0400745void
Florin Corasd6c30d92018-01-29 05:11:24 -0800746vl_unmap_shmem (void)
747{
748 vl_unmap_shmem_internal (0);
749}
750
751void
752vl_unmap_shmem_client (void)
753{
754 vl_unmap_shmem_internal (1);
755}
756
757void
Florin Corase86a8ed2018-01-05 03:20:25 -0800758vl_msg_api_send_shmem (svm_queue_t * q, u8 * elem)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700759{
Dave Barach39d69112019-11-27 11:42:13 -0500760 api_main_t *am = vlibapi_get_main ();
Benoît Ganne9fb6d402019-04-15 15:28:21 +0200761 void *msg = (void *) *(uword *) elem;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700762
Dave Barach371e4e12016-07-08 09:38:52 -0400763 if (am->tx_trace && am->tx_trace->enabled)
Benoît Ganne9fb6d402019-04-15 15:28:21 +0200764 vl_msg_api_trace (am, am->tx_trace, msg);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700765
Dave Barach0c4fec02018-11-18 12:03:48 -0500766 /*
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)))
Dave Barachb09f4d02019-07-15 16:00:03 -0400773 {
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 }
Benoît Ganne9fb6d402019-04-15 15:28:21 +0200794 VL_MSG_API_POISON (msg);
Florin Corase86a8ed2018-01-05 03:20:25 -0800795 (void) svm_queue_add (q, elem, 0 /* nowait */ );
Ed Warnickecb9cada2015-12-08 15:45:58 -0700796}
797
Florin Corasaf0ff5a2018-01-10 08:17:03 -0800798int
799vl_mem_api_can_send (svm_queue_t * q)
800{
801 return (q->cursize < q->maxsize);
802}
803
Dave Barach371e4e12016-07-08 09:38:52 -0400804void
Florin Corase86a8ed2018-01-05 03:20:25 -0800805vl_msg_api_send_shmem_nolock (svm_queue_t * q, u8 * elem)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700806{
Dave Barach39d69112019-11-27 11:42:13 -0500807 api_main_t *am = vlibapi_get_main ();
Benoît Ganne9fb6d402019-04-15 15:28:21 +0200808 void *msg = (void *) *(uword *) elem;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700809
Dave Barach371e4e12016-07-08 09:38:52 -0400810 if (am->tx_trace && am->tx_trace->enabled)
Benoît Ganne9fb6d402019-04-15 15:28:21 +0200811 vl_msg_api_trace (am, am->tx_trace, msg);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700812
Florin Corase86a8ed2018-01-05 03:20:25 -0800813 (void) svm_queue_add_nolock (q, elem);
Benoît Ganne9fb6d402019-04-15 15:28:21 +0200814 VL_MSG_API_POISON (msg);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700815}
816
Dave Barach371e4e12016-07-08 09:38:52 -0400817/*
818 * fd.io coding-style-patch-verification: ON
819 *
820 * Local Variables:
821 * eval: (c-set-style "gnu")
822 * End:
823 */