FD.io VPP  v16.06
Vector Packet Processing
cdp_input.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2011-2016 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #include <vnet/cdp/cdp_node.h>
16 
18 
19 #define DEBUG_TLV_DUMP 0 /* 1=> dump TLV's to stdout while processing them */
20 
21 /* Reliable multicast messages we use to keep peers updated */
24 
25 /*
26  * ported from an unspecified Cisco cdp implementation.
27  * Compute / return in HOST byte order. 0 => good checksum.
28  */
29 u16 cdp_checksum (void *p, int count)
30 {
31  u32 sum;
32  u16 i, *data;
33 
34  data = p;
35  sum = 0;
36  while (count > 1) {
37  sum += ntohs(*data);
38  data++;
39  count -= 2;
40  }
41 
42  if (count > 0)
43  sum += *(char *)data;
44 
45  while (sum >> 16) {
46  sum = (sum & 0xFFFF) + (sum >> 16);
47  }
48 
49  i = (i16) sum;
50  return (~i);
51 }
52 
53 /* TLV handler table */
54 typedef struct {
55  char *name;
57  void *format;
58  void *process;
60 
61 static tlv_handler_t tlv_handlers [];
62 
63 /* Display a generic TLV as a set of hex bytes */
64 static u8 * format_generic_tlv (u8 * s, va_list * va)
65 {
66  cdp_tlv_t *t = va_arg (*va, cdp_tlv_t *);
67  tlv_handler_t *h = &tlv_handlers[t->t];
68 
69  s = format (s, "%s(%d): %U\n", h->name,
70  t->t, format_hex_bytes, t->v, t->l - sizeof (*t));
71  return s;
72 }
73 
74 /* Ignore / skip a TLV we don't support */
76  cdp_tlv_t *t)
77 {
78 #if DEBUG_TLV_DUMP > 0
79  fformat(stdout, "%U", format_generic_tlv, t);
80 #endif
81 
82  return CDP_ERROR_NONE;
83 }
84 
85 /* print a text tlv */
86 static u8 * format_text_tlv (u8 * s, va_list * va)
87 {
88  cdp_tlv_t *t = va_arg (*va, cdp_tlv_t *);
89  tlv_handler_t *h = &tlv_handlers[t->t];
90  int i;
91 
92  s = format (s, "%s(%d): ", h->name, t->t);
93 
94  for (i = 0; i < (t->l - sizeof (*t)); i++)
95  vec_add1(s, t->v[i]);
96 
97  vec_add1(s, '\n');
98  return s;
99 }
100 
101 #if DEBUG_TLV_DUMP == 0
102 /* gcc warning be gone */
103 CLIB_UNUSED (static cdp_error_t
105  cdp_tlv_t *t));
106 #endif
107 
108 /* process / skip a generic text TLV that we don't support */
110  cdp_tlv_t *t)
111 {
112 #if DEBUG_TLV_DUMP > 0
113  fformat(stdout, "%U\n", format_text_tlv, t);
114 #endif
115 
116  return CDP_ERROR_NONE;
117 }
118 
119 /* per-TLV format function definitions */
120 #define format_unused_tlv format_generic_tlv
121 #define format_device_name_tlv format_text_tlv
122 #define format_address_tlv format_generic_tlv
123 #define format_port_id_tlv format_text_tlv
124 #define format_capabilities_tlv format_generic_tlv
125 #define format_version_tlv format_text_tlv
126 #define format_platform_tlv format_text_tlv
127 #define format_ipprefix_tlv format_generic_tlv
128 #define format_hello_tlv format_generic_tlv
129 #define format_vtp_domain_tlv format_generic_tlv
130 #define format_native_vlan_tlv format_generic_tlv
131 #define format_duplex_tlv format_generic_tlv
132 #define format_appl_vlan_tlv format_generic_tlv
133 #define format_trigger_tlv format_generic_tlv
134 #define format_power_tlv format_generic_tlv
135 #define format_mtu_tlv format_generic_tlv
136 #define format_trust_tlv format_generic_tlv
137 #define format_cos_tlv format_generic_tlv
138 #define format_sysname_tlv format_generic_tlv
139 #define format_sysobject_tlv format_generic_tlv
140 #define format_mgmt_addr_tlv format_generic_tlv
141 #define format_physical_loc_tlv format_generic_tlv
142 #define format_mgmt_addr2_tlv format_generic_tlv
143 #define format_power_requested_tlv format_generic_tlv
144 #define format_power_available_tlv format_generic_tlv
145 #define format_port_unidirectional_tlv format_generic_tlv
146 #define format_unknown_28_tlv format_generic_tlv
147 #define format_energywise_tlv format_generic_tlv
148 #define format_unknown_30_tlv format_generic_tlv
149 #define format_spare_poe_tlv format_generic_tlv
150 
151 /* tlv ID=0 is a mistake */
153  cdp_tlv_t *t)
154 {
155  return CDP_ERROR_BAD_TLV;
156 }
157 
158 /* list of text TLV's that we snapshoot */
159 #define foreach_text_to_struct_tlv \
160 _(device_name,DEBUG_TLV_DUMP) \
161 _(version,DEBUG_TLV_DUMP) \
162 _(platform,DEBUG_TLV_DUMP) \
163 _(port_id,DEBUG_TLV_DUMP)
164 
165 #define _(z,dbg) \
166 static \
167 cdp_error_t process_##z##_tlv (cdp_main_t *cm, cdp_neighbor_t *n, \
168  cdp_tlv_t *t) \
169 { \
170  int i; \
171  if (dbg) \
172  fformat(stdout, "%U\n", format_text_tlv, t); \
173  \
174  if (n->z) \
175  _vec_len(n->z) = 0; \
176  \
177  for (i = 0; i < (t->l - sizeof (*t)); i++) \
178  vec_add1(n->z, t->v[i]); \
179  \
180  vec_add1(n->z, 0); \
181  \
182  return CDP_ERROR_NONE; \
183 }
184 
186 #undef _
187 
188 #define process_address_tlv process_generic_tlv
189 #define process_capabilities_tlv process_generic_tlv
190 #define process_ipprefix_tlv process_generic_tlv
191 #define process_hello_tlv process_generic_tlv
192 #define process_vtp_domain_tlv process_generic_tlv
193 #define process_native_vlan_tlv process_generic_tlv
194 #define process_duplex_tlv process_generic_tlv
195 #define process_appl_vlan_tlv process_generic_tlv
196 #define process_trigger_tlv process_generic_tlv
197 #define process_power_tlv process_generic_tlv
198 #define process_mtu_tlv process_generic_tlv
199 #define process_trust_tlv process_generic_tlv
200 #define process_cos_tlv process_generic_tlv
201 #define process_sysname_tlv process_generic_tlv
202 #define process_sysobject_tlv process_generic_tlv
203 #define process_mgmt_addr_tlv process_generic_tlv
204 #define process_physical_loc_tlv process_generic_tlv
205 #define process_mgmt_addr2_tlv process_generic_tlv
206 #define process_power_requested_tlv process_generic_tlv
207 #define process_power_available_tlv process_generic_tlv
208 #define process_port_unidirectional_tlv process_generic_tlv
209 #define process_unknown_28_tlv process_generic_tlv
210 #define process_energywise_tlv process_generic_tlv
211 #define process_unknown_30_tlv process_generic_tlv
212 #define process_spare_poe_tlv process_generic_tlv
213 
214 static tlv_handler_t tlv_handlers [] =
215 {
216 #define _(a) {#a, CDP_TLV_##a, format_##a##_tlv, process_##a##_tlv},
218 #undef _
219 };
220 
221 #if DEBUG_TLV_DUMP == 0
222 CLIB_UNUSED(static u8 * format_cdp_hdr (u8 * s, va_list * va));
223 #endif
224 
225 static u8 * format_cdp_hdr (u8 * s, va_list * va)
226 {
227  cdp_hdr_t *h = va_arg (*va, cdp_hdr_t *);
228 
229  s = format (s, "version %d, ttl %d(secs), cksum 0x%04x\n",
230  h->version, h->ttl, h->checksum);
231  return s;
232 }
233 
235  cdp_hdr_t *h)
236 {
237 #if DEBUG_TLV_DUMP > 0
238  fformat(stdout, "%U", format_cdp_hdr, h);
239 #endif
240 
241  if (h->version != 1 && h->version != 2)
242  return CDP_ERROR_PROTOCOL_VERSION;
243 
244  n->ttl_in_seconds = h->ttl;
245 
246  return CDP_ERROR_NONE;
247 }
248 
249 /* scan a cdp packet; header, then tlv's */
251 {
252  u8 *cur = n->last_rx_pkt;
253  cdp_hdr_t *h;
254  cdp_tlv_t *tlv;
255  cdp_error_t e = CDP_ERROR_NONE;
256  tlv_handler_t *handler;
257  cdp_error_t (*fp)(cdp_main_t *, cdp_neighbor_t *, cdp_tlv_t *);
258  u16 computed_checksum;
259 
260  computed_checksum = cdp_checksum (cur, vec_len(cur));
261 
262  if (computed_checksum)
263  return CDP_ERROR_CHECKSUM;
264 
265  h = (cdp_hdr_t *)cur;
266 
267  e = process_cdp_hdr (cm, n, h);
268  if (e)
269  return e;
270 
271  cur = (u8 *)(h+1);
272 
273  while (cur < n->last_rx_pkt + vec_len (n->last_rx_pkt) - 1) {
274  tlv = (cdp_tlv_t *)cur;
275  tlv->t = ntohs(tlv->t);
276  tlv->l = ntohs(tlv->l);
277  if (tlv->t >= ARRAY_LEN(tlv_handlers))
278  return CDP_ERROR_BAD_TLV;
279  handler = &tlv_handlers[tlv->t];
280  fp = handler->process;
281  e = (*fp)(cm, n, tlv);
282  if (e)
283  return e;
284  /* tlv length includes (t, l) */
285  cur += tlv->l;
286  }
287 
288  return CDP_ERROR_NONE;
289 }
290 
291 /*
292  * cdp input routine
293  */
295 {
296  cdp_main_t * cm = &cdp_main;
297  cdp_neighbor_t *n;
298  uword *p, nbytes;
299  cdp_error_t e;
300  uword last_packet_signature;
301 
302  /* find or create a neighbor pool entry for the (sw) interface
303  upon which we received this pkt */
305  vnet_buffer(b0)->sw_if_index[VLIB_RX]);
306 
307  if (p == 0) {
308  pool_get (cm->neighbors, n);
309  memset (n, 0, sizeof (*n));
310  n->sw_if_index = vnet_buffer(b0)->sw_if_index[VLIB_RX];
311  n->packet_template_index = (u8)~0;
313  n - cm->neighbors);
314  } else {
315  n = pool_elt_at_index (cm->neighbors, p[0]);
316  }
317 
318  /*
319  * typical clib idiom. Don't repeatedly allocate and free
320  * the per-neighbor rx buffer. Reset its apparent length to zero
321  * and reuse it.
322  */
323 
324  if (n->last_rx_pkt)
325  _vec_len (n->last_rx_pkt) = 0;
326 
327  /* cdp disabled on this interface, we're done */
328  if (n->disabled)
329  return CDP_ERROR_DISABLED;
330 
331  /*
332  * Make sure the per-neighbor rx buffer is big enough to hold
333  * the data we're about to copy
334  */
336 
337  /*
338  * Coalesce / copy e the buffer chain into the per-neighbor
339  * rx buffer
340  */
341  nbytes = vlib_buffer_contents (vm, bi0, n->last_rx_pkt);
342  ASSERT(nbytes <= vec_len (n->last_rx_pkt));
343 
344  /*
345  * Compute Jenkins hash of the new packet, decide if we need to
346  * actually parse through the TLV's. CDP packets are all identical,
347  * so unless we time out the peer, we don't need to process the packet.
348  */
349  last_packet_signature =
350  hash_memory (n->last_rx_pkt, vec_len (n->last_rx_pkt), 0xd00b);
351 
353  n->last_packet_signature == last_packet_signature) {
354  e = CDP_ERROR_CACHE_HIT;
355  } else {
356  /* Actually scan the packet */
357  e = cdp_packet_scan (cm, n);
359  n->last_packet_signature = last_packet_signature;
360  }
361 
362  if (e == CDP_ERROR_NONE) {
363  n->last_heard = vlib_time_now (vm);
364  }
365 
366  return e;
367 }
368 
369 /*
370  * setup neighbor hash table
371  */
373 {
374  clib_error_t * error;
375  cdp_main_t *cm = &cdp_main;
376  void vnet_cdp_node_reference(void);
377 
379 
380  if ((error = vlib_call_init_function (vm, cdp_periodic_init)))
381  return error;
382 
383  cm->vlib_main = vm;
384  cm->vnet_main = vnet_get_main();
385  cm->neighbor_by_sw_if_index = hash_create (0, sizeof (uword));
386 
387  return 0;
388 }
389 
391 
392 
393 static u8 * format_cdp_neighbors (u8 * s, va_list * va)
394 {
395  CLIB_UNUSED(vlib_main_t *vm) = va_arg (*va, vlib_main_t *);
396  cdp_main_t *cm = va_arg (*va, cdp_main_t *);
397  vnet_main_t * vnm = & vnet_main;
398  cdp_neighbor_t *n;
400 
401  s = format (s,
402  "%=25s %=15s %=25s %=10s\n",
403  "Our Port", "Peer System", "Peer Port", "Last Heard");
404 
405  pool_foreach (n, cm->neighbors,
406  ({
407  hw = vnet_get_sup_hw_interface (vnm, n->sw_if_index);
408 
409  if (n->disabled == 0)
410  s = format (s, "%=25s %=15s %=25s %=10.1f\n",
411  hw->name, n->device_name, n->port_id,
412  n->last_heard);
413  }));
414  return s;
415 }
416 
417 
418 static clib_error_t *
420  unformat_input_t * input,
421  vlib_cli_command_t * cmd)
422 {
423  cdp_main_t *cm = &cdp_main;
424 
425  vlib_cli_output (vm, "%U\n", format_cdp_neighbors, vm, cm);
426 
427  return 0;
428 }
429 
430 VLIB_CLI_COMMAND (show_cdp_command, static) = {
431  .path = "show cdp",
432  .short_help = "Show cdp command",
433  .function = show_cdp,
434 };
435 
436 
437 /*
438  * packet trace format function, very similar to
439  * cdp_packet_scan except that we call the per TLV format
440  * functions instead of the per TLV processing functions
441  */
442 u8 * cdp_input_format_trace (u8 * s, va_list * args)
443 {
444  CLIB_UNUSED(vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
445  CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
446  cdp_input_trace_t * t = va_arg (*args, cdp_input_trace_t *);
447  u8 *cur;
448  cdp_hdr_t *h;
449  cdp_tlv_t *tlv;
450  tlv_handler_t *handler;
451  u8 * (*fp)(cdp_tlv_t *);
452 
453  cur = t->data;
454 
455  h = (cdp_hdr_t *)cur;
456  s = format (s, "%U", format_cdp_hdr, h);
457 
458  cur = (u8 *)(h+1);
459 
460  while (cur < t->data + t->len) {
461  tlv = (cdp_tlv_t *)cur;
462  tlv->t = ntohs(tlv->t);
463  tlv->l = ntohs(tlv->l);
464  if (tlv->t > ARRAY_LEN(tlv_handlers)) {
465  s = format (s, "BAD_TLV\n");
466  break;
467  }
468  handler = &tlv_handlers[tlv->t];
469  fp = handler->format;
470  s = format (s, " %U", fp, tlv);
471  /* tlv length includes (t, l) */
472  cur += tlv->l;
473  }
474 
475  return s;
476 }
#define vec_validate(V, I)
Make sure vector is long enough for given index (no header, unspecified alignment) ...
Definition: vec.h:394
char * name
Definition: cdp_input.c:55
#define hash_set(h, key, value)
Definition: hash.h:237
mc_serialize_msg_t serialize_cdp_keepalive_msg
Definition: cdp_input.c:23
sll srl srl sll sra u16x4 i
Definition: vector_sse2.h:267
static cdp_error_t process_unused_tlv(cdp_main_t *cm, cdp_neighbor_t *n, cdp_tlv_t *t)
Definition: cdp_input.c:152
u16 cdp_checksum(void *p, int count)
Definition: cdp_input.c:29
static clib_error_t * cdp_init(vlib_main_t *vm)
Definition: cdp_input.c:372
cdp_error_t
Definition: cdp_node.h:106
#define vec_add1(V, E)
Add 1 element to end of vector (unspecified alignment).
Definition: vec.h:480
static u8 * format_cdp_neighbors(u8 *s, va_list *va)
Definition: cdp_input.c:393
u8 packet_template_index
Definition: cdp_node.h:52
void * process
Definition: cdp_input.c:58
cdp_main_t cdp_main
Definition: cdp_input.c:17
#define pool_get(P, E)
Definition: pool.h:186
uword * neighbor_by_sw_if_index
Definition: cdp_node.h:82
static u8 * format_cdp_hdr(u8 *s, va_list *va)
Definition: cdp_input.c:225
vnet_main_t * vnet_get_main(void)
Definition: misc.c:45
#define pool_foreach(VAR, POOL, BODY)
Definition: pool.h:328
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:109
u8 * format_hex_bytes(u8 *s, va_list *va)
Definition: std-formats.c:79
always_inline uword vlib_buffer_length_in_chain(vlib_main_t *vm, vlib_buffer_t *b)
Get length in bytes of the buffer chain.
Definition: buffer_funcs.h:112
u8 last_packet_signature_valid
Definition: cdp_node.h:55
#define vlib_call_init_function(vm, x)
Definition: init.h:159
cdp_error_t cdp_input(vlib_main_t *vm, vlib_buffer_t *b0, u32 bi0)
Definition: cdp_input.c:294
vnet_main_t * vnet_main
Definition: cdp_node.h:92
CLIB_UNUSED(static cdp_error_t process_text_tlv(cdp_main_t *cm, cdp_neighbor_t *n, cdp_tlv_t *t))
#define hash_get(h, key)
Definition: hash.h:231
#define pool_elt_at_index(p, i)
Definition: pool.h:346
always_inline uword vlib_buffer_contents(vlib_main_t *vm, u32 buffer_index, u8 *contents)
Copy buffer contents to memory.
Definition: buffer_funcs.h:143
f64 last_heard
Definition: cdp_node.h:42
cdp_neighbor_t * neighbors
Definition: cdp_node.h:76
vnet_main_t vnet_main
Definition: misc.c:42
void vlib_cli_output(vlib_main_t *vm, char *fmt,...)
Definition: cli.c:538
uword last_packet_signature
Definition: cdp_node.h:56
void vnet_cdp_node_reference(void)
Definition: cdp_node.c:194
u8 * last_rx_pkt
Definition: cdp_node.h:65
static int cdp_packet_scan(cdp_main_t *cm, cdp_neighbor_t *n)
Definition: cdp_input.c:250
static clib_error_t * cdp_periodic_init(vlib_main_t *vm)
Definition: cdp_periodic.c:378
u8 ttl_in_seconds
Definition: cdp_node.h:46
static cdp_error_t process_text_tlv(cdp_main_t *cm, cdp_neighbor_t *n, cdp_tlv_t *t)
Definition: cdp_input.c:109
static cdp_error_t process_generic_tlv(cdp_main_t *cm, cdp_neighbor_t *n, cdp_tlv_t *t)
Definition: cdp_input.c:75
#define ARRAY_LEN(x)
Definition: clib.h:59
#define VLIB_CLI_COMMAND(x,...)
Definition: cli.h:150
#define hash_create(elts, value_bytes)
Definition: hash.h:615
#define ASSERT(truth)
static clib_error_t * show_cdp(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: cdp_input.c:419
unsigned int u32
Definition: types.h:88
#define vnet_buffer(b)
Definition: buffer.h:300
u8 * format(u8 *s, char *fmt,...)
Definition: format.c:405
uword hash_memory(void *p, word n_bytes, uword state)
Definition: hash.c:208
vlib_main_t * vlib_main
Definition: cdp_node.h:91
static u8 * format_generic_tlv(u8 *s, va_list *va)
Definition: cdp_input.c:64
void * format
Definition: cdp_input.c:57
u64 uword
Definition: types.h:112
unsigned short u16
Definition: types.h:57
#define foreach_text_to_struct_tlv
Definition: cdp_input.c:159
struct _mc_serialize_msg mc_serialize_msg_t
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
unsigned char u8
Definition: types.h:56
word fformat(FILE *f, char *fmt,...)
Definition: format.c:437
short i16
Definition: types.h:46
u8 * cdp_input_format_trace(u8 *s, va_list *args)
Definition: cdp_input.c:442
mc_serialize_msg_t serialize_cdp_neighbor_msg
Definition: cdp_input.c:22
always_inline f64 vlib_time_now(vlib_main_t *vm)
Definition: main.h:182
struct _unformat_input_t unformat_input_t
static cdp_error_t process_cdp_hdr(cdp_main_t *cm, cdp_neighbor_t *n, cdp_hdr_t *h)
Definition: cdp_input.c:234
u32 sw_if_index
Definition: cdp_node.h:39
static u8 * format_text_tlv(u8 *s, va_list *va)
Definition: cdp_input.c:86
Definition: defs.h:45