blob: fd2c4174285201fd46ef0c98bb18488b8bbc30ca [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
75static void
76init (void)
77{
Damjan Marion5fec1e82017-04-13 19:13:47 +020078 vac_main_t *pm = &vac_main;
Ole Troandfc9b7c2017-03-06 23:51:57 +010079 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);
87}
88
89static void
90cleanup (void)
91{
Damjan Marion5fec1e82017-04-13 19:13:47 +020092 vac_main_t *pm = &vac_main;
Ole Troandfc9b7c2017-03-06 23:51:57 +010093 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));
101}
Damjan Marion7cd468a2016-12-19 23:05:39 +0100102
103/*
104 * Satisfy external references when -lvlib is not available.
105 */
106void vlib_cli_output (struct vlib_main_t * vm, char * fmt, ...)
107{
108 clib_warning ("vlib_cli_output called...");
109}
110
111void
Damjan Marion5fec1e82017-04-13 19:13:47 +0200112vac_free (void * msg)
Damjan Marion7cd468a2016-12-19 23:05:39 +0100113{
114 vl_msg_api_free (msg);
115}
116
117static void
Damjan Marion5fec1e82017-04-13 19:13:47 +0200118vac_api_handler (void *msg)
Damjan Marion7cd468a2016-12-19 23:05:39 +0100119{
120 u16 id = ntohs(*((u16 *)msg));
Damjan Marion7cd468a2016-12-19 23:05:39 +0100121 msgbuf_t *msgbuf = (msgbuf_t *)(((u8 *)msg) - offsetof(msgbuf_t, data));
122 int l = ntohl(msgbuf->data_len);
123 if (l == 0)
124 clib_warning("Message ID %d has wrong length: %d\n", id, l);
125
126 /* Call Python callback */
Damjan Marion5fec1e82017-04-13 19:13:47 +0200127 ASSERT(vac_callback);
128 (vac_callback)(msg, l);
129 vac_free(msg);
Damjan Marion7cd468a2016-12-19 23:05:39 +0100130}
131
132static void *
Damjan Marion5fec1e82017-04-13 19:13:47 +0200133vac_rx_thread_fn (void *arg)
Damjan Marion7cd468a2016-12-19 23:05:39 +0100134{
Florin Corase86a8ed2018-01-05 03:20:25 -0800135 svm_queue_t *q;
Dave Barach59b25652017-09-10 15:04:27 -0400136 vl_api_memclnt_keepalive_t *mp;
137 vl_api_memclnt_keepalive_reply_t *rmp;
Damjan Marion5fec1e82017-04-13 19:13:47 +0200138 vac_main_t *pm = &vac_main;
Damjan Marion7cd468a2016-12-19 23:05:39 +0100139 api_main_t *am = &api_main;
Dave Barach59b25652017-09-10 15:04:27 -0400140 vl_shmem_hdr_t *shmem_hdr;
Damjan Marion7cd468a2016-12-19 23:05:39 +0100141 uword msg;
142
143 q = am->vl_input_queue;
144
Ole Troandfc9b7c2017-03-06 23:51:57 +0100145 while (1)
Mohsin Kazmi3fca5672018-01-04 18:57:26 +0100146 while (!svm_queue_sub(q, (u8 *)&msg, SVM_Q_WAIT, 0))
Ole Troandfc9b7c2017-03-06 23:51:57 +0100147 {
148 u16 id = ntohs(*((u16 *)msg));
149 switch (id) {
150 case VL_API_RX_THREAD_EXIT:
151 vl_msg_api_free((void *) msg);
152 /* signal waiting threads that this thread is about to terminate */
153 pthread_mutex_lock(&pm->queue_lock);
154 pthread_cond_signal(&pm->terminate_cv);
155 pthread_mutex_unlock(&pm->queue_lock);
156 pthread_exit(0);
157 return 0;
158 break;
159
160 case VL_API_MEMCLNT_RX_THREAD_SUSPEND:
161 vl_msg_api_free((void * )msg);
162 /* Suspend thread and signal reader */
163 pthread_mutex_lock(&pm->queue_lock);
164 pthread_cond_signal(&pm->suspend_cv);
165 /* Wait for the resume signal */
166 pthread_cond_wait (&pm->resume_cv, &pm->queue_lock);
167 pthread_mutex_unlock(&pm->queue_lock);
168 break;
169
170 case VL_API_MEMCLNT_READ_TIMEOUT:
171 clib_warning("Received read timeout in async thread\n");
172 vl_msg_api_free((void *) msg);
173 break;
174
Dave Barach59b25652017-09-10 15:04:27 -0400175 case VL_API_MEMCLNT_KEEPALIVE:
176 mp = (void *)msg;
177 rmp = vl_msg_api_alloc (sizeof (*rmp));
178 memset (rmp, 0, sizeof (*rmp));
179 rmp->_vl_msg_id = ntohs(VL_API_MEMCLNT_KEEPALIVE_REPLY);
180 rmp->context = mp->context;
181 shmem_hdr = am->shmem_hdr;
182 vl_msg_api_send_shmem(shmem_hdr->vl_input_queue, (u8 *)&rmp);
183 vl_msg_api_free((void *) msg);
184 break;
185
Ole Troandfc9b7c2017-03-06 23:51:57 +0100186 default:
Damjan Marion5fec1e82017-04-13 19:13:47 +0200187 vac_api_handler((void *)msg);
Ole Troandfc9b7c2017-03-06 23:51:57 +0100188 }
189 }
190}
191
192static void *
Damjan Marion5fec1e82017-04-13 19:13:47 +0200193vac_timeout_thread_fn (void *arg)
Ole Troandfc9b7c2017-03-06 23:51:57 +0100194{
195 vl_api_memclnt_read_timeout_t *ep;
Damjan Marion5fec1e82017-04-13 19:13:47 +0200196 vac_main_t *pm = &vac_main;
Ole Troandfc9b7c2017-03-06 23:51:57 +0100197 api_main_t *am = &api_main;
198 struct timespec ts;
199 struct timeval tv;
200 u16 timeout;
201 int rv;
202
203 while (1)
204 {
205 /* Wait for poke */
206 pthread_mutex_lock(&pm->timeout_lock);
207 pthread_cond_wait (&pm->timeout_cv, &pm->timeout_lock);
208 timeout = read_timeout;
209 gettimeofday(&tv, NULL);
210 ts.tv_sec = tv.tv_sec + timeout;
211 ts.tv_nsec = 0;
212 rv = pthread_cond_timedwait (&pm->timeout_cancel_cv,
213 &pm->timeout_lock, &ts);
214 pthread_mutex_unlock(&pm->timeout_lock);
215 if (rv == ETIMEDOUT)
216 {
217 ep = vl_msg_api_alloc (sizeof (*ep));
218 ep->_vl_msg_id = ntohs(VL_API_MEMCLNT_READ_TIMEOUT);
219 vl_msg_api_send_shmem(am->vl_input_queue, (u8 *)&ep);
220 }
221 }
Damjan Marion7cd468a2016-12-19 23:05:39 +0100222 pthread_exit(0);
223}
224
Ole Troandfc9b7c2017-03-06 23:51:57 +0100225void
Damjan Marion5fec1e82017-04-13 19:13:47 +0200226vac_rx_suspend (void)
Ole Troandfc9b7c2017-03-06 23:51:57 +0100227{
228 api_main_t *am = &api_main;
Damjan Marion5fec1e82017-04-13 19:13:47 +0200229 vac_main_t *pm = &vac_main;
Ole Troandfc9b7c2017-03-06 23:51:57 +0100230 vl_api_memclnt_rx_thread_suspend_t *ep;
231
232 if (!pm->rx_thread_handle) return;
233 pthread_mutex_lock(&pm->queue_lock);
234 if (rx_is_running)
235 {
236 ep = vl_msg_api_alloc (sizeof (*ep));
237 ep->_vl_msg_id = ntohs(VL_API_MEMCLNT_RX_THREAD_SUSPEND);
238 vl_msg_api_send_shmem(am->vl_input_queue, (u8 *)&ep);
239 /* Wait for RX thread to tell us it has suspendend */
240 pthread_cond_wait(&pm->suspend_cv, &pm->queue_lock);
241 rx_is_running = false;
242 }
243 pthread_mutex_unlock(&pm->queue_lock);
244}
245
246void
Damjan Marion5fec1e82017-04-13 19:13:47 +0200247vac_rx_resume (void)
Ole Troandfc9b7c2017-03-06 23:51:57 +0100248{
Damjan Marion5fec1e82017-04-13 19:13:47 +0200249 vac_main_t *pm = &vac_main;
Ole Troandfc9b7c2017-03-06 23:51:57 +0100250 if (!pm->rx_thread_handle) return;
251 pthread_mutex_lock(&pm->queue_lock);
Ole Troanad0697a2017-03-09 21:10:45 +0100252 if (rx_is_running) goto unlock;
Ole Troandfc9b7c2017-03-06 23:51:57 +0100253 pthread_cond_signal(&pm->resume_cv);
254 rx_is_running = true;
Ole Troanad0697a2017-03-09 21:10:45 +0100255 unlock:
Ole Troandfc9b7c2017-03-06 23:51:57 +0100256 pthread_mutex_unlock(&pm->queue_lock);
257}
258
Ole Troan3cc49712017-03-08 12:02:24 +0100259static uword *
Damjan Marion5fec1e82017-04-13 19:13:47 +0200260vac_msg_table_get_hash (void)
Damjan Marion7cd468a2016-12-19 23:05:39 +0100261{
262 api_main_t *am = &api_main;
263 return (am->msg_index_by_name_and_crc);
264}
265
266int
Damjan Marion5fec1e82017-04-13 19:13:47 +0200267vac_msg_table_size(void)
Damjan Marion7cd468a2016-12-19 23:05:39 +0100268{
269 api_main_t *am = &api_main;
270 return hash_elts(am->msg_index_by_name_and_crc);
271}
272
273int
Damjan Marion5fec1e82017-04-13 19:13:47 +0200274vac_connect (char * name, char * chroot_prefix, vac_callback_t cb,
Dave Barachf9526922017-01-06 16:33:06 -0500275 int rx_qlen)
Damjan Marion7cd468a2016-12-19 23:05:39 +0100276{
277 int rv = 0;
Damjan Marion5fec1e82017-04-13 19:13:47 +0200278 vac_main_t *pm = &vac_main;
Damjan Marion7cd468a2016-12-19 23:05:39 +0100279
Ole Troandfc9b7c2017-03-06 23:51:57 +0100280 init();
Damjan Marion7cd468a2016-12-19 23:05:39 +0100281 if (chroot_prefix != NULL)
282 vl_set_memory_root_path (chroot_prefix);
283
284 if ((rv = vl_client_api_map("/vpe-api"))) {
285 clib_warning ("vl_client_api map rv %d", rv);
286 return rv;
287 }
288
Dave Barachf9526922017-01-06 16:33:06 -0500289 if (vl_client_connect(name, 0, rx_qlen) < 0) {
Damjan Marion7cd468a2016-12-19 23:05:39 +0100290 vl_client_api_unmap();
291 return (-1);
292 }
293
294 if (cb) {
295 /* Start the rx queue thread */
Damjan Marion5fec1e82017-04-13 19:13:47 +0200296 rv = pthread_create(&pm->rx_thread_handle, NULL, vac_rx_thread_fn, 0);
Damjan Marion7cd468a2016-12-19 23:05:39 +0100297 if (rv) {
298 clib_warning("pthread_create returned %d", rv);
299 vl_client_api_unmap();
300 return (-1);
301 }
Damjan Marion5fec1e82017-04-13 19:13:47 +0200302 vac_callback = cb;
Ole Troandfc9b7c2017-03-06 23:51:57 +0100303 rx_is_running = true;
304 }
305
306 /* Start read timeout thread */
307 rv = pthread_create(&pm->timeout_thread_handle, NULL,
Damjan Marion5fec1e82017-04-13 19:13:47 +0200308 vac_timeout_thread_fn, 0);
Ole Troandfc9b7c2017-03-06 23:51:57 +0100309 if (rv) {
310 clib_warning("pthread_create returned %d", rv);
311 vl_client_api_unmap();
312 return (-1);
Damjan Marion7cd468a2016-12-19 23:05:39 +0100313 }
314
315 pm->connected_to_vlib = 1;
316
317 return (0);
318}
319
320int
Damjan Marion5fec1e82017-04-13 19:13:47 +0200321vac_disconnect (void)
Damjan Marion7cd468a2016-12-19 23:05:39 +0100322{
323 api_main_t *am = &api_main;
Damjan Marion5fec1e82017-04-13 19:13:47 +0200324 vac_main_t *pm = &vac_main;
Damjan Marion7cd468a2016-12-19 23:05:39 +0100325
Ole Troandfc9b7c2017-03-06 23:51:57 +0100326 if (!pm->connected_to_vlib) return 0;
327
328 if (pm->rx_thread_handle) {
Damjan Marion7cd468a2016-12-19 23:05:39 +0100329 vl_api_rx_thread_exit_t *ep;
330 uword junk;
331 ep = vl_msg_api_alloc (sizeof (*ep));
332 ep->_vl_msg_id = ntohs(VL_API_RX_THREAD_EXIT);
333 vl_msg_api_send_shmem(am->vl_input_queue, (u8 *)&ep);
Ole Troandfc9b7c2017-03-06 23:51:57 +0100334
335 /* wait (with timeout) until RX thread has finished */
336 struct timespec ts;
337 struct timeval tv;
338 gettimeofday(&tv, NULL);
339 ts.tv_sec = tv.tv_sec + 5;
340 ts.tv_nsec = 0;
341 pthread_mutex_lock(&pm->queue_lock);
342 int rv = pthread_cond_timedwait(&pm->terminate_cv, &pm->queue_lock, &ts);
343 pthread_mutex_unlock(&pm->queue_lock);
344 /* now join so we wait until thread has -really- finished */
345 if (rv == ETIMEDOUT)
346 pthread_cancel(pm->rx_thread_handle);
347 else
348 pthread_join(pm->rx_thread_handle, (void **) &junk);
Damjan Marion7cd468a2016-12-19 23:05:39 +0100349 }
Ole Troandfc9b7c2017-03-06 23:51:57 +0100350 if (pm->timeout_thread_handle)
351 pthread_cancel(pm->timeout_thread_handle);
352
353 vl_client_disconnect();
354 vl_client_api_unmap();
Damjan Marion5fec1e82017-04-13 19:13:47 +0200355 vac_callback = 0;
Ole Troandfc9b7c2017-03-06 23:51:57 +0100356
357 cleanup();
Damjan Marion7cd468a2016-12-19 23:05:39 +0100358
359 return (0);
360}
361
Ole Troandfc9b7c2017-03-06 23:51:57 +0100362static void
363set_timeout (unsigned short timeout)
364{
Damjan Marion5fec1e82017-04-13 19:13:47 +0200365 vac_main_t *pm = &vac_main;
Ole Troandfc9b7c2017-03-06 23:51:57 +0100366 pthread_mutex_lock(&pm->timeout_lock);
367 read_timeout = timeout;
368 pthread_cond_signal(&pm->timeout_cv);
369 pthread_mutex_unlock(&pm->timeout_lock);
370}
371
372static void
373unset_timeout (void)
374{
Damjan Marion5fec1e82017-04-13 19:13:47 +0200375 vac_main_t *pm = &vac_main;
Ole Troandfc9b7c2017-03-06 23:51:57 +0100376 pthread_mutex_lock(&pm->timeout_lock);
377 pthread_cond_signal(&pm->timeout_cancel_cv);
378 pthread_mutex_unlock(&pm->timeout_lock);
379}
380
Damjan Marion7cd468a2016-12-19 23:05:39 +0100381int
Damjan Marion5fec1e82017-04-13 19:13:47 +0200382vac_read (char **p, int *l, u16 timeout)
Damjan Marion7cd468a2016-12-19 23:05:39 +0100383{
Florin Corase86a8ed2018-01-05 03:20:25 -0800384 svm_queue_t *q;
Damjan Marion7cd468a2016-12-19 23:05:39 +0100385 api_main_t *am = &api_main;
Damjan Marion5fec1e82017-04-13 19:13:47 +0200386 vac_main_t *pm = &vac_main;
Dave Barach59b25652017-09-10 15:04:27 -0400387 vl_api_memclnt_keepalive_t *mp;
388 vl_api_memclnt_keepalive_reply_t *rmp;
Damjan Marion7cd468a2016-12-19 23:05:39 +0100389 uword msg;
Ole Troandfc9b7c2017-03-06 23:51:57 +0100390 msgbuf_t *msgbuf;
Dave Barach59b25652017-09-10 15:04:27 -0400391 int rv;
392 vl_shmem_hdr_t *shmem_hdr;
Damjan Marion7cd468a2016-12-19 23:05:39 +0100393
394 if (!pm->connected_to_vlib) return -1;
395
396 *l = 0;
397
398 if (am->our_pid == 0) return (-1);
399
Ole Troandfc9b7c2017-03-06 23:51:57 +0100400 /* Poke timeout thread */
401 if (timeout)
402 set_timeout(timeout);
403
Damjan Marion7cd468a2016-12-19 23:05:39 +0100404 q = am->vl_input_queue;
Dave Barach59b25652017-09-10 15:04:27 -0400405
406 again:
Mohsin Kazmi3fca5672018-01-04 18:57:26 +0100407 rv = svm_queue_sub(q, (u8 *)&msg, SVM_Q_WAIT, 0);
408
Damjan Marion7cd468a2016-12-19 23:05:39 +0100409 if (rv == 0) {
410 u16 msg_id = ntohs(*((u16 *)msg));
Ole Troandfc9b7c2017-03-06 23:51:57 +0100411 switch (msg_id) {
412 case VL_API_RX_THREAD_EXIT:
413 printf("Received thread exit\n");
414 return -1;
415 case VL_API_MEMCLNT_RX_THREAD_SUSPEND:
416 printf("Received thread suspend\n");
417 goto error;
418 case VL_API_MEMCLNT_READ_TIMEOUT:
419 printf("Received read timeout %ds\n", timeout);
420 goto error;
Dave Barach59b25652017-09-10 15:04:27 -0400421 case VL_API_MEMCLNT_KEEPALIVE:
422 /* Handle an alive-check ping from vpp. */
423 mp = (void *)msg;
424 rmp = vl_msg_api_alloc (sizeof (*rmp));
425 memset (rmp, 0, sizeof (*rmp));
426 rmp->_vl_msg_id = ntohs(VL_API_MEMCLNT_KEEPALIVE_REPLY);
427 rmp->context = mp->context;
428 shmem_hdr = am->shmem_hdr;
429 vl_msg_api_send_shmem(shmem_hdr->vl_input_queue, (u8 *)&rmp);
430 vl_msg_api_free((void *) msg);
431 /*
432 * Python code is blissfully unaware of these pings, so
433 * act as if it never happened...
434 */
435 goto again;
Ole Troandfc9b7c2017-03-06 23:51:57 +0100436
437 default:
438 msgbuf = (msgbuf_t *)(((u8 *)msg) - offsetof(msgbuf_t, data));
439 *l = ntohl(msgbuf->data_len);
440 if (*l == 0) {
441 printf("Unregistered API message: %d\n", msg_id);
442 goto error;
443 }
Damjan Marion7cd468a2016-12-19 23:05:39 +0100444 }
445 *p = (char *)msg;
Ole Troandfc9b7c2017-03-06 23:51:57 +0100446
447 /* Let timeout notification thread know we're done */
448 unset_timeout();
449
Damjan Marion7cd468a2016-12-19 23:05:39 +0100450 } else {
451 printf("Read failed with %d\n", rv);
452 }
453 return (rv);
Ole Troandfc9b7c2017-03-06 23:51:57 +0100454
455 error:
456 vl_msg_api_free((void *) msg);
457 /* Client might forget to resume RX thread on failure */
Damjan Marion5fec1e82017-04-13 19:13:47 +0200458 vac_rx_resume ();
Ole Troandfc9b7c2017-03-06 23:51:57 +0100459 return -1;
Damjan Marion7cd468a2016-12-19 23:05:39 +0100460}
461
462/*
463 * XXX: Makes the assumption that client_index is the first member
464 */
465typedef VL_API_PACKED(struct _vl_api_header {
466 u16 _vl_msg_id;
467 u32 client_index;
468}) vl_api_header_t;
469
470static unsigned int
Damjan Marion5fec1e82017-04-13 19:13:47 +0200471vac_client_index (void)
Damjan Marion7cd468a2016-12-19 23:05:39 +0100472{
473 return (api_main.my_client_index);
474}
475
476int
Damjan Marion5fec1e82017-04-13 19:13:47 +0200477vac_write (char *p, int l)
Damjan Marion7cd468a2016-12-19 23:05:39 +0100478{
479 int rv = -1;
480 api_main_t *am = &api_main;
481 vl_api_header_t *mp = vl_msg_api_alloc(l);
Florin Corase86a8ed2018-01-05 03:20:25 -0800482 svm_queue_t *q;
Damjan Marion5fec1e82017-04-13 19:13:47 +0200483 vac_main_t *pm = &vac_main;
Damjan Marion7cd468a2016-12-19 23:05:39 +0100484
485 if (!pm->connected_to_vlib) return -1;
486 if (!mp) return (-1);
Ole Troandfc9b7c2017-03-06 23:51:57 +0100487
Damjan Marion7cd468a2016-12-19 23:05:39 +0100488 memcpy(mp, p, l);
Damjan Marion5fec1e82017-04-13 19:13:47 +0200489 mp->client_index = vac_client_index();
Damjan Marion7cd468a2016-12-19 23:05:39 +0100490 q = am->shmem_hdr->vl_input_queue;
Florin Corase86a8ed2018-01-05 03:20:25 -0800491 rv = svm_queue_add(q, (u8 *)&mp, 0);
Damjan Marion7cd468a2016-12-19 23:05:39 +0100492 if (rv != 0) {
Ole Troandfc9b7c2017-03-06 23:51:57 +0100493 clib_warning("vpe_api_write fails: %d\n", rv);
Damjan Marion7cd468a2016-12-19 23:05:39 +0100494 /* Clear message */
Damjan Marion5fec1e82017-04-13 19:13:47 +0200495 vac_free(mp);
Damjan Marion7cd468a2016-12-19 23:05:39 +0100496 }
497 return (rv);
498}
499
Ole Troan3cc49712017-03-08 12:02:24 +0100500int
Damjan Marion5fec1e82017-04-13 19:13:47 +0200501vac_get_msg_index (unsigned char * name)
Damjan Marion7cd468a2016-12-19 23:05:39 +0100502{
Florin Corase86a8ed2018-01-05 03:20:25 -0800503 return vl_msg_api_get_msg_index (name);
Damjan Marion7cd468a2016-12-19 23:05:39 +0100504}
Ole Troan3cc49712017-03-08 12:02:24 +0100505
506int
Damjan Marion5fec1e82017-04-13 19:13:47 +0200507vac_msg_table_max_index(void)
Ole Troan3cc49712017-03-08 12:02:24 +0100508{
509 int max = 0;
510 hash_pair_t *hp;
Damjan Marion5fec1e82017-04-13 19:13:47 +0200511 uword *h = vac_msg_table_get_hash();
Ole Troan3cc49712017-03-08 12:02:24 +0100512 hash_foreach_pair (hp, h,
513 ({
514 if (hp->value[0] > max)
515 max = hp->value[0];
516 }));
517
518 return max;
519}
520
521void
Damjan Marion5fec1e82017-04-13 19:13:47 +0200522vac_set_error_handler (vac_error_callback_t cb)
Ole Troan3cc49712017-03-08 12:02:24 +0100523{
524 if (cb) clib_error_register_handler (cb, 0);
525}