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