aboutsummaryrefslogtreecommitdiffstats
path: root/extras/wireshark/packet-vpp.c
blob: 9584e7003e2c7dcc003787ac32e25388eb44c802 (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
/* packet-vpp.c
 * 
 * Routines for the disassembly of fd.io vpp project 
 * dispatch captures
 *
 * Copyright (c) 2018 Cisco and/or its affiliates.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 * This version is not to be upstreamed as-is, since it hooks up the
 * vpp dissector to WTAP_ENCAP_USER13, a test encap type.
 */

#include "config.h"

#include <epan/packet.h>
#include <epan/expert.h>
#include <epan/to_str.h>
#include <epan/in_cksum.h>
#include <epan/nlpid.h>
#include <epan/etypes.h>
#include <stdio.h>
#include <wsutil/ws_printf.h>

void proto_register_vpp(void);
void proto_reg_handoff_vpp(void);

static int proto_vpp = -1;
static int proto_vpp_opaque = -1;
static int proto_vpp_trace = -1;
static int hf_vpp_nodename = -1;
static int hf_vpp_buffer_index = -1;

static int hf_vpp_buffer_current_data = -1;
static int hf_vpp_buffer_current_length = -1;

static int hf_vpp_buffer_flags = -1;
static int hf_vpp_buffer_flag_non_default_freelist = -1;
static int hf_vpp_buffer_flag_traced = -1;
static int hf_vpp_buffer_flag_next_present = -1;
static int hf_vpp_buffer_flag_total_length_valid = -1;
static int hf_vpp_buffer_flag_ext_hdr_valid = -1;       
static int hf_vpp_buffer_flag_l4_checksum_computed = -1;
static int hf_vpp_buffer_flag_l4_checksum_correct = -1;
static int hf_vpp_buffer_flag_vlan_2_deep = -1;
static int hf_vpp_buffer_flag_vlan_1_deep = -1;
static int hf_vpp_buffer_flag_span_clone = -1;
static int hf_vpp_buffer_flag_loop_counter_valid = -1;
static int hf_vpp_buffer_flag_locally_originated = -1;
static int hf_vpp_buffer_flag_is_ip4 = -1;
static int hf_vpp_buffer_flag_is_ip6 = -1;
static int hf_vpp_buffer_flag_offload_ip_checksum = -1;
static int hf_vpp_buffer_flag_offload_tcp_checksum = -1;
static int hf_vpp_buffer_flag_offload_udp_checksum = -1;
static int hf_vpp_buffer_flag_is_natted = -1;
static int hf_vpp_buffer_flag_l2_hdr_offset_valid = -1;
static int hf_vpp_buffer_flag_l3_hdr_offset_valid = -1;
static int hf_vpp_buffer_flag_l4_hdr_offset_valid = -1;
static int hf_vpp_buffer_flag_flow_report = -1;
static int hf_vpp_buffer_flag_is_dvr = -1;
static int hf_vpp_buffer_flag_qos_data_valid = -1;
static int hf_vpp_buffer_flow_id = -1;
static int hf_vpp_buffer_next_buffer = -1;
static int hf_vpp_buffer_current_config_index = -1;
static int hf_vpp_buffer_error_index = -1;
static int hf_vpp_buffer_n_add_refs = -1;
static int hf_vpp_buffer_buffer_pool_index = -1;

static int hf_vpp_buffer_opaque_raw = -1;
static int hf_vpp_buffer_opaque_opaque = -1;

static int hf_vpp_buffer_trace = -1;

static gint ett_vpp = -1;
static gint ett_vpp_opaque = -1;
static gint ett_vpp_trace = -1;

static dissector_handle_t vpp_dissector_handle;
static dissector_handle_t vpp_opaque_dissector_handle;
static dissector_handle_t vpp_trace_dissector_handle;

static dissector_table_t vpp_subdissector_table;

/* List of next dissector names that we know about */
#define foreach_next_dissector                  \
_(eth_maybefcs)                                 \
_(ip)                                           \
_(ipv6)                                         \
_(udp)

#define _(a) static dissector_handle_t a##_dissector_handle;
foreach_next_dissector;
#undef _

/* 
 * node-name, next dissector name pairs
 * 
 * Unfortunately, -Werror=c++-compat causes
 * the node names "ipX-not-enabled" to throw a shoe if
 * not explicitly quoted. Never mind that the first macro 
 * arg is [only] used as a string (#xxx). 
 */

#define foreach_node_to_dissector_pair          \
_("ip6-lookup", ipv6)                           \
_("ip4-input", ip)                              \
_("ip4-not-enabled", ip)                        \
_("ip4-input-no-checksum", ip)                  \
_("ip4-lookup", ip)                             \
_("ip4-local", ip)                              \
_("ip4-udp-lookup", udp)                        \
_("ip4-icmp-error", ip)                         \
_("ip4-glean", ip)                              \
_("ethernet-input", eth_maybefcs)

static void
add_multi_line_string_to_tree(proto_tree *tree, tvbuff_t *tvb, gint start,
  gint len, int hf)
{
    gint next;
    int  line_len;
    int  data_len;

    while (len > 0) {
        line_len = tvb_find_line_end(tvb, start, len, &next, FALSE);
        data_len = next - start;
        proto_tree_add_string(tree, hf, tvb, start, data_len, 
                              tvb_format_stringzpad(tvb, start, line_len));
        start += data_len;
        len   -= data_len;
    }
}

static int
dissect_vpp_trace (tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, 
                    void* data _U_)
{
    int         offset   = 0;
    proto_item *ti;
    proto_tree *trace_tree;
    gint trace_string_length;
    
    col_set_str(pinfo->cinfo, COL_PROTOCOL, "VPP-Trace");
    col_clear(pinfo->cinfo, COL_INFO);

    ti = proto_tree_add_item(tree, proto_vpp_trace, tvb, offset, -1, ENC_NA);
    trace_tree = proto_item_add_subtree(ti, ett_vpp_trace);

    /* How long is the trace string? */
    (void ) tvb_get_stringz_enc (wmem_packet_scope(), tvb, 0,
                                 &trace_string_length, ENC_ASCII);
    
    add_multi_line_string_to_tree (trace_tree, tvb, 0,
                                   trace_string_length, 
                                   hf_vpp_buffer_trace);
    return tvb_captured_length(tvb);
}

/*
 * BIG FAT WARNING: it's impossible to #include the vpp header files,
 * so this is a private copy of .../src/vnet/buffer.h, with
 * some vpp typedefs thrown in for good measure.
 */

typedef unsigned int u32;
typedef unsigned short int u16;
typedef short int i16;
typedef unsigned char u8;
typedef unsigned long long u64;

typedef struct 
{
    u32 sw_if_index[2];
    i16 l2_hdr_offset;
    i16 l3_hdr_offset;
    i16 l4_hdr_offset;
    u8 feature_arc_index;
    u8 dont_waste_me;

    union
    {
        /* IP4/6 buffer opaque. */
        struct
        {
            /* Adjacency from destination IP address lookup [VLIB_TX].
               Adjacency from source IP address lookup [VLIB_RX].
               This gets set to ~0 until source lookup is performed. */
            u32 adj_index[2];

            union
            {
                struct
                {
                    /* Flow hash value for this packet computed from IP src/dst address
                       protocol and ports. */
                    u32 flow_hash;

                    union
                    {
                        /* next protocol */
                        u32 save_protocol;

                        /* Hint for transport protocols */
                        u32 fib_index;
                    };

                    /* Rewrite length */
                    u32 save_rewrite_length;

                    /* MFIB RPF ID */
                    u32 rpf_id;
                };

                /* ICMP */
                struct
                {
                    u8 type;
                    u8 code;
                    u32 data;
                } icmp;

                /* reassembly */
                union
                {
                    /* in/out variables */
                    struct
                    {
                        u32 next_index;	/* index of next node - ignored if "feature" node */
                        u16 estimated_mtu;	/* estimated MTU calculated during reassembly */
                    };
                    /* internal variables used during reassembly */
                    struct
                    {
                        u16 fragment_first;
                        u16 fragment_last;
                        u16 range_first;
                        u16 range_last;
                        u32 next_range_bi;
                        u16 ip6_frag_hdr_offset;
                    };
                } reass;
            };

        } ip;

        /*
         * MPLS:
         * data copied from the MPLS header that was popped from the packet
         * during the look-up.
         */
        struct
        {
            /* do not overlay w/ ip.adj_index[0,1] nor flow hash */
            u32 pad[3];
            u8 ttl;
            u8 exp;
            u8 first;
            /* Rewrite length */
            u32 save_rewrite_length;
            /*
             * BIER - the number of bytes in the header.
             *  the len field in the header is not authoritative. It's the
             * value in the table that counts.
             */
            struct
            {
                u8 n_bytes;
            } bier;
        } mpls;

        /* l2 bridging path, only valid there */
        struct opaque_l2
        {
            u32 feature_bitmap;
            u16 bd_index;		/* bridge-domain index */
            u8 l2_len;		/* ethernet header length */
            u8 shg;			/* split-horizon group */
            u16 l2fib_sn;		/* l2fib bd/int seq_num */
            u8 bd_age;		/* aging enabled */
        } l2;

        /* l2tpv3 softwire encap, only valid there */
        struct
        {
            u32 pad[4];		/* do not overlay w/ ip.adj_index[0,1] */
            u8 next_index;
            u32 session_index;
        } l2t;

        /* L2 classify */
        struct
        {
            struct opaque_l2 pad;
            union
            {
                u32 table_index;
                u32 opaque_index;
            };
            u64 hash;
        } l2_classify;

        /* vnet policer */
        struct
        {
            u32 pad[8 - 2 - 1];	/* to end of opaque */
            u32 index;
        } policer;

        /* interface output features */
        struct
        {
            u32 flags;
            u32 sad_index;
        } ipsec;

        /* MAP */
        struct
        {
            u16 mtu;
        } map;

        /* MAP-T */
        struct
        {
            u32 map_domain_index;
            struct
            {
                u32 saddr, daddr;
                u16 frag_offset;	//Fragmentation header offset
                u16 l4_offset;		//L4 header overall offset
                u8 l4_protocol;		//The final protocol number
            } v6;			//Used by ip6_map_t only
            u16 checksum_offset;	//L4 checksum overall offset
            u16 mtu;			//Exit MTU
        } map_t;

        /* IP Fragmentation */
        struct
        {
            u32 pad[2];		/* do not overlay w/ ip.adj_index[0,1] */
            u16 mtu;
            u8 next_index;
            u8 flags;			//See ip_frag.h
        } ip_frag;

        /* COP - configurable junk filter(s) */
        struct
        {
            /* Current configuration index. */
            u32 current_config_index;
        } cop;

        /* LISP */
        struct
        {
            /* overlay address family */
            u16 overlay_afi;
        } lisp;

        /* TCP */
        struct
        {
            u32 connection_index;
            u32 seq_number;
            u32 seq_end;
            u32 ack_number;
            u16 hdr_offset;		/**< offset relative to ip hdr */
            u16 data_offset;		/**< offset relative to ip hdr */
            u16 data_len;		/**< data len */
            u8 flags;
        } tcp;

        /* SCTP */
        struct
        {
            u32 connection_index;
            u16 sid; /**< Stream ID */
            u16 ssn; /**< Stream Sequence Number */
            u32 tsn; /**< Transmission Sequence Number */
            u16 hdr_offset;		/**< offset relative to ip hdr */
            u16 data_offset;		/**< offset relative to ip hdr */
            u16 data_len;		/**< data len */
            u8 subconn_idx; /**< index of the sub_connection being used */
            u8 flags;
        } sctp;

        /* SNAT */
        struct
        {
            u32 flags;
        } snat;

        u32 unused[6];
    };
} vnet_buffer_opaque_t;


#define PTAS proto_tree_add_string(opaque_tree,                         \
                                   hf_vpp_buffer_opaque_opaque,         \
                                   tvb, 0, strlen(tmpbuf), tmpbuf)

