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