blob: 5dcca22694db45caa4ee676de2d3844bccb7d0d7 [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;
Ole Troanc5607892019-04-10 19:32:02 +020038bool rx_thread_done;
Ole Troande728ac2018-10-08 11:24:22 +020039
Ole Troandfc9b7c2017-03-06 23:51:57 +010040/*
41 * Asynchronous mode:
42 * Client registers a callback. All messages are sent to the callback.
43 * Synchronous mode:
44 * Client calls blocking read().
45 * Clients are expected to collate events on a queue.
Damjan Marion5fec1e82017-04-13 19:13:47 +020046 * vac_write() -> suspends RX thread
47 * vac_read() -> resumes RX thread
Ole Troandfc9b7c2017-03-06 23:51:57 +010048 */
49
Damjan Marion7cd468a2016-12-19 23:05:39 +010050#define vl_typedefs /* define message structures */
51#include <vpp/api/vpe_all_api_h.h>
52#undef vl_typedefs
53
54#define vl_endianfun /* define message structures */
55#include <vpp/api/vpe_all_api_h.h>
56#undef vl_endianfun
57
58vlib_main_t vlib_global_main;
59vlib_main_t **vlib_mains;
60
61typedef struct {
Damjan Marion7cd468a2016-12-19 23:05:39 +010062 u8 connected_to_vlib;
Damjan Marion7cd468a2016-12-19 23:05:39 +010063 pthread_t rx_thread_handle;
Ole Troandfc9b7c2017-03-06 23:51:57 +010064 pthread_t timeout_thread_handle;
65 pthread_mutex_t queue_lock;
66 pthread_cond_t suspend_cv;
67 pthread_cond_t resume_cv;
68 pthread_mutex_t timeout_lock;
Neale Ranns1d652792018-07-26 08:05:53 -070069 u8 timeout_loop;
Ole Troandfc9b7c2017-03-06 23:51:57 +010070 pthread_cond_t timeout_cv;
71 pthread_cond_t timeout_cancel_cv;
72 pthread_cond_t terminate_cv;
Damjan Marion5fec1e82017-04-13 19:13:47 +020073} vac_main_t;
Damjan Marion7cd468a2016-12-19 23:05:39 +010074
Damjan Marion5fec1e82017-04-13 19:13:47 +020075vac_main_t vac_main;
76vac_callback_t vac_callback;
Ole Troandfc9b7c2017-03-06 23:51:57 +010077u16 read_timeout = 0;
78bool rx_is_running = false;
Ole Troan4e588aa2018-09-07 11:01:47 +020079bool timeout_thread_cancelled = false;
Ole Troandfc9b7c2017-03-06 23:51:57 +010080
Ole Troan73710c72018-06-04 22:27:49 +020081/* Set to true to enable memory tracing */
82bool mem_trace = false;
83
84__attribute__((constructor))
85static void
86vac_client_constructor (void)
87{
Ole Troan73710c72018-06-04 22:27:49 +020088 clib_mem_init (0, 1 << 30);
Ole Troan73710c72018-06-04 22:27:49 +020089 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;
Dave Barachb7b92992018-10-17 10:38:51 -0400108 clib_memset(pm, 0, sizeof(*pm));
Ole Troandfc9b7c2017-03-06 23:51:57 +0100109 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);
Neale Ranns1d652792018-07-26 08:05:53 -0700113 pm->timeout_loop = 1;
Ole Troandfc9b7c2017-03-06 23:51:57 +0100114 pthread_cond_init(&pm->timeout_cv, NULL);
115 pthread_cond_init(&pm->timeout_cancel_cv, NULL);
116 pthread_cond_init(&pm->terminate_cv, NULL);
117}
118
119static void
120cleanup (void)
121{
Damjan Marion5fec1e82017-04-13 19:13:47 +0200122 vac_main_t *pm = &vac_main;
Ole Troan73710c72018-06-04 22:27:49 +0200123 pthread_mutex_destroy(&pm->queue_lock);
Ole Troandfc9b7c2017-03-06 23:51:57 +0100124 pthread_cond_destroy(&pm->suspend_cv);
125 pthread_cond_destroy(&pm->resume_cv);
Ole Troan73710c72018-06-04 22:27:49 +0200126 pthread_mutex_destroy(&pm->timeout_lock);
Ole Troandfc9b7c2017-03-06 23:51:57 +0100127 pthread_cond_destroy(&pm->timeout_cv);
128 pthread_cond_destroy(&pm->timeout_cancel_cv);
129 pthread_cond_destroy(&pm->terminate_cv);
Dave Barachb7b92992018-10-17 10:38:51 -0400130 clib_memset(pm, 0, sizeof(*pm));
Ole Troandfc9b7c2017-03-06 23:51:57 +0100131}
Damjan Marion7cd468a2016-12-19 23:05:39 +0100132
133/*
134 * Satisfy external references when -lvlib is not available.
135 */
136void vlib_cli_output (struct vlib_main_t * vm, char * fmt, ...)
137{
138 clib_warning ("vlib_cli_output called...");
139}
140
141void
Damjan Marion5fec1e82017-04-13 19:13:47 +0200142vac_free (void * msg)
Damjan Marion7cd468a2016-12-19 23:05:39 +0100143{
144 vl_msg_api_free (msg);
145}
146
147static void
Damjan Marion5fec1e82017-04-13 19:13:47 +0200148vac_api_handler (void *msg)
Damjan Marion7cd468a2016-12-19 23:05:39 +0100149{
150 u16 id = ntohs(*((u16 *)msg));
Damjan Marion7cd468a2016-12-19 23:05:39 +0100151 msgbuf_t *msgbuf = (msgbuf_t *)(((u8 *)msg) - offsetof(msgbuf_t, data));
152 int l = ntohl(msgbuf->data_len);
153 if (l == 0)
154 clib_warning("Message ID %d has wrong length: %d\n", id, l);
155
156 /* Call Python callback */
Damjan Marion5fec1e82017-04-13 19:13:47 +0200157 ASSERT(vac_callback);
158 (vac_callback)(msg, l);
159 vac_free(msg);
Damjan Marion7cd468a2016-12-19 23:05:39 +0100160}
161
162static void *
Damjan Marion5fec1e82017-04-13 19:13:47 +0200163vac_rx_thread_fn (void *arg)
Damjan Marion7cd468a2016-12-19 23:05:39 +0100164{
Florin Corase86a8ed2018-01-05 03:20:25 -0800165 svm_queue_t *q;
Dave Barach59b25652017-09-10 15:04:27 -0400166 vl_api_memclnt_keepalive_t *mp;
167 vl_api_memclnt_keepalive_reply_t *rmp;
Damjan Marion5fec1e82017-04-13 19:13:47 +0200168 vac_main_t *pm = &vac_main;
Dave Barach39d69112019-11-27 11:42:13 -0500169 api_main_t *am = vlibapi_get_main();
Dave Barach59b25652017-09-10 15:04:27 -0400170 vl_shmem_hdr_t *shmem_hdr;
Damjan Marion7cd468a2016-12-19 23:05:39 +0100171 uword msg;
172
173 q = am->vl_input_queue;
174
Ole Troandfc9b7c2017-03-06 23:51:57 +0100175 while (1)
Mohsin Kazmi3fca5672018-01-04 18:57:26 +0100176 while (!svm_queue_sub(q, (u8 *)&msg, SVM_Q_WAIT, 0))
Ole Troandfc9b7c2017-03-06 23:51:57 +0100177 {
Benoît Ganne9fb6d402019-04-15 15:28:21 +0200178 VL_MSG_API_UNPOISON((void *)msg);
Ole Troandfc9b7c2017-03-06 23:51:57 +0100179 u16 id = ntohs(*((u16 *)msg));
180 switch (id) {
181 case VL_API_RX_THREAD_EXIT:
182 vl_msg_api_free((void *) msg);
183 /* signal waiting threads that this thread is about to terminate */
184 pthread_mutex_lock(&pm->queue_lock);
Ole Troanc5607892019-04-10 19:32:02 +0200185 rx_thread_done = true;
Ole Troandfc9b7c2017-03-06 23:51:57 +0100186 pthread_cond_signal(&pm->terminate_cv);
187 pthread_mutex_unlock(&pm->queue_lock);
188 pthread_exit(0);
189 return 0;
190 break;
191
192 case VL_API_MEMCLNT_RX_THREAD_SUSPEND:
193 vl_msg_api_free((void * )msg);
194 /* Suspend thread and signal reader */
195 pthread_mutex_lock(&pm->queue_lock);
196 pthread_cond_signal(&pm->suspend_cv);
197 /* Wait for the resume signal */
198 pthread_cond_wait (&pm->resume_cv, &pm->queue_lock);
199 pthread_mutex_unlock(&pm->queue_lock);
200 break;
201
202 case VL_API_MEMCLNT_READ_TIMEOUT:
203 clib_warning("Received read timeout in async thread\n");
204 vl_msg_api_free((void *) msg);
205 break;
206
Dave Barach59b25652017-09-10 15:04:27 -0400207 case VL_API_MEMCLNT_KEEPALIVE:
208 mp = (void *)msg;
209 rmp = vl_msg_api_alloc (sizeof (*rmp));
Dave Barachb7b92992018-10-17 10:38:51 -0400210 clib_memset (rmp, 0, sizeof (*rmp));
Dave Barach59b25652017-09-10 15:04:27 -0400211 rmp->_vl_msg_id = ntohs(VL_API_MEMCLNT_KEEPALIVE_REPLY);
212 rmp->context = mp->context;
213 shmem_hdr = am->shmem_hdr;
214 vl_msg_api_send_shmem(shmem_hdr->vl_input_queue, (u8 *)&rmp);
215 vl_msg_api_free((void *) msg);
216 break;
217
Ole Troandfc9b7c2017-03-06 23:51:57 +0100218 default:
Damjan Marion5fec1e82017-04-13 19:13:47 +0200219 vac_api_handler((void *)msg);
Ole Troandfc9b7c2017-03-06 23:51:57 +0100220 }
221 }
222}
223
224static void *
Damjan Marion5fec1e82017-04-13 19:13:47 +0200225vac_timeout_thread_fn (void *arg)
Ole Troandfc9b7c2017-03-06 23:51:57 +0100226{
227 vl_api_memclnt_read_timeout_t *ep;
Damjan Marion5fec1e82017-04-13 19:13:47 +0200228 vac_main_t *pm = &vac_main;
Dave Barach39d69112019-11-27 11:42:13 -0500229 api_main_t *am = vlibapi_get_main();
Ole Troandfc9b7c2017-03-06 23:51:57 +0100230 struct timespec ts;
231 struct timeval tv;
Ole Troandfc9b7c2017-03-06 23:51:57 +0100232 int rv;
233
Neale Ranns1d652792018-07-26 08:05:53 -0700234 while (pm->timeout_loop)
Ole Troandfc9b7c2017-03-06 23:51:57 +0100235 {
236 /* Wait for poke */
237 pthread_mutex_lock(&pm->timeout_lock);
Ole Troande728ac2018-10-08 11:24:22 +0200238 while (!timeout_in_progress)
239 pthread_cond_wait (&pm->timeout_cv, &pm->timeout_lock);
240
241 /* Starting timer */
Ole Troandfc9b7c2017-03-06 23:51:57 +0100242 gettimeofday(&tv, NULL);
Ole Troande728ac2018-10-08 11:24:22 +0200243 ts.tv_sec = tv.tv_sec + read_timeout;
Ole Troandfc9b7c2017-03-06 23:51:57 +0100244 ts.tv_nsec = 0;
Ole Troande728ac2018-10-08 11:24:22 +0200245
246 if (!timeout_cancelled) {
247 rv = pthread_cond_timedwait (&pm->timeout_cancel_cv,
248 &pm->timeout_lock, &ts);
249 if (rv == ETIMEDOUT && !timeout_thread_cancelled) {
Ole Troandfc9b7c2017-03-06 23:51:57 +0100250 ep = vl_msg_api_alloc (sizeof (*ep));
251 ep->_vl_msg_id = ntohs(VL_API_MEMCLNT_READ_TIMEOUT);
252 vl_msg_api_send_shmem(am->vl_input_queue, (u8 *)&ep);
253 }
Ole Troande728ac2018-10-08 11:24:22 +0200254 }
255
256 pthread_mutex_unlock(&pm->timeout_lock);
Ole Troandfc9b7c2017-03-06 23:51:57 +0100257 }
Damjan Marion7cd468a2016-12-19 23:05:39 +0100258 pthread_exit(0);
259}
260
Ole Troandfc9b7c2017-03-06 23:51:57 +0100261void
Damjan Marion5fec1e82017-04-13 19:13:47 +0200262vac_rx_suspend (void)
Ole Troandfc9b7c2017-03-06 23:51:57 +0100263{
Dave Barach39d69112019-11-27 11:42:13 -0500264 api_main_t *am = vlibapi_get_main();
Damjan Marion5fec1e82017-04-13 19:13:47 +0200265 vac_main_t *pm = &vac_main;
Ole Troandfc9b7c2017-03-06 23:51:57 +0100266 vl_api_memclnt_rx_thread_suspend_t *ep;
267
268 if (!pm->rx_thread_handle) return;
269 pthread_mutex_lock(&pm->queue_lock);
270 if (rx_is_running)
271 {
272 ep = vl_msg_api_alloc (sizeof (*ep));
273 ep->_vl_msg_id = ntohs(VL_API_MEMCLNT_RX_THREAD_SUSPEND);
274 vl_msg_api_send_shmem(am->vl_input_queue, (u8 *)&ep);
Paul Vinciguerraec11b132018-09-24 05:25:00 -0700275 /* Wait for RX thread to tell us it has suspended */
Ole Troandfc9b7c2017-03-06 23:51:57 +0100276 pthread_cond_wait(&pm->suspend_cv, &pm->queue_lock);
277 rx_is_running = false;
278 }
279 pthread_mutex_unlock(&pm->queue_lock);
280}
281
282void
Damjan Marion5fec1e82017-04-13 19:13:47 +0200283vac_rx_resume (void)
Ole Troandfc9b7c2017-03-06 23:51:57 +0100284{
Damjan Marion5fec1e82017-04-13 19:13:47 +0200285 vac_main_t *pm = &vac_main;
Ole Troandfc9b7c2017-03-06 23:51:57 +0100286 if (!pm->rx_thread_handle) return;
287 pthread_mutex_lock(&pm->queue_lock);
Ole Troanad0697a2017-03-09 21:10:45 +0100288 if (rx_is_running) goto unlock;
Ole Troandfc9b7c2017-03-06 23:51:57 +0100289 pthread_cond_signal(&pm->resume_cv);
290 rx_is_running = true;
Ole Troanad0697a2017-03-09 21:10:45 +0100291 unlock:
Ole Troandfc9b7c2017-03-06 23:51:57 +0100292 pthread_mutex_unlock(&pm->queue_lock);
293}
294
Ole Troan3cc49712017-03-08 12:02:24 +0100295static uword *
Damjan Marion5fec1e82017-04-13 19:13:47 +0200296vac_msg_table_get_hash (void)
Damjan Marion7cd468a2016-12-19 23:05:39 +0100297{
Dave Barach39d69112019-11-27 11:42:13 -0500298 api_main_t *am = vlibapi_get_main();
Damjan Marion7cd468a2016-12-19 23:05:39 +0100299 return (am->msg_index_by_name_and_crc);
300}
301
302int
Damjan Marion5fec1e82017-04-13 19:13:47 +0200303vac_msg_table_size(void)
Damjan Marion7cd468a2016-12-19 23:05:39 +0100304{
Dave Barach39d69112019-11-27 11:42:13 -0500305 api_main_t *am = vlibapi_get_main();
Damjan Marion7cd468a2016-12-19 23:05:39 +0100306 return hash_elts(am->msg_index_by_name_and_crc);
307}
308
309int
Damjan Marion5fec1e82017-04-13 19:13:47 +0200310vac_connect (char * name, char * chroot_prefix, vac_callback_t cb,
Dave Barachf9526922017-01-06 16:33:06 -0500311 int rx_qlen)
Damjan Marion7cd468a2016-12-19 23:05:39 +0100312{
Ole Troanc5607892019-04-10 19:32:02 +0200313 rx_thread_done = false;
Damjan Marion7cd468a2016-12-19 23:05:39 +0100314 int rv = 0;
Damjan Marion5fec1e82017-04-13 19:13:47 +0200315 vac_main_t *pm = &vac_main;
Damjan Marion7cd468a2016-12-19 23:05:39 +0100316
Ole Troandfc9b7c2017-03-06 23:51:57 +0100317 init();
Damjan Marion7cd468a2016-12-19 23:05:39 +0100318 if (chroot_prefix != NULL)
319 vl_set_memory_root_path (chroot_prefix);
320
321 if ((rv = vl_client_api_map("/vpe-api"))) {
Ondrej Fabrye29cb672018-08-16 08:36:19 +0200322 clib_warning ("vl_client_api_map returned %d", rv);
Damjan Marion7cd468a2016-12-19 23:05:39 +0100323 return rv;
324 }
325
Dave Barachf9526922017-01-06 16:33:06 -0500326 if (vl_client_connect(name, 0, rx_qlen) < 0) {
Damjan Marion7cd468a2016-12-19 23:05:39 +0100327 vl_client_api_unmap();
328 return (-1);
329 }
330
331 if (cb) {
332 /* Start the rx queue thread */
Damjan Marion5fec1e82017-04-13 19:13:47 +0200333 rv = pthread_create(&pm->rx_thread_handle, NULL, vac_rx_thread_fn, 0);
Damjan Marion7cd468a2016-12-19 23:05:39 +0100334 if (rv) {
335 clib_warning("pthread_create returned %d", rv);
336 vl_client_api_unmap();
337 return (-1);
338 }
Damjan Marion5fec1e82017-04-13 19:13:47 +0200339 vac_callback = cb;
Ole Troandfc9b7c2017-03-06 23:51:57 +0100340 rx_is_running = true;
341 }
342
343 /* Start read timeout thread */
344 rv = pthread_create(&pm->timeout_thread_handle, NULL,
Damjan Marion5fec1e82017-04-13 19:13:47 +0200345 vac_timeout_thread_fn, 0);
Ole Troandfc9b7c2017-03-06 23:51:57 +0100346 if (rv) {
347 clib_warning("pthread_create returned %d", rv);
348 vl_client_api_unmap();
349 return (-1);
Damjan Marion7cd468a2016-12-19 23:05:39 +0100350 }
351
352 pm->connected_to_vlib = 1;
353
354 return (0);
355}
Neale Rannsc16f6b32018-06-03 21:21:19 -0700356static void
357set_timeout (unsigned short timeout)
358{
359 vac_main_t *pm = &vac_main;
360 pthread_mutex_lock(&pm->timeout_lock);
361 read_timeout = timeout;
Ole Troande728ac2018-10-08 11:24:22 +0200362 timeout_in_progress = true;
363 timeout_cancelled = false;
Neale Rannsc16f6b32018-06-03 21:21:19 -0700364 pthread_cond_signal(&pm->timeout_cv);
365 pthread_mutex_unlock(&pm->timeout_lock);
366}
367
368static void
369unset_timeout (void)
370{
371 vac_main_t *pm = &vac_main;
372 pthread_mutex_lock(&pm->timeout_lock);
Ole Troande728ac2018-10-08 11:24:22 +0200373 timeout_in_progress = false;
374 timeout_cancelled = true;
Neale Rannsc16f6b32018-06-03 21:21:19 -0700375 pthread_cond_signal(&pm->timeout_cancel_cv);
376 pthread_mutex_unlock(&pm->timeout_lock);
377}
378
Damjan Marion7cd468a2016-12-19 23:05:39 +0100379int
Damjan Marion5fec1e82017-04-13 19:13:47 +0200380vac_disconnect (void)
Damjan Marion7cd468a2016-12-19 23:05:39 +0100381{
Dave Barach39d69112019-11-27 11:42:13 -0500382 api_main_t *am = vlibapi_get_main();
Damjan Marion5fec1e82017-04-13 19:13:47 +0200383 vac_main_t *pm = &vac_main;
Neale Rannsc16f6b32018-06-03 21:21:19 -0700384 uword junk;
Ole Troanc5607892019-04-10 19:32:02 +0200385 int rv = 0;
Damjan Marion7cd468a2016-12-19 23:05:39 +0100386
Ole Troandfc9b7c2017-03-06 23:51:57 +0100387 if (!pm->connected_to_vlib) return 0;
388
389 if (pm->rx_thread_handle) {
Damjan Marion7cd468a2016-12-19 23:05:39 +0100390 vl_api_rx_thread_exit_t *ep;
Damjan Marion7cd468a2016-12-19 23:05:39 +0100391 ep = vl_msg_api_alloc (sizeof (*ep));
392 ep->_vl_msg_id = ntohs(VL_API_RX_THREAD_EXIT);
393 vl_msg_api_send_shmem(am->vl_input_queue, (u8 *)&ep);
Ole Troandfc9b7c2017-03-06 23:51:57 +0100394
395 /* wait (with timeout) until RX thread has finished */
396 struct timespec ts;
397 struct timeval tv;
398 gettimeofday(&tv, NULL);
399 ts.tv_sec = tv.tv_sec + 5;
400 ts.tv_nsec = 0;
Ole Troanc5607892019-04-10 19:32:02 +0200401
Ole Troandfc9b7c2017-03-06 23:51:57 +0100402 pthread_mutex_lock(&pm->queue_lock);
Ole Troanc5607892019-04-10 19:32:02 +0200403 if (rx_thread_done == false)
404 rv = pthread_cond_timedwait(&pm->terminate_cv, &pm->queue_lock, &ts);
Ole Troandfc9b7c2017-03-06 23:51:57 +0100405 pthread_mutex_unlock(&pm->queue_lock);
Ole Troanc5607892019-04-10 19:32:02 +0200406
Ole Troandfc9b7c2017-03-06 23:51:57 +0100407 /* now join so we wait until thread has -really- finished */
408 if (rv == ETIMEDOUT)
409 pthread_cancel(pm->rx_thread_handle);
410 else
411 pthread_join(pm->rx_thread_handle, (void **) &junk);
Damjan Marion7cd468a2016-12-19 23:05:39 +0100412 }
Neale Rannsc16f6b32018-06-03 21:21:19 -0700413 if (pm->timeout_thread_handle) {
414 /* cancel, wake then join the timeout thread */
Neale Ranns1d652792018-07-26 08:05:53 -0700415 pm->timeout_loop = 0;
Ole Troan4e588aa2018-09-07 11:01:47 +0200416 timeout_thread_cancelled = true;
Neale Rannsc16f6b32018-06-03 21:21:19 -0700417 set_timeout(0);
418 pthread_join(pm->timeout_thread_handle, (void **) &junk);
419 }
Ole Troandfc9b7c2017-03-06 23:51:57 +0100420
421 vl_client_disconnect();
422 vl_client_api_unmap();
Damjan Marion5fec1e82017-04-13 19:13:47 +0200423 vac_callback = 0;
Ole Troandfc9b7c2017-03-06 23:51:57 +0100424
425 cleanup();
Damjan Marion7cd468a2016-12-19 23:05:39 +0100426
427 return (0);
428}
429
430int
Damjan Marion5fec1e82017-04-13 19:13:47 +0200431vac_read (char **p, int *l, u16 timeout)
Damjan Marion7cd468a2016-12-19 23:05:39 +0100432{
Florin Corase86a8ed2018-01-05 03:20:25 -0800433 svm_queue_t *q;
Dave Barach39d69112019-11-27 11:42:13 -0500434 api_main_t *am = vlibapi_get_main();
Damjan Marion5fec1e82017-04-13 19:13:47 +0200435 vac_main_t *pm = &vac_main;
Dave Barach59b25652017-09-10 15:04:27 -0400436 vl_api_memclnt_keepalive_t *mp;
437 vl_api_memclnt_keepalive_reply_t *rmp;
Damjan Marion7cd468a2016-12-19 23:05:39 +0100438 uword msg;
Ole Troandfc9b7c2017-03-06 23:51:57 +0100439 msgbuf_t *msgbuf;
Dave Barach59b25652017-09-10 15:04:27 -0400440 int rv;
441 vl_shmem_hdr_t *shmem_hdr;
Damjan Marion7cd468a2016-12-19 23:05:39 +0100442
Paul Vinciguerra08d82e92019-06-20 13:46:46 -0400443 /* svm_queue_sub(below) returns {-1, -2} */
444 if (!pm->connected_to_vlib) return -3;
Damjan Marion7cd468a2016-12-19 23:05:39 +0100445
446 *l = 0;
447
Paul Vinciguerra08d82e92019-06-20 13:46:46 -0400448 /* svm_queue_sub(below) returns {-1, -2} */
449 if (am->our_pid == 0) return (-4);
Damjan Marion7cd468a2016-12-19 23:05:39 +0100450
Ole Troandfc9b7c2017-03-06 23:51:57 +0100451 /* Poke timeout thread */
452 if (timeout)
453 set_timeout(timeout);
454
Damjan Marion7cd468a2016-12-19 23:05:39 +0100455 q = am->vl_input_queue;
Dave Barach59b25652017-09-10 15:04:27 -0400456
457 again:
Mohsin Kazmi3fca5672018-01-04 18:57:26 +0100458 rv = svm_queue_sub(q, (u8 *)&msg, SVM_Q_WAIT, 0);
459
Damjan Marion7cd468a2016-12-19 23:05:39 +0100460 if (rv == 0) {
Benoît Ganne9fb6d402019-04-15 15:28:21 +0200461 VL_MSG_API_UNPOISON((void *)msg);
Damjan Marion7cd468a2016-12-19 23:05:39 +0100462 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));
Dave Barachb7b92992018-10-17 10:38:51 -0400475 clib_memset (rmp, 0, sizeof (*rmp));
Dave Barach59b25652017-09-10 15:04:27 -0400476 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);
Dave Barach39d69112019-11-27 11:42:13 -0500481 /*
Dave Barach59b25652017-09-10 15:04:27 -0400482 * 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{
Dave Barach39d69112019-11-27 11:42:13 -0500527 return (vlibapi_get_main()->my_client_index);
Damjan Marion7cd468a2016-12-19 23:05:39 +0100528}
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;
Dave Barach39d69112019-11-27 11:42:13 -0500534 api_main_t *am = vlibapi_get_main();
Damjan Marion7cd468a2016-12-19 23:05:39 +0100535 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}