Klement Sekera | 8f2a4ea | 2017-05-04 06:15:18 +0200 | [diff] [blame] | 1 | /* |
| 2 | *------------------------------------------------------------------ |
| 3 | * Copyright (c) 2017 Cisco and/or its affiliates. |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at: |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | *------------------------------------------------------------------ |
| 16 | */ |
| 17 | |
| 18 | #ifndef vpp_api_h_included |
| 19 | #define vpp_api_h_included |
| 20 | |
| 21 | #include <string.h> |
| 22 | #include <stdbool.h> |
| 23 | #include <vppinfra/types.h> |
Ole Troan | 003d5da | 2018-12-18 12:23:13 +0100 | [diff] [blame] | 24 | #include <vlibapi/api_types.h> |
Klement Sekera | dc15be2 | 2017-06-12 06:49:33 +0200 | [diff] [blame] | 25 | #include <vapi/vapi_common.h> |
Mohsin Kazmi | 3fca567 | 2018-01-04 18:57:26 +0100 | [diff] [blame] | 26 | #include <svm/queue.h> |
Klement Sekera | dc15be2 | 2017-06-12 06:49:33 +0200 | [diff] [blame] | 27 | |
| 28 | #ifdef __cplusplus |
| 29 | extern "C" |
| 30 | { |
| 31 | #endif |
Klement Sekera | 8f2a4ea | 2017-05-04 06:15:18 +0200 | [diff] [blame] | 32 | |
| 33 | /** |
| 34 | * @file vapi.h |
| 35 | * |
| 36 | * common vpp api C declarations |
| 37 | * |
| 38 | * This file declares the common C API functions. These include connect, |
| 39 | * disconnect and utility functions as well as the low-level vapi_send and |
| 40 | * vapi_recv API. This is only the transport layer. |
| 41 | * |
| 42 | * Message formats and higher-level APIs are generated by running the |
| 43 | * vapi_c_gen.py script (which is run for in-tree APIs as part of the build |
| 44 | * process). It's not recommended to mix the higher and lower level APIs. Due |
| 45 | * to version issues, the higher-level APIs are not part of the shared library. |
| 46 | */ |
Ole Troan | 2ca88ff | 2022-01-27 16:25:43 +0100 | [diff] [blame] | 47 | typedef struct vapi_ctx_s *vapi_ctx_t; |
Klement Sekera | 8f2a4ea | 2017-05-04 06:15:18 +0200 | [diff] [blame] | 48 | |
| 49 | /** |
| 50 | * @brief allocate vapi message of given size |
| 51 | * |
| 52 | * @note message must be freed by vapi_msg_free if not consumed by vapi_send |
| 53 | * call |
| 54 | * |
| 55 | * @param ctx opaque vapi context |
| 56 | * |
| 57 | * @return pointer to message or NULL if out of memory |
| 58 | */ |
Ole Troan | 2ca88ff | 2022-01-27 16:25:43 +0100 | [diff] [blame] | 59 | void *vapi_msg_alloc (vapi_ctx_t ctx, size_t size); |
Klement Sekera | 8f2a4ea | 2017-05-04 06:15:18 +0200 | [diff] [blame] | 60 | |
| 61 | /** |
| 62 | * @brief free a vapi message |
| 63 | * |
| 64 | * @note messages received by vapi_recv must be freed when no longer needed |
| 65 | * |
| 66 | * @param ctx opaque vapi context |
| 67 | * @param msg message to be freed |
| 68 | */ |
Ole Troan | 2ca88ff | 2022-01-27 16:25:43 +0100 | [diff] [blame] | 69 | void vapi_msg_free (vapi_ctx_t ctx, void *msg); |
Klement Sekera | 8f2a4ea | 2017-05-04 06:15:18 +0200 | [diff] [blame] | 70 | |
| 71 | /** |
| 72 | * @brief allocate vapi context |
| 73 | * |
| 74 | * @param[out] pointer to result variable |
| 75 | * |
| 76 | * @return VAPI_OK on success, other error code on error |
| 77 | */ |
Ole Troan | 2ca88ff | 2022-01-27 16:25:43 +0100 | [diff] [blame] | 78 | vapi_error_e vapi_ctx_alloc (vapi_ctx_t *result); |
Klement Sekera | 8f2a4ea | 2017-05-04 06:15:18 +0200 | [diff] [blame] | 79 | |
| 80 | /** |
| 81 | * @brief free vapi context |
| 82 | */ |
Ole Troan | 2ca88ff | 2022-01-27 16:25:43 +0100 | [diff] [blame] | 83 | void vapi_ctx_free (vapi_ctx_t ctx); |
Klement Sekera | 8f2a4ea | 2017-05-04 06:15:18 +0200 | [diff] [blame] | 84 | |
| 85 | /** |
| 86 | * @brief check if message identified by it's message id is known by the vpp to |
| 87 | * which the connection is open |
| 88 | */ |
Ole Troan | 2ca88ff | 2022-01-27 16:25:43 +0100 | [diff] [blame] | 89 | bool vapi_is_msg_available (vapi_ctx_t ctx, vapi_msg_id_t type); |
Klement Sekera | 8f2a4ea | 2017-05-04 06:15:18 +0200 | [diff] [blame] | 90 | |
| 91 | /** |
| 92 | * @brief connect to vpp |
| 93 | * |
| 94 | * @param ctx opaque vapi context, must be allocated using vapi_ctx_alloc first |
| 95 | * @param name application name |
| 96 | * @param chroot_prefix shared memory prefix |
| 97 | * @param max_outstanding_requests max number of outstanding requests queued |
| 98 | * @param response_queue_size size of the response queue |
| 99 | * @param mode mode of operation - blocking or nonblocking |
Klement Sekera | dab732a | 2018-07-04 13:43:46 +0200 | [diff] [blame] | 100 | * @param handle_keepalives - if true, automatically handle memclnt_keepalive |
Klement Sekera | 8f2a4ea | 2017-05-04 06:15:18 +0200 | [diff] [blame] | 101 | * |
| 102 | * @return VAPI_OK on success, other error code on error |
| 103 | */ |
Ole Troan | 2ca88ff | 2022-01-27 16:25:43 +0100 | [diff] [blame] | 104 | vapi_error_e vapi_connect (vapi_ctx_t ctx, const char *name, |
| 105 | const char *chroot_prefix, |
| 106 | int max_outstanding_requests, |
| 107 | int response_queue_size, vapi_mode_e mode, |
| 108 | bool handle_keepalives); |
| 109 | |
| 110 | /** |
Stanislav Zaikin | dc4d21e | 2024-03-06 19:48:30 +0100 | [diff] [blame^] | 111 | * @brief connect to vpp |
| 112 | * |
| 113 | * @param ctx opaque vapi context, must be allocated using vapi_ctx_alloc first |
| 114 | * @param name application name |
| 115 | * @param path shared memory prefix or path to unix socket |
| 116 | * @param max_outstanding_requests max number of outstanding requests queued |
| 117 | * @param response_queue_size size of the response queue |
| 118 | * @param mode mode of operation - blocking or nonblocking |
| 119 | * @param handle_keepalives - if true, automatically handle memclnt_keepalive |
| 120 | * @param use_uds - if true, use unix domain socket transport |
| 121 | * |
| 122 | * @return VAPI_OK on success, other error code on error |
| 123 | */ |
| 124 | vapi_error_e vapi_connect_ex (vapi_ctx_t ctx, const char *name, |
| 125 | const char *path, int max_outstanding_requests, |
| 126 | int response_queue_size, vapi_mode_e mode, |
| 127 | bool handle_keepalives, bool use_uds); |
| 128 | |
| 129 | /** |
Ole Troan | 2ca88ff | 2022-01-27 16:25:43 +0100 | [diff] [blame] | 130 | * @brief connect to vpp from a client in same process |
| 131 | * @remark This MUST be called from a separate thread. If called |
| 132 | * from the main thread, it will deadlock. |
| 133 | * |
| 134 | * @param ctx opaque vapi context, must be allocated using vapi_ctx_alloc first |
| 135 | * @param name application name |
| 136 | * @param max_outstanding_requests max number of outstanding requests queued |
| 137 | * @param response_queue_size size of the response queue |
| 138 | * @param mode mode of operation - blocking or nonblocking |
| 139 | * @param handle_keepalives - if true, automatically handle memclnt_keepalive |
| 140 | * |
| 141 | * @return VAPI_OK on success, other error code on error |
| 142 | */ |
| 143 | vapi_error_e vapi_connect_from_vpp (vapi_ctx_t ctx, const char *name, |
| 144 | int max_outstanding_requests, |
| 145 | int response_queue_size, vapi_mode_e mode, |
| 146 | bool handle_keepalives); |
Klement Sekera | 8f2a4ea | 2017-05-04 06:15:18 +0200 | [diff] [blame] | 147 | |
| 148 | /** |
| 149 | * @brief disconnect from vpp |
| 150 | * |
| 151 | * @param ctx opaque vapi context |
| 152 | * |
| 153 | * @return VAPI_OK on success, other error code on error |
| 154 | */ |
Ole Troan | 2ca88ff | 2022-01-27 16:25:43 +0100 | [diff] [blame] | 155 | vapi_error_e vapi_disconnect (vapi_ctx_t ctx); |
| 156 | vapi_error_e vapi_disconnect_from_vpp (vapi_ctx_t ctx); |
Klement Sekera | 8f2a4ea | 2017-05-04 06:15:18 +0200 | [diff] [blame] | 157 | |
| 158 | /** |
| 159 | * @brief get event file descriptor |
| 160 | * |
| 161 | * @note this file descriptor becomes readable when messages (from vpp) |
| 162 | * are waiting in queue |
| 163 | * |
| 164 | * @param ctx opaque vapi context |
| 165 | * @param[out] fd pointer to result variable |
| 166 | * |
| 167 | * @return VAPI_OK on success, other error code on error |
| 168 | */ |
Ole Troan | 2ca88ff | 2022-01-27 16:25:43 +0100 | [diff] [blame] | 169 | vapi_error_e vapi_get_fd (vapi_ctx_t ctx, int *fd); |
Klement Sekera | 8f2a4ea | 2017-05-04 06:15:18 +0200 | [diff] [blame] | 170 | |
| 171 | /** |
| 172 | * @brief low-level api for sending messages to vpp |
| 173 | * |
| 174 | * @note it is not recommended to use this api directly, use generated api |
| 175 | * instead |
| 176 | * |
| 177 | * @param ctx opaque vapi context |
| 178 | * @param msg message to send |
| 179 | * |
| 180 | * @return VAPI_OK on success, other error code on error |
| 181 | */ |
Ole Troan | 2ca88ff | 2022-01-27 16:25:43 +0100 | [diff] [blame] | 182 | vapi_error_e vapi_send (vapi_ctx_t ctx, void *msg); |
Klement Sekera | 8f2a4ea | 2017-05-04 06:15:18 +0200 | [diff] [blame] | 183 | |
| 184 | /** |
| 185 | * @brief low-level api for atomically sending two messages to vpp - either |
| 186 | * both messages are sent or neither one is |
| 187 | * |
| 188 | * @note it is not recommended to use this api directly, use generated api |
| 189 | * instead |
| 190 | * |
| 191 | * @param ctx opaque vapi context |
| 192 | * @param msg1 first message to send |
| 193 | * @param msg2 second message to send |
| 194 | * |
| 195 | * @return VAPI_OK on success, other error code on error |
| 196 | */ |
Matthew Smith | 4b9935c | 2022-12-02 20:46:16 +0000 | [diff] [blame] | 197 | vapi_error_e vapi_send2 (vapi_ctx_t ctx, void *msg1, void *msg2); |
Klement Sekera | 8f2a4ea | 2017-05-04 06:15:18 +0200 | [diff] [blame] | 198 | |
| 199 | /** |
| 200 | * @brief low-level api for reading messages from vpp |
| 201 | * |
| 202 | * @note it is not recommended to use this api directly, use generated api |
| 203 | * instead |
| 204 | * |
| 205 | * @param ctx opaque vapi context |
| 206 | * @param[out] msg pointer to result variable containing message |
| 207 | * @param[out] msg_size pointer to result variable containing message size |
Mohsin Kazmi | 3fca567 | 2018-01-04 18:57:26 +0100 | [diff] [blame] | 208 | * @param cond enum type for blocking, non-blocking or timed wait call |
| 209 | * @param time in sec for timed wait |
Klement Sekera | 8f2a4ea | 2017-05-04 06:15:18 +0200 | [diff] [blame] | 210 | * |
| 211 | * @return VAPI_OK on success, other error code on error |
| 212 | */ |
Matthew Smith | 4b9935c | 2022-12-02 20:46:16 +0000 | [diff] [blame] | 213 | vapi_error_e vapi_recv (vapi_ctx_t ctx, void **msg, size_t *msg_size, |
| 214 | svm_q_conditional_wait_t cond, u32 time); |
Klement Sekera | 8f2a4ea | 2017-05-04 06:15:18 +0200 | [diff] [blame] | 215 | |
| 216 | /** |
Matthew Smith | 4b9935c | 2022-12-02 20:46:16 +0000 | [diff] [blame] | 217 | * @brief wait for connection to become readable |
Klement Sekera | 8f2a4ea | 2017-05-04 06:15:18 +0200 | [diff] [blame] | 218 | * |
| 219 | * @param ctx opaque vapi context |
Klement Sekera | 8f2a4ea | 2017-05-04 06:15:18 +0200 | [diff] [blame] | 220 | * |
| 221 | * @return VAPI_OK on success, other error code on error |
| 222 | */ |
Matthew Smith | 4b9935c | 2022-12-02 20:46:16 +0000 | [diff] [blame] | 223 | vapi_error_e vapi_wait (vapi_ctx_t ctx); |
Klement Sekera | 8f2a4ea | 2017-05-04 06:15:18 +0200 | [diff] [blame] | 224 | |
| 225 | /** |
| 226 | * @brief pick next message sent by vpp and call the appropriate callback |
| 227 | * |
| 228 | * @return VAPI_OK on success, other error code on error |
| 229 | */ |
Matthew Smith | 57f177d | 2022-12-15 22:18:08 +0000 | [diff] [blame] | 230 | vapi_error_e vapi_dispatch_one (vapi_ctx_t ctx); |
Klement Sekera | 8f2a4ea | 2017-05-04 06:15:18 +0200 | [diff] [blame] | 231 | |
| 232 | /** |
| 233 | * @brief loop vapi_dispatch_one until responses to all currently outstanding |
| 234 | * requests have been received and their callbacks called |
| 235 | * |
| 236 | * @note the dispatch loop is interrupted if any error is encountered or |
| 237 | * returned from the callback, in which case this error is returned as the |
| 238 | * result of vapi_dispatch. In this case it might be necessary to call dispatch |
| 239 | * again to process the remaining messages. Returning VAPI_EUSER from |
| 240 | * a callback allows the user to break the dispatch loop (and distinguish |
| 241 | * this case in the calling code from other failures). VAPI never returns |
| 242 | * VAPI_EUSER on its own. |
| 243 | * |
| 244 | * @return VAPI_OK on success, other error code on error |
| 245 | */ |
Matthew Smith | 57f177d | 2022-12-15 22:18:08 +0000 | [diff] [blame] | 246 | vapi_error_e vapi_dispatch (vapi_ctx_t ctx); |
Klement Sekera | 8f2a4ea | 2017-05-04 06:15:18 +0200 | [diff] [blame] | 247 | |
| 248 | /** generic vapi event callback */ |
Matthew Smith | 57f177d | 2022-12-15 22:18:08 +0000 | [diff] [blame] | 249 | typedef vapi_error_e (*vapi_event_cb) (vapi_ctx_t ctx, void *callback_ctx, |
| 250 | void *payload); |
Klement Sekera | 8f2a4ea | 2017-05-04 06:15:18 +0200 | [diff] [blame] | 251 | |
| 252 | /** |
| 253 | * @brief set event callback to call when message with given id is dispatched |
| 254 | * |
| 255 | * @param ctx opaque vapi context |
| 256 | * @param id message id |
| 257 | * @param callback callback |
| 258 | * @param callback_ctx context pointer stored and passed to callback |
| 259 | */ |
Matthew Smith | 57f177d | 2022-12-15 22:18:08 +0000 | [diff] [blame] | 260 | void vapi_set_event_cb (vapi_ctx_t ctx, vapi_msg_id_t id, |
| 261 | vapi_event_cb callback, void *callback_ctx); |
Klement Sekera | 8f2a4ea | 2017-05-04 06:15:18 +0200 | [diff] [blame] | 262 | |
| 263 | /** |
| 264 | * @brief clear event callback for given message id |
| 265 | * |
| 266 | * @param ctx opaque vapi context |
| 267 | * @param id message id |
| 268 | */ |
Matthew Smith | 57f177d | 2022-12-15 22:18:08 +0000 | [diff] [blame] | 269 | void vapi_clear_event_cb (vapi_ctx_t ctx, vapi_msg_id_t id); |
Klement Sekera | 8f2a4ea | 2017-05-04 06:15:18 +0200 | [diff] [blame] | 270 | |
| 271 | /** generic vapi event callback */ |
Matthew Smith | 57f177d | 2022-12-15 22:18:08 +0000 | [diff] [blame] | 272 | typedef vapi_error_e (*vapi_generic_event_cb) (vapi_ctx_t ctx, |
| 273 | void *callback_ctx, |
| 274 | vapi_msg_id_t id, void *msg); |
Klement Sekera | 8f2a4ea | 2017-05-04 06:15:18 +0200 | [diff] [blame] | 275 | /** |
| 276 | * @brief set generic event callback |
| 277 | * |
| 278 | * @note this callback is called by dispatch if no message-type specific |
| 279 | * callback is set (so it's a fallback callback) |
| 280 | * |
| 281 | * @param ctx opaque vapi context |
| 282 | * @param callback callback |
| 283 | * @param callback_ctx context pointer stored and passed to callback |
| 284 | */ |
Matthew Smith | 57f177d | 2022-12-15 22:18:08 +0000 | [diff] [blame] | 285 | void vapi_set_generic_event_cb (vapi_ctx_t ctx, vapi_generic_event_cb callback, |
| 286 | void *callback_ctx); |
Klement Sekera | 8f2a4ea | 2017-05-04 06:15:18 +0200 | [diff] [blame] | 287 | |
| 288 | /** |
| 289 | * @brief clear generic event callback |
| 290 | * |
| 291 | * @param ctx opaque vapi context |
| 292 | */ |
Matthew Smith | 57f177d | 2022-12-15 22:18:08 +0000 | [diff] [blame] | 293 | void vapi_clear_generic_event_cb (vapi_ctx_t ctx); |
| 294 | |
| 295 | /** |
| 296 | * @brief signal RX thread to exit |
| 297 | * |
| 298 | * @note This adds a message to the client input queue that indicates that |
| 299 | * an RX thread should stop processing incoming messages and exit. If an |
| 300 | * application has an RX thread which sleeps while waiting for incoming |
| 301 | * messages using vapi_wait(), this call will allow the application to |
| 302 | * wake up from the vapi_wait() call and figure out that it should stop |
| 303 | * running. |
| 304 | * |
| 305 | * @param ctx opaque vapi context |
| 306 | */ |
| 307 | void vapi_stop_rx_thread (vapi_ctx_t ctx); |
Klement Sekera | dc15be2 | 2017-06-12 06:49:33 +0200 | [diff] [blame] | 308 | |
| 309 | #ifdef __cplusplus |
| 310 | } |
| 311 | #endif |
Klement Sekera | 8f2a4ea | 2017-05-04 06:15:18 +0200 | [diff] [blame] | 312 | |
| 313 | #endif |
| 314 | |
| 315 | /* |
| 316 | * fd.io coding-style-patch-verification: ON |
| 317 | * |
| 318 | * Local Variables: |
| 319 | * eval: (c-set-style "gnu") |
| 320 | * End: |
| 321 | */ |