From 7cd468a3d7dee7d6c92f69a0bb7061ae208ec727 Mon Sep 17 00:00:00 2001 From: Damjan Marion Date: Mon, 19 Dec 2016 23:05:39 +0100 Subject: Reorganize source tree to use single autotools instance Change-Id: I7b51f88292e057c6443b12224486f2d0c9f8ae23 Signed-off-by: Damjan Marion --- src/vnet/lldp/lldp_output.c | 216 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 216 insertions(+) create mode 100644 src/vnet/lldp/lldp_output.c (limited to 'src/vnet/lldp/lldp_output.c') diff --git a/src/vnet/lldp/lldp_output.c b/src/vnet/lldp/lldp_output.c new file mode 100644 index 00000000..6cb26270 --- /dev/null +++ b/src/vnet/lldp/lldp_output.c @@ -0,0 +1,216 @@ +/* + * Copyright (c) 2011-2016 Cisco and/or its affiliates. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/** + * @file + * @brief LLDP packet generation implementation + */ +#include + +static void +lldp_add_chassis_id (const vnet_hw_interface_t * hw, u8 ** t0p) +{ + lldp_chassis_id_tlv_t *t = (lldp_chassis_id_tlv_t *) * t0p; + + lldp_tlv_set_code ((lldp_tlv_t *) t, LLDP_TLV_NAME (chassis_id)); + t->subtype = LLDP_CHASS_ID_SUBTYPE_NAME (mac_addr); + + const size_t addr_len = 6; + clib_memcpy (&t->id, hw->hw_address, addr_len); + const size_t len = + STRUCT_SIZE_OF (lldp_chassis_id_tlv_t, subtype) + addr_len; + lldp_tlv_set_length ((lldp_tlv_t *) t, len); + *t0p += STRUCT_SIZE_OF (lldp_tlv_t, head) + len; +} + +static void +lldp_add_port_id (const vnet_hw_interface_t * hw, u8 ** t0p) +{ + lldp_port_id_tlv_t *t = (lldp_port_id_tlv_t *) * t0p; + + lldp_tlv_set_code ((lldp_tlv_t *) t, LLDP_TLV_NAME (port_id)); + t->subtype = LLDP_PORT_ID_SUBTYPE_NAME (intf_name); + + const size_t name_len = vec_len (hw->name); + clib_memcpy (&t->id, hw->name, name_len); + const size_t len = STRUCT_SIZE_OF (lldp_port_id_tlv_t, subtype) + name_len; + lldp_tlv_set_length ((lldp_tlv_t *) t, len); + *t0p += STRUCT_SIZE_OF (lldp_tlv_t, head) + len; +} + +static void +lldp_add_ttl (const lldp_main_t * lm, u8 ** t0p, int shutdown) +{ + lldp_ttl_tlv_t *t = (lldp_ttl_tlv_t *) * t0p; + lldp_tlv_set_code ((lldp_tlv_t *) t, LLDP_TLV_NAME (ttl)); + if (shutdown) + { + t->ttl = 0; + } + else + { + if ((size_t) lm->msg_tx_interval * lm->msg_tx_hold + 1 > (1 << 16) - 1) + { + t->ttl = htons ((1 << 16) - 1); + } + else + { + t->ttl = htons (lm->msg_tx_hold * lm->msg_tx_interval + 1); + } + } + const size_t len = STRUCT_SIZE_OF (lldp_ttl_tlv_t, ttl); + lldp_tlv_set_length ((lldp_tlv_t *) t, len); + *t0p += STRUCT_SIZE_OF (lldp_tlv_t, head) + len; +} + +static void +lldp_add_sys_name (const lldp_main_t * lm, u8 ** t0p) +{ + const size_t len = vec_len (lm->sys_name); + if (len) + { + lldp_tlv_t *t = (lldp_tlv_t *) * t0p; + lldp_tlv_set_code (t, LLDP_TLV_NAME (sys_name)); + lldp_tlv_set_length (t, len); + clib_memcpy (t->v, lm->sys_name, len); + *t0p += STRUCT_SIZE_OF (lldp_tlv_t, head) + len; + } +} + +static void +lldp_add_pdu_end (u8 ** t0p) +{ + lldp_tlv_t *t = (lldp_tlv_t *) * t0p; + lldp_tlv_set_code (t, LLDP_TLV_NAME (pdu_end)); + lldp_tlv_set_length (t, 0); + *t0p += STRUCT_SIZE_OF (lldp_tlv_t, head); +} + +static void +lldp_add_tlvs (lldp_main_t * lm, vnet_hw_interface_t * hw, u8 ** t0p, + int shutdown) +{ + lldp_add_chassis_id (hw, t0p); + lldp_add_port_id (hw, t0p); + lldp_add_ttl (lm, t0p, shutdown); + lldp_add_sys_name (lm, t0p); + lldp_add_pdu_end (t0p); +} + +/* + * send a lldp pkt on an ethernet interface + */ +void +lldp_send_ethernet (lldp_main_t * lm, lldp_intf_t * n, int shutdown) +{ + u32 *to_next; + ethernet_header_t *h0; + vnet_hw_interface_t *hw; + u32 bi0; + vlib_buffer_t *b0; + u8 *t0; + vlib_frame_t *f; + vlib_main_t *vm = lm->vlib_main; + vnet_main_t *vnm = lm->vnet_main; + + /* + * see lldp_template_init() to understand what's already painted + * into the buffer by the packet template mechanism + */ + h0 = vlib_packet_template_get_packet (vm, &lm->packet_template, &bi0); + + /* Add the interface's ethernet source address */ + hw = vnet_get_hw_interface (vnm, n->hw_if_index); + + clib_memcpy (h0->src_address, hw->hw_address, vec_len (hw->hw_address)); + + u8 *data = ((u8 *) h0) + sizeof (*h0); + t0 = data; + + /* add TLVs */ + lldp_add_tlvs (lm, hw, &t0, shutdown); + + /* Set the outbound packet length */ + b0 = vlib_get_buffer (vm, bi0); + b0->current_length = sizeof (*h0) + t0 - data; + + /* And the outbound interface */ + vnet_buffer (b0)->sw_if_index[VLIB_TX] = hw->sw_if_index; + + /* And output the packet on the correct interface */ + f = vlib_get_frame_to_node (vm, hw->output_node_index); + to_next = vlib_frame_vector_args (f); + to_next[0] = bi0; + f->n_vectors = 1; + + vlib_put_frame_to_node (vm, hw->output_node_index, f); + n->last_sent = vlib_time_now (vm); +} + +void +lldp_delete_intf (lldp_main_t * lm, lldp_intf_t * n) +{ + if (n) + { + lldp_unschedule_intf (lm, n); + hash_unset (lm->intf_by_hw_if_index, n->hw_if_index); + vec_free (n->chassis_id); + vec_free (n->port_id); + pool_put (lm->intfs, n); + } +} + +static clib_error_t * +lldp_template_init (vlib_main_t * vm) +{ + lldp_main_t *lm = &lldp_main; + + /* Create the ethernet lldp packet template */ + { + ethernet_header_t h; + + memset (&h, 0, sizeof (h)); + + /* + * Send to 01:80:C2:00:00:0E - propagation constrained to a single + * physical link - stopped by all type of bridge + */ + h.dst_address[0] = 0x01; + h.dst_address[1] = 0x80; + h.dst_address[2] = 0xC2; + /* h.dst_address[3] = 0x00; (memset) */ + /* h.dst_address[4] = 0x00; (memset) */ + h.dst_address[5] = 0x0E; + + /* leave src address blank (fill in at send time) */ + + h.type = htons (ETHERNET_TYPE_802_1_LLDP); + + vlib_packet_template_init (vm, &lm->packet_template, + /* data */ &h, sizeof (h), + /* alloc chunk size */ 8, "lldp-ethernet"); + } + + return 0; +} + +VLIB_INIT_FUNCTION (lldp_template_init); + +/* + * fd.io coding-style-patch-verification: ON + * + * Local Variables: + * eval: (c-set-style "gnu") + * End: + */ -- cgit 1.2.3-korg From 99a0e60eb6f6acd7eabd5a4cb7ded1e0419ccd54 Mon Sep 17 00:00:00 2001 From: Steve Shin Date: Sat, 1 Jul 2017 04:16:20 +0000 Subject: Add API support for LLDP config/interface set Add API methods to configure LLDP and set interface to enable/disable. Also add port description TLV for LLDP. Change-Id: Ib959d488c2ab8a0069f143558871f41fcc43a5d3 Signed-off-by: Steve Shin --- src/vat/api_format.c | 95 ++++++++++++++++++++++++++++- src/vnet.am | 9 ++- src/vnet/lldp/lldp.api | 47 +++++++++++++++ src/vnet/lldp/lldp.h | 32 ++++++++++ src/vnet/lldp/lldp_api.c | 144 ++++++++++++++++++++++++++++++++++++++++++++ src/vnet/lldp/lldp_cli.c | 86 +++++++++++++++----------- src/vnet/lldp/lldp_doc.md | 10 +-- src/vnet/lldp/lldp_node.h | 3 + src/vnet/lldp/lldp_output.c | 20 +++++- src/vnet/vnet_all_api_h.h | 1 + src/vpp/api/vpe.api | 1 + 11 files changed, 401 insertions(+), 47 deletions(-) create mode 100644 src/vnet/lldp/lldp.api create mode 100644 src/vnet/lldp/lldp.h create mode 100644 src/vnet/lldp/lldp_api.c (limited to 'src/vnet/lldp/lldp_output.c') diff --git a/src/vat/api_format.c b/src/vat/api_format.c index 937d7c5d..40eca8c5 100644 --- a/src/vat/api_format.c +++ b/src/vat/api_format.c @@ -4633,7 +4633,9 @@ _(feature_enable_disable_reply) \ _(sw_interface_tag_add_del_reply) \ _(sw_interface_set_mtu_reply) \ _(p2p_ethernet_add_reply) \ -_(p2p_ethernet_del_reply) +_(p2p_ethernet_del_reply) \ +_(lldp_config_reply) \ +_(sw_interface_set_lldp_reply) #define _(n) \ static void vl_api_##n##_t_handler \ @@ -4915,7 +4917,9 @@ _(SW_INTERFACE_SET_MTU_REPLY, sw_interface_set_mtu_reply) \ _(IP_NEIGHBOR_DETAILS, ip_neighbor_details) \ _(SW_INTERFACE_GET_TABLE_REPLY, sw_interface_get_table_reply) \ _(P2P_ETHERNET_ADD_REPLY, p2p_ethernet_add_reply) \ -_(P2P_ETHERNET_DEL_REPLY, p2p_ethernet_del_reply) +_(P2P_ETHERNET_DEL_REPLY, p2p_ethernet_del_reply) \ +_(LLDP_CONFIG_REPLY, lldp_config_reply) \ +_(SW_INTERFACE_SET_LLDP_REPLY, sw_interface_set_lldp_reply) #define foreach_standalone_reply_msg \ _(SW_INTERFACE_SET_FLAGS, sw_interface_set_flags) \ @@ -19232,6 +19236,88 @@ api_p2p_ethernet_del (vat_main_t * vam) return ret; } +static int +api_lldp_config (vat_main_t * vam) +{ + unformat_input_t *i = vam->input; + vl_api_lldp_config_t *mp; + int tx_hold = 0; + int tx_interval = 0; + u8 *sys_name = NULL; + int ret; + + while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT) + { + if (unformat (i, "system-name %s", &sys_name)) + ; + else if (unformat (i, "tx-hold %d", &tx_hold)) + ; + else if (unformat (i, "tx-interval %d", &tx_interval)) + ; + else + { + clib_warning ("parse error '%U'", format_unformat_error, i); + return -99; + } + } + + vec_add1 (sys_name, 0); + + M (LLDP_CONFIG, mp); + mp->tx_hold = htonl (tx_hold); + mp->tx_interval = htonl (tx_interval); + clib_memcpy (mp->system_name, sys_name, vec_len (sys_name)); + vec_free (sys_name); + + S (mp); + W (ret); + return ret; +} + +static int +api_sw_interface_set_lldp (vat_main_t * vam) +{ + unformat_input_t *i = vam->input; + vl_api_sw_interface_set_lldp_t *mp; + u32 sw_if_index = ~0; + u32 enable = 1; + u8 *port_desc = NULL; + int ret; + + while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT) + { + if (unformat (i, "disable")) + enable = 0; + else + if (unformat (i, "%U", api_unformat_sw_if_index, vam, &sw_if_index)) + ; + else if (unformat (i, "sw_if_index %d", &sw_if_index)) + ; + else if (unformat (i, "port-desc %s", &port_desc)) + ; + else + break; + } + + if (sw_if_index == ~0) + { + errmsg ("missing interface name or sw_if_index"); + return -99; + } + + /* Construct the API message */ + vec_add1 (port_desc, 0); + M (SW_INTERFACE_SET_LLDP, mp); + mp->sw_if_index = ntohl (sw_if_index); + mp->enable = enable; + clib_memcpy (mp->port_desc, port_desc, vec_len (port_desc)); + vec_free (port_desc); + + S (mp); + W (ret); + return ret; +} + static int q_or_quit (vat_main_t * vam) { @@ -19994,7 +20080,10 @@ _(sw_interface_set_mtu, " | sw_if_index mtu ") \ _(ip_neighbor_dump, "[ip6] | sw_if_index ") \ _(sw_interface_get_table, " | sw_if_index [ipv6]") \ _(p2p_ethernet_add, " | sw_if_index remote_mac ") \ -_(p2p_ethernet_del, " | sw_if_index remote_mac ") +_(p2p_ethernet_del, " | sw_if_index remote_mac ") \ +_(lldp_config, "system-name tx-hold tx-interval ") \ +_(sw_interface_set_lldp, \ + " | sw_if_index [port-desc ] [disable]") /* List of command functions, CLI names map directly to functions */ #define foreach_cli_function \ diff --git a/src/vnet.am b/src/vnet.am index b5ce6d5a..ebcf0a0a 100644 --- a/src/vnet.am +++ b/src/vnet.am @@ -269,10 +269,15 @@ libvnet_la_SOURCES += \ vnet/lldp/lldp_input.c \ vnet/lldp/lldp_node.c \ vnet/lldp/lldp_output.c \ - vnet/lldp/lldp_cli.c + vnet/lldp/lldp_cli.c \ + vnet/lldp/lldp_api.c nobase_include_HEADERS += \ - vnet/lldp/lldp_protocol.h + vnet/lldp/lldp_protocol.h \ + vnet/lldp/lldp.h \ + vnet/lldp/lldp.api.h + +API_FILES += vnet/lldp/lldp.api ######################################## # Layer 2/3 "classify" diff --git a/src/vnet/lldp/lldp.api b/src/vnet/lldp/lldp.api new file mode 100644 index 00000000..02fe32ca --- /dev/null +++ b/src/vnet/lldp/lldp.api @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2017 Cisco and/or its affiliates. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** \brief configure global parameter for LLDP + @param client_index - opaque cookie to identify the sender + @param context - sender context, to match reply w/ request + @param system_name - VPP system name + @param tx_hold - multiplier for tx_interval when setting time-to-live (TTL) + value in the LLDP packets + @param tx_interval - time interval, in seconds, between each LLDP frames +*/ +autoreply define lldp_config +{ + u32 client_index; + u32 context; + u8 system_name[256]; + u32 tx_hold; + u32 tx_interval; +}; + +/** \brief Interface set LLDP request + @param client_index - opaque cookie to identify the sender + @param context - sender context, to match reply w/ request + @param sw_if_index - interface for which to enable/disable LLDP + @param port_desc - local port description + @param enable - if non-zero enable, else disable +*/ +autoreply define sw_interface_set_lldp +{ + u32 client_index; + u32 context; + u32 sw_if_index; + u8 port_desc[256]; + u8 enable; +}; diff --git a/src/vnet/lldp/lldp.h b/src/vnet/lldp/lldp.h new file mode 100644 index 00000000..473c2021 --- /dev/null +++ b/src/vnet/lldp/lldp.h @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2017 Cisco and/or its affiliates. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/** + * @file + * @brief LLDP external definition + */ +#ifndef __included_lldp_h__ +#define __included_lldp_h__ + +typedef enum lldp_cfg_err +{ + lldp_ok, + lldp_not_supported, + lldp_invalid_arg, +} lldp_cfg_err_t; + +lldp_cfg_err_t lldp_cfg_intf_set (u32 hw_if_index, u8 ** port_desc, int enable); +lldp_cfg_err_t lldp_cfg_set (u8 ** host, int hold_time, int tx_interval); + +#endif /* __included_lldp_h__ */ diff --git a/src/vnet/lldp/lldp_api.c b/src/vnet/lldp/lldp_api.c new file mode 100644 index 00000000..bdada897 --- /dev/null +++ b/src/vnet/lldp/lldp_api.c @@ -0,0 +1,144 @@ +/* + *------------------------------------------------------------------ + * lldp_api.c - lldp api + * + * Copyright (c) 2017 Cisco and/or its affiliates. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + *------------------------------------------------------------------ + */ + +#include +#include + +#include +#include +#include + +#include + +#define vl_typedefs /* define message structures */ +#include +#undef vl_typedefs + +#define vl_endianfun /* define message structures */ +#include +#undef vl_endianfun + +/* instantiate all the print functions we know about */ +#define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__) +#define vl_printfun +#include +#undef vl_printfun + +#include + +#define foreach_vpe_api_msg \ +_(LLDP_CONFIG, lldp_config) \ +_(SW_INTERFACE_SET_LLDP, sw_interface_set_lldp) + +static void +vl_api_lldp_config_t_handler (vl_api_lldp_config_t * mp) +{ + vl_api_lldp_config_reply_t *rmp; + int rv = 0; + u8 *sys_name = 0; + + vec_validate (sys_name, strlen ((char *) mp->system_name) - 1); + strncpy ((char *) sys_name, (char *) mp->system_name, vec_len (sys_name)); + + if (lldp_cfg_set (&sys_name, ntohl (mp->tx_hold), + ntohl (mp->tx_interval)) != lldp_ok) + { + vec_free (sys_name); + rv = VNET_API_ERROR_INVALID_VALUE; + } + + REPLY_MACRO (VL_API_LLDP_CONFIG_REPLY); +} + +static void +vl_api_sw_interface_set_lldp_t_handler (vl_api_sw_interface_set_lldp_t * mp) +{ + vl_api_sw_interface_set_lldp_reply_t *rmp; + int rv = 0; + u8 *port_desc = 0; + + vec_validate (port_desc, strlen ((char *) mp->port_desc) - 1); + strncpy ((char *) port_desc, (char *) mp->port_desc, vec_len (port_desc)); + + VALIDATE_SW_IF_INDEX (mp); + + if (lldp_cfg_intf_set (ntohl (mp->sw_if_index), &port_desc, + mp->enable) != lldp_ok) + { + vec_free (port_desc); + rv = VNET_API_ERROR_INVALID_VALUE; + } + + BAD_SW_IF_INDEX_LABEL; + + REPLY_MACRO (VL_API_SW_INTERFACE_SET_LLDP_REPLY); +} + + +/* + * * lldp_api_hookup + * * Add vpe's API message handlers to the table. + * * vlib has alread mapped shared memory and + * * added the client registration handlers. + * * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process() + * */ +#define vl_msg_name_crc_list +#include +#undef vl_msg_name_crc_list + +static void +setup_message_id_table (api_main_t * am) +{ +#define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id); + foreach_vl_msg_name_crc_lldp; +#undef _ +} + +static clib_error_t * +lldp_api_hookup (vlib_main_t * vm) +{ + api_main_t *am = &api_main; + +#define _(N,n) \ + vl_msg_api_set_handlers(VL_API_##N, #n, \ + vl_api_##n##_t_handler, \ + vl_noop_handler, \ + vl_api_##n##_t_endian, \ + vl_api_##n##_t_print, \ + sizeof(vl_api_##n##_t), 1); + foreach_vpe_api_msg; +#undef _ + + /* + * * Set up the (msg_name, crc, message-id) table + * */ + setup_message_id_table (am); + + return 0; +} + +VLIB_API_INIT_FUNCTION (lldp_api_hookup); + +/* + * fd.io coding-style-patch-verification: ON + * + * Local Variables: + * eval: (c-set-style "gnu") + * End: + */ diff --git a/src/vnet/lldp/lldp_cli.c b/src/vnet/lldp/lldp_cli.c index d6d84bfd..af18f90f 100644 --- a/src/vnet/lldp/lldp_cli.c +++ b/src/vnet/lldp/lldp_cli.c @@ -19,19 +19,13 @@ * */ #include +#include #include #ifndef ETHER_ADDR_LEN #include #endif -typedef enum lldp_cfg_err -{ - lldp_ok, - lldp_not_supported, - lldp_invalid_arg, -} lldp_cfg_err_t; - static clib_error_t * lldp_cfg_err_to_clib_err (lldp_cfg_err_t e) { @@ -48,8 +42,8 @@ lldp_cfg_err_to_clib_err (lldp_cfg_err_t e) return 0; } -static lldp_cfg_err_t -lldp_cfg_intf_set (u32 hw_if_index, int enable) +lldp_cfg_err_t +lldp_cfg_intf_set (u32 hw_if_index, u8 ** port_desc, int enable) { lldp_main_t *lm = &lldp_main; vnet_main_t *vnm = lm->vnet_main; @@ -68,9 +62,16 @@ lldp_cfg_intf_set (u32 hw_if_index, int enable) if (n) { /* already enabled */ - return 0; + return lldp_ok; } n = lldp_create_intf (lm, hw_if_index); + + if (port_desc && *port_desc) + { + n->port_desc = *port_desc; + *port_desc = NULL; + } + const vnet_sw_interface_t *sw = vnet_get_sw_interface (lm->vnet_main, hi->sw_if_index); if (sw->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) @@ -84,7 +85,7 @@ lldp_cfg_intf_set (u32 hw_if_index, int enable) lldp_delete_intf (lm, n); } - return 0; + return lldp_ok; } static clib_error_t * @@ -93,24 +94,33 @@ lldp_intf_cmd (vlib_main_t * vm, unformat_input_t * input, { lldp_main_t *lm = &lldp_main; vnet_main_t *vnm = lm->vnet_main; - u32 hw_if_index; - int enable = 0; + u32 sw_if_index = (u32) ~ 0; + int enable = 1; + u8 *port_desc = NULL; - if (unformat (input, "%U %U", unformat_vnet_hw_interface, vnm, &hw_if_index, - unformat_vlib_enable_disable, &enable)) - { - return - lldp_cfg_err_to_clib_err (lldp_cfg_intf_set (hw_if_index, enable)); - } - else + while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) { - return clib_error_return (0, "unknown input `%U'", - format_unformat_error, input); + if (unformat (input, "sw_if_index %d", &sw_if_index)) + ; + if (unformat + (input, "%U", unformat_vnet_sw_interface, vnm, &sw_if_index)) + ; + else if (unformat (input, "disable")) + enable = 0; + else if (unformat (input, "port-desc %s", &port_desc)) + ; + else + break; } - return 0; + + if (sw_if_index == (u32) ~ 0) + return clib_error_return (0, "Interface name is invalid!"); + + return lldp_cfg_err_to_clib_err (lldp_cfg_intf_set (sw_if_index, + &port_desc, enable)); } -static lldp_cfg_err_t +lldp_cfg_err_t lldp_cfg_set (u8 ** host, int hold_time, int tx_interval) { lldp_main_t *lm = &lldp_main; @@ -208,7 +218,8 @@ out: /* *INDENT-OFF* */ VLIB_CLI_COMMAND(set_interface_lldp_cmd, static) = { .path = "set interface lldp", - .short_help = "set interface lldp (enable | disable) ", + .short_help = "set interface lldp | sw_if_index " + " [port-desc ] [disable]", .function = lldp_intf_cmd, }; @@ -499,23 +510,26 @@ format_lldp_intfs_detail (u8 * s, vlib_main_t * vm, const lldp_main_t * lm) else if (now < n->last_heard + n->ttl) { s = format(s, - "\nInterface name: %s\nInterface/peer state: " - "active\nPeer chassis ID: %U\nRemote port ID: %U\nLast " - "packet sent: %U\nLast packet received: %U\n", - hw->name, format_lldp_chassis_id, n->chassis_id_subtype, - n->chassis_id, vec_len(n->chassis_id), 1, + "\nInterface name: %s\nPort Desc: %s\nInterface/peer " + "state: active\nPeer chassis ID: %U\nRemote port ID:" + " %U\nLast packet sent: %U\nLast packet received: %U\n", + hw->name, n->port_desc, format_lldp_chassis_id, + n->chassis_id_subtype, n->chassis_id, + vec_len(n->chassis_id), 1, format_lldp_port_id, n->port_id_subtype, n->port_id, vec_len(n->port_id), 1, format_time_ago, n->last_sent, now, format_time_ago, n->last_heard, now); } else { - s = format(s, "\nInterface name: %s\nInterface/peer state: " - "inactive(timeout)\nLast known peer chassis ID: " - "%U\nLast known peer port ID: %U\nLast packet sent: " - "%U\nLast packet received: %U\n", - hw->name, format_lldp_chassis_id, n->chassis_id_subtype, - n->chassis_id, vec_len(n->chassis_id), 1, + s = format(s, + "\nInterface name: %s\nPort Desc: %s\nInterface/peer " + "state: inactive(timeout)\nLast known peer chassis ID:" + "%U\nLast known peer port ID: %U\nLast packet sent: " + "%U\nLast packet received: %U\n", + hw->name, n->port_desc, format_lldp_chassis_id, + n->chassis_id_subtype, n->chassis_id, + vec_len(n->chassis_id), 1, format_lldp_port_id, n->port_id_subtype, n->port_id, vec_len(n->port_id), 1, format_time_ago, n->last_sent, now, format_time_ago, n->last_heard, now); diff --git a/src/vnet/lldp/lldp_doc.md b/src/vnet/lldp/lldp_doc.md index bac480a5..717de898 100644 --- a/src/vnet/lldp/lldp_doc.md +++ b/src/vnet/lldp/lldp_doc.md @@ -27,10 +27,12 @@ tx-interval: time interval between sending out LLDP packets Per interface setting is done using the "set interface lldp" command -set interface lldp (enable | disable) +set interface lldp | if_index [port-desc ] [disable] interface: the name of the interface for which to enable/disable LLDP - +if_index: sw interface index can be used if interface name is not used. +port-desc: port description +disable: LLDP feature can be enabled or disabled per interface. ### Configuration example @@ -38,9 +40,9 @@ Configure system-name as "VPP" and transmit interval to 10 seconds: set lldp system-name VPP tx-interval 10 -Enable LLDP on interface TenGigabitEthernet5/0/1 +Enable LLDP on interface TenGigabitEthernet5/0/1 with port description -set interface lldp TenGigabitEthernet5/0/1 enable +set interface lldp TenGigabitEthernet5/0/1 port-desc vtf:eth0 ### Operational data diff --git a/src/vnet/lldp/lldp_node.h b/src/vnet/lldp/lldp_node.h index 477ca7dc..14a10e33 100644 --- a/src/vnet/lldp/lldp_node.h +++ b/src/vnet/lldp/lldp_node.h @@ -43,6 +43,9 @@ typedef struct lldp_intf lldp_port_id_subtype_t port_id_subtype; lldp_chassis_id_subtype_t chassis_id_subtype; + /* Local info */ + u8 *port_desc; + } lldp_intf_t; typedef struct diff --git a/src/vnet/lldp/lldp_output.c b/src/vnet/lldp/lldp_output.c index 6cb26270..950b79ae 100644 --- a/src/vnet/lldp/lldp_output.c +++ b/src/vnet/lldp/lldp_output.c @@ -74,6 +74,20 @@ lldp_add_ttl (const lldp_main_t * lm, u8 ** t0p, int shutdown) *t0p += STRUCT_SIZE_OF (lldp_tlv_t, head) + len; } +static void +lldp_add_port_desc (const lldp_main_t * lm, lldp_intf_t * n, u8 ** t0p) +{ + const size_t len = vec_len (n->port_desc); + if (len) + { + lldp_tlv_t *t = (lldp_tlv_t *) * t0p; + lldp_tlv_set_code (t, LLDP_TLV_NAME (port_desc)); + lldp_tlv_set_length (t, len); + clib_memcpy (t->v, n->port_desc, len); + *t0p += STRUCT_SIZE_OF (lldp_tlv_t, head) + len; + } +} + static void lldp_add_sys_name (const lldp_main_t * lm, u8 ** t0p) { @@ -99,11 +113,12 @@ lldp_add_pdu_end (u8 ** t0p) static void lldp_add_tlvs (lldp_main_t * lm, vnet_hw_interface_t * hw, u8 ** t0p, - int shutdown) + int shutdown, lldp_intf_t * n) { lldp_add_chassis_id (hw, t0p); lldp_add_port_id (hw, t0p); lldp_add_ttl (lm, t0p, shutdown); + lldp_add_port_desc (lm, n, t0p); lldp_add_sys_name (lm, t0p); lldp_add_pdu_end (t0p); } @@ -139,7 +154,7 @@ lldp_send_ethernet (lldp_main_t * lm, lldp_intf_t * n, int shutdown) t0 = data; /* add TLVs */ - lldp_add_tlvs (lm, hw, &t0, shutdown); + lldp_add_tlvs (lm, hw, &t0, shutdown, n); /* Set the outbound packet length */ b0 = vlib_get_buffer (vm, bi0); @@ -167,6 +182,7 @@ lldp_delete_intf (lldp_main_t * lm, lldp_intf_t * n) hash_unset (lm->intf_by_hw_if_index, n->hw_if_index); vec_free (n->chassis_id); vec_free (n->port_id); + vec_free (n->port_desc); pool_put (lm->intfs, n); } } diff --git a/src/vnet/vnet_all_api_h.h b/src/vnet/vnet_all_api_h.h index 5da2acb7..c1eff61f 100644 --- a/src/vnet/vnet_all_api_h.h +++ b/src/vnet/vnet_all_api_h.h @@ -41,6 +41,7 @@ #include #include #include +#include #include #include #include diff --git a/src/vpp/api/vpe.api b/src/vpp/api/vpe.api index d3c7e985..2a763d19 100644 --- a/src/vpp/api/vpe.api +++ b/src/vpp/api/vpe.api @@ -26,6 +26,7 @@ * IP APIs: see .../src/vnet/ip/{ip.api, ip_api.c} * TAP APIs: see .../src/vnet/unix/{tap.api, tap_api.c} * VXLAN APIs: see .../src/vnet/vxlan/{vxlan.api, vxlan_api.c} + * LLDP APIs: see .../src/vnet/lldp/{lldp.api, lldp_api.c} * AF-PACKET APIs: see ... /vnet/devices/af_packet/{af_packet.api, af_packet_api.c} * NETMAP APIs: see ... /src/vnet/devices/netmap/{netmap.api, netmap_api.c} * VHOST-USER APIs: see .../vnet/devices/virtio/{vhost_user.api, vhost_user_api.c} -- cgit 1.2.3-korg From 1808f3c00a7bcdea7f0c004ef0613db2156c2065 Mon Sep 17 00:00:00 2001 From: Steven Date: Thu, 28 Sep 2017 16:38:56 -0700 Subject: cdp/lldp: punt for no buffer (VPP-997) When making a call to vlib_packet_template_get_packet(), it is possible to get back a NULL if the system runs out of buffer. This can happen when there is buffer leaks. But don't crash just because we run out of buffers, just punt. Change-Id: Ie90ea41f3dda6e583d48959cbd18ff124158d7f8 Signed-off-by: Steven (cherry picked from commit 0ff5c563d5048991dbd02a3892dccde8305a7e30) --- src/vnet/cdp/cdp_periodic.c | 3 +++ src/vnet/lldp/lldp_output.c | 3 +++ 2 files changed, 6 insertions(+) (limited to 'src/vnet/lldp/lldp_output.c') diff --git a/src/vnet/cdp/cdp_periodic.c b/src/vnet/cdp/cdp_periodic.c index de111079..8899c49c 100644 --- a/src/vnet/cdp/cdp_periodic.c +++ b/src/vnet/cdp/cdp_periodic.c @@ -125,6 +125,9 @@ send_ethernet_hello (cdp_main_t * cm, cdp_neighbor_t * n, int count) h0 = vlib_packet_template_get_packet (vm, &cm->packet_templates[n->packet_template_index], &bi0); + if (!h0) + break; + /* Add the interface's ethernet source address */ hw = vnet_get_sup_hw_interface (vnm, n->sw_if_index); diff --git a/src/vnet/lldp/lldp_output.c b/src/vnet/lldp/lldp_output.c index 950b79ae..8698ec97 100644 --- a/src/vnet/lldp/lldp_output.c +++ b/src/vnet/lldp/lldp_output.c @@ -145,6 +145,9 @@ lldp_send_ethernet (lldp_main_t * lm, lldp_intf_t * n, int shutdown) */ h0 = vlib_packet_template_get_packet (vm, &lm->packet_template, &bi0); + if (!h0) + return; + /* Add the interface's ethernet source address */ hw = vnet_get_hw_interface (vnm, n->hw_if_index); -- cgit 1.2.3-korg