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