[libvirt] [PATCH libvirt-glib 2/3] Add GVirDomainInterface

Christophe Fergeau cfergeau at redhat.com
Thu Nov 10 21:09:37 UTC 2011


A few preliminary remark below, they apply to the next patch as well.

On Thu, Nov 10, 2011 at 09:33:42PM +0100, Marc-André Lureau wrote:
> ---
>  libvirt-gobject/Makefile.am                        |    2 +
>  libvirt-gobject/libvirt-gobject-domain-interface.c |  213 ++++++++++++++++++++
>  libvirt-gobject/libvirt-gobject-domain-interface.h |   81 ++++++++
>  libvirt-gobject/libvirt-gobject.h                  |    1 +
>  libvirt-gobject/libvirt-gobject.sym                |    4 +
>  5 files changed, 301 insertions(+), 0 deletions(-)
>  create mode 100644 libvirt-gobject/libvirt-gobject-domain-interface.c
>  create mode 100644 libvirt-gobject/libvirt-gobject-domain-interface.h
> 
> diff --git a/libvirt-gobject/Makefile.am b/libvirt-gobject/Makefile.am
> index 56a047e..8b59109 100644
> --- a/libvirt-gobject/Makefile.am
> +++ b/libvirt-gobject/Makefile.am
> @@ -8,6 +8,7 @@ GOBJECT_HEADER_FILES = \
>  			libvirt-gobject-main.h \
>  			libvirt-gobject-domain-snapshot.h \
>  			libvirt-gobject-domain-device.h \
> +			libvirt-gobject-domain-interface.h \
>  			libvirt-gobject-domain.h \
>  			libvirt-gobject-interface.h \
>  			libvirt-gobject-network.h \
> @@ -23,6 +24,7 @@ GOBJECT_SOURCE_FILES = \
>  			libvirt-gobject-main.c \
>  			libvirt-gobject-domain-snapshot.c \
>  			libvirt-gobject-domain-device.c \
> +			libvirt-gobject-domain-interface.c \
>  			libvirt-gobject-domain.c \
>  			libvirt-gobject-interface.c \
>  			libvirt-gobject-network.c \
> diff --git a/libvirt-gobject/libvirt-gobject-domain-interface.c b/libvirt-gobject/libvirt-gobject-domain-interface.c
> new file mode 100644
> index 0000000..65a5467
> --- /dev/null
> +++ b/libvirt-gobject/libvirt-gobject-domain-interface.c
> @@ -0,0 +1,213 @@
> +/*
> + * libvirt-gobject-domain-interface.c: libvirt gobject integration
> + *
> + * Copyright (C) 2011 Red Hat
> + *
> + * This library is free software; you can redistribute it and/or
> + * modify it under the terms of the GNU Lesser General Public
> + * License as published by the Free Software Foundation; either
> + * version 2.1 of the License, or (at your option) any later version.
> + *
> + * This library is distributed in the hope that it will be useful,
> + * but WITHOUT ANY WARRANTY; without even the implied warranty of
> + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
> + * Lesser General Public License for more details.
> + *
> + * You should have received a copy of the GNU Lesser General Public
> + * License along with this library; if not, write to the Free Software
> + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
> + *
> + * Author: Marc-André Lureau <marcandre.lureau at redhat.com>
> + */
> +
> +#include <config.h>
> +
> +#include <libvirt/virterror.h>
> +#include <string.h>
> +
> +#include "libvirt-glib/libvirt-glib.h"
> +#include "libvirt-gobject/libvirt-gobject.h"
> +
> +#include "libvirt-gobject/libvirt-gobject-domain-device-private.h"
> +
> +extern gboolean debugFlag;
> +
> +#define DEBUG(fmt, ...) do { if (G_UNLIKELY(debugFlag)) g_debug(fmt, ## __VA_ARGS__); } while (0)
> +
> +#define GVIR_DOMAIN_INTERFACE_GET_PRIVATE(obj)                         \
> +        (G_TYPE_INSTANCE_GET_PRIVATE((obj), GVIR_TYPE_DOMAIN_INTERFACE, GVirDomainInterfacePrivate))
> +
> +struct _GVirDomainInterfacePrivate
> +{
> +    gchar *path;
> +};
> +
> +G_DEFINE_TYPE(GVirDomainInterface, gvir_domain_interface, GVIR_TYPE_DOMAIN_DEVICE);
> +
> +enum {
> +    PROP_0,
> +    PROP_PATH,
> +};
> +
> +#define GVIR_DOMAIN_INTERFACE_ERROR gvir_domain_interface_error_quark()
> +
> +
> +static GQuark
> +gvir_domain_interface_error_quark(void)
> +{
> +    return g_quark_from_static_string("gvir-domain-interface");
> +}
> +
> +static void gvir_domain_interface_get_property(GObject *object,
> +                                               guint prop_id,
> +                                               GValue *value,
> +                                               GParamSpec *pspec)
> +{
> +    GVirDomainInterface *self = GVIR_DOMAIN_INTERFACE(object);
> +    GVirDomainInterfacePrivate *priv = self->priv;
> +
> +    switch (prop_id) {
> +    case PROP_PATH:
> +        g_value_set_string(value, priv->path);
> +        break;
> +
> +    default:
> +        G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
> +    }
> +}
> +
> +
> +static void gvir_domain_interface_set_property(GObject *object,
> +                                          guint prop_id,
> +                                          const GValue *value,
> +                                          GParamSpec *pspec)
> +{
> +    GVirDomainInterface *self = GVIR_DOMAIN_INTERFACE(object);
> +    GVirDomainInterfacePrivate *priv = self->priv;
> +
> +    switch (prop_id) {
> +    case PROP_PATH:
> +        if (priv->path)
> +            g_free(priv->path);

You can safely call g_free on a NULL pointer, this makes the code a bit
simpler, there are several occurrences of this in the 2 patches.

> +        priv->path = g_value_dup_string(value);
> +        break;
> +
> +    default:
> +        G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
> +    }
> +}
> +
> +
> +static void gvir_domain_interface_finalize(GObject *object)
> +{
> +    GVirDomainInterface *self = GVIR_DOMAIN_INTERFACE(object);
> +    GVirDomainInterfacePrivate *priv = self->priv;
> +
> +    DEBUG("Finalize GVirDomainInterface=%p", self);
> +
> +    if (priv->path)
> +        g_free(priv->path);
> +
> +    G_OBJECT_CLASS(gvir_domain_interface_parent_class)->finalize(object);
> +}
> +
> +static void gvir_domain_interface_class_init(GVirDomainInterfaceClass *klass)
> +{
> +    GObjectClass *object_class = G_OBJECT_CLASS (klass);
> +
> +    object_class->finalize = gvir_domain_interface_finalize;
> +    object_class->get_property = gvir_domain_interface_get_property;
> +    object_class->set_property = gvir_domain_interface_set_property;
> +
> +    g_object_class_install_property(object_class,
> +                                    PROP_PATH,
> +                                    g_param_spec_string("path",
> +                                                        "Path",
> +                                                        "The interface path",
> +                                                        NULL,
> +                                                        G_PARAM_READWRITE |
> +                                                        G_PARAM_CONSTRUCT_ONLY |
> +                                                        G_PARAM_STATIC_STRINGS));
> +
> +    g_type_class_add_private(klass, sizeof(GVirDomainInterfacePrivate));
> +}
> +
> +static void gvir_domain_interface_init(GVirDomainInterface *self)
> +{
> +    DEBUG("Init GVirDomainInterface=%p", self);
> +
> +    self->priv = GVIR_DOMAIN_INTERFACE_GET_PRIVATE(self);
> +}
> +
> +static GVirDomainInterfaceStats *
> +gvir_domain_interface_stats_copy(GVirDomainInterfaceStats *stats)
> +{
> +    return g_slice_dup(GVirDomainInterfaceStats, stats);
> +}

