[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

Re: [libvirt] [PATCH 10/14] Basic qemu NIC hotplug support



On Mon, Jul 20, 2009 at 12:51:20PM +0100, Mark McLoughlin wrote:
> Implement basic NIC hotplug support using the 'host_net_add' and
> 'pci_add' qemu monitor commands.
> 
> For now, we don't support 'bridge' or 'network' types.
> 
> Also, if pci_add fails, we currently fail to remove the backend
> which we added.

Urgh, that rather sucks. I guess that is a current QEMU limitation ? 

> diff --git a/src/qemu_driver.c b/src/qemu_driver.c
> index cbc185c..cde789e 100644
> --- a/src/qemu_driver.c
> +++ b/src/qemu_driver.c
> @@ -4492,6 +4492,84 @@ static int qemudDomainAttachUsbMassstorageDevice(virConnectPtr conn,
>      return 0;
>  }
>  
> +static int qemudDomainAttachNetDevice(virConnectPtr conn,
> +                                      virDomainObjPtr vm,
> +                                      virDomainDeviceDefPtr dev,
> +                                      unsigned int qemuCmdFlags)
> +{
> +    virDomainNetDefPtr net = dev->data.net;
> +    char *cmd, *reply;
> +    int i;
> +
> +    if (!(qemuCmdFlags & QEMUD_CMD_FLAG_HOST_NET_ADD)) {
> +        qemudReportError(conn, dom, NULL, VIR_ERR_NO_SUPPORT, _("%s"),
> +                         "installed qemu version does not support host_net_add");
> +        return -1;
> +    }
> +
> +    if (net->type == VIR_DOMAIN_NET_TYPE_BRIDGE ||
> +        net->type == VIR_DOMAIN_NET_TYPE_NETWORK) {
> +        qemudReportError(conn, dom, NULL, VIR_ERR_NO_SUPPORT,
> +                         _("network device type '%s' cannot be attached"),
> +                         virDomainNetTypeToString(net->type));
> +        return -1;
> +    }
> +
> +    if (VIR_REALLOC_N(vm->def->nets, vm->def->nnets+1) < 0) {
> +        virReportOOMError(conn);
> +        return -1;
> +    }
> +
> +    if ((qemuCmdFlags & QEMUD_CMD_FLAG_NET_NAME) &&
> +        qemuAssignNetNames(vm->def, net) < 0) {
> +        virReportOOMError(conn);
> +        return -1;
> +    }
> +
> +    /* Choose a vlan value greater than all other values since
> +     * older versions did not store the value in the state file.
> +     */
> +    net->vlan = vm->def->nnets;
> +    for (i = 0; i < vm->def->nnets; i++)
> +        if (vm->def->nets[i]->vlan >= net->vlan)
> +            net->vlan = vm->def->nets[i]->vlan;
> +
> +    if (qemuBuildHostNetStr(conn, net,
> +                            "host_net_add ", ' ', net->vlan, -1, &cmd) < 0)
> +        return -1;
> +
> +    if (qemudMonitorCommand(vm, cmd, &reply) < 0) {
> +        qemudReportError(conn, dom, NULL, VIR_ERR_OPERATION_FAILED,
> +                         _("failed to add network backend with '%s'"), cmd);
> +        VIR_FREE(cmd);
> +        return -1;
> +    }
> +
> +    VIR_FREE(reply);
> +    VIR_FREE(cmd);
> +
> +    if (qemuBuildNicStr(conn, net,
> +                        "pci_add pci_addr=auto ", ' ', net->vlan, &cmd) < 0) {
> +        /* FIXME: try and remove the backend again */
> +        return -1;
> +    }
> +
> +    if (qemudMonitorCommand(vm, cmd, &reply) < 0) {
> +        /* FIXME: try and remove the backend again */
> +        qemudReportError(conn, dom, NULL, VIR_ERR_OPERATION_FAILED,
> +                         _("failed to add NIC with '%s'"), cmd);
> +        VIR_FREE(cmd);
> +        return -1;
> +    }
> +
> +    VIR_FREE(reply);
> +    VIR_FREE(cmd);
> +
> +    vm->def->nets[vm->def->nnets++] = net;
> +
> +    return 0;
> +}

I'd recommend adding a nice VIR_DEBUG() line in after each command, including
the reply string, so if something goes wrong we can debug it more easily.

Regards,
Daniel
-- 
|: Red Hat, Engineering, London   -o-   http://people.redhat.com/berrange/ :|
|: http://libvirt.org  -o-  http://virt-manager.org  -o-  http://ovirt.org :|
|: http://autobuild.org       -o-         http://search.cpan.org/~danberr/ :|
|: GnuPG: 7D3B9505  -o-  F3C9 553F A1DA 4AC2 5648 23C1 B3DF F742 7D3B 9505 :|


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]