blob: f0b05b70695745ac2e8a4234114d4d1ecb04b780 [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>
Dave Barach39d69112019-11-27 11:42:13 -050027#include <vlibapi/api_common.h>
Florin Corase86a8ed2018-01-05 03:20:25 -080028
29/* A hack. vl_client_get_first_plugin_msg_id depends on it */
30#include <vlibmemory/socket_client.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070031
32#include <vlibmemory/vl_memory_msg_enum.h>
33
Dave Barach371e4e12016-07-08 09:38:52 -040034#define vl_typedefs /* define message structures */
35#include <vlibmemory/vl_memory_api_h.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070036#undef vl_typedefs
37
Dave Barach371e4e12016-07-08 09:38:52 -040038#define vl_endianfun /* define message structures */
39#include <vlibmemory/vl_memory_api_h.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070040#undef vl_endianfun
41
42/* instantiate all the print functions we know about */
43#define vl_print(handle, ...) clib_warning (__VA_ARGS__)
44#define vl_printfun
45#include <vlibmemory/vl_memory_api_h.h>
46#undef vl_printfun
47
Ed Warnickecb9cada2015-12-08 15:45:58 -070048memory_client_main_t memory_client_main;
Dave Barach39d69112019-11-27 11:42:13 -050049__thread memory_client_main_t *my_memory_client_main = &memory_client_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -070050
Florin Coras684fb6e2019-12-09 16:42:50 -080051typedef struct rx_thread_fn_arg
52{
53 api_main_t *am;
54 memory_client_main_t *mm;
55} rx_thread_fn_arg_t;
56
Dave Barach371e4e12016-07-08 09:38:52 -040057static void *
58rx_thread_fn (void *arg)
Ed Warnickecb9cada2015-12-08 15:45:58 -070059{
Florin Coras684fb6e2019-12-09 16:42:50 -080060 rx_thread_fn_arg_t *a = (rx_thread_fn_arg_t *) arg;
61 memory_client_main_t *mm;
Florin Corase86a8ed2018-01-05 03:20:25 -080062 svm_queue_t *q;
Ed Warnickecb9cada2015-12-08 15:45:58 -070063
Florin Coras684fb6e2019-12-09 16:42:50 -080064 vlibapi_set_main (a->am);
65 vlibapi_set_memory_client_main (a->mm);
Florin Coras68f58d72020-05-11 18:03:40 +000066 free (a);
Florin Coras684fb6e2019-12-09 16:42:50 -080067
68 mm = vlibapi_get_memory_client_main ();
Dave Barach39d69112019-11-27 11:42:13 -050069 q = vlibapi_get_main ()->vl_input_queue;
Ed Warnickecb9cada2015-12-08 15:45:58 -070070
Dave Barach371e4e12016-07-08 09:38:52 -040071 /* So we can make the rx thread terminate cleanly */
72 if (setjmp (mm->rx_thread_jmpbuf) == 0)
73 {
74 mm->rx_thread_jmpbuf_valid = 1;
Nathan Skrzypczakd516ca42019-08-01 18:14:06 +020075 clib_mem_set_thread_index ();
Dave Barachcf5e8482017-10-17 11:48:29 -040076 while (1)
77 vl_msg_api_queue_handler (q);
Ed Warnickecb9cada2015-12-08 15:45:58 -070078 }
Dave Barach371e4e12016-07-08 09:38:52 -040079 pthread_exit (0);
Ed Warnickecb9cada2015-12-08 15:45:58 -070080}
81
Dave Barach371e4e12016-07-08 09:38:52 -040082static void
83vl_api_rx_thread_exit_t_handler (vl_api_rx_thread_exit_t * mp)
Ed Warnickecb9cada2015-12-08 15:45:58 -070084{
Dave Barach39d69112019-11-27 11:42:13 -050085 memory_client_main_t *mm = vlibapi_get_memory_client_main ();
Ole Troanc5607892019-04-10 19:32:02 +020086 if (mm->rx_thread_jmpbuf_valid)
87 longjmp (mm->rx_thread_jmpbuf, 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -070088}
89
Dave Barach371e4e12016-07-08 09:38:52 -040090static void
Ole Troan73710c72018-06-04 22:27:49 +020091vl_api_name_and_crc_free (void)
92{
Dave Barach39d69112019-11-27 11:42:13 -050093 api_main_t *am = vlibapi_get_main ();
Ole Troan73710c72018-06-04 22:27:49 +020094 int i;
95 u8 **keys = 0;
96 hash_pair_t *hp;
97
98 if (!am->msg_index_by_name_and_crc)
99 return;
100
101 /* *INDENT-OFF* */
102 hash_foreach_pair (hp, am->msg_index_by_name_and_crc,
103 ({
104 vec_add1 (keys, (u8 *) hp->key);
105 }));
106 /* *INDENT-ON* */
107 for (i = 0; i < vec_len (keys); i++)
108 vec_free (keys[i]);
109 vec_free (keys);
110 hash_free (am->msg_index_by_name_and_crc);
111}
112
BenoƮt Ganne9fb6d402019-04-15 15:28:21 +0200113CLIB_NOSANITIZE_ADDR static void
114VL_API_VEC_UNPOISON (const void *v)
115{
116 const vec_header_t *vh = &((vec_header_t *) v)[-1];
117 CLIB_MEM_UNPOISON (vh, sizeof (*vh) + vec_len (v));
118}
119
Ole Troan73710c72018-06-04 22:27:49 +0200120static void
Dave Barach5c6c4bf2017-04-11 13:12:48 -0400121vl_api_memclnt_create_reply_t_handler (vl_api_memclnt_create_reply_t * mp)
122{
123 serialize_main_t _sm, *sm = &_sm;
Dave Barach39d69112019-11-27 11:42:13 -0500124 api_main_t *am = vlibapi_get_main ();
Dave Barach5c6c4bf2017-04-11 13:12:48 -0400125 u8 *tblv;
126 u32 nmsgs;
127 int i;
128 u8 *name_and_crc;
129 u32 msg_index;
130
131 am->my_client_index = mp->index;
132 am->my_registration = (vl_api_registration_t *) (uword) mp->handle;
133
134 /* Clean out any previous hash table (unlikely) */
Ole Troan73710c72018-06-04 22:27:49 +0200135 vl_api_name_and_crc_free ();
Dave Barach5c6c4bf2017-04-11 13:12:48 -0400136
137 am->msg_index_by_name_and_crc = hash_create_string (0, sizeof (uword));
138
139 /* Recreate the vnet-side API message handler table */
Damjan Marion7bee80c2017-04-26 15:32:12 +0200140 tblv = uword_to_pointer (mp->message_table, u8 *);
Dave Barachcf5e8482017-10-17 11:48:29 -0400141 unserialize_open_data (sm, tblv, vec_len (tblv));
Dave Barach5c6c4bf2017-04-11 13:12:48 -0400142 unserialize_integer (sm, &nmsgs, sizeof (u32));
143
BenoƮt Ganne9fb6d402019-04-15 15:28:21 +0200144 VL_API_VEC_UNPOISON (tblv);
145
Dave Barach5c6c4bf2017-04-11 13:12:48 -0400146 for (i = 0; i < nmsgs; i++)
147 {
148 msg_index = unserialize_likely_small_unsigned_integer (sm);
149 unserialize_cstring (sm, (char **) &name_and_crc);
150 hash_set_mem (am->msg_index_by_name_and_crc, name_and_crc, msg_index);
151 }
152}
153
154static void
Dave Barach371e4e12016-07-08 09:38:52 -0400155noop_handler (void *notused)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700156{
157}
158
Ole Troan2e1c8962019-04-10 09:44:23 +0200159void vl_msg_api_send_shmem (svm_queue_t * q, u8 * elem);
Dave Barach5c6c4bf2017-04-11 13:12:48 -0400160int
Neale Rannse72be392017-04-26 13:59:20 -0700161vl_client_connect (const char *name, int ctx_quota, int input_queue_size)
Dave Barach5c6c4bf2017-04-11 13:12:48 -0400162{
Dave Barach5c6c4bf2017-04-11 13:12:48 -0400163 vl_api_memclnt_create_t *mp;
164 vl_api_memclnt_create_reply_t *rp;
Florin Corase86a8ed2018-01-05 03:20:25 -0800165 svm_queue_t *vl_input_queue;
Dave Barach5c6c4bf2017-04-11 13:12:48 -0400166 vl_shmem_hdr_t *shmem_hdr;
167 int rv = 0;
168 void *oldheap;
Dave Barach39d69112019-11-27 11:42:13 -0500169 api_main_t *am = vlibapi_get_main ();
Dave Barach5c6c4bf2017-04-11 13:12:48 -0400170
171 if (am->my_registration)
172 {
173 clib_warning ("client %s already connected...", name);
174 return -1;
175 }
176
177 if (am->vlib_rp == 0)
178 {
179 clib_warning ("am->vlib_rp NULL");
180 return -1;
181 }
182
Dave Barach5c6c4bf2017-04-11 13:12:48 -0400183 shmem_hdr = am->shmem_hdr;
184
185 if (shmem_hdr == 0 || shmem_hdr->vl_input_queue == 0)
186 {
187 clib_warning ("shmem_hdr / input queue NULL");
188 return -1;
189 }
190
BenoƮt Ganne9fb6d402019-04-15 15:28:21 +0200191 CLIB_MEM_UNPOISON (shmem_hdr, sizeof (*shmem_hdr));
192 VL_MSG_API_SVM_QUEUE_UNPOISON (shmem_hdr->vl_input_queue);
193
Nathan Skrzypczak0aa40132019-11-25 16:29:38 +0100194 oldheap = vl_msg_push_heap ();
Florin Corasc470e222018-08-01 07:53:18 -0700195 vl_input_queue = svm_queue_alloc_and_init (input_queue_size, sizeof (uword),
196 getpid ());
Nathan Skrzypczak0aa40132019-11-25 16:29:38 +0100197 vl_msg_pop_heap (oldheap);
Dave Barach5c6c4bf2017-04-11 13:12:48 -0400198
199 am->my_client_index = ~0;
200 am->my_registration = 0;
201 am->vl_input_queue = vl_input_queue;
202
203 mp = vl_msg_api_alloc (sizeof (vl_api_memclnt_create_t));
Dave Barachb7b92992018-10-17 10:38:51 -0400204 clib_memset (mp, 0, sizeof (*mp));
Dave Barach5c6c4bf2017-04-11 13:12:48 -0400205 mp->_vl_msg_id = ntohs (VL_API_MEMCLNT_CREATE);
206 mp->ctx_quota = ctx_quota;
207 mp->input_queue = (uword) vl_input_queue;
Ole Troan7adaa222019-08-27 15:05:27 +0200208 strncpy ((char *) mp->name, name, sizeof (mp->name) - 1);
Dave Barach5c6c4bf2017-04-11 13:12:48 -0400209
210 vl_msg_api_send_shmem (shmem_hdr->vl_input_queue, (u8 *) & mp);
211
212 while (1)
213 {
214 int qstatus;
215 struct timespec ts, tsrem;
216 int i;
217
218 /* Wait up to 10 seconds */
219 for (i = 0; i < 1000; i++)
220 {
Florin Corase86a8ed2018-01-05 03:20:25 -0800221 qstatus = svm_queue_sub (vl_input_queue, (u8 *) & rp,
Mohsin Kazmi3fca5672018-01-04 18:57:26 +0100222 SVM_Q_NOWAIT, 0);
Dave Barach5c6c4bf2017-04-11 13:12:48 -0400223 if (qstatus == 0)
224 goto read_one_msg;
225 ts.tv_sec = 0;
226 ts.tv_nsec = 10000 * 1000; /* 10 ms */
227 while (nanosleep (&ts, &tsrem) < 0)
228 ts = tsrem;
229 }
230 /* Timeout... */
231 clib_warning ("memclnt_create_reply timeout");
232 return -1;
233
234 read_one_msg:
BenoƮt Ganne9fb6d402019-04-15 15:28:21 +0200235 VL_MSG_API_UNPOISON (rp);
Dave Barach5c6c4bf2017-04-11 13:12:48 -0400236 if (ntohs (rp->_vl_msg_id) != VL_API_MEMCLNT_CREATE_REPLY)
237 {
238 clib_warning ("unexpected reply: id %d", ntohs (rp->_vl_msg_id));
239 continue;
240 }
241 rv = clib_net_to_host_u32 (rp->response);
242
243 vl_msg_api_handler ((void *) rp);
244 break;
245 }
246 return (rv);
247}
248
249static void
250vl_api_memclnt_delete_reply_t_handler (vl_api_memclnt_delete_reply_t * mp)
251{
252 void *oldheap;
Dave Barach39d69112019-11-27 11:42:13 -0500253 api_main_t *am = vlibapi_get_main ();
Dave Barach5c6c4bf2017-04-11 13:12:48 -0400254
Nathan Skrzypczak0aa40132019-11-25 16:29:38 +0100255 oldheap = vl_msg_push_heap ();
Florin Corase86a8ed2018-01-05 03:20:25 -0800256 svm_queue_free (am->vl_input_queue);
Nathan Skrzypczak0aa40132019-11-25 16:29:38 +0100257 vl_msg_pop_heap (oldheap);
Dave Barach5c6c4bf2017-04-11 13:12:48 -0400258
259 am->my_client_index = ~0;
260 am->my_registration = 0;
261 am->vl_input_queue = 0;
262}
263
Florin Coras940f78f2018-11-30 12:11:20 -0800264void
Florin Coraseaec2a62018-12-04 16:34:05 -0800265vl_client_send_disconnect (u8 do_cleanup)
Dave Barach5c6c4bf2017-04-11 13:12:48 -0400266{
267 vl_api_memclnt_delete_t *mp;
Dave Barach5c6c4bf2017-04-11 13:12:48 -0400268 vl_shmem_hdr_t *shmem_hdr;
Dave Barach39d69112019-11-27 11:42:13 -0500269 api_main_t *am = vlibapi_get_main ();
Dave Barach5c6c4bf2017-04-11 13:12:48 -0400270
271 ASSERT (am->vlib_rp);
272 shmem_hdr = am->shmem_hdr;
273 ASSERT (shmem_hdr && shmem_hdr->vl_input_queue);
274
Dave Barach5c6c4bf2017-04-11 13:12:48 -0400275 mp = vl_msg_api_alloc (sizeof (vl_api_memclnt_delete_t));
Dave Barachb7b92992018-10-17 10:38:51 -0400276 clib_memset (mp, 0, sizeof (*mp));
Dave Barach5c6c4bf2017-04-11 13:12:48 -0400277 mp->_vl_msg_id = ntohs (VL_API_MEMCLNT_DELETE);
278 mp->index = am->my_client_index;
279 mp->handle = (uword) am->my_registration;
Florin Coraseaec2a62018-12-04 16:34:05 -0800280 mp->do_cleanup = do_cleanup;
Dave Barach5c6c4bf2017-04-11 13:12:48 -0400281
282 vl_msg_api_send_shmem (shmem_hdr->vl_input_queue, (u8 *) & mp);
Florin Coras940f78f2018-11-30 12:11:20 -0800283}
284
285int
286vl_client_disconnect (void)
287{
288 vl_api_memclnt_delete_reply_t *rp;
289 svm_queue_t *vl_input_queue;
Dave Barach39d69112019-11-27 11:42:13 -0500290 api_main_t *am = vlibapi_get_main ();
Florin Coras940f78f2018-11-30 12:11:20 -0800291 time_t begin;
292
293 vl_input_queue = am->vl_input_queue;
Florin Coraseaec2a62018-12-04 16:34:05 -0800294 vl_client_send_disconnect (0 /* wait for reply */ );
Dave Barach5c6c4bf2017-04-11 13:12:48 -0400295
296 /*
297 * Have to be careful here, in case the client is disconnecting
298 * because e.g. the vlib process died, or is unresponsive.
299 */
Dave Barach5c6c4bf2017-04-11 13:12:48 -0400300 begin = time (0);
301 while (1)
302 {
303 time_t now;
304
305 now = time (0);
306
307 if (now >= (begin + 2))
308 {
309 clib_warning ("peer unresponsive, give up");
310 am->my_client_index = ~0;
311 am->my_registration = 0;
312 am->shmem_hdr = 0;
Florin Corasd6c30d92018-01-29 05:11:24 -0800313 return -1;
Dave Barach5c6c4bf2017-04-11 13:12:48 -0400314 }
Mohsin Kazmi3fca5672018-01-04 18:57:26 +0100315 if (svm_queue_sub (vl_input_queue, (u8 *) & rp, SVM_Q_NOWAIT, 0) < 0)
Dave Barach5c6c4bf2017-04-11 13:12:48 -0400316 continue;
317
BenoƮt Ganne9fb6d402019-04-15 15:28:21 +0200318 VL_MSG_API_UNPOISON (rp);
319
Dave Barach5c6c4bf2017-04-11 13:12:48 -0400320 /* drain the queue */
321 if (ntohs (rp->_vl_msg_id) != VL_API_MEMCLNT_DELETE_REPLY)
322 {
Dave Barachcf5e8482017-10-17 11:48:29 -0400323 clib_warning ("queue drain: %d", ntohs (rp->_vl_msg_id));
Dave Barach5c6c4bf2017-04-11 13:12:48 -0400324 vl_msg_api_handler ((void *) rp);
325 continue;
326 }
327 vl_msg_api_handler ((void *) rp);
328 break;
329 }
Ole Troan73710c72018-06-04 22:27:49 +0200330
331 vl_api_name_and_crc_free ();
Florin Corasd6c30d92018-01-29 05:11:24 -0800332 return 0;
Dave Barach5c6c4bf2017-04-11 13:12:48 -0400333}
334
Dave Barach59b25652017-09-10 15:04:27 -0400335/**
336 * Stave off the binary API dead client reaper
337 * Only sent to inactive clients
338 */
339static void
340vl_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 Barach39d69112019-11-27 11:42:13 -0500346 am = vlibapi_get_main ();
Dave Barach59b25652017-09-10 15:04:27 -0400347 shmem_hdr = am->shmem_hdr;
348
349 rmp = vl_msg_api_alloc_as_if_client (sizeof (*rmp));
Dave Barachb7b92992018-10-17 10:38:51 -0400350 clib_memset (rmp, 0, sizeof (*rmp));
Dave Barach59b25652017-09-10 15:04:27 -0400351 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 Barach5c6c4bf2017-04-11 13:12:48 -0400356#define foreach_api_msg \
357_(RX_THREAD_EXIT, rx_thread_exit) \
358_(MEMCLNT_CREATE_REPLY, memclnt_create_reply) \
Dave Barach59b25652017-09-10 15:04:27 -0400359_(MEMCLNT_DELETE_REPLY, memclnt_delete_reply) \
360_(MEMCLNT_KEEPALIVE, memclnt_keepalive)
Dave Barach5c6c4bf2017-04-11 13:12:48 -0400361
Dave Barach59b25652017-09-10 15:04:27 -0400362void
363vl_client_install_client_message_handlers (void)
Dave Barach5c6c4bf2017-04-11 13:12:48 -0400364{
Filip Tehlar36217e32021-07-23 08:51:10 +0000365 api_main_t *am = vlibapi_get_main ();
366#define _(N, n) \
367 vl_msg_api_set_handlers (VL_API_##N, #n, vl_api_##n##_t_handler, \
368 noop_handler, vl_api_##n##_t_endian, \
369 vl_api_##n##_t_print, sizeof (vl_api_##n##_t), 0, \
370 vl_api_##n##_t_print_json, vl_api_##n##_t_tojson, \
371 vl_api_##n##_t_fromjson); \
372 am->api_trace_cfg[VL_API_##N].replay_enable = 0;
Dave Barach5c6c4bf2017-04-11 13:12:48 -0400373 foreach_api_msg;
374#undef _
Dave Barach59b25652017-09-10 15:04:27 -0400375}
376
Dave Barach59b25652017-09-10 15:04:27 -0400377int
378vl_client_api_map (const char *region_name)
379{
380 int rv;
381
382 if ((rv = vl_map_shmem (region_name, 0 /* is_vlib */ )) < 0)
383 return rv;
384
385 vl_client_install_client_message_handlers ();
Dave Barach5c6c4bf2017-04-11 13:12:48 -0400386 return 0;
387}
388
389void
390vl_client_api_unmap (void)
391{
Florin Corasd6c30d92018-01-29 05:11:24 -0800392 vl_unmap_shmem_client ();
Dave Barach5c6c4bf2017-04-11 13:12:48 -0400393}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700394
Florin Corasb384b542018-01-15 01:08:33 -0800395u8
396vl_mem_client_is_connected (void)
397{
Florin Coras64cf4592019-12-12 12:01:24 -0800398 return (my_memory_client_main->connected_to_vlib != 0);
Florin Corasb384b542018-01-15 01:08:33 -0800399}
400
Dave Barach371e4e12016-07-08 09:38:52 -0400401static int
Neale Rannse72be392017-04-26 13:59:20 -0700402connect_to_vlib_internal (const char *svm_name,
403 const char *client_name,
Dave Barach920180e2019-11-14 08:03:48 -0500404 int rx_queue_size, void *(*thread_fn) (void *),
Dave Barach39d69112019-11-27 11:42:13 -0500405 void *thread_fn_arg, int do_map)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700406{
Dave Barach371e4e12016-07-08 09:38:52 -0400407 int rv = 0;
Dave Barach39d69112019-11-27 11:42:13 -0500408 memory_client_main_t *mm = vlibapi_get_memory_client_main ();
409 api_main_t *am = vlibapi_get_main ();
Dave Barach371e4e12016-07-08 09:38:52 -0400410
Dave Barach59b25652017-09-10 15:04:27 -0400411 if (do_map && (rv = vl_client_api_map (svm_name)))
Dave Barach371e4e12016-07-08 09:38:52 -0400412 {
413 clib_warning ("vl_client_api map rv %d", rv);
414 return rv;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700415 }
Dave Barach371e4e12016-07-08 09:38:52 -0400416
Dave Barach371e4e12016-07-08 09:38:52 -0400417 if (vl_client_connect (client_name, 0 /* punt quota */ ,
418 rx_queue_size /* input queue */ ) < 0)
419 {
420 vl_client_api_unmap ();
421 return -1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700422 }
423
Dave Barach371e4e12016-07-08 09:38:52 -0400424 /* Start the rx queue thread */
425
Dave Barach920180e2019-11-14 08:03:48 -0500426 if (thread_fn)
Dave Barach371e4e12016-07-08 09:38:52 -0400427 {
Florin Coras684fb6e2019-12-09 16:42:50 -0800428 if (thread_fn == rx_thread_fn)
429 {
430 rx_thread_fn_arg_t *arg;
Florin Coras68f58d72020-05-11 18:03:40 +0000431 arg = malloc (sizeof (*arg));
Florin Coras684fb6e2019-12-09 16:42:50 -0800432 arg->am = vlibapi_get_main ();
433 arg->mm = vlibapi_get_memory_client_main ();
434 thread_fn_arg = (void *) arg;
435 }
436
Dave Barach371e4e12016-07-08 09:38:52 -0400437 rv = pthread_create (&mm->rx_thread_handle,
Dave Barach39d69112019-11-27 11:42:13 -0500438 NULL /*attr */ , thread_fn, thread_fn_arg);
Dave Barach371e4e12016-07-08 09:38:52 -0400439 if (rv)
IJsbrand Wijnands82295802019-10-08 13:50:55 +0200440 {
441 clib_warning ("pthread_create returned %d", rv);
442 am->rx_thread_handle = 0;
443 }
444 else
445 {
446 am->rx_thread_handle = mm->rx_thread_handle;
447 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700448 }
Dave Barach371e4e12016-07-08 09:38:52 -0400449
450 mm->connected_to_vlib = 1;
451 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700452}
453
Dave Barach371e4e12016-07-08 09:38:52 -0400454int
Neale Rannse72be392017-04-26 13:59:20 -0700455vl_client_connect_to_vlib (const char *svm_name,
456 const char *client_name, int rx_queue_size)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700457{
Dave Barach371e4e12016-07-08 09:38:52 -0400458 return connect_to_vlib_internal (svm_name, client_name, rx_queue_size,
Dave Barach39d69112019-11-27 11:42:13 -0500459 rx_thread_fn, 0 /* thread fn arg */ ,
460 1 /* do map */ );
Ed Warnickecb9cada2015-12-08 15:45:58 -0700461}
462
Dave Barach371e4e12016-07-08 09:38:52 -0400463int
Neale Rannse72be392017-04-26 13:59:20 -0700464vl_client_connect_to_vlib_no_rx_pthread (const char *svm_name,
465 const char *client_name,
Dave Barach371e4e12016-07-08 09:38:52 -0400466 int rx_queue_size)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700467{
Dave Barach371e4e12016-07-08 09:38:52 -0400468 return connect_to_vlib_internal (svm_name, client_name, rx_queue_size,
Dave Barach920180e2019-11-14 08:03:48 -0500469 0 /* no rx_thread_fn */ ,
Dave Barach39d69112019-11-27 11:42:13 -0500470 0 /* no thread fn arg */ ,
Dave Barach59b25652017-09-10 15:04:27 -0400471 1 /* do map */ );
472}
473
474int
475vl_client_connect_to_vlib_no_map (const char *svm_name,
476 const char *client_name, int rx_queue_size)
477{
478 return connect_to_vlib_internal (svm_name, client_name, rx_queue_size,
Dave Barach39d69112019-11-27 11:42:13 -0500479 rx_thread_fn, 0 /* no thread fn arg */ ,
480 0 /* dont map */ );
Ed Warnickecb9cada2015-12-08 15:45:58 -0700481}
482
Tomasz Kulasek97dcf5b2019-01-31 18:26:32 +0100483int
484vl_client_connect_to_vlib_no_rx_pthread_no_map (const char *svm_name,
485 const char *client_name,
486 int rx_queue_size)
487{
488 return connect_to_vlib_internal (svm_name, client_name, rx_queue_size,
Dave Barach39d69112019-11-27 11:42:13 -0500489 0 /* no thread_fn */ ,
490 0 /* no thread fn arg */ ,
Tomasz Kulasek97dcf5b2019-01-31 18:26:32 +0100491 0 /* dont map */ );
492}
493
Dave Barach920180e2019-11-14 08:03:48 -0500494int
495vl_client_connect_to_vlib_thread_fn (const char *svm_name,
496 const char *client_name,
497 int rx_queue_size,
Dave Barach39d69112019-11-27 11:42:13 -0500498 void *(*thread_fn) (void *), void *arg)
Dave Barach920180e2019-11-14 08:03:48 -0500499{
500 return connect_to_vlib_internal (svm_name, client_name, rx_queue_size,
Dave Barach39d69112019-11-27 11:42:13 -0500501 thread_fn, arg, 1 /* do map */ );
Dave Barach920180e2019-11-14 08:03:48 -0500502}
503
504
Florin Corasb384b542018-01-15 01:08:33 -0800505static void
506disconnect_from_vlib_internal (u8 do_unmap)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700507{
Dave Barach39d69112019-11-27 11:42:13 -0500508 memory_client_main_t *mm = vlibapi_get_memory_client_main ();
509 api_main_t *am = vlibapi_get_main ();
Dave Barach371e4e12016-07-08 09:38:52 -0400510 uword junk;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700511
Dave Barach371e4e12016-07-08 09:38:52 -0400512 if (mm->rx_thread_jmpbuf_valid)
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 (am->vl_input_queue, (u8 *) & ep);
518 pthread_join (mm->rx_thread_handle, (void **) &junk);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700519 }
Dave Barach371e4e12016-07-08 09:38:52 -0400520 if (mm->connected_to_vlib)
521 {
522 vl_client_disconnect ();
Florin Corasb384b542018-01-15 01:08:33 -0800523 if (do_unmap)
524 vl_client_api_unmap ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700525 }
Dave Barachb7b92992018-10-17 10:38:51 -0400526 clib_memset (mm, 0, sizeof (*mm));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700527}
528
Florin Corasb384b542018-01-15 01:08:33 -0800529void
530vl_client_disconnect_from_vlib (void)
531{
532 disconnect_from_vlib_internal (1);
533}
534
535void
536vl_client_disconnect_from_vlib_no_unmap (void)
537{
538 disconnect_from_vlib_internal (0);
539}
540
Ed Warnickecb9cada2015-12-08 15:45:58 -0700541static void vl_api_get_first_msg_id_reply_t_handler
Dave Barach371e4e12016-07-08 09:38:52 -0400542 (vl_api_get_first_msg_id_reply_t * mp)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700543{
Dave Barach39d69112019-11-27 11:42:13 -0500544 memory_client_main_t *mm = vlibapi_get_memory_client_main ();
Dave Barach371e4e12016-07-08 09:38:52 -0400545 i32 retval = ntohl (mp->retval);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700546
Dave Barach371e4e12016-07-08 09:38:52 -0400547 mm->first_msg_id_reply = (retval >= 0) ? ntohs (mp->first_msg_id) : ~0;
548 mm->first_msg_id_reply_ready = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700549}
550
Dave Barach371e4e12016-07-08 09:38:52 -0400551u16
Neale Rannse72be392017-04-26 13:59:20 -0700552vl_client_get_first_plugin_msg_id (const char *plugin_name)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700553{
Dave Barach371e4e12016-07-08 09:38:52 -0400554 vl_api_get_first_msg_id_t *mp;
Dave Barach39d69112019-11-27 11:42:13 -0500555 api_main_t *am = vlibapi_get_main ();
556 memory_client_main_t *mm = vlibapi_get_memory_client_main ();
Dave Barach371e4e12016-07-08 09:38:52 -0400557 f64 timeout;
558 void *old_handler;
559 clib_time_t clib_time;
560 u16 rv = ~0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700561
Ole Troan7adaa222019-08-27 15:05:27 +0200562 if (strlen (plugin_name) + 1 > sizeof (mp->name))
Dave Barach371e4e12016-07-08 09:38:52 -0400563 return (rv);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700564
Dave Barachb7b92992018-10-17 10:38:51 -0400565 clib_memset (&clib_time, 0, sizeof (clib_time));
Dave Barach371e4e12016-07-08 09:38:52 -0400566 clib_time_init (&clib_time);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700567
Dave Barach371e4e12016-07-08 09:38:52 -0400568 /* Push this plugin's first_msg_id_reply handler */
569 old_handler = am->msg_handlers[VL_API_GET_FIRST_MSG_ID_REPLY];
570 am->msg_handlers[VL_API_GET_FIRST_MSG_ID_REPLY] = (void *)
571 vl_api_get_first_msg_id_reply_t_handler;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700572
Dave Barach371e4e12016-07-08 09:38:52 -0400573 /* Ask the data-plane for the message-ID base of the indicated plugin */
574 mm->first_msg_id_reply_ready = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700575
Florin Coras6d1caf92018-01-04 12:17:10 -0800576 /* Not using shm client */
577 if (!am->my_registration)
Dave Barach371e4e12016-07-08 09:38:52 -0400578 {
Florin Coras6d1caf92018-01-04 12:17:10 -0800579 mp = vl_socket_client_msg_alloc (sizeof (*mp));
Dave Barachb7b92992018-10-17 10:38:51 -0400580 clib_memset (mp, 0, sizeof (*mp));
Florin Coras6d1caf92018-01-04 12:17:10 -0800581 mp->_vl_msg_id = ntohs (VL_API_GET_FIRST_MSG_ID);
582 mp->client_index = am->my_client_index;
Ole Troan7adaa222019-08-27 15:05:27 +0200583 strncpy ((char *) mp->name, plugin_name, sizeof (mp->name) - 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700584
Florin Coras6d1caf92018-01-04 12:17:10 -0800585 if (vl_socket_client_write () <= 0)
586 goto sock_err;
587 if (vl_socket_client_read (1))
588 goto sock_err;
589
590 if (mm->first_msg_id_reply_ready == 1)
591 {
592 rv = mm->first_msg_id_reply;
593 goto result;
594 }
595
596 sock_err:
597 /* Restore old handler */
598 am->msg_handlers[VL_API_GET_FIRST_MSG_ID_REPLY] = old_handler;
599
600 return -1;
601 }
602 else
603 {
604 mp = vl_msg_api_alloc (sizeof (*mp));
Dave Barachb7b92992018-10-17 10:38:51 -0400605 clib_memset (mp, 0, sizeof (*mp));
Florin Coras6d1caf92018-01-04 12:17:10 -0800606 mp->_vl_msg_id = ntohs (VL_API_GET_FIRST_MSG_ID);
607 mp->client_index = am->my_client_index;
Ole Troan7adaa222019-08-27 15:05:27 +0200608 strncpy ((char *) mp->name, plugin_name, sizeof (mp->name) - 1);
Florin Coras6d1caf92018-01-04 12:17:10 -0800609
610 vl_msg_api_send_shmem (am->shmem_hdr->vl_input_queue, (u8 *) & mp);
611
612 /* Synchronously wait for the answer */
613 timeout = clib_time_now (&clib_time) + 1.0;
Dave Barach371e4e12016-07-08 09:38:52 -0400614 while (clib_time_now (&clib_time) < timeout)
615 {
616 if (mm->first_msg_id_reply_ready == 1)
617 {
618 rv = mm->first_msg_id_reply;
619 goto result;
620 }
621 }
622 /* Restore old handler */
623 am->msg_handlers[VL_API_GET_FIRST_MSG_ID_REPLY] = old_handler;
624
625 return rv;
626 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700627
628result:
629
Dave Barach371e4e12016-07-08 09:38:52 -0400630 /* Restore the old handler */
631 am->msg_handlers[VL_API_GET_FIRST_MSG_ID_REPLY] = old_handler;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700632
Dave Barach371e4e12016-07-08 09:38:52 -0400633 if (rv == (u16) ~ 0)
634 clib_warning ("plugin '%s' not registered", plugin_name);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700635
Dave Barach371e4e12016-07-08 09:38:52 -0400636 return rv;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700637}
Dave Barach6931f592016-05-13 12:55:01 -0400638
Dave Barach371e4e12016-07-08 09:38:52 -0400639/*
640 * fd.io coding-style-patch-verification: ON
641 *
642 * Local Variables:
643 * eval: (c-set-style "gnu")
644 * End:
645 */