Do we really need to use GSlice here? I consider GSlice as something to use
when you want to make many allocations of same-size objects, will we
allocate many of these stats objects?

> +
> +
> +static void
> +gvir_domain_interface_stats_free(GVirDomainInterfaceStats *stats)
> +{
> +    g_slice_free(GVirDomainInterfaceStats, stats);
> +}
> +
> +
> +GType gvir_domain_interface_stats_get_type(void)
> +{
> +    static GType stats_type = 0;
> +
> +    if (G_UNLIKELY(stats_type == 0))
> +        stats_type = g_boxed_type_register_static
> +            ("GVirDomainInterfaceStats",
> +             (GBoxedCopyFunc)gvir_domain_interface_stats_copy,
> +             (GBoxedFreeFunc)gvir_domain_interface_stats_free);
> +
> +    return stats_type;
> +}
> +
> +/**
> + * gvir_domain_interface_get_stats:
> + * @self: the domain interface
> + * @err: an error
> + *
> + * This function returns network interface stats. Individual fields
> + * within the stats structure may be returned as -1, which indicates
> + * that the hypervisor does not support that particular statistic.
> + *
> + * Returns: (transfer full): the stats or %NULL in case of error
> + **/
> +GVirDomainInterfaceStats *gvir_domain_interface_get_stats(GVirDomainInterface *self, GError **err)
> +{
> +    GVirDomainInterfaceStats *ret;
> +    virDomainInterfaceStatsStruct stats;
> +    GVirDomainInterfacePrivate *priv;
> +    virDomainPtr handle;
> +
> +    g_return_val_if_fail(GVIR_IS_DOMAIN_INTERFACE(self), NULL);
> +
> +    priv = self->priv;
> +    handle = gvir_domain_device_get_domain_handle(GVIR_DOMAIN_DEVICE(self));
> +
> +    if (virDomainInterfaceStats(handle, priv->path, &stats, sizeof (stats)) < 0) {
> +        if (err)
> +            *err = gvir_error_new_literal(GVIR_DOMAIN_INTERFACE_ERROR,
> +                                          0,
> +                                          "Unable to get domain interface stats");
> +        goto end;
> +    }
> +
> +    ret = g_slice_new(GVirDomainInterfaceStats);
> +    ret->rx_bytes = stats.rx_bytes;
> +    ret->rx_packets = stats.rx_packets;
> +    ret->rx_errs = stats.rx_errs;
> +    ret->rx_drop = stats.rx_drop;
> +    ret->tx_bytes = stats.tx_bytes;
> +    ret->tx_packets = stats.tx_packets;
> +    ret->tx_errs = stats.tx_errs;
> +    ret->tx_drop = stats.tx_drop;
> +
> +end:
> +    virDomainFree(handle);
> +    return ret;
> +}
> diff --git a/libvirt-gobject/libvirt-gobject-domain-interface.h b/libvirt-gobject/libvirt-gobject-domain-interface.h
> new file mode 100644
> index 0000000..5541f91
> --- /dev/null
> +++ b/libvirt-gobject/libvirt-gobject-domain-interface.h
> @@ -0,0 +1,81 @@
> +/*
> + * libvirt-gobject-domain-interface.h: libvirt gobject integration
> + *
> + * Copyright (C) 2011 Red Hat
> + *
> + * This library is free software; you can redistribute it and/or
> + * modify it under the terms of the GNU Lesser General Public
> + * License as published by the Free Software Foundation; either
> + * version 2.1 of the License, or (at your option) any later version.
> + *
> + * This library is distributed in the hope that it will be useful,
> + * but WITHOUT ANY WARRANTY; without even the implied warranty of
> + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
> + * Lesser General Public License for more details.
> + *
> + * You should have received a copy of the GNU Lesser General Public
> + * License along with this library; if not, write to the Free Software
> + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
> + *
> + * Author: Marc-André Lureau <marcandre.lureau at redhat.com>
> + */
> +
> +#if !defined(__LIBVIRT_GOBJECT_H__) && !defined(LIBVIRT_GOBJECT_BUILD)
> +#error "Only <libvirt-gobject/libvirt-gobject.h> can be included directly."
> +#endif
> +
> +#ifndef __LIBVIRT_GOBJECT_DOMAIN_INTERFACE_H__
> +#define __LIBVIRT_GOBJECT_DOMAIN_INTERFACE_H__
> +
> +G_BEGIN_DECLS
> +
> +#define GVIR_TYPE_DOMAIN_INTERFACE            (gvir_domain_interface_get_type ())
> +#define GVIR_DOMAIN_INTERFACE(obj)            (G_TYPE_CHECK_INSTANCE_CAST ((obj), GVIR_TYPE_DOMAIN_INTERFACE, GVirDomainInterface))
> +#define GVIR_DOMAIN_INTERFACE_CLASS(klass)    (G_TYPE_CHECK_CLASS_CAST ((klass), GVIR_TYPE_DOMAIN_INTERFACE, GVirDomainInterfaceClass))
> +#define GVIR_IS_DOMAIN_INTERFACE(obj)         (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GVIR_TYPE_DOMAIN_INTERFACE))
> +#define GVIR_IS_DOMAIN_INTERFACE_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GVIR_TYPE_DOMAIN_INTERFACE))
> +#define GVIR_DOMAIN_INTERFACE_GET_CLASS(obj)  (G_TYPE_INSTANCE_GET_CLASS ((obj), GVIR_TYPE_DOMAIN_INTERFACE, GVirDomainInterfaceClass))
> +
> +#define GVIR_TYPE_DOMAIN_INTERFACE_STATS       (gvir_domain_interface_stats_get_type())
> +
> +typedef struct _GVirDomainInterfaceStats GVirDomainInterfaceStats;
> +struct _GVirDomainInterfaceStats
> +{
> +    gint64 rx_bytes;
> +    gint64 rx_packets;
> +    gint64 rx_errs;
> +    gint64 rx_drop;
> +    gint64 tx_bytes;
> +    gint64 tx_packets;
> +    gint64 tx_errs;
> +    gint64 tx_drop;
> +};

