Add custom mac address option to vhost interfaces.

Generating a new MAC address everytime vpp reboots can quickly
become cumbersome (e.g. in test scenarios).
This commit allows vhost mac addresses to be configured
manually.

Change-Id: Ic6d16a2d14cdf8e49dc29041bbafde39d9fc8370
Signed-off-by: Pierre Pfister <ppfister@cisco.com>
diff --git a/vnet/vnet/devices/dpdk/dpdk.h b/vnet/vnet/devices/dpdk/dpdk.h
index 2a9d825..3e287a9 100644
--- a/vnet/vnet/devices/dpdk/dpdk.h
+++ b/vnet/vnet/devices/dpdk/dpdk.h
@@ -511,7 +511,8 @@
                               u8 is_server,
                               u32 * sw_if_index,
                               u64 feature_mask,
-                              u8 renumber, u32 custom_dev_instance);
+                              u8 renumber, u32 custom_dev_instance,
+                              u8 *hwaddr);
 int dpdk_vhost_user_modify_if (vnet_main_t * vnm, vlib_main_t * vm,
                               const char * sock_filename,
                               u8 is_server,
diff --git a/vnet/vnet/devices/dpdk/vhost_user.c b/vnet/vnet/devices/dpdk/vhost_user.c
index 0965456..a9b2082 100644
--- a/vnet/vnet/devices/dpdk/vhost_user.c
+++ b/vnet/vnet/devices/dpdk/vhost_user.c
@@ -187,7 +187,7 @@
 }
 
 static clib_error_t *
-dpdk_create_vhost_user_if_internal (u32 * hw_if_index, u32 if_id)
+dpdk_create_vhost_user_if_internal (u32 * hw_if_index, u32 if_id, u8 *hwaddr)
 {
   dpdk_main_t * dm = &dpdk_main;
   vlib_main_t * vm = vlib_get_main();
@@ -332,6 +332,9 @@
   /*
    * Generate random MAC address for the interface
    */
+  if (hwaddr) {
+    memcpy(addr, hwaddr, sizeof(addr));
+  } else {
     f64 now = vlib_time_now(vm);
     u32 rnd;
     rnd = (u32) (now * 1e6);
@@ -340,6 +343,7 @@
     memcpy (addr+2, &rnd, sizeof(rnd));
     addr[0] = 2;
     addr[1] = 0xfe;
+  }
 
   error = ethernet_register_interface
     (dm->vnet_main,
@@ -1275,7 +1279,8 @@
                               u8 is_server,
                               u32 * sw_if_index,
                               u64 feature_mask,
-                              u8 renumber, u32 custom_dev_instance)
+                              u8 renumber, u32 custom_dev_instance,
+                              u8 *hwaddr)
 {
   dpdk_main_t * dm = &dpdk_main;
   dpdk_device_t *xd;
@@ -1286,7 +1291,7 @@
   // using virtio vhost user?
   if (dm->use_virtio_vhost) {
       return vhost_user_create_if(vnm, vm, sock_filename, is_server,
-              sw_if_index, feature_mask, renumber, custom_dev_instance);
+              sw_if_index, feature_mask, renumber, custom_dev_instance, hwaddr);
   }
 
   if (is_server) {
@@ -1300,9 +1305,9 @@
       if (custom_dev_instance >= dm->next_vu_if_id)
           dm->next_vu_if_id = custom_dev_instance + 1;
 
-    dpdk_create_vhost_user_if_internal(&hw_if_idx, custom_dev_instance);
+    dpdk_create_vhost_user_if_internal(&hw_if_idx, custom_dev_instance, hwaddr);
   } else 
-    dpdk_create_vhost_user_if_internal(&hw_if_idx, (u32)~0);
+    dpdk_create_vhost_user_if_internal(&hw_if_idx, (u32)~0, hwaddr);
   DBG_SOCK("dpdk vhost-user interface created hw_if_index %d", hw_if_idx);
 
   xd = dpdk_vhost_user_device_from_hw_if_index(hw_if_idx);
@@ -1548,6 +1553,8 @@
   u64 feature_mask = (u64)~0;
   u8 renumber = 0;
   u32 custom_dev_instance = ~0;
