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 <jonshin@cisco.com>
diff --git a/src/vnet/lldp/lldp.api b/src/vnet/lldp/lldp.api
new file mode 100644
index 0000000..02fe32c
--- /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 0000000..473c202
--- /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 0000000..bdada89
--- /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 <vnet/vnet.h>
+#include <vlibmemory/api.h>
+
+#include <vnet/interface.h>
+#include <vnet/api_errno.h>
+#include <vnet/lldp/lldp.h>
+
+#include <vnet/vnet_msg_enum.h>
+
+#define vl_typedefs		/* define message structures */
+#include <vnet/vnet_all_api_h.h>
+#undef vl_typedefs
+
+#define vl_endianfun		/* define message structures */
+#include <vnet/vnet_all_api_h.h>
+#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 <vnet/vnet_all_api_h.h>
+#undef vl_printfun
+
+#include <vlibapi/api_helper_macros.h>
+
+#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 <vnet/vnet_all_api_h.h>
+#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 d6d84bf..af18f90 100644
--- a/src/vnet/lldp/lldp_cli.c
+++ b/src/vnet/lldp/lldp_cli.c
@@ -19,19 +19,13 @@
  *
  */
 #include <vnet/lisp-cp/lisp_types.h>
+#include <vnet/lldp/lldp.h>
 #include <vnet/lldp/lldp_node.h>
 
 #ifndef ETHER_ADDR_LEN
 #include <net/ethernet.h>
 #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 @@
   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 @@
       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_delete_intf (lm, n);
     }
 
-  return 0;
+  return lldp_ok;
 }
 
 static clib_error_t *
@@ -93,24 +94,33 @@
 {
   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))
+  while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
     {
-      return
-	lldp_cfg_err_to_clib_err (lldp_cfg_intf_set (hw_if_index, enable));
+      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;
     }
-  else
-    {
-      return clib_error_return (0, "unknown input `%U'",
-				format_unformat_error, input);
-    }
-  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 @@
 /* *INDENT-OFF* */
 VLIB_CLI_COMMAND(set_interface_lldp_cmd, static) = {
   .path = "set interface lldp",
-  .short_help = "set interface lldp <interface> (enable | disable) ",
+  .short_help = "set interface lldp <interface> | sw_if_index <idx>"
+                " [port-desc <string>] [disable]",
   .function = lldp_intf_cmd,
 };
 
@@ -499,23 +510,26 @@
         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 bac480a..717de89 100644
--- a/src/vnet/lldp/lldp_doc.md
+++ b/src/vnet/lldp/lldp_doc.md
@@ -27,10 +27,12 @@
 
 Per interface setting is done using the "set interface lldp" command
 
-set interface lldp <interface> (enable | disable)
+set interface lldp <interface> | if_index <idx> [port-desc <string>] [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 @@
 
 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 477ca7d..14a10e3 100644
--- a/src/vnet/lldp/lldp_node.h
+++ b/src/vnet/lldp/lldp_node.h
@@ -43,6 +43,9 @@
   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 6cb2627..950b79a 100644
--- a/src/vnet/lldp/lldp_output.c
+++ b/src/vnet/lldp/lldp_output.c
@@ -75,6 +75,20 @@
 }
 
 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)
 {
   const size_t len = vec_len (lm->sys_name);
@@ -99,11 +113,12 @@
 
 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 @@
   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 @@
       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);
     }
 }