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 <vppinfra/serialize.h>
24 #include <vppinfra/hash.h>
25 #include <vlibmemory/memory_client.h>
27 /* A hack. vl_client_get_first_plugin_msg_id depends on it */
28 #include <vlibmemory/socket_client.h>
30 #include <vlibmemory/vl_memory_msg_enum.h>
32 #define vl_typedefs /* define message structures */
33 #include <vlibmemory/vl_memory_api_h.h>
36 #define vl_endianfun /* define message structures */
37 #include <vlibmemory/vl_memory_api_h.h>
40 /* instantiate all the print functions we know about */
41 #define vl_print(handle, ...) clib_warning (__VA_ARGS__)
43 #include <vlibmemory/vl_memory_api_h.h>
48 u8 rx_thread_jmpbuf_valid;
50 jmp_buf rx_thread_jmpbuf;
51 pthread_t rx_thread_handle;
52 /* Plugin message base lookup scheme */
53 volatile u8 first_msg_id_reply_ready;
54 u16 first_msg_id_reply;
55 } memory_client_main_t;
57 memory_client_main_t memory_client_main;
60 rx_thread_fn (void *arg)
63 memory_client_main_t *mm = &memory_client_main;
64 api_main_t *am = &api_main;
67 q = am->vl_input_queue;
69 /* So we can make the rx thread terminate cleanly */
70 if (setjmp (mm->rx_thread_jmpbuf) == 0)
72 mm->rx_thread_jmpbuf_valid = 1;
74 * Find an unused slot in the per-cpu-mheaps array,
75 * and grab it for this thread. We need to be able to
76 * push/pop the thread heap without affecting other thread(s).
78 if (__os_thread_index == 0)
80 for (i = 0; i < ARRAY_LEN (clib_per_cpu_mheaps); i++)
82 if (clib_per_cpu_mheaps[i] == 0)
84 /* Copy the main thread mheap pointer */
85 clib_per_cpu_mheaps[i] = clib_per_cpu_mheaps[0];
86 __os_thread_index = i;
90 ASSERT (__os_thread_index > 0);
93 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 unserialize_open_data (sm, tblv, vec_len (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 svm_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 svm_queue_init (input_queue_size, sizeof (uword), getpid (), 0);
194 pthread_mutex_unlock (&svm->mutex);
195 svm_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 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 if (ntohs (rp->_vl_msg_id) != VL_API_MEMCLNT_CREATE_REPLY)
235 clib_warning ("unexpected reply: id %d", ntohs (rp->_vl_msg_id));
238 rv = clib_net_to_host_u32 (rp->response);
240 vl_msg_api_handler ((void *) rp);
247 vl_api_memclnt_delete_reply_t_handler (vl_api_memclnt_delete_reply_t * mp)
250 api_main_t *am = &api_main;
252 pthread_mutex_lock (&am->vlib_rp->mutex);
253 oldheap = svm_push_data_heap (am->vlib_rp);
254 svm_queue_free (am->vl_input_queue);
255 pthread_mutex_unlock (&am->vlib_rp->mutex);
256 svm_pop_heap (oldheap);
258 am->my_client_index = ~0;
259 am->my_registration = 0;
260 am->vl_input_queue = 0;
264 vl_client_disconnect (void)
266 vl_api_memclnt_delete_t *mp;
267 vl_api_memclnt_delete_reply_t *rp;
268 svm_queue_t *vl_input_queue;
269 vl_shmem_hdr_t *shmem_hdr;
271 api_main_t *am = &api_main;
273 ASSERT (am->vlib_rp);
274 shmem_hdr = am->shmem_hdr;
275 ASSERT (shmem_hdr && shmem_hdr->vl_input_queue);
277 vl_input_queue = am->vl_input_queue;
279 mp = vl_msg_api_alloc (sizeof (vl_api_memclnt_delete_t));
280 memset (mp, 0, sizeof (*mp));
281 mp->_vl_msg_id = ntohs (VL_API_MEMCLNT_DELETE);
282 mp->index = am->my_client_index;
283 mp->handle = (uword) am->my_registration;
285 vl_msg_api_send_shmem (shmem_hdr->vl_input_queue, (u8 *) & mp);
288 * Have to be careful here, in case the client is disconnecting
289 * because e.g. the vlib process died, or is unresponsive.
299 if (now >= (begin + 2))
301 clib_warning ("peer unresponsive, give up");
302 am->my_client_index = ~0;
303 am->my_registration = 0;
307 if (svm_queue_sub (vl_input_queue, (u8 *) & rp, 1) < 0)
310 /* drain the queue */
311 if (ntohs (rp->_vl_msg_id) != VL_API_MEMCLNT_DELETE_REPLY)
313 clib_warning ("queue drain: %d", ntohs (rp->_vl_msg_id));
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)
350 vl_client_install_client_message_handlers (void)
354 vl_msg_api_set_handlers(VL_API_##N, #n, \
355 vl_api_##n##_t_handler, \
357 vl_api_##n##_t_endian, \
358 vl_api_##n##_t_print, \
359 sizeof(vl_api_##n##_t), 1);
366 vl_client_api_map (const char *region_name)
370 if ((rv = vl_map_shmem (region_name, 0 /* is_vlib */ )) < 0)
373 vl_client_install_client_message_handlers ();
378 vl_client_api_unmap (void)
384 connect_to_vlib_internal (const char *svm_name,
385 const char *client_name,
386 int rx_queue_size, int want_pthread, int do_map)
389 memory_client_main_t *mm = &memory_client_main;
391 if (do_map && (rv = vl_client_api_map (svm_name)))
393 clib_warning ("vl_client_api map rv %d", rv);
397 if (vl_client_connect (client_name, 0 /* punt quota */ ,
398 rx_queue_size /* input queue */ ) < 0)
400 vl_client_api_unmap ();
404 /* Start the rx queue thread */
408 rv = pthread_create (&mm->rx_thread_handle,
409 NULL /*attr */ , rx_thread_fn, 0);
411 clib_warning ("pthread_create returned %d", rv);
414 mm->connected_to_vlib = 1;
419 vl_client_connect_to_vlib (const char *svm_name,
420 const char *client_name, int rx_queue_size)
422 return connect_to_vlib_internal (svm_name, client_name, rx_queue_size,
423 1 /* want pthread */ ,
428 vl_client_connect_to_vlib_no_rx_pthread (const char *svm_name,
429 const char *client_name,
432 return connect_to_vlib_internal (svm_name, client_name, rx_queue_size,
433 0 /* want pthread */ ,
438 vl_client_connect_to_vlib_no_map (const char *svm_name,
439 const char *client_name, int rx_queue_size)
441 return connect_to_vlib_internal (svm_name, client_name, rx_queue_size,
442 1 /* want pthread */ ,
447 vl_client_disconnect_from_vlib (void)
449 memory_client_main_t *mm = &memory_client_main;
450 api_main_t *am = &api_main;
453 if (mm->rx_thread_jmpbuf_valid)
455 vl_api_rx_thread_exit_t *ep;
456 ep = vl_msg_api_alloc (sizeof (*ep));
457 ep->_vl_msg_id = ntohs (VL_API_RX_THREAD_EXIT);
458 vl_msg_api_send_shmem (am->vl_input_queue, (u8 *) & ep);
459 pthread_join (mm->rx_thread_handle, (void **) &junk);
461 if (mm->connected_to_vlib)
463 vl_client_disconnect ();
464 vl_client_api_unmap ();
466 memset (mm, 0, sizeof (*mm));
469 static void vl_api_get_first_msg_id_reply_t_handler
470 (vl_api_get_first_msg_id_reply_t * mp)
472 memory_client_main_t *mm = &memory_client_main;
473 i32 retval = ntohl (mp->retval);
475 mm->first_msg_id_reply = (retval >= 0) ? ntohs (mp->first_msg_id) : ~0;
476 mm->first_msg_id_reply_ready = 1;
480 vl_client_get_first_plugin_msg_id (const char *plugin_name)
482 vl_api_get_first_msg_id_t *mp;
483 api_main_t *am = &api_main;
484 memory_client_main_t *mm = &memory_client_main;
487 clib_time_t clib_time;
490 if (strlen (plugin_name) + 1 > sizeof (mp->name))
493 memset (&clib_time, 0, sizeof (clib_time));
494 clib_time_init (&clib_time);
496 /* Push this plugin's first_msg_id_reply handler */
497 old_handler = am->msg_handlers[VL_API_GET_FIRST_MSG_ID_REPLY];
498 am->msg_handlers[VL_API_GET_FIRST_MSG_ID_REPLY] = (void *)
499 vl_api_get_first_msg_id_reply_t_handler;
501 /* Ask the data-plane for the message-ID base of the indicated plugin */
502 mm->first_msg_id_reply_ready = 0;
504 /* Not using shm client */
505 if (!am->my_registration)
507 mp = vl_socket_client_msg_alloc (sizeof (*mp));
508 memset (mp, 0, sizeof (*mp));
509 mp->_vl_msg_id = ntohs (VL_API_GET_FIRST_MSG_ID);
510 mp->client_index = am->my_client_index;
511 strncpy ((char *) mp->name, plugin_name, sizeof (mp->name) - 1);
513 if (vl_socket_client_write () <= 0)
515 if (vl_socket_client_read (1))
518 if (mm->first_msg_id_reply_ready == 1)
520 rv = mm->first_msg_id_reply;
525 /* Restore old handler */
526 am->msg_handlers[VL_API_GET_FIRST_MSG_ID_REPLY] = old_handler;
532 mp = vl_msg_api_alloc (sizeof (*mp));
533 memset (mp, 0, sizeof (*mp));
534 mp->_vl_msg_id = ntohs (VL_API_GET_FIRST_MSG_ID);
535 mp->client_index = am->my_client_index;
536 strncpy ((char *) mp->name, plugin_name, sizeof (mp->name) - 1);
538 vl_msg_api_send_shmem (am->shmem_hdr->vl_input_queue, (u8 *) & mp);
540 /* Synchronously wait for the answer */
541 timeout = clib_time_now (&clib_time) + 1.0;
542 while (clib_time_now (&clib_time) < timeout)
544 if (mm->first_msg_id_reply_ready == 1)
546 rv = mm->first_msg_id_reply;
550 /* Restore old handler */
551 am->msg_handlers[VL_API_GET_FIRST_MSG_ID_REPLY] = old_handler;
558 /* Restore the old handler */
559 am->msg_handlers[VL_API_GET_FIRST_MSG_ID_REPLY] = old_handler;
562 clib_warning ("plugin '%s' not registered", plugin_name);
568 * fd.io coding-style-patch-verification: ON
571 * eval: (c-set-style "gnu")