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)
104 hash_foreach_pair (hp, am->msg_index_by_name_and_crc,
106 vec_add1 (keys, (u8 *) hp->key);
108 for (i = 0; i < vec_len (keys); i++)
111 hash_free (am->msg_index_by_name_and_crc);
114 __clib_nosanitize_addr static void
115 VL_API_VEC_UNPOISON (const void *v)
117 const vec_header_t *vh = &((vec_header_t *) v)[-1];
118 clib_mem_unpoison (vh, sizeof (*vh) + vec_len (v));
122 vl_api_memclnt_create_reply_t_handler (vl_api_memclnt_create_reply_t * mp)
124 serialize_main_t _sm, *sm = &_sm;
125 api_main_t *am = vlibapi_get_main ();
132 am->my_client_index = mp->index;
133 am->my_registration = (vl_api_registration_t *) (uword) mp->handle;
135 /* Clean out any previous hash table (unlikely) */
136 vl_api_name_and_crc_free ();
138 am->msg_index_by_name_and_crc = hash_create_string (0, sizeof (uword));
140 /* Recreate the vnet-side API message handler table */
141 tblv = uword_to_pointer (mp->message_table, u8 *);
142 unserialize_open_data (sm, tblv, vec_len (tblv));
143 unserialize_integer (sm, &nmsgs, sizeof (u32));
145 VL_API_VEC_UNPOISON (tblv);
147 for (i = 0; i < nmsgs; i++)
149 msg_index = unserialize_likely_small_unsigned_integer (sm);
150 unserialize_cstring (sm, (char **) &name_and_crc);
151 hash_set_mem (am->msg_index_by_name_and_crc, name_and_crc, msg_index);
155 void vl_msg_api_send_shmem (svm_queue_t * q, u8 * elem);
157 vl_client_connect (const char *name, int ctx_quota, int input_queue_size)
159 vl_api_memclnt_create_t *mp;
160 vl_api_memclnt_create_reply_t *rp;
161 svm_queue_t *vl_input_queue;
162 vl_shmem_hdr_t *shmem_hdr;
165 api_main_t *am = vlibapi_get_main ();
167 if (am->my_registration)
169 clib_warning ("client %s already connected...", name);
173 if (am->vlib_rp == 0)
175 clib_warning ("am->vlib_rp NULL");
179 shmem_hdr = am->shmem_hdr;
181 if (shmem_hdr == 0 || shmem_hdr->vl_input_queue == 0)
183 clib_warning ("shmem_hdr / input queue NULL");
187 clib_mem_unpoison (shmem_hdr, sizeof (*shmem_hdr));
188 VL_MSG_API_SVM_QUEUE_UNPOISON (shmem_hdr->vl_input_queue);
190 oldheap = vl_msg_push_heap ();
191 vl_input_queue = svm_queue_alloc_and_init (input_queue_size, sizeof (uword),
193 vl_msg_pop_heap (oldheap);
195 am->my_client_index = ~0;
196 am->my_registration = 0;
197 am->vl_input_queue = vl_input_queue;
199 mp = vl_msg_api_alloc (sizeof (vl_api_memclnt_create_t));
200 clib_memset (mp, 0, sizeof (*mp));
201 mp->_vl_msg_id = ntohs (VL_API_MEMCLNT_CREATE);
202 mp->ctx_quota = ctx_quota;
203 mp->input_queue = (uword) vl_input_queue;
204 strncpy ((char *) mp->name, name, sizeof (mp->name) - 1);
206 vl_msg_api_send_shmem (shmem_hdr->vl_input_queue, (u8 *) & mp);
211 struct timespec ts, tsrem;
214 /* Wait up to 10 seconds */
215 for (i = 0; i < 1000; i++)
217 qstatus = svm_queue_sub (vl_input_queue, (u8 *) & rp,
222 ts.tv_nsec = 10000 * 1000; /* 10 ms */
223 while (nanosleep (&ts, &tsrem) < 0)
227 clib_warning ("memclnt_create_reply timeout");
231 VL_MSG_API_UNPOISON (rp);
232 if (ntohs (rp->_vl_msg_id) != VL_API_MEMCLNT_CREATE_REPLY)
234 clib_warning ("unexpected reply: id %d", ntohs (rp->_vl_msg_id));
237 rv = clib_net_to_host_u32 (rp->response);
239 msgbuf_t *msgbuf = (msgbuf_t *) ((u8 *) rp - offsetof (msgbuf_t, data));
240 vl_msg_api_handler ((void *) rp, ntohl (msgbuf->data_len));
247 vl_api_memclnt_delete_reply_t_handler (vl_api_memclnt_delete_reply_t * mp)
250 api_main_t *am = vlibapi_get_main ();
252 oldheap = vl_msg_push_heap ();
253 svm_queue_free (am->vl_input_queue);
254 vl_msg_pop_heap (oldheap);
256 am->my_client_index = ~0;
257 am->my_registration = 0;
258 am->vl_input_queue = 0;
262 vl_client_send_disconnect (u8 do_cleanup)
264 vl_api_memclnt_delete_t *mp;
265 vl_shmem_hdr_t *shmem_hdr;
266 api_main_t *am = vlibapi_get_main ();
268 ASSERT (am->vlib_rp);
269 shmem_hdr = am->shmem_hdr;
270 ASSERT (shmem_hdr && shmem_hdr->vl_input_queue);
272 mp = vl_msg_api_alloc (sizeof (vl_api_memclnt_delete_t));
273 clib_memset (mp, 0, sizeof (*mp));
274 mp->_vl_msg_id = ntohs (VL_API_MEMCLNT_DELETE);
275 mp->index = am->my_client_index;
276 mp->handle = (uword) am->my_registration;
277 mp->do_cleanup = do_cleanup;
279 vl_msg_api_send_shmem (shmem_hdr->vl_input_queue, (u8 *) & mp);
283 vl_client_disconnect (void)
285 vl_api_memclnt_delete_reply_t *rp;
286 svm_queue_t *vl_input_queue;
287 api_main_t *am = vlibapi_get_main ();
291 vl_input_queue = am->vl_input_queue;
292 vl_client_send_disconnect (0 /* wait for reply */ );
295 * Have to be careful here, in case the client is disconnecting
296 * because e.g. the vlib process died, or is unresponsive.
305 if (now >= (begin + 2))
307 clib_warning ("peer unresponsive, give up");
308 am->my_client_index = ~0;
309 am->my_registration = 0;
313 if (svm_queue_sub (vl_input_queue, (u8 *) & rp, SVM_Q_NOWAIT, 0) < 0)
316 VL_MSG_API_UNPOISON (rp);
318 /* drain the queue */
319 if (ntohs (rp->_vl_msg_id) != VL_API_MEMCLNT_DELETE_REPLY)
321 clib_warning ("queue drain: %d", ntohs (rp->_vl_msg_id));
322 msgbuf = (msgbuf_t *) ((u8 *) rp - offsetof (msgbuf_t, data));
323 vl_msg_api_handler ((void *) rp, ntohl (msgbuf->data_len));
326 msgbuf = (msgbuf_t *) ((u8 *) rp - offsetof (msgbuf_t, data));
327 vl_msg_api_handler ((void *) rp, ntohl (msgbuf->data_len));
331 vl_api_name_and_crc_free ();
336 * Stave off the binary API dead client reaper
337 * Only sent to inactive clients
340 vl_api_memclnt_keepalive_t_handler (vl_api_memclnt_keepalive_t * mp)
342 vl_api_memclnt_keepalive_reply_t *rmp;
344 vl_shmem_hdr_t *shmem_hdr;
346 am = vlibapi_get_main ();
347 shmem_hdr = am->shmem_hdr;
349 rmp = vl_msg_api_alloc_as_if_client (sizeof (*rmp));
350 clib_memset (rmp, 0, sizeof (*rmp));
351 rmp->_vl_msg_id = ntohs (VL_API_MEMCLNT_KEEPALIVE_REPLY);
352 rmp->context = mp->context;
353 vl_msg_api_send_shmem (shmem_hdr->vl_input_queue, (u8 *) & rmp);
356 #define foreach_api_msg \
357 _(RX_THREAD_EXIT, rx_thread_exit) \
358 _(MEMCLNT_CREATE_REPLY, memclnt_create_reply) \
359 _(MEMCLNT_DELETE_REPLY, memclnt_delete_reply) \
360 _(MEMCLNT_KEEPALIVE, memclnt_keepalive)
363 vl_client_install_client_message_handlers (void)
365 api_main_t *am = vlibapi_get_main ();
367 vl_msg_api_config (&(vl_msg_api_msg_config_t){ \
370 .handler = vl_api_##n##_t_handler, \
371 .endian = vl_api_##n##_t_endian, \
372 .format_fn = vl_api_##n##_t_format, \
373 .size = sizeof (vl_api_##n##_t), \
375 .tojson = vl_api_##n##_t_tojson, \
376 .fromjson = vl_api_##n##_t_fromjson, \
377 .calc_size = vl_api_##n##_t_calc_size, \
379 am->msg_data[VL_API_##N].replay_allowed = 0;
385 vl_client_api_map (const char *region_name)
389 if ((rv = vl_map_shmem (region_name, 0 /* is_vlib */ )) < 0)
392 vl_client_install_client_message_handlers ();
397 vl_client_api_unmap (void)
399 vl_unmap_shmem_client ();
403 vl_mem_client_is_connected (void)
405 return (my_memory_client_main->connected_to_vlib != 0);
409 connect_to_vlib_internal (const char *svm_name,
410 const char *client_name,
411 int rx_queue_size, void *(*thread_fn) (void *),
412 void *thread_fn_arg, int do_map)
415 memory_client_main_t *mm = vlibapi_get_memory_client_main ();
416 api_main_t *am = vlibapi_get_main ();
418 if (do_map && (rv = vl_client_api_map (svm_name)))
420 clib_warning ("vl_client_api map rv %d", rv);
424 if (vl_client_connect (client_name, 0 /* punt quota */ ,
425 rx_queue_size /* input queue */ ) < 0)
427 vl_client_api_unmap ();
431 /* Start the rx queue thread */
435 if (thread_fn == rx_thread_fn)
437 rx_thread_fn_arg_t *arg;
438 arg = malloc (sizeof (*arg));
439 arg->am = vlibapi_get_main ();
440 arg->mm = vlibapi_get_memory_client_main ();
441 thread_fn_arg = (void *) arg;
444 rv = pthread_create (&mm->rx_thread_handle,
445 NULL /*attr */ , thread_fn, thread_fn_arg);
448 clib_warning ("pthread_create returned %d", rv);
449 am->rx_thread_handle = 0;
453 am->rx_thread_handle = mm->rx_thread_handle;
457 mm->connected_to_vlib = 1;
462 vl_client_connect_to_vlib (const char *svm_name,
463 const char *client_name, int rx_queue_size)
465 return connect_to_vlib_internal (svm_name, client_name, rx_queue_size,
466 rx_thread_fn, 0 /* thread fn arg */ ,
471 vl_client_connect_to_vlib_no_rx_pthread (const char *svm_name,
472 const char *client_name,
475 return connect_to_vlib_internal (svm_name, client_name, rx_queue_size,
476 0 /* no rx_thread_fn */ ,
477 0 /* no thread fn arg */ ,
482 vl_client_connect_to_vlib_no_map (const char *svm_name,
483 const char *client_name, int rx_queue_size)
485 return connect_to_vlib_internal (svm_name, client_name, rx_queue_size,
486 rx_thread_fn, 0 /* no thread fn arg */ ,
491 vl_client_connect_to_vlib_no_rx_pthread_no_map (const char *svm_name,
492 const char *client_name,
495 return connect_to_vlib_internal (svm_name, client_name, rx_queue_size,
496 0 /* no thread_fn */ ,
497 0 /* no thread fn arg */ ,
502 vl_client_connect_to_vlib_thread_fn (const char *svm_name,
503 const char *client_name,
505 void *(*thread_fn) (void *), void *arg)
507 return connect_to_vlib_internal (svm_name, client_name, rx_queue_size,
508 thread_fn, arg, 1 /* do map */ );
512 vl_client_stop_rx_thread (svm_queue_t *vl_input_queue)
514 vl_api_rx_thread_exit_t *ep;
515 ep = vl_msg_api_alloc (sizeof (*ep));
516 ep->_vl_msg_id = ntohs (VL_API_RX_THREAD_EXIT);
517 vl_msg_api_send_shmem (vl_input_queue, (u8 *) &ep);
521 disconnect_from_vlib_internal (u8 do_unmap)
523 memory_client_main_t *mm = vlibapi_get_memory_client_main ();
524 api_main_t *am = vlibapi_get_main ();
527 if (mm->rx_thread_jmpbuf_valid)
529 vl_client_stop_rx_thread (am->vl_input_queue);
530 pthread_join (mm->rx_thread_handle, (void **) &junk);
532 if (mm->connected_to_vlib)
534 vl_client_disconnect ();
536 vl_client_api_unmap ();
538 clib_memset (mm, 0, sizeof (*mm));
542 vl_client_disconnect_from_vlib (void)
544 disconnect_from_vlib_internal (1);
548 vl_client_disconnect_from_vlib_no_unmap (void)
550 disconnect_from_vlib_internal (0);
553 static void vl_api_get_first_msg_id_reply_t_handler
554 (vl_api_get_first_msg_id_reply_t * mp)
556 memory_client_main_t *mm = vlibapi_get_memory_client_main ();
557 i32 retval = ntohl (mp->retval);
559 mm->first_msg_id_reply = (retval >= 0) ? ntohs (mp->first_msg_id) : ~0;
560 mm->first_msg_id_reply_ready = 1;
564 vl_client_get_first_plugin_msg_id (const char *plugin_name)
566 vl_api_get_first_msg_id_t *mp;
567 api_main_t *am = vlibapi_get_main ();
568 memory_client_main_t *mm = vlibapi_get_memory_client_main ();
569 vl_api_msg_data_t *m =
570 vl_api_get_msg_data (am, VL_API_GET_FIRST_MSG_ID_REPLY);
573 clib_time_t clib_time;
576 if (strlen (plugin_name) + 1 > sizeof (mp->name))
579 clib_memset (&clib_time, 0, sizeof (clib_time));
580 clib_time_init (&clib_time);
582 /* Push this plugin's first_msg_id_reply handler */
583 old_handler = m->handler;
584 m->handler = (void *) vl_api_get_first_msg_id_reply_t_handler;
585 if (!m->calc_size_func)
588 (uword (*) (void *)) vl_api_get_first_msg_id_reply_t_calc_size;
591 /* Ask the data-plane for the message-ID base of the indicated plugin */
592 mm->first_msg_id_reply_ready = 0;
594 /* Not using shm client */
595 if (!am->my_registration)
597 mp = vl_socket_client_msg_alloc (sizeof (*mp));
598 clib_memset (mp, 0, sizeof (*mp));
599 mp->_vl_msg_id = ntohs (VL_API_GET_FIRST_MSG_ID);
600 mp->client_index = am->my_client_index;
601 strncpy ((char *) mp->name, plugin_name, sizeof (mp->name) - 1);
603 if (vl_socket_client_write () <= 0)
605 if (vl_socket_client_read (1))
608 if (mm->first_msg_id_reply_ready == 1)
610 rv = mm->first_msg_id_reply;
615 /* Restore old handler */
616 m->handler = old_handler;
622 mp = vl_msg_api_alloc (sizeof (*mp));
623 clib_memset (mp, 0, sizeof (*mp));
624 mp->_vl_msg_id = ntohs (VL_API_GET_FIRST_MSG_ID);
625 mp->client_index = am->my_client_index;
626 strncpy ((char *) mp->name, plugin_name, sizeof (mp->name) - 1);
628 vl_msg_api_send_shmem (am->shmem_hdr->vl_input_queue, (u8 *) & mp);
630 /* Synchronously wait for the answer */
631 timeout = clib_time_now (&clib_time) + 1.0;
632 while (clib_time_now (&clib_time) < timeout)
634 if (mm->first_msg_id_reply_ready == 1)
636 rv = mm->first_msg_id_reply;
640 /* Restore old handler */
641 m->handler = old_handler;
648 /* Restore the old handler */
649 m->handler = old_handler;
652 clib_warning ("plugin '%s' not registered", plugin_name);
658 * fd.io coding-style-patch-verification: ON
661 * eval: (c-set-style "gnu")