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; |
Dave Barach | cf5e848 | 2017-10-17 11:48:29 -0400 | [diff] [blame] | 83 | int i; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 84 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 85 | q = am->vl_input_queue; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 86 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 87 | /* So we can make the rx thread terminate cleanly */ |
| 88 | if (setjmp (mm->rx_thread_jmpbuf) == 0) |
| 89 | { |
| 90 | mm->rx_thread_jmpbuf_valid = 1; |
Dave Barach | cf5e848 | 2017-10-17 11:48:29 -0400 | [diff] [blame] | 91 | /* |
| 92 | * Find an unused slot in the per-cpu-mheaps array, |
| 93 | * and grab it for this thread. We need to be able to |
| 94 | * push/pop the thread heap without affecting other thread(s). |
| 95 | */ |
| 96 | if (__os_thread_index == 0) |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 97 | { |
Dave Barach | cf5e848 | 2017-10-17 11:48:29 -0400 | [diff] [blame] | 98 | for (i = 0; i < ARRAY_LEN (clib_per_cpu_mheaps); i++) |
| 99 | { |
| 100 | if (clib_per_cpu_mheaps[i] == 0) |
| 101 | { |
| 102 | /* Copy the main thread mheap pointer */ |
| 103 | clib_per_cpu_mheaps[i] = clib_per_cpu_mheaps[0]; |
| 104 | __os_thread_index = i; |
| 105 | break; |
| 106 | } |
| 107 | } |
| 108 | ASSERT (__os_thread_index > 0); |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 109 | } |
Dave Barach | cf5e848 | 2017-10-17 11:48:29 -0400 | [diff] [blame] | 110 | while (1) |
| 111 | vl_msg_api_queue_handler (q); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 112 | } |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 113 | pthread_exit (0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 114 | } |
| 115 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 116 | static void |
| 117 | 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] | 118 | { |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 119 | memory_client_main_t *mm = &memory_client_main; |
| 120 | vl_msg_api_free (mp); |
| 121 | longjmp (mm->rx_thread_jmpbuf, 1); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 122 | } |
| 123 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 124 | static void |
Dave Barach | 5c6c4bf | 2017-04-11 13:12:48 -0400 | [diff] [blame] | 125 | vl_api_memclnt_create_reply_t_handler (vl_api_memclnt_create_reply_t * mp) |
| 126 | { |
| 127 | serialize_main_t _sm, *sm = &_sm; |
| 128 | api_main_t *am = &api_main; |
| 129 | u8 *tblv; |
| 130 | u32 nmsgs; |
| 131 | int i; |
| 132 | u8 *name_and_crc; |
| 133 | u32 msg_index; |
| 134 | |
| 135 | am->my_client_index = mp->index; |
| 136 | am->my_registration = (vl_api_registration_t *) (uword) mp->handle; |
| 137 | |
| 138 | /* Clean out any previous hash table (unlikely) */ |
| 139 | if (am->msg_index_by_name_and_crc) |
| 140 | { |
| 141 | int i; |
| 142 | u8 **keys = 0; |
| 143 | hash_pair_t *hp; |
| 144 | /* *INDENT-OFF* */ |
| 145 | hash_foreach_pair (hp, am->msg_index_by_name_and_crc, |
| 146 | ({ |
| 147 | vec_add1 (keys, (u8 *) hp->key); |
| 148 | })); |
| 149 | /* *INDENT-ON* */ |
| 150 | for (i = 0; i < vec_len (keys); i++) |
| 151 | vec_free (keys[i]); |
| 152 | vec_free (keys); |
| 153 | } |
| 154 | |
| 155 | am->msg_index_by_name_and_crc = hash_create_string (0, sizeof (uword)); |
| 156 | |
| 157 | /* Recreate the vnet-side API message handler table */ |
Damjan Marion | 7bee80c | 2017-04-26 15:32:12 +0200 | [diff] [blame] | 158 | tblv = uword_to_pointer (mp->message_table, u8 *); |
Dave Barach | cf5e848 | 2017-10-17 11:48:29 -0400 | [diff] [blame] | 159 | unserialize_open_data (sm, tblv, vec_len (tblv)); |
Dave Barach | 5c6c4bf | 2017-04-11 13:12:48 -0400 | [diff] [blame] | 160 | unserialize_integer (sm, &nmsgs, sizeof (u32)); |
| 161 | |
| 162 | for (i = 0; i < nmsgs; i++) |
| 163 | { |
| 164 | msg_index = unserialize_likely_small_unsigned_integer (sm); |
| 165 | unserialize_cstring (sm, (char **) &name_and_crc); |
| 166 | hash_set_mem (am->msg_index_by_name_and_crc, name_and_crc, msg_index); |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | static void |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 171 | noop_handler (void *notused) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 172 | { |
| 173 | } |
| 174 | |
Dave Barach | 5c6c4bf | 2017-04-11 13:12:48 -0400 | [diff] [blame] | 175 | int |
Neale Ranns | e72be39 | 2017-04-26 13:59:20 -0700 | [diff] [blame] | 176 | 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] | 177 | { |
| 178 | svm_region_t *svm; |
| 179 | vl_api_memclnt_create_t *mp; |
| 180 | vl_api_memclnt_create_reply_t *rp; |
| 181 | unix_shared_memory_queue_t *vl_input_queue; |
| 182 | vl_shmem_hdr_t *shmem_hdr; |
| 183 | int rv = 0; |
| 184 | void *oldheap; |
| 185 | api_main_t *am = &api_main; |
| 186 | |
| 187 | if (am->my_registration) |
| 188 | { |
| 189 | clib_warning ("client %s already connected...", name); |
| 190 | return -1; |
| 191 | } |
| 192 | |
| 193 | if (am->vlib_rp == 0) |
| 194 | { |
| 195 | clib_warning ("am->vlib_rp NULL"); |
| 196 | return -1; |
| 197 | } |
| 198 | |
| 199 | svm = am->vlib_rp; |
| 200 | shmem_hdr = am->shmem_hdr; |
| 201 | |
| 202 | if (shmem_hdr == 0 || shmem_hdr->vl_input_queue == 0) |
| 203 | { |
| 204 | clib_warning ("shmem_hdr / input queue NULL"); |
| 205 | return -1; |
| 206 | } |
| 207 | |
| 208 | pthread_mutex_lock (&svm->mutex); |
| 209 | oldheap = svm_push_data_heap (svm); |
| 210 | vl_input_queue = |
| 211 | unix_shared_memory_queue_init (input_queue_size, sizeof (uword), |
| 212 | getpid (), 0); |
| 213 | pthread_mutex_unlock (&svm->mutex); |
| 214 | svm_pop_heap (oldheap); |
| 215 | |
| 216 | am->my_client_index = ~0; |
| 217 | am->my_registration = 0; |
| 218 | am->vl_input_queue = vl_input_queue; |
| 219 | |
| 220 | mp = vl_msg_api_alloc (sizeof (vl_api_memclnt_create_t)); |
| 221 | memset (mp, 0, sizeof (*mp)); |
| 222 | mp->_vl_msg_id = ntohs (VL_API_MEMCLNT_CREATE); |
| 223 | mp->ctx_quota = ctx_quota; |
| 224 | mp->input_queue = (uword) vl_input_queue; |
| 225 | strncpy ((char *) mp->name, name, sizeof (mp->name) - 1); |
| 226 | |
| 227 | vl_msg_api_send_shmem (shmem_hdr->vl_input_queue, (u8 *) & mp); |
| 228 | |
| 229 | while (1) |
| 230 | { |
| 231 | int qstatus; |
| 232 | struct timespec ts, tsrem; |
| 233 | int i; |
| 234 | |
| 235 | /* Wait up to 10 seconds */ |
| 236 | for (i = 0; i < 1000; i++) |
| 237 | { |
| 238 | qstatus = unix_shared_memory_queue_sub (vl_input_queue, (u8 *) & rp, |
| 239 | 1 /* nowait */ ); |
| 240 | if (qstatus == 0) |
| 241 | goto read_one_msg; |
| 242 | ts.tv_sec = 0; |
| 243 | ts.tv_nsec = 10000 * 1000; /* 10 ms */ |
| 244 | while (nanosleep (&ts, &tsrem) < 0) |
| 245 | ts = tsrem; |
| 246 | } |
| 247 | /* Timeout... */ |
| 248 | clib_warning ("memclnt_create_reply timeout"); |
| 249 | return -1; |
| 250 | |
| 251 | read_one_msg: |
| 252 | if (ntohs (rp->_vl_msg_id) != VL_API_MEMCLNT_CREATE_REPLY) |
| 253 | { |
| 254 | clib_warning ("unexpected reply: id %d", ntohs (rp->_vl_msg_id)); |
| 255 | continue; |
| 256 | } |
| 257 | rv = clib_net_to_host_u32 (rp->response); |
| 258 | |
| 259 | vl_msg_api_handler ((void *) rp); |
| 260 | break; |
| 261 | } |
| 262 | return (rv); |
| 263 | } |
| 264 | |
| 265 | static void |
| 266 | vl_api_memclnt_delete_reply_t_handler (vl_api_memclnt_delete_reply_t * mp) |
| 267 | { |
| 268 | void *oldheap; |
| 269 | api_main_t *am = &api_main; |
| 270 | |
| 271 | pthread_mutex_lock (&am->vlib_rp->mutex); |
| 272 | oldheap = svm_push_data_heap (am->vlib_rp); |
| 273 | unix_shared_memory_queue_free (am->vl_input_queue); |
| 274 | pthread_mutex_unlock (&am->vlib_rp->mutex); |
| 275 | svm_pop_heap (oldheap); |
| 276 | |
| 277 | am->my_client_index = ~0; |
| 278 | am->my_registration = 0; |
| 279 | am->vl_input_queue = 0; |
| 280 | } |
| 281 | |
| 282 | void |
| 283 | vl_client_disconnect (void) |
| 284 | { |
| 285 | vl_api_memclnt_delete_t *mp; |
| 286 | vl_api_memclnt_delete_reply_t *rp; |
| 287 | unix_shared_memory_queue_t *vl_input_queue; |
| 288 | vl_shmem_hdr_t *shmem_hdr; |
| 289 | time_t begin; |
| 290 | api_main_t *am = &api_main; |
| 291 | |
| 292 | ASSERT (am->vlib_rp); |
| 293 | shmem_hdr = am->shmem_hdr; |
| 294 | ASSERT (shmem_hdr && shmem_hdr->vl_input_queue); |
| 295 | |
| 296 | vl_input_queue = am->vl_input_queue; |
| 297 | |
| 298 | mp = vl_msg_api_alloc (sizeof (vl_api_memclnt_delete_t)); |
| 299 | memset (mp, 0, sizeof (*mp)); |
| 300 | mp->_vl_msg_id = ntohs (VL_API_MEMCLNT_DELETE); |
| 301 | mp->index = am->my_client_index; |
| 302 | mp->handle = (uword) am->my_registration; |
| 303 | |
| 304 | vl_msg_api_send_shmem (shmem_hdr->vl_input_queue, (u8 *) & mp); |
| 305 | |
| 306 | /* |
| 307 | * Have to be careful here, in case the client is disconnecting |
| 308 | * because e.g. the vlib process died, or is unresponsive. |
| 309 | */ |
| 310 | |
| 311 | begin = time (0); |
| 312 | while (1) |
| 313 | { |
| 314 | time_t now; |
| 315 | |
| 316 | now = time (0); |
| 317 | |
| 318 | if (now >= (begin + 2)) |
| 319 | { |
| 320 | clib_warning ("peer unresponsive, give up"); |
| 321 | am->my_client_index = ~0; |
| 322 | am->my_registration = 0; |
| 323 | am->shmem_hdr = 0; |
| 324 | break; |
| 325 | } |
| 326 | if (unix_shared_memory_queue_sub (vl_input_queue, (u8 *) & rp, 1) < 0) |
| 327 | continue; |
| 328 | |
| 329 | /* drain the queue */ |
| 330 | if (ntohs (rp->_vl_msg_id) != VL_API_MEMCLNT_DELETE_REPLY) |
| 331 | { |
Dave Barach | cf5e848 | 2017-10-17 11:48:29 -0400 | [diff] [blame] | 332 | clib_warning ("queue drain: %d", ntohs (rp->_vl_msg_id)); |
Dave Barach | 5c6c4bf | 2017-04-11 13:12:48 -0400 | [diff] [blame] | 333 | vl_msg_api_handler ((void *) rp); |
| 334 | continue; |
| 335 | } |
| 336 | vl_msg_api_handler ((void *) rp); |
| 337 | break; |
| 338 | } |
| 339 | } |
| 340 | |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 341 | /** |
| 342 | * Stave off the binary API dead client reaper |
| 343 | * Only sent to inactive clients |
| 344 | */ |
| 345 | static void |
| 346 | vl_api_memclnt_keepalive_t_handler (vl_api_memclnt_keepalive_t * mp) |
| 347 | { |
| 348 | vl_api_memclnt_keepalive_reply_t *rmp; |
| 349 | api_main_t *am; |
| 350 | vl_shmem_hdr_t *shmem_hdr; |
| 351 | |
| 352 | am = &api_main; |
| 353 | shmem_hdr = am->shmem_hdr; |
| 354 | |
| 355 | rmp = vl_msg_api_alloc_as_if_client (sizeof (*rmp)); |
| 356 | memset (rmp, 0, sizeof (*rmp)); |
| 357 | rmp->_vl_msg_id = ntohs (VL_API_MEMCLNT_KEEPALIVE_REPLY); |
| 358 | rmp->context = mp->context; |
| 359 | vl_msg_api_send_shmem (shmem_hdr->vl_input_queue, (u8 *) & rmp); |
| 360 | } |
| 361 | |
Dave Barach | 5c6c4bf | 2017-04-11 13:12:48 -0400 | [diff] [blame] | 362 | #define foreach_api_msg \ |
| 363 | _(RX_THREAD_EXIT, rx_thread_exit) \ |
| 364 | _(MEMCLNT_CREATE_REPLY, memclnt_create_reply) \ |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 365 | _(MEMCLNT_DELETE_REPLY, memclnt_delete_reply) \ |
| 366 | _(MEMCLNT_KEEPALIVE, memclnt_keepalive) |
Dave Barach | 5c6c4bf | 2017-04-11 13:12:48 -0400 | [diff] [blame] | 367 | |
| 368 | |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 369 | void |
| 370 | vl_client_install_client_message_handlers (void) |
Dave Barach | 5c6c4bf | 2017-04-11 13:12:48 -0400 | [diff] [blame] | 371 | { |
Dave Barach | 5c6c4bf | 2017-04-11 13:12:48 -0400 | [diff] [blame] | 372 | |
| 373 | #define _(N,n) \ |
| 374 | vl_msg_api_set_handlers(VL_API_##N, #n, \ |
| 375 | vl_api_##n##_t_handler, \ |
| 376 | noop_handler, \ |
| 377 | vl_api_##n##_t_endian, \ |
| 378 | vl_api_##n##_t_print, \ |
| 379 | sizeof(vl_api_##n##_t), 1); |
| 380 | foreach_api_msg; |
| 381 | #undef _ |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 382 | } |
| 383 | |
| 384 | |
| 385 | int |
| 386 | vl_client_api_map (const char *region_name) |
| 387 | { |
| 388 | int rv; |
| 389 | |
| 390 | if ((rv = vl_map_shmem (region_name, 0 /* is_vlib */ )) < 0) |
| 391 | return rv; |
| 392 | |
| 393 | vl_client_install_client_message_handlers (); |
Dave Barach | 5c6c4bf | 2017-04-11 13:12:48 -0400 | [diff] [blame] | 394 | return 0; |
| 395 | } |
| 396 | |
| 397 | void |
| 398 | vl_client_api_unmap (void) |
| 399 | { |
| 400 | vl_unmap_shmem (); |
| 401 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 402 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 403 | static int |
Neale Ranns | e72be39 | 2017-04-26 13:59:20 -0700 | [diff] [blame] | 404 | connect_to_vlib_internal (const char *svm_name, |
| 405 | const char *client_name, |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 406 | int rx_queue_size, int want_pthread, int do_map) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 407 | { |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 408 | int rv = 0; |
| 409 | memory_client_main_t *mm = &memory_client_main; |
| 410 | |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 411 | if (do_map && (rv = vl_client_api_map (svm_name))) |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 412 | { |
| 413 | clib_warning ("vl_client_api map rv %d", rv); |
| 414 | return rv; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 415 | } |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 416 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 417 | if (vl_client_connect (client_name, 0 /* punt quota */ , |
| 418 | rx_queue_size /* input queue */ ) < 0) |
| 419 | { |
| 420 | vl_client_api_unmap (); |
| 421 | return -1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 422 | } |
| 423 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 424 | /* Start the rx queue thread */ |
| 425 | |
| 426 | if (want_pthread) |
| 427 | { |
| 428 | rv = pthread_create (&mm->rx_thread_handle, |
| 429 | NULL /*attr */ , rx_thread_fn, 0); |
| 430 | if (rv) |
| 431 | clib_warning ("pthread_create returned %d", rv); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 432 | } |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 433 | |
| 434 | mm->connected_to_vlib = 1; |
| 435 | return 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 436 | } |
| 437 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 438 | int |
Neale Ranns | e72be39 | 2017-04-26 13:59:20 -0700 | [diff] [blame] | 439 | vl_client_connect_to_vlib (const char *svm_name, |
| 440 | const char *client_name, int rx_queue_size) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 441 | { |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 442 | return connect_to_vlib_internal (svm_name, client_name, rx_queue_size, |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 443 | 1 /* want pthread */ , |
| 444 | 1 /* do map */ ); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 445 | } |
| 446 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 447 | int |
Neale Ranns | e72be39 | 2017-04-26 13:59:20 -0700 | [diff] [blame] | 448 | vl_client_connect_to_vlib_no_rx_pthread (const char *svm_name, |
| 449 | const char *client_name, |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 450 | int rx_queue_size) |
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 | return connect_to_vlib_internal (svm_name, client_name, rx_queue_size, |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 453 | 0 /* want pthread */ , |
| 454 | 1 /* do map */ ); |
| 455 | } |
| 456 | |
| 457 | int |
| 458 | vl_client_connect_to_vlib_no_map (const char *svm_name, |
| 459 | const char *client_name, int rx_queue_size) |
| 460 | { |
| 461 | return connect_to_vlib_internal (svm_name, client_name, rx_queue_size, |
| 462 | 1 /* want pthread */ , |
| 463 | 0 /* dont map */ ); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 464 | } |
| 465 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 466 | void |
| 467 | vl_client_disconnect_from_vlib (void) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 468 | { |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 469 | memory_client_main_t *mm = &memory_client_main; |
| 470 | api_main_t *am = &api_main; |
| 471 | uword junk; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 472 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 473 | if (mm->rx_thread_jmpbuf_valid) |
| 474 | { |
| 475 | vl_api_rx_thread_exit_t *ep; |
| 476 | ep = vl_msg_api_alloc (sizeof (*ep)); |
| 477 | ep->_vl_msg_id = ntohs (VL_API_RX_THREAD_EXIT); |
| 478 | vl_msg_api_send_shmem (am->vl_input_queue, (u8 *) & ep); |
| 479 | pthread_join (mm->rx_thread_handle, (void **) &junk); |
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 (mm->connected_to_vlib) |
| 482 | { |
| 483 | vl_client_disconnect (); |
| 484 | vl_client_api_unmap (); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 485 | } |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 486 | memset (mm, 0, sizeof (*mm)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 487 | } |
| 488 | |
| 489 | static void vl_api_get_first_msg_id_reply_t_handler |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 490 | (vl_api_get_first_msg_id_reply_t * mp) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 491 | { |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 492 | memory_client_main_t *mm = &memory_client_main; |
| 493 | i32 retval = ntohl (mp->retval); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 494 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 495 | mm->first_msg_id_reply = (retval >= 0) ? ntohs (mp->first_msg_id) : ~0; |
| 496 | mm->first_msg_id_reply_ready = 1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 497 | } |
| 498 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 499 | u16 |
Neale Ranns | e72be39 | 2017-04-26 13:59:20 -0700 | [diff] [blame] | 500 | vl_client_get_first_plugin_msg_id (const char *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 | vl_api_get_first_msg_id_t *mp; |
| 503 | api_main_t *am = &api_main; |
| 504 | memory_client_main_t *mm = &memory_client_main; |
| 505 | f64 timeout; |
| 506 | void *old_handler; |
| 507 | clib_time_t clib_time; |
| 508 | u16 rv = ~0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 509 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 510 | if (strlen (plugin_name) + 1 > sizeof (mp->name)) |
| 511 | return (rv); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 512 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 513 | memset (&clib_time, 0, sizeof (clib_time)); |
| 514 | clib_time_init (&clib_time); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 515 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 516 | /* Push this plugin's first_msg_id_reply handler */ |
| 517 | old_handler = am->msg_handlers[VL_API_GET_FIRST_MSG_ID_REPLY]; |
| 518 | am->msg_handlers[VL_API_GET_FIRST_MSG_ID_REPLY] = (void *) |
| 519 | vl_api_get_first_msg_id_reply_t_handler; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 520 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 521 | /* Ask the data-plane for the message-ID base of the indicated plugin */ |
| 522 | mm->first_msg_id_reply_ready = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 523 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 524 | mp = vl_msg_api_alloc (sizeof (*mp)); |
| 525 | memset (mp, 0, sizeof (*mp)); |
| 526 | mp->_vl_msg_id = ntohs (VL_API_GET_FIRST_MSG_ID); |
| 527 | mp->client_index = am->my_client_index; |
| 528 | strncpy ((char *) mp->name, plugin_name, sizeof (mp->name) - 1); |
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 | 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] | 531 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 532 | /* Synchronously wait for the answer */ |
| 533 | do |
| 534 | { |
| 535 | timeout = clib_time_now (&clib_time) + 1.0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 536 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 537 | while (clib_time_now (&clib_time) < timeout) |
| 538 | { |
| 539 | if (mm->first_msg_id_reply_ready == 1) |
| 540 | { |
| 541 | rv = mm->first_msg_id_reply; |
| 542 | goto result; |
| 543 | } |
| 544 | } |
| 545 | /* Restore old handler */ |
| 546 | am->msg_handlers[VL_API_GET_FIRST_MSG_ID_REPLY] = old_handler; |
| 547 | |
| 548 | return rv; |
| 549 | } |
| 550 | while (0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 551 | |
| 552 | result: |
| 553 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 554 | /* Restore the old handler */ |
| 555 | am->msg_handlers[VL_API_GET_FIRST_MSG_ID_REPLY] = old_handler; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 556 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 557 | if (rv == (u16) ~ 0) |
| 558 | clib_warning ("plugin '%s' not registered", plugin_name); |
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 | return rv; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 561 | } |
Dave Barach | 6931f59 | 2016-05-13 12:55:01 -0400 | [diff] [blame] | 562 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 563 | /* |
| 564 | * fd.io coding-style-patch-verification: ON |
| 565 | * |
| 566 | * Local Variables: |
| 567 | * eval: (c-set-style "gnu") |
| 568 | * End: |
| 569 | */ |