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;
42 * Client registers a callback. All messages are sent to the callback.
44 * Client calls blocking read().
45 * Clients are expected to collate events on a queue.
46 * vac_write() -> suspends RX thread
47 * vac_read() -> resumes RX thread
50 #define vl_typedefs /* define message structures */
51 #include <vpp/api/vpe_all_api_h.h>
54 #define vl_endianfun /* define message structures */
55 #include <vpp/api/vpe_all_api_h.h>
58 vlib_main_t vlib_global_main;
59 vlib_main_t **vlib_mains;
63 pthread_t rx_thread_handle;
64 pthread_t timeout_thread_handle;
65 pthread_mutex_t queue_lock;
66 pthread_cond_t suspend_cv;
67 pthread_cond_t resume_cv;
68 pthread_mutex_t timeout_lock;
70 pthread_cond_t timeout_cv;
71 pthread_cond_t timeout_cancel_cv;
72 pthread_cond_t terminate_cv;
76 vac_callback_t vac_callback;
78 bool rx_is_running = false;
79 bool timeout_thread_cancelled = false;
81 /* Set to true to enable memory tracing */
82 bool mem_trace = false;
84 __attribute__((constructor))
86 vac_client_constructor (void)
88 clib_mem_init (0, 1 << 30);
94 heap = clib_mem_get_per_cpu_heap ();
95 h = mheap_header (heap);
96 /* make the main heap thread-safe */
97 h->flags |= MHEAP_FLAG_THREAD_SAFE;
104 __attribute__((destructor))
106 vac_client_destructor (void)
109 fformat(stderr, "TRACE: %s",
111 format_mheap, clib_mem_get_heap (), 1));
118 vac_main_t *pm = &vac_main;
119 clib_memset(pm, 0, sizeof(*pm));
120 pthread_mutex_init(&pm->queue_lock, NULL);
121 pthread_cond_init(&pm->suspend_cv, NULL);
122 pthread_cond_init(&pm->resume_cv, NULL);
123 pthread_mutex_init(&pm->timeout_lock, NULL);
124 pm->timeout_loop = 1;
125 pthread_cond_init(&pm->timeout_cv, NULL);
126 pthread_cond_init(&pm->timeout_cancel_cv, NULL);
127 pthread_cond_init(&pm->terminate_cv, NULL);
133 vac_main_t *pm = &vac_main;
134 pthread_mutex_destroy(&pm->queue_lock);
135 pthread_cond_destroy(&pm->suspend_cv);
136 pthread_cond_destroy(&pm->resume_cv);
137 pthread_mutex_destroy(&pm->timeout_lock);
138 pthread_cond_destroy(&pm->timeout_cv);
139 pthread_cond_destroy(&pm->timeout_cancel_cv);
140 pthread_cond_destroy(&pm->terminate_cv);
141 clib_memset(pm, 0, sizeof(*pm));
145 * Satisfy external references when -lvlib is not available.
147 void vlib_cli_output (struct vlib_main_t * vm, char * fmt, ...)
149 clib_warning ("vlib_cli_output called...");
153 vac_free (void * msg)
155 vl_msg_api_free (msg);
159 vac_api_handler (void *msg)
161 u16 id = ntohs(*((u16 *)msg));
162 msgbuf_t *msgbuf = (msgbuf_t *)(((u8 *)msg) - offsetof(msgbuf_t, data));
163 int l = ntohl(msgbuf->data_len);
165 clib_warning("Message ID %d has wrong length: %d\n", id, l);
167 /* Call Python callback */
168 ASSERT(vac_callback);
169 (vac_callback)(msg, l);
174 vac_rx_thread_fn (void *arg)
177 vl_api_memclnt_keepalive_t *mp;
178 vl_api_memclnt_keepalive_reply_t *rmp;
179 vac_main_t *pm = &vac_main;
180 api_main_t *am = vlibapi_get_main();
181 vl_shmem_hdr_t *shmem_hdr;
184 q = am->vl_input_queue;
187 while (!svm_queue_sub(q, (u8 *)&msg, SVM_Q_WAIT, 0))
189 VL_MSG_API_UNPOISON((void *)msg);
190 u16 id = ntohs(*((u16 *)msg));
192 case VL_API_RX_THREAD_EXIT:
193 vl_msg_api_free((void *) msg);
194 /* signal waiting threads that this thread is about to terminate */
195 pthread_mutex_lock(&pm->queue_lock);
196 rx_thread_done = true;
197 pthread_cond_signal(&pm->terminate_cv);
198 pthread_mutex_unlock(&pm->queue_lock);
203 case VL_API_MEMCLNT_RX_THREAD_SUSPEND:
204 vl_msg_api_free((void * )msg);
205 /* Suspend thread and signal reader */
206 pthread_mutex_lock(&pm->queue_lock);
207 pthread_cond_signal(&pm->suspend_cv);
208 /* Wait for the resume signal */
209 pthread_cond_wait (&pm->resume_cv, &pm->queue_lock);
210 pthread_mutex_unlock(&pm->queue_lock);
213 case VL_API_MEMCLNT_READ_TIMEOUT:
214 clib_warning("Received read timeout in async thread\n");
215 vl_msg_api_free((void *) msg);
218 case VL_API_MEMCLNT_KEEPALIVE:
220 rmp = vl_msg_api_alloc (sizeof (*rmp));
221 clib_memset (rmp, 0, sizeof (*rmp));
222 rmp->_vl_msg_id = ntohs(VL_API_MEMCLNT_KEEPALIVE_REPLY);
223 rmp->context = mp->context;
224 shmem_hdr = am->shmem_hdr;
225 vl_msg_api_send_shmem(shmem_hdr->vl_input_queue, (u8 *)&rmp);
226 vl_msg_api_free((void *) msg);
230 vac_api_handler((void *)msg);
236 vac_timeout_thread_fn (void *arg)
238 vl_api_memclnt_read_timeout_t *ep;
239 vac_main_t *pm = &vac_main;
240 api_main_t *am = vlibapi_get_main();
245 while (pm->timeout_loop)
248 pthread_mutex_lock(&pm->timeout_lock);
249 while (!timeout_in_progress)
250 pthread_cond_wait (&pm->timeout_cv, &pm->timeout_lock);
253 gettimeofday(&tv, NULL);
254 ts.tv_sec = tv.tv_sec + read_timeout;
257 if (!timeout_cancelled) {
258 rv = pthread_cond_timedwait (&pm->timeout_cancel_cv,
259 &pm->timeout_lock, &ts);
260 if (rv == ETIMEDOUT && !timeout_thread_cancelled) {
261 ep = vl_msg_api_alloc (sizeof (*ep));
262 ep->_vl_msg_id = ntohs(VL_API_MEMCLNT_READ_TIMEOUT);
263 vl_msg_api_send_shmem(am->vl_input_queue, (u8 *)&ep);
267 pthread_mutex_unlock(&pm->timeout_lock);
273 vac_rx_suspend (void)
275 api_main_t *am = vlibapi_get_main();
276 vac_main_t *pm = &vac_main;
277 vl_api_memclnt_rx_thread_suspend_t *ep;
279 if (!pm->rx_thread_handle) return;
280 pthread_mutex_lock(&pm->queue_lock);
283 ep = vl_msg_api_alloc (sizeof (*ep));
284 ep->_vl_msg_id = ntohs(VL_API_MEMCLNT_RX_THREAD_SUSPEND);
285 vl_msg_api_send_shmem(am->vl_input_queue, (u8 *)&ep);
286 /* Wait for RX thread to tell us it has suspended */
287 pthread_cond_wait(&pm->suspend_cv, &pm->queue_lock);
288 rx_is_running = false;
290 pthread_mutex_unlock(&pm->queue_lock);
296 vac_main_t *pm = &vac_main;
297 if (!pm->rx_thread_handle) return;
298 pthread_mutex_lock(&pm->queue_lock);
299 if (rx_is_running) goto unlock;
300 pthread_cond_signal(&pm->resume_cv);
301 rx_is_running = true;
303 pthread_mutex_unlock(&pm->queue_lock);
307 vac_msg_table_get_hash (void)
309 api_main_t *am = vlibapi_get_main();
310 return (am->msg_index_by_name_and_crc);
314 vac_msg_table_size(void)
316 api_main_t *am = vlibapi_get_main();
317 return hash_elts(am->msg_index_by_name_and_crc);
321 vac_connect (char * name, char * chroot_prefix, vac_callback_t cb,
324 rx_thread_done = false;
326 vac_main_t *pm = &vac_main;
329 if (chroot_prefix != NULL)
330 vl_set_memory_root_path (chroot_prefix);
332 if ((rv = vl_client_api_map("/vpe-api"))) {
333 clib_warning ("vl_client_api_map returned %d", rv);
337 if (vl_client_connect(name, 0, rx_qlen) < 0) {
338 vl_client_api_unmap();
343 /* Start the rx queue thread */
344 rv = pthread_create(&pm->rx_thread_handle, NULL, vac_rx_thread_fn, 0);
346 clib_warning("pthread_create returned %d", rv);
347 vl_client_api_unmap();
351 rx_is_running = true;
354 /* Start read timeout thread */
355 rv = pthread_create(&pm->timeout_thread_handle, NULL,
356 vac_timeout_thread_fn, 0);
358 clib_warning("pthread_create returned %d", rv);
359 vl_client_api_unmap();
363 pm->connected_to_vlib = 1;
368 set_timeout (unsigned short timeout)
370 vac_main_t *pm = &vac_main;
371 pthread_mutex_lock(&pm->timeout_lock);
372 read_timeout = timeout;
373 timeout_in_progress = true;
374 timeout_cancelled = false;
375 pthread_cond_signal(&pm->timeout_cv);
376 pthread_mutex_unlock(&pm->timeout_lock);
382 vac_main_t *pm = &vac_main;
383 pthread_mutex_lock(&pm->timeout_lock);
384 timeout_in_progress = false;
385 timeout_cancelled = true;
386 pthread_cond_signal(&pm->timeout_cancel_cv);
387 pthread_mutex_unlock(&pm->timeout_lock);
391 vac_disconnect (void)
393 api_main_t *am = vlibapi_get_main();
394 vac_main_t *pm = &vac_main;
398 if (!pm->connected_to_vlib) return 0;
400 if (pm->rx_thread_handle) {
401 vl_api_rx_thread_exit_t *ep;
402 ep = vl_msg_api_alloc (sizeof (*ep));
403 ep->_vl_msg_id = ntohs(VL_API_RX_THREAD_EXIT);
404 vl_msg_api_send_shmem(am->vl_input_queue, (u8 *)&ep);
406 /* wait (with timeout) until RX thread has finished */
409 gettimeofday(&tv, NULL);
410 ts.tv_sec = tv.tv_sec + 5;
413 pthread_mutex_lock(&pm->queue_lock);
414 if (rx_thread_done == false)
415 rv = pthread_cond_timedwait(&pm->terminate_cv, &pm->queue_lock, &ts);
416 pthread_mutex_unlock(&pm->queue_lock);
418 /* now join so we wait until thread has -really- finished */
420 pthread_cancel(pm->rx_thread_handle);
422 pthread_join(pm->rx_thread_handle, (void **) &junk);
424 if (pm->timeout_thread_handle) {
425 /* cancel, wake then join the timeout thread */
426 pm->timeout_loop = 0;
427 timeout_thread_cancelled = true;
429 pthread_join(pm->timeout_thread_handle, (void **) &junk);
432 vl_client_disconnect();
433 vl_client_api_unmap();
442 vac_read (char **p, int *l, u16 timeout)
445 api_main_t *am = vlibapi_get_main();
446 vac_main_t *pm = &vac_main;
447 vl_api_memclnt_keepalive_t *mp;
448 vl_api_memclnt_keepalive_reply_t *rmp;
452 vl_shmem_hdr_t *shmem_hdr;
454 /* svm_queue_sub(below) returns {-1, -2} */
455 if (!pm->connected_to_vlib) return -3;
459 /* svm_queue_sub(below) returns {-1, -2} */
460 if (am->our_pid == 0) return (-4);
462 /* Poke timeout thread */
464 set_timeout(timeout);
466 q = am->vl_input_queue;
469 rv = svm_queue_sub(q, (u8 *)&msg, SVM_Q_WAIT, 0);
472 VL_MSG_API_UNPOISON((void *)msg);
473 u16 msg_id = ntohs(*((u16 *)msg));
475 case VL_API_RX_THREAD_EXIT:
476 vl_msg_api_free((void *) msg);
478 case VL_API_MEMCLNT_RX_THREAD_SUSPEND:
480 case VL_API_MEMCLNT_READ_TIMEOUT:
482 case VL_API_MEMCLNT_KEEPALIVE:
483 /* Handle an alive-check ping from vpp. */
485 rmp = vl_msg_api_alloc (sizeof (*rmp));
486 clib_memset (rmp, 0, sizeof (*rmp));
487 rmp->_vl_msg_id = ntohs(VL_API_MEMCLNT_KEEPALIVE_REPLY);
488 rmp->context = mp->context;
489 shmem_hdr = am->shmem_hdr;
490 vl_msg_api_send_shmem(shmem_hdr->vl_input_queue, (u8 *)&rmp);
491 vl_msg_api_free((void *) msg);
493 * Python code is blissfully unaware of these pings, so
494 * act as if it never happened...
499 msgbuf = (msgbuf_t *)(((u8 *)msg) - offsetof(msgbuf_t, data));
500 *l = ntohl(msgbuf->data_len);
502 fprintf(stderr, "Unregistered API message: %d\n", msg_id);
510 fprintf(stderr, "Read failed with %d\n", rv);
512 /* Let timeout notification thread know we're done */
521 vl_msg_api_free((void *) msg);
522 /* Client might forget to resume RX thread on failure */
528 * XXX: Makes the assumption that client_index is the first member
530 typedef VL_API_PACKED(struct _vl_api_header {
536 vac_client_index (void)
538 return (vlibapi_get_main()->my_client_index);
542 vac_write (char *p, int l)
545 api_main_t *am = vlibapi_get_main();
546 vl_api_header_t *mp = vl_msg_api_alloc(l);
548 vac_main_t *pm = &vac_main;
550 if (!pm->connected_to_vlib) return -1;
551 if (!mp) return (-1);
554 mp->client_index = vac_client_index();
555 q = am->shmem_hdr->vl_input_queue;
556 rv = svm_queue_add(q, (u8 *)&mp, 0);
558 fprintf(stderr, "vpe_api_write fails: %d\n", rv);
566 vac_get_msg_index (unsigned char * name)
568 return vl_msg_api_get_msg_index (name);
572 vac_msg_table_max_index(void)
576 uword *h = vac_msg_table_get_hash();
577 hash_foreach_pair (hp, h,
579 if (hp->value[0] > max)
587 vac_set_error_handler (vac_error_callback_t cb)
589 if (cb) clib_error_register_handler (cb, 0);