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