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