aboutsummaryrefslogtreecommitdiffstats
path: root/epan/dissectors/packet-vpp.c
blob: e1c9e41d12ba473fd26d8bed1c7d68a6095d3d5b (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
/* packet-vpp.c
 *
 * Routines for the disassembly of fd.io vpp project
 * dispatch captures
 *
 * Copyright 2019, Dave Barach <wireshark@barachs.net>
 *
 * Wireshark - Network traffic analyzer
 * By Gerald Combs <gerald@wireshark.org>
 * Copyright 1998 Gerald Combs
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 */

#include "config.h"

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

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

static int proto_vpp = -1;
static int proto_vpp_metadata = -1;
static int proto_vpp_opaque = -1;
static int proto_vpp_opaque2 = -1;
static int proto_vpp_trace = -1;
static int hf_vpp_nodename = -1;
static int hf_vpp_metadata = -1;
static int hf_vpp_buffer_index = -1;
static int hf_vpp_buffer_opaque = -1;
static int hf_vpp_buffer_opaque2 = -1;
static int hf_vpp_buffer_trace = -1;
static int hf_vpp_major_version = -1;
static int hf_vpp_minor_version = -1;
static int hf_vpp_protocol_hint = -1;

static expert_module_t* expert_vpp;

static expert_field ei_vpp_major_version_error = EI_INIT;
static expert_field ei_vpp_minor_version_error = EI_INIT;
static expert_field ei_vpp_protocol_hint_error = EI_INIT;

static gint ett_vpp = -1;
static gint ett_vpp_opaque = -1;
static gint ett_vpp_opaque2 = -1;
static gint ett_vpp_metadata = -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_opaque2_dissector_handle;
static dissector_handle_t vpp_metadata_dissector_handle;
static dissector_handle_t vpp_trace_dissector_handle;

#define VPP_MAJOR_VERSION       1
#define VPP_MINOR_VERSION       0
#define IP4_TYPICAL_VERSION_LENGTH      0x45
#define IP6_TYPICAL_VERSION_AND_TRAFFIC_CLASS   0x60

typedef enum
  {
    VLIB_NODE_PROTO_HINT_NONE = 0,
    VLIB_NODE_PROTO_HINT_ETHERNET,
    VLIB_NODE_PROTO_HINT_IP4,
    VLIB_NODE_PROTO_HINT_IP6,
    VLIB_NODE_PROTO_HINT_TCP,
    VLIB_NODE_PROTO_HINT_UDP,
    VLIB_NODE_N_PROTO_HINTS,
  } vlib_node_proto_hint_t;

static dissector_handle_t next_dissectors[VLIB_NODE_N_PROTO_HINTS];

/* List of next dissectors hints that we know about */
#define foreach_next_dissector                          \
_(VLIB_NODE_PROTO_HINT_ETHERNET, eth_withoutfcs)        \
_(VLIB_NODE_PROTO_HINT_IP4, ip)                         \
_(VLIB_NODE_PROTO_HINT_IP6, ipv6)                       \
_(VLIB_NODE_PROTO_HINT_TCP, tcp)                        \
_(VLIB_NODE_PROTO_HINT_UDP, udp)

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(wmem_packet_scope(), tvb, start, line_len));
        start += data_len;
        len -= data_len;
    }
}

static int
dissect_vpp_metadata(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree,
                     void* data _U_)
{
    int offset = 0;
    proto_item *ti;
    proto_tree *metadata_tree;
    gint metadata_string_length;

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

    ti = proto_tree_add_item(tree, proto_vpp_metadata, tvb, offset, -1, ENC_NA);
    metadata_tree = proto_item_add_subtree(ti, ett_vpp_metadata);

    /* How long is the metadata string? */
    metadata_string_length = tvb_strsize(tvb, offset);

    add_multi_line_string_to_tree(metadata_tree, tvb, 0,
                                  metadata_string_length,
                                  hf_vpp_metadata);
    return tvb_captured_length(tvb);
}

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? */
    trace_string_length = tvb_strsize(tvb, offset);

    add_multi_line_string_to_tree(trace_tree, tvb, 0,
                                  trace_string_length,
                                  hf_vpp_buffer_trace);
    return tvb_captured_length(tvb);
}


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;
    gint opaque_string_length;

    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);

    opaque_string_length = tvb_strsize(tvb, offset);
    add_multi_line_string_to_tree(opaque_tree, tvb, 0, opaque_string_length,
                                  hf_vpp_buffer_opaque);

    return tvb_captured_length(tvb);
}

