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