1e1d567a0db5cfdfd5055b0d5672ebf8787d9f74
[vpp.git] / src / vpp-api / vapi / vapi.h
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>
24
25 /**
26  * @file vapi.h
27  *
28  * common vpp api C declarations
29  *
30  * This file declares the common C API functions. These include connect,
31  * disconnect and utility functions as well as the low-level vapi_send and
32  * vapi_recv API. This is only the transport layer.
33  *
34  * Message formats and higher-level APIs are generated by running the
35  * vapi_c_gen.py script (which is run for in-tree APIs as part of the build
36  * process). It's not recommended to mix the higher and lower level APIs. Due
37  * to version issues, the higher-level APIs are not part of the shared library.
38  */
39
40 typedef enum
41 {
42   VAPI_OK = 0,        /**< success */
43   VAPI_EINVAL,        /**< invalid value encountered */
44   VAPI_EAGAIN,        /**< operation would block */
45   VAPI_ENOTSUP,       /**< operation not supported */
46   VAPI_ENOMEM,        /**< out of memory */
47   VAPI_ENORESP,       /**< no response to request */
48   VAPI_EMAP_FAIL,     /**< failure while mapping api */
49   VAPI_ECON_FAIL,     /**< failure while connecting to vpp */
50   VAPI_EINCOMPATIBLE, /**< fundamental incompatibility while connecting to vpp
51                            (control ping/control ping reply mismatch) */
52   VAPI_MUTEX_FAILURE, /**< failure manipulating internal mutex(es) */
53   VAPI_EUSER,         /**< user error used for breaking dispatch,
54                            never used by VAPI */
55 } vapi_error_e;
56
57 typedef enum
58 {
59   VAPI_MODE_BLOCKING = 1,    /**< operations block until response received */
60   VAPI_MODE_NONBLOCKING = 2, /**< operations never block */
61 } vapi_mode_e;
62
63 typedef enum
64 {
65   VAPI_WAIT_FOR_READ,       /**< wait until a message can be read */
66   VAPI_WAIT_FOR_WRITE,      /**< wait until a message can be written */
67   VAPI_WAIT_FOR_READ_WRITE, /**< wait until a read or write can be done */
68 } vapi_wait_mode_e;
69
70 typedef int vapi_msg_id_t;
71 typedef struct vapi_ctx_s *vapi_ctx_t;
72
73 /**
74  * @brief allocate vapi message of given size
75  *
76  * @note message must be freed by vapi_msg_free if not consumed by vapi_send
77  * call
78  *
79  * @param ctx opaque vapi context
80  *
81  * @return pointer to message or NULL if out of memory
82  */
83 void *vapi_msg_alloc (vapi_ctx_t ctx, size_t size);
84
85 /**
86  * @brief free a vapi message
87  *
88  * @note messages received by vapi_recv must be freed when no longer needed
89  *
90  * @param ctx opaque vapi context
91  * @param msg message to be freed
92  */
93 void vapi_msg_free (vapi_ctx_t ctx, void *msg);
94
95 /**
96  * @brief allocate vapi context
97  *
98  * @param[out] pointer to result variable
99  *
100  * @return VAPI_OK on success, other error code on error
101  */
102 vapi_error_e vapi_ctx_alloc (vapi_ctx_t * result);
103
104 /**
105  * @brief free vapi context
106  */
107 void vapi_ctx_free (vapi_ctx_t ctx);
108
109 /**
110  * @brief check if message identified by it's message id is known by the vpp to
111  * which the connection is open
112  */
113 bool vapi_is_msg_available (vapi_ctx_t ctx, vapi_msg_id_t type);
114
115 /**
116  * @brief connect to vpp
117  *
118  * @param ctx opaque vapi context, must be allocated using vapi_ctx_alloc first
119  * @param name application name
120  * @param chroot_prefix shared memory prefix
121  * @param max_outstanding_requests max number of outstanding requests queued
122  * @param response_queue_size size of the response queue
123  * @param mode mode of operation - blocking or nonblocking
124  *
125  * @return VAPI_OK on success, other error code on error
126  */
127 vapi_error_e vapi_connect (vapi_ctx_t ctx, const char *name,
128                            const char *chroot_prefix,
129                            int max_outstanding_requests,
130                            int response_queue_size, vapi_mode_e mode);
131
132 /**
133  * @brief disconnect from vpp
134  *
135  * @param ctx opaque vapi context
136  *
137  * @return VAPI_OK on success, other error code on error
138  */
139 vapi_error_e vapi_disconnect (vapi_ctx_t ctx);
140
141 /**
142  * @brief get event file descriptor
143  *
144  * @note this file descriptor becomes readable when messages (from vpp)
145  * are waiting in queue
146  *
147  * @param ctx opaque vapi context
148  * @param[out] fd pointer to result variable
149  *
150  * @return VAPI_OK on success, other error code on error
151  */
152 vapi_error_e vapi_get_fd (vapi_ctx_t ctx, int *fd);
153
154 /**
155  * @brief low-level api for sending messages to vpp
156  *
157  * @note it is not recommended to use this api directly, use generated api
158  * instead
159  *
160  * @param ctx opaque vapi context
161  * @param msg message to send
162  *
163  * @return VAPI_OK on success, other error code on error
164  */
165 vapi_error_e vapi_send (vapi_ctx_t ctx, void *msg);
166
167 /**
168  * @brief low-level api for atomically sending two messages to vpp - either
169  * both messages are sent or neither one is
170  *
171  * @note it is not recommended to use this api directly, use generated api
172  * instead
173  *
174  * @param ctx opaque vapi context
175  * @param msg1 first message to send
176  * @param msg2 second message to send
177  *
178  * @return VAPI_OK on success, other error code on error
179  */
180 vapi_error_e vapi_send2 (vapi_ctx_t ctx, void *msg1, void *msg2);
181
182 /**
183  * @brief low-level api for reading messages from vpp
184  *
185  * @note it is not recommended to use this api directly, use generated api
186  * instead
187  *
188  * @param ctx opaque vapi context
189  * @param[out] msg pointer to result variable containing message
190  * @param[out] msg_size pointer to result variable containing message size
191  *
192  * @return VAPI_OK on success, other error code on error
193  */
194 vapi_error_e vapi_recv (vapi_ctx_t ctx, void **msg, size_t * msg_size);
195
196 /**
197  * @brief wait for connection to become readable or writable
198  *
199  * @param ctx opaque vapi context
200  * @param mode type of property to wait for - readability, writability or both
201  *
202  * @return VAPI_OK on success, other error code on error
203  */
204 vapi_error_e vapi_wait (vapi_ctx_t ctx, vapi_wait_mode_e mode);
205
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  */
211 vapi_error_e vapi_dispatch_one (vapi_ctx_t ctx);
212
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  */
227 vapi_error_e vapi_dispatch (vapi_ctx_t ctx);
228
229 /** generic vapi event callback */
230 typedef vapi_error_e (*vapi_event_cb) (vapi_ctx_t ctx, void *callback_ctx,
231                                        void *payload);
232
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  */
241 void vapi_set_event_cb (vapi_ctx_t ctx, vapi_msg_id_t id,
242                         vapi_event_cb callback, void *callback_ctx);
243
244 /**
245  * @brief clear event callback for given message id
246  *
247  * @param ctx opaque vapi context
248  * @param id message id
249  */
250 void vapi_clear_event_cb (vapi_ctx_t ctx, vapi_msg_id_t id);
251
252 /** generic vapi event callback */
253 typedef vapi_error_e (*vapi_generic_event_cb) (vapi_ctx_t ctx,
254                                                void *callback_ctx,
255                                                vapi_msg_id_t id, void *msg);
256 /**
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  */
266 void vapi_set_generic_event_cb (vapi_ctx_t ctx,
267                                 vapi_generic_event_cb callback,
268                                 void *callback_ctx);
269
270 /**
271  * @brief clear generic event callback
272  *
273  * @param ctx opaque vapi context
274  */
275 void vapi_clear_generic_event_cb (vapi_ctx_t ctx);
276
277 #endif
278
279 /*
280  * fd.io coding-style-patch-verification: ON
281  *
282  * Local Variables:
283  * eval: (c-set-style "gnu")
284  * End:
285  */