1 # VPP API Language {#api_lang_doc}
3 The VPP binary API is a message passing API.
4 The VPP API language is used to define a RPC interface between VPP and its
5 control plane. The API messages supports shared memory transport and
6 Unix domain sockets (SOCK_STREAM).
8 The wire format is essentially that of a packed C struct.
10 The VPP API compiler is located in *src/tools/vppapigen* and can currently
11 compile to JSON or C (used by the VPP binary itself).
13 ## Language definition
15 ### Defining a messages
17 There are 3 types of message exchanges:
20 The client sends a request message and the server replies with a
21 single reply message. The convention is that the reply message is
22 named as method_name + \_reply.
25 The client sends a "bulk" request message to the server, and the
26 server replies with a set of detail messages. These messages may be of
27 different type. A dump/detail call must be enclosed in a control ping
28 block (Otherwise the client will not know the end of the bulk
29 transmission). The method name must end with method + "\_dump", the
30 reply message should be named method + "\_details". The exception here
31 is for the methods that return multiple message types
32 (e.g. sw_interface_dump). The Dump/Detail methods are typically used
33 for acquiring bulk information, like the complete FIB table.
36 The client can register for getting asynchronous notifications from
37 the server. This is useful for getting interface state changes, and so
38 on. The method name for requesting notifications is conventionally
39 prefixed with "want_". E.g. "want_interface_events". Which
40 notification types results from an event registration is defined in
41 the service definition.
43 A message from a client must include the 'client_index', an opaque
44 cookie identifying the sender, and a 'context' field to let the client
45 match request with reply.
47 An example of a message definition. The client sends the show_version request,
48 the server replies with the show_version_reply.
50 The *client_index* and *context* fields are required in all requests.
51 The *context* is returned by the server and is used by the client to
52 match up request and reply messages.
60 define show_version_reply
64 string program [limit = 32];
65 string version [limit = 32];
66 string build_date [limit = 32];
67 string build_directory [limit = 256];
72 The flags are not used by the clients, but have special meaning
73 for some of the tracing and debugging of the API.
74 The *autoreply* flag is a shorthand for a reply message with just a
78 define : DEFINE ID '{' block_statements_opt '}' ';'
79 define : flist DEFINE ID '{' block_statements_opt '}' ';'
87 block_statements_opt : block_statements
88 block_statements : block_statement
89 | block_statements block_statement
90 block_statement : declaration
92 declaration : type_specifier ID ';'
93 | type_specifier ID '[' ID '=' assignee ']' ';'
94 declaration : type_specifier ID '[' NUM ']' ';'
95 | type_specifier ID '[' ID ']' ';'
112 The *option* word is used to specify meta information.
113 The only current use is to specify a semantic version of the .api file itself.
117 option version = "1.0.0";
122 option : OPTION ID '=' assignee ';'
129 ### Defining new types
131 New user defined types are defined just like messages.
132 A typedef has two forms. It can either define an alias for a
133 different type (or array).
138 typedef u8 ip4_address[4];
139 typedef u8 ip6_address[16];
142 Where the above defines two new types *vl_api_ip4_address_t* and
143 *vl_api_ip6_address_t*. These are aliases for the underlaying
146 In the other form, it is used to specify an abstract data type.
149 enum address_family {
154 union address_union {
155 vl_api_ip4_address_t ip4;
156 vl_api_ip6_address_t ip6;
160 vl_api_address_family_t af;
161 vl_api_address_union_t un;
165 Where the new type *vl_api_address_t*
168 typedef : TYPEDEF ID '{' block_statements_opt '}' ';'
169 typedef : TYPEDEF declaration
173 ### Importing Definitions
174 You can use definitions from other .api files by importing them.
175 To import another .api's definitions, you add an import statement
176 to the top of your file:
178 import "vnet/ip/ip_types.api";
180 By default you can only use definitions from directly imported .api files.
182 The API compiler searches for imported files in a set of directories
183 specified on the API compiler command line using the --includedir flag.
185 import : IMPORT STRING_LITERAL ';'
190 The API language uses C style comments.
197 Enums are similar to enums in C.
199 Every enum definition must contain a constant that maps to zero
200 as its first element. This is because:
202 There must be a zero value, so that we can use 0 as a numeric default value.
203 The zero value needs to be the first element.
205 As in C, enums can be used as flags or just as numbers.
206 The on-wire, and in memory representation size of an enum can be specified.
207 Not all language bindings will support that. The default size is 4 (u32).
211 enum ip_neighbor_flags
213 IP_API_NEIGHBOR_FLAG_NONE = 0,
214 IP_API_NEIGHBOR_FLAG_STATIC = 0x1,
215 IP_API_NEIGHBOR_FLAG_NO_FIB_ENTRY = 0x2,
219 Which generates the vl_api_ip_neighbor_flags_t in the C binding.
220 In Python that is represented as an IntFlag object
221 VppEnum.vl_api_ip_neighbor_flags_t.
224 enum : ENUM ID '{' enum_statements '}' ';'
225 enum : ENUM ID ':' enum_size '{' enum_statements '}' ';'
229 enum_statements : enum_statement
230 | enum_statements enum_statement
231 enum_statement : ID '=' NUM ','
236 The service statement defines the relationship between messages.
237 For request/response and dump/details messages it ties the
238 request with the reply. For events, it specifies which events
239 that can be received for a given want_* call.
244 rpc want_interface_events returns want_interface_events_reply
245 events sw_interface_event;
250 Which states that the request want_interface_events returns a
251 want_interface_events_reply and if enabled the client will
252 receive sw_interface_event messages whenever interface states changes.
255 service : SERVICE '{' service_statements '}' ';'
256 service_statements : service_statement
257 | service_statements service_statement
258 service_statement : RPC ID RETURNS NULL ';'
259 | RPC ID RETURNS ID ';'
260 | RPC ID RETURNS STREAM ID ';'
261 | RPC ID RETURNS ID EVENTS event_list ';'
270 ### Scalar Value Types
272 .api type|size|C type|Python type
273 ---------|----|------------------
283 bool | 1|bool |boolean
284 string |variable|vl_api_string_t|str
286 ### User Defined Types
287 #### vnet/ip/ip_types.api
289 .api type|size|C type|Python type
290 ---------|----|------|-----------
291 vl_api_address_t|20|vl_api_address_t|`<class 'ipaddress.IPv4Address'> or <class 'ipaddress.IPv6Address'>`
292 vl_api_ip4_address_t|4|vl_api_ip4_address_t|`<class 'ipaddress.IPv4Address'>`
293 vl_api_ip6_address_t|16|vl_api_ip6_address_t|`<class 'ipaddress.IPv6Address'>`
294 vl_api_prefix_t|21|vl_api_prefix_t|`<class 'ipaddress.IPv4Network'> or <class 'ipaddress.IPv6Network'>`
295 vl_api_ip4_prefix_t|5|vl_api_ip4_prefix_t|`<class 'ipaddress.IPv4Network'>`
296 vl_api_ip6_prefix_t|17|vl_api_ip6_prefix_t|`<class 'ipaddress.IPv6Network'>`
298 #### vnet/ethernet/ethernet_types.api
299 .api type|size|C type|Python type
300 ---------|----|------|-----------
301 vl_api_mac_address_t|6|vl_api_mac_address_t|`class 'vpp_papi.MACAddress'>`
303 #### vnet/interface_types.api
304 .api type|size|C type|Python type
305 ---------|----|------|-----------
306 vl_api_interface_index_t|4|vl_api_interface_index_t|int
308 ### New explicit types
310 #### String versus bytes
311 A byte string with a maximum length of 64:
315 Before the "string" type was added, text string were defined like this.
316 The implications of that was the user would have to know if the field
317 represented a \0 ended C-string or a fixed length byte string.
318 The wire format of the 'string' type is a u32 length
320 An IPv4 or IPv6 address was previously defined like:
326 Which made it hard for language bindings to represent the
327 address as anything but a byte string.
328 The new explicit address types are shown above.
330 ## Language generators
332 The VPP API compiler currently has two output modules. One generating JSON
333 and one generating C header files that are directly used by the VPP
334 infrastructure and plugins.
336 The C/C++, Python, Go Lua, and Java language bindings are generated based
339 ### Future considerations
340 - [ ] Generate C/C++ (vapi) client code directly from vppapigen
341 - [ ] Embed JSON definitions into the API server, so dynamic languages
342 can download them directly without going via the filesystem and JSON