[libvirt] [PATCH 4/4] qemu_hostdev: Add support to install port profile and mac address on hostdevs

2012-03-04 Thread Roopa Prabhu
From: Roopa Prabhu ropra...@cisco.com

These changes are applied only if the hostdev has a parent net device.
If the parent netdevice has virtual port information, the original virtualport
associate functions are called (these set and restore both mac and port profile
on an interface). Else, only mac address is set on the device
using other methods depending on if its a sriov device or not.

Changes also include hotplug pci devices

Signed-off-by: Roopa Prabhu ropra...@cisco.com
---
 src/qemu/qemu_hostdev.c |  237 +--
 src/qemu/qemu_hostdev.h |8 +-
 src/qemu/qemu_hotplug.c |   11 ++
 3 files changed, 243 insertions(+), 13 deletions(-)


diff --git a/src/qemu/qemu_hostdev.c b/src/qemu/qemu_hostdev.c
index b3cad8e..62fe467 100644
--- a/src/qemu/qemu_hostdev.c
+++ b/src/qemu/qemu_hostdev.c
@@ -29,6 +29,13 @@
 #include memory.h
 #include pci.h
 #include hostusb.h
+#include virnetdev.h
+
+VIR_ENUM_IMPL(virNetDevVPort, VIR_NETDEV_VPORT_PROFILE_LAST,
+  none,
+  802.1Qbg,
+  802.1Qbh,
+  openvswitch)
 
 static pciDeviceList *
 qemuGetPciHostDeviceList(virDomainHostdevDefPtr *hostdevs, int nhostdevs)
@@ -156,19 +163,179 @@ int qemuUpdateActivePciHostdevs(struct qemud_driver 
*driver,
 return 0;
 }
 
