P2P Ethernet - API

API for P2P Ethernet feature

Change-Id: Id0280f42b9ce2428262e79c4dc309595037cd10e
Signed-off-by: Pavel Kotucek <pkotucek@cisco.com>
diff --git a/src/vat/api_format.c b/src/vat/api_format.c
index 79be42c..f33b459 100644
--- a/src/vat/api_format.c
+++ b/src/vat/api_format.c
@@ -4445,7 +4445,9 @@
 _(punt_reply)                                           \
 _(feature_enable_disable_reply)				\
 _(sw_interface_tag_add_del_reply)			\
-_(sw_interface_set_mtu_reply)
+_(sw_interface_set_mtu_reply)                           \
+_(p2p_ethernet_add_reply)                               \
+_(p2p_ethernet_del_reply)
 
 #define _(n)                                    \
     static void vl_api_##n##_t_handler          \
@@ -4720,7 +4722,9 @@
 _(L2_XCONNECT_DETAILS, l2_xconnect_details)                             \
 _(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)
+_(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)
 
 #define foreach_standalone_reply_msg					\
 _(SW_INTERFACE_SET_FLAGS, sw_interface_set_flags)                       \
@@ -18689,6 +18693,101 @@
   return ret;
 }
 
+static int
+api_p2p_ethernet_add (vat_main_t * vam)
+{
+  unformat_input_t *i = vam->input;
+  vl_api_p2p_ethernet_add_t *mp;
+  u32 parent_if_index = ~0;
+  u8 remote_mac[6];
+  u8 mac_set = 0;
+  int ret;
+
+  memset (remote_mac, 0, sizeof (remote_mac));
+  while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
+    {
+      if (unformat (i, "%U", api_unformat_sw_if_index, vam, &parent_if_index))
+	;
+      else if (unformat (i, "sw_if_index %d", &parent_if_index))
+	;
+      else
+	if (unformat
+	    (i, "remote_mac %U", unformat_ethernet_address, remote_mac))
+	mac_set++;
+      else
+	{
+	  clib_warning ("parse error '%U'", format_unformat_error, i);
+	  return -99;
+	}
+    }
+
+  if (parent_if_index == ~0)
+    {
+      errmsg ("missing interface name or sw_if_index");
+      return -99;
+    }
+  if (mac_set == 0)
+    {
+      errmsg ("missing remote mac address");
+      return -99;
+    }
+
+  M (P2P_ETHERNET_ADD, mp);
+  mp->parent_if_index = ntohl (parent_if_index);
+  clib_memcpy (mp->remote_mac, remote_mac, sizeof (remote_mac));
+
+  S (mp);
+  W (ret);
+  return ret;
+}
+
+static int
+api_p2p_ethernet_del (vat_main_t * vam)
+{
+  unformat_input_t *i = vam->input;
+  vl_api_p2p_ethernet_del_t *mp;
+  u32 parent_if_index = ~0;
+  u8 remote_mac[6];
+  u8 mac_set = 0;
+  int ret;
+
+  memset (remote_mac, 0, sizeof (remote_mac));
+  while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
+    {
+      if (unformat (i, "%U", api_unformat_sw_if_index, vam, &parent_if_index))
+	;
+      else if (unformat (i, "sw_if_index %d", &parent_if_index))
+	;
+      else
+	if (unformat
+	    (i, "remote_mac %U", unformat_ethernet_address, remote_mac))
+	mac_set++;
+      else
+	{
+	  clib_warning ("parse error '%U'", format_unformat_error, i);
+	  return -99;
+	}
+    }
+
+  if (parent_if_index == ~0)
+    {
+      errmsg ("missing interface name or sw_if_index");
+      return -99;
+    }
+  if (mac_set == 0)
+    {
+      errmsg ("missing remote mac address");
+      return -99;
+    }
+
+  M (P2P_ETHERNET_DEL, mp);
+  mp->parent_if_index = ntohl (parent_if_index);
+  clib_memcpy (mp->remote_mac, remote_mac, sizeof (remote_mac));
+
+  S (mp);
+  W (ret);
+  return ret;
+}
 
 static int
 q_or_quit (vat_main_t * vam)
