Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 1 | /* |
| 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 Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 25 | #include <stdbool.h> |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 26 | #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 Marion | 5fec1e8 | 2017-04-13 19:13:47 +0200 | [diff] [blame] | 34 | #include "vppapiclient.h" |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 35 | |
Ole Troan | de728ac | 2018-10-08 11:24:22 +0200 | [diff] [blame] | 36 | bool timeout_cancelled; |
| 37 | bool timeout_in_progress; |
Ole Troan | c560789 | 2019-04-10 19:32:02 +0200 | [diff] [blame] | 38 | bool rx_thread_done; |
Ole Troan | de728ac | 2018-10-08 11:24:22 +0200 | [diff] [blame] | 39 | |
Ole Troan | dfc9b7c | 2017-03-06 23:51:57 +0100 | [diff] [blame] | 40 | /* |
| 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 Marion | 5fec1e8 | 2017-04-13 19:13:47 +0200 | [diff] [blame] | 46 | * vac_write() -> suspends RX thread |
| 47 | * vac_read() -> resumes RX thread |
Ole Troan | dfc9b7c | 2017-03-06 23:51:57 +0100 | [diff] [blame] | 48 | */ |
| 49 | |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 50 | #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 | |
| 58 | vlib_main_t vlib_global_main; |
| 59 | vlib_main_t **vlib_mains; |
| 60 | |
| 61 | typedef struct { |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 62 | u8 connected_to_vlib; |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 63 | pthread_t rx_thread_handle; |
Ole Troan | dfc9b7c | 2017-03-06 23:51:57 +0100 | [diff] [blame] | 64 | 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 Ranns | 1d65279 | 2018-07-26 08:05:53 -0700 | [diff] [blame] | 69 | u8 timeout_loop; |
Ole Troan | dfc9b7c | 2017-03-06 23:51:57 +0100 | [diff] [blame] | 70 | pthread_cond_t timeout_cv; |
| 71 | pthread_cond_t timeout_cancel_cv; |
| 72 | pthread_cond_t terminate_cv; |
Damjan Marion | 5fec1e8 | 2017-04-13 19:13:47 +0200 | [diff] [blame] | 73 | } vac_main_t; |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 74 | |
Damjan Marion | 5fec1e8 | 2017-04-13 19:13:47 +0200 | [diff] [blame] | 75 | vac_main_t vac_main; |
| 76 | vac_callback_t vac_callback; |
Ole Troan | dfc9b7c | 2017-03-06 23:51:57 +0100 | [diff] [blame] | 77 | u16 read_timeout = 0; |
| 78 | bool rx_is_running = false; |
Ole Troan | 4e588aa | 2018-09-07 11:01:47 +0200 | [diff] [blame] | 79 | bool timeout_thread_cancelled = false; |
Ole Troan | dfc9b7c | 2017-03-06 23:51:57 +0100 | [diff] [blame] | 80 | |
Ole Troan | 73710c7 | 2018-06-04 22:27:49 +0200 | [diff] [blame] | 81 | /* Set to true to enable memory tracing */ |
| 82 | bool mem_trace = false; |
| 83 | |
| 84 | __attribute__((constructor)) |
| 85 | static void |
| 86 | vac_client_constructor (void) |
| 87 | { |
Ole Troan | 73710c7 | 2018-06-04 22:27:49 +0200 | [diff] [blame] | 88 | clib_mem_init (0, 1 << 30); |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 89 | #if USE_DLMALLOC == 0 |
| 90 | { |
| 91 | u8 *heap; |
| 92 | mheap_t *h; |
| 93 | |
| 94 | heap = clib_mem_get_per_cpu_heap (); |
| 95 | h = mheap_header (heap); |
| 96 | /* make the main heap thread-safe */ |
| 97 | h->flags |= MHEAP_FLAG_THREAD_SAFE; |
| 98 | } |
| 99 | #endif |
Ole Troan | 73710c7 | 2018-06-04 22:27:49 +0200 | [diff] [blame] | 100 | if (mem_trace) |
| 101 | clib_mem_trace (1); |
| 102 | } |
| 103 | |
| 104 | __attribute__((destructor)) |
| 105 | static void |
| 106 | vac_client_destructor (void) |
| 107 | { |
| 108 | if (mem_trace) |
| 109 | fformat(stderr, "TRACE: %s", |
| 110 | format (0, "%U\n", |
| 111 | format_mheap, clib_mem_get_heap (), 1)); |
| 112 | } |
| 113 | |
| 114 | |
Ole Troan | dfc9b7c | 2017-03-06 23:51:57 +0100 | [diff] [blame] | 115 | static void |
| 116 | init (void) |
| 117 | { |
Damjan Marion | 5fec1e8 | 2017-04-13 19:13:47 +0200 | [diff] [blame] | 118 | vac_main_t *pm = &vac_main; |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 119 | clib_memset(pm, 0, sizeof(*pm)); |
Ole Troan | dfc9b7c | 2017-03-06 23:51:57 +0100 | [diff] [blame] | 120 | pthread_mutex_init(&pm->queue_lock, NULL); |
| 121 | pthread_cond_init(&pm->suspend_cv, NULL); |
| 122 | pthread_cond_init(&pm->resume_cv, NULL); |
| 123 | pthread_mutex_init(&pm->timeout_lock, NULL); |
Neale Ranns | 1d65279 | 2018-07-26 08:05:53 -0700 | [diff] [blame] | 124 | pm->timeout_loop = 1; |
Ole Troan | dfc9b7c | 2017-03-06 23:51:57 +0100 | [diff] [blame] | 125 | pthread_cond_init(&pm->timeout_cv, NULL); |
| 126 | pthread_cond_init(&pm->timeout_cancel_cv, NULL); |
| 127 | pthread_cond_init(&pm->terminate_cv, NULL); |
| 128 | } |
| 129 | |
| 130 | static void |
| 131 | cleanup (void) |
| 132 | { |
Damjan Marion | 5fec1e8 | 2017-04-13 19:13:47 +0200 | [diff] [blame] | 133 | vac_main_t *pm = &vac_main; |
Ole Troan | 73710c7 | 2018-06-04 22:27:49 +0200 | [diff] [blame] | 134 | pthread_mutex_destroy(&pm->queue_lock); |
Ole Troan | dfc9b7c | 2017-03-06 23:51:57 +0100 | [diff] [blame] | 135 | pthread_cond_destroy(&pm->suspend_cv); |
| 136 | pthread_cond_destroy(&pm->resume_cv); |
Ole Troan | 73710c7 | 2018-06-04 22:27:49 +0200 | [diff] [blame] | 137 | pthread_mutex_destroy(&pm->timeout_lock); |
Ole Troan | dfc9b7c | 2017-03-06 23:51:57 +0100 | [diff] [blame] | 138 | pthread_cond_destroy(&pm->timeout_cv); |
| 139 | pthread_cond_destroy(&pm->timeout_cancel_cv); |
| 140 | pthread_cond_destroy(&pm->terminate_cv); |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 141 | clib_memset(pm, 0, sizeof(*pm)); |
Ole Troan | dfc9b7c | 2017-03-06 23:51:57 +0100 | [diff] [blame] | 142 | } |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 143 | |
| 144 | /* |
| 145 | * Satisfy external references when -lvlib is not available. |
| 146 | */ |
| 147 | void vlib_cli_output (struct vlib_main_t * vm, char * fmt, ...) |
| 148 | { |
| 149 | clib_warning ("vlib_cli_output called..."); |
| 150 | } |
| 151 | |
| 152 | void |
Damjan Marion | 5fec1e8 | 2017-04-13 19:13:47 +0200 | [diff] [blame] | 153 | vac_free (void * msg) |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 154 | { |
| 155 | vl_msg_api_free (msg); |
| 156 | } |
| 157 | |
| 158 | static void |
Damjan Marion | 5fec1e8 | 2017-04-13 19:13:47 +0200 | [diff] [blame] | 159 | vac_api_handler (void *msg) |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 160 | { |
| 161 | u16 id = ntohs(*((u16 *)msg)); |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 162 | msgbuf_t *msgbuf = (msgbuf_t *)(((u8 *)msg) - offsetof(msgbuf_t, data)); |
| 163 | int l = ntohl(msgbuf->data_len); |
| 164 | if (l == 0) |
| 165 | clib_warning("Message ID %d has wrong length: %d\n", id, l); |
| 166 | |
| 167 | /* Call Python callback */ |
Damjan Marion | 5fec1e8 | 2017-04-13 19:13:47 +0200 | [diff] [blame] | 168 | ASSERT(vac_callback); |
| 169 | (vac_callback)(msg, l); |
| 170 | vac_free(msg); |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | static void * |
Damjan Marion | 5fec1e8 | 2017-04-13 19:13:47 +0200 | [diff] [blame] | 174 | vac_rx_thread_fn (void *arg) |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 175 | { |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 176 | svm_queue_t *q; |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 177 | vl_api_memclnt_keepalive_t *mp; |
| 178 | vl_api_memclnt_keepalive_reply_t *rmp; |
Damjan Marion | 5fec1e8 | 2017-04-13 19:13:47 +0200 | [diff] [blame] | 179 | vac_main_t *pm = &vac_main; |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 180 | api_main_t *am = &api_main; |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 181 | vl_shmem_hdr_t *shmem_hdr; |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 182 | uword msg; |
| 183 | |
| 184 | q = am->vl_input_queue; |
| 185 | |
Ole Troan | dfc9b7c | 2017-03-06 23:51:57 +0100 | [diff] [blame] | 186 | while (1) |
Mohsin Kazmi | 3fca567 | 2018-01-04 18:57:26 +0100 | [diff] [blame] | 187 | while (!svm_queue_sub(q, (u8 *)&msg, SVM_Q_WAIT, 0)) |
Ole Troan | dfc9b7c | 2017-03-06 23:51:57 +0100 | [diff] [blame] | 188 | { |
| 189 | u16 id = ntohs(*((u16 *)msg)); |
| 190 | switch (id) { |
| 191 | case VL_API_RX_THREAD_EXIT: |
| 192 | vl_msg_api_free((void *) msg); |
| 193 | /* signal waiting threads that this thread is about to terminate */ |
| 194 | pthread_mutex_lock(&pm->queue_lock); |
Ole Troan | c560789 | 2019-04-10 19:32:02 +0200 | [diff] [blame] | 195 | rx_thread_done = true; |
Ole Troan | dfc9b7c | 2017-03-06 23:51:57 +0100 | [diff] [blame] | 196 | pthread_cond_signal(&pm->terminate_cv); |
| 197 | pthread_mutex_unlock(&pm->queue_lock); |
| 198 | pthread_exit(0); |
| 199 | return 0; |
| 200 | break; |
| 201 | |
| 202 | case VL_API_MEMCLNT_RX_THREAD_SUSPEND: |
| 203 | vl_msg_api_free((void * )msg); |
| 204 | /* Suspend thread and signal reader */ |
| 205 | pthread_mutex_lock(&pm->queue_lock); |
| 206 | pthread_cond_signal(&pm->suspend_cv); |
| 207 | /* Wait for the resume signal */ |
| 208 | pthread_cond_wait (&pm->resume_cv, &pm->queue_lock); |
| 209 | pthread_mutex_unlock(&pm->queue_lock); |
| 210 | break; |
| 211 | |
| 212 | case VL_API_MEMCLNT_READ_TIMEOUT: |
| 213 | clib_warning("Received read timeout in async thread\n"); |
| 214 | vl_msg_api_free((void *) msg); |
| 215 | break; |
| 216 | |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 217 | case VL_API_MEMCLNT_KEEPALIVE: |
| 218 | mp = (void *)msg; |
| 219 | rmp = vl_msg_api_alloc (sizeof (*rmp)); |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 220 | clib_memset (rmp, 0, sizeof (*rmp)); |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 221 | rmp->_vl_msg_id = ntohs(VL_API_MEMCLNT_KEEPALIVE_REPLY); |
| 222 | rmp->context = mp->context; |
| 223 | shmem_hdr = am->shmem_hdr; |
| 224 | vl_msg_api_send_shmem(shmem_hdr->vl_input_queue, (u8 *)&rmp); |
| 225 | vl_msg_api_free((void *) msg); |
| 226 | break; |
| 227 | |
Ole Troan | dfc9b7c | 2017-03-06 23:51:57 +0100 | [diff] [blame] | 228 | default: |
Damjan Marion | 5fec1e8 | 2017-04-13 19:13:47 +0200 | [diff] [blame] | 229 | vac_api_handler((void *)msg); |
Ole Troan | dfc9b7c | 2017-03-06 23:51:57 +0100 | [diff] [blame] | 230 | } |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | static void * |
Damjan Marion | 5fec1e8 | 2017-04-13 19:13:47 +0200 | [diff] [blame] | 235 | vac_timeout_thread_fn (void *arg) |
Ole Troan | dfc9b7c | 2017-03-06 23:51:57 +0100 | [diff] [blame] | 236 | { |
| 237 | vl_api_memclnt_read_timeout_t *ep; |
Damjan Marion | 5fec1e8 | 2017-04-13 19:13:47 +0200 | [diff] [blame] | 238 | vac_main_t *pm = &vac_main; |
Ole Troan | dfc9b7c | 2017-03-06 23:51:57 +0100 | [diff] [blame] | 239 | api_main_t *am = &api_main; |
| 240 | struct timespec ts; |
| 241 | struct timeval tv; |
Ole Troan | dfc9b7c | 2017-03-06 23:51:57 +0100 | [diff] [blame] | 242 | int rv; |
| 243 | |
Neale Ranns | 1d65279 | 2018-07-26 08:05:53 -0700 | [diff] [blame] | 244 | while (pm->timeout_loop) |
Ole Troan | dfc9b7c | 2017-03-06 23:51:57 +0100 | [diff] [blame] | 245 | { |
| 246 | /* Wait for poke */ |
| 247 | pthread_mutex_lock(&pm->timeout_lock); |
Ole Troan | de728ac | 2018-10-08 11:24:22 +0200 | [diff] [blame] | 248 | while (!timeout_in_progress) |
| 249 | pthread_cond_wait (&pm->timeout_cv, &pm->timeout_lock); |
| 250 | |
| 251 | /* Starting timer */ |
Ole Troan | dfc9b7c | 2017-03-06 23:51:57 +0100 | [diff] [blame] | 252 | gettimeofday(&tv, NULL); |
Ole Troan | de728ac | 2018-10-08 11:24:22 +0200 | [diff] [blame] | 253 | ts.tv_sec = tv.tv_sec + read_timeout; |
Ole Troan | dfc9b7c | 2017-03-06 23:51:57 +0100 | [diff] [blame] | 254 | ts.tv_nsec = 0; |
Ole Troan | de728ac | 2018-10-08 11:24:22 +0200 | [diff] [blame] | 255 | |
| 256 | if (!timeout_cancelled) { |
| 257 | rv = pthread_cond_timedwait (&pm->timeout_cancel_cv, |
| 258 | &pm->timeout_lock, &ts); |
| 259 | if (rv == ETIMEDOUT && !timeout_thread_cancelled) { |
Ole Troan | dfc9b7c | 2017-03-06 23:51:57 +0100 | [diff] [blame] | 260 | ep = vl_msg_api_alloc (sizeof (*ep)); |
| 261 | ep->_vl_msg_id = ntohs(VL_API_MEMCLNT_READ_TIMEOUT); |
| 262 | vl_msg_api_send_shmem(am->vl_input_queue, (u8 *)&ep); |
| 263 | } |
Ole Troan | de728ac | 2018-10-08 11:24:22 +0200 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | pthread_mutex_unlock(&pm->timeout_lock); |
Ole Troan | dfc9b7c | 2017-03-06 23:51:57 +0100 | [diff] [blame] | 267 | } |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 268 | pthread_exit(0); |
| 269 | } |
| 270 | |
Ole Troan | dfc9b7c | 2017-03-06 23:51:57 +0100 | [diff] [blame] | 271 | void |
Damjan Marion | 5fec1e8 | 2017-04-13 19:13:47 +0200 | [diff] [blame] | 272 | vac_rx_suspend (void) |
Ole Troan | dfc9b7c | 2017-03-06 23:51:57 +0100 | [diff] [blame] | 273 | { |
| 274 | api_main_t *am = &api_main; |
Damjan Marion | 5fec1e8 | 2017-04-13 19:13:47 +0200 | [diff] [blame] | 275 | vac_main_t *pm = &vac_main; |
Ole Troan | dfc9b7c | 2017-03-06 23:51:57 +0100 | [diff] [blame] | 276 | vl_api_memclnt_rx_thread_suspend_t *ep; |
| 277 | |
| 278 | if (!pm->rx_thread_handle) return; |
| 279 | pthread_mutex_lock(&pm->queue_lock); |
| 280 | if (rx_is_running) |
| 281 | { |
| 282 | ep = vl_msg_api_alloc (sizeof (*ep)); |
| 283 | ep->_vl_msg_id = ntohs(VL_API_MEMCLNT_RX_THREAD_SUSPEND); |
| 284 | vl_msg_api_send_shmem(am->vl_input_queue, (u8 *)&ep); |
Paul Vinciguerra | ec11b13 | 2018-09-24 05:25:00 -0700 | [diff] [blame] | 285 | /* Wait for RX thread to tell us it has suspended */ |
Ole Troan | dfc9b7c | 2017-03-06 23:51:57 +0100 | [diff] [blame] | 286 | pthread_cond_wait(&pm->suspend_cv, &pm->queue_lock); |
| 287 | rx_is_running = false; |
| 288 | } |
| 289 | pthread_mutex_unlock(&pm->queue_lock); |
| 290 | } |
| 291 | |
| 292 | void |
Damjan Marion | 5fec1e8 | 2017-04-13 19:13:47 +0200 | [diff] [blame] | 293 | vac_rx_resume (void) |
Ole Troan | dfc9b7c | 2017-03-06 23:51:57 +0100 | [diff] [blame] | 294 | { |
Damjan Marion | 5fec1e8 | 2017-04-13 19:13:47 +0200 | [diff] [blame] | 295 | vac_main_t *pm = &vac_main; |
Ole Troan | dfc9b7c | 2017-03-06 23:51:57 +0100 | [diff] [blame] | 296 | if (!pm->rx_thread_handle) return; |
| 297 | pthread_mutex_lock(&pm->queue_lock); |
Ole Troan | ad0697a | 2017-03-09 21:10:45 +0100 | [diff] [blame] | 298 | if (rx_is_running) goto unlock; |
Ole Troan | dfc9b7c | 2017-03-06 23:51:57 +0100 | [diff] [blame] | 299 | pthread_cond_signal(&pm->resume_cv); |
| 300 | rx_is_running = true; |
Ole Troan | ad0697a | 2017-03-09 21:10:45 +0100 | [diff] [blame] | 301 | unlock: |
Ole Troan | dfc9b7c | 2017-03-06 23:51:57 +0100 | [diff] [blame] | 302 | pthread_mutex_unlock(&pm->queue_lock); |
| 303 | } |
| 304 | |
Ole Troan | 3cc4971 | 2017-03-08 12:02:24 +0100 | [diff] [blame] | 305 | static uword * |
Damjan Marion | 5fec1e8 | 2017-04-13 19:13:47 +0200 | [diff] [blame] | 306 | vac_msg_table_get_hash (void) |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 307 | { |
| 308 | api_main_t *am = &api_main; |
| 309 | return (am->msg_index_by_name_and_crc); |
| 310 | } |
| 311 | |
| 312 | int |
Damjan Marion | 5fec1e8 | 2017-04-13 19:13:47 +0200 | [diff] [blame] | 313 | vac_msg_table_size(void) |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 314 | { |
| 315 | api_main_t *am = &api_main; |
| 316 | return hash_elts(am->msg_index_by_name_and_crc); |
| 317 | } |
| 318 | |
| 319 | int |
Damjan Marion | 5fec1e8 | 2017-04-13 19:13:47 +0200 | [diff] [blame] | 320 | vac_connect (char * name, char * chroot_prefix, vac_callback_t cb, |
Dave Barach | f952692 | 2017-01-06 16:33:06 -0500 | [diff] [blame] | 321 | int rx_qlen) |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 322 | { |
Ole Troan | c560789 | 2019-04-10 19:32:02 +0200 | [diff] [blame] | 323 | rx_thread_done = false; |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 324 | int rv = 0; |
Damjan Marion | 5fec1e8 | 2017-04-13 19:13:47 +0200 | [diff] [blame] | 325 | vac_main_t *pm = &vac_main; |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 326 | |
Ole Troan | dfc9b7c | 2017-03-06 23:51:57 +0100 | [diff] [blame] | 327 | init(); |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 328 | if (chroot_prefix != NULL) |
| 329 | vl_set_memory_root_path (chroot_prefix); |
| 330 | |
| 331 | if ((rv = vl_client_api_map("/vpe-api"))) { |
Ondrej Fabry | e29cb67 | 2018-08-16 08:36:19 +0200 | [diff] [blame] | 332 | clib_warning ("vl_client_api_map returned %d", rv); |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 333 | return rv; |
| 334 | } |
| 335 | |
Dave Barach | f952692 | 2017-01-06 16:33:06 -0500 | [diff] [blame] | 336 | if (vl_client_connect(name, 0, rx_qlen) < 0) { |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 337 | vl_client_api_unmap(); |
| 338 | return (-1); |
| 339 | } |
| 340 | |
| 341 | if (cb) { |
| 342 | /* Start the rx queue thread */ |
Damjan Marion | 5fec1e8 | 2017-04-13 19:13:47 +0200 | [diff] [blame] | 343 | rv = pthread_create(&pm->rx_thread_handle, NULL, vac_rx_thread_fn, 0); |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 344 | if (rv) { |
| 345 | clib_warning("pthread_create returned %d", rv); |
| 346 | vl_client_api_unmap(); |
| 347 | return (-1); |
| 348 | } |
Damjan Marion | 5fec1e8 | 2017-04-13 19:13:47 +0200 | [diff] [blame] | 349 | vac_callback = cb; |
Ole Troan | dfc9b7c | 2017-03-06 23:51:57 +0100 | [diff] [blame] | 350 | rx_is_running = true; |
| 351 | } |
| 352 | |
| 353 | /* Start read timeout thread */ |
| 354 | rv = pthread_create(&pm->timeout_thread_handle, NULL, |
Damjan Marion | 5fec1e8 | 2017-04-13 19:13:47 +0200 | [diff] [blame] | 355 | vac_timeout_thread_fn, 0); |
Ole Troan | dfc9b7c | 2017-03-06 23:51:57 +0100 | [diff] [blame] | 356 | if (rv) { |
| 357 | clib_warning("pthread_create returned %d", rv); |
| 358 | vl_client_api_unmap(); |
| 359 | return (-1); |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 360 | } |
| 361 | |
| 362 | pm->connected_to_vlib = 1; |
| 363 | |
| 364 | return (0); |
| 365 | } |
Neale Ranns | c16f6b3 | 2018-06-03 21:21:19 -0700 | [diff] [blame] | 366 | static void |
| 367 | set_timeout (unsigned short timeout) |
| 368 | { |
| 369 | vac_main_t *pm = &vac_main; |
| 370 | pthread_mutex_lock(&pm->timeout_lock); |
| 371 | read_timeout = timeout; |
Ole Troan | de728ac | 2018-10-08 11:24:22 +0200 | [diff] [blame] | 372 | timeout_in_progress = true; |
| 373 | timeout_cancelled = false; |
Neale Ranns | c16f6b3 | 2018-06-03 21:21:19 -0700 | [diff] [blame] | 374 | pthread_cond_signal(&pm->timeout_cv); |
| 375 | pthread_mutex_unlock(&pm->timeout_lock); |
| 376 | } |
| 377 | |
| 378 | static void |
| 379 | unset_timeout (void) |
| 380 | { |
| 381 | vac_main_t *pm = &vac_main; |
| 382 | pthread_mutex_lock(&pm->timeout_lock); |
Ole Troan | de728ac | 2018-10-08 11:24:22 +0200 | [diff] [blame] | 383 | timeout_in_progress = false; |
| 384 | timeout_cancelled = true; |
Neale Ranns | c16f6b3 | 2018-06-03 21:21:19 -0700 | [diff] [blame] | 385 | pthread_cond_signal(&pm->timeout_cancel_cv); |
| 386 | pthread_mutex_unlock(&pm->timeout_lock); |
| 387 | } |
| 388 | |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 389 | int |
Damjan Marion | 5fec1e8 | 2017-04-13 19:13:47 +0200 | [diff] [blame] | 390 | vac_disconnect (void) |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 391 | { |
| 392 | api_main_t *am = &api_main; |
Damjan Marion | 5fec1e8 | 2017-04-13 19:13:47 +0200 | [diff] [blame] | 393 | vac_main_t *pm = &vac_main; |
Neale Ranns | c16f6b3 | 2018-06-03 21:21:19 -0700 | [diff] [blame] | 394 | uword junk; |
Ole Troan | c560789 | 2019-04-10 19:32:02 +0200 | [diff] [blame] | 395 | int rv = 0; |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 396 | |
Ole Troan | dfc9b7c | 2017-03-06 23:51:57 +0100 | [diff] [blame] | 397 | if (!pm->connected_to_vlib) return 0; |
| 398 | |
| 399 | if (pm->rx_thread_handle) { |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 400 | vl_api_rx_thread_exit_t *ep; |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 401 | ep = vl_msg_api_alloc (sizeof (*ep)); |
| 402 | ep->_vl_msg_id = ntohs(VL_API_RX_THREAD_EXIT); |
| 403 | vl_msg_api_send_shmem(am->vl_input_queue, (u8 *)&ep); |
Ole Troan | dfc9b7c | 2017-03-06 23:51:57 +0100 | [diff] [blame] | 404 | |
| 405 | /* wait (with timeout) until RX thread has finished */ |
| 406 | struct timespec ts; |
| 407 | struct timeval tv; |
| 408 | gettimeofday(&tv, NULL); |
| 409 | ts.tv_sec = tv.tv_sec + 5; |
| 410 | ts.tv_nsec = 0; |
Ole Troan | c560789 | 2019-04-10 19:32:02 +0200 | [diff] [blame] | 411 | |
Ole Troan | dfc9b7c | 2017-03-06 23:51:57 +0100 | [diff] [blame] | 412 | pthread_mutex_lock(&pm->queue_lock); |
Ole Troan | c560789 | 2019-04-10 19:32:02 +0200 | [diff] [blame] | 413 | if (rx_thread_done == false) |
| 414 | rv = pthread_cond_timedwait(&pm->terminate_cv, &pm->queue_lock, &ts); |
Ole Troan | dfc9b7c | 2017-03-06 23:51:57 +0100 | [diff] [blame] | 415 | pthread_mutex_unlock(&pm->queue_lock); |
Ole Troan | c560789 | 2019-04-10 19:32:02 +0200 | [diff] [blame] | 416 | |
Ole Troan | dfc9b7c | 2017-03-06 23:51:57 +0100 | [diff] [blame] | 417 | /* now join so we wait until thread has -really- finished */ |
| 418 | if (rv == ETIMEDOUT) |
| 419 | pthread_cancel(pm->rx_thread_handle); |
| 420 | else |
| 421 | pthread_join(pm->rx_thread_handle, (void **) &junk); |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 422 | } |
Neale Ranns | c16f6b3 | 2018-06-03 21:21:19 -0700 | [diff] [blame] | 423 | if (pm->timeout_thread_handle) { |
| 424 | /* cancel, wake then join the timeout thread */ |
Neale Ranns | 1d65279 | 2018-07-26 08:05:53 -0700 | [diff] [blame] | 425 | pm->timeout_loop = 0; |
Ole Troan | 4e588aa | 2018-09-07 11:01:47 +0200 | [diff] [blame] | 426 | timeout_thread_cancelled = true; |
Neale Ranns | c16f6b3 | 2018-06-03 21:21:19 -0700 | [diff] [blame] | 427 | set_timeout(0); |
| 428 | pthread_join(pm->timeout_thread_handle, (void **) &junk); |
| 429 | } |
Ole Troan | dfc9b7c | 2017-03-06 23:51:57 +0100 | [diff] [blame] | 430 | |
| 431 | vl_client_disconnect(); |
| 432 | vl_client_api_unmap(); |
Damjan Marion | 5fec1e8 | 2017-04-13 19:13:47 +0200 | [diff] [blame] | 433 | vac_callback = 0; |
Ole Troan | dfc9b7c | 2017-03-06 23:51:57 +0100 | [diff] [blame] | 434 | |
| 435 | cleanup(); |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 436 | |
| 437 | return (0); |
| 438 | } |
| 439 | |
| 440 | int |
Damjan Marion | 5fec1e8 | 2017-04-13 19:13:47 +0200 | [diff] [blame] | 441 | vac_read (char **p, int *l, u16 timeout) |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 442 | { |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 443 | svm_queue_t *q; |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 444 | api_main_t *am = &api_main; |
Damjan Marion | 5fec1e8 | 2017-04-13 19:13:47 +0200 | [diff] [blame] | 445 | vac_main_t *pm = &vac_main; |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 446 | vl_api_memclnt_keepalive_t *mp; |
| 447 | vl_api_memclnt_keepalive_reply_t *rmp; |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 448 | uword msg; |
Ole Troan | dfc9b7c | 2017-03-06 23:51:57 +0100 | [diff] [blame] | 449 | msgbuf_t *msgbuf; |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 450 | int rv; |
| 451 | vl_shmem_hdr_t *shmem_hdr; |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 452 | |
| 453 | if (!pm->connected_to_vlib) return -1; |
| 454 | |
| 455 | *l = 0; |
| 456 | |
| 457 | if (am->our_pid == 0) return (-1); |
| 458 | |
Ole Troan | dfc9b7c | 2017-03-06 23:51:57 +0100 | [diff] [blame] | 459 | /* Poke timeout thread */ |
| 460 | if (timeout) |
| 461 | set_timeout(timeout); |
| 462 | |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 463 | q = am->vl_input_queue; |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 464 | |
| 465 | again: |
Mohsin Kazmi | 3fca567 | 2018-01-04 18:57:26 +0100 | [diff] [blame] | 466 | rv = svm_queue_sub(q, (u8 *)&msg, SVM_Q_WAIT, 0); |
| 467 | |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 468 | if (rv == 0) { |
| 469 | u16 msg_id = ntohs(*((u16 *)msg)); |
Ole Troan | dfc9b7c | 2017-03-06 23:51:57 +0100 | [diff] [blame] | 470 | switch (msg_id) { |
| 471 | case VL_API_RX_THREAD_EXIT: |
Ole Troan | 73710c7 | 2018-06-04 22:27:49 +0200 | [diff] [blame] | 472 | vl_msg_api_free((void *) msg); |
Ole Troan | de728ac | 2018-10-08 11:24:22 +0200 | [diff] [blame] | 473 | goto error; |
Ole Troan | dfc9b7c | 2017-03-06 23:51:57 +0100 | [diff] [blame] | 474 | case VL_API_MEMCLNT_RX_THREAD_SUSPEND: |
Ole Troan | dfc9b7c | 2017-03-06 23:51:57 +0100 | [diff] [blame] | 475 | goto error; |
| 476 | case VL_API_MEMCLNT_READ_TIMEOUT: |
Ole Troan | dfc9b7c | 2017-03-06 23:51:57 +0100 | [diff] [blame] | 477 | goto error; |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 478 | case VL_API_MEMCLNT_KEEPALIVE: |
| 479 | /* Handle an alive-check ping from vpp. */ |
| 480 | mp = (void *)msg; |
| 481 | rmp = vl_msg_api_alloc (sizeof (*rmp)); |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 482 | clib_memset (rmp, 0, sizeof (*rmp)); |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 483 | rmp->_vl_msg_id = ntohs(VL_API_MEMCLNT_KEEPALIVE_REPLY); |
| 484 | rmp->context = mp->context; |
| 485 | shmem_hdr = am->shmem_hdr; |
| 486 | vl_msg_api_send_shmem(shmem_hdr->vl_input_queue, (u8 *)&rmp); |
| 487 | vl_msg_api_free((void *) msg); |
| 488 | /* |
| 489 | * Python code is blissfully unaware of these pings, so |
| 490 | * act as if it never happened... |
| 491 | */ |
| 492 | goto again; |
Ole Troan | dfc9b7c | 2017-03-06 23:51:57 +0100 | [diff] [blame] | 493 | |
| 494 | default: |
| 495 | msgbuf = (msgbuf_t *)(((u8 *)msg) - offsetof(msgbuf_t, data)); |
| 496 | *l = ntohl(msgbuf->data_len); |
| 497 | if (*l == 0) { |
Ole Troan | 4e588aa | 2018-09-07 11:01:47 +0200 | [diff] [blame] | 498 | fprintf(stderr, "Unregistered API message: %d\n", msg_id); |
Ole Troan | dfc9b7c | 2017-03-06 23:51:57 +0100 | [diff] [blame] | 499 | goto error; |
| 500 | } |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 501 | } |
| 502 | *p = (char *)msg; |
Ole Troan | dfc9b7c | 2017-03-06 23:51:57 +0100 | [diff] [blame] | 503 | |
Ole Troan | dfc9b7c | 2017-03-06 23:51:57 +0100 | [diff] [blame] | 504 | |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 505 | } else { |
Ole Troan | 4e588aa | 2018-09-07 11:01:47 +0200 | [diff] [blame] | 506 | fprintf(stderr, "Read failed with %d\n", rv); |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 507 | } |
Ole Troan | de728ac | 2018-10-08 11:24:22 +0200 | [diff] [blame] | 508 | /* Let timeout notification thread know we're done */ |
| 509 | if (timeout) |
| 510 | unset_timeout(); |
| 511 | |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 512 | return (rv); |
Ole Troan | dfc9b7c | 2017-03-06 23:51:57 +0100 | [diff] [blame] | 513 | |
| 514 | error: |
Ole Troan | de728ac | 2018-10-08 11:24:22 +0200 | [diff] [blame] | 515 | if (timeout) |
| 516 | unset_timeout(); |
Ole Troan | dfc9b7c | 2017-03-06 23:51:57 +0100 | [diff] [blame] | 517 | vl_msg_api_free((void *) msg); |
| 518 | /* Client might forget to resume RX thread on failure */ |
Damjan Marion | 5fec1e8 | 2017-04-13 19:13:47 +0200 | [diff] [blame] | 519 | vac_rx_resume (); |
Ole Troan | dfc9b7c | 2017-03-06 23:51:57 +0100 | [diff] [blame] | 520 | return -1; |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 521 | } |
| 522 | |
| 523 | /* |
| 524 | * XXX: Makes the assumption that client_index is the first member |
| 525 | */ |
| 526 | typedef VL_API_PACKED(struct _vl_api_header { |
| 527 | u16 _vl_msg_id; |
| 528 | u32 client_index; |
| 529 | }) vl_api_header_t; |
| 530 | |
Ole Troan | 94495f2 | 2018-08-02 11:58:12 +0200 | [diff] [blame] | 531 | static u32 |
Damjan Marion | 5fec1e8 | 2017-04-13 19:13:47 +0200 | [diff] [blame] | 532 | vac_client_index (void) |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 533 | { |
| 534 | return (api_main.my_client_index); |
| 535 | } |
| 536 | |
| 537 | int |
Damjan Marion | 5fec1e8 | 2017-04-13 19:13:47 +0200 | [diff] [blame] | 538 | vac_write (char *p, int l) |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 539 | { |
| 540 | int rv = -1; |
| 541 | api_main_t *am = &api_main; |
| 542 | vl_api_header_t *mp = vl_msg_api_alloc(l); |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 543 | svm_queue_t *q; |
Damjan Marion | 5fec1e8 | 2017-04-13 19:13:47 +0200 | [diff] [blame] | 544 | vac_main_t *pm = &vac_main; |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 545 | |
| 546 | if (!pm->connected_to_vlib) return -1; |
| 547 | if (!mp) return (-1); |
Ole Troan | dfc9b7c | 2017-03-06 23:51:57 +0100 | [diff] [blame] | 548 | |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 549 | memcpy(mp, p, l); |
Damjan Marion | 5fec1e8 | 2017-04-13 19:13:47 +0200 | [diff] [blame] | 550 | mp->client_index = vac_client_index(); |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 551 | q = am->shmem_hdr->vl_input_queue; |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 552 | rv = svm_queue_add(q, (u8 *)&mp, 0); |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 553 | if (rv != 0) { |
Ole Troan | 4e588aa | 2018-09-07 11:01:47 +0200 | [diff] [blame] | 554 | fprintf(stderr, "vpe_api_write fails: %d\n", rv); |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 555 | /* Clear message */ |
Damjan Marion | 5fec1e8 | 2017-04-13 19:13:47 +0200 | [diff] [blame] | 556 | vac_free(mp); |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 557 | } |
| 558 | return (rv); |
| 559 | } |
| 560 | |
Ole Troan | 3cc4971 | 2017-03-08 12:02:24 +0100 | [diff] [blame] | 561 | int |
Damjan Marion | 5fec1e8 | 2017-04-13 19:13:47 +0200 | [diff] [blame] | 562 | vac_get_msg_index (unsigned char * name) |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 563 | { |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 564 | return vl_msg_api_get_msg_index (name); |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 565 | } |
Ole Troan | 3cc4971 | 2017-03-08 12:02:24 +0100 | [diff] [blame] | 566 | |
| 567 | int |
Damjan Marion | 5fec1e8 | 2017-04-13 19:13:47 +0200 | [diff] [blame] | 568 | vac_msg_table_max_index(void) |
Ole Troan | 3cc4971 | 2017-03-08 12:02:24 +0100 | [diff] [blame] | 569 | { |
| 570 | int max = 0; |
| 571 | hash_pair_t *hp; |
Damjan Marion | 5fec1e8 | 2017-04-13 19:13:47 +0200 | [diff] [blame] | 572 | uword *h = vac_msg_table_get_hash(); |
Ole Troan | 3cc4971 | 2017-03-08 12:02:24 +0100 | [diff] [blame] | 573 | hash_foreach_pair (hp, h, |
| 574 | ({ |
| 575 | if (hp->value[0] > max) |
| 576 | max = hp->value[0]; |
| 577 | })); |
| 578 | |
| 579 | return max; |
| 580 | } |
| 581 | |
| 582 | void |
Damjan Marion | 5fec1e8 | 2017-04-13 19:13:47 +0200 | [diff] [blame] | 583 | vac_set_error_handler (vac_error_callback_t cb) |
Ole Troan | 3cc4971 | 2017-03-08 12:02:24 +0100 | [diff] [blame] | 584 | { |
| 585 | if (cb) clib_error_register_handler (cb, 0); |
| 586 | } |