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 |
Ole Troan | 73710c7 | 2018-06-04 22:27:49 +0200 | [diff] [blame] | 108 | vl_api_name_and_crc_free (void) |
| 109 | { |
| 110 | api_main_t *am = &api_main; |
| 111 | int i; |
| 112 | u8 **keys = 0; |
| 113 | hash_pair_t *hp; |
| 114 | |
| 115 | if (!am->msg_index_by_name_and_crc) |
| 116 | return; |
| 117 | |
| 118 | /* *INDENT-OFF* */ |
| 119 | hash_foreach_pair (hp, am->msg_index_by_name_and_crc, |
| 120 | ({ |
| 121 | vec_add1 (keys, (u8 *) hp->key); |
| 122 | })); |
| 123 | /* *INDENT-ON* */ |
| 124 | for (i = 0; i < vec_len (keys); i++) |
| 125 | vec_free (keys[i]); |
| 126 | vec_free (keys); |
| 127 | hash_free (am->msg_index_by_name_and_crc); |
| 128 | } |
| 129 | |
| 130 | static void |
Dave Barach | 5c6c4bf | 2017-04-11 13:12:48 -0400 | [diff] [blame] | 131 | vl_api_memclnt_create_reply_t_handler (vl_api_memclnt_create_reply_t * mp) |
| 132 | { |
| 133 | serialize_main_t _sm, *sm = &_sm; |
| 134 | api_main_t *am = &api_main; |
| 135 | u8 *tblv; |
| 136 | u32 nmsgs; |
| 137 | int i; |
| 138 | u8 *name_and_crc; |
| 139 | u32 msg_index; |
| 140 | |
| 141 | am->my_client_index = mp->index; |
| 142 | am->my_registration = (vl_api_registration_t *) (uword) mp->handle; |
| 143 | |
| 144 | /* Clean out any previous hash table (unlikely) */ |
Ole Troan | 73710c7 | 2018-06-04 22:27:49 +0200 | [diff] [blame] | 145 | vl_api_name_and_crc_free (); |
Dave Barach | 5c6c4bf | 2017-04-11 13:12:48 -0400 | [diff] [blame] | 146 | |
| 147 | am->msg_index_by_name_and_crc = hash_create_string (0, sizeof (uword)); |
| 148 | |
| 149 | /* Recreate the vnet-side API message handler table */ |
Damjan Marion | 7bee80c | 2017-04-26 15:32:12 +0200 | [diff] [blame] | 150 | tblv = uword_to_pointer (mp->message_table, u8 *); |
Dave Barach | cf5e848 | 2017-10-17 11:48:29 -0400 | [diff] [blame] | 151 | unserialize_open_data (sm, tblv, vec_len (tblv)); |
Dave Barach | 5c6c4bf | 2017-04-11 13:12:48 -0400 | [diff] [blame] | 152 | unserialize_integer (sm, &nmsgs, sizeof (u32)); |
| 153 | |
| 154 | for (i = 0; i < nmsgs; i++) |
| 155 | { |
| 156 | msg_index = unserialize_likely_small_unsigned_integer (sm); |
| 157 | unserialize_cstring (sm, (char **) &name_and_crc); |
| 158 | hash_set_mem (am->msg_index_by_name_and_crc, name_and_crc, msg_index); |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | static void |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 163 | noop_handler (void *notused) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 164 | { |
| 165 | } |
| 166 | |
Dave Barach | 5c6c4bf | 2017-04-11 13:12:48 -0400 | [diff] [blame] | 167 | int |
Neale Ranns | e72be39 | 2017-04-26 13:59:20 -0700 | [diff] [blame] | 168 | 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] | 169 | { |
| 170 | svm_region_t *svm; |
| 171 | vl_api_memclnt_create_t *mp; |
| 172 | vl_api_memclnt_create_reply_t *rp; |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 173 | svm_queue_t *vl_input_queue; |
Dave Barach | 5c6c4bf | 2017-04-11 13:12:48 -0400 | [diff] [blame] | 174 | vl_shmem_hdr_t *shmem_hdr; |
| 175 | int rv = 0; |
| 176 | void *oldheap; |
| 177 | api_main_t *am = &api_main; |
| 178 | |
| 179 | if (am->my_registration) |
| 180 | { |
| 181 | clib_warning ("client %s already connected...", name); |
| 182 | return -1; |
| 183 | } |
| 184 | |
| 185 | if (am->vlib_rp == 0) |
| 186 | { |
| 187 | clib_warning ("am->vlib_rp NULL"); |
| 188 | return -1; |
| 189 | } |
| 190 | |
| 191 | svm = am->vlib_rp; |
| 192 | shmem_hdr = am->shmem_hdr; |
| 193 | |
| 194 | if (shmem_hdr == 0 || shmem_hdr->vl_input_queue == 0) |
| 195 | { |
| 196 | clib_warning ("shmem_hdr / input queue NULL"); |
| 197 | return -1; |
| 198 | } |
| 199 | |
| 200 | pthread_mutex_lock (&svm->mutex); |
| 201 | oldheap = svm_push_data_heap (svm); |
Florin Coras | c470e22 | 2018-08-01 07:53:18 -0700 | [diff] [blame] | 202 | vl_input_queue = svm_queue_alloc_and_init (input_queue_size, sizeof (uword), |
| 203 | getpid ()); |
Dave Barach | 5c6c4bf | 2017-04-11 13:12:48 -0400 | [diff] [blame] | 204 | svm_pop_heap (oldheap); |
Ole Troan | 73710c7 | 2018-06-04 22:27:49 +0200 | [diff] [blame] | 205 | pthread_mutex_unlock (&svm->mutex); |
Dave Barach | 5c6c4bf | 2017-04-11 13:12:48 -0400 | [diff] [blame] | 206 | |
| 207 | am->my_client_index = ~0; |
| 208 | am->my_registration = 0; |
| 209 | am->vl_input_queue = vl_input_queue; |
| 210 | |
| 211 | mp = vl_msg_api_alloc (sizeof (vl_api_memclnt_create_t)); |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 212 | clib_memset (mp, 0, sizeof (*mp)); |
Dave Barach | 5c6c4bf | 2017-04-11 13:12:48 -0400 | [diff] [blame] | 213 | mp->_vl_msg_id = ntohs (VL_API_MEMCLNT_CREATE); |
| 214 | mp->ctx_quota = ctx_quota; |
| 215 | mp->input_queue = (uword) vl_input_queue; |
| 216 | strncpy ((char *) mp->name, name, sizeof (mp->name) - 1); |
| 217 | |
| 218 | vl_msg_api_send_shmem (shmem_hdr->vl_input_queue, (u8 *) & mp); |
| 219 | |
| 220 | while (1) |
| 221 | { |
| 222 | int qstatus; |
| 223 | struct timespec ts, tsrem; |
| 224 | int i; |
| 225 | |
| 226 | /* Wait up to 10 seconds */ |
| 227 | for (i = 0; i < 1000; i++) |
| 228 | { |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 229 | qstatus = svm_queue_sub (vl_input_queue, (u8 *) & rp, |
Mohsin Kazmi | 3fca567 | 2018-01-04 18:57:26 +0100 | [diff] [blame] | 230 | SVM_Q_NOWAIT, 0); |
Dave Barach | 5c6c4bf | 2017-04-11 13:12:48 -0400 | [diff] [blame] | 231 | if (qstatus == 0) |
| 232 | goto read_one_msg; |
| 233 | ts.tv_sec = 0; |
| 234 | ts.tv_nsec = 10000 * 1000; /* 10 ms */ |
| 235 | while (nanosleep (&ts, &tsrem) < 0) |
| 236 | ts = tsrem; |
| 237 | } |
| 238 | /* Timeout... */ |
| 239 | clib_warning ("memclnt_create_reply timeout"); |
| 240 | return -1; |
| 241 | |
| 242 | read_one_msg: |
| 243 | if (ntohs (rp->_vl_msg_id) != VL_API_MEMCLNT_CREATE_REPLY) |
| 244 | { |
| 245 | clib_warning ("unexpected reply: id %d", ntohs (rp->_vl_msg_id)); |
| 246 | continue; |
| 247 | } |
| 248 | rv = clib_net_to_host_u32 (rp->response); |
| 249 | |
| 250 | vl_msg_api_handler ((void *) rp); |
| 251 | break; |
| 252 | } |
| 253 | return (rv); |
| 254 | } |
| 255 | |
| 256 | static void |
| 257 | vl_api_memclnt_delete_reply_t_handler (vl_api_memclnt_delete_reply_t * mp) |
| 258 | { |
| 259 | void *oldheap; |
| 260 | api_main_t *am = &api_main; |
| 261 | |
| 262 | pthread_mutex_lock (&am->vlib_rp->mutex); |
| 263 | oldheap = svm_push_data_heap (am->vlib_rp); |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 264 | svm_queue_free (am->vl_input_queue); |
Dave Barach | 5c6c4bf | 2017-04-11 13:12:48 -0400 | [diff] [blame] | 265 | pthread_mutex_unlock (&am->vlib_rp->mutex); |
| 266 | svm_pop_heap (oldheap); |
| 267 | |
| 268 | am->my_client_index = ~0; |
| 269 | am->my_registration = 0; |
| 270 | am->vl_input_queue = 0; |
| 271 | } |
| 272 | |
Florin Coras | 940f78f | 2018-11-30 12:11:20 -0800 | [diff] [blame] | 273 | void |
Florin Coras | eaec2a6 | 2018-12-04 16:34:05 -0800 | [diff] [blame] | 274 | vl_client_send_disconnect (u8 do_cleanup) |
Dave Barach | 5c6c4bf | 2017-04-11 13:12:48 -0400 | [diff] [blame] | 275 | { |
| 276 | vl_api_memclnt_delete_t *mp; |
Dave Barach | 5c6c4bf | 2017-04-11 13:12:48 -0400 | [diff] [blame] | 277 | vl_shmem_hdr_t *shmem_hdr; |
Dave Barach | 5c6c4bf | 2017-04-11 13:12:48 -0400 | [diff] [blame] | 278 | api_main_t *am = &api_main; |
| 279 | |
| 280 | ASSERT (am->vlib_rp); |
| 281 | shmem_hdr = am->shmem_hdr; |
| 282 | ASSERT (shmem_hdr && shmem_hdr->vl_input_queue); |
| 283 | |
Dave Barach | 5c6c4bf | 2017-04-11 13:12:48 -0400 | [diff] [blame] | 284 | mp = vl_msg_api_alloc (sizeof (vl_api_memclnt_delete_t)); |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 285 | clib_memset (mp, 0, sizeof (*mp)); |
Dave Barach | 5c6c4bf | 2017-04-11 13:12:48 -0400 | [diff] [blame] | 286 | mp->_vl_msg_id = ntohs (VL_API_MEMCLNT_DELETE); |
| 287 | mp->index = am->my_client_index; |
| 288 | mp->handle = (uword) am->my_registration; |
Florin Coras | eaec2a6 | 2018-12-04 16:34:05 -0800 | [diff] [blame] | 289 | mp->do_cleanup = do_cleanup; |
Dave Barach | 5c6c4bf | 2017-04-11 13:12:48 -0400 | [diff] [blame] | 290 | |
| 291 | vl_msg_api_send_shmem (shmem_hdr->vl_input_queue, (u8 *) & mp); |
Florin Coras | 940f78f | 2018-11-30 12:11:20 -0800 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | int |
| 295 | vl_client_disconnect (void) |
| 296 | { |
| 297 | vl_api_memclnt_delete_reply_t *rp; |
| 298 | svm_queue_t *vl_input_queue; |
| 299 | api_main_t *am = &api_main; |
| 300 | time_t begin; |
| 301 | |
| 302 | vl_input_queue = am->vl_input_queue; |
Florin Coras | eaec2a6 | 2018-12-04 16:34:05 -0800 | [diff] [blame] | 303 | vl_client_send_disconnect (0 /* wait for reply */ ); |
Dave Barach | 5c6c4bf | 2017-04-11 13:12:48 -0400 | [diff] [blame] | 304 | |
| 305 | /* |
| 306 | * Have to be careful here, in case the client is disconnecting |
| 307 | * because e.g. the vlib process died, or is unresponsive. |
| 308 | */ |
Dave Barach | 5c6c4bf | 2017-04-11 13:12:48 -0400 | [diff] [blame] | 309 | begin = time (0); |
| 310 | while (1) |
| 311 | { |
| 312 | time_t now; |
| 313 | |
| 314 | now = time (0); |
| 315 | |
| 316 | if (now >= (begin + 2)) |
| 317 | { |
| 318 | clib_warning ("peer unresponsive, give up"); |
| 319 | am->my_client_index = ~0; |
| 320 | am->my_registration = 0; |
| 321 | am->shmem_hdr = 0; |
Florin Coras | d6c30d9 | 2018-01-29 05:11:24 -0800 | [diff] [blame] | 322 | return -1; |
Dave Barach | 5c6c4bf | 2017-04-11 13:12:48 -0400 | [diff] [blame] | 323 | } |
Mohsin Kazmi | 3fca567 | 2018-01-04 18:57:26 +0100 | [diff] [blame] | 324 | 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] | 325 | continue; |
| 326 | |
| 327 | /* drain the queue */ |
| 328 | if (ntohs (rp->_vl_msg_id) != VL_API_MEMCLNT_DELETE_REPLY) |
| 329 | { |
Dave Barach | cf5e848 | 2017-10-17 11:48:29 -0400 | [diff] [blame] | 330 | clib_warning ("queue drain: %d", ntohs (rp->_vl_msg_id)); |
Dave Barach | 5c6c4bf | 2017-04-11 13:12:48 -0400 | [diff] [blame] | 331 | vl_msg_api_handler ((void *) rp); |
| 332 | continue; |
| 333 | } |
| 334 | vl_msg_api_handler ((void *) rp); |
| 335 | break; |
| 336 | } |
Ole Troan | 73710c7 | 2018-06-04 22:27:49 +0200 | [diff] [blame] | 337 | |
| 338 | vl_api_name_and_crc_free (); |
Florin Coras | d6c30d9 | 2018-01-29 05:11:24 -0800 | [diff] [blame] | 339 | return 0; |
Dave Barach | 5c6c4bf | 2017-04-11 13:12:48 -0400 | [diff] [blame] | 340 | } |
| 341 | |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 342 | /** |
| 343 | * Stave off the binary API dead client reaper |
| 344 | * Only sent to inactive clients |
| 345 | */ |
| 346 | static void |
| 347 | vl_api_memclnt_keepalive_t_handler (vl_api_memclnt_keepalive_t * mp) |
| 348 | { |
| 349 | vl_api_memclnt_keepalive_reply_t *rmp; |
| 350 | api_main_t *am; |
| 351 | vl_shmem_hdr_t *shmem_hdr; |
| 352 | |
| 353 | am = &api_main; |
| 354 | shmem_hdr = am->shmem_hdr; |
| 355 | |
| 356 | rmp = vl_msg_api_alloc_as_if_client (sizeof (*rmp)); |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 357 | clib_memset (rmp, 0, sizeof (*rmp)); |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 358 | rmp->_vl_msg_id = ntohs (VL_API_MEMCLNT_KEEPALIVE_REPLY); |
| 359 | rmp->context = mp->context; |
| 360 | vl_msg_api_send_shmem (shmem_hdr->vl_input_queue, (u8 *) & rmp); |
| 361 | } |
| 362 | |
Dave Barach | 5c6c4bf | 2017-04-11 13:12:48 -0400 | [diff] [blame] | 363 | #define foreach_api_msg \ |
| 364 | _(RX_THREAD_EXIT, rx_thread_exit) \ |
| 365 | _(MEMCLNT_CREATE_REPLY, memclnt_create_reply) \ |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 366 | _(MEMCLNT_DELETE_REPLY, memclnt_delete_reply) \ |
| 367 | _(MEMCLNT_KEEPALIVE, memclnt_keepalive) |
Dave Barach | 5c6c4bf | 2017-04-11 13:12:48 -0400 | [diff] [blame] | 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 | |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 384 | int |
| 385 | vl_client_api_map (const char *region_name) |
| 386 | { |
| 387 | int rv; |
| 388 | |
| 389 | if ((rv = vl_map_shmem (region_name, 0 /* is_vlib */ )) < 0) |
| 390 | return rv; |
| 391 | |
| 392 | vl_client_install_client_message_handlers (); |
Dave Barach | 5c6c4bf | 2017-04-11 13:12:48 -0400 | [diff] [blame] | 393 | return 0; |
| 394 | } |
| 395 | |
| 396 | void |
| 397 | vl_client_api_unmap (void) |
| 398 | { |
Florin Coras | d6c30d9 | 2018-01-29 05:11:24 -0800 | [diff] [blame] | 399 | vl_unmap_shmem_client (); |
Dave Barach | 5c6c4bf | 2017-04-11 13:12:48 -0400 | [diff] [blame] | 400 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 401 | |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 402 | u8 |
| 403 | vl_mem_client_is_connected (void) |
| 404 | { |
| 405 | return (memory_client_main.connected_to_vlib != 0); |
| 406 | } |
| 407 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 408 | static int |
Neale Ranns | e72be39 | 2017-04-26 13:59:20 -0700 | [diff] [blame] | 409 | connect_to_vlib_internal (const char *svm_name, |
| 410 | const char *client_name, |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 411 | int rx_queue_size, int want_pthread, int do_map) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 412 | { |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 413 | int rv = 0; |
| 414 | memory_client_main_t *mm = &memory_client_main; |
| 415 | |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 416 | if (do_map && (rv = vl_client_api_map (svm_name))) |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 417 | { |
| 418 | clib_warning ("vl_client_api map rv %d", rv); |
| 419 | return rv; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 420 | } |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 421 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 422 | if (vl_client_connect (client_name, 0 /* punt quota */ , |
| 423 | rx_queue_size /* input queue */ ) < 0) |
| 424 | { |
| 425 | vl_client_api_unmap (); |
| 426 | return -1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 427 | } |
| 428 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 429 | /* Start the rx queue thread */ |
| 430 | |
| 431 | if (want_pthread) |
| 432 | { |
| 433 | rv = pthread_create (&mm->rx_thread_handle, |
| 434 | NULL /*attr */ , rx_thread_fn, 0); |
| 435 | if (rv) |
| 436 | clib_warning ("pthread_create returned %d", rv); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 437 | } |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 438 | |
| 439 | mm->connected_to_vlib = 1; |
| 440 | return 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 441 | } |
| 442 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 443 | int |
Neale Ranns | e72be39 | 2017-04-26 13:59:20 -0700 | [diff] [blame] | 444 | vl_client_connect_to_vlib (const char *svm_name, |
| 445 | const char *client_name, int rx_queue_size) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 446 | { |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 447 | return connect_to_vlib_internal (svm_name, client_name, rx_queue_size, |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 448 | 1 /* want pthread */ , |
| 449 | 1 /* do map */ ); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 450 | } |
| 451 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 452 | int |
Neale Ranns | e72be39 | 2017-04-26 13:59:20 -0700 | [diff] [blame] | 453 | vl_client_connect_to_vlib_no_rx_pthread (const char *svm_name, |
| 454 | const char *client_name, |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 455 | int rx_queue_size) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 456 | { |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 457 | return connect_to_vlib_internal (svm_name, client_name, rx_queue_size, |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 458 | 0 /* want pthread */ , |
| 459 | 1 /* do map */ ); |
| 460 | } |
| 461 | |
| 462 | int |
| 463 | vl_client_connect_to_vlib_no_map (const char *svm_name, |
| 464 | const char *client_name, int rx_queue_size) |
| 465 | { |
| 466 | return connect_to_vlib_internal (svm_name, client_name, rx_queue_size, |
| 467 | 1 /* want pthread */ , |
| 468 | 0 /* dont map */ ); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 469 | } |
| 470 | |
Tomasz Kulasek | 97dcf5b | 2019-01-31 18:26:32 +0100 | [diff] [blame] | 471 | int |
| 472 | vl_client_connect_to_vlib_no_rx_pthread_no_map (const char *svm_name, |
| 473 | const char *client_name, |
| 474 | int rx_queue_size) |
| 475 | { |
| 476 | return connect_to_vlib_internal (svm_name, client_name, rx_queue_size, |
| 477 | 0 /* want pthread */ , |
| 478 | 0 /* dont map */ ); |
| 479 | } |
| 480 | |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 481 | static void |
| 482 | disconnect_from_vlib_internal (u8 do_unmap) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 483 | { |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 484 | memory_client_main_t *mm = &memory_client_main; |
| 485 | api_main_t *am = &api_main; |
| 486 | uword junk; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 487 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 488 | if (mm->rx_thread_jmpbuf_valid) |
| 489 | { |
| 490 | vl_api_rx_thread_exit_t *ep; |
| 491 | ep = vl_msg_api_alloc (sizeof (*ep)); |
| 492 | ep->_vl_msg_id = ntohs (VL_API_RX_THREAD_EXIT); |
| 493 | vl_msg_api_send_shmem (am->vl_input_queue, (u8 *) & ep); |
| 494 | pthread_join (mm->rx_thread_handle, (void **) &junk); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 495 | } |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 496 | if (mm->connected_to_vlib) |
| 497 | { |
| 498 | vl_client_disconnect (); |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 499 | if (do_unmap) |
| 500 | vl_client_api_unmap (); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 501 | } |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 502 | clib_memset (mm, 0, sizeof (*mm)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 503 | } |
| 504 | |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 505 | void |
| 506 | vl_client_disconnect_from_vlib (void) |
| 507 | { |
| 508 | disconnect_from_vlib_internal (1); |
| 509 | } |
| 510 | |
| 511 | void |
| 512 | vl_client_disconnect_from_vlib_no_unmap (void) |
| 513 | { |
| 514 | disconnect_from_vlib_internal (0); |
| 515 | } |
| 516 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 517 | static void vl_api_get_first_msg_id_reply_t_handler |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 518 | (vl_api_get_first_msg_id_reply_t * mp) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 519 | { |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 520 | memory_client_main_t *mm = &memory_client_main; |
| 521 | i32 retval = ntohl (mp->retval); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 522 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 523 | mm->first_msg_id_reply = (retval >= 0) ? ntohs (mp->first_msg_id) : ~0; |
| 524 | mm->first_msg_id_reply_ready = 1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 525 | } |
| 526 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 527 | u16 |
Neale Ranns | e72be39 | 2017-04-26 13:59:20 -0700 | [diff] [blame] | 528 | vl_client_get_first_plugin_msg_id (const char *plugin_name) |
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_api_get_first_msg_id_t *mp; |
| 531 | api_main_t *am = &api_main; |
| 532 | memory_client_main_t *mm = &memory_client_main; |
| 533 | f64 timeout; |
| 534 | void *old_handler; |
| 535 | clib_time_t clib_time; |
| 536 | u16 rv = ~0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 537 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 538 | if (strlen (plugin_name) + 1 > sizeof (mp->name)) |
| 539 | return (rv); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 540 | |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 541 | clib_memset (&clib_time, 0, sizeof (clib_time)); |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 542 | clib_time_init (&clib_time); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 543 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 544 | /* Push this plugin's first_msg_id_reply handler */ |
| 545 | old_handler = am->msg_handlers[VL_API_GET_FIRST_MSG_ID_REPLY]; |
| 546 | am->msg_handlers[VL_API_GET_FIRST_MSG_ID_REPLY] = (void *) |
| 547 | vl_api_get_first_msg_id_reply_t_handler; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 548 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 549 | /* Ask the data-plane for the message-ID base of the indicated plugin */ |
| 550 | mm->first_msg_id_reply_ready = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 551 | |
Florin Coras | 6d1caf9 | 2018-01-04 12:17:10 -0800 | [diff] [blame] | 552 | /* Not using shm client */ |
| 553 | if (!am->my_registration) |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 554 | { |
Florin Coras | 6d1caf9 | 2018-01-04 12:17:10 -0800 | [diff] [blame] | 555 | mp = vl_socket_client_msg_alloc (sizeof (*mp)); |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 556 | clib_memset (mp, 0, sizeof (*mp)); |
Florin Coras | 6d1caf9 | 2018-01-04 12:17:10 -0800 | [diff] [blame] | 557 | mp->_vl_msg_id = ntohs (VL_API_GET_FIRST_MSG_ID); |
| 558 | mp->client_index = am->my_client_index; |
| 559 | strncpy ((char *) mp->name, plugin_name, sizeof (mp->name) - 1); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 560 | |
Florin Coras | 6d1caf9 | 2018-01-04 12:17:10 -0800 | [diff] [blame] | 561 | if (vl_socket_client_write () <= 0) |
| 562 | goto sock_err; |
| 563 | if (vl_socket_client_read (1)) |
| 564 | goto sock_err; |
| 565 | |
| 566 | if (mm->first_msg_id_reply_ready == 1) |
| 567 | { |
| 568 | rv = mm->first_msg_id_reply; |
| 569 | goto result; |
| 570 | } |
| 571 | |
| 572 | sock_err: |
| 573 | /* Restore old handler */ |
| 574 | am->msg_handlers[VL_API_GET_FIRST_MSG_ID_REPLY] = old_handler; |
| 575 | |
| 576 | return -1; |
| 577 | } |
| 578 | else |
| 579 | { |
| 580 | mp = vl_msg_api_alloc (sizeof (*mp)); |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 581 | clib_memset (mp, 0, sizeof (*mp)); |
Florin Coras | 6d1caf9 | 2018-01-04 12:17:10 -0800 | [diff] [blame] | 582 | mp->_vl_msg_id = ntohs (VL_API_GET_FIRST_MSG_ID); |
| 583 | mp->client_index = am->my_client_index; |
| 584 | strncpy ((char *) mp->name, plugin_name, sizeof (mp->name) - 1); |
| 585 | |
| 586 | vl_msg_api_send_shmem (am->shmem_hdr->vl_input_queue, (u8 *) & mp); |
| 587 | |
| 588 | /* Synchronously wait for the answer */ |
| 589 | timeout = clib_time_now (&clib_time) + 1.0; |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 590 | while (clib_time_now (&clib_time) < timeout) |
| 591 | { |
| 592 | if (mm->first_msg_id_reply_ready == 1) |
| 593 | { |
| 594 | rv = mm->first_msg_id_reply; |
| 595 | goto result; |
| 596 | } |
| 597 | } |
| 598 | /* Restore old handler */ |
| 599 | am->msg_handlers[VL_API_GET_FIRST_MSG_ID_REPLY] = old_handler; |
| 600 | |
| 601 | return rv; |
| 602 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 603 | |
| 604 | result: |
| 605 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 606 | /* Restore the old handler */ |
| 607 | am->msg_handlers[VL_API_GET_FIRST_MSG_ID_REPLY] = old_handler; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 608 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 609 | if (rv == (u16) ~ 0) |
| 610 | clib_warning ("plugin '%s' not registered", plugin_name); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 611 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 612 | return rv; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 613 | } |
Dave Barach | 6931f59 | 2016-05-13 12:55:01 -0400 | [diff] [blame] | 614 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 615 | /* |
| 616 | * fd.io coding-style-patch-verification: ON |
| 617 | * |
| 618 | * Local Variables: |
| 619 | * eval: (c-set-style "gnu") |
| 620 | * End: |
| 621 | */ |