aboutsummaryrefslogtreecommitdiffstats
path: root/src/vpp-api/vapi/vapi_c_gen.py
blob: 37f5ac12447660d00acbb08d59711696447c086c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
#!/usr/bin/env python3

import argparse
import inspect
import os
import sys
import logging
from vapi_json_parser import (
    Field,
    Struct,
    Enum,
    Union,
    Message,
    JsonParser,
    SimpleType,
    StructType,
    Alias,
)


class CField(Field):
    def get_c_name(self):
        return "vapi_type_%s" % self.name

    def get_c_def(self):
        if self.type.get_c_name() == "vl_api_string_t":
            if self.len:
                return "u8 %s[%d];" % (self.name, self.len)
            else:
                return "vl_api_string_t %s;" % (self.name)
        else:
            if self.len is not None and type(self.len) != dict:
                return "%s %s[%d];" % (self.type.get_c_name(), self.name, self.len)
            else:
                return "%s %s;" % (self.type.get_c_name(), self.name)

    def get_swap_to_be_code(self, struct, var):
        if self.len is not None and type(self.len) != dict:
            if self.len > 0:
                return (
                    "do { unsigned i; for (i = 0; i < %d; ++i) { %s } }"
                    " while(0);"
                    % (self.len, self.type.get_swap_to_be_code(struct, "%s[i]" % var))
                )
            else:
                if self.nelem_field.needs_byte_swap():
                    nelem_field = "%s(%s%s)" % (
                        self.nelem_field.type.get_swap_to_host_func_name(),
                        struct,
                        self.nelem_field.name,
                    )
                else:
                    nelem_field = "%s%s" % (struct, self.nelem_field.name)
                return (
                    "do { unsigned i; for (i = 0; i < %s; ++i) { %s } }"
                    " while(0);"
                    % (
                        nelem_field,
                        self.type.get_swap_to_be_code(struct, "%s[i]" % var),
                    )
                )
        return self.type.get_swap_to_be_code(struct, "%s" % var)

    def get_swap_to_host_code(self, struct, var):
        if self.len is not None and type(self.len) != dict:
            if self.len > 0:
                return (
                    "do { unsigned i; for (i = 0; i < %d; ++i) { %s } }"
                    " while(0);"
                    % (self.len, self.type.get_swap_to_host_code(struct, "%s[i]" % var))
                )
            else:
                # nelem_field already swapped to host here...
                return (
                    "do { unsigned i; for (i = 0; i < %s%s; ++i) { %s } }"
                    " while(0);"
                    % (
                        struct,
                        self.nelem_field.name,
                        self.type.get_swap_to_host_code(struct, "%s[i]" % var),
                    )
                )
        return self.type.get_swap_to_host_code(struct, "%s" % var)

    def needs_byte_swap(self):
        return self.type.needs_byte_swap()

    def get_vla_field_length_name(self, path):
        return "%s_%s_array_size" % ("_".join(path), self.name)

    def get_alloc_vla_param_names(self, path):
        if self.is_vla():
            result = [self.get_vla_field_length_name(path)]
        else:
            result = []
        if self.type.has_vla():
            t = self.type.get_alloc_vla_param_names(path + [self.name])
            result.extend(t)
        return result

    def get_vla_calc_size_code(self, prefix, path):
        if self.is_vla():
            result = [
                "sizeof(%s.%s[0]) * %s"
                % (
                    ".".join([prefix] + path),
                    self.name,
                    self.get_vla_field_length_name(path),
                )
            ]
        else:
            result = []
        if self.type.has_vla():
            t = self.type.get_vla_calc_size_code(prefix, path + [self.name])
            result.extend(t)
        return result

    def get_vla_assign_code(self, prefix, path):
        result = []
        if self.is_vla():
            result.append(
                "%s.%s = %s"
                % (
                    ".".join([prefix] + path),
                    self.nelem_field.name,
                    self.get_vla_field_length_name(path),
                )
            )
        if self.type.has_vla():
            t = self.type.get_vla_assign_code(prefix, path + [self.name])
            result.extend(t)
        return result