static int
dissect_vpp_opaque2(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree,
                    void* data _U_)
{
    int offset = 0;
    proto_item *ti;
    proto_tree *opaque2_tree;
    gint opaque2_string_length;

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

    ti = proto_tree_add_item(tree, proto_vpp_opaque2, tvb, offset, -1, ENC_NA);
    opaque2_tree = proto_item_add_subtree(ti, ett_vpp_opaque2);

    opaque2_string_length = tvb_strsize(tvb, offset);
    add_multi_line_string_to_tree(opaque2_tree, tvb, 0, opaque2_string_length,
                                  hf_vpp_buffer_opaque2);

    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 *metadata_tvb, *opaque_tvb, *opaque2_tvb, *eth_tvb, *trace_tvb;
    int offset = 0;
    guint8 major_version, minor_version, string_count, protocol_hint;
    guint8 *name;
    guint len;
    guint8 maybe_protocol_id;
    dissector_handle_t use_this_dissector;

    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);
    /* If the major version doesn't match, quit on the spot */
    if(major_version != VPP_MAJOR_VERSION) {
        proto_item *major_version_item;
        major_version_item =
            proto_tree_add_item(tree, hf_vpp_major_version,
                                tvb, offset, 1, ENC_NA);
        expert_add_info_format(pinfo, major_version_item,
                               &ei_vpp_major_version_error,
                               "Major Version Mismatch read %d not %d",
                               (int)major_version, VPP_MAJOR_VERSION);
        return tvb_captured_length(tvb);
    }
    offset++;

    minor_version = tvb_get_guint8(tvb, offset);
    /* If the minor version doesn't match, make a note and try to continue */
    if(minor_version != VPP_MINOR_VERSION) {
        proto_item *minor_version_item;
        minor_version_item =
            proto_tree_add_item(tree, hf_vpp_minor_version,
                                tvb, offset, 1, ENC_NA);
        expert_add_info_format(pinfo, minor_version_item,
                               &ei_vpp_minor_version_error,
                               "Minor Version Mismatch read %d not %d",
                               (int)minor_version, VPP_MINOR_VERSION);
    }
    offset++;

    /* Number of counted strings in this trace record */
    string_count = tvb_get_guint8(tvb, offset);
    offset++;

    /*
     * Hint: protocol which should be at b->data[b->current_data]
     * It will be a while before vpp sends useful hints for every
     * possible node, see heuristic below.
     */
    protocol_hint = tvb_get_guint8(tvb, offset);

    if(protocol_hint >= array_length(next_dissectors)) {
        proto_item *protocol_hint_item;
        protocol_hint_item =
            proto_tree_add_item(tree, hf_vpp_protocol_hint,
                                tvb, offset, 1, ENC_NA);
        expert_add_info_format(pinfo, protocol_hint_item,
                               &ei_vpp_protocol_hint_error,
                               "Protocol hint %d out of range, max %d",
                               (int)protocol_hint,
                               (int)array_length(next_dissectors));
        protocol_hint = 0;
    }

    offset++;

    /* Buffer Index */
    proto_tree_add_item(vpp_tree, hf_vpp_buffer_index, tvb,
                        offset, 4, ENC_BIG_ENDIAN);
    offset += 4;

    /* Nodename */
    len = tvb_strsize(tvb, offset);
    name = tvb_get_string_enc(pinfo->pool, tvb, offset, len,
                              ENC_ASCII);
    proto_tree_add_string(tree, hf_vpp_nodename, tvb, offset, len, name);
    offset += len;

    /* Metadata */
    len = tvb_strsize(tvb, offset);
    metadata_tvb = tvb_new_subset_remaining(tvb, offset);
    call_dissector(vpp_metadata_dissector_handle, metadata_tvb, pinfo, tree);
    offset += len;

    /* Opaque */
    len = tvb_strsize(tvb, offset);
    opaque_tvb = tvb_new_subset_remaining(tvb, offset);
    call_dissector(vpp_opaque_dissector_handle, opaque_tvb, pinfo, tree);
    offset += len;

    /* Opaque2 */
    len = tvb_strsize(tvb, offset);
    opaque2_tvb = tvb_new_subset_remaining(tvb, offset);
    call_dissector(vpp_opaque2_dissector_handle, opaque2_tvb, pinfo, tree);
    offset += len;

    /* Trace, if present */
    if(string_count > 4) {
        len = tvb_strsize(tvb, offset);
        trace_tvb = tvb_new_subset_remaining(tvb, offset);
        call_dissector(vpp_trace_dissector_handle, trace_tvb, pinfo, tree);
        offset += len;
    }

    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
     */
    /* See setup for hint == 0 below */
    use_this_dissector = next_dissectors [protocol_hint];
    if(protocol_hint == 0) {
        maybe_protocol_id = tvb_get_guint8(tvb, offset);

        switch(maybe_protocol_id) {
        case IP4_TYPICAL_VERSION_LENGTH:
            use_this_dissector = next_dissectors[VLIB_NODE_PROTO_HINT_IP4];
            break;
        case IP6_TYPICAL_VERSION_AND_TRAFFIC_CLASS:
            use_this_dissector = next_dissectors[VLIB_NODE_PROTO_HINT_IP6];
            break;
        default:
            break;
        }
    }
    call_dissector(use_this_dissector, eth_tvb, pinfo, tree);
    return tvb_captured_length(tvb);
}

