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 <vlibmemory/memclnt.api_enum.h>
34 #include <vlibmemory/memclnt.api_types.h>
36 #include "vppapiclient.h"
38 bool timeout_cancelled;
39 bool timeout_in_progress;
44 * Client registers a callback. All messages are sent to the callback.
46 * Client calls blocking read().
47 * Clients are expected to collate events on a queue.
48 * vac_write() -> suspends RX thread
49 * vac_read() -> resumes RX thread
54 pthread_t rx_thread_handle;
55 pthread_t timeout_thread_handle;
56 pthread_mutex_t queue_lock;
57 pthread_cond_t suspend_cv;
58 pthread_cond_t resume_cv;
59 pthread_mutex_t timeout_lock;
61 pthread_cond_t timeout_cv;
62 pthread_cond_t timeout_cancel_cv;
63 pthread_cond_t terminate_cv;
67 vac_callback_t vac_callback;
69 bool rx_is_running = false;
70 bool timeout_thread_cancelled = false;
72 /* Only ever allocate one heap */
73 bool mem_initialized = false;
78 vac_main_t *pm = &vac_main;
79 clib_memset(pm, 0, sizeof(*pm));
80 pthread_mutex_init(&pm->queue_lock, NULL);
81 pthread_cond_init(&pm->suspend_cv, NULL);
82 pthread_cond_init(&pm->resume_cv, NULL);
83 pthread_mutex_init(&pm->timeout_lock, NULL);
85 pthread_cond_init(&pm->timeout_cv, NULL);
86 pthread_cond_init(&pm->timeout_cancel_cv, NULL);
87 pthread_cond_init(&pm->terminate_cv, NULL);
93 vac_main_t *pm = &vac_main;
94 pthread_mutex_destroy(&pm->queue_lock);
95 pthread_cond_destroy(&pm->suspend_cv);
96 pthread_cond_destroy(&pm->resume_cv);
97 pthread_mutex_destroy(&pm->timeout_lock);
98 pthread_cond_destroy(&pm->timeout_cv);
99 pthread_cond_destroy(&pm->timeout_cancel_cv);
100 pthread_cond_destroy(&pm->terminate_cv);
101 clib_memset(pm, 0, sizeof(*pm));
105 * Satisfy external references when -lvlib is not available.
107 void vlib_cli_output (struct vlib_main_t * vm, char * fmt, ...)
109 clib_warning ("vlib_cli_output called...");
113 vac_free (void * msg)
115 vl_msg_api_free (msg);
119 vac_api_handler (void *msg)
121 u16 id = ntohs(*((u16 *)msg));
122 msgbuf_t *msgbuf = (msgbuf_t *)(((u8 *)msg) - offsetof(msgbuf_t, data));
123 int l = ntohl(msgbuf->data_len);
125 clib_warning("Message ID %d has wrong length: %d\n", id, l);
127 /* Call Python callback */
128 ASSERT(vac_callback);
129 (vac_callback)(msg, l);
134 vac_rx_thread_fn (void *arg)
137 vl_api_memclnt_keepalive_t *mp;
138 vl_api_memclnt_keepalive_reply_t *rmp;
139 vac_main_t *pm = &vac_main;
140 api_main_t *am = vlibapi_get_main();
141 vl_shmem_hdr_t *shmem_hdr;
144 q = am->vl_input_queue;
147 while (!svm_queue_sub(q, (u8 *)&msg, SVM_Q_WAIT, 0))
149 VL_MSG_API_UNPOISON((void *)msg);
150 u16 id = ntohs(*((u16 *)msg));
152 case VL_API_RX_THREAD_EXIT:
153 vl_msg_api_free((void *) msg);
154 /* signal waiting threads that this thread is about to terminate */
155 pthread_mutex_lock(&pm->queue_lock);
156 rx_thread_done = true;
157 pthread_cond_signal(&pm->terminate_cv);
158 pthread_mutex_unlock(&pm->queue_lock);
163 case VL_API_MEMCLNT_RX_THREAD_SUSPEND:
164 vl_msg_api_free((void * )msg);
165 /* Suspend thread and signal reader */
166 pthread_mutex_lock(&pm->queue_lock);
167 pthread_cond_signal(&pm->suspend_cv);
168 /* Wait for the resume signal */
169 pthread_cond_wait (&pm->resume_cv, &pm->queue_lock);
170 pthread_mutex_unlock(&pm->queue_lock);
173 case VL_API_MEMCLNT_READ_TIMEOUT:
174 clib_warning("Received read timeout in async thread\n");
175 vl_msg_api_free((void *) msg);
178 case VL_API_MEMCLNT_KEEPALIVE:
180 rmp = vl_msg_api_alloc (sizeof (*rmp));
181 clib_memset (rmp, 0, sizeof (*rmp));
182 rmp->_vl_msg_id = ntohs(VL_API_MEMCLNT_KEEPALIVE_REPLY);
183 rmp->context = mp->context;
184 shmem_hdr = am->shmem_hdr;
185 vl_msg_api_send_shmem(shmem_hdr->vl_input_queue, (u8 *)&rmp);
186 vl_msg_api_free((void *) msg);
190 vac_api_handler((void *)msg);
196 vac_timeout_thread_fn (void *arg)
198 vl_api_memclnt_read_timeout_t *ep;
199 vac_main_t *pm = &vac_main;
200 api_main_t *am = vlibapi_get_main();
205 while (pm->timeout_loop)
208 pthread_mutex_lock(&pm->timeout_lock);
209 while (!timeout_in_progress)
210 pthread_cond_wait (&pm->timeout_cv, &pm->timeout_lock);
213 gettimeofday(&tv, NULL);
214 ts.tv_sec = tv.tv_sec + read_timeout;
217 if (!timeout_cancelled) {
218 rv = pthread_cond_timedwait (&pm->timeout_cancel_cv,
219 &pm->timeout_lock, &ts);
220 if (rv == ETIMEDOUT && !timeout_thread_cancelled) {
221 ep = vl_msg_api_alloc (sizeof (*ep));
222 ep->_vl_msg_id = ntohs(VL_API_MEMCLNT_READ_TIMEOUT);
223 vl_msg_api_send_shmem(am->vl_input_queue, (u8 *)&ep);
227 pthread_mutex_unlock(&pm->timeout_lock);
233 vac_rx_suspend (void)
235 api_main_t *am = vlibapi_get_main();
236 vac_main_t *pm = &vac_main;
237 vl_api_memclnt_rx_thread_suspend_t *ep;
239 if (!pm->rx_thread_handle) return;
240 pthread_mutex_lock(&pm->queue_lock);
243 ep = vl_msg_api_alloc (sizeof (*ep));
244 ep->_vl_msg_id = ntohs(VL_API_MEMCLNT_RX_THREAD_SUSPEND);
245 vl_msg_api_send_shmem(am->vl_input_queue, (u8 *)&ep);
246 /* Wait for RX thread to tell us it has suspended */
247 pthread_cond_wait(&pm->suspend_cv, &pm->queue_lock);
248 rx_is_running = false;
250 pthread_mutex_unlock(&pm->queue_lock);
256 vac_main_t *pm = &vac_main;
257 if (!pm->rx_thread_handle) return;
258 pthread_mutex_lock(&pm->queue_lock);
259 if (rx_is_running) goto unlock;
260 pthread_cond_signal(&pm->resume_cv);
261 rx_is_running = true;
263 pthread_mutex_unlock(&pm->queue_lock);
267 vac_msg_table_get_hash (void)
269 api_main_t *am = vlibapi_get_main();
270 return (am->msg_index_by_name_and_crc);
274 vac_msg_table_size(void)
276 api_main_t *am = vlibapi_get_main();
277 return hash_elts(am->msg_index_by_name_and_crc);
281 vac_connect (char * name, char * chroot_prefix, vac_callback_t cb,
284 rx_thread_done = false;
286 vac_main_t *pm = &vac_main;
288 assert (clib_mem_get_heap ());
290 if (chroot_prefix != NULL)
291 vl_set_memory_root_path (chroot_prefix);
293 if ((rv = vl_client_api_map("/vpe-api"))) {
294 clib_warning ("vl_client_api_map returned %d", rv);
298 if (vl_client_connect(name, 0, rx_qlen) < 0) {
299 vl_client_api_unmap();
304 /* Start the rx queue thread */
305 rv = pthread_create(&pm->rx_thread_handle, NULL, vac_rx_thread_fn, 0);
307 clib_warning("pthread_create returned %d", rv);
308 vl_client_api_unmap();
312 rx_is_running = true;
315 /* Start read timeout thread */
316 rv = pthread_create(&pm->timeout_thread_handle, NULL,
317 vac_timeout_thread_fn, 0);
319 clib_warning("pthread_create returned %d", rv);
320 vl_client_api_unmap();
324 pm->connected_to_vlib = 1;
329 set_timeout (unsigned short timeout)
331 vac_main_t *pm = &vac_main;
332 pthread_mutex_lock(&pm->timeout_lock);
333 read_timeout = timeout;
334 timeout_in_progress = true;
335 timeout_cancelled = false;
336 pthread_cond_signal(&pm->timeout_cv);
337 pthread_mutex_unlock(&pm->timeout_lock);
343 vac_main_t *pm = &vac_main;
344 pthread_mutex_lock(&pm->timeout_lock);
345 timeout_in_progress = false;
346 timeout_cancelled = true;
347 pthread_cond_signal(&pm->timeout_cancel_cv);
348 pthread_mutex_unlock(&pm->timeout_lock);
352 vac_disconnect (void)
354 api_main_t *am = vlibapi_get_main();
355 vac_main_t *pm = &vac_main;
359 if (!pm->connected_to_vlib) return 0;
361 if (pm->rx_thread_handle) {
362 vl_api_rx_thread_exit_t *ep;
363 ep = vl_msg_api_alloc (sizeof (*ep));
364 ep->_vl_msg_id = ntohs(VL_API_RX_THREAD_EXIT);
365 vl_msg_api_send_shmem(am->vl_input_queue, (u8 *)&ep);
367 /* wait (with timeout) until RX thread has finished */
370 gettimeofday(&tv, NULL);
371 ts.tv_sec = tv.tv_sec + 5;
374 pthread_mutex_lock(&pm->queue_lock);
375 if (rx_thread_done == false)
376 rv = pthread_cond_timedwait(&pm->terminate_cv, &pm->queue_lock, &ts);
377 pthread_mutex_unlock(&pm->queue_lock);
379 /* now join so we wait until thread has -really- finished */
381 pthread_cancel(pm->rx_thread_handle);
383 pthread_join(pm->rx_thread_handle, (void **) &junk);
385 if (pm->timeout_thread_handle) {
386 /* cancel, wake then join the timeout thread */
387 pm->timeout_loop = 0;
388 timeout_thread_cancelled = true;
390 pthread_join(pm->timeout_thread_handle, (void **) &junk);
393 vl_client_disconnect();
394 vl_client_api_unmap();
403 vac_read (char **p, int *l, u16 timeout)
406 api_main_t *am = vlibapi_get_main();
407 vac_main_t *pm = &vac_main;
408 vl_api_memclnt_keepalive_t *mp;
409 vl_api_memclnt_keepalive_reply_t *rmp;
413 vl_shmem_hdr_t *shmem_hdr;
415 /* svm_queue_sub(below) returns {-1, -2} */
416 if (!pm->connected_to_vlib)
417 return VAC_NOT_CONNECTED;
421 /* svm_queue_sub(below) returns {-1, -2} */
422 if (am->our_pid == 0)
423 return (VAC_SHM_NOT_READY);
425 /* Poke timeout thread */
427 set_timeout(timeout);
429 q = am->vl_input_queue;
432 rv = svm_queue_sub(q, (u8 *)&msg, SVM_Q_WAIT, 0);
435 VL_MSG_API_UNPOISON((void *)msg);
436 u16 msg_id = ntohs(*((u16 *)msg));
438 case VL_API_RX_THREAD_EXIT:
439 vl_msg_api_free((void *) msg);
441 case VL_API_MEMCLNT_RX_THREAD_SUSPEND:
443 case VL_API_MEMCLNT_READ_TIMEOUT:
445 case VL_API_MEMCLNT_KEEPALIVE:
446 /* Handle an alive-check ping from vpp. */
448 rmp = vl_msg_api_alloc (sizeof (*rmp));
449 clib_memset (rmp, 0, sizeof (*rmp));
450 rmp->_vl_msg_id = ntohs(VL_API_MEMCLNT_KEEPALIVE_REPLY);
451 rmp->context = mp->context;
452 shmem_hdr = am->shmem_hdr;
453 vl_msg_api_send_shmem(shmem_hdr->vl_input_queue, (u8 *)&rmp);
454 vl_msg_api_free((void *) msg);
456 * Python code is blissfully unaware of these pings, so
457 * act as if it never happened...
462 msgbuf = (msgbuf_t *)(((u8 *)msg) - offsetof(msgbuf_t, data));
463 *l = ntohl(msgbuf->data_len);
465 fprintf(stderr, "Unregistered API message: %d\n", msg_id);
473 fprintf(stderr, "Read failed with %d\n", rv);
475 /* Let timeout notification thread know we're done */
484 vl_msg_api_free((void *) msg);
485 /* Client might forget to resume RX thread on failure */
491 * XXX: Makes the assumption that client_index is the first member
493 typedef struct _vl_api_header
497 } __attribute__ ((packed)) vl_api_header_t;
500 vac_client_index (void)
502 return (vlibapi_get_main()->my_client_index);
506 vac_write (char *p, int l)
509 api_main_t *am = vlibapi_get_main();
510 vl_api_header_t *mp = vl_msg_api_alloc(l);
512 vac_main_t *pm = &vac_main;
514 if (!pm->connected_to_vlib)
515 return VAC_NOT_CONNECTED;
516 if (!mp) return (-1);
519 mp->client_index = vac_client_index();
520 q = am->shmem_hdr->vl_input_queue;
521 rv = svm_queue_add(q, (u8 *)&mp, 0);
523 fprintf(stderr, "vpe_api_write fails: %d\n", rv);
531 vac_get_msg_index (char * name)
533 return vl_msg_api_get_msg_index ((u8 *)name);
537 vac_msg_table_max_index(void)
541 uword *h = vac_msg_table_get_hash();
542 hash_foreach_pair (hp, h,
544 if (hp->value[0] > max)
552 vac_set_error_handler (vac_error_callback_t cb)
554 assert (clib_mem_get_heap ());
555 if (cb) clib_error_register_handler (cb, 0);
559 * Required if application doesn't use a VPP heap.
562 vac_mem_init (size_t size)
567 clib_mem_init (0, 1 << 30); // default
569 clib_mem_init (0, size);
570 mem_initialized = true;