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