Blame libnm/nm-device-team.c

Packit Service 87a54e
/* SPDX-License-Identifier: LGPL-2.1-or-later */
Packit 5756e2
/*
Packit 5756e2
 * Copyright (C) 2013 Jiri Pirko <jiri@resnulli.us>
Packit 5756e2
 */
Packit 5756e2
Packit Service 2bceb2
#include "libnm/nm-default-libnm.h"
Packit 5756e2
Packit 5756e2
#include "nm-device-team.h"
Packit 5756e2
Packit 5756e2
#include "nm-setting-connection.h"
Packit 5756e2
#include "nm-setting-team.h"
Packit 5756e2
#include "nm-utils.h"
Packit 5756e2
#include "nm-object-private.h"
Packit 5756e2
#include "nm-core-internal.h"
Packit 5756e2
Packit 5756e2
/*****************************************************************************/
Packit 5756e2
Packit Service a1bd4f
NM_GOBJECT_PROPERTIES_DEFINE_BASE(PROP_CARRIER, PROP_SLAVES, PROP_CONFIG, );
Packit 5756e2
Packit 5756e2
typedef struct {
Packit Service a1bd4f
    NMLDBusPropertyAO slaves;
Packit Service a1bd4f
    char *            config;
Packit Service a1bd4f
    bool              carrier;
Packit 5756e2
} NMDeviceTeamPrivate;
Packit 5756e2
Packit 5756e2
struct _NMDeviceTeam {
Packit Service a1bd4f
    NMDevice            parent;
Packit Service a1bd4f
    NMDeviceTeamPrivate _priv;
Packit 5756e2
};
Packit 5756e2
Packit 5756e2
struct _NMDeviceTeamClass {
Packit Service a1bd4f
    NMDeviceClass parent;
Packit 5756e2
};
Packit 5756e2
Packit Service a1bd4f
G_DEFINE_TYPE(NMDeviceTeam, nm_device_team, NM_TYPE_DEVICE)
Packit 5756e2
Packit Service a1bd4f
#define NM_DEVICE_TEAM_GET_PRIVATE(self) \
Packit Service a1bd4f
    _NM_GET_PRIVATE(self, NMDeviceTeam, NM_IS_DEVICE_TEAM, NMObject, NMDevice)
Packit 5756e2
Packit 5756e2
/*****************************************************************************/
Packit 5756e2
Packit 5756e2
/**
Packit 5756e2
 * nm_device_team_get_hw_address: (skip)
Packit 5756e2
 * @device: a #NMDeviceTeam
Packit 5756e2
 *
Packit 5756e2
 * Gets the hardware (MAC) address of the #NMDeviceTeam
Packit 5756e2
 *
Packit 5756e2
 * Returns: the hardware address. This is the internal string used by the
Packit 5756e2
 * device, and must not be modified.
Packit 5756e2
 *
Packit 5756e2
 * Deprecated: 1.24: Use nm_device_get_hw_address() instead.
Packit 5756e2
 **/
