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