blob: c66c71c43b1ebf19d67853760b66b075da075226 [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
Klement Sekera9b7e8ac2021-11-22 21:26:20 +010042#define vl_calcsizefun
43#include <vlibmemory/vl_memory_api_h.h>
44#undef vl_calcsizefun
45
Ed Warnickecb9cada2015-12-08 15:45:58 -070046/* instantiate all the print functions we know about */
Ed Warnickecb9cada2015-12-08 15:45:58 -070047#define vl_printfun
48#include <vlibmemory/vl_memory_api_h.h>
49#undef vl_printfun
50
Ed Warnickecb9cada2015-12-08 15:45:58 -070051memory_client_main_t memory_client_main;
Dave Barach39d69112019-11-27 11:42:13 -050052__thread memory_client_main_t *my_memory_client_main = &memory_client_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -070053
Florin Coras684fb6e2019-12-09 16:42:50 -080054typedef 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 Barach371e4e12016-07-08 09:38:52 -040060static void *
61rx_thread_fn (void *arg)
Ed Warnickecb9cada2015-12-08 15:45:58 -070062{
Florin Coras684fb6e2019-12-09 16:42:50 -080063 rx_thread_fn_arg_t *a = (rx_thread_fn_arg_t *) arg;
64 memory_client_main_t *mm;
Florin Corase86a8ed2018-01-05 03:20:25 -080065 svm_queue_t *q;
Ed Warnickecb9cada2015-12-08 15:45:58 -070066
Florin Coras684fb6e2019-12-09 16:42:50 -080067 vlibapi_set_main (a->am);
68 vlibapi_set_memory_client_main (a->mm);
Florin Coras68f58d72020-05-11 18:03:40 +000069 free (a);
Florin Coras684fb6e2019-12-09 16:42:50 -080070
71 mm = vlibapi_get_memory_client_main ();
Dave Barach39d69112019-11-27 11:42:13 -050072 q = vlibapi_get_main ()->vl_input_queue;
Ed Warnickecb9cada2015-12-08 15:45:58 -070073
Dave Barach371e4e12016-07-08 09:38:52 -040074 /* 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 Skrzypczakd516ca42019-08-01 18:14:06 +020078 clib_mem_set_thread_index ();
Dave Barachcf5e8482017-10-17 11:48:29 -040079 while (1)
80 vl_msg_api_queue_handler (q);
Ed Warnickecb9cada2015-12-08 15:45:58 -070081 }
Dave Barach371e4e12016-07-08 09:38:52 -040082 pthread_exit (0);
Ed Warnickecb9cada2015-12-08 15:45:58 -070083}
84
Dave Barach371e4e12016-07-08 09:38:52 -040085static void
86vl_api_rx_thread_exit_t_handler (vl_api_rx_thread_exit_t * mp)
Ed Warnickecb9cada2015-12-08 15:45:58 -070087{
Dave Barach39d69112019-11-27 11:42:13 -050088 memory_client_main_t *mm = vlibapi_get_memory_client_main ();
Ole Troanc5607892019-04-10 19:32:02 +020089 if (mm->rx_thread_jmpbuf_valid)
90 longjmp (mm->rx_thread_jmpbuf, 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -070091}
92
Dave Barach371e4e12016-07-08 09:38:52 -040093static void
Ole Troan73710c72018-06-04 22:27:49 +020094vl_api_name_and_crc_free (void)
95{
Dave Barach39d69112019-11-27 11:42:13 -050096 api_main_t *am = vlibapi_get_main ();
Ole Troan73710c72018-06-04 22:27:49 +020097 int i;
98 u8 **keys = 0;
99 hash_pair_t *hp;
100
101 if (!am->msg_index_by_name_and_crc)
102 return;
103
104 /* *INDENT-OFF* */
105 hash_foreach_pair (hp, am->msg_index_by_name_and_crc,
106 ({
107 vec_add1 (keys, (u8 *) hp->key);
108 }));
109 /* *INDENT-ON* */
110 for (i = 0; i < vec_len (keys); i++)
111 vec_free (keys[i]);
112 vec_free (keys);
113 hash_free (am->msg_index_by_name_and_crc);
114}
115
Damjan Marion79934e82022-04-05 12:40:31 +0200116__clib_nosanitize_addr static void
BenoƮt Ganne9fb6d402019-04-15 15:28:21 +0200117VL_API_VEC_UNPOISON (const void *v)
118{
119 const vec_header_t *vh = &((vec_header_t *) v)[-1];
Damjan Marion79934e82022-04-05 12:40:31 +0200120 clib_mem_unpoison (vh, sizeof (*vh) + vec_len (v));
BenoƮt Ganne9fb6d402019-04-15 15:28:21 +0200121}
122
Ole Troan73710c72018-06-04 22:27:49 +0200123static void
Dave Barach5c6c4bf2017-04-11 13:12:48 -0400124vl_api_memclnt_create_reply_t_handler (vl_api_memclnt_create_reply_t * mp)
125{
126 serialize_main_t _sm, *sm = &_sm;
Dave Barach39d69112019-11-27 11:42:13 -0500127 api_main_t *am = vlibapi_get_main ();
Dave Barach5c6c4bf2017-04-11 13:12:48 -0400128 u8 *tblv;
129 u32 nmsgs;
130 int i;
131 u8 *name_and_crc;
132 u32 msg_index;
133
134 am->my_client_index = mp->index;
135 am->my_registration = (vl_api_registration_t *) (uword) mp->handle;
136
137 /* Clean out any previous hash table (unlikely) */
Ole Troan73710c72018-06-04 22:27:49 +0200138 vl_api_name_and_crc_free ();
Dave Barach5c6c4bf2017-04-11 13:12:48 -0400139
140 am->msg_index_by_name_and_crc = hash_create_string (0, sizeof (uword));
141
142 /* Recreate the vnet-side API message handler table */
Damjan Marion7bee80c2017-04-26 15:32:12 +0200143 tblv = uword_to_pointer (mp->message_table, u8 *);
Dave Barachcf5e8482017-10-17 11:48:29 -0400144 unserialize_open_data (sm, tblv, vec_len (tblv));
Dave Barach5c6c4bf2017-04-11 13:12:48 -0400145 unserialize_integer (sm, &nmsgs, sizeof (u32));
146
BenoƮt Ganne9fb6d402019-04-15 15:28:21 +0200147 VL_API_VEC_UNPOISON (tblv);
148
Dave Barach5c6c4bf2017-04-11 13:12:48 -0400149 for (i = 0; i < nmsgs; i++)
150 {
151 msg_index = unserialize_likely_small_unsigned_integer (sm);
152 unserialize_cstring (sm, (char **) &name_and_crc);
153 hash_set_mem (am->msg_index_by_name_and_crc, name_and_crc, msg_index);
154 }
155}
156
Ole Troan2e1c8962019-04-10 09:44:23 +0200157void vl_msg_api_send_shmem (svm_queue_t * q, u8 * elem);
Dave Barach5c6c4bf2017-04-11 13:12:48 -0400158int
Neale Rannse72be392017-04-26 13:59:20 -0700159vl_client_connect (const char *name, int ctx_quota, int input_queue_size)
Dave Barach5c6c4bf2017-04-11 13:12:48 -0400160{
Dave Barach5c6c4bf2017-04-11 13:12:48 -0400161 vl_api_memclnt_create_t *mp;
162 vl_api_memclnt_create_reply_t *rp;
Florin Corase86a8ed2018-01-05 03:20:25 -0800163 svm_queue_t *vl_input_queue;
Dave Barach5c6c4bf2017-04-11 13:12:48 -0400164 vl_shmem_hdr_t *shmem_hdr;
165 int rv = 0;
166 void *oldheap;
Dave Barach39d69112019-11-27 11:42:13 -0500167 api_main_t *am = vlibapi_get_main ();
Dave Barach5c6c4bf2017-04-11 13:12:48 -0400168
169 if (am->my_registration)
170 {
171 clib_warning ("client %s already connected...", name);
172 return -1;
173 }
174
175 if (am->vlib_rp == 0)
176 {
177 clib_warning ("am->vlib_rp NULL");
178 return -1;
179 }
180
Dave Barach5c6c4bf2017-04-11 13:12:48 -0400181 shmem_hdr = am->shmem_hdr;
182
183 if (shmem_hdr == 0 || shmem_hdr->vl_input_queue == 0)
184 {
185 clib_warning ("shmem_hdr / input queue NULL");
186 return -1;
187 }
188
Damjan Marion79934e82022-04-05 12:40:31 +0200189 clib_mem_unpoison (shmem_hdr, sizeof (*shmem_hdr));
BenoƮt Ganne9fb6d402019-04-15 15:28:21 +0200190 VL_MSG_API_SVM_QUEUE_UNPOISON (shmem_hdr->vl_input_queue);
191
Nathan Skrzypczak0aa40132019-11-25 16:29:38 +0100192 oldheap = vl_msg_push_heap ();
Florin Corasc470e222018-08-01 07:53:18 -0700193 vl_input_queue = svm_queue_alloc_and_init (input_queue_size, sizeof (uword),
194 getpid ());
Nathan Skrzypczak0aa40132019-11-25 16:29:38 +0100195 vl_msg_pop_heap (oldheap);
Dave Barach5c6c4bf2017-04-11 13:12:48 -0400196
197 am->my_client_index = ~0;
198 am->my_registration = 0;
199 am->vl_input_queue = vl_input_queue;
200
201 mp = vl_msg_api_alloc (sizeof (vl_api_memclnt_create_t));
Dave Barachb7b92992018-10-17 10:38:51 -0400202 clib_memset (mp, 0, sizeof (*mp));
Dave Barach5c6c4bf2017-04-11 13:12:48 -0400203 mp->_vl_msg_id = ntohs (VL_API_MEMCLNT_CREATE);
204 mp->ctx_quota = ctx_quota;
205 mp->input_queue = (uword) vl_input_queue;
Ole Troan7adaa222019-08-27 15:05:27 +0200206 strncpy ((char *) mp->name, name, sizeof (mp->name) - 1);
Dave Barach5c6c4bf2017-04-11 13:12:48 -0400207
208 vl_msg_api_send_shmem (shmem_hdr->vl_input_queue, (u8 *) & mp);
209
210 while (1)
211 {
212 int qstatus;
213 struct timespec ts, tsrem;
214 int i;
215
216 /* Wait up to 10 seconds */
217 for (i = 0; i < 1000; i++)
218 {
Florin Corase86a8ed2018-01-05 03:20:25 -0800219 qstatus = svm_queue_sub (vl_input_queue, (u8 *) & rp,
Mohsin Kazmi3fca5672018-01-04 18:57:26 +0100220 SVM_Q_NOWAIT, 0);
Dave Barach5c6c4bf2017-04-11 13:12:48 -0400221 if (qstatus == 0)
222 goto read_one_msg;
223 ts.tv_sec = 0;
224 ts.tv_nsec = 10000 * 1000; /* 10 ms */
225 while (nanosleep (&ts, &tsrem) < 0)
226 ts = tsrem;
227 }
228 /* Timeout... */
229 clib_warning ("memclnt_create_reply timeout");
230 return -1;
231
232 read_one_msg:
BenoƮt Ganne9fb6d402019-04-15 15:28:21 +0200233 VL_MSG_API_UNPOISON (rp);
Dave Barach5c6c4bf2017-04-11 13:12:48 -0400234 if (ntohs (rp->_vl_msg_id) != VL_API_MEMCLNT_CREATE_REPLY)
235 {
236 clib_warning ("unexpected reply: id %d", ntohs (rp->_vl_msg_id));
237 continue;
238 }
239 rv = clib_net_to_host_u32 (rp->response);
240
Klement Sekera9b7e8ac2021-11-22 21:26:20 +0100241 msgbuf_t *msgbuf = (msgbuf_t *) ((u8 *) rp - offsetof (msgbuf_t, data));
242 vl_msg_api_handler ((void *) rp, ntohl (msgbuf->data_len));
Dave Barach5c6c4bf2017-04-11 13:12:48 -0400243 break;
244 }
245 return (rv);
246}
247
248static void
249vl_api_memclnt_delete_reply_t_handler (vl_api_memclnt_delete_reply_t * mp)
250{
251 void *oldheap;
Dave Barach39d69112019-11-27 11:42:13 -0500252 api_main_t *am = vlibapi_get_main ();
Dave Barach5c6c4bf2017-04-11 13:12:48 -0400253
Nathan Skrzypczak0aa40132019-11-25 16:29:38 +0100254 oldheap = vl_msg_push_heap ();
Florin Corase86a8ed2018-01-05 03:20:25 -0800255 svm_queue_free (am->vl_input_queue);
Nathan Skrzypczak0aa40132019-11-25 16:29:38 +0100256 vl_msg_pop_heap (oldheap);
Dave Barach5c6c4bf2017-04-11 13:12:48 -0400257
258 am->my_client_index = ~0;
259 am->my_registration = 0;
260 am->vl_input_queue = 0;
261}
262
Florin Coras940f78f2018-11-30 12:11:20 -0800263void
Florin Coraseaec2a62018-12-04 16:34:05 -0800264vl_client_send_disconnect (u8 do_cleanup)
Dave Barach5c6c4bf2017-04-11 13:12:48 -0400265{
266 vl_api_memclnt_delete_t *mp;
Dave Barach5c6c4bf2017-04-11 13:12:48 -0400267 vl_shmem_hdr_t *shmem_hdr;
Dave Barach39d69112019-11-27 11:42:13 -0500268 api_main_t *am = vlibapi_get_main ();
Dave Barach5c6c4bf2017-04-11 13:12:48 -0400269
270 ASSERT (am->vlib_rp);
271 shmem_hdr = am->shmem_hdr;
272 ASSERT (shmem_hdr && shmem_hdr->vl_input_queue);
273
Dave Barach5c6c4bf2017-04-11 13:12:48 -0400274 mp = vl_msg_api_alloc (sizeof (vl_api_memclnt_delete_t));
Dave Barachb7b92992018-10-17 10:38:51 -0400275 clib_memset (mp, 0, sizeof (*mp));
Dave Barach5c6c4bf2017-04-11 13:12:48 -0400276 mp->_vl_msg_id = ntohs (VL_API_MEMCLNT_DELETE);
277 mp->index = am->my_client_index;
278 mp->handle = (uword) am->my_registration;
Florin Coraseaec2a62018-12-04 16:34:05 -0800279 mp->do_cleanup = do_cleanup;
Dave Barach5c6c4bf2017-04-11 13:12:48 -0400280
281 vl_msg_api_send_shmem (shmem_hdr->vl_input_queue, (u8 *) & mp);
Florin Coras940f78f2018-11-30 12:11:20 -0800282}
283
284int
285vl_client_disconnect (void)
286{
287 vl_api_memclnt_delete_reply_t *rp;
288 svm_queue_t *vl_input_queue;
Dave Barach39d69112019-11-27 11:42:13 -0500289 api_main_t *am = vlibapi_get_main ();
Florin Coras940f78f2018-11-30 12:11:20 -0800290 time_t begin;
Klement Sekera9b7e8ac2021-11-22 21:26:20 +0100291 msgbuf_t *msgbuf;
Florin Coras940f78f2018-11-30 12:11:20 -0800292
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));
Klement Sekera9b7e8ac2021-11-22 21:26:20 +0100324 msgbuf = (msgbuf_t *) ((u8 *) rp - offsetof (msgbuf_t, data));
325 vl_msg_api_handler ((void *) rp, ntohl (msgbuf->data_len));
Dave Barach5c6c4bf2017-04-11 13:12:48 -0400326 continue;
327 }
Klement Sekera9b7e8ac2021-11-22 21:26:20 +0100328 msgbuf = (msgbuf_t *) ((u8 *) rp - offsetof (msgbuf_t, data));
329 vl_msg_api_handler ((void *) rp, ntohl (msgbuf->data_len));
Dave Barach5c6c4bf2017-04-11 13:12:48 -0400330 break;
331 }
Ole Troan73710c72018-06-04 22:27:49 +0200332
333 vl_api_name_and_crc_free ();
Florin Corasd6c30d92018-01-29 05:11:24 -0800334 return 0;
Dave Barach5c6c4bf2017-04-11 13:12:48 -0400335}
336
Dave Barach59b25652017-09-10 15:04:27 -0400337/**
338 * Stave off the binary API dead client reaper
339 * Only sent to inactive clients
340 */
341static void
342vl_api_memclnt_keepalive_t_handler (vl_api_memclnt_keepalive_t * mp)
343{
344 vl_api_memclnt_keepalive_reply_t *rmp;
345 api_main_t *am;
346 vl_shmem_hdr_t *shmem_hdr;
347
Dave Barach39d69112019-11-27 11:42:13 -0500348 am = vlibapi_get_main ();
Dave Barach59b25652017-09-10 15:04:27 -0400349 shmem_hdr = am->shmem_hdr;
350
351 rmp = vl_msg_api_alloc_as_if_client (sizeof (*rmp));
Dave Barachb7b92992018-10-17 10:38:51 -0400352 clib_memset (rmp, 0, sizeof (*rmp));
Dave Barach59b25652017-09-10 15:04:27 -0400353 rmp->_vl_msg_id = ntohs (VL_API_MEMCLNT_KEEPALIVE_REPLY);
354 rmp->context = mp->context;
355 vl_msg_api_send_shmem (shmem_hdr->vl_input_queue, (u8 *) & rmp);
356}
357
Dave Barach5c6c4bf2017-04-11 13:12:48 -0400358#define foreach_api_msg \
359_(RX_THREAD_EXIT, rx_thread_exit) \
360_(MEMCLNT_CREATE_REPLY, memclnt_create_reply) \
Dave Barach59b25652017-09-10 15:04:27 -0400361_(MEMCLNT_DELETE_REPLY, memclnt_delete_reply) \
362_(MEMCLNT_KEEPALIVE, memclnt_keepalive)
Dave Barach5c6c4bf2017-04-11 13:12:48 -0400363
Dave Barach59b25652017-09-10 15:04:27 -0400364void
365vl_client_install_client_message_handlers (void)
Dave Barach5c6c4bf2017-04-11 13:12:48 -0400366{
Filip Tehlar36217e32021-07-23 08:51:10 +0000367 api_main_t *am = vlibapi_get_main ();
368#define _(N, n) \
Damjan Mariona2eb5072022-05-20 20:06:01 +0200369 vl_msg_api_config (&(vl_msg_api_msg_config_t){ \
370 .id = VL_API_##N, \
371 .name = #n, \
372 .handler = vl_api_##n##_t_handler, \
373 .endian = vl_api_##n##_t_endian, \
374 .format_fn = vl_api_##n##_t_format, \
375 .size = sizeof (vl_api_##n##_t), \
376 .traced = 0, \
377 .tojson = vl_api_##n##_t_tojson, \
378 .fromjson = vl_api_##n##_t_fromjson, \
379 .calc_size = vl_api_##n##_t_calc_size, \
380 }); \
Damjan Marioncada9eb2022-05-18 22:16:11 +0200381 am->msg_data[VL_API_##N].replay_allowed = 0;
Dave Barach5c6c4bf2017-04-11 13:12:48 -0400382 foreach_api_msg;
383#undef _
Dave Barach59b25652017-09-10 15:04:27 -0400384}
385
Dave Barach59b25652017-09-10 15:04:27 -0400386int
387vl_client_api_map (const char *region_name)
388{
389 int rv;
390
391 if ((rv = vl_map_shmem (region_name, 0 /* is_vlib */ )) < 0)
392 return rv;
393
394 vl_client_install_client_message_handlers ();
Dave Barach5c6c4bf2017-04-11 13:12:48 -0400395 return 0;
396}
397
398void
399vl_client_api_unmap (void)
400{
Florin Corasd6c30d92018-01-29 05:11:24 -0800401 vl_unmap_shmem_client ();
Dave Barach5c6c4bf2017-04-11 13:12:48 -0400402}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700403
Florin Corasb384b542018-01-15 01:08:33 -0800404u8
405vl_mem_client_is_connected (void)
406{
Florin Coras64cf4592019-12-12 12:01:24 -0800407 return (my_memory_client_main->connected_to_vlib != 0);
Florin Corasb384b542018-01-15 01:08:33 -0800408}
409
Dave Barach371e4e12016-07-08 09:38:52 -0400410static int
Neale Rannse72be392017-04-26 13:59:20 -0700411connect_to_vlib_internal (const char *svm_name,
412 const char *client_name,
Dave Barach920180e2019-11-14 08:03:48 -0500413 int rx_queue_size, void *(*thread_fn) (void *),
Dave Barach39d69112019-11-27 11:42:13 -0500414 void *thread_fn_arg, int do_map)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700415{
Dave Barach371e4e12016-07-08 09:38:52 -0400416 int rv = 0;
Dave Barach39d69112019-11-27 11:42:13 -0500417 memory_client_main_t *mm = vlibapi_get_memory_client_main ();
418 api_main_t *am = vlibapi_get_main ();
Dave Barach371e4e12016-07-08 09:38:52 -0400419
Dave Barach59b25652017-09-10 15:04:27 -0400420 if (do_map && (rv = vl_client_api_map (svm_name)))
Dave Barach371e4e12016-07-08 09:38:52 -0400421 {
422 clib_warning ("vl_client_api map rv %d", rv);
423 return rv;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700424 }
Dave Barach371e4e12016-07-08 09:38:52 -0400425
Dave Barach371e4e12016-07-08 09:38:52 -0400426 if (vl_client_connect (client_name, 0 /* punt quota */ ,
427 rx_queue_size /* input queue */ ) < 0)
428 {
429 vl_client_api_unmap ();
430 return -1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700431 }
432
Dave Barach371e4e12016-07-08 09:38:52 -0400433 /* Start the rx queue thread */
434
Dave Barach920180e2019-11-14 08:03:48 -0500435 if (thread_fn)
Dave Barach371e4e12016-07-08 09:38:52 -0400436 {
Florin Coras684fb6e2019-12-09 16:42:50 -0800437 if (thread_fn == rx_thread_fn)
438 {
439 rx_thread_fn_arg_t *arg;
Florin Coras68f58d72020-05-11 18:03:40 +0000440 arg = malloc (sizeof (*arg));
Florin Coras684fb6e2019-12-09 16:42:50 -0800441 arg->am = vlibapi_get_main ();
442 arg->mm = vlibapi_get_memory_client_main ();
443 thread_fn_arg = (void *) arg;
444 }
445
Dave Barach371e4e12016-07-08 09:38:52 -0400446 rv = pthread_create (&mm->rx_thread_handle,
Dave Barach39d69112019-11-27 11:42:13 -0500447 NULL /*attr */ , thread_fn, thread_fn_arg);
Dave Barach371e4e12016-07-08 09:38:52 -0400448 if (rv)
IJsbrand Wijnands82295802019-10-08 13:50:55 +0200449 {
450 clib_warning ("pthread_create returned %d", rv);
451 am->rx_thread_handle = 0;
452 }
453 else
454 {
455 am->rx_thread_handle = mm->rx_thread_handle;
456 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700457 }
Dave Barach371e4e12016-07-08 09:38:52 -0400458
459 mm->connected_to_vlib = 1;
460 return 0;
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 (const char *svm_name,
465 const char *client_name, int rx_queue_size)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700466{
Dave Barach371e4e12016-07-08 09:38:52 -0400467 return connect_to_vlib_internal (svm_name, client_name, rx_queue_size,
Dave Barach39d69112019-11-27 11:42:13 -0500468 rx_thread_fn, 0 /* thread fn arg */ ,
469 1 /* do map */ );
Ed Warnickecb9cada2015-12-08 15:45:58 -0700470}
471
Dave Barach371e4e12016-07-08 09:38:52 -0400472int
Neale Rannse72be392017-04-26 13:59:20 -0700473vl_client_connect_to_vlib_no_rx_pthread (const char *svm_name,
474 const char *client_name,
Dave Barach371e4e12016-07-08 09:38:52 -0400475 int rx_queue_size)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700476{
Dave Barach371e4e12016-07-08 09:38:52 -0400477 return connect_to_vlib_internal (svm_name, client_name, rx_queue_size,
Dave Barach920180e2019-11-14 08:03:48 -0500478 0 /* no rx_thread_fn */ ,
Dave Barach39d69112019-11-27 11:42:13 -0500479 0 /* no thread fn arg */ ,
Dave Barach59b25652017-09-10 15:04:27 -0400480 1 /* do map */ );
481}
482
483int
484vl_client_connect_to_vlib_no_map (const char *svm_name,
485 const char *client_name, int rx_queue_size)
486{
487 return connect_to_vlib_internal (svm_name, client_name, rx_queue_size,
Dave Barach39d69112019-11-27 11:42:13 -0500488 rx_thread_fn, 0 /* no thread fn arg */ ,
489 0 /* dont map */ );
Ed Warnickecb9cada2015-12-08 15:45:58 -0700490}
491
Tomasz Kulasek97dcf5b2019-01-31 18:26:32 +0100492int
493vl_client_connect_to_vlib_no_rx_pthread_no_map (const char *svm_name,
494 const char *client_name,
495 int rx_queue_size)
496{
497 return connect_to_vlib_internal (svm_name, client_name, rx_queue_size,
Dave Barach39d69112019-11-27 11:42:13 -0500498 0 /* no thread_fn */ ,
499 0 /* no thread fn arg */ ,
Tomasz Kulasek97dcf5b2019-01-31 18:26:32 +0100500 0 /* dont map */ );
501}
502
Dave Barach920180e2019-11-14 08:03:48 -0500503int
504vl_client_connect_to_vlib_thread_fn (const char *svm_name,
505 const char *client_name,
506 int rx_queue_size,
Dave Barach39d69112019-11-27 11:42:13 -0500507 void *(*thread_fn) (void *), void *arg)
Dave Barach920180e2019-11-14 08:03:48 -0500508{
509 return connect_to_vlib_internal (svm_name, client_name, rx_queue_size,
Dave Barach39d69112019-11-27 11:42:13 -0500510 thread_fn, arg, 1 /* do map */ );
Dave Barach920180e2019-11-14 08:03:48 -0500511}
512
Matthew Smith57f177d2022-12-15 22:18:08 +0000513void
514vl_client_stop_rx_thread (svm_queue_t *vl_input_queue)
515{
516 vl_api_rx_thread_exit_t *ep;
517 ep = vl_msg_api_alloc (sizeof (*ep));
518 ep->_vl_msg_id = ntohs (VL_API_RX_THREAD_EXIT);
519 vl_msg_api_send_shmem (vl_input_queue, (u8 *) &ep);
520}
Dave Barach920180e2019-11-14 08:03:48 -0500521
Florin Corasb384b542018-01-15 01:08:33 -0800522static void
523disconnect_from_vlib_internal (u8 do_unmap)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700524{
Dave Barach39d69112019-11-27 11:42:13 -0500525 memory_client_main_t *mm = vlibapi_get_memory_client_main ();
526 api_main_t *am = vlibapi_get_main ();
Dave Barach371e4e12016-07-08 09:38:52 -0400527 uword junk;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700528
Dave Barach371e4e12016-07-08 09:38:52 -0400529 if (mm->rx_thread_jmpbuf_valid)
530 {
Matthew Smith57f177d2022-12-15 22:18:08 +0000531 vl_client_stop_rx_thread (am->vl_input_queue);
Dave Barach371e4e12016-07-08 09:38:52 -0400532 pthread_join (mm->rx_thread_handle, (void **) &junk);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700533 }
Dave Barach371e4e12016-07-08 09:38:52 -0400534 if (mm->connected_to_vlib)
535 {
536 vl_client_disconnect ();
Florin Corasb384b542018-01-15 01:08:33 -0800537 if (do_unmap)
538 vl_client_api_unmap ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700539 }
Dave Barachb7b92992018-10-17 10:38:51 -0400540 clib_memset (mm, 0, sizeof (*mm));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700541}
542
Florin Corasb384b542018-01-15 01:08:33 -0800543void
544vl_client_disconnect_from_vlib (void)
545{
546 disconnect_from_vlib_internal (1);
547}
548
549void
550vl_client_disconnect_from_vlib_no_unmap (void)
551{
552 disconnect_from_vlib_internal (0);
553}
554
Ed Warnickecb9cada2015-12-08 15:45:58 -0700555static void vl_api_get_first_msg_id_reply_t_handler
Dave Barach371e4e12016-07-08 09:38:52 -0400556 (vl_api_get_first_msg_id_reply_t * mp)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700557{
Dave Barach39d69112019-11-27 11:42:13 -0500558 memory_client_main_t *mm = vlibapi_get_memory_client_main ();
Dave Barach371e4e12016-07-08 09:38:52 -0400559 i32 retval = ntohl (mp->retval);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700560
Dave Barach371e4e12016-07-08 09:38:52 -0400561 mm->first_msg_id_reply = (retval >= 0) ? ntohs (mp->first_msg_id) : ~0;
562 mm->first_msg_id_reply_ready = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700563}
564
Dave Barach371e4e12016-07-08 09:38:52 -0400565u16
Neale Rannse72be392017-04-26 13:59:20 -0700566vl_client_get_first_plugin_msg_id (const char *plugin_name)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700567{
Dave Barach371e4e12016-07-08 09:38:52 -0400568 vl_api_get_first_msg_id_t *mp;
Dave Barach39d69112019-11-27 11:42:13 -0500569 api_main_t *am = vlibapi_get_main ();
570 memory_client_main_t *mm = vlibapi_get_memory_client_main ();
Damjan Marioncada9eb2022-05-18 22:16:11 +0200571 vl_api_msg_data_t *m =
572 vl_api_get_msg_data (am, VL_API_GET_FIRST_MSG_ID_REPLY);
Dave Barach371e4e12016-07-08 09:38:52 -0400573 f64 timeout;
574 void *old_handler;
575 clib_time_t clib_time;
576 u16 rv = ~0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700577
Ole Troan7adaa222019-08-27 15:05:27 +0200578 if (strlen (plugin_name) + 1 > sizeof (mp->name))
Dave Barach371e4e12016-07-08 09:38:52 -0400579 return (rv);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700580
Dave Barachb7b92992018-10-17 10:38:51 -0400581 clib_memset (&clib_time, 0, sizeof (clib_time));
Dave Barach371e4e12016-07-08 09:38:52 -0400582 clib_time_init (&clib_time);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700583
Dave Barach371e4e12016-07-08 09:38:52 -0400584 /* Push this plugin's first_msg_id_reply handler */
Damjan Marioncada9eb2022-05-18 22:16:11 +0200585 old_handler = m->handler;
586 m->handler = (void *) vl_api_get_first_msg_id_reply_t_handler;
587 if (!m->calc_size_func)
Klement Sekera9b7e8ac2021-11-22 21:26:20 +0100588 {
Damjan Marioncada9eb2022-05-18 22:16:11 +0200589 m->calc_size_func =
Klement Sekera9b7e8ac2021-11-22 21:26:20 +0100590 (uword (*) (void *)) vl_api_get_first_msg_id_reply_t_calc_size;
591 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700592
Dave Barach371e4e12016-07-08 09:38:52 -0400593 /* Ask the data-plane for the message-ID base of the indicated plugin */
594 mm->first_msg_id_reply_ready = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700595
Florin Coras6d1caf92018-01-04 12:17:10 -0800596 /* Not using shm client */
597 if (!am->my_registration)
Dave Barach371e4e12016-07-08 09:38:52 -0400598 {
Florin Coras6d1caf92018-01-04 12:17:10 -0800599 mp = vl_socket_client_msg_alloc (sizeof (*mp));
Dave Barachb7b92992018-10-17 10:38:51 -0400600 clib_memset (mp, 0, sizeof (*mp));
Florin Coras6d1caf92018-01-04 12:17:10 -0800601 mp->_vl_msg_id = ntohs (VL_API_GET_FIRST_MSG_ID);
602 mp->client_index = am->my_client_index;
Ole Troan7adaa222019-08-27 15:05:27 +0200603 strncpy ((char *) mp->name, plugin_name, sizeof (mp->name) - 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700604
Florin Coras6d1caf92018-01-04 12:17:10 -0800605 if (vl_socket_client_write () <= 0)
606 goto sock_err;
607 if (vl_socket_client_read (1))
608 goto sock_err;
609
610 if (mm->first_msg_id_reply_ready == 1)
611 {
612 rv = mm->first_msg_id_reply;
613 goto result;
614 }
615
616 sock_err:
617 /* Restore old handler */
Damjan Marioncada9eb2022-05-18 22:16:11 +0200618 m->handler = old_handler;
Florin Coras6d1caf92018-01-04 12:17:10 -0800619
620 return -1;
621 }
622 else
623 {
624 mp = vl_msg_api_alloc (sizeof (*mp));
Dave Barachb7b92992018-10-17 10:38:51 -0400625 clib_memset (mp, 0, sizeof (*mp));
Florin Coras6d1caf92018-01-04 12:17:10 -0800626 mp->_vl_msg_id = ntohs (VL_API_GET_FIRST_MSG_ID);
627 mp->client_index = am->my_client_index;
Ole Troan7adaa222019-08-27 15:05:27 +0200628 strncpy ((char *) mp->name, plugin_name, sizeof (mp->name) - 1);
Florin Coras6d1caf92018-01-04 12:17:10 -0800629
630 vl_msg_api_send_shmem (am->shmem_hdr->vl_input_queue, (u8 *) & mp);
631
632 /* Synchronously wait for the answer */
633 timeout = clib_time_now (&clib_time) + 1.0;
Dave Barach371e4e12016-07-08 09:38:52 -0400634 while (clib_time_now (&clib_time) < timeout)
635 {
636 if (mm->first_msg_id_reply_ready == 1)
637 {
638 rv = mm->first_msg_id_reply;
639 goto result;
640 }
641 }
642 /* Restore old handler */
Damjan Marioncada9eb2022-05-18 22:16:11 +0200643 m->handler = old_handler;
Dave Barach371e4e12016-07-08 09:38:52 -0400644
645 return rv;
646 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700647
648result:
649
Dave Barach371e4e12016-07-08 09:38:52 -0400650 /* Restore the old handler */
Damjan Marioncada9eb2022-05-18 22:16:11 +0200651 m->handler = old_handler;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700652
Dave Barach371e4e12016-07-08 09:38:52 -0400653 if (rv == (u16) ~ 0)
654 clib_warning ("plugin '%s' not registered", plugin_name);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700655
Dave Barach371e4e12016-07-08 09:38:52 -0400656 return rv;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700657}
Dave Barach6931f592016-05-13 12:55:01 -0400658
Dave Barach371e4e12016-07-08 09:38:52 -0400659/*
660 * fd.io coding-style-patch-verification: ON
661 *
662 * Local Variables:
663 * eval: (c-set-style "gnu")
664 * End:
665 */