Packit 5756e2
const char *
Packit Service a1bd4f
nm_device_team_get_hw_address(NMDeviceTeam *device)
Packit 5756e2
{
Packit Service a1bd4f
    g_return_val_if_fail(NM_IS_DEVICE_TEAM(device), NULL);
Packit 5756e2
Packit Service a1bd4f
    return nm_device_get_hw_address(NM_DEVICE(device));
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
/**
Packit 5756e2
 * nm_device_team_get_carrier:
Packit 5756e2
 * @device: a #NMDeviceTeam
Packit 5756e2
 *
Packit 5756e2
 * Whether the device has carrier.
Packit 5756e2
 *
Packit 5756e2
 * Returns: %TRUE if the device has carrier
Packit 5756e2
 **/
Packit 5756e2
gboolean
Packit Service a1bd4f
nm_device_team_get_carrier(NMDeviceTeam *device)
Packit 5756e2
{
Packit Service a1bd4f
    g_return_val_if_fail(NM_IS_DEVICE_TEAM(device), FALSE);
Packit 5756e2
Packit Service a1bd4f
    return NM_DEVICE_TEAM_GET_PRIVATE(device)->carrier;
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
/**
Packit 5756e2
 * nm_device_team_get_slaves:
Packit 5756e2
 * @device: a #NMDeviceTeam
Packit 5756e2
 *
Packit 5756e2
 * Gets the devices currently enslaved to @device.
Packit 5756e2
 *
Packit 5756e2
 * Returns: (element-type NMDevice): the #GPtrArray containing
Packit 5756e2
 * #NMDevices that are slaves of @device. This is the internal
Packit 5756e2
 * copy used by the device, and must not be modified.
Packit 5756e2
 **/
Packit 5756e2
const GPtrArray *
Packit Service a1bd4f
nm_device_team_get_slaves(NMDeviceTeam *device)
Packit 5756e2
{
Packit Service a1bd4f
    g_return_val_if_fail(NM_IS_DEVICE_TEAM(device), FALSE);
Packit 5756e2
Packit Service a1bd4f
    return nml_dbus_property_ao_get_objs_as_ptrarray(&NM_DEVICE_TEAM_GET_PRIVATE(device)->slaves);
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
/**
Packit 5756e2
 * nm_device_team_get_config:
Packit 5756e2
 * @device: a #NMDeviceTeam
Packit 5756e2
 *
Packit 5756e2
 * Gets the current JSON configuration of the #NMDeviceTeam
Packit 5756e2
 *
Packit 5756e2
 * Returns: the current configuration. This is the internal string used by the
Packit 5756e2
 * device, and must not be modified.
Packit 5756e2
 *
Packit 5756e2
 * Since: 1.4
Packit 5756e2
 **/
Packit 5756e2
const char *
Packit Service a1bd4f
nm_device_team_get_config(NMDeviceTeam *device)
Packit 5756e2
{
Packit Service a1bd4f
    g_return_val_if_fail(NM_IS_DEVICE_TEAM(device), NULL);
Packit 5756e2
Packit Service a1bd4f
    return _nml_coerce_property_str_not_empty(NM_DEVICE_TEAM_GET_PRIVATE(device)->config);
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
static gboolean
Packit Service a1bd4f
connection_compatible(NMDevice *device, NMConnection *connection, GError **error)
Packit 5756e2
{
Packit Service a1bd4f
    if (!NM_DEVICE_CLASS(nm_device_team_parent_class)
Packit Service a1bd4f
             ->connection_compatible(device, connection, error))
Packit Service a1bd4f
        return FALSE;
Packit 5756e2
Packit Service a1bd4f
    if (!nm_connection_is_type(connection, NM_SETTING_TEAM_SETTING_NAME)) {
Packit Service a1bd4f
        g_set_error_literal(error,
Packit Service a1bd4f
                            NM_DEVICE_ERROR,
Packit Service a1bd4f
                            NM_DEVICE_ERROR_INVALID_CONNECTION,
Packit Service a1bd4f
                            _("The connection was not a team connection."));
Packit Service a1bd4f
        return FALSE;
Packit Service a1bd4f
    }
Packit 5756e2
Packit Service a1bd4f
    /* FIXME: check slaves? */
Packit 5756e2
Packit Service a1bd4f
    return TRUE;
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
static GType
Packit Service a1bd4f
get_setting_type(NMDevice *device)
Packit 5756e2
{
Packit Service a1bd4f
    return NM_TYPE_SETTING_TEAM;
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
/*****************************************************************************/
Packit 5756e2
Packit 5756e2
static void
Packit Service a1bd4f
nm_device_team_init(NMDeviceTeam *device)
Packit Service a1bd4f
{}
Packit 5756e2
Packit 5756e2
static void
Packit Service a1bd4f
finalize(GObject *object)
Packit 5756e2
{
Packit Service a1bd4f
    NMDeviceTeamPrivate *priv = NM_DEVICE_TEAM_GET_PRIVATE(object);
Packit 5756e2
Packit Service a1bd4f
    g_free(priv->config);
Packit 5756e2
Packit Service a1bd4f
    G_OBJECT_CLASS(nm_device_team_parent_class)->finalize(object);
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
static void
Packit Service a1bd4f
get_property(GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
Packit 5756e2
{
Packit Service a1bd4f
    NMDeviceTeam *device = NM_DEVICE_TEAM(object);
Packit Service a1bd4f
Packit Service a1bd4f
    switch (prop_id) {
Packit Service a1bd4f
    case PROP_CARRIER:
Packit Service a1bd4f
        g_value_set_boolean(value, nm_device_team_get_carrier(device));
Packit Service a1bd4f
        break;
Packit Service a1bd4f
    case PROP_SLAVES:
Packit Service a1bd4f
        g_value_take_boxed(value, _nm_utils_copy_object_array(nm_device_team_get_slaves(device)));
Packit Service a1bd4f
        break;
Packit Service a1bd4f
    case PROP_CONFIG:
Packit Service a1bd4f
        g_value_set_string(value, nm_device_team_get_config(device));
Packit Service a1bd4f
        break;
Packit Service a1bd4f
    default:
Packit Service a1bd4f
        G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
Packit Service a1bd4f
        break;
Packit Service a1bd4f
    }
Packit 5756e2
}
Packit 5756e2
Packit Service a1bd4f
const NMLDBusMetaIface _nml_dbus_meta_iface_nm_device_team = NML_DBUS_META_IFACE_INIT_PROP(
Packit Service a1bd4f
    NM_DBUS_INTERFACE_DEVICE_TEAM,
Packit Service a1bd4f
    nm_device_team_get_type,
Packit Service a1bd4f
    NML_DBUS_META_INTERFACE_PRIO_INSTANTIATE_30,
Packit Service a1bd4f
    NML_DBUS_META_IFACE_DBUS_PROPERTIES(
Packit Service a1bd4f
        NML_DBUS_META_PROPERTY_INIT_B("Carrier", PROP_CARRIER, NMDeviceTeam, _priv.carrier),
Packit Service a1bd4f
        NML_DBUS_META_PROPERTY_INIT_S("Config", PROP_CONFIG, NMDeviceTeam, _priv.config),
Packit Service a1bd4f
        NML_DBUS_META_PROPERTY_INIT_FCN("HwAddress",
Packit Service a1bd4f
                                        0,
Packit Service a1bd4f
                                        "s",
Packit Service a1bd4f
                                        _nm_device_notify_update_prop_hw_address),
Packit Service a1bd4f
        NML_DBUS_META_PROPERTY_INIT_AO_PROP("Slaves",
Packit Service a1bd4f
                                            PROP_SLAVES,
Packit Service a1bd4f
                                            NMDeviceTeam,
Packit Service a1bd4f
                                            _priv.slaves,
Packit Service a1bd4f
                                            nm_device_get_type), ), );
Packit 5756e2
Packit 5756e2
static void
Packit Service a1bd4f
nm_device_team_class_init(NMDeviceTeamClass *klass)
Packit 5756e2
{
Packit Service a1bd4f
    GObjectClass * object_class    = G_OBJECT_CLASS(klass);
Packit Service a1bd4f
    NMObjectClass *nm_object_class = NM_OBJECT_CLASS(klass);
Packit Service a1bd4f
    NMDeviceClass *device_class    = NM_DEVICE_CLASS(klass);
Packit Service a1bd4f
Packit Service a1bd4f
    object_class->get_property = get_property;
Packit Service a1bd4f
    object_class->finalize     = finalize;
Packit Service a1bd4f
Packit Service a1bd4f
    _NM_OBJECT_CLASS_INIT_PRIV_PTR_DIRECT(nm_object_class, NMDeviceTeam);
Packit Service a1bd4f
Packit Service a1bd4f
    _NM_OBJECT_CLASS_INIT_PROPERTY_AO_FIELDS_1(nm_object_class, NMDeviceTeamPrivate, slaves);
Packit Service a1bd4f
Packit Service a1bd4f
    device_class->connection_compatible = connection_compatible;
Packit Service a1bd4f
    device_class->get_setting_type      = get_setting_type;
Packit Service a1bd4f
Packit Service a1bd4f
    /**
Packit Service a1bd4f
     * NMDeviceTeam:carrier:
Packit Service a1bd4f
     *
Packit Service a1bd4f
     * Whether the device has carrier.
Packit Service a1bd4f
     **/
Packit Service a1bd4f
    obj_properties[PROP_CARRIER] = g_param_spec_boolean(NM_DEVICE_TEAM_CARRIER,
Packit Service a1bd4f
                                                        "",
Packit Service a1bd4f
                                                        "",
Packit Service a1bd4f
                                                        FALSE,
Packit Service a1bd4f
                                                        G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
Packit Service a1bd4f
Packit Service a1bd4f
    /**
Packit Service a1bd4f
     * NMDeviceTeam:slaves: (type GPtrArray(NMDevice))
Packit Service a1bd4f
     *
Packit Service a1bd4f
     * The devices enslaved to the team device.
Packit Service a1bd4f
     **/
Packit Service a1bd4f
    obj_properties[PROP_SLAVES] = g_param_spec_boxed(NM_DEVICE_TEAM_SLAVES,
Packit Service a1bd4f
                                                     "",
Packit Service a1bd4f
                                                     "",
Packit Service a1bd4f
                                                     G_TYPE_PTR_ARRAY,
Packit Service a1bd4f
                                                     G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
Packit Service a1bd4f
Packit Service a1bd4f
    /**
Packit Service a1bd4f
     * NMDeviceTeam:config:
Packit Service a1bd4f
     *
Packit Service a1bd4f
     * The current JSON configuration of the device.
Packit Service a1bd4f
     *
Packit Service a1bd4f
     * Since: 1.4
Packit Service a1bd4f
     **/
Packit Service a1bd4f
    obj_properties[PROP_CONFIG] = g_param_spec_string(NM_DEVICE_TEAM_CONFIG,
Packit Service a1bd4f
                                                      "",
Packit Service a1bd4f
                                                      "",
Packit Service a1bd4f
                                                      NULL,
Packit Service a1bd4f
                                                      G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
Packit Service a1bd4f
Packit Service a1bd4f
    _nml_dbus_meta_class_init_with_properties(object_class, &_nml_dbus_meta_iface_nm_device_team);
Packit 5756e2
}