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; |
Brian Nesbitt | 194212c | 2019-01-22 14:47:53 +0000 | [diff] [blame] | 179 | rv->gc_mark_timestamp = 0; |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 180 | svm_pop_heap (oldheap); |
| 181 | pthread_mutex_unlock (&am->vlib_rp->mutex); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 182 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 183 | out: |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 184 | #if DEBUG_MESSAGE_BUFFER_OVERRUN > 0 |
| 185 | { |
| 186 | nbytes -= 4; |
| 187 | u32 *overrun; |
| 188 | overrun = (u32 *) (rv->data + nbytes - sizeof (msgbuf_t)); |
| 189 | *overrun = 0x1badbabe; |
| 190 | } |
| 191 | #endif |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 192 | rv->data_len = htonl (nbytes - sizeof (msgbuf_t)); |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 193 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 194 | return (rv->data); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 195 | } |
| 196 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 197 | void * |
| 198 | vl_msg_api_alloc (int nbytes) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 199 | { |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 200 | int pool; |
| 201 | api_main_t *am = &api_main; |
| 202 | vl_shmem_hdr_t *shmem_hdr = am->shmem_hdr; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 203 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 204 | /* |
| 205 | * Clients use pool-0, vlib proc uses pool 1 |
| 206 | */ |
| 207 | pool = (am->our_pid == shmem_hdr->vl_pid); |
Dave Barach | 7737833 | 2016-10-13 17:35:09 -0400 | [diff] [blame] | 208 | return vl_msg_api_alloc_internal (nbytes, pool, 0 /* may_return_null */ ); |
| 209 | } |
| 210 | |
| 211 | void * |
Vratko Polak | fc4828c | 2019-07-02 11:07:24 +0200 | [diff] [blame] | 212 | vl_msg_api_alloc_zero (int nbytes) |
| 213 | { |
| 214 | void *ret; |
| 215 | |
| 216 | ret = vl_msg_api_alloc (nbytes); |
| 217 | clib_memset (ret, 0, nbytes); |
| 218 | return ret; |
| 219 | } |
| 220 | |
| 221 | void * |
Dave Barach | 7737833 | 2016-10-13 17:35:09 -0400 | [diff] [blame] | 222 | vl_msg_api_alloc_or_null (int nbytes) |
| 223 | { |
| 224 | int pool; |
| 225 | api_main_t *am = &api_main; |
| 226 | vl_shmem_hdr_t *shmem_hdr = am->shmem_hdr; |
| 227 | |
| 228 | pool = (am->our_pid == shmem_hdr->vl_pid); |
| 229 | return vl_msg_api_alloc_internal (nbytes, pool, 1 /* may_return_null */ ); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 230 | } |
| 231 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 232 | void * |
| 233 | vl_msg_api_alloc_as_if_client (int nbytes) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 234 | { |
Dave Barach | 7737833 | 2016-10-13 17:35:09 -0400 | [diff] [blame] | 235 | return vl_msg_api_alloc_internal (nbytes, 0, 0 /* may_return_null */ ); |
| 236 | } |
| 237 | |
| 238 | void * |
Vratko Polak | fc4828c | 2019-07-02 11:07:24 +0200 | [diff] [blame] | 239 | vl_msg_api_alloc_zero_as_if_client (int nbytes) |
| 240 | { |
| 241 | void *ret; |
| 242 | |
| 243 | ret = vl_msg_api_alloc_as_if_client (nbytes); |
| 244 | clib_memset (ret, 0, nbytes); |
| 245 | return ret; |
| 246 | } |
| 247 | |
| 248 | void * |
Dave Barach | 7737833 | 2016-10-13 17:35:09 -0400 | [diff] [blame] | 249 | vl_msg_api_alloc_as_if_client_or_null (int nbytes) |
| 250 | { |
| 251 | return vl_msg_api_alloc_internal (nbytes, 0, 1 /* may_return_null */ ); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 252 | } |
| 253 | |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 254 | void * |
| 255 | vl_mem_api_alloc_as_if_client_w_reg (vl_api_registration_t * reg, int nbytes) |
| 256 | { |
| 257 | api_main_t *am = &api_main; |
| 258 | vl_shmem_hdr_t *save_shmem_hdr = am->shmem_hdr; |
| 259 | svm_region_t *vlib_rp, *save_vlib_rp = am->vlib_rp; |
| 260 | void *msg; |
| 261 | |
| 262 | vlib_rp = am->vlib_rp = reg->vlib_rp; |
| 263 | am->shmem_hdr = (void *) vlib_rp->user_ctx; |
| 264 | |
| 265 | msg = vl_msg_api_alloc_internal (nbytes, 0, 0 /* may_return_null */ ); |
| 266 | |
| 267 | am->shmem_hdr = save_shmem_hdr; |
| 268 | am->vlib_rp = save_vlib_rp; |
| 269 | |
| 270 | return msg; |
| 271 | } |
| 272 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 273 | void |
| 274 | vl_msg_api_free (void *a) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 275 | { |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 276 | msgbuf_t *rv; |
| 277 | void *oldheap; |
| 278 | api_main_t *am = &api_main; |
Ole Troan | 6855f6c | 2016-04-09 03:16:30 +0200 | [diff] [blame] | 279 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 280 | rv = (msgbuf_t *) (((u8 *) a) - offsetof (msgbuf_t, data)); |
| 281 | |
| 282 | /* |
| 283 | * Here's the beauty of the scheme. Only one proc/thread has |
| 284 | * control of a given message buffer. To free a buffer, we just clear the |
| 285 | * queue field, and leave. No locks, no hits, no errors... |
| 286 | */ |
| 287 | if (rv->q) |
| 288 | { |
| 289 | rv->q = 0; |
Dave Barach | 842b9c5 | 2017-01-09 15:54:00 -0500 | [diff] [blame] | 290 | rv->gc_mark_timestamp = 0; |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 291 | #if DEBUG_MESSAGE_BUFFER_OVERRUN > 0 |
| 292 | { |
| 293 | u32 *overrun; |
| 294 | overrun = (u32 *) (rv->data + ntohl (rv->data_len)); |
| 295 | ASSERT (*overrun == 0x1badbabe); |
| 296 | } |
| 297 | #endif |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 298 | return; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 299 | } |
| 300 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 301 | pthread_mutex_lock (&am->vlib_rp->mutex); |
| 302 | oldheap = svm_push_data_heap (am->vlib_rp); |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 303 | |
| 304 | #if DEBUG_MESSAGE_BUFFER_OVERRUN > 0 |
| 305 | { |
| 306 | u32 *overrun; |
| 307 | overrun = (u32 *) (rv->data + ntohl (rv->data_len)); |
| 308 | ASSERT (*overrun == 0x1badbabe); |
| 309 | } |
| 310 | #endif |
| 311 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 312 | clib_mem_free (rv); |
| 313 | svm_pop_heap (oldheap); |
| 314 | pthread_mutex_unlock (&am->vlib_rp->mutex); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 315 | } |
| 316 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 317 | static void |
| 318 | vl_msg_api_free_nolock (void *a) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 319 | { |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 320 | msgbuf_t *rv; |
| 321 | void *oldheap; |
| 322 | api_main_t *am = &api_main; |
| 323 | |
| 324 | rv = (msgbuf_t *) (((u8 *) a) - offsetof (msgbuf_t, data)); |
| 325 | /* |
| 326 | * Here's the beauty of the scheme. Only one proc/thread has |
| 327 | * control of a given message buffer. To free a buffer, we just clear the |
| 328 | * queue field, and leave. No locks, no hits, no errors... |
| 329 | */ |
| 330 | if (rv->q) |
| 331 | { |
| 332 | rv->q = 0; |
| 333 | return; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 334 | } |
| 335 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 336 | oldheap = svm_push_data_heap (am->vlib_rp); |
| 337 | clib_mem_free (rv); |
| 338 | svm_pop_heap (oldheap); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 339 | } |
| 340 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 341 | void |
Neale Ranns | e72be39 | 2017-04-26 13:59:20 -0700 | [diff] [blame] | 342 | vl_set_memory_root_path (const char *name) |
Dave Barach | 309bef2 | 2016-01-22 16:09:52 -0500 | [diff] [blame] | 343 | { |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 344 | api_main_t *am = &api_main; |
Dave Barach | 309bef2 | 2016-01-22 16:09:52 -0500 | [diff] [blame] | 345 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 346 | am->root_path = name; |
Dave Barach | 309bef2 | 2016-01-22 16:09:52 -0500 | [diff] [blame] | 347 | } |
| 348 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 349 | void |
| 350 | vl_set_memory_uid (int uid) |
Dave Barach | 16c75df | 2016-05-31 14:05:46 -0400 | [diff] [blame] | 351 | { |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 352 | api_main_t *am = &api_main; |
Dave Barach | 16c75df | 2016-05-31 14:05:46 -0400 | [diff] [blame] | 353 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 354 | am->api_uid = uid; |
Dave Barach | 16c75df | 2016-05-31 14:05:46 -0400 | [diff] [blame] | 355 | } |
| 356 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 357 | void |
| 358 | vl_set_memory_gid (int gid) |
Dave Barach | 16c75df | 2016-05-31 14:05:46 -0400 | [diff] [blame] | 359 | { |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 360 | api_main_t *am = &api_main; |
Dave Barach | 16c75df | 2016-05-31 14:05:46 -0400 | [diff] [blame] | 361 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 362 | am->api_gid = gid; |
Dave Barach | 16c75df | 2016-05-31 14:05:46 -0400 | [diff] [blame] | 363 | } |
| 364 | |
Dave Barach | b3d93da | 2016-08-03 14:34:38 -0400 | [diff] [blame] | 365 | void |
| 366 | vl_set_global_memory_baseva (u64 baseva) |
| 367 | { |
| 368 | api_main_t *am = &api_main; |
| 369 | |
| 370 | am->global_baseva = baseva; |
| 371 | } |
| 372 | |
| 373 | void |
| 374 | vl_set_global_memory_size (u64 size) |
| 375 | { |
| 376 | api_main_t *am = &api_main; |
| 377 | |
| 378 | am->global_size = size; |
| 379 | } |
| 380 | |
| 381 | void |
| 382 | vl_set_api_memory_size (u64 size) |
| 383 | { |
| 384 | api_main_t *am = &api_main; |
| 385 | |
| 386 | am->api_size = size; |
| 387 | } |
| 388 | |
| 389 | void |
| 390 | vl_set_global_pvt_heap_size (u64 size) |
| 391 | { |
| 392 | api_main_t *am = &api_main; |
| 393 | |
| 394 | am->global_pvt_heap_size = size; |
| 395 | } |
| 396 | |
| 397 | void |
| 398 | vl_set_api_pvt_heap_size (u64 size) |
| 399 | { |
| 400 | api_main_t *am = &api_main; |
| 401 | |
| 402 | am->api_pvt_heap_size = size; |
| 403 | } |
| 404 | |
Florin Coras | 90a6398 | 2017-12-19 04:50:01 -0800 | [diff] [blame] | 405 | static void |
| 406 | vl_api_default_mem_config (vl_shmem_hdr_t * shmem_hdr) |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 407 | { |
| 408 | api_main_t *am = &api_main; |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 409 | u32 vlib_input_queue_length; |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 410 | |
| 411 | /* vlib main input queue */ |
| 412 | vlib_input_queue_length = 1024; |
| 413 | if (am->vlib_input_queue_length) |
| 414 | vlib_input_queue_length = am->vlib_input_queue_length; |
| 415 | |
| 416 | shmem_hdr->vl_input_queue = |
Florin Coras | c470e22 | 2018-08-01 07:53:18 -0700 | [diff] [blame] | 417 | svm_queue_alloc_and_init (vlib_input_queue_length, sizeof (uword), |
| 418 | getpid ()); |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 419 | |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 420 | #define _(sz,n) \ |
| 421 | do { \ |
| 422 | ring_alloc_t _rp; \ |
Florin Coras | c470e22 | 2018-08-01 07:53:18 -0700 | [diff] [blame] | 423 | _rp.rp = svm_queue_alloc_and_init ((n), (sz), 0); \ |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 424 | _rp.size = (sz); \ |
| 425 | _rp.nitems = n; \ |
| 426 | _rp.hits = 0; \ |
| 427 | _rp.misses = 0; \ |
| 428 | vec_add1(shmem_hdr->vl_rings, _rp); \ |
| 429 | } while (0); |
| 430 | |
| 431 | foreach_vl_aring_size; |
| 432 | #undef _ |
| 433 | |
| 434 | #define _(sz,n) \ |
| 435 | do { \ |
| 436 | ring_alloc_t _rp; \ |
Florin Coras | c470e22 | 2018-08-01 07:53:18 -0700 | [diff] [blame] | 437 | _rp.rp = svm_queue_alloc_and_init ((n), (sz), 0); \ |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 438 | _rp.size = (sz); \ |
| 439 | _rp.nitems = n; \ |
| 440 | _rp.hits = 0; \ |
| 441 | _rp.misses = 0; \ |
| 442 | vec_add1(shmem_hdr->client_rings, _rp); \ |
| 443 | } while (0); |
| 444 | |
| 445 | foreach_clnt_aring_size; |
| 446 | #undef _ |
Florin Coras | 90a6398 | 2017-12-19 04:50:01 -0800 | [diff] [blame] | 447 | } |
| 448 | |
| 449 | void |
| 450 | vl_api_mem_config (vl_shmem_hdr_t * hdr, vl_api_shm_elem_config_t * config) |
| 451 | { |
Florin Coras | 90a6398 | 2017-12-19 04:50:01 -0800 | [diff] [blame] | 452 | vl_api_shm_elem_config_t *c; |
| 453 | ring_alloc_t *rp; |
| 454 | u32 size; |
| 455 | |
| 456 | if (!config) |
| 457 | { |
| 458 | vl_api_default_mem_config (hdr); |
| 459 | return; |
| 460 | } |
| 461 | |
| 462 | vec_foreach (c, config) |
| 463 | { |
| 464 | switch (c->type) |
| 465 | { |
| 466 | case VL_API_QUEUE: |
Florin Coras | c470e22 | 2018-08-01 07:53:18 -0700 | [diff] [blame] | 467 | hdr->vl_input_queue = svm_queue_alloc_and_init (c->count, c->size, |
| 468 | getpid ()); |
Florin Coras | 90a6398 | 2017-12-19 04:50:01 -0800 | [diff] [blame] | 469 | continue; |
| 470 | case VL_API_VLIB_RING: |
| 471 | vec_add2 (hdr->vl_rings, rp, 1); |
| 472 | break; |
| 473 | case VL_API_CLIENT_RING: |
| 474 | vec_add2 (hdr->client_rings, rp, 1); |
| 475 | break; |
| 476 | default: |
| 477 | clib_warning ("unknown config type: %d", c->type); |
| 478 | continue; |
| 479 | } |
| 480 | |
| 481 | size = sizeof (ring_alloc_t) + c->size; |
Florin Coras | c470e22 | 2018-08-01 07:53:18 -0700 | [diff] [blame] | 482 | rp->rp = svm_queue_alloc_and_init (c->count, size, 0); |
Florin Coras | 90a6398 | 2017-12-19 04:50:01 -0800 | [diff] [blame] | 483 | rp->size = size; |
| 484 | rp->nitems = c->count; |
| 485 | rp->hits = 0; |
| 486 | rp->misses = 0; |
| 487 | } |
| 488 | } |
| 489 | |
| 490 | void |
| 491 | vl_init_shmem (svm_region_t * vlib_rp, vl_api_shm_elem_config_t * config, |
| 492 | int is_vlib, int is_private_region) |
| 493 | { |
| 494 | api_main_t *am = &api_main; |
| 495 | vl_shmem_hdr_t *shmem_hdr = 0; |
| 496 | void *oldheap; |
| 497 | ASSERT (vlib_rp); |
| 498 | |
| 499 | /* $$$$ need private region config parameters */ |
| 500 | |
| 501 | oldheap = svm_push_data_heap (vlib_rp); |
| 502 | |
| 503 | vec_validate (shmem_hdr, 0); |
| 504 | shmem_hdr->version = VL_SHM_VERSION; |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 505 | shmem_hdr->clib_file_index = VL_API_INVALID_FI; |
Florin Coras | 90a6398 | 2017-12-19 04:50:01 -0800 | [diff] [blame] | 506 | |
| 507 | /* Set up the queue and msg ring allocator */ |
| 508 | vl_api_mem_config (shmem_hdr, config); |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 509 | |
| 510 | if (is_private_region == 0) |
| 511 | { |
| 512 | am->shmem_hdr = shmem_hdr; |
| 513 | am->vlib_rp = vlib_rp; |
| 514 | am->our_pid = getpid (); |
| 515 | if (is_vlib) |
| 516 | am->shmem_hdr->vl_pid = am->our_pid; |
| 517 | } |
| 518 | else |
| 519 | shmem_hdr->vl_pid = am->our_pid; |
| 520 | |
| 521 | svm_pop_heap (oldheap); |
| 522 | |
| 523 | /* |
| 524 | * After absolutely everything that a client might see is set up, |
| 525 | * declare the shmem region valid |
| 526 | */ |
| 527 | vlib_rp->user_ctx = shmem_hdr; |
| 528 | |
| 529 | pthread_mutex_unlock (&vlib_rp->mutex); |
| 530 | } |
| 531 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 532 | int |
Neale Ranns | e72be39 | 2017-04-26 13:59:20 -0700 | [diff] [blame] | 533 | vl_map_shmem (const char *region_name, int is_vlib) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 534 | { |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 535 | svm_map_region_args_t _a, *a = &_a; |
| 536 | svm_region_t *vlib_rp, *root_rp; |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 537 | api_main_t *am = &api_main; |
Neale Ranns | 30307af | 2019-02-19 00:41:22 -0800 | [diff] [blame] | 538 | int i; |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 539 | struct timespec ts, tsrem; |
Dave Wallace | cfc997e | 2017-08-22 18:32:34 -0400 | [diff] [blame] | 540 | char *vpe_api_region_suffix = "-vpe-api"; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 541 | |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 542 | clib_memset (a, 0, sizeof (*a)); |
Jan Srnicek | 5beec81 | 2017-03-24 10:18:11 +0100 | [diff] [blame] | 543 | |
Dave Wallace | cfc997e | 2017-08-22 18:32:34 -0400 | [diff] [blame] | 544 | if (strstr (region_name, vpe_api_region_suffix)) |
Jan Srnicek | 5beec81 | 2017-03-24 10:18:11 +0100 | [diff] [blame] | 545 | { |
Dave Wallace | cfc997e | 2017-08-22 18:32:34 -0400 | [diff] [blame] | 546 | u8 *root_path = format (0, "%s", region_name); |
| 547 | _vec_len (root_path) = (vec_len (root_path) - |
| 548 | strlen (vpe_api_region_suffix)); |
| 549 | vec_terminate_c_string (root_path); |
| 550 | a->root_path = (const char *) root_path; |
| 551 | am->root_path = (const char *) root_path; |
Jan Srnicek | 5beec81 | 2017-03-24 10:18:11 +0100 | [diff] [blame] | 552 | } |
| 553 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 554 | if (is_vlib == 0) |
Ole Troan | 3cdc25f | 2017-08-17 11:07:33 +0200 | [diff] [blame] | 555 | { |
Dave Barach | 22af447 | 2018-12-27 13:57:41 -0500 | [diff] [blame] | 556 | int tfd; |
| 557 | u8 *api_name; |
| 558 | /* |
| 559 | * Clients wait for vpp to set up the root / API regioins |
| 560 | */ |
| 561 | if (am->root_path) |
| 562 | api_name = format (0, "/dev/shm/%s-%s%c", am->root_path, |
| 563 | region_name + 1, 0); |
| 564 | else |
| 565 | api_name = format (0, "/dev/shm%s%c", region_name, 0); |
| 566 | |
| 567 | /* Wait up to 100 seconds... */ |
| 568 | for (i = 0; i < 10000; i++) |
| 569 | { |
| 570 | ts.tv_sec = 0; |
| 571 | ts.tv_nsec = 10000 * 1000; /* 10 ms */ |
| 572 | while (nanosleep (&ts, &tsrem) < 0) |
| 573 | ts = tsrem; |
| 574 | tfd = open ((char *) api_name, O_RDWR); |
Steven Luong | 8905347 | 2019-03-05 20:07:31 -0800 | [diff] [blame] | 575 | if (tfd >= 0) |
Dave Barach | 22af447 | 2018-12-27 13:57:41 -0500 | [diff] [blame] | 576 | break; |
| 577 | } |
| 578 | vec_free (api_name); |
| 579 | if (tfd < 0) |
| 580 | { |
| 581 | clib_warning ("region init fail"); |
| 582 | return -2; |
| 583 | } |
| 584 | close (tfd); |
Neale Ranns | 30307af | 2019-02-19 00:41:22 -0800 | [diff] [blame] | 585 | svm_region_init_chroot_uid_gid (am->root_path, getuid (), getgid ()); |
Ole Troan | 3cdc25f | 2017-08-17 11:07:33 +0200 | [diff] [blame] | 586 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 587 | |
Jan Srnicek | 5beec81 | 2017-03-24 10:18:11 +0100 | [diff] [blame] | 588 | if (a->root_path != NULL) |
| 589 | { |
| 590 | a->name = "/vpe-api"; |
| 591 | } |
| 592 | else |
| 593 | a->name = region_name; |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 594 | a->size = am->api_size ? am->api_size : (16 << 20); |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 595 | a->flags = SVM_FLAGS_MHEAP; |
| 596 | a->uid = am->api_uid; |
| 597 | a->gid = am->api_gid; |
Dave Barach | b3d93da | 2016-08-03 14:34:38 -0400 | [diff] [blame] | 598 | a->pvt_heap_size = am->api_pvt_heap_size; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 599 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 600 | vlib_rp = svm_region_find_or_create (a); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 601 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 602 | if (vlib_rp == 0) |
| 603 | return (-2); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 604 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 605 | pthread_mutex_lock (&vlib_rp->mutex); |
| 606 | /* Has someone else set up the shared-memory variable table? */ |
| 607 | if (vlib_rp->user_ctx) |
| 608 | { |
| 609 | am->shmem_hdr = (void *) vlib_rp->user_ctx; |
| 610 | am->our_pid = getpid (); |
| 611 | if (is_vlib) |
| 612 | { |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 613 | svm_queue_t *q; |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 614 | uword old_msg; |
| 615 | /* |
| 616 | * application restart. Reset cached pids, API message |
| 617 | * rings, list of clients; otherwise, various things |
| 618 | * fail. (e.g. queue non-empty notification) |
| 619 | */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 620 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 621 | /* ghosts keep the region from disappearing properly */ |
| 622 | svm_client_scan_this_region_nolock (vlib_rp); |
| 623 | am->shmem_hdr->application_restarts++; |
| 624 | q = am->shmem_hdr->vl_input_queue; |
| 625 | am->shmem_hdr->vl_pid = getpid (); |
| 626 | q->consumer_pid = am->shmem_hdr->vl_pid; |
| 627 | /* Drain the input queue, freeing msgs */ |
| 628 | for (i = 0; i < 10; i++) |
| 629 | { |
| 630 | if (pthread_mutex_trylock (&q->mutex) == 0) |
| 631 | { |
| 632 | pthread_mutex_unlock (&q->mutex); |
| 633 | goto mutex_ok; |
| 634 | } |
| 635 | ts.tv_sec = 0; |
| 636 | ts.tv_nsec = 10000 * 1000; /* 10 ms */ |
| 637 | while (nanosleep (&ts, &tsrem) < 0) |
| 638 | ts = tsrem; |
| 639 | } |
| 640 | /* Mutex buggered, "fix" it */ |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 641 | clib_memset (&q->mutex, 0, sizeof (q->mutex)); |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 642 | clib_warning ("forcibly release main input queue mutex"); |
| 643 | |
| 644 | mutex_ok: |
| 645 | am->vlib_rp = vlib_rp; |
Mohsin Kazmi | 3fca567 | 2018-01-04 18:57:26 +0100 | [diff] [blame] | 646 | while (svm_queue_sub (q, (u8 *) & old_msg, SVM_Q_NOWAIT, 0) |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 647 | != -2 /* queue underflow */ ) |
| 648 | { |
| 649 | vl_msg_api_free_nolock ((void *) old_msg); |
| 650 | am->shmem_hdr->restart_reclaims++; |
| 651 | } |
| 652 | pthread_mutex_unlock (&vlib_rp->mutex); |
| 653 | root_rp = svm_get_root_rp (); |
| 654 | ASSERT (root_rp); |
| 655 | /* Clean up the root region client list */ |
| 656 | pthread_mutex_lock (&root_rp->mutex); |
| 657 | svm_client_scan_this_region_nolock (root_rp); |
| 658 | pthread_mutex_unlock (&root_rp->mutex); |
| 659 | } |
| 660 | else |
| 661 | { |
| 662 | pthread_mutex_unlock (&vlib_rp->mutex); |
| 663 | } |
| 664 | am->vlib_rp = vlib_rp; |
| 665 | vec_add1 (am->mapped_shmem_regions, vlib_rp); |
| 666 | return 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 667 | } |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 668 | /* Clients simply have to wait... */ |
| 669 | if (!is_vlib) |
| 670 | { |
| 671 | pthread_mutex_unlock (&vlib_rp->mutex); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 672 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 673 | /* Wait up to 100 seconds... */ |
| 674 | for (i = 0; i < 10000; i++) |
| 675 | { |
| 676 | ts.tv_sec = 0; |
| 677 | ts.tv_nsec = 10000 * 1000; /* 10 ms */ |
| 678 | while (nanosleep (&ts, &tsrem) < 0) |
| 679 | ts = tsrem; |
| 680 | if (vlib_rp->user_ctx) |
| 681 | goto ready; |
| 682 | } |
| 683 | /* Clean up and leave... */ |
| 684 | svm_region_unmap (vlib_rp); |
| 685 | clib_warning ("region init fail"); |
| 686 | return (-2); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 687 | |
| 688 | ready: |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 689 | am->shmem_hdr = (void *) vlib_rp->user_ctx; |
| 690 | am->our_pid = getpid (); |
| 691 | am->vlib_rp = vlib_rp; |
| 692 | vec_add1 (am->mapped_shmem_regions, vlib_rp); |
| 693 | return 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 694 | } |
| 695 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 696 | /* Nope, it's our problem... */ |
Florin Coras | 90a6398 | 2017-12-19 04:50:01 -0800 | [diff] [blame] | 697 | vl_init_shmem (vlib_rp, 0 /* default config */ , 1 /* is vlib */ , |
| 698 | 0 /* is_private_region */ ); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 699 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 700 | vec_add1 (am->mapped_shmem_regions, vlib_rp); |
| 701 | return 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 702 | } |
| 703 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 704 | void |
| 705 | vl_register_mapped_shmem_region (svm_region_t * rp) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 706 | { |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 707 | api_main_t *am = &api_main; |
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 | vec_add1 (am->mapped_shmem_regions, rp); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 710 | } |
| 711 | |
Florin Coras | d6c30d9 | 2018-01-29 05:11:24 -0800 | [diff] [blame] | 712 | static void |
| 713 | vl_unmap_shmem_internal (u8 is_client) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 714 | { |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 715 | svm_region_t *rp; |
| 716 | int i; |
| 717 | api_main_t *am = &api_main; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 718 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 719 | if (!svm_get_root_rp ()) |
| 720 | return; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 721 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 722 | for (i = 0; i < vec_len (am->mapped_shmem_regions); i++) |
| 723 | { |
| 724 | rp = am->mapped_shmem_regions[i]; |
Florin Coras | d6c30d9 | 2018-01-29 05:11:24 -0800 | [diff] [blame] | 725 | is_client ? svm_region_unmap_client (rp) : svm_region_unmap (rp); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 726 | } |
| 727 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 728 | vec_free (am->mapped_shmem_regions); |
| 729 | am->shmem_hdr = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 730 | |
Florin Coras | d6c30d9 | 2018-01-29 05:11:24 -0800 | [diff] [blame] | 731 | is_client ? svm_region_exit_client () : svm_region_exit (); |
| 732 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 733 | /* $$$ more careful cleanup, valgrind run... */ |
| 734 | vec_free (am->msg_handlers); |
| 735 | vec_free (am->msg_endian_handlers); |
| 736 | vec_free (am->msg_print_handlers); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 737 | } |
| 738 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 739 | void |
Florin Coras | d6c30d9 | 2018-01-29 05:11:24 -0800 | [diff] [blame] | 740 | vl_unmap_shmem (void) |
| 741 | { |
| 742 | vl_unmap_shmem_internal (0); |
| 743 | } |
| 744 | |
| 745 | void |
| 746 | vl_unmap_shmem_client (void) |
| 747 | { |
| 748 | vl_unmap_shmem_internal (1); |
| 749 | } |
| 750 | |
| 751 | void |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 752 | vl_msg_api_send_shmem (svm_queue_t * q, u8 * elem) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 753 | { |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 754 | api_main_t *am = &api_main; |
| 755 | uword *trace = (uword *) elem; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 756 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 757 | if (am->tx_trace && am->tx_trace->enabled) |
| 758 | vl_msg_api_trace (am, am->tx_trace, (void *) trace[0]); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 759 | |
Dave Barach | 0c4fec0 | 2018-11-18 12:03:48 -0500 | [diff] [blame] | 760 | /* |
| 761 | * Announce a probable binary API client bug: |
| 762 | * some client's input queue is stuffed. |
| 763 | * The situation may be recoverable, or not. |
| 764 | */ |
| 765 | if (PREDICT_FALSE |
| 766 | (am->vl_clients /* vpp side */ && (q->cursize == q->maxsize))) |
| 767 | clib_warning ("WARNING: client input queue at %llx is stuffed...", q); |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 768 | (void) svm_queue_add (q, elem, 0 /* nowait */ ); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 769 | } |
| 770 | |
Florin Coras | af0ff5a | 2018-01-10 08:17:03 -0800 | [diff] [blame] | 771 | int |
| 772 | vl_mem_api_can_send (svm_queue_t * q) |
| 773 | { |
| 774 | return (q->cursize < q->maxsize); |
| 775 | } |
| 776 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 777 | void |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 778 | vl_msg_api_send_shmem_nolock (svm_queue_t * q, u8 * elem) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 779 | { |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 780 | api_main_t *am = &api_main; |
| 781 | uword *trace = (uword *) elem; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 782 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 783 | if (am->tx_trace && am->tx_trace->enabled) |
| 784 | vl_msg_api_trace (am, am->tx_trace, (void *) trace[0]); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 785 | |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 786 | (void) svm_queue_add_nolock (q, elem); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 787 | } |
| 788 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 789 | /* |
| 790 | * fd.io coding-style-patch-verification: ON |
| 791 | * |
| 792 | * Local Variables: |
| 793 | * eval: (c-set-style "gnu") |
| 794 | * End: |
| 795 | */ |