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