class CAlias(CField):
    def get_c_name(self):
        return "vapi_type_%s" % self.name

    def get_c_def(self):
        if self.len is not None:
            return "typedef %s vapi_type_%s[%d];" % (
                self.type.get_c_name(),
                self.name,
                self.len,
            )
        else:
            return "typedef %s vapi_type_%s;" % (self.type.get_c_name(), self.name)


class CStruct(Struct):
    def get_c_def(self):
        return "\n".join(
            [
                "typedef struct __attribute__((__packed__)) {\n%s"
                % ("\n".join(["  %s" % x.get_c_def() for x in self.fields])),
                "} %s;" % self.get_c_name(),
            ]
        )

    def get_vla_assign_code(self, prefix, path):
        return [
            x
            for f in self.fields
            if f.has_vla()
            for x in f.get_vla_assign_code(prefix, path)
        ]

    def get_alloc_vla_param_names(self, path):
        return [
            x
            for f in self.fields
            if f.has_vla()
            for x in f.get_alloc_vla_param_names(path)
        ]

    def get_vla_calc_size_code(self, prefix, path):
        return [
            x
            for f in self.fields
            if f.has_vla()
            for x in f.get_vla_calc_size_code(prefix, path)
        ]


class CSimpleType(SimpleType):
    swap_to_be_dict = {
        "i16": "htobe16",
        "u16": "htobe16",
        "i32": "htobe32",
        "u32": "htobe32",
        "i64": "htobe64",
        "u64": "htobe64",
    }

    swap_to_host_dict = {
        "i16": "be16toh",
        "u16": "be16toh",
        "i32": "be32toh",
        "u32": "be32toh",
        "i64": "be64toh",
        "u64": "be64toh",
    }

    __packed = "__attribute__((packed))"
    pack_dict = {
        "i8": __packed,
        "u8": __packed,
        "i16": __packed,
        "u16": __packed,
    }

    def get_c_name(self):
        return self.name

    def get_swap_to_be_func_name(self):
        return self.swap_to_be_dict[self.name]

    def get_swap_to_host_func_name(self):
        return self.swap_to_host_dict[self.name]

    def get_packed_string(self):
        return self.pack_dict[self.name]

    def get_swap_to_be_code(self, struct, var, cast=None):
        x = "%s%s" % (struct, var)
        return "%s = %s%s(%s);" % (
            x,
            "(%s)" % cast if cast else "",
            self.get_swap_to_be_func_name(),
            x,
        )

    def get_swap_to_host_code(self, struct, var, cast=None):
        x = "%s%s" % (struct, var)
        return "%s = %s%s(%s);" % (
            x,
            "(%s)" % cast if cast else "",
            self.get_swap_to_host_func_name(),
            x,
        )

    def needs_byte_swap(self):
        try:
            self.get_swap_to_host_func_name()
            return True
        except KeyError:
            pass
        return False

    def get_packed(self):
        return self.pack_dict.get(self.name, "")


class CEnum(Enum):
    def get_c_name(self):
        return "vapi_enum_%s" % self.name

    def get_c_def(self):
        return "typedef enum {\n%s\n} %s %s;" % (
            "\n".join(["  %s = %s," % (i, j) for i, j in self.value_pairs]),
            self.type.get_packed(),
            self.get_c_name(),
        )

    def needs_byte_swap(self):
        return self.type.needs_byte_swap()

    def get_swap_to_be_code(self, struct, var):
        return self.type.get_swap_to_be_code(struct, var, self.get_c_name())

    def get_swap_to_host_code(self, struct, var):
        return self.type.get_swap_to_host_code(struct, var, self.get_c_name())


class CUnion(Union):
    def get_c_name(self):
        return "vapi_union_%s" % self.name

    def get_c_def(self):
        return "typedef union {\n%s\n} %s;" % (
            "\n".join(["  %s %s;" % (i.get_c_name(), j) for i, j in self.type_pairs]),
            self.get_c_name(),
        )

    def needs_byte_swap(self):
        return False


