On Wed, 18 May 2011 15:41:31 +0800
Wen Congyang <wency(a)cn.fujitsu.com> wrote:
At 05/18/2011 02:13 PM, KAMEZAWA Hiroyuki Write:
>>From 226239905ec9b01fd0bd0ce050de97d7f0d9e19c Mon Sep 17 00:00:00 2001
> From: KAMEZAWA Hiroyuki <kamezawa.hiroyu(a)jp.fujitsu.com>
> Date: Wed, 18 May 2011 15:14:28 +0900
> Subject: [PATCH] qemu - support persistent add/delete network interface
>
> This patch allows to modify interfaces of domain(qemu).
>
> Signed-off-by: KAMEZAWA Hiroyuki <kamezawa.hiroyu(a)jp.fujitsu.com>
>
> * src/conf/domain_conf.c:
> (virDomainNetInsert) : Insert a network device to domain definition.
> (virDomainNetIndexByMac/Ifname) : Returns an index of net device in array.
> (virDomainNetRemoveByMac): Remove a NIC of passed MAC address.
>
> * src/qemu/qemu_driver.c
> (qemuDomainAttachDeviceConfig): add codes for NIC.
> (qemuDomainDetachDeviceConfig): add codes for NIC.
> ---
> src/conf/domain_conf.c | 55 ++++++++++++++++++++++++++++++++++++++++++++++
> src/conf/domain_conf.h | 5 ++++
> src/libvirt_private.syms | 4 +++
> src/qemu/qemu_driver.c | 32 ++++++++++++++++++++++++++
> 4 files changed, 96 insertions(+), 0 deletions(-)
>
> diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c
> index 498438a..de9abe9 100644
> --- a/src/conf/domain_conf.c
> +++ b/src/conf/domain_conf.c
> @@ -5163,6 +5163,61 @@ int virDomainDiskRemoveByName(virDomainDefPtr def, const char
*name)
> return 0;
> }
>
> +int virDomainNetInsert(virDomainDefPtr def, virDomainNetDefPtr net)
> +{
> + if (VIR_REALLOC_N(def->nets, def->nnets) < 0)
s/def->nnets/def->nnets+1/
Ah, yes. My test worked just in lucky.
> + case VIR_DOMAIN_DEVICE_NET:
> + net =
dev->data.net;
> + if (net->ifname && virDomainNetIndexByIfname(vmdef,
net->ifname)) {
If the interface already exists, the return value of virDomainNetIndexByIfname()
is great or equal than 0, not a boolean.
I think you mean 'virDomainNetIndexByIfname(vmdef, net->ifname) >= 0'
yes.
> + qemuReportError(VIR_ERR_INVALID_ARG,
> + _("iface %s already exists"),
net->ifname);
> + return -1;
> + }
> + if (virDomainNetIndexByMac(vmdef, net->mac) >= 0) {
> + char macbuf[VIR_MAC_STRING_BUFLEN];
> + virFormatMacAddr(net->mac, macbuf);
> + qemuReportError(VIR_ERR_INVALID_ARG,
> + _("mac %s already exists"), macbuf);
> + return -1;
> + }
> + if (virDomainNetInsert(vmdef, net)) {
> + virReportOOMError();
> + return -1;
> + }
> +
dev->data.net = NULL;
> + if (qemuDomainAssignPCIAddresses(vmdef) < 0)
> + return -1;
> + break;
> default:
> qemuReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
> _("persistent attach of device is not
supported"));
> @@ -4281,6 +4304,7 @@ qemuDomainDetachDeviceConfig(virDomainDefPtr vmdef,
> virDomainDeviceDefPtr dev)
> {
> virDomainDiskDefPtr disk;
> + virDomainNetDefPtr net;
>
> switch (dev->type) {
> case VIR_DOMAIN_DEVICE_DISK:
> @@ -4291,6 +4315,14 @@ qemuDomainDetachDeviceConfig(virDomainDefPtr vmdef,
> return -1;
> }
> break;
> + case VIR_DOMAIN_DEVICE_NET:
> + net =
dev->data.net;
> + if (virDomainNetRemoveByMac(vmdef, net->mac)) {
> + qemuReportError(VIR_ERR_INVALID_ARG,
> + _("no target device %s"), disk->dst);
If disk->dst is correct, but net->mac is wrong, this error message
may confuse the user.
I'm very shamed :(. I will fix and post v2.
Thanks,
-Kame