@@ -19444,7 +19543,9 @@
 _(l2_xconnect_dump, "")                                             	\
 _(sw_interface_set_mtu, "<intfc> | sw_if_index <nn> mtu <nn>")        \
 _(ip_neighbor_dump, "[ip6] <intfc> | sw_if_index <nn>")                 \
-_(sw_interface_get_table, "<intfc> | sw_if_index <id> [ipv6]")
+_(sw_interface_get_table, "<intfc> | sw_if_index <id> [ipv6]")          \
+_(p2p_ethernet_add, "<intfc> | sw_if_index <nn> remote_mac <mac-address>") \
+_(p2p_ethernet_del, "<intfc> | sw_if_index <nn> remote_mac <mac-address>")
 
 /* 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 361a838..b5ce6d5 100644
--- a/src/vnet.am
+++ b/src/vnet.am
@@ -113,7 +113,9 @@
  vnet/ethernet/interface.c			\
  vnet/ethernet/node.c				\
  vnet/ethernet/pg.c				\
- vnet/ethernet/sfp.c
+ vnet/ethernet/sfp.c				\
+ vnet/ethernet/p2p_ethernet.c			\
+ vnet/ethernet/p2p_ethernet_api.c
 
 nobase_include_HEADERS +=			\
  vnet/ethernet/arp_packet.h			\
@@ -121,7 +123,11 @@
  vnet/ethernet/ethernet.h			\
  vnet/ethernet/packet.h				\
  vnet/ethernet/types.def			\
- vnet/ethernet/sfp.h
+ vnet/ethernet/sfp.h				\
+ vnet/ethernet/p2p_ethernet.api.h		\
+ vnet/ethernet/p2p_ethernet.h
+
+API_FILES += vnet/ethernet/p2p_ethernet.api
 
 ########################################
 # Layer 2 protocol: Ethernet bridging
diff --git a/src/vnet/ethernet/p2p_ethernet.api b/src/vnet/ethernet/p2p_ethernet.api
new file mode 100644
index 0000000..72a7342
--- /dev/null
+++ b/src/vnet/ethernet/p2p_ethernet.api
@@ -0,0 +1,49 @@
+/*
+ * Copyright (c) 2015-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.
+ */
+
+define p2p_ethernet_add
+{
+  u32 client_index;
+  u32 context;
+  u32 parent_if_index;
+  u8  remote_mac[6];
+};
+
+define p2p_ethernet_add_reply
+{
+  u32 context;
+  i32 retval;
+  u32 sw_if_index;
+};
+
+define p2p_ethernet_del
+{
+  u32 client_index;
+  u32 context;
+  u32 parent_if_index;
+  u8  remote_mac[6];
+};
+
+define p2p_ethernet_del_reply
+{
+  u32 context;
+  i32 retval;
+};
+
+/*
+ * Local Variables:
+ * eval: (c-set-style "gnu")
+ * End:
+ */
\ No newline at end of file
diff --git a/src/vnet/ethernet/p2p_ethernet.c b/src/vnet/ethernet/p2p_ethernet.c
new file mode 100644
index 0000000..3c07731
--- /dev/null
+++ b/src/vnet/ethernet/p2p_ethernet.c
@@ -0,0 +1,102 @@
+/*
+ * p2p_ethernet.c: p2p ethernet
+ *
+ * Copyright (c) 2012 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 <vppinfra/bihash_16_8.h>
+#include <vnet/vnet.h>
+#include <vnet/ethernet/p2p_ethernet.h>
+
+int
+p2p_ethernet_add_del (vlib_main_t * vm, u32 parent_if_index,
+		      u8 * client_mac, int is_add)
+{
+  return 0;
+}
+
+static clib_error_t *
+vnet_p2p_ethernet_add_del (vlib_main_t * vm, unformat_input_t * input,
+			   vlib_cli_command_t * cmd)
+{
+  vnet_main_t *vnm = vnet_get_main ();
+
+  int is_add = 1;
+  int remote_mac = 0;
+  u32 hw_if_index = ~0;
+  u8 client_mac[6];
+
+  while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
+    {
+      if (unformat
+	  (input, "%U", unformat_vnet_hw_interface, vnm, &hw_if_index))
+	;
+      else if (unformat (input, "%U", unformat_ethernet_address, &client_mac))
+	remote_mac = 1;
+      else if (unformat (input, "del"))
+	is_add = 0;
+      else
+	break;
+    }
+
+  if (hw_if_index == ~0)
+    return clib_error_return (0, "Please specify parent interface ...");
+  if (!remote_mac)
+    return clib_error_return (0, "Please specify client MAC address ...");
+
+  u32 rv;
+  rv = p2p_ethernet_add_del (vm, hw_if_index, client_mac, is_add);
+  switch (rv)
+    {
+    case VNET_API_ERROR_BOND_SLAVE_NOT_ALLOWED:
+      return clib_error_return (0,
+				"not allowed as parent interface belongs to a BondEthernet interface");
+    case -1:
+      return clib_error_return (0,
+				"p2p ethernet for given parent interface and client mac already exists");
+    case -2:
+      return clib_error_return (0,
+				"couldn't create p2p ethernet subinterface");
+    case -3:
+      return clib_error_return (0,
+				"p2p ethernet for given parent interface and client mac doesn't exist");
+    default:
+      break;
+    }
+  return 0;
+}
+
+/* *INDENT-OFF* */
+VLIB_CLI_COMMAND (p2p_ethernet_add_del_command, static) =
+{
+  .path = "p2p_ethernet ",
+  .function = vnet_p2p_ethernet_add_del,
+  .short_help = "p2p_ethernet <intfc> <mac-address> [del]",};
+/* *INDENT-ON* */
+
+static clib_error_t *
+p2p_ethernet_init (vlib_main_t * vm)
+{
+  return 0;
+}
+
+VLIB_INIT_FUNCTION (p2p_ethernet_init);
+
+/*
+ * fd.io coding-style-patch-verification: ON
+ *
+ * Local Variables:
+ * eval: (c-set-style "gnu")
+ * End:
+ */
diff --git a/src/vnet/ethernet/p2p_ethernet.h b/src/vnet/ethernet/p2p_ethernet.h
new file mode 100644
index 0000000..31b93d8
--- /dev/null
+++ b/src/vnet/ethernet/p2p_ethernet.h
@@ -0,0 +1,23 @@
+/*
+ * Copyright (c) 2015 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.
+ */
+#ifndef included_vnet_p2p_ethernet_h
+#define included_vnet_p2p_ethernet_h
+
+#include <vnet/vnet.h>
+#include <vnet/ethernet/ethernet.h>
+
+int p2p_ethernet_add_del (vlib_main_t * vm, u32 parent_if_index, u8 * client_mac, int is_add);
+
+#endif /* included_vnet_p2p_ethernet_h */
diff --git a/src/vnet/ethernet/p2p_ethernet_api.c b/src/vnet/ethernet/p2p_ethernet_api.c
new file mode 100644
index 0000000..1d9eaeb
--- /dev/null
+++ b/src/vnet/ethernet/p2p_ethernet_api.c
@@ -0,0 +1,128 @@
+/*
+ *------------------------------------------------------------------
+ * p2p_ethernet_api.c - p2p ethernet api
+ *
+ * Copyright (c) 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.
+ *------------------------------------------------------------------
+ */
+
+#include <vnet/vnet.h>
+#include <vlibmemory/api.h>
+
+#include <vnet/vnet_msg_enum.h>
+#include <vnet/ethernet/p2p_ethernet.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                               \
+_(P2P_ETHERNET_ADD, p2p_ethernet_add)                     \
+_(P2P_ETHERNET_DEL, p2p_ethernet_del)
+
+void
+vl_api_p2p_ethernet_add_t_handler (vl_api_p2p_ethernet_add_t * mp)
+{
+  vl_api_p2p_ethernet_add_reply_t *rmp;
+  vlib_main_t *vm = vlib_get_main ();
+  int rv;
+
+  u32 parent_if_index = htonl (mp->parent_if_index);
+  u8 remote_mac[6];
+
+  clib_memcpy (remote_mac, mp->remote_mac, 6);
+  rv = p2p_ethernet_add_del (vm, parent_if_index, remote_mac, 1);
+
+  REPLY_MACRO (VL_API_P2P_ETHERNET_ADD_REPLY);
+}
+
+void
+vl_api_p2p_ethernet_del_t_handler (vl_api_p2p_ethernet_del_t * mp)
+{
+  vl_api_p2p_ethernet_del_reply_t *rmp;
+  vlib_main_t *vm = vlib_get_main ();
+  int rv;
+
+  u32 parent_if_index = htonl (mp->parent_if_index);
+  u8 remote_mac[6];
+
+  clib_memcpy (remote_mac, mp->remote_mac, 6);
+  rv = p2p_ethernet_add_del (vm, parent_if_index, remote_mac, 0);
+
+  REPLY_MACRO (VL_API_P2P_ETHERNET_DEL_REPLY);
+}
+
+/*
+ * p2p_ethernet_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_p2p_ethernet;
+#undef _
+}
+
+static clib_error_t *
+p2p_ethernet_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 (p2p_ethernet_api_hookup);
+
+/*
+ * fd.io coding-style-patch-verification: ON
+ *
+ * Local Variables:
+ * eval: (c-set-style "gnu")
+ * End:
+ */
diff --git a/src/vnet/vnet_all_api_h.h b/src/vnet/vnet_all_api_h.h
index 566e22e..5da2acb 100644
--- a/src/vnet/vnet_all_api_h.h
+++ b/src/vnet/vnet_all_api_h.h
@@ -56,6 +56,7 @@
 #include <vnet/dhcp/dhcp.api.h>
 #include <vnet/cop/cop.api.h>
 #include <vnet/policer/policer.api.h>
