2 * Copyright (c) 2016 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
18 #include <sys/types.h>
19 #include <sys/socket.h>
22 #include <netinet/in.h>
26 #include <vnet/vnet.h>
27 #include <vlib/vlib.h>
28 #include <vlib/unix/unix.h>
29 #include <vlibapi/api.h>
30 #include <vlibmemory/api.h>
32 #include <vpp/api/vpe_msg_enum.h>
34 #include "vppapiclient.h"
38 * Client registers a callback. All messages are sent to the callback.
40 * Client calls blocking read().
41 * Clients are expected to collate events on a queue.
42 * vac_write() -> suspends RX thread
43 * vac_read() -> resumes RX thread
46 #define vl_typedefs /* define message structures */
47 #include <vpp/api/vpe_all_api_h.h>
50 #define vl_endianfun /* define message structures */
51 #include <vpp/api/vpe_all_api_h.h>
54 vlib_main_t vlib_global_main;
55 vlib_main_t **vlib_mains;
59 pthread_t rx_thread_handle;
60 pthread_t timeout_thread_handle;
61 pthread_mutex_t queue_lock;
62 pthread_cond_t suspend_cv;
63 pthread_cond_t resume_cv;
64 pthread_mutex_t timeout_lock;
66 pthread_cond_t timeout_cv;
67 pthread_cond_t timeout_cancel_cv;
68 pthread_cond_t terminate_cv;
72 vac_callback_t vac_callback;
74 bool rx_is_running = false;
75 bool timeout_thread_cancelled = false;
77 /* Set to true to enable memory tracing */
78 bool mem_trace = false;
80 __attribute__((constructor))
82 vac_client_constructor (void)
84 clib_mem_init (0, 1 << 30);
90 heap = clib_mem_get_per_cpu_heap ();
91 h = mheap_header (heap);
92 /* make the main heap thread-safe */
93 h->flags |= MHEAP_FLAG_THREAD_SAFE;
100 __attribute__((destructor))
102 vac_client_destructor (void)
105 fformat(stderr, "TRACE: %s",
107 format_mheap, clib_mem_get_heap (), 1));
114 vac_main_t *pm = &vac_main;
115 memset(pm, 0, sizeof(*pm));
116 pthread_mutex_init(&pm->queue_lock, NULL);
117 pthread_cond_init(&pm->suspend_cv, NULL);
118 pthread_cond_init(&pm->resume_cv, NULL);
119 pthread_mutex_init(&pm->timeout_lock, NULL);
120 pm->timeout_loop = 1;
121 pthread_cond_init(&pm->timeout_cv, NULL);
122 pthread_cond_init(&pm->timeout_cancel_cv, NULL);
123 pthread_cond_init(&pm->terminate_cv, NULL);
129 vac_main_t *pm = &vac_main;
130 pthread_mutex_destroy(&pm->queue_lock);
131 pthread_cond_destroy(&pm->suspend_cv);
132 pthread_cond_destroy(&pm->resume_cv);
133 pthread_mutex_destroy(&pm->timeout_lock);
134 pthread_cond_destroy(&pm->timeout_cv);
135 pthread_cond_destroy(&pm->timeout_cancel_cv);
136 pthread_cond_destroy(&pm->terminate_cv);
137 memset(pm, 0, sizeof(*pm));
141 * Satisfy external references when -lvlib is not available.
143 void vlib_cli_output (struct vlib_main_t * vm, char * fmt, ...)
145 clib_warning ("vlib_cli_output called...");
149 vac_free (void * msg)
151 vl_msg_api_free (msg);
155 vac_api_handler (void *msg)
157 u16 id = ntohs(*((u16 *)msg));
158 msgbuf_t *msgbuf = (msgbuf_t *)(((u8 *)msg) - offsetof(msgbuf_t, data));
159 int l = ntohl(msgbuf->data_len);
161 clib_warning("Message ID %d has wrong length: %d\n", id, l);
163 /* Call Python callback */
164 ASSERT(vac_callback);
165 (vac_callback)(msg, l);
170 vac_rx_thread_fn (void *arg)
173 vl_api_memclnt_keepalive_t *mp;
174 vl_api_memclnt_keepalive_reply_t *rmp;
175 vac_main_t *pm = &vac_main;
176 api_main_t *am = &api_main;
177 vl_shmem_hdr_t *shmem_hdr;
180 q = am->vl_input_queue;
183 while (!svm_queue_sub(q, (u8 *)&msg, SVM_Q_WAIT, 0))
185 u16 id = ntohs(*((u16 *)msg));
187 case VL_API_RX_THREAD_EXIT:
188 vl_msg_api_free((void *) msg);
189 /* signal waiting threads that this thread is about to terminate */
190 pthread_mutex_lock(&pm->queue_lock);
191 pthread_cond_signal(&pm->terminate_cv);
192 pthread_mutex_unlock(&pm->queue_lock);
197 case VL_API_MEMCLNT_RX_THREAD_SUSPEND:
198 vl_msg_api_free((void * )msg);
199 /* Suspend thread and signal reader */
200 pthread_mutex_lock(&pm->queue_lock);
201 pthread_cond_signal(&pm->suspend_cv);
202 /* Wait for the resume signal */
203 pthread_cond_wait (&pm->resume_cv, &pm->queue_lock);
204 pthread_mutex_unlock(&pm->queue_lock);
207 case VL_API_MEMCLNT_READ_TIMEOUT:
208 clib_warning("Received read timeout in async thread\n");
209 vl_msg_api_free((void *) msg);
212 case VL_API_MEMCLNT_KEEPALIVE:
214 rmp = vl_msg_api_alloc (sizeof (*rmp));
215 memset (rmp, 0, sizeof (*rmp));
216 rmp->_vl_msg_id = ntohs(VL_API_MEMCLNT_KEEPALIVE_REPLY);
217 rmp->context = mp->context;
218 shmem_hdr = am->shmem_hdr;
219 vl_msg_api_send_shmem(shmem_hdr->vl_input_queue, (u8 *)&rmp);
220 vl_msg_api_free((void *) msg);
224 vac_api_handler((void *)msg);
230 vac_timeout_thread_fn (void *arg)
232 vl_api_memclnt_read_timeout_t *ep;
233 vac_main_t *pm = &vac_main;
234 api_main_t *am = &api_main;
240 while (pm->timeout_loop)
243 pthread_mutex_lock(&pm->timeout_lock);
244 pthread_cond_wait (&pm->timeout_cv, &pm->timeout_lock);
245 timeout = read_timeout;
246 gettimeofday(&tv, NULL);
247 ts.tv_sec = tv.tv_sec + timeout;
249 rv = pthread_cond_timedwait (&pm->timeout_cancel_cv,
250 &pm->timeout_lock, &ts);
251 pthread_mutex_unlock(&pm->timeout_lock);
252 if (rv == ETIMEDOUT && !timeout_thread_cancelled)
254 ep = vl_msg_api_alloc (sizeof (*ep));
255 ep->_vl_msg_id = ntohs(VL_API_MEMCLNT_READ_TIMEOUT);
256 vl_msg_api_send_shmem(am->vl_input_queue, (u8 *)&ep);
263 vac_rx_suspend (void)
265 api_main_t *am = &api_main;
266 vac_main_t *pm = &vac_main;
267 vl_api_memclnt_rx_thread_suspend_t *ep;
269 if (!pm->rx_thread_handle) return;
270 pthread_mutex_lock(&pm->queue_lock);
273 ep = vl_msg_api_alloc (sizeof (*ep));
274 ep->_vl_msg_id = ntohs(VL_API_MEMCLNT_RX_THREAD_SUSPEND);
275 vl_msg_api_send_shmem(am->vl_input_queue, (u8 *)&ep);
276 /* Wait for RX thread to tell us it has suspended */
277 pthread_cond_wait(&pm->suspend_cv, &pm->queue_lock);
278 rx_is_running = false;
280 pthread_mutex_unlock(&pm->queue_lock);
286 vac_main_t *pm = &vac_main;
287 if (!pm->rx_thread_handle) return;
288 pthread_mutex_lock(&pm->queue_lock);
289 if (rx_is_running) goto unlock;
290 pthread_cond_signal(&pm->resume_cv);
291 rx_is_running = true;
293 pthread_mutex_unlock(&pm->queue_lock);
297 vac_msg_table_get_hash (void)
299 api_main_t *am = &api_main;
300 return (am->msg_index_by_name_and_crc);
304 vac_msg_table_size(void)
306 api_main_t *am = &api_main;
307 return hash_elts(am->msg_index_by_name_and_crc);
311 vac_connect (char * name, char * chroot_prefix, vac_callback_t cb,
315 vac_main_t *pm = &vac_main;
318 if (chroot_prefix != NULL)
319 vl_set_memory_root_path (chroot_prefix);
321 if ((rv = vl_client_api_map("/vpe-api"))) {
322 clib_warning ("vl_client_api_map returned %d", rv);
326 if (vl_client_connect(name, 0, rx_qlen) < 0) {
327 vl_client_api_unmap();
332 /* Start the rx queue thread */
333 rv = pthread_create(&pm->rx_thread_handle, NULL, vac_rx_thread_fn, 0);
335 clib_warning("pthread_create returned %d", rv);
336 vl_client_api_unmap();
340 rx_is_running = true;
343 /* Start read timeout thread */
344 rv = pthread_create(&pm->timeout_thread_handle, NULL,
345 vac_timeout_thread_fn, 0);
347 clib_warning("pthread_create returned %d", rv);
348 vl_client_api_unmap();
352 pm->connected_to_vlib = 1;
358 set_timeout (unsigned short timeout)
360 vac_main_t *pm = &vac_main;
361 pthread_mutex_lock(&pm->timeout_lock);
362 read_timeout = timeout;
363 pthread_cond_signal(&pm->timeout_cv);
364 pthread_mutex_unlock(&pm->timeout_lock);
370 vac_main_t *pm = &vac_main;
371 pthread_mutex_lock(&pm->timeout_lock);
372 pthread_cond_signal(&pm->timeout_cancel_cv);
373 pthread_mutex_unlock(&pm->timeout_lock);
377 vac_disconnect (void)
379 api_main_t *am = &api_main;
380 vac_main_t *pm = &vac_main;
383 if (!pm->connected_to_vlib) return 0;
385 if (pm->rx_thread_handle) {
386 vl_api_rx_thread_exit_t *ep;
387 ep = vl_msg_api_alloc (sizeof (*ep));
388 ep->_vl_msg_id = ntohs(VL_API_RX_THREAD_EXIT);
389 vl_msg_api_send_shmem(am->vl_input_queue, (u8 *)&ep);
391 /* wait (with timeout) until RX thread has finished */
394 gettimeofday(&tv, NULL);
395 ts.tv_sec = tv.tv_sec + 5;
397 pthread_mutex_lock(&pm->queue_lock);
398 int rv = pthread_cond_timedwait(&pm->terminate_cv, &pm->queue_lock, &ts);
399 pthread_mutex_unlock(&pm->queue_lock);
400 /* now join so we wait until thread has -really- finished */
402 pthread_cancel(pm->rx_thread_handle);
404 pthread_join(pm->rx_thread_handle, (void **) &junk);
406 if (pm->timeout_thread_handle) {
407 /* cancel, wake then join the timeout thread */
408 pm->timeout_loop = 0;
409 timeout_thread_cancelled = true;
411 pthread_join(pm->timeout_thread_handle, (void **) &junk);
414 vl_client_disconnect();
415 vl_client_api_unmap();
424 vac_read (char **p, int *l, u16 timeout)
427 api_main_t *am = &api_main;
428 vac_main_t *pm = &vac_main;
429 vl_api_memclnt_keepalive_t *mp;
430 vl_api_memclnt_keepalive_reply_t *rmp;
434 vl_shmem_hdr_t *shmem_hdr;
436 if (!pm->connected_to_vlib) return -1;
440 if (am->our_pid == 0) return (-1);
442 /* Poke timeout thread */
444 set_timeout(timeout);
446 q = am->vl_input_queue;
449 rv = svm_queue_sub(q, (u8 *)&msg, SVM_Q_WAIT, 0);
452 u16 msg_id = ntohs(*((u16 *)msg));
454 case VL_API_RX_THREAD_EXIT:
455 vl_msg_api_free((void *) msg);
457 case VL_API_MEMCLNT_RX_THREAD_SUSPEND:
459 case VL_API_MEMCLNT_READ_TIMEOUT:
461 case VL_API_MEMCLNT_KEEPALIVE:
462 /* Handle an alive-check ping from vpp. */
464 rmp = vl_msg_api_alloc (sizeof (*rmp));
465 memset (rmp, 0, sizeof (*rmp));
466 rmp->_vl_msg_id = ntohs(VL_API_MEMCLNT_KEEPALIVE_REPLY);
467 rmp->context = mp->context;
468 shmem_hdr = am->shmem_hdr;
469 vl_msg_api_send_shmem(shmem_hdr->vl_input_queue, (u8 *)&rmp);
470 vl_msg_api_free((void *) msg);
472 * Python code is blissfully unaware of these pings, so
473 * act as if it never happened...
478 msgbuf = (msgbuf_t *)(((u8 *)msg) - offsetof(msgbuf_t, data));
479 *l = ntohl(msgbuf->data_len);
481 fprintf(stderr, "Unregistered API message: %d\n", msg_id);
487 /* Let timeout notification thread know we're done */
491 fprintf(stderr, "Read failed with %d\n", rv);
496 vl_msg_api_free((void *) msg);
497 /* Client might forget to resume RX thread on failure */
503 * XXX: Makes the assumption that client_index is the first member
505 typedef VL_API_PACKED(struct _vl_api_header {
511 vac_client_index (void)
513 return (api_main.my_client_index);
517 vac_write (char *p, int l)
520 api_main_t *am = &api_main;
521 vl_api_header_t *mp = vl_msg_api_alloc(l);
523 vac_main_t *pm = &vac_main;
525 if (!pm->connected_to_vlib) return -1;
526 if (!mp) return (-1);
529 mp->client_index = vac_client_index();
530 q = am->shmem_hdr->vl_input_queue;
531 rv = svm_queue_add(q, (u8 *)&mp, 0);
533 fprintf(stderr, "vpe_api_write fails: %d\n", rv);
541 vac_get_msg_index (unsigned char * name)
543 return vl_msg_api_get_msg_index (name);
547 vac_msg_table_max_index(void)
551 uword *h = vac_msg_table_get_hash();
552 hash_foreach_pair (hp, h,
554 if (hp->value[0] > max)
562 vac_set_error_handler (vac_error_callback_t cb)
564 if (cb) clib_error_register_handler (cb, 0);