8 from vapi_json_parser import (
23 return "vapi_type_%s" % self.name
26 if self.type.get_c_name() == "vl_api_string_t":
28 return "u8 %s[%d];" % (self.name, self.len)
30 return "vl_api_string_t %s;" % (self.name)
32 if self.len is not None and type(self.len) != dict:
33 return "%s %s[%d];" % (self.type.get_c_name(), self.name, self.len)
35 return "%s %s;" % (self.type.get_c_name(), self.name)
37 def get_swap_to_be_code(self, struct, var):
38 if self.len is not None and type(self.len) != dict:
41 "do { unsigned i; for (i = 0; i < %d; ++i) { %s } }"
43 % (self.len, self.type.get_swap_to_be_code(struct, "%s[i]" % var))
46 if self.nelem_field.needs_byte_swap():
47 nelem_field = "%s(%s%s)" % (
48 self.nelem_field.type.get_swap_to_host_func_name(),
50 self.nelem_field.name,
53 nelem_field = "%s%s" % (struct, self.nelem_field.name)
55 "do { unsigned i; for (i = 0; i < %s; ++i) { %s } }"
59 self.type.get_swap_to_be_code(struct, "%s[i]" % var),
62 return self.type.get_swap_to_be_code(struct, "%s" % var)
64 def get_swap_to_host_code(self, struct, var):
65 if self.len is not None and type(self.len) != dict:
68 "do { unsigned i; for (i = 0; i < %d; ++i) { %s } }"
70 % (self.len, self.type.get_swap_to_host_code(struct, "%s[i]" % var))
73 # nelem_field already swapped to host here...
75 "do { unsigned i; for (i = 0; i < %s%s; ++i) { %s } }"
79 self.nelem_field.name,
80 self.type.get_swap_to_host_code(struct, "%s[i]" % var),
83 return self.type.get_swap_to_host_code(struct, "%s" % var)
85 def needs_byte_swap(self):
86 return self.type.needs_byte_swap()
88 def get_vla_field_length_name(self, path):
89 return "%s_%s_array_size" % ("_".join(path), self.name)
91 def get_alloc_vla_param_names(self, path):
93 result = [self.get_vla_field_length_name(path)]
96 if self.type.has_vla():
97 t = self.type.get_alloc_vla_param_names(path + [self.name])
101 def get_vla_calc_size_code(self, prefix, path):
104 "sizeof(%s.%s[0]) * %s"
106 ".".join([prefix] + path),
108 self.get_vla_field_length_name(path),
113 if self.type.has_vla():
114 t = self.type.get_vla_calc_size_code(prefix, path + [self.name])
118 def get_vla_assign_code(self, prefix, path):
124 ".".join([prefix] + path),
125 self.nelem_field.name,
126 self.get_vla_field_length_name(path),
129 if self.type.has_vla():
130 t = self.type.get_vla_assign_code(prefix, path + [self.name])
135 class CAlias(CField):
136 def get_c_name(self):
137 return "vapi_type_%s" % self.name
140 if self.len is not None:
141 return "typedef %s vapi_type_%s[%d];" % (
142 self.type.get_c_name(),
147 return "typedef %s vapi_type_%s;" % (self.type.get_c_name(), self.name)
150 class CStruct(Struct):
154 "typedef struct __attribute__((__packed__)) {\n%s"
155 % ("\n".join([" %s" % x.get_c_def() for x in self.fields])),
156 "} %s;" % self.get_c_name(),
160 def get_vla_assign_code(self, prefix, path):
165 for x in f.get_vla_assign_code(prefix, path)
168 def get_alloc_vla_param_names(self, path):
173 for x in f.get_alloc_vla_param_names(path)
176 def get_vla_calc_size_code(self, prefix, path):
181 for x in f.get_vla_calc_size_code(prefix, path)
185 class CSimpleType(SimpleType):
196 swap_to_host_dict = {
205 __packed = "__attribute__((packed))"
213 def get_c_name(self):
216 def get_swap_to_be_func_name(self):
217 return self.swap_to_be_dict[self.name]
219 def get_swap_to_host_func_name(self):
220 return self.swap_to_host_dict[self.name]
222 def get_packed_string(self):
223 return self.pack_dict[self.name]
225 def get_swap_to_be_code(self, struct, var, cast=None):
226 x = "%s%s" % (struct, var)
227 return "%s = %s%s(%s);" % (
229 "(%s)" % cast if cast else "",
230 self.get_swap_to_be_func_name(),
234 def get_swap_to_host_code(self, struct, var, cast=None):
235 x = "%s%s" % (struct, var)
236 return "%s = %s%s(%s);" % (
238 "(%s)" % cast if cast else "",
239 self.get_swap_to_host_func_name(),
243 def needs_byte_swap(self):
245 self.get_swap_to_host_func_name()
251 def get_packed(self):
252 return self.pack_dict.get(self.name, "")
256 def get_c_name(self):
257 return "vapi_enum_%s" % self.name
260 return "typedef enum {\n%s\n} %s %s;" % (
261 "\n".join([" %s = %s," % (i, j) for i, j in self.value_pairs]),
262 self.type.get_packed(),
266 def needs_byte_swap(self):
267 return self.type.needs_byte_swap()
269 def get_swap_to_be_code(self, struct, var):
270 return self.type.get_swap_to_be_code(struct, var, self.get_c_name())
272 def get_swap_to_host_code(self, struct, var):
273 return self.type.get_swap_to_host_code(struct, var, self.get_c_name())
277 def get_c_name(self):
278 return "vapi_union_%s" % self.name
281 return "typedef union {\n%s\n} %s;" % (
282 "\n".join([" %s %s;" % (i.get_c_name(), j) for i, j in self.type_pairs]),
286 def needs_byte_swap(self):
290 class CStructType(StructType, CStruct):
291 def get_c_name(self):
292 return "vapi_type_%s" % self.name
294 def get_swap_to_be_func_name(self):
295 return "%s_hton" % self.get_c_name()
297 def get_swap_to_host_func_name(self):
298 return "%s_ntoh" % self.get_c_name()
300 def get_swap_to_be_func_decl(self):
301 return "void %s(%s *msg)" % (self.get_swap_to_be_func_name(), self.get_c_name())
303 def get_swap_to_be_func_def(self):
304 return "%s\n{\n%s\n}" % (
305 self.get_swap_to_be_func_decl(),
308 " %s" % p.get_swap_to_be_code("msg->", "%s" % p.name)
310 if p.needs_byte_swap()
315 def get_swap_to_host_func_decl(self):
316 return "void %s(%s *msg)" % (
317 self.get_swap_to_host_func_name(),
321 def get_swap_to_host_func_def(self):
322 return "%s\n{\n%s\n}" % (
323 self.get_swap_to_host_func_decl(),
326 " %s" % p.get_swap_to_host_code("msg->", "%s" % p.name)
328 if p.needs_byte_swap()
333 def get_swap_to_be_code(self, struct, var):
334 return "%s(&%s%s);" % (self.get_swap_to_be_func_name(), struct, var)
336 def get_swap_to_host_code(self, struct, var):
337 return "%s(&%s%s);" % (self.get_swap_to_host_func_name(), struct, var)
339 def needs_byte_swap(self):
340 for f in self.fields:
341 if f.needs_byte_swap():
346 class CMessage(Message):
347 def __init__(self, logger, definition, json_parser):
348 super(CMessage, self).__init__(logger, definition, json_parser)
349 self.payload_members = [
350 " %s" % p.get_c_def() for p in self.fields if p.type != self.header
353 def has_payload(self):
354 return len(self.payload_members) > 0
356 def get_msg_id_name(self):
357 return "vapi_msg_id_%s" % self.name
359 def get_c_name(self):
360 return "vapi_msg_%s" % self.name
362 def get_payload_struct_name(self):
363 return "vapi_payload_%s" % self.name
365 def get_alloc_func_name(self):
366 return "vapi_alloc_%s" % self.name
368 def get_alloc_vla_param_names(self):
373 for x in f.get_alloc_vla_param_names([])
376 def get_alloc_func_decl(self):
377 return "%s* %s(struct vapi_ctx_s *ctx%s)" % (
379 self.get_alloc_func_name(),
380 "".join([", size_t %s" % n for n in self.get_alloc_vla_param_names()]),
383 def get_alloc_func_def(self):
385 if self.header.has_field("client_index"):
386 extra.append(" msg->header.client_index = vapi_get_client_index(ctx);")
387 if self.header.has_field("context"):
388 extra.append(" msg->header.context = 0;")
391 "%s" % self.get_alloc_func_decl(),
393 " %s *msg = NULL;" % self.get_c_name(),
394 " const size_t size = sizeof(%s)%s;"
402 for x in f.get_vla_calc_size_code("msg->payload", [])
406 " /* cast here required to play nicely with C++ world ... */",
407 " msg = (%s*)vapi_msg_alloc(ctx, size);" % self.get_c_name(),
414 " msg->header._vl_msg_id = vapi_lookup_vl_msg_id(ctx, %s);"
415 % self.get_msg_id_name(),
421 for line in f.get_vla_assign_code("msg->payload", [])
429 def get_calc_msg_size_func_name(self):
430 return "vapi_calc_%s_msg_size" % self.name
432 def get_calc_msg_size_func_decl(self):
433 return "uword %s(%s *msg)" % (
434 self.get_calc_msg_size_func_name(),
438 def get_calc_msg_size_func_def(self):
441 "%s" % self.get_calc_msg_size_func_decl(),
443 " return sizeof(*msg)%s;"
446 "+ msg->payload.%s * sizeof(msg->payload.%s[0])"
447 % (f.nelem_field.name, f.name)
449 if f.nelem_field is not None
456 def get_verify_msg_size_func_name(self):
457 return f"vapi_verify_{self.name}_msg_size"
459 def get_verify_msg_size_func_decl(self):
460 return "int %s(%s *msg, uword buf_size)" % (
461 self.get_verify_msg_size_func_name(),
465 def get_verify_msg_size_func_def(self):
466 return inspect.cleandoc(
468 {self.get_verify_msg_size_func_decl()}
470 if (sizeof({self.get_c_name()}) > buf_size)
472 VAPI_ERR("Truncated '{self.name}' msg received, received %lu"
473 "bytes, expected %lu bytes.", buf_size,
474 sizeof({self.get_c_name()}));
477 if ({self.get_calc_msg_size_func_name()}(msg) > buf_size)
479 VAPI_ERR("Truncated '{self.name}' msg received, received %lu"
480 "bytes, expected %lu bytes.", buf_size,
481 sizeof({self.get_calc_msg_size_func_name()}));
490 if self.has_payload():
493 "typedef struct __attribute__ ((__packed__)) {",
494 "%s " % "\n".join(self.payload_members),
495 "} %s;" % self.get_payload_struct_name(),
497 "typedef struct __attribute__ ((__packed__)) {",
499 " %s %s;" % (self.header.get_c_name(), self.fields[0].name)
500 if self.header is not None
503 " %s payload;" % self.get_payload_struct_name(),
504 "} %s;" % self.get_c_name(),
510 "typedef struct __attribute__ ((__packed__)) {",
512 " %s %s;" % (self.header.get_c_name(), self.fields[0].name)
513 if self.header is not None
516 "} %s;" % self.get_c_name(),
520 def get_swap_payload_to_host_func_name(self):
521 return "%s_payload_ntoh" % self.get_c_name()
523 def get_swap_payload_to_be_func_name(self):
524 return "%s_payload_hton" % self.get_c_name()
526 def get_swap_payload_to_host_func_decl(self):
527 return "void %s(%s *payload)" % (
528 self.get_swap_payload_to_host_func_name(),
529 self.get_payload_struct_name(),
532 def get_swap_payload_to_be_func_decl(self):
533 return "void %s(%s *payload)" % (
534 self.get_swap_payload_to_be_func_name(),
535 self.get_payload_struct_name(),
538 def get_swap_payload_to_be_func_def(self):
539 return "%s\n{\n%s\n}" % (
540 self.get_swap_payload_to_be_func_decl(),
543 " %s" % p.get_swap_to_be_code("payload->", "%s" % p.name)
545 if p.needs_byte_swap() and p.type != self.header
550 def get_swap_payload_to_host_func_def(self):
551 return "%s\n{\n%s\n}" % (
552 self.get_swap_payload_to_host_func_decl(),
555 " %s" % p.get_swap_to_host_code("payload->", "%s" % p.name)
557 if p.needs_byte_swap() and p.type != self.header
562 def get_swap_to_host_func_name(self):
563 return "%s_ntoh" % self.get_c_name()
565 def get_swap_to_be_func_name(self):
566 return "%s_hton" % self.get_c_name()
568 def get_swap_to_host_func_decl(self):
569 return "void %s(%s *msg)" % (
570 self.get_swap_to_host_func_name(),
574 def get_swap_to_be_func_decl(self):
575 return "void %s(%s *msg)" % (self.get_swap_to_be_func_name(), self.get_c_name())
577 def get_swap_to_be_func_def(self):
580 "%s" % self.get_swap_to_be_func_decl(),
583 ' VAPI_DBG("Swapping `%s\'@%%p to big endian", msg);'
586 " %s(&msg->header);" % self.header.get_swap_to_be_func_name()
587 if self.header is not None
589 " %s(&msg->payload);" % self.get_swap_payload_to_be_func_name()
590 if self.has_payload()
596 def get_swap_to_host_func_def(self):
599 "%s" % self.get_swap_to_host_func_decl(),
602 ' VAPI_DBG("Swapping `%s\'@%%p to host byte order", msg);'
605 " %s(&msg->header);" % self.header.get_swap_to_host_func_name()
606 if self.header is not None
608 " %s(&msg->payload);" % self.get_swap_payload_to_host_func_name()
609 if self.has_payload()
615 def get_op_func_name(self):
616 return "vapi_%s" % self.name
618 def get_op_func_decl(self):
619 if self.reply.has_payload():
620 return "vapi_error_e %s(%s)" % (
621 self.get_op_func_name(),
624 "struct vapi_ctx_s *ctx",
625 "%s *msg" % self.get_c_name(),
626 "vapi_error_e (*callback)(struct vapi_ctx_s *ctx",
627 " void *callback_ctx",
631 % self.reply.get_payload_struct_name(),
632 "void *callback_ctx",
637 return "vapi_error_e %s(%s)" % (
638 self.get_op_func_name(),
641 "struct vapi_ctx_s *ctx",
642 "%s *msg" % self.get_c_name(),
643 "vapi_error_e (*callback)(struct vapi_ctx_s *ctx",
644 " void *callback_ctx",
647 "void *callback_ctx",
652 def get_op_func_def(self):
655 "%s" % self.get_op_func_decl(),
657 " if (!msg || !callback) {",
658 " return VAPI_EINVAL;",
660 " if (vapi_is_nonblocking(ctx) && vapi_requests_full(ctx)) {",
661 " return VAPI_EAGAIN;",
664 " if (VAPI_OK != (rv = vapi_producer_lock (ctx))) {",
667 " u32 req_context = vapi_gen_req_context(ctx);",
668 " msg->header.context = req_context;",
669 " %s(msg);" % self.get_swap_to_be_func_name(),
671 " if (VAPI_OK == (rv = vapi_send_with_control_ping "
672 "(ctx, msg, req_context))) {"
673 if self.reply_is_stream
674 else " if (VAPI_OK == (rv = vapi_send (ctx, msg))) {"
677 " vapi_store_request(ctx, req_context, %s, "
678 "(vapi_cb_t)callback, callback_ctx);"
679 % ("true" if self.reply_is_stream else "false")
681 " if (VAPI_OK != vapi_producer_unlock (ctx)) {",
682 " abort (); /* this really shouldn't happen */",
684 " if (vapi_is_nonblocking(ctx)) {",
687 " rv = vapi_dispatch(ctx);",
690 " %s(msg);" % self.get_swap_to_host_func_name(),
691 " if (VAPI_OK != vapi_producer_unlock (ctx)) {",
692 " abort (); /* this really shouldn't happen */",
701 def get_event_cb_func_decl(self):
702 if not self.is_reply and not self.is_event:
703 raise Exception("Cannot register event callback for non-reply message")
704 if self.has_payload():
707 "void vapi_set_%s_event_cb (" % self.get_c_name(),
708 " struct vapi_ctx_s *ctx, ",
710 " vapi_error_e (*callback)(struct vapi_ctx_s *ctx, "
711 "void *callback_ctx, %s *payload),"
712 % self.get_payload_struct_name()
714 " void *callback_ctx)",
720 "void vapi_set_%s_event_cb (" % self.get_c_name(),
721 " struct vapi_ctx_s *ctx, ",
722 " vapi_error_e (*callback)(struct vapi_ctx_s *ctx, "
723 "void *callback_ctx),",
724 " void *callback_ctx)",
728 def get_event_cb_func_def(self):
729 if not self.is_reply and not self.is_event:
730 raise Exception("Cannot register event callback for non-reply function")
733 "%s" % self.get_event_cb_func_decl(),
736 " vapi_set_event_cb(ctx, %s, (vapi_event_cb)callback, "
737 "callback_ctx);" % self.get_msg_id_name()
743 def get_c_metadata_struct_name(self):
744 return "__vapi_metadata_%s" % self.name
746 def get_c_constructor(self):
748 if self.header is not None:
749 has_context = self.header.has_field("context")
752 "static void __attribute__((constructor)) __vapi_constructor_%s()"
755 ' static const char name[] = "%s";' % self.name,
756 ' static const char name_with_crc[] = "%s_%s";'
757 % (self.name, self.crc[2:]),
758 " static vapi_message_desc_t %s = {"
759 % self.get_c_metadata_struct_name(),
761 " sizeof(name) - 1,",
763 " sizeof(name_with_crc) - 1,",
764 " true," if has_context else " false,",
765 " offsetof(%s, context)," % self.header.get_c_name()
768 (" offsetof(%s, payload)," % self.get_c_name())
769 if self.has_payload()
770 else " VAPI_INVALID_MSG_ID,",
771 " (verify_msg_size_fn_t)%s," % self.get_verify_msg_size_func_name(),
772 " (generic_swap_fn_t)%s," % self.get_swap_to_be_func_name(),
773 " (generic_swap_fn_t)%s," % self.get_swap_to_host_func_name(),
774 " VAPI_INVALID_MSG_ID,",
777 " %s = vapi_register_msg(&%s);"
778 % (self.get_msg_id_name(), self.get_c_metadata_struct_name()),
779 ' VAPI_DBG("Assigned msg id %%d to %s", %s);'
780 % (self.name, self.get_msg_id_name()),
786 def emit_definition(parser, json_file, emitted, o):
789 if o.name in ("msg_header1_t", "msg_header2_t"):
791 if hasattr(o, "depends"):
793 emit_definition(parser, json_file, emitted, x)
794 if hasattr(o, "reply"):
795 emit_definition(parser, json_file, emitted, o.reply)
796 if hasattr(o, "get_c_def"):
798 o not in parser.enums_by_json[json_file]
799 and o not in parser.types_by_json[json_file]
800 and o not in parser.unions_by_json[json_file]
801 and o.name not in parser.messages_by_json[json_file]
802 and o not in parser.aliases_by_json[json_file]
805 guard = "defined_%s" % o.get_c_name()
806 print("#ifndef %s" % guard)
807 print("#define %s" % guard)
808 print("%s" % o.get_c_def())
810 function_attrs = "static inline "
811 if o.name in parser.messages_by_json[json_file]:
813 print("%s%s" % (function_attrs, o.get_swap_payload_to_be_func_def()))
815 print("%s%s" % (function_attrs, o.get_swap_payload_to_host_func_def()))
817 print("%s%s" % (function_attrs, o.get_swap_to_be_func_def()))
819 print("%s%s" % (function_attrs, o.get_swap_to_host_func_def()))
821 print("%s%s" % (function_attrs, o.get_calc_msg_size_func_def()))
823 print("%s%s" % (function_attrs, o.get_verify_msg_size_func_def()))
824 if not o.is_reply and not o.is_event:
826 print("%s%s" % (function_attrs, o.get_alloc_func_def()))
828 print("%s%s" % (function_attrs, o.get_op_func_def()))
830 print("%s" % o.get_c_constructor())
831 if o.is_reply or o.is_event:
833 print("%s%s;" % (function_attrs, o.get_event_cb_func_def()))
834 elif hasattr(o, "get_swap_to_be_func_def"):
835 print("%s%s" % (function_attrs, o.get_swap_to_be_func_def()))
837 print("%s%s" % (function_attrs, o.get_swap_to_host_func_def()))
843 def gen_json_unified_header(parser, logger, j, io, name):
844 d, f = os.path.split(j)
845 logger.info("Generating header `%s'" % name)
846 orig_stdout = sys.stdout
848 include_guard = "__included_%s" % (
855 print("#ifndef %s" % include_guard)
856 print("#define %s" % include_guard)
858 print("#include <stdlib.h>")
859 print("#include <stddef.h>")
860 print("#include <arpa/inet.h>")
861 print("#include <vapi/vapi_internal.h>")
862 print("#include <vapi/vapi.h>")
863 print("#include <vapi/vapi_dbg.h>")
865 print("#ifdef __cplusplus")
866 print('extern "C" {')
868 if name == "memclnt.api.vapi.h":
871 "static inline vapi_error_e vapi_send_with_control_ping "
872 "(vapi_ctx_t ctx, void * msg, u32 context);"
874 elif name == "vlib.api.vapi.h":
875 print("#include <vapi/memclnt.api.vapi.h>")
877 print("#include <vapi/vlib.api.vapi.h>")
879 for m in parser.messages_by_json[j].values():
880 print("extern vapi_msg_id_t %s;" % m.get_msg_id_name())
883 "#define DEFINE_VAPI_MSG_IDS_%s\\"
884 % f.replace(".", "_").replace("/", "_").replace("-", "_").upper()
889 " vapi_msg_id_t %s;" % m.get_msg_id_name()
890 for m in parser.messages_by_json[j].values()
897 for e in parser.enums_by_json[j]:
898 emit_definition(parser, j, emitted, e)
899 for u in parser.unions_by_json[j]:
900 emit_definition(parser, j, emitted, u)
901 for t in parser.types_by_json[j]:
902 emit_definition(parser, j, emitted, t)
903 for a in parser.aliases_by_json[j]:
904 emit_definition(parser, j, emitted, a)
905 for m in parser.messages_by_json[j].values():
906 emit_definition(parser, j, emitted, m)
910 if name == "vlib.api.vapi.h":
911 vapi_send_with_control_ping_function = """
912 static inline vapi_error_e
913 vapi_send_with_control_ping (vapi_ctx_t ctx, void *msg, u32 context)
915 vapi_msg_control_ping *ping = vapi_alloc_control_ping (ctx);
920 ping->header.context = context;
921 vapi_msg_control_ping_hton (ping);
922 return vapi_send2 (ctx, msg, ping);
925 print("%s" % vapi_send_with_control_ping_function)
928 print("#ifdef __cplusplus")
933 sys.stdout = orig_stdout
936 def json_to_c_header_name(json_name):
937 if json_name.endswith(".json"):
938 return "%s.vapi.h" % os.path.splitext(json_name)[0]
939 raise Exception("Unexpected json name `%s'!" % json_name)
942 def gen_c_unified_headers(parser, logger, prefix, remove_path):
943 if prefix == "" or prefix is None:
946 prefix = "%s/" % prefix
947 for j in parser.json_files:
949 d, f = os.path.split(j)
952 with open("%s%s" % (prefix, json_to_c_header_name(f)), "w") as io:
953 gen_json_unified_header(parser, logger, j, io, json_to_c_header_name(f))
956 if __name__ == "__main__":
958 verbose = int(os.getenv("V", 0))
969 logging.basicConfig(stream=sys.stdout, level=log_level)
970 logger = logging.getLogger("VAPI C GEN")
971 logger.setLevel(log_level)
973 argparser = argparse.ArgumentParser(description="VPP C API generator")
974 argparser.add_argument(
979 help="json api file" "(may be specified multiple times)",
981 argparser.add_argument("--prefix", action="store", default=None, help="path prefix")
982 argparser.add_argument(
983 "--remove-path", action="store_true", help="remove path from filename"
985 args = argparser.parse_args()
987 jsonparser = JsonParser(
990 simple_type_class=CSimpleType,
993 struct_type_class=CStructType,
995 message_class=CMessage,
999 # not using the model of having separate generated header and code files
1000 # with generated symbols present in shared library (per discussion with
1001 # Damjan), to avoid symbol version issues in .so
1002 # gen_c_headers_and_code(jsonparser, logger, args.prefix)
1004 gen_c_unified_headers(jsonparser, logger, args.prefix, args.remove_path)
1006 for e in jsonparser.exceptions: