1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301 USA.
19 * Copyright 2011 - 2013 Red Hat, Inc.
22 #include "nm-default.h"
25 #include <dbus/dbus-glib.h>
26 #include <linux/if_infiniband.h>
28 #include "nm-setting-infiniband.h"
29 #include "nm-param-spec-specialized.h"
31 #include "nm-utils-private.h"
32 #include "nm-setting-private.h"
33 #include "nm-setting-connection.h"
36 * SECTION:nm-setting-infiniband
37 * @short_description: Describes connection properties for IP-over-InfiniBand networks
38 * @include: nm-setting-infiniband.h
40 * The #NMSettingInfiniband object is a #NMSetting subclass that describes properties
41 * necessary for connection to IP-over-InfiniBand networks.
45 * nm_setting_infiniband_error_quark:
47 * Registers an error quark for #NMSettingInfiniband if necessary.
49 * Returns: the error quark used for #NMSettingInfiniband errors.
52 nm_setting_infiniband_error_quark (void)
56 if (G_UNLIKELY (!quark))
57 quark = g_quark_from_static_string ("nm-setting-infiniband-error-quark");
61 G_DEFINE_TYPE_WITH_CODE (NMSettingInfiniband, nm_setting_infiniband, NM_TYPE_SETTING,
62 _nm_register_setting (NM_SETTING_INFINIBAND_SETTING_NAME,
65 NM_SETTING_INFINIBAND_ERROR))
66 NM_SETTING_REGISTER_TYPE (NM_TYPE_SETTING_INFINIBAND)
68 #define NM_SETTING_INFINIBAND_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), NM_TYPE_SETTING_INFINIBAND, NMSettingInfinibandPrivate))
71 GByteArray *mac_address;
75 char *parent, *virtual_iface_name;
76 } NMSettingInfinibandPrivate;
90 * nm_setting_infiniband_new:
92 * Creates a new #NMSettingInfiniband object with default values.
94 * Returns: (transfer full): the new empty #NMSettingInfiniband object
97 nm_setting_infiniband_new (void)
99 return (NMSetting *) g_object_new (NM_TYPE_SETTING_INFINIBAND, NULL);
103 * nm_setting_infiniband_get_mac_address:
104 * @setting: the #NMSettingInfiniband
106 * Returns: the #NMSettingInfiniband:mac-address property of the setting
109 nm_setting_infiniband_get_mac_address (NMSettingInfiniband *setting)
111 g_return_val_if_fail (NM_IS_SETTING_INFINIBAND (setting), NULL);
113 return NM_SETTING_INFINIBAND_GET_PRIVATE (setting)->mac_address;
117 * nm_setting_infiniband_get_mtu:
118 * @setting: the #NMSettingInfiniband
120 * Returns: the #NMSettingInfiniband:mtu property of the setting
123 nm_setting_infiniband_get_mtu (NMSettingInfiniband *setting)
125 g_return_val_if_fail (NM_IS_SETTING_INFINIBAND (setting), 0);
127 return NM_SETTING_INFINIBAND_GET_PRIVATE (setting)->mtu;
131 * nm_setting_infiniband_get_transport_mode:
132 * @setting: the #NMSettingInfiniband
134 * Returns the transport mode for this device. Either 'datagram' or
137 * Returns: the IPoIB transport mode
140 nm_setting_infiniband_get_transport_mode (NMSettingInfiniband *setting)
142 g_return_val_if_fail (NM_IS_SETTING_INFINIBAND (setting), NULL);
144 return NM_SETTING_INFINIBAND_GET_PRIVATE (setting)->transport_mode;
148 * nm_setting_infiniband_get_p_key:
149 * @setting: the #NMSettingInfiniband
151 * Returns the P_Key to use for this device. A value of -1 means to
152 * use the default P_Key (aka "the P_Key at index 0"). Otherwise it is
153 * a 16-bit unsigned integer.
155 * Returns: the IPoIB P_Key
158 nm_setting_infiniband_get_p_key (NMSettingInfiniband *setting)
160 g_return_val_if_fail (NM_IS_SETTING_INFINIBAND (setting), -1);
162 return NM_SETTING_INFINIBAND_GET_PRIVATE (setting)->p_key;
166 * nm_setting_infiniband_get_parent:
167 * @setting: the #NMSettingInfiniband
169 * Returns the parent interface name for this device, if set.
171 * Returns: the parent interface name
174 nm_setting_infiniband_get_parent (NMSettingInfiniband *setting)
176 g_return_val_if_fail (NM_IS_SETTING_INFINIBAND (setting), NULL);
178 return NM_SETTING_INFINIBAND_GET_PRIVATE (setting)->parent;
182 get_virtual_iface_name (NMSetting *setting)
184 NMSettingInfinibandPrivate *priv = NM_SETTING_INFINIBAND_GET_PRIVATE (setting);
186 if (priv->p_key == -1 || !priv->parent)
189 if (!priv->virtual_iface_name)
190 priv->virtual_iface_name = g_strdup_printf ("%s.%04x", priv->parent, priv->p_key);
192 return NM_SETTING_INFINIBAND_GET_PRIVATE (setting)->virtual_iface_name;
196 verify (NMSetting *setting, GSList *all_settings, GError **error)
198 NMSettingConnection *s_con;
199 NMSettingInfinibandPrivate *priv = NM_SETTING_INFINIBAND_GET_PRIVATE (setting);
201 if (priv->mac_address && priv->mac_address->len != INFINIBAND_ALEN) {
202 g_set_error_literal (error,
203 NM_SETTING_INFINIBAND_ERROR,
204 NM_SETTING_INFINIBAND_ERROR_INVALID_PROPERTY,
205 _("property is invalid"));
206 g_prefix_error (error, "%s.%s: ", NM_SETTING_INFINIBAND_SETTING_NAME, NM_SETTING_INFINIBAND_MAC_ADDRESS);
210 /* FIXME: verify() should not modify the setting, but return NORMALIZABLE success. */
211 if (!g_strcmp0 (priv->transport_mode, "datagram")) {
212 if (priv->mtu > 2044)
214 } else if (!g_strcmp0 (priv->transport_mode, "connected")) {
215 if (priv->mtu > 65520)
218 g_set_error_literal (error,
219 NM_SETTING_INFINIBAND_ERROR,
220 NM_SETTING_INFINIBAND_ERROR_INVALID_PROPERTY,
221 _("property is invalid"));
222 g_prefix_error (error, "%s.%s: ", NM_SETTING_INFINIBAND_SETTING_NAME, NM_SETTING_INFINIBAND_TRANSPORT_MODE);
227 if (!nm_utils_iface_valid_name (priv->parent)) {
228 g_set_error_literal (error,
229 NM_SETTING_INFINIBAND_ERROR,
230 NM_SETTING_INFINIBAND_ERROR_INVALID_PROPERTY,
231 _("not a valid interface name"));
232 g_prefix_error (error, "%s: ", NM_SETTING_INFINIBAND_PARENT);
235 if (priv->p_key == -1) {
236 g_set_error_literal (error,
237 NM_SETTING_INFINIBAND_ERROR,
238 NM_SETTING_INFINIBAND_ERROR_INVALID_PROPERTY,
239 _("Must specify a P_Key if specifying parent"));
240 g_prefix_error (error, "%s: ", NM_SETTING_INFINIBAND_PARENT);
244 if (priv->p_key != -1) {
245 if (!priv->mac_address && !priv->parent) {
246 g_set_error_literal (error,
247 NM_SETTING_INFINIBAND_ERROR,
248 NM_SETTING_INFINIBAND_ERROR_MISSING_PROPERTY,
249 _("InfiniBand P_Key connection did not specify parent interface name"));
250 g_prefix_error (error, "%s: ", NM_SETTING_INFINIBAND_PARENT);
255 s_con = NM_SETTING_CONNECTION (nm_setting_find_in_list (all_settings, NM_SETTING_CONNECTION_SETTING_NAME));
257 const char *interface_name = nm_setting_connection_get_interface_name (s_con);
261 else if (!nm_utils_iface_valid_name (interface_name)) {
262 /* report the error for NMSettingConnection:interface-name, because
263 * it's that property that is invalid -- although we currently verify()
264 * NMSettingInfiniband.
267 NM_SETTING_CONNECTION_ERROR,
268 NM_SETTING_CONNECTION_ERROR_INVALID_PROPERTY,
269 _("'%s' is not a valid interface name"),
271 g_prefix_error (error, "%s.%s: ", NM_SETTING_CONNECTION_SETTING_NAME, NM_SETTING_CONNECTION_INTERFACE_NAME);
274 if (priv->p_key != -1) {
275 if (!priv->virtual_iface_name)
276 priv->virtual_iface_name = g_strdup_printf ("%s.%04x", priv->parent, priv->p_key);
278 if (strcmp (interface_name, priv->virtual_iface_name) != 0) {
279 /* We don't support renaming software infiniband devices. Later we might, but
280 * for now just reject such connections.
283 NM_SETTING_CONNECTION_ERROR,
284 NM_SETTING_CONNECTION_ERROR_INVALID_PROPERTY,
285 _("interface name of software infiniband device must be '%s' or unset (instead it is '%s')"),
286 priv->virtual_iface_name, interface_name);
287 g_prefix_error (error, "%s.%s: ", NM_SETTING_CONNECTION_SETTING_NAME, NM_SETTING_CONNECTION_INTERFACE_NAME);
298 nm_setting_infiniband_init (NMSettingInfiniband *setting)
303 finalize (GObject *object)
305 NMSettingInfinibandPrivate *priv = NM_SETTING_INFINIBAND_GET_PRIVATE (object);
307 g_free (priv->transport_mode);
308 if (priv->mac_address)
309 g_byte_array_free (priv->mac_address, TRUE);
310 g_free (priv->parent);
311 g_free (priv->virtual_iface_name);
313 G_OBJECT_CLASS (nm_setting_infiniband_parent_class)->finalize (object);
317 set_property (GObject *object, guint prop_id,
318 const GValue *value, GParamSpec *pspec)
320 NMSettingInfinibandPrivate *priv = NM_SETTING_INFINIBAND_GET_PRIVATE (object);
323 case PROP_MAC_ADDRESS:
324 if (priv->mac_address)
325 g_byte_array_free (priv->mac_address, TRUE);
326 priv->mac_address = g_value_dup_boxed (value);
329 priv->mtu = g_value_get_uint (value);
331 case PROP_TRANSPORT_MODE:
332 g_free (priv->transport_mode);
333 priv->transport_mode = g_value_dup_string (value);
336 priv->p_key = g_value_get_int (value);
337 g_clear_pointer (&priv->virtual_iface_name, g_free);
340 g_free (priv->parent);
341 priv->parent = g_value_dup_string (value);
342 g_clear_pointer (&priv->virtual_iface_name, g_free);
345 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
351 get_property (GObject *object, guint prop_id,
352 GValue *value, GParamSpec *pspec)
354 NMSettingInfiniband *setting = NM_SETTING_INFINIBAND (object);
357 case PROP_MAC_ADDRESS:
358 g_value_set_boxed (value, nm_setting_infiniband_get_mac_address (setting));
361 g_value_set_uint (value, nm_setting_infiniband_get_mtu (setting));
363 case PROP_TRANSPORT_MODE:
364 g_value_set_string (value, nm_setting_infiniband_get_transport_mode (setting));
367 g_value_set_int (value, nm_setting_infiniband_get_p_key (setting));
370 g_value_set_string (value, nm_setting_infiniband_get_parent (setting));
373 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
379 nm_setting_infiniband_class_init (NMSettingInfinibandClass *setting_class)
381 GObjectClass *object_class = G_OBJECT_CLASS (setting_class);
382 NMSettingClass *parent_class = NM_SETTING_CLASS (setting_class);
384 g_type_class_add_private (setting_class, sizeof (NMSettingInfinibandPrivate));
386 /* virtual methods */
387 object_class->set_property = set_property;
388 object_class->get_property = get_property;
389 object_class->finalize = finalize;
391 parent_class->verify = verify;
392 parent_class->get_virtual_iface_name = get_virtual_iface_name;
396 * NMSettingInfiniband:mac-address:
398 * If specified, this connection will only apply to the IPoIB device whose
399 * permanent MAC address matches. This property does not change the MAC
400 * address of the device (i.e. MAC spoofing).
402 g_object_class_install_property
403 (object_class, PROP_MAC_ADDRESS,
404 _nm_param_spec_specialized (NM_SETTING_INFINIBAND_MAC_ADDRESS, "", "",
405 DBUS_TYPE_G_UCHAR_ARRAY,
407 NM_SETTING_PARAM_INFERRABLE |
408 G_PARAM_STATIC_STRINGS));
411 * NMSettingInfiniband:mtu:
413 * If non-zero, only transmit packets of the specified size or smaller,
414 * breaking larger packets up into multiple frames.
416 g_object_class_install_property
417 (object_class, PROP_MTU,
418 g_param_spec_uint (NM_SETTING_INFINIBAND_MTU, "", "",
422 NM_SETTING_PARAM_FUZZY_IGNORE |
423 G_PARAM_STATIC_STRINGS));
426 * NMSettingInfiniband:transport-mode:
428 * The IP-over-InfiniBand transport mode. Either "datagram" or
431 g_object_class_install_property
432 (object_class, PROP_TRANSPORT_MODE,
433 g_param_spec_string (NM_SETTING_INFINIBAND_TRANSPORT_MODE, "", "",
437 NM_SETTING_PARAM_INFERRABLE |
438 G_PARAM_STATIC_STRINGS));
441 * NMSettingInfiniband:p-key:
443 * The InfiniBand P_Key to use for this device. A value of -1 means to use
444 * the default P_Key (aka "the P_Key at index 0"). Otherwise it is a 16-bit
445 * unsigned integer, whose high bit is set if it is a "full membership"
448 g_object_class_install_property
449 (object_class, PROP_P_KEY,
450 g_param_spec_int (NM_SETTING_INFINIBAND_P_KEY, "", "",
454 NM_SETTING_PARAM_INFERRABLE |
455 G_PARAM_STATIC_STRINGS));
458 * NMSettingInfiniband:parent:
460 * The interface name of the parent device of this device. Normally %NULL,
461 * but if the #NMSettingInfiniband:p_key property is set, then you must
462 * specify the base device by setting either this property or
463 * #NMSettingInfiniband:mac-address.
465 g_object_class_install_property
466 (object_class, PROP_PARENT,
467 g_param_spec_string (NM_SETTING_INFINIBAND_PARENT, "", "",
471 NM_SETTING_PARAM_INFERRABLE |
472 G_PARAM_STATIC_STRINGS));