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> |
Klement Sekera | dc15be2 | 2017-06-12 06:49:33 +0200 | [diff] [blame] | 24 | #include <vapi/vapi_common.h> |
Mohsin Kazmi | 3fca567 | 2018-01-04 18:57:26 +0100 | [diff] [blame] | 25 | #include <svm/queue.h> |
Klement Sekera | dc15be2 | 2017-06-12 06:49:33 +0200 | [diff] [blame] | 26 | |
| 27 | #ifdef __cplusplus |
| 28 | extern "C" |
| 29 | { |
| 30 | #endif |
Klement Sekera | 8f2a4ea | 2017-05-04 06:15:18 +0200 | [diff] [blame] | 31 | |
| 32 | /** |
| 33 | * @file vapi.h |
| 34 | * |
| 35 | * common vpp api C declarations |
| 36 | * |
| 37 | * This file declares the common C API functions. These include connect, |
| 38 | * disconnect and utility functions as well as the low-level vapi_send and |
| 39 | * vapi_recv API. This is only the transport layer. |
| 40 | * |
| 41 | * Message formats and higher-level APIs are generated by running the |
| 42 | * vapi_c_gen.py script (which is run for in-tree APIs as part of the build |
| 43 | * process). It's not recommended to mix the higher and lower level APIs. Due |
| 44 | * to version issues, the higher-level APIs are not part of the shared library. |
| 45 | */ |
Klement Sekera | dc15be2 | 2017-06-12 06:49:33 +0200 | [diff] [blame] | 46 | typedef struct vapi_ctx_s *vapi_ctx_t; |
Klement Sekera | 8f2a4ea | 2017-05-04 06:15:18 +0200 | [diff] [blame] | 47 | |
| 48 | /** |
| 49 | * @brief allocate vapi message of given size |
| 50 | * |
| 51 | * @note message must be freed by vapi_msg_free if not consumed by vapi_send |
| 52 | * call |
| 53 | * |
| 54 | * @param ctx opaque vapi context |
| 55 | * |
| 56 | * @return pointer to message or NULL if out of memory |
| 57 | */ |
Klement Sekera | dc15be2 | 2017-06-12 06:49:33 +0200 | [diff] [blame] | 58 | void *vapi_msg_alloc (vapi_ctx_t ctx, size_t size); |
Klement Sekera | 8f2a4ea | 2017-05-04 06:15:18 +0200 | [diff] [blame] | 59 | |
| 60 | /** |
| 61 | * @brief free a vapi message |
| 62 | * |
| 63 | * @note messages received by vapi_recv must be freed when no longer needed |
| 64 | * |
| 65 | * @param ctx opaque vapi context |
| 66 | * @param msg message to be freed |
| 67 | */ |
Klement Sekera | dc15be2 | 2017-06-12 06:49:33 +0200 | [diff] [blame] | 68 | void vapi_msg_free (vapi_ctx_t ctx, void *msg); |
Klement Sekera | 8f2a4ea | 2017-05-04 06:15:18 +0200 | [diff] [blame] | 69 | |
| 70 | /** |
| 71 | * @brief allocate vapi context |
| 72 | * |
| 73 | * @param[out] pointer to result variable |
| 74 | * |
| 75 | * @return VAPI_OK on success, other error code on error |
| 76 | */ |
Klement Sekera | dc15be2 | 2017-06-12 06:49:33 +0200 | [diff] [blame] | 77 | vapi_error_e vapi_ctx_alloc (vapi_ctx_t * result); |
Klement Sekera | 8f2a4ea | 2017-05-04 06:15:18 +0200 | [diff] [blame] | 78 | |
| 79 | /** |
| 80 | * @brief free vapi context |
| 81 | */ |
Klement Sekera | dc15be2 | 2017-06-12 06:49:33 +0200 | [diff] [blame] | 82 | void vapi_ctx_free (vapi_ctx_t ctx); |
Klement Sekera | 8f2a4ea | 2017-05-04 06:15:18 +0200 | [diff] [blame] | 83 | |
| 84 | /** |
| 85 | * @brief check if message identified by it's message id is known by the vpp to |
| 86 | * which the connection is open |
| 87 | */ |
Klement Sekera | dc15be2 | 2017-06-12 06:49:33 +0200 | [diff] [blame] | 88 | 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] | 89 | |
| 90 | /** |
| 91 | * @brief connect to vpp |
| 92 | * |
| 93 | * @param ctx opaque vapi context, must be allocated using vapi_ctx_alloc first |
| 94 | * @param name application name |
| 95 | * @param chroot_prefix shared memory prefix |
| 96 | * @param max_outstanding_requests max number of outstanding requests queued |
| 97 | * @param response_queue_size size of the response queue |
| 98 | * @param mode mode of operation - blocking or nonblocking |
| 99 | * |
| 100 | * @return VAPI_OK on success, other error code on error |
| 101 | */ |
Klement Sekera | dc15be2 | 2017-06-12 06:49:33 +0200 | [diff] [blame] | 102 | vapi_error_e vapi_connect (vapi_ctx_t ctx, const char *name, |
| 103 | const char *chroot_prefix, |
| 104 | int max_outstanding_requests, |
| 105 | int response_queue_size, vapi_mode_e mode); |
Klement Sekera | 8f2a4ea | 2017-05-04 06:15:18 +0200 | [diff] [blame] | 106 | |
| 107 | /** |
| 108 | * @brief disconnect from vpp |
| 109 | * |
| 110 | * @param ctx opaque vapi context |
| 111 | * |
| 112 | * @return VAPI_OK on success, other error code on error |
| 113 | */ |
Klement Sekera | dc15be2 | 2017-06-12 06:49:33 +0200 | [diff] [blame] | 114 | vapi_error_e vapi_disconnect (vapi_ctx_t ctx); |
Klement Sekera | 8f2a4ea | 2017-05-04 06:15:18 +0200 | [diff] [blame] | 115 | |
| 116 | /** |
| 117 | * @brief get event file descriptor |
| 118 | * |
| 119 | * @note this file descriptor becomes readable when messages (from vpp) |
| 120 | * are waiting in queue |
| 121 | * |
| 122 | * @param ctx opaque vapi context |
| 123 | * @param[out] fd pointer to result variable |
| 124 | * |
| 125 | * @return VAPI_OK on success, other error code on error |
| 126 | */ |
Klement Sekera | dc15be2 | 2017-06-12 06:49:33 +0200 | [diff] [blame] | 127 | vapi_error_e vapi_get_fd (vapi_ctx_t ctx, int *fd); |
Klement Sekera | 8f2a4ea | 2017-05-04 06:15:18 +0200 | [diff] [blame] | 128 | |
| 129 | /** |
| 130 | * @brief low-level api for sending messages to vpp |
| 131 | * |
| 132 | * @note it is not recommended to use this api directly, use generated api |
| 133 | * instead |
| 134 | * |
| 135 | * @param ctx opaque vapi context |
| 136 | * @param msg message to send |
| 137 | * |
| 138 | * @return VAPI_OK on success, other error code on error |
| 139 | */ |
Klement Sekera | dc15be2 | 2017-06-12 06:49:33 +0200 | [diff] [blame] | 140 | vapi_error_e vapi_send (vapi_ctx_t ctx, void *msg); |
Klement Sekera | 8f2a4ea | 2017-05-04 06:15:18 +0200 | [diff] [blame] | 141 | |
| 142 | /** |
| 143 | * @brief low-level api for atomically sending two messages to vpp - either |
| 144 | * both messages are sent or neither one is |
| 145 | * |
| 146 | * @note it is not recommended to use this api directly, use generated api |
| 147 | * instead |
| 148 | * |
| 149 | * @param ctx opaque vapi context |
| 150 | * @param msg1 first message to send |
| 151 | * @param msg2 second message to send |
| 152 | * |
| 153 | * @return VAPI_OK on success, other error code on error |
| 154 | */ |
Klement Sekera | dc15be2 | 2017-06-12 06:49:33 +0200 | [diff] [blame] | 155 | 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] | 156 | |
| 157 | /** |
| 158 | * @brief low-level api for reading messages from vpp |
| 159 | * |
| 160 | * @note it is not recommended to use this api directly, use generated api |
| 161 | * instead |
| 162 | * |
| 163 | * @param ctx opaque vapi context |
| 164 | * @param[out] msg pointer to result variable containing message |
| 165 | * @param[out] msg_size pointer to result variable containing message size |
Mohsin Kazmi | 3fca567 | 2018-01-04 18:57:26 +0100 | [diff] [blame] | 166 | * @param cond enum type for blocking, non-blocking or timed wait call |
| 167 | * @param time in sec for timed wait |
Klement Sekera | 8f2a4ea | 2017-05-04 06:15:18 +0200 | [diff] [blame] | 168 | * |
| 169 | * @return VAPI_OK on success, other error code on error |
| 170 | */ |
Mohsin Kazmi | 3fca567 | 2018-01-04 18:57:26 +0100 | [diff] [blame] | 171 | vapi_error_e vapi_recv (vapi_ctx_t ctx, void **msg, size_t * msg_size, |
| 172 | svm_q_conditional_wait_t cond, u32 time); |
Klement Sekera | 8f2a4ea | 2017-05-04 06:15:18 +0200 | [diff] [blame] | 173 | |
| 174 | /** |
| 175 | * @brief wait for connection to become readable or writable |
| 176 | * |
| 177 | * @param ctx opaque vapi context |
| 178 | * @param mode type of property to wait for - readability, writability or both |
| 179 | * |
| 180 | * @return VAPI_OK on success, other error code on error |
| 181 | */ |
Klement Sekera | dc15be2 | 2017-06-12 06:49:33 +0200 | [diff] [blame] | 182 | vapi_error_e vapi_wait (vapi_ctx_t ctx, vapi_wait_mode_e mode); |
Klement Sekera | 8f2a4ea | 2017-05-04 06:15:18 +0200 | [diff] [blame] | 183 | |
| 184 | /** |
| 185 | * @brief pick next message sent by vpp and call the appropriate callback |
| 186 | * |
| 187 | * @return VAPI_OK on success, other error code on error |
| 188 | */ |
Klement Sekera | dc15be2 | 2017-06-12 06:49:33 +0200 | [diff] [blame] | 189 | vapi_error_e vapi_dispatch_one (vapi_ctx_t ctx); |
Klement Sekera | 8f2a4ea | 2017-05-04 06:15:18 +0200 | [diff] [blame] | 190 | |
| 191 | /** |
| 192 | * @brief loop vapi_dispatch_one until responses to all currently outstanding |
| 193 | * requests have been received and their callbacks called |
| 194 | * |
| 195 | * @note the dispatch loop is interrupted if any error is encountered or |
| 196 | * returned from the callback, in which case this error is returned as the |
| 197 | * result of vapi_dispatch. In this case it might be necessary to call dispatch |
| 198 | * again to process the remaining messages. Returning VAPI_EUSER from |
| 199 | * a callback allows the user to break the dispatch loop (and distinguish |
| 200 | * this case in the calling code from other failures). VAPI never returns |
| 201 | * VAPI_EUSER on its own. |
| 202 | * |
| 203 | * @return VAPI_OK on success, other error code on error |
| 204 | */ |
Klement Sekera | dc15be2 | 2017-06-12 06:49:33 +0200 | [diff] [blame] | 205 | vapi_error_e vapi_dispatch (vapi_ctx_t ctx); |
Klement Sekera | 8f2a4ea | 2017-05-04 06:15:18 +0200 | [diff] [blame] | 206 | |
| 207 | /** generic vapi event callback */ |
Klement Sekera | dc15be2 | 2017-06-12 06:49:33 +0200 | [diff] [blame] | 208 | typedef vapi_error_e (*vapi_event_cb) (vapi_ctx_t ctx, void *callback_ctx, |
| 209 | void *payload); |
Klement Sekera | 8f2a4ea | 2017-05-04 06:15:18 +0200 | [diff] [blame] | 210 | |
| 211 | /** |
| 212 | * @brief set event callback to call when message with given id is dispatched |
| 213 | * |
| 214 | * @param ctx opaque vapi context |
| 215 | * @param id message id |
| 216 | * @param callback callback |
| 217 | * @param callback_ctx context pointer stored and passed to callback |
| 218 | */ |
Klement Sekera | dc15be2 | 2017-06-12 06:49:33 +0200 | [diff] [blame] | 219 | void vapi_set_event_cb (vapi_ctx_t ctx, vapi_msg_id_t id, |
| 220 | vapi_event_cb callback, void *callback_ctx); |
Klement Sekera | 8f2a4ea | 2017-05-04 06:15:18 +0200 | [diff] [blame] | 221 | |
| 222 | /** |
| 223 | * @brief clear event callback for given message id |
| 224 | * |
| 225 | * @param ctx opaque vapi context |
| 226 | * @param id message id |
| 227 | */ |
Klement Sekera | dc15be2 | 2017-06-12 06:49:33 +0200 | [diff] [blame] | 228 | 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] | 229 | |
| 230 | /** generic vapi event callback */ |
Klement Sekera | dc15be2 | 2017-06-12 06:49:33 +0200 | [diff] [blame] | 231 | typedef vapi_error_e (*vapi_generic_event_cb) (vapi_ctx_t ctx, |
| 232 | void *callback_ctx, |
| 233 | vapi_msg_id_t id, void *msg); |
Klement Sekera | 8f2a4ea | 2017-05-04 06:15:18 +0200 | [diff] [blame] | 234 | /** |
| 235 | * @brief set generic event callback |
| 236 | * |
| 237 | * @note this callback is called by dispatch if no message-type specific |
| 238 | * callback is set (so it's a fallback callback) |
| 239 | * |
| 240 | * @param ctx opaque vapi context |
| 241 | * @param callback callback |
| 242 | * @param callback_ctx context pointer stored and passed to callback |
| 243 | */ |
Klement Sekera | dc15be2 | 2017-06-12 06:49:33 +0200 | [diff] [blame] | 244 | void vapi_set_generic_event_cb (vapi_ctx_t ctx, |
| 245 | vapi_generic_event_cb callback, |
| 246 | void *callback_ctx); |
Klement Sekera | 8f2a4ea | 2017-05-04 06:15:18 +0200 | [diff] [blame] | 247 | |
| 248 | /** |
| 249 | * @brief clear generic event callback |
| 250 | * |
| 251 | * @param ctx opaque vapi context |
| 252 | */ |
Klement Sekera | dc15be2 | 2017-06-12 06:49:33 +0200 | [diff] [blame] | 253 | void vapi_clear_generic_event_cb (vapi_ctx_t ctx); |
| 254 | |
| 255 | #ifdef __cplusplus |
| 256 | } |
| 257 | #endif |
Klement Sekera | 8f2a4ea | 2017-05-04 06:15:18 +0200 | [diff] [blame] | 258 | |
| 259 | #endif |
| 260 | |
| 261 | /* |
| 262 | * fd.io coding-style-patch-verification: ON |
| 263 | * |
| 264 | * Local Variables: |
| 265 | * eval: (c-set-style "gnu") |
| 266 | * End: |
| 267 | */ |