void
proto_register_vpp(void)
{
    static hf_register_info vpp_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_major_version,
          { "MajorVersion", "vpp.MajorVersion",  FT_UINT8, BASE_DEC, NULL, 0x0,
            NULL, HFILL },
        },
        { &hf_vpp_minor_version,
          { "MinorVersion", "vpp.MinorVersion",  FT_UINT8, BASE_DEC, NULL, 0x0,
            NULL, HFILL },
        },
        { &hf_vpp_protocol_hint,
          { "ProtocolHint", "vpp.ProtocolHint",  FT_UINT8, BASE_DEC, NULL, 0x0,
            NULL, HFILL },
        },
    };

    static ei_register_info vpp_ei[] = {
        { &ei_vpp_major_version_error,
          { "vpp.bad_major_version", PI_MALFORMED, PI_ERROR,
            "Bad Major Version", EXPFILL }},
        { &ei_vpp_minor_version_error,
          { "vpp.bad_minor_version", PI_UNDECODED, PI_WARN,
            "Bad Minor Version", EXPFILL }},
        { &ei_vpp_protocol_hint_error,
          { "vpp.bad_protocol_hint", PI_PROTOCOL, PI_WARN,
            "Bad Protocol Hint", EXPFILL }},
    };

    static hf_register_info metadata_hf[] = {
        { &hf_vpp_metadata,
          { "Metadata", "vpp.metadata",
            FT_STRINGZ, BASE_NONE, NULL, 0x0, NULL, HFILL },
        },
    };

    static hf_register_info opaque_hf[] = {
        { &hf_vpp_buffer_opaque,
          { "Opaque", "vpp.opaque",
            FT_STRINGZ, BASE_NONE, NULL, 0x0, NULL, HFILL },
        },
    };

    static hf_register_info opaque2_hf[] = {
        { &hf_vpp_buffer_opaque2,
          { "Opaque2", "vpp.opaque2",
            FT_STRINGZ, BASE_NONE, NULL, 0x0, NULL, HFILL },
        },
    };

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

    static gint *vpp_ett[] = {
        &ett_vpp,
    };
    static gint *ett_metadata[] = {
        &ett_vpp_metadata,
    };
    static gint *ett_opaque[] = {
        &ett_vpp_opaque,
    };
    static gint *ett_opaque2[] = {
        &ett_vpp_opaque2,
    };
    static gint *ett_trace[] = {
        &ett_vpp_trace,
    };

    proto_vpp = proto_register_protocol("VPP Dispatch Trace", "VPP", "vpp");
    proto_register_field_array(proto_vpp, vpp_hf, array_length(vpp_hf));
    proto_register_subtree_array(vpp_ett, array_length(vpp_ett));
    register_dissector("vpp", dissect_vpp, proto_vpp);

    expert_vpp = expert_register_protocol(proto_vpp);
    expert_register_field_array(expert_vpp, vpp_ei, array_length(vpp_ei));

    proto_vpp_metadata = proto_register_protocol("VPP Buffer Metadata",
                                                 "VPP-Metadata",
                                                 "vpp-metadata");
    proto_register_field_array(proto_vpp_metadata, metadata_hf,
                               array_length(metadata_hf));
    proto_register_subtree_array(ett_metadata, array_length(ett_metadata));
    register_dissector("vppMetadata", dissect_vpp_metadata, proto_vpp_metadata);

    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_opaque2 = proto_register_protocol("VPP Buffer Opaque2",
                                                "VPP-Opaque2", "vpp-opaque2");
    proto_register_field_array(proto_vpp_opaque2, opaque2_hf,
                               array_length(opaque2_hf));
    proto_register_subtree_array(ett_opaque2, array_length(ett_opaque2));
    register_dissector("vppOpaque2", dissect_vpp_opaque2, proto_vpp_opaque2);


    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);

#define _(idx,dname) next_dissectors[idx] = find_dissector(#dname);
    foreach_next_dissector;
#undef _

    /* if all else fails, dissect data as if ethernet MAC */
    next_dissectors[VLIB_NODE_PROTO_HINT_NONE] =
        next_dissectors [VLIB_NODE_PROTO_HINT_ETHERNET];
}

void
proto_reg_handoff_vpp(void)
{
    vpp_dissector_handle = find_dissector("vpp");
    vpp_metadata_dissector_handle = find_dissector("vppMetadata");
    vpp_opaque_dissector_handle = find_dissector("vppOpaque");
    vpp_opaque2_dissector_handle = find_dissector("vppOpaque2");
    vpp_trace_dissector_handle = find_dissector("vppTrace");
    dissector_add_uint("wtap_encap", WTAP_ENCAP_VPP, vpp_dissector_handle);
}

/*
 * Editor modelines  -  https://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:
 */