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/ethernet/ethernet.h>
22 #include <vnet/ip/ip.h>
23 #include <lldp/lldp.h>
24 #include <lldp/lldp_node.h>
26 #ifndef ETHER_ADDR_LEN
27 #include <net/ethernet.h>
31 lldp_cfg_err_to_clib_err (lldp_cfg_err_t e)
38 case lldp_not_supported:
39 return clib_error_return (0, "not supported");
40 case lldp_invalid_arg:
41 return clib_error_return (0, "invalid argument");
42 case lldp_internal_error:
43 return clib_error_return (0, "internal error");
49 lldp_cfg_intf_set (u32 hw_if_index, u8 ** port_desc, u8 ** mgmt_ip4,
50 u8 ** mgmt_ip6, u8 ** mgmt_oid, int enable)
52 clib_error_t *error = 0;
53 lldp_main_t *lm = &lldp_main;
54 vnet_main_t *vnm = lm->vnet_main;
55 ethernet_main_t *em = ðernet_main;
56 const vnet_hw_interface_t *hi;
57 const ethernet_interface_t *eif;
59 if (pool_is_free_index (vnm->interface_main.hw_interfaces, hw_if_index))
61 return lldp_invalid_arg;
64 hi = vnet_get_hw_interface (vnm, hw_if_index);
65 eif = ethernet_get_interface (em, hw_if_index);
68 return lldp_not_supported;
73 lldp_intf_t *n = lldp_get_intf (lm, hw_if_index);
79 n = lldp_create_intf (lm, hw_if_index);
81 if (port_desc && *port_desc)
83 n->port_desc = *port_desc;
87 if (mgmt_ip4 && *mgmt_ip4)
89 n->mgmt_ip4 = *mgmt_ip4;
93 if (mgmt_ip6 && *mgmt_ip6)
95 n->mgmt_ip6 = *mgmt_ip6;
99 if (mgmt_oid && *mgmt_oid)
101 n->mgmt_oid = *mgmt_oid;
106 vnet_hw_interface_add_del_mac_address (lm->vnet_main, hw_if_index,
111 clib_error_free (error);
112 lldp_delete_intf (lm, n);
113 return lldp_internal_error;
116 const vnet_sw_interface_t *sw =
117 vnet_get_sw_interface (lm->vnet_main, hi->sw_if_index);
118 if (sw->flags & (VNET_SW_INTERFACE_FLAG_ADMIN_UP))
120 lldp_schedule_intf (lm, n);
125 lldp_intf_t *n = lldp_get_intf (lm, hi->sw_if_index);
126 lldp_delete_intf (lm, n);
130 vnet_hw_interface_add_del_mac_address (lm->vnet_main, hw_if_index,
135 clib_error_free (error);
143 static clib_error_t *
144 lldp_intf_cmd (vlib_main_t * vm, unformat_input_t * input,
145 vlib_cli_command_t * cmd)
147 lldp_main_t *lm = &lldp_main;
148 vnet_main_t *vnm = lm->vnet_main;
149 u32 sw_if_index = (u32) ~ 0;
151 u8 *port_desc = NULL;
152 u8 *mgmt_ip4 = NULL, *mgmt_ip6 = NULL, *mgmt_oid = NULL;
153 ip4_address_t ip4_addr;
154 ip6_address_t ip6_addr;
156 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
158 if (unformat (input, "sw_if_index %d", &sw_if_index))
161 (input, "%U", unformat_vnet_sw_interface, vnm, &sw_if_index))
163 else if (unformat (input, "disable"))
165 else if (unformat (input, "port-desc %s", &port_desc))
168 if (unformat (input, "mgmt-ip4 %U", unformat_ip4_address, &ip4_addr))
170 vec_validate (mgmt_ip4, sizeof (ip4_address_t) - 1);
171 clib_memcpy (mgmt_ip4, &ip4_addr, vec_len (mgmt_ip4));
174 if (unformat (input, "mgmt-ip6 %U", unformat_ip6_address, &ip6_addr))
176 vec_validate (mgmt_ip6, sizeof (ip6_address_t) - 1);
177 clib_memcpy (mgmt_ip6, &ip6_addr, vec_len (mgmt_ip6));
179 else if (unformat (input, "mgmt-oid %s", &mgmt_oid))
185 if (sw_if_index == (u32) ~ 0)
186 return clib_error_return (0, "Interface name is invalid!");
188 return lldp_cfg_err_to_clib_err (lldp_cfg_intf_set (sw_if_index,
189 &port_desc, &mgmt_ip4,
190 &mgmt_ip6, &mgmt_oid,
195 lldp_cfg_set (u8 ** host, int hold_time, int tx_interval)
197 lldp_main_t *lm = &lldp_main;
202 vec_free (lm->sys_name);
203 lm->sys_name = *host;
209 if (hold_time < LLDP_MIN_TX_HOLD || hold_time > LLDP_MAX_TX_HOLD)
211 return lldp_invalid_arg;
213 if (lm->msg_tx_hold != hold_time)
215 lm->msg_tx_hold = hold_time;
222 if (tx_interval < LLDP_MIN_TX_INTERVAL ||
223 tx_interval > LLDP_MAX_TX_INTERVAL)
225 return lldp_invalid_arg;
227 if (lm->msg_tx_interval != tx_interval)
230 lm->msg_tx_interval = tx_interval;
236 vlib_process_signal_event (lm->vlib_main, lm->lldp_process_node_index,
237 LLDP_EVENT_RESCHEDULE, 0);
243 static clib_error_t *
244 lldp_cfg_cmd (vlib_main_t * vm, unformat_input_t * input,
245 vlib_cli_command_t * cmd)
250 clib_error_t *ret = NULL;
252 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
254 if (unformat (input, "system-name %s", &host))
257 else if (unformat (input, "tx-hold %d", &hold_time))
259 if (hold_time < LLDP_MIN_TX_HOLD || hold_time > LLDP_MAX_TX_HOLD)
262 clib_error_return (0,
263 "invalid tx-hold `%d' (out of range <%d,%d>)",
264 hold_time, LLDP_MIN_TX_HOLD,
269 else if (unformat (input, "tx-interval %d", &tx_interval))
271 if (tx_interval < LLDP_MIN_TX_INTERVAL ||
272 tx_interval > LLDP_MAX_TX_INTERVAL)
275 clib_error_return (0,
276 "invalid tx-interval `%d' (out of range <%d,%d>)",
277 tx_interval, LLDP_MIN_TX_INTERVAL,
278 LLDP_MAX_TX_INTERVAL);
288 lldp_cfg_err_to_clib_err (lldp_cfg_set (&host, hold_time, tx_interval));
295 VLIB_CLI_COMMAND(set_interface_lldp_cmd, static) = {
296 .path = "set interface lldp",
297 .short_help = "set interface lldp <interface> | sw_if_index <idx>"
298 " [port-desc <string>] [mgmt-ip4 <string>]"
299 " [mgmt-ip6 <string>] [mgmt-oid <string>] [disable]",
300 .function = lldp_intf_cmd,
303 VLIB_CLI_COMMAND(set_lldp_cmd, static) = {
305 .short_help = "set lldp [system-name <string>] [tx-hold <value>] "
306 "[tx-interval <value>]",
307 .function = lldp_cfg_cmd,
312 lldp_chassis_id_subtype_str (lldp_chassis_id_subtype_t t)
316 #define F(num, val, str) \
319 foreach_chassis_id_subtype (F)
322 return "unknown chassis subtype";
326 lldp_port_id_subtype_str (lldp_port_id_subtype_t t)
330 #define F(num, val, str) \
333 foreach_port_id_subtype (F)
336 return "unknown port subtype";
340 * format port id subtype&value
342 * @param va - 1st argument - unsigned - port id subtype
343 * @param va - 2nd argument - u8* - port id
344 * @param va - 3rd argument - unsigned - port id length
345 * @param va - 4th argument - int - 1 for detailed output, 0 for simple
348 format_lldp_port_id (u8 * s, va_list * va)
350 const lldp_port_id_subtype_t subtype = va_arg (*va, unsigned);
351 const u8 *id = va_arg (*va, u8 *);
352 const unsigned len = va_arg (*va, unsigned);
353 const int detail = va_arg (*va, int);
360 case LLDP_PORT_ID_SUBTYPE_NAME (intf_alias):
362 case LLDP_PORT_ID_SUBTYPE_NAME (port_comp):
364 case LLDP_PORT_ID_SUBTYPE_NAME (local):
366 case LLDP_PORT_ID_SUBTYPE_NAME (intf_name):
369 s = format (s, "%U(%s)", format_ascii_bytes, id, len,
370 lldp_port_id_subtype_str (subtype));
374 s = format (s, "%U", format_ascii_bytes, id, len);
377 case LLDP_PORT_ID_SUBTYPE_NAME (mac_addr):
378 if (ETHER_ADDR_LEN == len)
382 s = format (s, "%U(%s)", format_mac_address, id,
383 lldp_port_id_subtype_str (subtype));
387 s = format (s, "%U", format_mac_address, id);
392 case LLDP_PORT_ID_SUBTYPE_NAME (net_addr):
398 s = format (s, "%U(%s)", format_hex_bytes, id, len,
399 lldp_port_id_subtype_str (subtype));
403 s = format (s, "%U", format_hex_bytes, id, len);
411 * format chassis id subtype&value
413 * @param s format string
414 * @param va - 1st argument - unsigned - chassis id subtype
415 * @param va - 2nd argument - u8* - chassis id
416 * @param va - 3rd argument - unsigned - chassis id length
417 * @param va - 4th argument - int - 1 for detailed output, 0 for simple
420 format_lldp_chassis_id (u8 * s, va_list * va)
422 const lldp_chassis_id_subtype_t subtype =
423 va_arg (*va, lldp_chassis_id_subtype_t);
424 const u8 *id = va_arg (*va, u8 *);
425 const unsigned len = va_arg (*va, unsigned);
426 const int detail = va_arg (*va, int);
433 case LLDP_CHASS_ID_SUBTYPE_NAME (chassis_comp):
435 case LLDP_CHASS_ID_SUBTYPE_NAME (intf_alias):
437 case LLDP_CHASS_ID_SUBTYPE_NAME (port_comp):
439 case LLDP_PORT_ID_SUBTYPE_NAME (local):
441 case LLDP_CHASS_ID_SUBTYPE_NAME (intf_name):
444 s = format (s, "%U(%s)", format_ascii_bytes, id, len,
445 lldp_chassis_id_subtype_str (subtype));
449 s = format (s, "%U", format_ascii_bytes, id, len);
452 case LLDP_CHASS_ID_SUBTYPE_NAME (mac_addr):
453 if (ETHER_ADDR_LEN == len)
457 s = format (s, "%U(%s)", format_mac_address, id,
458 lldp_chassis_id_subtype_str (subtype));
462 s = format (s, "%U", format_mac_address, id);
467 case LLDP_CHASS_ID_SUBTYPE_NAME (net_addr):
472 s = format (s, "%U(%s)", format_hex_bytes, id, len,
473 lldp_chassis_id_subtype_str (subtype));
477 s = format (s, "%U", format_hex_bytes, id, len);
485 * convert a tlv code to human-readable string
488 lldp_tlv_code_str (lldp_tlv_code_t t)
495 foreach_lldp_tlv_type (F)
498 return "unknown lldp tlv";
502 * format a single LLDP TLV
504 * @param s format string
505 * @param va variable list - pointer to lldp_tlv_t is expected
508 format_lldp_tlv (u8 * s, va_list * va)
510 const lldp_tlv_t *tlv = va_arg (*va, lldp_tlv_t *);
515 u16 l = lldp_tlv_get_length (tlv);
516 switch (lldp_tlv_get_code (tlv))
518 case LLDP_TLV_NAME (chassis_id):
519 s = format (s, "%U", format_lldp_chassis_id,
520 ((lldp_chassis_id_tlv_t *) tlv)->subtype,
521 ((lldp_chassis_id_tlv_t *) tlv)->id,
522 l - STRUCT_SIZE_OF (lldp_chassis_id_tlv_t, subtype), 1);
524 case LLDP_TLV_NAME (port_id):
525 s = format (s, "%U", format_lldp_port_id,
526 ((lldp_port_id_tlv_t *) tlv)->subtype,
527 ((lldp_port_id_tlv_t *) tlv)->id,
528 l - STRUCT_SIZE_OF (lldp_port_id_tlv_t, subtype), 1);
530 case LLDP_TLV_NAME (ttl):
531 s = format (s, "%d", ntohs (((lldp_ttl_tlv_t *) tlv)->ttl));
533 case LLDP_TLV_NAME (sys_name):
535 case LLDP_TLV_NAME (sys_desc):
536 s = format (s, "%U", format_ascii_bytes, tlv->v, l);
539 s = format (s, "%U", format_hex_bytes, tlv->v, l);
546 format_time_ago (u8 * s, va_list * va)
548 f64 ago = va_arg (*va, double);
549 f64 now = va_arg (*va, double);
552 return format (s, "never");
554 return format (s, "%.1fs ago", now - ago);
558 format_lldp_intfs_detail (u8 * s, vlib_main_t * vm, const lldp_main_t * lm)
560 vnet_main_t *vnm = &vnet_main;
561 const lldp_intf_t *n;
562 const vnet_hw_interface_t *hw;
563 const vnet_sw_interface_t *sw;
564 s = format (s, "LLDP configuration:\n");
567 s = format (s, "Configured system name: %U\n", format_ascii_bytes,
568 lm->sys_name, vec_len (lm->sys_name));
571 s = format (s, "Configured tx-hold: %d\n", (int) lm->msg_tx_hold);
572 s = format (s, "Configured tx-interval: %d\n", (int) lm->msg_tx_interval);
573 s = format (s, "\nLLDP-enabled interface table:\n");
574 f64 now = vlib_time_now (vm);
579 hw = vnet_get_hw_interface(vnm, n->hw_if_index);
580 sw = vnet_get_sw_interface(lm->vnet_main, hw->sw_if_index);
582 s = format(s, "\nLocal Interface name: %v\n"
583 "Local Port Description: %s\n",
584 hw->name, n->port_desc);
587 s = format (s, "Local Management address: %U\n",
588 format_ip4_address, n->mgmt_ip4, vec_len (n->mgmt_ip4));
593 s = format (s, "Local Management address IPV6: %U\n",
594 format_ip6_address, n->mgmt_ip6, vec_len (n->mgmt_ip6));
599 s = format (s, "Local Management address OID: %U\n",
600 format_ascii_bytes, n->mgmt_oid, vec_len (n->mgmt_oid));
603 /* Interface shutdown */
604 if (!(sw->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP))
606 s = format(s, "Interface/peer state: interface down\n"
607 "Last packet sent: %U\n",
608 format_time_ago, n->last_sent, now);
610 else if (now < n->last_heard + n->ttl)
613 "Interface/peer state: active\n"
614 "Peer chassis ID: %U\nRemote port ID: %U\n"
615 "Last packet sent: %U\nLast packet received: %U\n",
616 format_lldp_chassis_id, n->chassis_id_subtype,
617 n->chassis_id, vec_len(n->chassis_id), 1,
618 format_lldp_port_id, n->port_id_subtype, n->port_id,
619 vec_len(n->port_id), 1, format_time_ago, n->last_sent,
620 now, format_time_ago, n->last_heard, now);
625 "Interface/peer state: inactive(timeout)\n"
626 "Last known peer chassis ID: %U\n"
627 "Last known peer port ID: %U\nLast packet sent: %U\n"
628 "Last packet received: %U\n",
629 format_lldp_chassis_id, n->chassis_id_subtype,
630 n->chassis_id, vec_len(n->chassis_id), 1,
631 format_lldp_port_id, n->port_id_subtype, n->port_id,
632 vec_len(n->port_id), 1, format_time_ago, n->last_sent,
633 now, format_time_ago, n->last_heard, now);
641 format_lldp_intfs (u8 * s, va_list * va)
643 vlib_main_t *vm = va_arg (*va, vlib_main_t *);
644 const lldp_main_t *lm = va_arg (*va, lldp_main_t *);
645 const int detail = va_arg (*va, int);
646 vnet_main_t *vnm = &vnet_main;
647 const lldp_intf_t *n;
651 return format_lldp_intfs_detail (s, vm, lm);
654 f64 now = vlib_time_now (vm);
655 s = format (s, "%-25s %-25s %-25s %=15s %=15s %=10s\n", "Local interface",
656 "Peer chassis ID", "Remote port ID", "Last heard", "Last sent",
662 const vnet_hw_interface_t *hw =
663 vnet_get_hw_interface(vnm, n->hw_if_index);
664 const vnet_sw_interface_t *sw =
665 vnet_get_sw_interface(lm->vnet_main, hw->sw_if_index);
666 /* Interface shutdown */
667 if (!(sw->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP))
669 if (now < n->last_heard + n->ttl)
671 s = format(s, "%-25v %-25U %-25U %=15U %=15U %=10s\n", hw->name,
672 format_lldp_chassis_id, n->chassis_id_subtype,
673 n->chassis_id, vec_len(n->chassis_id), 0,
674 format_lldp_port_id, n->port_id_subtype, n->port_id,
675 vec_len(n->port_id), 0, format_time_ago, n->last_heard,
676 now, format_time_ago, n->last_sent, now, "active");
680 s = format(s, "%-25v %-25s %-25s %=15U %=15U %=10s\n", hw->name,
681 "", "", format_time_ago, n->last_heard, now,
682 format_time_ago, n->last_sent, now, "inactive");
689 static clib_error_t *
690 show_lldp (vlib_main_t * vm, unformat_input_t * input,
691 CLIB_UNUSED (vlib_cli_command_t * lmd))
693 lldp_main_t *lm = &lldp_main;
695 if (unformat (input, "detail"))
697 vlib_cli_output (vm, "%U\n", format_lldp_intfs, vm, lm, 1);
701 vlib_cli_output (vm, "%U\n", format_lldp_intfs, vm, lm, 0);
707 VLIB_CLI_COMMAND(show_lldp_command, static) = {
709 .short_help = "show lldp [detail]",
710 .function = show_lldp,
715 * packet trace format function, very similar to
716 * lldp_packet_scan except that we call the per TLV format
717 * functions instead of the per TLV processing functions
720 lldp_input_format_trace (u8 * s, va_list * args)
722 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
723 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
724 const lldp_input_trace_t *t = va_arg (*args, lldp_input_trace_t *);
726 const lldp_tlv_t *tlv;
728 while (((cur + lldp_tlv_get_length ((lldp_tlv_t *) cur)) <
731 tlv = (lldp_tlv_t *) cur;
734 s = format (s, "TLV #%d(%s): %U\n", lldp_tlv_get_code (tlv),
735 lldp_tlv_code_str (lldp_tlv_get_code (tlv)),
736 format_lldp_tlv, tlv);
740 s = format (s, " TLV #%d(%s): %U\n", lldp_tlv_get_code (tlv),
741 lldp_tlv_code_str (lldp_tlv_get_code (tlv)),
742 format_lldp_tlv, tlv);
744 cur += STRUCT_SIZE_OF (lldp_tlv_t, head) + lldp_tlv_get_length (tlv);
751 * fd.io coding-style-patch-verification: ON
754 * eval: (c-set-style "gnu")