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