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