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 2011 - 2012 Red Hat, Inc.
21 #include "nm-default.h"
24 #include <linux/if_infiniband.h>
25 #include <netinet/ether.h>
27 #include "nm-setting-connection.h"
28 #include "nm-setting-infiniband.h"
31 #include "nm-device-infiniband.h"
32 #include "nm-device-private.h"
33 #include "nm-object-private.h"
35 G_DEFINE_TYPE (NMDeviceInfiniband, nm_device_infiniband, NM_TYPE_DEVICE)
37 #define NM_DEVICE_INFINIBAND_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), NM_TYPE_DEVICE_INFINIBAND, NMDeviceInfinibandPrivate))
44 } NMDeviceInfinibandPrivate;
55 * nm_device_infiniband_error_quark:
57 * Registers an error quark for #NMDeviceInfiniband if necessary.
59 * Returns: the error quark used for #NMDeviceInfiniband errors.
62 nm_device_infiniband_error_quark (void)
64 static GQuark quark = 0;
66 if (G_UNLIKELY (quark == 0))
67 quark = g_quark_from_static_string ("nm-device-infiniband-error-quark");
72 * nm_device_infiniband_new:
73 * @connection: the #DBusGConnection
74 * @path: the DBus object path of the device
76 * Creates a new #NMDeviceInfiniband.
78 * Returns: (transfer full): a new device
81 nm_device_infiniband_new (DBusGConnection *connection, const char *path)
85 g_return_val_if_fail (connection != NULL, NULL);
86 g_return_val_if_fail (path != NULL, NULL);
88 device = g_object_new (NM_TYPE_DEVICE_INFINIBAND,
89 NM_OBJECT_DBUS_CONNECTION, connection,
90 NM_OBJECT_DBUS_PATH, path,
92 _nm_object_ensure_inited (NM_OBJECT (device));
97 * nm_device_infiniband_get_hw_address:
98 * @device: a #NMDeviceInfiniband
100 * Gets the hardware (MAC) address of the #NMDeviceInfiniband
102 * Returns: the hardware address. This is the internal string used by the
103 * device, and must not be modified.
106 nm_device_infiniband_get_hw_address (NMDeviceInfiniband *device)
108 g_return_val_if_fail (NM_IS_DEVICE_INFINIBAND (device), NULL);
110 _nm_object_ensure_inited (NM_OBJECT (device));
111 return NM_DEVICE_INFINIBAND_GET_PRIVATE (device)->hw_address;
115 * nm_device_infiniband_get_carrier:
116 * @device: a #NMDeviceInfiniband
118 * Whether the device has carrier.
120 * Returns: %TRUE if the device has carrier
123 nm_device_infiniband_get_carrier (NMDeviceInfiniband *device)
125 g_return_val_if_fail (NM_IS_DEVICE_INFINIBAND (device), FALSE);
127 _nm_object_ensure_inited (NM_OBJECT (device));
128 return NM_DEVICE_INFINIBAND_GET_PRIVATE (device)->carrier;
132 connection_compatible (NMDevice *device, NMConnection *connection, GError **error)
134 NMSettingConnection *s_con;
135 NMSettingInfiniband *s_infiniband;
136 const char *ctype, *hwaddr_str;
137 const GByteArray *mac;
138 guint8 *hwaddr, hwaddr_buf[INFINIBAND_ALEN];
140 s_con = nm_connection_get_setting_connection (connection);
143 ctype = nm_setting_connection_get_connection_type (s_con);
144 if (strcmp (ctype, NM_SETTING_INFINIBAND_SETTING_NAME) != 0) {
145 g_set_error (error, NM_DEVICE_INFINIBAND_ERROR, NM_DEVICE_INFINIBAND_ERROR_NOT_INFINIBAND_CONNECTION,
146 "The connection was not a InfiniBand connection.");
150 s_infiniband = nm_connection_get_setting_infiniband (connection);
152 g_set_error (error, NM_DEVICE_INFINIBAND_ERROR, NM_DEVICE_INFINIBAND_ERROR_INVALID_INFINIBAND_CONNECTION,
153 "The connection was not a valid InfiniBand connection.");
157 hwaddr_str = nm_device_infiniband_get_hw_address (NM_DEVICE_INFINIBAND (device));
159 hwaddr = nm_utils_hwaddr_aton (hwaddr_str, ARPHRD_INFINIBAND, hwaddr_buf);
161 g_set_error (error, NM_DEVICE_INFINIBAND_ERROR, NM_DEVICE_INFINIBAND_ERROR_INVALID_DEVICE_MAC,
162 "Invalid device MAC address.");
165 mac = nm_setting_infiniband_get_mac_address (s_infiniband);
167 /* We only match against the last 8 bytes */
168 if (mac && hwaddr && memcmp (mac->data + INFINIBAND_ALEN - 8, hwaddr + INFINIBAND_ALEN - 8, 8)) {
169 g_set_error (error, NM_DEVICE_INFINIBAND_ERROR, NM_DEVICE_INFINIBAND_ERROR_MAC_MISMATCH,
170 "The MACs of the device and the connection didn't match.");
175 return NM_DEVICE_CLASS (nm_device_infiniband_parent_class)->connection_compatible (device, connection, error);
179 get_setting_type (NMDevice *device)
181 return NM_TYPE_SETTING_INFINIBAND;
185 get_hw_address (NMDevice *device)
187 return nm_device_infiniband_get_hw_address (NM_DEVICE_INFINIBAND (device));
190 /***********************************************************/
193 nm_device_infiniband_init (NMDeviceInfiniband *device)
195 _nm_device_set_device_type (NM_DEVICE (device), NM_DEVICE_TYPE_INFINIBAND);
199 register_properties (NMDeviceInfiniband *device)
201 NMDeviceInfinibandPrivate *priv = NM_DEVICE_INFINIBAND_GET_PRIVATE (device);
202 const NMPropertiesInfo property_info[] = {
203 { NM_DEVICE_INFINIBAND_HW_ADDRESS, &priv->hw_address },
204 { NM_DEVICE_INFINIBAND_CARRIER, &priv->carrier },
208 _nm_object_register_properties (NM_OBJECT (device),
214 constructed (GObject *object)
216 NMDeviceInfinibandPrivate *priv = NM_DEVICE_INFINIBAND_GET_PRIVATE (object);
218 G_OBJECT_CLASS (nm_device_infiniband_parent_class)->constructed (object);
220 priv->proxy = _nm_object_new_proxy (NM_OBJECT (object), NULL, NM_DBUS_INTERFACE_DEVICE_INFINIBAND);
221 register_properties (NM_DEVICE_INFINIBAND (object));
225 dispose (GObject *object)
227 NMDeviceInfinibandPrivate *priv = NM_DEVICE_INFINIBAND_GET_PRIVATE (object);
229 g_clear_object (&priv->proxy);
231 G_OBJECT_CLASS (nm_device_infiniband_parent_class)->dispose (object);
235 finalize (GObject *object)
237 NMDeviceInfinibandPrivate *priv = NM_DEVICE_INFINIBAND_GET_PRIVATE (object);
239 g_free (priv->hw_address);
241 G_OBJECT_CLASS (nm_device_infiniband_parent_class)->finalize (object);
245 get_property (GObject *object,
250 NMDeviceInfiniband *device = NM_DEVICE_INFINIBAND (object);
252 _nm_object_ensure_inited (NM_OBJECT (object));
255 case PROP_HW_ADDRESS:
256 g_value_set_string (value, nm_device_infiniband_get_hw_address (device));
259 g_value_set_boolean (value, nm_device_infiniband_get_carrier (device));
262 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
268 nm_device_infiniband_class_init (NMDeviceInfinibandClass *ib_class)
270 GObjectClass *object_class = G_OBJECT_CLASS (ib_class);
271 NMDeviceClass *device_class = NM_DEVICE_CLASS (ib_class);
273 g_type_class_add_private (ib_class, sizeof (NMDeviceInfinibandPrivate));
275 /* virtual methods */
276 object_class->constructed = constructed;
277 object_class->dispose = dispose;
278 object_class->finalize = finalize;
279 object_class->get_property = get_property;
280 device_class->connection_compatible = connection_compatible;
281 device_class->get_setting_type = get_setting_type;
282 device_class->get_hw_address = get_hw_address;
287 * NMDeviceInfiniband:hw-address:
289 * The hardware (MAC) address of the device.
291 g_object_class_install_property
292 (object_class, PROP_HW_ADDRESS,
293 g_param_spec_string (NM_DEVICE_INFINIBAND_HW_ADDRESS, "", "",
296 G_PARAM_STATIC_STRINGS));
299 * NMDeviceInfiniband:carrier:
301 * Whether the device has carrier.
303 g_object_class_install_property
304 (object_class, PROP_CARRIER,
305 g_param_spec_boolean (NM_DEVICE_INFINIBAND_CARRIER, "", "",
308 G_PARAM_STATIC_STRINGS));