+  u8 hwaddr[6];
+  u8 *hw = NULL;
 
   if (dm->use_virtio_vhost) {
       return vhost_user_connect_command_fn(vm, input, cmd);
@@ -1564,6 +1571,8 @@
       is_server = 1;
     else if (unformat (line_input, "feature-mask 0x%llx", &feature_mask))
       ;
+    else if (unformat (line_input, "hwaddr %U", unformat_ethernet_address, hwaddr))
+      hw = hwaddr;
     else if (unformat (line_input, "renumber %d", &custom_dev_instance)) {
         renumber = 1;
     }
@@ -1579,7 +1588,7 @@
 
   dpdk_vhost_user_create_if(vnm, vm, (char *)sock_filename,
                             is_server, &sw_if_index, feature_mask,
-                            renumber, custom_dev_instance);
+                            renumber, custom_dev_instance, hw);
 
   vec_free(sock_filename);
   return 0;
diff --git a/vnet/vnet/devices/virtio/vhost-user.c b/vnet/vnet/devices/virtio/vhost-user.c
index 180393d..df09db8 100644
--- a/vnet/vnet/devices/virtio/vhost-user.c
+++ b/vnet/vnet/devices/virtio/vhost-user.c
@@ -1429,14 +1429,16 @@
 
 // create ethernet interface for vhost user intf
 static void vhost_user_create_ethernet(vnet_main_t * vnm, vlib_main_t * vm,
-                                       vhost_user_intf_t *vui)
+                                       vhost_user_intf_t *vui, u8 *hwaddress)
 {
   vhost_user_main_t * vum = &vhost_user_main;
   u8 hwaddr[6];
   clib_error_t * error;
 
   /* create hw and sw interface */
-  {
+  if (hwaddress) {
+    memcpy(hwaddr, hwaddress, 6);
+  } else {
     f64 now = vlib_time_now(vm);
     u32 rnd;
     rnd = (u32) (now * 1e6);
@@ -1529,7 +1531,8 @@
                          u8 is_server,
                          u32 * sw_if_index,
                          u64 feature_mask,
-                         u8 renumber, u32 custom_dev_instance)
+                         u8 renumber, u32 custom_dev_instance,
+                         u8 *hwaddr)
 {
   vhost_user_intf_t * vui = NULL;
   dpdk_main_t * dm = &dpdk_main;
@@ -1553,7 +1556,7 @@
   vui = vhost_user_vui_new ();
   ASSERT(vui != NULL);
 
-  vhost_user_create_ethernet (vnm, vm, vui);
+  vhost_user_create_ethernet (vnm, vm, vui, hwaddr);
   vhost_user_vui_init (vnm, vui, sockfd, sock_filename, is_server,
                        feature_mask, &sw_if_idx);
 
@@ -1626,6 +1629,8 @@
   u64 feature_mask = (u64)~0;
   u8 renumber = 0;
   u32 custom_dev_instance = ~0;
+  u8 hwaddr[6];
+  u8 *hw = NULL;
 
   /* Get a line of input. */
   if (! unformat_user (input, unformat_line_input, line_input))
@@ -1638,6 +1643,8 @@
       is_server = 1;
     else if (unformat (line_input, "feature-mask 0x%llx", &feature_mask))
       ;
+    else if (unformat (line_input, "hwaddr %U", unformat_ethernet_address, hwaddr))
+          hw = hwaddr;
     else if (unformat (line_input, "renumber %d", &custom_dev_instance)) {
         renumber = 1;
     }
@@ -1651,7 +1658,7 @@
 
   vhost_user_create_if(vnm, vm, (char *)sock_filename,
                        is_server, &sw_if_index, feature_mask,
-                       renumber, custom_dev_instance);
+                       renumber, custom_dev_instance, hw);
 
   vec_free(sock_filename);
 
diff --git a/vnet/vnet/devices/virtio/vhost-user.h b/vnet/vnet/devices/virtio/vhost-user.h
index 1fbc0f0..4eb4b81 100644
--- a/vnet/vnet/devices/virtio/vhost-user.h
+++ b/vnet/vnet/devices/virtio/vhost-user.h
@@ -50,7 +50,7 @@
 int vhost_user_create_if(vnet_main_t * vnm, vlib_main_t * vm, 
     const char * sock_filename, u8 is_server,
     u32 * sw_if_index, u64 feature_mask,
-    u8 renumber, u32 custom_dev_instance);
+    u8 renumber, u32 custom_dev_instance, u8 *hwaddr);
 int vhost_user_modify_if(vnet_main_t * vnm, vlib_main_t * vm,
     const char * sock_filename, u8 is_server,
     u32 sw_if_index, u64 feature_mask,
diff --git a/vpp-api-test/vat/api_format.c b/vpp-api-test/vat/api_format.c
index 13b2c0d..a1a023b 100644
--- a/vpp-api-test/vat/api_format.c
+++ b/vpp-api-test/vat/api_format.c
@@ -6893,6 +6893,8 @@
     u8 is_server = 0;
     u8 file_name_set = 0;
     u32 custom_dev_instance = ~0;
+    u8 hwaddr[6];
+    u8 use_custom_mac = 0;
 
     while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT) {
       if (unformat (i, "socket %s", &file_name)) {
@@ -6900,6 +6902,8 @@
       }
       else if (unformat (i, "renumber %"PRIu32, &custom_dev_instance))
         ;
+      else if (unformat (i, "mac %U", unformat_ethernet_address, hwaddr))
+        use_custom_mac = 1;
       else if (unformat (i, "server"))
         is_server = 1;
       else
@@ -6926,6 +6930,8 @@
         mp->renumber = 1;
         mp->custom_dev_instance = ntohl(custom_dev_instance);
     }
