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