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