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"
38 * Client registers a callback. All messages are sent to the callback.
40 * Client calls blocking read().
41 * Clients are expected to collate events on a queue.
42 * vac_write() -> suspends RX thread
43 * vac_read() -> resumes RX thread
46 #define vl_typedefs /* define message structures */
47 #include <vpp/api/vpe_all_api_h.h>
50 #define vl_endianfun /* define message structures */
51 #include <vpp/api/vpe_all_api_h.h>
54 vlib_main_t vlib_global_main;
55 vlib_main_t **vlib_mains;
59 pthread_t rx_thread_handle;
60 pthread_t timeout_thread_handle;
61 pthread_mutex_t queue_lock;
62 pthread_cond_t suspend_cv;
63 pthread_cond_t resume_cv;
64 pthread_mutex_t timeout_lock;
65 pthread_cond_t timeout_cv;
66 pthread_cond_t timeout_cancel_cv;
67 pthread_cond_t terminate_cv;
71 vac_callback_t vac_callback;
73 bool rx_is_running = false;
78 vac_main_t *pm = &vac_main;
79 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);
84 pthread_cond_init(&pm->timeout_cv, NULL);
85 pthread_cond_init(&pm->timeout_cancel_cv, NULL);
86 pthread_cond_init(&pm->terminate_cv, NULL);
92 vac_main_t *pm = &vac_main;
93 pthread_cond_destroy(&pm->suspend_cv);
94 pthread_cond_destroy(&pm->resume_cv);
95 pthread_cond_destroy(&pm->timeout_cv);
96 pthread_cond_destroy(&pm->timeout_cancel_cv);
97 pthread_cond_destroy(&pm->terminate_cv);
98 pthread_mutex_destroy(&pm->queue_lock);
99 pthread_mutex_destroy(&pm->timeout_lock);
100 memset (pm, 0, sizeof (*pm));
104 * Satisfy external references when -lvlib is not available.
106 void vlib_cli_output (struct vlib_main_t * vm, char * fmt, ...)
108 clib_warning ("vlib_cli_output called...");
112 vac_free (void * msg)
114 vl_msg_api_free (msg);
118 vac_api_handler (void *msg)
120 u16 id = ntohs(*((u16 *)msg));
121 msgbuf_t *msgbuf = (msgbuf_t *)(((u8 *)msg) - offsetof(msgbuf_t, data));
122 int l = ntohl(msgbuf->data_len);
124 clib_warning("Message ID %d has wrong length: %d\n", id, l);
126 /* Call Python callback */
127 ASSERT(vac_callback);
128 (vac_callback)(msg, l);
133 vac_rx_thread_fn (void *arg)
135 unix_shared_memory_queue_t *q;
136 vac_main_t *pm = &vac_main;
137 api_main_t *am = &api_main;
140 q = am->vl_input_queue;
143 while (!unix_shared_memory_queue_sub(q, (u8 *)&msg, 0))
145 u16 id = ntohs(*((u16 *)msg));
147 case VL_API_RX_THREAD_EXIT:
148 vl_msg_api_free((void *) msg);
149 /* signal waiting threads that this thread is about to terminate */
150 pthread_mutex_lock(&pm->queue_lock);
151 pthread_cond_signal(&pm->terminate_cv);
152 pthread_mutex_unlock(&pm->queue_lock);
157 case VL_API_MEMCLNT_RX_THREAD_SUSPEND:
158 vl_msg_api_free((void * )msg);
159 /* Suspend thread and signal reader */
160 pthread_mutex_lock(&pm->queue_lock);
161 pthread_cond_signal(&pm->suspend_cv);
162 /* Wait for the resume signal */
163 pthread_cond_wait (&pm->resume_cv, &pm->queue_lock);
164 pthread_mutex_unlock(&pm->queue_lock);
167 case VL_API_MEMCLNT_READ_TIMEOUT:
168 clib_warning("Received read timeout in async thread\n");
169 vl_msg_api_free((void *) msg);
173 vac_api_handler((void *)msg);
179 vac_timeout_thread_fn (void *arg)
181 vl_api_memclnt_read_timeout_t *ep;
182 vac_main_t *pm = &vac_main;
183 api_main_t *am = &api_main;
192 pthread_mutex_lock(&pm->timeout_lock);
193 pthread_cond_wait (&pm->timeout_cv, &pm->timeout_lock);
194 timeout = read_timeout;
195 gettimeofday(&tv, NULL);
196 ts.tv_sec = tv.tv_sec + timeout;
198 rv = pthread_cond_timedwait (&pm->timeout_cancel_cv,
199 &pm->timeout_lock, &ts);
200 pthread_mutex_unlock(&pm->timeout_lock);
203 ep = vl_msg_api_alloc (sizeof (*ep));
204 ep->_vl_msg_id = ntohs(VL_API_MEMCLNT_READ_TIMEOUT);
205 vl_msg_api_send_shmem(am->vl_input_queue, (u8 *)&ep);
212 vac_rx_suspend (void)
214 api_main_t *am = &api_main;
215 vac_main_t *pm = &vac_main;
216 vl_api_memclnt_rx_thread_suspend_t *ep;
218 if (!pm->rx_thread_handle) return;
219 pthread_mutex_lock(&pm->queue_lock);
222 ep = vl_msg_api_alloc (sizeof (*ep));
223 ep->_vl_msg_id = ntohs(VL_API_MEMCLNT_RX_THREAD_SUSPEND);
224 vl_msg_api_send_shmem(am->vl_input_queue, (u8 *)&ep);
225 /* Wait for RX thread to tell us it has suspendend */
226 pthread_cond_wait(&pm->suspend_cv, &pm->queue_lock);
227 rx_is_running = false;
229 pthread_mutex_unlock(&pm->queue_lock);
235 vac_main_t *pm = &vac_main;
236 if (!pm->rx_thread_handle) return;
237 pthread_mutex_lock(&pm->queue_lock);
238 if (rx_is_running) goto unlock;
239 pthread_cond_signal(&pm->resume_cv);
240 rx_is_running = true;
242 pthread_mutex_unlock(&pm->queue_lock);
246 vac_msg_table_get_hash (void)
248 api_main_t *am = &api_main;
249 return (am->msg_index_by_name_and_crc);
253 vac_msg_table_size(void)
255 api_main_t *am = &api_main;
256 return hash_elts(am->msg_index_by_name_and_crc);
260 vac_connect (char * name, char * chroot_prefix, vac_callback_t cb,
264 vac_main_t *pm = &vac_main;
267 if (chroot_prefix != NULL)
268 vl_set_memory_root_path (chroot_prefix);
270 if ((rv = vl_client_api_map("/vpe-api"))) {
271 clib_warning ("vl_client_api map rv %d", rv);
275 if (vl_client_connect(name, 0, rx_qlen) < 0) {
276 vl_client_api_unmap();
281 /* Start the rx queue thread */
282 rv = pthread_create(&pm->rx_thread_handle, NULL, vac_rx_thread_fn, 0);
284 clib_warning("pthread_create returned %d", rv);
285 vl_client_api_unmap();
289 rx_is_running = true;
292 /* Start read timeout thread */
293 rv = pthread_create(&pm->timeout_thread_handle, NULL,
294 vac_timeout_thread_fn, 0);
296 clib_warning("pthread_create returned %d", rv);
297 vl_client_api_unmap();
301 pm->connected_to_vlib = 1;
307 vac_disconnect (void)
309 api_main_t *am = &api_main;
310 vac_main_t *pm = &vac_main;
312 if (!pm->connected_to_vlib) return 0;
314 if (pm->rx_thread_handle) {
315 vl_api_rx_thread_exit_t *ep;
317 ep = vl_msg_api_alloc (sizeof (*ep));
318 ep->_vl_msg_id = ntohs(VL_API_RX_THREAD_EXIT);
319 vl_msg_api_send_shmem(am->vl_input_queue, (u8 *)&ep);
321 /* wait (with timeout) until RX thread has finished */
324 gettimeofday(&tv, NULL);
325 ts.tv_sec = tv.tv_sec + 5;
327 pthread_mutex_lock(&pm->queue_lock);
328 int rv = pthread_cond_timedwait(&pm->terminate_cv, &pm->queue_lock, &ts);
329 pthread_mutex_unlock(&pm->queue_lock);
330 /* now join so we wait until thread has -really- finished */
332 pthread_cancel(pm->rx_thread_handle);
334 pthread_join(pm->rx_thread_handle, (void **) &junk);
336 if (pm->timeout_thread_handle)
337 pthread_cancel(pm->timeout_thread_handle);
339 vl_client_disconnect();
340 vl_client_api_unmap();
349 set_timeout (unsigned short timeout)
351 vac_main_t *pm = &vac_main;
352 pthread_mutex_lock(&pm->timeout_lock);
353 read_timeout = timeout;
354 pthread_cond_signal(&pm->timeout_cv);
355 pthread_mutex_unlock(&pm->timeout_lock);
361 vac_main_t *pm = &vac_main;
362 pthread_mutex_lock(&pm->timeout_lock);
363 pthread_cond_signal(&pm->timeout_cancel_cv);
364 pthread_mutex_unlock(&pm->timeout_lock);
368 vac_read (char **p, int *l, u16 timeout)
370 unix_shared_memory_queue_t *q;
371 api_main_t *am = &api_main;
372 vac_main_t *pm = &vac_main;
376 if (!pm->connected_to_vlib) return -1;
380 if (am->our_pid == 0) return (-1);
382 /* Poke timeout thread */
384 set_timeout(timeout);
386 q = am->vl_input_queue;
387 int rv = unix_shared_memory_queue_sub(q, (u8 *)&msg, 0);
389 u16 msg_id = ntohs(*((u16 *)msg));
391 case VL_API_RX_THREAD_EXIT:
392 printf("Received thread exit\n");
394 case VL_API_MEMCLNT_RX_THREAD_SUSPEND:
395 printf("Received thread suspend\n");
397 case VL_API_MEMCLNT_READ_TIMEOUT:
398 printf("Received read timeout %ds\n", timeout);
402 msgbuf = (msgbuf_t *)(((u8 *)msg) - offsetof(msgbuf_t, data));
403 *l = ntohl(msgbuf->data_len);
405 printf("Unregistered API message: %d\n", msg_id);
411 /* Let timeout notification thread know we're done */
415 printf("Read failed with %d\n", rv);
420 vl_msg_api_free((void *) msg);
421 /* Client might forget to resume RX thread on failure */
427 * XXX: Makes the assumption that client_index is the first member
429 typedef VL_API_PACKED(struct _vl_api_header {
435 vac_client_index (void)
437 return (api_main.my_client_index);
441 vac_write (char *p, int l)
444 api_main_t *am = &api_main;
445 vl_api_header_t *mp = vl_msg_api_alloc(l);
446 unix_shared_memory_queue_t *q;
447 vac_main_t *pm = &vac_main;
449 if (!pm->connected_to_vlib) return -1;
450 if (!mp) return (-1);
453 mp->client_index = vac_client_index();
454 q = am->shmem_hdr->vl_input_queue;
455 rv = unix_shared_memory_queue_add(q, (u8 *)&mp, 0);
457 clib_warning("vpe_api_write fails: %d\n", rv);
465 vac_get_msg_index (unsigned char * name)
467 return vl_api_get_msg_index (name);
471 vac_msg_table_max_index(void)
475 uword *h = vac_msg_table_get_hash();
476 hash_foreach_pair (hp, h,
478 if (hp->value[0] > max)
486 vac_set_error_handler (vac_error_callback_t cb)
488 if (cb) clib_error_register_handler (cb, 0);