Blame libvirt-gobject/libvirt-gobject-domain-interface.c

Packit Service ea0369
/*
Packit Service ea0369
 * libvirt-gobject-domain-interface.c: libvirt gobject integration
Packit Service ea0369
 *
Packit Service ea0369
 * Copyright (C) 2011 Red Hat, Inc.
Packit Service ea0369
 *
Packit Service ea0369
 * This library is free software; you can redistribute it and/or
Packit Service ea0369
 * modify it under the terms of the GNU Lesser General Public
Packit Service ea0369
 * License as published by the Free Software Foundation; either
Packit Service ea0369
 * version 2.1 of the License, or (at your option) any later version.
Packit Service ea0369
 *
Packit Service ea0369
 * This library is distributed in the hope that it will be useful,
Packit Service ea0369
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service ea0369
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit Service ea0369
 * Lesser General Public License for more details.
Packit Service ea0369
 *
Packit Service ea0369
 * You should have received a copy of the GNU Lesser General Public
Packit Service ea0369
 * License along with this library. If not, see
Packit Service ea0369
 * <http://www.gnu.org/licenses/>.
Packit Service ea0369
 *
Packit Service ea0369
 * Author: Marc-André Lureau <marcandre.lureau@redhat.com>
Packit Service ea0369
 */
Packit Service ea0369
Packit Service ea0369
#include <config.h>
Packit Service ea0369
Packit Service ea0369
#include <libvirt/virterror.h>
Packit Service ea0369
#include <string.h>
Packit Service ea0369
Packit Service ea0369
#include "libvirt-glib/libvirt-glib.h"
Packit Service ea0369
#include "libvirt-gobject/libvirt-gobject.h"
Packit Service ea0369
#include "libvirt-gobject-compat.h"
Packit Service ea0369
Packit Service ea0369
#include "libvirt-gobject/libvirt-gobject-domain-device-private.h"
Packit Service ea0369
Packit Service ea0369
#define GVIR_DOMAIN_INTERFACE_GET_PRIVATE(obj)                         \
Packit Service ea0369
        (G_TYPE_INSTANCE_GET_PRIVATE((obj), GVIR_TYPE_DOMAIN_INTERFACE, GVirDomainInterfacePrivate))
Packit Service ea0369
Packit Service ea0369
struct _GVirDomainInterfacePrivate
Packit Service ea0369
{
Packit Service ea0369
    gboolean unused;
Packit Service ea0369
};
Packit Service ea0369
Packit Service ea0369
G_DEFINE_TYPE_WITH_PRIVATE(GVirDomainInterface, gvir_domain_interface, GVIR_TYPE_DOMAIN_DEVICE);
Packit Service ea0369
Packit Service ea0369
#define GVIR_DOMAIN_INTERFACE_ERROR gvir_domain_interface_error_quark()
Packit Service ea0369
Packit Service ea0369
static GQuark
Packit Service ea0369
gvir_domain_interface_error_quark(void)
Packit Service ea0369
{
Packit Service ea0369
    return g_quark_from_static_string("gvir-domain-interface");
Packit Service ea0369
}
Packit Service ea0369
Packit Service ea0369
static void gvir_domain_interface_class_init(GVirDomainInterfaceClass *klass G_GNUC_UNUSED)
Packit Service ea0369
{
Packit Service ea0369
}
Packit Service ea0369
Packit Service ea0369
static void gvir_domain_interface_init(GVirDomainInterface *self)
Packit Service ea0369
{
Packit Service ea0369
    self->priv = GVIR_DOMAIN_INTERFACE_GET_PRIVATE(self);
Packit Service ea0369
}
Packit Service ea0369
Packit Service ea0369
static GVirDomainInterfaceStats *
Packit Service ea0369
gvir_domain_interface_stats_copy(GVirDomainInterfaceStats *stats)
Packit Service ea0369
{
Packit Service ea0369
    return g_slice_dup(GVirDomainInterfaceStats, stats);
Packit Service ea0369
}
Packit Service ea0369
Packit Service ea0369
static void
Packit Service ea0369
gvir_domain_interface_stats_free(GVirDomainInterfaceStats *stats)
Packit Service ea0369
{
Packit Service ea0369
    g_slice_free(GVirDomainInterfaceStats, stats);
Packit Service ea0369
}
Packit Service ea0369
Packit Service ea0369
G_DEFINE_BOXED_TYPE(GVirDomainInterfaceStats, gvir_domain_interface_stats,
Packit Service ea0369
                    gvir_domain_interface_stats_copy, gvir_domain_interface_stats_free)
