FD.io VPP
v20.05.1-6-gf53edbc3b
Vector Packet Processing
|
VPP API module allows communicating with VPP over shared memory interface. The API consists of 3 parts:
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++ 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++ (vapi_cpp_gen.py
) code.
This can then be included in the client application and provides convenient way to interact with VPP. This includes:
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 (e.g. show_version
), the call blocks until a response comes back (e.g. show_version_reply
).
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 vapi.h
header for description of functions. It's recommened to use the safer, high-level API provided by specialized headers (e.g. vpe.api.vapi.h
or vpe.api.vapi.hpp
).
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)
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.
In C++ API, the response is automatically tied to the corresponding Request
, Dump
or Event_registration
object. Optionally a callback might be specified, which then gets called when the response is received.
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.
0. Create on object of Connection
type and call connect()
to connect to vpp.
Request
or Dump
type using it's typedef (e.g. Show_version
)get_request()
to obtain and manipulate the underlying request if required.execute()
to send the request.wait_for_response()
or dispatch()
to wait for the response.get_response_state()
to get the state and get_response()
to read the response.0. Create a Connection
and execute the appropriate Request
to subscribe to events (e.g. Want_stats
)
Event_registration
with a template argument being the type of event you are insterested in.dispatch()
or 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).