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) |
| 107 | clib_warning ("garbage collect pool %d ring %d index %d", |
| 108 | pool, i, q->head); |
| 109 | shmem_hdr->garbage_collects++; |
| 110 | goto collected; |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 115 | /* yes, loser; try next larger pool */ |
| 116 | ap[i].misses++; |
| 117 | if (pool == 0) |
| 118 | pthread_mutex_unlock (&q->mutex); |
| 119 | continue; |
| 120 | } |
Dave Barach | 842b9c5 | 2017-01-09 15:54:00 -0500 | [diff] [blame] | 121 | collected: |
| 122 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 123 | /* OK, we have a winner */ |
| 124 | ap[i].hits++; |
| 125 | /* |
| 126 | * Remember the source queue, although we |
| 127 | * don't need to know the queue to free the item. |
| 128 | */ |
| 129 | rv->q = q; |
Dave Barach | 842b9c5 | 2017-01-09 15:54:00 -0500 | [diff] [blame] | 130 | rv->gc_mark_timestamp = 0; |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 131 | q->head++; |
| 132 | if (q->head == q->maxsize) |
| 133 | q->head = 0; |
| 134 | |
| 135 | if (pool == 0) |
| 136 | pthread_mutex_unlock (&q->mutex); |
| 137 | goto out; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 138 | } |
| 139 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 140 | /* |
| 141 | * Request too big, or head element of all size-compatible rings |
| 142 | * still in use. Fall back to shared-memory malloc. |
| 143 | */ |
| 144 | am->ring_misses++; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 145 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 146 | pthread_mutex_lock (&am->vlib_rp->mutex); |
| 147 | oldheap = svm_push_data_heap (am->vlib_rp); |
Dave Barach | 7737833 | 2016-10-13 17:35:09 -0400 | [diff] [blame] | 148 | if (may_return_null) |
| 149 | { |
| 150 | rv = clib_mem_alloc_or_null (nbytes); |
| 151 | if (PREDICT_FALSE (rv == 0)) |
| 152 | { |
| 153 | svm_pop_heap (oldheap); |
| 154 | pthread_mutex_unlock (&am->vlib_rp->mutex); |
| 155 | return 0; |
| 156 | } |
| 157 | } |
| 158 | else |
| 159 | rv = clib_mem_alloc (nbytes); |
| 160 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 161 | rv->q = 0; |
| 162 | svm_pop_heap (oldheap); |
| 163 | pthread_mutex_unlock (&am->vlib_rp->mutex); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 164 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 165 | out: |
| 166 | rv->data_len = htonl (nbytes - sizeof (msgbuf_t)); |
| 167 | return (rv->data); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 168 | } |
| 169 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 170 | void * |
| 171 | vl_msg_api_alloc (int nbytes) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 172 | { |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 173 | int pool; |
| 174 | api_main_t *am = &api_main; |
| 175 | vl_shmem_hdr_t *shmem_hdr = am->shmem_hdr; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 176 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 177 | /* |
| 178 | * Clients use pool-0, vlib proc uses pool 1 |
| 179 | */ |
| 180 | pool = (am->our_pid == shmem_hdr->vl_pid); |
Dave Barach | 7737833 | 2016-10-13 17:35:09 -0400 | [diff] [blame] | 181 | return vl_msg_api_alloc_internal (nbytes, pool, 0 /* may_return_null */ ); |
| 182 | } |
| 183 | |
| 184 | void * |
| 185 | vl_msg_api_alloc_or_null (int nbytes) |
| 186 | { |
| 187 | int pool; |
| 188 | api_main_t *am = &api_main; |
| 189 | vl_shmem_hdr_t *shmem_hdr = am->shmem_hdr; |
| 190 | |
| 191 | pool = (am->our_pid == shmem_hdr->vl_pid); |
| 192 | return vl_msg_api_alloc_internal (nbytes, pool, 1 /* may_return_null */ ); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 193 | } |
| 194 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 195 | void * |
| 196 | vl_msg_api_alloc_as_if_client (int nbytes) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 197 | { |
Dave Barach | 7737833 | 2016-10-13 17:35:09 -0400 | [diff] [blame] | 198 | return vl_msg_api_alloc_internal (nbytes, 0, 0 /* may_return_null */ ); |
| 199 | } |
| 200 | |
| 201 | void * |
| 202 | vl_msg_api_alloc_as_if_client_or_null (int nbytes) |
| 203 | { |
| 204 | return vl_msg_api_alloc_internal (nbytes, 0, 1 /* may_return_null */ ); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 205 | } |
| 206 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 207 | void |
| 208 | vl_msg_api_free (void *a) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 209 | { |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 210 | msgbuf_t *rv; |
| 211 | void *oldheap; |
| 212 | api_main_t *am = &api_main; |
Ole Troan | 6855f6c | 2016-04-09 03:16:30 +0200 | [diff] [blame] | 213 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 214 | rv = (msgbuf_t *) (((u8 *) a) - offsetof (msgbuf_t, data)); |
| 215 | |
| 216 | /* |
| 217 | * Here's the beauty of the scheme. Only one proc/thread has |
| 218 | * control of a given message buffer. To free a buffer, we just clear the |
| 219 | * queue field, and leave. No locks, no hits, no errors... |
| 220 | */ |
| 221 | if (rv->q) |
| 222 | { |
| 223 | rv->q = 0; |
Dave Barach | 842b9c5 | 2017-01-09 15:54:00 -0500 | [diff] [blame] | 224 | rv->gc_mark_timestamp = 0; |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 225 | return; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 226 | } |
| 227 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 228 | pthread_mutex_lock (&am->vlib_rp->mutex); |
| 229 | oldheap = svm_push_data_heap (am->vlib_rp); |
| 230 | clib_mem_free (rv); |
| 231 | svm_pop_heap (oldheap); |
| 232 | pthread_mutex_unlock (&am->vlib_rp->mutex); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 233 | } |
| 234 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 235 | static void |
| 236 | vl_msg_api_free_nolock (void *a) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 237 | { |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 238 | msgbuf_t *rv; |
| 239 | void *oldheap; |
| 240 | api_main_t *am = &api_main; |
| 241 | |
| 242 | rv = (msgbuf_t *) (((u8 *) a) - offsetof (msgbuf_t, data)); |
| 243 | /* |
| 244 | * Here's the beauty of the scheme. Only one proc/thread has |
| 245 | * control of a given message buffer. To free a buffer, we just clear the |
| 246 | * queue field, and leave. No locks, no hits, no errors... |
| 247 | */ |
| 248 | if (rv->q) |
| 249 | { |
| 250 | rv->q = 0; |
| 251 | return; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 252 | } |
| 253 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 254 | oldheap = svm_push_data_heap (am->vlib_rp); |
| 255 | clib_mem_free (rv); |
| 256 | svm_pop_heap (oldheap); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 257 | } |
| 258 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 259 | void |
| 260 | vl_set_memory_root_path (char *name) |
Dave Barach | 309bef2 | 2016-01-22 16:09:52 -0500 | [diff] [blame] | 261 | { |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 262 | api_main_t *am = &api_main; |
Dave Barach | 309bef2 | 2016-01-22 16:09:52 -0500 | [diff] [blame] | 263 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 264 | am->root_path = name; |
Dave Barach | 309bef2 | 2016-01-22 16:09:52 -0500 | [diff] [blame] | 265 | } |
| 266 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 267 | void |
| 268 | vl_set_memory_uid (int uid) |
Dave Barach | 16c75df | 2016-05-31 14:05:46 -0400 | [diff] [blame] | 269 | { |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 270 | api_main_t *am = &api_main; |
Dave Barach | 16c75df | 2016-05-31 14:05:46 -0400 | [diff] [blame] | 271 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 272 | am->api_uid = uid; |
Dave Barach | 16c75df | 2016-05-31 14:05:46 -0400 | [diff] [blame] | 273 | } |
| 274 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 275 | void |
| 276 | vl_set_memory_gid (int gid) |
Dave Barach | 16c75df | 2016-05-31 14:05:46 -0400 | [diff] [blame] | 277 | { |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 278 | api_main_t *am = &api_main; |
Dave Barach | 16c75df | 2016-05-31 14:05:46 -0400 | [diff] [blame] | 279 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 280 | am->api_gid = gid; |
Dave Barach | 16c75df | 2016-05-31 14:05:46 -0400 | [diff] [blame] | 281 | } |
| 282 | |
Dave Barach | b3d93da | 2016-08-03 14:34:38 -0400 | [diff] [blame] | 283 | void |
| 284 | vl_set_global_memory_baseva (u64 baseva) |
| 285 | { |
| 286 | api_main_t *am = &api_main; |
| 287 | |
| 288 | am->global_baseva = baseva; |
| 289 | } |
| 290 | |
| 291 | void |
| 292 | vl_set_global_memory_size (u64 size) |
| 293 | { |
| 294 | api_main_t *am = &api_main; |
| 295 | |
| 296 | am->global_size = size; |
| 297 | } |
| 298 | |
| 299 | void |
| 300 | vl_set_api_memory_size (u64 size) |
| 301 | { |
| 302 | api_main_t *am = &api_main; |
| 303 | |
| 304 | am->api_size = size; |
| 305 | } |
| 306 | |
| 307 | void |
| 308 | vl_set_global_pvt_heap_size (u64 size) |
| 309 | { |
| 310 | api_main_t *am = &api_main; |
| 311 | |
| 312 | am->global_pvt_heap_size = size; |
| 313 | } |
| 314 | |
| 315 | void |
| 316 | vl_set_api_pvt_heap_size (u64 size) |
| 317 | { |
| 318 | api_main_t *am = &api_main; |
| 319 | |
| 320 | am->api_pvt_heap_size = size; |
| 321 | } |
| 322 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 323 | int |
| 324 | vl_map_shmem (char *region_name, int is_vlib) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 325 | { |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 326 | svm_map_region_args_t _a, *a = &_a; |
| 327 | svm_region_t *vlib_rp, *root_rp; |
| 328 | void *oldheap; |
| 329 | vl_shmem_hdr_t *shmem_hdr = 0; |
| 330 | api_main_t *am = &api_main; |
| 331 | int i; |
| 332 | struct timespec ts, tsrem; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 333 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 334 | if (is_vlib == 0) |
| 335 | svm_region_init_chroot (am->root_path); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 336 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 337 | memset (a, 0, sizeof (*a)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 338 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 339 | a->name = region_name; |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 340 | a->size = am->api_size ? am->api_size : (16 << 20); |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 341 | a->flags = SVM_FLAGS_MHEAP; |
| 342 | a->uid = am->api_uid; |
| 343 | a->gid = am->api_gid; |
Dave Barach | b3d93da | 2016-08-03 14:34:38 -0400 | [diff] [blame] | 344 | a->pvt_heap_size = am->api_pvt_heap_size; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 345 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 346 | vlib_rp = svm_region_find_or_create (a); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 347 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 348 | if (vlib_rp == 0) |
| 349 | return (-2); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 350 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 351 | pthread_mutex_lock (&vlib_rp->mutex); |
| 352 | /* Has someone else set up the shared-memory variable table? */ |
| 353 | if (vlib_rp->user_ctx) |
| 354 | { |
| 355 | am->shmem_hdr = (void *) vlib_rp->user_ctx; |
| 356 | am->our_pid = getpid (); |
| 357 | if (is_vlib) |
| 358 | { |
| 359 | unix_shared_memory_queue_t *q; |
| 360 | uword old_msg; |
| 361 | /* |
| 362 | * application restart. Reset cached pids, API message |
| 363 | * rings, list of clients; otherwise, various things |
| 364 | * fail. (e.g. queue non-empty notification) |
| 365 | */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 366 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 367 | /* ghosts keep the region from disappearing properly */ |
| 368 | svm_client_scan_this_region_nolock (vlib_rp); |
| 369 | am->shmem_hdr->application_restarts++; |
| 370 | q = am->shmem_hdr->vl_input_queue; |
| 371 | am->shmem_hdr->vl_pid = getpid (); |
| 372 | q->consumer_pid = am->shmem_hdr->vl_pid; |
| 373 | /* Drain the input queue, freeing msgs */ |
| 374 | for (i = 0; i < 10; i++) |
| 375 | { |
| 376 | if (pthread_mutex_trylock (&q->mutex) == 0) |
| 377 | { |
| 378 | pthread_mutex_unlock (&q->mutex); |
| 379 | goto mutex_ok; |
| 380 | } |
| 381 | ts.tv_sec = 0; |
| 382 | ts.tv_nsec = 10000 * 1000; /* 10 ms */ |
| 383 | while (nanosleep (&ts, &tsrem) < 0) |
| 384 | ts = tsrem; |
| 385 | } |
| 386 | /* Mutex buggered, "fix" it */ |
| 387 | memset (&q->mutex, 0, sizeof (q->mutex)); |
| 388 | clib_warning ("forcibly release main input queue mutex"); |
| 389 | |
| 390 | mutex_ok: |
| 391 | am->vlib_rp = vlib_rp; |
| 392 | while (unix_shared_memory_queue_sub (q, |
| 393 | (u8 *) & old_msg, |
| 394 | 1 /* nowait */ ) |
| 395 | != -2 /* queue underflow */ ) |
| 396 | { |
| 397 | vl_msg_api_free_nolock ((void *) old_msg); |
| 398 | am->shmem_hdr->restart_reclaims++; |
| 399 | } |
| 400 | pthread_mutex_unlock (&vlib_rp->mutex); |
| 401 | root_rp = svm_get_root_rp (); |
| 402 | ASSERT (root_rp); |
| 403 | /* Clean up the root region client list */ |
| 404 | pthread_mutex_lock (&root_rp->mutex); |
| 405 | svm_client_scan_this_region_nolock (root_rp); |
| 406 | pthread_mutex_unlock (&root_rp->mutex); |
| 407 | } |
| 408 | else |
| 409 | { |
| 410 | pthread_mutex_unlock (&vlib_rp->mutex); |
| 411 | } |
| 412 | am->vlib_rp = vlib_rp; |
| 413 | vec_add1 (am->mapped_shmem_regions, vlib_rp); |
| 414 | return 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 415 | } |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 416 | /* Clients simply have to wait... */ |
| 417 | if (!is_vlib) |
| 418 | { |
| 419 | pthread_mutex_unlock (&vlib_rp->mutex); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 420 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 421 | /* Wait up to 100 seconds... */ |
| 422 | for (i = 0; i < 10000; i++) |
| 423 | { |
| 424 | ts.tv_sec = 0; |
| 425 | ts.tv_nsec = 10000 * 1000; /* 10 ms */ |
| 426 | while (nanosleep (&ts, &tsrem) < 0) |
| 427 | ts = tsrem; |
| 428 | if (vlib_rp->user_ctx) |
| 429 | goto ready; |
| 430 | } |
| 431 | /* Clean up and leave... */ |
| 432 | svm_region_unmap (vlib_rp); |
| 433 | clib_warning ("region init fail"); |
| 434 | return (-2); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 435 | |
| 436 | ready: |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 437 | am->shmem_hdr = (void *) vlib_rp->user_ctx; |
| 438 | am->our_pid = getpid (); |
| 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 | } |
| 443 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 444 | /* Nope, it's our problem... */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 445 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 446 | oldheap = svm_push_data_heap (vlib_rp); |
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 | vec_validate (shmem_hdr, 0); |
| 449 | shmem_hdr->version = VL_SHM_VERSION; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 450 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 451 | /* vlib main input queue */ |
| 452 | shmem_hdr->vl_input_queue = |
| 453 | unix_shared_memory_queue_init (1024, sizeof (uword), getpid (), |
| 454 | am->vlib_signal); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 455 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 456 | /* Set up the msg ring allocator */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 457 | #define _(sz,n) \ |
| 458 | do { \ |
| 459 | ring_alloc_t _rp; \ |
| 460 | _rp.rp = unix_shared_memory_queue_init ((n), (sz), 0, 0); \ |
| 461 | _rp.size = (sz); \ |
| 462 | _rp.nitems = n; \ |
| 463 | _rp.hits = 0; \ |
| 464 | _rp.misses = 0; \ |
| 465 | vec_add1(shmem_hdr->vl_rings, _rp); \ |
| 466 | } while (0); |
| 467 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 468 | foreach_vl_aring_size; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 469 | #undef _ |
| 470 | |
| 471 | #define _(sz,n) \ |
| 472 | do { \ |
| 473 | ring_alloc_t _rp; \ |
| 474 | _rp.rp = unix_shared_memory_queue_init ((n), (sz), 0, 0); \ |
| 475 | _rp.size = (sz); \ |
| 476 | _rp.nitems = n; \ |
| 477 | _rp.hits = 0; \ |
| 478 | _rp.misses = 0; \ |
| 479 | vec_add1(shmem_hdr->client_rings, _rp); \ |
| 480 | } while (0); |
| 481 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 482 | foreach_clnt_aring_size; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 483 | #undef _ |
| 484 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 485 | am->shmem_hdr = shmem_hdr; |
| 486 | am->vlib_rp = vlib_rp; |
| 487 | am->our_pid = getpid (); |
| 488 | if (is_vlib) |
| 489 | am->shmem_hdr->vl_pid = am->our_pid; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 490 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 491 | svm_pop_heap (oldheap); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 492 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 493 | /* |
| 494 | * After absolutely everything that a client might see is set up, |
| 495 | * declare the shmem region valid |
| 496 | */ |
| 497 | vlib_rp->user_ctx = shmem_hdr; |
| 498 | |
| 499 | pthread_mutex_unlock (&vlib_rp->mutex); |
| 500 | vec_add1 (am->mapped_shmem_regions, vlib_rp); |
| 501 | return 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 502 | } |
| 503 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 504 | void |
| 505 | vl_register_mapped_shmem_region (svm_region_t * rp) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 506 | { |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 507 | api_main_t *am = &api_main; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 508 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 509 | vec_add1 (am->mapped_shmem_regions, rp); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 510 | } |
| 511 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 512 | void |
| 513 | vl_unmap_shmem (void) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 514 | { |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 515 | svm_region_t *rp; |
| 516 | int i; |
| 517 | api_main_t *am = &api_main; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 518 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 519 | if (!svm_get_root_rp ()) |
| 520 | return; |
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 | for (i = 0; i < vec_len (am->mapped_shmem_regions); i++) |
| 523 | { |
| 524 | rp = am->mapped_shmem_regions[i]; |
| 525 | svm_region_unmap (rp); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 526 | } |
| 527 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 528 | vec_free (am->mapped_shmem_regions); |
| 529 | am->shmem_hdr = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 530 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 531 | svm_region_exit (); |
| 532 | /* $$$ more careful cleanup, valgrind run... */ |
| 533 | vec_free (am->msg_handlers); |
| 534 | vec_free (am->msg_endian_handlers); |
| 535 | vec_free (am->msg_print_handlers); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 536 | } |
| 537 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 538 | void |
| 539 | 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] | 540 | { |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 541 | api_main_t *am = &api_main; |
| 542 | uword *trace = (uword *) elem; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 543 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 544 | if (am->tx_trace && am->tx_trace->enabled) |
| 545 | vl_msg_api_trace (am, am->tx_trace, (void *) trace[0]); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 546 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 547 | (void) unix_shared_memory_queue_add (q, elem, 0 /* nowait */ ); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 548 | } |
| 549 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 550 | void |
| 551 | 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] | 552 | { |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 553 | api_main_t *am = &api_main; |
| 554 | uword *trace = (uword *) elem; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 555 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 556 | if (am->tx_trace && am->tx_trace->enabled) |
| 557 | vl_msg_api_trace (am, am->tx_trace, (void *) trace[0]); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 558 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 559 | (void) unix_shared_memory_queue_add_nolock (q, elem); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 560 | } |
| 561 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 562 | static void |
| 563 | vl_api_memclnt_create_reply_t_handler (vl_api_memclnt_create_reply_t * mp) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 564 | { |
Dave Barach | 557d128 | 2016-11-10 14:22:49 -0500 | [diff] [blame] | 565 | serialize_main_t _sm, *sm = &_sm; |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 566 | api_main_t *am = &api_main; |
Dave Barach | 557d128 | 2016-11-10 14:22:49 -0500 | [diff] [blame] | 567 | u8 *tblv; |
| 568 | u32 nmsgs; |
| 569 | int i; |
| 570 | u8 *name_and_crc; |
| 571 | u32 msg_index; |
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 | am->my_client_index = mp->index; |
| 574 | am->my_registration = (vl_api_registration_t *) (uword) mp->handle; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 575 | |
Dave Barach | 557d128 | 2016-11-10 14:22:49 -0500 | [diff] [blame] | 576 | /* Clean out any previous hash table (unlikely) */ |
| 577 | if (am->msg_index_by_name_and_crc) |
| 578 | { |
| 579 | int i; |
| 580 | u8 **keys = 0; |
| 581 | hash_pair_t *hp; |
| 582 | /* *INDENT-OFF* */ |
| 583 | hash_foreach_pair (hp, am->msg_index_by_name_and_crc, |
| 584 | ({ |
| 585 | vec_add1 (keys, (u8 *) hp->key); |
| 586 | })); |
| 587 | /* *INDENT-ON* */ |
| 588 | for (i = 0; i < vec_len (keys); i++) |
| 589 | vec_free (keys[i]); |
| 590 | vec_free (keys); |
| 591 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 592 | |
Dave Barach | 557d128 | 2016-11-10 14:22:49 -0500 | [diff] [blame] | 593 | am->msg_index_by_name_and_crc = hash_create_string (0, sizeof (uword)); |
| 594 | |
| 595 | /* Recreate the vnet-side API message handler table */ |
| 596 | tblv = (u8 *) mp->message_table; |
| 597 | serialize_open_vector (sm, tblv); |
| 598 | unserialize_integer (sm, &nmsgs, sizeof (u32)); |
| 599 | |
| 600 | for (i = 0; i < nmsgs; i++) |
| 601 | { |
| 602 | msg_index = unserialize_likely_small_unsigned_integer (sm); |
| 603 | unserialize_cstring (sm, (char **) &name_and_crc); |
| 604 | hash_set_mem (am->msg_index_by_name_and_crc, name_and_crc, msg_index); |
| 605 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 606 | } |
| 607 | |
Dave Barach | 557d128 | 2016-11-10 14:22:49 -0500 | [diff] [blame] | 608 | u32 |
| 609 | vl_api_get_msg_index (u8 * name_and_crc) |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 610 | { |
Dave Barach | 557d128 | 2016-11-10 14:22:49 -0500 | [diff] [blame] | 611 | api_main_t *am = &api_main; |
| 612 | uword *p; |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 613 | |
Dave Barach | 557d128 | 2016-11-10 14:22:49 -0500 | [diff] [blame] | 614 | if (am->msg_index_by_name_and_crc) |
| 615 | { |
| 616 | p = hash_get_mem (am->msg_index_by_name_and_crc, name_and_crc); |
| 617 | if (p) |
| 618 | return p[0]; |
| 619 | } |
| 620 | return ~0; |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 621 | } |
| 622 | |
| 623 | int |
| 624 | vl_client_connect (char *name, int ctx_quota, int input_queue_size) |
| 625 | { |
| 626 | svm_region_t *svm; |
| 627 | vl_api_memclnt_create_t *mp; |
| 628 | vl_api_memclnt_create_reply_t *rp; |
| 629 | unix_shared_memory_queue_t *vl_input_queue; |
| 630 | vl_shmem_hdr_t *shmem_hdr; |
| 631 | int rv = 0; |
| 632 | void *oldheap; |
| 633 | api_main_t *am = &api_main; |
| 634 | |
| 635 | if (am->my_registration) |
| 636 | { |
| 637 | clib_warning ("client %s already connected...", name); |
| 638 | return -1; |
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 | if (am->vlib_rp == 0) |
| 642 | { |
| 643 | clib_warning ("am->vlib_rp NULL"); |
| 644 | return -1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 645 | } |
| 646 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 647 | svm = am->vlib_rp; |
| 648 | shmem_hdr = am->shmem_hdr; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 649 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 650 | if (shmem_hdr == 0 || shmem_hdr->vl_input_queue == 0) |
| 651 | { |
| 652 | clib_warning ("shmem_hdr / input queue NULL"); |
| 653 | return -1; |
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 | pthread_mutex_lock (&svm->mutex); |
| 657 | oldheap = svm_push_data_heap (svm); |
| 658 | vl_input_queue = |
| 659 | unix_shared_memory_queue_init (input_queue_size, sizeof (uword), |
| 660 | getpid (), 0); |
| 661 | pthread_mutex_unlock (&svm->mutex); |
| 662 | svm_pop_heap (oldheap); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 663 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 664 | am->my_client_index = ~0; |
| 665 | am->my_registration = 0; |
| 666 | am->vl_input_queue = vl_input_queue; |
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 | mp = vl_msg_api_alloc (sizeof (vl_api_memclnt_create_t)); |
| 669 | memset (mp, 0, sizeof (*mp)); |
| 670 | mp->_vl_msg_id = ntohs (VL_API_MEMCLNT_CREATE); |
| 671 | mp->ctx_quota = ctx_quota; |
| 672 | mp->input_queue = (uword) vl_input_queue; |
| 673 | strncpy ((char *) mp->name, name, sizeof (mp->name) - 1); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 674 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 675 | vl_msg_api_send_shmem (shmem_hdr->vl_input_queue, (u8 *) & mp); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 676 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 677 | while (1) |
| 678 | { |
| 679 | int qstatus; |
| 680 | struct timespec ts, tsrem; |
| 681 | int i; |
| 682 | |
| 683 | /* Wait up to 10 seconds */ |
| 684 | for (i = 0; i < 1000; i++) |
| 685 | { |
| 686 | qstatus = unix_shared_memory_queue_sub (vl_input_queue, (u8 *) & rp, |
| 687 | 1 /* nowait */ ); |
| 688 | if (qstatus == 0) |
| 689 | goto read_one_msg; |
| 690 | ts.tv_sec = 0; |
| 691 | ts.tv_nsec = 10000 * 1000; /* 10 ms */ |
| 692 | while (nanosleep (&ts, &tsrem) < 0) |
| 693 | ts = tsrem; |
| 694 | } |
| 695 | /* Timeout... */ |
| 696 | clib_warning ("memclnt_create_reply timeout"); |
| 697 | return -1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 698 | |
| 699 | read_one_msg: |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 700 | if (ntohs (rp->_vl_msg_id) != VL_API_MEMCLNT_CREATE_REPLY) |
| 701 | { |
| 702 | clib_warning ("unexpected reply: id %d", ntohs (rp->_vl_msg_id)); |
| 703 | continue; |
| 704 | } |
| 705 | rv = clib_net_to_host_u32 (rp->response); |
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 | vl_msg_api_handler ((void *) rp); |
| 708 | break; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 709 | } |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 710 | return (rv); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 711 | } |
| 712 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 713 | static void |
| 714 | vl_api_memclnt_delete_reply_t_handler (vl_api_memclnt_delete_reply_t * mp) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 715 | { |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 716 | void *oldheap; |
| 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 | pthread_mutex_lock (&am->vlib_rp->mutex); |
| 720 | oldheap = svm_push_data_heap (am->vlib_rp); |
| 721 | unix_shared_memory_queue_free (am->vl_input_queue); |
| 722 | pthread_mutex_unlock (&am->vlib_rp->mutex); |
| 723 | svm_pop_heap (oldheap); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 724 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 725 | am->my_client_index = ~0; |
| 726 | am->my_registration = 0; |
| 727 | am->vl_input_queue = 0; |
| 728 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 729 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 730 | void |
| 731 | vl_client_disconnect (void) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 732 | { |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 733 | vl_api_memclnt_delete_t *mp; |
| 734 | vl_api_memclnt_delete_reply_t *rp; |
| 735 | unix_shared_memory_queue_t *vl_input_queue; |
| 736 | vl_shmem_hdr_t *shmem_hdr; |
| 737 | time_t begin; |
| 738 | api_main_t *am = &api_main; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 739 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 740 | ASSERT (am->vlib_rp); |
| 741 | shmem_hdr = am->shmem_hdr; |
| 742 | ASSERT (shmem_hdr && shmem_hdr->vl_input_queue); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 743 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 744 | vl_input_queue = am->vl_input_queue; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 745 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 746 | mp = vl_msg_api_alloc (sizeof (vl_api_memclnt_delete_t)); |
| 747 | memset (mp, 0, sizeof (*mp)); |
| 748 | mp->_vl_msg_id = ntohs (VL_API_MEMCLNT_DELETE); |
| 749 | mp->index = am->my_client_index; |
| 750 | mp->handle = (uword) am->my_registration; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 751 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 752 | vl_msg_api_send_shmem (shmem_hdr->vl_input_queue, (u8 *) & mp); |
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 | /* |
| 755 | * Have to be careful here, in case the client is disconnecting |
| 756 | * because e.g. the vlib process died, or is unresponsive. |
| 757 | */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 758 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 759 | begin = time (0); |
| 760 | while (1) |
| 761 | { |
| 762 | time_t now; |
| 763 | |
| 764 | now = time (0); |
| 765 | |
| 766 | if (now >= (begin + 2)) |
| 767 | { |
| 768 | clib_warning ("peer unresponsive, give up"); |
| 769 | am->my_client_index = ~0; |
| 770 | am->my_registration = 0; |
| 771 | am->shmem_hdr = 0; |
| 772 | break; |
| 773 | } |
| 774 | if (unix_shared_memory_queue_sub (vl_input_queue, (u8 *) & rp, 1) < 0) |
| 775 | continue; |
| 776 | |
| 777 | /* drain the queue */ |
| 778 | if (ntohs (rp->_vl_msg_id) != VL_API_MEMCLNT_DELETE_REPLY) |
| 779 | { |
| 780 | vl_msg_api_handler ((void *) rp); |
| 781 | continue; |
| 782 | } |
| 783 | vl_msg_api_handler ((void *) rp); |
| 784 | break; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 785 | } |
| 786 | } |
| 787 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 788 | static inline vl_api_registration_t * |
| 789 | vl_api_client_index_to_registration_internal (u32 handle) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 790 | { |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 791 | vl_api_registration_t **regpp; |
| 792 | vl_api_registration_t *regp; |
| 793 | api_main_t *am = &api_main; |
| 794 | u32 index; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 795 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 796 | index = vl_msg_api_handle_get_index (handle); |
| 797 | if ((am->shmem_hdr->application_restarts & VL_API_EPOCH_MASK) |
| 798 | != vl_msg_api_handle_get_epoch (handle)) |
| 799 | { |
| 800 | vl_msg_api_increment_missing_client_counter (); |
| 801 | return 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 802 | } |
| 803 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 804 | regpp = am->vl_clients + index; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 805 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 806 | if (pool_is_free (am->vl_clients, regpp)) |
| 807 | { |
| 808 | vl_msg_api_increment_missing_client_counter (); |
| 809 | return 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 810 | } |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 811 | regp = *regpp; |
| 812 | return (regp); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 813 | } |
| 814 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 815 | vl_api_registration_t * |
| 816 | vl_api_client_index_to_registration (u32 index) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 817 | { |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 818 | return (vl_api_client_index_to_registration_internal (index)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 819 | } |
| 820 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 821 | unix_shared_memory_queue_t * |
| 822 | vl_api_client_index_to_input_queue (u32 index) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 823 | { |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 824 | vl_api_registration_t *regp; |
Dave Barach | fc262a0 | 2016-12-14 14:49:55 -0500 | [diff] [blame] | 825 | api_main_t *am = &api_main; |
| 826 | |
| 827 | /* Special case: vlib trying to send itself a message */ |
| 828 | if (index == (u32) ~ 0) |
| 829 | return (am->shmem_hdr->vl_input_queue); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 830 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 831 | regp = vl_api_client_index_to_registration_internal (index); |
| 832 | if (!regp) |
| 833 | return 0; |
| 834 | return (regp->vl_input_queue); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 835 | } |
| 836 | |
| 837 | #define foreach_api_client_msg \ |
| 838 | _(MEMCLNT_CREATE_REPLY, memclnt_create_reply) \ |
| 839 | _(MEMCLNT_DELETE_REPLY, memclnt_delete_reply) |
| 840 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 841 | int |
| 842 | vl_client_api_map (char *region_name) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 843 | { |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 844 | int rv; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 845 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 846 | if ((rv = vl_map_shmem (region_name, 0 /* is_vlib */ )) < 0) |
| 847 | { |
| 848 | return rv; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 849 | } |
| 850 | |
| 851 | #define _(N,n) \ |
| 852 | vl_msg_api_set_handlers(VL_API_##N, 0 /* name */, \ |
| 853 | vl_api_##n##_t_handler, \ |
| 854 | 0/* cleanup */, 0/* endian */, 0/* print */, \ |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 855 | sizeof(vl_api_##n##_t), 1); |
| 856 | foreach_api_client_msg; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 857 | #undef _ |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 858 | return 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 859 | } |
| 860 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 861 | void |
| 862 | vl_client_api_unmap (void) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 863 | { |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 864 | vl_unmap_shmem (); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 865 | } |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 866 | |
| 867 | /* |
| 868 | * fd.io coding-style-patch-verification: ON |
| 869 | * |
| 870 | * Local Variables: |
| 871 | * eval: (c-set-style "gnu") |
| 872 | * End: |
| 873 | */ |