FD.io VPP  v20.05.1-5-g09f167997
Vector Packet Processing
vapi.h
Go to the documentation of this file.
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 #include <vlibapi/api_types.h>
25 #include <vapi/vapi_common.h>
26 #include <svm/queue.h>
27 
28 #ifdef __cplusplus
29 extern "C"
30 {
31 #endif
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  */
47  typedef struct vapi_ctx_s *vapi_ctx_t;
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  */
59  void *vapi_msg_alloc (vapi_ctx_t ctx, size_t size);
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  */
69  void vapi_msg_free (vapi_ctx_t ctx, void *msg);
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  */
78  vapi_error_e vapi_ctx_alloc (vapi_ctx_t * result);
79 
80 /**
81  * @brief free vapi context
82  */
83  void vapi_ctx_free (vapi_ctx_t ctx);
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  */
89  bool vapi_is_msg_available (vapi_ctx_t ctx, vapi_msg_id_t type);
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
100  * @param handle_keepalives - if true, automatically handle memclnt_keepalive
101  *
102  * @return VAPI_OK on success, other error code on error
103  */
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 /**
111  * @brief disconnect from vpp
112  *
113  * @param ctx opaque vapi context
114  *
115  * @return VAPI_OK on success, other error code on error
116  */
117  vapi_error_e vapi_disconnect (vapi_ctx_t ctx);
118 
119 /**
120  * @brief get event file descriptor
121  *
122  * @note this file descriptor becomes readable when messages (from vpp)
123  * are waiting in queue
124  *
125  * @param ctx opaque vapi context
126  * @param[out] fd pointer to result variable
127  *
128  * @return VAPI_OK on success, other error code on error
129  */
130  vapi_error_e vapi_get_fd (vapi_ctx_t ctx, int *fd);
131 
132 /**
133  * @brief low-level api for sending messages to vpp
134  *
135  * @note it is not recommended to use this api directly, use generated api
136  * instead
137  *
138  * @param ctx opaque vapi context
139  * @param msg message to send
140  *
141  * @return VAPI_OK on success, other error code on error
142  */
143  vapi_error_e vapi_send (vapi_ctx_t ctx, void *msg);
144 
145 /**
146  * @brief low-level api for atomically sending two messages to vpp - either
147  * both messages are sent or neither one is
148  *
149  * @note it is not recommended to use this api directly, use generated api
150  * instead
151  *
152  * @param ctx opaque vapi context
153  * @param msg1 first message to send
154  * @param msg2 second message to send
155  *
156  * @return VAPI_OK on success, other error code on error
157  */
158  vapi_error_e vapi_send2 (vapi_ctx_t ctx, void *msg1, void *msg2);
159 
160 /**
161  * @brief low-level api for reading messages from vpp
162  *
163  * @note it is not recommended to use this api directly, use generated api
164  * instead
165  *
166  * @param ctx opaque vapi context
167  * @param[out] msg pointer to result variable containing message
168  * @param[out] msg_size pointer to result variable containing message size
169  * @param cond enum type for blocking, non-blocking or timed wait call
170  * @param time in sec for timed wait
171  *
172  * @return VAPI_OK on success, other error code on error
173  */
174  vapi_error_e vapi_recv (vapi_ctx_t ctx, void **msg, size_t * msg_size,
175  svm_q_conditional_wait_t cond, u32 time);
176 
177 /**
178  * @brief wait for connection to become readable or writable
179  *
180  * @param ctx opaque vapi context
181  * @param mode type of property to wait for - readability, writability or both
182  *
183  * @return VAPI_OK on success, other error code on error
184  */
186 
187 /**
188  * @brief pick next message sent by vpp and call the appropriate callback
189  *
190  * @return VAPI_OK on success, other error code on error
191  */
192  vapi_error_e vapi_dispatch_one (vapi_ctx_t ctx);
193 
194 /**
195  * @brief loop vapi_dispatch_one until responses to all currently outstanding
196  * requests have been received and their callbacks called
197  *
198  * @note the dispatch loop is interrupted if any error is encountered or
199  * returned from the callback, in which case this error is returned as the
200  * result of vapi_dispatch. In this case it might be necessary to call dispatch
201  * again to process the remaining messages. Returning VAPI_EUSER from
202  * a callback allows the user to break the dispatch loop (and distinguish
203  * this case in the calling code from other failures). VAPI never returns
204  * VAPI_EUSER on its own.
205  *
206  * @return VAPI_OK on success, other error code on error
207  */
208  vapi_error_e vapi_dispatch (vapi_ctx_t ctx);
209 
210 /** generic vapi event callback */
211  typedef vapi_error_e (*vapi_event_cb) (vapi_ctx_t ctx, void *callback_ctx,
212  void *payload);
213 
214 /**
215  * @brief set event callback to call when message with given id is dispatched
216  *
217  * @param ctx opaque vapi context
218  * @param id message id
219  * @param callback callback
220  * @param callback_ctx context pointer stored and passed to callback
221  */
222  void vapi_set_event_cb (vapi_ctx_t ctx, vapi_msg_id_t id,
223  vapi_event_cb callback, void *callback_ctx);
224 
225 /**
226  * @brief clear event callback for given message id
227  *
228  * @param ctx opaque vapi context
229  * @param id message id
230  */
231  void vapi_clear_event_cb (vapi_ctx_t ctx, vapi_msg_id_t id);
232 
233 /** generic vapi event callback */
234  typedef vapi_error_e (*vapi_generic_event_cb) (vapi_ctx_t ctx,
235  void *callback_ctx,
236  vapi_msg_id_t id, void *msg);
237 /**
238  * @brief set generic event callback
239  *
240  * @note this callback is called by dispatch if no message-type specific
241  * callback is set (so it's a fallback callback)
242  *
243  * @param ctx opaque vapi context
244  * @param callback callback
245  * @param callback_ctx context pointer stored and passed to callback
246  */
247  void vapi_set_generic_event_cb (vapi_ctx_t ctx,
248  vapi_generic_event_cb callback,
249  void *callback_ctx);
250 
251 /**
252  * @brief clear generic event callback
253  *
254  * @param ctx opaque vapi context
255  */
256  void vapi_clear_generic_event_cb (vapi_ctx_t ctx);
257 
258 #ifdef __cplusplus
259 }
260 #endif
261 
262 #endif
263 
264 /*
265  * fd.io coding-style-patch-verification: ON
266  *
267  * Local Variables:
268  * eval: (c-set-style "gnu")
269  * End:
270  */
void * vapi_msg_alloc(vapi_ctx_t ctx, size_t size)
allocate vapi message of given size
Definition: vapi.c:217
void vapi_clear_generic_event_cb(vapi_ctx_t ctx)
clear generic event callback
Definition: vapi.c:847
vapi_error_e vapi_disconnect(vapi_ctx_t ctx)
disconnect from vpp
Definition: vapi.c:419
Optimized string handling code, including c11-compliant "safe C library" variants.
bool handle_keepalives
Definition: vapi.c:89
vapi_error_e vapi_wait(vapi_ctx_t ctx, vapi_wait_mode_e mode)
wait for connection to become readable or writable
Definition: vapi.c:621
vapi_mode_e
Definition: vapi_common.h:42
u8 id[64]
Definition: dhcp.api:160
vapi_error_e(* vapi_generic_event_cb)(vapi_ctx_t ctx, void *callback_ctx, vapi_msg_id_t id, void *msg)
generic vapi event callback
Definition: vapi.h:234
vapi_error_e vapi_send2(vapi_ctx_t ctx, void *msg1, void *msg2)
low-level api for atomically sending two messages to vpp - either both messages are sent or neither o...
Definition: vapi.c:485
unsigned int u32
Definition: types.h:88
void vapi_ctx_free(vapi_ctx_t ctx)
free vapi context
Definition: vapi.c:283
vl_api_fib_path_type_t type
Definition: fib_types.api:123
long ctx[MAX_CONNS]
Definition: main.c:144
u64 size
Definition: vhost_user.h:150
vapi_error_e vapi_dispatch(vapi_ctx_t ctx)
loop vapi_dispatch_one until responses to all currently outstanding requests have been received and t...
Definition: vapi.c:809
vl_api_tunnel_mode_t mode
Definition: gre.api:48
svm_q_conditional_wait_t
Definition: queue.h:40
void vapi_clear_event_cb(vapi_ctx_t ctx, vapi_msg_id_t id)
clear event callback for given message id
Definition: vapi.c:833
struct vapi_ctx_s * vapi_ctx_t
Definition: vapi.h:47
vapi_error_e vapi_connect(vapi_ctx_t ctx, const char *name, const char *chroot_prefix, int max_outstanding_requests, int response_queue_size, vapi_mode_e mode, bool handle_keepalives)
connect to vpp
Definition: vapi.c:301
vapi_wait_mode_e
Definition: vapi_common.h:48
string name[64]
Definition: ip.api:44
void vapi_msg_free(vapi_ctx_t ctx, void *msg)
free a vapi message
Definition: vapi.c:228
vapi_error_e vapi_ctx_alloc(vapi_ctx_t *result)
allocate vapi context
Definition: vapi.c:251
bool vapi_is_msg_available(vapi_ctx_t ctx, vapi_msg_id_t type)
check if message identified by it&#39;s message id is known by the vpp to which the connection is open ...
Definition: vapi.c:295
vapi_error_e vapi_dispatch_one(vapi_ctx_t ctx)
pick next message sent by vpp and call the appropriate callback
Definition: vapi.c:752
vapi_error_e(* vapi_event_cb)(vapi_ctx_t ctx, void *callback_ctx, void *payload)
generic vapi event callback
Definition: vapi.h:211
void vapi_set_generic_event_cb(vapi_ctx_t ctx, vapi_generic_event_cb callback, void *callback_ctx)
set generic event callback
Definition: vapi.c:839
vapi_error_e vapi_get_fd(vapi_ctx_t ctx, int *fd)
get event file descriptor
Definition: vapi.c:435
void vapi_set_event_cb(vapi_ctx_t ctx, vapi_msg_id_t id, vapi_event_cb callback, void *callback_ctx)
set event callback to call when message with given id is dispatched
Definition: vapi.c:824
unsigned int vapi_msg_id_t
Definition: vapi_common.h:55
vapi_error_e vapi_recv(vapi_ctx_t ctx, void **msg, size_t *msg_size, svm_q_conditional_wait_t cond, u32 time)
low-level api for reading messages from vpp
Definition: vapi.c:531
vapi_error_e vapi_send(vapi_ctx_t ctx, void *msg)
low-level api for sending messages to vpp
Definition: vapi.c:441
vapi_error_e
Definition: vapi_common.h:25