blob: a57db28925070b8b2371bf8a91df862c380426e6 [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 Troande728ac2018-10-08 11:24:22 +020036bool timeout_cancelled;
37bool timeout_in_progress;
38
Ole Troandfc9b7c2017-03-06 23:51:57 +010039/*
40 * Asynchronous mode:
41 * Client registers a callback. All messages are sent to the callback.
42 * Synchronous mode:
43 * Client calls blocking read().
44 * Clients are expected to collate events on a queue.
Damjan Marion5fec1e82017-04-13 19:13:47 +020045 * vac_write() -> suspends RX thread
46 * vac_read() -> resumes RX thread
Ole Troandfc9b7c2017-03-06 23:51:57 +010047 */
48
Damjan Marion7cd468a2016-12-19 23:05:39 +010049#define vl_typedefs /* define message structures */
50#include <vpp/api/vpe_all_api_h.h>
51#undef vl_typedefs
52
53#define vl_endianfun /* define message structures */
54#include <vpp/api/vpe_all_api_h.h>
55#undef vl_endianfun
56
57vlib_main_t vlib_global_main;
58vlib_main_t **vlib_mains;
59
60typedef struct {
Damjan Marion7cd468a2016-12-19 23:05:39 +010061 u8 connected_to_vlib;
Damjan Marion7cd468a2016-12-19 23:05:39 +010062 pthread_t rx_thread_handle;
Ole Troandfc9b7c2017-03-06 23:51:57 +010063 pthread_t timeout_thread_handle;
64 pthread_mutex_t queue_lock;
65 pthread_cond_t suspend_cv;
66 pthread_cond_t resume_cv;
67 pthread_mutex_t timeout_lock;
Neale Ranns1d652792018-07-26 08:05:53 -070068 u8 timeout_loop;
Ole Troandfc9b7c2017-03-06 23:51:57 +010069 pthread_cond_t timeout_cv;
70 pthread_cond_t timeout_cancel_cv;
71 pthread_cond_t terminate_cv;
Damjan Marion5fec1e82017-04-13 19:13:47 +020072} vac_main_t;
Damjan Marion7cd468a2016-12-19 23:05:39 +010073
Damjan Marion5fec1e82017-04-13 19:13:47 +020074vac_main_t vac_main;
75vac_callback_t vac_callback;
Ole Troandfc9b7c2017-03-06 23:51:57 +010076u16 read_timeout = 0;
77bool rx_is_running = false;
Ole Troan4e588aa2018-09-07 11:01:47 +020078bool timeout_thread_cancelled = false;
Ole Troandfc9b7c2017-03-06 23:51:57 +010079
Ole Troan73710c72018-06-04 22:27:49 +020080/* Set to true to enable memory tracing */
81bool mem_trace = false;
82
83__attribute__((constructor))
84static void
85vac_client_constructor (void)
86{
Ole Troan73710c72018-06-04 22:27:49 +020087 clib_mem_init (0, 1 << 30);
Dave Barach6a5adc32018-07-04 10:56:23 -040088#if USE_DLMALLOC == 0
89 {
90 u8 *heap;
91 mheap_t *h;
92
93 heap = clib_mem_get_per_cpu_heap ();
94 h = mheap_header (heap);
95 /* make the main heap thread-safe */
96 h->flags |= MHEAP_FLAG_THREAD_SAFE;
97 }
98#endif
Ole Troan73710c72018-06-04 22:27:49 +020099 if (mem_trace)
100 clib_mem_trace (1);
101}
102
103__attribute__((destructor))
104static void
105vac_client_destructor (void)
106{
107 if (mem_trace)
108 fformat(stderr, "TRACE: %s",
109 format (0, "%U\n",
110 format_mheap, clib_mem_get_heap (), 1));
111}
112
113
Ole Troandfc9b7c2017-03-06 23:51:57 +0100114static void
115init (void)
116{
Damjan Marion5fec1e82017-04-13 19:13:47 +0200117 vac_main_t *pm = &vac_main;
Ole Troandfc9b7c2017-03-06 23:51:57 +0100118 memset(pm, 0, sizeof(*pm));
119 pthread_mutex_init(&pm->queue_lock, NULL);
120 pthread_cond_init(&pm->suspend_cv, NULL);
121 pthread_cond_init(&pm->resume_cv, NULL);
122 pthread_mutex_init(&pm->timeout_lock, NULL);
Neale Ranns1d652792018-07-26 08:05:53 -0700123 pm->timeout_loop = 1;
Ole Troandfc9b7c2017-03-06 23:51:57 +0100124 pthread_cond_init(&pm->timeout_cv, NULL);
125 pthread_cond_init(&pm->timeout_cancel_cv, NULL);
126 pthread_cond_init(&pm->terminate_cv, NULL);
127}
128
129static void
130cleanup (void)
131{
Damjan Marion5fec1e82017-04-13 19:13:47 +0200132 vac_main_t *pm = &vac_main;
Ole Troan73710c72018-06-04 22:27:49 +0200133 pthread_mutex_destroy(&pm->queue_lock);
Ole Troandfc9b7c2017-03-06 23:51:57 +0100134 pthread_cond_destroy(&pm->suspend_cv);
135 pthread_cond_destroy(&pm->resume_cv);
Ole Troan73710c72018-06-04 22:27:49 +0200136 pthread_mutex_destroy(&pm->timeout_lock);
Ole Troandfc9b7c2017-03-06 23:51:57 +0100137 pthread_cond_destroy(&pm->timeout_cv);
138 pthread_cond_destroy(&pm->timeout_cancel_cv);
139 pthread_cond_destroy(&pm->terminate_cv);
Ole Troan73710c72018-06-04 22:27:49 +0200140 memset(pm, 0, sizeof(*pm));
Ole Troandfc9b7c2017-03-06 23:51:57 +0100141}
Damjan Marion7cd468a2016-12-19 23:05:39 +0100142
143/*
144 * Satisfy external references when -lvlib is not available.
145 */
146void vlib_cli_output (struct vlib_main_t * vm, char * fmt, ...)
147{
148 clib_warning ("vlib_cli_output called...");
149}
150
151void
Damjan Marion5fec1e82017-04-13 19:13:47 +0200152vac_free (void * msg)
Damjan Marion7cd468a2016-12-19 23:05:39 +0100153{
154 vl_msg_api_free (msg);
155}
156
157static void
Damjan Marion5fec1e82017-04-13 19:13:47 +0200158vac_api_handler (void *msg)
Damjan Marion7cd468a2016-12-19 23:05:39 +0100159{
160 u16 id = ntohs(*((u16 *)msg));
Damjan Marion7cd468a2016-12-19 23:05:39 +0100161 msgbuf_t *msgbuf = (msgbuf_t *)(((u8 *)msg) - offsetof(msgbuf_t, data));
162 int l = ntohl(msgbuf->data_len);
163 if (l == 0)
164 clib_warning("Message ID %d has wrong length: %d\n", id, l);
165
166 /* Call Python callback */
Damjan Marion5fec1e82017-04-13 19:13:47 +0200167 ASSERT(vac_callback);
168 (vac_callback)(msg, l);
169 vac_free(msg);
Damjan Marion7cd468a2016-12-19 23:05:39 +0100170}
171
172static void *
Damjan Marion5fec1e82017-04-13 19:13:47 +0200173vac_rx_thread_fn (void *arg)
Damjan Marion7cd468a2016-12-19 23:05:39 +0100174{
Florin Corase86a8ed2018-01-05 03:20:25 -0800175 svm_queue_t *q;
Dave Barach59b25652017-09-10 15:04:27 -0400176 vl_api_memclnt_keepalive_t *mp;
177 vl_api_memclnt_keepalive_reply_t *rmp;
Damjan Marion5fec1e82017-04-13 19:13:47 +0200178 vac_main_t *pm = &vac_main;
Damjan Marion7cd468a2016-12-19 23:05:39 +0100179 api_main_t *am = &api_main;
Dave Barach59b25652017-09-10 15:04:27 -0400180 vl_shmem_hdr_t *shmem_hdr;
Damjan Marion7cd468a2016-12-19 23:05:39 +0100181 uword msg;
182
183 q = am->vl_input_queue;
184
Ole Troandfc9b7c2017-03-06 23:51:57 +0100185 while (1)
Mohsin Kazmi3fca5672018-01-04 18:57:26 +0100186 while (!svm_queue_sub(q, (u8 *)&msg, SVM_Q_WAIT, 0))
Ole Troandfc9b7c2017-03-06 23:51:57 +0100187 {
188 u16 id = ntohs(*((u16 *)msg));
189 switch (id) {
190 case VL_API_RX_THREAD_EXIT:
191 vl_msg_api_free((void *) msg);
192 /* signal waiting threads that this thread is about to terminate */
193 pthread_mutex_lock(&pm->queue_lock);
194 pthread_cond_signal(&pm->terminate_cv);
195 pthread_mutex_unlock(&pm->queue_lock);
196 pthread_exit(0);
197 return 0;
198 break;
199
200 case VL_API_MEMCLNT_RX_THREAD_SUSPEND:
201 vl_msg_api_free((void * )msg);
202 /* Suspend thread and signal reader */
203 pthread_mutex_lock(&pm->queue_lock);
204 pthread_cond_signal(&pm->suspend_cv);
205 /* Wait for the resume signal */
206 pthread_cond_wait (&pm->resume_cv, &pm->queue_lock);
207 pthread_mutex_unlock(&pm->queue_lock);
208 break;
209
210 case VL_API_MEMCLNT_READ_TIMEOUT:
211 clib_warning("Received read timeout in async thread\n");
212 vl_msg_api_free((void *) msg);
213 break;
214
Dave Barach59b25652017-09-10 15:04:27 -0400215 case VL_API_MEMCLNT_KEEPALIVE:
216 mp = (void *)msg;
217 rmp = vl_msg_api_alloc (sizeof (*rmp));
218 memset (rmp, 0, sizeof (*rmp));
219 rmp->_vl_msg_id = ntohs(VL_API_MEMCLNT_KEEPALIVE_REPLY);
220 rmp->context = mp->context;
221 shmem_hdr = am->shmem_hdr;
222 vl_msg_api_send_shmem(shmem_hdr->vl_input_queue, (u8 *)&rmp);
223 vl_msg_api_free((void *) msg);
224 break;
225
Ole Troandfc9b7c2017-03-06 23:51:57 +0100226 default:
Damjan Marion5fec1e82017-04-13 19:13:47 +0200227 vac_api_handler((void *)msg);
Ole Troandfc9b7c2017-03-06 23:51:57 +0100228 }
229 }
230}
231
232static void *
Damjan Marion5fec1e82017-04-13 19:13:47 +0200233vac_timeout_thread_fn (void *arg)
Ole Troandfc9b7c2017-03-06 23:51:57 +0100234{
235 vl_api_memclnt_read_timeout_t *ep;
Damjan Marion5fec1e82017-04-13 19:13:47 +0200236 vac_main_t *pm = &vac_main;
Ole Troandfc9b7c2017-03-06 23:51:57 +0100237 api_main_t *am = &api_main;
238 struct timespec ts;
239 struct timeval tv;
Ole Troandfc9b7c2017-03-06 23:51:57 +0100240 int rv;
241
Neale Ranns1d652792018-07-26 08:05:53 -0700242 while (pm->timeout_loop)
Ole Troandfc9b7c2017-03-06 23:51:57 +0100243 {
244 /* Wait for poke */
245 pthread_mutex_lock(&pm->timeout_lock);
Ole Troande728ac2018-10-08 11:24:22 +0200246 while (!timeout_in_progress)
247 pthread_cond_wait (&pm->timeout_cv, &pm->timeout_lock);
248
249 /* Starting timer */
Ole Troandfc9b7c2017-03-06 23:51:57 +0100250 gettimeofday(&tv, NULL);
Ole Troande728ac2018-10-08 11:24:22 +0200251 ts.tv_sec = tv.tv_sec + read_timeout;
Ole Troandfc9b7c2017-03-06 23:51:57 +0100252 ts.tv_nsec = 0;
Ole Troande728ac2018-10-08 11:24:22 +0200253
254 if (!timeout_cancelled) {
255 rv = pthread_cond_timedwait (&pm->timeout_cancel_cv,
256 &pm->timeout_lock, &ts);
257 if (rv == ETIMEDOUT && !timeout_thread_cancelled) {
Ole Troandfc9b7c2017-03-06 23:51:57 +0100258 ep = vl_msg_api_alloc (sizeof (*ep));
259 ep->_vl_msg_id = ntohs(VL_API_MEMCLNT_READ_TIMEOUT);
260 vl_msg_api_send_shmem(am->vl_input_queue, (u8 *)&ep);
261 }
Ole Troande728ac2018-10-08 11:24:22 +0200262 }
263
264 pthread_mutex_unlock(&pm->timeout_lock);
Ole Troandfc9b7c2017-03-06 23:51:57 +0100265 }
Damjan Marion7cd468a2016-12-19 23:05:39 +0100266 pthread_exit(0);
267}
268
Ole Troandfc9b7c2017-03-06 23:51:57 +0100269void
Damjan Marion5fec1e82017-04-13 19:13:47 +0200270vac_rx_suspend (void)
Ole Troandfc9b7c2017-03-06 23:51:57 +0100271{
272 api_main_t *am = &api_main;
Damjan Marion5fec1e82017-04-13 19:13:47 +0200273 vac_main_t *pm = &vac_main;
Ole Troandfc9b7c2017-03-06 23:51:57 +0100274 vl_api_memclnt_rx_thread_suspend_t *ep;
275
276 if (!pm->rx_thread_handle) return;
277 pthread_mutex_lock(&pm->queue_lock);
278 if (rx_is_running)
279 {
280 ep = vl_msg_api_alloc (sizeof (*ep));
281 ep->_vl_msg_id = ntohs(VL_API_MEMCLNT_RX_THREAD_SUSPEND);
282 vl_msg_api_send_shmem(am->vl_input_queue, (u8 *)&ep);
Paul Vinciguerraec11b132018-09-24 05:25:00 -0700283 /* Wait for RX thread to tell us it has suspended */
Ole Troandfc9b7c2017-03-06 23:51:57 +0100284 pthread_cond_wait(&pm->suspend_cv, &pm->queue_lock);
285 rx_is_running = false;
286 }
287 pthread_mutex_unlock(&pm->queue_lock);
288}
289
290void
Damjan Marion5fec1e82017-04-13 19:13:47 +0200291vac_rx_resume (void)
Ole Troandfc9b7c2017-03-06 23:51:57 +0100292{
Damjan Marion5fec1e82017-04-13 19:13:47 +0200293 vac_main_t *pm = &vac_main;
Ole Troandfc9b7c2017-03-06 23:51:57 +0100294 if (!pm->rx_thread_handle) return;
295 pthread_mutex_lock(&pm->queue_lock);
Ole Troanad0697a2017-03-09 21:10:45 +0100296 if (rx_is_running) goto unlock;
Ole Troandfc9b7c2017-03-06 23:51:57 +0100297 pthread_cond_signal(&pm->resume_cv);
298 rx_is_running = true;
Ole Troanad0697a2017-03-09 21:10:45 +0100299 unlock:
Ole Troandfc9b7c2017-03-06 23:51:57 +0100300 pthread_mutex_unlock(&pm->queue_lock);
301}
302
Ole Troan3cc49712017-03-08 12:02:24 +0100303static uword *
Damjan Marion5fec1e82017-04-13 19:13:47 +0200304vac_msg_table_get_hash (void)
Damjan Marion7cd468a2016-12-19 23:05:39 +0100305{
306 api_main_t *am = &api_main;
307 return (am->msg_index_by_name_and_crc);
308}
309
310int
Damjan Marion5fec1e82017-04-13 19:13:47 +0200311vac_msg_table_size(void)
Damjan Marion7cd468a2016-12-19 23:05:39 +0100312{
313 api_main_t *am = &api_main;
314 return hash_elts(am->msg_index_by_name_and_crc);
315}
316
317int
Damjan Marion5fec1e82017-04-13 19:13:47 +0200318vac_connect (char * name, char * chroot_prefix, vac_callback_t cb,
Dave Barachf9526922017-01-06 16:33:06 -0500319 int rx_qlen)
Damjan Marion7cd468a2016-12-19 23:05:39 +0100320{
321 int rv = 0;
Damjan Marion5fec1e82017-04-13 19:13:47 +0200322 vac_main_t *pm = &vac_main;
Damjan Marion7cd468a2016-12-19 23:05:39 +0100323
Ole Troandfc9b7c2017-03-06 23:51:57 +0100324 init();
Damjan Marion7cd468a2016-12-19 23:05:39 +0100325 if (chroot_prefix != NULL)
326 vl_set_memory_root_path (chroot_prefix);
327
328 if ((rv = vl_client_api_map("/vpe-api"))) {
Ondrej Fabrye29cb672018-08-16 08:36:19 +0200329 clib_warning ("vl_client_api_map returned %d", rv);
Damjan Marion7cd468a2016-12-19 23:05:39 +0100330 return rv;
331 }
332
Dave Barachf9526922017-01-06 16:33:06 -0500333 if (vl_client_connect(name, 0, rx_qlen) < 0) {
Damjan Marion7cd468a2016-12-19 23:05:39 +0100334 vl_client_api_unmap();
335 return (-1);
336 }
337
338 if (cb) {
339 /* Start the rx queue thread */
Damjan Marion5fec1e82017-04-13 19:13:47 +0200340 rv = pthread_create(&pm->rx_thread_handle, NULL, vac_rx_thread_fn, 0);
Damjan Marion7cd468a2016-12-19 23:05:39 +0100341 if (rv) {
342 clib_warning("pthread_create returned %d", rv);
343 vl_client_api_unmap();
344 return (-1);
345 }
Damjan Marion5fec1e82017-04-13 19:13:47 +0200346 vac_callback = cb;
Ole Troandfc9b7c2017-03-06 23:51:57 +0100347 rx_is_running = true;
348 }
349
350 /* Start read timeout thread */
351 rv = pthread_create(&pm->timeout_thread_handle, NULL,
Damjan Marion5fec1e82017-04-13 19:13:47 +0200352 vac_timeout_thread_fn, 0);
Ole Troandfc9b7c2017-03-06 23:51:57 +0100353 if (rv) {
354 clib_warning("pthread_create returned %d", rv);
355 vl_client_api_unmap();
356 return (-1);
Damjan Marion7cd468a2016-12-19 23:05:39 +0100357 }
358
359 pm->connected_to_vlib = 1;
360
361 return (0);
362}
Neale Rannsc16f6b32018-06-03 21:21:19 -0700363static void
364set_timeout (unsigned short timeout)
365{
366 vac_main_t *pm = &vac_main;
367 pthread_mutex_lock(&pm->timeout_lock);
368 read_timeout = timeout;
Ole Troande728ac2018-10-08 11:24:22 +0200369 timeout_in_progress = true;
370 timeout_cancelled = false;
Neale Rannsc16f6b32018-06-03 21:21:19 -0700371 pthread_cond_signal(&pm->timeout_cv);
372 pthread_mutex_unlock(&pm->timeout_lock);
373}
374
375static void
376unset_timeout (void)
377{
378 vac_main_t *pm = &vac_main;
379 pthread_mutex_lock(&pm->timeout_lock);
Ole Troande728ac2018-10-08 11:24:22 +0200380 timeout_in_progress = false;
381 timeout_cancelled = true;
Neale Rannsc16f6b32018-06-03 21:21:19 -0700382 pthread_cond_signal(&pm->timeout_cancel_cv);
383 pthread_mutex_unlock(&pm->timeout_lock);
384}
385
Damjan Marion7cd468a2016-12-19 23:05:39 +0100386int
Damjan Marion5fec1e82017-04-13 19:13:47 +0200387vac_disconnect (void)
Damjan Marion7cd468a2016-12-19 23:05:39 +0100388{
389 api_main_t *am = &api_main;
Damjan Marion5fec1e82017-04-13 19:13:47 +0200390 vac_main_t *pm = &vac_main;
Neale Rannsc16f6b32018-06-03 21:21:19 -0700391 uword junk;
Damjan Marion7cd468a2016-12-19 23:05:39 +0100392
Ole Troandfc9b7c2017-03-06 23:51:57 +0100393 if (!pm->connected_to_vlib) return 0;
394
395 if (pm->rx_thread_handle) {
Damjan Marion7cd468a2016-12-19 23:05:39 +0100396 vl_api_rx_thread_exit_t *ep;
Damjan Marion7cd468a2016-12-19 23:05:39 +0100397 ep = vl_msg_api_alloc (sizeof (*ep));
398 ep->_vl_msg_id = ntohs(VL_API_RX_THREAD_EXIT);
399 vl_msg_api_send_shmem(am->vl_input_queue, (u8 *)&ep);
Ole Troandfc9b7c2017-03-06 23:51:57 +0100400
401 /* wait (with timeout) until RX thread has finished */
402 struct timespec ts;
403 struct timeval tv;
404 gettimeofday(&tv, NULL);
405 ts.tv_sec = tv.tv_sec + 5;
406 ts.tv_nsec = 0;
407 pthread_mutex_lock(&pm->queue_lock);
408 int rv = pthread_cond_timedwait(&pm->terminate_cv, &pm->queue_lock, &ts);
409 pthread_mutex_unlock(&pm->queue_lock);
410 /* now join so we wait until thread has -really- finished */
411 if (rv == ETIMEDOUT)
412 pthread_cancel(pm->rx_thread_handle);
413 else
414 pthread_join(pm->rx_thread_handle, (void **) &junk);
Damjan Marion7cd468a2016-12-19 23:05:39 +0100415 }
Neale Rannsc16f6b32018-06-03 21:21:19 -0700416 if (pm->timeout_thread_handle) {
417 /* cancel, wake then join the timeout thread */
Neale Ranns1d652792018-07-26 08:05:53 -0700418 pm->timeout_loop = 0;
Ole Troan4e588aa2018-09-07 11:01:47 +0200419 timeout_thread_cancelled = true;
Neale Rannsc16f6b32018-06-03 21:21:19 -0700420 set_timeout(0);
421 pthread_join(pm->timeout_thread_handle, (void **) &junk);
422 }
Ole Troandfc9b7c2017-03-06 23:51:57 +0100423
424 vl_client_disconnect();
425 vl_client_api_unmap();
Damjan Marion5fec1e82017-04-13 19:13:47 +0200426 vac_callback = 0;
Ole Troandfc9b7c2017-03-06 23:51:57 +0100427
428 cleanup();
Damjan Marion7cd468a2016-12-19 23:05:39 +0100429
430 return (0);
431}
432
433int
Damjan Marion5fec1e82017-04-13 19:13:47 +0200434vac_read (char **p, int *l, u16 timeout)
Damjan Marion7cd468a2016-12-19 23:05:39 +0100435{
Florin Corase86a8ed2018-01-05 03:20:25 -0800436 svm_queue_t *q;
Damjan Marion7cd468a2016-12-19 23:05:39 +0100437 api_main_t *am = &api_main;
Damjan Marion5fec1e82017-04-13 19:13:47 +0200438 vac_main_t *pm = &vac_main;
Dave Barach59b25652017-09-10 15:04:27 -0400439 vl_api_memclnt_keepalive_t *mp;
440 vl_api_memclnt_keepalive_reply_t *rmp;
Damjan Marion7cd468a2016-12-19 23:05:39 +0100441 uword msg;
Ole Troandfc9b7c2017-03-06 23:51:57 +0100442 msgbuf_t *msgbuf;
Dave Barach59b25652017-09-10 15:04:27 -0400443 int rv;
444 vl_shmem_hdr_t *shmem_hdr;
Damjan Marion7cd468a2016-12-19 23:05:39 +0100445
446 if (!pm->connected_to_vlib) return -1;
447
448 *l = 0;
449
450 if (am->our_pid == 0) return (-1);
451
Ole Troandfc9b7c2017-03-06 23:51:57 +0100452 /* Poke timeout thread */
453 if (timeout)
454 set_timeout(timeout);
455
Damjan Marion7cd468a2016-12-19 23:05:39 +0100456 q = am->vl_input_queue;
Dave Barach59b25652017-09-10 15:04:27 -0400457
458 again:
Mohsin Kazmi3fca5672018-01-04 18:57:26 +0100459 rv = svm_queue_sub(q, (u8 *)&msg, SVM_Q_WAIT, 0);
460
Damjan Marion7cd468a2016-12-19 23:05:39 +0100461 if (rv == 0) {
462 u16 msg_id = ntohs(*((u16 *)msg));
Ole Troandfc9b7c2017-03-06 23:51:57 +0100463 switch (msg_id) {
464 case VL_API_RX_THREAD_EXIT:
Ole Troan73710c72018-06-04 22:27:49 +0200465 vl_msg_api_free((void *) msg);
Ole Troande728ac2018-10-08 11:24:22 +0200466 goto error;
Ole Troandfc9b7c2017-03-06 23:51:57 +0100467 case VL_API_MEMCLNT_RX_THREAD_SUSPEND:
Ole Troandfc9b7c2017-03-06 23:51:57 +0100468 goto error;
469 case VL_API_MEMCLNT_READ_TIMEOUT:
Ole Troandfc9b7c2017-03-06 23:51:57 +0100470 goto error;
Dave Barach59b25652017-09-10 15:04:27 -0400471 case VL_API_MEMCLNT_KEEPALIVE:
472 /* Handle an alive-check ping from vpp. */
473 mp = (void *)msg;
474 rmp = vl_msg_api_alloc (sizeof (*rmp));
475 memset (rmp, 0, sizeof (*rmp));
476 rmp->_vl_msg_id = ntohs(VL_API_MEMCLNT_KEEPALIVE_REPLY);
477 rmp->context = mp->context;
478 shmem_hdr = am->shmem_hdr;
479 vl_msg_api_send_shmem(shmem_hdr->vl_input_queue, (u8 *)&rmp);
480 vl_msg_api_free((void *) msg);
481 /*
482 * Python code is blissfully unaware of these pings, so
483 * act as if it never happened...
484 */
485 goto again;
Ole Troandfc9b7c2017-03-06 23:51:57 +0100486
487 default:
488 msgbuf = (msgbuf_t *)(((u8 *)msg) - offsetof(msgbuf_t, data));
489 *l = ntohl(msgbuf->data_len);
490 if (*l == 0) {
Ole Troan4e588aa2018-09-07 11:01:47 +0200491 fprintf(stderr, "Unregistered API message: %d\n", msg_id);
Ole Troandfc9b7c2017-03-06 23:51:57 +0100492 goto error;
493 }
Damjan Marion7cd468a2016-12-19 23:05:39 +0100494 }
495 *p = (char *)msg;
Ole Troandfc9b7c2017-03-06 23:51:57 +0100496
Ole Troandfc9b7c2017-03-06 23:51:57 +0100497
Damjan Marion7cd468a2016-12-19 23:05:39 +0100498 } else {
Ole Troan4e588aa2018-09-07 11:01:47 +0200499 fprintf(stderr, "Read failed with %d\n", rv);
Damjan Marion7cd468a2016-12-19 23:05:39 +0100500 }
Ole Troande728ac2018-10-08 11:24:22 +0200501 /* Let timeout notification thread know we're done */
502 if (timeout)
503 unset_timeout();
504
Damjan Marion7cd468a2016-12-19 23:05:39 +0100505 return (rv);
Ole Troandfc9b7c2017-03-06 23:51:57 +0100506
507 error:
Ole Troande728ac2018-10-08 11:24:22 +0200508 if (timeout)
509 unset_timeout();
Ole Troandfc9b7c2017-03-06 23:51:57 +0100510 vl_msg_api_free((void *) msg);
511 /* Client might forget to resume RX thread on failure */
Damjan Marion5fec1e82017-04-13 19:13:47 +0200512 vac_rx_resume ();
Ole Troandfc9b7c2017-03-06 23:51:57 +0100513 return -1;
Damjan Marion7cd468a2016-12-19 23:05:39 +0100514}
515
516/*
517 * XXX: Makes the assumption that client_index is the first member
518 */
519typedef VL_API_PACKED(struct _vl_api_header {
520 u16 _vl_msg_id;
521 u32 client_index;
522}) vl_api_header_t;
523
Ole Troan94495f22018-08-02 11:58:12 +0200524static u32
Damjan Marion5fec1e82017-04-13 19:13:47 +0200525vac_client_index (void)
Damjan Marion7cd468a2016-12-19 23:05:39 +0100526{
527 return (api_main.my_client_index);
528}
529
530int
Damjan Marion5fec1e82017-04-13 19:13:47 +0200531vac_write (char *p, int l)
Damjan Marion7cd468a2016-12-19 23:05:39 +0100532{
533 int rv = -1;
534 api_main_t *am = &api_main;
535 vl_api_header_t *mp = vl_msg_api_alloc(l);
Florin Corase86a8ed2018-01-05 03:20:25 -0800536 svm_queue_t *q;
Damjan Marion5fec1e82017-04-13 19:13:47 +0200537 vac_main_t *pm = &vac_main;
Damjan Marion7cd468a2016-12-19 23:05:39 +0100538
539 if (!pm->connected_to_vlib) return -1;
540 if (!mp) return (-1);
Ole Troandfc9b7c2017-03-06 23:51:57 +0100541
Damjan Marion7cd468a2016-12-19 23:05:39 +0100542 memcpy(mp, p, l);
Damjan Marion5fec1e82017-04-13 19:13:47 +0200543 mp->client_index = vac_client_index();
Damjan Marion7cd468a2016-12-19 23:05:39 +0100544 q = am->shmem_hdr->vl_input_queue;
Florin Corase86a8ed2018-01-05 03:20:25 -0800545 rv = svm_queue_add(q, (u8 *)&mp, 0);
Damjan Marion7cd468a2016-12-19 23:05:39 +0100546 if (rv != 0) {
Ole Troan4e588aa2018-09-07 11:01:47 +0200547 fprintf(stderr, "vpe_api_write fails: %d\n", rv);
Damjan Marion7cd468a2016-12-19 23:05:39 +0100548 /* Clear message */
Damjan Marion5fec1e82017-04-13 19:13:47 +0200549 vac_free(mp);
Damjan Marion7cd468a2016-12-19 23:05:39 +0100550 }
551 return (rv);
552}
553
Ole Troan3cc49712017-03-08 12:02:24 +0100554int
Damjan Marion5fec1e82017-04-13 19:13:47 +0200555vac_get_msg_index (unsigned char * name)
Damjan Marion7cd468a2016-12-19 23:05:39 +0100556{
Florin Corase86a8ed2018-01-05 03:20:25 -0800557 return vl_msg_api_get_msg_index (name);
Damjan Marion7cd468a2016-12-19 23:05:39 +0100558}
Ole Troan3cc49712017-03-08 12:02:24 +0100559
560int
Damjan Marion5fec1e82017-04-13 19:13:47 +0200561vac_msg_table_max_index(void)
Ole Troan3cc49712017-03-08 12:02:24 +0100562{
563 int max = 0;
564 hash_pair_t *hp;
Damjan Marion5fec1e82017-04-13 19:13:47 +0200565 uword *h = vac_msg_table_get_hash();
Ole Troan3cc49712017-03-08 12:02:24 +0100566 hash_foreach_pair (hp, h,
567 ({
568 if (hp->value[0] > max)
569 max = hp->value[0];
570 }));
571
572 return max;
573}
574
575void
Damjan Marion5fec1e82017-04-13 19:13:47 +0200576vac_set_error_handler (vac_error_callback_t cb)
Ole Troan3cc49712017-03-08 12:02:24 +0100577{
578 if (cb) clib_error_register_handler (cb, 0);
579}