FD.io VPP  v18.04-17-g3a0d853
Vector Packet Processing
cdp_periodic.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2011-2018 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 <cdp/cdp.h>
16 #include <vppinfra/hash.h>
17 #include <vnet/unix/pcap.h>
18 #include <vnet/srp/srp.h>
19 #include <vnet/ppp/ppp.h>
20 #include <vnet/hdlc/hdlc.h>
21 #include <vnet/srp/packet.h>
22 
23 /*
24  * Generate a set of specific CDP TLVs.
25  *
26  * $$$ eventually these need to fish better data from
27  * other data structures; e.g. the hostname, software version info
28  * etc.
29  */
30 
31 static void
33 {
34  cdp_tlv_t *t = (cdp_tlv_t *) * t0p;
35 
36  t->t = htons (CDP_TLV_device_name);
37  t->l = htons (3 + sizeof (*t));
38  clib_memcpy (&t->v, "VPP", 3);
39 
40  *t0p += ntohs (t->l);
41 }
42 
43 static void
45 {
46  cdp_tlv_t *t = (cdp_tlv_t *) * t0p;
47 
48  t->t = htons (CDP_TLV_port_id);
49  t->l = htons (vec_len (hw->name) + sizeof (*t));
50  clib_memcpy (&t->v, hw->name, vec_len (hw->name));
51  *t0p += ntohs (t->l);
52 }
53 
54 static void
56 {
57  cdp_tlv_t *t = (cdp_tlv_t *) * t0p;
58 
59  t->t = htons (CDP_TLV_version);
60  t->l = htons (12 + sizeof (*t));
61  clib_memcpy (&t->v, "VPP Software", 12);
62  *t0p += ntohs (t->l);
63 }
64 
65 static void
67 {
68  cdp_tlv_t *t = (cdp_tlv_t *) * t0p;
69 
70  t->t = htons (CDP_TLV_platform);
71  t->l = htons (2 + sizeof (*t));
72  clib_memcpy (&t->v, "SW", 2);
73  *t0p += ntohs (t->l);
74 }
75 
76 static void
78 {
79  cdp_tlv_t *t = (cdp_tlv_t *) * t0p;
80  u32 capabilities;
81 
82  t->t = htons (CDP_TLV_capabilities);
83  t->l = htons (4 + sizeof (*t));
84  capabilities = CDP_ROUTER_DEVICE;
85  capabilities = htonl (capabilities);
86  clib_memcpy (&t->v, &capabilities, sizeof (capabilities));
87  *t0p += ntohs (t->l);
88 }
89 
90 static void
92 {
93  add_device_name_tlv (hw, t0p);
94  add_port_id_tlv (hw, t0p);
95  add_version_tlv (hw, t0p);
96  add_platform_tlv (hw, t0p);
97  add_capability_tlv (hw, t0p);
98 }
99 
100 /*
101  * send a cdp pkt on an ethernet interface
102  */
103 static void
105 {
106  u32 *to_next;
107  ethernet_llc_snap_and_cdp_header_t *h0;
109  u32 bi0;
110  vlib_buffer_t *b0;
111  u8 *t0;
112  u16 checksum;
113  int nbytes_to_checksum;
114  int i;
115  vlib_frame_t *f;
116  vlib_main_t *vm = cm->vlib_main;
117  vnet_main_t *vnm = cm->vnet_main;
118 
119  for (i = 0; i < count; i++)
120  {
121  /*
122  * see cdp_periodic_init() to understand what's already painted
123  * into the buffer by the packet template mechanism
124  */
126  (vm, &cm->packet_templates[n->packet_template_index], &bi0);
127 
128  if (!h0)
129  break;
130 
131  /* Add the interface's ethernet source address */
132  hw = vnet_get_sup_hw_interface (vnm, n->sw_if_index);
133 
134  clib_memcpy (h0->ethernet.src_address, hw->hw_address,
135  vec_len (hw->hw_address));
136 
137  t0 = (u8 *) & h0->cdp.data;
138 
139  /* add TLVs */
140  add_tlvs (cm, hw, &t0);
141 
142  /* add the cdp packet checksum */
143  nbytes_to_checksum = t0 - (u8 *) & h0->cdp;
144  checksum = cdp_checksum (&h0->cdp, nbytes_to_checksum);
145  h0->cdp.checksum = htons (checksum);
146 
147  /* Set the outbound packet length */
148  b0 = vlib_get_buffer (vm, bi0);
149  b0->current_length = nbytes_to_checksum + sizeof (*h0)
150  - sizeof (cdp_hdr_t);
151 
152  /* And the outbound interface */
153  vnet_buffer (b0)->sw_if_index[VLIB_TX] = hw->sw_if_index;
154 
155  /* Set the 802.3 ethernet length */
156  h0->ethernet.len = htons (b0->current_length
157  - sizeof (ethernet_802_3_header_t));
158 
159  /* And output the packet on the correct interface */
161  to_next = vlib_frame_vector_args (f);
162  to_next[0] = bi0;
163  f->n_vectors = 1;
164 
166  n->last_sent = vlib_time_now (vm);
167  }
168 }
169 
170 /*
171  * send a cdp pkt on an hdlc interface
172  */
173 static void
175 {
176  u32 *to_next;
177  hdlc_and_cdp_header_t *h0;
179  u32 bi0;
180  vlib_buffer_t *b0;
181  u8 *t0;
182  u16 checksum;
183  int nbytes_to_checksum;
184  int i;
185  vlib_frame_t *f;
186  vlib_main_t *vm = cm->vlib_main;
187  vnet_main_t *vnm = cm->vnet_main;
188 
189  for (i = 0; i < count; i++)
190  {
191  /*
192  * see cdp_periodic_init() to understand what's already painted
193  * into the buffer by the packet template mechanism
194  */
196  (vm, &cm->packet_templates[n->packet_template_index], &bi0);
197 
198  hw = vnet_get_sup_hw_interface (vnm, n->sw_if_index);
199 
200  t0 = (u8 *) & h0->cdp.data;
201 
202  /* add TLVs */
203  add_tlvs (cm, hw, &t0);
204 
205  /* add the cdp packet checksum */
206  nbytes_to_checksum = t0 - (u8 *) & h0->cdp;
207  checksum = cdp_checksum (&h0->cdp, nbytes_to_checksum);
208  h0->cdp.checksum = htons (checksum);
209 
210  /* Set the outbound packet length */
211  b0 = vlib_get_buffer (vm, bi0);
212  b0->current_length = nbytes_to_checksum + sizeof (*h0)
213  - sizeof (cdp_hdr_t);
214 
215  /* And output the packet on the correct interface */
217  to_next = vlib_frame_vector_args (f);
218  to_next[0] = bi0;
219  f->n_vectors = 1;
220 
222  n->last_sent = vlib_time_now (vm);
223  }
224 }
225 
226 /*
227  * send a cdp pkt on an srp interface
228  */
229 static void
231 {
232  u32 *to_next;
233  srp_and_cdp_header_t *h0;
235  u32 bi0;
236  vlib_buffer_t *b0;
237  u8 *t0;
238  u16 checksum;
239  int nbytes_to_checksum;
240  int i;
241  vlib_frame_t *f;
242  vlib_main_t *vm = cm->vlib_main;
243  vnet_main_t *vnm = cm->vnet_main;
244 
245  for (i = 0; i < count; i++)
246  {
247  /*
248  * see cdp_periodic_init() to understand what's already painted
249  * into the buffer by the packet template mechanism
250  */
252  (vm, &cm->packet_templates[n->packet_template_index], &bi0);
253 
254  hw = vnet_get_sup_hw_interface (vnm, n->sw_if_index);
255 
256  t0 = (u8 *) & h0->cdp.data;
257 
258  /* add TLVs */
259  add_tlvs (cm, hw, &t0);
260 
261  /* Add the interface's ethernet source address */
262  clib_memcpy (h0->ethernet.src_address, hw->hw_address,
263  vec_len (hw->hw_address));
264 
265  /* add the cdp packet checksum */
266  nbytes_to_checksum = t0 - (u8 *) & h0->cdp;
267  checksum = cdp_checksum (&h0->cdp, nbytes_to_checksum);
268  h0->cdp.checksum = htons (checksum);
269 
270  /* Set the outbound packet length */
271  b0 = vlib_get_buffer (vm, bi0);
272  b0->current_length = nbytes_to_checksum + sizeof (*h0)
273  - sizeof (cdp_hdr_t);
274 
275  /* And output the packet on the correct interface */
277  to_next = vlib_frame_vector_args (f);
278  to_next[0] = bi0;
279  f->n_vectors = 1;
280 
282  n->last_sent = vlib_time_now (vm);
283  }
284 }
285 
286 /*
287  * Decide which cdp packet template to use
288  */
289 static int
291 {
293 
294  return 0;
295 }
296 
297 /* Send a cdp neighbor announcement */
298 static void
300 {
301  if (n->packet_template_index == (u8) ~ 0)
302  {
303  /* If we don't know how to talk to this peer, don't try again */
304  if (pick_packet_template (cm, n))
305  {
306  n->last_sent = 1e70;
307  return;
308  }
309  }
310 
311  switch (n->packet_template_index)
312  {
314  send_ethernet_hello (cm, n, count);
315  break;
316 
318  send_hdlc_hello (cm, n, count);
319  break;
320 
322  send_srp_hello (cm, n, count);
323  break;
324 
325  default:
326  ASSERT (0);
327  }
328  n->last_sent = vlib_time_now (cm->vlib_main);
329 }
330 
331 static void
332 delete_neighbor (cdp_main_t * cm, cdp_neighbor_t * n, int want_broadcast)
333 {
335  vec_free (n->device_name);
336  vec_free (n->version);
337  vec_free (n->port_id);
338  vec_free (n->platform);
339  vec_free (n->last_rx_pkt);
340  pool_put (cm->neighbors, n);
341 }
342 
343 void
345 {
346  cdp_main_t *cm = &cdp_main;
347  cdp_neighbor_t *n;
348  f64 now = vlib_time_now (vm);
350  static u32 *delete_list = 0;
351  int i;
352  static cdp_neighbor_t **n_list = 0;
353 
354  /* *INDENT-OFF* */
355  pool_foreach (n, cm->neighbors,
356  ({
357  vec_add1 (n_list, n);
358  }));
359  /* *INDENT-ON* */
360 
361  /* Across all cdp neighbors known to the system */
362  for (i = 0; i < vec_len (n_list); i++)
363  {
364  n = n_list[i];
365 
366  /* "no cdp run" provisioned on the interface? */
367  if (n->disabled == 1)
368  continue;
369 
371 
372  /* Interface shutdown or rx timeout? */
374  || (now > (n->last_heard + (f64) n->ttl_in_seconds)))
375  /* add to list of neighbors to delete */
376  vec_add1 (delete_list, n - cm->neighbors);
377  else if (n->last_sent == 0.0)
378  /* First time, send 3 hellos */
379  send_hello (cm, n, 3 /* three to begin with */ );
380  else if (now > (n->last_sent + (((f64) n->ttl_in_seconds) / 6.0)))
381  /* Normal keepalive, send one */
382  send_hello (cm, n, 1 /* one as a keepalive */ );
383  }
384 
385  for (i = 0; i < vec_len (delete_list); i++)
386  {
387  n = vec_elt_at_index (cm->neighbors, delete_list[i]);
388  delete_neighbor (cm, n, 1);
389  }
390  if (delete_list)
391  _vec_len (delete_list) = 0;
392  if (n_list)
393  _vec_len (n_list) = 0;
394 }
395 
396 static clib_error_t *
398 {
399  cdp_main_t *cm = &cdp_main;
400 
401  /* Create the ethernet cdp hello packet template */
402  {
403  ethernet_llc_snap_and_cdp_header_t h;
404 
405  memset (&h, 0, sizeof (h));
406 
407  /* Send to 01:00:0c:cc:cc */
408  h.ethernet.dst_address[0] = 0x01;
409  /* h.ethernet.dst_address[1] = 0x00; (memset) */
410  h.ethernet.dst_address[2] = 0x0C;
411  h.ethernet.dst_address[3] = 0xCC;
412  h.ethernet.dst_address[4] = 0xCC;
413  h.ethernet.dst_address[5] = 0xCC;
414 
415  /* leave src address blank (fill in at send time) */
416 
417  /* leave length blank (fill in at send time) */
418 
419  /* LLC */
420  h.llc.dst_sap = h.llc.src_sap = 0xAA; /* SNAP */
421  h.llc.control = 0x03; /* UI (no extended control bytes) */
422 
423  /* SNAP */
424  /* h.snap.oui[0] = 0x00; (memset) */
425  /* h.snap.oui[1] = 0x00; (memset) */
426  h.snap.oui[2] = 0x0C; /* Cisco = 0x00000C */
427  h.snap.protocol = htons (0x2000); /* CDP = 0x2000 */
428 
429  /* CDP */
430  h.cdp.version = 2;
431  h.cdp.ttl = 180;
432 
435  /* data */ &h,
436  sizeof (h),
437  /* alloc chunk size */ 8,
438  "cdp-ethernet");
439  }
440 
441 #if 0 /* retain for reference */
442 
443  /* Create the hdlc cdp hello packet template */
444  {
445  hdlc_and_cdp_header_t h;
446 
447  memset (&h, 0, sizeof (h));
448 
449  h.hdlc.address = 0x0f;
450  /* h.hdlc.control = 0; (memset) */
451  h.hdlc.protocol = htons (0x2000); /* CDP = 0x2000 */
452 
453  /* CDP */
454  h.cdp.version = 2;
455  h.cdp.ttl = 180;
456 
459  /* data */ &h,
460  sizeof (h),
461  /* alloc chunk size */ 8,
462  "cdp-hdlc");
463  }
464 
465  /* Create the srp cdp hello packet template */
466  {
467  srp_and_cdp_header_t h;
468 
469  memset (&h, 0, sizeof (h));
470 
471  /* Send to 01:00:0c:cc:cc */
472  h.ethernet.dst_address[0] = 0x01;
473  /* h.ethernet.dst_address[1] = 0x00; (memset) */
474  h.ethernet.dst_address[2] = 0x0C;
475  h.ethernet.dst_address[3] = 0xCC;
476  h.ethernet.dst_address[4] = 0xCC;
477  h.ethernet.dst_address[5] = 0xCC;
478 
479  /* leave src address blank (fill in at send time) */
480 
481  /* The srp header is filled in at xmt */
482  h.srp.ttl = 1;
483  h.srp.priority = 7;
484  h.srp.mode = SRP_MODE_data;
485  srp_header_compute_parity (&h.srp);
486 
487  /* Inner ring and parity will be set at send time */
488 
489  h.ethernet.type = htons (0x2000); /* CDP = 0x2000 */
490 
491  /* CDP */
492  h.cdp.version = 2;
493  h.cdp.ttl = 180;
494 
497  /* data */ &h,
498  sizeof (h),
499  /* alloc chunk size */ 8,
500  "cdp-srp");
501  }
502 #endif
503 
504  return 0;
505 }
506 
508 
509 /*
510  * fd.io coding-style-patch-verification: ON
511  *
512  * Local Variables:
513  * eval: (c-set-style "gnu")
514  * End:
515  */
static void add_capability_tlv(vnet_hw_interface_t *hw, u8 **t0p)
Definition: cdp_periodic.c:77
void cdp_periodic(vlib_main_t *vm)
Definition: cdp_periodic.c:344
#define hash_unset(h, key)
Definition: hash.h:260
static vnet_hw_interface_t * vnet_get_sup_hw_interface(vnet_main_t *vnm, u32 sw_if_index)
static void delete_neighbor(cdp_main_t *cm, cdp_neighbor_t *n, int want_broadcast)
Definition: cdp_periodic.c:332
PCAP utility definitions.
static f64 vlib_time_now(vlib_main_t *vm)
Definition: main.h:227
cdp_main_t cdp_main
Definition: cdp_input.c:17
f64 last_sent
Definition: cdp.h:48
#define vec_add1(V, E)
Add 1 element to end of vector (unspecified alignment).
Definition: vec.h:520
static void add_tlvs(cdp_main_t *cm, vnet_hw_interface_t *hw, u8 **t0p)
Definition: cdp_periodic.c:91
u8 * version
Definition: cdp.h:65
int i
u16 cdp_checksum(void *p, int count)
Definition: cdp_input.c:26
static vnet_sw_interface_t * vnet_get_sw_interface(vnet_main_t *vnm, u32 sw_if_index)
static void srp_header_compute_parity(srp_header_t *h)
Definition: packet.h:89
u8 packet_template_index
Definition: cdp.h:57
static void send_ethernet_hello(cdp_main_t *cm, cdp_neighbor_t *n, int count)
Definition: cdp_periodic.c:104
uword * neighbor_by_sw_if_index
Definition: cdp.h:91
void * vlib_packet_template_get_packet(vlib_main_t *vm, vlib_packet_template_t *t, u32 *bi_result)
Definition: buffer.c:773
#define pool_foreach(VAR, POOL, BODY)
Iterate through pool.
Definition: pool.h:440
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:111
static void add_port_id_tlv(vnet_hw_interface_t *hw, u8 **t0p)
Definition: cdp_periodic.c:44
#define vec_elt_at_index(v, i)
Get vector value at index i checking that i is in bounds.
vlib_frame_t * vlib_get_frame_to_node(vlib_main_t *vm, u32 to_node_index)
Definition: main.c:182
static void add_version_tlv(vnet_hw_interface_t *hw, u8 **t0p)
Definition: cdp_periodic.c:55
vnet_main_t * vnet_main
Definition: cdp.h:104
void vlib_packet_template_init(vlib_main_t *vm, vlib_packet_template_t *t, void *packet_data, uword n_packet_data_bytes, uword min_n_buffers_each_alloc, char *fmt,...)
Definition: buffer.c:725
u16 current_length
Nbytes between current data and the end of this buffer.
Definition: buffer.h:108
void vlib_put_frame_to_node(vlib_main_t *vm, u32 to_node_index, vlib_frame_t *f)
Definition: main.c:191
#define pool_put(P, E)
Free an object E in pool P.
Definition: pool.h:273
f64 last_heard
Definition: cdp.h:47
cdp_neighbor_t * neighbors
Definition: cdp.h:82
static void send_hello(cdp_main_t *cm, cdp_neighbor_t *n, int count)
Definition: cdp_periodic.c:299
static void send_hdlc_hello(cdp_main_t *cm, cdp_neighbor_t *n, int count)
Definition: cdp_periodic.c:174
u16 n_vectors
Definition: node.h:344
vlib_main_t * vm
Definition: buffer.c:294
#define CDP_ROUTER_DEVICE
Definition: cdp_protocol.h:148
static void send_srp_hello(cdp_main_t *cm, cdp_neighbor_t *n, int count)
Definition: cdp_periodic.c:230
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:336
u8 * last_rx_pkt
Definition: cdp.h:70
u8 * device_name
Definition: cdp.h:64
#define clib_memcpy(a, b, c)
Definition: string.h:75
static clib_error_t * cdp_periodic_init(vlib_main_t *vm)
Definition: cdp_periodic.c:397
u8 ttl_in_seconds
Definition: cdp.h:51
#define VNET_SW_INTERFACE_FLAG_ADMIN_UP
Definition: interface.h:588
#define ASSERT(truth)
unsigned int u32
Definition: types.h:88
vlib_main_t * vlib_main
Definition: cdp.h:103
size_t count
Definition: vapi.c:42
vlib_packet_template_t packet_templates[CDP_N_PACKET_TEMPLATES]
Definition: cdp.h:100
u8 disabled
Definition: cdp.h:54
static void add_device_name_tlv(vnet_hw_interface_t *hw, u8 **t0p)
Definition: cdp_periodic.c:32
Definition: defs.h:47
unsigned short u16
Definition: types.h:57
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
double f64
Definition: types.h:142
unsigned char u8
Definition: types.h:56
u8 * port_id
Definition: cdp.h:66
static void * vlib_frame_vector_args(vlib_frame_t *f)
Get pointer to frame vector data.
Definition: node_funcs.h:267
Definition: cdp.h:79
static int pick_packet_template(cdp_main_t *cm, cdp_neighbor_t *n)
Definition: cdp_periodic.c:290
#define vnet_buffer(b)
Definition: buffer.h:372
u8 * platform
Definition: cdp.h:67
static void add_platform_tlv(vnet_hw_interface_t *hw, u8 **t0p)
Definition: cdp_periodic.c:66
u32 sw_if_index
Definition: cdp.h:44
static vlib_buffer_t * vlib_get_buffer(vlib_main_t *vm, u32 buffer_index)
Translate buffer index into buffer pointer.
Definition: buffer_funcs.h:57