class CStructType(StructType, CStruct):
    def get_c_name(self):
        return "vapi_type_%s" % self.name

    def get_swap_to_be_func_name(self):
        return "%s_hton" % self.get_c_name()

    def get_swap_to_host_func_name(self):
        return "%s_ntoh" % self.get_c_name()

    def get_swap_to_be_func_decl(self):
        return "void %s(%s *msg)" % (self.get_swap_to_be_func_name(), self.get_c_name())

    def get_swap_to_be_func_def(self):
        return "%s\n{\n%s\n}" % (
            self.get_swap_to_be_func_decl(),
            "\n".join(
                [
                    "  %s" % p.get_swap_to_be_code("msg->", "%s" % p.name)
                    for p in self.fields
                    if p.needs_byte_swap()
                ]
            ),
        )

    def get_swap_to_host_func_decl(self):
        return "void %s(%s *msg)" % (
            self.get_swap_to_host_func_name(),
            self.get_c_name(),
        )

    def get_swap_to_host_func_def(self):
        return "%s\n{\n%s\n}" % (
            self.get_swap_to_host_func_decl(),
            "\n".join(
                [
                    "  %s" % p.get_swap_to_host_code("msg->", "%s" % p.name)
                    for p in self.fields
                    if p.needs_byte_swap()
                ]
            ),
        )

    def get_swap_to_be_code(self, struct, var):
        return "%s(&%s%s);" % (self.get_swap_to_be_func_name(), struct, var)

    def get_swap_to_host_code(self, struct, var):
        return "%s(&%s%s);" % (self.get_swap_to_host_func_name(), struct, var)

    def needs_byte_swap(self):
        for f in self.fields:
            if f.needs_byte_swap():
                return True
        return False


