1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
3 * This library is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU Lesser General Public
5 * License as published by the Free Software Foundation; either
6 * version 2 of the License, or (at your option) any later version.
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.
13 * You should have received a copy of the GNU Lesser General Public
14 * License along with this library; if not, write to the
15 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
16 * Boston, MA 02110-1301 USA.
18 * Copyright 2013 Jiri Pirko <jiri@resnulli.us>
21 #include "nm-default.h"
24 #include <netinet/ether.h>
26 #include "nm-setting-connection.h"
27 #include "nm-setting-team.h"
30 #include "nm-device-team.h"
31 #include "nm-device-private.h"
32 #include "nm-object-private.h"
35 G_DEFINE_TYPE (NMDeviceTeam, nm_device_team, NM_TYPE_DEVICE)
37 #define NM_DEVICE_TEAM_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), NM_TYPE_DEVICE_TEAM, NMDeviceTeamPrivate))
45 } NMDeviceTeamPrivate;
57 * nm_device_team_error_quark:
59 * Registers an error quark for #NMDeviceTeam if necessary.
61 * Returns: the error quark used for #NMDeviceTeam errors.
66 nm_device_team_error_quark (void)
68 static GQuark quark = 0;
70 if (G_UNLIKELY (quark == 0))
71 quark = g_quark_from_static_string ("nm-device-team-error-quark");
77 * @connection: the #DBusGConnection
78 * @path: the DBus object path of the device
80 * Creates a new #NMDeviceTeam.
82 * Returns: (transfer full): a new device
87 nm_device_team_new (DBusGConnection *connection, const char *path)
91 g_return_val_if_fail (connection != NULL, NULL);
92 g_return_val_if_fail (path != NULL, NULL);
94 device = g_object_new (NM_TYPE_DEVICE_TEAM,
95 NM_OBJECT_DBUS_CONNECTION, connection,
96 NM_OBJECT_DBUS_PATH, path,
98 _nm_object_ensure_inited (NM_OBJECT (device));
103 * nm_device_team_get_hw_address:
104 * @device: a #NMDeviceTeam
106 * Gets the hardware (MAC) address of the #NMDeviceTeam
108 * Returns: the hardware address. This is the internal string used by the
109 * device, and must not be modified.
114 nm_device_team_get_hw_address (NMDeviceTeam *device)
116 g_return_val_if_fail (NM_IS_DEVICE_TEAM (device), NULL);
118 _nm_object_ensure_inited (NM_OBJECT (device));
119 return NM_DEVICE_TEAM_GET_PRIVATE (device)->hw_address;
123 * nm_device_team_get_carrier:
124 * @device: a #NMDeviceTeam
126 * Whether the device has carrier.
128 * Returns: %TRUE if the device has carrier
133 nm_device_team_get_carrier (NMDeviceTeam *device)
135 g_return_val_if_fail (NM_IS_DEVICE_TEAM (device), FALSE);
137 _nm_object_ensure_inited (NM_OBJECT (device));
138 return NM_DEVICE_TEAM_GET_PRIVATE (device)->carrier;
142 * nm_device_team_get_slaves:
143 * @device: a #NMDeviceTeam
145 * Gets the devices currently enslaved to @device.
147 * Returns: (element-type NMDevice): the #GPtrArray containing
148 * #NMDevices that are slaves of @device. This is the internal
149 * copy used by the device, and must not be modified.
154 nm_device_team_get_slaves (NMDeviceTeam *device)
156 g_return_val_if_fail (NM_IS_DEVICE_TEAM (device), FALSE);
158 _nm_object_ensure_inited (NM_OBJECT (device));
159 return handle_ptr_array_return (NM_DEVICE_TEAM_GET_PRIVATE (device)->slaves);
163 get_hw_address (NMDevice *device)
165 return nm_device_team_get_hw_address (NM_DEVICE_TEAM (device));
169 connection_compatible (NMDevice *device, NMConnection *connection, GError **error)
171 NMSettingConnection *s_con;
172 NMSettingTeam *s_team;
173 const char *ctype, *dev_iface_name, *team_iface_name;
175 s_con = nm_connection_get_setting_connection (connection);
178 ctype = nm_setting_connection_get_connection_type (s_con);
179 if (strcmp (ctype, NM_SETTING_TEAM_SETTING_NAME) != 0) {
180 g_set_error (error, NM_DEVICE_TEAM_ERROR, NM_DEVICE_TEAM_ERROR_NOT_TEAM_CONNECTION,
181 "The connection was not a team connection.");
185 s_team = nm_connection_get_setting_team (connection);
187 g_set_error (error, NM_DEVICE_TEAM_ERROR, NM_DEVICE_TEAM_ERROR_INVALID_TEAM_CONNECTION,
188 "The connection was not a valid team connection.");
192 dev_iface_name = nm_device_get_iface (device);
193 team_iface_name = nm_setting_team_get_interface_name (s_team);
194 if (g_strcmp0 (dev_iface_name, team_iface_name) != 0) {
195 g_set_error (error, NM_DEVICE_TEAM_ERROR, NM_DEVICE_TEAM_ERROR_INTERFACE_MISMATCH,
196 "The interfaces of the device and the connection didn't match.");
200 /* FIXME: check slaves? */
202 return NM_DEVICE_CLASS (nm_device_team_parent_class)->connection_compatible (device, connection, error);
206 get_setting_type (NMDevice *device)
208 return NM_TYPE_SETTING_TEAM;
211 /***********************************************************/
214 nm_device_team_init (NMDeviceTeam *device)
216 _nm_device_set_device_type (NM_DEVICE (device), NM_DEVICE_TYPE_TEAM);
220 register_properties (NMDeviceTeam *device)
222 NMDeviceTeamPrivate *priv = NM_DEVICE_TEAM_GET_PRIVATE (device);
223 const NMPropertiesInfo property_info[] = {
224 { NM_DEVICE_TEAM_HW_ADDRESS, &priv->hw_address },
225 { NM_DEVICE_TEAM_CARRIER, &priv->carrier },
226 { NM_DEVICE_TEAM_SLAVES, &priv->slaves, NULL, NM_TYPE_DEVICE },
230 _nm_object_register_properties (NM_OBJECT (device),
236 constructed (GObject *object)
238 NMDeviceTeamPrivate *priv = NM_DEVICE_TEAM_GET_PRIVATE (object);
240 G_OBJECT_CLASS (nm_device_team_parent_class)->constructed (object);
242 priv->proxy = _nm_object_new_proxy (NM_OBJECT (object), NULL, NM_DBUS_INTERFACE_DEVICE_TEAM);
243 register_properties (NM_DEVICE_TEAM (object));
247 dispose (GObject *object)
249 NMDeviceTeamPrivate *priv = NM_DEVICE_TEAM_GET_PRIVATE (object);
251 g_clear_object (&priv->proxy);
254 g_ptr_array_set_free_func (priv->slaves, g_object_unref);
255 g_ptr_array_free (priv->slaves, TRUE);
259 G_OBJECT_CLASS (nm_device_team_parent_class)->dispose (object);
263 finalize (GObject *object)
265 NMDeviceTeamPrivate *priv = NM_DEVICE_TEAM_GET_PRIVATE (object);
267 g_free (priv->hw_address);
269 G_OBJECT_CLASS (nm_device_team_parent_class)->finalize (object);
273 get_property (GObject *object,
278 NMDeviceTeam *device = NM_DEVICE_TEAM (object);
280 _nm_object_ensure_inited (NM_OBJECT (object));
283 case PROP_HW_ADDRESS:
284 g_value_set_string (value, nm_device_team_get_hw_address (device));
287 g_value_set_boolean (value, nm_device_team_get_carrier (device));
290 g_value_set_boxed (value, nm_device_team_get_slaves (device));
293 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
299 nm_device_team_class_init (NMDeviceTeamClass *team_class)
301 GObjectClass *object_class = G_OBJECT_CLASS (team_class);
302 NMDeviceClass *device_class = NM_DEVICE_CLASS (team_class);
304 g_type_class_add_private (team_class, sizeof (NMDeviceTeamPrivate));
306 /* virtual methods */
307 object_class->constructed = constructed;
308 object_class->dispose = dispose;
309 object_class->finalize = finalize;
310 object_class->get_property = get_property;
311 device_class->connection_compatible = connection_compatible;
312 device_class->get_setting_type = get_setting_type;
313 device_class->get_hw_address = get_hw_address;
318 * NMDeviceTeam:hw-address:
320 * The hardware (MAC) address of the device.
322 g_object_class_install_property
323 (object_class, PROP_HW_ADDRESS,
324 g_param_spec_string (NM_DEVICE_TEAM_HW_ADDRESS, "", "",
327 G_PARAM_STATIC_STRINGS));
330 * NMDeviceTeam:carrier:
332 * Whether the device has carrier.
334 g_object_class_install_property
335 (object_class, PROP_CARRIER,
336 g_param_spec_boolean (NM_DEVICE_TEAM_CARRIER, "", "",
339 G_PARAM_STATIC_STRINGS));
342 * NMDeviceTeam:slaves:
344 * The devices (#NMDevice) enslaved to the team device.
346 g_object_class_install_property
347 (object_class, PROP_SLAVES,
348 g_param_spec_boxed (NM_DEVICE_TEAM_SLAVES, "", "",
349 NM_TYPE_OBJECT_ARRAY,
351 G_PARAM_STATIC_STRINGS));