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 *------------------------------------------------------------------
23 #include <sys/types.h>
26 #include <netinet/in.h>
33 #include <vppinfra/clib.h>
34 #include <vppinfra/vec.h>
35 #include <vppinfra/hash.h>
36 #include <vppinfra/bitmap.h>
37 #include <vppinfra/fifo.h>
38 #include <vppinfra/time.h>
39 #include <vppinfra/mheap.h>
40 #include <vppinfra/heap.h>
41 #include <vppinfra/pool.h>
42 #include <vppinfra/format.h>
44 #include <vlib/vlib.h>
45 #include <vlib/unix/unix.h>
46 #include <vlibmemory/api.h>
48 #include <vlibmemory/vl_memory_msg_enum.h>
50 #define vl_typedefs /* define message structures */
51 #include <vlibmemory/vl_memory_api_h.h>
54 #define vl_endianfun /* define message structures */
55 #include <vlibmemory/vl_memory_api_h.h>
58 /* instantiate all the print functions we know about */
59 #define vl_print(handle, ...) clib_warning (__VA_ARGS__)
61 #include <vlibmemory/vl_memory_api_h.h>
66 u8 rx_thread_jmpbuf_valid;
68 jmp_buf rx_thread_jmpbuf;
69 pthread_t rx_thread_handle;
70 /* Plugin message base lookup scheme */
71 volatile u8 first_msg_id_reply_ready;
72 u16 first_msg_id_reply;
73 } memory_client_main_t;
75 memory_client_main_t memory_client_main;
78 rx_thread_fn (void *arg)
80 unix_shared_memory_queue_t *q;
81 memory_client_main_t *mm = &memory_client_main;
82 api_main_t *am = &api_main;
84 q = am->vl_input_queue;
86 /* So we can make the rx thread terminate cleanly */
87 if (setjmp (mm->rx_thread_jmpbuf) == 0)
89 mm->rx_thread_jmpbuf_valid = 1;
92 vl_msg_api_queue_handler (q);
99 vl_api_rx_thread_exit_t_handler (vl_api_rx_thread_exit_t * mp)
101 memory_client_main_t *mm = &memory_client_main;
102 vl_msg_api_free (mp);
103 longjmp (mm->rx_thread_jmpbuf, 1);
107 vl_api_memclnt_create_reply_t_handler (vl_api_memclnt_create_reply_t * mp)
109 serialize_main_t _sm, *sm = &_sm;
110 api_main_t *am = &api_main;
117 am->my_client_index = mp->index;
118 am->my_registration = (vl_api_registration_t *) (uword) mp->handle;
120 /* Clean out any previous hash table (unlikely) */
121 if (am->msg_index_by_name_and_crc)
127 hash_foreach_pair (hp, am->msg_index_by_name_and_crc,
129 vec_add1 (keys, (u8 *) hp->key);
132 for (i = 0; i < vec_len (keys); i++)
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 serialize_open_vector (sm, tblv);
142 unserialize_integer (sm, &nmsgs, sizeof (u32));
144 for (i = 0; i < nmsgs; i++)
146 msg_index = unserialize_likely_small_unsigned_integer (sm);
147 unserialize_cstring (sm, (char **) &name_and_crc);
148 hash_set_mem (am->msg_index_by_name_and_crc, name_and_crc, msg_index);
153 noop_handler (void *notused)
158 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 unix_shared_memory_queue_t *vl_input_queue;
164 vl_shmem_hdr_t *shmem_hdr;
167 api_main_t *am = &api_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");
182 shmem_hdr = am->shmem_hdr;
184 if (shmem_hdr == 0 || shmem_hdr->vl_input_queue == 0)
186 clib_warning ("shmem_hdr / input queue NULL");
190 pthread_mutex_lock (&svm->mutex);
191 oldheap = svm_push_data_heap (svm);
193 unix_shared_memory_queue_init (input_queue_size, sizeof (uword),
195 pthread_mutex_unlock (&svm->mutex);
196 svm_pop_heap (oldheap);
198 am->my_client_index = ~0;
199 am->my_registration = 0;
200 am->vl_input_queue = vl_input_queue;
202 mp = vl_msg_api_alloc (sizeof (vl_api_memclnt_create_t));
203 memset (mp, 0, sizeof (*mp));
204 mp->_vl_msg_id = ntohs (VL_API_MEMCLNT_CREATE);
205 mp->ctx_quota = ctx_quota;
206 mp->input_queue = (uword) vl_input_queue;
207 strncpy ((char *) mp->name, name, sizeof (mp->name) - 1);
209 vl_msg_api_send_shmem (shmem_hdr->vl_input_queue, (u8 *) & mp);
214 struct timespec ts, tsrem;
217 /* Wait up to 10 seconds */
218 for (i = 0; i < 1000; i++)
220 qstatus = unix_shared_memory_queue_sub (vl_input_queue, (u8 *) & rp,
225 ts.tv_nsec = 10000 * 1000; /* 10 ms */
226 while (nanosleep (&ts, &tsrem) < 0)
230 clib_warning ("memclnt_create_reply timeout");
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 vl_msg_api_handler ((void *) rp);
248 vl_api_memclnt_delete_reply_t_handler (vl_api_memclnt_delete_reply_t * mp)
251 api_main_t *am = &api_main;
253 pthread_mutex_lock (&am->vlib_rp->mutex);
254 oldheap = svm_push_data_heap (am->vlib_rp);
255 unix_shared_memory_queue_free (am->vl_input_queue);
256 pthread_mutex_unlock (&am->vlib_rp->mutex);
257 svm_pop_heap (oldheap);
259 am->my_client_index = ~0;
260 am->my_registration = 0;
261 am->vl_input_queue = 0;
265 vl_client_disconnect (void)
267 vl_api_memclnt_delete_t *mp;
268 vl_api_memclnt_delete_reply_t *rp;
269 unix_shared_memory_queue_t *vl_input_queue;
270 vl_shmem_hdr_t *shmem_hdr;
272 api_main_t *am = &api_main;
274 ASSERT (am->vlib_rp);
275 shmem_hdr = am->shmem_hdr;
276 ASSERT (shmem_hdr && shmem_hdr->vl_input_queue);
278 vl_input_queue = am->vl_input_queue;
280 mp = vl_msg_api_alloc (sizeof (vl_api_memclnt_delete_t));
281 memset (mp, 0, sizeof (*mp));
282 mp->_vl_msg_id = ntohs (VL_API_MEMCLNT_DELETE);
283 mp->index = am->my_client_index;
284 mp->handle = (uword) am->my_registration;
286 vl_msg_api_send_shmem (shmem_hdr->vl_input_queue, (u8 *) & mp);
289 * Have to be careful here, in case the client is disconnecting
290 * because e.g. the vlib process died, or is unresponsive.
300 if (now >= (begin + 2))
302 clib_warning ("peer unresponsive, give up");
303 am->my_client_index = ~0;
304 am->my_registration = 0;
308 if (unix_shared_memory_queue_sub (vl_input_queue, (u8 *) & rp, 1) < 0)
311 /* drain the queue */
312 if (ntohs (rp->_vl_msg_id) != VL_API_MEMCLNT_DELETE_REPLY)
314 vl_msg_api_handler ((void *) rp);
317 vl_msg_api_handler ((void *) rp);
323 * Stave off the binary API dead client reaper
324 * Only sent to inactive clients
327 vl_api_memclnt_keepalive_t_handler (vl_api_memclnt_keepalive_t * mp)
329 vl_api_memclnt_keepalive_reply_t *rmp;
331 vl_shmem_hdr_t *shmem_hdr;
334 shmem_hdr = am->shmem_hdr;
336 rmp = vl_msg_api_alloc_as_if_client (sizeof (*rmp));
337 memset (rmp, 0, sizeof (*rmp));
338 rmp->_vl_msg_id = ntohs (VL_API_MEMCLNT_KEEPALIVE_REPLY);
339 rmp->context = mp->context;
340 vl_msg_api_send_shmem (shmem_hdr->vl_input_queue, (u8 *) & rmp);
343 #define foreach_api_msg \
344 _(RX_THREAD_EXIT, rx_thread_exit) \
345 _(MEMCLNT_CREATE_REPLY, memclnt_create_reply) \
346 _(MEMCLNT_DELETE_REPLY, memclnt_delete_reply) \
347 _(MEMCLNT_KEEPALIVE, memclnt_keepalive)
351 vl_client_install_client_message_handlers (void)
355 vl_msg_api_set_handlers(VL_API_##N, #n, \
356 vl_api_##n##_t_handler, \
358 vl_api_##n##_t_endian, \
359 vl_api_##n##_t_print, \
360 sizeof(vl_api_##n##_t), 1);
367 vl_client_api_map (const char *region_name)
371 if ((rv = vl_map_shmem (region_name, 0 /* is_vlib */ )) < 0)
374 vl_client_install_client_message_handlers ();
379 vl_client_api_unmap (void)
385 connect_to_vlib_internal (const char *svm_name,
386 const char *client_name,
387 int rx_queue_size, int want_pthread, int do_map)
390 memory_client_main_t *mm = &memory_client_main;
392 if (do_map && (rv = vl_client_api_map (svm_name)))
394 clib_warning ("vl_client_api map rv %d", rv);
398 if (vl_client_connect (client_name, 0 /* punt quota */ ,
399 rx_queue_size /* input queue */ ) < 0)
401 vl_client_api_unmap ();
405 /* Start the rx queue thread */
409 rv = pthread_create (&mm->rx_thread_handle,
410 NULL /*attr */ , rx_thread_fn, 0);
412 clib_warning ("pthread_create returned %d", rv);
415 mm->connected_to_vlib = 1;
420 vl_client_connect_to_vlib (const char *svm_name,
421 const char *client_name, int rx_queue_size)
423 return connect_to_vlib_internal (svm_name, client_name, rx_queue_size,
424 1 /* want pthread */ ,
429 vl_client_connect_to_vlib_no_rx_pthread (const char *svm_name,
430 const char *client_name,
433 return connect_to_vlib_internal (svm_name, client_name, rx_queue_size,
434 0 /* want pthread */ ,
439 vl_client_connect_to_vlib_no_map (const char *svm_name,
440 const char *client_name, int rx_queue_size)
442 return connect_to_vlib_internal (svm_name, client_name, rx_queue_size,
443 1 /* want pthread */ ,
448 vl_client_disconnect_from_vlib (void)
450 memory_client_main_t *mm = &memory_client_main;
451 api_main_t *am = &api_main;
454 if (mm->rx_thread_jmpbuf_valid)
456 vl_api_rx_thread_exit_t *ep;
457 ep = vl_msg_api_alloc (sizeof (*ep));
458 ep->_vl_msg_id = ntohs (VL_API_RX_THREAD_EXIT);
459 vl_msg_api_send_shmem (am->vl_input_queue, (u8 *) & ep);
460 pthread_join (mm->rx_thread_handle, (void **) &junk);
462 if (mm->connected_to_vlib)
464 vl_client_disconnect ();
465 vl_client_api_unmap ();
467 memset (mm, 0, sizeof (*mm));
470 static void vl_api_get_first_msg_id_reply_t_handler
471 (vl_api_get_first_msg_id_reply_t * mp)
473 memory_client_main_t *mm = &memory_client_main;
474 i32 retval = ntohl (mp->retval);
476 mm->first_msg_id_reply = (retval >= 0) ? ntohs (mp->first_msg_id) : ~0;
477 mm->first_msg_id_reply_ready = 1;
481 vl_client_get_first_plugin_msg_id (const char *plugin_name)
483 vl_api_get_first_msg_id_t *mp;
484 api_main_t *am = &api_main;
485 memory_client_main_t *mm = &memory_client_main;
488 clib_time_t clib_time;
491 if (strlen (plugin_name) + 1 > sizeof (mp->name))
494 memset (&clib_time, 0, sizeof (clib_time));
495 clib_time_init (&clib_time);
497 /* Push this plugin's first_msg_id_reply handler */
498 old_handler = am->msg_handlers[VL_API_GET_FIRST_MSG_ID_REPLY];
499 am->msg_handlers[VL_API_GET_FIRST_MSG_ID_REPLY] = (void *)
500 vl_api_get_first_msg_id_reply_t_handler;
502 /* Ask the data-plane for the message-ID base of the indicated plugin */
503 mm->first_msg_id_reply_ready = 0;
505 mp = vl_msg_api_alloc (sizeof (*mp));
506 memset (mp, 0, sizeof (*mp));
507 mp->_vl_msg_id = ntohs (VL_API_GET_FIRST_MSG_ID);
508 mp->client_index = am->my_client_index;
509 strncpy ((char *) mp->name, plugin_name, sizeof (mp->name) - 1);
511 vl_msg_api_send_shmem (am->shmem_hdr->vl_input_queue, (u8 *) & mp);
513 /* Synchronously wait for the answer */
516 timeout = clib_time_now (&clib_time) + 1.0;
518 while (clib_time_now (&clib_time) < timeout)
520 if (mm->first_msg_id_reply_ready == 1)
522 rv = mm->first_msg_id_reply;
526 /* Restore old handler */
527 am->msg_handlers[VL_API_GET_FIRST_MSG_ID_REPLY] = old_handler;
535 /* Restore the old handler */
536 am->msg_handlers[VL_API_GET_FIRST_MSG_ID_REPLY] = old_handler;
539 clib_warning ("plugin '%s' not registered", plugin_name);
545 vlib_node_sync_stats (vlib_main_t * vm, vlib_node_t * n)
547 clib_warning ("STUB called...");
551 * fd.io coding-style-patch-verification: ON
554 * eval: (c-set-style "gnu")