VPP-363: add ability to change mac address of the interface
[vpp.git] / vnet / vnet / interface_cli.c
index 7d828f5..03b5133 100644 (file)
@@ -45,6 +45,8 @@
 #include <vnet/vnet.h>
 #include <vnet/ip/ip.h>
 #include <vppinfra/bitmap.h>
+#include <vnet/fib/ip4_fib.h>
+#include <vnet/fib/ip6_fib.h>
 
 static int
 compare_interface_names (void *a1, void *a2)
@@ -290,8 +292,8 @@ show_sw_interfaces (vlib_main_t * vm,
          fib_index6 = vec_elt (im6->fib_index_by_sw_if_index,
                                si->sw_if_index);
 
-       fib4 = vec_elt_at_index (im4->fibs, fib_index4);
-       fib6 = vec_elt_at_index (im6->fibs, fib_index6);
+       fib4 = ip4_fib_get (fib_index4);
+       fib6 = ip6_fib_get (fib_index6);
 
        if (si->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED)
          vlib_cli_output
@@ -634,7 +636,6 @@ create_sub_interfaces (vlib_main_t * vm,
   else if (unformat (input, "%d-%d", &id_min, &id_max))
     {
       template.sub.eth.flags.one_tag = 1;
-      template.sub.eth.outer_vlan_id = id_min;
       template.sub.eth.flags.exact_match = 1;
       if (id_min > id_max)
        goto id_error;
@@ -687,6 +688,9 @@ create_sub_interfaces (vlib_main_t * vm,
       template.type = VNET_SW_INTERFACE_TYPE_SUB;
       template.sup_sw_if_index = hi->sw_if_index;
       template.sub.id = id;
+      if (id_min < id_max)
+       template.sub.eth.outer_vlan_id = id;
+
       error = vnet_create_sw_interface (vnm, &template, &sw_if_index);
       if (error)
        goto done;
@@ -813,27 +817,17 @@ set_unnumbered (vlib_main_t * vm,
   int is_set = 0;
   int is_del = 0;
 
-  while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
-    {
-
-      if (unformat (input, "%U use %U",
-                   unformat_vnet_sw_interface, vnm, &unnumbered_sw_if_index,
-                   unformat_vnet_sw_interface, vnm,
-                   &inherit_from_sw_if_index))
-       is_set = 1;
-      else if (unformat (input, "del %U",
-                        unformat_vnet_sw_interface,
-                        vnm, &unnumbered_sw_if_index))
-       is_del = 1;
-      else
-       {
-         if (is_set || is_del)
-           break;
-         else
-           return clib_error_return
-             (0, "parse error '%U'", format_unformat_error, input);
-       }
-    }
+  if (unformat (input, "%U use %U",
+               unformat_vnet_sw_interface, vnm, &unnumbered_sw_if_index,
+               unformat_vnet_sw_interface, vnm, &inherit_from_sw_if_index))
+    is_set = 1;
+  else if (unformat (input, "del %U",
+                    unformat_vnet_sw_interface, vnm,
+                    &unnumbered_sw_if_index))
+    is_del = 1;
+  else
+    return clib_error_return (0, "parse error '%U'",
+                             format_unformat_error, input);
 
   si = vnet_get_sw_interface (vnm, unnumbered_sw_if_index);
   if (is_del)
@@ -841,7 +835,7 @@ set_unnumbered (vlib_main_t * vm,
       si->flags &= ~(VNET_SW_INTERFACE_FLAG_UNNUMBERED);
       si->unnumbered_sw_if_index = (u32) ~ 0;
     }
-  else
+  else if (is_set)
     {
       si->flags |= VNET_SW_INTERFACE_FLAG_UNNUMBERED;
       si->unnumbered_sw_if_index = inherit_from_sw_if_index;
@@ -853,7 +847,7 @@ set_unnumbered (vlib_main_t * vm,
 /* *INDENT-OFF* */
 VLIB_CLI_COMMAND (set_unnumbered_command, static) = {
   .path = "set interface unnumbered",
-  .short_help = "set interface unnumbered [<intfc> use <intfc>][del <intfc>]",
+  .short_help = "set interface unnumbered [<intfc> use <intfc> | del <intfc>]",
   .function = set_unnumbered,
 };
 /* *INDENT-ON* */
@@ -1033,6 +1027,54 @@ VLIB_CLI_COMMAND (set_interface_mtu_cmd, static) = {
 };
 /* *INDENT-ON* */
 
+static clib_error_t *
+set_interface_mac_address (vlib_main_t * vm, unformat_input_t * input,
+                          vlib_cli_command_t * cmd)
+{
+  vnet_main_t *vnm = vnet_get_main ();
+  clib_error_t *error = 0;
+  u32 sw_if_index = ~0;
+  u64 mac = 0;
+
+  if (!unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
+    {
+      error = clib_error_return (0, "unknown interface `%U'",
+                                format_unformat_error, input);
+      goto done;
+    }
+  if (!unformat_user (input, unformat_ethernet_address, &mac))
+    {
+      error = clib_error_return (0, "expected mac address `%U'",
+                                format_unformat_error, input);
+      goto done;
+    }
+  error = vnet_hw_interface_change_mac_address (vnm, sw_if_index, mac);
+done:
+  return error;
+}
+
+/*?
+ * The '<em>set interface mac address </em>' command allows to set MAC address of given interface.
+ * In case of NIC interfaces the one has to support MAC address change. A side effect of MAC address
+ * change are changes of MAC addresses in FIB tables (ipv4 and ipv6).
+ *
+ * @cliexpar
+ * @parblock
+ * Example of how to change MAC Address of interface:
+ * @cliexcmd{set interface mac address GigabitEthernet0/8/0 aa:bb:cc:dd:ee:01}
+ * @cliexcmd{set interface mac address host-vpp0 aa:bb:cc:dd:ee:02}
+ * @cliexcmd{set interface mac address tap-0 aa:bb:cc:dd:ee:03}
+ * @cliexcmd{set interface mac address pg0 aa:bb:cc:dd:ee:04}
+ * @endparblock
+?*/
+/* *INDENT-OFF* */
+VLIB_CLI_COMMAND (set_interface_mac_address_cmd, static) = {
+  .path = "set interface mac address",
+  .short_help = "set interface mac address <intfc> <mac-address>",
+  .function = set_interface_mac_address,
+};
+/* *INDENT-ON* */
+
 /*
  * fd.io coding-style-patch-verification: ON
  *