blob: cd0b5b9e45dde17097bdc39fa6736f0868d7fbd5 [file] [log] [blame]
Damjan Marion7cd468a2016-12-19 23:05:39 +01001/*
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:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
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.
14 */
15#include <stdio.h>
16#include <stdlib.h>
17#include <stddef.h>
18#include <sys/types.h>
19#include <sys/socket.h>
20#include <sys/mman.h>
21#include <sys/stat.h>
22#include <netinet/in.h>
23#include <netdb.h>
24#include <signal.h>
Damjan Marion7cd468a2016-12-19 23:05:39 +010025#include <stdbool.h>
Damjan Marion7cd468a2016-12-19 23:05:39 +010026#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>
31
32#include <vpp/api/vpe_msg_enum.h>
33
Damjan Marion5fec1e82017-04-13 19:13:47 +020034#include "vppapiclient.h"
Damjan Marion7cd468a2016-12-19 23:05:39 +010035
Ole Troandfc9b7c2017-03-06 23:51:57 +010036/*
37 * Asynchronous mode:
38 * Client registers a callback. All messages are sent to the callback.
39 * Synchronous mode:
40 * Client calls blocking read().
41 * Clients are expected to collate events on a queue.
Damjan Marion5fec1e82017-04-13 19:13:47 +020042 * vac_write() -> suspends RX thread
43 * vac_read() -> resumes RX thread
Ole Troandfc9b7c2017-03-06 23:51:57 +010044 */
45
Damjan Marion7cd468a2016-12-19 23:05:39 +010046#define vl_typedefs /* define message structures */
47#include <vpp/api/vpe_all_api_h.h>
48#undef vl_typedefs
49
50#define vl_endianfun /* define message structures */
51#include <vpp/api/vpe_all_api_h.h>
52#undef vl_endianfun
53
54vlib_main_t vlib_global_main;
55vlib_main_t **vlib_mains;
56
57typedef struct {
Damjan Marion7cd468a2016-12-19 23:05:39 +010058 u8 connected_to_vlib;
Damjan Marion7cd468a2016-12-19 23:05:39 +010059 pthread_t rx_thread_handle;
Ole Troandfc9b7c2017-03-06 23:51:57 +010060 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;
Damjan Marion5fec1e82017-04-13 19:13:47 +020068} vac_main_t;
Damjan Marion7cd468a2016-12-19 23:05:39 +010069
Damjan Marion5fec1e82017-04-13 19:13:47 +020070vac_main_t vac_main;
71vac_callback_t vac_callback;
Ole Troandfc9b7c2017-03-06 23:51:57 +010072u16 read_timeout = 0;
73bool rx_is_running = false;
74
Ole Troan73710c72018-06-04 22:27:49 +020075/* Set to true to enable memory tracing */
76bool mem_trace = false;
77
78__attribute__((constructor))
79static void
80vac_client_constructor (void)
81{
82 u8 *heap;
83 mheap_t *h;
84 clib_mem_init (0, 1 << 30);
85 heap = clib_mem_get_per_cpu_heap ();
86 h = mheap_header (heap);
87 /* make the main heap thread-safe */
88 h->flags |= MHEAP_FLAG_THREAD_SAFE;
89 if (mem_trace)
90 clib_mem_trace (1);
91}
92
93__attribute__((destructor))
94static void
95vac_client_destructor (void)
96{
97 if (mem_trace)
98 fformat(stderr, "TRACE: %s",
99 format (0, "%U\n",
100 format_mheap, clib_mem_get_heap (), 1));
101}
102
103
Ole Troandfc9b7c2017-03-06 23:51:57 +0100104static void
105init (void)
106{
Damjan Marion5fec1e82017-04-13 19:13:47 +0200107 vac_main_t *pm = &vac_main;
Ole Troandfc9b7c2017-03-06 23:51:57 +0100108 memset(pm, 0, sizeof(*pm));
109 pthread_mutex_init(&pm->queue_lock, NULL);
110 pthread_cond_init(&pm->suspend_cv, NULL);
111 pthread_cond_init(&pm->resume_cv, NULL);
112 pthread_mutex_init(&pm->timeout_lock, NULL);
113 pthread_cond_init(&pm->timeout_cv, NULL);
114 pthread_cond_init(&pm->timeout_cancel_cv, NULL);
115 pthread_cond_init(&pm->terminate_cv, NULL);
116}
117
118static void
119cleanup (void)
120{
Damjan Marion5fec1e82017-04-13 19:13:47 +0200121 vac_main_t *pm = &vac_main;
Ole Troan73710c72018-06-04 22:27:49 +0200122 pthread_mutex_destroy(&pm->queue_lock);
Ole Troandfc9b7c2017-03-06 23:51:57 +0100123 pthread_cond_destroy(&pm->suspend_cv);
124 pthread_cond_destroy(&pm->resume_cv);
Ole Troan73710c72018-06-04 22:27:49 +0200125 pthread_mutex_destroy(&pm->timeout_lock);
Ole Troandfc9b7c2017-03-06 23:51:57 +0100126 pthread_cond_destroy(&pm->timeout_cv);
127 pthread_cond_destroy(&pm->timeout_cancel_cv);
128 pthread_cond_destroy(&pm->terminate_cv);
Ole Troan73710c72018-06-04 22:27:49 +0200129 memset(pm, 0, sizeof(*pm));
Ole Troandfc9b7c2017-03-06 23:51:57 +0100130}
Damjan Marion7cd468a2016-12-19 23:05:39 +0100131
132/*
133 * Satisfy external references when -lvlib is not available.
134 */
135void vlib_cli_output (struct vlib_main_t * vm, char * fmt, ...)
136{
137 clib_warning ("vlib_cli_output called...");
138}
139
140void
Damjan Marion5fec1e82017-04-13 19:13:47 +0200141vac_free (void * msg)
Damjan Marion7cd468a2016-12-19 23:05:39 +0100142{
143 vl_msg_api_free (msg);
144}
145
146static void
Damjan Marion5fec1e82017-04-13 19:13:47 +0200147vac_api_handler (void *msg)
Damjan Marion7cd468a2016-12-19 23:05:39 +0100148{
149 u16 id = ntohs(*((u16 *)msg));
Damjan Marion7cd468a2016-12-19 23:05:39 +0100150 msgbuf_t *msgbuf = (msgbuf_t *)(((u8 *)msg) - offsetof(msgbuf_t, data));
151 int l = ntohl(msgbuf->data_len);
152 if (l == 0)
153 clib_warning("Message ID %d has wrong length: %d\n", id, l);
154
155 /* Call Python callback */
Damjan Marion5fec1e82017-04-13 19:13:47 +0200156 ASSERT(vac_callback);
157 (vac_callback)(msg, l);
158 vac_free(msg);
Damjan Marion7cd468a2016-12-19 23:05:39 +0100159}
160
161static void *
Damjan Marion5fec1e82017-04-13 19:13:47 +0200162vac_rx_thread_fn (void *arg)
Damjan Marion7cd468a2016-12-19 23:05:39 +0100163{
Florin Corase86a8ed2018-01-05 03:20:25 -0800164 svm_queue_t *q;
Dave Barach59b25652017-09-10 15:04:27 -0400165 vl_api_memclnt_keepalive_t *mp;
166 vl_api_memclnt_keepalive_reply_t *rmp;
Damjan Marion5fec1e82017-04-13 19:13:47 +0200167 vac_main_t *pm = &vac_main;
Damjan Marion7cd468a2016-12-19 23:05:39 +0100168 api_main_t *am = &api_main;
Dave Barach59b25652017-09-10 15:04:27 -0400169 vl_shmem_hdr_t *shmem_hdr;
Damjan Marion7cd468a2016-12-19 23:05:39 +0100170 uword msg;
171
172 q = am->vl_input_queue;
173
Ole Troandfc9b7c2017-03-06 23:51:57 +0100174 while (1)
Mohsin Kazmi3fca5672018-01-04 18:57:26 +0100175 while (!svm_queue_sub(q, (u8 *)&msg, SVM_Q_WAIT, 0))
Ole Troandfc9b7c2017-03-06 23:51:57 +0100176 {
177 u16 id = ntohs(*((u16 *)msg));
178 switch (id) {
179 case VL_API_RX_THREAD_EXIT:
180 vl_msg_api_free((void *) msg);
181 /* signal waiting threads that this thread is about to terminate */
182 pthread_mutex_lock(&pm->queue_lock);
183 pthread_cond_signal(&pm->terminate_cv);
184 pthread_mutex_unlock(&pm->queue_lock);
185 pthread_exit(0);
186 return 0;
187 break;
188
189 case VL_API_MEMCLNT_RX_THREAD_SUSPEND:
190 vl_msg_api_free((void * )msg);
191 /* Suspend thread and signal reader */
192 pthread_mutex_lock(&pm->queue_lock);
193 pthread_cond_signal(&pm->suspend_cv);
194 /* Wait for the resume signal */
195 pthread_cond_wait (&pm->resume_cv, &pm->queue_lock);
196 pthread_mutex_unlock(&pm->queue_lock);
197 break;
198
199 case VL_API_MEMCLNT_READ_TIMEOUT:
200 clib_warning("Received read timeout in async thread\n");
201 vl_msg_api_free((void *) msg);
202 break;
203
Dave Barach59b25652017-09-10 15:04:27 -0400204 case VL_API_MEMCLNT_KEEPALIVE:
205 mp = (void *)msg;
206 rmp = vl_msg_api_alloc (sizeof (*rmp));
207 memset (rmp, 0, sizeof (*rmp));
208 rmp->_vl_msg_id = ntohs(VL_API_MEMCLNT_KEEPALIVE_REPLY);
209 rmp->context = mp->context;
210 shmem_hdr = am->shmem_hdr;
211 vl_msg_api_send_shmem(shmem_hdr->vl_input_queue, (u8 *)&rmp);
212 vl_msg_api_free((void *) msg);
213 break;
214
Ole Troandfc9b7c2017-03-06 23:51:57 +0100215 default:
Damjan Marion5fec1e82017-04-13 19:13:47 +0200216 vac_api_handler((void *)msg);
Ole Troandfc9b7c2017-03-06 23:51:57 +0100217 }
218 }
219}
220
221static void *
Damjan Marion5fec1e82017-04-13 19:13:47 +0200222vac_timeout_thread_fn (void *arg)
Ole Troandfc9b7c2017-03-06 23:51:57 +0100223{
224 vl_api_memclnt_read_timeout_t *ep;
Damjan Marion5fec1e82017-04-13 19:13:47 +0200225 vac_main_t *pm = &vac_main;
Ole Troandfc9b7c2017-03-06 23:51:57 +0100226 api_main_t *am = &api_main;
227 struct timespec ts;
228 struct timeval tv;
229 u16 timeout;
230 int rv;
231
232 while (1)
233 {
234 /* Wait for poke */
235 pthread_mutex_lock(&pm->timeout_lock);
236 pthread_cond_wait (&pm->timeout_cv, &pm->timeout_lock);
237 timeout = read_timeout;
238 gettimeofday(&tv, NULL);
239 ts.tv_sec = tv.tv_sec + timeout;
240 ts.tv_nsec = 0;
241 rv = pthread_cond_timedwait (&pm->timeout_cancel_cv,
242 &pm->timeout_lock, &ts);
243 pthread_mutex_unlock(&pm->timeout_lock);
244 if (rv == ETIMEDOUT)
245 {
246 ep = vl_msg_api_alloc (sizeof (*ep));
247 ep->_vl_msg_id = ntohs(VL_API_MEMCLNT_READ_TIMEOUT);
248 vl_msg_api_send_shmem(am->vl_input_queue, (u8 *)&ep);
249 }
250 }
Damjan Marion7cd468a2016-12-19 23:05:39 +0100251 pthread_exit(0);
252}
253
Ole Troandfc9b7c2017-03-06 23:51:57 +0100254void
Damjan Marion5fec1e82017-04-13 19:13:47 +0200255vac_rx_suspend (void)
Ole Troandfc9b7c2017-03-06 23:51:57 +0100256{
257 api_main_t *am = &api_main;
Damjan Marion5fec1e82017-04-13 19:13:47 +0200258 vac_main_t *pm = &vac_main;
Ole Troandfc9b7c2017-03-06 23:51:57 +0100259 vl_api_memclnt_rx_thread_suspend_t *ep;
260
261 if (!pm->rx_thread_handle) return;
262 pthread_mutex_lock(&pm->queue_lock);
263 if (rx_is_running)
264 {
265 ep = vl_msg_api_alloc (sizeof (*ep));
266 ep->_vl_msg_id = ntohs(VL_API_MEMCLNT_RX_THREAD_SUSPEND);
267 vl_msg_api_send_shmem(am->vl_input_queue, (u8 *)&ep);
268 /* Wait for RX thread to tell us it has suspendend */
269 pthread_cond_wait(&pm->suspend_cv, &pm->queue_lock);
270 rx_is_running = false;
271 }
272 pthread_mutex_unlock(&pm->queue_lock);
273}
274
275void
Damjan Marion5fec1e82017-04-13 19:13:47 +0200276vac_rx_resume (void)
Ole Troandfc9b7c2017-03-06 23:51:57 +0100277{
Damjan Marion5fec1e82017-04-13 19:13:47 +0200278 vac_main_t *pm = &vac_main;
Ole Troandfc9b7c2017-03-06 23:51:57 +0100279 if (!pm->rx_thread_handle) return;
280 pthread_mutex_lock(&pm->queue_lock);
Ole Troanad0697a2017-03-09 21:10:45 +0100281 if (rx_is_running) goto unlock;
Ole Troandfc9b7c2017-03-06 23:51:57 +0100282 pthread_cond_signal(&pm->resume_cv);
283 rx_is_running = true;
Ole Troanad0697a2017-03-09 21:10:45 +0100284 unlock:
Ole Troandfc9b7c2017-03-06 23:51:57 +0100285 pthread_mutex_unlock(&pm->queue_lock);
286}
287
Ole Troan3cc49712017-03-08 12:02:24 +0100288static uword *
Damjan Marion5fec1e82017-04-13 19:13:47 +0200289vac_msg_table_get_hash (void)
Damjan Marion7cd468a2016-12-19 23:05:39 +0100290{
291 api_main_t *am = &api_main;
292 return (am->msg_index_by_name_and_crc);
293}
294
295int
Damjan Marion5fec1e82017-04-13 19:13:47 +0200296vac_msg_table_size(void)
Damjan Marion7cd468a2016-12-19 23:05:39 +0100297{
298 api_main_t *am = &api_main;
299 return hash_elts(am->msg_index_by_name_and_crc);
300}
301
302int
Damjan Marion5fec1e82017-04-13 19:13:47 +0200303vac_connect (char * name, char * chroot_prefix, vac_callback_t cb,
Dave Barachf9526922017-01-06 16:33:06 -0500304 int rx_qlen)
Damjan Marion7cd468a2016-12-19 23:05:39 +0100305{
306 int rv = 0;
Damjan Marion5fec1e82017-04-13 19:13:47 +0200307 vac_main_t *pm = &vac_main;
Damjan Marion7cd468a2016-12-19 23:05:39 +0100308
Ole Troandfc9b7c2017-03-06 23:51:57 +0100309 init();
Damjan Marion7cd468a2016-12-19 23:05:39 +0100310 if (chroot_prefix != NULL)
311 vl_set_memory_root_path (chroot_prefix);
312
313 if ((rv = vl_client_api_map("/vpe-api"))) {
314 clib_warning ("vl_client_api map rv %d", rv);
315 return rv;
316 }
317
Dave Barachf9526922017-01-06 16:33:06 -0500318 if (vl_client_connect(name, 0, rx_qlen) < 0) {
Damjan Marion7cd468a2016-12-19 23:05:39 +0100319 vl_client_api_unmap();
320 return (-1);
321 }
322
323 if (cb) {
324 /* Start the rx queue thread */
Damjan Marion5fec1e82017-04-13 19:13:47 +0200325 rv = pthread_create(&pm->rx_thread_handle, NULL, vac_rx_thread_fn, 0);
Damjan Marion7cd468a2016-12-19 23:05:39 +0100326 if (rv) {
327 clib_warning("pthread_create returned %d", rv);
328 vl_client_api_unmap();
329 return (-1);
330 }
Damjan Marion5fec1e82017-04-13 19:13:47 +0200331 vac_callback = cb;
Ole Troandfc9b7c2017-03-06 23:51:57 +0100332 rx_is_running = true;
333 }
334
335 /* Start read timeout thread */
336 rv = pthread_create(&pm->timeout_thread_handle, NULL,
Damjan Marion5fec1e82017-04-13 19:13:47 +0200337 vac_timeout_thread_fn, 0);
Ole Troandfc9b7c2017-03-06 23:51:57 +0100338 if (rv) {
339 clib_warning("pthread_create returned %d", rv);
340 vl_client_api_unmap();
341 return (-1);
Damjan Marion7cd468a2016-12-19 23:05:39 +0100342 }
343
344 pm->connected_to_vlib = 1;
345
346 return (0);
347}
348
Neale Rannsc16f6b32018-06-03 21:21:19 -0700349static void
350set_timeout (unsigned short timeout)
351{
352 vac_main_t *pm = &vac_main;
353 pthread_mutex_lock(&pm->timeout_lock);
354 read_timeout = timeout;
355 pthread_cond_signal(&pm->timeout_cv);
356 pthread_mutex_unlock(&pm->timeout_lock);
357}
358
359static void
360unset_timeout (void)
361{
362 vac_main_t *pm = &vac_main;
363 pthread_mutex_lock(&pm->timeout_lock);
364 pthread_cond_signal(&pm->timeout_cancel_cv);
365 pthread_mutex_unlock(&pm->timeout_lock);
366}
367
Damjan Marion7cd468a2016-12-19 23:05:39 +0100368int
Damjan Marion5fec1e82017-04-13 19:13:47 +0200369vac_disconnect (void)
Damjan Marion7cd468a2016-12-19 23:05:39 +0100370{
371 api_main_t *am = &api_main;
Damjan Marion5fec1e82017-04-13 19:13:47 +0200372 vac_main_t *pm = &vac_main;
Neale Rannsc16f6b32018-06-03 21:21:19 -0700373 uword junk;
Damjan Marion7cd468a2016-12-19 23:05:39 +0100374
Ole Troandfc9b7c2017-03-06 23:51:57 +0100375 if (!pm->connected_to_vlib) return 0;
376
377 if (pm->rx_thread_handle) {
Damjan Marion7cd468a2016-12-19 23:05:39 +0100378 vl_api_rx_thread_exit_t *ep;
Damjan Marion7cd468a2016-12-19 23:05:39 +0100379 ep = vl_msg_api_alloc (sizeof (*ep));
380 ep->_vl_msg_id = ntohs(VL_API_RX_THREAD_EXIT);
381 vl_msg_api_send_shmem(am->vl_input_queue, (u8 *)&ep);
Ole Troandfc9b7c2017-03-06 23:51:57 +0100382
383 /* wait (with timeout) until RX thread has finished */
384 struct timespec ts;
385 struct timeval tv;
386 gettimeofday(&tv, NULL);
387 ts.tv_sec = tv.tv_sec + 5;
388 ts.tv_nsec = 0;
389 pthread_mutex_lock(&pm->queue_lock);
390 int rv = pthread_cond_timedwait(&pm->terminate_cv, &pm->queue_lock, &ts);
391 pthread_mutex_unlock(&pm->queue_lock);
392 /* now join so we wait until thread has -really- finished */
393 if (rv == ETIMEDOUT)
394 pthread_cancel(pm->rx_thread_handle);
395 else
396 pthread_join(pm->rx_thread_handle, (void **) &junk);
Damjan Marion7cd468a2016-12-19 23:05:39 +0100397 }
Neale Rannsc16f6b32018-06-03 21:21:19 -0700398 if (pm->timeout_thread_handle) {
399 /* cancel, wake then join the timeout thread */
Ole Troandfc9b7c2017-03-06 23:51:57 +0100400 pthread_cancel(pm->timeout_thread_handle);
Neale Rannsc16f6b32018-06-03 21:21:19 -0700401 set_timeout(0);
402 pthread_join(pm->timeout_thread_handle, (void **) &junk);
403 }
Ole Troandfc9b7c2017-03-06 23:51:57 +0100404
405 vl_client_disconnect();
406 vl_client_api_unmap();
Damjan Marion5fec1e82017-04-13 19:13:47 +0200407 vac_callback = 0;
Ole Troandfc9b7c2017-03-06 23:51:57 +0100408
409 cleanup();
Damjan Marion7cd468a2016-12-19 23:05:39 +0100410
411 return (0);
412}
413
414int
Damjan Marion5fec1e82017-04-13 19:13:47 +0200415vac_read (char **p, int *l, u16 timeout)
Damjan Marion7cd468a2016-12-19 23:05:39 +0100416{
Florin Corase86a8ed2018-01-05 03:20:25 -0800417 svm_queue_t *q;
Damjan Marion7cd468a2016-12-19 23:05:39 +0100418 api_main_t *am = &api_main;
Damjan Marion5fec1e82017-04-13 19:13:47 +0200419 vac_main_t *pm = &vac_main;
Dave Barach59b25652017-09-10 15:04:27 -0400420 vl_api_memclnt_keepalive_t *mp;
421 vl_api_memclnt_keepalive_reply_t *rmp;
Damjan Marion7cd468a2016-12-19 23:05:39 +0100422 uword msg;
Ole Troandfc9b7c2017-03-06 23:51:57 +0100423 msgbuf_t *msgbuf;
Dave Barach59b25652017-09-10 15:04:27 -0400424 int rv;
425 vl_shmem_hdr_t *shmem_hdr;
Damjan Marion7cd468a2016-12-19 23:05:39 +0100426
427 if (!pm->connected_to_vlib) return -1;
428
429 *l = 0;
430
431 if (am->our_pid == 0) return (-1);
432
Ole Troandfc9b7c2017-03-06 23:51:57 +0100433 /* Poke timeout thread */
434 if (timeout)
435 set_timeout(timeout);
436
Damjan Marion7cd468a2016-12-19 23:05:39 +0100437 q = am->vl_input_queue;
Dave Barach59b25652017-09-10 15:04:27 -0400438
439 again:
Mohsin Kazmi3fca5672018-01-04 18:57:26 +0100440 rv = svm_queue_sub(q, (u8 *)&msg, SVM_Q_WAIT, 0);
441
Damjan Marion7cd468a2016-12-19 23:05:39 +0100442 if (rv == 0) {
443 u16 msg_id = ntohs(*((u16 *)msg));
Ole Troandfc9b7c2017-03-06 23:51:57 +0100444 switch (msg_id) {
445 case VL_API_RX_THREAD_EXIT:
446 printf("Received thread exit\n");
Ole Troan73710c72018-06-04 22:27:49 +0200447 vl_msg_api_free((void *) msg);
Ole Troandfc9b7c2017-03-06 23:51:57 +0100448 return -1;
449 case VL_API_MEMCLNT_RX_THREAD_SUSPEND:
450 printf("Received thread suspend\n");
451 goto error;
452 case VL_API_MEMCLNT_READ_TIMEOUT:
453 printf("Received read timeout %ds\n", timeout);
454 goto error;
Dave Barach59b25652017-09-10 15:04:27 -0400455 case VL_API_MEMCLNT_KEEPALIVE:
456 /* Handle an alive-check ping from vpp. */
457 mp = (void *)msg;
458 rmp = vl_msg_api_alloc (sizeof (*rmp));
459 memset (rmp, 0, sizeof (*rmp));
460 rmp->_vl_msg_id = ntohs(VL_API_MEMCLNT_KEEPALIVE_REPLY);
461 rmp->context = mp->context;
462 shmem_hdr = am->shmem_hdr;
463 vl_msg_api_send_shmem(shmem_hdr->vl_input_queue, (u8 *)&rmp);
464 vl_msg_api_free((void *) msg);
465 /*
466 * Python code is blissfully unaware of these pings, so
467 * act as if it never happened...
468 */
469 goto again;
Ole Troandfc9b7c2017-03-06 23:51:57 +0100470
471 default:
472 msgbuf = (msgbuf_t *)(((u8 *)msg) - offsetof(msgbuf_t, data));
473 *l = ntohl(msgbuf->data_len);
474 if (*l == 0) {
475 printf("Unregistered API message: %d\n", msg_id);
476 goto error;
477 }
Damjan Marion7cd468a2016-12-19 23:05:39 +0100478 }
479 *p = (char *)msg;
Ole Troandfc9b7c2017-03-06 23:51:57 +0100480
481 /* Let timeout notification thread know we're done */
482 unset_timeout();
483
Damjan Marion7cd468a2016-12-19 23:05:39 +0100484 } else {
485 printf("Read failed with %d\n", rv);
486 }
487 return (rv);
Ole Troandfc9b7c2017-03-06 23:51:57 +0100488
489 error:
490 vl_msg_api_free((void *) msg);
491 /* Client might forget to resume RX thread on failure */
Damjan Marion5fec1e82017-04-13 19:13:47 +0200492 vac_rx_resume ();
Ole Troandfc9b7c2017-03-06 23:51:57 +0100493 return -1;
Damjan Marion7cd468a2016-12-19 23:05:39 +0100494}
495
496/*
497 * XXX: Makes the assumption that client_index is the first member
498 */
499typedef VL_API_PACKED(struct _vl_api_header {
500 u16 _vl_msg_id;
501 u32 client_index;
502}) vl_api_header_t;
503
504static unsigned int
Damjan Marion5fec1e82017-04-13 19:13:47 +0200505vac_client_index (void)
Damjan Marion7cd468a2016-12-19 23:05:39 +0100506{
507 return (api_main.my_client_index);
508}
509
510int
Damjan Marion5fec1e82017-04-13 19:13:47 +0200511vac_write (char *p, int l)
Damjan Marion7cd468a2016-12-19 23:05:39 +0100512{
513 int rv = -1;
514 api_main_t *am = &api_main;
515 vl_api_header_t *mp = vl_msg_api_alloc(l);
Florin Corase86a8ed2018-01-05 03:20:25 -0800516 svm_queue_t *q;
Damjan Marion5fec1e82017-04-13 19:13:47 +0200517 vac_main_t *pm = &vac_main;
Damjan Marion7cd468a2016-12-19 23:05:39 +0100518
519 if (!pm->connected_to_vlib) return -1;
520 if (!mp) return (-1);
Ole Troandfc9b7c2017-03-06 23:51:57 +0100521
Damjan Marion7cd468a2016-12-19 23:05:39 +0100522 memcpy(mp, p, l);
Damjan Marion5fec1e82017-04-13 19:13:47 +0200523 mp->client_index = vac_client_index();
Damjan Marion7cd468a2016-12-19 23:05:39 +0100524 q = am->shmem_hdr->vl_input_queue;
Florin Corase86a8ed2018-01-05 03:20:25 -0800525 rv = svm_queue_add(q, (u8 *)&mp, 0);
Damjan Marion7cd468a2016-12-19 23:05:39 +0100526 if (rv != 0) {
Ole Troandfc9b7c2017-03-06 23:51:57 +0100527 clib_warning("vpe_api_write fails: %d\n", rv);
Damjan Marion7cd468a2016-12-19 23:05:39 +0100528 /* Clear message */
Damjan Marion5fec1e82017-04-13 19:13:47 +0200529 vac_free(mp);
Damjan Marion7cd468a2016-12-19 23:05:39 +0100530 }
531 return (rv);
532}
533
Ole Troan3cc49712017-03-08 12:02:24 +0100534int
Damjan Marion5fec1e82017-04-13 19:13:47 +0200535vac_get_msg_index (unsigned char * name)
Damjan Marion7cd468a2016-12-19 23:05:39 +0100536{
Florin Corase86a8ed2018-01-05 03:20:25 -0800537 return vl_msg_api_get_msg_index (name);
Damjan Marion7cd468a2016-12-19 23:05:39 +0100538}
Ole Troan3cc49712017-03-08 12:02:24 +0100539
540int
Damjan Marion5fec1e82017-04-13 19:13:47 +0200541vac_msg_table_max_index(void)
Ole Troan3cc49712017-03-08 12:02:24 +0100542{
543 int max = 0;
544 hash_pair_t *hp;
Damjan Marion5fec1e82017-04-13 19:13:47 +0200545 uword *h = vac_msg_table_get_hash();
Ole Troan3cc49712017-03-08 12:02:24 +0100546 hash_foreach_pair (hp, h,
547 ({
548 if (hp->value[0] > max)
549 max = hp->value[0];
550 }));
551
552 return max;
553}
554
555void
Damjan Marion5fec1e82017-04-13 19:13:47 +0200556vac_set_error_handler (vac_error_callback_t cb)
Ole Troan3cc49712017-03-08 12:02:24 +0100557{
558 if (cb) clib_error_register_handler (cb, 0);
559}