blob: f032ae77d198aa7d85a70d47de39a8c2ee715166 [file] [log] [blame]
Dave Barach371e4e12016-07-08 09:38:52 -04001/*
Ed Warnickecb9cada2015-12-08 15:45:58 -07002 *------------------------------------------------------------------
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 Warnickecb9cada2015-12-08 15:45:58 -070020#include <setjmp.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070021
Florin Corase86a8ed2018-01-05 03:20:25 -080022#include <svm/svm.h>
Florin Coras4d9b9d82018-01-14 12:25:50 -080023#include <svm/ssvm.h>
Florin Corase86a8ed2018-01-05 03:20:25 -080024#include <vppinfra/serialize.h>
25#include <vppinfra/hash.h>
26#include <vlibmemory/memory_client.h>
27
28/* A hack. vl_client_get_first_plugin_msg_id depends on it */
29#include <vlibmemory/socket_client.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070030
31#include <vlibmemory/vl_memory_msg_enum.h>
32
Dave Barach371e4e12016-07-08 09:38:52 -040033#define vl_typedefs /* define message structures */
34#include <vlibmemory/vl_memory_api_h.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070035#undef vl_typedefs
36
Dave Barach371e4e12016-07-08 09:38:52 -040037#define vl_endianfun /* define message structures */
38#include <vlibmemory/vl_memory_api_h.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070039#undef vl_endianfun
40
41/* instantiate all the print functions we know about */
42#define vl_print(handle, ...) clib_warning (__VA_ARGS__)
43#define vl_printfun
44#include <vlibmemory/vl_memory_api_h.h>
45#undef vl_printfun
46
Dave Barach371e4e12016-07-08 09:38:52 -040047typedef struct
48{
49 u8 rx_thread_jmpbuf_valid;
50 u8 connected_to_vlib;
51 jmp_buf rx_thread_jmpbuf;
52 pthread_t rx_thread_handle;
53 /* Plugin message base lookup scheme */
54 volatile u8 first_msg_id_reply_ready;
55 u16 first_msg_id_reply;
Ed Warnickecb9cada2015-12-08 15:45:58 -070056} memory_client_main_t;
57
58memory_client_main_t memory_client_main;
59
Dave Barach371e4e12016-07-08 09:38:52 -040060static void *
61rx_thread_fn (void *arg)
Ed Warnickecb9cada2015-12-08 15:45:58 -070062{
Florin Corase86a8ed2018-01-05 03:20:25 -080063 svm_queue_t *q;
Dave Barach371e4e12016-07-08 09:38:52 -040064 memory_client_main_t *mm = &memory_client_main;
65 api_main_t *am = &api_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -070066
Dave Barach371e4e12016-07-08 09:38:52 -040067 q = am->vl_input_queue;
Ed Warnickecb9cada2015-12-08 15:45:58 -070068
Dave Barach371e4e12016-07-08 09:38:52 -040069 /* So we can make the rx thread terminate cleanly */
70 if (setjmp (mm->rx_thread_jmpbuf) == 0)
71 {
72 mm->rx_thread_jmpbuf_valid = 1;
Nathan Skrzypczakd516ca42019-08-01 18:14:06 +020073 clib_mem_set_thread_index ();
Dave Barachcf5e8482017-10-17 11:48:29 -040074 while (1)
75 vl_msg_api_queue_handler (q);
Ed Warnickecb9cada2015-12-08 15:45:58 -070076 }
Dave Barach371e4e12016-07-08 09:38:52 -040077 pthread_exit (0);
Ed Warnickecb9cada2015-12-08 15:45:58 -070078}
79
Dave Barach371e4e12016-07-08 09:38:52 -040080static void
81vl_api_rx_thread_exit_t_handler (vl_api_rx_thread_exit_t * mp)
Ed Warnickecb9cada2015-12-08 15:45:58 -070082{
Dave Barach371e4e12016-07-08 09:38:52 -040083 memory_client_main_t *mm = &memory_client_main;
Ole Troanc5607892019-04-10 19:32:02 +020084 if (mm->rx_thread_jmpbuf_valid)
85 longjmp (mm->rx_thread_jmpbuf, 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -070086}
87
Dave Barach371e4e12016-07-08 09:38:52 -040088static void
Ole Troan73710c72018-06-04 22:27:49 +020089vl_api_name_and_crc_free (void)
90{
91 api_main_t *am = &api_main;
92 int i;
93 u8 **keys = 0;
94 hash_pair_t *hp;
95
96 if (!am->msg_index_by_name_and_crc)
97 return;
98
99 /* *INDENT-OFF* */
100 hash_foreach_pair (hp, am->msg_index_by_name_and_crc,
101 ({
102 vec_add1 (keys, (u8 *) hp->key);
103 }));
104 /* *INDENT-ON* */
105 for (i = 0; i < vec_len (keys); i++)
106 vec_free (keys[i]);
107 vec_free (keys);
108 hash_free (am->msg_index_by_name_and_crc);
109}
110
111static void
Dave Barach5c6c4bf2017-04-11 13:12:48 -0400112vl_api_memclnt_create_reply_t_handler (vl_api_memclnt_create_reply_t * mp)
113{
114 serialize_main_t _sm, *sm = &_sm;
115 api_main_t *am = &api_main;
116 u8 *tblv;
117 u32 nmsgs;
118 int i;
119 u8 *name_and_crc;
120 u32 msg_index;
121
122 am->my_client_index = mp->index;
123 am->my_registration = (vl_api_registration_t *) (uword) mp->handle;
124
125 /* Clean out any previous hash table (unlikely) */
Ole Troan73710c72018-06-04 22:27:49 +0200126 vl_api_name_and_crc_free ();
Dave Barach5c6c4bf2017-04-11 13:12:48 -0400127
128 am->msg_index_by_name_and_crc = hash_create_string (0, sizeof (uword));
129
130 /* Recreate the vnet-side API message handler table */
Damjan Marion7bee80c2017-04-26 15:32:12 +0200131 tblv = uword_to_pointer (mp->message_table, u8 *);
Dave Barachcf5e8482017-10-17 11:48:29 -0400132 unserialize_open_data (sm, tblv, vec_len (tblv));
Dave Barach5c6c4bf2017-04-11 13:12:48 -0400133 unserialize_integer (sm, &nmsgs, sizeof (u32));
134
135 for (i = 0; i < nmsgs; i++)
136 {
137 msg_index = unserialize_likely_small_unsigned_integer (sm);
138 unserialize_cstring (sm, (char **) &name_and_crc);
139 hash_set_mem (am->msg_index_by_name_and_crc, name_and_crc, msg_index);
140 }
141}
142
143static void
Dave Barach371e4e12016-07-08 09:38:52 -0400144noop_handler (void *notused)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700145{
146}
147
Ole Troan2e1c8962019-04-10 09:44:23 +0200148void vl_msg_api_send_shmem (svm_queue_t * q, u8 * elem);
Dave Barach5c6c4bf2017-04-11 13:12:48 -0400149int
Neale Rannse72be392017-04-26 13:59:20 -0700150vl_client_connect (const char *name, int ctx_quota, int input_queue_size)
Dave Barach5c6c4bf2017-04-11 13:12:48 -0400151{
152 svm_region_t *svm;
153 vl_api_memclnt_create_t *mp;
154 vl_api_memclnt_create_reply_t *rp;
Florin Corase86a8ed2018-01-05 03:20:25 -0800155 svm_queue_t *vl_input_queue;
Dave Barach5c6c4bf2017-04-11 13:12:48 -0400156 vl_shmem_hdr_t *shmem_hdr;
157 int rv = 0;
158 void *oldheap;
159 api_main_t *am = &api_main;
160
161 if (am->my_registration)
162 {
163 clib_warning ("client %s already connected...", name);
164 return -1;
165 }
166
167 if (am->vlib_rp == 0)
168 {
169 clib_warning ("am->vlib_rp NULL");
170 return -1;
171 }
172
173 svm = am->vlib_rp;
174 shmem_hdr = am->shmem_hdr;
175
176 if (shmem_hdr == 0 || shmem_hdr->vl_input_queue == 0)
177 {
178 clib_warning ("shmem_hdr / input queue NULL");
179 return -1;
180 }
181
182 pthread_mutex_lock (&svm->mutex);
183 oldheap = svm_push_data_heap (svm);
Florin Corasc470e222018-08-01 07:53:18 -0700184 vl_input_queue = svm_queue_alloc_and_init (input_queue_size, sizeof (uword),
185 getpid ());
Dave Barach5c6c4bf2017-04-11 13:12:48 -0400186 svm_pop_heap (oldheap);
Ole Troan73710c72018-06-04 22:27:49 +0200187 pthread_mutex_unlock (&svm->mutex);
Dave Barach5c6c4bf2017-04-11 13:12:48 -0400188
189 am->my_client_index = ~0;
190 am->my_registration = 0;
191 am->vl_input_queue = vl_input_queue;
192
193 mp = vl_msg_api_alloc (sizeof (vl_api_memclnt_create_t));
Dave Barachb7b92992018-10-17 10:38:51 -0400194 clib_memset (mp, 0, sizeof (*mp));
Dave Barach5c6c4bf2017-04-11 13:12:48 -0400195 mp->_vl_msg_id = ntohs (VL_API_MEMCLNT_CREATE);
196 mp->ctx_quota = ctx_quota;
197 mp->input_queue = (uword) vl_input_queue;
Ole Troan7adaa222019-08-27 15:05:27 +0200198 strncpy ((char *) mp->name, name, sizeof (mp->name) - 1);
Dave Barach5c6c4bf2017-04-11 13:12:48 -0400199
200 vl_msg_api_send_shmem (shmem_hdr->vl_input_queue, (u8 *) & mp);
201
202 while (1)
203 {
204 int qstatus;
205 struct timespec ts, tsrem;
206 int i;
207
208 /* Wait up to 10 seconds */
209 for (i = 0; i < 1000; i++)
210 {
Florin Corase86a8ed2018-01-05 03:20:25 -0800211 qstatus = svm_queue_sub (vl_input_queue, (u8 *) & rp,
Mohsin Kazmi3fca5672018-01-04 18:57:26 +0100212 SVM_Q_NOWAIT, 0);
Dave Barach5c6c4bf2017-04-11 13:12:48 -0400213 if (qstatus == 0)
214 goto read_one_msg;
215 ts.tv_sec = 0;
216 ts.tv_nsec = 10000 * 1000; /* 10 ms */
217 while (nanosleep (&ts, &tsrem) < 0)
218 ts = tsrem;
219 }
220 /* Timeout... */
221 clib_warning ("memclnt_create_reply timeout");
222 return -1;
223
224 read_one_msg:
225 if (ntohs (rp->_vl_msg_id) != VL_API_MEMCLNT_CREATE_REPLY)
226 {
227 clib_warning ("unexpected reply: id %d", ntohs (rp->_vl_msg_id));
228 continue;
229 }
230 rv = clib_net_to_host_u32 (rp->response);
231
232 vl_msg_api_handler ((void *) rp);
233 break;
234 }
235 return (rv);
236}
237
238static void
239vl_api_memclnt_delete_reply_t_handler (vl_api_memclnt_delete_reply_t * mp)
240{
241 void *oldheap;
242 api_main_t *am = &api_main;
243
244 pthread_mutex_lock (&am->vlib_rp->mutex);
245 oldheap = svm_push_data_heap (am->vlib_rp);
Florin Corase86a8ed2018-01-05 03:20:25 -0800246 svm_queue_free (am->vl_input_queue);
Dave Barach5c6c4bf2017-04-11 13:12:48 -0400247 pthread_mutex_unlock (&am->vlib_rp->mutex);
248 svm_pop_heap (oldheap);
249
250 am->my_client_index = ~0;
251 am->my_registration = 0;
252 am->vl_input_queue = 0;
253}
254
Florin Coras940f78f2018-11-30 12:11:20 -0800255void
Florin Coraseaec2a62018-12-04 16:34:05 -0800256vl_client_send_disconnect (u8 do_cleanup)
Dave Barach5c6c4bf2017-04-11 13:12:48 -0400257{
258 vl_api_memclnt_delete_t *mp;
Dave Barach5c6c4bf2017-04-11 13:12:48 -0400259 vl_shmem_hdr_t *shmem_hdr;
Dave Barach5c6c4bf2017-04-11 13:12:48 -0400260 api_main_t *am = &api_main;
261
262 ASSERT (am->vlib_rp);
263 shmem_hdr = am->shmem_hdr;
264 ASSERT (shmem_hdr && shmem_hdr->vl_input_queue);
265
Dave Barach5c6c4bf2017-04-11 13:12:48 -0400266 mp = vl_msg_api_alloc (sizeof (vl_api_memclnt_delete_t));
Dave Barachb7b92992018-10-17 10:38:51 -0400267 clib_memset (mp, 0, sizeof (*mp));
Dave Barach5c6c4bf2017-04-11 13:12:48 -0400268 mp->_vl_msg_id = ntohs (VL_API_MEMCLNT_DELETE);
269 mp->index = am->my_client_index;
270 mp->handle = (uword) am->my_registration;
Florin Coraseaec2a62018-12-04 16:34:05 -0800271 mp->do_cleanup = do_cleanup;
Dave Barach5c6c4bf2017-04-11 13:12:48 -0400272
273 vl_msg_api_send_shmem (shmem_hdr->vl_input_queue, (u8 *) & mp);
Florin Coras940f78f2018-11-30 12:11:20 -0800274}
275
276int
277vl_client_disconnect (void)
278{
279 vl_api_memclnt_delete_reply_t *rp;
280 svm_queue_t *vl_input_queue;
281 api_main_t *am = &api_main;
282 time_t begin;
283
284 vl_input_queue = am->vl_input_queue;
Florin Coraseaec2a62018-12-04 16:34:05 -0800285 vl_client_send_disconnect (0 /* wait for reply */ );
Dave Barach5c6c4bf2017-04-11 13:12:48 -0400286
287 /*
288 * Have to be careful here, in case the client is disconnecting
289 * because e.g. the vlib process died, or is unresponsive.
290 */
Dave Barach5c6c4bf2017-04-11 13:12:48 -0400291 begin = time (0);
292 while (1)
293 {
294 time_t now;
295
296 now = time (0);
297
298 if (now >= (begin + 2))
299 {
300 clib_warning ("peer unresponsive, give up");
301 am->my_client_index = ~0;
302 am->my_registration = 0;
303 am->shmem_hdr = 0;
Florin Corasd6c30d92018-01-29 05:11:24 -0800304 return -1;
Dave Barach5c6c4bf2017-04-11 13:12:48 -0400305 }
Mohsin Kazmi3fca5672018-01-04 18:57:26 +0100306 if (svm_queue_sub (vl_input_queue, (u8 *) & rp, SVM_Q_NOWAIT, 0) < 0)
Dave Barach5c6c4bf2017-04-11 13:12:48 -0400307 continue;
308
309 /* drain the queue */
310 if (ntohs (rp->_vl_msg_id) != VL_API_MEMCLNT_DELETE_REPLY)
311 {
Dave Barachcf5e8482017-10-17 11:48:29 -0400312 clib_warning ("queue drain: %d", ntohs (rp->_vl_msg_id));
Dave Barach5c6c4bf2017-04-11 13:12:48 -0400313 vl_msg_api_handler ((void *) rp);
314 continue;
315 }
316 vl_msg_api_handler ((void *) rp);
317 break;
318 }
Ole Troan73710c72018-06-04 22:27:49 +0200319
320 vl_api_name_and_crc_free ();
Florin Corasd6c30d92018-01-29 05:11:24 -0800321 return 0;
Dave Barach5c6c4bf2017-04-11 13:12:48 -0400322}
323
Dave Barach59b25652017-09-10 15:04:27 -0400324/**
325 * Stave off the binary API dead client reaper
326 * Only sent to inactive clients
327 */
328static void
329vl_api_memclnt_keepalive_t_handler (vl_api_memclnt_keepalive_t * mp)
330{
331 vl_api_memclnt_keepalive_reply_t *rmp;
332 api_main_t *am;
333 vl_shmem_hdr_t *shmem_hdr;
334
335 am = &api_main;
336 shmem_hdr = am->shmem_hdr;
337
338 rmp = vl_msg_api_alloc_as_if_client (sizeof (*rmp));
Dave Barachb7b92992018-10-17 10:38:51 -0400339 clib_memset (rmp, 0, sizeof (*rmp));
Dave Barach59b25652017-09-10 15:04:27 -0400340 rmp->_vl_msg_id = ntohs (VL_API_MEMCLNT_KEEPALIVE_REPLY);
341 rmp->context = mp->context;
342 vl_msg_api_send_shmem (shmem_hdr->vl_input_queue, (u8 *) & rmp);
343}
344
Dave Barach5c6c4bf2017-04-11 13:12:48 -0400345#define foreach_api_msg \
346_(RX_THREAD_EXIT, rx_thread_exit) \
347_(MEMCLNT_CREATE_REPLY, memclnt_create_reply) \
Dave Barach59b25652017-09-10 15:04:27 -0400348_(MEMCLNT_DELETE_REPLY, memclnt_delete_reply) \
349_(MEMCLNT_KEEPALIVE, memclnt_keepalive)
Dave Barach5c6c4bf2017-04-11 13:12:48 -0400350
Dave Barach59b25652017-09-10 15:04:27 -0400351void
352vl_client_install_client_message_handlers (void)
Dave Barach5c6c4bf2017-04-11 13:12:48 -0400353{
Dave Barach5c6c4bf2017-04-11 13:12:48 -0400354
355#define _(N,n) \
356 vl_msg_api_set_handlers(VL_API_##N, #n, \
357 vl_api_##n##_t_handler, \
358 noop_handler, \
359 vl_api_##n##_t_endian, \
360 vl_api_##n##_t_print, \
361 sizeof(vl_api_##n##_t), 1);
362 foreach_api_msg;
363#undef _
Dave Barach59b25652017-09-10 15:04:27 -0400364}
365
Dave Barach59b25652017-09-10 15:04:27 -0400366int
367vl_client_api_map (const char *region_name)
368{
369 int rv;
370
371 if ((rv = vl_map_shmem (region_name, 0 /* is_vlib */ )) < 0)
372 return rv;
373
374 vl_client_install_client_message_handlers ();
Dave Barach5c6c4bf2017-04-11 13:12:48 -0400375 return 0;
376}
377
378void
379vl_client_api_unmap (void)
380{
Florin Corasd6c30d92018-01-29 05:11:24 -0800381 vl_unmap_shmem_client ();
Dave Barach5c6c4bf2017-04-11 13:12:48 -0400382}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700383
Florin Corasb384b542018-01-15 01:08:33 -0800384u8
385vl_mem_client_is_connected (void)
386{
387 return (memory_client_main.connected_to_vlib != 0);
388}
389
Dave Barach371e4e12016-07-08 09:38:52 -0400390static int
Neale Rannse72be392017-04-26 13:59:20 -0700391connect_to_vlib_internal (const char *svm_name,
392 const char *client_name,
Dave Barach59b25652017-09-10 15:04:27 -0400393 int rx_queue_size, int want_pthread, int do_map)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700394{
Dave Barach371e4e12016-07-08 09:38:52 -0400395 int rv = 0;
396 memory_client_main_t *mm = &memory_client_main;
397
Dave Barach59b25652017-09-10 15:04:27 -0400398 if (do_map && (rv = vl_client_api_map (svm_name)))
Dave Barach371e4e12016-07-08 09:38:52 -0400399 {
400 clib_warning ("vl_client_api map rv %d", rv);
401 return rv;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700402 }
Dave Barach371e4e12016-07-08 09:38:52 -0400403
Dave Barach371e4e12016-07-08 09:38:52 -0400404 if (vl_client_connect (client_name, 0 /* punt quota */ ,
405 rx_queue_size /* input queue */ ) < 0)
406 {
407 vl_client_api_unmap ();
408 return -1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700409 }
410
Dave Barach371e4e12016-07-08 09:38:52 -0400411 /* Start the rx queue thread */
412
413 if (want_pthread)
414 {
415 rv = pthread_create (&mm->rx_thread_handle,
416 NULL /*attr */ , rx_thread_fn, 0);
417 if (rv)
418 clib_warning ("pthread_create returned %d", rv);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700419 }
Dave Barach371e4e12016-07-08 09:38:52 -0400420
421 mm->connected_to_vlib = 1;
422 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700423}
424
Dave Barach371e4e12016-07-08 09:38:52 -0400425int
Neale Rannse72be392017-04-26 13:59:20 -0700426vl_client_connect_to_vlib (const char *svm_name,
427 const char *client_name, int rx_queue_size)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700428{
Dave Barach371e4e12016-07-08 09:38:52 -0400429 return connect_to_vlib_internal (svm_name, client_name, rx_queue_size,
Dave Barach59b25652017-09-10 15:04:27 -0400430 1 /* want pthread */ ,
431 1 /* do map */ );
Ed Warnickecb9cada2015-12-08 15:45:58 -0700432}
433
Dave Barach371e4e12016-07-08 09:38:52 -0400434int
Neale Rannse72be392017-04-26 13:59:20 -0700435vl_client_connect_to_vlib_no_rx_pthread (const char *svm_name,
436 const char *client_name,
Dave Barach371e4e12016-07-08 09:38:52 -0400437 int rx_queue_size)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700438{
Dave Barach371e4e12016-07-08 09:38:52 -0400439 return connect_to_vlib_internal (svm_name, client_name, rx_queue_size,
Dave Barach59b25652017-09-10 15:04:27 -0400440 0 /* want pthread */ ,
441 1 /* do map */ );
442}
443
444int
445vl_client_connect_to_vlib_no_map (const char *svm_name,
446 const char *client_name, int rx_queue_size)
447{
448 return connect_to_vlib_internal (svm_name, client_name, rx_queue_size,
449 1 /* want pthread */ ,
450 0 /* dont map */ );
Ed Warnickecb9cada2015-12-08 15:45:58 -0700451}
452
Tomasz Kulasek97dcf5b2019-01-31 18:26:32 +0100453int
454vl_client_connect_to_vlib_no_rx_pthread_no_map (const char *svm_name,
455 const char *client_name,
456 int rx_queue_size)
457{
458 return connect_to_vlib_internal (svm_name, client_name, rx_queue_size,
459 0 /* want pthread */ ,
460 0 /* dont map */ );
461}
462
Florin Corasb384b542018-01-15 01:08:33 -0800463static void
464disconnect_from_vlib_internal (u8 do_unmap)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700465{
Dave Barach371e4e12016-07-08 09:38:52 -0400466 memory_client_main_t *mm = &memory_client_main;
467 api_main_t *am = &api_main;
468 uword junk;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700469
Dave Barach371e4e12016-07-08 09:38:52 -0400470 if (mm->rx_thread_jmpbuf_valid)
471 {
472 vl_api_rx_thread_exit_t *ep;
473 ep = vl_msg_api_alloc (sizeof (*ep));
474 ep->_vl_msg_id = ntohs (VL_API_RX_THREAD_EXIT);
475 vl_msg_api_send_shmem (am->vl_input_queue, (u8 *) & ep);
476 pthread_join (mm->rx_thread_handle, (void **) &junk);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700477 }
Dave Barach371e4e12016-07-08 09:38:52 -0400478 if (mm->connected_to_vlib)
479 {
480 vl_client_disconnect ();
Florin Corasb384b542018-01-15 01:08:33 -0800481 if (do_unmap)
482 vl_client_api_unmap ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700483 }
Dave Barachb7b92992018-10-17 10:38:51 -0400484 clib_memset (mm, 0, sizeof (*mm));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700485}
486
Florin Corasb384b542018-01-15 01:08:33 -0800487void
488vl_client_disconnect_from_vlib (void)
489{
490 disconnect_from_vlib_internal (1);
491}
492
493void
494vl_client_disconnect_from_vlib_no_unmap (void)
495{
496 disconnect_from_vlib_internal (0);
497}
498
Ed Warnickecb9cada2015-12-08 15:45:58 -0700499static void vl_api_get_first_msg_id_reply_t_handler
Dave Barach371e4e12016-07-08 09:38:52 -0400500 (vl_api_get_first_msg_id_reply_t * mp)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700501{
Dave Barach371e4e12016-07-08 09:38:52 -0400502 memory_client_main_t *mm = &memory_client_main;
503 i32 retval = ntohl (mp->retval);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700504
Dave Barach371e4e12016-07-08 09:38:52 -0400505 mm->first_msg_id_reply = (retval >= 0) ? ntohs (mp->first_msg_id) : ~0;
506 mm->first_msg_id_reply_ready = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700507}
508
Dave Barach371e4e12016-07-08 09:38:52 -0400509u16
Neale Rannse72be392017-04-26 13:59:20 -0700510vl_client_get_first_plugin_msg_id (const char *plugin_name)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700511{
Dave Barach371e4e12016-07-08 09:38:52 -0400512 vl_api_get_first_msg_id_t *mp;
513 api_main_t *am = &api_main;
514 memory_client_main_t *mm = &memory_client_main;
515 f64 timeout;
516 void *old_handler;
517 clib_time_t clib_time;
518 u16 rv = ~0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700519
Ole Troan7adaa222019-08-27 15:05:27 +0200520 if (strlen (plugin_name) + 1 > sizeof (mp->name))
Dave Barach371e4e12016-07-08 09:38:52 -0400521 return (rv);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700522
Dave Barachb7b92992018-10-17 10:38:51 -0400523 clib_memset (&clib_time, 0, sizeof (clib_time));
Dave Barach371e4e12016-07-08 09:38:52 -0400524 clib_time_init (&clib_time);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700525
Dave Barach371e4e12016-07-08 09:38:52 -0400526 /* Push this plugin's first_msg_id_reply handler */
527 old_handler = am->msg_handlers[VL_API_GET_FIRST_MSG_ID_REPLY];
528 am->msg_handlers[VL_API_GET_FIRST_MSG_ID_REPLY] = (void *)
529 vl_api_get_first_msg_id_reply_t_handler;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700530
Dave Barach371e4e12016-07-08 09:38:52 -0400531 /* Ask the data-plane for the message-ID base of the indicated plugin */
532 mm->first_msg_id_reply_ready = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700533
Florin Coras6d1caf92018-01-04 12:17:10 -0800534 /* Not using shm client */
535 if (!am->my_registration)
Dave Barach371e4e12016-07-08 09:38:52 -0400536 {
Florin Coras6d1caf92018-01-04 12:17:10 -0800537 mp = vl_socket_client_msg_alloc (sizeof (*mp));
Dave Barachb7b92992018-10-17 10:38:51 -0400538 clib_memset (mp, 0, sizeof (*mp));
Florin Coras6d1caf92018-01-04 12:17:10 -0800539 mp->_vl_msg_id = ntohs (VL_API_GET_FIRST_MSG_ID);
540 mp->client_index = am->my_client_index;
Ole Troan7adaa222019-08-27 15:05:27 +0200541 strncpy ((char *) mp->name, plugin_name, sizeof (mp->name) - 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700542
Florin Coras6d1caf92018-01-04 12:17:10 -0800543 if (vl_socket_client_write () <= 0)
544 goto sock_err;
545 if (vl_socket_client_read (1))
546 goto sock_err;
547
548 if (mm->first_msg_id_reply_ready == 1)
549 {
550 rv = mm->first_msg_id_reply;
551 goto result;
552 }
553
554 sock_err:
555 /* Restore old handler */
556 am->msg_handlers[VL_API_GET_FIRST_MSG_ID_REPLY] = old_handler;
557
558 return -1;
559 }
560 else
561 {
562 mp = vl_msg_api_alloc (sizeof (*mp));
Dave Barachb7b92992018-10-17 10:38:51 -0400563 clib_memset (mp, 0, sizeof (*mp));
Florin Coras6d1caf92018-01-04 12:17:10 -0800564 mp->_vl_msg_id = ntohs (VL_API_GET_FIRST_MSG_ID);
565 mp->client_index = am->my_client_index;
Ole Troan7adaa222019-08-27 15:05:27 +0200566 strncpy ((char *) mp->name, plugin_name, sizeof (mp->name) - 1);
Florin Coras6d1caf92018-01-04 12:17:10 -0800567
568 vl_msg_api_send_shmem (am->shmem_hdr->vl_input_queue, (u8 *) & mp);
569
570 /* Synchronously wait for the answer */
571 timeout = clib_time_now (&clib_time) + 1.0;
Dave Barach371e4e12016-07-08 09:38:52 -0400572 while (clib_time_now (&clib_time) < timeout)
573 {
574 if (mm->first_msg_id_reply_ready == 1)
575 {
576 rv = mm->first_msg_id_reply;
577 goto result;
578 }
579 }
580 /* Restore old handler */
581 am->msg_handlers[VL_API_GET_FIRST_MSG_ID_REPLY] = old_handler;
582
583 return rv;
584 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700585
586result:
587
Dave Barach371e4e12016-07-08 09:38:52 -0400588 /* Restore the old handler */
589 am->msg_handlers[VL_API_GET_FIRST_MSG_ID_REPLY] = old_handler;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700590
Dave Barach371e4e12016-07-08 09:38:52 -0400591 if (rv == (u16) ~ 0)
592 clib_warning ("plugin '%s' not registered", plugin_name);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700593
Dave Barach371e4e12016-07-08 09:38:52 -0400594 return rv;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700595}
Dave Barach6931f592016-05-13 12:55:01 -0400596
Dave Barach371e4e12016-07-08 09:38:52 -0400597/*
598 * fd.io coding-style-patch-verification: ON
599 *
600 * Local Variables:
601 * eval: (c-set-style "gnu")
602 * End:
603 */