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;
51 typedef struct rx_thread_fn_arg
54 memory_client_main_t *mm;
58 rx_thread_fn (void *arg)
60 rx_thread_fn_arg_t *a = (rx_thread_fn_arg_t *) arg;
61 memory_client_main_t *mm;
64 vlibapi_set_main (a->am);
65 vlibapi_set_memory_client_main (a->mm);
68 mm = vlibapi_get_memory_client_main ();
69 q = vlibapi_get_main ()->vl_input_queue;
71 /* So we can make the rx thread terminate cleanly */
72 if (setjmp (mm->rx_thread_jmpbuf) == 0)
74 mm->rx_thread_jmpbuf_valid = 1;
75 clib_mem_set_thread_index ();
77 vl_msg_api_queue_handler (q);
83 vl_api_rx_thread_exit_t_handler (vl_api_rx_thread_exit_t * mp)
85 memory_client_main_t *mm = vlibapi_get_memory_client_main ();
86 if (mm->rx_thread_jmpbuf_valid)
87 longjmp (mm->rx_thread_jmpbuf, 1);
91 vl_api_name_and_crc_free (void)
93 api_main_t *am = vlibapi_get_main ();
98 if (!am->msg_index_by_name_and_crc)
102 hash_foreach_pair (hp, am->msg_index_by_name_and_crc,
104 vec_add1 (keys, (u8 *) hp->key);
107 for (i = 0; i < vec_len (keys); i++)
110 hash_free (am->msg_index_by_name_and_crc);
113 CLIB_NOSANITIZE_ADDR static void
114 VL_API_VEC_UNPOISON (const void *v)
116 const vec_header_t *vh = &((vec_header_t *) v)[-1];
117 CLIB_MEM_UNPOISON (vh, sizeof (*vh) + vec_len (v));
121 vl_api_memclnt_create_reply_t_handler (vl_api_memclnt_create_reply_t * mp)
123 serialize_main_t _sm, *sm = &_sm;
124 api_main_t *am = vlibapi_get_main ();
131 am->my_client_index = mp->index;
132 am->my_registration = (vl_api_registration_t *) (uword) mp->handle;
134 /* Clean out any previous hash table (unlikely) */
135 vl_api_name_and_crc_free ();
137 am->msg_index_by_name_and_crc = hash_create_string (0, sizeof (uword));
139 /* Recreate the vnet-side API message handler table */
140 tblv = uword_to_pointer (mp->message_table, u8 *);
141 unserialize_open_data (sm, tblv, vec_len (tblv));
142 unserialize_integer (sm, &nmsgs, sizeof (u32));
144 VL_API_VEC_UNPOISON (tblv);
146 for (i = 0; i < nmsgs; i++)
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);
155 noop_handler (void *notused)
159 void vl_msg_api_send_shmem (svm_queue_t * q, u8 * elem);
161 vl_client_connect (const char *name, int ctx_quota, int input_queue_size)
163 vl_api_memclnt_create_t *mp;
164 vl_api_memclnt_create_reply_t *rp;
165 svm_queue_t *vl_input_queue;
166 vl_shmem_hdr_t *shmem_hdr;
169 api_main_t *am = vlibapi_get_main ();
171 if (am->my_registration)
173 clib_warning ("client %s already connected...", name);
177 if (am->vlib_rp == 0)
179 clib_warning ("am->vlib_rp NULL");
183 shmem_hdr = am->shmem_hdr;
185 if (shmem_hdr == 0 || shmem_hdr->vl_input_queue == 0)
187 clib_warning ("shmem_hdr / input queue NULL");
191 CLIB_MEM_UNPOISON (shmem_hdr, sizeof (*shmem_hdr));
192 VL_MSG_API_SVM_QUEUE_UNPOISON (shmem_hdr->vl_input_queue);
194 oldheap = vl_msg_push_heap ();
195 vl_input_queue = svm_queue_alloc_and_init (input_queue_size, sizeof (uword),
197 vl_msg_pop_heap (oldheap);
199 am->my_client_index = ~0;
200 am->my_registration = 0;
201 am->vl_input_queue = vl_input_queue;
203 mp = vl_msg_api_alloc (sizeof (vl_api_memclnt_create_t));
204 clib_memset (mp, 0, sizeof (*mp));
205 mp->_vl_msg_id = ntohs (VL_API_MEMCLNT_CREATE);
206 mp->ctx_quota = ctx_quota;
207 mp->input_queue = (uword) vl_input_queue;
208 strncpy ((char *) mp->name, name, sizeof (mp->name) - 1);
210 vl_msg_api_send_shmem (shmem_hdr->vl_input_queue, (u8 *) & mp);
215 struct timespec ts, tsrem;
218 /* Wait up to 10 seconds */
219 for (i = 0; i < 1000; i++)
221 qstatus = svm_queue_sub (vl_input_queue, (u8 *) & rp,
226 ts.tv_nsec = 10000 * 1000; /* 10 ms */
227 while (nanosleep (&ts, &tsrem) < 0)
231 clib_warning ("memclnt_create_reply timeout");
235 VL_MSG_API_UNPOISON (rp);
236 if (ntohs (rp->_vl_msg_id) != VL_API_MEMCLNT_CREATE_REPLY)
238 clib_warning ("unexpected reply: id %d", ntohs (rp->_vl_msg_id));
241 rv = clib_net_to_host_u32 (rp->response);
243 vl_msg_api_handler ((void *) rp);
250 vl_api_memclnt_delete_reply_t_handler (vl_api_memclnt_delete_reply_t * mp)
253 api_main_t *am = vlibapi_get_main ();
255 oldheap = vl_msg_push_heap ();
256 svm_queue_free (am->vl_input_queue);
257 vl_msg_pop_heap (oldheap);
259 am->my_client_index = ~0;
260 am->my_registration = 0;
261 am->vl_input_queue = 0;
265 vl_client_send_disconnect (u8 do_cleanup)
267 vl_api_memclnt_delete_t *mp;
268 vl_shmem_hdr_t *shmem_hdr;
269 api_main_t *am = vlibapi_get_main ();
271 ASSERT (am->vlib_rp);
272 shmem_hdr = am->shmem_hdr;
273 ASSERT (shmem_hdr && shmem_hdr->vl_input_queue);
275 mp = vl_msg_api_alloc (sizeof (vl_api_memclnt_delete_t));
276 clib_memset (mp, 0, sizeof (*mp));
277 mp->_vl_msg_id = ntohs (VL_API_MEMCLNT_DELETE);
278 mp->index = am->my_client_index;
279 mp->handle = (uword) am->my_registration;
280 mp->do_cleanup = do_cleanup;
282 vl_msg_api_send_shmem (shmem_hdr->vl_input_queue, (u8 *) & mp);
286 vl_client_disconnect (void)
288 vl_api_memclnt_delete_reply_t *rp;
289 svm_queue_t *vl_input_queue;
290 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 vl_msg_api_handler ((void *) rp);
327 vl_msg_api_handler ((void *) rp);
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)
367 vl_msg_api_set_handlers(VL_API_##N, #n, \
368 vl_api_##n##_t_handler, \
370 vl_api_##n##_t_endian, \
371 vl_api_##n##_t_print, \
372 sizeof(vl_api_##n##_t), 1);
378 vl_client_api_map (const char *region_name)
382 if ((rv = vl_map_shmem (region_name, 0 /* is_vlib */ )) < 0)
385 vl_client_install_client_message_handlers ();
390 vl_client_api_unmap (void)
392 vl_unmap_shmem_client ();
396 vl_mem_client_is_connected (void)
398 return (my_memory_client_main->connected_to_vlib != 0);
402 connect_to_vlib_internal (const char *svm_name,
403 const char *client_name,
404 int rx_queue_size, void *(*thread_fn) (void *),
405 void *thread_fn_arg, int do_map)
408 memory_client_main_t *mm = vlibapi_get_memory_client_main ();
409 api_main_t *am = vlibapi_get_main ();
411 if (do_map && (rv = vl_client_api_map (svm_name)))
413 clib_warning ("vl_client_api map rv %d", rv);
417 if (vl_client_connect (client_name, 0 /* punt quota */ ,
418 rx_queue_size /* input queue */ ) < 0)
420 vl_client_api_unmap ();
424 /* Start the rx queue thread */
428 if (thread_fn == rx_thread_fn)
430 rx_thread_fn_arg_t *arg;
431 arg = clib_mem_alloc (sizeof (*arg));
432 arg->am = vlibapi_get_main ();
433 arg->mm = vlibapi_get_memory_client_main ();
434 thread_fn_arg = (void *) arg;
437 rv = pthread_create (&mm->rx_thread_handle,
438 NULL /*attr */ , thread_fn, thread_fn_arg);
441 clib_warning ("pthread_create returned %d", rv);
442 am->rx_thread_handle = 0;
446 am->rx_thread_handle = mm->rx_thread_handle;
450 mm->connected_to_vlib = 1;
455 vl_client_connect_to_vlib (const char *svm_name,
456 const char *client_name, int rx_queue_size)
458 return connect_to_vlib_internal (svm_name, client_name, rx_queue_size,
459 rx_thread_fn, 0 /* thread fn arg */ ,
464 vl_client_connect_to_vlib_no_rx_pthread (const char *svm_name,
465 const char *client_name,
468 return connect_to_vlib_internal (svm_name, client_name, rx_queue_size,
469 0 /* no rx_thread_fn */ ,
470 0 /* no thread fn arg */ ,
475 vl_client_connect_to_vlib_no_map (const char *svm_name,
476 const char *client_name, int rx_queue_size)
478 return connect_to_vlib_internal (svm_name, client_name, rx_queue_size,
479 rx_thread_fn, 0 /* no thread fn arg */ ,
484 vl_client_connect_to_vlib_no_rx_pthread_no_map (const char *svm_name,
485 const char *client_name,
488 return connect_to_vlib_internal (svm_name, client_name, rx_queue_size,
489 0 /* no thread_fn */ ,
490 0 /* no thread fn arg */ ,
495 vl_client_connect_to_vlib_thread_fn (const char *svm_name,
496 const char *client_name,
498 void *(*thread_fn) (void *), void *arg)
500 return connect_to_vlib_internal (svm_name, client_name, rx_queue_size,
501 thread_fn, arg, 1 /* do map */ );
506 disconnect_from_vlib_internal (u8 do_unmap)
508 memory_client_main_t *mm = vlibapi_get_memory_client_main ();
509 api_main_t *am = vlibapi_get_main ();
512 if (mm->rx_thread_jmpbuf_valid)
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);
520 if (mm->connected_to_vlib)
522 vl_client_disconnect ();
524 vl_client_api_unmap ();
526 clib_memset (mm, 0, sizeof (*mm));
530 vl_client_disconnect_from_vlib (void)
532 disconnect_from_vlib_internal (1);
536 vl_client_disconnect_from_vlib_no_unmap (void)
538 disconnect_from_vlib_internal (0);
541 static void vl_api_get_first_msg_id_reply_t_handler
542 (vl_api_get_first_msg_id_reply_t * mp)
544 memory_client_main_t *mm = vlibapi_get_memory_client_main ();
545 i32 retval = ntohl (mp->retval);
547 mm->first_msg_id_reply = (retval >= 0) ? ntohs (mp->first_msg_id) : ~0;
548 mm->first_msg_id_reply_ready = 1;
552 vl_client_get_first_plugin_msg_id (const char *plugin_name)
554 vl_api_get_first_msg_id_t *mp;
555 api_main_t *am = vlibapi_get_main ();
556 memory_client_main_t *mm = vlibapi_get_memory_client_main ();
559 clib_time_t clib_time;
562 if (strlen (plugin_name) + 1 > sizeof (mp->name))
565 clib_memset (&clib_time, 0, sizeof (clib_time));
566 clib_time_init (&clib_time);
568 /* 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;
573 /* Ask the data-plane for the message-ID base of the indicated plugin */
574 mm->first_msg_id_reply_ready = 0;
576 /* Not using shm client */
577 if (!am->my_registration)
579 mp = vl_socket_client_msg_alloc (sizeof (*mp));
580 clib_memset (mp, 0, sizeof (*mp));
581 mp->_vl_msg_id = ntohs (VL_API_GET_FIRST_MSG_ID);
582 mp->client_index = am->my_client_index;
583 strncpy ((char *) mp->name, plugin_name, sizeof (mp->name) - 1);
585 if (vl_socket_client_write () <= 0)
587 if (vl_socket_client_read (1))
590 if (mm->first_msg_id_reply_ready == 1)
592 rv = mm->first_msg_id_reply;
597 /* Restore old handler */
598 am->msg_handlers[VL_API_GET_FIRST_MSG_ID_REPLY] = old_handler;
604 mp = vl_msg_api_alloc (sizeof (*mp));
605 clib_memset (mp, 0, sizeof (*mp));
606 mp->_vl_msg_id = ntohs (VL_API_GET_FIRST_MSG_ID);
607 mp->client_index = am->my_client_index;
608 strncpy ((char *) mp->name, plugin_name, sizeof (mp->name) - 1);
610 vl_msg_api_send_shmem (am->shmem_hdr->vl_input_queue, (u8 *) & mp);
612 /* Synchronously wait for the answer */
613 timeout = clib_time_now (&clib_time) + 1.0;
614 while (clib_time_now (&clib_time) < timeout)
616 if (mm->first_msg_id_reply_ready == 1)
618 rv = mm->first_msg_id_reply;
622 /* Restore old handler */
623 am->msg_handlers[VL_API_GET_FIRST_MSG_ID_REPLY] = old_handler;
630 /* Restore the old handler */
631 am->msg_handlers[VL_API_GET_FIRST_MSG_ID_REPLY] = old_handler;
634 clib_warning ("plugin '%s' not registered", plugin_name);
640 * fd.io coding-style-patch-verification: ON
643 * eval: (c-set-style "gnu")