Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 1 | /* |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2 | *------------------------------------------------------------------ |
| 3 | * memory_client.c - API message handling, client code. |
| 4 | * |
| 5 | * Copyright (c) 2010 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 <setjmp.h> |
| 23 | #include <sys/types.h> |
| 24 | #include <sys/mman.h> |
| 25 | #include <sys/stat.h> |
| 26 | #include <netinet/in.h> |
| 27 | #include <signal.h> |
| 28 | #include <pthread.h> |
| 29 | #include <unistd.h> |
| 30 | #include <time.h> |
| 31 | #include <fcntl.h> |
| 32 | #include <string.h> |
| 33 | #include <vppinfra/clib.h> |
| 34 | #include <vppinfra/vec.h> |
| 35 | #include <vppinfra/hash.h> |
| 36 | #include <vppinfra/bitmap.h> |
| 37 | #include <vppinfra/fifo.h> |
| 38 | #include <vppinfra/time.h> |
| 39 | #include <vppinfra/mheap.h> |
| 40 | #include <vppinfra/heap.h> |
| 41 | #include <vppinfra/pool.h> |
| 42 | #include <vppinfra/format.h> |
| 43 | |
| 44 | #include <vlib/vlib.h> |
| 45 | #include <vlib/unix/unix.h> |
| 46 | #include <vlibmemory/api.h> |
| 47 | |
| 48 | #include <vlibmemory/vl_memory_msg_enum.h> |
| 49 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 50 | #define vl_typedefs /* define message structures */ |
| 51 | #include <vlibmemory/vl_memory_api_h.h> |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 52 | #undef vl_typedefs |
| 53 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 54 | #define vl_endianfun /* define message structures */ |
| 55 | #include <vlibmemory/vl_memory_api_h.h> |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 56 | #undef vl_endianfun |
| 57 | |
| 58 | /* instantiate all the print functions we know about */ |
| 59 | #define vl_print(handle, ...) clib_warning (__VA_ARGS__) |
| 60 | #define vl_printfun |
| 61 | #include <vlibmemory/vl_memory_api_h.h> |
| 62 | #undef vl_printfun |
| 63 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 64 | typedef struct |
| 65 | { |
| 66 | u8 rx_thread_jmpbuf_valid; |
| 67 | u8 connected_to_vlib; |
| 68 | jmp_buf rx_thread_jmpbuf; |
| 69 | pthread_t rx_thread_handle; |
| 70 | /* Plugin message base lookup scheme */ |
| 71 | volatile u8 first_msg_id_reply_ready; |
| 72 | u16 first_msg_id_reply; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 73 | } memory_client_main_t; |
| 74 | |
| 75 | memory_client_main_t memory_client_main; |
| 76 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 77 | static void * |
| 78 | rx_thread_fn (void *arg) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 79 | { |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 80 | unix_shared_memory_queue_t *q; |
| 81 | memory_client_main_t *mm = &memory_client_main; |
| 82 | api_main_t *am = &api_main; |
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 | q = am->vl_input_queue; |
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 | /* So we can make the rx thread terminate cleanly */ |
| 87 | if (setjmp (mm->rx_thread_jmpbuf) == 0) |
| 88 | { |
| 89 | mm->rx_thread_jmpbuf_valid = 1; |
| 90 | while (1) |
| 91 | { |
| 92 | vl_msg_api_queue_handler (q); |
| 93 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 94 | } |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 95 | pthread_exit (0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 96 | } |
| 97 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 98 | static void |
| 99 | vl_api_rx_thread_exit_t_handler (vl_api_rx_thread_exit_t * mp) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 100 | { |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 101 | memory_client_main_t *mm = &memory_client_main; |
| 102 | vl_msg_api_free (mp); |
| 103 | longjmp (mm->rx_thread_jmpbuf, 1); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 104 | } |
| 105 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 106 | static void |
Dave Barach | 5c6c4bf | 2017-04-11 13:12:48 -0400 | [diff] [blame] | 107 | vl_api_memclnt_create_reply_t_handler (vl_api_memclnt_create_reply_t * mp) |
| 108 | { |
| 109 | serialize_main_t _sm, *sm = &_sm; |
| 110 | api_main_t *am = &api_main; |
| 111 | u8 *tblv; |
| 112 | u32 nmsgs; |
| 113 | int i; |
| 114 | u8 *name_and_crc; |
| 115 | u32 msg_index; |
| 116 | |
| 117 | am->my_client_index = mp->index; |
| 118 | am->my_registration = (vl_api_registration_t *) (uword) mp->handle; |
| 119 | |
| 120 | /* Clean out any previous hash table (unlikely) */ |
| 121 | if (am->msg_index_by_name_and_crc) |
| 122 | { |
| 123 | int i; |
| 124 | u8 **keys = 0; |
| 125 | hash_pair_t *hp; |
| 126 | /* *INDENT-OFF* */ |
| 127 | hash_foreach_pair (hp, am->msg_index_by_name_and_crc, |
| 128 | ({ |
| 129 | vec_add1 (keys, (u8 *) hp->key); |
| 130 | })); |
| 131 | /* *INDENT-ON* */ |
| 132 | for (i = 0; i < vec_len (keys); i++) |
| 133 | vec_free (keys[i]); |
| 134 | vec_free (keys); |
| 135 | } |
| 136 | |
| 137 | am->msg_index_by_name_and_crc = hash_create_string (0, sizeof (uword)); |
| 138 | |
| 139 | /* Recreate the vnet-side API message handler table */ |
Damjan Marion | 7bee80c | 2017-04-26 15:32:12 +0200 | [diff] [blame] | 140 | tblv = uword_to_pointer (mp->message_table, u8 *); |
Dave Barach | 5c6c4bf | 2017-04-11 13:12:48 -0400 | [diff] [blame] | 141 | serialize_open_vector (sm, tblv); |
| 142 | unserialize_integer (sm, &nmsgs, sizeof (u32)); |
| 143 | |
| 144 | for (i = 0; i < nmsgs; i++) |
| 145 | { |
| 146 | msg_index = unserialize_likely_small_unsigned_integer (sm); |
| 147 | unserialize_cstring (sm, (char **) &name_and_crc); |
| 148 | hash_set_mem (am->msg_index_by_name_and_crc, name_and_crc, msg_index); |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | static void |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 153 | noop_handler (void *notused) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 154 | { |
| 155 | } |
| 156 | |
Dave Barach | 5c6c4bf | 2017-04-11 13:12:48 -0400 | [diff] [blame] | 157 | int |
Neale Ranns | e72be39 | 2017-04-26 13:59:20 -0700 | [diff] [blame] | 158 | vl_client_connect (const char *name, int ctx_quota, int input_queue_size) |
Dave Barach | 5c6c4bf | 2017-04-11 13:12:48 -0400 | [diff] [blame] | 159 | { |
| 160 | svm_region_t *svm; |
| 161 | vl_api_memclnt_create_t *mp; |
| 162 | vl_api_memclnt_create_reply_t *rp; |
| 163 | unix_shared_memory_queue_t *vl_input_queue; |
| 164 | vl_shmem_hdr_t *shmem_hdr; |
| 165 | int rv = 0; |
| 166 | void *oldheap; |
| 167 | api_main_t *am = &api_main; |
| 168 | |
| 169 | if (am->my_registration) |
| 170 | { |
| 171 | clib_warning ("client %s already connected...", name); |
| 172 | return -1; |
| 173 | } |
| 174 | |
| 175 | if (am->vlib_rp == 0) |
| 176 | { |
| 177 | clib_warning ("am->vlib_rp NULL"); |
| 178 | return -1; |
| 179 | } |
| 180 | |
| 181 | svm = am->vlib_rp; |
| 182 | shmem_hdr = am->shmem_hdr; |
| 183 | |
| 184 | if (shmem_hdr == 0 || shmem_hdr->vl_input_queue == 0) |
| 185 | { |
| 186 | clib_warning ("shmem_hdr / input queue NULL"); |
| 187 | return -1; |
| 188 | } |
| 189 | |
| 190 | pthread_mutex_lock (&svm->mutex); |
| 191 | oldheap = svm_push_data_heap (svm); |
| 192 | vl_input_queue = |
| 193 | unix_shared_memory_queue_init (input_queue_size, sizeof (uword), |
| 194 | getpid (), 0); |
| 195 | pthread_mutex_unlock (&svm->mutex); |
| 196 | svm_pop_heap (oldheap); |
| 197 | |
| 198 | am->my_client_index = ~0; |
| 199 | am->my_registration = 0; |
| 200 | am->vl_input_queue = vl_input_queue; |
| 201 | |
| 202 | mp = vl_msg_api_alloc (sizeof (vl_api_memclnt_create_t)); |
| 203 | memset (mp, 0, sizeof (*mp)); |
| 204 | mp->_vl_msg_id = ntohs (VL_API_MEMCLNT_CREATE); |
| 205 | mp->ctx_quota = ctx_quota; |
| 206 | mp->input_queue = (uword) vl_input_queue; |
| 207 | strncpy ((char *) mp->name, name, sizeof (mp->name) - 1); |
| 208 | |
| 209 | vl_msg_api_send_shmem (shmem_hdr->vl_input_queue, (u8 *) & mp); |
| 210 | |
| 211 | while (1) |
| 212 | { |
| 213 | int qstatus; |
| 214 | struct timespec ts, tsrem; |
| 215 | int i; |
| 216 | |
| 217 | /* Wait up to 10 seconds */ |
| 218 | for (i = 0; i < 1000; i++) |
| 219 | { |
| 220 | qstatus = unix_shared_memory_queue_sub (vl_input_queue, (u8 *) & rp, |
| 221 | 1 /* nowait */ ); |
| 222 | if (qstatus == 0) |
| 223 | goto read_one_msg; |
| 224 | ts.tv_sec = 0; |
| 225 | ts.tv_nsec = 10000 * 1000; /* 10 ms */ |
| 226 | while (nanosleep (&ts, &tsrem) < 0) |
| 227 | ts = tsrem; |
| 228 | } |
| 229 | /* Timeout... */ |
| 230 | clib_warning ("memclnt_create_reply timeout"); |
| 231 | return -1; |
| 232 | |
| 233 | read_one_msg: |
| 234 | if (ntohs (rp->_vl_msg_id) != VL_API_MEMCLNT_CREATE_REPLY) |
| 235 | { |
| 236 | clib_warning ("unexpected reply: id %d", ntohs (rp->_vl_msg_id)); |
| 237 | continue; |
| 238 | } |
| 239 | rv = clib_net_to_host_u32 (rp->response); |
| 240 | |
| 241 | vl_msg_api_handler ((void *) rp); |
| 242 | break; |
| 243 | } |
| 244 | return (rv); |
| 245 | } |
| 246 | |
| 247 | static void |
| 248 | vl_api_memclnt_delete_reply_t_handler (vl_api_memclnt_delete_reply_t * mp) |
| 249 | { |
| 250 | void *oldheap; |
| 251 | api_main_t *am = &api_main; |
| 252 | |
| 253 | pthread_mutex_lock (&am->vlib_rp->mutex); |
| 254 | oldheap = svm_push_data_heap (am->vlib_rp); |
| 255 | unix_shared_memory_queue_free (am->vl_input_queue); |
| 256 | pthread_mutex_unlock (&am->vlib_rp->mutex); |
| 257 | svm_pop_heap (oldheap); |
| 258 | |
| 259 | am->my_client_index = ~0; |
| 260 | am->my_registration = 0; |
| 261 | am->vl_input_queue = 0; |
| 262 | } |
| 263 | |
| 264 | void |
| 265 | vl_client_disconnect (void) |
| 266 | { |
| 267 | vl_api_memclnt_delete_t *mp; |
| 268 | vl_api_memclnt_delete_reply_t *rp; |
| 269 | unix_shared_memory_queue_t *vl_input_queue; |
| 270 | vl_shmem_hdr_t *shmem_hdr; |
| 271 | time_t begin; |
| 272 | api_main_t *am = &api_main; |
| 273 | |
| 274 | ASSERT (am->vlib_rp); |
| 275 | shmem_hdr = am->shmem_hdr; |
| 276 | ASSERT (shmem_hdr && shmem_hdr->vl_input_queue); |
| 277 | |
| 278 | vl_input_queue = am->vl_input_queue; |
| 279 | |
| 280 | mp = vl_msg_api_alloc (sizeof (vl_api_memclnt_delete_t)); |
| 281 | memset (mp, 0, sizeof (*mp)); |
| 282 | mp->_vl_msg_id = ntohs (VL_API_MEMCLNT_DELETE); |
| 283 | mp->index = am->my_client_index; |
| 284 | mp->handle = (uword) am->my_registration; |
| 285 | |
| 286 | vl_msg_api_send_shmem (shmem_hdr->vl_input_queue, (u8 *) & mp); |
| 287 | |
| 288 | /* |
| 289 | * Have to be careful here, in case the client is disconnecting |
| 290 | * because e.g. the vlib process died, or is unresponsive. |
| 291 | */ |
| 292 | |
| 293 | begin = time (0); |
| 294 | while (1) |
| 295 | { |
| 296 | time_t now; |
| 297 | |
| 298 | now = time (0); |
| 299 | |
| 300 | if (now >= (begin + 2)) |
| 301 | { |
| 302 | clib_warning ("peer unresponsive, give up"); |
| 303 | am->my_client_index = ~0; |
| 304 | am->my_registration = 0; |
| 305 | am->shmem_hdr = 0; |
| 306 | break; |
| 307 | } |
| 308 | if (unix_shared_memory_queue_sub (vl_input_queue, (u8 *) & rp, 1) < 0) |
| 309 | continue; |
| 310 | |
| 311 | /* drain the queue */ |
| 312 | if (ntohs (rp->_vl_msg_id) != VL_API_MEMCLNT_DELETE_REPLY) |
| 313 | { |
| 314 | vl_msg_api_handler ((void *) rp); |
| 315 | continue; |
| 316 | } |
| 317 | vl_msg_api_handler ((void *) rp); |
| 318 | break; |
| 319 | } |
| 320 | } |
| 321 | |
| 322 | #define foreach_api_msg \ |
| 323 | _(RX_THREAD_EXIT, rx_thread_exit) \ |
| 324 | _(MEMCLNT_CREATE_REPLY, memclnt_create_reply) \ |
| 325 | _(MEMCLNT_DELETE_REPLY, memclnt_delete_reply) |
| 326 | |
| 327 | |
| 328 | int |
Neale Ranns | e72be39 | 2017-04-26 13:59:20 -0700 | [diff] [blame] | 329 | vl_client_api_map (const char *region_name) |
Dave Barach | 5c6c4bf | 2017-04-11 13:12:48 -0400 | [diff] [blame] | 330 | { |
| 331 | int rv; |
| 332 | |
| 333 | if ((rv = vl_map_shmem (region_name, 0 /* is_vlib */ )) < 0) |
| 334 | { |
| 335 | return rv; |
| 336 | } |
| 337 | |
| 338 | #define _(N,n) \ |
| 339 | vl_msg_api_set_handlers(VL_API_##N, #n, \ |
| 340 | vl_api_##n##_t_handler, \ |
| 341 | noop_handler, \ |
| 342 | vl_api_##n##_t_endian, \ |
| 343 | vl_api_##n##_t_print, \ |
| 344 | sizeof(vl_api_##n##_t), 1); |
| 345 | foreach_api_msg; |
| 346 | #undef _ |
| 347 | return 0; |
| 348 | } |
| 349 | |
| 350 | void |
| 351 | vl_client_api_unmap (void) |
| 352 | { |
| 353 | vl_unmap_shmem (); |
| 354 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 355 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 356 | static int |
Neale Ranns | e72be39 | 2017-04-26 13:59:20 -0700 | [diff] [blame] | 357 | connect_to_vlib_internal (const char *svm_name, |
| 358 | const char *client_name, |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 359 | int rx_queue_size, int want_pthread) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 360 | { |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 361 | int rv = 0; |
| 362 | memory_client_main_t *mm = &memory_client_main; |
| 363 | |
| 364 | if ((rv = vl_client_api_map (svm_name))) |
| 365 | { |
| 366 | clib_warning ("vl_client_api map rv %d", rv); |
| 367 | return rv; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 368 | } |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 369 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 370 | if (vl_client_connect (client_name, 0 /* punt quota */ , |
| 371 | rx_queue_size /* input queue */ ) < 0) |
| 372 | { |
| 373 | vl_client_api_unmap (); |
| 374 | return -1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 375 | } |
| 376 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 377 | /* Start the rx queue thread */ |
| 378 | |
| 379 | if (want_pthread) |
| 380 | { |
| 381 | rv = pthread_create (&mm->rx_thread_handle, |
| 382 | NULL /*attr */ , rx_thread_fn, 0); |
| 383 | if (rv) |
| 384 | clib_warning ("pthread_create returned %d", rv); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 385 | } |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 386 | |
| 387 | mm->connected_to_vlib = 1; |
| 388 | return 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 389 | } |
| 390 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 391 | int |
Neale Ranns | e72be39 | 2017-04-26 13:59:20 -0700 | [diff] [blame] | 392 | vl_client_connect_to_vlib (const char *svm_name, |
| 393 | const char *client_name, int rx_queue_size) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 394 | { |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 395 | return connect_to_vlib_internal (svm_name, client_name, rx_queue_size, |
| 396 | 1 /* want pthread */ ); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 397 | } |
| 398 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 399 | int |
Neale Ranns | e72be39 | 2017-04-26 13:59:20 -0700 | [diff] [blame] | 400 | vl_client_connect_to_vlib_no_rx_pthread (const char *svm_name, |
| 401 | const char *client_name, |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 402 | int rx_queue_size) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 403 | { |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 404 | return connect_to_vlib_internal (svm_name, client_name, rx_queue_size, |
| 405 | 0 /* want pthread */ ); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 406 | } |
| 407 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 408 | void |
| 409 | vl_client_disconnect_from_vlib (void) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 410 | { |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 411 | memory_client_main_t *mm = &memory_client_main; |
| 412 | api_main_t *am = &api_main; |
| 413 | uword junk; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 414 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 415 | if (mm->rx_thread_jmpbuf_valid) |
| 416 | { |
| 417 | vl_api_rx_thread_exit_t *ep; |
| 418 | ep = vl_msg_api_alloc (sizeof (*ep)); |
| 419 | ep->_vl_msg_id = ntohs (VL_API_RX_THREAD_EXIT); |
| 420 | vl_msg_api_send_shmem (am->vl_input_queue, (u8 *) & ep); |
| 421 | pthread_join (mm->rx_thread_handle, (void **) &junk); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 422 | } |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 423 | if (mm->connected_to_vlib) |
| 424 | { |
| 425 | vl_client_disconnect (); |
| 426 | vl_client_api_unmap (); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 427 | } |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 428 | memset (mm, 0, sizeof (*mm)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 429 | } |
| 430 | |
| 431 | static void vl_api_get_first_msg_id_reply_t_handler |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 432 | (vl_api_get_first_msg_id_reply_t * mp) |
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 | memory_client_main_t *mm = &memory_client_main; |
| 435 | i32 retval = ntohl (mp->retval); |
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 | mm->first_msg_id_reply = (retval >= 0) ? ntohs (mp->first_msg_id) : ~0; |
| 438 | mm->first_msg_id_reply_ready = 1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 439 | } |
| 440 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 441 | u16 |
Neale Ranns | e72be39 | 2017-04-26 13:59:20 -0700 | [diff] [blame] | 442 | vl_client_get_first_plugin_msg_id (const char *plugin_name) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 443 | { |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 444 | vl_api_get_first_msg_id_t *mp; |
| 445 | api_main_t *am = &api_main; |
| 446 | memory_client_main_t *mm = &memory_client_main; |
| 447 | f64 timeout; |
| 448 | void *old_handler; |
| 449 | clib_time_t clib_time; |
| 450 | u16 rv = ~0; |
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 | if (strlen (plugin_name) + 1 > sizeof (mp->name)) |
| 453 | return (rv); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 454 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 455 | memset (&clib_time, 0, sizeof (clib_time)); |
| 456 | clib_time_init (&clib_time); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 457 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 458 | /* Push this plugin's first_msg_id_reply handler */ |
| 459 | old_handler = am->msg_handlers[VL_API_GET_FIRST_MSG_ID_REPLY]; |
| 460 | am->msg_handlers[VL_API_GET_FIRST_MSG_ID_REPLY] = (void *) |
| 461 | vl_api_get_first_msg_id_reply_t_handler; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 462 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 463 | /* Ask the data-plane for the message-ID base of the indicated plugin */ |
| 464 | mm->first_msg_id_reply_ready = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 465 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 466 | mp = vl_msg_api_alloc (sizeof (*mp)); |
| 467 | memset (mp, 0, sizeof (*mp)); |
| 468 | mp->_vl_msg_id = ntohs (VL_API_GET_FIRST_MSG_ID); |
| 469 | mp->client_index = am->my_client_index; |
| 470 | strncpy ((char *) mp->name, plugin_name, sizeof (mp->name) - 1); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 471 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 472 | vl_msg_api_send_shmem (am->shmem_hdr->vl_input_queue, (u8 *) & mp); |
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 | /* Synchronously wait for the answer */ |
| 475 | do |
| 476 | { |
| 477 | timeout = clib_time_now (&clib_time) + 1.0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 478 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 479 | while (clib_time_now (&clib_time) < timeout) |
| 480 | { |
| 481 | if (mm->first_msg_id_reply_ready == 1) |
| 482 | { |
| 483 | rv = mm->first_msg_id_reply; |
| 484 | goto result; |
| 485 | } |
| 486 | } |
| 487 | /* Restore old handler */ |
| 488 | am->msg_handlers[VL_API_GET_FIRST_MSG_ID_REPLY] = old_handler; |
| 489 | |
| 490 | return rv; |
| 491 | } |
| 492 | while (0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 493 | |
| 494 | result: |
| 495 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 496 | /* Restore the old handler */ |
| 497 | am->msg_handlers[VL_API_GET_FIRST_MSG_ID_REPLY] = old_handler; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 498 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 499 | if (rv == (u16) ~ 0) |
| 500 | clib_warning ("plugin '%s' not registered", plugin_name); |
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 | return rv; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 503 | } |
Dave Barach | 6931f59 | 2016-05-13 12:55:01 -0400 | [diff] [blame] | 504 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 505 | void |
| 506 | vlib_node_sync_stats (vlib_main_t * vm, vlib_node_t * n) |
| 507 | { |
| 508 | clib_warning ("STUB called..."); |
| 509 | } |
| 510 | |
| 511 | /* |
| 512 | * fd.io coding-style-patch-verification: ON |
| 513 | * |
| 514 | * Local Variables: |
| 515 | * eval: (c-set-style "gnu") |
| 516 | * End: |
| 517 | */ |