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 /* instantiate all the print functions we know about */
43 #define vl_print(handle, ...) clib_warning (__VA_ARGS__)
45 #include <vlibmemory/vl_memory_api_h.h>
48 memory_client_main_t memory_client_main;
49 __thread memory_client_main_t *my_memory_client_main = &memory_client_main;
52 rx_thread_fn (void *arg)
55 memory_client_main_t *mm = vlibapi_get_memory_client_main ();
57 q = vlibapi_get_main ()->vl_input_queue;
59 /* So we can make the rx thread terminate cleanly */
60 if (setjmp (mm->rx_thread_jmpbuf) == 0)
62 mm->rx_thread_jmpbuf_valid = 1;
63 clib_mem_set_thread_index ();
65 vl_msg_api_queue_handler (q);
71 vl_api_rx_thread_exit_t_handler (vl_api_rx_thread_exit_t * mp)
73 memory_client_main_t *mm = vlibapi_get_memory_client_main ();
74 if (mm->rx_thread_jmpbuf_valid)
75 longjmp (mm->rx_thread_jmpbuf, 1);
79 vl_api_name_and_crc_free (void)
81 api_main_t *am = vlibapi_get_main ();
86 if (!am->msg_index_by_name_and_crc)
90 hash_foreach_pair (hp, am->msg_index_by_name_and_crc,
92 vec_add1 (keys, (u8 *) hp->key);
95 for (i = 0; i < vec_len (keys); i++)
98 hash_free (am->msg_index_by_name_and_crc);
101 CLIB_NOSANITIZE_ADDR static void
102 VL_API_VEC_UNPOISON (const void *v)
104 const vec_header_t *vh = &((vec_header_t *) v)[-1];
105 CLIB_MEM_UNPOISON (vh, sizeof (*vh) + vec_len (v));
109 vl_api_memclnt_create_reply_t_handler (vl_api_memclnt_create_reply_t * mp)
111 serialize_main_t _sm, *sm = &_sm;
112 api_main_t *am = vlibapi_get_main ();
119 am->my_client_index = mp->index;
120 am->my_registration = (vl_api_registration_t *) (uword) mp->handle;
122 /* Clean out any previous hash table (unlikely) */
123 vl_api_name_and_crc_free ();
125 am->msg_index_by_name_and_crc = hash_create_string (0, sizeof (uword));
127 /* Recreate the vnet-side API message handler table */
128 tblv = uword_to_pointer (mp->message_table, u8 *);
129 unserialize_open_data (sm, tblv, vec_len (tblv));
130 unserialize_integer (sm, &nmsgs, sizeof (u32));
132 VL_API_VEC_UNPOISON (tblv);
134 for (i = 0; i < nmsgs; i++)
136 msg_index = unserialize_likely_small_unsigned_integer (sm);
137 unserialize_cstring (sm, (char **) &name_and_crc);
138 hash_set_mem (am->msg_index_by_name_and_crc, name_and_crc, msg_index);
143 noop_handler (void *notused)
147 void vl_msg_api_send_shmem (svm_queue_t * q, u8 * elem);
149 vl_client_connect (const char *name, int ctx_quota, int input_queue_size)
152 vl_api_memclnt_create_t *mp;
153 vl_api_memclnt_create_reply_t *rp;
154 svm_queue_t *vl_input_queue;
155 vl_shmem_hdr_t *shmem_hdr;
158 api_main_t *am = vlibapi_get_main ();
160 if (am->my_registration)
162 clib_warning ("client %s already connected...", name);
166 if (am->vlib_rp == 0)
168 clib_warning ("am->vlib_rp NULL");
173 shmem_hdr = am->shmem_hdr;
175 if (shmem_hdr == 0 || shmem_hdr->vl_input_queue == 0)
177 clib_warning ("shmem_hdr / input queue NULL");
181 CLIB_MEM_UNPOISON (shmem_hdr, sizeof (*shmem_hdr));
182 VL_MSG_API_SVM_QUEUE_UNPOISON (shmem_hdr->vl_input_queue);
184 pthread_mutex_lock (&svm->mutex);
185 oldheap = svm_push_data_heap (svm);
186 vl_input_queue = svm_queue_alloc_and_init (input_queue_size, sizeof (uword),
188 svm_pop_heap (oldheap);
189 pthread_mutex_unlock (&svm->mutex);
191 am->my_client_index = ~0;
192 am->my_registration = 0;
193 am->vl_input_queue = vl_input_queue;
195 mp = vl_msg_api_alloc (sizeof (vl_api_memclnt_create_t));
196 clib_memset (mp, 0, sizeof (*mp));
197 mp->_vl_msg_id = ntohs (VL_API_MEMCLNT_CREATE);
198 mp->ctx_quota = ctx_quota;
199 mp->input_queue = (uword) vl_input_queue;
200 strncpy ((char *) mp->name, name, sizeof (mp->name) - 1);
202 vl_msg_api_send_shmem (shmem_hdr->vl_input_queue, (u8 *) & mp);
207 struct timespec ts, tsrem;
210 /* Wait up to 10 seconds */
211 for (i = 0; i < 1000; i++)
213 qstatus = svm_queue_sub (vl_input_queue, (u8 *) & rp,
218 ts.tv_nsec = 10000 * 1000; /* 10 ms */
219 while (nanosleep (&ts, &tsrem) < 0)
223 clib_warning ("memclnt_create_reply timeout");
227 VL_MSG_API_UNPOISON (rp);
228 if (ntohs (rp->_vl_msg_id) != VL_API_MEMCLNT_CREATE_REPLY)
230 clib_warning ("unexpected reply: id %d", ntohs (rp->_vl_msg_id));
233 rv = clib_net_to_host_u32 (rp->response);
235 vl_msg_api_handler ((void *) rp);
242 vl_api_memclnt_delete_reply_t_handler (vl_api_memclnt_delete_reply_t * mp)
245 api_main_t *am = vlibapi_get_main ();
247 pthread_mutex_lock (&am->vlib_rp->mutex);
248 oldheap = svm_push_data_heap (am->vlib_rp);
249 svm_queue_free (am->vl_input_queue);
250 pthread_mutex_unlock (&am->vlib_rp->mutex);
251 svm_pop_heap (oldheap);
253 am->my_client_index = ~0;
254 am->my_registration = 0;
255 am->vl_input_queue = 0;
259 vl_client_send_disconnect (u8 do_cleanup)
261 vl_api_memclnt_delete_t *mp;
262 vl_shmem_hdr_t *shmem_hdr;
263 api_main_t *am = vlibapi_get_main ();
265 ASSERT (am->vlib_rp);
266 shmem_hdr = am->shmem_hdr;
267 ASSERT (shmem_hdr && shmem_hdr->vl_input_queue);
269 mp = vl_msg_api_alloc (sizeof (vl_api_memclnt_delete_t));
270 clib_memset (mp, 0, sizeof (*mp));
271 mp->_vl_msg_id = ntohs (VL_API_MEMCLNT_DELETE);
272 mp->index = am->my_client_index;
273 mp->handle = (uword) am->my_registration;
274 mp->do_cleanup = do_cleanup;
276 vl_msg_api_send_shmem (shmem_hdr->vl_input_queue, (u8 *) & mp);
280 vl_client_disconnect (void)
282 vl_api_memclnt_delete_reply_t *rp;
283 svm_queue_t *vl_input_queue;
284 api_main_t *am = vlibapi_get_main ();
287 vl_input_queue = am->vl_input_queue;
288 vl_client_send_disconnect (0 /* wait for reply */ );
291 * Have to be careful here, in case the client is disconnecting
292 * because e.g. the vlib process died, or is unresponsive.
301 if (now >= (begin + 2))
303 clib_warning ("peer unresponsive, give up");
304 am->my_client_index = ~0;
305 am->my_registration = 0;
309 if (svm_queue_sub (vl_input_queue, (u8 *) & rp, SVM_Q_NOWAIT, 0) < 0)
312 VL_MSG_API_UNPOISON (rp);
314 /* drain the queue */
315 if (ntohs (rp->_vl_msg_id) != VL_API_MEMCLNT_DELETE_REPLY)
317 clib_warning ("queue drain: %d", ntohs (rp->_vl_msg_id));
318 vl_msg_api_handler ((void *) rp);
321 vl_msg_api_handler ((void *) rp);
325 vl_api_name_and_crc_free ();
330 * Stave off the binary API dead client reaper
331 * Only sent to inactive clients
334 vl_api_memclnt_keepalive_t_handler (vl_api_memclnt_keepalive_t * mp)
336 vl_api_memclnt_keepalive_reply_t *rmp;
338 vl_shmem_hdr_t *shmem_hdr;
340 am = vlibapi_get_main ();
341 shmem_hdr = am->shmem_hdr;
343 rmp = vl_msg_api_alloc_as_if_client (sizeof (*rmp));
344 clib_memset (rmp, 0, sizeof (*rmp));
345 rmp->_vl_msg_id = ntohs (VL_API_MEMCLNT_KEEPALIVE_REPLY);
346 rmp->context = mp->context;
347 vl_msg_api_send_shmem (shmem_hdr->vl_input_queue, (u8 *) & rmp);
350 #define foreach_api_msg \
351 _(RX_THREAD_EXIT, rx_thread_exit) \
352 _(MEMCLNT_CREATE_REPLY, memclnt_create_reply) \
353 _(MEMCLNT_DELETE_REPLY, memclnt_delete_reply) \
354 _(MEMCLNT_KEEPALIVE, memclnt_keepalive)
357 vl_client_install_client_message_handlers (void)
361 vl_msg_api_set_handlers(VL_API_##N, #n, \
362 vl_api_##n##_t_handler, \
364 vl_api_##n##_t_endian, \
365 vl_api_##n##_t_print, \
366 sizeof(vl_api_##n##_t), 1);
372 vl_client_api_map (const char *region_name)
376 if ((rv = vl_map_shmem (region_name, 0 /* is_vlib */ )) < 0)
379 vl_client_install_client_message_handlers ();
384 vl_client_api_unmap (void)
386 vl_unmap_shmem_client ();
390 vl_mem_client_is_connected (void)
392 return (memory_client_main.connected_to_vlib != 0);
396 connect_to_vlib_internal (const char *svm_name,
397 const char *client_name,
398 int rx_queue_size, void *(*thread_fn) (void *),
399 void *thread_fn_arg, int do_map)
402 memory_client_main_t *mm = vlibapi_get_memory_client_main ();
403 api_main_t *am = vlibapi_get_main ();
405 if (do_map && (rv = vl_client_api_map (svm_name)))
407 clib_warning ("vl_client_api map rv %d", rv);
411 if (vl_client_connect (client_name, 0 /* punt quota */ ,
412 rx_queue_size /* input queue */ ) < 0)
414 vl_client_api_unmap ();
418 /* Start the rx queue thread */
422 rv = pthread_create (&mm->rx_thread_handle,
423 NULL /*attr */ , thread_fn, thread_fn_arg);
426 clib_warning ("pthread_create returned %d", rv);
427 am->rx_thread_handle = 0;
431 am->rx_thread_handle = mm->rx_thread_handle;
435 mm->connected_to_vlib = 1;
440 vl_client_connect_to_vlib (const char *svm_name,
441 const char *client_name, int rx_queue_size)
443 return connect_to_vlib_internal (svm_name, client_name, rx_queue_size,
444 rx_thread_fn, 0 /* thread fn arg */ ,
449 vl_client_connect_to_vlib_no_rx_pthread (const char *svm_name,
450 const char *client_name,
453 return connect_to_vlib_internal (svm_name, client_name, rx_queue_size,
454 0 /* no rx_thread_fn */ ,
455 0 /* no thread fn arg */ ,
460 vl_client_connect_to_vlib_no_map (const char *svm_name,
461 const char *client_name, int rx_queue_size)
463 return connect_to_vlib_internal (svm_name, client_name, rx_queue_size,
464 rx_thread_fn, 0 /* no thread fn arg */ ,
469 vl_client_connect_to_vlib_no_rx_pthread_no_map (const char *svm_name,
470 const char *client_name,
473 return connect_to_vlib_internal (svm_name, client_name, rx_queue_size,
474 0 /* no thread_fn */ ,
475 0 /* no thread fn arg */ ,
480 vl_client_connect_to_vlib_thread_fn (const char *svm_name,
481 const char *client_name,
483 void *(*thread_fn) (void *), void *arg)
485 return connect_to_vlib_internal (svm_name, client_name, rx_queue_size,
486 thread_fn, arg, 1 /* do map */ );
491 disconnect_from_vlib_internal (u8 do_unmap)
493 memory_client_main_t *mm = vlibapi_get_memory_client_main ();
494 api_main_t *am = vlibapi_get_main ();
497 if (mm->rx_thread_jmpbuf_valid)
499 vl_api_rx_thread_exit_t *ep;
500 ep = vl_msg_api_alloc (sizeof (*ep));
501 ep->_vl_msg_id = ntohs (VL_API_RX_THREAD_EXIT);
502 vl_msg_api_send_shmem (am->vl_input_queue, (u8 *) & ep);
503 pthread_join (mm->rx_thread_handle, (void **) &junk);
505 if (mm->connected_to_vlib)
507 vl_client_disconnect ();
509 vl_client_api_unmap ();
511 clib_memset (mm, 0, sizeof (*mm));
515 vl_client_disconnect_from_vlib (void)
517 disconnect_from_vlib_internal (1);
521 vl_client_disconnect_from_vlib_no_unmap (void)
523 disconnect_from_vlib_internal (0);
526 static void vl_api_get_first_msg_id_reply_t_handler
527 (vl_api_get_first_msg_id_reply_t * mp)
529 memory_client_main_t *mm = vlibapi_get_memory_client_main ();
530 i32 retval = ntohl (mp->retval);
532 mm->first_msg_id_reply = (retval >= 0) ? ntohs (mp->first_msg_id) : ~0;
533 mm->first_msg_id_reply_ready = 1;
537 vl_client_get_first_plugin_msg_id (const char *plugin_name)
539 vl_api_get_first_msg_id_t *mp;
540 api_main_t *am = vlibapi_get_main ();
541 memory_client_main_t *mm = vlibapi_get_memory_client_main ();
544 clib_time_t clib_time;
547 if (strlen (plugin_name) + 1 > sizeof (mp->name))
550 clib_memset (&clib_time, 0, sizeof (clib_time));
551 clib_time_init (&clib_time);
553 /* Push this plugin's first_msg_id_reply handler */
554 old_handler = am->msg_handlers[VL_API_GET_FIRST_MSG_ID_REPLY];
555 am->msg_handlers[VL_API_GET_FIRST_MSG_ID_REPLY] = (void *)
556 vl_api_get_first_msg_id_reply_t_handler;
558 /* Ask the data-plane for the message-ID base of the indicated plugin */
559 mm->first_msg_id_reply_ready = 0;
561 /* Not using shm client */
562 if (!am->my_registration)
564 mp = vl_socket_client_msg_alloc (sizeof (*mp));
565 clib_memset (mp, 0, sizeof (*mp));
566 mp->_vl_msg_id = ntohs (VL_API_GET_FIRST_MSG_ID);
567 mp->client_index = am->my_client_index;
568 strncpy ((char *) mp->name, plugin_name, sizeof (mp->name) - 1);
570 if (vl_socket_client_write () <= 0)
572 if (vl_socket_client_read (1))
575 if (mm->first_msg_id_reply_ready == 1)
577 rv = mm->first_msg_id_reply;
582 /* Restore old handler */
583 am->msg_handlers[VL_API_GET_FIRST_MSG_ID_REPLY] = old_handler;
589 mp = vl_msg_api_alloc (sizeof (*mp));
590 clib_memset (mp, 0, sizeof (*mp));
591 mp->_vl_msg_id = ntohs (VL_API_GET_FIRST_MSG_ID);
592 mp->client_index = am->my_client_index;
593 strncpy ((char *) mp->name, plugin_name, sizeof (mp->name) - 1);
595 vl_msg_api_send_shmem (am->shmem_hdr->vl_input_queue, (u8 *) & mp);
597 /* Synchronously wait for the answer */
598 timeout = clib_time_now (&clib_time) + 1.0;
599 while (clib_time_now (&clib_time) < timeout)
601 if (mm->first_msg_id_reply_ready == 1)
603 rv = mm->first_msg_id_reply;
607 /* Restore old handler */
608 am->msg_handlers[VL_API_GET_FIRST_MSG_ID_REPLY] = old_handler;
615 /* Restore the old handler */
616 am->msg_handlers[VL_API_GET_FIRST_MSG_ID_REPLY] = old_handler;
619 clib_warning ("plugin '%s' not registered", plugin_name);
625 * fd.io coding-style-patch-verification: ON
628 * eval: (c-set-style "gnu")