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;
85 vac_main_t *pm = &vac_main;
86 clib_memset(pm, 0, sizeof(*pm));
87 pthread_mutex_init(&pm->queue_lock, NULL);
88 pthread_cond_init(&pm->suspend_cv, NULL);
89 pthread_cond_init(&pm->resume_cv, NULL);
90 pthread_mutex_init(&pm->timeout_lock, NULL);
92 pthread_cond_init(&pm->timeout_cv, NULL);
93 pthread_cond_init(&pm->timeout_cancel_cv, NULL);
94 pthread_cond_init(&pm->terminate_cv, NULL);
100 vac_main_t *pm = &vac_main;
101 pthread_mutex_destroy(&pm->queue_lock);
102 pthread_cond_destroy(&pm->suspend_cv);
103 pthread_cond_destroy(&pm->resume_cv);
104 pthread_mutex_destroy(&pm->timeout_lock);
105 pthread_cond_destroy(&pm->timeout_cv);
106 pthread_cond_destroy(&pm->timeout_cancel_cv);
107 pthread_cond_destroy(&pm->terminate_cv);
108 clib_memset(pm, 0, sizeof(*pm));
112 * Satisfy external references when -lvlib is not available.
114 void vlib_cli_output (struct vlib_main_t * vm, char * fmt, ...)
116 clib_warning ("vlib_cli_output called...");
120 vac_free (void * msg)
122 vl_msg_api_free (msg);
126 vac_api_handler (void *msg)
128 u16 id = ntohs(*((u16 *)msg));
129 msgbuf_t *msgbuf = (msgbuf_t *)(((u8 *)msg) - offsetof(msgbuf_t, data));
130 int l = ntohl(msgbuf->data_len);
132 clib_warning("Message ID %d has wrong length: %d\n", id, l);
134 /* Call Python callback */
135 ASSERT(vac_callback);
136 (vac_callback)(msg, l);
141 vac_rx_thread_fn (void *arg)
144 vl_api_memclnt_keepalive_t *mp;
145 vl_api_memclnt_keepalive_reply_t *rmp;
146 vac_main_t *pm = &vac_main;
147 api_main_t *am = vlibapi_get_main();
148 vl_shmem_hdr_t *shmem_hdr;
151 q = am->vl_input_queue;
154 while (!svm_queue_sub(q, (u8 *)&msg, SVM_Q_WAIT, 0))
156 VL_MSG_API_UNPOISON((void *)msg);
157 u16 id = ntohs(*((u16 *)msg));
159 case VL_API_RX_THREAD_EXIT:
160 vl_msg_api_free((void *) msg);
161 /* signal waiting threads that this thread is about to terminate */
162 pthread_mutex_lock(&pm->queue_lock);
163 rx_thread_done = true;
164 pthread_cond_signal(&pm->terminate_cv);
165 pthread_mutex_unlock(&pm->queue_lock);
170 case VL_API_MEMCLNT_RX_THREAD_SUSPEND:
171 vl_msg_api_free((void * )msg);
172 /* Suspend thread and signal reader */
173 pthread_mutex_lock(&pm->queue_lock);
174 pthread_cond_signal(&pm->suspend_cv);
175 /* Wait for the resume signal */
176 pthread_cond_wait (&pm->resume_cv, &pm->queue_lock);
177 pthread_mutex_unlock(&pm->queue_lock);
180 case VL_API_MEMCLNT_READ_TIMEOUT:
181 clib_warning("Received read timeout in async thread\n");
182 vl_msg_api_free((void *) msg);
185 case VL_API_MEMCLNT_KEEPALIVE:
187 rmp = vl_msg_api_alloc (sizeof (*rmp));
188 clib_memset (rmp, 0, sizeof (*rmp));
189 rmp->_vl_msg_id = ntohs(VL_API_MEMCLNT_KEEPALIVE_REPLY);
190 rmp->context = mp->context;
191 shmem_hdr = am->shmem_hdr;
192 vl_msg_api_send_shmem(shmem_hdr->vl_input_queue, (u8 *)&rmp);
193 vl_msg_api_free((void *) msg);
197 vac_api_handler((void *)msg);
203 vac_timeout_thread_fn (void *arg)
205 vl_api_memclnt_read_timeout_t *ep;
206 vac_main_t *pm = &vac_main;
207 api_main_t *am = vlibapi_get_main();
212 while (pm->timeout_loop)
215 pthread_mutex_lock(&pm->timeout_lock);
216 while (!timeout_in_progress)
217 pthread_cond_wait (&pm->timeout_cv, &pm->timeout_lock);
220 gettimeofday(&tv, NULL);
221 ts.tv_sec = tv.tv_sec + read_timeout;
224 if (!timeout_cancelled) {
225 rv = pthread_cond_timedwait (&pm->timeout_cancel_cv,
226 &pm->timeout_lock, &ts);
227 if (rv == ETIMEDOUT && !timeout_thread_cancelled) {
228 ep = vl_msg_api_alloc (sizeof (*ep));
229 ep->_vl_msg_id = ntohs(VL_API_MEMCLNT_READ_TIMEOUT);
230 vl_msg_api_send_shmem(am->vl_input_queue, (u8 *)&ep);
234 pthread_mutex_unlock(&pm->timeout_lock);
240 vac_rx_suspend (void)
242 api_main_t *am = vlibapi_get_main();
243 vac_main_t *pm = &vac_main;
244 vl_api_memclnt_rx_thread_suspend_t *ep;
246 if (!pm->rx_thread_handle) return;
247 pthread_mutex_lock(&pm->queue_lock);
250 ep = vl_msg_api_alloc (sizeof (*ep));
251 ep->_vl_msg_id = ntohs(VL_API_MEMCLNT_RX_THREAD_SUSPEND);
252 vl_msg_api_send_shmem(am->vl_input_queue, (u8 *)&ep);
253 /* Wait for RX thread to tell us it has suspended */
254 pthread_cond_wait(&pm->suspend_cv, &pm->queue_lock);
255 rx_is_running = false;
257 pthread_mutex_unlock(&pm->queue_lock);
263 vac_main_t *pm = &vac_main;
264 if (!pm->rx_thread_handle) return;
265 pthread_mutex_lock(&pm->queue_lock);
266 if (rx_is_running) goto unlock;
267 pthread_cond_signal(&pm->resume_cv);
268 rx_is_running = true;
270 pthread_mutex_unlock(&pm->queue_lock);
274 vac_msg_table_get_hash (void)
276 api_main_t *am = vlibapi_get_main();
277 return (am->msg_index_by_name_and_crc);
281 vac_msg_table_size(void)
283 api_main_t *am = vlibapi_get_main();
284 return hash_elts(am->msg_index_by_name_and_crc);
288 vac_connect (char * name, char * chroot_prefix, vac_callback_t cb,
291 rx_thread_done = false;
293 vac_main_t *pm = &vac_main;
295 assert (clib_mem_get_heap ());
297 if (chroot_prefix != NULL)
298 vl_set_memory_root_path (chroot_prefix);
300 if ((rv = vl_client_api_map("/vpe-api"))) {
301 clib_warning ("vl_client_api_map returned %d", rv);
305 if (vl_client_connect(name, 0, rx_qlen) < 0) {
306 vl_client_api_unmap();
311 /* Start the rx queue thread */
312 rv = pthread_create(&pm->rx_thread_handle, NULL, vac_rx_thread_fn, 0);
314 clib_warning("pthread_create returned %d", rv);
315 vl_client_api_unmap();
319 rx_is_running = true;
322 /* Start read timeout thread */
323 rv = pthread_create(&pm->timeout_thread_handle, NULL,
324 vac_timeout_thread_fn, 0);
326 clib_warning("pthread_create returned %d", rv);
327 vl_client_api_unmap();
331 pm->connected_to_vlib = 1;
336 set_timeout (unsigned short timeout)
338 vac_main_t *pm = &vac_main;
339 pthread_mutex_lock(&pm->timeout_lock);
340 read_timeout = timeout;
341 timeout_in_progress = true;
342 timeout_cancelled = false;
343 pthread_cond_signal(&pm->timeout_cv);
344 pthread_mutex_unlock(&pm->timeout_lock);
350 vac_main_t *pm = &vac_main;
351 pthread_mutex_lock(&pm->timeout_lock);
352 timeout_in_progress = false;
353 timeout_cancelled = true;
354 pthread_cond_signal(&pm->timeout_cancel_cv);
355 pthread_mutex_unlock(&pm->timeout_lock);
359 vac_disconnect (void)
361 api_main_t *am = vlibapi_get_main();
362 vac_main_t *pm = &vac_main;
366 if (!pm->connected_to_vlib) return 0;
368 if (pm->rx_thread_handle) {
369 vl_api_rx_thread_exit_t *ep;
370 ep = vl_msg_api_alloc (sizeof (*ep));
371 ep->_vl_msg_id = ntohs(VL_API_RX_THREAD_EXIT);
372 vl_msg_api_send_shmem(am->vl_input_queue, (u8 *)&ep);
374 /* wait (with timeout) until RX thread has finished */
377 gettimeofday(&tv, NULL);
378 ts.tv_sec = tv.tv_sec + 5;
381 pthread_mutex_lock(&pm->queue_lock);
382 if (rx_thread_done == false)
383 rv = pthread_cond_timedwait(&pm->terminate_cv, &pm->queue_lock, &ts);
384 pthread_mutex_unlock(&pm->queue_lock);
386 /* now join so we wait until thread has -really- finished */
388 pthread_cancel(pm->rx_thread_handle);
390 pthread_join(pm->rx_thread_handle, (void **) &junk);
392 if (pm->timeout_thread_handle) {
393 /* cancel, wake then join the timeout thread */
394 pm->timeout_loop = 0;
395 timeout_thread_cancelled = true;
397 pthread_join(pm->timeout_thread_handle, (void **) &junk);
400 vl_client_disconnect();
401 vl_client_api_unmap();
410 vac_read (char **p, int *l, u16 timeout)
413 api_main_t *am = vlibapi_get_main();
414 vac_main_t *pm = &vac_main;
415 vl_api_memclnt_keepalive_t *mp;
416 vl_api_memclnt_keepalive_reply_t *rmp;
420 vl_shmem_hdr_t *shmem_hdr;
422 /* svm_queue_sub(below) returns {-1, -2} */
423 if (!pm->connected_to_vlib) return -3;
427 /* svm_queue_sub(below) returns {-1, -2} */
428 if (am->our_pid == 0) return (-4);
430 /* Poke timeout thread */
432 set_timeout(timeout);
434 q = am->vl_input_queue;
437 rv = svm_queue_sub(q, (u8 *)&msg, SVM_Q_WAIT, 0);
440 VL_MSG_API_UNPOISON((void *)msg);
441 u16 msg_id = ntohs(*((u16 *)msg));
443 case VL_API_RX_THREAD_EXIT:
444 vl_msg_api_free((void *) msg);
446 case VL_API_MEMCLNT_RX_THREAD_SUSPEND:
448 case VL_API_MEMCLNT_READ_TIMEOUT:
450 case VL_API_MEMCLNT_KEEPALIVE:
451 /* Handle an alive-check ping from vpp. */
453 rmp = vl_msg_api_alloc (sizeof (*rmp));
454 clib_memset (rmp, 0, sizeof (*rmp));
455 rmp->_vl_msg_id = ntohs(VL_API_MEMCLNT_KEEPALIVE_REPLY);
456 rmp->context = mp->context;
457 shmem_hdr = am->shmem_hdr;
458 vl_msg_api_send_shmem(shmem_hdr->vl_input_queue, (u8 *)&rmp);
459 vl_msg_api_free((void *) msg);
461 * Python code is blissfully unaware of these pings, so
462 * act as if it never happened...
467 msgbuf = (msgbuf_t *)(((u8 *)msg) - offsetof(msgbuf_t, data));
468 *l = ntohl(msgbuf->data_len);
470 fprintf(stderr, "Unregistered API message: %d\n", msg_id);
478 fprintf(stderr, "Read failed with %d\n", rv);
480 /* Let timeout notification thread know we're done */
489 vl_msg_api_free((void *) msg);
490 /* Client might forget to resume RX thread on failure */
496 * XXX: Makes the assumption that client_index is the first member
498 typedef VL_API_PACKED(struct _vl_api_header {
504 vac_client_index (void)
506 return (vlibapi_get_main()->my_client_index);
510 vac_write (char *p, int l)
513 api_main_t *am = vlibapi_get_main();
514 vl_api_header_t *mp = vl_msg_api_alloc(l);
516 vac_main_t *pm = &vac_main;
518 if (!pm->connected_to_vlib) return -1;
519 if (!mp) return (-1);
522 mp->client_index = vac_client_index();
523 q = am->shmem_hdr->vl_input_queue;
524 rv = svm_queue_add(q, (u8 *)&mp, 0);
526 fprintf(stderr, "vpe_api_write fails: %d\n", rv);
534 vac_get_msg_index (unsigned char * name)
536 return vl_msg_api_get_msg_index (name);
540 vac_msg_table_max_index(void)
544 uword *h = vac_msg_table_get_hash();
545 hash_foreach_pair (hp, h,
547 if (hp->value[0] > max)
555 vac_set_error_handler (vac_error_callback_t cb)
557 assert (clib_mem_get_heap ());
558 if (cb) clib_error_register_handler (cb, 0);
562 * Required if application doesn't use a VPP heap.
565 vac_mem_init (size_t size)
568 clib_mem_init (0, 1 << 30); // default
570 clib_mem_init (0, size);