class CMessage(Message):
    def __init__(self, logger, definition, json_parser):
        super(CMessage, self).__init__(logger, definition, json_parser)
        self.payload_members = [
            "  %s" % p.get_c_def() for p in self.fields if p.type != self.header
        ]

    def has_payload(self):
        return len(self.payload_members) > 0

    def get_msg_id_name(self):
        return "vapi_msg_id_%s" % self.name

    def get_c_name(self):
        return "vapi_msg_%s" % self.name

    def get_payload_struct_name(self):
        return "vapi_payload_%s" % self.name

    def get_alloc_func_name(self):
        return "vapi_alloc_%s" % self.name

    def get_alloc_vla_param_names(self):
        return [
            x
            for f in self.fields
            if f.has_vla()
            for x in f.get_alloc_vla_param_names([])
        ]

    def get_alloc_func_decl(self):
        return "%s* %s(struct vapi_ctx_s *ctx%s)" % (
            self.get_c_name(),
            self.get_alloc_func_name(),
            "".join([", size_t %s" % n for n in self.get_alloc_vla_param_names()]),
        )

    def get_alloc_func_def(self):
        extra = []
        if self.header.has_field("client_index"):
            extra.append("  msg->header.client_index = vapi_get_client_index(ctx);")
        if self.header.has_field("context"):
            extra.append("  msg->header.context = 0;")
        return "\n".join(
            [
                "%s" % self.get_alloc_func_decl(),
                "{",
                "  %s *msg = NULL;" % self.get_c_name(),
                "  const size_t size = sizeof(%s)%s;"
                % (
                    self.get_c_name(),
                    "".join(
                        [
                            " + %s" % x
                            for f in self.fields
                            if f.has_vla()
                            for x in f.get_vla_calc_size_code("msg->payload", [])
                        ]
                    ),
                ),
                "  /* cast here required to play nicely with C++ world ... */",
                "  msg = (%s*)vapi_msg_alloc(ctx, size);" % self.get_c_name(),
                "  if (!msg) {",
                "    return NULL;",
                "  }",
            ]
            + extra
            + [
                "  msg->header._vl_msg_id = vapi_lookup_vl_msg_id(ctx, %s);"
                % self.get_msg_id_name(),
                "".join(
                    [
                        "  %s;\n" % line
                        for f in self.fields
                        if f.has_vla()
                        for line in f.get_vla_assign_code("msg->payload", [])
                    ]
                ),
                "  return msg;",
                "}",
            ]
        )

    def get_calc_msg_size_func_name(self):
        return "vapi_calc_%s_msg_size" % self.name

    def get_calc_msg_size_func_decl(self):
        return "uword %s(%s *msg)" % (
            self.get_calc_msg_size_func_name(),
            self.get_c_name(),
        )

    def get_calc_msg_size_func_def(self):
        return "\n".join(
            [
                "%s" % self.get_calc_msg_size_func_decl(),
                "{",
                "  return sizeof(*msg)%s;"
                % "".join(
                    [
                        "+ msg->payload.%s * sizeof(msg->payload.%s[0])"
                        % (f.nelem_field.name, f.name)
                        for f in self.fields
                        if f.nelem_field is not None
                    ]
                ),
                "}",
            ]
        )

    def get_verify_msg_size_func_name(self):
        return f"vapi_verify_{self.name}_msg_size"

    def get_verify_msg_size_func_decl(self):
        return "int %s(%s *msg, uword buf_size)" % (
            self.get_verify_msg_size_func_name(),
            self.get_c_name(),
        )

    def get_verify_msg_size_func_def(self):
        return inspect.cleandoc(
            f"""
            {self.get_verify_msg_size_func_decl()}
            {{
              if (sizeof({self.get_c_name()}) > buf_size)
                {{
                  VAPI_ERR("Truncated '{self.name}' msg received, received %lu"
                    "bytes, expected %lu bytes.", buf_size,
                    sizeof({self.get_c_name()}));
                  return -1;
                }}
              if ({self.get_calc_msg_size_func_name()}(msg) > buf_size)
                {{
                  VAPI_ERR("Truncated '{self.name}' msg received, received %lu"
                    "bytes, expected %lu bytes.", buf_size,
                    {self.get_calc_msg_size_func_name()}(msg));
                  return -1;
                }}
              return 0;
            }}
        """
        )

    def get_c_def(self):
        if self.has_payload():
            return "\n".join(
                [
                    "typedef struct __attribute__ ((__packed__)) {",
                    "%s " % "\n".join(self.payload_members),
                    "} %s;" % self.get_payload_struct_name(),
                    "",
                    "typedef struct __attribute__ ((__packed__)) {",
                    (
                        "  %s %s;" % (self.header.get_c_name(), self.fields[0].name)
                        if self.header is not None
                        else ""
                    ),
                    "  %s payload;" % self.get_payload_struct_name(),
                    "} %s;" % self.get_c_name(),
                ]
            )
        else:
            return "\n".join(
                [
                    "typedef struct __attribute__ ((__packed__)) {",
                    (
                        "  %s %s;" % (self.header.get_c_name(), self.fields[0].name)
                        if self.header is not None
                        else ""
                    ),
                    "} %s;" % self.get_c_name(),
                ]
            )

    def get_swap_payload_to_host_func_name(self):
        return "%s_payload_ntoh" % self.get_c_name()

    def get_swap_payload_to_be_func_name(self):
        return "%s_payload_hton" % self.get_c_name()

    def get_swap_payload_to_host_func_decl(self):
        return "void %s(%s *payload)" % (
            self.get_swap_payload_to_host_func_name(),
            self.get_payload_struct_name(),
        )

    def get_swap_payload_to_be_func_decl(self):
        return "void %s(%s *payload)" % (
            self.get_swap_payload_to_be_func_name(),
            self.get_payload_struct_name(),
        )

    def get_swap_payload_to_be_func_def(self):
        return "%s\n{\n%s\n}" % (
            self.get_swap_payload_to_be_func_decl(),
            "\n".join(
                [
                    "  %s" % p.get_swap_to_be_code("payload->", "%s" % p.name)
                    for p in self.fields
                    if p.needs_byte_swap() and p.type != self.header
                ]
            ),
        )

    def get_swap_payload_to_host_func_def(self):
        return "%s\n{\n%s\n}" % (
            self.get_swap_payload_to_host_func_decl(),
            "\n".join(
                [
                    "  %s" % p.get_swap_to_host_code("payload->", "%s" % p.name)
                    for p in self.fields
                    if p.needs_byte_swap() and p.type != self.header
                ]
            ),
        )

    def get_swap_to_host_func_name(self):
        return "%s_ntoh" % self.get_c_name()

    def get_swap_to_be_func_name(self):
        return "%s_hton" % self.get_c_name()

    def get_swap_to_host_func_decl(self):
        return "void %s(%s *msg)" % (
            self.get_swap_to_host_func_name(),
            self.get_c_name(),
        )

    def get_swap_to_be_func_decl(self):
        return "void %s(%s *msg)" % (self.get_swap_to_be_func_name(), self.get_c_name())

    def get_swap_to_be_func_def(self):
        return "\n".join(
            [
                "%s" % self.get_swap_to_be_func_decl(),
                "{",
                (
                    '  VAPI_DBG("Swapping `%s\'@%%p to big endian", msg);'
                    % self.get_c_name()
                ),
                "  %s(&msg->header);" % self.header.get_swap_to_be_func_name()
                if self.header is not None
                else "",
                "  %s(&msg->payload);" % self.get_swap_payload_to_be_func_name()
                if self.has_payload()
                else "",
                "}",
            ]
        )

    def get_swap_to_host_func_def(self):
        return "\n".join(
            [
                "%s" % self.get_swap_to_host_func_decl(),
                "{",
                (
                    '  VAPI_DBG("Swapping `%s\'@%%p to host byte order", msg);'
                    % self.get_c_name()
                ),
                "  %s(&msg->header);" % self.header.get_swap_to_host_func_name()
                if self.header is not None
                else "",
                "  %s(&msg->payload);" % self.get_swap_payload_to_host_func_name()
                if self.has_payload()
                else "",
                "}",
            ]
        )

    def get_op_func_name(self):
        return "vapi_%s" % self.name

    def get_op_func_decl(self):
        stream_param_lines = []
        if self.has_stream_msg:
            stream_param_lines = [
                "vapi_error_e (*details_callback)(struct vapi_ctx_s *ctx",
                "                                 void *callback_ctx",
                "                                 vapi_error_e rv",
                "                                 bool is_last",
                "                                 %s *details)"
                % self.stream_msg.get_payload_struct_name(),
                "void *details_callback_ctx",
            ]

        return "vapi_error_e %s(%s)" % (
            self.get_op_func_name(),
            ",\n  ".join(
                [
                    "struct vapi_ctx_s *ctx",
                    "%s *msg" % self.get_c_name(),
                    "vapi_error_e (*reply_callback)(struct vapi_ctx_s *ctx",
                    "                               void *callback_ctx",
                    "                               vapi_error_e rv",
                    "                               bool is_last",
                    "                               %s *reply)"
                    % self.reply.get_payload_struct_name(),
                ]
                + [
                    "void *reply_callback_ctx",
                ]
                + stream_param_lines
            ),
        )

    def get_op_func_def(self):
        param_check_lines = ["  if (!msg || !reply_callback) {"]
        store_request_lines = [
            "    vapi_store_request(ctx, req_context, %s, %s, "
            % (
                self.reply.get_msg_id_name(),
                "VAPI_REQUEST_DUMP" if self.reply_is_stream else "VAPI_REQUEST_REG",
            ),
            "                       (vapi_cb_t)reply_callback, reply_callback_ctx);",
        ]
        if self.has_stream_msg:
            param_check_lines = [
                "  if (!msg || !reply_callback || !details_callback) {"
            ]
            store_request_lines = [
                f"    vapi_store_request(ctx, req_context, {self.stream_msg.get_msg_id_name()}, VAPI_REQUEST_STREAM, ",
                "                       (vapi_cb_t)details_callback, details_callback_ctx);",
                f"    vapi_store_request(ctx, req_context, {self.reply.get_msg_id_name()}, VAPI_REQUEST_REG, ",
                "                       (vapi_cb_t)reply_callback, reply_callback_ctx);",
            ]

        return "\n".join(
            [
                "%s" % self.get_op_func_decl(),
                "{",
            ]
            + param_check_lines
            + [
                "    return VAPI_EINVAL;",
                "  }",
                "  if (vapi_is_nonblocking(ctx) && vapi_requests_full(ctx)) {",
                "    return VAPI_EAGAIN;",
                "  }",
                "  vapi_error_e rv;",
                "  if (VAPI_OK != (rv = vapi_producer_lock (ctx))) {",
                "    return rv;",
                "  }",
                "  u32 req_context = vapi_gen_req_context(ctx);",
                "  msg->header.context = req_context;",
                "  %s(msg);" % self.get_swap_to_be_func_name(),
                (
                    "  if (VAPI_OK == (rv = vapi_send_with_control_ping "
                    "(ctx, msg, req_context))) {"
                    if (self.reply_is_stream and not self.has_stream_msg)
                    else "  if (VAPI_OK == (rv = vapi_send (ctx, msg))) {"
                ),
            ]
            + store_request_lines
            + [
                "    if (VAPI_OK != vapi_producer_unlock (ctx)) {",
                "      abort (); /* this really shouldn't happen */",
                "    }",
                "    if (vapi_is_nonblocking(ctx)) {",
                "      rv = VAPI_OK;",
                "    } else {",
                "      rv = vapi_dispatch(ctx);",
                "    }",
                "  } else {",
                "    %s(msg);" % self.get_swap_to_host_func_name(),
                "    if (VAPI_OK != vapi_producer_unlock (ctx)) {",
                "      abort (); /* this really shouldn't happen */",
                "    }",
                "  }",
                "  return rv;",
                "}",
                "",
            ]
        )

    def get_event_cb_func_decl(self):
        if not self.is_reply and not self.is_event:
            raise Exception("Cannot register event callback for non-reply message")
        if self.has_payload():
            return "\n".join(
                [
                    "void vapi_set_%s_event_cb (" % self.get_c_name(),
                    "  struct vapi_ctx_s *ctx, ",
                    (
                        "  vapi_error_e (*callback)(struct vapi_ctx_s *ctx, "
                        "void *callback_ctx, %s *payload),"
                        % self.get_payload_struct_name()
                    ),
                    "  void *callback_ctx)",
                ]
            )
        else:
            return "\n".join(
                [
                    "void vapi_set_%s_event_cb (" % self.get_c_name(),
                    "  struct vapi_ctx_s *ctx, ",
                    "  vapi_error_e (*callback)(struct vapi_ctx_s *ctx, "
                    "void *callback_ctx),",
                    "  void *callback_ctx)",
                ]
            )

    def get_event_cb_func_def(self):
        if not self.is_reply and not self.is_event:
            raise Exception("Cannot register event callback for non-reply function")
        return "\n".join(
            [
                "%s" % self.get_event_cb_func_decl(),
                "{",
                (
                    "  vapi_set_event_cb(ctx, %s, (vapi_event_cb)callback, "
                    "callback_ctx);" % self.get_msg_id_name()
                ),
                "}",
            ]
        )

    def get_c_metadata_struct_name(self):
        return "__vapi_metadata_%s" % self.name

    def get_c_constructor(self):
        has_context = False
        if self.header is not None:
            has_context = self.header.has_field("context")
        return "\n".join(
            [
                "static void __attribute__((constructor)) __vapi_constructor_%s()"
                % self.name,
                "{",
                '  static const char name[] = "%s";' % self.name,
                '  static const char name_with_crc[] = "%s_%s";'
                % (self.name, self.crc[2:]),
                "  static vapi_message_desc_t %s = {"
                % self.get_c_metadata_struct_name(),
                "    name,",
                "    sizeof(name) - 1,",
                "    name_with_crc,",
                "    sizeof(name_with_crc) - 1,",
                "    true," if has_context else "    false,",
                "    offsetof(%s, context)," % self.header.get_c_name()
                if has_context
                else "    0,",
                ("    offsetof(%s, payload)," % self.get_c_name())
                if self.has_payload()
                else "    VAPI_INVALID_MSG_ID,",
                "    (verify_msg_size_fn_t)%s," % self.get_verify_msg_size_func_name(),
                "    (generic_swap_fn_t)%s," % self.get_swap_to_be_func_name(),
                "    (generic_swap_fn_t)%s," % self.get_swap_to_host_func_name(),
                "    VAPI_INVALID_MSG_ID,",
                "  };",
                "",
                "  %s = vapi_register_msg(&%s);"
                % (self.get_msg_id_name(), self.get_c_metadata_struct_name()),
                '  VAPI_DBG("Assigned msg id %%d to %s", %s);'
                % (self.name, self.get_msg_id_name()),
                "}",
            ]
        )