+static int
+qemuDomainHostdevPciSysfsPath(virDomainHostdevDefPtr hostdev, char 
**sysfs_path)
+{
+struct pci_config_address config_address;
+
+config_address.domain = hostdev-source.subsys.u.pci.domain;
+config_address.bus = hostdev-source.subsys.u.pci.bus;
+config_address.slot = hostdev-source.subsys.u.pci.slot;
+config_address.function = hostdev-source.subsys.u.pci.function;
+
+return pciConfigAddressToSysfsFile(config_address, sysfs_path);
+}
+
+int
+qemuDomainHostdevIsVirtualFunction(virDomainHostdevDefPtr hostdev)
+{
+char *sysfs_path = NULL;
+int ret = -1;
+
+if (qemuDomainHostdevPciSysfsPath(hostdev, sysfs_path)  0)
+return ret;
+
+ret = pciDeviceIsVirtualFunction(sysfs_path);
+
+VIR_FREE(sysfs_path);
+
+return ret;
+}
+
+static int
+qemuDomainHostdevNetDevice(virDomainHostdevDefPtr hostdev, char **linkdev,
+   int *vf)
+{
+int ret = -1;
+char *sysfs_path = NULL;
+
+if (qemuDomainHostdevPciSysfsPath(hostdev, sysfs_path)  0)
+return ret;
+
+if (pciDeviceIsVirtualFunction(sysfs_path) == 1) {
+if (pciDeviceGetVirtualFunctionInfo(sysfs_path, linkdev,
+vf)  0)
+goto cleanup;
+} else {
+if (pciDeviceNetName(sysfs_path, linkdev)  0)
+goto cleanup;
+*vf = -1;
+}
+
+ret = 0;
+
+cleanup:
+VIR_FREE(sysfs_path);
+
+return ret;
+}
+
+static int
+qemuDomainHostdevNetConfigVirtPortProfile(const char *linkdev, int vf,
+  virNetDevVPortProfilePtr virtPort,
+  const unsigned char *macaddr,
+  const unsigned char *uuid,
+  int associate)
+{
+int ret = -1;
+
+if (!virtPort)
+return ret;
+
+switch(virtPort-virtPortType) {
+case VIR_NETDEV_VPORT_PROFILE_NONE:
+case VIR_NETDEV_VPORT_PROFILE_OPENVSWITCH:
+case VIR_NETDEV_VPORT_PROFILE_8021QBG:
+case VIR_NETDEV_VPORT_PROFILE_LAST:
+qemuReportError(VIR_ERR_CONFIG_UNSUPPORTED, _(virtualport type %s is 
+currently not supported on interfaces of type 
+hostdev),
+virNetDevVPortTypeToString(virtPort-virtPortType));
+break;
+
+case VIR_NETDEV_VPORT_PROFILE_8021QBH:
+if (associate)
+ret = virNetDevVPortProfileAssociate(NULL, virtPort, macaddr,
+ linkdev, vf, uuid,
+ 
VIR_NETDEV_VPORT_PROFILE_OP_CREATE);
+else
+ret = virNetDevVPortProfileDisassociate(NULL, virtPort,
+macaddr, linkdev, vf,
+
VIR_NETDEV_VPORT_PROFILE_OP_DESTROY);
+break;
+}
+
+return ret;
+}
+
+int
+qemuDomainHostdevNetConfigReplace(virDomainHostdevDefPtr hostdev,
+  const unsigned char *uuid,
+  char *stateDir)
+{
+char *linkdev = NULL;
+virNetDevVPortProfilePtr virtPort;
+int ret = -1;
+int vf = -1;
+int vlanid = -1;
+int port_profile_associate = 1;
+
+if (qemuDomainHostdevNetDevice(hostdev, linkdev, vf)  0)
+return ret;
+
+virtPort = virDomainNetGetActualVirtPortProfile(
+ hostdev-parent.data.net);
+if (virtPort)
+ret = qemuDomainHostdevNetConfigVirtPortProfile(linkdev, vf,
+

[libvirt] [PATCH 4/4] qemu_hostdev: Add support to install port profile and mac address on hostdevs

2012-03-01 Thread Roopa Prabhu
From: Roopa Prabhu ropra...@cisco.com

These changes are applied only if the hostdev has a parent net device.
If the parent netdevice has virtual port information, the original virtualport
associate functions are called (these set and restore both mac and port profile
on an interface). Else, only mac address is set on the device
using other methods depending on if its a sriov device or not.

Changes also include hotplug pci devices

Signed-off-by: Roopa Prabhu ropra...@cisco.com
---
 src/qemu/qemu_hostdev.c |  229 +--
 src/qemu/qemu_hostdev.h |8 +-
 src/qemu/qemu_hotplug.c |   19 
 3 files changed, 242 insertions(+), 14 deletions(-)


diff --git a/src/qemu/qemu_hostdev.c b/src/qemu/qemu_hostdev.c
index b3cad8e..43d9dd7 100644
--- a/src/qemu/qemu_hostdev.c
+++ b/src/qemu/qemu_hostdev.c
@@ -29,6 +29,7 @@
 #include memory.h
 #include pci.h
 #include hostusb.h
+#include virnetdev.h
 
 static pciDeviceList *
 qemuGetPciHostDeviceList(virDomainHostdevDefPtr *hostdevs, int nhostdevs)
@@ -156,12 +157,131 @@ int qemuUpdateActivePciHostdevs(struct qemud_driver 
*driver,
 return 0;
 }
 
+static int
+qemuDomainHostdevPciSysfsPath(virDomainHostdevDefPtr hostdev, char 
**sysfs_path)
+{
+struct pci_config_address config_address;
+
+config_address.domain = hostdev-source.subsys.u.pci.domain;
+config_address.bus = hostdev-source.subsys.u.pci.bus;
+config_address.slot = hostdev-source.subsys.u.pci.slot;
+config_address.function = hostdev-source.subsys.u.pci.function;
+
+return pciConfigAddressToSysfsFile(config_address, sysfs_path);
+}
+
+int
+qemuDomainHostdevIsVirtualFunction(virDomainHostdevDefPtr hostdev)
+{
+char *sysfs_path = NULL;
+int ret = -1;
+
+if (qemuDomainHostdevPciSysfsPath(hostdev, sysfs_path)  0)
+return ret;
+
+ret = pciDeviceIsVirtualFunction(sysfs_path);
+
+VIR_FREE(sysfs_path);
+
+return ret;
+}
+
+static int
+qemuDomainHostdevNetDevice(virDomainHostdevDefPtr hostdev, char **linkdev,
+   int *vf)
+{
+int ret = -1;
+char *sysfs_path = NULL;
+
+if (qemuDomainHostdevPciSysfsPath(hostdev, sysfs_path)  0)
+return ret;
+
+if (pciDeviceIsVirtualFunction(sysfs_path) == 1) {
+if (pciDeviceGetVirtualFunctionInfo(sysfs_path, linkdev,
+vf)  0)
+goto cleanup;
+}
+else {
+if (pciDeviceNetName(sysfs_path, linkdev)  0)
+goto cleanup;
+*vf = -1;
+}
+
+ret = 0;
+
+cleanup:
+VIR_FREE(sysfs_path);
+
+return ret;
+}
+
+int
+qemuDomainHostdevNetConfigReplace(virDomainHostdevDefPtr hostdev,
+  const unsigned char *uuid,
+  char *stateDir)
+{
+char *linkdev = NULL;
+virNetDevVPortProfilePtr virtPortProfile;
+int ret = -1;
+int vf = -1;
+int vlanid = -1;
+
+if (qemuDomainHostdevNetDevice(hostdev, linkdev, vf)  0)
+return ret;
+
+virtPortProfile = virDomainNetGetActualVirtPortProfile(
+ hostdev-parent.data.net);
+if (virtPortProfile)
+ret = virNetDevVPortProfileAssociate(NULL, virtPortProfile,
+ hostdev-parent.data.net-mac,
+ linkdev, vf, uuid,
+ 
VIR_NETDEV_VPORT_PROFILE_OP_CREATE);
+else
+/* Set only mac */
+ret = virNetDevReplaceNetConfig(linkdev, vf,
+hostdev-parent.data.net-mac, vlanid,
+stateDir);
+
+VIR_FREE(linkdev);
+
+return ret;
+}
+
+int
+qemuDomainHostdevNetConfigRestore(virDomainHostdevDefPtr hostdev,
+  char *stateDir)
+{
+char *linkdev = NULL;
+virNetDevVPortProfilePtr virtPortProfile;
+int ret = -1;
+int vf = -1;
+
+if (qemuDomainHostdevNetDevice(hostdev, linkdev, vf)  0)
+return ret;
+
+virtPortProfile = virDomainNetGetActualVirtPortProfile(
+ hostdev-parent.data.net);
+if (virtPortProfile)
+ret = virNetDevVPortProfileDisassociate(NULL, virtPortProfile,
+hostdev-parent.data.net-mac,
+linkdev, vf,
+
VIR_NETDEV_VPORT_PROFILE_OP_DESTROY);
+else
+ret = virNetDevRestoreNetConfig(linkdev, vf, stateDir);
+
+VIR_FREE(linkdev);
+
+return ret;
+}
+
 int qemuPrepareHostdevPCIDevices(struct qemud_driver *driver,
  const char *name,
+ const unsigned char *uuid,
  virDomainHostdevDefPtr *hostdevs,
  int nhostdevs)
 {
 pciDeviceList *pcidevs;
+int last_processed_hostdev = -1,