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