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"
36 bool timeout_cancelled;
37 bool timeout_in_progress;
41 * Client registers a callback. All messages are sent to the callback.
43 * Client calls blocking read().
44 * Clients are expected to collate events on a queue.
45 * vac_write() -> suspends RX thread
46 * vac_read() -> resumes RX thread
49 #define vl_typedefs /* define message structures */
50 #include <vpp/api/vpe_all_api_h.h>
53 #define vl_endianfun /* define message structures */
54 #include <vpp/api/vpe_all_api_h.h>
57 vlib_main_t vlib_global_main;
58 vlib_main_t **vlib_mains;
62 pthread_t rx_thread_handle;
63 pthread_t timeout_thread_handle;
64 pthread_mutex_t queue_lock;
65 pthread_cond_t suspend_cv;
66 pthread_cond_t resume_cv;
67 pthread_mutex_t timeout_lock;
69 pthread_cond_t timeout_cv;
70 pthread_cond_t timeout_cancel_cv;
71 pthread_cond_t terminate_cv;
75 vac_callback_t vac_callback;
77 bool rx_is_running = false;
78 bool timeout_thread_cancelled = false;
80 /* Set to true to enable memory tracing */
81 bool mem_trace = false;
83 __attribute__((constructor))
85 vac_client_constructor (void)
87 clib_mem_init (0, 1 << 30);
93 heap = clib_mem_get_per_cpu_heap ();
94 h = mheap_header (heap);
95 /* make the main heap thread-safe */
96 h->flags |= MHEAP_FLAG_THREAD_SAFE;
103 __attribute__((destructor))
105 vac_client_destructor (void)
108 fformat(stderr, "TRACE: %s",
110 format_mheap, clib_mem_get_heap (), 1));
117 vac_main_t *pm = &vac_main;
118 clib_memset(pm, 0, sizeof(*pm));
119 pthread_mutex_init(&pm->queue_lock, NULL);
120 pthread_cond_init(&pm->suspend_cv, NULL);
121 pthread_cond_init(&pm->resume_cv, NULL);
122 pthread_mutex_init(&pm->timeout_lock, NULL);
123 pm->timeout_loop = 1;
124 pthread_cond_init(&pm->timeout_cv, NULL);
125 pthread_cond_init(&pm->timeout_cancel_cv, NULL);
126 pthread_cond_init(&pm->terminate_cv, NULL);
132 vac_main_t *pm = &vac_main;
133 pthread_mutex_destroy(&pm->queue_lock);
134 pthread_cond_destroy(&pm->suspend_cv);
135 pthread_cond_destroy(&pm->resume_cv);
136 pthread_mutex_destroy(&pm->timeout_lock);
137 pthread_cond_destroy(&pm->timeout_cv);
138 pthread_cond_destroy(&pm->timeout_cancel_cv);
139 pthread_cond_destroy(&pm->terminate_cv);
140 clib_memset(pm, 0, sizeof(*pm));
144 * Satisfy external references when -lvlib is not available.
146 void vlib_cli_output (struct vlib_main_t * vm, char * fmt, ...)
148 clib_warning ("vlib_cli_output called...");
152 vac_free (void * msg)
154 vl_msg_api_free (msg);
158 vac_api_handler (void *msg)
160 u16 id = ntohs(*((u16 *)msg));
161 msgbuf_t *msgbuf = (msgbuf_t *)(((u8 *)msg) - offsetof(msgbuf_t, data));
162 int l = ntohl(msgbuf->data_len);
164 clib_warning("Message ID %d has wrong length: %d\n", id, l);
166 /* Call Python callback */
167 ASSERT(vac_callback);
168 (vac_callback)(msg, l);
173 vac_rx_thread_fn (void *arg)
176 vl_api_memclnt_keepalive_t *mp;
177 vl_api_memclnt_keepalive_reply_t *rmp;
178 vac_main_t *pm = &vac_main;
179 api_main_t *am = &api_main;
180 vl_shmem_hdr_t *shmem_hdr;
183 q = am->vl_input_queue;
186 while (!svm_queue_sub(q, (u8 *)&msg, SVM_Q_WAIT, 0))
188 u16 id = ntohs(*((u16 *)msg));
190 case VL_API_RX_THREAD_EXIT:
191 vl_msg_api_free((void *) msg);
192 /* signal waiting threads that this thread is about to terminate */
193 pthread_mutex_lock(&pm->queue_lock);
194 pthread_cond_signal(&pm->terminate_cv);
195 pthread_mutex_unlock(&pm->queue_lock);
200 case VL_API_MEMCLNT_RX_THREAD_SUSPEND:
201 vl_msg_api_free((void * )msg);
202 /* Suspend thread and signal reader */
203 pthread_mutex_lock(&pm->queue_lock);
204 pthread_cond_signal(&pm->suspend_cv);
205 /* Wait for the resume signal */
206 pthread_cond_wait (&pm->resume_cv, &pm->queue_lock);
207 pthread_mutex_unlock(&pm->queue_lock);
210 case VL_API_MEMCLNT_READ_TIMEOUT:
211 clib_warning("Received read timeout in async thread\n");
212 vl_msg_api_free((void *) msg);
215 case VL_API_MEMCLNT_KEEPALIVE:
217 rmp = vl_msg_api_alloc (sizeof (*rmp));
218 clib_memset (rmp, 0, sizeof (*rmp));
219 rmp->_vl_msg_id = ntohs(VL_API_MEMCLNT_KEEPALIVE_REPLY);
220 rmp->context = mp->context;
221 shmem_hdr = am->shmem_hdr;
222 vl_msg_api_send_shmem(shmem_hdr->vl_input_queue, (u8 *)&rmp);
223 vl_msg_api_free((void *) msg);
227 vac_api_handler((void *)msg);
233 vac_timeout_thread_fn (void *arg)
235 vl_api_memclnt_read_timeout_t *ep;
236 vac_main_t *pm = &vac_main;
237 api_main_t *am = &api_main;
242 while (pm->timeout_loop)
245 pthread_mutex_lock(&pm->timeout_lock);
246 while (!timeout_in_progress)
247 pthread_cond_wait (&pm->timeout_cv, &pm->timeout_lock);
250 gettimeofday(&tv, NULL);
251 ts.tv_sec = tv.tv_sec + read_timeout;
254 if (!timeout_cancelled) {
255 rv = pthread_cond_timedwait (&pm->timeout_cancel_cv,
256 &pm->timeout_lock, &ts);
257 if (rv == ETIMEDOUT && !timeout_thread_cancelled) {
258 ep = vl_msg_api_alloc (sizeof (*ep));
259 ep->_vl_msg_id = ntohs(VL_API_MEMCLNT_READ_TIMEOUT);
260 vl_msg_api_send_shmem(am->vl_input_queue, (u8 *)&ep);
264 pthread_mutex_unlock(&pm->timeout_lock);
270 vac_rx_suspend (void)
272 api_main_t *am = &api_main;
273 vac_main_t *pm = &vac_main;
274 vl_api_memclnt_rx_thread_suspend_t *ep;
276 if (!pm->rx_thread_handle) return;
277 pthread_mutex_lock(&pm->queue_lock);
280 ep = vl_msg_api_alloc (sizeof (*ep));
281 ep->_vl_msg_id = ntohs(VL_API_MEMCLNT_RX_THREAD_SUSPEND);
282 vl_msg_api_send_shmem(am->vl_input_queue, (u8 *)&ep);
283 /* Wait for RX thread to tell us it has suspended */
284 pthread_cond_wait(&pm->suspend_cv, &pm->queue_lock);
285 rx_is_running = false;
287 pthread_mutex_unlock(&pm->queue_lock);
293 vac_main_t *pm = &vac_main;
294 if (!pm->rx_thread_handle) return;
295 pthread_mutex_lock(&pm->queue_lock);
296 if (rx_is_running) goto unlock;
297 pthread_cond_signal(&pm->resume_cv);
298 rx_is_running = true;
300 pthread_mutex_unlock(&pm->queue_lock);
304 vac_msg_table_get_hash (void)
306 api_main_t *am = &api_main;
307 return (am->msg_index_by_name_and_crc);
311 vac_msg_table_size(void)
313 api_main_t *am = &api_main;
314 return hash_elts(am->msg_index_by_name_and_crc);
318 vac_connect (char * name, char * chroot_prefix, vac_callback_t cb,
322 vac_main_t *pm = &vac_main;
325 if (chroot_prefix != NULL)
326 vl_set_memory_root_path (chroot_prefix);
328 if ((rv = vl_client_api_map("/vpe-api"))) {
329 clib_warning ("vl_client_api_map returned %d", rv);
333 if (vl_client_connect(name, 0, rx_qlen) < 0) {
334 vl_client_api_unmap();
339 /* Start the rx queue thread */
340 rv = pthread_create(&pm->rx_thread_handle, NULL, vac_rx_thread_fn, 0);
342 clib_warning("pthread_create returned %d", rv);
343 vl_client_api_unmap();
347 rx_is_running = true;
350 /* Start read timeout thread */
351 rv = pthread_create(&pm->timeout_thread_handle, NULL,
352 vac_timeout_thread_fn, 0);
354 clib_warning("pthread_create returned %d", rv);
355 vl_client_api_unmap();
359 pm->connected_to_vlib = 1;
364 set_timeout (unsigned short timeout)
366 vac_main_t *pm = &vac_main;
367 pthread_mutex_lock(&pm->timeout_lock);
368 read_timeout = timeout;
369 timeout_in_progress = true;
370 timeout_cancelled = false;
371 pthread_cond_signal(&pm->timeout_cv);
372 pthread_mutex_unlock(&pm->timeout_lock);
378 vac_main_t *pm = &vac_main;
379 pthread_mutex_lock(&pm->timeout_lock);
380 timeout_in_progress = false;
381 timeout_cancelled = true;
382 pthread_cond_signal(&pm->timeout_cancel_cv);
383 pthread_mutex_unlock(&pm->timeout_lock);
387 vac_disconnect (void)
389 api_main_t *am = &api_main;
390 vac_main_t *pm = &vac_main;
393 if (!pm->connected_to_vlib) return 0;
395 if (pm->rx_thread_handle) {
396 vl_api_rx_thread_exit_t *ep;
397 ep = vl_msg_api_alloc (sizeof (*ep));
398 ep->_vl_msg_id = ntohs(VL_API_RX_THREAD_EXIT);
399 vl_msg_api_send_shmem(am->vl_input_queue, (u8 *)&ep);
401 /* wait (with timeout) until RX thread has finished */
404 gettimeofday(&tv, NULL);
405 ts.tv_sec = tv.tv_sec + 5;
407 pthread_mutex_lock(&pm->queue_lock);
408 int rv = pthread_cond_timedwait(&pm->terminate_cv, &pm->queue_lock, &ts);
409 pthread_mutex_unlock(&pm->queue_lock);
410 /* now join so we wait until thread has -really- finished */
412 pthread_cancel(pm->rx_thread_handle);
414 pthread_join(pm->rx_thread_handle, (void **) &junk);
416 if (pm->timeout_thread_handle) {
417 /* cancel, wake then join the timeout thread */
418 pm->timeout_loop = 0;
419 timeout_thread_cancelled = true;
421 pthread_join(pm->timeout_thread_handle, (void **) &junk);
424 vl_client_disconnect();
425 vl_client_api_unmap();
434 vac_read (char **p, int *l, u16 timeout)
437 api_main_t *am = &api_main;
438 vac_main_t *pm = &vac_main;
439 vl_api_memclnt_keepalive_t *mp;
440 vl_api_memclnt_keepalive_reply_t *rmp;
444 vl_shmem_hdr_t *shmem_hdr;
446 if (!pm->connected_to_vlib) return -1;
450 if (am->our_pid == 0) return (-1);
452 /* Poke timeout thread */
454 set_timeout(timeout);
456 q = am->vl_input_queue;
459 rv = svm_queue_sub(q, (u8 *)&msg, SVM_Q_WAIT, 0);
462 u16 msg_id = ntohs(*((u16 *)msg));
464 case VL_API_RX_THREAD_EXIT:
465 vl_msg_api_free((void *) msg);
467 case VL_API_MEMCLNT_RX_THREAD_SUSPEND:
469 case VL_API_MEMCLNT_READ_TIMEOUT:
471 case VL_API_MEMCLNT_KEEPALIVE:
472 /* Handle an alive-check ping from vpp. */
474 rmp = vl_msg_api_alloc (sizeof (*rmp));
475 clib_memset (rmp, 0, sizeof (*rmp));
476 rmp->_vl_msg_id = ntohs(VL_API_MEMCLNT_KEEPALIVE_REPLY);
477 rmp->context = mp->context;
478 shmem_hdr = am->shmem_hdr;
479 vl_msg_api_send_shmem(shmem_hdr->vl_input_queue, (u8 *)&rmp);
480 vl_msg_api_free((void *) msg);
482 * Python code is blissfully unaware of these pings, so
483 * act as if it never happened...
488 msgbuf = (msgbuf_t *)(((u8 *)msg) - offsetof(msgbuf_t, data));
489 *l = ntohl(msgbuf->data_len);
491 fprintf(stderr, "Unregistered API message: %d\n", msg_id);
499 fprintf(stderr, "Read failed with %d\n", rv);
501 /* Let timeout notification thread know we're done */
510 vl_msg_api_free((void *) msg);
511 /* Client might forget to resume RX thread on failure */
517 * XXX: Makes the assumption that client_index is the first member
519 typedef VL_API_PACKED(struct _vl_api_header {
525 vac_client_index (void)
527 return (api_main.my_client_index);
531 vac_write (char *p, int l)
534 api_main_t *am = &api_main;
535 vl_api_header_t *mp = vl_msg_api_alloc(l);
537 vac_main_t *pm = &vac_main;
539 if (!pm->connected_to_vlib) return -1;
540 if (!mp) return (-1);
543 mp->client_index = vac_client_index();
544 q = am->shmem_hdr->vl_input_queue;
545 rv = svm_queue_add(q, (u8 *)&mp, 0);
547 fprintf(stderr, "vpe_api_write fails: %d\n", rv);
555 vac_get_msg_index (unsigned char * name)
557 return vl_msg_api_get_msg_index (name);
561 vac_msg_table_max_index(void)
565 uword *h = vac_msg_table_get_hash();
566 hash_foreach_pair (hp, h,
568 if (hp->value[0] > max)
576 vac_set_error_handler (vac_error_callback_t cb)
578 if (cb) clib_error_register_handler (cb, 0);