+#include <vnet/ethernet/p2p_ethernet.api.h>
 
 /*
  * fd.io coding-style-patch-verification: ON
diff --git a/src/vpp/api/custom_dump.c b/src/vpp/api/custom_dump.c
index 9071883..dc9fee9 100644
--- a/src/vpp/api/custom_dump.c
+++ b/src/vpp/api/custom_dump.c
@@ -2932,6 +2932,30 @@
   FINISH;
 }
 
+static void *vl_api_p2p_ethernet_add_t_print
+  (vl_api_p2p_ethernet_add_t * mp, void *handle)
+{
+  u8 *s;
+
+  s = format (0, "SCRIPT: p2p_ethernet_add ");
+  s = format (s, "sw_if_index %d ", ntohl (mp->parent_if_index));
+  s = format (s, "remote_mac %U ", format_ethernet_address, mp->remote_mac);
+
+  FINISH;
+}
+
+static void *vl_api_p2p_ethernet_del_t_print
+  (vl_api_p2p_ethernet_del_t * mp, void *handle)
+{
+  u8 *s;
+
+  s = format (0, "SCRIPT: p2p_ethernet_del ");
+  s = format (s, "sw_if_index %d ", ntohl (mp->parent_if_index));
+  s = format (s, "remote_mac %U ", format_ethernet_address, mp->remote_mac);
+
+  FINISH;
+}
+
 #define foreach_custom_print_no_arg_function                            \
 _(lisp_eid_table_vni_dump)                                              \
 _(lisp_map_resolver_dump)                                               \
@@ -3112,7 +3136,9 @@
 _(IP6_FIB_DUMP, ip6_fib_dump)                                           \
 _(FEATURE_ENABLE_DISABLE, feature_enable_disable)			\
 _(SW_INTERFACE_TAG_ADD_DEL, sw_interface_tag_add_del)			\
-_(SW_INTERFACE_SET_MTU, sw_interface_set_mtu)
+_(SW_INTERFACE_SET_MTU, sw_interface_set_mtu)                           \
+_(P2P_ETHERNET_ADD, p2p_ethernet_add)                                   \
+_(P2P_ETHERNET_DEL, p2p_ethernet_del)
   void
 vl_msg_api_custom_dump_configure (api_main_t * am)
 {