Add custom mac address option to vhost interfaces.
[vpp.git] / vnet / vnet / devices / dpdk / vhost_user.c
index d6ed35f..a9b2082 100644 (file)
@@ -39,7 +39,7 @@
 #define DBG_SOCK(args...)
 #endif
 
-static const char *vhost_message_str[] = {
+static const char *vhost_message_str[] __attribute__((unused)) = {
     [VHOST_USER_NONE] = "VHOST_USER_NONE",
     [VHOST_USER_GET_FEATURES] = "VHOST_USER_GET_FEATURES",
     [VHOST_USER_SET_FEATURES] = "VHOST_USER_SET_FEATURES",
@@ -187,7 +187,7 @@ static inline void * map_guest_mem(dpdk_device_t * xd, u64 addr)
 }
 
 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();
@@ -241,8 +241,10 @@ dpdk_create_vhost_user_if_internal (u32 * hw_if_index, u32 if_id)
        }
 
       // reset lockp
-      if (xd->lockp)
-          memset ((void *) xd->lockp, 0, CLIB_CACHE_LINE_BYTES);
+      dpdk_device_lock_free(xd);
+
+      if (xd->tx_q_used < tm->n_vlib_mains)
+        dpdk_device_lock_init(xd);
 
       // reset tx vectors
       for (j = 0; j < tm->n_vlib_mains; j++)
@@ -299,12 +301,8 @@ dpdk_create_vhost_user_if_internal (u32 * hw_if_index, u32 if_id)
           xd->vu_vhost_dev.virtqueue[j]->backend = -1; 
       }
 
-      xd->lockp = NULL;
-      if (xd->tx_q_used < dm->input_cpu_count) {
-          xd->lockp = clib_mem_alloc_aligned (CLIB_CACHE_LINE_BYTES,
-            CLIB_CACHE_LINE_BYTES);
-          memset ((void *) xd->lockp, 0, CLIB_CACHE_LINE_BYTES);
-      }
+      if (xd->tx_q_used < dm->input_cpu_count)
+        dpdk_device_lock_init(xd);
 
       DBG_SOCK("tm->n_vlib_mains: %d. TX %d, RX: %d, num_qpairs: %d, Lock: %p",
         tm->n_vlib_mains, xd->tx_q_used, xd->rx_q_used, num_qpairs, xd->lockp);
@@ -334,6 +332,9 @@ dpdk_create_vhost_user_if_internal (u32 * hw_if_index, u32 if_id)
   /*
    * 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);
@@ -342,6 +343,7 @@ dpdk_create_vhost_user_if_internal (u32 * hw_if_index, u32 if_id)
     memcpy (addr+2, &rnd, sizeof(rnd));
     addr[0] = 2;
     addr[1] = 0xfe;
+  }
 
   error = ethernet_register_interface
     (dm->vnet_main,
@@ -1277,7 +1279,8 @@ int dpdk_vhost_user_create_if(vnet_main_t * vnm, vlib_main_t * vm,
                               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;
@@ -1288,7 +1291,7 @@ int dpdk_vhost_user_create_if(vnet_main_t * vnm, vlib_main_t * vm,
   // 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) {
@@ -1302,9 +1305,9 @@ int dpdk_vhost_user_create_if(vnet_main_t * vnm, vlib_main_t * vm,
       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);
@@ -1550,6 +1553,8 @@ dpdk_vhost_user_connect_command_fn (vlib_main_t * vm,
   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);
@@ -1566,6 +1571,8 @@ dpdk_vhost_user_connect_command_fn (vlib_main_t * vm,
       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;
     }
@@ -1581,7 +1588,7 @@ dpdk_vhost_user_connect_command_fn (vlib_main_t * vm,
 
   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;
@@ -1795,4 +1802,3 @@ VLIB_CLI_COMMAND (show_vhost_user_command, static) = {
     .short_help = "show vhost-user interface",
     .function = show_dpdk_vhost_user_command_fn,
 };
-