blob: 0d7b1dcafc81c3de167caaab1a6d821c5fc6c6d1 [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
82static void
83init (void)
84{
Damjan Marion5fec1e82017-04-13 19:13:47 +020085 vac_main_t *pm = &vac_main;
Dave Barachb7b92992018-10-17 10:38:51 -040086 clib_memset(pm, 0, sizeof(*pm));
Ole Troandfc9b7c2017-03-06 23:51:57 +010087 pthread_mutex_init(&pm->queue_lock, NULL);
88 pthread_cond_init(&pm->suspend_cv, NULL);
89 pthread_cond_init(&pm->resume_cv, NULL);
90 pthread_mutex_init(&pm->timeout_lock, NULL);
Neale Ranns1d652792018-07-26 08:05:53 -070091 pm->timeout_loop = 1;
Ole Troandfc9b7c2017-03-06 23:51:57 +010092 pthread_cond_init(&pm->timeout_cv, NULL);
93 pthread_cond_init(&pm->timeout_cancel_cv, NULL);
94 pthread_cond_init(&pm->terminate_cv, NULL);
95}
96
97static void
98cleanup (void)
99{
Damjan Marion5fec1e82017-04-13 19:13:47 +0200100 vac_main_t *pm = &vac_main;
Ole Troan73710c72018-06-04 22:27:49 +0200101 pthread_mutex_destroy(&pm->queue_lock);
Ole Troandfc9b7c2017-03-06 23:51:57 +0100102 pthread_cond_destroy(&pm->suspend_cv);
103 pthread_cond_destroy(&pm->resume_cv);
Ole Troan73710c72018-06-04 22:27:49 +0200104 pthread_mutex_destroy(&pm->timeout_lock);
Ole Troandfc9b7c2017-03-06 23:51:57 +0100105 pthread_cond_destroy(&pm->timeout_cv);
106 pthread_cond_destroy(&pm->timeout_cancel_cv);
107 pthread_cond_destroy(&pm->terminate_cv);
Dave Barachb7b92992018-10-17 10:38:51 -0400108 clib_memset(pm, 0, sizeof(*pm));
Ole Troandfc9b7c2017-03-06 23:51:57 +0100109}
Damjan Marion7cd468a2016-12-19 23:05:39 +0100110
111/*
112 * Satisfy external references when -lvlib is not available.
113 */
114void vlib_cli_output (struct vlib_main_t * vm, char * fmt, ...)
115{
116 clib_warning ("vlib_cli_output called...");
117}
118
119void
Damjan Marion5fec1e82017-04-13 19:13:47 +0200120vac_free (void * msg)
Damjan Marion7cd468a2016-12-19 23:05:39 +0100121{
122 vl_msg_api_free (msg);
123}
124
125static void
Damjan Marion5fec1e82017-04-13 19:13:47 +0200126vac_api_handler (void *msg)
Damjan Marion7cd468a2016-12-19 23:05:39 +0100127{
128 u16 id = ntohs(*((u16 *)msg));
Damjan Marion7cd468a2016-12-19 23:05:39 +0100129 msgbuf_t *msgbuf = (msgbuf_t *)(((u8 *)msg) - offsetof(msgbuf_t, data));
130 int l = ntohl(msgbuf->data_len);
131 if (l == 0)
132 clib_warning("Message ID %d has wrong length: %d\n", id, l);
133
134 /* Call Python callback */
Damjan Marion5fec1e82017-04-13 19:13:47 +0200135 ASSERT(vac_callback);
136 (vac_callback)(msg, l);
137 vac_free(msg);
Damjan Marion7cd468a2016-12-19 23:05:39 +0100138}
139
140static void *
Damjan Marion5fec1e82017-04-13 19:13:47 +0200141vac_rx_thread_fn (void *arg)
Damjan Marion7cd468a2016-12-19 23:05:39 +0100142{
Florin Corase86a8ed2018-01-05 03:20:25 -0800143 svm_queue_t *q;
Dave Barach59b25652017-09-10 15:04:27 -0400144 vl_api_memclnt_keepalive_t *mp;
145 vl_api_memclnt_keepalive_reply_t *rmp;
Damjan Marion5fec1e82017-04-13 19:13:47 +0200146 vac_main_t *pm = &vac_main;
Dave Barach39d69112019-11-27 11:42:13 -0500147 api_main_t *am = vlibapi_get_main();
Dave Barach59b25652017-09-10 15:04:27 -0400148 vl_shmem_hdr_t *shmem_hdr;
Damjan Marion7cd468a2016-12-19 23:05:39 +0100149 uword msg;
150
151 q = am->vl_input_queue;
152
Ole Troandfc9b7c2017-03-06 23:51:57 +0100153 while (1)
Mohsin Kazmi3fca5672018-01-04 18:57:26 +0100154 while (!svm_queue_sub(q, (u8 *)&msg, SVM_Q_WAIT, 0))
Ole Troandfc9b7c2017-03-06 23:51:57 +0100155 {
Benoît Ganne9fb6d402019-04-15 15:28:21 +0200156 VL_MSG_API_UNPOISON((void *)msg);
Ole Troandfc9b7c2017-03-06 23:51:57 +0100157 u16 id = ntohs(*((u16 *)msg));
158 switch (id) {
159 case VL_API_RX_THREAD_EXIT:
160 vl_msg_api_free((void *) msg);
161 /* signal waiting threads that this thread is about to terminate */
162 pthread_mutex_lock(&pm->queue_lock);
Ole Troanc5607892019-04-10 19:32:02 +0200163 rx_thread_done = true;
Ole Troandfc9b7c2017-03-06 23:51:57 +0100164 pthread_cond_signal(&pm->terminate_cv);
165 pthread_mutex_unlock(&pm->queue_lock);
166 pthread_exit(0);
167 return 0;
168 break;
169
170 case VL_API_MEMCLNT_RX_THREAD_SUSPEND:
171 vl_msg_api_free((void * )msg);
172 /* Suspend thread and signal reader */
173 pthread_mutex_lock(&pm->queue_lock);
174 pthread_cond_signal(&pm->suspend_cv);
175 /* Wait for the resume signal */
176 pthread_cond_wait (&pm->resume_cv, &pm->queue_lock);
177 pthread_mutex_unlock(&pm->queue_lock);
178 break;
179
180 case VL_API_MEMCLNT_READ_TIMEOUT:
181 clib_warning("Received read timeout in async thread\n");
182 vl_msg_api_free((void *) msg);
183 break;
184
Dave Barach59b25652017-09-10 15:04:27 -0400185 case VL_API_MEMCLNT_KEEPALIVE:
186 mp = (void *)msg;
187 rmp = vl_msg_api_alloc (sizeof (*rmp));
Dave Barachb7b92992018-10-17 10:38:51 -0400188 clib_memset (rmp, 0, sizeof (*rmp));
Dave Barach59b25652017-09-10 15:04:27 -0400189 rmp->_vl_msg_id = ntohs(VL_API_MEMCLNT_KEEPALIVE_REPLY);
190 rmp->context = mp->context;
191 shmem_hdr = am->shmem_hdr;
192 vl_msg_api_send_shmem(shmem_hdr->vl_input_queue, (u8 *)&rmp);
193 vl_msg_api_free((void *) msg);
194 break;
195
Ole Troandfc9b7c2017-03-06 23:51:57 +0100196 default:
Damjan Marion5fec1e82017-04-13 19:13:47 +0200197 vac_api_handler((void *)msg);
Ole Troandfc9b7c2017-03-06 23:51:57 +0100198 }
199 }
200}
201
202static void *
Damjan Marion5fec1e82017-04-13 19:13:47 +0200203vac_timeout_thread_fn (void *arg)
Ole Troandfc9b7c2017-03-06 23:51:57 +0100204{
205 vl_api_memclnt_read_timeout_t *ep;
Damjan Marion5fec1e82017-04-13 19:13:47 +0200206 vac_main_t *pm = &vac_main;
Dave Barach39d69112019-11-27 11:42:13 -0500207 api_main_t *am = vlibapi_get_main();
Ole Troandfc9b7c2017-03-06 23:51:57 +0100208 struct timespec ts;
209 struct timeval tv;
Ole Troandfc9b7c2017-03-06 23:51:57 +0100210 int rv;
211
Neale Ranns1d652792018-07-26 08:05:53 -0700212 while (pm->timeout_loop)
Ole Troandfc9b7c2017-03-06 23:51:57 +0100213 {
214 /* Wait for poke */
215 pthread_mutex_lock(&pm->timeout_lock);
Ole Troande728ac2018-10-08 11:24:22 +0200216 while (!timeout_in_progress)
217 pthread_cond_wait (&pm->timeout_cv, &pm->timeout_lock);
218
219 /* Starting timer */
Ole Troandfc9b7c2017-03-06 23:51:57 +0100220 gettimeofday(&tv, NULL);
Ole Troande728ac2018-10-08 11:24:22 +0200221 ts.tv_sec = tv.tv_sec + read_timeout;
Ole Troandfc9b7c2017-03-06 23:51:57 +0100222 ts.tv_nsec = 0;
Ole Troande728ac2018-10-08 11:24:22 +0200223
224 if (!timeout_cancelled) {
225 rv = pthread_cond_timedwait (&pm->timeout_cancel_cv,
226 &pm->timeout_lock, &ts);
227 if (rv == ETIMEDOUT && !timeout_thread_cancelled) {
Ole Troandfc9b7c2017-03-06 23:51:57 +0100228 ep = vl_msg_api_alloc (sizeof (*ep));
229 ep->_vl_msg_id = ntohs(VL_API_MEMCLNT_READ_TIMEOUT);
230 vl_msg_api_send_shmem(am->vl_input_queue, (u8 *)&ep);
231 }
Ole Troande728ac2018-10-08 11:24:22 +0200232 }
233
234 pthread_mutex_unlock(&pm->timeout_lock);
Ole Troandfc9b7c2017-03-06 23:51:57 +0100235 }
Damjan Marion7cd468a2016-12-19 23:05:39 +0100236 pthread_exit(0);
237}
238
Ole Troandfc9b7c2017-03-06 23:51:57 +0100239void
Damjan Marion5fec1e82017-04-13 19:13:47 +0200240vac_rx_suspend (void)
Ole Troandfc9b7c2017-03-06 23:51:57 +0100241{
Dave Barach39d69112019-11-27 11:42:13 -0500242 api_main_t *am = vlibapi_get_main();
Damjan Marion5fec1e82017-04-13 19:13:47 +0200243 vac_main_t *pm = &vac_main;
Ole Troandfc9b7c2017-03-06 23:51:57 +0100244 vl_api_memclnt_rx_thread_suspend_t *ep;
245
246 if (!pm->rx_thread_handle) return;
247 pthread_mutex_lock(&pm->queue_lock);
248 if (rx_is_running)
249 {
250 ep = vl_msg_api_alloc (sizeof (*ep));
251 ep->_vl_msg_id = ntohs(VL_API_MEMCLNT_RX_THREAD_SUSPEND);
252 vl_msg_api_send_shmem(am->vl_input_queue, (u8 *)&ep);
Paul Vinciguerraec11b132018-09-24 05:25:00 -0700253 /* Wait for RX thread to tell us it has suspended */
Ole Troandfc9b7c2017-03-06 23:51:57 +0100254 pthread_cond_wait(&pm->suspend_cv, &pm->queue_lock);
255 rx_is_running = false;
256 }
257 pthread_mutex_unlock(&pm->queue_lock);
258}
259
260void
Damjan Marion5fec1e82017-04-13 19:13:47 +0200261vac_rx_resume (void)
Ole Troandfc9b7c2017-03-06 23:51:57 +0100262{
Damjan Marion5fec1e82017-04-13 19:13:47 +0200263 vac_main_t *pm = &vac_main;
Ole Troandfc9b7c2017-03-06 23:51:57 +0100264 if (!pm->rx_thread_handle) return;
265 pthread_mutex_lock(&pm->queue_lock);
Ole Troanad0697a2017-03-09 21:10:45 +0100266 if (rx_is_running) goto unlock;
Ole Troandfc9b7c2017-03-06 23:51:57 +0100267 pthread_cond_signal(&pm->resume_cv);
268 rx_is_running = true;
Ole Troanad0697a2017-03-09 21:10:45 +0100269 unlock:
Ole Troandfc9b7c2017-03-06 23:51:57 +0100270 pthread_mutex_unlock(&pm->queue_lock);
271}
272
Ole Troan3cc49712017-03-08 12:02:24 +0100273static uword *
Damjan Marion5fec1e82017-04-13 19:13:47 +0200274vac_msg_table_get_hash (void)
Damjan Marion7cd468a2016-12-19 23:05:39 +0100275{
Dave Barach39d69112019-11-27 11:42:13 -0500276 api_main_t *am = vlibapi_get_main();
Damjan Marion7cd468a2016-12-19 23:05:39 +0100277 return (am->msg_index_by_name_and_crc);
278}
279
280int
Damjan Marion5fec1e82017-04-13 19:13:47 +0200281vac_msg_table_size(void)
Damjan Marion7cd468a2016-12-19 23:05:39 +0100282{
Dave Barach39d69112019-11-27 11:42:13 -0500283 api_main_t *am = vlibapi_get_main();
Damjan Marion7cd468a2016-12-19 23:05:39 +0100284 return hash_elts(am->msg_index_by_name_and_crc);
285}
286
287int
Damjan Marion5fec1e82017-04-13 19:13:47 +0200288vac_connect (char * name, char * chroot_prefix, vac_callback_t cb,
Ole Troanf68fccf2020-09-28 16:15:18 +0200289 int rx_qlen)
Damjan Marion7cd468a2016-12-19 23:05:39 +0100290{
Ole Troanc5607892019-04-10 19:32:02 +0200291 rx_thread_done = false;
Damjan Marion7cd468a2016-12-19 23:05:39 +0100292 int rv = 0;
Damjan Marion5fec1e82017-04-13 19:13:47 +0200293 vac_main_t *pm = &vac_main;
Damjan Marion7cd468a2016-12-19 23:05:39 +0100294
Ole Troanf68fccf2020-09-28 16:15:18 +0200295 assert (clib_mem_get_heap ());
Ole Troandfc9b7c2017-03-06 23:51:57 +0100296 init();
Damjan Marion7cd468a2016-12-19 23:05:39 +0100297 if (chroot_prefix != NULL)
298 vl_set_memory_root_path (chroot_prefix);
299
300 if ((rv = vl_client_api_map("/vpe-api"))) {
Ondrej Fabrye29cb672018-08-16 08:36:19 +0200301 clib_warning ("vl_client_api_map returned %d", rv);
Damjan Marion7cd468a2016-12-19 23:05:39 +0100302 return rv;
303 }
304
Dave Barachf9526922017-01-06 16:33:06 -0500305 if (vl_client_connect(name, 0, rx_qlen) < 0) {
Damjan Marion7cd468a2016-12-19 23:05:39 +0100306 vl_client_api_unmap();
307 return (-1);
308 }
309
310 if (cb) {
311 /* Start the rx queue thread */
Damjan Marion5fec1e82017-04-13 19:13:47 +0200312 rv = pthread_create(&pm->rx_thread_handle, NULL, vac_rx_thread_fn, 0);
Damjan Marion7cd468a2016-12-19 23:05:39 +0100313 if (rv) {
314 clib_warning("pthread_create returned %d", rv);
315 vl_client_api_unmap();
316 return (-1);
317 }
Damjan Marion5fec1e82017-04-13 19:13:47 +0200318 vac_callback = cb;
Ole Troandfc9b7c2017-03-06 23:51:57 +0100319 rx_is_running = true;
320 }
321
322 /* Start read timeout thread */
323 rv = pthread_create(&pm->timeout_thread_handle, NULL,
Damjan Marion5fec1e82017-04-13 19:13:47 +0200324 vac_timeout_thread_fn, 0);
Ole Troandfc9b7c2017-03-06 23:51:57 +0100325 if (rv) {
326 clib_warning("pthread_create returned %d", rv);
327 vl_client_api_unmap();
328 return (-1);
Damjan Marion7cd468a2016-12-19 23:05:39 +0100329 }
330
331 pm->connected_to_vlib = 1;
332
333 return (0);
334}
Neale Rannsc16f6b32018-06-03 21:21:19 -0700335static void
336set_timeout (unsigned short timeout)
337{
338 vac_main_t *pm = &vac_main;
339 pthread_mutex_lock(&pm->timeout_lock);
340 read_timeout = timeout;
Ole Troande728ac2018-10-08 11:24:22 +0200341 timeout_in_progress = true;
342 timeout_cancelled = false;
Neale Rannsc16f6b32018-06-03 21:21:19 -0700343 pthread_cond_signal(&pm->timeout_cv);
344 pthread_mutex_unlock(&pm->timeout_lock);
345}
346
347static void
348unset_timeout (void)
349{
350 vac_main_t *pm = &vac_main;
351 pthread_mutex_lock(&pm->timeout_lock);
Ole Troande728ac2018-10-08 11:24:22 +0200352 timeout_in_progress = false;
353 timeout_cancelled = true;
Neale Rannsc16f6b32018-06-03 21:21:19 -0700354 pthread_cond_signal(&pm->timeout_cancel_cv);
355 pthread_mutex_unlock(&pm->timeout_lock);
356}
357
Damjan Marion7cd468a2016-12-19 23:05:39 +0100358int
Damjan Marion5fec1e82017-04-13 19:13:47 +0200359vac_disconnect (void)
Damjan Marion7cd468a2016-12-19 23:05:39 +0100360{
Dave Barach39d69112019-11-27 11:42:13 -0500361 api_main_t *am = vlibapi_get_main();
Damjan Marion5fec1e82017-04-13 19:13:47 +0200362 vac_main_t *pm = &vac_main;
Neale Rannsc16f6b32018-06-03 21:21:19 -0700363 uword junk;
Ole Troanc5607892019-04-10 19:32:02 +0200364 int rv = 0;
Damjan Marion7cd468a2016-12-19 23:05:39 +0100365
Ole Troandfc9b7c2017-03-06 23:51:57 +0100366 if (!pm->connected_to_vlib) return 0;
367
368 if (pm->rx_thread_handle) {
Damjan Marion7cd468a2016-12-19 23:05:39 +0100369 vl_api_rx_thread_exit_t *ep;
Damjan Marion7cd468a2016-12-19 23:05:39 +0100370 ep = vl_msg_api_alloc (sizeof (*ep));
371 ep->_vl_msg_id = ntohs(VL_API_RX_THREAD_EXIT);
372 vl_msg_api_send_shmem(am->vl_input_queue, (u8 *)&ep);
Ole Troandfc9b7c2017-03-06 23:51:57 +0100373
374 /* wait (with timeout) until RX thread has finished */
375 struct timespec ts;
376 struct timeval tv;
377 gettimeofday(&tv, NULL);
378 ts.tv_sec = tv.tv_sec + 5;
379 ts.tv_nsec = 0;
Ole Troanc5607892019-04-10 19:32:02 +0200380
Ole Troandfc9b7c2017-03-06 23:51:57 +0100381 pthread_mutex_lock(&pm->queue_lock);
Ole Troanc5607892019-04-10 19:32:02 +0200382 if (rx_thread_done == false)
383 rv = pthread_cond_timedwait(&pm->terminate_cv, &pm->queue_lock, &ts);
Ole Troandfc9b7c2017-03-06 23:51:57 +0100384 pthread_mutex_unlock(&pm->queue_lock);
Ole Troanc5607892019-04-10 19:32:02 +0200385
Ole Troandfc9b7c2017-03-06 23:51:57 +0100386 /* now join so we wait until thread has -really- finished */
387 if (rv == ETIMEDOUT)
388 pthread_cancel(pm->rx_thread_handle);
389 else
390 pthread_join(pm->rx_thread_handle, (void **) &junk);
Damjan Marion7cd468a2016-12-19 23:05:39 +0100391 }
Neale Rannsc16f6b32018-06-03 21:21:19 -0700392 if (pm->timeout_thread_handle) {
393 /* cancel, wake then join the timeout thread */
Neale Ranns1d652792018-07-26 08:05:53 -0700394 pm->timeout_loop = 0;
Ole Troan4e588aa2018-09-07 11:01:47 +0200395 timeout_thread_cancelled = true;
Neale Rannsc16f6b32018-06-03 21:21:19 -0700396 set_timeout(0);
397 pthread_join(pm->timeout_thread_handle, (void **) &junk);
398 }
Ole Troandfc9b7c2017-03-06 23:51:57 +0100399
400 vl_client_disconnect();
401 vl_client_api_unmap();
Damjan Marion5fec1e82017-04-13 19:13:47 +0200402 vac_callback = 0;
Ole Troandfc9b7c2017-03-06 23:51:57 +0100403
404 cleanup();
Damjan Marion7cd468a2016-12-19 23:05:39 +0100405
406 return (0);
407}
408
409int
Damjan Marion5fec1e82017-04-13 19:13:47 +0200410vac_read (char **p, int *l, u16 timeout)
Damjan Marion7cd468a2016-12-19 23:05:39 +0100411{
Florin Corase86a8ed2018-01-05 03:20:25 -0800412 svm_queue_t *q;
Dave Barach39d69112019-11-27 11:42:13 -0500413 api_main_t *am = vlibapi_get_main();
Damjan Marion5fec1e82017-04-13 19:13:47 +0200414 vac_main_t *pm = &vac_main;
Dave Barach59b25652017-09-10 15:04:27 -0400415 vl_api_memclnt_keepalive_t *mp;
416 vl_api_memclnt_keepalive_reply_t *rmp;
Damjan Marion7cd468a2016-12-19 23:05:39 +0100417 uword msg;
Ole Troandfc9b7c2017-03-06 23:51:57 +0100418 msgbuf_t *msgbuf;
Dave Barach59b25652017-09-10 15:04:27 -0400419 int rv;
420 vl_shmem_hdr_t *shmem_hdr;
Damjan Marion7cd468a2016-12-19 23:05:39 +0100421
Paul Vinciguerra08d82e92019-06-20 13:46:46 -0400422 /* svm_queue_sub(below) returns {-1, -2} */
423 if (!pm->connected_to_vlib) return -3;
Damjan Marion7cd468a2016-12-19 23:05:39 +0100424
425 *l = 0;
426
Paul Vinciguerra08d82e92019-06-20 13:46:46 -0400427 /* svm_queue_sub(below) returns {-1, -2} */
428 if (am->our_pid == 0) return (-4);
Damjan Marion7cd468a2016-12-19 23:05:39 +0100429
Ole Troandfc9b7c2017-03-06 23:51:57 +0100430 /* Poke timeout thread */
431 if (timeout)
432 set_timeout(timeout);
433
Damjan Marion7cd468a2016-12-19 23:05:39 +0100434 q = am->vl_input_queue;
Dave Barach59b25652017-09-10 15:04:27 -0400435
436 again:
Mohsin Kazmi3fca5672018-01-04 18:57:26 +0100437 rv = svm_queue_sub(q, (u8 *)&msg, SVM_Q_WAIT, 0);
438
Damjan Marion7cd468a2016-12-19 23:05:39 +0100439 if (rv == 0) {
Benoît Ganne9fb6d402019-04-15 15:28:21 +0200440 VL_MSG_API_UNPOISON((void *)msg);
Damjan Marion7cd468a2016-12-19 23:05:39 +0100441 u16 msg_id = ntohs(*((u16 *)msg));
Ole Troandfc9b7c2017-03-06 23:51:57 +0100442 switch (msg_id) {
443 case VL_API_RX_THREAD_EXIT:
Ole Troan73710c72018-06-04 22:27:49 +0200444 vl_msg_api_free((void *) msg);
Ole Troande728ac2018-10-08 11:24:22 +0200445 goto error;
Ole Troandfc9b7c2017-03-06 23:51:57 +0100446 case VL_API_MEMCLNT_RX_THREAD_SUSPEND:
Ole Troandfc9b7c2017-03-06 23:51:57 +0100447 goto error;
448 case VL_API_MEMCLNT_READ_TIMEOUT:
Ole Troandfc9b7c2017-03-06 23:51:57 +0100449 goto error;
Dave Barach59b25652017-09-10 15:04:27 -0400450 case VL_API_MEMCLNT_KEEPALIVE:
451 /* Handle an alive-check ping from vpp. */
452 mp = (void *)msg;
453 rmp = vl_msg_api_alloc (sizeof (*rmp));
Dave Barachb7b92992018-10-17 10:38:51 -0400454 clib_memset (rmp, 0, sizeof (*rmp));
Dave Barach59b25652017-09-10 15:04:27 -0400455 rmp->_vl_msg_id = ntohs(VL_API_MEMCLNT_KEEPALIVE_REPLY);
456 rmp->context = mp->context;
457 shmem_hdr = am->shmem_hdr;
458 vl_msg_api_send_shmem(shmem_hdr->vl_input_queue, (u8 *)&rmp);
459 vl_msg_api_free((void *) msg);
Dave Barach39d69112019-11-27 11:42:13 -0500460 /*
Dave Barach59b25652017-09-10 15:04:27 -0400461 * Python code is blissfully unaware of these pings, so
462 * act as if it never happened...
463 */
464 goto again;
Ole Troandfc9b7c2017-03-06 23:51:57 +0100465
466 default:
467 msgbuf = (msgbuf_t *)(((u8 *)msg) - offsetof(msgbuf_t, data));
468 *l = ntohl(msgbuf->data_len);
469 if (*l == 0) {
Ole Troan4e588aa2018-09-07 11:01:47 +0200470 fprintf(stderr, "Unregistered API message: %d\n", msg_id);
Ole Troandfc9b7c2017-03-06 23:51:57 +0100471 goto error;
472 }
Damjan Marion7cd468a2016-12-19 23:05:39 +0100473 }
474 *p = (char *)msg;
Ole Troandfc9b7c2017-03-06 23:51:57 +0100475
Ole Troandfc9b7c2017-03-06 23:51:57 +0100476
Damjan Marion7cd468a2016-12-19 23:05:39 +0100477 } else {
Ole Troan4e588aa2018-09-07 11:01:47 +0200478 fprintf(stderr, "Read failed with %d\n", rv);
Damjan Marion7cd468a2016-12-19 23:05:39 +0100479 }
Ole Troande728ac2018-10-08 11:24:22 +0200480 /* Let timeout notification thread know we're done */
481 if (timeout)
482 unset_timeout();
483
Damjan Marion7cd468a2016-12-19 23:05:39 +0100484 return (rv);
Ole Troandfc9b7c2017-03-06 23:51:57 +0100485
486 error:
Ole Troande728ac2018-10-08 11:24:22 +0200487 if (timeout)
488 unset_timeout();
Ole Troandfc9b7c2017-03-06 23:51:57 +0100489 vl_msg_api_free((void *) msg);
490 /* Client might forget to resume RX thread on failure */
Damjan Marion5fec1e82017-04-13 19:13:47 +0200491 vac_rx_resume ();
Ole Troandfc9b7c2017-03-06 23:51:57 +0100492 return -1;
Damjan Marion7cd468a2016-12-19 23:05:39 +0100493}
494
495/*
496 * XXX: Makes the assumption that client_index is the first member
497 */
498typedef VL_API_PACKED(struct _vl_api_header {
499 u16 _vl_msg_id;
500 u32 client_index;
501}) vl_api_header_t;
502
Ole Troan94495f22018-08-02 11:58:12 +0200503static u32
Damjan Marion5fec1e82017-04-13 19:13:47 +0200504vac_client_index (void)
Damjan Marion7cd468a2016-12-19 23:05:39 +0100505{
Dave Barach39d69112019-11-27 11:42:13 -0500506 return (vlibapi_get_main()->my_client_index);
Damjan Marion7cd468a2016-12-19 23:05:39 +0100507}
508
509int
Damjan Marion5fec1e82017-04-13 19:13:47 +0200510vac_write (char *p, int l)
Damjan Marion7cd468a2016-12-19 23:05:39 +0100511{
512 int rv = -1;
Dave Barach39d69112019-11-27 11:42:13 -0500513 api_main_t *am = vlibapi_get_main();
Damjan Marion7cd468a2016-12-19 23:05:39 +0100514 vl_api_header_t *mp = vl_msg_api_alloc(l);
Florin Corase86a8ed2018-01-05 03:20:25 -0800515 svm_queue_t *q;
Damjan Marion5fec1e82017-04-13 19:13:47 +0200516 vac_main_t *pm = &vac_main;
Damjan Marion7cd468a2016-12-19 23:05:39 +0100517
518 if (!pm->connected_to_vlib) return -1;
519 if (!mp) return (-1);
Ole Troandfc9b7c2017-03-06 23:51:57 +0100520
Damjan Marion7cd468a2016-12-19 23:05:39 +0100521 memcpy(mp, p, l);
Damjan Marion5fec1e82017-04-13 19:13:47 +0200522 mp->client_index = vac_client_index();
Damjan Marion7cd468a2016-12-19 23:05:39 +0100523 q = am->shmem_hdr->vl_input_queue;
Florin Corase86a8ed2018-01-05 03:20:25 -0800524 rv = svm_queue_add(q, (u8 *)&mp, 0);
Damjan Marion7cd468a2016-12-19 23:05:39 +0100525 if (rv != 0) {
Ole Troan4e588aa2018-09-07 11:01:47 +0200526 fprintf(stderr, "vpe_api_write fails: %d\n", rv);
Damjan Marion7cd468a2016-12-19 23:05:39 +0100527 /* Clear message */
Damjan Marion5fec1e82017-04-13 19:13:47 +0200528 vac_free(mp);
Damjan Marion7cd468a2016-12-19 23:05:39 +0100529 }
530 return (rv);
531}
532
Ole Troan3cc49712017-03-08 12:02:24 +0100533int
Damjan Marion5fec1e82017-04-13 19:13:47 +0200534vac_get_msg_index (unsigned char * name)
Damjan Marion7cd468a2016-12-19 23:05:39 +0100535{
Florin Corase86a8ed2018-01-05 03:20:25 -0800536 return vl_msg_api_get_msg_index (name);
Damjan Marion7cd468a2016-12-19 23:05:39 +0100537}
Ole Troan3cc49712017-03-08 12:02:24 +0100538
539int
Damjan Marion5fec1e82017-04-13 19:13:47 +0200540vac_msg_table_max_index(void)
Ole Troan3cc49712017-03-08 12:02:24 +0100541{
542 int max = 0;
543 hash_pair_t *hp;
Damjan Marion5fec1e82017-04-13 19:13:47 +0200544 uword *h = vac_msg_table_get_hash();
Ole Troan3cc49712017-03-08 12:02:24 +0100545 hash_foreach_pair (hp, h,
546 ({
547 if (hp->value[0] > max)
548 max = hp->value[0];
549 }));
550
551 return max;
552}
553
554void
Damjan Marion5fec1e82017-04-13 19:13:47 +0200555vac_set_error_handler (vac_error_callback_t cb)
Ole Troan3cc49712017-03-08 12:02:24 +0100556{
Ole Troanf68fccf2020-09-28 16:15:18 +0200557 assert (clib_mem_get_heap ());
Ole Troan3cc49712017-03-08 12:02:24 +0100558 if (cb) clib_error_register_handler (cb, 0);
559}
Ole Troanf68fccf2020-09-28 16:15:18 +0200560
561/*
562 * Required if application doesn't use a VPP heap.
563 */
564void
565vac_mem_init (size_t size)
566{
567 if (size == 0)
568 clib_mem_init (0, 1 << 30); // default
569 else
570 clib_mem_init (0, size);
571}