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