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