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/lisp-cp/lisp_types.h>
23 #include <vnet/lldp/lldp.h>
24 #include <vnet/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");
47 lldp_cfg_intf_set (u32 hw_if_index, u8 ** port_desc, u8 ** mgmt_ip4,
48 u8 ** mgmt_ip6, u8 ** mgmt_oid, int enable)
50 lldp_main_t *lm = &lldp_main;
51 vnet_main_t *vnm = lm->vnet_main;
52 ethernet_main_t *em = ðernet_main;
53 const vnet_hw_interface_t *hi;
54 const ethernet_interface_t *eif;
56 if (pool_is_free_index (vnm->interface_main.hw_interfaces, hw_if_index))
58 return lldp_invalid_arg;
61 hi = vnet_get_hw_interface (vnm, hw_if_index);
62 eif = ethernet_get_interface (em, hw_if_index);
65 return lldp_not_supported;
70 lldp_intf_t *n = lldp_get_intf (lm, hw_if_index);
76 n = lldp_create_intf (lm, hw_if_index);
78 if (port_desc && *port_desc)
80 n->port_desc = *port_desc;
84 if (mgmt_ip4 && *mgmt_ip4)
86 n->mgmt_ip4 = *mgmt_ip4;
90 if (mgmt_ip6 && *mgmt_ip6)
92 n->mgmt_ip6 = *mgmt_ip6;
96 if (mgmt_oid && *mgmt_oid)
98 n->mgmt_oid = *mgmt_oid;
102 const vnet_sw_interface_t *sw =
103 vnet_get_sw_interface (lm->vnet_main, hi->sw_if_index);
104 if (sw->flags & (VNET_SW_INTERFACE_FLAG_ADMIN_UP))
106 lldp_schedule_intf (lm, n);
111 lldp_intf_t *n = lldp_get_intf (lm, hi->sw_if_index);
112 lldp_delete_intf (lm, n);
118 static clib_error_t *
119 lldp_intf_cmd (vlib_main_t * vm, unformat_input_t * input,
120 vlib_cli_command_t * cmd)
122 lldp_main_t *lm = &lldp_main;
123 vnet_main_t *vnm = lm->vnet_main;
124 u32 sw_if_index = (u32) ~ 0;
126 u8 *port_desc = NULL;
127 u8 *mgmt_ip4 = NULL, *mgmt_ip6 = NULL, *mgmt_oid = NULL;
128 ip4_address_t ip4_addr;
129 ip6_address_t ip6_addr;
131 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
133 if (unformat (input, "sw_if_index %d", &sw_if_index))
136 (input, "%U", unformat_vnet_sw_interface, vnm, &sw_if_index))
138 else if (unformat (input, "disable"))
140 else if (unformat (input, "port-desc %s", &port_desc))
143 if (unformat (input, "mgmt-ip4 %U", unformat_ip4_address, &ip4_addr))
145 vec_validate (mgmt_ip4, sizeof (ip4_address_t) - 1);
146 clib_memcpy (mgmt_ip4, &ip4_addr, vec_len (mgmt_ip4));
149 if (unformat (input, "mgmt-ip6 %U", unformat_ip6_address, &ip6_addr))
151 vec_validate (mgmt_ip6, sizeof (ip6_address_t) - 1);
152 clib_memcpy (mgmt_ip6, &ip6_addr, vec_len (mgmt_ip6));
154 else if (unformat (input, "mgmt-oid %s", &mgmt_oid))
160 if (sw_if_index == (u32) ~ 0)
161 return clib_error_return (0, "Interface name is invalid!");
163 return lldp_cfg_err_to_clib_err (lldp_cfg_intf_set (sw_if_index,
164 &port_desc, &mgmt_ip4,
165 &mgmt_ip6, &mgmt_oid,
170 lldp_cfg_set (u8 ** host, int hold_time, int tx_interval)
172 lldp_main_t *lm = &lldp_main;
177 vec_free (lm->sys_name);
178 lm->sys_name = *host;
184 if (hold_time < LLDP_MIN_TX_HOLD || hold_time > LLDP_MAX_TX_HOLD)
186 return lldp_invalid_arg;
188 if (lm->msg_tx_hold != hold_time)
190 lm->msg_tx_hold = hold_time;
197 if (tx_interval < LLDP_MIN_TX_INTERVAL ||
198 tx_interval > LLDP_MAX_TX_INTERVAL)
200 return lldp_invalid_arg;
202 if (lm->msg_tx_interval != tx_interval)
205 lm->msg_tx_interval = tx_interval;
211 vlib_process_signal_event (lm->vlib_main, lm->lldp_process_node_index,
212 LLDP_EVENT_RESCHEDULE, 0);
218 static clib_error_t *
219 lldp_cfg_cmd (vlib_main_t * vm, unformat_input_t * input,
220 vlib_cli_command_t * cmd)
225 clib_error_t *ret = NULL;
227 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
229 if (unformat (input, "system-name %s", &host))
232 else if (unformat (input, "tx-hold %d", &hold_time))
234 if (hold_time < LLDP_MIN_TX_HOLD || hold_time > LLDP_MAX_TX_HOLD)
237 clib_error_return (0,
238 "invalid tx-hold `%d' (out of range <%d,%d>)",
239 hold_time, LLDP_MIN_TX_HOLD,
244 else if (unformat (input, "tx-interval %d", &tx_interval))
246 if (tx_interval < LLDP_MIN_TX_INTERVAL ||
247 tx_interval > LLDP_MAX_TX_INTERVAL)
250 clib_error_return (0,
251 "invalid tx-interval `%d' (out of range <%d,%d>)",
252 tx_interval, LLDP_MIN_TX_INTERVAL,
253 LLDP_MAX_TX_INTERVAL);
263 lldp_cfg_err_to_clib_err (lldp_cfg_set (&host, hold_time, tx_interval));
270 VLIB_CLI_COMMAND(set_interface_lldp_cmd, static) = {
271 .path = "set interface lldp",
272 .short_help = "set interface lldp <interface> | sw_if_index <idx>"
273 " [port-desc <string>] [mgmt-ip4 <string>]"
274 " [mgmt-ip6 <string>] [mgmt-oid <string>] [disable]",
275 .function = lldp_intf_cmd,
278 VLIB_CLI_COMMAND(set_lldp_cmd, static) = {
280 .short_help = "set lldp [system-name <string>] [tx-hold <value>] "
281 "[tx-interval <value>]",
282 .function = lldp_cfg_cmd,
287 lldp_chassis_id_subtype_str (lldp_chassis_id_subtype_t t)
291 #define F(num, val, str) \
294 foreach_chassis_id_subtype (F)
297 return "unknown chassis subtype";
301 lldp_port_id_subtype_str (lldp_port_id_subtype_t t)
305 #define F(num, val, str) \
308 foreach_port_id_subtype (F)
311 return "unknown port subtype";
315 * format port id subtype&value
317 * @param va - 1st argument - unsigned - port id subtype
318 * @param va - 2nd argument - u8* - port id
319 * @param va - 3rd argument - unsigned - port id length
320 * @param va - 4th argument - int - 1 for detailed output, 0 for simple
323 format_lldp_port_id (u8 * s, va_list * va)
325 const lldp_port_id_subtype_t subtype = va_arg (*va, unsigned);
326 const u8 *id = va_arg (*va, u8 *);
327 const unsigned len = va_arg (*va, unsigned);
328 const int detail = va_arg (*va, int);
335 case LLDP_PORT_ID_SUBTYPE_NAME (intf_alias):
337 case LLDP_PORT_ID_SUBTYPE_NAME (port_comp):
339 case LLDP_PORT_ID_SUBTYPE_NAME (local):
341 case LLDP_PORT_ID_SUBTYPE_NAME (intf_name):
344 s = format (s, "%U(%s)", format_ascii_bytes, id, len,
345 lldp_port_id_subtype_str (subtype));
349 s = format (s, "%U", format_ascii_bytes, id, len);
352 case LLDP_PORT_ID_SUBTYPE_NAME (mac_addr):
353 if (ETHER_ADDR_LEN == len)
357 s = format (s, "%U(%s)", format_mac_address, id,
358 lldp_port_id_subtype_str (subtype));
362 s = format (s, "%U", format_mac_address, id);
367 case LLDP_PORT_ID_SUBTYPE_NAME (net_addr):
373 s = format (s, "%U(%s)", format_hex_bytes, id, len,
374 lldp_port_id_subtype_str (subtype));
378 s = format (s, "%U", format_hex_bytes, id, len);
386 * format chassis id subtype&value
388 * @param s format string
389 * @param va - 1st argument - unsigned - chassis id subtype
390 * @param va - 2nd argument - u8* - chassis id
391 * @param va - 3rd argument - unsigned - chassis id length
392 * @param va - 4th argument - int - 1 for detailed output, 0 for simple
395 format_lldp_chassis_id (u8 * s, va_list * va)
397 const lldp_chassis_id_subtype_t subtype =
398 va_arg (*va, lldp_chassis_id_subtype_t);
399 const u8 *id = va_arg (*va, u8 *);
400 const unsigned len = va_arg (*va, unsigned);
401 const int detail = va_arg (*va, int);
408 case LLDP_CHASS_ID_SUBTYPE_NAME (chassis_comp):
410 case LLDP_CHASS_ID_SUBTYPE_NAME (intf_alias):
412 case LLDP_CHASS_ID_SUBTYPE_NAME (port_comp):
414 case LLDP_PORT_ID_SUBTYPE_NAME (local):
416 case LLDP_CHASS_ID_SUBTYPE_NAME (intf_name):
419 s = format (s, "%U(%s)", format_ascii_bytes, id, len,
420 lldp_chassis_id_subtype_str (subtype));
424 s = format (s, "%U", format_ascii_bytes, id, len);
427 case LLDP_CHASS_ID_SUBTYPE_NAME (mac_addr):
428 if (ETHER_ADDR_LEN == len)
432 s = format (s, "%U(%s)", format_mac_address, id,
433 lldp_chassis_id_subtype_str (subtype));
437 s = format (s, "%U", format_mac_address, id);
442 case LLDP_CHASS_ID_SUBTYPE_NAME (net_addr):
447 s = format (s, "%U(%s)", format_hex_bytes, id, len,
448 lldp_chassis_id_subtype_str (subtype));
452 s = format (s, "%U", format_hex_bytes, id, len);
460 * convert a tlv code to human-readable string
463 lldp_tlv_code_str (lldp_tlv_code_t t)
470 foreach_lldp_tlv_type (F)
473 return "unknown lldp tlv";
477 * format a single LLDP TLV
479 * @param s format string
480 * @param va variable list - pointer to lldp_tlv_t is expected
483 format_lldp_tlv (u8 * s, va_list * va)
485 const lldp_tlv_t *tlv = va_arg (*va, lldp_tlv_t *);
490 u16 l = lldp_tlv_get_length (tlv);
491 switch (lldp_tlv_get_code (tlv))
493 case LLDP_TLV_NAME (chassis_id):
494 s = format (s, "%U", format_lldp_chassis_id,
495 ((lldp_chassis_id_tlv_t *) tlv)->subtype,
496 ((lldp_chassis_id_tlv_t *) tlv)->id,
497 l - STRUCT_SIZE_OF (lldp_chassis_id_tlv_t, subtype), 1);
499 case LLDP_TLV_NAME (port_id):
500 s = format (s, "%U", format_lldp_port_id,
501 ((lldp_port_id_tlv_t *) tlv)->subtype,
502 ((lldp_port_id_tlv_t *) tlv)->id,
503 l - STRUCT_SIZE_OF (lldp_port_id_tlv_t, subtype), 1);
505 case LLDP_TLV_NAME (ttl):
506 s = format (s, "%d", ntohs (((lldp_ttl_tlv_t *) tlv)->ttl));
508 case LLDP_TLV_NAME (sys_name):
510 case LLDP_TLV_NAME (sys_desc):
511 s = format (s, "%U", format_ascii_bytes, tlv->v, l);
514 s = format (s, "%U", format_hex_bytes, tlv->v, l);
521 format_time_ago (u8 * s, va_list * va)
523 f64 ago = va_arg (*va, double);
524 f64 now = va_arg (*va, double);
527 return format (s, "never");
529 return format (s, "%.1fs ago", now - ago);
533 format_lldp_intfs_detail (u8 * s, vlib_main_t * vm, const lldp_main_t * lm)
535 vnet_main_t *vnm = &vnet_main;
536 const lldp_intf_t *n;
537 const vnet_hw_interface_t *hw;
538 const vnet_sw_interface_t *sw;
539 s = format (s, "LLDP configuration:\n");
542 s = format (s, "Configured system name: %U\n", format_ascii_bytes,
543 lm->sys_name, vec_len (lm->sys_name));
546 s = format (s, "Configured tx-hold: %d\n", (int) lm->msg_tx_hold);
547 s = format (s, "Configured tx-interval: %d\n", (int) lm->msg_tx_interval);
548 s = format (s, "\nLLDP-enabled interface table:\n");
549 f64 now = vlib_time_now (vm);
554 hw = vnet_get_hw_interface(vnm, n->hw_if_index);
555 sw = vnet_get_sw_interface(lm->vnet_main, hw->sw_if_index);
557 s = format(s, "\nLocal Interface name: %s\n"
558 "Local Port Description: %s\n",
559 hw->name, n->port_desc);
562 s = format (s, "Local Management address: %U\n",
563 format_ip4_address, n->mgmt_ip4, vec_len (n->mgmt_ip4));
568 s = format (s, "Local Management address IPV6: %U\n",
569 format_ip6_address, n->mgmt_ip6, vec_len (n->mgmt_ip6));
574 s = format (s, "Local Management address OID: %U\n",
575 format_ascii_bytes, n->mgmt_oid, vec_len (n->mgmt_oid));
578 /* Interface shutdown */
579 if (!(sw->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP))
581 s = format(s, "Interface/peer state: interface down\n"
582 "Last packet sent: %U\n",
583 format_time_ago, n->last_sent, now);
585 else if (now < n->last_heard + n->ttl)
588 "Interface/peer state: active\n"
589 "Peer chassis ID: %U\nRemote port ID: %U\n"
590 "Last packet sent: %U\nLast packet received: %U\n",
591 format_lldp_chassis_id, n->chassis_id_subtype,
592 n->chassis_id, vec_len(n->chassis_id), 1,
593 format_lldp_port_id, n->port_id_subtype, n->port_id,
594 vec_len(n->port_id), 1, format_time_ago, n->last_sent,
595 now, format_time_ago, n->last_heard, now);
600 "Interface/peer state: inactive(timeout)\n"
601 "Last known peer chassis ID: %U\n"
602 "Last known peer port ID: %U\nLast packet sent: %U\n"
603 "Last packet received: %U\n",
604 format_lldp_chassis_id, n->chassis_id_subtype,
605 n->chassis_id, vec_len(n->chassis_id), 1,
606 format_lldp_port_id, n->port_id_subtype, n->port_id,
607 vec_len(n->port_id), 1, format_time_ago, n->last_sent,
608 now, format_time_ago, n->last_heard, now);
616 format_lldp_intfs (u8 * s, va_list * va)
618 vlib_main_t *vm = va_arg (*va, vlib_main_t *);
619 const lldp_main_t *lm = va_arg (*va, lldp_main_t *);
620 const int detail = va_arg (*va, int);
621 vnet_main_t *vnm = &vnet_main;
622 const lldp_intf_t *n;
626 return format_lldp_intfs_detail (s, vm, lm);
629 f64 now = vlib_time_now (vm);
630 s = format (s, "%-25s %-25s %-25s %=15s %=15s %=10s\n", "Local interface",
631 "Peer chassis ID", "Remote port ID", "Last heard", "Last sent",
637 const vnet_hw_interface_t *hw =
638 vnet_get_hw_interface(vnm, n->hw_if_index);
639 const vnet_sw_interface_t *sw =
640 vnet_get_sw_interface(lm->vnet_main, hw->sw_if_index);
641 /* Interface shutdown */
642 if (!(sw->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP))
644 if (now < n->last_heard + n->ttl)
646 s = format(s, "%-25s %-25U %-25U %=15U %=15U %=10s\n", hw->name,
647 format_lldp_chassis_id, n->chassis_id_subtype,
648 n->chassis_id, vec_len(n->chassis_id), 0,
649 format_lldp_port_id, n->port_id_subtype, n->port_id,
650 vec_len(n->port_id), 0, format_time_ago, n->last_heard,
651 now, format_time_ago, n->last_sent, now, "active");
655 s = format(s, "%-25s %-25s %-25s %=15U %=15U %=10s\n", hw->name,
656 "", "", format_time_ago, n->last_heard, now,
657 format_time_ago, n->last_sent, now, "inactive");
664 static clib_error_t *
665 show_lldp (vlib_main_t * vm, unformat_input_t * input,
666 CLIB_UNUSED (vlib_cli_command_t * lmd))
668 lldp_main_t *lm = &lldp_main;
670 if (unformat (input, "detail"))
672 vlib_cli_output (vm, "%U\n", format_lldp_intfs, vm, lm, 1);
676 vlib_cli_output (vm, "%U\n", format_lldp_intfs, vm, lm, 0);
682 VLIB_CLI_COMMAND(show_lldp_command, static) = {
684 .short_help = "show lldp [detail]",
685 .function = show_lldp,
690 * packet trace format function, very similar to
691 * lldp_packet_scan except that we call the per TLV format
692 * functions instead of the per TLV processing functions
695 lldp_input_format_trace (u8 * s, va_list * args)
697 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
698 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
699 const lldp_input_trace_t *t = va_arg (*args, lldp_input_trace_t *);
701 const lldp_tlv_t *tlv;
703 while (((cur + lldp_tlv_get_length ((lldp_tlv_t *) cur)) <
706 tlv = (lldp_tlv_t *) cur;
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);
715 s = format (s, " TLV #%d(%s): %U\n", lldp_tlv_get_code (tlv),
716 lldp_tlv_code_str (lldp_tlv_get_code (tlv)),
717 format_lldp_tlv, tlv);
719 cur += STRUCT_SIZE_OF (lldp_tlv_t, head) + lldp_tlv_get_length (tlv);
726 * fd.io coding-style-patch-verification: ON
729 * eval: (c-set-style "gnu")