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 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 20 | #include <setjmp.h> |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 21 | |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 22 | #include <svm/svm.h> |
Florin Coras | 4d9b9d8 | 2018-01-14 12:25:50 -0800 | [diff] [blame] | 23 | #include <svm/ssvm.h> |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 24 | #include <vppinfra/serialize.h> |
| 25 | #include <vppinfra/hash.h> |
| 26 | #include <vlibmemory/memory_client.h> |
| 27 | |
| 28 | /* A hack. vl_client_get_first_plugin_msg_id depends on it */ |
| 29 | #include <vlibmemory/socket_client.h> |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 30 | |
| 31 | #include <vlibmemory/vl_memory_msg_enum.h> |
| 32 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 33 | #define vl_typedefs /* define message structures */ |
| 34 | #include <vlibmemory/vl_memory_api_h.h> |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 35 | #undef vl_typedefs |
| 36 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 37 | #define vl_endianfun /* define message structures */ |
| 38 | #include <vlibmemory/vl_memory_api_h.h> |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 39 | #undef vl_endianfun |
| 40 | |
| 41 | /* instantiate all the print functions we know about */ |
| 42 | #define vl_print(handle, ...) clib_warning (__VA_ARGS__) |
| 43 | #define vl_printfun |
| 44 | #include <vlibmemory/vl_memory_api_h.h> |
| 45 | #undef vl_printfun |
| 46 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 47 | typedef struct |
| 48 | { |
| 49 | u8 rx_thread_jmpbuf_valid; |
| 50 | u8 connected_to_vlib; |
| 51 | jmp_buf rx_thread_jmpbuf; |
| 52 | pthread_t rx_thread_handle; |
| 53 | /* Plugin message base lookup scheme */ |
| 54 | volatile u8 first_msg_id_reply_ready; |
| 55 | u16 first_msg_id_reply; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 56 | } memory_client_main_t; |
| 57 | |
| 58 | memory_client_main_t memory_client_main; |
| 59 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 60 | static void * |
| 61 | rx_thread_fn (void *arg) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 62 | { |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 63 | svm_queue_t *q; |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 64 | memory_client_main_t *mm = &memory_client_main; |
| 65 | api_main_t *am = &api_main; |
Dave Barach | cf5e848 | 2017-10-17 11:48:29 -0400 | [diff] [blame] | 66 | int i; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 67 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 68 | q = am->vl_input_queue; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 69 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 70 | /* So we can make the rx thread terminate cleanly */ |
| 71 | if (setjmp (mm->rx_thread_jmpbuf) == 0) |
| 72 | { |
| 73 | mm->rx_thread_jmpbuf_valid = 1; |
Dave Barach | cf5e848 | 2017-10-17 11:48:29 -0400 | [diff] [blame] | 74 | /* |
| 75 | * Find an unused slot in the per-cpu-mheaps array, |
| 76 | * and grab it for this thread. We need to be able to |
| 77 | * push/pop the thread heap without affecting other thread(s). |
| 78 | */ |
| 79 | if (__os_thread_index == 0) |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 80 | { |
Dave Barach | cf5e848 | 2017-10-17 11:48:29 -0400 | [diff] [blame] | 81 | for (i = 0; i < ARRAY_LEN (clib_per_cpu_mheaps); i++) |
| 82 | { |
| 83 | if (clib_per_cpu_mheaps[i] == 0) |
| 84 | { |
| 85 | /* Copy the main thread mheap pointer */ |
| 86 | clib_per_cpu_mheaps[i] = clib_per_cpu_mheaps[0]; |
| 87 | __os_thread_index = i; |
| 88 | break; |
| 89 | } |
| 90 | } |
| 91 | ASSERT (__os_thread_index > 0); |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 92 | } |
Dave Barach | cf5e848 | 2017-10-17 11:48:29 -0400 | [diff] [blame] | 93 | while (1) |
| 94 | vl_msg_api_queue_handler (q); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 95 | } |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 96 | pthread_exit (0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 97 | } |
| 98 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 99 | static void |
| 100 | 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] | 101 | { |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 102 | memory_client_main_t *mm = &memory_client_main; |
| 103 | vl_msg_api_free (mp); |
| 104 | longjmp (mm->rx_thread_jmpbuf, 1); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 105 | } |
| 106 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 107 | static void |
Dave Barach | 5c6c4bf | 2017-04-11 13:12:48 -0400 | [diff] [blame] | 108 | vl_api_memclnt_create_reply_t_handler (vl_api_memclnt_create_reply_t * mp) |
| 109 | { |
| 110 | serialize_main_t _sm, *sm = &_sm; |
| 111 | api_main_t *am = &api_main; |
| 112 | u8 *tblv; |
| 113 | u32 nmsgs; |
| 114 | int i; |
| 115 | u8 *name_and_crc; |
| 116 | u32 msg_index; |
| 117 | |
| 118 | am->my_client_index = mp->index; |
| 119 | am->my_registration = (vl_api_registration_t *) (uword) mp->handle; |
| 120 | |
| 121 | /* Clean out any previous hash table (unlikely) */ |
| 122 | if (am->msg_index_by_name_and_crc) |
| 123 | { |
| 124 | int i; |
| 125 | u8 **keys = 0; |
| 126 | hash_pair_t *hp; |
| 127 | /* *INDENT-OFF* */ |
| 128 | hash_foreach_pair (hp, am->msg_index_by_name_and_crc, |
| 129 | ({ |
| 130 | vec_add1 (keys, (u8 *) hp->key); |
| 131 | })); |
| 132 | /* *INDENT-ON* */ |
| 133 | for (i = 0; i < vec_len (keys); i++) |
| 134 | vec_free (keys[i]); |
| 135 | vec_free (keys); |
| 136 | } |
| 137 | |
| 138 | am->msg_index_by_name_and_crc = hash_create_string (0, sizeof (uword)); |
| 139 | |
| 140 | /* Recreate the vnet-side API message handler table */ |
Damjan Marion | 7bee80c | 2017-04-26 15:32:12 +0200 | [diff] [blame] | 141 | tblv = uword_to_pointer (mp->message_table, u8 *); |
Dave Barach | cf5e848 | 2017-10-17 11:48:29 -0400 | [diff] [blame] | 142 | unserialize_open_data (sm, tblv, vec_len (tblv)); |
Dave Barach | 5c6c4bf | 2017-04-11 13:12:48 -0400 | [diff] [blame] | 143 | unserialize_integer (sm, &nmsgs, sizeof (u32)); |
| 144 | |
| 145 | for (i = 0; i < nmsgs; i++) |
| 146 | { |
| 147 | msg_index = unserialize_likely_small_unsigned_integer (sm); |
| 148 | unserialize_cstring (sm, (char **) &name_and_crc); |
| 149 | hash_set_mem (am->msg_index_by_name_and_crc, name_and_crc, msg_index); |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | static void |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 154 | noop_handler (void *notused) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 155 | { |
| 156 | } |
| 157 | |
Dave Barach | 5c6c4bf | 2017-04-11 13:12:48 -0400 | [diff] [blame] | 158 | int |
Neale Ranns | e72be39 | 2017-04-26 13:59:20 -0700 | [diff] [blame] | 159 | 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] | 160 | { |
| 161 | svm_region_t *svm; |
| 162 | vl_api_memclnt_create_t *mp; |
| 163 | vl_api_memclnt_create_reply_t *rp; |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 164 | svm_queue_t *vl_input_queue; |
Dave Barach | 5c6c4bf | 2017-04-11 13:12:48 -0400 | [diff] [blame] | 165 | vl_shmem_hdr_t *shmem_hdr; |
| 166 | int rv = 0; |
| 167 | void *oldheap; |
| 168 | api_main_t *am = &api_main; |
| 169 | |
| 170 | if (am->my_registration) |
| 171 | { |
| 172 | clib_warning ("client %s already connected...", name); |
| 173 | return -1; |
| 174 | } |
| 175 | |
| 176 | if (am->vlib_rp == 0) |
| 177 | { |
| 178 | clib_warning ("am->vlib_rp NULL"); |
| 179 | return -1; |
| 180 | } |
| 181 | |
| 182 | svm = am->vlib_rp; |
| 183 | shmem_hdr = am->shmem_hdr; |
| 184 | |
| 185 | if (shmem_hdr == 0 || shmem_hdr->vl_input_queue == 0) |
| 186 | { |
| 187 | clib_warning ("shmem_hdr / input queue NULL"); |
| 188 | return -1; |
| 189 | } |
| 190 | |
| 191 | pthread_mutex_lock (&svm->mutex); |
| 192 | oldheap = svm_push_data_heap (svm); |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 193 | vl_input_queue = svm_queue_init (input_queue_size, sizeof (uword), |
| 194 | getpid (), 0); |
Dave Barach | 5c6c4bf | 2017-04-11 13:12:48 -0400 | [diff] [blame] | 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 | { |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 220 | qstatus = svm_queue_sub (vl_input_queue, (u8 *) & rp, |
Mohsin Kazmi | 3fca567 | 2018-01-04 18:57:26 +0100 | [diff] [blame] | 221 | SVM_Q_NOWAIT, 0); |
Dave Barach | 5c6c4bf | 2017-04-11 13:12:48 -0400 | [diff] [blame] | 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); |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 255 | svm_queue_free (am->vl_input_queue); |
Dave Barach | 5c6c4bf | 2017-04-11 13:12:48 -0400 | [diff] [blame] | 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; |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 269 | svm_queue_t *vl_input_queue; |
Dave Barach | 5c6c4bf | 2017-04-11 13:12:48 -0400 | [diff] [blame] | 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 | */ |
Dave Barach | 5c6c4bf | 2017-04-11 13:12:48 -0400 | [diff] [blame] | 292 | begin = time (0); |
| 293 | while (1) |
| 294 | { |
| 295 | time_t now; |
| 296 | |
| 297 | now = time (0); |
| 298 | |
| 299 | if (now >= (begin + 2)) |
| 300 | { |
| 301 | clib_warning ("peer unresponsive, give up"); |
| 302 | am->my_client_index = ~0; |
| 303 | am->my_registration = 0; |
| 304 | am->shmem_hdr = 0; |
| 305 | break; |
| 306 | } |
Mohsin Kazmi | 3fca567 | 2018-01-04 18:57:26 +0100 | [diff] [blame] | 307 | if (svm_queue_sub (vl_input_queue, (u8 *) & rp, SVM_Q_NOWAIT, 0) < 0) |
Dave Barach | 5c6c4bf | 2017-04-11 13:12:48 -0400 | [diff] [blame] | 308 | continue; |
| 309 | |
| 310 | /* drain the queue */ |
| 311 | if (ntohs (rp->_vl_msg_id) != VL_API_MEMCLNT_DELETE_REPLY) |
| 312 | { |
Dave Barach | cf5e848 | 2017-10-17 11:48:29 -0400 | [diff] [blame] | 313 | clib_warning ("queue drain: %d", ntohs (rp->_vl_msg_id)); |
Dave Barach | 5c6c4bf | 2017-04-11 13:12:48 -0400 | [diff] [blame] | 314 | vl_msg_api_handler ((void *) rp); |
| 315 | continue; |
| 316 | } |
| 317 | vl_msg_api_handler ((void *) rp); |
| 318 | break; |
| 319 | } |
| 320 | } |
| 321 | |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 322 | /** |
| 323 | * Stave off the binary API dead client reaper |
| 324 | * Only sent to inactive clients |
| 325 | */ |
| 326 | static void |
| 327 | vl_api_memclnt_keepalive_t_handler (vl_api_memclnt_keepalive_t * mp) |
| 328 | { |
| 329 | vl_api_memclnt_keepalive_reply_t *rmp; |
| 330 | api_main_t *am; |
| 331 | vl_shmem_hdr_t *shmem_hdr; |
| 332 | |
| 333 | am = &api_main; |
| 334 | shmem_hdr = am->shmem_hdr; |
| 335 | |
| 336 | rmp = vl_msg_api_alloc_as_if_client (sizeof (*rmp)); |
| 337 | memset (rmp, 0, sizeof (*rmp)); |
| 338 | rmp->_vl_msg_id = ntohs (VL_API_MEMCLNT_KEEPALIVE_REPLY); |
| 339 | rmp->context = mp->context; |
| 340 | vl_msg_api_send_shmem (shmem_hdr->vl_input_queue, (u8 *) & rmp); |
| 341 | } |
| 342 | |
Dave Barach | 5c6c4bf | 2017-04-11 13:12:48 -0400 | [diff] [blame] | 343 | #define foreach_api_msg \ |
| 344 | _(RX_THREAD_EXIT, rx_thread_exit) \ |
| 345 | _(MEMCLNT_CREATE_REPLY, memclnt_create_reply) \ |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 346 | _(MEMCLNT_DELETE_REPLY, memclnt_delete_reply) \ |
| 347 | _(MEMCLNT_KEEPALIVE, memclnt_keepalive) |
Dave Barach | 5c6c4bf | 2017-04-11 13:12:48 -0400 | [diff] [blame] | 348 | |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 349 | void |
| 350 | vl_client_install_client_message_handlers (void) |
Dave Barach | 5c6c4bf | 2017-04-11 13:12:48 -0400 | [diff] [blame] | 351 | { |
Dave Barach | 5c6c4bf | 2017-04-11 13:12:48 -0400 | [diff] [blame] | 352 | |
| 353 | #define _(N,n) \ |
| 354 | vl_msg_api_set_handlers(VL_API_##N, #n, \ |
| 355 | vl_api_##n##_t_handler, \ |
| 356 | noop_handler, \ |
| 357 | vl_api_##n##_t_endian, \ |
| 358 | vl_api_##n##_t_print, \ |
| 359 | sizeof(vl_api_##n##_t), 1); |
| 360 | foreach_api_msg; |
| 361 | #undef _ |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 362 | } |
| 363 | |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 364 | int |
| 365 | vl_client_api_map (const char *region_name) |
| 366 | { |
| 367 | int rv; |
| 368 | |
| 369 | if ((rv = vl_map_shmem (region_name, 0 /* is_vlib */ )) < 0) |
| 370 | return rv; |
| 371 | |
| 372 | vl_client_install_client_message_handlers (); |
Dave Barach | 5c6c4bf | 2017-04-11 13:12:48 -0400 | [diff] [blame] | 373 | return 0; |
| 374 | } |
| 375 | |
| 376 | void |
| 377 | vl_client_api_unmap (void) |
| 378 | { |
| 379 | vl_unmap_shmem (); |
| 380 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 381 | |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 382 | u8 |
| 383 | vl_mem_client_is_connected (void) |
| 384 | { |
| 385 | return (memory_client_main.connected_to_vlib != 0); |
| 386 | } |
| 387 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 388 | static int |
Neale Ranns | e72be39 | 2017-04-26 13:59:20 -0700 | [diff] [blame] | 389 | connect_to_vlib_internal (const char *svm_name, |
| 390 | const char *client_name, |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 391 | int rx_queue_size, int want_pthread, int do_map) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 392 | { |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 393 | int rv = 0; |
| 394 | memory_client_main_t *mm = &memory_client_main; |
| 395 | |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 396 | if (do_map && (rv = vl_client_api_map (svm_name))) |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 397 | { |
| 398 | clib_warning ("vl_client_api map rv %d", rv); |
| 399 | return rv; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 400 | } |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 401 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 402 | if (vl_client_connect (client_name, 0 /* punt quota */ , |
| 403 | rx_queue_size /* input queue */ ) < 0) |
| 404 | { |
| 405 | vl_client_api_unmap (); |
| 406 | return -1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 407 | } |
| 408 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 409 | /* Start the rx queue thread */ |
| 410 | |
| 411 | if (want_pthread) |
| 412 | { |
| 413 | rv = pthread_create (&mm->rx_thread_handle, |
| 414 | NULL /*attr */ , rx_thread_fn, 0); |
| 415 | if (rv) |
| 416 | clib_warning ("pthread_create returned %d", rv); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 417 | } |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 418 | |
| 419 | mm->connected_to_vlib = 1; |
| 420 | return 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 421 | } |
| 422 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 423 | int |
Neale Ranns | e72be39 | 2017-04-26 13:59:20 -0700 | [diff] [blame] | 424 | vl_client_connect_to_vlib (const char *svm_name, |
| 425 | const char *client_name, int rx_queue_size) |
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 | return connect_to_vlib_internal (svm_name, client_name, rx_queue_size, |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 428 | 1 /* want pthread */ , |
| 429 | 1 /* do map */ ); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 430 | } |
| 431 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 432 | int |
Neale Ranns | e72be39 | 2017-04-26 13:59:20 -0700 | [diff] [blame] | 433 | vl_client_connect_to_vlib_no_rx_pthread (const char *svm_name, |
| 434 | const char *client_name, |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 435 | int rx_queue_size) |
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 | return connect_to_vlib_internal (svm_name, client_name, rx_queue_size, |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 438 | 0 /* want pthread */ , |
| 439 | 1 /* do map */ ); |
| 440 | } |
| 441 | |
| 442 | int |
| 443 | vl_client_connect_to_vlib_no_map (const char *svm_name, |
| 444 | const char *client_name, int rx_queue_size) |
| 445 | { |
| 446 | return connect_to_vlib_internal (svm_name, client_name, rx_queue_size, |
| 447 | 1 /* want pthread */ , |
| 448 | 0 /* dont map */ ); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 449 | } |
| 450 | |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 451 | static void |
| 452 | disconnect_from_vlib_internal (u8 do_unmap) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 453 | { |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 454 | memory_client_main_t *mm = &memory_client_main; |
| 455 | api_main_t *am = &api_main; |
| 456 | uword junk; |
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 | if (mm->rx_thread_jmpbuf_valid) |
| 459 | { |
| 460 | vl_api_rx_thread_exit_t *ep; |
| 461 | ep = vl_msg_api_alloc (sizeof (*ep)); |
| 462 | ep->_vl_msg_id = ntohs (VL_API_RX_THREAD_EXIT); |
| 463 | vl_msg_api_send_shmem (am->vl_input_queue, (u8 *) & ep); |
| 464 | pthread_join (mm->rx_thread_handle, (void **) &junk); |
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 | if (mm->connected_to_vlib) |
| 467 | { |
| 468 | vl_client_disconnect (); |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 469 | if (do_unmap) |
| 470 | vl_client_api_unmap (); |
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 | memset (mm, 0, sizeof (*mm)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 473 | } |
| 474 | |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 475 | void |
| 476 | vl_client_disconnect_from_vlib (void) |
| 477 | { |
| 478 | disconnect_from_vlib_internal (1); |
| 479 | } |
| 480 | |
| 481 | void |
| 482 | vl_client_disconnect_from_vlib_no_unmap (void) |
| 483 | { |
| 484 | disconnect_from_vlib_internal (0); |
| 485 | } |
| 486 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 487 | static void vl_api_get_first_msg_id_reply_t_handler |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 488 | (vl_api_get_first_msg_id_reply_t * mp) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 489 | { |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 490 | memory_client_main_t *mm = &memory_client_main; |
| 491 | i32 retval = ntohl (mp->retval); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 492 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 493 | mm->first_msg_id_reply = (retval >= 0) ? ntohs (mp->first_msg_id) : ~0; |
| 494 | mm->first_msg_id_reply_ready = 1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 495 | } |
| 496 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 497 | u16 |
Neale Ranns | e72be39 | 2017-04-26 13:59:20 -0700 | [diff] [blame] | 498 | vl_client_get_first_plugin_msg_id (const char *plugin_name) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 499 | { |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 500 | vl_api_get_first_msg_id_t *mp; |
| 501 | api_main_t *am = &api_main; |
| 502 | memory_client_main_t *mm = &memory_client_main; |
| 503 | f64 timeout; |
| 504 | void *old_handler; |
| 505 | clib_time_t clib_time; |
| 506 | u16 rv = ~0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 507 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 508 | if (strlen (plugin_name) + 1 > sizeof (mp->name)) |
| 509 | return (rv); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 510 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 511 | memset (&clib_time, 0, sizeof (clib_time)); |
| 512 | clib_time_init (&clib_time); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 513 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 514 | /* Push this plugin's first_msg_id_reply handler */ |
| 515 | old_handler = am->msg_handlers[VL_API_GET_FIRST_MSG_ID_REPLY]; |
| 516 | am->msg_handlers[VL_API_GET_FIRST_MSG_ID_REPLY] = (void *) |
| 517 | vl_api_get_first_msg_id_reply_t_handler; |
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 | /* Ask the data-plane for the message-ID base of the indicated plugin */ |
| 520 | mm->first_msg_id_reply_ready = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 521 | |
Florin Coras | 6d1caf9 | 2018-01-04 12:17:10 -0800 | [diff] [blame] | 522 | /* Not using shm client */ |
| 523 | if (!am->my_registration) |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 524 | { |
Florin Coras | 6d1caf9 | 2018-01-04 12:17:10 -0800 | [diff] [blame] | 525 | mp = vl_socket_client_msg_alloc (sizeof (*mp)); |
| 526 | memset (mp, 0, sizeof (*mp)); |
| 527 | mp->_vl_msg_id = ntohs (VL_API_GET_FIRST_MSG_ID); |
| 528 | mp->client_index = am->my_client_index; |
| 529 | strncpy ((char *) mp->name, plugin_name, sizeof (mp->name) - 1); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 530 | |
Florin Coras | 6d1caf9 | 2018-01-04 12:17:10 -0800 | [diff] [blame] | 531 | if (vl_socket_client_write () <= 0) |
| 532 | goto sock_err; |
| 533 | if (vl_socket_client_read (1)) |
| 534 | goto sock_err; |
| 535 | |
| 536 | if (mm->first_msg_id_reply_ready == 1) |
| 537 | { |
| 538 | rv = mm->first_msg_id_reply; |
| 539 | goto result; |
| 540 | } |
| 541 | |
| 542 | sock_err: |
| 543 | /* Restore old handler */ |
| 544 | am->msg_handlers[VL_API_GET_FIRST_MSG_ID_REPLY] = old_handler; |
| 545 | |
| 546 | return -1; |
| 547 | } |
| 548 | else |
| 549 | { |
| 550 | mp = vl_msg_api_alloc (sizeof (*mp)); |
| 551 | memset (mp, 0, sizeof (*mp)); |
| 552 | mp->_vl_msg_id = ntohs (VL_API_GET_FIRST_MSG_ID); |
| 553 | mp->client_index = am->my_client_index; |
| 554 | strncpy ((char *) mp->name, plugin_name, sizeof (mp->name) - 1); |
| 555 | |
| 556 | vl_msg_api_send_shmem (am->shmem_hdr->vl_input_queue, (u8 *) & mp); |
| 557 | |
| 558 | /* Synchronously wait for the answer */ |
| 559 | timeout = clib_time_now (&clib_time) + 1.0; |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 560 | while (clib_time_now (&clib_time) < timeout) |
| 561 | { |
| 562 | if (mm->first_msg_id_reply_ready == 1) |
| 563 | { |
| 564 | rv = mm->first_msg_id_reply; |
| 565 | goto result; |
| 566 | } |
| 567 | } |
| 568 | /* Restore old handler */ |
| 569 | am->msg_handlers[VL_API_GET_FIRST_MSG_ID_REPLY] = old_handler; |
| 570 | |
| 571 | return rv; |
| 572 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 573 | |
| 574 | result: |
| 575 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 576 | /* Restore the old handler */ |
| 577 | am->msg_handlers[VL_API_GET_FIRST_MSG_ID_REPLY] = old_handler; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 578 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 579 | if (rv == (u16) ~ 0) |
| 580 | clib_warning ("plugin '%s' not registered", plugin_name); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 581 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 582 | return rv; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 583 | } |
Dave Barach | 6931f59 | 2016-05-13 12:55:01 -0400 | [diff] [blame] | 584 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 585 | /* |
| 586 | * fd.io coding-style-patch-verification: ON |
| 587 | * |
| 588 | * Local Variables: |
| 589 | * eval: (c-set-style "gnu") |
| 590 | * End: |
| 591 | */ |