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