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>
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 * pneum_write() -> suspends RX thread
43 * pneum_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;
70 pneum_main_t pneum_main;
71 pneum_callback_t pneum_callback;
73 bool rx_is_running = false;
78 pneum_main_t *pm = &pneum_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 pneum_main_t *pm = &pneum_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 pneum_free (void * msg)
114 vl_msg_api_free (msg);
118 pneum_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(pneum_callback);
128 (pneum_callback)(msg, l);
133 pneum_rx_thread_fn (void *arg)
135 unix_shared_memory_queue_t *q;
136 pneum_main_t *pm = &pneum_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 pneum_api_handler((void *)msg);
179 pneum_timeout_thread_fn (void *arg)
181 vl_api_memclnt_read_timeout_t *ep;
182 pneum_main_t *pm = &pneum_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 pneum_rx_suspend (void)
214 api_main_t *am = &api_main;
215 pneum_main_t *pm = &pneum_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);
233 pneum_rx_resume (void)
235 pneum_main_t *pm = &pneum_main;
236 if (!pm->rx_thread_handle) return;
237 pthread_mutex_lock(&pm->queue_lock);
238 if (rx_is_running) return;
239 pthread_cond_signal(&pm->resume_cv);
240 rx_is_running = true;
241 pthread_mutex_unlock(&pm->queue_lock);
245 pneum_msg_table_get_hash (void)
247 api_main_t *am = &api_main;
248 return (am->msg_index_by_name_and_crc);
252 pneum_msg_table_size(void)
254 api_main_t *am = &api_main;
255 return hash_elts(am->msg_index_by_name_and_crc);
259 pneum_connect (char * name, char * chroot_prefix, pneum_callback_t cb,
263 pneum_main_t *pm = &pneum_main;
266 if (chroot_prefix != NULL)
267 vl_set_memory_root_path (chroot_prefix);
269 if ((rv = vl_client_api_map("/vpe-api"))) {
270 clib_warning ("vl_client_api map rv %d", rv);
274 if (vl_client_connect(name, 0, rx_qlen) < 0) {
275 vl_client_api_unmap();
280 /* Start the rx queue thread */
281 rv = pthread_create(&pm->rx_thread_handle, NULL, pneum_rx_thread_fn, 0);
283 clib_warning("pthread_create returned %d", rv);
284 vl_client_api_unmap();
288 rx_is_running = true;
291 /* Start read timeout thread */
292 rv = pthread_create(&pm->timeout_thread_handle, NULL,
293 pneum_timeout_thread_fn, 0);
295 clib_warning("pthread_create returned %d", rv);
296 vl_client_api_unmap();
300 pm->connected_to_vlib = 1;
306 pneum_disconnect (void)
308 api_main_t *am = &api_main;
309 pneum_main_t *pm = &pneum_main;
311 if (!pm->connected_to_vlib) return 0;
313 if (pm->rx_thread_handle) {
314 vl_api_rx_thread_exit_t *ep;
316 ep = vl_msg_api_alloc (sizeof (*ep));
317 ep->_vl_msg_id = ntohs(VL_API_RX_THREAD_EXIT);
318 vl_msg_api_send_shmem(am->vl_input_queue, (u8 *)&ep);
320 /* wait (with timeout) until RX thread has finished */
323 gettimeofday(&tv, NULL);
324 ts.tv_sec = tv.tv_sec + 5;
326 pthread_mutex_lock(&pm->queue_lock);
327 int rv = pthread_cond_timedwait(&pm->terminate_cv, &pm->queue_lock, &ts);
328 pthread_mutex_unlock(&pm->queue_lock);
329 /* now join so we wait until thread has -really- finished */
331 pthread_cancel(pm->rx_thread_handle);
333 pthread_join(pm->rx_thread_handle, (void **) &junk);
335 if (pm->timeout_thread_handle)
336 pthread_cancel(pm->timeout_thread_handle);
338 vl_client_disconnect();
339 vl_client_api_unmap();
348 set_timeout (unsigned short timeout)
350 pneum_main_t *pm = &pneum_main;
351 pthread_mutex_lock(&pm->timeout_lock);
352 read_timeout = timeout;
353 pthread_cond_signal(&pm->timeout_cv);
354 pthread_mutex_unlock(&pm->timeout_lock);
360 pneum_main_t *pm = &pneum_main;
361 pthread_mutex_lock(&pm->timeout_lock);
362 pthread_cond_signal(&pm->timeout_cancel_cv);
363 pthread_mutex_unlock(&pm->timeout_lock);
367 pneum_read (char **p, int *l, u16 timeout)
369 unix_shared_memory_queue_t *q;
370 api_main_t *am = &api_main;
371 pneum_main_t *pm = &pneum_main;
375 if (!pm->connected_to_vlib) return -1;
379 if (am->our_pid == 0) return (-1);
381 /* Poke timeout thread */
383 set_timeout(timeout);
385 q = am->vl_input_queue;
386 int rv = unix_shared_memory_queue_sub(q, (u8 *)&msg, 0);
388 u16 msg_id = ntohs(*((u16 *)msg));
390 case VL_API_RX_THREAD_EXIT:
391 printf("Received thread exit\n");
393 case VL_API_MEMCLNT_RX_THREAD_SUSPEND:
394 printf("Received thread suspend\n");
396 case VL_API_MEMCLNT_READ_TIMEOUT:
397 printf("Received read timeout %ds\n", timeout);
401 msgbuf = (msgbuf_t *)(((u8 *)msg) - offsetof(msgbuf_t, data));
402 *l = ntohl(msgbuf->data_len);
404 printf("Unregistered API message: %d\n", msg_id);
410 /* Let timeout notification thread know we're done */
414 printf("Read failed with %d\n", rv);
419 vl_msg_api_free((void *) msg);
420 /* Client might forget to resume RX thread on failure */
426 * XXX: Makes the assumption that client_index is the first member
428 typedef VL_API_PACKED(struct _vl_api_header {
434 pneum_client_index (void)
436 return (api_main.my_client_index);
440 pneum_write (char *p, int l)
443 api_main_t *am = &api_main;
444 vl_api_header_t *mp = vl_msg_api_alloc(l);
445 unix_shared_memory_queue_t *q;
446 pneum_main_t *pm = &pneum_main;
448 if (!pm->connected_to_vlib) return -1;
449 if (!mp) return (-1);
452 mp->client_index = pneum_client_index();
453 q = am->shmem_hdr->vl_input_queue;
454 rv = unix_shared_memory_queue_add(q, (u8 *)&mp, 0);
456 clib_warning("vpe_api_write fails: %d\n", rv);
464 pneum_get_msg_index (unsigned char * name)
466 return vl_api_get_msg_index (name);