From 30605477f2ade95f2eedacbf193cd0a003a67613 Mon Sep 17 00:00:00 2001 From: Mark McLoughlin Date: Fri, 17 Jul 2009 22:08:34 +0100 Subject: [PATCH] Store the interface vlan number in the domain state Currently, an interface's vlan number corresponds to its index in the table of network interfaces. That is no longer true when we allow devices to be removed. To fix this, we store the vlan number in the domain's state XML so that it survives libvirtd restarts. * src/domain_conf.h: add vlan number to virDomainNetDef * src/domain_conf.c: store it in XML as , defaulting to -1 if this is state saved by a previous version of libvirt * src/qemu_conf.c: assign vlan numbers before starting qemu --- src/domain_conf.c | 12 ++++++++++++ src/domain_conf.h | 1 + src/qemu_conf.c | 7 +++++-- 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/src/domain_conf.c b/src/domain_conf.c index 2750c88a25..0fd6cc8e75 100644 --- a/src/domain_conf.c +++ b/src/domain_conf.c @@ -962,6 +962,7 @@ virDomainNetDefParseXML(virConnectPtr conn, char *internal = NULL; char *nic_name = NULL; char *hostnet_name = NULL; + char *vlan = NULL; if (VIR_ALLOC(def) < 0) { virReportOOMError(conn); @@ -1031,6 +1032,7 @@ virDomainNetDefParseXML(virConnectPtr conn, xmlStrEqual(cur->name, BAD_CAST "state")) { nic_name = virXMLPropString(cur, "nic"); hostnet_name = virXMLPropString(cur, "hostnet"); + vlan = virXMLPropString(cur, "vlan"); } } cur = cur->next; @@ -1046,6 +1048,13 @@ virDomainNetDefParseXML(virConnectPtr conn, def->hostnet_name = hostnet_name; nic_name = hostnet_name = NULL; + def->vlan = -1; + if (vlan && virStrToLong_i(vlan, NULL, 10, &def->vlan) < 0) { + virDomainReportError(conn, VIR_ERR_INTERNAL_ERROR, "%s", + _("Cannot parse 'vlan' attribute")); + goto error; + } + switch (def->type) { case VIR_DOMAIN_NET_TYPE_NETWORK: if (network == NULL) { @@ -1167,6 +1176,7 @@ cleanup: VIR_FREE(internal); VIR_FREE(nic_name); VIR_FREE(hostnet_name); + VIR_FREE(vlan); return def; @@ -3624,6 +3634,8 @@ virDomainNetDefFormat(virConnectPtr conn, virBufferEscapeString(buf, " nic='%s'", def->nic_name); if (def->hostnet_name) virBufferEscapeString(buf, " hostnet='%s'", def->hostnet_name); + if (def->vlan > 0) + virBufferVSprintf(buf, " vlan='%d'", def->vlan); virBufferAddLit(buf, "/>\n"); } diff --git a/src/domain_conf.h b/src/domain_conf.h index a274efe17e..2836b013f1 100644 --- a/src/domain_conf.h +++ b/src/domain_conf.h @@ -192,6 +192,7 @@ struct _virDomainNetDef { char *ifname; char *nic_name; char *hostnet_name; + int vlan; }; enum virDomainChrSrcType { diff --git a/src/qemu_conf.c b/src/qemu_conf.c index 0362d765df..6d876cb5a3 100644 --- a/src/qemu_conf.c +++ b/src/qemu_conf.c @@ -1535,11 +1535,13 @@ int qemudBuildCommandLine(virConnectPtr conn, char *nic, *host; int tapfd = -1; + net->vlan = i; + if ((qemuCmdFlags & QEMUD_CMD_FLAG_NET_NAME) && qemuAssignNetNames(def, net) < 0) goto no_memory; - if (qemuBuildNicStr(conn, net, NULL, ',', i, &nic) < 0) + if (qemuBuildNicStr(conn, net, NULL, ',', net->vlan, &nic) < 0) goto error; ADD_ARG_LIT("-net"); @@ -1565,7 +1567,8 @@ int qemudBuildCommandLine(virConnectPtr conn, (*tapfds)[(*ntapfds)++] = tapfd; } - if (qemuBuildHostNetStr(conn, net, NULL, ',', i, tapfd, &host) < 0) + if (qemuBuildHostNetStr(conn, net, NULL, ',', + net->vlan, tapfd, &host) < 0) goto error; ADD_ARG_LIT("-net");