Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 1 | /* |
| 2 | *------------------------------------------------------------------ |
| 3 | * Copyright (c) 2018 Cisco and/or its affiliates. |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at: |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | *------------------------------------------------------------------ |
| 16 | */ |
| 17 | #include <signal.h> |
| 18 | |
| 19 | #include <vlib/vlib.h> |
| 20 | #include <vlibapi/api.h> |
| 21 | #include <vlibmemory/api.h> |
| 22 | #include <vlibmemory/memory_api.h> |
| 23 | |
| 24 | #include <vlibmemory/vl_memory_msg_enum.h> /* enumerate all vlib messages */ |
| 25 | |
| 26 | #define vl_typedefs /* define message structures */ |
| 27 | #include <vlibmemory/vl_memory_api_h.h> |
| 28 | #undef vl_typedefs |
| 29 | |
| 30 | /* instantiate all the print functions we know about */ |
| 31 | #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__) |
| 32 | #define vl_printfun |
| 33 | #include <vlibmemory/vl_memory_api_h.h> |
| 34 | #undef vl_printfun |
| 35 | |
| 36 | /* instantiate all the endian swap functions we know about */ |
| 37 | #define vl_endianfun |
| 38 | #include <vlibmemory/vl_memory_api_h.h> |
| 39 | #undef vl_endianfun |
| 40 | |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 41 | volatile int **vl_api_queue_cursizes; |
| 42 | |
| 43 | static void |
| 44 | memclnt_queue_callback (vlib_main_t * vm) |
| 45 | { |
| 46 | int i; |
Dave Barach | 39d6911 | 2019-11-27 11:42:13 -0500 | [diff] [blame] | 47 | api_main_t *am = vlibapi_get_main (); |
Xiaoming Jiang | 405debc | 2021-07-13 03:55:59 +0000 | [diff] [blame] | 48 | int have_pending_rpcs; |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 49 | |
| 50 | if (PREDICT_FALSE (vec_len (vl_api_queue_cursizes) != |
| 51 | 1 + vec_len (am->vlib_private_rps))) |
| 52 | { |
| 53 | vl_shmem_hdr_t *shmem_hdr = am->shmem_hdr; |
| 54 | svm_queue_t *q; |
| 55 | |
| 56 | if (shmem_hdr == 0) |
| 57 | return; |
| 58 | |
| 59 | q = shmem_hdr->vl_input_queue; |
| 60 | if (q == 0) |
| 61 | return; |
| 62 | |
| 63 | vec_add1 (vl_api_queue_cursizes, &q->cursize); |
| 64 | |
| 65 | for (i = 0; i < vec_len (am->vlib_private_rps); i++) |
| 66 | { |
| 67 | svm_region_t *vlib_rp = am->vlib_private_rps[i]; |
| 68 | |
| 69 | shmem_hdr = (void *) vlib_rp->user_ctx; |
| 70 | q = shmem_hdr->vl_input_queue; |
| 71 | vec_add1 (vl_api_queue_cursizes, &q->cursize); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | for (i = 0; i < vec_len (vl_api_queue_cursizes); i++) |
| 76 | { |
| 77 | if (*vl_api_queue_cursizes[i]) |
| 78 | { |
| 79 | vm->queue_signal_pending = 1; |
| 80 | vm->api_queue_nonempty = 1; |
| 81 | vlib_process_signal_event (vm, vl_api_clnt_node.index, |
| 82 | /* event_type */ QUEUE_SIGNAL_EVENT, |
| 83 | /* event_data */ 0); |
| 84 | break; |
| 85 | } |
| 86 | } |
Xiaoming Jiang | 405debc | 2021-07-13 03:55:59 +0000 | [diff] [blame] | 87 | |
| 88 | clib_spinlock_lock_if_init (&vm->pending_rpc_lock); |
| 89 | have_pending_rpcs = vec_len (vm->pending_rpc_requests) > 0; |
| 90 | clib_spinlock_unlock_if_init (&vm->pending_rpc_lock); |
| 91 | |
| 92 | if (have_pending_rpcs) |
Dave Barach | f6c68d7 | 2018-11-01 08:12:52 -0400 | [diff] [blame] | 93 | { |
| 94 | vm->queue_signal_pending = 1; |
| 95 | vm->api_queue_nonempty = 1; |
| 96 | vlib_process_signal_event (vm, vl_api_clnt_node.index, |
| 97 | /* event_type */ QUEUE_SIGNAL_EVENT, |
| 98 | /* event_data */ 0); |
| 99 | } |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | /* |
| 103 | * vl_api_memclnt_create_internal |
| 104 | */ |
| 105 | u32 |
| 106 | vl_api_memclnt_create_internal (char *name, svm_queue_t * q) |
| 107 | { |
| 108 | vl_api_registration_t **regpp; |
| 109 | vl_api_registration_t *regp; |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 110 | void *oldheap; |
Dave Barach | 39d6911 | 2019-11-27 11:42:13 -0500 | [diff] [blame] | 111 | api_main_t *am = vlibapi_get_main (); |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 112 | |
| 113 | ASSERT (vlib_get_thread_index () == 0); |
| 114 | pool_get (am->vl_clients, regpp); |
| 115 | |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 116 | |
Nathan Skrzypczak | 0aa4013 | 2019-11-25 16:29:38 +0100 | [diff] [blame] | 117 | oldheap = vl_msg_push_heap (); |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 118 | *regpp = clib_mem_alloc (sizeof (vl_api_registration_t)); |
| 119 | |
| 120 | regp = *regpp; |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 121 | clib_memset (regp, 0, sizeof (*regp)); |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 122 | regp->registration_type = REGISTRATION_TYPE_SHMEM; |
| 123 | regp->vl_api_registration_pool_index = regpp - am->vl_clients; |
Nathan Skrzypczak | 0aa4013 | 2019-11-25 16:29:38 +0100 | [diff] [blame] | 124 | regp->vlib_rp = am->vlib_rp; |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 125 | regp->shmem_hdr = am->shmem_hdr; |
| 126 | |
| 127 | regp->vl_input_queue = q; |
| 128 | regp->name = format (0, "%s%c", name, 0); |
| 129 | |
Nathan Skrzypczak | 0aa4013 | 2019-11-25 16:29:38 +0100 | [diff] [blame] | 130 | vl_msg_pop_heap (oldheap); |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 131 | return vl_msg_api_handle_from_index_and_epoch |
| 132 | (regp->vl_api_registration_pool_index, |
| 133 | am->shmem_hdr->application_restarts); |
| 134 | } |
| 135 | |
| 136 | /* |
| 137 | * vl_api_memclnt_create_t_handler |
| 138 | */ |
| 139 | void |
| 140 | vl_api_memclnt_create_t_handler (vl_api_memclnt_create_t * mp) |
| 141 | { |
| 142 | vl_api_registration_t **regpp; |
| 143 | vl_api_registration_t *regp; |
| 144 | vl_api_memclnt_create_reply_t *rp; |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 145 | svm_queue_t *q; |
| 146 | int rv = 0; |
| 147 | void *oldheap; |
Dave Barach | 39d6911 | 2019-11-27 11:42:13 -0500 | [diff] [blame] | 148 | api_main_t *am = vlibapi_get_main (); |
Florin Coras | e25c9bf | 2018-08-06 12:19:29 -0700 | [diff] [blame] | 149 | u8 *msg_table; |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 150 | |
| 151 | /* |
| 152 | * This is tortured. Maintain a vlib-address-space private |
| 153 | * pool of client registrations. We use the shared-memory virtual |
| 154 | * address of client structure as a handle, to allow direct |
| 155 | * manipulation of context quota vbls from the client library. |
| 156 | * |
| 157 | * This scheme causes trouble w/ API message trace replay, since |
| 158 | * some random VA from clib_mem_alloc() certainly won't |
| 159 | * occur in the Linux sim. The (very) few places |
| 160 | * that care need to use the pool index. |
| 161 | * |
| 162 | * Putting the registration object(s) into a pool in shared memory and |
| 163 | * using the pool index as a handle seems like a great idea. |
| 164 | * Unfortunately, each and every reference to that pool would need |
| 165 | * to be protected by a mutex: |
| 166 | * |
| 167 | * Client VLIB |
| 168 | * ------ ---- |
| 169 | * convert pool index to |
| 170 | * pointer. |
| 171 | * <deschedule> |
| 172 | * expand pool |
| 173 | * <deschedule> |
| 174 | * kaboom! |
| 175 | */ |
| 176 | |
| 177 | pool_get (am->vl_clients, regpp); |
| 178 | |
Nathan Skrzypczak | 0aa4013 | 2019-11-25 16:29:38 +0100 | [diff] [blame] | 179 | oldheap = vl_msg_push_heap (); |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 180 | *regpp = clib_mem_alloc (sizeof (vl_api_registration_t)); |
| 181 | |
| 182 | regp = *regpp; |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 183 | clib_memset (regp, 0, sizeof (*regp)); |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 184 | regp->registration_type = REGISTRATION_TYPE_SHMEM; |
| 185 | regp->vl_api_registration_pool_index = regpp - am->vl_clients; |
Nathan Skrzypczak | 0aa4013 | 2019-11-25 16:29:38 +0100 | [diff] [blame] | 186 | regp->vlib_rp = am->vlib_rp; |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 187 | regp->shmem_hdr = am->shmem_hdr; |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 188 | regp->clib_file_index = am->shmem_hdr->clib_file_index; |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 189 | |
| 190 | q = regp->vl_input_queue = (svm_queue_t *) (uword) mp->input_queue; |
Benoît Ganne | 9fb6d40 | 2019-04-15 15:28:21 +0200 | [diff] [blame] | 191 | VL_MSG_API_SVM_QUEUE_UNPOISON (q); |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 192 | |
Ole Troan | 7adaa22 | 2019-08-27 15:05:27 +0200 | [diff] [blame] | 193 | regp->name = format (0, "%s", mp->name); |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 194 | vec_add1 (regp->name, 0); |
| 195 | |
| 196 | if (am->serialized_message_table_in_shmem == 0) |
| 197 | am->serialized_message_table_in_shmem = |
| 198 | vl_api_serialize_message_table (am, 0); |
| 199 | |
Florin Coras | e25c9bf | 2018-08-06 12:19:29 -0700 | [diff] [blame] | 200 | if (am->vlib_rp != am->vlib_primary_rp) |
| 201 | msg_table = vl_api_serialize_message_table (am, 0); |
| 202 | else |
| 203 | msg_table = am->serialized_message_table_in_shmem; |
| 204 | |
Nathan Skrzypczak | 0aa4013 | 2019-11-25 16:29:38 +0100 | [diff] [blame] | 205 | vl_msg_pop_heap (oldheap); |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 206 | |
| 207 | rp = vl_msg_api_alloc (sizeof (*rp)); |
| 208 | rp->_vl_msg_id = ntohs (VL_API_MEMCLNT_CREATE_REPLY); |
| 209 | rp->handle = (uword) regp; |
| 210 | rp->index = vl_msg_api_handle_from_index_and_epoch |
| 211 | (regp->vl_api_registration_pool_index, |
| 212 | am->shmem_hdr->application_restarts); |
| 213 | rp->context = mp->context; |
| 214 | rp->response = ntohl (rv); |
Florin Coras | e25c9bf | 2018-08-06 12:19:29 -0700 | [diff] [blame] | 215 | rp->message_table = pointer_to_uword (msg_table); |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 216 | |
| 217 | vl_msg_api_send_shmem (q, (u8 *) & rp); |
| 218 | } |
| 219 | |
Dave Barach | 38ca6e6 | 2020-07-17 17:16:34 -0400 | [diff] [blame] | 220 | void |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 221 | vl_api_call_reaper_functions (u32 client_index) |
| 222 | { |
| 223 | clib_error_t *error = 0; |
| 224 | _vl_msg_api_function_list_elt_t *i; |
| 225 | |
Dave Barach | 39d6911 | 2019-11-27 11:42:13 -0500 | [diff] [blame] | 226 | i = vlibapi_get_main ()->reaper_function_registrations; |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 227 | while (i) |
| 228 | { |
| 229 | error = i->f (client_index); |
| 230 | if (error) |
| 231 | clib_error_report (error); |
| 232 | i = i->next_init_function; |
| 233 | } |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | /* |
| 237 | * vl_api_memclnt_delete_t_handler |
| 238 | */ |
| 239 | void |
| 240 | vl_api_memclnt_delete_t_handler (vl_api_memclnt_delete_t * mp) |
| 241 | { |
| 242 | vl_api_registration_t **regpp; |
| 243 | vl_api_registration_t *regp; |
| 244 | vl_api_memclnt_delete_reply_t *rp; |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 245 | void *oldheap; |
Dave Barach | 39d6911 | 2019-11-27 11:42:13 -0500 | [diff] [blame] | 246 | api_main_t *am = vlibapi_get_main (); |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 247 | u32 handle, client_index, epoch; |
| 248 | |
| 249 | handle = mp->index; |
| 250 | |
Dave Barach | 38ca6e6 | 2020-07-17 17:16:34 -0400 | [diff] [blame] | 251 | vl_api_call_reaper_functions (handle); |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 252 | |
| 253 | epoch = vl_msg_api_handle_get_epoch (handle); |
| 254 | client_index = vl_msg_api_handle_get_index (handle); |
| 255 | |
| 256 | if (epoch != (am->shmem_hdr->application_restarts & VL_API_EPOCH_MASK)) |
| 257 | { |
| 258 | clib_warning |
| 259 | ("Stale clnt delete index %d old epoch %d cur epoch %d", |
| 260 | client_index, epoch, |
| 261 | (am->shmem_hdr->application_restarts & VL_API_EPOCH_MASK)); |
| 262 | return; |
| 263 | } |
| 264 | |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 265 | regpp = pool_elt_at_index (am->vl_clients, client_index); |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 266 | |
| 267 | if (!pool_is_free (am->vl_clients, regpp)) |
| 268 | { |
| 269 | int i; |
| 270 | regp = *regpp; |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 271 | int private_registration = 0; |
| 272 | |
Florin Coras | eaec2a6 | 2018-12-04 16:34:05 -0800 | [diff] [blame] | 273 | /* Send reply unless client asked us to do the cleanup */ |
| 274 | if (!mp->do_cleanup) |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 275 | { |
Florin Coras | eaec2a6 | 2018-12-04 16:34:05 -0800 | [diff] [blame] | 276 | /* |
| 277 | * Note: the API message handling path will set am->vlib_rp |
| 278 | * as appropriate for pairwise / private memory segments |
| 279 | */ |
| 280 | rp = vl_msg_api_alloc (sizeof (*rp)); |
| 281 | rp->_vl_msg_id = ntohs (VL_API_MEMCLNT_DELETE_REPLY); |
| 282 | rp->handle = mp->handle; |
| 283 | rp->response = 1; |
| 284 | |
| 285 | vl_msg_api_send_shmem (regp->vl_input_queue, (u8 *) & rp); |
| 286 | if (client_index != regp->vl_api_registration_pool_index) |
| 287 | { |
| 288 | clib_warning ("mismatch client_index %d pool_index %d", |
| 289 | client_index, |
| 290 | regp->vl_api_registration_pool_index); |
| 291 | vl_msg_api_free (rp); |
| 292 | return; |
| 293 | } |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 294 | } |
| 295 | |
Benoît Ganne | f26b251 | 2019-09-11 16:43:44 +0200 | [diff] [blame] | 296 | /* No dangling references, please */ |
| 297 | *regpp = 0; |
| 298 | |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 299 | /* For horizontal scaling, add a hash table... */ |
| 300 | for (i = 0; i < vec_len (am->vlib_private_rps); i++) |
| 301 | { |
| 302 | /* Is this a pairwise / private API segment? */ |
Nathan Skrzypczak | 0aa4013 | 2019-11-25 16:29:38 +0100 | [diff] [blame] | 303 | if (am->vlib_private_rps[i] == am->vlib_rp) |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 304 | { |
| 305 | /* Note: account for the memfd header page */ |
Nathan Skrzypczak | 0aa4013 | 2019-11-25 16:29:38 +0100 | [diff] [blame] | 306 | uword virtual_base = am->vlib_rp->virtual_base - MMAP_PAGESIZE; |
| 307 | uword virtual_size = am->vlib_rp->virtual_size + MMAP_PAGESIZE; |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 308 | |
| 309 | /* |
| 310 | * Kill the registration pool element before we make |
| 311 | * the index vanish forever |
| 312 | */ |
| 313 | pool_put_index (am->vl_clients, |
| 314 | regp->vl_api_registration_pool_index); |
| 315 | |
| 316 | vec_delete (am->vlib_private_rps, 1, i); |
| 317 | /* Kill it, accounting for the memfd header page */ |
| 318 | if (munmap ((void *) virtual_base, virtual_size) < 0) |
| 319 | clib_unix_warning ("munmap"); |
| 320 | /* Reset the queue-length-address cache */ |
| 321 | vec_reset_length (vl_api_queue_cursizes); |
| 322 | private_registration = 1; |
| 323 | break; |
| 324 | } |
| 325 | } |
| 326 | |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 327 | if (private_registration == 0) |
| 328 | { |
| 329 | pool_put_index (am->vl_clients, |
| 330 | regp->vl_api_registration_pool_index); |
Nathan Skrzypczak | 0aa4013 | 2019-11-25 16:29:38 +0100 | [diff] [blame] | 331 | oldheap = vl_msg_push_heap (); |
Florin Coras | eaec2a6 | 2018-12-04 16:34:05 -0800 | [diff] [blame] | 332 | if (mp->do_cleanup) |
| 333 | svm_queue_free (regp->vl_input_queue); |
Ole Troan | 73710c7 | 2018-06-04 22:27:49 +0200 | [diff] [blame] | 334 | vec_free (regp->name); |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 335 | /* Poison the old registration */ |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 336 | clib_memset (regp, 0xF1, sizeof (*regp)); |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 337 | clib_mem_free (regp); |
Nathan Skrzypczak | 0aa4013 | 2019-11-25 16:29:38 +0100 | [diff] [blame] | 338 | vl_msg_pop_heap (oldheap); |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 339 | /* |
| 340 | * These messages must be freed manually, since they're set up |
| 341 | * as "bounce" messages. In the private_registration == 1 case, |
| 342 | * we kill the shared-memory segment which contains the message |
| 343 | * with munmap. |
| 344 | */ |
| 345 | vl_msg_api_free (mp); |
| 346 | } |
| 347 | } |
| 348 | else |
| 349 | { |
| 350 | clib_warning ("unknown client ID %d", mp->index); |
| 351 | } |
| 352 | } |
| 353 | |
| 354 | /** |
| 355 | * client answered a ping, stave off the grim reaper... |
| 356 | */ |
| 357 | void |
| 358 | vl_api_memclnt_keepalive_reply_t_handler |
| 359 | (vl_api_memclnt_keepalive_reply_t * mp) |
| 360 | { |
| 361 | vl_api_registration_t *regp; |
| 362 | vlib_main_t *vm = vlib_get_main (); |
| 363 | |
| 364 | regp = vl_api_client_index_to_registration (mp->context); |
| 365 | if (regp) |
| 366 | { |
| 367 | regp->last_heard = vlib_time_now (vm); |
| 368 | regp->unanswered_pings = 0; |
| 369 | } |
| 370 | else |
| 371 | clib_warning ("BUG: anonymous memclnt_keepalive_reply"); |
| 372 | } |
| 373 | |
| 374 | /** |
| 375 | * We can send ourselves these messages if someone uses the |
| 376 | * builtin binary api test tool... |
| 377 | */ |
| 378 | static void |
| 379 | vl_api_memclnt_keepalive_t_handler (vl_api_memclnt_keepalive_t * mp) |
| 380 | { |
| 381 | vl_api_memclnt_keepalive_reply_t *rmp; |
| 382 | api_main_t *am; |
| 383 | vl_shmem_hdr_t *shmem_hdr; |
| 384 | |
Dave Barach | 39d6911 | 2019-11-27 11:42:13 -0500 | [diff] [blame] | 385 | am = vlibapi_get_main (); |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 386 | shmem_hdr = am->shmem_hdr; |
| 387 | |
| 388 | rmp = vl_msg_api_alloc_as_if_client (sizeof (*rmp)); |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 389 | clib_memset (rmp, 0, sizeof (*rmp)); |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 390 | rmp->_vl_msg_id = ntohs (VL_API_MEMCLNT_KEEPALIVE_REPLY); |
| 391 | rmp->context = mp->context; |
| 392 | vl_msg_api_send_shmem (shmem_hdr->vl_input_queue, (u8 *) & rmp); |
| 393 | } |
| 394 | |
Dave Barach | c89c767 | 2019-07-19 17:40:18 -0400 | [diff] [blame] | 395 | /* |
| 396 | * To avoid filling the API trace buffer with boring messages, |
| 397 | * don't trace memclnt_keepalive[_reply] msgs |
| 398 | */ |
| 399 | |
Filip Tehlar | 36217e3 | 2021-07-23 08:51:10 +0000 | [diff] [blame] | 400 | #define foreach_vlib_api_msg \ |
| 401 | _ (MEMCLNT_CREATE, memclnt_create, 0) \ |
| 402 | _ (MEMCLNT_DELETE, memclnt_delete, 0) \ |
| 403 | _ (MEMCLNT_KEEPALIVE, memclnt_keepalive, 0) \ |
| 404 | _ (MEMCLNT_KEEPALIVE_REPLY, memclnt_keepalive_reply, 0) |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 405 | |
| 406 | /* |
| 407 | * memory_api_init |
| 408 | */ |
| 409 | int |
| 410 | vl_mem_api_init (const char *region_name) |
| 411 | { |
| 412 | int rv; |
Dave Barach | 39d6911 | 2019-11-27 11:42:13 -0500 | [diff] [blame] | 413 | api_main_t *am = vlibapi_get_main (); |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 414 | vl_msg_api_msg_config_t cfg; |
| 415 | vl_msg_api_msg_config_t *c = &cfg; |
| 416 | vl_shmem_hdr_t *shm; |
| 417 | vlib_main_t *vm = vlib_get_main (); |
| 418 | |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 419 | clib_memset (c, 0, sizeof (*c)); |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 420 | |
| 421 | if ((rv = vl_map_shmem (region_name, 1 /* is_vlib */ )) < 0) |
| 422 | return rv; |
| 423 | |
Dave Barach | c89c767 | 2019-07-19 17:40:18 -0400 | [diff] [blame] | 424 | #define _(N,n,t) do { \ |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 425 | c->id = VL_API_##N; \ |
| 426 | c->name = #n; \ |
| 427 | c->handler = vl_api_##n##_t_handler; \ |
| 428 | c->cleanup = vl_noop_handler; \ |
| 429 | c->endian = vl_api_##n##_t_endian; \ |
| 430 | c->print = vl_api_##n##_t_print; \ |
| 431 | c->size = sizeof(vl_api_##n##_t); \ |
Dave Barach | c89c767 | 2019-07-19 17:40:18 -0400 | [diff] [blame] | 432 | c->traced = t; /* trace, so these msgs print */ \ |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 433 | c->replay = 0; /* don't replay client create/delete msgs */ \ |
| 434 | c->message_bounce = 0; /* don't bounce this message */ \ |
| 435 | vl_msg_api_config(c);} while (0); |
| 436 | |
| 437 | foreach_vlib_api_msg; |
| 438 | #undef _ |
| 439 | |
Ole Troan | 3459ece | 2021-09-27 17:11:34 +0200 | [diff] [blame] | 440 | #define vl_msg_name_crc_list |
| 441 | #include <vlibmemory/memclnt.api.h> |
| 442 | #undef vl_msg_name_crc_list |
| 443 | |
| 444 | #define _(id, n, crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id); |
| 445 | foreach_vl_msg_name_crc_memclnt; |
| 446 | #undef _ |
| 447 | |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 448 | /* |
| 449 | * special-case freeing of memclnt_delete messages, so we can |
| 450 | * simply munmap pairwise / private API segments... |
| 451 | */ |
| 452 | am->message_bounce[VL_API_MEMCLNT_DELETE] = 1; |
| 453 | am->is_mp_safe[VL_API_MEMCLNT_KEEPALIVE_REPLY] = 1; |
Dave Barach | c898a4f | 2019-06-14 17:29:55 -0400 | [diff] [blame] | 454 | am->is_mp_safe[VL_API_MEMCLNT_KEEPALIVE] = 1; |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 455 | |
| 456 | vlib_set_queue_signal_callback (vm, memclnt_queue_callback); |
| 457 | |
| 458 | shm = am->shmem_hdr; |
| 459 | ASSERT (shm && shm->vl_input_queue); |
| 460 | |
| 461 | /* Make a note so we can always find the primary region easily */ |
| 462 | am->vlib_primary_rp = am->vlib_rp; |
| 463 | |
| 464 | return 0; |
| 465 | } |
| 466 | |
Dave Barach | 1f80658 | 2018-06-14 09:18:21 -0400 | [diff] [blame] | 467 | clib_error_t * |
Dave Barach | 048a4e5 | 2018-06-01 18:52:25 -0400 | [diff] [blame] | 468 | map_api_segment_init (vlib_main_t * vm) |
| 469 | { |
Dave Barach | 39d6911 | 2019-11-27 11:42:13 -0500 | [diff] [blame] | 470 | api_main_t *am = vlibapi_get_main (); |
Dave Barach | 048a4e5 | 2018-06-01 18:52:25 -0400 | [diff] [blame] | 471 | int rv; |
| 472 | |
| 473 | if ((rv = vl_mem_api_init (am->region_name)) < 0) |
| 474 | { |
| 475 | return clib_error_return (0, "vl_mem_api_init (%s) failed", |
| 476 | am->region_name); |
| 477 | } |
| 478 | return 0; |
| 479 | } |
| 480 | |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 481 | static void |
| 482 | send_memclnt_keepalive (vl_api_registration_t * regp, f64 now) |
| 483 | { |
| 484 | vl_api_memclnt_keepalive_t *mp; |
Florin Coras | 8d82085 | 2019-11-27 09:15:25 -0800 | [diff] [blame] | 485 | svm_queue_t *q; |
Dave Barach | 39d6911 | 2019-11-27 11:42:13 -0500 | [diff] [blame] | 486 | api_main_t *am = vlibapi_get_main (); |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 487 | |
| 488 | q = regp->vl_input_queue; |
| 489 | |
| 490 | /* |
| 491 | * If the queue head is moving, assume that the client is processing |
| 492 | * messages and skip the ping. This heuristic may fail if the queue |
| 493 | * is in the same position as last time, net of wrapping; in which |
| 494 | * case, the client will receive a keepalive. |
| 495 | */ |
| 496 | if (regp->last_queue_head != q->head) |
| 497 | { |
| 498 | regp->last_heard = now; |
| 499 | regp->unanswered_pings = 0; |
| 500 | regp->last_queue_head = q->head; |
| 501 | return; |
| 502 | } |
| 503 | |
| 504 | /* |
| 505 | * push/pop shared memory segment, so this routine |
| 506 | * will work with "normal" as well as "private segment" |
| 507 | * memory clients.. |
| 508 | */ |
| 509 | |
Florin Coras | 8d82085 | 2019-11-27 09:15:25 -0800 | [diff] [blame] | 510 | mp = vl_mem_api_alloc_as_if_client_w_reg (regp, sizeof (*mp)); |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 511 | clib_memset (mp, 0, sizeof (*mp)); |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 512 | mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_MEMCLNT_KEEPALIVE); |
| 513 | mp->context = mp->client_index = |
| 514 | vl_msg_api_handle_from_index_and_epoch |
| 515 | (regp->vl_api_registration_pool_index, |
| 516 | am->shmem_hdr->application_restarts); |
| 517 | |
| 518 | regp->unanswered_pings++; |
| 519 | |
| 520 | /* Failure-to-send due to a stuffed queue is absolutely expected */ |
| 521 | if (svm_queue_add (q, (u8 *) & mp, 1 /* nowait */ )) |
Florin Coras | 8d82085 | 2019-11-27 09:15:25 -0800 | [diff] [blame] | 522 | vl_msg_api_free_w_region (regp->vlib_rp, mp); |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 523 | } |
| 524 | |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 525 | static void |
| 526 | vl_mem_send_client_keepalive_w_reg (api_main_t * am, f64 now, |
| 527 | vl_api_registration_t ** regpp, |
| 528 | u32 ** dead_indices, |
| 529 | u32 ** confused_indices) |
| 530 | { |
| 531 | vl_api_registration_t *regp = *regpp; |
| 532 | if (regp) |
| 533 | { |
| 534 | /* If we haven't heard from this client recently... */ |
| 535 | if (regp->last_heard < (now - 10.0)) |
| 536 | { |
| 537 | if (regp->unanswered_pings == 2) |
| 538 | { |
| 539 | svm_queue_t *q; |
| 540 | q = regp->vl_input_queue; |
| 541 | if (kill (q->consumer_pid, 0) >= 0) |
| 542 | { |
| 543 | clib_warning ("REAPER: lazy binary API client '%s'", |
| 544 | regp->name); |
| 545 | regp->unanswered_pings = 0; |
| 546 | regp->last_heard = now; |
| 547 | } |
| 548 | else |
| 549 | { |
| 550 | clib_warning ("REAPER: binary API client '%s' died", |
| 551 | regp->name); |
| 552 | vec_add1 (*dead_indices, regpp - am->vl_clients); |
| 553 | } |
| 554 | } |
| 555 | else |
| 556 | send_memclnt_keepalive (regp, now); |
| 557 | } |
| 558 | else |
| 559 | regp->unanswered_pings = 0; |
| 560 | } |
| 561 | else |
| 562 | { |
| 563 | clib_warning ("NULL client registration index %d", |
| 564 | regpp - am->vl_clients); |
| 565 | vec_add1 (*confused_indices, regpp - am->vl_clients); |
| 566 | } |
| 567 | } |
| 568 | |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 569 | void |
| 570 | vl_mem_api_dead_client_scan (api_main_t * am, vl_shmem_hdr_t * shm, f64 now) |
| 571 | { |
| 572 | vl_api_registration_t **regpp; |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 573 | static u32 *dead_indices; |
| 574 | static u32 *confused_indices; |
| 575 | |
| 576 | vec_reset_length (dead_indices); |
| 577 | vec_reset_length (confused_indices); |
| 578 | |
| 579 | /* *INDENT-OFF* */ |
Damjan Marion | b2c31b6 | 2020-12-13 21:47:40 +0100 | [diff] [blame] | 580 | pool_foreach (regpp, am->vl_clients) { |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 581 | vl_mem_send_client_keepalive_w_reg (am, now, regpp, &dead_indices, |
| 582 | &confused_indices); |
Damjan Marion | b2c31b6 | 2020-12-13 21:47:40 +0100 | [diff] [blame] | 583 | } |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 584 | /* *INDENT-ON* */ |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 585 | |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 586 | /* This should "never happen," but if it does, fix it... */ |
| 587 | if (PREDICT_FALSE (vec_len (confused_indices) > 0)) |
| 588 | { |
| 589 | int i; |
| 590 | for (i = 0; i < vec_len (confused_indices); i++) |
| 591 | { |
| 592 | pool_put_index (am->vl_clients, confused_indices[i]); |
| 593 | } |
| 594 | } |
| 595 | |
| 596 | if (PREDICT_FALSE (vec_len (dead_indices) > 0)) |
| 597 | { |
| 598 | int i; |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 599 | void *oldheap; |
| 600 | |
| 601 | /* Allow the application to clean up its registrations */ |
| 602 | for (i = 0; i < vec_len (dead_indices); i++) |
| 603 | { |
| 604 | regpp = pool_elt_at_index (am->vl_clients, dead_indices[i]); |
| 605 | if (regpp) |
| 606 | { |
| 607 | u32 handle; |
| 608 | |
| 609 | handle = vl_msg_api_handle_from_index_and_epoch |
| 610 | (dead_indices[i], shm->application_restarts); |
Dave Barach | 38ca6e6 | 2020-07-17 17:16:34 -0400 | [diff] [blame] | 611 | vl_api_call_reaper_functions (handle); |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 612 | } |
| 613 | } |
| 614 | |
Nathan Skrzypczak | 0aa4013 | 2019-11-25 16:29:38 +0100 | [diff] [blame] | 615 | oldheap = vl_msg_push_heap (); |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 616 | |
| 617 | for (i = 0; i < vec_len (dead_indices); i++) |
| 618 | { |
| 619 | regpp = pool_elt_at_index (am->vl_clients, dead_indices[i]); |
| 620 | if (regpp) |
| 621 | { |
| 622 | /* Is this a pairwise SVM segment? */ |
Nathan Skrzypczak | 0aa4013 | 2019-11-25 16:29:38 +0100 | [diff] [blame] | 623 | if ((*regpp)->vlib_rp != am->vlib_rp) |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 624 | { |
| 625 | int i; |
| 626 | svm_region_t *dead_rp = (*regpp)->vlib_rp; |
| 627 | /* Note: account for the memfd header page */ |
David Johnson | d9818dd | 2018-12-14 14:53:41 -0500 | [diff] [blame] | 628 | uword virtual_base = dead_rp->virtual_base - MMAP_PAGESIZE; |
| 629 | uword virtual_size = dead_rp->virtual_size + MMAP_PAGESIZE; |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 630 | |
| 631 | /* For horizontal scaling, add a hash table... */ |
| 632 | for (i = 0; i < vec_len (am->vlib_private_rps); i++) |
| 633 | if (am->vlib_private_rps[i] == dead_rp) |
| 634 | { |
| 635 | vec_delete (am->vlib_private_rps, 1, i); |
| 636 | goto found; |
| 637 | } |
Nathan Skrzypczak | 5ed3fe3 | 2019-11-07 16:00:57 +0100 | [diff] [blame] | 638 | svm_pop_heap (oldheap); |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 639 | clib_warning ("private rp %llx AWOL", dead_rp); |
Nathan Skrzypczak | 0aa4013 | 2019-11-25 16:29:38 +0100 | [diff] [blame] | 640 | oldheap = svm_push_data_heap (am->vlib_rp); |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 641 | |
| 642 | found: |
| 643 | /* Kill it, accounting for the memfd header page */ |
Nathan Skrzypczak | 5ed3fe3 | 2019-11-07 16:00:57 +0100 | [diff] [blame] | 644 | svm_pop_heap (oldheap); |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 645 | if (munmap ((void *) virtual_base, virtual_size) < 0) |
| 646 | clib_unix_warning ("munmap"); |
| 647 | /* Reset the queue-length-address cache */ |
| 648 | vec_reset_length (vl_api_queue_cursizes); |
Nathan Skrzypczak | 0aa4013 | 2019-11-25 16:29:38 +0100 | [diff] [blame] | 649 | oldheap = svm_push_data_heap (am->vlib_rp); |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 650 | } |
| 651 | else |
| 652 | { |
| 653 | /* Poison the old registration */ |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 654 | clib_memset (*regpp, 0xF3, sizeof (**regpp)); |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 655 | clib_mem_free (*regpp); |
| 656 | } |
| 657 | /* no dangling references, please */ |
| 658 | *regpp = 0; |
| 659 | } |
| 660 | else |
| 661 | { |
| 662 | svm_pop_heap (oldheap); |
| 663 | clib_warning ("Duplicate free, client index %d", |
| 664 | regpp - am->vl_clients); |
Nathan Skrzypczak | 0aa4013 | 2019-11-25 16:29:38 +0100 | [diff] [blame] | 665 | oldheap = svm_push_data_heap (am->vlib_rp); |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 666 | } |
| 667 | } |
| 668 | |
| 669 | svm_client_scan_this_region_nolock (am->vlib_rp); |
| 670 | |
Nathan Skrzypczak | 0aa4013 | 2019-11-25 16:29:38 +0100 | [diff] [blame] | 671 | vl_msg_pop_heap (oldheap); |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 672 | for (i = 0; i < vec_len (dead_indices); i++) |
| 673 | pool_put_index (am->vl_clients, dead_indices[i]); |
| 674 | } |
| 675 | } |
| 676 | |
Benoît Ganne | 7f9256e | 2022-03-04 17:17:04 +0100 | [diff] [blame] | 677 | void (*vl_mem_api_fuzz_hook) (u16, void *); |
| 678 | |
| 679 | /* This is only to be called from a vlib/vnet app */ |
| 680 | static void |
| 681 | vl_mem_api_handler_with_vm_node (api_main_t *am, svm_region_t *vlib_rp, |
| 682 | void *the_msg, vlib_main_t *vm, |
| 683 | vlib_node_runtime_t *node, u8 is_private) |
| 684 | { |
| 685 | u16 id = clib_net_to_host_u16 (*((u16 *) the_msg)); |
| 686 | u8 *(*handler) (void *, void *, void *); |
| 687 | u8 *(*print_fp) (void *, void *); |
| 688 | svm_region_t *old_vlib_rp; |
| 689 | void *save_shmem_hdr; |
| 690 | int is_mp_safe = 1; |
| 691 | |
| 692 | if (PREDICT_FALSE (am->elog_trace_api_messages)) |
| 693 | { |
| 694 | ELOG_TYPE_DECLARE (e) = { |
| 695 | .format = "api-msg: %s", |
| 696 | .format_args = "T4", |
| 697 | }; |
| 698 | struct |
| 699 | { |
| 700 | u32 c; |
| 701 | } * ed; |
| 702 | ed = ELOG_DATA (am->elog_main, e); |
| 703 | if (id < vec_len (am->msg_names) && am->msg_names[id]) |
| 704 | ed->c = elog_string (am->elog_main, (char *) am->msg_names[id]); |
| 705 | else |
| 706 | ed->c = elog_string (am->elog_main, "BOGUS"); |
| 707 | } |
| 708 | |
| 709 | if (id < vec_len (am->msg_handlers) && am->msg_handlers[id]) |
| 710 | { |
| 711 | handler = (void *) am->msg_handlers[id]; |
| 712 | |
| 713 | if (PREDICT_FALSE (am->rx_trace && am->rx_trace->enabled)) |
| 714 | vl_msg_api_trace (am, am->rx_trace, the_msg); |
| 715 | |
| 716 | if (PREDICT_FALSE (am->msg_print_flag)) |
| 717 | { |
| 718 | fformat (stdout, "[%d]: %s\n", id, am->msg_names[id]); |
| 719 | print_fp = (void *) am->msg_print_handlers[id]; |
| 720 | if (print_fp == 0) |
| 721 | { |
| 722 | fformat (stdout, " [no registered print fn for msg %d]\n", id); |
| 723 | } |
| 724 | else |
| 725 | { |
| 726 | (*print_fp) (the_msg, vm); |
| 727 | } |
| 728 | } |
| 729 | is_mp_safe = am->is_mp_safe[id]; |
| 730 | |
| 731 | if (!is_mp_safe) |
| 732 | { |
| 733 | vl_msg_api_barrier_trace_context (am->msg_names[id]); |
| 734 | vl_msg_api_barrier_sync (); |
| 735 | } |
| 736 | if (is_private) |
| 737 | { |
| 738 | old_vlib_rp = am->vlib_rp; |
| 739 | save_shmem_hdr = am->shmem_hdr; |
| 740 | am->vlib_rp = vlib_rp; |
| 741 | am->shmem_hdr = (void *) vlib_rp->user_ctx; |
| 742 | } |
| 743 | |
| 744 | if (PREDICT_FALSE (vl_mem_api_fuzz_hook != 0)) |
| 745 | (*vl_mem_api_fuzz_hook) (id, the_msg); |
| 746 | |
| 747 | if (am->is_autoendian[id]) |
| 748 | { |
| 749 | void (*endian_fp) (void *); |
| 750 | endian_fp = am->msg_endian_handlers[id]; |
| 751 | (*endian_fp) (the_msg); |
| 752 | } |
| 753 | if (PREDICT_FALSE (vec_len (am->perf_counter_cbs) != 0)) |
| 754 | clib_call_callbacks (am->perf_counter_cbs, am, id, 0 /* before */); |
| 755 | |
| 756 | (*handler) (the_msg, vm, node); |
| 757 | |
| 758 | if (PREDICT_FALSE (vec_len (am->perf_counter_cbs) != 0)) |
| 759 | clib_call_callbacks (am->perf_counter_cbs, am, id, 1 /* after */); |
| 760 | if (is_private) |
| 761 | { |
| 762 | am->vlib_rp = old_vlib_rp; |
| 763 | am->shmem_hdr = save_shmem_hdr; |
| 764 | } |
| 765 | if (!is_mp_safe) |
| 766 | vl_msg_api_barrier_release (); |
| 767 | } |
| 768 | else |
| 769 | { |
| 770 | clib_warning ("no handler for msg id %d", id); |
| 771 | } |
| 772 | |
| 773 | /* |
| 774 | * Special-case, so we can e.g. bounce messages off the vnet |
| 775 | * main thread without copying them... |
| 776 | */ |
| 777 | if (id >= vec_len (am->message_bounce) || !(am->message_bounce[id])) |
| 778 | { |
| 779 | if (is_private) |
| 780 | { |
| 781 | old_vlib_rp = am->vlib_rp; |
| 782 | save_shmem_hdr = am->shmem_hdr; |
| 783 | am->vlib_rp = vlib_rp; |
| 784 | am->shmem_hdr = (void *) vlib_rp->user_ctx; |
| 785 | } |
| 786 | vl_msg_api_free (the_msg); |
| 787 | if (is_private) |
| 788 | { |
| 789 | am->vlib_rp = old_vlib_rp; |
| 790 | am->shmem_hdr = save_shmem_hdr; |
| 791 | } |
| 792 | } |
| 793 | |
| 794 | if (PREDICT_FALSE (am->elog_trace_api_messages)) |
| 795 | { |
| 796 | ELOG_TYPE_DECLARE (e) = { .format = "api-msg-done(%s): %s", |
| 797 | .format_args = "t4T4", |
| 798 | .n_enum_strings = 2, |
| 799 | .enum_strings = { |
| 800 | "barrier", |
| 801 | "mp-safe", |
| 802 | } }; |
| 803 | |
| 804 | struct |
| 805 | { |
| 806 | u32 barrier; |
| 807 | u32 c; |
| 808 | } * ed; |
| 809 | ed = ELOG_DATA (am->elog_main, e); |
| 810 | if (id < vec_len (am->msg_names) && am->msg_names[id]) |
| 811 | ed->c = elog_string (am->elog_main, (char *) am->msg_names[id]); |
| 812 | else |
| 813 | ed->c = elog_string (am->elog_main, "BOGUS"); |
| 814 | ed->barrier = is_mp_safe; |
| 815 | } |
| 816 | } |
| 817 | |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 818 | static inline int |
Florin Coras | 8d82085 | 2019-11-27 09:15:25 -0800 | [diff] [blame] | 819 | void_mem_api_handle_msg_i (api_main_t * am, svm_region_t * vlib_rp, |
| 820 | vlib_main_t * vm, vlib_node_runtime_t * node, |
| 821 | u8 is_private) |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 822 | { |
Florin Coras | 8d82085 | 2019-11-27 09:15:25 -0800 | [diff] [blame] | 823 | svm_queue_t *q; |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 824 | uword mp; |
Florin Coras | 8d82085 | 2019-11-27 09:15:25 -0800 | [diff] [blame] | 825 | |
| 826 | q = ((vl_shmem_hdr_t *) (void *) vlib_rp->user_ctx)->vl_input_queue; |
| 827 | |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 828 | if (!svm_queue_sub2 (q, (u8 *) & mp)) |
| 829 | { |
Benoît Ganne | 9fb6d40 | 2019-04-15 15:28:21 +0200 | [diff] [blame] | 830 | VL_MSG_API_UNPOISON ((void *) mp); |
Benoît Ganne | 7f9256e | 2022-03-04 17:17:04 +0100 | [diff] [blame] | 831 | vl_mem_api_handler_with_vm_node (am, vlib_rp, (void *) mp, vm, node, |
Florin Coras | 8d82085 | 2019-11-27 09:15:25 -0800 | [diff] [blame] | 832 | is_private); |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 833 | return 0; |
| 834 | } |
| 835 | return -1; |
| 836 | } |
| 837 | |
| 838 | int |
| 839 | vl_mem_api_handle_msg_main (vlib_main_t * vm, vlib_node_runtime_t * node) |
| 840 | { |
Dave Barach | 39d6911 | 2019-11-27 11:42:13 -0500 | [diff] [blame] | 841 | api_main_t *am = vlibapi_get_main (); |
Florin Coras | 8d82085 | 2019-11-27 09:15:25 -0800 | [diff] [blame] | 842 | return void_mem_api_handle_msg_i (am, am->vlib_rp, vm, node, |
| 843 | 0 /* is_private */ ); |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 844 | } |
| 845 | |
| 846 | int |
Dave Barach | f6c68d7 | 2018-11-01 08:12:52 -0400 | [diff] [blame] | 847 | vl_mem_api_handle_rpc (vlib_main_t * vm, vlib_node_runtime_t * node) |
| 848 | { |
Dave Barach | 39d6911 | 2019-11-27 11:42:13 -0500 | [diff] [blame] | 849 | api_main_t *am = vlibapi_get_main (); |
Dave Barach | f6c68d7 | 2018-11-01 08:12:52 -0400 | [diff] [blame] | 850 | int i; |
| 851 | uword *tmp, mp; |
| 852 | |
| 853 | /* |
| 854 | * Swap pending and processing vectors, then process the RPCs |
| 855 | * Avoid deadlock conditions by construction. |
| 856 | */ |
| 857 | clib_spinlock_lock_if_init (&vm->pending_rpc_lock); |
| 858 | tmp = vm->processing_rpc_requests; |
| 859 | vec_reset_length (tmp); |
| 860 | vm->processing_rpc_requests = vm->pending_rpc_requests; |
| 861 | vm->pending_rpc_requests = tmp; |
| 862 | clib_spinlock_unlock_if_init (&vm->pending_rpc_lock); |
| 863 | |
Dave Barach | 1bb981d | 2019-02-26 17:04:40 -0500 | [diff] [blame] | 864 | /* |
| 865 | * RPCs are used to reflect function calls to thread 0 |
| 866 | * when the underlying code is not thread-safe. |
| 867 | * |
| 868 | * Grabbing the thread barrier across a set of RPCs |
| 869 | * greatly increases efficiency, and avoids |
| 870 | * running afoul of the barrier sync holddown timer. |
| 871 | * The barrier sync code supports recursive locking. |
| 872 | * |
| 873 | * We really need to rewrite RPC-based code... |
| 874 | */ |
| 875 | if (PREDICT_TRUE (vec_len (vm->processing_rpc_requests))) |
Dave Barach | f6c68d7 | 2018-11-01 08:12:52 -0400 | [diff] [blame] | 876 | { |
Dave Barach | 1bb981d | 2019-02-26 17:04:40 -0500 | [diff] [blame] | 877 | vl_msg_api_barrier_sync (); |
| 878 | for (i = 0; i < vec_len (vm->processing_rpc_requests); i++) |
| 879 | { |
| 880 | mp = vm->processing_rpc_requests[i]; |
Benoît Ganne | 7f9256e | 2022-03-04 17:17:04 +0100 | [diff] [blame] | 881 | vl_mem_api_handler_with_vm_node (am, am->vlib_rp, (void *) mp, vm, |
| 882 | node, 0 /* is_private */); |
Dave Barach | 1bb981d | 2019-02-26 17:04:40 -0500 | [diff] [blame] | 883 | } |
| 884 | vl_msg_api_barrier_release (); |
Dave Barach | f6c68d7 | 2018-11-01 08:12:52 -0400 | [diff] [blame] | 885 | } |
Dave Barach | 1bb981d | 2019-02-26 17:04:40 -0500 | [diff] [blame] | 886 | |
Dave Barach | f6c68d7 | 2018-11-01 08:12:52 -0400 | [diff] [blame] | 887 | return 0; |
| 888 | } |
| 889 | |
| 890 | int |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 891 | vl_mem_api_handle_msg_private (vlib_main_t * vm, vlib_node_runtime_t * node, |
| 892 | u32 reg_index) |
| 893 | { |
Dave Barach | 39d6911 | 2019-11-27 11:42:13 -0500 | [diff] [blame] | 894 | api_main_t *am = vlibapi_get_main (); |
Florin Coras | 8d82085 | 2019-11-27 09:15:25 -0800 | [diff] [blame] | 895 | return void_mem_api_handle_msg_i (am, am->vlib_private_rps[reg_index], vm, |
| 896 | node, 1 /* is_private */ ); |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 897 | } |
| 898 | |
| 899 | vl_api_registration_t * |
| 900 | vl_mem_api_client_index_to_registration (u32 handle) |
| 901 | { |
| 902 | vl_api_registration_t **regpp; |
| 903 | vl_api_registration_t *regp; |
Dave Barach | 39d6911 | 2019-11-27 11:42:13 -0500 | [diff] [blame] | 904 | api_main_t *am = vlibapi_get_main (); |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 905 | vl_shmem_hdr_t *shmem_hdr; |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 906 | u32 index; |
| 907 | |
| 908 | index = vl_msg_api_handle_get_index (handle); |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 909 | regpp = am->vl_clients + index; |
| 910 | |
| 911 | if (pool_is_free (am->vl_clients, regpp)) |
| 912 | { |
| 913 | vl_msg_api_increment_missing_client_counter (); |
| 914 | return 0; |
| 915 | } |
| 916 | regp = *regpp; |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 917 | |
| 918 | shmem_hdr = (vl_shmem_hdr_t *) regp->shmem_hdr; |
| 919 | if (!vl_msg_api_handle_is_valid (handle, shmem_hdr->application_restarts)) |
| 920 | { |
| 921 | vl_msg_api_increment_missing_client_counter (); |
| 922 | return 0; |
| 923 | } |
| 924 | |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 925 | return (regp); |
| 926 | } |
| 927 | |
| 928 | svm_queue_t * |
| 929 | vl_api_client_index_to_input_queue (u32 index) |
| 930 | { |
| 931 | vl_api_registration_t *regp; |
Dave Barach | 39d6911 | 2019-11-27 11:42:13 -0500 | [diff] [blame] | 932 | api_main_t *am = vlibapi_get_main (); |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 933 | |
| 934 | /* Special case: vlib trying to send itself a message */ |
| 935 | if (index == (u32) ~ 0) |
| 936 | return (am->shmem_hdr->vl_input_queue); |
| 937 | |
| 938 | regp = vl_mem_api_client_index_to_registration (index); |
| 939 | if (!regp) |
| 940 | return 0; |
| 941 | return (regp->vl_input_queue); |
| 942 | } |
| 943 | |
| 944 | static clib_error_t * |
| 945 | setup_memclnt_exit (vlib_main_t * vm) |
| 946 | { |
| 947 | atexit (vl_unmap_shmem); |
| 948 | return 0; |
| 949 | } |
| 950 | |
| 951 | VLIB_INIT_FUNCTION (setup_memclnt_exit); |
| 952 | |
| 953 | u8 * |
| 954 | format_api_message_rings (u8 * s, va_list * args) |
| 955 | { |
| 956 | api_main_t *am = va_arg (*args, api_main_t *); |
| 957 | vl_shmem_hdr_t *shmem_hdr = va_arg (*args, vl_shmem_hdr_t *); |
| 958 | int main_segment = va_arg (*args, int); |
| 959 | ring_alloc_t *ap; |
| 960 | int i; |
| 961 | |
| 962 | if (shmem_hdr == 0) |
| 963 | return format (s, "%8s %8s %8s %8s %8s\n", |
| 964 | "Owner", "Size", "Nitems", "Hits", "Misses"); |
| 965 | |
| 966 | ap = shmem_hdr->vl_rings; |
| 967 | |
| 968 | for (i = 0; i < vec_len (shmem_hdr->vl_rings); i++) |
| 969 | { |
| 970 | s = format (s, "%8s %8d %8d %8d %8d\n", |
| 971 | "vlib", ap->size, ap->nitems, ap->hits, ap->misses); |
| 972 | ap++; |
| 973 | } |
| 974 | |
| 975 | ap = shmem_hdr->client_rings; |
| 976 | |
| 977 | for (i = 0; i < vec_len (shmem_hdr->client_rings); i++) |
| 978 | { |
| 979 | s = format (s, "%8s %8d %8d %8d %8d\n", |
| 980 | "clnt", ap->size, ap->nitems, ap->hits, ap->misses); |
| 981 | ap++; |
| 982 | } |
| 983 | |
| 984 | if (main_segment) |
| 985 | { |
| 986 | s = format (s, "%d ring miss fallback allocations\n", am->ring_misses); |
| 987 | s = format |
| 988 | (s, |
| 989 | "%d application restarts, %d reclaimed msgs, %d garbage collects\n", |
| 990 | shmem_hdr->application_restarts, shmem_hdr->restart_reclaims, |
| 991 | shmem_hdr->garbage_collects); |
| 992 | } |
| 993 | return s; |
| 994 | } |
| 995 | |
| 996 | static clib_error_t * |
| 997 | vl_api_ring_command (vlib_main_t * vm, |
| 998 | unformat_input_t * input, vlib_cli_command_t * cli_cmd) |
| 999 | { |
| 1000 | int i; |
| 1001 | vl_shmem_hdr_t *shmem_hdr; |
Dave Barach | 39d6911 | 2019-11-27 11:42:13 -0500 | [diff] [blame] | 1002 | api_main_t *am = vlibapi_get_main (); |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 1003 | |
| 1004 | /* First, dump the primary region rings.. */ |
| 1005 | |
| 1006 | if (am->vlib_primary_rp == 0 || am->vlib_primary_rp->user_ctx == 0) |
| 1007 | { |
| 1008 | vlib_cli_output (vm, "Shared memory segment not initialized...\n"); |
| 1009 | return 0; |
| 1010 | } |
| 1011 | |
| 1012 | shmem_hdr = (void *) am->vlib_primary_rp->user_ctx; |
| 1013 | |
| 1014 | vlib_cli_output (vm, "Main API segment rings:"); |
| 1015 | |
| 1016 | vlib_cli_output (vm, "%U", format_api_message_rings, am, |
| 1017 | 0 /* print header */ , 0 /* notused */ ); |
| 1018 | |
| 1019 | vlib_cli_output (vm, "%U", format_api_message_rings, am, |
| 1020 | shmem_hdr, 1 /* main segment */ ); |
| 1021 | |
| 1022 | for (i = 0; i < vec_len (am->vlib_private_rps); i++) |
| 1023 | { |
| 1024 | svm_region_t *vlib_rp = am->vlib_private_rps[i]; |
| 1025 | shmem_hdr = (void *) vlib_rp->user_ctx; |
| 1026 | vl_api_registration_t **regpp; |
| 1027 | vl_api_registration_t *regp = 0; |
| 1028 | |
| 1029 | /* For horizontal scaling, add a hash table... */ |
| 1030 | /* *INDENT-OFF* */ |
Damjan Marion | b2c31b6 | 2020-12-13 21:47:40 +0100 | [diff] [blame] | 1031 | pool_foreach (regpp, am->vl_clients) |
| 1032 | { |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 1033 | regp = *regpp; |
| 1034 | if (regp && regp->vlib_rp == vlib_rp) |
| 1035 | { |
| 1036 | vlib_cli_output (vm, "%s segment rings:", regp->name); |
| 1037 | goto found; |
| 1038 | } |
Damjan Marion | b2c31b6 | 2020-12-13 21:47:40 +0100 | [diff] [blame] | 1039 | } |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 1040 | vlib_cli_output (vm, "regp %llx not found?", regp); |
| 1041 | continue; |
| 1042 | /* *INDENT-ON* */ |
| 1043 | found: |
| 1044 | vlib_cli_output (vm, "%U", format_api_message_rings, am, |
| 1045 | 0 /* print header */ , 0 /* notused */ ); |
| 1046 | vlib_cli_output (vm, "%U", format_api_message_rings, am, |
| 1047 | shmem_hdr, 0 /* main segment */ ); |
| 1048 | } |
| 1049 | |
| 1050 | return 0; |
| 1051 | } |
| 1052 | |
| 1053 | /*? |
| 1054 | * Display binary api message allocation ring statistics |
| 1055 | ?*/ |
| 1056 | /* *INDENT-OFF* */ |
| 1057 | VLIB_CLI_COMMAND (cli_show_api_ring_command, static) = |
| 1058 | { |
| 1059 | .path = "show api ring-stats", |
| 1060 | .short_help = "Message ring statistics", |
| 1061 | .function = vl_api_ring_command, |
| 1062 | }; |
| 1063 | /* *INDENT-ON* */ |
| 1064 | |
| 1065 | clib_error_t * |
| 1066 | vlibmemory_init (vlib_main_t * vm) |
| 1067 | { |
Dave Barach | 39d6911 | 2019-11-27 11:42:13 -0500 | [diff] [blame] | 1068 | api_main_t *am = vlibapi_get_main (); |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 1069 | svm_map_region_args_t _a, *a = &_a; |
Dave Barach | b220467 | 2018-11-30 16:46:29 -0500 | [diff] [blame] | 1070 | u8 *remove_path1, *remove_path2; |
Dave Barach | f8d5068 | 2019-05-14 18:01:44 -0400 | [diff] [blame] | 1071 | void vlibsocket_reference (void); |
| 1072 | |
| 1073 | vlibsocket_reference (); |
Dave Barach | b220467 | 2018-11-30 16:46:29 -0500 | [diff] [blame] | 1074 | |
| 1075 | /* |
| 1076 | * By popular request / to avoid support fires, remove any old api segment |
| 1077 | * files Right Here. |
| 1078 | */ |
| 1079 | if (am->root_path == 0) |
| 1080 | { |
| 1081 | remove_path1 = format (0, "/dev/shm/global_vm%c", 0); |
| 1082 | remove_path2 = format (0, "/dev/shm/vpe-api%c", 0); |
| 1083 | } |
| 1084 | else |
| 1085 | { |
| 1086 | remove_path1 = format (0, "/dev/shm/%s-global_vm%c", am->root_path, 0); |
| 1087 | remove_path2 = format (0, "/dev/shm/%s-vpe-api%c", am->root_path, 0); |
| 1088 | } |
| 1089 | |
| 1090 | (void) unlink ((char *) remove_path1); |
| 1091 | (void) unlink ((char *) remove_path2); |
| 1092 | |
| 1093 | vec_free (remove_path1); |
| 1094 | vec_free (remove_path2); |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 1095 | |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 1096 | clib_memset (a, 0, sizeof (*a)); |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 1097 | a->root_path = am->root_path; |
| 1098 | a->name = SVM_GLOBAL_REGION_NAME; |
| 1099 | a->baseva = (am->global_baseva != 0) ? |
Damjan Marion | aec8f89 | 2018-01-08 16:35:35 +0100 | [diff] [blame] | 1100 | am->global_baseva : +svm_get_global_region_base_va (); |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 1101 | a->size = (am->global_size != 0) ? am->global_size : SVM_GLOBAL_REGION_SIZE; |
| 1102 | a->flags = SVM_FLAGS_NODATA; |
| 1103 | a->uid = am->api_uid; |
| 1104 | a->gid = am->api_gid; |
| 1105 | a->pvt_heap_size = |
| 1106 | (am->global_pvt_heap_size != |
| 1107 | 0) ? am->global_pvt_heap_size : SVM_PVT_MHEAP_SIZE; |
| 1108 | |
| 1109 | svm_region_init_args (a); |
| 1110 | |
Dave Barach | f8d5068 | 2019-05-14 18:01:44 -0400 | [diff] [blame] | 1111 | return 0; |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 1112 | } |
| 1113 | |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 1114 | void |
| 1115 | vl_set_memory_region_name (const char *name) |
| 1116 | { |
Dave Barach | 39d6911 | 2019-11-27 11:42:13 -0500 | [diff] [blame] | 1117 | api_main_t *am = vlibapi_get_main (); |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 1118 | am->region_name = name; |
| 1119 | } |
| 1120 | |
| 1121 | /* |
| 1122 | * fd.io coding-style-patch-verification: ON |
| 1123 | * |
| 1124 | * Local Variables: |
| 1125 | * eval: (c-set-style "gnu") |
| 1126 | * End: |
| 1127 | */ |