def emit_definition(parser, json_file, emitted, o):
    if o in emitted:
        return
    if o.name in ("msg_header1_t", "msg_header2_t"):
        return
    if hasattr(o, "depends"):
        for x in o.depends:
            emit_definition(parser, json_file, emitted, x)
    if hasattr(o, "reply"):
        emit_definition(parser, json_file, emitted, o.reply)
    if hasattr(o, "stream_msg"):
        emit_definition(parser, json_file, emitted, o.stream_msg)
    if hasattr(o, "get_c_def"):
        if (
            o not in parser.enums_by_json[json_file]
            and o not in parser.types_by_json[json_file]
            and o not in parser.unions_by_json[json_file]
            and o.name not in parser.messages_by_json[json_file]
            and o not in parser.aliases_by_json[json_file]
        ):
            return
        guard = "defined_%s" % o.get_c_name()
        print("#ifndef %s" % guard)
        print("#define %s" % guard)
        print("%s" % o.get_c_def())
        print("")
        function_attrs = "static inline "
        if o.name in parser.messages_by_json[json_file]:
            if o.has_payload():
                print("%s%s" % (function_attrs, o.get_swap_payload_to_be_func_def()))
                print("")
                print("%s%s" % (function_attrs, o.get_swap_payload_to_host_func_def()))
                print("")
            print("%s%s" % (function_attrs, o.get_swap_to_be_func_def()))
            print("")
            print("%s%s" % (function_attrs, o.get_swap_to_host_func_def()))
            print("")
            print("%s%s" % (function_attrs, o.get_calc_msg_size_func_def()))
            print("")
            print("%s%s" % (function_attrs, o.get_verify_msg_size_func_def()))
            if not o.is_reply and not o.is_event and not o.is_stream:
                print("")
                print("%s%s" % (function_attrs, o.get_alloc_func_def()))
                print("")
                print("%s%s" % (function_attrs, o.get_op_func_def()))
            print("")
            print("%s" % o.get_c_constructor())
            if (o.is_reply or o.is_event) and not o.is_stream:
                print("")
                print("%s%s;" % (function_attrs, o.get_event_cb_func_def()))
        elif hasattr(o, "get_swap_to_be_func_def"):
            print("%s%s" % (function_attrs, o.get_swap_to_be_func_def()))
            print("")
            print("%s%s" % (function_attrs, o.get_swap_to_host_func_def()))
        print("#endif")
        print("")
    emitted.append(o)


