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