Improve interface naming for NICs ports sharing same PCI ID

Change-Id: I68d708f5dd76dc7a46c3c8634aa8f18c515177e5
Signed-off-by: Damjan Marion <damarion@cisco.com>
diff --git a/vnet/vnet/devices/dpdk/dpdk.h b/vnet/vnet/devices/dpdk/dpdk.h
index 05f74b8..00caeaf 100644
--- a/vnet/vnet/devices/dpdk/dpdk.h
+++ b/vnet/vnet/devices/dpdk/dpdk.h
@@ -210,6 +210,8 @@
 
   CLIB_CACHE_LINE_ALIGN_MARK(cacheline1);
 
+  u8 * interface_name_suffix;
+
   /* PMD related */
   u16 tx_q_used;
   u16 rx_q_used;
diff --git a/vnet/vnet/devices/dpdk/format.c b/vnet/vnet/devices/dpdk/format.c
index 68e4c45..25591c2 100644
--- a/vnet/vnet/devices/dpdk/format.c
+++ b/vnet/vnet/devices/dpdk/format.c
@@ -213,22 +213,8 @@
 		 dev_info.pci_dev->addr.devid,
 		 dev_info.pci_dev->addr.function);
 
-  /* address Chelsio cards which share PCI address */
-	if (dm->devices[i].pmd ==  VNET_DPDK_PMD_CXGBE) {
-    struct rte_eth_dev_info di;
-
-    di.pci_dev = 0;
-    rte_eth_dev_info_get(i+1, &di);
-    if (di.pci_dev && memcmp(&dev_info.pci_dev->addr, &di.pci_dev->addr,
-        sizeof(struct rte_pci_addr)) == 0)
-	    return format(ret, "/0");
-
-    di.pci_dev = 0;
-    rte_eth_dev_info_get(i-1, &di);
-    if (di.pci_dev && memcmp(&dev_info.pci_dev->addr, &di.pci_dev->addr,
-        sizeof(struct rte_pci_addr)) == 0)
-	    return format(ret, "/1");
-	}
+  if (dm->devices[i].interface_name_suffix)
+    return format (ret, "/%s", dm->devices[i].interface_name_suffix);
   return ret;
 }
 
diff --git a/vnet/vnet/devices/dpdk/init.c b/vnet/vnet/devices/dpdk/init.c
index 6ac5dbe..5fe22b9 100644
--- a/vnet/vnet/devices/dpdk/init.c
+++ b/vnet/vnet/devices/dpdk/init.c
@@ -234,11 +234,14 @@
   vnet_sw_interface_t * sw;
   vnet_hw_interface_t * hi;
   dpdk_device_t * xd;
+  vlib_pci_addr_t last_pci_addr;
+  u32 last_pci_addr_port = 0;
   vlib_thread_registration_t * tr;
   uword * p;
 
   u32 next_cpu = 0;
   u8 af_packet_port_id = 0;
+  last_pci_addr.as_u32 = ~0;
 
   dm->input_cpu_first_index = 0;
   dm->input_cpu_count = 1;
@@ -316,6 +319,30 @@
       xd->nb_tx_desc = DPDK_NB_TX_DESC_DEFAULT;
       xd->cpu_socket = (i8) rte_eth_dev_socket_id(i);
 
+      /* Handle interface naming for devices with multiple ports sharing same PCI ID */
+      if (dev_info.pci_dev)
+	{
+	  struct rte_eth_dev_info di = {0};
+	  rte_eth_dev_info_get (i + 1, &di);
+	  if (di.pci_dev && pci_addr.as_u32 != last_pci_addr.as_u32 &&
+	      memcmp(&dev_info.pci_dev->addr, &di.pci_dev->addr, sizeof(struct rte_pci_addr)) == 0)
+	    {
+	      xd->interface_name_suffix = format (0, "0");
+	      last_pci_addr.as_u32 = pci_addr.as_u32;
+	      last_pci_addr_port = i;
+	    }
+	  else if (pci_addr.as_u32 == last_pci_addr.as_u32)
+	    {
+	      xd->interface_name_suffix = format (0, "%u", i - last_pci_addr_port);
+	    }
+	  else
+	    {
+	      last_pci_addr.as_u32 = ~0;
+	    }
+	}
+      else
+	last_pci_addr.as_u32 = ~0;
+
       clib_memcpy(&xd->tx_conf, &dev_info.default_txconf,
              sizeof(struct rte_eth_txconf));
       if (dm->conf->no_multi_seg)