2 *------------------------------------------------------------------
3 * memory_client.c - API message handling, client code.
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:
10 * http://www.apache.org/licenses/LICENSE-2.0
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 *------------------------------------------------------------------
24 #include <vppinfra/serialize.h>
25 #include <vppinfra/hash.h>
26 #include <vlibmemory/memory_client.h>
27 #include <vlibapi/api_common.h>
29 /* A hack. vl_client_get_first_plugin_msg_id depends on it */
30 #include <vlibmemory/socket_client.h>
32 #include <vlibmemory/vl_memory_msg_enum.h>
34 #define vl_typedefs /* define message structures */
35 #include <vlibmemory/vl_memory_api_h.h>
38 #define vl_endianfun /* define message structures */
39 #include <vlibmemory/vl_memory_api_h.h>
42 #define vl_calcsizefun
43 #include <vlibmemory/vl_memory_api_h.h>
46 /* instantiate all the print functions we know about */
48 #include <vlibmemory/vl_memory_api_h.h>
51 memory_client_main_t memory_client_main;
52 __thread memory_client_main_t *my_memory_client_main = &memory_client_main;
54 typedef struct rx_thread_fn_arg
57 memory_client_main_t *mm;
61 rx_thread_fn (void *arg)
63 rx_thread_fn_arg_t *a = (rx_thread_fn_arg_t *) arg;
64 memory_client_main_t *mm;
67 vlibapi_set_main (a->am);
68 vlibapi_set_memory_client_main (a->mm);
71 mm = vlibapi_get_memory_client_main ();
72 q = vlibapi_get_main ()->vl_input_queue;
74 /* So we can make the rx thread terminate cleanly */
75 if (setjmp (mm->rx_thread_jmpbuf) == 0)
77 mm->rx_thread_jmpbuf_valid = 1;
78 clib_mem_set_thread_index ();
80 vl_msg_api_queue_handler (q);
86 vl_api_rx_thread_exit_t_handler (vl_api_rx_thread_exit_t * mp)
88 memory_client_main_t *mm = vlibapi_get_memory_client_main ();
89 if (mm->rx_thread_jmpbuf_valid)
90 longjmp (mm->rx_thread_jmpbuf, 1);
94 vl_api_name_and_crc_free (void)
96 api_main_t *am = vlibapi_get_main ();
101 if (!am->msg_index_by_name_and_crc)
105 hash_foreach_pair (hp, am->msg_index_by_name_and_crc,
107 vec_add1 (keys, (u8 *) hp->key);
110 for (i = 0; i < vec_len (keys); i++)
113 hash_free (am->msg_index_by_name_and_crc);
116 __clib_nosanitize_addr static void
117 VL_API_VEC_UNPOISON (const void *v)
119 const vec_header_t *vh = &((vec_header_t *) v)[-1];
120 clib_mem_unpoison (vh, sizeof (*vh) + vec_len (v));
124 vl_api_memclnt_create_reply_t_handler (vl_api_memclnt_create_reply_t * mp)
126 serialize_main_t _sm, *sm = &_sm;
127 api_main_t *am = vlibapi_get_main ();
134 am->my_client_index = mp->index;
135 am->my_registration = (vl_api_registration_t *) (uword) mp->handle;
137 /* Clean out any previous hash table (unlikely) */
138 vl_api_name_and_crc_free ();
140 am->msg_index_by_name_and_crc = hash_create_string (0, sizeof (uword));
142 /* Recreate the vnet-side API message handler table */
143 tblv = uword_to_pointer (mp->message_table, u8 *);
144 unserialize_open_data (sm, tblv, vec_len (tblv));
145 unserialize_integer (sm, &nmsgs, sizeof (u32));
147 VL_API_VEC_UNPOISON (tblv);
149 for (i = 0; i < nmsgs; i++)
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);
157 void vl_msg_api_send_shmem (svm_queue_t * q, u8 * elem);
159 vl_client_connect (const char *name, int ctx_quota, int input_queue_size)
161 vl_api_memclnt_create_t *mp;
162 vl_api_memclnt_create_reply_t *rp;
163 svm_queue_t *vl_input_queue;
164 vl_shmem_hdr_t *shmem_hdr;
167 api_main_t *am = vlibapi_get_main ();
169 if (am->my_registration)
171 clib_warning ("client %s already connected...", name);
175 if (am->vlib_rp == 0)
177 clib_warning ("am->vlib_rp NULL");
181 shmem_hdr = am->shmem_hdr;
183 if (shmem_hdr == 0 || shmem_hdr->vl_input_queue == 0)
185 clib_warning ("shmem_hdr / input queue NULL");
189 clib_mem_unpoison (shmem_hdr, sizeof (*shmem_hdr));
190 VL_MSG_API_SVM_QUEUE_UNPOISON (shmem_hdr->vl_input_queue);
192 oldheap = vl_msg_push_heap ();
193 vl_input_queue = svm_queue_alloc_and_init (input_queue_size, sizeof (uword),
195 vl_msg_pop_heap (oldheap);
197 am->my_client_index = ~0;
198 am->my_registration = 0;
199 am->vl_input_queue = vl_input_queue;
201 mp = vl_msg_api_alloc (sizeof (vl_api_memclnt_create_t));
202 clib_memset (mp, 0, sizeof (*mp));
203 mp->_vl_msg_id = ntohs (VL_API_MEMCLNT_CREATE);
204 mp->ctx_quota = ctx_quota;
205 mp->input_queue = (uword) vl_input_queue;
206 strncpy ((char *) mp->name, name, sizeof (mp->name) - 1);
208 vl_msg_api_send_shmem (shmem_hdr->vl_input_queue, (u8 *) & mp);
213 struct timespec ts, tsrem;
216 /* Wait up to 10 seconds */
217 for (i = 0; i < 1000; i++)
219 qstatus = svm_queue_sub (vl_input_queue, (u8 *) & rp,
224 ts.tv_nsec = 10000 * 1000; /* 10 ms */
225 while (nanosleep (&ts, &tsrem) < 0)
229 clib_warning ("memclnt_create_reply timeout");
233 VL_MSG_API_UNPOISON (rp);
234 if (ntohs (rp->_vl_msg_id) != VL_API_MEMCLNT_CREATE_REPLY)
236 clib_warning ("unexpected reply: id %d", ntohs (rp->_vl_msg_id));
239 rv = clib_net_to_host_u32 (rp->response);
241 msgbuf_t *msgbuf = (msgbuf_t *) ((u8 *) rp - offsetof (msgbuf_t, data));
242 vl_msg_api_handler ((void *) rp, ntohl (msgbuf->data_len));
249 vl_api_memclnt_delete_reply_t_handler (vl_api_memclnt_delete_reply_t * mp)
252 api_main_t *am = vlibapi_get_main ();
254 oldheap = vl_msg_push_heap ();
255 svm_queue_free (am->vl_input_queue);
256 vl_msg_pop_heap (oldheap);
258 am->my_client_index = ~0;
259 am->my_registration = 0;
260 am->vl_input_queue = 0;
264 vl_client_send_disconnect (u8 do_cleanup)
266 vl_api_memclnt_delete_t *mp;
267 vl_shmem_hdr_t *shmem_hdr;
268 api_main_t *am = vlibapi_get_main ();
270 ASSERT (am->vlib_rp);
271 shmem_hdr = am->shmem_hdr;
272 ASSERT (shmem_hdr && shmem_hdr->vl_input_queue);
274 mp = vl_msg_api_alloc (sizeof (vl_api_memclnt_delete_t));
275 clib_memset (mp, 0, sizeof (*mp));
276 mp->_vl_msg_id = ntohs (VL_API_MEMCLNT_DELETE);
277 mp->index = am->my_client_index;
278 mp->handle = (uword) am->my_registration;
279 mp->do_cleanup = do_cleanup;
281 vl_msg_api_send_shmem (shmem_hdr->vl_input_queue, (u8 *) & mp);
285 vl_client_disconnect (void)
287 vl_api_memclnt_delete_reply_t *rp;
288 svm_queue_t *vl_input_queue;
289 api_main_t *am = vlibapi_get_main ();
293 vl_input_queue = am->vl_input_queue;
294 vl_client_send_disconnect (0 /* wait for reply */ );
297 * Have to be careful here, in case the client is disconnecting
298 * because e.g. the vlib process died, or is unresponsive.
307 if (now >= (begin + 2))
309 clib_warning ("peer unresponsive, give up");
310 am->my_client_index = ~0;
311 am->my_registration = 0;
315 if (svm_queue_sub (vl_input_queue, (u8 *) & rp, SVM_Q_NOWAIT, 0) < 0)
318 VL_MSG_API_UNPOISON (rp);
320 /* drain the queue */
321 if (ntohs (rp->_vl_msg_id) != VL_API_MEMCLNT_DELETE_REPLY)
323 clib_warning ("queue drain: %d", ntohs (rp->_vl_msg_id));
324 msgbuf = (msgbuf_t *) ((u8 *) rp - offsetof (msgbuf_t, data));
325 vl_msg_api_handler ((void *) rp, ntohl (msgbuf->data_len));
328 msgbuf = (msgbuf_t *) ((u8 *) rp - offsetof (msgbuf_t, data));
329 vl_msg_api_handler ((void *) rp, ntohl (msgbuf->data_len));
333 vl_api_name_and_crc_free ();
338 * Stave off the binary API dead client reaper
339 * Only sent to inactive clients
342 vl_api_memclnt_keepalive_t_handler (vl_api_memclnt_keepalive_t * mp)
344 vl_api_memclnt_keepalive_reply_t *rmp;
346 vl_shmem_hdr_t *shmem_hdr;
348 am = vlibapi_get_main ();
349 shmem_hdr = am->shmem_hdr;
351 rmp = vl_msg_api_alloc_as_if_client (sizeof (*rmp));
352 clib_memset (rmp, 0, sizeof (*rmp));
353 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);
358 #define foreach_api_msg \
359 _(RX_THREAD_EXIT, rx_thread_exit) \
360 _(MEMCLNT_CREATE_REPLY, memclnt_create_reply) \
361 _(MEMCLNT_DELETE_REPLY, memclnt_delete_reply) \
362 _(MEMCLNT_KEEPALIVE, memclnt_keepalive)
365 vl_client_install_client_message_handlers (void)
367 api_main_t *am = vlibapi_get_main ();
369 vl_msg_api_config (&(vl_msg_api_msg_config_t){ \
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), \
377 .tojson = vl_api_##n##_t_tojson, \
378 .fromjson = vl_api_##n##_t_fromjson, \
379 .calc_size = vl_api_##n##_t_calc_size, \
381 am->msg_data[VL_API_##N].replay_allowed = 0;
387 vl_client_api_map (const char *region_name)
391 if ((rv = vl_map_shmem (region_name, 0 /* is_vlib */ )) < 0)
394 vl_client_install_client_message_handlers ();
399 vl_client_api_unmap (void)
401 vl_unmap_shmem_client ();
405 vl_mem_client_is_connected (void)
407 return (my_memory_client_main->connected_to_vlib != 0);
411 connect_to_vlib_internal (const char *svm_name,
412 const char *client_name,
413 int rx_queue_size, void *(*thread_fn) (void *),
414 void *thread_fn_arg, int do_map)
417 memory_client_main_t *mm = vlibapi_get_memory_client_main ();
418 api_main_t *am = vlibapi_get_main ();
420 if (do_map && (rv = vl_client_api_map (svm_name)))
422 clib_warning ("vl_client_api map rv %d", rv);
426 if (vl_client_connect (client_name, 0 /* punt quota */ ,
427 rx_queue_size /* input queue */ ) < 0)
429 vl_client_api_unmap ();
433 /* Start the rx queue thread */
437 if (thread_fn == rx_thread_fn)
439 rx_thread_fn_arg_t *arg;
440 arg = malloc (sizeof (*arg));
441 arg->am = vlibapi_get_main ();
442 arg->mm = vlibapi_get_memory_client_main ();
443 thread_fn_arg = (void *) arg;
446 rv = pthread_create (&mm->rx_thread_handle,
447 NULL /*attr */ , thread_fn, thread_fn_arg);
450 clib_warning ("pthread_create returned %d", rv);
451 am->rx_thread_handle = 0;
455 am->rx_thread_handle = mm->rx_thread_handle;
459 mm->connected_to_vlib = 1;
464 vl_client_connect_to_vlib (const char *svm_name,
465 const char *client_name, int rx_queue_size)
467 return connect_to_vlib_internal (svm_name, client_name, rx_queue_size,
468 rx_thread_fn, 0 /* thread fn arg */ ,
473 vl_client_connect_to_vlib_no_rx_pthread (const char *svm_name,
474 const char *client_name,
477 return connect_to_vlib_internal (svm_name, client_name, rx_queue_size,
478 0 /* no rx_thread_fn */ ,
479 0 /* no thread fn arg */ ,
484 vl_client_connect_to_vlib_no_map (const char *svm_name,
485 const char *client_name, int rx_queue_size)
487 return connect_to_vlib_internal (svm_name, client_name, rx_queue_size,
488 rx_thread_fn, 0 /* no thread fn arg */ ,
493 vl_client_connect_to_vlib_no_rx_pthread_no_map (const char *svm_name,
494 const char *client_name,
497 return connect_to_vlib_internal (svm_name, client_name, rx_queue_size,
498 0 /* no thread_fn */ ,
499 0 /* no thread fn arg */ ,
504 vl_client_connect_to_vlib_thread_fn (const char *svm_name,
505 const char *client_name,
507 void *(*thread_fn) (void *), void *arg)
509 return connect_to_vlib_internal (svm_name, client_name, rx_queue_size,
510 thread_fn, arg, 1 /* do map */ );
514 vl_client_stop_rx_thread (svm_queue_t *vl_input_queue)
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);
523 disconnect_from_vlib_internal (u8 do_unmap)
525 memory_client_main_t *mm = vlibapi_get_memory_client_main ();
526 api_main_t *am = vlibapi_get_main ();
529 if (mm->rx_thread_jmpbuf_valid)
531 vl_client_stop_rx_thread (am->vl_input_queue);
532 pthread_join (mm->rx_thread_handle, (void **) &junk);
534 if (mm->connected_to_vlib)
536 vl_client_disconnect ();
538 vl_client_api_unmap ();
540 clib_memset (mm, 0, sizeof (*mm));
544 vl_client_disconnect_from_vlib (void)
546 disconnect_from_vlib_internal (1);
550 vl_client_disconnect_from_vlib_no_unmap (void)
552 disconnect_from_vlib_internal (0);
555 static void vl_api_get_first_msg_id_reply_t_handler
556 (vl_api_get_first_msg_id_reply_t * mp)
558 memory_client_main_t *mm = vlibapi_get_memory_client_main ();
559 i32 retval = ntohl (mp->retval);
561 mm->first_msg_id_reply = (retval >= 0) ? ntohs (mp->first_msg_id) : ~0;
562 mm->first_msg_id_reply_ready = 1;
566 vl_client_get_first_plugin_msg_id (const char *plugin_name)
568 vl_api_get_first_msg_id_t *mp;
569 api_main_t *am = vlibapi_get_main ();
570 memory_client_main_t *mm = vlibapi_get_memory_client_main ();
571 vl_api_msg_data_t *m =
572 vl_api_get_msg_data (am, VL_API_GET_FIRST_MSG_ID_REPLY);
575 clib_time_t clib_time;
578 if (strlen (plugin_name) + 1 > sizeof (mp->name))
581 clib_memset (&clib_time, 0, sizeof (clib_time));
582 clib_time_init (&clib_time);
584 /* Push this plugin's first_msg_id_reply handler */
585 old_handler = m->handler;
586 m->handler = (void *) vl_api_get_first_msg_id_reply_t_handler;
587 if (!m->calc_size_func)
590 (uword (*) (void *)) vl_api_get_first_msg_id_reply_t_calc_size;
593 /* Ask the data-plane for the message-ID base of the indicated plugin */
594 mm->first_msg_id_reply_ready = 0;
596 /* Not using shm client */
597 if (!am->my_registration)
599 mp = vl_socket_client_msg_alloc (sizeof (*mp));
600 clib_memset (mp, 0, sizeof (*mp));
601 mp->_vl_msg_id = ntohs (VL_API_GET_FIRST_MSG_ID);
602 mp->client_index = am->my_client_index;
603 strncpy ((char *) mp->name, plugin_name, sizeof (mp->name) - 1);
605 if (vl_socket_client_write () <= 0)
607 if (vl_socket_client_read (1))
610 if (mm->first_msg_id_reply_ready == 1)
612 rv = mm->first_msg_id_reply;
617 /* Restore old handler */
618 m->handler = old_handler;
624 mp = vl_msg_api_alloc (sizeof (*mp));
625 clib_memset (mp, 0, sizeof (*mp));
626 mp->_vl_msg_id = ntohs (VL_API_GET_FIRST_MSG_ID);
627 mp->client_index = am->my_client_index;
628 strncpy ((char *) mp->name, plugin_name, sizeof (mp->name) - 1);
630 vl_msg_api_send_shmem (am->shmem_hdr->vl_input_queue, (u8 *) & mp);
632 /* Synchronously wait for the answer */
633 timeout = clib_time_now (&clib_time) + 1.0;
634 while (clib_time_now (&clib_time) < timeout)
636 if (mm->first_msg_id_reply_ready == 1)
638 rv = mm->first_msg_id_reply;
642 /* Restore old handler */
643 m->handler = old_handler;
650 /* Restore the old handler */
651 m->handler = old_handler;
654 clib_warning ("plugin '%s' not registered", plugin_name);
660 * fd.io coding-style-patch-verification: ON
663 * eval: (c-set-style "gnu")