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