Signed-off-by: Michal Privoznik <mprivozn@xxxxxxxxxx> --- src/libxl/libxl_driver.c | 22 +++++++++------------- src/lxc/lxc_driver.c | 22 +++++++++------------- src/openvz/openvz_driver.c | 22 +++++++++------------- src/qemu/qemu_driver.c | 11 +---------- src/test/test_driver.c | 28 ++++++---------------------- 5 files changed, 34 insertions(+), 71 deletions(-) diff --git a/src/libxl/libxl_driver.c b/src/libxl/libxl_driver.c index bf3625e34..a174d892e 100644 --- a/src/libxl/libxl_driver.c +++ b/src/libxl/libxl_driver.c @@ -4961,7 +4961,7 @@ libxlDomainInterfaceStats(virDomainPtr dom, { libxlDriverPrivatePtr driver = dom->conn->privateData; virDomainObjPtr vm; - size_t i; + virDomainNetDefPtr net = NULL; int ret = -1; if (!(vm = libxlDomObjFromDomain(dom))) @@ -4979,20 +4979,16 @@ libxlDomainInterfaceStats(virDomainPtr dom, goto endjob; } - /* Check the path is one of the domain's network interfaces. */ - for (i = 0; i < vm->def->nnets; i++) { - if (vm->def->nets[i]->ifname && - STREQ(vm->def->nets[i]->ifname, path)) { - ret = 0; - break; - } - } - - if (ret == 0) - ret = virNetDevTapInterfaceStats(path, stats); - else + if (!(net = virDomainNetFindByName(vm->def, path))) { virReportError(VIR_ERR_INVALID_ARG, _("'%s' is not a known interface"), path); + goto endjob; + } + + if (virNetDevTapInterfaceStats(path, stats) < 0) + goto endjob; + + ret = 0; endjob: libxlDomainObjEndJob(driver, vm); diff --git a/src/lxc/lxc_driver.c b/src/lxc/lxc_driver.c index 4d24d2870..c0ef0c210 100644 --- a/src/lxc/lxc_driver.c +++ b/src/lxc/lxc_driver.c @@ -2853,9 +2853,9 @@ lxcDomainInterfaceStats(virDomainPtr dom, virDomainInterfaceStatsPtr stats) { virDomainObjPtr vm; - size_t i; int ret = -1; virLXCDriverPtr driver = dom->conn->privateData; + virDomainNetDefPtr net = NULL; if (!(vm = lxcDomObjFromDomain(dom))) goto cleanup; @@ -2872,20 +2872,16 @@ lxcDomainInterfaceStats(virDomainPtr dom, goto endjob; } - /* Check the path is one of the domain's network interfaces. */ - for (i = 0; i < vm->def->nnets; i++) { - if (vm->def->nets[i]->ifname && - STREQ(vm->def->nets[i]->ifname, path)) { - ret = 0; - break; - } - } - - if (ret == 0) - ret = virNetDevTapInterfaceStats(path, stats); - else + if (!(net = virDomainNetFindByName(vm->def, path))) { virReportError(VIR_ERR_INVALID_ARG, _("Invalid path, '%s' is not a known interface"), path); + goto endjob; + } + + if (virNetDevTapInterfaceStats(path, stats) < 0) + goto endjob; + + ret = 0; endjob: virLXCDomainObjEndJob(driver, vm); diff --git a/src/openvz/openvz_driver.c b/src/openvz/openvz_driver.c index a1485fc88..3c24020ce 100644 --- a/src/openvz/openvz_driver.c +++ b/src/openvz/openvz_driver.c @@ -1985,7 +1985,7 @@ openvzDomainInterfaceStats(virDomainPtr dom, { struct openvz_driver *driver = dom->conn->privateData; virDomainObjPtr vm; - size_t i; + virDomainNetDefPtr net = NULL; int ret = -1; openvzDriverLock(driver); @@ -2006,20 +2006,16 @@ openvzDomainInterfaceStats(virDomainPtr dom, goto cleanup; } - /* Check the path is one of the domain's network interfaces. */ - for (i = 0; i < vm->def->nnets; i++) { - if (vm->def->nets[i]->ifname && - STREQ(vm->def->nets[i]->ifname, path)) { - ret = 0; - break; - } - } - - if (ret == 0) - ret = virNetDevTapInterfaceStats(path, stats); - else + if (!(net = virDomainNetFindByName(vm->def, path))) { virReportError(VIR_ERR_INVALID_ARG, _("invalid path, '%s' is not a known interface"), path); + goto cleanup; + } + + if (virNetDevTapInterfaceStats(path, stats) < 0) + goto cleanup; + + ret = 0; cleanup: if (vm) diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c index 2c8ea19e3..1ab16e57c 100644 --- a/src/qemu/qemu_driver.c +++ b/src/qemu/qemu_driver.c @@ -11016,7 +11016,6 @@ qemuDomainInterfaceStats(virDomainPtr dom, { virDomainObjPtr vm; virDomainNetDefPtr net = NULL; - size_t i; int ret = -1; if (!(vm = qemuDomObjFromDomain(dom))) @@ -11031,15 +11030,7 @@ qemuDomainInterfaceStats(virDomainPtr dom, goto cleanup; } - /* Check the path is one of the domain's network interfaces. */ - for (i = 0; i < vm->def->nnets; i++) { - if (STREQ_NULLABLE(vm->def->nets[i]->ifname, path)) { - net = vm->def->nets[i]; - break; - } - } - - if (!net) { + if (!(net = virDomainNetFindByName(vm->def, path))) { virReportError(VIR_ERR_INVALID_ARG, _("invalid path, '%s' is not a known interface"), path); goto cleanup; diff --git a/src/test/test_driver.c b/src/test/test_driver.c index 9b434e9a0..e92768a97 100644 --- a/src/test/test_driver.c +++ b/src/test/test_driver.c @@ -593,25 +593,16 @@ testDomainGenerateIfname(virDomainDefPtr domdef) { int maxif = 1024; int ifctr; - size_t i; for (ifctr = 0; ifctr < maxif; ++ifctr) { + virDomainNetDefPtr net = NULL; char *ifname; - int found = 0; if (virAsprintf(&ifname, "testnet%d", ifctr) < 0) return NULL; /* Generate network interface names */ - for (i = 0; i < domdef->nnets; i++) { - if (domdef->nets[i]->ifname && - STREQ(domdef->nets[i]->ifname, ifname)) { - found = 1; - break; - } - } - - if (!found) + if (!(net = virDomainNetFindByName(domdef, ifname))) return ifname; VIR_FREE(ifname); } @@ -3176,8 +3167,9 @@ static int testDomainInterfaceStats(virDomainPtr domain, virDomainObjPtr privdom; struct timeval tv; unsigned long long statbase; - size_t i; - int found = 0, ret = -1; + virDomainNetDefPtr net = NULL; + int ret = -1; + if (!(privdom = testDomObjFromDomain(domain))) return -1; @@ -3188,15 +3180,7 @@ static int testDomainInterfaceStats(virDomainPtr domain, goto error; } - for (i = 0; i < privdom->def->nnets; i++) { - if (privdom->def->nets[i]->ifname && - STREQ(privdom->def->nets[i]->ifname, path)) { - found = 1; - break; - } - } - - if (!found) { + if (!(net = virDomainNetFindByName(privdom->def, path))) { virReportError(VIR_ERR_INVALID_ARG, _("invalid path, '%s' is not a known interface"), path); goto error; -- 2.13.5 -- libvir-list mailing list libvir-list@xxxxxxxxxx https://www.redhat.com/mailman/listinfo/libvir-list