[libvirt] [PATCH 2/4] virDomainInterfaceStats: Accept MAC address too

John Ferlan jferlan at redhat.com
Fri Oct 13 17:45:26 UTC 2017



On 10/05/2017 10:18 AM, Michal Privoznik wrote:
> https://bugzilla.redhat.com/show_bug.cgi?id=1497396
> 
> The other APIs accept both, ifname and MAC address. There's no
> reason virDomainInterfaceStats can't do the same.
> 
> Signed-off-by: Michal Privoznik <mprivozn at redhat.com>
> ---
>  include/libvirt/libvirt-domain.h |  2 +-
>  src/driver-hypervisor.h          |  2 +-
>  src/libvirt-domain.c             | 15 ++++++++-------
>  src/libxl/libxl_driver.c         |  8 ++++----
>  src/lxc/lxc_driver.c             |  8 ++++----
>  src/openvz/openvz_driver.c       |  8 ++++----
>  src/qemu/qemu_driver.c           | 10 +++++-----
>  src/remote/remote_protocol.x     |  2 +-
>  src/remote_protocol-structs      |  2 +-
>  src/test/test_driver.c           | 11 ++++++-----
>  src/vz/vz_driver.c               |  4 ++--
>  src/vz/vz_sdk.c                  |  9 +++++++--
>  src/xen/xen_driver.c             | 11 +++++++++--
>  tools/virsh.pod                  |  3 ++-
>  14 files changed, 55 insertions(+), 40 deletions(-)
> 

Order-wise - I think patch 4 should go after patch 1. That way this
patch doesn't need all those error messages adjusted - they can just be
removed when calling virDomainNetFind since it would already provide the
message.


