VPP API module
VPP API module allows communicating with VPP over shared memory interface. The API consists of 3 parts:
common code - low-level API
generated code - high-level API
code generator - to generate your own high-level API e.g. for custom plugins
C common code
C common code represents the basic, low-level API, providing functions to connect/disconnect, perform message discovery and send/receive messages. The C variant is in vapi.h.
C++ common code
C++ is provided by vapi.hpp and contains high-level API templates, which are specialized by generated code.
Each API file present in the source tree is automatically translated to
JSON file, which the code generator parses and generates either C
vapi_c_gen.py) or C++ (
This can then be included in the client application and provides convenient way to interact with VPP. This includes:
automatic request-response matching based on context
automatic casts to appropriate types (type-safety) when calling callbacks
automatic sending of control-pings for dump messages
The API supports two modes of operation:
In blocking mode, whenever an operation is initiated, the code waits
until it can finish. This means that when sending a message, the call
blocks until the message can be written to shared memory. Similarly,
receiving a message blocks until a message becomes available. On higher
level, this also means that when doing a request
show_version), the call blocks until a response comes back
In non-blocking mode, these are decoupled, the API returns VAPI_EAGAIN whenever an operation cannot be performed and after sending a request, it’s up to the client to wait for and process a response.
Python code generator comes in two flavors - C and C++ and generates high-level API headers. All the code is stored in the headers.
Refer to inline API documentation in doxygen format in
for description of functions. It’s recommended to use the safer,
high-level API provided by specialized headers (e.g.
C high-level API
The C high-level API is strictly callback-based for maximum efficiency. Whenever an operation is initiated a callback with a callback context is part of that operation. The callback is then invoked when the response (or multiple responses) arrive which are tied to the request. Also, callbacks are invoked whenever an event arrives, if such callback is registered. All the pointers to responses/events point to shared memory and are immediately freed after callback finishes so the client needs to extract/copy any data in which it is interested in.
In simple blocking mode, the whole operation (being a simple request or a dump) is finished and it’s callback is called (potentially multiple times for dumps) during function call.
Example pseudo-code for a simple request in this mode:
` vapi_show_version(message, callback, callback_context)
generate unique internal context and assign it to message.header.context
byteswap the message to network byte order
send message to vpp (message is now consumed and vpp will free it)
create internal “outstanding request context” which stores the callback, callback context and the internal context value
call dispatch, which in this mode receives and processes responses until the internal “outstanding requests” queue is empty. In blocking mode, this queue always contains at most one item. `
Note: it’s possible for different - unrelated callbacks to be called before the response callbacks is called in cases where e.g. events are stored in shared memory queue.
In non-blocking mode, all the requests are only byte-swapped and the context information along with callbacks is stored locally (so in the above example, only steps 1-4 are executed and step 5 is skipped). Calling dispatch is up to the client application. This allows to alternate between sending/receiving messages or have a dedicated thread which calls dispatch.
C++ high level API
In C++ API, the response is automatically tied to the corresponding
Event_registration object. Optionally a
callback might be specified, which then gets called when the response is
Note: responses take up shared memory space and should be freed either manually (in case of result sets) or automatically (by destroying the object owning them) when no longer needed. Once a Request or Dump object was executed, it cannot be re-sent, since the request itself (stores in shared memory) is consumed by vpp and inaccessible (set to nullptr) anymore.
Requests & dumps
Create on object of
Connectiontype and call
connect()to connect to vpp.
Create an object of
Dumptype using it’s typedef (e.g.
get_request()to obtain and manipulate the underlying request if required.
execute()to send the request.
dispatch()to wait for the response.
get_response_state()to get the state and
get_response()to read the response.
Connectionand execute the appropriate
Requestto subscribe to events (e.g.
Event_registrationwith a template argument being the type of event you are interested in.
wait_for_response()to wait for the event. A callback will be called when an event occurs (if passed to
Event_registration()constructor). Alternatively, read the result set.
Note: events stored in the result set take up space in shared memory and should be freed regularly (e.g. in the callback, once the event is processed).