blob: 2de880d803b998073c3aa015b43b3e6a445370bb [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 */
Ole Troanf68fccf2020-09-28 16:15:18 +020015#include <assert.h>
Damjan Marion7cd468a2016-12-19 23:05:39 +010016#include <stdio.h>
17#include <stdlib.h>
18#include <stddef.h>
19#include <sys/types.h>
20#include <sys/socket.h>
21#include <sys/mman.h>
22#include <sys/stat.h>
23#include <netinet/in.h>
24#include <netdb.h>
25#include <signal.h>
Damjan Marion7cd468a2016-12-19 23:05:39 +010026#include <stdbool.h>
Damjan Marion7cd468a2016-12-19 23:05:39 +010027#include <vnet/vnet.h>
28#include <vlib/vlib.h>
29#include <vlib/unix/unix.h>
30#include <vlibapi/api.h>
31#include <vlibmemory/api.h>
32
33#include <vpp/api/vpe_msg_enum.h>
34
Damjan Marion5fec1e82017-04-13 19:13:47 +020035#include "vppapiclient.h"
Damjan Marion7cd468a2016-12-19 23:05:39 +010036
Ole Troande728ac2018-10-08 11:24:22 +020037bool timeout_cancelled;
38bool timeout_in_progress;
Ole Troanc5607892019-04-10 19:32:02 +020039bool rx_thread_done;
Ole Troande728ac2018-10-08 11:24:22 +020040
Ole Troandfc9b7c2017-03-06 23:51:57 +010041/*
42 * Asynchronous mode:
43 * Client registers a callback. All messages are sent to the callback.
44 * Synchronous mode:
45 * Client calls blocking read().
46 * Clients are expected to collate events on a queue.
Damjan Marion5fec1e82017-04-13 19:13:47 +020047 * vac_write() -> suspends RX thread
48 * vac_read() -> resumes RX thread
Ole Troandfc9b7c2017-03-06 23:51:57 +010049 */
50
Damjan Marion7cd468a2016-12-19 23:05:39 +010051#define vl_typedefs /* define message structures */
52#include <vpp/api/vpe_all_api_h.h>
53#undef vl_typedefs
54
55#define vl_endianfun /* define message structures */
56#include <vpp/api/vpe_all_api_h.h>
57#undef vl_endianfun
58
59vlib_main_t vlib_global_main;
60vlib_main_t **vlib_mains;
61
62typedef struct {
Damjan Marion7cd468a2016-12-19 23:05:39 +010063 u8 connected_to_vlib;
Damjan Marion7cd468a2016-12-19 23:05:39 +010064 pthread_t rx_thread_handle;
Ole Troandfc9b7c2017-03-06 23:51:57 +010065 pthread_t timeout_thread_handle;
66 pthread_mutex_t queue_lock;
67 pthread_cond_t suspend_cv;
68 pthread_cond_t resume_cv;
69 pthread_mutex_t timeout_lock;
Neale Ranns1d652792018-07-26 08:05:53 -070070 u8 timeout_loop;
Ole Troandfc9b7c2017-03-06 23:51:57 +010071 pthread_cond_t timeout_cv;
72 pthread_cond_t timeout_cancel_cv;
73 pthread_cond_t terminate_cv;
Damjan Marion5fec1e82017-04-13 19:13:47 +020074} vac_main_t;
Damjan Marion7cd468a2016-12-19 23:05:39 +010075
Damjan Marion5fec1e82017-04-13 19:13:47 +020076vac_main_t vac_main;
77vac_callback_t vac_callback;
Ole Troandfc9b7c2017-03-06 23:51:57 +010078u16 read_timeout = 0;
79bool rx_is_running = false;
Ole Troan4e588aa2018-09-07 11:01:47 +020080bool timeout_thread_cancelled = false;
Ole Troandfc9b7c2017-03-06 23:51:57 +010081
Ole Troan65fa0362020-09-30 10:43:00 +020082/* Only ever allocate one heap */
83bool mem_initialized = false;
84
Ole Troandfc9b7c2017-03-06 23:51:57 +010085static void
86init (void)
87{
Damjan Marion5fec1e82017-04-13 19:13:47 +020088 vac_main_t *pm = &vac_main;
Dave Barachb7b92992018-10-17 10:38:51 -040089 clib_memset(pm, 0, sizeof(*pm));
Ole Troandfc9b7c2017-03-06 23:51:57 +010090 pthread_mutex_init(&pm->queue_lock, NULL);
91 pthread_cond_init(&pm->suspend_cv, NULL);
92 pthread_cond_init(&pm->resume_cv, NULL);
93 pthread_mutex_init(&pm->timeout_lock, NULL);
Neale Ranns1d652792018-07-26 08:05:53 -070094 pm->timeout_loop = 1;
Ole Troandfc9b7c2017-03-06 23:51:57 +010095 pthread_cond_init(&pm->timeout_cv, NULL);
96 pthread_cond_init(&pm->timeout_cancel_cv, NULL);
97 pthread_cond_init(&pm->terminate_cv, NULL);
98}
99
100static void
101cleanup (void)
102{
Damjan Marion5fec1e82017-04-13 19:13:47 +0200103 vac_main_t *pm = &vac_main;
Ole Troan73710c72018-06-04 22:27:49 +0200104 pthread_mutex_destroy(&pm->queue_lock);
Ole Troandfc9b7c2017-03-06 23:51:57 +0100105 pthread_cond_destroy(&pm->suspend_cv);
106 pthread_cond_destroy(&pm->resume_cv);
Ole Troan73710c72018-06-04 22:27:49 +0200107 pthread_mutex_destroy(&pm->timeout_lock);
Ole Troandfc9b7c2017-03-06 23:51:57 +0100108 pthread_cond_destroy(&pm->timeout_cv);
109 pthread_cond_destroy(&pm->timeout_cancel_cv);
110 pthread_cond_destroy(&pm->terminate_cv);
Dave Barachb7b92992018-10-17 10:38:51 -0400111 clib_memset(pm, 0, sizeof(*pm));
Ole Troandfc9b7c2017-03-06 23:51:57 +0100112}
Damjan Marion7cd468a2016-12-19 23:05:39 +0100113
114/*
115 * Satisfy external references when -lvlib is not available.
116 */
117void vlib_cli_output (struct vlib_main_t * vm, char * fmt, ...)
118{
119 clib_warning ("vlib_cli_output called...");
120}
121
122void
Damjan Marion5fec1e82017-04-13 19:13:47 +0200123vac_free (void * msg)
Damjan Marion7cd468a2016-12-19 23:05:39 +0100124{
125 vl_msg_api_free (msg);
126}
127
128static void
Damjan Marion5fec1e82017-04-13 19:13:47 +0200129vac_api_handler (void *msg)
Damjan Marion7cd468a2016-12-19 23:05:39 +0100130{
131 u16 id = ntohs(*((u16 *)msg));
Damjan Marion7cd468a2016-12-19 23:05:39 +0100132 msgbuf_t *msgbuf = (msgbuf_t *)(((u8 *)msg) - offsetof(msgbuf_t, data));
133 int l = ntohl(msgbuf->data_len);
134 if (l == 0)
135 clib_warning("Message ID %d has wrong length: %d\n", id, l);
136
137 /* Call Python callback */
Damjan Marion5fec1e82017-04-13 19:13:47 +0200138 ASSERT(vac_callback);
139 (vac_callback)(msg, l);
140 vac_free(msg);
Damjan Marion7cd468a2016-12-19 23:05:39 +0100141}
142
143static void *
Damjan Marion5fec1e82017-04-13 19:13:47 +0200144vac_rx_thread_fn (void *arg)
Damjan Marion7cd468a2016-12-19 23:05:39 +0100145{
Florin Corase86a8ed2018-01-05 03:20:25 -0800146 svm_queue_t *q;
Dave Barach59b25652017-09-10 15:04:27 -0400147 vl_api_memclnt_keepalive_t *mp;
148 vl_api_memclnt_keepalive_reply_t *rmp;
Damjan Marion5fec1e82017-04-13 19:13:47 +0200149 vac_main_t *pm = &vac_main;
Dave Barach39d69112019-11-27 11:42:13 -0500150 api_main_t *am = vlibapi_get_main();
Dave Barach59b25652017-09-10 15:04:27 -0400151 vl_shmem_hdr_t *shmem_hdr;
Damjan Marion7cd468a2016-12-19 23:05:39 +0100152 uword msg;
153
154 q = am->vl_input_queue;
155
Ole Troandfc9b7c2017-03-06 23:51:57 +0100156 while (1)
Mohsin Kazmi3fca5672018-01-04 18:57:26 +0100157 while (!svm_queue_sub(q, (u8 *)&msg, SVM_Q_WAIT, 0))
Ole Troandfc9b7c2017-03-06 23:51:57 +0100158 {
Benoît Ganne9fb6d402019-04-15 15:28:21 +0200159 VL_MSG_API_UNPOISON((void *)msg);
Ole Troandfc9b7c2017-03-06 23:51:57 +0100160 u16 id = ntohs(*((u16 *)msg));
161 switch (id) {
162 case VL_API_RX_THREAD_EXIT:
163 vl_msg_api_free((void *) msg);
164 /* signal waiting threads that this thread is about to terminate */
165 pthread_mutex_lock(&pm->queue_lock);
Ole Troanc5607892019-04-10 19:32:02 +0200166 rx_thread_done = true;
Ole Troandfc9b7c2017-03-06 23:51:57 +0100167 pthread_cond_signal(&pm->terminate_cv);
168 pthread_mutex_unlock(&pm->queue_lock);
169 pthread_exit(0);
170 return 0;
171 break;
172
173 case VL_API_MEMCLNT_RX_THREAD_SUSPEND:
174 vl_msg_api_free((void * )msg);
175 /* Suspend thread and signal reader */
176 pthread_mutex_lock(&pm->queue_lock);
177 pthread_cond_signal(&pm->suspend_cv);
178 /* Wait for the resume signal */
179 pthread_cond_wait (&pm->resume_cv, &pm->queue_lock);
180 pthread_mutex_unlock(&pm->queue_lock);
181 break;
182
183 case VL_API_MEMCLNT_READ_TIMEOUT:
184 clib_warning("Received read timeout in async thread\n");
185 vl_msg_api_free((void *) msg);
186 break;
187
Dave Barach59b25652017-09-10 15:04:27 -0400188 case VL_API_MEMCLNT_KEEPALIVE:
189 mp = (void *)msg;
190 rmp = vl_msg_api_alloc (sizeof (*rmp));
Dave Barachb7b92992018-10-17 10:38:51 -0400191 clib_memset (rmp, 0, sizeof (*rmp));
Dave Barach59b25652017-09-10 15:04:27 -0400192 rmp->_vl_msg_id = ntohs(VL_API_MEMCLNT_KEEPALIVE_REPLY);
193 rmp->context = mp->context;
194 shmem_hdr = am->shmem_hdr;
195 vl_msg_api_send_shmem(shmem_hdr->vl_input_queue, (u8 *)&rmp);
196 vl_msg_api_free((void *) msg);
197 break;
198
Ole Troandfc9b7c2017-03-06 23:51:57 +0100199 default:
Damjan Marion5fec1e82017-04-13 19:13:47 +0200200 vac_api_handler((void *)msg);
Ole Troandfc9b7c2017-03-06 23:51:57 +0100201 }
202 }
203}
204
205static void *
Damjan Marion5fec1e82017-04-13 19:13:47 +0200206vac_timeout_thread_fn (void *arg)
Ole Troandfc9b7c2017-03-06 23:51:57 +0100207{
208 vl_api_memclnt_read_timeout_t *ep;
Damjan Marion5fec1e82017-04-13 19:13:47 +0200209 vac_main_t *pm = &vac_main;
Dave Barach39d69112019-11-27 11:42:13 -0500210 api_main_t *am = vlibapi_get_main();
Ole Troandfc9b7c2017-03-06 23:51:57 +0100211 struct timespec ts;
212 struct timeval tv;
Ole Troandfc9b7c2017-03-06 23:51:57 +0100213 int rv;
214
Neale Ranns1d652792018-07-26 08:05:53 -0700215 while (pm->timeout_loop)
Ole Troandfc9b7c2017-03-06 23:51:57 +0100216 {
217 /* Wait for poke */
218 pthread_mutex_lock(&pm->timeout_lock);
Ole Troande728ac2018-10-08 11:24:22 +0200219 while (!timeout_in_progress)
220 pthread_cond_wait (&pm->timeout_cv, &pm->timeout_lock);
221
222 /* Starting timer */
Ole Troandfc9b7c2017-03-06 23:51:57 +0100223 gettimeofday(&tv, NULL);
Ole Troande728ac2018-10-08 11:24:22 +0200224 ts.tv_sec = tv.tv_sec + read_timeout;
Ole Troandfc9b7c2017-03-06 23:51:57 +0100225 ts.tv_nsec = 0;
Ole Troande728ac2018-10-08 11:24:22 +0200226
227 if (!timeout_cancelled) {
228 rv = pthread_cond_timedwait (&pm->timeout_cancel_cv,
229 &pm->timeout_lock, &ts);
230 if (rv == ETIMEDOUT && !timeout_thread_cancelled) {
Ole Troandfc9b7c2017-03-06 23:51:57 +0100231 ep = vl_msg_api_alloc (sizeof (*ep));
232 ep->_vl_msg_id = ntohs(VL_API_MEMCLNT_READ_TIMEOUT);
233 vl_msg_api_send_shmem(am->vl_input_queue, (u8 *)&ep);
234 }
Ole Troande728ac2018-10-08 11:24:22 +0200235 }
236
237 pthread_mutex_unlock(&pm->timeout_lock);
Ole Troandfc9b7c2017-03-06 23:51:57 +0100238 }
Damjan Marion7cd468a2016-12-19 23:05:39 +0100239 pthread_exit(0);
240}
241
Ole Troandfc9b7c2017-03-06 23:51:57 +0100242void
Damjan Marion5fec1e82017-04-13 19:13:47 +0200243vac_rx_suspend (void)
Ole Troandfc9b7c2017-03-06 23:51:57 +0100244{
Dave Barach39d69112019-11-27 11:42:13 -0500245 api_main_t *am = vlibapi_get_main();
Damjan Marion5fec1e82017-04-13 19:13:47 +0200246 vac_main_t *pm = &vac_main;
Ole Troandfc9b7c2017-03-06 23:51:57 +0100247 vl_api_memclnt_rx_thread_suspend_t *ep;
248
249 if (!pm->rx_thread_handle) return;
250 pthread_mutex_lock(&pm->queue_lock);
251 if (rx_is_running)
252 {
253 ep = vl_msg_api_alloc (sizeof (*ep));
254 ep->_vl_msg_id = ntohs(VL_API_MEMCLNT_RX_THREAD_SUSPEND);
255 vl_msg_api_send_shmem(am->vl_input_queue, (u8 *)&ep);
Paul Vinciguerraec11b132018-09-24 05:25:00 -0700256 /* Wait for RX thread to tell us it has suspended */
Ole Troandfc9b7c2017-03-06 23:51:57 +0100257 pthread_cond_wait(&pm->suspend_cv, &pm->queue_lock);
258 rx_is_running = false;
259 }
260 pthread_mutex_unlock(&pm->queue_lock);
261}
262
263void
Damjan Marion5fec1e82017-04-13 19:13:47 +0200264vac_rx_resume (void)
Ole Troandfc9b7c2017-03-06 23:51:57 +0100265{
Damjan Marion5fec1e82017-04-13 19:13:47 +0200266 vac_main_t *pm = &vac_main;
Ole Troandfc9b7c2017-03-06 23:51:57 +0100267 if (!pm->rx_thread_handle) return;
268 pthread_mutex_lock(&pm->queue_lock);
Ole Troanad0697a2017-03-09 21:10:45 +0100269 if (rx_is_running) goto unlock;
Ole Troandfc9b7c2017-03-06 23:51:57 +0100270 pthread_cond_signal(&pm->resume_cv);
271 rx_is_running = true;
Ole Troanad0697a2017-03-09 21:10:45 +0100272 unlock:
Ole Troandfc9b7c2017-03-06 23:51:57 +0100273 pthread_mutex_unlock(&pm->queue_lock);
274}
275
Ole Troan3cc49712017-03-08 12:02:24 +0100276static uword *
Damjan Marion5fec1e82017-04-13 19:13:47 +0200277vac_msg_table_get_hash (void)
Damjan Marion7cd468a2016-12-19 23:05:39 +0100278{
Dave Barach39d69112019-11-27 11:42:13 -0500279 api_main_t *am = vlibapi_get_main();
Damjan Marion7cd468a2016-12-19 23:05:39 +0100280 return (am->msg_index_by_name_and_crc);
281}
282
283int
Damjan Marion5fec1e82017-04-13 19:13:47 +0200284vac_msg_table_size(void)
Damjan Marion7cd468a2016-12-19 23:05:39 +0100285{
Dave Barach39d69112019-11-27 11:42:13 -0500286 api_main_t *am = vlibapi_get_main();
Damjan Marion7cd468a2016-12-19 23:05:39 +0100287 return hash_elts(am->msg_index_by_name_and_crc);
288}
289
290int
Damjan Marion5fec1e82017-04-13 19:13:47 +0200291vac_connect (char * name, char * chroot_prefix, vac_callback_t cb,
Ole Troanf68fccf2020-09-28 16:15:18 +0200292 int rx_qlen)
Damjan Marion7cd468a2016-12-19 23:05:39 +0100293{
Ole Troanc5607892019-04-10 19:32:02 +0200294 rx_thread_done = false;
Damjan Marion7cd468a2016-12-19 23:05:39 +0100295 int rv = 0;
Damjan Marion5fec1e82017-04-13 19:13:47 +0200296 vac_main_t *pm = &vac_main;
Damjan Marion7cd468a2016-12-19 23:05:39 +0100297
Ole Troanf68fccf2020-09-28 16:15:18 +0200298 assert (clib_mem_get_heap ());
Ole Troandfc9b7c2017-03-06 23:51:57 +0100299 init();
Damjan Marion7cd468a2016-12-19 23:05:39 +0100300 if (chroot_prefix != NULL)
301 vl_set_memory_root_path (chroot_prefix);
302
303 if ((rv = vl_client_api_map("/vpe-api"))) {
Ondrej Fabrye29cb672018-08-16 08:36:19 +0200304 clib_warning ("vl_client_api_map returned %d", rv);
Damjan Marion7cd468a2016-12-19 23:05:39 +0100305 return rv;
306 }
307
Dave Barachf9526922017-01-06 16:33:06 -0500308 if (vl_client_connect(name, 0, rx_qlen) < 0) {
Damjan Marion7cd468a2016-12-19 23:05:39 +0100309 vl_client_api_unmap();
310 return (-1);
311 }
312
313 if (cb) {
314 /* Start the rx queue thread */
Damjan Marion5fec1e82017-04-13 19:13:47 +0200315 rv = pthread_create(&pm->rx_thread_handle, NULL, vac_rx_thread_fn, 0);
Damjan Marion7cd468a2016-12-19 23:05:39 +0100316 if (rv) {
317 clib_warning("pthread_create returned %d", rv);
318 vl_client_api_unmap();
319 return (-1);
320 }
Damjan Marion5fec1e82017-04-13 19:13:47 +0200321 vac_callback = cb;
Ole Troandfc9b7c2017-03-06 23:51:57 +0100322 rx_is_running = true;
323 }
324
325 /* Start read timeout thread */
326 rv = pthread_create(&pm->timeout_thread_handle, NULL,
Damjan Marion5fec1e82017-04-13 19:13:47 +0200327 vac_timeout_thread_fn, 0);
Ole Troandfc9b7c2017-03-06 23:51:57 +0100328 if (rv) {
329 clib_warning("pthread_create returned %d", rv);
330 vl_client_api_unmap();
331 return (-1);
Damjan Marion7cd468a2016-12-19 23:05:39 +0100332 }
333
334 pm->connected_to_vlib = 1;
335
336 return (0);
337}
Neale Rannsc16f6b32018-06-03 21:21:19 -0700338static void
339set_timeout (unsigned short timeout)
340{
341 vac_main_t *pm = &vac_main;
342 pthread_mutex_lock(&pm->timeout_lock);
343 read_timeout = timeout;
Ole Troande728ac2018-10-08 11:24:22 +0200344 timeout_in_progress = true;
345 timeout_cancelled = false;
Neale Rannsc16f6b32018-06-03 21:21:19 -0700346 pthread_cond_signal(&pm->timeout_cv);
347 pthread_mutex_unlock(&pm->timeout_lock);
348}
349
350static void
351unset_timeout (void)
352{
353 vac_main_t *pm = &vac_main;
354 pthread_mutex_lock(&pm->timeout_lock);
Ole Troande728ac2018-10-08 11:24:22 +0200355 timeout_in_progress = false;
356 timeout_cancelled = true;
Neale Rannsc16f6b32018-06-03 21:21:19 -0700357 pthread_cond_signal(&pm->timeout_cancel_cv);
358 pthread_mutex_unlock(&pm->timeout_lock);
359}
360
Damjan Marion7cd468a2016-12-19 23:05:39 +0100361int
Damjan Marion5fec1e82017-04-13 19:13:47 +0200362vac_disconnect (void)
Damjan Marion7cd468a2016-12-19 23:05:39 +0100363{
Dave Barach39d69112019-11-27 11:42:13 -0500364 api_main_t *am = vlibapi_get_main();
Damjan Marion5fec1e82017-04-13 19:13:47 +0200365 vac_main_t *pm = &vac_main;
Neale Rannsc16f6b32018-06-03 21:21:19 -0700366 uword junk;
Ole Troanc5607892019-04-10 19:32:02 +0200367 int rv = 0;
Damjan Marion7cd468a2016-12-19 23:05:39 +0100368
Ole Troandfc9b7c2017-03-06 23:51:57 +0100369 if (!pm->connected_to_vlib) return 0;
370
371 if (pm->rx_thread_handle) {
Damjan Marion7cd468a2016-12-19 23:05:39 +0100372 vl_api_rx_thread_exit_t *ep;
Damjan Marion7cd468a2016-12-19 23:05:39 +0100373 ep = vl_msg_api_alloc (sizeof (*ep));
374 ep->_vl_msg_id = ntohs(VL_API_RX_THREAD_EXIT);
375 vl_msg_api_send_shmem(am->vl_input_queue, (u8 *)&ep);
Ole Troandfc9b7c2017-03-06 23:51:57 +0100376
377 /* wait (with timeout) until RX thread has finished */
378 struct timespec ts;
379 struct timeval tv;
380 gettimeofday(&tv, NULL);
381 ts.tv_sec = tv.tv_sec + 5;
382 ts.tv_nsec = 0;
Ole Troanc5607892019-04-10 19:32:02 +0200383
Ole Troandfc9b7c2017-03-06 23:51:57 +0100384 pthread_mutex_lock(&pm->queue_lock);
Ole Troanc5607892019-04-10 19:32:02 +0200385 if (rx_thread_done == false)
386 rv = pthread_cond_timedwait(&pm->terminate_cv, &pm->queue_lock, &ts);
Ole Troandfc9b7c2017-03-06 23:51:57 +0100387 pthread_mutex_unlock(&pm->queue_lock);
Ole Troanc5607892019-04-10 19:32:02 +0200388
Ole Troandfc9b7c2017-03-06 23:51:57 +0100389 /* now join so we wait until thread has -really- finished */
390 if (rv == ETIMEDOUT)
391 pthread_cancel(pm->rx_thread_handle);
392 else
393 pthread_join(pm->rx_thread_handle, (void **) &junk);
Damjan Marion7cd468a2016-12-19 23:05:39 +0100394 }
Neale Rannsc16f6b32018-06-03 21:21:19 -0700395 if (pm->timeout_thread_handle) {
396 /* cancel, wake then join the timeout thread */
Neale Ranns1d652792018-07-26 08:05:53 -0700397 pm->timeout_loop = 0;
Ole Troan4e588aa2018-09-07 11:01:47 +0200398 timeout_thread_cancelled = true;
Neale Rannsc16f6b32018-06-03 21:21:19 -0700399 set_timeout(0);
400 pthread_join(pm->timeout_thread_handle, (void **) &junk);
401 }
Ole Troandfc9b7c2017-03-06 23:51:57 +0100402
403 vl_client_disconnect();
404 vl_client_api_unmap();
Ole Troandf87f802020-11-18 19:17:48 +0100405 //vac_callback = 0;
Ole Troandfc9b7c2017-03-06 23:51:57 +0100406
407 cleanup();
Damjan Marion7cd468a2016-12-19 23:05:39 +0100408
409 return (0);
410}
411
412int
Damjan Marion5fec1e82017-04-13 19:13:47 +0200413vac_read (char **p, int *l, u16 timeout)
Damjan Marion7cd468a2016-12-19 23:05:39 +0100414{
Florin Corase86a8ed2018-01-05 03:20:25 -0800415 svm_queue_t *q;
Dave Barach39d69112019-11-27 11:42:13 -0500416 api_main_t *am = vlibapi_get_main();
Damjan Marion5fec1e82017-04-13 19:13:47 +0200417 vac_main_t *pm = &vac_main;
Dave Barach59b25652017-09-10 15:04:27 -0400418 vl_api_memclnt_keepalive_t *mp;
419 vl_api_memclnt_keepalive_reply_t *rmp;
Damjan Marion7cd468a2016-12-19 23:05:39 +0100420 uword msg;
Ole Troandfc9b7c2017-03-06 23:51:57 +0100421 msgbuf_t *msgbuf;
Dave Barach59b25652017-09-10 15:04:27 -0400422 int rv;
423 vl_shmem_hdr_t *shmem_hdr;
Damjan Marion7cd468a2016-12-19 23:05:39 +0100424
Paul Vinciguerra08d82e92019-06-20 13:46:46 -0400425 /* svm_queue_sub(below) returns {-1, -2} */
Paul Vinciguerrabad47662019-12-19 18:26:29 -0500426 if (!pm->connected_to_vlib)
427 return VAC_NOT_CONNECTED;
Damjan Marion7cd468a2016-12-19 23:05:39 +0100428
429 *l = 0;
430
Paul Vinciguerra08d82e92019-06-20 13:46:46 -0400431 /* svm_queue_sub(below) returns {-1, -2} */
Paul Vinciguerrabad47662019-12-19 18:26:29 -0500432 if (am->our_pid == 0)
433 return (VAC_SHM_NOT_READY);
Damjan Marion7cd468a2016-12-19 23:05:39 +0100434
Ole Troandfc9b7c2017-03-06 23:51:57 +0100435 /* Poke timeout thread */
436 if (timeout)
437 set_timeout(timeout);
438
Damjan Marion7cd468a2016-12-19 23:05:39 +0100439 q = am->vl_input_queue;
Dave Barach59b25652017-09-10 15:04:27 -0400440
441 again:
Mohsin Kazmi3fca5672018-01-04 18:57:26 +0100442 rv = svm_queue_sub(q, (u8 *)&msg, SVM_Q_WAIT, 0);
443
Damjan Marion7cd468a2016-12-19 23:05:39 +0100444 if (rv == 0) {
Benoît Ganne9fb6d402019-04-15 15:28:21 +0200445 VL_MSG_API_UNPOISON((void *)msg);
Damjan Marion7cd468a2016-12-19 23:05:39 +0100446 u16 msg_id = ntohs(*((u16 *)msg));
Ole Troandfc9b7c2017-03-06 23:51:57 +0100447 switch (msg_id) {
448 case VL_API_RX_THREAD_EXIT:
Ole Troan73710c72018-06-04 22:27:49 +0200449 vl_msg_api_free((void *) msg);
Ole Troande728ac2018-10-08 11:24:22 +0200450 goto error;
Ole Troandfc9b7c2017-03-06 23:51:57 +0100451 case VL_API_MEMCLNT_RX_THREAD_SUSPEND:
Ole Troandfc9b7c2017-03-06 23:51:57 +0100452 goto error;
453 case VL_API_MEMCLNT_READ_TIMEOUT:
Ole Troandfc9b7c2017-03-06 23:51:57 +0100454 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));
Dave Barachb7b92992018-10-17 10:38:51 -0400459 clib_memset (rmp, 0, sizeof (*rmp));
Dave Barach59b25652017-09-10 15:04:27 -0400460 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);
Dave Barach39d69112019-11-27 11:42:13 -0500465 /*
Dave Barach59b25652017-09-10 15:04:27 -0400466 * 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) {
Ole Troan4e588aa2018-09-07 11:01:47 +0200475 fprintf(stderr, "Unregistered API message: %d\n", msg_id);
Ole Troandfc9b7c2017-03-06 23:51:57 +0100476 goto error;
477 }
Damjan Marion7cd468a2016-12-19 23:05:39 +0100478 }
479 *p = (char *)msg;
Ole Troandfc9b7c2017-03-06 23:51:57 +0100480
Ole Troandfc9b7c2017-03-06 23:51:57 +0100481
Damjan Marion7cd468a2016-12-19 23:05:39 +0100482 } else {
Ole Troan4e588aa2018-09-07 11:01:47 +0200483 fprintf(stderr, "Read failed with %d\n", rv);
Damjan Marion7cd468a2016-12-19 23:05:39 +0100484 }
Ole Troande728ac2018-10-08 11:24:22 +0200485 /* Let timeout notification thread know we're done */
486 if (timeout)
487 unset_timeout();
488
Damjan Marion7cd468a2016-12-19 23:05:39 +0100489 return (rv);
Ole Troandfc9b7c2017-03-06 23:51:57 +0100490
491 error:
Ole Troande728ac2018-10-08 11:24:22 +0200492 if (timeout)
493 unset_timeout();
Ole Troandfc9b7c2017-03-06 23:51:57 +0100494 vl_msg_api_free((void *) msg);
495 /* Client might forget to resume RX thread on failure */
Damjan Marion5fec1e82017-04-13 19:13:47 +0200496 vac_rx_resume ();
Paul Vinciguerrabad47662019-12-19 18:26:29 -0500497 return VAC_TIMEOUT;
Damjan Marion7cd468a2016-12-19 23:05:39 +0100498}
499
500/*
501 * XXX: Makes the assumption that client_index is the first member
502 */
503typedef VL_API_PACKED(struct _vl_api_header {
504 u16 _vl_msg_id;
505 u32 client_index;
506}) vl_api_header_t;
507
Ole Troan94495f22018-08-02 11:58:12 +0200508static u32
Damjan Marion5fec1e82017-04-13 19:13:47 +0200509vac_client_index (void)
Damjan Marion7cd468a2016-12-19 23:05:39 +0100510{
Dave Barach39d69112019-11-27 11:42:13 -0500511 return (vlibapi_get_main()->my_client_index);
Damjan Marion7cd468a2016-12-19 23:05:39 +0100512}
513
514int
Damjan Marion5fec1e82017-04-13 19:13:47 +0200515vac_write (char *p, int l)
Damjan Marion7cd468a2016-12-19 23:05:39 +0100516{
517 int rv = -1;
Dave Barach39d69112019-11-27 11:42:13 -0500518 api_main_t *am = vlibapi_get_main();
Damjan Marion7cd468a2016-12-19 23:05:39 +0100519 vl_api_header_t *mp = vl_msg_api_alloc(l);
Florin Corase86a8ed2018-01-05 03:20:25 -0800520 svm_queue_t *q;
Damjan Marion5fec1e82017-04-13 19:13:47 +0200521 vac_main_t *pm = &vac_main;
Damjan Marion7cd468a2016-12-19 23:05:39 +0100522
Paul Vinciguerrabad47662019-12-19 18:26:29 -0500523 if (!pm->connected_to_vlib)
524 return VAC_NOT_CONNECTED;
Damjan Marion7cd468a2016-12-19 23:05:39 +0100525 if (!mp) return (-1);
Ole Troandfc9b7c2017-03-06 23:51:57 +0100526
Damjan Marion7cd468a2016-12-19 23:05:39 +0100527 memcpy(mp, p, l);
Damjan Marion5fec1e82017-04-13 19:13:47 +0200528 mp->client_index = vac_client_index();
Damjan Marion7cd468a2016-12-19 23:05:39 +0100529 q = am->shmem_hdr->vl_input_queue;
Florin Corase86a8ed2018-01-05 03:20:25 -0800530 rv = svm_queue_add(q, (u8 *)&mp, 0);
Damjan Marion7cd468a2016-12-19 23:05:39 +0100531 if (rv != 0) {
Ole Troan4e588aa2018-09-07 11:01:47 +0200532 fprintf(stderr, "vpe_api_write fails: %d\n", rv);
Damjan Marion7cd468a2016-12-19 23:05:39 +0100533 /* Clear message */
Damjan Marion5fec1e82017-04-13 19:13:47 +0200534 vac_free(mp);
Damjan Marion7cd468a2016-12-19 23:05:39 +0100535 }
536 return (rv);
537}
538
Ole Troan3cc49712017-03-08 12:02:24 +0100539int
Ole Troandf87f802020-11-18 19:17:48 +0100540vac_get_msg_index (char * name)
Damjan Marion7cd468a2016-12-19 23:05:39 +0100541{
Ole Troandf87f802020-11-18 19:17:48 +0100542 return vl_msg_api_get_msg_index ((u8 *)name);
Damjan Marion7cd468a2016-12-19 23:05:39 +0100543}
Ole Troan3cc49712017-03-08 12:02:24 +0100544
545int
Damjan Marion5fec1e82017-04-13 19:13:47 +0200546vac_msg_table_max_index(void)
Ole Troan3cc49712017-03-08 12:02:24 +0100547{
548 int max = 0;
549 hash_pair_t *hp;
Damjan Marion5fec1e82017-04-13 19:13:47 +0200550 uword *h = vac_msg_table_get_hash();
Ole Troan3cc49712017-03-08 12:02:24 +0100551 hash_foreach_pair (hp, h,
552 ({
553 if (hp->value[0] > max)
554 max = hp->value[0];
555 }));
556
557 return max;
558}
559
560void
Damjan Marion5fec1e82017-04-13 19:13:47 +0200561vac_set_error_handler (vac_error_callback_t cb)
Ole Troan3cc49712017-03-08 12:02:24 +0100562{
Ole Troanf68fccf2020-09-28 16:15:18 +0200563 assert (clib_mem_get_heap ());
Ole Troan3cc49712017-03-08 12:02:24 +0100564 if (cb) clib_error_register_handler (cb, 0);
565}
Ole Troanf68fccf2020-09-28 16:15:18 +0200566
567/*
568 * Required if application doesn't use a VPP heap.
569 */
570void
571vac_mem_init (size_t size)
572{
Ole Troan65fa0362020-09-30 10:43:00 +0200573 if (mem_initialized)
574 return;
Ole Troanf68fccf2020-09-28 16:15:18 +0200575 if (size == 0)
576 clib_mem_init (0, 1 << 30); // default
577 else
578 clib_mem_init (0, size);
Ole Troan65fa0362020-09-30 10:43:00 +0200579 mem_initialized = true;
Ole Troanf68fccf2020-09-28 16:15:18 +0200580}