> diff --git a/include/libvirt/libvirt-domain.h b/include/libvirt/libvirt-domain.h
> index 030a62c43..ebf47a9bb 100644
> --- a/include/libvirt/libvirt-domain.h
> +++ b/include/libvirt/libvirt-domain.h
> @@ -1571,7 +1571,7 @@ int                     virDomainBlockStatsFlags (virDomainPtr dom,
>                                                    int *nparams,
>                                                    unsigned int flags);
>  int                     virDomainInterfaceStats (virDomainPtr dom,
> -                                                 const char *path,
> +                                                 const char *device,
>                                                   virDomainInterfaceStatsPtr stats,
>                                                   size_t size);
>  
> diff --git a/src/driver-hypervisor.h b/src/driver-hypervisor.h
> index 6c3f7d795..4de0581c3 100644
> --- a/src/driver-hypervisor.h
> +++ b/src/driver-hypervisor.h
> @@ -486,7 +486,7 @@ typedef int
>  
>  typedef int
>  (*virDrvDomainInterfaceStats)(virDomainPtr domain,
> -                              const char *path,
> +                              const char *device,
>                                virDomainInterfaceStatsPtr stats);
>  
>  typedef int
> diff --git a/src/libvirt-domain.c b/src/libvirt-domain.c
> index d2d022a66..34a91d683 100644
> --- a/src/libvirt-domain.c
> +++ b/src/libvirt-domain.c
> @@ -5507,14 +5507,15 @@ virDomainBlockStatsFlags(virDomainPtr dom,
>  /**
>   * virDomainInterfaceStats:
>   * @dom: pointer to the domain object
> - * @path: path to the interface
> + * @device: the interface name or MAC address
>   * @stats: network interface stats (returned)
>   * @size: size of stats structure
>   *
>   * This function returns network interface stats for interfaces
>   * attached to the domain.
>   *
> - * The path parameter is the name of the network interface.
> + * The @device parameter is the name of the network interface or
> + * its MAC address.

The @device parameter is the network interface either by name or MAC
addresss.

>   *
>   * Domains may have more than one network interface.  To get stats for
>   * each you should make multiple calls to this function.
> @@ -5528,20 +5529,20 @@ virDomainBlockStatsFlags(virDomainPtr dom,
>   * Returns: 0 in case of success or -1 in case of failure.
>   */
>  int
> -virDomainInterfaceStats(virDomainPtr dom, const char *path,
> +virDomainInterfaceStats(virDomainPtr dom, const char *device,
>                          virDomainInterfaceStatsPtr stats, size_t size)
>  {
>      virConnectPtr conn;
>      virDomainInterfaceStatsStruct stats2 = { -1, -1, -1, -1,
>                                               -1, -1, -1, -1 };
>  
> -    VIR_DOMAIN_DEBUG(dom, "path=%s, stats=%p, size=%zi",
> -                     path, stats, size);
> +    VIR_DOMAIN_DEBUG(dom, "device=%s, stats=%p, size=%zi",
> +                     device, stats, size);
>  
>      virResetLastError();
>  
>      virCheckDomainReturn(dom, -1);
> -    virCheckNonNullArgGoto(path, error);
> +    virCheckNonNullArgGoto(device, error);
>      virCheckNonNullArgGoto(stats, error);
>      if (size > sizeof(stats2)) {
>          virReportInvalidArg(size,
> @@ -5553,7 +5554,7 @@ virDomainInterfaceStats(virDomainPtr dom, const char *path,
>      conn = dom->conn;
>  
>      if (conn->driver->domainInterfaceStats) {
> -        if (conn->driver->domainInterfaceStats(dom, path, &stats2) == -1)
> +        if (conn->driver->domainInterfaceStats(dom, device, &stats2) == -1)
>              goto error;
>  
>          memcpy(stats, &stats2, size);
> diff --git a/src/libxl/libxl_driver.c b/src/libxl/libxl_driver.c
> index 8483d6ecf..9db6f3503 100644
> --- a/src/libxl/libxl_driver.c
> +++ b/src/libxl/libxl_driver.c
> @@ -4956,7 +4956,7 @@ libxlDomainIsUpdated(virDomainPtr dom)
>  
>  static int
>  libxlDomainInterfaceStats(virDomainPtr dom,
> -                          const char *path,
> +                          const char *device,
>                            virDomainInterfaceStatsPtr stats)
>  {
>      libxlDriverPrivatePtr driver = dom->conn->privateData;
> @@ -4979,13 +4979,13 @@ libxlDomainInterfaceStats(virDomainPtr dom,
>          goto endjob;
>      }
>  
> -    if (!(net = virDomainNetFindByName(vm->def, path))) {
> +    if (!(net = virDomainNetFind(vm->def, device))) {
>          virReportError(VIR_ERR_INVALID_ARG,
> -                       _("'%s' is not a known interface"), path);
> +                       _("'%s' is not a known interface"), device);

If you adjust the order of patches, then message just gets deleted.

>          goto endjob;
>      }
>  
> -    if (virNetDevTapInterfaceStats(path, stats,
> +    if (virNetDevTapInterfaceStats(device, stats,
>                                     !virDomainNetTypeSharesHostView(net)) < 0)
>          goto endjob;
>  
> diff --git a/src/lxc/lxc_driver.c b/src/lxc/lxc_driver.c
> index 6ad61bdb7..4ab05a7ff 100644
> --- a/src/lxc/lxc_driver.c
> +++ b/src/lxc/lxc_driver.c
> @@ -2849,7 +2849,7 @@ lxcDomainGetBlkioParameters(virDomainPtr dom,
>  
>  static int
>  lxcDomainInterfaceStats(virDomainPtr dom,
> -                        const char *path,
> +                        const char *device,
>                          virDomainInterfaceStatsPtr stats)
>  {
>      virDomainObjPtr vm;
> @@ -2872,13 +2872,13 @@ lxcDomainInterfaceStats(virDomainPtr dom,
>          goto endjob;
>      }
>  
> -    if (!(net = virDomainNetFindByName(vm->def, path))) {
> +    if (!(net = virDomainNetFind(vm->def, device))) {
>          virReportError(VIR_ERR_INVALID_ARG,
> -                       _("Invalid path, '%s' is not a known interface"), path);
> +                       _("'%s' is not a known interface"), device);

Same...

>          goto endjob;
>      }
>  
> -    if (virNetDevTapInterfaceStats(path, stats,
> +    if (virNetDevTapInterfaceStats(device, stats,
>                                     !virDomainNetTypeSharesHostView(net)) < 0)
>          goto endjob;
>  
> diff --git a/src/openvz/openvz_driver.c b/src/openvz/openvz_driver.c
> index 11173898d..05ed2bcae 100644
> --- a/src/openvz/openvz_driver.c
> +++ b/src/openvz/openvz_driver.c
> @@ -1980,7 +1980,7 @@ openvzGetVEStatus(virDomainObjPtr vm, int *status, int *reason)
>  
>  static int
>  openvzDomainInterfaceStats(virDomainPtr dom,
> -                           const char *path,
> +                           const char *device,
>                             virDomainInterfaceStatsPtr stats)
>  {
>      struct openvz_driver *driver = dom->conn->privateData;
> @@ -2006,13 +2006,13 @@ openvzDomainInterfaceStats(virDomainPtr dom,
>          goto cleanup;
>      }
>  
> -    if (!(net = virDomainNetFindByName(vm->def, path))) {
> +    if (!(net = virDomainNetFind(vm->def, device))) {
>          virReportError(VIR_ERR_INVALID_ARG,
> -                       _("invalid path, '%s' is not a known interface"), path);
> +                       _("'%s' is not a known interface"), device);

Same...

>          goto cleanup;
>      }
>  
> -    if (virNetDevTapInterfaceStats(path, stats,
> +    if (virNetDevTapInterfaceStats(device, stats,
>                                     !virDomainNetTypeSharesHostView(net)) < 0)
>          goto cleanup;
>  
> diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c
> index 7c6f1674a..f2cc0f0a5 100644
> --- a/src/qemu/qemu_driver.c
> +++ b/src/qemu/qemu_driver.c
> @@ -11021,7 +11021,7 @@ qemuDomainBlockStatsFlags(virDomainPtr dom,
>  
>  static int
>  qemuDomainInterfaceStats(virDomainPtr dom,
> -                         const char *path,
> +                         const char *device,
>                           virDomainInterfaceStatsPtr stats)
>  {
>      virDomainObjPtr vm;
> @@ -11040,17 +11040,17 @@ qemuDomainInterfaceStats(virDomainPtr dom,
>          goto cleanup;
>      }
>  
> -    if (!(net = virDomainNetFindByName(vm->def, path))) {
> +    if (!(net = virDomainNetFind(vm->def, device))) {
>          virReportError(VIR_ERR_INVALID_ARG,
> -                       _("invalid path, '%s' is not a known interface"), path);
> +                       _("'%s' is not a known interface"), device);

Same.

>          goto cleanup;
>      }
>  
>      if (virDomainNetGetActualType(net) == VIR_DOMAIN_NET_TYPE_VHOSTUSER) {
> -        if (virNetDevOpenvswitchInterfaceStats(path, stats) < 0)
> +        if (virNetDevOpenvswitchInterfaceStats(device, stats) < 0)
>              goto cleanup;
>      } else {
> -        if (virNetDevTapInterfaceStats(path, stats,
> +        if (virNetDevTapInterfaceStats(device, stats,
>                                         !virDomainNetTypeSharesHostView(net)) < 0)
>              goto cleanup;
>      }
> diff --git a/src/remote/remote_protocol.x b/src/remote/remote_protocol.x
> index 07463b781..e3014f66b 100644
> --- a/src/remote/remote_protocol.x
> +++ b/src/remote/remote_protocol.x
> @@ -682,7 +682,7 @@ struct remote_domain_block_stats_flags_ret {
>  
>  struct remote_domain_interface_stats_args {
>      remote_nonnull_domain dom;
> -    remote_nonnull_string path;
> +    remote_nonnull_string device;
>  };
>  
>  struct remote_domain_interface_stats_ret { /* insert at 2 */
> diff --git a/src/remote_protocol-structs b/src/remote_protocol-structs
> index 6038bf138..dc78d51c4 100644
> --- a/src/remote_protocol-structs
> +++ b/src/remote_protocol-structs
> @@ -348,7 +348,7 @@ struct remote_domain_block_stats_flags_ret {
>  };
>  struct remote_domain_interface_stats_args {
>          remote_nonnull_domain      dom;
> -        remote_nonnull_string      path;
> +        remote_nonnull_string      device;
>  };
>  struct remote_domain_interface_stats_ret {
>          int64_t                    rx_bytes;
> diff --git a/src/test/test_driver.c b/src/test/test_driver.c
> index e92768a97..3e286635e 100644
> --- a/src/test/test_driver.c
> +++ b/src/test/test_driver.c
> @@ -3160,9 +3160,10 @@ static int testDomainBlockStats(virDomainPtr domain,
>      return ret;
>  }
>  
> -static int testDomainInterfaceStats(virDomainPtr domain,
> -                                    const char *path,
> -                                    virDomainInterfaceStatsPtr stats)
> +static int
> +testDomainInterfaceStats(virDomainPtr domain,
> +                         const char *device,
> +                         virDomainInterfaceStatsPtr stats)
>  {
>      virDomainObjPtr privdom;
>      struct timeval tv;
> @@ -3180,9 +3181,9 @@ static int testDomainInterfaceStats(virDomainPtr domain,
>          goto error;
>      }
>  
> -    if (!(net = virDomainNetFindByName(privdom->def, path))) {
> +    if (!(net = virDomainNetFind(privdom->def, device))) {
>          virReportError(VIR_ERR_INVALID_ARG,
> -                       _("invalid path, '%s' is not a known interface"), path);
> +                       _("'%s' is not a known interface"), device);

Same...

>          goto error;
>      }
>  
> diff --git a/src/vz/vz_driver.c b/src/vz/vz_driver.c
> index 9ebb51d60..c33962229 100644
> --- a/src/vz/vz_driver.c
> +++ b/src/vz/vz_driver.c
> @@ -1873,7 +1873,7 @@ vzDomainBlockStatsFlags(virDomainPtr domain,
>  
>  static int
>  vzDomainInterfaceStats(virDomainPtr domain,
> -                         const char *path,
> +                         const char *device,
>                           virDomainInterfaceStatsPtr stats)
>  {
>      virDomainObjPtr dom = NULL;
> @@ -1888,7 +1888,7 @@ vzDomainInterfaceStats(virDomainPtr domain,
>  
>      privdom = dom->privateData;
>  
> -    ret = prlsdkGetNetStats(privdom->stats, privdom->sdkdom, path, stats);
> +    ret = prlsdkGetNetStats(privdom->stats, privdom->sdkdom, device, stats);
>  
>   cleanup:
>      virDomainObjEndAPI(&dom);
> diff --git a/src/vz/vz_sdk.c b/src/vz/vz_sdk.c
> index 6ead47a0f..5f377147c 100644
> --- a/src/vz/vz_sdk.c
> +++ b/src/vz/vz_sdk.c
> @@ -4484,7 +4484,7 @@ prlsdkFindNetByPath(PRL_HANDLE sdkdom, const char *path)
>  }
>  
>  int
> -prlsdkGetNetStats(PRL_HANDLE sdkstats, PRL_HANDLE sdkdom, const char *path,
> +prlsdkGetNetStats(PRL_HANDLE sdkstats, PRL_HANDLE sdkdom, const char *device,
>                    virDomainInterfaceStatsPtr stats)
>  {
>      int ret = -1;
> @@ -4492,8 +4492,13 @@ prlsdkGetNetStats(PRL_HANDLE sdkstats, PRL_HANDLE sdkdom, const char *path,
>      char *name = NULL;
>      PRL_RESULT pret;
>      PRL_HANDLE net = PRL_INVALID_HANDLE;
> +    virMacAddr mac;
> +
> +    if (virMacAddrParse(device, &mac) == 0)
> +        net = prlsdkFindNetByMAC(sdkdom, device);
> +    else
> +        net = prlsdkFindNetByPath(sdkdom, device);
>  
> -    net = prlsdkFindNetByPath(sdkdom, path);
>      if (net == PRL_INVALID_HANDLE)
>         goto cleanup;
>  
> diff --git a/src/xen/xen_driver.c b/src/xen/xen_driver.c
> index dae0f4f28..4235ca0ce 100644
> --- a/src/xen/xen_driver.c
> +++ b/src/xen/xen_driver.c
> @@ -2109,10 +2109,11 @@ xenUnifiedDomainBlockStats(virDomainPtr dom, const char *path,
>  }
>  
>  static int
> -xenUnifiedDomainInterfaceStats(virDomainPtr dom, const char *path,
> +xenUnifiedDomainInterfaceStats(virDomainPtr dom, const char *device,
>                                 virDomainInterfaceStatsPtr stats)
>  {
>      virDomainDefPtr def = NULL;
> +    virDomainNetDefPtr net = NULL;
>      int ret = -1;
>  
>      if (!(def = xenGetDomainDefForDom(dom)))
> @@ -2121,7 +2122,13 @@ xenUnifiedDomainInterfaceStats(virDomainPtr dom, const char *path,
>      if (virDomainInterfaceStatsEnsureACL(dom->conn, def) < 0)
>          goto cleanup;
>  
> -    ret = xenHypervisorDomainInterfaceStats(def, path, stats);
> +    if (!(net = virDomainNetFind(def, device))) {
> +        virReportError(VIR_ERR_INVALID_ARG,
> +                       _("'%s' is not a known interface"), device);

Same...

> +        goto cleanup;
> +    }
> +
> +    ret = xenHypervisorDomainInterfaceStats(def, net->ifname, stats);
>  
>   cleanup:
>      virDomainDefFree(def);
> diff --git a/tools/virsh.pod b/tools/virsh.pod
> index 632f202e8..d21c5df72 100644
> --- a/tools/virsh.pod
> +++ b/tools/virsh.pod
> @@ -777,7 +777,8 @@ the guest OS via an agent. If unspecified, 'lease' is the default.
>  
>  Get network interface stats for a running domain. This might be
>  unavailable for some types of interface which don't have
> -representation in the host, e.g. user.
> +representation in the host, e.g. user. I<interface-device> can be
> +the interface's target name or the MAC address.

the interface target by name or MAC address.

With ordering adjustment....

Reviewed-by: John Ferlan <jferlan at redhat.com>

John

>  
>  =item B<domif-setlink> I<domain> I<interface-device> I<state> [I<--config>]
>  
> 




More information about the libvir-list mailing list