From 44304c6eb739c81828a70224925c7fe0e8be6694 Mon Sep 17 00:00:00 2001
From: Chunyan Liu <cyliu@suse.com>
Date: Wed, 15 Jun 2016 14:00:10 +0800
Subject: [PATCH 3/6] libxl: support usb controller hotplug

Support USB controller hot-plug and hot-unplug.

 #virsh attach-device dom usbctrl.xml
 #virsh detach-device dom usbctrl.xml
 usbctrl.xml example:
 <controller type='usb' index='0' model='qusb2'>

Signed-off-by: Chunyan Liu <cyliu@suse.com>
---
 src/libxl/libxl_driver.c | 144 +++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 144 insertions(+)

Index: libvirt-2.0.0/src/libxl/libxl_driver.c
===================================================================
--- libvirt-2.0.0.orig/src/libxl/libxl_driver.c
+++ libvirt-2.0.0/src/libxl/libxl_driver.c
@@ -3006,6 +3006,60 @@ libxlDomainAttachHostPCIDevice(libxlDriv
 
 #ifdef LIBXL_HAVE_PVUSB
 static int
+libxlDomainAttachControllerDevice(libxlDriverPrivatePtr driver,
+                                  virDomainObjPtr vm,
+                                  virDomainControllerDefPtr controller)
+{
+    libxlDriverConfigPtr cfg = libxlDriverConfigGet(driver);
+    const char *type = virDomainControllerTypeToString(controller->type);
+    libxl_device_usbctrl usbctrl;
+    int ret = -1;
+
+    libxl_device_usbctrl_init(&usbctrl);
+
+    if (controller->type != VIR_DOMAIN_CONTROLLER_TYPE_USB) {
+        virReportError(VIR_ERR_OPERATION_UNSUPPORTED,
+                       _("'%s' controller cannot be hot plugged."),
+                       type);
+        goto cleanup;
+    }
+
+    if (controller->idx == -1)
+        controller->idx = virDomainControllerFindUnusedIndex(vm->def,
+                                                             controller->type);
+
+    if (controller->opts.usbopts.ports == -1)
+        controller->opts.usbopts.ports = 8;
+
+    if (virDomainControllerFind(vm->def, controller->type, controller->idx) >= 0) {
+        virReportError(VIR_ERR_OPERATION_FAILED,
+                       _("target %s:%d already exists"),
+                       type, controller->idx);
+        goto cleanup;
+    }
+
+    if (VIR_REALLOC_N(vm->def->controllers, vm->def->ncontrollers + 1) < 0)
+        goto cleanup;
+
+    if (libxlMakeUSBController(controller, &usbctrl) < 0)
+        goto cleanup;
+
+    if (libxl_device_usbctrl_add(cfg->ctx, vm->def->id, &usbctrl, 0) < 0) {
+        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
+                       _("libxenlight failed to attach USB controller"));
+        goto cleanup;
+    }
+
+    virDomainControllerInsertPreAlloced(vm->def, controller);
+    ret = 0;
+
+ cleanup:
+    virObjectUnref(cfg);
+    libxl_device_usbctrl_dispose(&usbctrl);
+    return ret;
+}
+
+static int
 libxlDomainAttachHostUSBDevice(libxlDriverPrivatePtr driver,
                                virDomainObjPtr vm,
                                virDomainHostdevDefPtr hostdev)