def gen_json_unified_header(parser, logger, j, io, name):
    d, f = os.path.split(j)
    logger.info("Generating header `%s'" % name)
    orig_stdout = sys.stdout
    sys.stdout = io
    include_guard = "__included_%s" % (
        j.replace(".", "_")
        .replace("/", "_")
        .replace("-", "_")
        .replace("+", "_")
        .replace("@", "_")
    )
    print("#ifndef %s" % include_guard)
    print("#define %s" % include_guard)
    print("")
    print("#include <stdlib.h>")
    print("#include <stddef.h>")
    print("#include <arpa/inet.h>")
    print("#include <vapi/vapi_internal.h>")
    print("#include <vapi/vapi.h>")
    print("#include <vapi/vapi_dbg.h>")
    print("")
    print("#ifdef __cplusplus")
    print('extern "C" {')
    print("#endif")
    if name == "memclnt.api.vapi.h":
        print("")
        print(
            "static inline vapi_error_e vapi_send_with_control_ping "
            "(vapi_ctx_t ctx, void * msg, u32 context);"
        )
    elif name == "vlib.api.vapi.h":
        print("#include <vapi/memclnt.api.vapi.h>")
    else:
        print("#include <vapi/vlib.api.vapi.h>")
    print("")
    for m in parser.messages_by_json[j].values():
        print("extern vapi_msg_id_t %s;" % m.get_msg_id_name())
    print("")
    print(
        "#define DEFINE_VAPI_MSG_IDS_%s\\"
        % f.replace(".", "_").replace("/", "_").replace("-", "_").upper()
    )
    print(
        "\\\n".join(
            [
                "  vapi_msg_id_t %s;" % m.get_msg_id_name()
                for m in parser.messages_by_json[j].values()
            ]
        )
    )
    print("")
    print("")
    emitted = []
    for e in parser.enums_by_json[j]:
        emit_definition(parser, j, emitted, e)
    for u in parser.unions_by_json[j]:
        emit_definition(parser, j, emitted, u)
    for t in parser.types_by_json[j]:
        emit_definition(parser, j, emitted, t)
    for a in parser.aliases_by_json[j]:
        emit_definition(parser, j, emitted, a)
    for m in parser.messages_by_json[j].values():
        emit_definition(parser, j, emitted, m)

    print("")

    if name == "vlib.api.vapi.h":
        vapi_send_with_control_ping_function = """
static inline vapi_error_e
vapi_send_with_control_ping (vapi_ctx_t ctx, void *msg, u32 context)
{
  vapi_msg_control_ping *ping = vapi_alloc_control_ping (ctx);
  if (!ping)
    {
      return VAPI_ENOMEM;
    }
  ping->header.context = context;
  vapi_msg_control_ping_hton (ping);
  return vapi_send2 (ctx, msg, ping);
}
"""
        print("%s" % vapi_send_with_control_ping_function)
        print("")

    print("#ifdef __cplusplus")
    print("}")
    print("#endif")
    print("")
    print("#endif")
    sys.stdout = orig_stdout


