summaryrefslogtreecommitdiffstats
path: root/src/vnet/cdp/cdp_input.c
blob: 3574de6853489f9ba53940e06507ac45c58868b4 (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
@media only all and (prefers-color-scheme: dark) {
.highlight .hll { background-color: #49483e }
.highlight .c { color: #75715e } /* Comment */
.highlight .err { color: #960050; background-color: #1e0010 } /* Error */
.highlight .k { color: #66d9ef } /* Keyword */
.highlight .l { color: #ae81ff } /* Literal */
.highlight .n { color: #f8f8f2 } /* Name */
.highlight .o { color: #f92672 } /* Operator */
.highlight .p { color: #f8f8f2 } /* Punctuation */
.highlight .ch { color: #75715e } /* Comment.Hashbang */
.highlight .cm { color: #75715e } /* Comment.Multiline */
.highlight .cp { color: #75715e } /* Comment.Preproc */
.highlight .cpf { color: #75715e } /* Comment.PreprocFile */
.highlight .c1 { color: #75715e } /* Comment.Single */
.highlight .cs { color: #75715e } /* Comment.Special */
.highlight .gd { color: #f92672 } /* Generic.Deleted */
.highlight .ge { font-style: italic } /* Generic.Emph */
.highlight .gi { color: #a6e22e } /* Generic.Inserted */
.highlight .gs { font-weight: bold } /* Generic.Strong */
.highlight .gu { color: #75715e } /* Generic.Subheading */
.highlight .kc { color: #66d9ef } /* Keyword.Constant */
.highlight .kd { color: #66d9ef } /* Keyword.Declaration */
.highlight .kn { color: #f92672 } /* Keyword.Namespace */
.highlight .kp { color: #66d9ef } /* Keyword.Pseudo */
.highlight .kr { color: #66d9ef } /* Keyword.Reserved */
.highlight .kt { color: #66d9ef } /* Keyword.Type */
.highlight .ld { color: #e6db74 } /* Literal.Date */
.highlight .m { color: #ae81ff } /* Literal.Number */
.highlight .s { color: #e6db74 } /* Literal.String */
.highlight .na { color: #a6e22e } /* Name.Attribute */
.highlight .nb { color: #f8f8f2 } /* Name.Builtin */
.highlight .nc { color: #a6e22e } /* Name.Class */
.highlight .no { color: #66d9ef } /* Name.Constant */
.highlight .nd { color: #a6e22e } /* Name.Decorator */
.highlight .ni { color: #f8f8f2 } /* Name.Entity */
.highlight .ne { color: #a6e22e } /* Name.Exception */
.highlight .nf { color: #a6e22e } /* Name.Function */
.highlight .nl { color: #f8f8f2 } /* Name.Label */
.highlight .nn { color: #f8f8f2 } /* Name.Namespace */
.highlight .nx { color: #a6e22e } /* Name.Other */
.highlight .py { color: #f8f8f2 } /* Name.Property */
.highlight .nt { color: #f92672 } /* Name.Tag */
.highlight .nv { color: #f8f8f2 } /* Name.Variable */
.highlight .ow { color: #f92672 } /* Operator.Word */
.highlight .w { color: #f8f8f2 } /* Text.Whitespace */
.highlight .mb { color: #ae81ff } /* Literal.Number.Bin */
.highlight .mf { color: #ae81ff } /* Literal.Number.Float */
.highlight .mh { color: #ae81ff } /* Literal.Number.Hex */
.highlight .mi { color: #ae81ff } /* Literal.Number.Integer */
.highlight .mo { color: #ae81ff } /* Literal.Number.Oct */
.highlight .sa { color: #e6db74 } /* Literal.String.Affix */
.highlight .sb { color: #e6db74 } /* Literal.String.Backtick */
.highlight .sc { color: #e6db74 } /* Literal.String.Char */
.highlight .dl { color: #e6db74 } /* Literal.String.Delimiter */
.highlight .sd { color: #e6db74 } /* Literal.String.Doc */
.highlight .s2 { color: #e6db74 } /* Literal.String.Double */
.highlight .se { color: #ae81ff } /* Literal.String.Escape */
.highlight .sh { color: #e6db74 } /* Literal.String.Heredoc */
.highlight .si { color: #e6db74 } /* Literal.String.Interpol */
.highlight .sx { color: #e6db74 } /* Literal.String.Other */
.highlight .sr { color: #e6db74 } /* Literal.String.Regex */
.highlight .s1 { color: #e6db74 } /* Literal.String.Single */
.highlight .ss { color: #e6db74 } /* Literal.String.Symbol */
.highlight .bp { color: #f8f8f2 } /* Name.Builtin.Pseudo */
.highlight .fm { color: #a6e22e } /* Name.Function.Magic */
.highlight .vc { color: #f8f8f2 } /* Name.Variable.Class */
.highlight .vg { color: #f8f8f2 } /* Name.Variable.Global */
.highlight .vi { color: #f8f8f2 } /* Name.Variable.Instance */
.highlight .vm { color: #f8f8f2 } /* Name.Variable.Magic */
.highlight .il { color: #ae81ff } /* Literal.Number.Integer.Long */
}
@media (prefers-color-scheme: light) {
.highlight .hll { background-color: #ffffcc }
.highlight .c { color: #888888 } /* Comment */
.highlight .err { color: #a61717; background-color: #e3d2d2 } /* Error */
.highlight .k { color: #008800; font-weight: bold } /* Keyword */
.highlight .ch { color: #888888 } /* Comment.Hashbang */
.highlight .cm { color: #888888 } /* Comment.Multiline */
.highlight .cp { color: #cc0000; font-weight: bold } /* Comment.Preproc */
.highlight .cpf { color: #888888 } /* Comment.PreprocFile */
.highlight .c1 { color: #888888 } /* Comment.Single */
.highlight .cs { color: #cc0000; font-weight: bold; background-color: #fff0f0 } /* Comment.Special */
.highlight .gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */
.highlight .ge { font-style: italic } /* Generic.Emph */
.highlight .gr { color: #aa0000 } /* Generic.Error */
.highlight .gh { color: #333333 } /* Generic.Heading */
.highlight .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */
.highlight .go { color: #888888 } /* Generic.Output */
.highlight .gp { color: #555555 } /* Generic.Prompt */
.highlight .gs { font-weight: bold } /* Generic.Strong */
.highlight .gu { color: #666666 } /* Generic.Subheading */
.highlight .gt { color: #aa0000 } /* Generic.Traceback */
.highlight .kc { color: #008800; font-weight: bold } /* Keyword.Constant */
.highlight .kd { color: #008800; font-weight: bold } /* Keyword.Declaration */
.highlight .kn { color: #008800; font-weight: bold } /* Keyword.Namespace */
.highlight .kp { color: #008800 } /* Keyword.Pseudo */
.highlight .kr { color: #008800; font-weight: bold } /* Keyword.Reserved */
.highlight .kt { color: #888888; font-weight: bold } /* Keyword.Type */
.highlight .m { color: #0000DD; font-weight: bold } /* Literal.Number */
.highlight .s { color: #dd2200; background-color: #fff0f0 } /* Literal.String */
.highlight .na { color: #336699 } /* Name.Attribute */
.highlight .nb { color: #003388 } /* Name.Builtin */
.highlight .nc { color: #bb0066; font-weight: bold } /* Name.Class */
.highlight .no { color: #003366; font-weight: bold } /* Name.Constant */
.highlight .nd { color: #555555 } /* Name.Decorator */
.highlight .ne { color: #bb0066; font-weight: bold } /* Name.Exception */
.highlight .nf { color: #0066bb; font-weight: bold } /* Name.Function */
.highlight .nl { color: #336699; font-style: italic } /* Name.Label */
.highlight .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */
.highlight .py { color: #336699; font-weight: bold } /* Name.Property */
.highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */
.highlight .nv { color: #336699 } /* Name.Variable */
.highlight .ow { color: #008800 } /* Operator.Word */
.highlight .w { color: #bbbbbb } /* Text.Whitespace */
.highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */
.highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */
.highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */
.highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */
.highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */
.highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */
.highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */
.highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */
.highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */
.highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */
.highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */
.highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */
.highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */
.highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */
.highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */
.highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */
.highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */
.highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */
.highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */
.highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */
.highlight .vc { color: #336699 } /* Name.Variable.Class */
.highlight .vg { color: #dd7700 } /* Name.Variable.Global */
.highlight .vi { color: #3333bb } /* Name.Variable.Instance */
.highlight .vm { color: #336699 } /* Name.Variable.Magic */
.highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */
}
# Set the default behavior, in case people don't have core.autocrlf set.
* text=auto

# Explicitly declare text files you want to always be normalized and converted
# to native line endings on checkout.
*.robot text
*.rst text
*.yaml text
*.vat text

## Declare files that will always have CRLF line endings on checkout.
#*.sln text eol=crlf

# Denote all files that are truly binary and should not be modified.
*.png binary
*.jpg binary
@media only all and (prefers-color-scheme: dark) { .highlight .hll { background-color: #49483e } .highlight .c { color: #75715e } /* Comment */ .highlight .err { color: #960050; background-color: #1e0010 } /* Error */ .highlight .k { color: #66d9ef } /* Keyword */ .highlight .l { color: #ae81ff } /* Literal */ .highlight .n { color: #f8f8f2 } /* Name */ .highlight .o { color: #f92672 } /* Operator */ .highlight .p { color: #f8f8f2 } /* Punctuation */ .highlight .ch { color: #75715e } /* Comment.Hashbang */ .highlight .cm { color: #75715e } /* Comment.Multiline */ .highlight .cp { color: #75715e } /* Comment.Preproc */ .highlight .cpf { color: #75715e } /* Comment.PreprocFile */ .highlight .c1 { color: #75715e } /* Comment.Single */ .highlight .cs { color: #75715e } /* Comment.Special */ .highlight .gd { color: #f92672 } /* Generic.Deleted */ .highlight .ge { font-style: italic } /* Generic.Emph */ .highlight .gi { color: #a6e22e } /* Generic.Inserted */ .highlight .gs { font-weight: bold } /* Generic.Strong */ .highlight .gu { color: #75715e } /* Generic.Subheading */ .highlight .kc { color: #66d9ef } /* Keyword.Constant */ .highlight .kd { color: #66d9ef } /* Keyword.Declaration */ .highlight .kn { color: #f92672 } /* Keyword.Namespace */ .highlight .kp { color: #66d9ef } /* Keyword.Pseudo */ .highlight .kr { color: #66d9ef } /* Keyword.Reserved */ .highlight .kt { color: #66d9ef } /* Keyword.Type */ .highlight .ld { color: #e6db74 } /* Literal.Date */ .highlight .m { color: #ae81ff } /* Literal.Number */ .highlight .s { color: #e6db74 } /* Literal.String */ .highlight .na { color: #a6e22e } /* Name.Attribute */ .highlight .nb { color: #f8f8f2 } /* Name.Builtin */ .highlight .nc { color: #a6e22e } /* Name.Class */ .highlight .no { color: #66d9ef } /* Name.Constant */ .highlight .nd { color: #a6e22e } /* Name.Decorator */ .highlight .ni { color: #f8f8f2 } /* Name.Entity */ .highlight .ne { color: #a6e22e } /* Name.Exception */ .highlight .nf { color: #a6e22e } /* Name.Function */ .highlight .nl { color: #f8f8f2 } /* Name.Label */ .highlight .nn { color: #f8f8f2 } /* Name.Namespace */ .highlight .nx { color: #a6e22e } /* Name.Other */ .highlight .py { color: #f8f8f2 } /* Name.Property */ .highlight .nt { color: #f92672 } /* Name.Tag */ .highlight .nv { color: #f8f8f2 } /* Name.Variable */ .highlight .ow { color: #f92672 } /* Operator.Word */ .highlight .w { color: #f8f8f2 } /* Text.Whitespace */ .highlight .mb { color: #ae81ff } /* Literal.Number.Bin */ .highlight .mf { color: #ae81ff } /* Literal.Number.Float */ .highlight .mh { color: #ae81ff } /* Literal.Number.Hex */ .highlight .mi { color: #ae81ff } /* Literal.Number.Integer */ .highlight .mo { color: #ae81ff } /* Literal.Number.Oct */ .highlight .sa { color: #e6db74 } /* Literal.String.Affix */ .highlight .sb { color: #e6db74 } /* Literal.String.Backtick */ .highlight .sc { color: #e6db74 } /* Literal.String.Char */ .highlight .dl { color: #e6db74 } /* Literal.String.Delimiter */ .highlight .sd { color: #e6db74 } /* Literal.String.Doc */ .highlight .s2 { color: #e6db74 } /* Literal.String.Double */ .highlight .se { color: #ae81ff } /* Literal.String.Escape */ .highlight .sh { color: #e6db74 } /* Literal.String.Heredoc */ .highlight .si { color: #e6db74 } /* Literal.String.Interpol */ .highlight .sx { color: #e6db74 } /* Literal.String.Other */ .highlight .sr { color: #e6db74 } /* Literal.String.Regex */ .highlight .s1 { color: #e6db74 } /* Literal.String.Single */ .highlight .ss { color: #e6db74 } /* Literal.String.Symbol */ .highlight .bp { color: #f8f8f2 } /* Name.Builtin.Pseudo */ .highlight .fm { color: #a6e22e } /* Name.Function.Magic */ .highlight .vc { color: #f8f8f2 } /* Name.Variable.Class */ .highlight .vg { color: #f8f8f2 } /* Name.Variable.Global */ .highlight .vi { color: #f8f8f2 } /* Name.Variable.Instance */ .highlight .vm { color: #f8f8f2 } /* Name.Variable.Magic */ .highlight .il { color: #ae81ff } /* Literal.Number.Integer.Long */ } @media (prefers-color-scheme: light) { .highlight .hll { background-color: #ffffcc } .highlight .c { color: #888888 } /* Comment */ .highlight .err { color: #a61717; background-color: #e3d2d2 } /* Error */ .highlight .k { color: #008800; font-weight: bold } /* Keyword */ .highlight .ch { color: #888888 } /* Comment.Hashbang */ .highlight .cm { color: #888888 } /* Comment.Multiline */ .highlight .cp { color: #cc0000; font-weight: bold } /* Comment.Preproc */ .highlight .cpf { color: #888888 } /* Comment.PreprocFile */ .highlight .c1 { color: #888888 } /* Comment.Single */ .highlight .cs { color: #cc0000; font-weight: bold; background-color: #fff0f0 } /* Comment.Special */ .highlight .gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */ .highlight .ge { font-style: italic } /* Generic.Emph */ .highlight .gr { color: #aa0000 } /* Generic.Error */ .highlight .gh { color: #333333 } /* Generic.Heading */ .highlight .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */ .highlight .go { color: #888888 } /* Generic.Output */ .highlight .gp { color: #555555 } /* Generic.Prompt */ .highlight .gs { font-weight: bold } /* Generic.Strong */ .highlight .gu { color: #666666 } /* Generic.Subheading */ .highlight .gt { color: #aa0000 } /* Generic.Traceback */ .highlight .kc { color: #008800; font-weight: bold } /* Keyword.Constant */ .highlight .kd { color: #008800; font-weight: bold } /* Keyword.Declaration */ .highlight .kn { color: #008800; font-weight: bold } /* Keyword.Namespace */ .highlight .kp { color: #008800 } /* Keyword.Pseudo */ .highlight .kr { color: #008800; font-weight: bold } /* Keyword.Reserved */ .highlight .kt { color: #888888; font-weight: bold } /* Keyword.Type */ .highlight .m { color: #0000DD; font-weight: bold } /* Literal.Number */ .highlight .s { color: #dd2200; background-color: #fff0f0 } /* Literal.String */ .highlight .na { color: #336699 } /* Name.Attribute */ .highlight .nb { color: #003388 } /* Name.Builtin */ .highlight .nc { color: #bb0066; font-weight: bold } /* Name.Class */ .highlight .no { color: #003366; font-weight: bold } /* Name.Constant */ .highlight .nd { color: #555555 } /* Name.Decorator */ .highlight .ne { color: #bb0066; font-weight: bold } /* Name.Exception */ .highlight .nf { color: #0066bb; font-weight: bold } /* Name.Function */ .highlight .nl { color: #336699; font-style: italic } /* Name.Label */ .highlight .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */ .highlight .py { color: #336699; font-weight: bold } /* Name.Property */ .highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */ .highlight .nv { color: #336699 } /* Name.Variable */ .highlight .ow { color: #008800 } /* Operator.Word */ .highlight .w { color: #bbbbbb } /* Text.Whitespace */ .highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */ .highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */ .highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */ .highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */ .highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */ .highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */ .highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */ .highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */ .highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */ .highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */ .highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */ .highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */ .highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */ .highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */ .highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */ .highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */ .highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */ .highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */ .highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */ .highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */ .highlight .vc { color: #336699 } /* Name.Variable.Class */ .highlight .vg { color: #dd7700 } /* Name.Variable.Global */ .highlight .vi { color: #3333bb } /* Name.Variable.Instance */ .highlight .vm { color: #336699 } /* Name.Variable.Magic */ .highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */ }
/*
 * Copyright (c) 2011-2016 Cisco and/or its affiliates.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#include <vnet/cdp/cdp_node.h>

cdp_main_t cdp_main;

#define DEBUG_TLV_DUMP 0	/* 1=> dump TLV's to stdout while processing them */

/* Reliable multicast messages we use to keep peers updated */
mc_serialize_msg_t serialize_cdp_neighbor_msg;
mc_serialize_msg_t serialize_cdp_keepalive_msg;

/*
 * ported from an unspecified Cisco cdp implementation.
 * Compute / return in HOST byte order. 0 => good checksum.
 */
u16
cdp_checksum (void *p, int count)
{
  u32 sum;
  u16 i, *data;

  data = p;
  sum = 0;
  while (count > 1)
    {
      sum += ntohs (*data);
      data++;
      count -= 2;
    }

  if (count > 0)
    sum += *(char *) data;

  while (sum >> 16)
    {
      sum = (sum & 0xFFFF) + (sum >> 16);
    }

  i = (i16) sum;
  return (~i);
}

/* TLV handler table */
typedef struct
{
  char *name;
  u32 tlv_id;
  void *format;
  void *process;
} tlv_handler_t;

static tlv_handler_t tlv_handlers[];

/* Display a generic TLV as a set of hex bytes */
static u8 *
format_generic_tlv (u8 * s, va_list * va)
{
  cdp_tlv_t *t = va_arg (*va, cdp_tlv_t *);
  tlv_handler_t *h = &tlv_handlers[t->t];

  s = format (s, "%s(%d): %U\n", h->name,
	      t->t, format_hex_bytes, t->v, t->l - sizeof (*t));
  return s;
}

/* Ignore / skip a TLV we don't support */
static cdp_error_t
process_generic_tlv (cdp_main_t * cm, cdp_neighbor_t * n, cdp_tlv_t * t)
{
#if DEBUG_TLV_DUMP > 0
  fformat (stdout, "%U", format_generic_tlv, t);
#endif

  return CDP_ERROR_NONE;
}

/* print a text tlv */
static u8 *
format_text_tlv (u8 * s, va_list * va)
{
  cdp_tlv_t *t = va_arg (*va, cdp_tlv_t *);
  tlv_handler_t *h = &tlv_handlers[t->t];
  int i;

  s = format (s, "%s(%d): ", h->name, t->t);

  for (i = 0; i < (t->l - sizeof (*t)); i++)
    vec_add1 (s, t->v[i]);

  vec_add1 (s, '\n');
  return s;
}

#if DEBUG_TLV_DUMP == 0
/* gcc warning be gone */
CLIB_UNUSED (static cdp_error_t
	     process_text_tlv (cdp_main_t * cm, cdp_neighbor_t * n,
			       cdp_tlv_t * t));
#endif

/* process / skip a generic text TLV that we don't support */
static cdp_error_t
process_text_tlv (cdp_main_t * cm, cdp_neighbor_t * n, cdp_tlv_t * t)
{
#if DEBUG_TLV_DUMP > 0
  fformat (stdout, "%U\n", format_text_tlv, t);
#endif

  return CDP_ERROR_NONE;
}

/* per-TLV format function definitions */
#define format_unused_tlv format_generic_tlv
#define format_device_name_tlv format_text_tlv
#define format_address_tlv format_generic_tlv
#define format_port_id_tlv format_text_tlv
#define format_capabilities_tlv format_generic_tlv
#define format_version_tlv format_text_tlv
#define format_platform_tlv format_text_tlv
#define format_ipprefix_tlv format_generic_tlv
#define format_hello_tlv format_generic_tlv
#define format_vtp_domain_tlv format_generic_tlv
#define format_native_vlan_tlv format_generic_tlv
#define format_duplex_tlv format_generic_tlv
#define format_appl_vlan_tlv format_generic_tlv
#define format_trigger_tlv format_generic_tlv
#define format_power_tlv format_generic_tlv
#define format_mtu_tlv format_generic_tlv
#define format_trust_tlv format_generic_tlv
#define format_cos_tlv format_generic_tlv
#define format_sysname_tlv format_generic_tlv
#define format_sysobject_tlv format_generic_tlv
#define format_mgmt_addr_tlv format_generic_tlv
#define format_physical_loc_tlv format_generic_tlv
#define format_mgmt_addr2_tlv format_generic_tlv
#define format_power_requested_tlv format_generic_tlv
#define format_power_available_tlv format_generic_tlv
#define format_port_unidirectional_tlv format_generic_tlv
#define format_unknown_28_tlv format_generic_tlv
#define format_energywise_tlv format_generic_tlv
#define format_unknown_30_tlv format_generic_tlv
#define format_spare_poe_tlv format_generic_tlv

/* tlv ID=0 is a mistake */
static cdp_error_t
process_unused_tlv (cdp_main_t * cm, cdp_neighbor_t * n, cdp_tlv_t * t)
{
  return CDP_ERROR_BAD_TLV;
}

/* list of text TLV's that we snapshoot */
#define foreach_text_to_struct_tlv              \
_(device_name,DEBUG_TLV_DUMP)                   \
_(version,DEBUG_TLV_DUMP)                       \
_(platform,DEBUG_TLV_DUMP)                      \
_(port_id,DEBUG_TLV_DUMP)

#define _(z,dbg)                                                        \
static                                                                  \
cdp_error_t process_##z##_tlv (cdp_main_t *cm, cdp_neighbor_t *n,       \
                                  cdp_tlv_t *t)                         \
{                                                                       \
    int i;                                                              \
    if (dbg)                                                            \
       fformat(stdout, "%U\n", format_text_tlv, t);                     \
                                                                        \
    if (n->z)                                                           \
        _vec_len(n->z) = 0;                                             \
                                                                        \
    for (i = 0; i < (t->l - sizeof (*t)); i++)                          \
        vec_add1(n->z, t->v[i]);                                        \
                                                                        \
    vec_add1(n->z, 0);                                                  \
                                                                        \
    return CDP_ERROR_NONE;                                              \
}

foreach_text_to_struct_tlv
#undef _
#define process_address_tlv process_generic_tlv
#define process_capabilities_tlv process_generic_tlv
#define process_ipprefix_tlv process_generic_tlv
#define process_hello_tlv process_generic_tlv
#define process_vtp_domain_tlv process_generic_tlv
#define process_native_vlan_tlv process_generic_tlv
#define process_duplex_tlv process_generic_tlv
#define process_appl_vlan_tlv process_generic_tlv
#define process_trigger_tlv process_generic_tlv
#define process_power_tlv process_generic_tlv
#define process_mtu_tlv process_generic_tlv
#define process_trust_tlv process_generic_tlv
#define process_cos_tlv process_generic_tlv
#define process_sysname_tlv process_generic_tlv
#define process_sysobject_tlv process_generic_tlv
#define process_mgmt_addr_tlv process_generic_tlv
#define process_physical_loc_tlv process_generic_tlv
#define process_mgmt_addr2_tlv process_generic_tlv
#define process_power_requested_tlv process_generic_tlv
#define process_power_available_tlv process_generic_tlv
#define process_port_unidirectional_tlv process_generic_tlv
#define process_unknown_28_tlv process_generic_tlv
#define process_energywise_tlv process_generic_tlv
#define process_unknown_30_tlv process_generic_tlv
#define process_spare_poe_tlv process_generic_tlv
static tlv_handler_t tlv_handlers[] = {
#define _(a) {#a, CDP_TLV_##a, format_##a##_tlv, process_##a##_tlv},
  foreach_cdp_tlv_type
#undef _
};

#if DEBUG_TLV_DUMP == 0
CLIB_UNUSED (static u8 * format_cdp_hdr (u8 * s, va_list * va));
#endif

static u8 *
format_cdp_hdr (u8 * s, va_list * va)
{
  cdp_hdr_t *h = va_arg (*va, cdp_hdr_t *);

  s = format (s, "version %d, ttl %d(secs), cksum 0x%04x\n",
	      h->version, h->ttl, h->checksum);
  return s;
}

static cdp_error_t
process_cdp_hdr (cdp_main_t * cm, cdp_neighbor_t * n, cdp_hdr_t * h)
{
#if DEBUG_TLV_DUMP > 0
  fformat (stdout, "%U", format_cdp_hdr, h);
#endif

  if (h->version != 1 && h->version != 2)
    return CDP_ERROR_PROTOCOL_VERSION;

  n->ttl_in_seconds = h->ttl;

  return CDP_ERROR_NONE;
}

/* scan a cdp packet; header, then tlv's */
static int
cdp_packet_scan (cdp_main_t * cm, cdp_neighbor_t * n)
{
  u8 *cur = n->last_rx_pkt;
  cdp_hdr_t *h;
  cdp_tlv_t *tlv;
  cdp_error_t e = CDP_ERROR_NONE;
  tlv_handler_t *handler;
  cdp_error_t (*fp) (cdp_main_t *, cdp_neighbor_t *, cdp_tlv_t *);
  u16 computed_checksum;

  computed_checksum = cdp_checksum (cur, vec_len (cur));

  if (computed_checksum)
    return CDP_ERROR_CHECKSUM;

  h = (cdp_hdr_t *) cur;

  e = process_cdp_hdr (cm, n, h);
  if (e)
    return e;

  cur = (u8 *) (h + 1);

  while (cur < n->last_rx_pkt + vec_len (n->last_rx_pkt) - 1)
    {
      tlv = (cdp_tlv_t *) cur;
      tlv->t = ntohs (tlv->t);
      tlv->l = ntohs (tlv->l);
      if (tlv->t >= ARRAY_LEN (tlv_handlers))
	return CDP_ERROR_BAD_TLV;
      handler = &tlv_handlers[tlv->t];
      fp = handler->process;
      e = (*fp) (cm, n, tlv);
      if (e)
	return e;
      /* tlv length includes (t, l) */
      cur += tlv->l;
    }

  return CDP_ERROR_NONE;
}

/*
 * cdp input routine
 */
cdp_error_t
cdp_input (vlib_main_t * vm, vlib_buffer_t * b0, u32 bi0)
{
  cdp_main_t *cm = &cdp_main;
  cdp_neighbor_t *n;
  uword *p, nbytes;
  cdp_error_t e;
  uword last_packet_signature;

  /* find or create a neighbor pool entry for the (sw) interface
     upon which we received this pkt */
  p = hash_get (cm->neighbor_by_sw_if_index,
		vnet_buffer (b0)->sw_if_index[VLIB_RX]);

  if (p == 0)
    {
      pool_get (cm->neighbors, n);
      memset (n, 0, sizeof (*n));
      n->sw_if_index = vnet_buffer (b0)->sw_if_index[VLIB_RX];
      n->packet_template_index = (u8) ~ 0;
      hash_set (cm->neighbor_by_sw_if_index, n->sw_if_index,
		n - cm->neighbors);
    }
  else
    {
      n = pool_elt_at_index (cm->neighbors, p[0]);
    }

  /*
   * typical clib idiom. Don't repeatedly allocate and free
   * the per-neighbor rx buffer. Reset its apparent length to zero
   * and reuse it.
   */

  if (n->last_rx_pkt)
    _vec_len (n->last_rx_pkt) = 0;

  /* cdp disabled on this interface, we're done */
  if (n->disabled)
    return CDP_ERROR_DISABLED;

  /*
   * Make sure the per-neighbor rx buffer is big enough to hold
   * the data we're about to copy
   */
  vec_validate (n->last_rx_pkt, vlib_buffer_length_in_chain (vm, b0) - 1);

  /*
   * Coalesce / copy e the buffer chain into the per-neighbor
   * rx buffer
   */
  nbytes = vlib_buffer_contents (vm, bi0, n->last_rx_pkt);
  ASSERT (nbytes <= vec_len (n->last_rx_pkt));

  /*
   * Compute Jenkins hash of the new packet, decide if we need to
   * actually parse through the TLV's. CDP packets are all identical,
   * so unless we time out the peer, we don't need to process the packet.
   */
  last_packet_signature =
    hash_memory (n->last_rx_pkt, vec_len (n->last_rx_pkt), 0xd00b);

  if (n->last_packet_signature_valid &&
      n->last_packet_signature == last_packet_signature)
    {
      e = CDP_ERROR_CACHE_HIT;
    }
  else
    {
      /* Actually scan the packet */
      e = cdp_packet_scan (cm, n);
      n->last_packet_signature_valid = 1;
      n->last_packet_signature = last_packet_signature;
    }

  if (e == CDP_ERROR_NONE)
    {
      n->last_heard = vlib_time_now (vm);
    }

  return e;
}

/*
 * setup neighbor hash table
 */
static clib_error_t *
cdp_init (vlib_main_t * vm)
{
  clib_error_t *error;
  cdp_main_t *cm = &cdp_main;
  void vnet_cdp_node_reference (void);

  vnet_cdp_node_reference ();

  if ((error = vlib_call_init_function (vm, cdp_periodic_init)))
    return error;

  cm->vlib_main = vm;
  cm->vnet_main = vnet_get_main ();
  cm->neighbor_by_sw_if_index = hash_create (0, sizeof (uword));

  return 0;
}

VLIB_INIT_FUNCTION (cdp_init);


static u8 *
format_cdp_neighbors (u8 * s, va_list * va)
{
  CLIB_UNUSED (vlib_main_t * vm) = va_arg (*va, vlib_main_t *);
  cdp_main_t *cm = va_arg (*va, cdp_main_t *);
  vnet_main_t *vnm = &vnet_main;
  cdp_neighbor_t *n;
  vnet_hw_interface_t *hw;

  s = format (s,
	      "%=25s %=15s %=25s %=10s\n",
	      "Our Port", "Peer System", "Peer Port", "Last Heard");

  /* *INDENT-OFF* */
  pool_foreach (n, cm->neighbors,
  ({
    hw = vnet_get_sup_hw_interface (vnm, n->sw_if_index);

    if (n->disabled == 0)
      s = format (s, "%=25s %=15s %=25s %=10.1f\n",
                  hw->name, n->device_name, n->port_id,
                  n->last_heard);
  }));
  /* *INDENT-ON* */
  return s;
}


static clib_error_t *
show_cdp (vlib_main_t * vm,
	  unformat_input_t * input, vlib_cli_command_t * cmd)
{
  cdp_main_t *cm = &cdp_main;

  vlib_cli_output (vm, "%U\n", format_cdp_neighbors, vm, cm);

  return 0;
}

/* *INDENT-OFF* */
VLIB_CLI_COMMAND (show_cdp_command, static) = {
  .path = "show cdp",
  .short_help = "Show cdp command",
  .function = show_cdp,
};
/* *INDENT-ON* */


/*
 * packet trace format function, very similar to
 * cdp_packet_scan except that we call the per TLV format
 * functions instead of the per TLV processing functions
 */
u8 *
cdp_input_format_trace (u8 * s, va_list * args)
{
  CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
  CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
  cdp_input_trace_t *t = va_arg (*args, cdp_input_trace_t *);
  u8 *cur;
  cdp_hdr_t *h;
  cdp_tlv_t *tlv;
  tlv_handler_t *handler;
  u8 *(*fp) (cdp_tlv_t *);

  cur = t->data;

  h = (cdp_hdr_t *) cur;
  s = format (s, "%U", format_cdp_hdr, h);

  cur = (u8 *) (h + 1);

  while (cur < t->data + t->len)
    {
      tlv = (cdp_tlv_t *) cur;
      tlv->t = ntohs (tlv->t);
      tlv->l = ntohs (tlv->l);
      if (tlv->t >= ARRAY_LEN (tlv_handlers))
	{
	  s = format (s, "BAD_TLV\n");
	  break;
	}
      handler = &tlv_handlers[tlv->t];
      fp = handler->format;
      s = format (s, "  %U", fp, tlv);
      /* tlv length includes (t, l) */
      cur += tlv->l;
    }

  return s;
}

/*
 * fd.io coding-style-patch-verification: ON
 *
 * Local Variables:
 * eval: (c-set-style "gnu")
 * End:
 */