static int
dissect_vpp_opaque (tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, 
                    void* data _U_)
{
    int         offset   = 0;
    proto_item *ti;
    proto_tree *opaque_tree;
    char tmpbuf [512];
    int print_offset;
    guint32 opaque[10];
    vnet_buffer_opaque_t _o, *o = &_o;

    int i;

    col_set_str(pinfo->cinfo, COL_PROTOCOL, "VPP-Opaque");
    col_clear(pinfo->cinfo, COL_INFO);

    ti = proto_tree_add_item(tree, proto_vpp_opaque, tvb, offset, -1, ENC_NA);
    opaque_tree = proto_item_add_subtree(ti, ett_vpp_opaque);

    print_offset = 0;
    for (i = 0; i < 10; i++) {
        opaque[i] = tvb_get_guint32 (tvb, offset + 4*i, ENC_LITTLE_ENDIAN);
        snprintf (tmpbuf + print_offset, sizeof(tmpbuf) - print_offset, 
                  "%08x ", opaque[i]);
        print_offset = strlen (tmpbuf);
    }
    offset += 40;

    proto_tree_add_string (opaque_tree, hf_vpp_buffer_opaque_raw, tvb, 0,
                           strlen(tmpbuf), tmpbuf);

    memset (o, 0, sizeof (*o));
    memcpy (o, opaque, sizeof (opaque));
 
    snprintf (tmpbuf, sizeof(tmpbuf), 
              "sw_if_index[VLIB_RX]: %d, sw_if_index[VLIB_TX]: %d",
              o->sw_if_index[0], o->sw_if_index[1]);
    PTAS;

    snprintf (tmpbuf, sizeof(tmpbuf),
              "L2 offset %d, L3 offset %d, L4 offset %d, feature arc index %d",
              (u32)(o->l2_hdr_offset),
              (u32)(o->l3_hdr_offset),
              (u32)(o->l4_hdr_offset), 
              (u32)(o->feature_arc_index));
    PTAS;

    snprintf (tmpbuf, sizeof(tmpbuf), 
              "ip.adj_index[VLIB_RX]: %d, ip.adj_index[VLIB_TX]: %d",
              (u32)(o->ip.adj_index[0]),
              (u32)(o->ip.adj_index[1]));
    PTAS;

    snprintf (tmpbuf, sizeof(tmpbuf), 
              "ip.flow_hash: 0x%x, ip.save_protocol: 0x%x, ip.fib_index: %d",
              o->ip.flow_hash, o->ip.save_protocol, o->ip.fib_index);
    PTAS;

    snprintf (tmpbuf, sizeof(tmpbuf), 
              "ip.save_rewrite_length: %d, ip.rpf_id: %d",
              o->ip.save_rewrite_length, o->ip.rpf_id);
    PTAS;

    snprintf (tmpbuf, sizeof(tmpbuf),
              "ip.icmp.type: %d ip.icmp.code: %d, ip.icmp.data: 0x%x",
              (u32)(o->ip.icmp.type),
              (u32)(o->ip.icmp.code),
              o->ip.icmp.data);
    PTAS;

    snprintf (tmpbuf, sizeof(tmpbuf),
              "ip.reass.next_index: %d, ip.reass.estimated_mtu: %d",
              o->ip.reass.next_index, (u32)(o->ip.reass.estimated_mtu));
    PTAS;
    snprintf (tmpbuf, sizeof(tmpbuf),
              "ip.reass.fragment_first: %d ip.reass.fragment_last: %d",
              (u32)(o->ip.reass.fragment_first),
              (u32)(o->ip.reass.fragment_last));
    PTAS;
    snprintf (tmpbuf, sizeof(tmpbuf),
              "ip.reass.range_first: %d ip.reass.range_last: %d",
              (u32)(o->ip.reass.range_first),
              (u32)(o->ip.reass.range_last));
    PTAS;

    snprintf (tmpbuf, sizeof(tmpbuf),
              "ip.reass.next_range_bi: 0x%x, ip.reass.ip6_frag_hdr_offset: %d",
              o->ip.reass.next_range_bi, 
              (u32)(o->ip.reass.ip6_frag_hdr_offset));
    PTAS;

    snprintf (tmpbuf, sizeof(tmpbuf),
              "mpls.ttl: %d, mpls.exp: %d, mpls.first: %d, "
              "mpls.save_rewrite_length: %d, mpls.bier.n_bytes: %d",
              (u32)(o->mpls.ttl), (u32)(o->mpls.exp), (u32)(o->mpls.first),
              o->mpls.save_rewrite_length, (u32)(o->mpls.bier.n_bytes));
    PTAS;

    snprintf (tmpbuf, sizeof(tmpbuf),
              "l2.feature_bitmap: %08x, l2.bd_index: %d, l2.l2_len: %d, "
              "l2.shg: %d, l2.l2fib_sn: %d, l2.bd_age: %d",
              o->l2.feature_bitmap, (u32)(o->l2.bd_index),
              (u32)(o->l2.l2_len), (u32)(o->l2.shg), (u32)(o->l2.l2fib_sn),
              (u32)(o->l2.bd_age));
    PTAS;
        
    snprintf (tmpbuf, sizeof(tmpbuf),
              "l2t.next_index: %d, l2t.session_index: %d",
              (u32)(o->l2t.next_index), o->l2t.session_index);
    PTAS;

    snprintf (tmpbuf, sizeof(tmpbuf),
              "l2_classify.table_index: %d, l2_classify.opaque_index: %d, "
              "l2_classify.hash: 0x%llx",
              o->l2_classify.table_index,
              o->l2_classify.opaque_index,
              o->l2_classify.hash);
    PTAS;

    snprintf (tmpbuf, sizeof(tmpbuf),
              "policer.index: %d", o->policer.index);
    PTAS;

    snprintf (tmpbuf, sizeof(tmpbuf),
              "ipsec.flags: 0x%x, ipsec.sad_index: %d",
              o->ipsec.flags, o->ipsec.sad_index);
    PTAS;

    snprintf (tmpbuf, sizeof(tmpbuf),
              "map.mtu: %d", (u32)(o->map.mtu));
    PTAS;

    snprintf (tmpbuf, sizeof(tmpbuf),
              "map_t.v6.saddr: 0x%x, map_t.v6.daddr: 0x%x, "
              "map_t.v6.frag_offset: %d, map_t.v6.l4_offset: %d",
              o->map_t.v6.saddr,
              o->map_t.v6.daddr,
              (u32)(o->map_t.v6.frag_offset),
              (u32)(o->map_t.v6.l4_offset));
    PTAS;
    snprintf (tmpbuf, sizeof(tmpbuf),
              "map_t.v6.l4_protocol: %d, map_t.checksum_offset: %d, "
              "map_t.mtu: %d",
              (u32)(o->map_t.v6.l4_protocol),
              (u32)(o->map_t.checksum_offset),
              (u32)(o->map_t.mtu));
    PTAS;
    
    snprintf (tmpbuf, sizeof(tmpbuf),
              "ip_frag.mtu: %d, ip_frag.next_index: %d, ip_frag.flags: 0x%x",
              (u32)(o->ip_frag.mtu),
              (u32)(o->ip_frag.next_index),
              (u32)(o->ip_frag.flags));
    PTAS;

    snprintf (tmpbuf, sizeof(tmpbuf),
              "cop.current_config_index: %d",
              o->cop.current_config_index);
    PTAS;
    
    snprintf (tmpbuf, sizeof(tmpbuf),
              "lisp.overlay_afi: %d",
              (u32)(o->lisp.overlay_afi));
    PTAS;

    snprintf (tmpbuf, sizeof(tmpbuf),
              "tcp.connection_index: %d, tcp.seq_number: %d, tcp.seq_end: %d, "
              "tcp.ack_number: %d, tcp.hdr_offset: %d, tcp.data_offset: %d",
              o->tcp.connection_index,
              o->tcp.seq_number,
              o->tcp.seq_end,
              o->tcp.ack_number,
              (u32)(o->tcp.hdr_offset),
              (u32)(o->tcp.data_offset));
    PTAS;

    snprintf (tmpbuf, sizeof(tmpbuf),
              "tcp.data_len: %d, tcp.flags: 0x%x",
              (u32)(o->tcp.data_len),
              (u32)(o->tcp.flags));
    PTAS;

    snprintf (tmpbuf, sizeof(tmpbuf),
              "sctp.connection_index: %d, sctp.sid: %d, sctp.ssn: %d, "
              "sctp.tsn: %d, sctp.hdr_offset: %d",
              o->sctp.connection_index,
              (u32)(o->sctp.sid),
              (u32)(o->sctp.ssn),
              (u32)(o->sctp.tsn),
              (u32)(o->sctp.hdr_offset));
    PTAS;
    snprintf (tmpbuf, sizeof(tmpbuf),
              "sctp.data_offset: %d, sctp.data_len: %d, sctp.subconn_idx: %d, "
              "sctp.flags: 0x%x",
              (u32)(o->sctp.data_offset),
              (u32)(o->sctp.data_len),
              (u32)(o->sctp.subconn_idx),
              (u32)(o->sctp.flags));
    PTAS;
              
    snprintf (tmpbuf, sizeof(tmpbuf),
              "snat.flags: 0x%x",
              o->snat.flags);
    PTAS;

    return tvb_captured_length(tvb);
}


