Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 1 | /* |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2 | *------------------------------------------------------------------ |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 3 | * memory_vlib.c |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 4 | * |
| 5 | * Copyright (c) 2009 Cisco and/or its affiliates. |
| 6 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | * you may not use this file except in compliance with the License. |
| 8 | * You may obtain a copy of the License at: |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, software |
| 13 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | * See the License for the specific language governing permissions and |
| 16 | * limitations under the License. |
| 17 | *------------------------------------------------------------------ |
| 18 | */ |
| 19 | |
| 20 | #include <stdio.h> |
| 21 | #include <stdlib.h> |
| 22 | #include <string.h> |
| 23 | #include <unistd.h> |
| 24 | #include <sys/types.h> |
| 25 | #include <signal.h> |
| 26 | #include <pthread.h> |
| 27 | #include <vppinfra/vec.h> |
| 28 | #include <vppinfra/hash.h> |
| 29 | #include <vppinfra/pool.h> |
| 30 | #include <vppinfra/format.h> |
| 31 | #include <vppinfra/byte_order.h> |
| 32 | #include <vppinfra/elog.h> |
| 33 | #include <stdarg.h> |
| 34 | #include <vlib/vlib.h> |
| 35 | #include <vlib/unix/unix.h> |
| 36 | #include <vlibapi/api.h> |
| 37 | #include <vlibmemory/api.h> |
| 38 | |
| 39 | #define TRACE_VLIB_MEMORY_QUEUE 0 |
| 40 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 41 | #include <vlibmemory/vl_memory_msg_enum.h> /* enumerate all vlib messages */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 42 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 43 | #define vl_typedefs /* define message structures */ |
| 44 | #include <vlibmemory/vl_memory_api_h.h> |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 45 | #undef vl_typedefs |
| 46 | |
| 47 | /* instantiate all the print functions we know about */ |
| 48 | #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__) |
| 49 | #define vl_printfun |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 50 | #include <vlibmemory/vl_memory_api_h.h> |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 51 | #undef vl_printfun |
| 52 | |
| 53 | static inline void * |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 54 | vl_api_memclnt_create_t_print (vl_api_memclnt_create_t * a, void *handle) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 55 | { |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 56 | vl_print (handle, "vl_api_memclnt_create_t:\n"); |
| 57 | vl_print (handle, "name: %s\n", a->name); |
| 58 | vl_print (handle, "input_queue: 0x%wx\n", a->input_queue); |
| 59 | vl_print (handle, "context: %u\n", (unsigned) a->context); |
| 60 | vl_print (handle, "ctx_quota: %ld\n", (long) a->ctx_quota); |
| 61 | return handle; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | static inline void * |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 65 | vl_api_memclnt_delete_t_print (vl_api_memclnt_delete_t * a, void *handle) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 66 | { |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 67 | vl_print (handle, "vl_api_memclnt_delete_t:\n"); |
| 68 | vl_print (handle, "index: %u\n", (unsigned) a->index); |
| 69 | vl_print (handle, "handle: 0x%wx\n", a->handle); |
| 70 | return handle; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | /* instantiate all the endian swap functions we know about */ |
| 74 | #define vl_endianfun |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 75 | #include <vlibmemory/vl_memory_api_h.h> |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 76 | #undef vl_endianfun |
| 77 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 78 | void vl_socket_api_send (vl_api_registration_t * rp, u8 * elem) |
| 79 | __attribute__ ((weak)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 80 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 81 | void |
| 82 | vl_socket_api_send (vl_api_registration_t * rp, u8 * elem) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 83 | { |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 84 | static int count; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 85 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 86 | if (count++ < 5) |
| 87 | clib_warning ("need to link against -lvlibsocket, msg not sent!"); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 88 | } |
| 89 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 90 | void |
| 91 | vl_msg_api_send (vl_api_registration_t * rp, u8 * elem) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 92 | { |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 93 | if (PREDICT_FALSE (rp->registration_type > REGISTRATION_TYPE_SHMEM)) |
| 94 | { |
| 95 | vl_socket_api_send (rp, elem); |
| 96 | } |
| 97 | else |
| 98 | { |
| 99 | vl_msg_api_send_shmem (rp->vl_input_queue, elem); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 100 | } |
| 101 | } |
| 102 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 103 | int vl_msg_api_version_check (vl_api_memclnt_create_t * mp) |
| 104 | __attribute__ ((weak)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 105 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 106 | int |
| 107 | vl_msg_api_version_check (vl_api_memclnt_create_t * mp) |
| 108 | { |
| 109 | return 0; |
| 110 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 111 | |
| 112 | /* |
| 113 | * vl_api_memclnt_create_t_handler |
| 114 | */ |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 115 | void |
| 116 | vl_api_memclnt_create_t_handler (vl_api_memclnt_create_t * mp) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 117 | { |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 118 | vl_api_registration_t **regpp; |
| 119 | vl_api_registration_t *regp; |
| 120 | vl_api_memclnt_create_reply_t *rp; |
| 121 | svm_region_t *svm; |
| 122 | unix_shared_memory_queue_t *q; |
| 123 | int rv; |
| 124 | void *oldheap; |
| 125 | api_main_t *am = &api_main; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 126 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 127 | /* Indicate API version mismatch if appropriate */ |
| 128 | rv = vl_msg_api_version_check (mp); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 129 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 130 | /* |
| 131 | * This is tortured. Maintain a vlib-address-space private |
| 132 | * pool of client registrations. We use the shared-memory virtual |
| 133 | * address of client structure as a handle, to allow direct |
| 134 | * manipulation of context quota vbls from the client library. |
| 135 | * |
| 136 | * This scheme causes trouble w/ API message trace replay, since |
| 137 | * some random VA from clib_mem_alloc() certainly won't |
| 138 | * occur in the Linux sim. The (very) few places |
| 139 | * that care need to use the pool index. |
| 140 | * |
| 141 | * Putting the registration object(s) into a pool in shared memory and |
| 142 | * using the pool index as a handle seems like a great idea. |
| 143 | * Unfortunately, each and every reference to that pool would need |
| 144 | * to be protected by a mutex: |
| 145 | * |
| 146 | * Client VLIB |
| 147 | * ------ ---- |
| 148 | * convert pool index to |
| 149 | * pointer. |
| 150 | * <deschedule> |
| 151 | * expand pool |
| 152 | * <deschedule> |
| 153 | * kaboom! |
| 154 | */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 155 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 156 | pool_get (am->vl_clients, regpp); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 157 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 158 | svm = am->vlib_rp; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 159 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 160 | pthread_mutex_lock (&svm->mutex); |
| 161 | oldheap = svm_push_data_heap (svm); |
| 162 | *regpp = clib_mem_alloc (sizeof (vl_api_registration_t)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 163 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 164 | regp = *regpp; |
| 165 | memset (regp, 0, sizeof (*regp)); |
| 166 | regp->registration_type = REGISTRATION_TYPE_SHMEM; |
| 167 | regp->vl_api_registration_pool_index = regpp - am->vl_clients; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 168 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 169 | q = regp->vl_input_queue = (unix_shared_memory_queue_t *) (uword) |
| 170 | mp->input_queue; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 171 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 172 | regp->name = format (0, "%s", mp->name); |
| 173 | vec_add1 (regp->name, 0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 174 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 175 | pthread_mutex_unlock (&svm->mutex); |
| 176 | svm_pop_heap (oldheap); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 177 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 178 | rp = vl_msg_api_alloc (sizeof (*rp)); |
| 179 | rp->_vl_msg_id = ntohs (VL_API_MEMCLNT_CREATE_REPLY); |
| 180 | rp->handle = (uword) regp; |
| 181 | rp->index = vl_msg_api_handle_from_index_and_epoch |
| 182 | (regp->vl_api_registration_pool_index, |
| 183 | am->shmem_hdr->application_restarts); |
| 184 | rp->context = mp->context; |
| 185 | rp->response = ntohl (rv); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 186 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 187 | vl_msg_api_send_shmem (q, (u8 *) & rp); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | /* Application callback to clean up leftover registrations from this client */ |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 191 | int vl_api_memclnt_delete_callback (u32 client_index) __attribute__ ((weak)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 192 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 193 | int |
| 194 | vl_api_memclnt_delete_callback (u32 client_index) |
| 195 | { |
| 196 | return 0; |
| 197 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 198 | |
| 199 | /* |
| 200 | * vl_api_memclnt_delete_t_handler |
| 201 | */ |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 202 | void |
| 203 | vl_api_memclnt_delete_t_handler (vl_api_memclnt_delete_t * mp) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 204 | { |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 205 | vl_api_registration_t **regpp; |
| 206 | vl_api_registration_t *regp; |
| 207 | vl_api_memclnt_delete_reply_t *rp; |
| 208 | svm_region_t *svm; |
| 209 | void *oldheap; |
| 210 | api_main_t *am = &api_main; |
| 211 | u32 handle, client_index, epoch; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 212 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 213 | handle = mp->index; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 214 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 215 | if (vl_api_memclnt_delete_callback (handle)) |
| 216 | return; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 217 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 218 | epoch = vl_msg_api_handle_get_epoch (handle); |
| 219 | client_index = vl_msg_api_handle_get_index (handle); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 220 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 221 | if (epoch != (am->shmem_hdr->application_restarts & VL_API_EPOCH_MASK)) |
| 222 | { |
| 223 | clib_warning |
| 224 | ("Stale clnt delete index %d old epoch %d cur epoch %d", |
| 225 | client_index, epoch, |
| 226 | (am->shmem_hdr->application_restarts & VL_API_EPOCH_MASK)); |
| 227 | return; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 228 | } |
| 229 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 230 | regpp = am->vl_clients + client_index; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 231 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 232 | if (!pool_is_free (am->vl_clients, regpp)) |
| 233 | { |
| 234 | regp = *regpp; |
| 235 | svm = am->vlib_rp; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 236 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 237 | /* $$$ check the input queue for e.g. punted sf's */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 238 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 239 | rp = vl_msg_api_alloc (sizeof (*rp)); |
| 240 | rp->_vl_msg_id = ntohs (VL_API_MEMCLNT_DELETE_REPLY); |
| 241 | rp->handle = mp->handle; |
| 242 | rp->response = 1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 243 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 244 | vl_msg_api_send_shmem (regp->vl_input_queue, (u8 *) & rp); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 245 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 246 | if (client_index != regp->vl_api_registration_pool_index) |
| 247 | { |
| 248 | clib_warning ("mismatch client_index %d pool_index %d", |
| 249 | client_index, regp->vl_api_registration_pool_index); |
| 250 | vl_msg_api_free (rp); |
| 251 | return; |
| 252 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 253 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 254 | /* No dangling references, please */ |
| 255 | *regpp = 0; |
| 256 | |
| 257 | pool_put_index (am->vl_clients, regp->vl_api_registration_pool_index); |
| 258 | |
| 259 | pthread_mutex_lock (&svm->mutex); |
| 260 | oldheap = svm_push_data_heap (svm); |
| 261 | /* Poison the old registration */ |
| 262 | memset (regp, 0xF1, sizeof (*regp)); |
| 263 | clib_mem_free (regp); |
| 264 | pthread_mutex_unlock (&svm->mutex); |
| 265 | svm_pop_heap (oldheap); |
| 266 | } |
| 267 | else |
| 268 | { |
| 269 | clib_warning ("unknown client ID %d", mp->index); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 270 | } |
| 271 | } |
| 272 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 273 | void |
| 274 | vl_api_get_first_msg_id_t_handler (vl_api_get_first_msg_id_t * mp) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 275 | { |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 276 | vl_api_get_first_msg_id_reply_t *rmp; |
| 277 | unix_shared_memory_queue_t *q; |
| 278 | uword *p; |
| 279 | api_main_t *am = &api_main; |
| 280 | vl_api_msg_range_t *rp; |
| 281 | u8 name[64]; |
| 282 | u16 first_msg_id = ~0; |
| 283 | int rv = -7; /* VNET_API_ERROR_INVALID_VALUE */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 284 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 285 | q = vl_api_client_index_to_input_queue (mp->client_index); |
| 286 | if (!q) |
| 287 | return; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 288 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 289 | if (am->msg_range_by_name == 0) |
| 290 | goto out; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 291 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 292 | strncpy ((char *) name, (char *) mp->name, ARRAY_LEN (name) - 1); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 293 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 294 | p = hash_get_mem (am->msg_range_by_name, name); |
| 295 | if (p == 0) |
| 296 | goto out; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 297 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 298 | rp = vec_elt_at_index (am->msg_ranges, p[0]); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 299 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 300 | first_msg_id = rp->first_msg_id; |
| 301 | rv = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 302 | |
| 303 | out: |
| 304 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 305 | rmp = vl_msg_api_alloc (sizeof (*rmp)); |
| 306 | rmp->_vl_msg_id = ntohs (VL_API_GET_FIRST_MSG_ID_REPLY); |
| 307 | rmp->context = mp->context; |
| 308 | rmp->retval = ntohl (rv); |
| 309 | rmp->first_msg_id = ntohs (first_msg_id); |
| 310 | vl_msg_api_send_shmem (q, (u8 *) & rmp); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 311 | } |
| 312 | |
| 313 | #define foreach_vlib_api_msg \ |
| 314 | _(MEMCLNT_CREATE, memclnt_create) \ |
| 315 | _(MEMCLNT_DELETE, memclnt_delete) \ |
| 316 | _(GET_FIRST_MSG_ID, get_first_msg_id) |
| 317 | |
| 318 | /* |
| 319 | * vl_api_init |
| 320 | */ |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 321 | static int |
| 322 | memory_api_init (char *region_name) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 323 | { |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 324 | int rv; |
| 325 | vl_msg_api_msg_config_t cfg; |
| 326 | vl_msg_api_msg_config_t *c = &cfg; |
| 327 | |
| 328 | if ((rv = vl_map_shmem (region_name, 1 /* is_vlib */ )) < 0) |
| 329 | return rv; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 330 | |
| 331 | #define _(N,n) do { \ |
| 332 | c->id = VL_API_##N; \ |
| 333 | c->name = #n; \ |
| 334 | c->handler = vl_api_##n##_t_handler; \ |
| 335 | c->cleanup = vl_noop_handler; \ |
| 336 | c->endian = vl_api_##n##_t_endian; \ |
| 337 | c->print = vl_api_##n##_t_print; \ |
| 338 | c->size = sizeof(vl_api_##n##_t); \ |
| 339 | c->traced = 1; /* trace, so these msgs print */ \ |
| 340 | c->replay = 0; /* don't replay client create/delete msgs */ \ |
| 341 | vl_msg_api_config(c);} while (0); |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 342 | |
| 343 | foreach_vlib_api_msg; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 344 | #undef _ |
| 345 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 346 | return 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 347 | } |
| 348 | |
| 349 | #define foreach_histogram_bucket \ |
| 350 | _(400) \ |
| 351 | _(200) \ |
| 352 | _(100) \ |
| 353 | _(10) |
| 354 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 355 | typedef enum |
| 356 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 357 | #define _(n) SLEEP_##n##_US, |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 358 | foreach_histogram_bucket |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 359 | #undef _ |
| 360 | SLEEP_N_BUCKETS, |
| 361 | } histogram_index_t; |
| 362 | |
| 363 | static u64 vector_rate_histogram[SLEEP_N_BUCKETS]; |
| 364 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 365 | static void memclnt_queue_callback (vlib_main_t * vm); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 366 | |
| 367 | static uword |
| 368 | memclnt_process (vlib_main_t * vm, |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 369 | vlib_node_runtime_t * node, vlib_frame_t * f) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 370 | { |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 371 | uword mp; |
| 372 | vl_shmem_hdr_t *shm; |
| 373 | unix_shared_memory_queue_t *q; |
| 374 | clib_error_t *e; |
| 375 | int rv; |
| 376 | api_main_t *am = &api_main; |
| 377 | f64 dead_client_scan_time; |
| 378 | f64 sleep_time, start_time; |
| 379 | f64 vector_rate; |
| 380 | |
| 381 | vlib_set_queue_signal_callback (vm, memclnt_queue_callback); |
| 382 | |
| 383 | if ((rv = memory_api_init (am->region_name)) < 0) |
| 384 | { |
| 385 | clib_warning ("memory_api_init returned %d, wait for godot...", rv); |
| 386 | vlib_process_suspend (vm, 1e70); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 387 | } |
| 388 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 389 | shm = am->shmem_hdr; |
| 390 | ASSERT (shm); |
| 391 | q = shm->vl_input_queue; |
| 392 | ASSERT (q); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 393 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 394 | e = vlib_call_init_exit_functions |
| 395 | (vm, vm->api_init_function_registrations, 1 /* call_once */ ); |
| 396 | if (e) |
| 397 | clib_error_report (e); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 398 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 399 | sleep_time = 20.0; |
| 400 | dead_client_scan_time = vlib_time_now (vm) + 20.0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 401 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 402 | /* $$$ pay attention to frame size, control CPU usage */ |
| 403 | while (1) |
| 404 | { |
| 405 | uword event_type __attribute__ ((unused)); |
| 406 | i8 *headp; |
| 407 | int need_broadcast; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 408 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 409 | /* |
| 410 | * There's a reason for checking the queue before |
| 411 | * sleeping. If the vlib application crashes, it's entirely |
| 412 | * possible for a client to enqueue a connect request |
| 413 | * during the process restart interval. |
| 414 | * |
| 415 | * Unless some force of physics causes the new incarnation |
| 416 | * of the application to process the request, the client will |
| 417 | * sit and wait for Godot... |
| 418 | */ |
| 419 | vector_rate = vlib_last_vector_length_per_node (vm); |
| 420 | start_time = vlib_time_now (vm); |
| 421 | while (1) |
| 422 | { |
| 423 | pthread_mutex_lock (&q->mutex); |
| 424 | if (q->cursize == 0) |
| 425 | { |
| 426 | vm->api_queue_nonempty = 0; |
| 427 | pthread_mutex_unlock (&q->mutex); |
| 428 | |
| 429 | if (TRACE_VLIB_MEMORY_QUEUE) |
| 430 | { |
| 431 | /* *INDENT-OFF* */ |
| 432 | ELOG_TYPE_DECLARE (e) = |
| 433 | { |
| 434 | .format = "q-underflow: len %d", |
| 435 | .format_args = "i4", |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 436 | }; |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 437 | /* *INDENT-ON* */ |
| 438 | struct |
| 439 | { |
| 440 | u32 len; |
| 441 | } *ed; |
| 442 | ed = ELOG_DATA (&vm->elog_main, e); |
| 443 | ed->len = 0; |
| 444 | } |
| 445 | sleep_time = 20.0; |
| 446 | break; |
| 447 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 448 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 449 | headp = (i8 *) (q->data + sizeof (uword) * q->head); |
| 450 | clib_memcpy (&mp, headp, sizeof (uword)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 451 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 452 | q->head++; |
| 453 | need_broadcast = (q->cursize == q->maxsize / 2); |
| 454 | q->cursize--; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 455 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 456 | if (PREDICT_FALSE (q->head == q->maxsize)) |
| 457 | q->head = 0; |
| 458 | pthread_mutex_unlock (&q->mutex); |
| 459 | if (need_broadcast) |
| 460 | (void) pthread_cond_broadcast (&q->condvar); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 461 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 462 | vl_msg_api_handler_with_vm_node (am, (void *) mp, vm, node); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 463 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 464 | /* Allow no more than 10us without a pause */ |
| 465 | if (vlib_time_now (vm) > start_time + 10e-6) |
| 466 | { |
| 467 | int index = SLEEP_400_US; |
| 468 | if (vector_rate > 40.0) |
| 469 | sleep_time = 400e-6; |
| 470 | else if (vector_rate > 20.0) |
| 471 | { |
| 472 | index = SLEEP_200_US; |
| 473 | sleep_time = 200e-6; |
| 474 | } |
| 475 | else if (vector_rate >= 1.0) |
| 476 | { |
| 477 | index = SLEEP_100_US; |
| 478 | sleep_time = 100e-6; |
| 479 | } |
| 480 | else |
| 481 | { |
| 482 | index = SLEEP_10_US; |
| 483 | sleep_time = 10e-6; |
| 484 | } |
| 485 | vector_rate_histogram[index] += 1; |
| 486 | break; |
| 487 | } |
| 488 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 489 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 490 | event_type = vlib_process_wait_for_event_or_clock (vm, sleep_time); |
| 491 | vm->queue_signal_pending = 0; |
| 492 | vlib_process_get_events (vm, 0 /* event_data */ ); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 493 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 494 | if (vlib_time_now (vm) > dead_client_scan_time) |
| 495 | { |
| 496 | vl_api_registration_t **regpp; |
| 497 | vl_api_registration_t *regp; |
| 498 | unix_shared_memory_queue_t *q; |
| 499 | static u32 *dead_indices; |
| 500 | static u32 *confused_indices; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 501 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 502 | vec_reset_length (dead_indices); |
| 503 | vec_reset_length (confused_indices); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 504 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 505 | /* *INDENT-OFF* */ |
| 506 | pool_foreach (regpp, am->vl_clients, |
| 507 | ({ |
| 508 | regp = *regpp; |
| 509 | if (regp) |
| 510 | { |
| 511 | q = regp->vl_input_queue; |
| 512 | if (kill (q->consumer_pid, 0) < 0) |
| 513 | { |
| 514 | vec_add1(dead_indices, regpp - am->vl_clients); |
| 515 | } |
| 516 | } |
| 517 | else |
| 518 | { |
| 519 | clib_warning ("NULL client registration index %d", |
| 520 | regpp - am->vl_clients); |
| 521 | vec_add1 (confused_indices, regpp - am->vl_clients); |
| 522 | } |
| 523 | })); |
| 524 | /* *INDENT-ON* */ |
| 525 | /* This should "never happen," but if it does, fix it... */ |
| 526 | if (PREDICT_FALSE (vec_len (confused_indices) > 0)) |
| 527 | { |
| 528 | int i; |
| 529 | for (i = 0; i < vec_len (confused_indices); i++) |
| 530 | { |
| 531 | pool_put_index (am->vl_clients, confused_indices[i]); |
| 532 | } |
| 533 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 534 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 535 | if (PREDICT_FALSE (vec_len (dead_indices) > 0)) |
| 536 | { |
| 537 | int i; |
| 538 | svm_region_t *svm; |
| 539 | void *oldheap; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 540 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 541 | /* Allow the application to clean up its registrations */ |
| 542 | for (i = 0; i < vec_len (dead_indices); i++) |
| 543 | { |
| 544 | regpp = pool_elt_at_index (am->vl_clients, dead_indices[i]); |
| 545 | if (regpp) |
| 546 | { |
| 547 | u32 handle; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 548 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 549 | handle = vl_msg_api_handle_from_index_and_epoch |
| 550 | (dead_indices[i], shm->application_restarts); |
| 551 | (void) vl_api_memclnt_delete_callback (handle); |
| 552 | } |
| 553 | } |
| 554 | |
| 555 | svm = am->vlib_rp; |
| 556 | pthread_mutex_lock (&svm->mutex); |
| 557 | oldheap = svm_push_data_heap (svm); |
| 558 | |
| 559 | for (i = 0; i < vec_len (dead_indices); i++) |
| 560 | { |
| 561 | regpp = pool_elt_at_index (am->vl_clients, dead_indices[i]); |
| 562 | if (regpp) |
| 563 | { |
| 564 | /* Poison the old registration */ |
| 565 | memset (*regpp, 0xF3, sizeof (**regpp)); |
| 566 | clib_mem_free (*regpp); |
| 567 | /* no dangling references, please */ |
| 568 | *regpp = 0; |
| 569 | } |
| 570 | else |
| 571 | { |
| 572 | svm_pop_heap (oldheap); |
| 573 | clib_warning ("Duplicate free, client index %d", |
| 574 | regpp - am->vl_clients); |
| 575 | oldheap = svm_push_data_heap (svm); |
| 576 | } |
| 577 | } |
| 578 | |
| 579 | svm_client_scan_this_region_nolock (am->vlib_rp); |
| 580 | |
| 581 | pthread_mutex_unlock (&svm->mutex); |
| 582 | svm_pop_heap (oldheap); |
| 583 | for (i = 0; i < vec_len (dead_indices); i++) |
| 584 | pool_put_index (am->vl_clients, dead_indices[i]); |
| 585 | } |
| 586 | |
| 587 | dead_client_scan_time = vlib_time_now (vm) + 20.0; |
| 588 | } |
| 589 | |
| 590 | if (TRACE_VLIB_MEMORY_QUEUE) |
| 591 | { |
| 592 | /* *INDENT-OFF* */ |
| 593 | ELOG_TYPE_DECLARE (e) = { |
| 594 | .format = "q-awake: len %d", |
| 595 | .format_args = "i4", |
| 596 | }; |
| 597 | /* *INDENT-ON* */ |
| 598 | struct |
| 599 | { |
| 600 | u32 len; |
| 601 | } *ed; |
| 602 | ed = ELOG_DATA (&vm->elog_main, e); |
| 603 | ed->len = q->cursize; |
| 604 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 605 | } |
| 606 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 607 | return 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 608 | } |
| 609 | |
| 610 | static clib_error_t * |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 611 | vl_api_show_histogram_command (vlib_main_t * vm, |
| 612 | unformat_input_t * input, |
| 613 | vlib_cli_command_t * cli_cmd) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 614 | { |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 615 | u64 total_counts = 0; |
| 616 | int i; |
| 617 | |
| 618 | for (i = 0; i < SLEEP_N_BUCKETS; i++) |
| 619 | { |
| 620 | total_counts += vector_rate_histogram[i]; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 621 | } |
| 622 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 623 | if (total_counts == 0) |
| 624 | { |
| 625 | vlib_cli_output (vm, "No control-plane activity."); |
| 626 | return 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 627 | } |
| 628 | |
| 629 | #define _(n) \ |
| 630 | do { \ |
| 631 | f64 percent; \ |
| 632 | percent = ((f64) vector_rate_histogram[SLEEP_##n##_US]) \ |
| 633 | / (f64) total_counts; \ |
| 634 | percent *= 100.0; \ |
| 635 | vlib_cli_output (vm, "Sleep %3d us: %llu, %.2f%%",n, \ |
| 636 | vector_rate_histogram[SLEEP_##n##_US], \ |
| 637 | percent); \ |
| 638 | } while (0); |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 639 | foreach_histogram_bucket; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 640 | #undef _ |
| 641 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 642 | return 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 643 | } |
| 644 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 645 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 646 | VLIB_CLI_COMMAND (cli_show_api_histogram_command, static) = { |
Calvin | 1664937 | 2016-07-28 13:52:05 -0400 | [diff] [blame] | 647 | .path = "show api histogram", |
| 648 | .short_help = "show api histogram", |
| 649 | .function = vl_api_show_histogram_command, |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 650 | }; |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 651 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 652 | |
| 653 | static clib_error_t * |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 654 | vl_api_clear_histogram_command (vlib_main_t * vm, |
| 655 | unformat_input_t * input, |
| 656 | vlib_cli_command_t * cli_cmd) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 657 | { |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 658 | int i; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 659 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 660 | for (i = 0; i < SLEEP_N_BUCKETS; i++) |
| 661 | vector_rate_histogram[i] = 0; |
| 662 | return 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 663 | } |
| 664 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 665 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 666 | VLIB_CLI_COMMAND (cli_clear_api_histogram_command, static) = { |
| 667 | .path = "clear api histogram", |
| 668 | .short_help = "clear api histogram", |
| 669 | .function = vl_api_clear_histogram_command, |
| 670 | }; |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 671 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 672 | |
| 673 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 674 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 675 | VLIB_REGISTER_NODE (memclnt_node,static) = { |
| 676 | .function = memclnt_process, |
| 677 | .type = VLIB_NODE_TYPE_PROCESS, |
| 678 | .name = "api-rx-from-ring", |
| 679 | .state = VLIB_NODE_STATE_DISABLED, |
| 680 | }; |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 681 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 682 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 683 | static void |
| 684 | memclnt_queue_callback (vlib_main_t * vm) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 685 | { |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 686 | static volatile int *cursizep; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 687 | |
Dave Barach | 16c75df | 2016-05-31 14:05:46 -0400 | [diff] [blame] | 688 | if (PREDICT_FALSE (cursizep == 0)) |
| 689 | { |
| 690 | api_main_t *am = &api_main; |
| 691 | vl_shmem_hdr_t *shmem_hdr = am->shmem_hdr; |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 692 | unix_shared_memory_queue_t *q; |
| 693 | |
Dave Barach | 16c75df | 2016-05-31 14:05:46 -0400 | [diff] [blame] | 694 | if (shmem_hdr == 0) |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 695 | return; |
| 696 | |
Dave Barach | 16c75df | 2016-05-31 14:05:46 -0400 | [diff] [blame] | 697 | q = shmem_hdr->vl_input_queue; |
| 698 | if (q == 0) |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 699 | return; |
Dave Barach | 16c75df | 2016-05-31 14:05:46 -0400 | [diff] [blame] | 700 | cursizep = &q->cursize; |
| 701 | } |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 702 | |
Dave Barach | 16c75df | 2016-05-31 14:05:46 -0400 | [diff] [blame] | 703 | if (*cursizep >= 1) |
| 704 | { |
| 705 | vm->queue_signal_pending = 1; |
| 706 | vm->api_queue_nonempty = 1; |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 707 | vlib_process_signal_event (vm, memclnt_node.index, |
| 708 | /* event_type */ 0, /* event_data */ 0); |
Dave Barach | 16c75df | 2016-05-31 14:05:46 -0400 | [diff] [blame] | 709 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 710 | } |
| 711 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 712 | void |
| 713 | vl_enable_disable_memory_api (vlib_main_t * vm, int enable) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 714 | { |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 715 | vlib_node_set_state (vm, memclnt_node.index, |
| 716 | (enable |
| 717 | ? VLIB_NODE_STATE_POLLING |
| 718 | : VLIB_NODE_STATE_DISABLED)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 719 | } |
| 720 | |
| 721 | static uword |
| 722 | api_rx_from_node (vlib_main_t * vm, |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 723 | vlib_node_runtime_t * node, vlib_frame_t * frame) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 724 | { |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 725 | uword n_packets = frame->n_vectors; |
| 726 | uword n_left_from; |
| 727 | u32 *from; |
| 728 | static u8 *long_msg; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 729 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 730 | vec_validate (long_msg, 4095); |
| 731 | n_left_from = frame->n_vectors; |
| 732 | from = vlib_frame_args (frame); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 733 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 734 | while (n_left_from > 0) |
| 735 | { |
| 736 | u32 bi0; |
| 737 | vlib_buffer_t *b0; |
| 738 | void *msg; |
| 739 | uword msg_len; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 740 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 741 | bi0 = from[0]; |
| 742 | b0 = vlib_get_buffer (vm, bi0); |
| 743 | from += 1; |
| 744 | n_left_from -= 1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 745 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 746 | msg = b0->data + b0->current_data; |
| 747 | msg_len = b0->current_length; |
| 748 | if (b0->flags & VLIB_BUFFER_NEXT_PRESENT) |
| 749 | { |
| 750 | ASSERT (long_msg != 0); |
| 751 | _vec_len (long_msg) = 0; |
| 752 | vec_add (long_msg, msg, msg_len); |
| 753 | while (b0->flags & VLIB_BUFFER_NEXT_PRESENT) |
| 754 | { |
| 755 | b0 = vlib_get_buffer (vm, b0->next_buffer); |
| 756 | msg = b0->data + b0->current_data; |
| 757 | msg_len = b0->current_length; |
| 758 | vec_add (long_msg, msg, msg_len); |
| 759 | } |
| 760 | msg = long_msg; |
| 761 | } |
| 762 | vl_msg_api_handler_no_trace_no_free (msg); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 763 | } |
| 764 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 765 | /* Free what we've been given. */ |
| 766 | vlib_buffer_free (vm, vlib_frame_args (frame), n_packets); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 767 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 768 | return n_packets; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 769 | } |
| 770 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 771 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 772 | VLIB_REGISTER_NODE (api_rx_from_node_node,static) = { |
| 773 | .function = api_rx_from_node, |
| 774 | .type = VLIB_NODE_TYPE_INTERNAL, |
| 775 | .vector_size = 4, |
| 776 | .name = "api-rx-from-node", |
| 777 | }; |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 778 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 779 | |
| 780 | static clib_error_t * |
| 781 | setup_memclnt_exit (vlib_main_t * vm) |
| 782 | { |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 783 | atexit (vl_unmap_shmem); |
| 784 | return 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 785 | } |
| 786 | |
| 787 | VLIB_INIT_FUNCTION (setup_memclnt_exit); |
| 788 | |
| 789 | |
| 790 | static clib_error_t * |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 791 | vl_api_ring_command (vlib_main_t * vm, |
| 792 | unformat_input_t * input, vlib_cli_command_t * cli_cmd) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 793 | { |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 794 | int i; |
| 795 | ring_alloc_t *ap; |
| 796 | vl_shmem_hdr_t *shmem_hdr; |
| 797 | api_main_t *am = &api_main; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 798 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 799 | shmem_hdr = am->shmem_hdr; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 800 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 801 | if (shmem_hdr == 0) |
| 802 | { |
| 803 | vlib_cli_output (vm, "Shared memory segment not initialized...\n"); |
| 804 | return 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 805 | } |
| 806 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 807 | vlib_cli_output (vm, "%8s %8s %8s %8s %8s\n", |
| 808 | "Owner", "Size", "Nitems", "Hits", "Misses"); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 809 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 810 | ap = shmem_hdr->vl_rings; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 811 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 812 | for (i = 0; i < vec_len (shmem_hdr->vl_rings); i++) |
| 813 | { |
| 814 | vlib_cli_output (vm, "%8s %8d %8d %8d %8d\n", |
| 815 | "vlib", ap->size, ap->nitems, ap->hits, ap->misses); |
| 816 | ap++; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 817 | } |
| 818 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 819 | ap = shmem_hdr->client_rings; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 820 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 821 | for (i = 0; i < vec_len (shmem_hdr->client_rings); i++) |
| 822 | { |
| 823 | vlib_cli_output (vm, "%8s %8d %8d %8d %8d\n", |
| 824 | "clnt", ap->size, ap->nitems, ap->hits, ap->misses); |
| 825 | ap++; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 826 | } |
| 827 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 828 | vlib_cli_output (vm, "%d ring miss fallback allocations\n", |
| 829 | am->ring_misses); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 830 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 831 | vlib_cli_output (vm, "%d application restarts, %d reclaimed msgs\n", |
| 832 | shmem_hdr->application_restarts, |
| 833 | shmem_hdr->restart_reclaims); |
| 834 | return 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 835 | } |
| 836 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 837 | void dump_socket_clients (vlib_main_t * vm, api_main_t * am) |
| 838 | __attribute__ ((weak)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 839 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 840 | void |
| 841 | dump_socket_clients (vlib_main_t * vm, api_main_t * am) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 842 | { |
| 843 | } |
| 844 | |
| 845 | static clib_error_t * |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 846 | vl_api_client_command (vlib_main_t * vm, |
| 847 | unformat_input_t * input, vlib_cli_command_t * cli_cmd) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 848 | { |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 849 | vl_api_registration_t **regpp, *regp; |
| 850 | unix_shared_memory_queue_t *q; |
| 851 | char *health; |
| 852 | api_main_t *am = &api_main; |
| 853 | u32 *confused_indices = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 854 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 855 | if (!pool_elts (am->vl_clients)) |
| 856 | goto socket_clients; |
| 857 | vlib_cli_output (vm, "Shared memory clients"); |
| 858 | vlib_cli_output (vm, "%16s %8s %14s %18s %s", |
| 859 | "Name", "PID", "Queue Length", "Queue VA", "Health"); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 860 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 861 | /* *INDENT-OFF* */ |
| 862 | pool_foreach (regpp, am->vl_clients, |
| 863 | ({ |
| 864 | regp = *regpp; |
| 865 | |
| 866 | if (regp) |
| 867 | { |
| 868 | q = regp->vl_input_queue; |
| 869 | if (kill (q->consumer_pid, 0) < 0) |
| 870 | { |
| 871 | health = "DEAD"; |
| 872 | } |
| 873 | else |
| 874 | { |
| 875 | health = "alive"; |
| 876 | } |
| 877 | vlib_cli_output (vm, "%16s %8d %14d 0x%016llx %s\n", |
| 878 | regp->name, q->consumer_pid, q->cursize, |
| 879 | q, health); |
| 880 | } |
| 881 | else |
| 882 | { |
| 883 | clib_warning ("NULL client registration index %d", |
| 884 | regpp - am->vl_clients); |
| 885 | vec_add1 (confused_indices, regpp - am->vl_clients); |
| 886 | } |
| 887 | })); |
| 888 | /* *INDENT-ON* */ |
| 889 | |
| 890 | /* This should "never happen," but if it does, fix it... */ |
| 891 | if (PREDICT_FALSE (vec_len (confused_indices) > 0)) |
| 892 | { |
| 893 | int i; |
| 894 | for (i = 0; i < vec_len (confused_indices); i++) |
| 895 | { |
| 896 | pool_put_index (am->vl_clients, confused_indices[i]); |
| 897 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 898 | } |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 899 | vec_free (confused_indices); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 900 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 901 | if (am->missing_clients) |
| 902 | vlib_cli_output (vm, "%u messages with missing clients", |
| 903 | am->missing_clients); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 904 | socket_clients: |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 905 | dump_socket_clients (vm, am); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 906 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 907 | return 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 908 | } |
| 909 | |
Calvin | 1664937 | 2016-07-28 13:52:05 -0400 | [diff] [blame] | 910 | static clib_error_t * |
| 911 | vl_api_status_command (vlib_main_t * vm, |
| 912 | unformat_input_t * input, vlib_cli_command_t * cli_cmd) |
| 913 | { |
| 914 | api_main_t *am = &api_main; |
| 915 | |
| 916 | // check if rx_trace and tx_trace are not null pointers |
| 917 | |
| 918 | if (am->rx_trace == 0) |
| 919 | { |
| 920 | vlib_cli_output (vm, "RX Trace disabled\n"); |
| 921 | } |
| 922 | else |
| 923 | { |
| 924 | if (am->rx_trace->enabled == 0) |
| 925 | vlib_cli_output (vm, "RX Trace disabled\n"); |
| 926 | else |
| 927 | vlib_cli_output (vm, "RX Trace enabled\n"); |
| 928 | } |
| 929 | |
| 930 | if (am->tx_trace == 0) |
| 931 | { |
| 932 | vlib_cli_output (vm, "TX Trace disabled\n"); |
| 933 | } |
| 934 | else |
| 935 | { |
| 936 | if (am->tx_trace->enabled == 0) |
| 937 | vlib_cli_output (vm, "TX Trace disabled\n"); |
| 938 | else |
| 939 | vlib_cli_output (vm, "TX Trace enabled\n"); |
| 940 | } |
| 941 | |
| 942 | return 0; |
| 943 | } |
| 944 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 945 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 946 | VLIB_CLI_COMMAND (cli_show_api_command, static) = { |
| 947 | .path = "show api", |
| 948 | .short_help = "Show API information", |
| 949 | }; |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 950 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 951 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 952 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 953 | VLIB_CLI_COMMAND (cli_show_api_ring_command, static) = { |
| 954 | .path = "show api ring-stats", |
| 955 | .short_help = "Message ring statistics", |
| 956 | .function = vl_api_ring_command, |
| 957 | }; |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 958 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 959 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 960 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 961 | VLIB_CLI_COMMAND (cli_show_api_clients_command, static) = { |
| 962 | .path = "show api clients", |
| 963 | .short_help = "Client information", |
| 964 | .function = vl_api_client_command, |
| 965 | }; |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 966 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 967 | |
Calvin | 1664937 | 2016-07-28 13:52:05 -0400 | [diff] [blame] | 968 | /* *INDENT-OFF* */ |
| 969 | VLIB_CLI_COMMAND (cli_show_api_status_command, static) = { |
| 970 | .path = "show api status", |
| 971 | .short_help = "Show API trace status", |
| 972 | .function = vl_api_status_command, |
| 973 | }; |
| 974 | /* *INDENT-ON* */ |
| 975 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 976 | static clib_error_t * |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 977 | vl_api_message_table_command (vlib_main_t * vm, |
| 978 | unformat_input_t * input, |
| 979 | vlib_cli_command_t * cli_cmd) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 980 | { |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 981 | api_main_t *am = &api_main; |
| 982 | int i; |
| 983 | int verbose = 0; |
| 984 | |
| 985 | if (unformat (input, "verbose")) |
| 986 | verbose = 1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 987 | |
| 988 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 989 | if (verbose == 0) |
| 990 | vlib_cli_output (vm, "%-4s %s", "ID", "Name"); |
| 991 | else |
| 992 | vlib_cli_output (vm, "%-4s %-40s %6s %7s", "ID", "Name", "Bounce", |
| 993 | "MP-safe"); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 994 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 995 | for (i = 1; i < vec_len (am->msg_names); i++) |
| 996 | { |
| 997 | if (verbose == 0) |
| 998 | { |
| 999 | vlib_cli_output (vm, "%-4d %s", i, |
| 1000 | am->msg_names[i] ? am->msg_names[i] : |
| 1001 | " [no handler]"); |
| 1002 | } |
| 1003 | else |
| 1004 | { |
| 1005 | vlib_cli_output (vm, "%-4d %-40s %6d %7d", i, |
| 1006 | am->msg_names[i] ? am->msg_names[i] : |
| 1007 | " [no handler]", am->message_bounce[i], |
| 1008 | am->is_mp_safe[i]); |
| 1009 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1010 | } |
| 1011 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 1012 | return 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1013 | } |
| 1014 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 1015 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1016 | VLIB_CLI_COMMAND (cli_show_api_message_table_command, static) = { |
| 1017 | .path = "show api message-table", |
| 1018 | .short_help = "Message Table", |
| 1019 | .function = vl_api_message_table_command, |
| 1020 | }; |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 1021 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1022 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 1023 | void |
| 1024 | vl_api_trace_print_file_cmd (vlib_main_t * vm, u32 first, u32 last, |
| 1025 | u8 * filename) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1026 | { |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 1027 | FILE *fp; |
| 1028 | static vl_api_trace_t *tp = 0; |
| 1029 | int endian_swap = 0; |
| 1030 | u32 i; |
| 1031 | u16 msg_id; |
| 1032 | static u8 *msg_buf = 0; |
| 1033 | void (*endian_fp) (void *); |
| 1034 | u8 *(*print_fp) (void *, void *); |
| 1035 | int size; |
| 1036 | api_main_t *am = &api_main; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1037 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 1038 | /* |
| 1039 | * On-demand: allocate enough space for the largest message |
| 1040 | */ |
| 1041 | if (msg_buf == 0) |
| 1042 | { |
| 1043 | vec_validate (tp, 0); |
| 1044 | int max_size = 0; |
| 1045 | for (i = 0; i < vec_len (am->api_trace_cfg); i++) |
| 1046 | { |
| 1047 | if (am->api_trace_cfg[i].size > max_size) |
| 1048 | max_size = am->api_trace_cfg[i].size; |
| 1049 | } |
| 1050 | /* round size to a multiple of the cache-line size */ |
| 1051 | max_size = (max_size + (CLIB_CACHE_LINE_BYTES - 1)) & |
| 1052 | (~(CLIB_CACHE_LINE_BYTES - 1)); |
| 1053 | vec_validate (msg_buf, max_size - 1); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1054 | } |
| 1055 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 1056 | fp = fopen ((char *) filename, "r"); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1057 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 1058 | if (fp == NULL) |
| 1059 | { |
| 1060 | vlib_cli_output (vm, "Couldn't open %s\n", filename); |
| 1061 | return; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1062 | } |
| 1063 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 1064 | /* first, fish the header record from the file */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1065 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 1066 | if (fread (tp, sizeof (*tp), 1, fp) != 1) |
| 1067 | { |
| 1068 | fclose (fp); |
| 1069 | vlib_cli_output (vm, "Header read error\n"); |
| 1070 | return; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1071 | } |
| 1072 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 1073 | /* Endian swap required? */ |
| 1074 | if (clib_arch_is_big_endian != tp->endian) |
| 1075 | { |
| 1076 | endian_swap = 1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1077 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1078 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 1079 | for (i = 0; i <= last; i++) |
| 1080 | { |
| 1081 | /* First 2 bytes are the message type */ |
| 1082 | if (fread (&msg_id, sizeof (u16), 1, fp) != 1) |
| 1083 | { |
| 1084 | break; |
| 1085 | } |
| 1086 | msg_id = ntohs (msg_id); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1087 | |
Dave Barach | ac0798d | 2016-07-26 10:30:50 -0400 | [diff] [blame] | 1088 | if (fseek (fp, -2, SEEK_CUR) < 0) |
Ed Warnicke | 853e720 | 2016-08-12 11:42:26 -0700 | [diff] [blame] | 1089 | { |
| 1090 | vlib_cli_output (vm, "fseek failed, %s", strerror (errno)); |
| 1091 | fclose (fp); |
| 1092 | return; |
| 1093 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1094 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 1095 | /* Mild sanity check */ |
| 1096 | if (msg_id >= vec_len (am->msg_handlers)) |
| 1097 | { |
| 1098 | fclose (fp); |
| 1099 | vlib_cli_output (vm, "msg_id %d out of bounds\n", msg_id); |
| 1100 | return; |
| 1101 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1102 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 1103 | size = am->api_trace_cfg[msg_id].size; |
| 1104 | |
| 1105 | if (fread (msg_buf, size, 1, fp) != 1) |
| 1106 | { |
| 1107 | fclose (fp); |
| 1108 | vlib_cli_output (vm, "read error on %s\n", filename); |
| 1109 | return; |
| 1110 | } |
| 1111 | |
| 1112 | if (i < first) |
| 1113 | continue; |
| 1114 | |
| 1115 | if (endian_swap) |
| 1116 | { |
| 1117 | endian_fp = am->msg_endian_handlers[msg_id]; |
| 1118 | (*endian_fp) (msg_buf); |
| 1119 | } |
| 1120 | |
| 1121 | vlib_cli_output (vm, "[%d]: %s\n", i, am->msg_names[msg_id]); |
| 1122 | |
| 1123 | print_fp = (void *) am->msg_print_handlers[msg_id]; |
| 1124 | (*print_fp) (msg_buf, vm); |
| 1125 | vlib_cli_output (vm, "-------------\n"); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1126 | } |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 1127 | fclose (fp); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1128 | } |
| 1129 | |
| 1130 | static clib_error_t * |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 1131 | vl_api_trace_command (vlib_main_t * vm, |
| 1132 | unformat_input_t * input, vlib_cli_command_t * cli_cmd) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1133 | { |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 1134 | u32 nitems = 1024; |
| 1135 | vl_api_trace_which_t which = VL_API_TRACE_RX; |
| 1136 | u8 *filename; |
| 1137 | u32 first = 0; |
| 1138 | u32 last = ~0; |
| 1139 | api_main_t *am = &api_main; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1140 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 1141 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
| 1142 | { |
| 1143 | if (unformat (input, "rx nitems %u", &nitems) || unformat (input, "rx")) |
| 1144 | goto configure; |
| 1145 | else if (unformat (input, "tx nitems %u", &nitems) |
| 1146 | || unformat (input, "tx")) |
| 1147 | { |
| 1148 | which = VL_API_TRACE_RX; |
| 1149 | goto configure; |
| 1150 | } |
| 1151 | else if (unformat (input, "on rx")) |
| 1152 | { |
| 1153 | vl_msg_api_trace_onoff (am, VL_API_TRACE_RX, 1); |
| 1154 | } |
| 1155 | else if (unformat (input, "on tx")) |
| 1156 | { |
| 1157 | vl_msg_api_trace_onoff (am, VL_API_TRACE_TX, 1); |
| 1158 | } |
| 1159 | else if (unformat (input, "on")) |
| 1160 | { |
| 1161 | vl_msg_api_trace_onoff (am, VL_API_TRACE_RX, 1); |
| 1162 | } |
| 1163 | else if (unformat (input, "off")) |
| 1164 | { |
| 1165 | vl_msg_api_trace_onoff (am, VL_API_TRACE_RX, 0); |
| 1166 | vl_msg_api_trace_onoff (am, VL_API_TRACE_TX, 0); |
| 1167 | } |
| 1168 | else if (unformat (input, "free")) |
| 1169 | { |
| 1170 | vl_msg_api_trace_onoff (am, VL_API_TRACE_RX, 0); |
| 1171 | vl_msg_api_trace_onoff (am, VL_API_TRACE_TX, 0); |
| 1172 | vl_msg_api_trace_free (am, VL_API_TRACE_RX); |
| 1173 | vl_msg_api_trace_free (am, VL_API_TRACE_TX); |
| 1174 | } |
| 1175 | else if (unformat (input, "print %s from %d to %d", &filename, |
| 1176 | &first, &last) |
| 1177 | || unformat (input, "print %s", &filename)) |
| 1178 | { |
| 1179 | goto print; |
| 1180 | } |
| 1181 | else if (unformat (input, "debug on")) |
| 1182 | { |
| 1183 | am->msg_print_flag = 1; |
| 1184 | } |
| 1185 | else if (unformat (input, "debug off")) |
| 1186 | { |
| 1187 | am->msg_print_flag = 0; |
| 1188 | } |
| 1189 | else |
| 1190 | return clib_error_return (0, "unknown input `%U'", |
| 1191 | format_unformat_error, input); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1192 | } |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 1193 | return 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1194 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 1195 | print: |
| 1196 | vl_api_trace_print_file_cmd (vm, first, last, filename); |
| 1197 | goto out; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1198 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 1199 | configure: |
| 1200 | if (vl_msg_api_trace_configure (am, which, nitems)) |
| 1201 | { |
| 1202 | vlib_cli_output (vm, "warning: trace configure error (%d, %d)", |
| 1203 | which, nitems); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1204 | } |
| 1205 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 1206 | out: |
| 1207 | return 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1208 | } |
| 1209 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 1210 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1211 | VLIB_CLI_COMMAND (trace, static) = { |
| 1212 | .path = "set api-trace", |
| 1213 | .short_help = "API trace", |
| 1214 | .function = vl_api_trace_command, |
| 1215 | }; |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 1216 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1217 | |
| 1218 | clib_error_t * |
| 1219 | vlibmemory_init (vlib_main_t * vm) |
| 1220 | { |
Dave Barach | 309bef2 | 2016-01-22 16:09:52 -0500 | [diff] [blame] | 1221 | api_main_t *am = &api_main; |
Dave Barach | b3d93da | 2016-08-03 14:34:38 -0400 | [diff] [blame] | 1222 | svm_map_region_args_t _a, *a = &_a; |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 1223 | |
Dave Barach | b3d93da | 2016-08-03 14:34:38 -0400 | [diff] [blame] | 1224 | memset (a, 0, sizeof (*a)); |
| 1225 | a->root_path = am->root_path; |
| 1226 | a->name = SVM_GLOBAL_REGION_NAME; |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 1227 | a->baseva = (am->global_baseva != 0) ? |
Dave Barach | b3d93da | 2016-08-03 14:34:38 -0400 | [diff] [blame] | 1228 | am->global_baseva : SVM_GLOBAL_REGION_BASEVA; |
| 1229 | a->size = (am->global_size != 0) ? am->global_size : SVM_GLOBAL_REGION_SIZE; |
| 1230 | a->flags = SVM_FLAGS_NODATA; |
| 1231 | a->uid = am->api_uid; |
| 1232 | a->gid = am->api_gid; |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 1233 | a->pvt_heap_size = |
| 1234 | (am->global_pvt_heap_size != |
| 1235 | 0) ? am->global_pvt_heap_size : SVM_PVT_MHEAP_SIZE; |
Dave Barach | b3d93da | 2016-08-03 14:34:38 -0400 | [diff] [blame] | 1236 | |
| 1237 | svm_region_init_args (a); |
Dave Barach | 309bef2 | 2016-01-22 16:09:52 -0500 | [diff] [blame] | 1238 | return 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1239 | } |
| 1240 | |
| 1241 | VLIB_INIT_FUNCTION (vlibmemory_init); |
| 1242 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 1243 | void |
| 1244 | vl_set_memory_region_name (char *name) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1245 | { |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 1246 | api_main_t *am = &api_main; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1247 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 1248 | am->region_name = name; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1249 | } |
| 1250 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 1251 | static int |
| 1252 | range_compare (vl_api_msg_range_t * a0, vl_api_msg_range_t * a1) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1253 | { |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 1254 | int len0, len1, clen; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1255 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 1256 | len0 = vec_len (a0->name); |
| 1257 | len1 = vec_len (a1->name); |
| 1258 | clen = len0 < len1 ? len0 : len1; |
| 1259 | return (strncmp ((char *) a0->name, (char *) a1->name, clen)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1260 | } |
| 1261 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 1262 | static u8 * |
| 1263 | format_api_msg_range (u8 * s, va_list * args) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1264 | { |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 1265 | vl_api_msg_range_t *rp = va_arg (*args, vl_api_msg_range_t *); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1266 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 1267 | if (rp == 0) |
| 1268 | s = format (s, "%-20s%9s%9s", "Name", "First-ID", "Last-ID"); |
| 1269 | else |
| 1270 | s = format (s, "%-20s%9d%9d", rp->name, rp->first_msg_id, |
| 1271 | rp->last_msg_id); |
| 1272 | |
| 1273 | return s; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1274 | } |
| 1275 | |
| 1276 | static clib_error_t * |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 1277 | vl_api_show_plugin_command (vlib_main_t * vm, |
| 1278 | unformat_input_t * input, |
| 1279 | vlib_cli_command_t * cli_cmd) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1280 | { |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 1281 | api_main_t *am = &api_main; |
| 1282 | vl_api_msg_range_t *rp = 0; |
| 1283 | int i; |
| 1284 | |
| 1285 | if (vec_len (am->msg_ranges) == 0) |
| 1286 | { |
| 1287 | vlib_cli_output (vm, "No plugin API message ranges configured..."); |
| 1288 | return 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1289 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1290 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 1291 | rp = vec_dup (am->msg_ranges); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1292 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 1293 | vec_sort_with_function (rp, range_compare); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1294 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 1295 | vlib_cli_output (vm, "Plugin API message ID ranges...\n"); |
| 1296 | vlib_cli_output (vm, "%U", format_api_msg_range, 0 /* header */ ); |
| 1297 | |
| 1298 | for (i = 0; i < vec_len (rp); i++) |
| 1299 | vlib_cli_output (vm, "%U", format_api_msg_range, rp + i); |
| 1300 | |
| 1301 | return 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1302 | } |
| 1303 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 1304 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1305 | VLIB_CLI_COMMAND (cli_show_api_plugin_command, static) = { |
| 1306 | .path = "show api plugin", |
| 1307 | .short_help = "show api plugin", |
| 1308 | .function = vl_api_show_plugin_command, |
| 1309 | }; |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 1310 | /* *INDENT-ON* */ |
Dave Barach | 4e281a4 | 2015-12-14 11:13:29 -0500 | [diff] [blame] | 1311 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 1312 | static void |
| 1313 | vl_api_rpc_call_t_handler (vl_api_rpc_call_t * mp) |
Dave Barach | 4e281a4 | 2015-12-14 11:13:29 -0500 | [diff] [blame] | 1314 | { |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 1315 | vl_api_rpc_reply_t *rmp; |
| 1316 | int (*fp) (void *); |
Dave Barach | 4e281a4 | 2015-12-14 11:13:29 -0500 | [diff] [blame] | 1317 | i32 rv = 0; |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 1318 | vlib_main_t *vm = vlib_get_main (); |
Dave Barach | 4e281a4 | 2015-12-14 11:13:29 -0500 | [diff] [blame] | 1319 | |
| 1320 | if (mp->function == 0) |
| 1321 | { |
| 1322 | rv = -1; |
| 1323 | clib_warning ("rpc NULL function pointer"); |
| 1324 | } |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 1325 | |
Dave Barach | 4e281a4 | 2015-12-14 11:13:29 -0500 | [diff] [blame] | 1326 | else |
| 1327 | { |
| 1328 | if (mp->need_barrier_sync) |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 1329 | vlib_worker_thread_barrier_sync (vm); |
Dave Barach | 4e281a4 | 2015-12-14 11:13:29 -0500 | [diff] [blame] | 1330 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 1331 | fp = uword_to_pointer (mp->function, int (*)(void *)); |
| 1332 | rv = fp (mp->data); |
Dave Barach | 4e281a4 | 2015-12-14 11:13:29 -0500 | [diff] [blame] | 1333 | |
| 1334 | if (mp->need_barrier_sync) |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 1335 | vlib_worker_thread_barrier_release (vm); |
Dave Barach | 4e281a4 | 2015-12-14 11:13:29 -0500 | [diff] [blame] | 1336 | } |
| 1337 | |
| 1338 | if (mp->send_reply) |
| 1339 | { |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 1340 | unix_shared_memory_queue_t *q = |
| 1341 | vl_api_client_index_to_input_queue (mp->client_index); |
Dave Barach | 4e281a4 | 2015-12-14 11:13:29 -0500 | [diff] [blame] | 1342 | if (q) |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 1343 | { |
| 1344 | rmp = vl_msg_api_alloc_as_if_client (sizeof (*rmp)); |
| 1345 | rmp->_vl_msg_id = ntohs (VL_API_RPC_REPLY); |
| 1346 | rmp->context = mp->context; |
| 1347 | rmp->retval = rv; |
| 1348 | vl_msg_api_send_shmem (q, (u8 *) & rmp); |
| 1349 | } |
Dave Barach | 4e281a4 | 2015-12-14 11:13:29 -0500 | [diff] [blame] | 1350 | } |
| 1351 | if (mp->multicast) |
| 1352 | { |
| 1353 | clib_warning ("multicast not yet implemented..."); |
| 1354 | } |
| 1355 | } |
| 1356 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 1357 | static void |
| 1358 | vl_api_rpc_reply_t_handler (vl_api_rpc_reply_t * mp) |
Dave Barach | 4e281a4 | 2015-12-14 11:13:29 -0500 | [diff] [blame] | 1359 | { |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 1360 | clib_warning ("unimplemented"); |
| 1361 | } |
| 1362 | |
| 1363 | void |
| 1364 | vl_api_rpc_call_main_thread (void *fp, u8 * data, u32 data_length) |
| 1365 | { |
| 1366 | vl_api_rpc_call_t *mp; |
Dave Barach | 4e281a4 | 2015-12-14 11:13:29 -0500 | [diff] [blame] | 1367 | api_main_t *am = &api_main; |
| 1368 | vl_shmem_hdr_t *shmem_hdr = am->shmem_hdr; |
Dave Barach | e918363 | 2016-10-04 16:53:56 -0400 | [diff] [blame] | 1369 | unix_shared_memory_queue_t *q; |
Dave Barach | 4e281a4 | 2015-12-14 11:13:29 -0500 | [diff] [blame] | 1370 | |
Dave Barach | e918363 | 2016-10-04 16:53:56 -0400 | [diff] [blame] | 1371 | /* Main thread: call the function directly */ |
| 1372 | if (os_get_cpu_number () == 0) |
| 1373 | { |
| 1374 | vlib_main_t *vm = vlib_get_main (); |
| 1375 | void (*call_fp) (void *); |
| 1376 | |
| 1377 | vlib_worker_thread_barrier_sync (vm); |
| 1378 | |
| 1379 | call_fp = fp; |
| 1380 | call_fp (data); |
| 1381 | |
| 1382 | vlib_worker_thread_barrier_release (vm); |
| 1383 | return; |
| 1384 | } |
| 1385 | |
| 1386 | /* Any other thread, actually do an RPC call... */ |
Dave Barach | 4e281a4 | 2015-12-14 11:13:29 -0500 | [diff] [blame] | 1387 | mp = vl_msg_api_alloc_as_if_client (sizeof (*mp) + data_length); |
Dave Barach | e918363 | 2016-10-04 16:53:56 -0400 | [diff] [blame] | 1388 | |
Dave Barach | 4e281a4 | 2015-12-14 11:13:29 -0500 | [diff] [blame] | 1389 | memset (mp, 0, sizeof (*mp)); |
Damjan Marion | f1213b8 | 2016-03-13 02:22:06 +0100 | [diff] [blame] | 1390 | clib_memcpy (mp->data, data, data_length); |
Dave Barach | 4e281a4 | 2015-12-14 11:13:29 -0500 | [diff] [blame] | 1391 | mp->_vl_msg_id = ntohs (VL_API_RPC_CALL); |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 1392 | mp->function = pointer_to_uword (fp); |
Dave Barach | 4e281a4 | 2015-12-14 11:13:29 -0500 | [diff] [blame] | 1393 | mp->need_barrier_sync = 1; |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 1394 | |
Dave Barach | e918363 | 2016-10-04 16:53:56 -0400 | [diff] [blame] | 1395 | /* |
| 1396 | * Use the "normal" control-plane mechanism for the main thread. |
| 1397 | * Well, almost. if the main input queue is full, we cannot |
| 1398 | * block. Otherwise, we can expect a barrier sync timeout. |
| 1399 | */ |
| 1400 | q = shmem_hdr->vl_input_queue; |
| 1401 | |
| 1402 | while (pthread_mutex_trylock (&q->mutex)) |
| 1403 | vlib_worker_thread_barrier_check (); |
| 1404 | |
| 1405 | while (PREDICT_FALSE (unix_shared_memory_queue_is_full (q))) |
| 1406 | { |
| 1407 | pthread_mutex_unlock (&q->mutex); |
| 1408 | vlib_worker_thread_barrier_check (); |
| 1409 | while (pthread_mutex_trylock (&q->mutex)) |
| 1410 | vlib_worker_thread_barrier_check (); |
| 1411 | } |
| 1412 | |
| 1413 | vl_msg_api_send_shmem_nolock (q, (u8 *) & mp); |
| 1414 | |
| 1415 | pthread_mutex_unlock (&q->mutex); |
Dave Barach | 4e281a4 | 2015-12-14 11:13:29 -0500 | [diff] [blame] | 1416 | } |
| 1417 | |
| 1418 | #define foreach_rpc_api_msg \ |
| 1419 | _(RPC_CALL,rpc_call) \ |
| 1420 | _(RPC_REPLY,rpc_reply) |
| 1421 | |
| 1422 | static clib_error_t * |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 1423 | rpc_api_hookup (vlib_main_t * vm) |
Dave Barach | 4e281a4 | 2015-12-14 11:13:29 -0500 | [diff] [blame] | 1424 | { |
| 1425 | #define _(N,n) \ |
| 1426 | vl_msg_api_set_handlers(VL_API_##N, #n, \ |
| 1427 | vl_api_##n##_t_handler, \ |
| 1428 | vl_noop_handler, \ |
| 1429 | vl_noop_handler, \ |
| 1430 | vl_api_##n##_t_print, \ |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 1431 | sizeof(vl_api_##n##_t), 0 /* do not trace */); |
| 1432 | foreach_rpc_api_msg; |
Dave Barach | 4e281a4 | 2015-12-14 11:13:29 -0500 | [diff] [blame] | 1433 | #undef _ |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 1434 | return 0; |
Dave Barach | 4e281a4 | 2015-12-14 11:13:29 -0500 | [diff] [blame] | 1435 | } |
| 1436 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 1437 | VLIB_API_INIT_FUNCTION (rpc_api_hookup); |
| 1438 | |
| 1439 | /* |
| 1440 | * fd.io coding-style-patch-verification: ON |
| 1441 | * |
| 1442 | * Local Variables: |
| 1443 | * eval: (c-set-style "gnu") |
| 1444 | * End: |
| 1445 | */ |