@@ -3240,6 +3294,12 @@ libxlDomainAttachDeviceLive(libxlDriverP
                 dev->data.disk = NULL;
             break;
 
+        case VIR_DOMAIN_DEVICE_CONTROLLER:
+            ret = libxlDomainAttachControllerDevice(driver, vm, dev->data.controller);
+            if (!ret)
+                dev->data.controller = NULL;
+            break;
+
         case VIR_DOMAIN_DEVICE_NET:
             ret = libxlDomainAttachNetDevice(driver, vm,
                                              dev->data.net);
@@ -3270,6 +3330,7 @@ libxlDomainAttachDeviceConfig(virDomainD
     virDomainDiskDefPtr disk;
     virDomainNetDefPtr net;
     virDomainHostdevDefPtr hostdev;
+    virDomainControllerDefPtr controller;
     virDomainHostdevDefPtr found;
     char mac[VIR_MAC_STRING_BUFLEN];
 
@@ -3287,6 +3348,21 @@ libxlDomainAttachDeviceConfig(virDomainD
             dev->data.disk = NULL;
             break;
 
+        case VIR_DOMAIN_DEVICE_CONTROLLER:
+            controller = dev->data.controller;
+            if (controller->idx != -1 &&
+                virDomainControllerFind(vmdef, controller->type,
+                                        controller->idx) >= 0) {
+                virReportError(VIR_ERR_OPERATION_INVALID, "%s",
+                               _("Target already exists"));
+                return -1;
+            }
+
+            if (virDomainControllerInsert(vmdef, controller) < 0)
+                return -1;
+            dev->data.controller = NULL;
+            break;
+
         case VIR_DOMAIN_DEVICE_NET:
             net = dev->data.net;
             if (virDomainHasNet(vmdef, net)) {
@@ -3425,6 +3501,57 @@ libxlDomainDetachHostPCIDevice(libxlDriv
 
 #ifdef LIBXL_HAVE_PVUSB
 static int
+libxlDomainDetachControllerDevice(libxlDriverPrivatePtr driver,
+                                  virDomainObjPtr vm,
+                                  virDomainDeviceDefPtr dev)
+{
+    int idx, ret = -1;
+    virDomainControllerDefPtr detach = NULL;
+    virDomainControllerDefPtr controller = dev->data.controller;
+    const char *type = virDomainControllerTypeToString(controller->type);
+    libxl_device_usbctrl usbctrl;
+    libxlDriverConfigPtr cfg = libxlDriverConfigGet(driver);
+
+    libxl_device_usbctrl_init(&usbctrl);
+
+    if (controller->type != VIR_DOMAIN_CONTROLLER_TYPE_USB) {
+        virReportError(VIR_ERR_OPERATION_UNSUPPORTED,
+                       _("'%s' controller cannot be hot plugged."),
+                       type);
+        goto cleanup;
+    }
+
+    if ((idx = virDomainControllerFind(vm->def,
+                                       controller->type,
+                                       controller->idx)) < 0) {
+        virReportError(VIR_ERR_OPERATION_FAILED,
+                       _("controller %s:%d not found"),
+                       type, controller->idx);
+        goto cleanup;
+    }
+
+    detach = vm->def->controllers[idx];
+
+    if (libxlMakeUSBController(controller, &usbctrl) < 0)
+        goto cleanup;
+
+    if (libxl_device_usbctrl_remove(cfg->ctx, vm->def->id, &usbctrl, 0) < 0) {
+        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
+                       _("libxenlight failed to detach USB controller"));
+        goto cleanup;
+    }
+
+    virDomainControllerRemove(vm->def, idx);
+    ret = 0;
+
+ cleanup:
+    virDomainControllerDefFree(detach);
+    virObjectUnref(cfg);
+    libxl_device_usbctrl_dispose(&usbctrl);
+    return ret;
+}
+
+static int
 libxlDomainDetachHostUSBDevice(libxlDriverPrivatePtr driver,
                                virDomainObjPtr vm,
                                virDomainHostdevDefPtr hostdev)
@@ -3588,6 +3715,10 @@ libxlDomainDetachDeviceLive(libxlDriverP
             ret = libxlDomainDetachDeviceDiskLive(vm, dev);
             break;
 
+        case VIR_DOMAIN_DEVICE_CONTROLLER:
+            ret = libxlDomainDetachControllerDevice(driver, vm, dev);
+            break;
+
         case VIR_DOMAIN_DEVICE_NET:
             ret = libxlDomainDetachNetDevice(driver, vm,
                                              dev->data.net);
@@ -3622,6 +3753,7 @@ libxlDomainDetachDeviceConfig(virDomainD
 {
     virDomainDiskDefPtr disk, detach;
     virDomainHostdevDefPtr hostdev, det_hostdev;
+    virDomainControllerDefPtr cont, det_cont;
     virDomainNetDefPtr net;
     int idx;
 
@@ -3636,6 +3768,18 @@ libxlDomainDetachDeviceConfig(virDomainD
             virDomainDiskDefFree(detach);
             break;
 
+        case VIR_DOMAIN_DEVICE_CONTROLLER:
+            cont = dev->data.controller;
+            if ((idx = virDomainControllerFind(vmdef, cont->type,
+                                               cont->idx)) < 0) {
+                virReportError(VIR_ERR_INVALID_ARG, "%s",
+                               _("device not present in domain configuration"));
+                return -1;
+            }
+            det_cont = virDomainControllerRemove(vmdef, idx);
+            virDomainControllerDefFree(det_cont);
+            break;
+
         case VIR_DOMAIN_DEVICE_NET:
             net = dev->data.net;
             if ((idx = virDomainNetFindIdx(vmdef, net)) < 0)
