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.h>
23 #include <vnet/lldp/lldp_node.h>
25 #ifndef ETHER_ADDR_LEN
26 #include <net/ethernet.h>
30 lldp_cfg_err_to_clib_err (lldp_cfg_err_t e)
37 case lldp_not_supported:
38 return clib_error_return (0, "not supported");
39 case lldp_invalid_arg:
40 return clib_error_return (0, "invalid argument");
46 lldp_cfg_intf_set (u32 hw_if_index, u8 ** port_desc, u8 ** mgmt_ip4,
47 u8 ** mgmt_ip6, u8 ** mgmt_oid, int enable)
49 lldp_main_t *lm = &lldp_main;
50 vnet_main_t *vnm = lm->vnet_main;
51 ethernet_main_t *em = ðernet_main;
52 const vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm, hw_if_index);
53 const ethernet_interface_t *eif = ethernet_get_interface (em, hw_if_index);
57 return lldp_not_supported;
62 lldp_intf_t *n = lldp_get_intf (lm, hw_if_index);
68 n = lldp_create_intf (lm, hw_if_index);
70 if (port_desc && *port_desc)
72 n->port_desc = *port_desc;
76 if (mgmt_ip4 && *mgmt_ip4)
78 n->mgmt_ip4 = *mgmt_ip4;
82 if (mgmt_ip6 && *mgmt_ip6)
84 n->mgmt_ip6 = *mgmt_ip6;
88 if (mgmt_oid && *mgmt_oid)
90 n->mgmt_oid = *mgmt_oid;
94 const vnet_sw_interface_t *sw =
95 vnet_get_sw_interface (lm->vnet_main, hi->sw_if_index);
96 if (sw->flags & (VNET_SW_INTERFACE_FLAG_ADMIN_UP |
97 VNET_SW_INTERFACE_FLAG_BOND_SLAVE))
99 lldp_schedule_intf (lm, n);
104 lldp_intf_t *n = lldp_get_intf (lm, hi->sw_if_index);
105 lldp_delete_intf (lm, n);
111 static clib_error_t *
112 lldp_intf_cmd (vlib_main_t * vm, unformat_input_t * input,
113 vlib_cli_command_t * cmd)
115 lldp_main_t *lm = &lldp_main;
116 vnet_main_t *vnm = lm->vnet_main;
117 u32 sw_if_index = (u32) ~ 0;
119 u8 *port_desc = NULL;
120 u8 *mgmt_ip4 = NULL, *mgmt_ip6 = NULL, *mgmt_oid = NULL;
121 ip4_address_t ip4_addr;
122 ip6_address_t ip6_addr;
124 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
126 if (unformat (input, "sw_if_index %d", &sw_if_index))
129 (input, "%U", unformat_vnet_sw_interface, vnm, &sw_if_index))
131 else if (unformat (input, "disable"))
133 else if (unformat (input, "port-desc %s", &port_desc))
136 if (unformat (input, "mgmt-ip4 %U", unformat_ip4_address, &ip4_addr))
138 vec_validate (mgmt_ip4, sizeof (ip4_address_t) - 1);
139 clib_memcpy (mgmt_ip4, &ip4_addr, vec_len (mgmt_ip4));
142 if (unformat (input, "mgmt-ip6 %U", unformat_ip6_address, &ip6_addr))
144 vec_validate (mgmt_ip6, sizeof (ip6_address_t) - 1);
145 clib_memcpy (mgmt_ip6, &ip6_addr, vec_len (mgmt_ip6));
147 else if (unformat (input, "mgmt-oid %s", &mgmt_oid))
153 if (sw_if_index == (u32) ~ 0)
154 return clib_error_return (0, "Interface name is invalid!");
156 return lldp_cfg_err_to_clib_err (lldp_cfg_intf_set (sw_if_index,
157 &port_desc, &mgmt_ip4,
158 &mgmt_ip6, &mgmt_oid,
163 lldp_cfg_set (u8 ** host, int hold_time, int tx_interval)
165 lldp_main_t *lm = &lldp_main;
170 vec_free (lm->sys_name);
171 lm->sys_name = *host;
177 if (hold_time < LLDP_MIN_TX_HOLD || hold_time > LLDP_MAX_TX_HOLD)
179 return lldp_invalid_arg;
181 if (lm->msg_tx_hold != hold_time)
183 lm->msg_tx_hold = hold_time;
190 if (tx_interval < LLDP_MIN_TX_INTERVAL ||
191 tx_interval > LLDP_MAX_TX_INTERVAL)
193 return lldp_invalid_arg;
195 if (lm->msg_tx_interval != tx_interval)
198 lm->msg_tx_interval = tx_interval;
204 vlib_process_signal_event (lm->vlib_main, lm->lldp_process_node_index,
205 LLDP_EVENT_RESCHEDULE, 0);
211 static clib_error_t *
212 lldp_cfg_cmd (vlib_main_t * vm, unformat_input_t * input,
213 vlib_cli_command_t * cmd)
218 clib_error_t *ret = NULL;
220 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
222 if (unformat (input, "system-name %s", &host))
225 else if (unformat (input, "tx-hold %d", &hold_time))
227 if (hold_time < LLDP_MIN_TX_HOLD || hold_time > LLDP_MAX_TX_HOLD)
230 clib_error_return (0,
231 "invalid tx-hold `%d' (out of range <%d,%d>)",
232 hold_time, LLDP_MIN_TX_HOLD,
237 else if (unformat (input, "tx-interval %d", &tx_interval))
239 if (tx_interval < LLDP_MIN_TX_INTERVAL ||
240 tx_interval > LLDP_MAX_TX_INTERVAL)
243 clib_error_return (0,
244 "invalid tx-interval `%d' (out of range <%d,%d>)",
245 tx_interval, LLDP_MIN_TX_INTERVAL,
246 LLDP_MAX_TX_INTERVAL);
256 lldp_cfg_err_to_clib_err (lldp_cfg_set (&host, hold_time, tx_interval));
263 VLIB_CLI_COMMAND(set_interface_lldp_cmd, static) = {
264 .path = "set interface lldp",
265 .short_help = "set interface lldp <interface> | sw_if_index <idx>"
266 " [port-desc <string>] [mgmt-ip4 <string>]"
267 " [mgmt-ip6 <string>] [mgmt-oid <string>] [disable]",
268 .function = lldp_intf_cmd,
271 VLIB_CLI_COMMAND(set_lldp_cmd, static) = {
273 .short_help = "set lldp [system-name <string>] [tx-hold <value>] "
274 "[tx-interval <value>]",
275 .function = lldp_cfg_cmd,
280 lldp_chassis_id_subtype_str (lldp_chassis_id_subtype_t t)
284 #define F(num, val, str) \
287 foreach_chassis_id_subtype (F)
290 return "unknown chassis subtype";
294 lldp_port_id_subtype_str (lldp_port_id_subtype_t t)
298 #define F(num, val, str) \
301 foreach_port_id_subtype (F)
304 return "unknown port subtype";
308 * format port id subtype&value
310 * @param va - 1st argument - unsigned - port id subtype
311 * @param va - 2nd argument - u8* - port id
312 * @param va - 3rd argument - unsigned - port id length
313 * @param va - 4th argument - int - 1 for detailed output, 0 for simple
316 format_lldp_port_id (u8 * s, va_list * va)
318 const lldp_port_id_subtype_t subtype = va_arg (*va, unsigned);
319 const u8 *id = va_arg (*va, u8 *);
320 const unsigned len = va_arg (*va, unsigned);
321 const int detail = va_arg (*va, int);
328 case LLDP_PORT_ID_SUBTYPE_NAME (intf_alias):
330 case LLDP_PORT_ID_SUBTYPE_NAME (port_comp):
332 case LLDP_PORT_ID_SUBTYPE_NAME (local):
334 case LLDP_PORT_ID_SUBTYPE_NAME (intf_name):
337 s = format (s, "%U(%s)", format_ascii_bytes, id, len,
338 lldp_port_id_subtype_str (subtype));
342 s = format (s, "%U", format_ascii_bytes, id, len);
345 case LLDP_PORT_ID_SUBTYPE_NAME (mac_addr):
346 if (ETHER_ADDR_LEN == len)
350 s = format (s, "%U(%s)", format_mac_address, id,
351 lldp_port_id_subtype_str (subtype));
355 s = format (s, "%U", format_mac_address, id);
360 case LLDP_PORT_ID_SUBTYPE_NAME (net_addr):
366 s = format (s, "%U(%s)", format_hex_bytes, id, len,
367 lldp_port_id_subtype_str (subtype));
371 s = format (s, "%U", format_hex_bytes, id, len);
379 * format chassis id subtype&value
381 * @param s format string
382 * @param va - 1st argument - unsigned - chassis id subtype
383 * @param va - 2nd argument - u8* - chassis id
384 * @param va - 3rd argument - unsigned - chassis id length
385 * @param va - 4th argument - int - 1 for detailed output, 0 for simple
388 format_lldp_chassis_id (u8 * s, va_list * va)
390 const lldp_chassis_id_subtype_t subtype =
391 va_arg (*va, lldp_chassis_id_subtype_t);
392 const u8 *id = va_arg (*va, u8 *);
393 const unsigned len = va_arg (*va, unsigned);
394 const int detail = va_arg (*va, int);
401 case LLDP_CHASS_ID_SUBTYPE_NAME (chassis_comp):
403 case LLDP_CHASS_ID_SUBTYPE_NAME (intf_alias):
405 case LLDP_CHASS_ID_SUBTYPE_NAME (port_comp):
407 case LLDP_PORT_ID_SUBTYPE_NAME (local):
409 case LLDP_CHASS_ID_SUBTYPE_NAME (intf_name):
412 s = format (s, "%U(%s)", format_ascii_bytes, id, len,
413 lldp_chassis_id_subtype_str (subtype));
417 s = format (s, "%U", format_ascii_bytes, id, len);
420 case LLDP_CHASS_ID_SUBTYPE_NAME (mac_addr):
421 if (ETHER_ADDR_LEN == len)
425 s = format (s, "%U(%s)", format_mac_address, id,
426 lldp_chassis_id_subtype_str (subtype));
430 s = format (s, "%U", format_mac_address, id);
435 case LLDP_CHASS_ID_SUBTYPE_NAME (net_addr):
440 s = format (s, "%U(%s)", format_hex_bytes, id, len,
441 lldp_chassis_id_subtype_str (subtype));
445 s = format (s, "%U", format_hex_bytes, id, len);
453 * convert a tlv code to human-readable string
456 lldp_tlv_code_str (lldp_tlv_code_t t)
463 foreach_lldp_tlv_type (F)
466 return "unknown lldp tlv";
470 * format a single LLDP TLV
472 * @param s format string
473 * @param va variable list - pointer to lldp_tlv_t is expected
476 format_lldp_tlv (u8 * s, va_list * va)
478 const lldp_tlv_t *tlv = va_arg (*va, lldp_tlv_t *);
483 u16 l = lldp_tlv_get_length (tlv);
484 switch (lldp_tlv_get_code (tlv))
486 case LLDP_TLV_NAME (chassis_id):
487 s = format (s, "%U", format_lldp_chassis_id,
488 ((lldp_chassis_id_tlv_t *) tlv)->subtype,
489 ((lldp_chassis_id_tlv_t *) tlv)->id,
490 l - STRUCT_SIZE_OF (lldp_chassis_id_tlv_t, subtype), 1);
492 case LLDP_TLV_NAME (port_id):
493 s = format (s, "%U", format_lldp_port_id,
494 ((lldp_port_id_tlv_t *) tlv)->subtype,
495 ((lldp_port_id_tlv_t *) tlv)->id,
496 l - STRUCT_SIZE_OF (lldp_port_id_tlv_t, subtype), 1);
498 case LLDP_TLV_NAME (ttl):
499 s = format (s, "%d", ntohs (((lldp_ttl_tlv_t *) tlv)->ttl));
501 case LLDP_TLV_NAME (sys_name):
503 case LLDP_TLV_NAME (sys_desc):
504 s = format (s, "%U", format_ascii_bytes, tlv->v, l);
507 s = format (s, "%U", format_hex_bytes, tlv->v, l);
514 format_time_ago (u8 * s, va_list * va)
516 f64 ago = va_arg (*va, double);
517 f64 now = va_arg (*va, double);
520 return format (s, "never");
522 return format (s, "%.1fs ago", now - ago);
526 format_lldp_intfs_detail (u8 * s, vlib_main_t * vm, const lldp_main_t * lm)
528 vnet_main_t *vnm = &vnet_main;
529 const lldp_intf_t *n;
530 const vnet_hw_interface_t *hw;
531 const vnet_sw_interface_t *sw;
532 s = format (s, "LLDP configuration:\n");
535 s = format (s, "Configured system name: %U\n", format_ascii_bytes,
536 lm->sys_name, vec_len (lm->sys_name));
539 s = format (s, "Configured tx-hold: %d\n", (int) lm->msg_tx_hold);
540 s = format (s, "Configured tx-interval: %d\n", (int) lm->msg_tx_interval);
541 s = format (s, "\nLLDP-enabled interface table:\n");
542 f64 now = vlib_time_now (vm);
547 hw = vnet_get_hw_interface(vnm, n->hw_if_index);
548 sw = vnet_get_sw_interface(lm->vnet_main, hw->sw_if_index);
550 s = format(s, "\nLocal Interface name: %s\n"
551 "Local Port Description: %s\n",
552 hw->name, n->port_desc);
555 s = format (s, "Local Management address: %U\n",
556 format_ip4_address, n->mgmt_ip4, vec_len (n->mgmt_ip4));
561 s = format (s, "Local Management address IPV6: %U\n",
562 format_ip6_address, n->mgmt_ip6, vec_len (n->mgmt_ip6));
567 s = format (s, "Local Management address OID: %U\n",
568 format_ascii_bytes, n->mgmt_oid, vec_len (n->mgmt_oid));
571 /* Interface shutdown */
572 if (!(sw->flags & (VNET_SW_INTERFACE_FLAG_ADMIN_UP |
573 VNET_SW_INTERFACE_FLAG_BOND_SLAVE)))
575 s = format(s, "Interface/peer state: interface down\n"
576 "Last packet sent: %U\n",
577 format_time_ago, n->last_sent, now);
579 else if (now < n->last_heard + n->ttl)
582 "Interface/peer state: active\n"
583 "Peer chassis ID: %U\nRemote port ID: %U\n"
584 "Last packet sent: %U\nLast packet received: %U\n",
585 format_lldp_chassis_id, n->chassis_id_subtype,
586 n->chassis_id, vec_len(n->chassis_id), 1,
587 format_lldp_port_id, n->port_id_subtype, n->port_id,
588 vec_len(n->port_id), 1, format_time_ago, n->last_sent,
589 now, format_time_ago, n->last_heard, now);
594 "Interface/peer state: inactive(timeout)\n"
595 "Last known peer chassis ID: %U\n"
596 "Last known peer port ID: %U\nLast packet sent: %U\n"
597 "Last packet received: %U\n",
598 format_lldp_chassis_id, n->chassis_id_subtype,
599 n->chassis_id, vec_len(n->chassis_id), 1,
600 format_lldp_port_id, n->port_id_subtype, n->port_id,
601 vec_len(n->port_id), 1, format_time_ago, n->last_sent,
602 now, format_time_ago, n->last_heard, now);
610 format_lldp_intfs (u8 * s, va_list * va)
612 vlib_main_t *vm = va_arg (*va, vlib_main_t *);
613 const lldp_main_t *lm = va_arg (*va, lldp_main_t *);
614 const int detail = va_arg (*va, int);
615 vnet_main_t *vnm = &vnet_main;
616 const lldp_intf_t *n;
620 return format_lldp_intfs_detail (s, vm, lm);
623 f64 now = vlib_time_now (vm);
624 s = format (s, "%-25s %-25s %-25s %=15s %=15s %=10s\n", "Local interface",
625 "Peer chassis ID", "Remote port ID", "Last heard", "Last sent",
631 const vnet_hw_interface_t *hw =
632 vnet_get_hw_interface(vnm, n->hw_if_index);
633 const vnet_sw_interface_t *sw =
634 vnet_get_sw_interface(lm->vnet_main, hw->sw_if_index);
635 /* Interface shutdown */
636 if (!(sw->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP))
638 if (now < n->last_heard + n->ttl)
640 s = format(s, "%-25s %-25U %-25U %=15U %=15U %=10s\n", hw->name,
641 format_lldp_chassis_id, n->chassis_id_subtype,
642 n->chassis_id, vec_len(n->chassis_id), 0,
643 format_lldp_port_id, n->port_id_subtype, n->port_id,
644 vec_len(n->port_id), 0, format_time_ago, n->last_heard,
645 now, format_time_ago, n->last_sent, now, "active");
649 s = format(s, "%-25s %-25s %-25s %=15U %=15U %=10s\n", hw->name,
650 "", "", format_time_ago, n->last_heard, now,
651 format_time_ago, n->last_sent, now, "inactive");
658 static clib_error_t *
659 show_lldp (vlib_main_t * vm, unformat_input_t * input,
660 CLIB_UNUSED (vlib_cli_command_t * lmd))
662 lldp_main_t *lm = &lldp_main;
664 if (unformat (input, "detail"))
666 vlib_cli_output (vm, "%U\n", format_lldp_intfs, vm, lm, 1);
670 vlib_cli_output (vm, "%U\n", format_lldp_intfs, vm, lm, 0);
676 VLIB_CLI_COMMAND(show_lldp_command, static) = {
678 .short_help = "show lldp [detail]",
679 .function = show_lldp,
684 * packet trace format function, very similar to
685 * lldp_packet_scan except that we call the per TLV format
686 * functions instead of the per TLV processing functions
689 lldp_input_format_trace (u8 * s, va_list * args)
691 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
692 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
693 const lldp_input_trace_t *t = va_arg (*args, lldp_input_trace_t *);
695 const lldp_tlv_t *tlv;
697 while (((cur + lldp_tlv_get_length ((lldp_tlv_t *) cur)) <
700 tlv = (lldp_tlv_t *) cur;
703 s = format (s, "TLV #%d(%s): %U\n", lldp_tlv_get_code (tlv),
704 lldp_tlv_code_str (lldp_tlv_get_code (tlv)),
705 format_lldp_tlv, tlv);
709 s = format (s, " TLV #%d(%s): %U\n", lldp_tlv_get_code (tlv),
710 lldp_tlv_code_str (lldp_tlv_get_code (tlv)),
711 format_lldp_tlv, tlv);
713 cur += STRUCT_SIZE_OF (lldp_tlv_t, head) + lldp_tlv_get_length (tlv);
720 * fd.io coding-style-patch-verification: ON
723 * eval: (c-set-style "gnu")