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