2 questions about this:
* should we use more explicit names (which probably means longer ones)?
* how do we handle ABI compatibility the day we need to add fields to this
  struct?

> +
> +typedef struct _GVirDomainInterface GVirDomainInterface;
> +typedef struct _GVirDomainInterfacePrivate GVirDomainInterfacePrivate;
> +typedef struct _GVirDomainInterfaceClass GVirDomainInterfaceClass;
> +
> +struct _GVirDomainInterface
> +{
> +    GVirDomainDevice parent;
> +
> +    GVirDomainInterfacePrivate *priv;
> +
> +    /* Do not add fields to this struct */
> +};
> +
> +struct _GVirDomainInterfaceClass
> +{
> +    GVirDomainDeviceClass parent_class;
> +
> +    gpointer padding[20];
> +};
> +
> +GType gvir_domain_interface_get_type(void);
> +GType gvir_domain_interface_stats_get_type(void);
> +
> +GVirDomainInterfaceStats *gvir_domain_interface_get_stats(GVirDomainInterface *self, GError **err);
> +
> +G_END_DECLS
> +
> +#endif /* __LIBVIRT_GOBJECT_DOMAIN_INTERFACE_H__ */
> diff --git a/libvirt-gobject/libvirt-gobject.h b/libvirt-gobject/libvirt-gobject.h
> index 3bec2c9..6038036 100644
> --- a/libvirt-gobject/libvirt-gobject.h
> +++ b/libvirt-gobject/libvirt-gobject.h
> @@ -31,6 +31,7 @@
>  #include <libvirt-gobject/libvirt-gobject-enums.h>
>  #include <libvirt-gobject/libvirt-gobject-stream.h>
>  #include <libvirt-gobject/libvirt-gobject-domain-device.h>
> +#include <libvirt-gobject/libvirt-gobject-domain-interface.h>
>  #include <libvirt-gobject/libvirt-gobject-domain-snapshot.h>
>  #include <libvirt-gobject/libvirt-gobject-domain.h>
>  #include <libvirt-gobject/libvirt-gobject-interface.h>
> diff --git a/libvirt-gobject/libvirt-gobject.sym b/libvirt-gobject/libvirt-gobject.sym
> index da03001..9118b11 100644
> --- a/libvirt-gobject/libvirt-gobject.sym
> +++ b/libvirt-gobject/libvirt-gobject.sym
> @@ -31,6 +31,10 @@ LIBVIRT_GOBJECT_0.0.1 {
>  
>  	gvir_domain_device_get_type;
>  
> +	gvir_domain_interface_get_type;
> +	gvir_domain_interface_stats_get_type;
> +	gvir_domain_interface_get_stats;
> +
>  	gvir_domain_get_type;
>  	gvir_domain_handle_get_type;
>  	gvir_domain_info_get_type;
> -- 
> 1.7.7
> 
> --
> libvir-list mailing list
> libvir-list at redhat.com
> https://www.redhat.com/mailman/listinfo/libvir-list
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 198 bytes
Desc: not available
URL: <http://listman.redhat.com/archives/libvir-list/attachments/20111110/ee0972e3/attachment-0001.sig>


More information about the libvir-list mailing list