blob: c131920a88f26d6c7ae1cb91c1b1519d08b90d2e [file] [log] [blame]
Klement Sekera8f2a4ea2017-05-04 06:15:18 +02001/*
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 Troan003d5da2018-12-18 12:23:13 +010024#include <vlibapi/api_types.h>
Klement Sekeradc15be22017-06-12 06:49:33 +020025#include <vapi/vapi_common.h>
Mohsin Kazmi3fca5672018-01-04 18:57:26 +010026#include <svm/queue.h>
Klement Sekeradc15be22017-06-12 06:49:33 +020027
28#ifdef __cplusplus
29extern "C"
30{
31#endif
Klement Sekera8f2a4ea2017-05-04 06:15:18 +020032
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 Troan2ca88ff2022-01-27 16:25:43 +010047typedef struct vapi_ctx_s *vapi_ctx_t;
Klement Sekera8f2a4ea2017-05-04 06:15:18 +020048
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 Troan2ca88ff2022-01-27 16:25:43 +010059void *vapi_msg_alloc (vapi_ctx_t ctx, size_t size);
Klement Sekera8f2a4ea2017-05-04 06:15:18 +020060
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 Troan2ca88ff2022-01-27 16:25:43 +010069void vapi_msg_free (vapi_ctx_t ctx, void *msg);
Klement Sekera8f2a4ea2017-05-04 06:15:18 +020070
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 Troan2ca88ff2022-01-27 16:25:43 +010078vapi_error_e vapi_ctx_alloc (vapi_ctx_t *result);
Klement Sekera8f2a4ea2017-05-04 06:15:18 +020079
80/**
81 * @brief free vapi context
82 */
Ole Troan2ca88ff2022-01-27 16:25:43 +010083void vapi_ctx_free (vapi_ctx_t ctx);
Klement Sekera8f2a4ea2017-05-04 06:15:18 +020084
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 Troan2ca88ff2022-01-27 16:25:43 +010089bool vapi_is_msg_available (vapi_ctx_t ctx, vapi_msg_id_t type);
Klement Sekera8f2a4ea2017-05-04 06:15:18 +020090
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 Sekeradab732a2018-07-04 13:43:46 +0200100 * @param handle_keepalives - if true, automatically handle memclnt_keepalive
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200101 *
102 * @return VAPI_OK on success, other error code on error
103 */
Ole Troan2ca88ff2022-01-27 16:25:43 +0100104vapi_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/**
111 * @brief connect to vpp from a client in same process
112 * @remark This MUST be called from a separate thread. If called
113 * from the main thread, it will deadlock.
114 *
115 * @param ctx opaque vapi context, must be allocated using vapi_ctx_alloc first
116 * @param name application name
117 * @param max_outstanding_requests max number of outstanding requests queued
118 * @param response_queue_size size of the response queue
119 * @param mode mode of operation - blocking or nonblocking
120 * @param handle_keepalives - if true, automatically handle memclnt_keepalive
121 *
122 * @return VAPI_OK on success, other error code on error
123 */
124vapi_error_e vapi_connect_from_vpp (vapi_ctx_t ctx, const char *name,
125 int max_outstanding_requests,
126 int response_queue_size, vapi_mode_e mode,
127 bool handle_keepalives);
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200128
129/**
130 * @brief disconnect from vpp
131 *
132 * @param ctx opaque vapi context
133 *
134 * @return VAPI_OK on success, other error code on error
135 */
Ole Troan2ca88ff2022-01-27 16:25:43 +0100136vapi_error_e vapi_disconnect (vapi_ctx_t ctx);
137vapi_error_e vapi_disconnect_from_vpp (vapi_ctx_t ctx);
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200138
139/**
140 * @brief get event file descriptor
141 *
142 * @note this file descriptor becomes readable when messages (from vpp)
143 * are waiting in queue
144 *
145 * @param ctx opaque vapi context
146 * @param[out] fd pointer to result variable
147 *
148 * @return VAPI_OK on success, other error code on error
149 */
Ole Troan2ca88ff2022-01-27 16:25:43 +0100150vapi_error_e vapi_get_fd (vapi_ctx_t ctx, int *fd);
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200151
152/**
153 * @brief low-level api for sending messages to vpp
154 *
155 * @note it is not recommended to use this api directly, use generated api
156 * instead
157 *
158 * @param ctx opaque vapi context
159 * @param msg message to send
160 *
161 * @return VAPI_OK on success, other error code on error
162 */
Ole Troan2ca88ff2022-01-27 16:25:43 +0100163vapi_error_e vapi_send (vapi_ctx_t ctx, void *msg);
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200164
165/**
166 * @brief low-level api for atomically sending two messages to vpp - either
167 * both messages are sent or neither one is
168 *
169 * @note it is not recommended to use this api directly, use generated api
170 * instead
171 *
172 * @param ctx opaque vapi context
173 * @param msg1 first message to send
174 * @param msg2 second message to send
175 *
176 * @return VAPI_OK on success, other error code on error
177 */
Matthew Smith4b9935c2022-12-02 20:46:16 +0000178vapi_error_e vapi_send2 (vapi_ctx_t ctx, void *msg1, void *msg2);
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200179
180/**
181 * @brief low-level api for reading messages from vpp
182 *
183 * @note it is not recommended to use this api directly, use generated api
184 * instead
185 *
186 * @param ctx opaque vapi context
187 * @param[out] msg pointer to result variable containing message
188 * @param[out] msg_size pointer to result variable containing message size
Mohsin Kazmi3fca5672018-01-04 18:57:26 +0100189 * @param cond enum type for blocking, non-blocking or timed wait call
190 * @param time in sec for timed wait
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200191 *
192 * @return VAPI_OK on success, other error code on error
193 */
Matthew Smith4b9935c2022-12-02 20:46:16 +0000194vapi_error_e vapi_recv (vapi_ctx_t ctx, void **msg, size_t *msg_size,
195 svm_q_conditional_wait_t cond, u32 time);
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200196
197/**
Matthew Smith4b9935c2022-12-02 20:46:16 +0000198 * @brief wait for connection to become readable
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200199 *
200 * @param ctx opaque vapi context
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200201 *
202 * @return VAPI_OK on success, other error code on error
203 */
Matthew Smith4b9935c2022-12-02 20:46:16 +0000204vapi_error_e vapi_wait (vapi_ctx_t ctx);
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200205
206/**
207 * @brief pick next message sent by vpp and call the appropriate callback
208 *
209 * @return VAPI_OK on success, other error code on error
210 */
Matthew Smith57f177d2022-12-15 22:18:08 +0000211vapi_error_e vapi_dispatch_one (vapi_ctx_t ctx);
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200212
213/**
214 * @brief loop vapi_dispatch_one until responses to all currently outstanding
215 * requests have been received and their callbacks called
216 *
217 * @note the dispatch loop is interrupted if any error is encountered or
218 * returned from the callback, in which case this error is returned as the
219 * result of vapi_dispatch. In this case it might be necessary to call dispatch
220 * again to process the remaining messages. Returning VAPI_EUSER from
221 * a callback allows the user to break the dispatch loop (and distinguish
222 * this case in the calling code from other failures). VAPI never returns
223 * VAPI_EUSER on its own.
224 *
225 * @return VAPI_OK on success, other error code on error
226 */
Matthew Smith57f177d2022-12-15 22:18:08 +0000227vapi_error_e vapi_dispatch (vapi_ctx_t ctx);
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200228
229/** generic vapi event callback */
Matthew Smith57f177d2022-12-15 22:18:08 +0000230typedef vapi_error_e (*vapi_event_cb) (vapi_ctx_t ctx, void *callback_ctx,
231 void *payload);
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200232
233/**
234 * @brief set event callback to call when message with given id is dispatched
235 *
236 * @param ctx opaque vapi context
237 * @param id message id
238 * @param callback callback
239 * @param callback_ctx context pointer stored and passed to callback
240 */
Matthew Smith57f177d2022-12-15 22:18:08 +0000241void vapi_set_event_cb (vapi_ctx_t ctx, vapi_msg_id_t id,
242 vapi_event_cb callback, void *callback_ctx);
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200243
244/**
245 * @brief clear event callback for given message id
246 *
247 * @param ctx opaque vapi context
248 * @param id message id
249 */
Matthew Smith57f177d2022-12-15 22:18:08 +0000250void vapi_clear_event_cb (vapi_ctx_t ctx, vapi_msg_id_t id);
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200251
252/** generic vapi event callback */
Matthew Smith57f177d2022-12-15 22:18:08 +0000253typedef vapi_error_e (*vapi_generic_event_cb) (vapi_ctx_t ctx,
254 void *callback_ctx,
255 vapi_msg_id_t id, void *msg);
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200256/**
257 * @brief set generic event callback
258 *
259 * @note this callback is called by dispatch if no message-type specific
260 * callback is set (so it's a fallback callback)
261 *
262 * @param ctx opaque vapi context
263 * @param callback callback
264 * @param callback_ctx context pointer stored and passed to callback
265 */
Matthew Smith57f177d2022-12-15 22:18:08 +0000266void vapi_set_generic_event_cb (vapi_ctx_t ctx, vapi_generic_event_cb callback,
267 void *callback_ctx);
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200268
269/**
270 * @brief clear generic event callback
271 *
272 * @param ctx opaque vapi context
273 */
Matthew Smith57f177d2022-12-15 22:18:08 +0000274void vapi_clear_generic_event_cb (vapi_ctx_t ctx);
275
276/**
277 * @brief signal RX thread to exit
278 *
279 * @note This adds a message to the client input queue that indicates that
280 * an RX thread should stop processing incoming messages and exit. If an
281 * application has an RX thread which sleeps while waiting for incoming
282 * messages using vapi_wait(), this call will allow the application to
283 * wake up from the vapi_wait() call and figure out that it should stop
284 * running.
285 *
286 * @param ctx opaque vapi context
287 */
288void vapi_stop_rx_thread (vapi_ctx_t ctx);
Klement Sekeradc15be22017-06-12 06:49:33 +0200289
290#ifdef __cplusplus
291}
292#endif
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200293
294#endif
295
296/*
297 * fd.io coding-style-patch-verification: ON
298 *
299 * Local Variables:
300 * eval: (c-set-style "gnu")
301 * End:
302 */