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