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