fbc815432ebafab4932f0815c9df1fd153122acc
[vpp.git] / extras / wireshark / packet-vpp.c
1 /* packet-vpp.c
2  * 
3  * Routines for the disassembly of fd.io vpp project 
4  * dispatch captures
5  *
6  * Copyright (c) 2018 Cisco and/or its affiliates.
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at:
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * 
19  * This version is not to be upstreamed as-is, since it hooks up the
20  * vpp dissector to WTAP_ENCAP_USER13, a test encap type.
21  */
22
23 #include "config.h"
24
25 #include <epan/packet.h>
26 #include <epan/expert.h>
27 #include <epan/to_str.h>
28 #include <epan/in_cksum.h>
29 #include <epan/nlpid.h>
30 #include <epan/etypes.h>
31 #include <stdio.h>
32 #include <wsutil/ws_printf.h>
33
34 void proto_register_vpp(void);
35 void proto_reg_handoff_vpp(void);
36
37 static int proto_vpp = -1;
38 static int proto_vpp_opaque = -1;
39 static int proto_vpp_trace = -1;
40 static int hf_vpp_nodename = -1;
41 static int hf_vpp_buffer_index = -1;
42
43 static int hf_vpp_buffer_current_data = -1;
44 static int hf_vpp_buffer_current_length = -1;
45
46 static int hf_vpp_buffer_flags = -1;
47 static int hf_vpp_buffer_flag_non_default_freelist = -1;
48 static int hf_vpp_buffer_flag_traced = -1;
49 static int hf_vpp_buffer_flag_next_present = -1;
50 static int hf_vpp_buffer_flag_total_length_valid = -1;
51 static int hf_vpp_buffer_flag_ext_hdr_valid = -1;       
52 static int hf_vpp_buffer_flag_l4_checksum_computed = -1;
53 static int hf_vpp_buffer_flag_l4_checksum_correct = -1;
54 static int hf_vpp_buffer_flag_vlan_2_deep = -1;
55 static int hf_vpp_buffer_flag_vlan_1_deep = -1;
56 static int hf_vpp_buffer_flag_span_clone = -1;
57 static int hf_vpp_buffer_flag_loop_counter_valid = -1;
58 static int hf_vpp_buffer_flag_locally_originated = -1;
59 static int hf_vpp_buffer_flag_is_ip4 = -1;
60 static int hf_vpp_buffer_flag_is_ip6 = -1;
61 static int hf_vpp_buffer_flag_offload_ip_checksum = -1;
62 static int hf_vpp_buffer_flag_offload_tcp_checksum = -1;
63 static int hf_vpp_buffer_flag_offload_udp_checksum = -1;
64 static int hf_vpp_buffer_flag_is_natted = -1;
65 static int hf_vpp_buffer_flag_l2_hdr_offset_valid = -1;
66 static int hf_vpp_buffer_flag_l3_hdr_offset_valid = -1;
67 static int hf_vpp_buffer_flag_l4_hdr_offset_valid = -1;
68 static int hf_vpp_buffer_flag_flow_report = -1;
69 static int hf_vpp_buffer_flag_is_dvr = -1;
70 static int hf_vpp_buffer_flag_qos_data_valid = -1;
71 static int hf_vpp_buffer_flow_id = -1;
72 static int hf_vpp_buffer_next_buffer = -1;
73 static int hf_vpp_buffer_current_config_index = -1;
74 static int hf_vpp_buffer_error_index = -1;
75 static int hf_vpp_buffer_n_add_refs = -1;
76 static int hf_vpp_buffer_buffer_pool_index = -1;
77
78 static int hf_vpp_buffer_opaque_raw = -1;
79 static int hf_vpp_buffer_opaque_opaque = -1;
80
81 static int hf_vpp_buffer_trace = -1;
82
83 static gint ett_vpp = -1;
84 static gint ett_vpp_opaque = -1;
85 static gint ett_vpp_trace = -1;
86
87 static dissector_handle_t vpp_dissector_handle;
88 static dissector_handle_t vpp_opaque_dissector_handle;
89 static dissector_handle_t vpp_trace_dissector_handle;
90
91 static dissector_handle_t eth_dissector_handle;
92 static dissector_handle_t ip4_dissector_handle;
93 static dissector_handle_t ip6_dissector_handle;
94 static dissector_handle_t udp_dissector_handle;
95
96 #define foreach_node_to_dissector_handle                        \
97 _("ip6-lookup", "ipv6", ip6_dissector_handle)                   \
98 _("ip4-input-no-checksum", "ip", ip4_dissector_handle)          \
99 _("ip4-lookup", "ip", ip4_dissector_handle)                     \
100 _("ip4-local", "ip", ip4_dissector_handle)                      \
101 _("ip4-udp-lookup", "ip", udp_dissector_handle)                 \
102 _("ip4-icmp-error", "ip", ip4_dissector_handle)                 \
103 _("ip4-glean", "ip", ip4_dissector_handle)                      \
104 _("ethernet-input", "eth_maybefcs", eth_dissector_handle)
105
106 static void
107 add_multi_line_string_to_tree(proto_tree *tree, tvbuff_t *tvb, gint start,
108   gint len, int hf)
109 {
110     gint next;
111     int  line_len;
112     int  data_len;
113
114     while (len > 0) {
115         line_len = tvb_find_line_end(tvb, start, len, &next, FALSE);
116         data_len = next - start;
117         proto_tree_add_string(tree, hf, tvb, start, data_len, 
118                               tvb_format_stringzpad(tvb, start, line_len));
119         start += data_len;
120         len   -= data_len;
121     }
122 }
123
124 static int
125 dissect_vpp_trace (tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, 
126                     void* data _U_)
127 {
128     int         offset   = 0;
129     proto_item *ti;
130     proto_tree *trace_tree;
131     gint trace_string_length;
132     
133     col_set_str(pinfo->cinfo, COL_PROTOCOL, "VPP-Trace");
134     col_clear(pinfo->cinfo, COL_INFO);
135
136     ti = proto_tree_add_item(tree, proto_vpp_trace, tvb, offset, -1, ENC_NA);
137     trace_tree = proto_item_add_subtree(ti, ett_vpp_trace);
138
139     /* How long is the trace string? */
140     (void ) tvb_get_stringz_enc (wmem_packet_scope(), tvb, 0,
141                                  &trace_string_length, ENC_ASCII);
142     
143     add_multi_line_string_to_tree (trace_tree, tvb, 0,
144                                    trace_string_length, 
145                                    hf_vpp_buffer_trace);
146     return tvb_captured_length(tvb);
147 }
148
149 /*
150  * BIG FAT WARNING: it's impossible to #include the vpp header files,
151  * so this is a private copy of .../src/vnet/buffer.h, with
152  * some vpp typedefs thrown in for good measure.
153  */
154
155 typedef unsigned int u32;
156 typedef unsigned short int u16;
157 typedef short int i16;
158 typedef unsigned char u8;
159 typedef unsigned long long u64;
160
161 typedef struct 
162 {
163     u32 sw_if_index[2];
164     i16 l2_hdr_offset;
165     i16 l3_hdr_offset;
166     i16 l4_hdr_offset;
167     u8 feature_arc_index;
168     u8 dont_waste_me;
169
170     union
171     {
172         /* IP4/6 buffer opaque. */
173         struct
174         {
175             /* Adjacency from destination IP address lookup [VLIB_TX].
176                Adjacency from source IP address lookup [VLIB_RX].
177                This gets set to ~0 until source lookup is performed. */
178             u32 adj_index[2];
179
180             union
181             {
182                 struct
183                 {
184                     /* Flow hash value for this packet computed from IP src/dst address
185                        protocol and ports. */
186                     u32 flow_hash;
187
188                     union
189                     {
190                         /* next protocol */
191                         u32 save_protocol;
192
193                         /* Hint for transport protocols */
194                         u32 fib_index;
195                     };
196
197                     /* Rewrite length */
198                     u32 save_rewrite_length;
199
200                     /* MFIB RPF ID */
201                     u32 rpf_id;
202                 };
203
204                 /* ICMP */
205                 struct
206                 {
207                     u8 type;
208                     u8 code;
209                     u32 data;
210                 } icmp;
211
212                 /* reassembly */
213                 union
214                 {
215                     /* in/out variables */
216                     struct
217                     {
218                         u32 next_index; /* index of next node - ignored if "feature" node */
219                         u16 estimated_mtu;      /* estimated MTU calculated during reassembly */
220                     };
221                     /* internal variables used during reassembly */
222                     struct
223                     {
224                         u16 fragment_first;
225                         u16 fragment_last;
226                         u16 range_first;
227                         u16 range_last;
228                         u32 next_range_bi;
229                         u16 ip6_frag_hdr_offset;
230                     };
231                 } reass;
232             };
233
234         } ip;
235
236         /*
237          * MPLS:
238          * data copied from the MPLS header that was popped from the packet
239          * during the look-up.
240          */
241         struct
242         {
243             /* do not overlay w/ ip.adj_index[0,1] nor flow hash */
244             u32 pad[3];
245             u8 ttl;
246             u8 exp;
247             u8 first;
248             /* Rewrite length */
249             u32 save_rewrite_length;
250             /*
251              * BIER - the number of bytes in the header.
252              *  the len field in the header is not authoritative. It's the
253              * value in the table that counts.
254              */
255             struct
256             {
257                 u8 n_bytes;
258             } bier;
259         } mpls;
260
261         /* l2 bridging path, only valid there */
262         struct opaque_l2
263         {
264             u32 feature_bitmap;
265             u16 bd_index;               /* bridge-domain index */
266             u8 l2_len;          /* ethernet header length */
267             u8 shg;                     /* split-horizon group */
268             u16 l2fib_sn;               /* l2fib bd/int seq_num */
269             u8 bd_age;          /* aging enabled */
270         } l2;
271
272         /* l2tpv3 softwire encap, only valid there */
273         struct
274         {
275             u32 pad[4];         /* do not overlay w/ ip.adj_index[0,1] */
276             u8 next_index;
277             u32 session_index;
278         } l2t;
279
280         /* L2 classify */
281         struct
282         {
283             struct opaque_l2 pad;
284             union
285             {
286                 u32 table_index;
287                 u32 opaque_index;
288             };
289             u64 hash;
290         } l2_classify;
291
292         /* vnet policer */
293         struct
294         {
295             u32 pad[8 - 2 - 1]; /* to end of opaque */
296             u32 index;
297         } policer;
298
299         /* interface output features */
300         struct
301         {
302             u32 flags;
303             u32 sad_index;
304         } ipsec;
305
306         /* MAP */
307         struct
308         {
309             u16 mtu;
310         } map;
311
312         /* MAP-T */
313         struct
314         {
315             u32 map_domain_index;
316             struct
317             {
318                 u32 saddr, daddr;
319                 u16 frag_offset;        //Fragmentation header offset
320                 u16 l4_offset;          //L4 header overall offset
321                 u8 l4_protocol;         //The final protocol number
322             } v6;                       //Used by ip6_map_t only
323             u16 checksum_offset;        //L4 checksum overall offset
324             u16 mtu;                    //Exit MTU
325         } map_t;
326
327         /* IP Fragmentation */
328         struct
329         {
330             u32 pad[2];         /* do not overlay w/ ip.adj_index[0,1] */
331             u16 mtu;
332             u8 next_index;
333             u8 flags;                   //See ip_frag.h
334         } ip_frag;
335
336         /* COP - configurable junk filter(s) */
337         struct
338         {
339             /* Current configuration index. */
340             u32 current_config_index;
341         } cop;
342
343         /* LISP */
344         struct
345         {
346             /* overlay address family */
347             u16 overlay_afi;
348         } lisp;
349
350         /* TCP */
351         struct
352         {
353             u32 connection_index;
354             u32 seq_number;
355             u32 seq_end;
356             u32 ack_number;
357             u16 hdr_offset;             /**< offset relative to ip hdr */
358             u16 data_offset;            /**< offset relative to ip hdr */
359             u16 data_len;               /**< data len */
360             u8 flags;
361         } tcp;
362
363         /* SCTP */
364         struct
365         {
366             u32 connection_index;
367             u16 sid; /**< Stream ID */
368             u16 ssn; /**< Stream Sequence Number */
369             u32 tsn; /**< Transmission Sequence Number */
370             u16 hdr_offset;             /**< offset relative to ip hdr */
371             u16 data_offset;            /**< offset relative to ip hdr */
372             u16 data_len;               /**< data len */
373             u8 subconn_idx; /**< index of the sub_connection being used */
374             u8 flags;
375         } sctp;
376
377         /* SNAT */
378         struct
379         {
380             u32 flags;
381         } snat;
382
383         u32 unused[6];
384     };
385 } vnet_buffer_opaque_t;
386
387
388 #define PTAS proto_tree_add_string(opaque_tree,                         \
389                                    hf_vpp_buffer_opaque_opaque,         \
390                                    tvb, 0, strlen(tmpbuf), tmpbuf)
391
392 static int
393 dissect_vpp_opaque (tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, 
394                     void* data _U_)
395 {
396     int         offset   = 0;
397     proto_item *ti;
398     proto_tree *opaque_tree;
399     char tmpbuf [512];
400     int print_offset;
401     guint32 opaque[10];
402     vnet_buffer_opaque_t _o, *o = &_o;
403
404     int i;
405
406     col_set_str(pinfo->cinfo, COL_PROTOCOL, "VPP-Opaque");
407     col_clear(pinfo->cinfo, COL_INFO);
408
409     ti = proto_tree_add_item(tree, proto_vpp_opaque, tvb, offset, -1, ENC_NA);
410     opaque_tree = proto_item_add_subtree(ti, ett_vpp_opaque);
411
412     print_offset = 0;
413     for (i = 0; i < 10; i++) {
414         opaque[i] = tvb_get_guint32 (tvb, offset + 4*i, ENC_LITTLE_ENDIAN);
415         snprintf (tmpbuf + print_offset, sizeof(tmpbuf) - print_offset, 
416                   "%08x ", opaque[i]);
417         print_offset = strlen (tmpbuf);
418     }
419     offset += 40;
420
421     proto_tree_add_string (opaque_tree, hf_vpp_buffer_opaque_raw, tvb, 0,
422                            strlen(tmpbuf), tmpbuf);
423
424     memset (o, 0, sizeof (*o));
425     memcpy (o, opaque, sizeof (opaque));
426  
427     snprintf (tmpbuf, sizeof(tmpbuf), 
428               "sw_if_index[VLIB_RX]: %d, sw_if_index[VLIB_TX]: %d",
429               o->sw_if_index[0], o->sw_if_index[1]);
430     PTAS;
431
432     snprintf (tmpbuf, sizeof(tmpbuf),
433               "L2 offset %d, L3 offset %d, L4 offset %d, feature arc index %d",
434               (u32)(o->l2_hdr_offset),
435               (u32)(o->l3_hdr_offset),
436               (u32)(o->l4_hdr_offset), 
437               (u32)(o->feature_arc_index));
438     PTAS;
439
440     snprintf (tmpbuf, sizeof(tmpbuf), 
441               "ip.adj_index[VLIB_RX]: %d, ip.adj_index[VLIB_TX]: %d",
442               (u32)(o->ip.adj_index[0]),
443               (u32)(o->ip.adj_index[1]));
444     PTAS;
445
446     snprintf (tmpbuf, sizeof(tmpbuf), 
447               "ip.flow_hash: 0x%x, ip.save_protocol: 0x%x, ip.fib_index: %d",
448               o->ip.flow_hash, o->ip.save_protocol, o->ip.fib_index);
449     PTAS;
450
451     snprintf (tmpbuf, sizeof(tmpbuf), 
452               "ip.save_rewrite_length: %d, ip.rpf_id: %d",
453               o->ip.save_rewrite_length, o->ip.rpf_id);
454     PTAS;
455
456     snprintf (tmpbuf, sizeof(tmpbuf),
457               "ip.icmp.type: %d ip.icmp.code: %d, ip.icmp.data: 0x%x",
458               (u32)(o->ip.icmp.type),
459               (u32)(o->ip.icmp.code),
460               o->ip.icmp.data);
461     PTAS;
462
463     snprintf (tmpbuf, sizeof(tmpbuf),
464               "ip.reass.next_index: %d, ip.reass.estimated_mtu: %d",
465               o->ip.reass.next_index, (u32)(o->ip.reass.estimated_mtu));
466     PTAS;
467     snprintf (tmpbuf, sizeof(tmpbuf),
468               "ip.reass.fragment_first: %d ip.reass.fragment_last: %d",
469               (u32)(o->ip.reass.fragment_first),
470               (u32)(o->ip.reass.fragment_last));
471     PTAS;
472     snprintf (tmpbuf, sizeof(tmpbuf),
473               "ip.reass.range_first: %d ip.reass.range_last: %d",
474               (u32)(o->ip.reass.range_first),
475               (u32)(o->ip.reass.range_last));
476     PTAS;
477
478     snprintf (tmpbuf, sizeof(tmpbuf),
479               "ip.reass.next_range_bi: 0x%x, ip.reass.ip6_frag_hdr_offset: %d",
480               o->ip.reass.next_range_bi, 
481               (u32)(o->ip.reass.ip6_frag_hdr_offset));
482     PTAS;
483
484     snprintf (tmpbuf, sizeof(tmpbuf),
485               "mpls.ttl: %d, mpls.exp: %d, mpls.first: %d, "
486               "mpls.save_rewrite_length: %d, mpls.bier.n_bytes: %d",
487               (u32)(o->mpls.ttl), (u32)(o->mpls.exp), (u32)(o->mpls.first),
488               o->mpls.save_rewrite_length, (u32)(o->mpls.bier.n_bytes));
489     PTAS;
490
491     snprintf (tmpbuf, sizeof(tmpbuf),
492               "l2.feature_bitmap: %08x, l2.bd_index: %d, l2.l2_len: %d, "
493               "l2.shg: %d, l2.l2fib_sn: %d, l2.bd_age: %d",
494               o->l2.feature_bitmap, (u32)(o->l2.bd_index),
495               (u32)(o->l2.l2_len), (u32)(o->l2.shg), (u32)(o->l2.l2fib_sn),
496               (u32)(o->l2.bd_age));
497     PTAS;
498         
499     snprintf (tmpbuf, sizeof(tmpbuf),
500               "l2t.next_index: %d, l2t.session_index: %d",
501               (u32)(o->l2t.next_index), o->l2t.session_index);
502     PTAS;
503
504     snprintf (tmpbuf, sizeof(tmpbuf),
505               "l2_classify.table_index: %d, l2_classify.opaque_index: %d, "
506               "l2_classify.hash: 0x%llx",
507               o->l2_classify.table_index,
508               o->l2_classify.opaque_index,
509               o->l2_classify.hash);
510     PTAS;
511
512     snprintf (tmpbuf, sizeof(tmpbuf),
513               "policer.index: %d", o->policer.index);
514     PTAS;
515
516     snprintf (tmpbuf, sizeof(tmpbuf),
517               "ipsec.flags: 0x%x, ipsec.sad_index: %d",
518               o->ipsec.flags, o->ipsec.sad_index);
519     PTAS;
520
521     snprintf (tmpbuf, sizeof(tmpbuf),
522               "map.mtu: %d", (u32)(o->map.mtu));
523     PTAS;
524
525     snprintf (tmpbuf, sizeof(tmpbuf),
526               "map_t.v6.saddr: 0x%x, map_t.v6.daddr: 0x%x, "
527               "map_t.v6.frag_offset: %d, map_t.v6.l4_offset: %d",
528               o->map_t.v6.saddr,
529               o->map_t.v6.daddr,
530               (u32)(o->map_t.v6.frag_offset),
531               (u32)(o->map_t.v6.l4_offset));
532     PTAS;
533     snprintf (tmpbuf, sizeof(tmpbuf),
534               "map_t.v6.l4_protocol: %d, map_t.checksum_offset: %d, "
535               "map_t.mtu: %d",
536               (u32)(o->map_t.v6.l4_protocol),
537               (u32)(o->map_t.checksum_offset),
538               (u32)(o->map_t.mtu));
539     PTAS;
540     
541     snprintf (tmpbuf, sizeof(tmpbuf),
542               "ip_frag.mtu: %d, ip_frag.next_index: %d, ip_frag.flags: 0x%x",
543               (u32)(o->ip_frag.mtu),
544               (u32)(o->ip_frag.next_index),
545               (u32)(o->ip_frag.flags));
546     PTAS;
547
548     snprintf (tmpbuf, sizeof(tmpbuf),
549               "cop.current_config_index: %d",
550               o->cop.current_config_index);
551     PTAS;
552     
553     snprintf (tmpbuf, sizeof(tmpbuf),
554               "lisp.overlay_afi: %d",
555               (u32)(o->lisp.overlay_afi));
556     PTAS;
557
558     snprintf (tmpbuf, sizeof(tmpbuf),
559               "tcp.connection_index: %d, tcp.seq_number: %d, tcp.seq_end: %d, "
560               "tcp.ack_number: %d, tcp.hdr_offset: %d, tcp.data_offset: %d",
561               o->tcp.connection_index,
562               o->tcp.seq_number,
563               o->tcp.seq_end,
564               o->tcp.ack_number,
565               (u32)(o->tcp.hdr_offset),
566               (u32)(o->tcp.data_offset));
567     PTAS;
568
569     snprintf (tmpbuf, sizeof(tmpbuf),
570               "tcp.data_len: %d, tcp.flags: 0x%x",
571               (u32)(o->tcp.data_len),
572               (u32)(o->tcp.flags));
573     PTAS;
574
575     snprintf (tmpbuf, sizeof(tmpbuf),
576               "sctp.connection_index: %d, sctp.sid: %d, sctp.ssn: %d, "
577               "sctp.tsn: %d, sctp.hdr_offset: %d",
578               o->sctp.connection_index,
579               (u32)(o->sctp.sid),
580               (u32)(o->sctp.ssn),
581               (u32)(o->sctp.tsn),
582               (u32)(o->sctp.hdr_offset));
583     PTAS;
584     snprintf (tmpbuf, sizeof(tmpbuf),
585               "sctp.data_offset: %d, sctp.data_len: %d, sctp.subconn_idx: %d, "
586               "sctp.flags: 0x%x",
587               (u32)(o->sctp.data_offset),
588               (u32)(o->sctp.data_len),
589               (u32)(o->sctp.subconn_idx),
590               (u32)(o->sctp.flags));
591     PTAS;
592               
593     snprintf (tmpbuf, sizeof(tmpbuf),
594               "snat.flags: 0x%x",
595               o->snat.flags);
596     PTAS;
597
598     return tvb_captured_length(tvb);
599 }
600
601
602 static int
603 dissect_vpp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void* data _U_)
604 {
605     proto_item *ti;
606     proto_tree *vpp_tree;
607     tvbuff_t *opaque_tvb, *eth_tvb, *trace_tvb;
608     int         offset   = 0;
609     guint8 major_version, minor_version;
610     guint8 name_length;
611     guint8 *name;
612     guint16 trace_length;
613     int i, found;
614     static const int *buffer_flags[] = {
615         &hf_vpp_buffer_flag_non_default_freelist,
616         &hf_vpp_buffer_flag_traced,
617         &hf_vpp_buffer_flag_next_present,
618         &hf_vpp_buffer_flag_total_length_valid,
619         &hf_vpp_buffer_flag_ext_hdr_valid,
620         &hf_vpp_buffer_flag_l4_checksum_computed,
621         &hf_vpp_buffer_flag_l4_checksum_correct,
622         &hf_vpp_buffer_flag_vlan_2_deep,
623         &hf_vpp_buffer_flag_vlan_1_deep,
624         &hf_vpp_buffer_flag_span_clone,
625         &hf_vpp_buffer_flag_loop_counter_valid,
626         &hf_vpp_buffer_flag_locally_originated,
627         &hf_vpp_buffer_flag_is_ip4,
628         &hf_vpp_buffer_flag_is_ip6,
629         &hf_vpp_buffer_flag_offload_ip_checksum,
630         &hf_vpp_buffer_flag_offload_tcp_checksum,
631         &hf_vpp_buffer_flag_offload_udp_checksum,
632         &hf_vpp_buffer_flag_is_natted,
633         &hf_vpp_buffer_flag_l2_hdr_offset_valid,
634         &hf_vpp_buffer_flag_l3_hdr_offset_valid,
635         &hf_vpp_buffer_flag_l4_hdr_offset_valid,
636         &hf_vpp_buffer_flag_flow_report,
637         &hf_vpp_buffer_flag_is_dvr,
638         &hf_vpp_buffer_flag_qos_data_valid,
639         NULL
640     };
641
642     col_set_str(pinfo->cinfo, COL_PROTOCOL, "VPP");
643     col_clear(pinfo->cinfo, COL_INFO);
644
645     ti = proto_tree_add_item(tree, proto_vpp, tvb, offset, -1, ENC_NA);
646     vpp_tree = proto_item_add_subtree(ti, ett_vpp);
647
648     major_version = tvb_get_guint8 (tvb, offset);
649     offset++;
650
651     minor_version = tvb_get_guint8 (tvb, offset);
652     offset++;
653
654     if (major_version != 1 || minor_version != 0)
655         ws_debug_printf ("WARNING: version mismatch (%d, %d)",
656                          major_version, minor_version);
657
658     /* Skip the buffer index */
659     offset += 4;
660
661     /* Recover the node name */
662     name_length = tvb_get_guint8 (tvb, offset);
663     offset++;
664
665     name = (guint8 *) g_malloc (name_length + 1);
666     for (i = 0; i < name_length; i++, offset++) {
667         name[i] = tvb_get_guint8 (tvb, offset);
668     }
669     name[i] = 0;
670     offset++;
671
672     proto_tree_add_string(vpp_tree, hf_vpp_nodename, tvb, 5, name_length,
673                           name);
674     proto_tree_add_item(vpp_tree, hf_vpp_buffer_index, tvb,
675                         0 /* bi at offset 0 */, 4, ENC_LITTLE_ENDIAN);
676
677     proto_tree_add_item(vpp_tree, hf_vpp_buffer_current_data, tvb,
678                         offset, 2, ENC_LITTLE_ENDIAN);
679     offset += 2;
680     proto_tree_add_item(vpp_tree, hf_vpp_buffer_current_length, tvb,
681                         offset, 2, ENC_LITTLE_ENDIAN);
682     offset += 2;
683
684     proto_tree_add_bitmask(vpp_tree, tvb, offset,
685                            hf_vpp_buffer_flags, ett_vpp,
686                            buffer_flags, ENC_LITTLE_ENDIAN);
687     offset += 4;
688
689     proto_tree_add_item(vpp_tree, hf_vpp_buffer_flow_id, tvb,
690                         offset, 4, ENC_LITTLE_ENDIAN);
691     offset += 4;
692
693     proto_tree_add_item(vpp_tree, hf_vpp_buffer_next_buffer, tvb,
694                         offset, 4, ENC_LITTLE_ENDIAN);
695     offset += 4;
696
697     proto_tree_add_item(vpp_tree, hf_vpp_buffer_current_config_index, tvb,
698                         offset, 4, ENC_LITTLE_ENDIAN);
699     offset += 4;
700
701     proto_tree_add_item(vpp_tree, hf_vpp_buffer_error_index, tvb,
702                         offset, 2, ENC_LITTLE_ENDIAN);
703     offset += 2;
704
705     proto_tree_add_item(vpp_tree, hf_vpp_buffer_n_add_refs, tvb,
706                         offset, 1, ENC_LITTLE_ENDIAN);
707     offset += 1;
708
709     proto_tree_add_item(vpp_tree, hf_vpp_buffer_buffer_pool_index, tvb,
710                         offset, 1, ENC_LITTLE_ENDIAN);
711     offset += 1;
712     
713     opaque_tvb = tvb_new_subset_remaining (tvb, offset);
714     call_dissector (vpp_opaque_dissector_handle, opaque_tvb, pinfo, tree);
715     
716     /* Skip opaque */
717     offset += 40;
718     /* skip second opaque line */
719     offset += 64; 
720
721     trace_length = tvb_get_guint16 (tvb, offset, ENC_LITTLE_ENDIAN);
722     offset += 2;
723
724     if (trace_length > 0) {
725         trace_tvb = tvb_new_subset_remaining (tvb, offset);
726         offset += trace_length;
727
728         call_dissector (vpp_trace_dissector_handle, trace_tvb, pinfo, tree);
729     }
730
731     eth_tvb = tvb_new_subset_remaining (tvb, offset);
732     
733     found = 0;
734
735 #define _(a,b,c)                                        \
736      {                                                  \
737         if (!strcmp (name, a)) {                        \
738             call_dissector (c, eth_tvb, pinfo, tree);   \
739             found = 1;                                  \
740         }                                               \
741       }
742     foreach_node_to_dissector_handle;
743 #undef _
744     if (found == 0)
745         call_dissector (eth_dissector_handle, eth_tvb, pinfo, tree);
746
747     g_free (name);
748     return tvb_captured_length(tvb);
749 }
750
751 void
752 proto_register_vpp(void)
753 {
754   static hf_register_info hf[] = {
755       { &hf_vpp_buffer_index,
756         { "BufferIndex", "vpp.bufferindex",  FT_UINT32, BASE_HEX, NULL, 0x0,
757           NULL, HFILL },
758       },
759       { &hf_vpp_nodename,
760         { "NodeName", "vpp.nodename",  FT_STRINGZ, BASE_NONE, NULL, 0x0,
761           NULL, HFILL },
762       },
763       { &hf_vpp_buffer_current_data,
764         { "CurrentData", "vpp.current_data", FT_INT16, BASE_DEC, NULL, 0x0,
765           NULL, HFILL },
766       },
767       { &hf_vpp_buffer_current_length,
768         { "CurrentLength", "vpp.current_length", FT_UINT16, BASE_DEC, NULL, 0x0,
769           NULL, HFILL },
770       },
771
772       /* 
773        * Warning: buffer flag bits are not cast in concrete, and it's
774        * impossible to imagine trying to compile WS with even a subset 
775        * of the actual header files. 
776        * 
777        * See .../src/vlib/buffer.h, .../src/vnet/buffer.h in
778        * the fd.io vpp source tree.
779        */
780
781       { &hf_vpp_buffer_flags,
782         { "BufferFlags", "vpp.flags", FT_UINT32, BASE_HEX, NULL, 0x0,
783           NULL, HFILL },
784       },
785
786       { &hf_vpp_buffer_flag_non_default_freelist,
787         { "NonDefaultFreelist", "vpp.flags.non_default_freelist",
788           FT_BOOLEAN, 32, TFS(&tfs_true_false), 0x1, NULL, HFILL },
789       },
790       { &hf_vpp_buffer_flag_traced,
791         { "Traced", "vpp.flags.traced",
792           FT_BOOLEAN, 32, TFS(&tfs_true_false), 0x2, NULL, HFILL },
793       },
794       { &hf_vpp_buffer_flag_next_present,
795         { "NextPresent", "vpp.flags.next_present",
796           FT_BOOLEAN, 32, TFS(&tfs_true_false), 0x4, NULL, HFILL },
797       },
798       { &hf_vpp_buffer_flag_total_length_valid,
799         { "TotalLengthValid", "vpp.flags.total_length_valid",
800           FT_BOOLEAN, 32, TFS(&tfs_true_false), 0x8, NULL, HFILL },
801       },
802       { &hf_vpp_buffer_flag_ext_hdr_valid,
803         { "ExtHeaderValid", "vpp.flags.ext_hdr_valid",
804           FT_BOOLEAN, 32, TFS(&tfs_true_false), 0x10, NULL, HFILL },
805       },
806
807       { &hf_vpp_buffer_flag_l4_checksum_computed,
808         { "L4ChecksumComputed", "vpp.flags.l4_checksum_computed",
809           FT_BOOLEAN, 32, TFS(&tfs_true_false), 0x80000000, NULL, HFILL },
810       },
811       { &hf_vpp_buffer_flag_l4_checksum_correct,
812         { "L4ChecksumCorrect", "vpp.flags.l4_checksum_correct",
813           FT_BOOLEAN, 32, TFS(&tfs_true_false), 0x40000000, NULL, HFILL },
814       },
815       { &hf_vpp_buffer_flag_vlan_2_deep,
816         { "Vlan2Deep", "vpp.flags.vlan_2_deep",
817           FT_BOOLEAN, 32, TFS(&tfs_true_false), 0x20000000, NULL, HFILL },
818       },
819       { &hf_vpp_buffer_flag_vlan_1_deep,
820         { "Vlan1Deep", "vpp.flags.vlan_1_deep",
821           FT_BOOLEAN, 32, TFS(&tfs_true_false), 0x10000000, NULL, HFILL },
822       },
823       { &hf_vpp_buffer_flag_span_clone,
824         { "SpanClone", "vpp.flags.span_clone",
825           FT_BOOLEAN, 32, TFS(&tfs_true_false), 0x08000000, NULL, HFILL },
826       },
827       { &hf_vpp_buffer_flag_loop_counter_valid,
828         { "LoopCounterValid", "vpp.flags.loop_counter_valid",
829           FT_BOOLEAN, 32, TFS(&tfs_true_false), 0x04000000, NULL, HFILL },
830       },
831       { &hf_vpp_buffer_flag_locally_originated,
832         { "LocallyOriginated", "vpp.flags.locally_originated",
833           FT_BOOLEAN, 32, TFS(&tfs_true_false), 0x02000000, NULL, HFILL },
834       },
835       { &hf_vpp_buffer_flag_is_ip4,
836         { "IsIP4", "vpp.flags.is_ip4",
837           FT_BOOLEAN, 32, TFS(&tfs_true_false), 0x01000000, NULL, HFILL },
838       },
839       { &hf_vpp_buffer_flag_is_ip6,
840         { "IsIP4", "vpp.flags.is_ip6",
841           FT_BOOLEAN, 32, TFS(&tfs_true_false), 0x00800000, NULL, HFILL },
842       },
843       { &hf_vpp_buffer_flag_offload_ip_checksum,
844         { "OffloadIPChecksum", "vpp.flags.offload_ip_checksum",
845           FT_BOOLEAN, 32, TFS(&tfs_true_false), 0x00400000, NULL, HFILL },
846       },
847       { &hf_vpp_buffer_flag_offload_tcp_checksum,
848         { "OffloadTCPChecksum", "vpp.flags.offload_tcp_checksum",
849           FT_BOOLEAN, 32, TFS(&tfs_true_false), 0x00200000, NULL, HFILL },
850       },
851       { &hf_vpp_buffer_flag_offload_udp_checksum,
852         { "OffloadUDPChecksum", "vpp.flags.offload_udp_checksum",
853           FT_BOOLEAN, 32, TFS(&tfs_true_false), 0x00100000, NULL, HFILL },
854       },
855       { &hf_vpp_buffer_flag_is_natted,
856         { "IsNATted", "vpp.flags.is_natted",
857           FT_BOOLEAN, 32, TFS(&tfs_true_false), 0x00080000, NULL, HFILL },
858       },
859       { &hf_vpp_buffer_flag_l2_hdr_offset_valid,
860         { "L2HdrOffsetValid", "vpp.flags.l2_hdr_offset_valid",
861           FT_BOOLEAN, 32, TFS(&tfs_true_false), 0x00040000, NULL, HFILL },
862       },
863       { &hf_vpp_buffer_flag_l3_hdr_offset_valid,
864         { "L3HdrOffsetValid", "vpp.flags.l3_hdr_offset_valid",
865           FT_BOOLEAN, 32, TFS(&tfs_true_false), 0x00020000, NULL, HFILL },
866       },
867       { &hf_vpp_buffer_flag_l4_hdr_offset_valid,
868         { "L4HdrOffsetValid", "vpp.flags.l4_hdr_offset_valid",
869           FT_BOOLEAN, 32, TFS(&tfs_true_false), 0x00010000, NULL, HFILL },
870       },
871       { &hf_vpp_buffer_flag_flow_report,
872         { "FlowReport", "vpp.flags.flow_report",
873           FT_BOOLEAN, 32, TFS(&tfs_true_false), 0x00008000, NULL, HFILL },
874       },
875       { &hf_vpp_buffer_flag_is_dvr,
876         { "IsDVR", "vpp.flags.is_dvr",
877           FT_BOOLEAN, 32, TFS(&tfs_true_false), 0x00004000, NULL, HFILL },
878       },
879       { &hf_vpp_buffer_flag_qos_data_valid,
880         { "QOSDataValid", "vpp.flags.qos_data_valid",
881           FT_BOOLEAN, 32, TFS(&tfs_true_false), 0x00002000, NULL, HFILL },
882       },
883
884       { &hf_vpp_buffer_flow_id,
885         { "FlowID", "vpp.flow_id", FT_UINT32, BASE_DEC, NULL, 0x0,
886           NULL, HFILL },
887       },
888       { &hf_vpp_buffer_next_buffer,
889         { "NextBuffer", "vpp.next_buffer", FT_UINT32, BASE_DEC, NULL, 0x0,
890           NULL, HFILL },
891       },
892       { &hf_vpp_buffer_current_config_index,
893         { "CurrentConfigIndex", "vpp.current_config_index", 
894           FT_UINT32, BASE_DEC, NULL, 0x0,
895           NULL, HFILL },
896       },
897       { &hf_vpp_buffer_error_index,
898         { "ErrorIndex", "vpp.error_index", 
899           FT_UINT16, BASE_DEC, NULL, 0x0,
900           NULL, HFILL },
901       },
902       { &hf_vpp_buffer_n_add_refs,
903         { "AddRefs", "vpp.n_add_refs_index", 
904           FT_UINT8, BASE_DEC, NULL, 0x0,
905           NULL, HFILL },
906       },
907       { &hf_vpp_buffer_buffer_pool_index,
908         { "BufferPoolIndex", "vpp.buffer_pool_index", 
909           FT_UINT8, BASE_DEC, NULL, 0x0,
910           NULL, HFILL },
911       },
912   };
913
914   static hf_register_info opaque_hf[] = {
915       { &hf_vpp_buffer_opaque_raw,
916         { "Raw   ", "vppMetadata.opaque_raw",  FT_STRINGZ, BASE_NONE, NULL, 0x0,
917           NULL, HFILL },
918       },
919       { &hf_vpp_buffer_opaque_opaque,
920         { "Opaque", "vppMetadata.opaque",  
921           FT_STRINGZ, BASE_NONE, NULL, 0x0, NULL, HFILL },
922       },
923   };
924
925   static hf_register_info trace_hf[] = {
926       { &hf_vpp_buffer_trace,
927         { "Trace", "vppTrace.trace",  FT_STRINGZ, BASE_NONE, NULL, 0x0,
928           NULL, HFILL },
929       },
930   };
931
932   static gint *ett[] = {
933     &ett_vpp,
934   };
935   static gint *ett_opaque[] = {
936     &ett_vpp_opaque,
937   };
938   static gint *ett_trace[] = {
939     &ett_vpp_trace,
940   };
941
942   proto_vpp = proto_register_protocol("VPP Buffer Metadata", "VPP", "vpp");
943   proto_register_field_array(proto_vpp, hf, array_length(hf));
944   proto_register_subtree_array (ett, array_length(ett));
945   register_dissector("vpp", dissect_vpp, proto_vpp);
946
947   proto_vpp_opaque = proto_register_protocol("VPP Buffer Opaque", "VPP-Opaque", 
948                                              "vpp-opaque");
949   proto_register_field_array(proto_vpp_opaque, opaque_hf, 
950                              array_length(opaque_hf));
951   proto_register_subtree_array (ett_opaque, array_length(ett_opaque));
952   register_dissector("vppOpaque", dissect_vpp_opaque, proto_vpp_opaque);
953
954   proto_vpp_trace = proto_register_protocol("VPP Buffer Trace", "VPP-Trace", 
955                                              "vpp-trace");
956   proto_register_field_array(proto_vpp_trace, trace_hf, 
957                              array_length(trace_hf));
958   proto_register_subtree_array (ett_trace, array_length(ett_trace));
959   register_dissector("vppTrace", dissect_vpp_trace, proto_vpp_trace);
960   
961 #define _(a,b,c) c = find_dissector(b);
962   foreach_node_to_dissector_handle;
963 #undef _
964 }
965
966 void
967 proto_reg_handoff_vpp(void)
968 {
969     vpp_dissector_handle = find_dissector("vpp");
970     vpp_opaque_dissector_handle = find_dissector("vppOpaque");
971     vpp_trace_dissector_handle = find_dissector("vppTrace");
972     dissector_add_uint("wtap_encap", WTAP_ENCAP_USER13, vpp_dissector_handle);
973 }
974
975 /*
976  * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
977  *
978  * Local variables:
979  * c-basic-offset: 4
980  * tab-width: 8
981  * indent-tabs-mode: nil
982  * End:
983  *
984  * vi: set shiftwidth=4 tabstop=8 expandtab:
985  * :indentSize=4:tabSize=8:noTabs=true:
986  */