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