device: renew dhcp leases on awake for software devices
[NetworkManager.git] / libnm-util / nm-setting-infiniband.c
1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
2
3 /*
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.
8  *
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.
13  *
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.
18  *
19  * Copyright 2011 - 2013 Red Hat, Inc.
20  */
21
22 #include "nm-default.h"
23
24 #include <stdlib.h>
25 #include <dbus/dbus-glib.h>
26 #include <linux/if_infiniband.h>
27
28 #include "nm-setting-infiniband.h"
29 #include "nm-param-spec-specialized.h"
30 #include "nm-utils.h"
31 #include "nm-utils-private.h"
32 #include "nm-setting-private.h"
33 #include "nm-setting-connection.h"
34
35 /**
36  * SECTION:nm-setting-infiniband
37  * @short_description: Describes connection properties for IP-over-InfiniBand networks
38  * @include: nm-setting-infiniband.h
39  *
40  * The #NMSettingInfiniband object is a #NMSetting subclass that describes properties
41  * necessary for connection to IP-over-InfiniBand networks.
42  **/
43
44 /**
45  * nm_setting_infiniband_error_quark:
46  *
47  * Registers an error quark for #NMSettingInfiniband if necessary.
48  *
49  * Returns: the error quark used for #NMSettingInfiniband errors.
50  **/
51 GQuark
52 nm_setting_infiniband_error_quark (void)
53 {
54         static GQuark quark;
55
56         if (G_UNLIKELY (!quark))
57                 quark = g_quark_from_static_string ("nm-setting-infiniband-error-quark");
58         return quark;
59 }
60
61 G_DEFINE_TYPE_WITH_CODE (NMSettingInfiniband, nm_setting_infiniband, NM_TYPE_SETTING,
62                          _nm_register_setting (NM_SETTING_INFINIBAND_SETTING_NAME,
63                                                g_define_type_id,
64                                                1,
65                                                NM_SETTING_INFINIBAND_ERROR))
66 NM_SETTING_REGISTER_TYPE (NM_TYPE_SETTING_INFINIBAND)
67
68 #define NM_SETTING_INFINIBAND_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), NM_TYPE_SETTING_INFINIBAND, NMSettingInfinibandPrivate))
69
70 typedef struct {
71         GByteArray *mac_address;
72         char *transport_mode;
73         guint32 mtu;
74         int p_key;
75         char *parent, *virtual_iface_name;
76 } NMSettingInfinibandPrivate;
77
78 enum {
79         PROP_0,
80         PROP_MAC_ADDRESS,
81         PROP_MTU,
82         PROP_TRANSPORT_MODE,
83         PROP_P_KEY,
84         PROP_PARENT,
85
86         LAST_PROP
87 };
88
89 /**
90  * nm_setting_infiniband_new:
91  *
92  * Creates a new #NMSettingInfiniband object with default values.
93  *
94  * Returns: (transfer full): the new empty #NMSettingInfiniband object
95  **/
96 NMSetting *
97 nm_setting_infiniband_new (void)
98 {
99         return (NMSetting *) g_object_new (NM_TYPE_SETTING_INFINIBAND, NULL);
100 }
101
102 /**
103  * nm_setting_infiniband_get_mac_address:
104  * @setting: the #NMSettingInfiniband
105  *
106  * Returns: the #NMSettingInfiniband:mac-address property of the setting
107  **/
108 const GByteArray *
109 nm_setting_infiniband_get_mac_address (NMSettingInfiniband *setting)
110 {
111         g_return_val_if_fail (NM_IS_SETTING_INFINIBAND (setting), NULL);
112
113         return NM_SETTING_INFINIBAND_GET_PRIVATE (setting)->mac_address;
114 }
115
116 /**
117  * nm_setting_infiniband_get_mtu:
118  * @setting: the #NMSettingInfiniband
119  *
120  * Returns: the #NMSettingInfiniband:mtu property of the setting
121  **/
122 guint32
123 nm_setting_infiniband_get_mtu (NMSettingInfiniband *setting)
124 {
125         g_return_val_if_fail (NM_IS_SETTING_INFINIBAND (setting), 0);
126
127         return NM_SETTING_INFINIBAND_GET_PRIVATE (setting)->mtu;
128 }
129
130 /**
131  * nm_setting_infiniband_get_transport_mode:
132  * @setting: the #NMSettingInfiniband
133  *
134  * Returns the transport mode for this device. Either 'datagram' or
135  * 'connected'.
136  *
137  * Returns: the IPoIB transport mode
138  **/
139 const char *
140 nm_setting_infiniband_get_transport_mode (NMSettingInfiniband *setting)
141 {
142         g_return_val_if_fail (NM_IS_SETTING_INFINIBAND (setting), NULL);
143
144         return NM_SETTING_INFINIBAND_GET_PRIVATE (setting)->transport_mode;
145 }
146
147 /**
148  * nm_setting_infiniband_get_p_key:
149  * @setting: the #NMSettingInfiniband
150  *
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.
154  *
155  * Returns: the IPoIB P_Key
156  **/
157 int
158 nm_setting_infiniband_get_p_key (NMSettingInfiniband *setting)
159 {
160         g_return_val_if_fail (NM_IS_SETTING_INFINIBAND (setting), -1);
161
162         return NM_SETTING_INFINIBAND_GET_PRIVATE (setting)->p_key;
163 }
164
165 /**
166  * nm_setting_infiniband_get_parent:
167  * @setting: the #NMSettingInfiniband
168  *
169  * Returns the parent interface name for this device, if set.
170  *
171  * Returns: the parent interface name
172  **/
173 const char *
174 nm_setting_infiniband_get_parent (NMSettingInfiniband *setting)
175 {
176         g_return_val_if_fail (NM_IS_SETTING_INFINIBAND (setting), NULL);
177
178         return NM_SETTING_INFINIBAND_GET_PRIVATE (setting)->parent;
179 }
180
181 static const char *
182 get_virtual_iface_name (NMSetting *setting)
183 {
184         NMSettingInfinibandPrivate *priv = NM_SETTING_INFINIBAND_GET_PRIVATE (setting);
185
186         if (priv->p_key == -1 || !priv->parent)
187                 return NULL;
188
189         if (!priv->virtual_iface_name)
190                 priv->virtual_iface_name = g_strdup_printf ("%s.%04x", priv->parent, priv->p_key);
191
192         return NM_SETTING_INFINIBAND_GET_PRIVATE (setting)->virtual_iface_name;
193 }
194
195 static gboolean
196 verify (NMSetting *setting, GSList *all_settings, GError **error)
197 {
198         NMSettingConnection *s_con;
199         NMSettingInfinibandPrivate *priv = NM_SETTING_INFINIBAND_GET_PRIVATE (setting);
200
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);
207                 return FALSE;
208         }
209
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)
213                         priv->mtu = 2044;
214         } else if (!g_strcmp0 (priv->transport_mode, "connected")) {
215                 if (priv->mtu > 65520)
216                         priv->mtu = 65520;
217         } else {
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);
223                 return FALSE;
224         }
225
226         if (priv->parent) {
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);
233                         return FALSE;
234                 }
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);
241                 }
242         }
243
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);
251                         return FALSE;
252                 }
253         }
254
255         s_con = NM_SETTING_CONNECTION (nm_setting_find_in_list (all_settings, NM_SETTING_CONNECTION_SETTING_NAME));
256         if (s_con) {
257                 const char *interface_name = nm_setting_connection_get_interface_name (s_con);
258
259                 if (!interface_name)
260                         ;
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.
265                          **/
266                         g_set_error (error,
267                                      NM_SETTING_CONNECTION_ERROR,
268                                      NM_SETTING_CONNECTION_ERROR_INVALID_PROPERTY,
269                                      _("'%s' is not a valid interface name"),
270                                      interface_name);
271                         g_prefix_error (error, "%s.%s: ", NM_SETTING_CONNECTION_SETTING_NAME, NM_SETTING_CONNECTION_INTERFACE_NAME);
272                         return FALSE;
273                 } else {
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);
277
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.
281                                          **/
282                                         g_set_error (error,
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);
288                                         return FALSE;
289                                 }
290                         }
291                 }
292         }
293
294         return TRUE;
295 }
296
297 static void
298 nm_setting_infiniband_init (NMSettingInfiniband *setting)
299 {
300 }
301
302 static void
303 finalize (GObject *object)
304 {
305         NMSettingInfinibandPrivate *priv = NM_SETTING_INFINIBAND_GET_PRIVATE (object);
306
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);
312
313         G_OBJECT_CLASS (nm_setting_infiniband_parent_class)->finalize (object);
314 }
315
316 static void
317 set_property (GObject *object, guint prop_id,
318               const GValue *value, GParamSpec *pspec)
319 {
320         NMSettingInfinibandPrivate *priv = NM_SETTING_INFINIBAND_GET_PRIVATE (object);
321
322         switch (prop_id) {
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);
327                 break;
328         case PROP_MTU:
329                 priv->mtu = g_value_get_uint (value);
330                 break;
331         case PROP_TRANSPORT_MODE:
332                 g_free (priv->transport_mode);
333                 priv->transport_mode = g_value_dup_string (value);
334                 break;
335         case PROP_P_KEY:
336                 priv->p_key = g_value_get_int (value);
337                 g_clear_pointer (&priv->virtual_iface_name, g_free);
338                 break;
339         case PROP_PARENT:
340                 g_free (priv->parent);
341                 priv->parent = g_value_dup_string (value);
342                 g_clear_pointer (&priv->virtual_iface_name, g_free);
343                 break;
344         default:
345                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
346                 break;
347         }
348 }
349
350 static void
351 get_property (GObject *object, guint prop_id,
352               GValue *value, GParamSpec *pspec)
353 {
354         NMSettingInfiniband *setting = NM_SETTING_INFINIBAND (object);
355
356         switch (prop_id) {
357         case PROP_MAC_ADDRESS:
358                 g_value_set_boxed (value, nm_setting_infiniband_get_mac_address (setting));
359                 break;
360         case PROP_MTU:
361                 g_value_set_uint (value, nm_setting_infiniband_get_mtu (setting));
362                 break;
363         case PROP_TRANSPORT_MODE:
364                 g_value_set_string (value, nm_setting_infiniband_get_transport_mode (setting));
365                 break;
366         case PROP_P_KEY:
367                 g_value_set_int (value, nm_setting_infiniband_get_p_key (setting));
368                 break;
369         case PROP_PARENT:
370                 g_value_set_string (value, nm_setting_infiniband_get_parent (setting));
371                 break;
372         default:
373                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
374                 break;
375         }
376 }
377
378 static void
379 nm_setting_infiniband_class_init (NMSettingInfinibandClass *setting_class)
380 {
381         GObjectClass *object_class = G_OBJECT_CLASS (setting_class);
382         NMSettingClass *parent_class = NM_SETTING_CLASS (setting_class);
383
384         g_type_class_add_private (setting_class, sizeof (NMSettingInfinibandPrivate));
385
386         /* virtual methods */
387         object_class->set_property = set_property;
388         object_class->get_property = get_property;
389         object_class->finalize     = finalize;
390
391         parent_class->verify                 = verify;
392         parent_class->get_virtual_iface_name = get_virtual_iface_name;
393
394         /* Properties */
395         /**
396          * NMSettingInfiniband:mac-address:
397          *
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).
401          **/
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,
406                                              G_PARAM_READWRITE |
407                                              NM_SETTING_PARAM_INFERRABLE |
408                                              G_PARAM_STATIC_STRINGS));
409
410         /**
411          * NMSettingInfiniband:mtu:
412          *
413          * If non-zero, only transmit packets of the specified size or smaller,
414          * breaking larger packets up into multiple frames.
415          **/
416         g_object_class_install_property
417                 (object_class, PROP_MTU,
418                  g_param_spec_uint (NM_SETTING_INFINIBAND_MTU, "", "",
419                                     0, G_MAXUINT32, 0,
420                                     G_PARAM_READWRITE |
421                                     G_PARAM_CONSTRUCT |
422                                     NM_SETTING_PARAM_FUZZY_IGNORE |
423                                     G_PARAM_STATIC_STRINGS));
424
425         /**
426          * NMSettingInfiniband:transport-mode:
427          *
428          * The IP-over-InfiniBand transport mode. Either "datagram" or
429          * "connected".
430          **/
431         g_object_class_install_property
432                 (object_class, PROP_TRANSPORT_MODE,
433                  g_param_spec_string (NM_SETTING_INFINIBAND_TRANSPORT_MODE, "", "",
434                                       NULL,
435                                       G_PARAM_READWRITE |
436                                       G_PARAM_CONSTRUCT |
437                                       NM_SETTING_PARAM_INFERRABLE |
438                                       G_PARAM_STATIC_STRINGS));
439
440         /**
441          * NMSettingInfiniband:p-key:
442          *
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"
446          * P_Key.
447          **/
448         g_object_class_install_property
449                 (object_class, PROP_P_KEY,
450                  g_param_spec_int (NM_SETTING_INFINIBAND_P_KEY, "", "",
451                                    -1, 0xFFFF, -1,
452                                    G_PARAM_READWRITE |
453                                    G_PARAM_CONSTRUCT |
454                                    NM_SETTING_PARAM_INFERRABLE |
455                                    G_PARAM_STATIC_STRINGS));
456
457         /**
458          * NMSettingInfiniband:parent:
459          *
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.
464          **/
465         g_object_class_install_property
466                 (object_class, PROP_PARENT,
467                  g_param_spec_string (NM_SETTING_INFINIBAND_PARENT, "", "",
468                                       NULL,
469                                       G_PARAM_READWRITE |
470                                       G_PARAM_CONSTRUCT |
471                                       NM_SETTING_PARAM_INFERRABLE |
472                                       G_PARAM_STATIC_STRINGS));
473
474 }