static int
dissect_vpp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void* data _U_)
{
    proto_item *ti;
    proto_tree *vpp_tree;
    tvbuff_t *opaque_tvb, *eth_tvb, *trace_tvb;
    int         offset   = 0;
    guint8 major_version, minor_version;
    guint8 name_length;
    guint8 *name;
    guint16 trace_length;
    int i;
    static const int *buffer_flags[] = {
        &hf_vpp_buffer_flag_non_default_freelist,
        &hf_vpp_buffer_flag_traced,
        &hf_vpp_buffer_flag_next_present,
        &hf_vpp_buffer_flag_total_length_valid,
        &hf_vpp_buffer_flag_ext_hdr_valid,
        &hf_vpp_buffer_flag_l4_checksum_computed,
        &hf_vpp_buffer_flag_l4_checksum_correct,
        &hf_vpp_buffer_flag_vlan_2_deep,
        &hf_vpp_buffer_flag_vlan_1_deep,
        &hf_vpp_buffer_flag_span_clone,
        &hf_vpp_buffer_flag_loop_counter_valid,
        &hf_vpp_buffer_flag_locally_originated,
        &hf_vpp_buffer_flag_is_ip4,
        &hf_vpp_buffer_flag_is_ip6,
        &hf_vpp_buffer_flag_offload_ip_checksum,
        &hf_vpp_buffer_flag_offload_tcp_checksum,
        &hf_vpp_buffer_flag_offload_udp_checksum,
        &hf_vpp_buffer_flag_is_natted,
        &hf_vpp_buffer_flag_l2_hdr_offset_valid,
        &hf_vpp_buffer_flag_l3_hdr_offset_valid,
        &hf_vpp_buffer_flag_l4_hdr_offset_valid,
        &hf_vpp_buffer_flag_flow_report,
        &hf_vpp_buffer_flag_is_dvr,
        &hf_vpp_buffer_flag_qos_data_valid,
        NULL
    };

    col_set_str(pinfo->cinfo, COL_PROTOCOL, "VPP");
    col_clear(pinfo->cinfo, COL_INFO);

    ti = proto_tree_add_item(tree, proto_vpp, tvb, offset, -1, ENC_NA);
    vpp_tree = proto_item_add_subtree(ti, ett_vpp);

    major_version = tvb_get_guint8 (tvb, offset);
    offset++;

    minor_version = tvb_get_guint8 (tvb, offset);
    offset++;

    if (major_version != 1 || minor_version != 0)
        ws_debug_printf ("WARNING: version mismatch (%d, %d)",
                         major_version, minor_version);

    /* Skip the buffer index */
    offset += 4;

    /* Recover the node name */
    name_length = tvb_get_guint8 (tvb, offset);
    offset++;

    name = (guint8 *) g_malloc (name_length + 1);
    for (i = 0; i < name_length; i++, offset++) {
        name[i] = tvb_get_guint8 (tvb, offset);
    }
    name[i] = 0;
    offset++;

    proto_tree_add_string(vpp_tree, hf_vpp_nodename, tvb, 5, name_length,
                          name);
    proto_tree_add_item(vpp_tree, hf_vpp_buffer_index, tvb,
                        0 /* bi at offset 0 */, 4, ENC_LITTLE_ENDIAN);

    proto_tree_add_item(vpp_tree, hf_vpp_buffer_current_data, tvb,
                        offset, 2, ENC_LITTLE_ENDIAN);
    offset += 2;
    proto_tree_add_item(vpp_tree, hf_vpp_buffer_current_length, tvb,
                        offset, 2, ENC_LITTLE_ENDIAN);
    offset += 2;

    proto_tree_add_bitmask(vpp_tree, tvb, offset,
                           hf_vpp_buffer_flags, ett_vpp,
                           buffer_flags, ENC_LITTLE_ENDIAN);
    offset += 4;

    proto_tree_add_item(vpp_tree, hf_vpp_buffer_flow_id, tvb,
                        offset, 4, ENC_LITTLE_ENDIAN);
    offset += 4;

    proto_tree_add_item(vpp_tree, hf_vpp_buffer_next_buffer, tvb,
                        offset, 4, ENC_LITTLE_ENDIAN);
    offset += 4;

    proto_tree_add_item(vpp_tree, hf_vpp_buffer_current_config_index, tvb,
                        offset, 4, ENC_LITTLE_ENDIAN);
    offset += 4;

    proto_tree_add_item(vpp_tree, hf_vpp_buffer_error_index, tvb,
                        offset, 2, ENC_LITTLE_ENDIAN);
    offset += 2;

    proto_tree_add_item(vpp_tree, hf_vpp_buffer_n_add_refs, tvb,
                        offset, 1, ENC_LITTLE_ENDIAN);
    offset += 1;

    proto_tree_add_item(vpp_tree, hf_vpp_buffer_buffer_pool_index, tvb,
                        offset, 1, ENC_LITTLE_ENDIAN);
    offset += 1;
    
    opaque_tvb = tvb_new_subset_remaining (tvb, offset);
    call_dissector (vpp_opaque_dissector_handle, opaque_tvb, pinfo, tree);
    
    /* Skip opaque */
    offset += 40;
    /* skip second opaque line */
    offset += 64; 

    trace_length = tvb_get_guint16 (tvb, offset, ENC_LITTLE_ENDIAN);
    offset += 2;

    if (trace_length > 0) {
        trace_tvb = tvb_new_subset_remaining (tvb, offset);
        offset += trace_length;

        call_dissector (vpp_trace_dissector_handle, trace_tvb, pinfo, tree);
    }

    eth_tvb = tvb_new_subset_remaining (tvb, offset);
    
    /* 
     * Delegate the rest of the packet dissection to the per-node
     * next dissector in the foreach_node_to_dissector_pair list
     *
     * Failing that, pretend its an ethernet packet
     */ 
    if (!dissector_try_string (vpp_subdissector_table, name, eth_tvb,
                               pinfo, tree, NULL))
        call_dissector (eth_maybefcs_dissector_handle, eth_tvb, pinfo, tree);

    g_free (name);
    return tvb_captured_length(tvb);
}

