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