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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * @brief LLDP CLI handling
21 #include <vnet/lisp-cp/lisp_types.h>
22 #include <vnet/lldp/lldp_node.h>
24 #ifndef ETHER_ADDR_LEN
25 #include <net/ethernet.h>
28 typedef enum lldp_cfg_err
36 lldp_cfg_err_to_clib_err (lldp_cfg_err_t e)
43 case lldp_not_supported:
44 return clib_error_return (0, "not supported");
45 case lldp_invalid_arg:
46 return clib_error_return (0, "invalid argument");
52 lldp_cfg_intf_set (u32 hw_if_index, int enable)
54 lldp_main_t *lm = &lldp_main;
55 vnet_main_t *vnm = lm->vnet_main;
56 ethernet_main_t *em = ðernet_main;
57 const vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm, hw_if_index);
58 const ethernet_interface_t *eif = ethernet_get_interface (em, hw_if_index);
62 return lldp_not_supported;
67 lldp_intf_t *n = lldp_get_intf (lm, hw_if_index);
73 n = lldp_create_intf (lm, hw_if_index);
74 const vnet_sw_interface_t *sw =
75 vnet_get_sw_interface (lm->vnet_main, hi->sw_if_index);
76 if (sw->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP)
78 lldp_schedule_intf (lm, n);
83 lldp_intf_t *n = lldp_get_intf (lm, hi->sw_if_index);
84 lldp_delete_intf (lm, n);
91 lldp_intf_cmd (vlib_main_t * vm, unformat_input_t * input,
92 vlib_cli_command_t * cmd)
94 lldp_main_t *lm = &lldp_main;
95 vnet_main_t *vnm = lm->vnet_main;
99 if (unformat (input, "%U %U", unformat_vnet_hw_interface, vnm, &hw_if_index,
100 unformat_vlib_enable_disable, &enable))
103 lldp_cfg_err_to_clib_err (lldp_cfg_intf_set (hw_if_index, enable));
107 return clib_error_return (0, "unknown input `%U'",
108 format_unformat_error, input);
113 static lldp_cfg_err_t
114 lldp_cfg_set (u8 ** host, int hold_time, int tx_interval)
116 lldp_main_t *lm = &lldp_main;
120 vec_free (lm->sys_name);
121 lm->sys_name = *host;
126 if (hold_time < LLDP_MIN_TX_HOLD || hold_time > LLDP_MAX_TX_HOLD)
128 return lldp_invalid_arg;
130 if (lm->msg_tx_hold != hold_time)
132 lm->msg_tx_hold = hold_time;
138 if (tx_interval < LLDP_MIN_TX_INTERVAL ||
139 tx_interval > LLDP_MAX_TX_INTERVAL)
141 return lldp_invalid_arg;
143 if (lm->msg_tx_interval != tx_interval)
146 lm->msg_tx_interval = tx_interval;
151 vlib_process_signal_event (lm->vlib_main, lm->lldp_process_node_index,
152 LLDP_EVENT_RESCHEDULE, 0);
157 static clib_error_t *
158 lldp_cfg_cmd (vlib_main_t * vm, unformat_input_t * input,
159 vlib_cli_command_t * cmd)
164 clib_error_t *ret = NULL;
166 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
168 if (unformat (input, "system-name %s", &host))
171 else if (unformat (input, "tx-hold %d", &hold_time))
173 if (hold_time < LLDP_MIN_TX_HOLD || hold_time > LLDP_MAX_TX_HOLD)
176 clib_error_return (0,
177 "invalid tx-hold `%d' (out of range <%d,%d>)",
178 hold_time, LLDP_MIN_TX_HOLD,
183 else if (unformat (input, "tx-interval %d", &tx_interval))
185 if (tx_interval < LLDP_MIN_TX_INTERVAL ||
186 tx_interval > LLDP_MAX_TX_INTERVAL)
189 clib_error_return (0,
190 "invalid tx-interval `%d' (out of range <%d,%d>)",
191 tx_interval, LLDP_MIN_TX_INTERVAL,
192 LLDP_MAX_TX_INTERVAL);
202 lldp_cfg_err_to_clib_err (lldp_cfg_set (&host, hold_time, tx_interval));
209 VLIB_CLI_COMMAND(set_interface_lldp_cmd, static) = {
210 .path = "set interface lldp",
211 .short_help = "set interface lldp <interface> (enable | disable) ",
212 .function = lldp_intf_cmd,
215 VLIB_CLI_COMMAND(set_lldp_cmd, static) = {
217 .short_help = "set lldp [system-name <string>] [tx-hold <value>] "
218 "[tx-interval <value>]",
219 .function = lldp_cfg_cmd,
224 lldp_chassis_id_subtype_str (lldp_chassis_id_subtype_t t)
228 #define F(num, val, str) \
231 foreach_chassis_id_subtype (F)
234 return "unknown chassis subtype";
238 lldp_port_id_subtype_str (lldp_port_id_subtype_t t)
242 #define F(num, val, str) \
245 foreach_port_id_subtype (F)
248 return "unknown port subtype";
252 * format port id subtype&value
254 * @param va - 1st argument - unsigned - port id subtype
255 * @param va - 2nd argument - u8* - port id
256 * @param va - 3rd argument - unsigned - port id length
257 * @param va - 4th argument - int - 1 for detailed output, 0 for simple
260 format_lldp_port_id (u8 * s, va_list * va)
262 const lldp_port_id_subtype_t subtype = va_arg (*va, unsigned);
263 const u8 *id = va_arg (*va, u8 *);
264 const unsigned len = va_arg (*va, unsigned);
265 const int detail = va_arg (*va, int);
272 case LLDP_PORT_ID_SUBTYPE_NAME (intf_alias):
274 case LLDP_PORT_ID_SUBTYPE_NAME (port_comp):
276 case LLDP_PORT_ID_SUBTYPE_NAME (local):
278 case LLDP_PORT_ID_SUBTYPE_NAME (intf_name):
281 s = format (s, "%U(%s)", format_ascii_bytes, id, len,
282 lldp_port_id_subtype_str (subtype));
286 s = format (s, "%U", format_ascii_bytes, id, len);
289 case LLDP_PORT_ID_SUBTYPE_NAME (mac_addr):
290 if (ETHER_ADDR_LEN == len)
294 s = format (s, "%U(%s)", format_mac_address, id,
295 lldp_port_id_subtype_str (subtype));
299 s = format (s, "%U", format_mac_address, id);
304 case LLDP_PORT_ID_SUBTYPE_NAME (net_addr):
310 s = format (s, "%U(%s)", format_hex_bytes, id, len,
311 lldp_port_id_subtype_str (subtype));
315 s = format (s, "%U", format_hex_bytes, id, len);
323 * format chassis id subtype&value
325 * @param s format string
326 * @param va - 1st argument - unsigned - chassis id subtype
327 * @param va - 2nd argument - u8* - chassis id
328 * @param va - 3rd argument - unsigned - chassis id length
329 * @param va - 4th argument - int - 1 for detailed output, 0 for simple
332 format_lldp_chassis_id (u8 * s, va_list * va)
334 const lldp_chassis_id_subtype_t subtype =
335 va_arg (*va, lldp_chassis_id_subtype_t);
336 const u8 *id = va_arg (*va, u8 *);
337 const unsigned len = va_arg (*va, unsigned);
338 const int detail = va_arg (*va, int);
345 case LLDP_CHASS_ID_SUBTYPE_NAME (chassis_comp):
347 case LLDP_CHASS_ID_SUBTYPE_NAME (intf_alias):
349 case LLDP_CHASS_ID_SUBTYPE_NAME (port_comp):
351 case LLDP_PORT_ID_SUBTYPE_NAME (local):
353 case LLDP_CHASS_ID_SUBTYPE_NAME (intf_name):
356 s = format (s, "%U(%s)", format_ascii_bytes, id, len,
357 lldp_chassis_id_subtype_str (subtype));
361 s = format (s, "%U", format_ascii_bytes, id, len);
364 case LLDP_CHASS_ID_SUBTYPE_NAME (mac_addr):
365 if (ETHER_ADDR_LEN == len)
369 s = format (s, "%U(%s)", format_mac_address, id,
370 lldp_chassis_id_subtype_str (subtype));
374 s = format (s, "%U", format_mac_address, id);
379 case LLDP_CHASS_ID_SUBTYPE_NAME (net_addr):
384 s = format (s, "%U(%s)", format_hex_bytes, id, len,
385 lldp_chassis_id_subtype_str (subtype));
389 s = format (s, "%U", format_hex_bytes, id, len);
397 * convert a tlv code to human-readable string
400 lldp_tlv_code_str (lldp_tlv_code_t t)
407 foreach_lldp_tlv_type (F)
410 return "unknown lldp tlv";
414 * format a single LLDP TLV
416 * @param s format string
417 * @param va variable list - pointer to lldp_tlv_t is expected
420 format_lldp_tlv (u8 * s, va_list * va)
422 const lldp_tlv_t *tlv = va_arg (*va, lldp_tlv_t *);
427 u16 l = lldp_tlv_get_length (tlv);
428 switch (lldp_tlv_get_code (tlv))
430 case LLDP_TLV_NAME (chassis_id):
431 s = format (s, "%U", format_lldp_chassis_id,
432 ((lldp_chassis_id_tlv_t *) tlv)->subtype,
433 ((lldp_chassis_id_tlv_t *) tlv)->id,
434 l - STRUCT_SIZE_OF (lldp_chassis_id_tlv_t, subtype), 1);
436 case LLDP_TLV_NAME (port_id):
437 s = format (s, "%U", format_lldp_port_id,
438 ((lldp_port_id_tlv_t *) tlv)->subtype,
439 ((lldp_port_id_tlv_t *) tlv)->id,
440 l - STRUCT_SIZE_OF (lldp_port_id_tlv_t, subtype), 1);
442 case LLDP_TLV_NAME (ttl):
443 s = format (s, "%d", ntohs (((lldp_ttl_tlv_t *) tlv)->ttl));
445 case LLDP_TLV_NAME (sys_name):
447 case LLDP_TLV_NAME (sys_desc):
448 s = format (s, "%U", format_ascii_bytes, tlv->v, l);
451 s = format (s, "%U", format_hex_bytes, tlv->v, l);
458 format_time_ago (u8 * s, va_list * va)
460 f64 ago = va_arg (*va, double);
461 f64 now = va_arg (*va, double);
464 return format (s, "never");
466 return format (s, "%.1fs ago", now - ago);
470 format_lldp_intfs_detail (u8 * s, vlib_main_t * vm, const lldp_main_t * lm)
472 vnet_main_t *vnm = &vnet_main;
473 const lldp_intf_t *n;
474 const vnet_hw_interface_t *hw;
475 const vnet_sw_interface_t *sw;
476 s = format (s, "LLDP configuration:\n");
479 s = format (s, "Configured system name: %U\n", format_ascii_bytes,
480 lm->sys_name, vec_len (lm->sys_name));
482 s = format (s, "Configured tx-hold: %d\n", (int) lm->msg_tx_hold);
483 s = format (s, "Configured tx-interval: %d\n", (int) lm->msg_tx_interval);
484 s = format (s, "\nLLDP-enabled interface table:\n");
485 f64 now = vlib_time_now (vm);
490 hw = vnet_get_hw_interface(vnm, n->hw_if_index);
491 sw = vnet_get_sw_interface(lm->vnet_main, hw->sw_if_index);
492 /* Interface shutdown */
493 if (!(sw->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP))
495 s = format(s, "\nInterface name: %s\nInterface/peer state: "
496 "interface down\nLast packet sent: %U\n",
497 hw->name, format_time_ago, n->last_sent, now);
499 else if (now < n->last_heard + n->ttl)
502 "\nInterface name: %s\nInterface/peer state: "
503 "active\nPeer chassis ID: %U\nRemote port ID: %U\nLast "
504 "packet sent: %U\nLast packet received: %U\n",
505 hw->name, format_lldp_chassis_id, n->chassis_id_subtype,
506 n->chassis_id, vec_len(n->chassis_id), 1,
507 format_lldp_port_id, n->port_id_subtype, n->port_id,
508 vec_len(n->port_id), 1, format_time_ago, n->last_sent,
509 now, format_time_ago, n->last_heard, now);
513 s = format(s, "\nInterface name: %s\nInterface/peer state: "
514 "inactive(timeout)\nLast known peer chassis ID: "
515 "%U\nLast known peer port ID: %U\nLast packet sent: "
516 "%U\nLast packet received: %U\n",
517 hw->name, format_lldp_chassis_id, n->chassis_id_subtype,
518 n->chassis_id, vec_len(n->chassis_id), 1,
519 format_lldp_port_id, n->port_id_subtype, n->port_id,
520 vec_len(n->port_id), 1, format_time_ago, n->last_sent,
521 now, format_time_ago, n->last_heard, now);
529 format_lldp_intfs (u8 * s, va_list * va)
531 vlib_main_t *vm = va_arg (*va, vlib_main_t *);
532 const lldp_main_t *lm = va_arg (*va, lldp_main_t *);
533 const int detail = va_arg (*va, int);
534 vnet_main_t *vnm = &vnet_main;
535 const lldp_intf_t *n;
539 return format_lldp_intfs_detail (s, vm, lm);
542 f64 now = vlib_time_now (vm);
543 s = format (s, "%-25s %-25s %-25s %=15s %=15s %=10s\n", "Local interface",
544 "Peer chassis ID", "Remote port ID", "Last heard", "Last sent",
550 const vnet_hw_interface_t *hw =
551 vnet_get_hw_interface(vnm, n->hw_if_index);
552 const vnet_sw_interface_t *sw =
553 vnet_get_sw_interface(lm->vnet_main, hw->sw_if_index);
554 /* Interface shutdown */
555 if (!(sw->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP))
557 if (now < n->last_heard + n->ttl)
559 s = format(s, "%-25s %-25U %-25U %=15U %=15U %=10s\n", hw->name,
560 format_lldp_chassis_id, n->chassis_id_subtype,
561 n->chassis_id, vec_len(n->chassis_id), 0,
562 format_lldp_port_id, n->port_id_subtype, n->port_id,
563 vec_len(n->port_id), 0, format_time_ago, n->last_heard,
564 now, format_time_ago, n->last_sent, now, "active");
568 s = format(s, "%-25s %-25s %-25s %=15U %=15U %=10s\n", hw->name,
569 "", "", format_time_ago, n->last_heard, now,
570 format_time_ago, n->last_sent, now, "inactive");
577 static clib_error_t *
578 show_lldp (vlib_main_t * vm, unformat_input_t * input,
579 CLIB_UNUSED (vlib_cli_command_t * lmd))
581 lldp_main_t *lm = &lldp_main;
583 if (unformat (input, "detail"))
585 vlib_cli_output (vm, "%U\n", format_lldp_intfs, vm, lm, 1);
589 vlib_cli_output (vm, "%U\n", format_lldp_intfs, vm, lm, 0);
595 VLIB_CLI_COMMAND(show_lldp_command, static) = {
597 .short_help = "show lldp [detail]",
598 .function = show_lldp,
603 * packet trace format function, very similar to
604 * lldp_packet_scan except that we call the per TLV format
605 * functions instead of the per TLV processing functions
608 lldp_input_format_trace (u8 * s, va_list * args)
610 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
611 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
612 const lldp_input_trace_t *t = va_arg (*args, lldp_input_trace_t *);
614 const lldp_tlv_t *tlv;
616 while (((cur + lldp_tlv_get_length ((lldp_tlv_t *) cur)) <
619 tlv = (lldp_tlv_t *) cur;
622 s = format (s, "TLV #%d(%s): %U\n", lldp_tlv_get_code (tlv),
623 lldp_tlv_code_str (lldp_tlv_get_code (tlv)),
624 format_lldp_tlv, tlv);
628 s = format (s, " TLV #%d(%s): %U\n", lldp_tlv_get_code (tlv),
629 lldp_tlv_code_str (lldp_tlv_get_code (tlv)),
630 format_lldp_tlv, tlv);
632 cur += STRUCT_SIZE_OF (lldp_tlv_t, head) + lldp_tlv_get_length (tlv);
639 * fd.io coding-style-patch-verification: ON
642 * eval: (c-set-style "gnu")