Packit Service ea0369
Packit Service ea0369
static const gchar *gvir_domain_interface_get_path(GVirDomainInterface *self)
Packit Service ea0369
{
Packit Service ea0369
    GVirConfigDomainDevice *config;
Packit Service ea0369
    const gchar *path = NULL;
Packit Service ea0369
Packit Service ea0369
    config = gvir_domain_device_get_config(GVIR_DOMAIN_DEVICE(self));
Packit Service ea0369
    if (GVIR_CONFIG_IS_DOMAIN_INTERFACE_USER(self))
Packit Service ea0369
        /* FIXME: One of the limitations of user-mode networking of libvirt */
Packit Service ea0369
        g_debug("Statistics gathering for user-mode network not yet supported");
Packit Service ea0369
    else
Packit Service ea0369
        path = gvir_config_domain_interface_get_ifname(GVIR_CONFIG_DOMAIN_INTERFACE (config));
Packit Service ea0369
Packit Service ea0369
    g_object_unref (config);
Packit Service ea0369
Packit Service ea0369
    return path;
Packit Service ea0369
}
Packit Service ea0369
Packit Service ea0369
/**
Packit Service ea0369
 * gvir_domain_interface_get_stats:
Packit Service ea0369
 * @self: the domain interface
Packit Service ea0369
 * @err: an error
Packit Service ea0369
 *
Packit Service ea0369
 * This function returns network interface stats. Individual fields
Packit Service ea0369
 * within the stats structure may be returned as -1, which indicates
Packit Service ea0369
 * that the hypervisor does not support that particular statistic.
Packit Service ea0369
 *
Packit Service ea0369
 * Returns: (transfer full): the stats or %NULL in case of error. The
Packit Service ea0369
 * returned object should be unreffed with g_object_unref() when no longer
Packit Service ea0369
 * needed.
Packit Service ea0369
 **/
Packit Service ea0369
GVirDomainInterfaceStats *gvir_domain_interface_get_stats(GVirDomainInterface *self, GError **err)
Packit Service ea0369
{
Packit Service ea0369
    GVirDomainInterfaceStats *ret = NULL;
Packit Service ea0369
    virDomainInterfaceStatsStruct stats;
Packit Service ea0369
    virDomainPtr handle;
Packit Service ea0369
    const gchar *path;
Packit Service ea0369
Packit Service ea0369
    g_return_val_if_fail(GVIR_IS_DOMAIN_INTERFACE(self), NULL);
Packit Service ea0369
    g_return_val_if_fail(err == NULL || *err == NULL, NULL);
Packit Service ea0369
Packit Service ea0369
    handle = gvir_domain_device_get_domain_handle(GVIR_DOMAIN_DEVICE(self));
Packit Service ea0369
    path = gvir_domain_interface_get_path (self);
Packit Service ea0369
    if (path == NULL) {
Packit Service ea0369
        ret = g_slice_new0(GVirDomainInterfaceStats);
Packit Service ea0369
        goto end;
Packit Service ea0369
    }
Packit Service ea0369
Packit Service ea0369
    if (virDomainInterfaceStats(handle, path, &stats, sizeof (stats)) < 0) {
Packit Service ea0369
        gvir_set_error_literal(err, GVIR_DOMAIN_INTERFACE_ERROR,
Packit Service ea0369
                               0,
Packit Service ea0369
                               "Unable to get domain interface stats");
Packit Service ea0369
        goto end;
Packit Service ea0369
    }
Packit Service ea0369
Packit Service ea0369
    ret = g_slice_new(GVirDomainInterfaceStats);
Packit Service ea0369
    ret->rx_bytes = stats.rx_bytes;
Packit Service ea0369
    ret->rx_packets = stats.rx_packets;
Packit Service ea0369
    ret->rx_errs = stats.rx_errs;
Packit Service ea0369
    ret->rx_drop = stats.rx_drop;
Packit Service ea0369
    ret->tx_bytes = stats.tx_bytes;
Packit Service ea0369
    ret->tx_packets = stats.tx_packets;
Packit Service ea0369
    ret->tx_errs = stats.tx_errs;
Packit Service ea0369
    ret->tx_drop = stats.tx_drop;
Packit Service ea0369
Packit Service ea0369
end:
Packit Service ea0369
    virDomainFree(handle);
Packit Service ea0369
    return ret;
Packit Service ea0369
}