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.
19 #include <sys/types.h>
20 #include <sys/socket.h>
23 #include <netinet/in.h>
27 #include <vnet/vnet.h>
28 #include <vlib/vlib.h>
29 #include <vlib/unix/unix.h>
30 #include <vlibapi/api.h>
31 #include <vlibmemory/api.h>
33 #include <vpp/api/vpe_msg_enum.h>
35 #include "vppapiclient.h"
37 bool timeout_cancelled;
38 bool timeout_in_progress;
43 * Client registers a callback. All messages are sent to the callback.
45 * Client calls blocking read().
46 * Clients are expected to collate events on a queue.
47 * vac_write() -> suspends RX thread
48 * vac_read() -> resumes RX thread
51 #define vl_typedefs /* define message structures */
52 #include <vpp/api/vpe_all_api_h.h>
55 #define vl_endianfun /* define message structures */
56 #include <vpp/api/vpe_all_api_h.h>
59 vlib_main_t vlib_global_main;
60 vlib_main_t **vlib_mains;
64 pthread_t rx_thread_handle;
65 pthread_t timeout_thread_handle;
66 pthread_mutex_t queue_lock;
67 pthread_cond_t suspend_cv;
68 pthread_cond_t resume_cv;
69 pthread_mutex_t timeout_lock;
71 pthread_cond_t timeout_cv;
72 pthread_cond_t timeout_cancel_cv;
73 pthread_cond_t terminate_cv;
77 vac_callback_t vac_callback;
79 bool rx_is_running = false;
80 bool timeout_thread_cancelled = false;
82 /* Only ever allocate one heap */
83 bool mem_initialized = false;
88 vac_main_t *pm = &vac_main;
89 clib_memset(pm, 0, sizeof(*pm));
90 pthread_mutex_init(&pm->queue_lock, NULL);
91 pthread_cond_init(&pm->suspend_cv, NULL);
92 pthread_cond_init(&pm->resume_cv, NULL);
93 pthread_mutex_init(&pm->timeout_lock, NULL);
95 pthread_cond_init(&pm->timeout_cv, NULL);
96 pthread_cond_init(&pm->timeout_cancel_cv, NULL);
97 pthread_cond_init(&pm->terminate_cv, NULL);
103 vac_main_t *pm = &vac_main;
104 pthread_mutex_destroy(&pm->queue_lock);
105 pthread_cond_destroy(&pm->suspend_cv);
106 pthread_cond_destroy(&pm->resume_cv);
107 pthread_mutex_destroy(&pm->timeout_lock);
108 pthread_cond_destroy(&pm->timeout_cv);
109 pthread_cond_destroy(&pm->timeout_cancel_cv);
110 pthread_cond_destroy(&pm->terminate_cv);
111 clib_memset(pm, 0, sizeof(*pm));
115 * Satisfy external references when -lvlib is not available.
117 void vlib_cli_output (struct vlib_main_t * vm, char * fmt, ...)
119 clib_warning ("vlib_cli_output called...");
123 vac_free (void * msg)
125 vl_msg_api_free (msg);
129 vac_api_handler (void *msg)
131 u16 id = ntohs(*((u16 *)msg));
132 msgbuf_t *msgbuf = (msgbuf_t *)(((u8 *)msg) - offsetof(msgbuf_t, data));
133 int l = ntohl(msgbuf->data_len);
135 clib_warning("Message ID %d has wrong length: %d\n", id, l);
137 /* Call Python callback */
138 ASSERT(vac_callback);
139 (vac_callback)(msg, l);
144 vac_rx_thread_fn (void *arg)
147 vl_api_memclnt_keepalive_t *mp;
148 vl_api_memclnt_keepalive_reply_t *rmp;
149 vac_main_t *pm = &vac_main;
150 api_main_t *am = vlibapi_get_main();
151 vl_shmem_hdr_t *shmem_hdr;
154 q = am->vl_input_queue;
157 while (!svm_queue_sub(q, (u8 *)&msg, SVM_Q_WAIT, 0))
159 VL_MSG_API_UNPOISON((void *)msg);
160 u16 id = ntohs(*((u16 *)msg));
162 case VL_API_RX_THREAD_EXIT:
163 vl_msg_api_free((void *) msg);
164 /* signal waiting threads that this thread is about to terminate */
165 pthread_mutex_lock(&pm->queue_lock);
166 rx_thread_done = true;
167 pthread_cond_signal(&pm->terminate_cv);
168 pthread_mutex_unlock(&pm->queue_lock);
173 case VL_API_MEMCLNT_RX_THREAD_SUSPEND:
174 vl_msg_api_free((void * )msg);
175 /* Suspend thread and signal reader */
176 pthread_mutex_lock(&pm->queue_lock);
177 pthread_cond_signal(&pm->suspend_cv);
178 /* Wait for the resume signal */
179 pthread_cond_wait (&pm->resume_cv, &pm->queue_lock);
180 pthread_mutex_unlock(&pm->queue_lock);
183 case VL_API_MEMCLNT_READ_TIMEOUT:
184 clib_warning("Received read timeout in async thread\n");
185 vl_msg_api_free((void *) msg);
188 case VL_API_MEMCLNT_KEEPALIVE:
190 rmp = vl_msg_api_alloc (sizeof (*rmp));
191 clib_memset (rmp, 0, sizeof (*rmp));
192 rmp->_vl_msg_id = ntohs(VL_API_MEMCLNT_KEEPALIVE_REPLY);
193 rmp->context = mp->context;
194 shmem_hdr = am->shmem_hdr;
195 vl_msg_api_send_shmem(shmem_hdr->vl_input_queue, (u8 *)&rmp);
196 vl_msg_api_free((void *) msg);
200 vac_api_handler((void *)msg);
206 vac_timeout_thread_fn (void *arg)
208 vl_api_memclnt_read_timeout_t *ep;
209 vac_main_t *pm = &vac_main;
210 api_main_t *am = vlibapi_get_main();
215 while (pm->timeout_loop)
218 pthread_mutex_lock(&pm->timeout_lock);
219 while (!timeout_in_progress)
220 pthread_cond_wait (&pm->timeout_cv, &pm->timeout_lock);
223 gettimeofday(&tv, NULL);
224 ts.tv_sec = tv.tv_sec + read_timeout;
227 if (!timeout_cancelled) {
228 rv = pthread_cond_timedwait (&pm->timeout_cancel_cv,
229 &pm->timeout_lock, &ts);
230 if (rv == ETIMEDOUT && !timeout_thread_cancelled) {
231 ep = vl_msg_api_alloc (sizeof (*ep));
232 ep->_vl_msg_id = ntohs(VL_API_MEMCLNT_READ_TIMEOUT);
233 vl_msg_api_send_shmem(am->vl_input_queue, (u8 *)&ep);
237 pthread_mutex_unlock(&pm->timeout_lock);
243 vac_rx_suspend (void)
245 api_main_t *am = vlibapi_get_main();
246 vac_main_t *pm = &vac_main;
247 vl_api_memclnt_rx_thread_suspend_t *ep;
249 if (!pm->rx_thread_handle) return;
250 pthread_mutex_lock(&pm->queue_lock);
253 ep = vl_msg_api_alloc (sizeof (*ep));
254 ep->_vl_msg_id = ntohs(VL_API_MEMCLNT_RX_THREAD_SUSPEND);
255 vl_msg_api_send_shmem(am->vl_input_queue, (u8 *)&ep);
256 /* Wait for RX thread to tell us it has suspended */
257 pthread_cond_wait(&pm->suspend_cv, &pm->queue_lock);
258 rx_is_running = false;
260 pthread_mutex_unlock(&pm->queue_lock);
266 vac_main_t *pm = &vac_main;
267 if (!pm->rx_thread_handle) return;
268 pthread_mutex_lock(&pm->queue_lock);
269 if (rx_is_running) goto unlock;
270 pthread_cond_signal(&pm->resume_cv);
271 rx_is_running = true;
273 pthread_mutex_unlock(&pm->queue_lock);
277 vac_msg_table_get_hash (void)
279 api_main_t *am = vlibapi_get_main();
280 return (am->msg_index_by_name_and_crc);
284 vac_msg_table_size(void)
286 api_main_t *am = vlibapi_get_main();
287 return hash_elts(am->msg_index_by_name_and_crc);
291 vac_connect (char * name, char * chroot_prefix, vac_callback_t cb,
294 rx_thread_done = false;
296 vac_main_t *pm = &vac_main;
298 assert (clib_mem_get_heap ());
300 if (chroot_prefix != NULL)
301 vl_set_memory_root_path (chroot_prefix);
303 if ((rv = vl_client_api_map("/vpe-api"))) {
304 clib_warning ("vl_client_api_map returned %d", rv);
308 if (vl_client_connect(name, 0, rx_qlen) < 0) {
309 vl_client_api_unmap();
314 /* Start the rx queue thread */
315 rv = pthread_create(&pm->rx_thread_handle, NULL, vac_rx_thread_fn, 0);
317 clib_warning("pthread_create returned %d", rv);
318 vl_client_api_unmap();
322 rx_is_running = true;
325 /* Start read timeout thread */
326 rv = pthread_create(&pm->timeout_thread_handle, NULL,
327 vac_timeout_thread_fn, 0);
329 clib_warning("pthread_create returned %d", rv);
330 vl_client_api_unmap();
334 pm->connected_to_vlib = 1;
339 set_timeout (unsigned short timeout)
341 vac_main_t *pm = &vac_main;
342 pthread_mutex_lock(&pm->timeout_lock);
343 read_timeout = timeout;
344 timeout_in_progress = true;
345 timeout_cancelled = false;
346 pthread_cond_signal(&pm->timeout_cv);
347 pthread_mutex_unlock(&pm->timeout_lock);
353 vac_main_t *pm = &vac_main;
354 pthread_mutex_lock(&pm->timeout_lock);
355 timeout_in_progress = false;
356 timeout_cancelled = true;
357 pthread_cond_signal(&pm->timeout_cancel_cv);
358 pthread_mutex_unlock(&pm->timeout_lock);
362 vac_disconnect (void)
364 api_main_t *am = vlibapi_get_main();
365 vac_main_t *pm = &vac_main;
369 if (!pm->connected_to_vlib) return 0;
371 if (pm->rx_thread_handle) {
372 vl_api_rx_thread_exit_t *ep;
373 ep = vl_msg_api_alloc (sizeof (*ep));
374 ep->_vl_msg_id = ntohs(VL_API_RX_THREAD_EXIT);
375 vl_msg_api_send_shmem(am->vl_input_queue, (u8 *)&ep);
377 /* wait (with timeout) until RX thread has finished */
380 gettimeofday(&tv, NULL);
381 ts.tv_sec = tv.tv_sec + 5;
384 pthread_mutex_lock(&pm->queue_lock);
385 if (rx_thread_done == false)
386 rv = pthread_cond_timedwait(&pm->terminate_cv, &pm->queue_lock, &ts);
387 pthread_mutex_unlock(&pm->queue_lock);
389 /* now join so we wait until thread has -really- finished */
391 pthread_cancel(pm->rx_thread_handle);
393 pthread_join(pm->rx_thread_handle, (void **) &junk);
395 if (pm->timeout_thread_handle) {
396 /* cancel, wake then join the timeout thread */
397 pm->timeout_loop = 0;
398 timeout_thread_cancelled = true;
400 pthread_join(pm->timeout_thread_handle, (void **) &junk);
403 vl_client_disconnect();
404 vl_client_api_unmap();
413 vac_read (char **p, int *l, u16 timeout)
416 api_main_t *am = vlibapi_get_main();
417 vac_main_t *pm = &vac_main;
418 vl_api_memclnt_keepalive_t *mp;
419 vl_api_memclnt_keepalive_reply_t *rmp;
423 vl_shmem_hdr_t *shmem_hdr;
425 /* svm_queue_sub(below) returns {-1, -2} */
426 if (!pm->connected_to_vlib) return -3;
430 /* svm_queue_sub(below) returns {-1, -2} */
431 if (am->our_pid == 0) return (-4);
433 /* Poke timeout thread */
435 set_timeout(timeout);
437 q = am->vl_input_queue;
440 rv = svm_queue_sub(q, (u8 *)&msg, SVM_Q_WAIT, 0);
443 VL_MSG_API_UNPOISON((void *)msg);
444 u16 msg_id = ntohs(*((u16 *)msg));
446 case VL_API_RX_THREAD_EXIT:
447 vl_msg_api_free((void *) msg);
449 case VL_API_MEMCLNT_RX_THREAD_SUSPEND:
451 case VL_API_MEMCLNT_READ_TIMEOUT:
453 case VL_API_MEMCLNT_KEEPALIVE:
454 /* Handle an alive-check ping from vpp. */
456 rmp = vl_msg_api_alloc (sizeof (*rmp));
457 clib_memset (rmp, 0, sizeof (*rmp));
458 rmp->_vl_msg_id = ntohs(VL_API_MEMCLNT_KEEPALIVE_REPLY);
459 rmp->context = mp->context;
460 shmem_hdr = am->shmem_hdr;
461 vl_msg_api_send_shmem(shmem_hdr->vl_input_queue, (u8 *)&rmp);
462 vl_msg_api_free((void *) msg);
464 * Python code is blissfully unaware of these pings, so
465 * act as if it never happened...
470 msgbuf = (msgbuf_t *)(((u8 *)msg) - offsetof(msgbuf_t, data));
471 *l = ntohl(msgbuf->data_len);
473 fprintf(stderr, "Unregistered API message: %d\n", msg_id);
481 fprintf(stderr, "Read failed with %d\n", rv);
483 /* Let timeout notification thread know we're done */
492 vl_msg_api_free((void *) msg);
493 /* Client might forget to resume RX thread on failure */
499 * XXX: Makes the assumption that client_index is the first member
501 typedef VL_API_PACKED(struct _vl_api_header {
507 vac_client_index (void)
509 return (vlibapi_get_main()->my_client_index);
513 vac_write (char *p, int l)
516 api_main_t *am = vlibapi_get_main();
517 vl_api_header_t *mp = vl_msg_api_alloc(l);
519 vac_main_t *pm = &vac_main;
521 if (!pm->connected_to_vlib) return -1;
522 if (!mp) return (-1);
525 mp->client_index = vac_client_index();
526 q = am->shmem_hdr->vl_input_queue;
527 rv = svm_queue_add(q, (u8 *)&mp, 0);
529 fprintf(stderr, "vpe_api_write fails: %d\n", rv);
537 vac_get_msg_index (char * name)
539 return vl_msg_api_get_msg_index ((u8 *)name);
543 vac_msg_table_max_index(void)
547 uword *h = vac_msg_table_get_hash();
548 hash_foreach_pair (hp, h,
550 if (hp->value[0] > max)
558 vac_set_error_handler (vac_error_callback_t cb)
560 assert (clib_mem_get_heap ());
561 if (cb) clib_error_register_handler (cb, 0);
565 * Required if application doesn't use a VPP heap.
568 vac_mem_init (size_t size)
573 clib_mem_init (0, 1 << 30); // default
575 clib_mem_init (0, size);
576 mem_initialized = true;