void
proto_register_vpp(void)
{
  static hf_register_info hf[] = {
      { &hf_vpp_buffer_index,
        { "BufferIndex", "vpp.bufferindex",  FT_UINT32, BASE_HEX, NULL, 0x0,
          NULL, HFILL },
      },
      { &hf_vpp_nodename,
        { "NodeName", "vpp.nodename",  FT_STRINGZ, BASE_NONE, NULL, 0x0,
          NULL, HFILL },
      },
      { &hf_vpp_buffer_current_data,
        { "CurrentData", "vpp.current_data", FT_INT16, BASE_DEC, NULL, 0x0,
          NULL, HFILL },
      },
      { &hf_vpp_buffer_current_length,
        { "CurrentLength", "vpp.current_length", FT_UINT16, BASE_DEC, NULL, 0x0,
          NULL, HFILL },
      },

      /* 
       * Warning: buffer flag bits are not cast in concrete, and it's
       * impossible to imagine trying to compile WS with even a subset 
       * of the actual header files. 
       * 
       * See .../src/vlib/buffer.h, .../src/vnet/buffer.h in
       * the fd.io vpp source tree.
       */

      { &hf_vpp_buffer_flags,
        { "BufferFlags", "vpp.flags", FT_UINT32, BASE_HEX, NULL, 0x0,
          NULL, HFILL },
      },

      { &hf_vpp_buffer_flag_non_default_freelist,
        { "NonDefaultFreelist", "vpp.flags.non_default_freelist",
          FT_BOOLEAN, 32, TFS(&tfs_true_false), 0x1, NULL, HFILL },
      },
      { &hf_vpp_buffer_flag_traced,
        { "Traced", "vpp.flags.traced",
          FT_BOOLEAN, 32, TFS(&tfs_true_false), 0x2, NULL, HFILL },
      },
      { &hf_vpp_buffer_flag_next_present,
        { "NextPresent", "vpp.flags.next_present",
          FT_BOOLEAN, 32, TFS(&tfs_true_false), 0x4, NULL, HFILL },
      },
      { &hf_vpp_buffer_flag_total_length_valid,
        { "TotalLengthValid", "vpp.flags.total_length_valid",
          FT_BOOLEAN, 32, TFS(&tfs_true_false), 0x8, NULL, HFILL },
      },
      { &hf_vpp_buffer_flag_ext_hdr_valid,
        { "ExtHeaderValid", "vpp.flags.ext_hdr_valid",
          FT_BOOLEAN, 32, TFS(&tfs_true_false), 0x10, NULL, HFILL },
      },

      { &hf_vpp_buffer_flag_l4_checksum_computed,
        { "L4ChecksumComputed", "vpp.flags.l4_checksum_computed",
          FT_BOOLEAN, 32, TFS(&tfs_true_false), 0x80000000, NULL, HFILL },
      },
      { &hf_vpp_buffer_flag_l4_checksum_correct,
        { "L4ChecksumCorrect", "vpp.flags.l4_checksum_correct",
          FT_BOOLEAN, 32, TFS(&tfs_true_false), 0x40000000, NULL, HFILL },
      },
      { &hf_vpp_buffer_flag_vlan_2_deep,
        { "Vlan2Deep", "vpp.flags.vlan_2_deep",
          FT_BOOLEAN, 32, TFS(&tfs_true_false), 0x20000000, NULL, HFILL },
      },
      { &hf_vpp_buffer_flag_vlan_1_deep,
        { "Vlan1Deep", "vpp.flags.vlan_1_deep",
          FT_BOOLEAN, 32, TFS(&tfs_true_false), 0x10000000, NULL, HFILL },
      },
      { &hf_vpp_buffer_flag_span_clone,
        { "SpanClone", "vpp.flags.span_clone",
          FT_BOOLEAN, 32, TFS(&tfs_true_false), 0x08000000, NULL, HFILL },
      },
      { &hf_vpp_buffer_flag_loop_counter_valid,
        { "LoopCounterValid", "vpp.flags.loop_counter_valid",
          FT_BOOLEAN, 32, TFS(&tfs_true_false), 0x04000000, NULL, HFILL },
      },
      { &hf_vpp_buffer_flag_locally_originated,
        { "LocallyOriginated", "vpp.flags.locally_originated",
          FT_BOOLEAN, 32, TFS(&tfs_true_false), 0x02000000, NULL, HFILL },
      },
      { &hf_vpp_buffer_flag_is_ip4,
        { "IsIP4", "vpp.flags.is_ip4",
          FT_BOOLEAN, 32, TFS(&tfs_true_false), 0x01000000, NULL, HFILL },
      },
      { &hf_vpp_buffer_flag_is_ip6,
        { "IsIP4", "vpp.flags.is_ip6",
          FT_BOOLEAN, 32, TFS(&tfs_true_false), 0x00800000, NULL, HFILL },
      },
      { &hf_vpp_buffer_flag_offload_ip_checksum,
        { "OffloadIPChecksum", "vpp.flags.offload_ip_checksum",
          FT_BOOLEAN, 32, TFS(&tfs_true_false), 0x00400000, NULL, HFILL },
      },
      { &hf_vpp_buffer_flag_offload_tcp_checksum,
        { "OffloadTCPChecksum", "vpp.flags.offload_tcp_checksum",
          FT_BOOLEAN, 32, TFS(&tfs_true_false), 0x00200000, NULL, HFILL },
      },
      { &hf_vpp_buffer_flag_offload_udp_checksum,
        { "OffloadUDPChecksum", "vpp.flags.offload_udp_checksum",
          FT_BOOLEAN, 32, TFS(&tfs_true_false), 0x00100000, NULL, HFILL },
      },
      { &hf_vpp_buffer_flag_is_natted,
        { "IsNATted", "vpp.flags.is_natted",
          FT_BOOLEAN, 32, TFS(&tfs_true_false), 0x00080000, NULL, HFILL },
      },
      { &hf_vpp_buffer_flag_l2_hdr_offset_valid,
        { "L2HdrOffsetValid", "vpp.flags.l2_hdr_offset_valid",
          FT_BOOLEAN, 32, TFS(&tfs_true_false), 0x00040000, NULL, HFILL },
      },
      { &hf_vpp_buffer_flag_l3_hdr_offset_valid,
        { "L3HdrOffsetValid", "vpp.flags.l3_hdr_offset_valid",
          FT_BOOLEAN, 32, TFS(&tfs_true_false), 0x00020000, NULL, HFILL },
      },
      { &hf_vpp_buffer_flag_l4_hdr_offset_valid,
        { "L4HdrOffsetValid", "vpp.flags.l4_hdr_offset_valid",
          FT_BOOLEAN, 32, TFS(&tfs_true_false), 0x00010000, NULL, HFILL },
      },
      { &hf_vpp_buffer_flag_flow_report,
        { "FlowReport", "vpp.flags.flow_report",
          FT_BOOLEAN, 32, TFS(&tfs_true_false), 0x00008000, NULL, HFILL },
      },
      { &hf_vpp_buffer_flag_is_dvr,
        { "IsDVR", "vpp.flags.is_dvr",
          FT_BOOLEAN, 32, TFS(&tfs_true_false), 0x00004000, NULL, HFILL },
      },
      { &hf_vpp_buffer_flag_qos_data_valid,
        { "QOSDataValid", "vpp.flags.qos_data_valid",
          FT_BOOLEAN, 32, TFS(&tfs_true_false), 0x00002000, NULL, HFILL },
      },

      { &hf_vpp_buffer_flow_id,
        { "FlowID", "vpp.flow_id", FT_UINT32, BASE_DEC, NULL, 0x0,
          NULL, HFILL },
      },
      { &hf_vpp_buffer_next_buffer,
        { "NextBuffer", "vpp.next_buffer", FT_UINT32, BASE_DEC, NULL, 0x0,
          NULL, HFILL },
      },
      { &hf_vpp_buffer_current_config_index,
        { "CurrentConfigIndex", "vpp.current_config_index", 
          FT_UINT32, BASE_DEC, NULL, 0x0,
          NULL, HFILL },
      },
      { &hf_vpp_buffer_error_index,
        { "ErrorIndex", "vpp.error_index", 
          FT_UINT16, BASE_DEC, NULL, 0x0,
          NULL, HFILL },
      },
      { &hf_vpp_buffer_n_add_refs,
        { "AddRefs", "vpp.n_add_refs_index", 
          FT_UINT8, BASE_DEC, NULL, 0x0,
          NULL, HFILL },
      },
      { &hf_vpp_buffer_buffer_pool_index,
        { "BufferPoolIndex", "vpp.buffer_pool_index", 
          FT_UINT8, BASE_DEC, NULL, 0x0,
          NULL, HFILL },
      },
  };

  static hf_register_info opaque_hf[] = {
      { &hf_vpp_buffer_opaque_raw,
        { "Raw   ", "vppMetadata.opaque_raw",  FT_STRINGZ, BASE_NONE, NULL, 0x0,
          NULL, HFILL },
      },
      { &hf_vpp_buffer_opaque_opaque,
        { "Opaque", "vppMetadata.opaque",  
          FT_STRINGZ, BASE_NONE, NULL, 0x0, NULL, HFILL },
      },
  };

  static hf_register_info trace_hf[] = {
      { &hf_vpp_buffer_trace,
        { "Trace", "vppTrace.trace",  FT_STRINGZ, BASE_NONE, NULL, 0x0,
          NULL, HFILL },
      },
  };

  static gint *ett[] = {
    &ett_vpp,
  };
  static gint *ett_opaque[] = {
    &ett_vpp_opaque,
  };
  static gint *ett_trace[] = {
    &ett_vpp_trace,
  };

  proto_vpp = proto_register_protocol("VPP Buffer Metadata", "VPP", "vpp");
  proto_register_field_array(proto_vpp, hf, array_length(hf));
  proto_register_subtree_array (ett, array_length(ett));
  register_dissector("vpp", dissect_vpp, proto_vpp);

  proto_vpp_opaque = proto_register_protocol("VPP Buffer Opaque", "VPP-Opaque", 
                                             "vpp-opaque");
  proto_register_field_array(proto_vpp_opaque, opaque_hf, 
                             array_length(opaque_hf));
  proto_register_subtree_array (ett_opaque, array_length(ett_opaque));
  register_dissector("vppOpaque", dissect_vpp_opaque, proto_vpp_opaque);

  proto_vpp_trace = proto_register_protocol("VPP Buffer Trace", "VPP-Trace", 
                                             "vpp-trace");
  proto_register_field_array(proto_vpp_trace, trace_hf, 
                             array_length(trace_hf));
  proto_register_subtree_array (ett_trace, array_length(ett_trace));
  register_dissector("vppTrace", dissect_vpp_trace, proto_vpp_trace);
  
  vpp_subdissector_table = register_dissector_table 
      ("vpp", "VPP per-node next dissector table", proto_vpp,
       FT_STRING, BASE_NONE);

#define _(n,d)                                                  \
  d##_dissector_handle = find_dissector(#d);                    \
  dissector_add_string ("vpp", n, d##_dissector_handle);
  foreach_node_to_dissector_pair;
#undef _
}

void
proto_reg_handoff_vpp(void)
{
    vpp_dissector_handle = find_dissector("vpp");
    vpp_opaque_dissector_handle = find_dissector("vppOpaque");
    vpp_trace_dissector_handle = find_dissector("vppTrace");
    dissector_add_uint("wtap_encap", WTAP_ENCAP_USER13, vpp_dissector_handle);
}

/*
 * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
 *
 * Local variables:
 * c-basic-offset: 4
 * tab-width: 8
 * indent-tabs-mode: nil
 * End:
 *
 * vi: set shiftwidth=4 tabstop=8 expandtab:
 * :indentSize=4:tabSize=8:noTabs=true:
 */