def json_to_c_header_name(json_name):
    if json_name.endswith(".json"):
        return "%s.vapi.h" % os.path.splitext(json_name)[0]
    raise Exception("Unexpected json name `%s'!" % json_name)


def gen_c_unified_headers(parser, logger, prefix, remove_path):
    if prefix == "" or prefix is None:
        prefix = ""
    else:
        prefix = "%s/" % prefix
    for j in parser.json_files:
        if remove_path:
            d, f = os.path.split(j)
        else:
            f = j
        with open("%s%s" % (prefix, json_to_c_header_name(f)), "w") as io:
            gen_json_unified_header(parser, logger, j, io, json_to_c_header_name(f))


if __name__ == "__main__":
    try:
        verbose = int(os.getenv("V", 0))
    except:
        verbose = 0

    if verbose >= 2:
        log_level = 10
    elif verbose == 1:
        log_level = 20
    else:
        log_level = 40

    logging.basicConfig(stream=sys.stdout, level=log_level)
    logger = logging.getLogger("VAPI C GEN")
    logger.setLevel(log_level)

    argparser = argparse.ArgumentParser(description="VPP C API generator")
    argparser.add_argument(
        "files",
        metavar="api-file",
        action="append",
        type=str,
        help="json api file" "(may be specified multiple times)",
    )
    argparser.add_argument("--prefix", action="store", default=None, help="path prefix")
    argparser.add_argument(
        "--remove-path", action="store_true", help="remove path from filename"
    )
    args = argparser.parse_args()

    jsonparser = JsonParser(
        logger,
        args.files,
        simple_type_class=CSimpleType,
        enum_class=CEnum,
        union_class=CUnion,
        struct_type_class=CStructType,
        field_class=CField,
        message_class=CMessage,
        alias_class=CAlias,
    )

    # not using the model of having separate generated header and code files
    # with generated symbols present in shared library (per discussion with
    # Damjan), to avoid symbol version issues in .so
    # gen_c_headers_and_code(jsonparser, logger, args.prefix)

    gen_c_unified_headers(jsonparser, logger, args.prefix, args.remove_path)

    for e in jsonparser.exceptions:
        logger.warning(e)