diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c index c290baf953c95eb5a5843ccaca64cdf45ef77c23..7d83a3d1c639852da59cd54d989c562b1c44bc65 100644 --- a/src/conf/domain_conf.c +++ b/src/conf/domain_conf.c @@ -17169,6 +17169,47 @@ virDomainNetRemove(virDomainDefPtr def, size_t i) return net; } + +int +virDomainNetUpdate(virDomainDefPtr def, + size_t netidx, + virDomainNetDefPtr newnet) +{ + size_t hostdevidx; + virDomainNetDefPtr oldnet = def->nets[netidx]; + virDomainHostdevDefPtr oldhostdev = virDomainNetGetActualHostdev(oldnet); + virDomainHostdevDefPtr newhostdev = virDomainNetGetActualHostdev(newnet); + + /* + * if newnet or oldnet has a valid hostdev*, we need to update the + * hostdevs list + */ + if (oldhostdev) { + for (hostdevidx = 0; hostdevidx < def->nhostdevs; hostdevidx++) { + if (def->hostdevs[hostdevidx] == oldhostdev) + break; + } + } + + if (oldhostdev && hostdevidx < def->nhostdevs) { + if (newhostdev) { + /* update existing entry in def->hostdevs */ + def->hostdevs[hostdevidx] = newhostdev; + } else { + /* delete oldhostdev from def->hostdevs */ + virDomainHostdevRemove(def, hostdevidx); + } + } else if (newhostdev) { + /* add newhostdev to end of def->hostdevs */ + if (VIR_APPEND_ELEMENT(def->hostdevs, def->nhostdevs, newhostdev) < 0) + return -1; + } + + def->nets[netidx] = newnet; + return 0; +} + + int virDomainControllerInsert(virDomainDefPtr def, virDomainControllerDefPtr controller) { diff --git a/src/conf/domain_conf.h b/src/conf/domain_conf.h index 53bdee22fb1b3c5abc07e36df31478a9ed53147e..2884af49d8c31be3bb6d0f39be943953e28679f7 100644 --- a/src/conf/domain_conf.h +++ b/src/conf/domain_conf.h @@ -3174,6 +3174,7 @@ virDomainNetDefPtr virDomainNetFind(virDomainDefPtr def, const char *device); virDomainNetDefPtr virDomainNetFindByName(virDomainDefPtr def, const char *ifname); bool virDomainHasNet(virDomainDefPtr def, virDomainNetDefPtr net); int virDomainNetInsert(virDomainDefPtr def, virDomainNetDefPtr net); +int virDomainNetUpdate(virDomainDefPtr def, size_t netidx, virDomainNetDefPtr newnet); virDomainNetDefPtr virDomainNetRemove(virDomainDefPtr def, size_t i); void virDomainNetRemoveHostdev(virDomainDefPtr def, virDomainNetDefPtr net); diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index 287e63bffa58eeb22a9d02b054b0a0c3b0ce647e..2b10a1030e7413ed267d630cd48c06290e8ae298 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -508,6 +508,7 @@ virDomainNetSetModelString; virDomainNetTypeFromString; virDomainNetTypeSharesHostView; virDomainNetTypeToString; +virDomainNetUpdate; virDomainNostateReasonTypeFromString; virDomainNostateReasonTypeToString; virDomainObjAssignDef; diff --git a/src/lxc/lxc_driver.c b/src/lxc/lxc_driver.c index 5453f49064c8e981e22a82b9d732f8c9e62f68e5..cf1dd1428e48035bb0a67043b109bdc3048a71f5 100644 --- a/src/lxc/lxc_driver.c +++ b/src/lxc/lxc_driver.c @@ -3486,8 +3486,10 @@ lxcDomainUpdateDeviceConfig(virDomainDefPtr vmdef, false) < 0) return -1; - virDomainNetDefFree(vmdef->nets[idx]); - vmdef->nets[idx] = net; + if (virDomainNetUpdate(vmdef, idx, net) < 0) + return -1; + + virDomainNetDefFree(oldDev.data.net); dev->data.net = NULL; ret = 0; diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c index 0c65414a1a3cdb7312e1a9e2ee2662196333b250..286f5bd9846931425bae8447480a45580ae34326 100644 --- a/src/qemu/qemu_driver.c +++ b/src/qemu/qemu_driver.c @@ -8786,8 +8786,10 @@ qemuDomainUpdateDeviceConfig(virDomainDefPtr vmdef, false) < 0) return -1; - virDomainNetDefFree(vmdef->nets[pos]); - vmdef->nets[pos] = net; + if (virDomainNetUpdate(vmdef, pos, net)) + return -1; + + virDomainNetDefFree(oldDev.data.net); dev->data.net = NULL; break;