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;
53 const ethernet_interface_t *eif;
55 if (pool_is_free_index (vnm->interface_main.hw_interfaces, hw_if_index))
57 return lldp_invalid_arg;
60 hi = vnet_get_hw_interface (vnm, hw_if_index);
61 eif = ethernet_get_interface (em, hw_if_index);
64 return lldp_not_supported;
69 lldp_intf_t *n = lldp_get_intf (lm, hw_if_index);
75 n = lldp_create_intf (lm, hw_if_index);
77 if (port_desc && *port_desc)
79 n->port_desc = *port_desc;
83 if (mgmt_ip4 && *mgmt_ip4)
85 n->mgmt_ip4 = *mgmt_ip4;
89 if (mgmt_ip6 && *mgmt_ip6)
91 n->mgmt_ip6 = *mgmt_ip6;
95 if (mgmt_oid && *mgmt_oid)
97 n->mgmt_oid = *mgmt_oid;
101 const vnet_sw_interface_t *sw =
102 vnet_get_sw_interface (lm->vnet_main, hi->sw_if_index);
103 if (sw->flags & (VNET_SW_INTERFACE_FLAG_ADMIN_UP))
105 lldp_schedule_intf (lm, n);
110 lldp_intf_t *n = lldp_get_intf (lm, hi->sw_if_index);
111 lldp_delete_intf (lm, n);
117 static clib_error_t *
118 lldp_intf_cmd (vlib_main_t * vm, unformat_input_t * input,
119 vlib_cli_command_t * cmd)
121 lldp_main_t *lm = &lldp_main;
122 vnet_main_t *vnm = lm->vnet_main;
123 u32 sw_if_index = (u32) ~ 0;
125 u8 *port_desc = NULL;
126 u8 *mgmt_ip4 = NULL, *mgmt_ip6 = NULL, *mgmt_oid = NULL;
127 ip4_address_t ip4_addr;
128 ip6_address_t ip6_addr;
130 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
132 if (unformat (input, "sw_if_index %d", &sw_if_index))
135 (input, "%U", unformat_vnet_sw_interface, vnm, &sw_if_index))
137 else if (unformat (input, "disable"))
139 else if (unformat (input, "port-desc %s", &port_desc))
142 if (unformat (input, "mgmt-ip4 %U", unformat_ip4_address, &ip4_addr))
144 vec_validate (mgmt_ip4, sizeof (ip4_address_t) - 1);
145 clib_memcpy (mgmt_ip4, &ip4_addr, vec_len (mgmt_ip4));
148 if (unformat (input, "mgmt-ip6 %U", unformat_ip6_address, &ip6_addr))
150 vec_validate (mgmt_ip6, sizeof (ip6_address_t) - 1);
151 clib_memcpy (mgmt_ip6, &ip6_addr, vec_len (mgmt_ip6));
153 else if (unformat (input, "mgmt-oid %s", &mgmt_oid))
159 if (sw_if_index == (u32) ~ 0)
160 return clib_error_return (0, "Interface name is invalid!");
162 return lldp_cfg_err_to_clib_err (lldp_cfg_intf_set (sw_if_index,
163 &port_desc, &mgmt_ip4,
164 &mgmt_ip6, &mgmt_oid,
169 lldp_cfg_set (u8 ** host, int hold_time, int tx_interval)
171 lldp_main_t *lm = &lldp_main;
176 vec_free (lm->sys_name);
177 lm->sys_name = *host;
183 if (hold_time < LLDP_MIN_TX_HOLD || hold_time > LLDP_MAX_TX_HOLD)
185 return lldp_invalid_arg;
187 if (lm->msg_tx_hold != hold_time)
189 lm->msg_tx_hold = hold_time;
196 if (tx_interval < LLDP_MIN_TX_INTERVAL ||
197 tx_interval > LLDP_MAX_TX_INTERVAL)
199 return lldp_invalid_arg;
201 if (lm->msg_tx_interval != tx_interval)
204 lm->msg_tx_interval = tx_interval;
210 vlib_process_signal_event (lm->vlib_main, lm->lldp_process_node_index,
211 LLDP_EVENT_RESCHEDULE, 0);
217 static clib_error_t *
218 lldp_cfg_cmd (vlib_main_t * vm, unformat_input_t * input,
219 vlib_cli_command_t * cmd)
224 clib_error_t *ret = NULL;
226 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
228 if (unformat (input, "system-name %s", &host))
231 else if (unformat (input, "tx-hold %d", &hold_time))
233 if (hold_time < LLDP_MIN_TX_HOLD || hold_time > LLDP_MAX_TX_HOLD)
236 clib_error_return (0,
237 "invalid tx-hold `%d' (out of range <%d,%d>)",
238 hold_time, LLDP_MIN_TX_HOLD,
243 else if (unformat (input, "tx-interval %d", &tx_interval))
245 if (tx_interval < LLDP_MIN_TX_INTERVAL ||
246 tx_interval > LLDP_MAX_TX_INTERVAL)
249 clib_error_return (0,
250 "invalid tx-interval `%d' (out of range <%d,%d>)",
251 tx_interval, LLDP_MIN_TX_INTERVAL,
252 LLDP_MAX_TX_INTERVAL);
262 lldp_cfg_err_to_clib_err (lldp_cfg_set (&host, hold_time, tx_interval));
269 VLIB_CLI_COMMAND(set_interface_lldp_cmd, static) = {
270 .path = "set interface lldp",
271 .short_help = "set interface lldp <interface> | sw_if_index <idx>"
272 " [port-desc <string>] [mgmt-ip4 <string>]"
273 " [mgmt-ip6 <string>] [mgmt-oid <string>] [disable]",
274 .function = lldp_intf_cmd,
277 VLIB_CLI_COMMAND(set_lldp_cmd, static) = {
279 .short_help = "set lldp [system-name <string>] [tx-hold <value>] "
280 "[tx-interval <value>]",
281 .function = lldp_cfg_cmd,
286 lldp_chassis_id_subtype_str (lldp_chassis_id_subtype_t t)
290 #define F(num, val, str) \
293 foreach_chassis_id_subtype (F)
296 return "unknown chassis subtype";
300 lldp_port_id_subtype_str (lldp_port_id_subtype_t t)
304 #define F(num, val, str) \
307 foreach_port_id_subtype (F)
310 return "unknown port subtype";
314 * format port id subtype&value
316 * @param va - 1st argument - unsigned - port id subtype
317 * @param va - 2nd argument - u8* - port id
318 * @param va - 3rd argument - unsigned - port id length
319 * @param va - 4th argument - int - 1 for detailed output, 0 for simple
322 format_lldp_port_id (u8 * s, va_list * va)
324 const lldp_port_id_subtype_t subtype = va_arg (*va, unsigned);
325 const u8 *id = va_arg (*va, u8 *);
326 const unsigned len = va_arg (*va, unsigned);
327 const int detail = va_arg (*va, int);
334 case LLDP_PORT_ID_SUBTYPE_NAME (intf_alias):
336 case LLDP_PORT_ID_SUBTYPE_NAME (port_comp):
338 case LLDP_PORT_ID_SUBTYPE_NAME (local):
340 case LLDP_PORT_ID_SUBTYPE_NAME (intf_name):
343 s = format (s, "%U(%s)", format_ascii_bytes, id, len,
344 lldp_port_id_subtype_str (subtype));
348 s = format (s, "%U", format_ascii_bytes, id, len);
351 case LLDP_PORT_ID_SUBTYPE_NAME (mac_addr):
352 if (ETHER_ADDR_LEN == len)
356 s = format (s, "%U(%s)", format_mac_address, id,
357 lldp_port_id_subtype_str (subtype));
361 s = format (s, "%U", format_mac_address, id);
366 case LLDP_PORT_ID_SUBTYPE_NAME (net_addr):
372 s = format (s, "%U(%s)", format_hex_bytes, id, len,
373 lldp_port_id_subtype_str (subtype));
377 s = format (s, "%U", format_hex_bytes, id, len);
385 * format chassis id subtype&value
387 * @param s format string
388 * @param va - 1st argument - unsigned - chassis id subtype
389 * @param va - 2nd argument - u8* - chassis id
390 * @param va - 3rd argument - unsigned - chassis id length
391 * @param va - 4th argument - int - 1 for detailed output, 0 for simple
394 format_lldp_chassis_id (u8 * s, va_list * va)
396 const lldp_chassis_id_subtype_t subtype =
397 va_arg (*va, lldp_chassis_id_subtype_t);
398 const u8 *id = va_arg (*va, u8 *);
399 const unsigned len = va_arg (*va, unsigned);
400 const int detail = va_arg (*va, int);
407 case LLDP_CHASS_ID_SUBTYPE_NAME (chassis_comp):
409 case LLDP_CHASS_ID_SUBTYPE_NAME (intf_alias):
411 case LLDP_CHASS_ID_SUBTYPE_NAME (port_comp):
413 case LLDP_PORT_ID_SUBTYPE_NAME (local):
415 case LLDP_CHASS_ID_SUBTYPE_NAME (intf_name):
418 s = format (s, "%U(%s)", format_ascii_bytes, id, len,
419 lldp_chassis_id_subtype_str (subtype));
423 s = format (s, "%U", format_ascii_bytes, id, len);
426 case LLDP_CHASS_ID_SUBTYPE_NAME (mac_addr):
427 if (ETHER_ADDR_LEN == len)
431 s = format (s, "%U(%s)", format_mac_address, id,
432 lldp_chassis_id_subtype_str (subtype));
436 s = format (s, "%U", format_mac_address, id);
441 case LLDP_CHASS_ID_SUBTYPE_NAME (net_addr):
446 s = format (s, "%U(%s)", format_hex_bytes, id, len,
447 lldp_chassis_id_subtype_str (subtype));
451 s = format (s, "%U", format_hex_bytes, id, len);
459 * convert a tlv code to human-readable string
462 lldp_tlv_code_str (lldp_tlv_code_t t)
469 foreach_lldp_tlv_type (F)
472 return "unknown lldp tlv";
476 * format a single LLDP TLV
478 * @param s format string
479 * @param va variable list - pointer to lldp_tlv_t is expected
482 format_lldp_tlv (u8 * s, va_list * va)
484 const lldp_tlv_t *tlv = va_arg (*va, lldp_tlv_t *);
489 u16 l = lldp_tlv_get_length (tlv);
490 switch (lldp_tlv_get_code (tlv))
492 case LLDP_TLV_NAME (chassis_id):
493 s = format (s, "%U", format_lldp_chassis_id,
494 ((lldp_chassis_id_tlv_t *) tlv)->subtype,
495 ((lldp_chassis_id_tlv_t *) tlv)->id,
496 l - STRUCT_SIZE_OF (lldp_chassis_id_tlv_t, subtype), 1);
498 case LLDP_TLV_NAME (port_id):
499 s = format (s, "%U", format_lldp_port_id,
500 ((lldp_port_id_tlv_t *) tlv)->subtype,
501 ((lldp_port_id_tlv_t *) tlv)->id,
502 l - STRUCT_SIZE_OF (lldp_port_id_tlv_t, subtype), 1);
504 case LLDP_TLV_NAME (ttl):
505 s = format (s, "%d", ntohs (((lldp_ttl_tlv_t *) tlv)->ttl));
507 case LLDP_TLV_NAME (sys_name):
509 case LLDP_TLV_NAME (sys_desc):
510 s = format (s, "%U", format_ascii_bytes, tlv->v, l);
513 s = format (s, "%U", format_hex_bytes, tlv->v, l);
520 format_time_ago (u8 * s, va_list * va)
522 f64 ago = va_arg (*va, double);
523 f64 now = va_arg (*va, double);
526 return format (s, "never");
528 return format (s, "%.1fs ago", now - ago);
532 format_lldp_intfs_detail (u8 * s, vlib_main_t * vm, const lldp_main_t * lm)
534 vnet_main_t *vnm = &vnet_main;
535 const lldp_intf_t *n;
536 const vnet_hw_interface_t *hw;
537 const vnet_sw_interface_t *sw;
538 s = format (s, "LLDP configuration:\n");
541 s = format (s, "Configured system name: %U\n", format_ascii_bytes,
542 lm->sys_name, vec_len (lm->sys_name));
545 s = format (s, "Configured tx-hold: %d\n", (int) lm->msg_tx_hold);
546 s = format (s, "Configured tx-interval: %d\n", (int) lm->msg_tx_interval);
547 s = format (s, "\nLLDP-enabled interface table:\n");
548 f64 now = vlib_time_now (vm);
553 hw = vnet_get_hw_interface(vnm, n->hw_if_index);
554 sw = vnet_get_sw_interface(lm->vnet_main, hw->sw_if_index);
556 s = format(s, "\nLocal Interface name: %s\n"
557 "Local Port Description: %s\n",
558 hw->name, n->port_desc);
561 s = format (s, "Local Management address: %U\n",
562 format_ip4_address, n->mgmt_ip4, vec_len (n->mgmt_ip4));
567 s = format (s, "Local Management address IPV6: %U\n",
568 format_ip6_address, n->mgmt_ip6, vec_len (n->mgmt_ip6));
573 s = format (s, "Local Management address OID: %U\n",
574 format_ascii_bytes, n->mgmt_oid, vec_len (n->mgmt_oid));
577 /* Interface shutdown */
578 if (!(sw->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP))
580 s = format(s, "Interface/peer state: interface down\n"
581 "Last packet sent: %U\n",
582 format_time_ago, n->last_sent, now);
584 else if (now < n->last_heard + n->ttl)
587 "Interface/peer state: active\n"
588 "Peer chassis ID: %U\nRemote port ID: %U\n"
589 "Last packet sent: %U\nLast packet received: %U\n",
590 format_lldp_chassis_id, n->chassis_id_subtype,
591 n->chassis_id, vec_len(n->chassis_id), 1,
592 format_lldp_port_id, n->port_id_subtype, n->port_id,
593 vec_len(n->port_id), 1, format_time_ago, n->last_sent,
594 now, format_time_ago, n->last_heard, now);
599 "Interface/peer state: inactive(timeout)\n"
600 "Last known peer chassis ID: %U\n"
601 "Last known peer port ID: %U\nLast packet sent: %U\n"
602 "Last packet received: %U\n",
603 format_lldp_chassis_id, n->chassis_id_subtype,
604 n->chassis_id, vec_len(n->chassis_id), 1,
605 format_lldp_port_id, n->port_id_subtype, n->port_id,
606 vec_len(n->port_id), 1, format_time_ago, n->last_sent,
607 now, format_time_ago, n->last_heard, now);
615 format_lldp_intfs (u8 * s, va_list * va)
617 vlib_main_t *vm = va_arg (*va, vlib_main_t *);
618 const lldp_main_t *lm = va_arg (*va, lldp_main_t *);
619 const int detail = va_arg (*va, int);
620 vnet_main_t *vnm = &vnet_main;
621 const lldp_intf_t *n;
625 return format_lldp_intfs_detail (s, vm, lm);
628 f64 now = vlib_time_now (vm);
629 s = format (s, "%-25s %-25s %-25s %=15s %=15s %=10s\n", "Local interface",
630 "Peer chassis ID", "Remote port ID", "Last heard", "Last sent",
636 const vnet_hw_interface_t *hw =
637 vnet_get_hw_interface(vnm, n->hw_if_index);
638 const vnet_sw_interface_t *sw =
639 vnet_get_sw_interface(lm->vnet_main, hw->sw_if_index);
640 /* Interface shutdown */
641 if (!(sw->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP))
643 if (now < n->last_heard + n->ttl)
645 s = format(s, "%-25s %-25U %-25U %=15U %=15U %=10s\n", hw->name,
646 format_lldp_chassis_id, n->chassis_id_subtype,
647 n->chassis_id, vec_len(n->chassis_id), 0,
648 format_lldp_port_id, n->port_id_subtype, n->port_id,
649 vec_len(n->port_id), 0, format_time_ago, n->last_heard,
650 now, format_time_ago, n->last_sent, now, "active");
654 s = format(s, "%-25s %-25s %-25s %=15U %=15U %=10s\n", hw->name,
655 "", "", format_time_ago, n->last_heard, now,
656 format_time_ago, n->last_sent, now, "inactive");
663 static clib_error_t *
664 show_lldp (vlib_main_t * vm, unformat_input_t * input,
665 CLIB_UNUSED (vlib_cli_command_t * lmd))
667 lldp_main_t *lm = &lldp_main;
669 if (unformat (input, "detail"))
671 vlib_cli_output (vm, "%U\n", format_lldp_intfs, vm, lm, 1);
675 vlib_cli_output (vm, "%U\n", format_lldp_intfs, vm, lm, 0);
681 VLIB_CLI_COMMAND(show_lldp_command, static) = {
683 .short_help = "show lldp [detail]",
684 .function = show_lldp,
689 * packet trace format function, very similar to
690 * lldp_packet_scan except that we call the per TLV format
691 * functions instead of the per TLV processing functions
694 lldp_input_format_trace (u8 * s, va_list * args)
696 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
697 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
698 const lldp_input_trace_t *t = va_arg (*args, lldp_input_trace_t *);
700 const lldp_tlv_t *tlv;
702 while (((cur + lldp_tlv_get_length ((lldp_tlv_t *) cur)) <
705 tlv = (lldp_tlv_t *) cur;
708 s = format (s, "TLV #%d(%s): %U\n", lldp_tlv_get_code (tlv),
709 lldp_tlv_code_str (lldp_tlv_get_code (tlv)),
710 format_lldp_tlv, tlv);
714 s = format (s, " TLV #%d(%s): %U\n", lldp_tlv_get_code (tlv),
715 lldp_tlv_code_str (lldp_tlv_get_code (tlv)),
716 format_lldp_tlv, tlv);
718 cur += STRUCT_SIZE_OF (lldp_tlv_t, head) + lldp_tlv_get_length (tlv);
725 * fd.io coding-style-patch-verification: ON
728 * eval: (c-set-style "gnu")