+    mp->use_custom_mac = use_custom_mac;
+    memcpy(mp->mac_address, hwaddr, 6);
 
     S; W;
     /* NOTREACHED */
@@ -8993,7 +8999,8 @@
   "[disable][push-[1|2]][pop-[1|2]][translate-1-[1|2]] \n"              \
   "[translate-2-[1|2]] [push_dot1q 0] tag1 <nn> tag2 <nn>")             \
 _(create_vhost_user_if,                                                 \
-        "socket <filename> [server] [renumber <dev_instance>]")         \
+        "socket <filename> [server] [renumber <dev_instance>] "         \
+        "[mac <mac_address>]")                                          \
 _(modify_vhost_user_if,                                                 \
         "<intfc> | sw_if_index <nn> socket <filename>\n"                \
         "[server] [renumber <dev_instance>]")                           \
diff --git a/vpp/api/api.c b/vpp/api/api.c
index e14eef6..e30240c 100644
--- a/vpp/api/api.c
+++ b/vpp/api/api.c
@@ -3551,7 +3551,8 @@
 
     rv = dpdk_vhost_user_create_if(vnm, vm, (char *)mp->sock_filename,
                               mp->is_server, &sw_if_index, (u64)~0,
-                              mp->renumber, ntohl(mp->custom_dev_instance));
+                              mp->renumber, ntohl(mp->custom_dev_instance),
+                              (mp->use_custom_mac)?mp->mac_address:NULL);
 
     REPLY_MACRO2(VL_API_CREATE_VHOST_USER_IF_REPLY,
     ({
diff --git a/vpp/api/vpe.api b/vpp/api/vpe.api
index d76a5c8..05ba556 100644
--- a/vpp/api/vpe.api
+++ b/vpp/api/vpe.api
@@ -1903,6 +1903,8 @@
     @param client_index - opaque cookie to identify the sender
     @param is_server - our side is socket server
     @param sock_filename - unix socket filename, used to speak with frontend
+    @param use_custom_mac - enable or disable the use of the provided hardware address
+    @param mac_address - hardware address to use if 'use_custom_mac' is set
 */
 define create_vhost_user_if {
    u32 client_index;
@@ -1911,6 +1913,8 @@
    u8 sock_filename[256];
    u8 renumber;
    u32 custom_dev_instance;
+   u8 use_custom_mac;
+   u8 mac_address[6];
 };
 
 /** \brief vhost-user interface create response