bond: fix re-assuming of connections
[NetworkManager.git] / src / devices / nm-device-bond.c
1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
2 /* NetworkManager -- Network link manager
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program 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
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17  *
18  * Copyright 2011 - 2012 Red Hat, Inc.
19  */
20
21 #include "nm-default.h"
22
23 #include <errno.h>
24 #include <stdlib.h>
25
26 #include "nm-device-bond.h"
27 #include "NetworkManagerUtils.h"
28 #include "nm-device-private.h"
29 #include "nm-platform.h"
30 #include "nm-enum-types.h"
31 #include "nm-device-factory.h"
32 #include "nm-core-internal.h"
33 #include "nm-ip4-config.h"
34
35 #include "nmdbus-device-bond.h"
36
37 #include "nm-device-logging.h"
38 _LOG_DECLARE_SELF(NMDeviceBond);
39
40 G_DEFINE_TYPE (NMDeviceBond, nm_device_bond, NM_TYPE_DEVICE)
41
42 #define NM_DEVICE_BOND_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), NM_TYPE_DEVICE_BOND, NMDeviceBondPrivate))
43
44 typedef struct {
45         int dummy;
46 } NMDeviceBondPrivate;
47
48 /******************************************************************/
49
50 static NMDeviceCapabilities
51 get_generic_capabilities (NMDevice *dev)
52 {
53         return NM_DEVICE_CAP_CARRIER_DETECT | NM_DEVICE_CAP_IS_SOFTWARE;
54 }
55
56 static gboolean
57 is_available (NMDevice *dev, NMDeviceCheckDevAvailableFlags flags)
58 {
59         return TRUE;
60 }
61
62 static gboolean
63 check_connection_available (NMDevice *device,
64                             NMConnection *connection,
65                             NMDeviceCheckConAvailableFlags flags,
66                             const char *specific_object)
67 {
68         /* Connections are always available because the carrier state is determined
69          * by the slave carrier states, not the bonds's state.
70          */
71         return TRUE;
72 }
73
74 static gboolean
75 check_connection_compatible (NMDevice *device, NMConnection *connection)
76 {
77         NMSettingBond *s_bond;
78
79         if (!NM_DEVICE_CLASS (nm_device_bond_parent_class)->check_connection_compatible (device, connection))
80                 return FALSE;
81
82         s_bond = nm_connection_get_setting_bond (connection);
83         if (!s_bond || !nm_connection_is_type (connection, NM_SETTING_BOND_SETTING_NAME))
84                 return FALSE;
85
86         /* FIXME: match bond properties like mode, etc? */
87
88         return TRUE;
89 }
90
91 static gboolean
92 complete_connection (NMDevice *device,
93                      NMConnection *connection,
94                      const char *specific_object,
95                      const GSList *existing_connections,
96                      GError **error)
97 {
98         NMSettingBond *s_bond;
99
100         nm_utils_complete_generic (NM_PLATFORM_GET,
101                                    connection,
102                                    NM_SETTING_BOND_SETTING_NAME,
103                                    existing_connections,
104                                    NULL,
105                                    _("Bond connection"),
106                                    "bond",
107                                    TRUE);
108
109         s_bond = nm_connection_get_setting_bond (connection);
110         if (!s_bond) {
111                 s_bond = (NMSettingBond *) nm_setting_bond_new ();
112                 nm_connection_add_setting (connection, NM_SETTING (s_bond));
113         }
114
115         return TRUE;
116 }
117
118 /******************************************************************/
119
120 static gboolean
121 set_bond_attr (NMDevice *device, const char *attr, const char *value)
122 {
123         NMDeviceBond *self = NM_DEVICE_BOND (device);
124         gboolean ret;
125         int ifindex = nm_device_get_ifindex (device);
126
127         ret = nm_platform_sysctl_master_set_option (NM_PLATFORM_GET, ifindex, attr, value);
128         if (!ret)
129                 _LOGW (LOGD_HW, "failed to set bonding attribute '%s' to '%s'", attr, value);
130         return ret;
131 }
132
133 /* Ignore certain bond options if they are zero (off/disabled) */
134 static gboolean
135 ignore_if_zero (const char *option, const char *value)
136 {
137         if (strcmp (option, "arp_interval") &&
138             strcmp (option, "miimon") &&
139             strcmp (option, "downdelay") &&
140             strcmp (option, "updelay"))
141                 return FALSE;
142
143         return g_strcmp0 (value, "0") == 0 ? TRUE : FALSE;
144 }
145
146 static void
147 update_connection (NMDevice *device, NMConnection *connection)
148 {
149         NMSettingBond *s_bond = nm_connection_get_setting_bond (connection);
150         int ifindex = nm_device_get_ifindex (device);
151         const char **options;
152
153         if (!s_bond) {
154                 s_bond = (NMSettingBond *) nm_setting_bond_new ();
155                 nm_connection_add_setting (connection, (NMSetting *) s_bond);
156         }
157
158         /* Read bond options from sysfs and update the Bond setting to match */
159         options = nm_setting_bond_get_valid_options (s_bond);
160         while (options && *options) {
161                 gs_free char *value = nm_platform_sysctl_master_get_option (NM_PLATFORM_GET, ifindex, *options);
162                 const char *defvalue = nm_setting_bond_get_option_default (s_bond, *options);
163                 char *p;
164
165                 if (_nm_setting_bond_get_option_type (s_bond, *options) == NM_BOND_OPTION_TYPE_BOTH) {
166                         p = strchr (value, ' ');
167                         if (p)
168                                 *p = '\0';
169                 }
170
171                 if (   value
172                     && value[0]
173                     && !ignore_if_zero (*options, value)
174                     && !nm_streq0 (value, defvalue)) {
175                         /* Replace " " with "," for arp_ip_targets from the kernel */
176                         if (strcmp (*options, "arp_ip_target") == 0) {
177                                 for (p = value; *p; p++) {
178                                         if (*p == ' ')
179                                                 *p = ',';
180                                 }
181                         }
182
183                         nm_setting_bond_add_option (s_bond, *options, value);
184                 }
185                 options++;
186         }
187 }
188
189 static gboolean
190 master_update_slave_connection (NMDevice *self,
191                                 NMDevice *slave,
192                                 NMConnection *connection,
193                                 GError **error)
194 {
195         g_object_set (nm_connection_get_setting_connection (connection),
196                       NM_SETTING_CONNECTION_MASTER, nm_device_get_iface (self),
197                       NM_SETTING_CONNECTION_SLAVE_TYPE, NM_SETTING_BOND_SETTING_NAME,
198                       NULL);
199         return TRUE;
200 }
201
202 static void
203 set_arp_targets (NMDevice *device,
204                  const char *value,
205                  const char *delim,
206                  const char *prefix)
207 {
208         char **items, **iter, *tmp;
209
210         if (!value || !*value)
211                 return;
212
213         items = g_strsplit_set (value, delim, 0);
214         for (iter = items; iter && *iter; iter++) {
215                 if (*iter[0]) {
216                         tmp = g_strdup_printf ("%s%s", prefix, *iter);
217                         set_bond_attr (device, "arp_ip_target", tmp);
218                         g_free (tmp);
219                 }
220         }
221         g_strfreev (items);
222 }
223
224 static void
225 set_simple_option (NMDevice *device,
226                    const char *attr,
227                    NMSettingBond *s_bond,
228                    const char *opt)
229 {
230         const char *value;
231
232         value = nm_setting_bond_get_option_by_name (s_bond, opt);
233         if (!value)
234                 value = nm_setting_bond_get_option_default (s_bond, opt);
235         set_bond_attr (device, attr, value);
236 }
237
238 static NMActStageReturn
239 apply_bonding_config (NMDevice *device)
240 {
241         NMConnection *connection;
242         NMSettingBond *s_bond;
243         int ifindex = nm_device_get_ifindex (device);
244         const char *mode, *value;
245         char *contents;
246         gboolean set_arp_interval = TRUE;
247
248         /* Option restrictions:
249          *
250          * arp_interval conflicts miimon > 0
251          * arp_interval conflicts [ alb, tlb ]
252          * arp_validate needs [ active-backup ]
253          * downdelay needs miimon
254          * updelay needs miimon
255          * primary needs [ active-backup, tlb, alb ]
256          *
257          * clearing miimon requires that arp_interval be 0, but clearing
258          *     arp_interval doesn't require miimon to be 0
259          */
260
261         connection = nm_device_get_applied_connection (device);
262         g_assert (connection);
263         s_bond = nm_connection_get_setting_bond (connection);
264         g_assert (s_bond);
265
266         mode = nm_setting_bond_get_option_by_name (s_bond, NM_SETTING_BOND_OPTION_MODE);
267         if (mode == NULL)
268                 mode = "balance-rr";
269
270         value = nm_setting_bond_get_option_by_name (s_bond, NM_SETTING_BOND_OPTION_MIIMON);
271         if (value && atoi (value)) {
272                 /* clear arp interval */
273                 set_bond_attr (device, "arp_interval", "0");
274                 set_arp_interval = FALSE;
275
276                 set_bond_attr (device, "miimon", value);
277                 set_simple_option (device, "updelay", s_bond, NM_SETTING_BOND_OPTION_UPDELAY);
278                 set_simple_option (device, "downdelay", s_bond, NM_SETTING_BOND_OPTION_DOWNDELAY);
279         } else if (!value) {
280                 /* If not given, and arp_interval is not given, default to 100 */
281                 long int val_int;
282                 char *end;
283
284                 value = nm_setting_bond_get_option_by_name (s_bond, NM_SETTING_BOND_OPTION_ARP_INTERVAL);
285                 errno = 0;
286                 val_int = strtol (value ? value : "0", &end, 10);
287                 if (!value || (val_int == 0 && errno == 0 && *end == '\0'))
288                         set_bond_attr (device, "miimon", "100");
289         }
290
291         /* The stuff after 'mode' requires the given mode or doesn't care */
292         set_bond_attr (device, "mode", mode);
293
294         /* arp_interval not compatible with ALB, TLB */
295         if (g_strcmp0 (mode, "balance-alb") == 0 || g_strcmp0 (mode, "balance-tlb") == 0)
296                 set_arp_interval = FALSE;
297
298         if (set_arp_interval) {
299                 set_simple_option (device, "arp_interval", s_bond, NM_SETTING_BOND_OPTION_ARP_INTERVAL);
300
301                 /* Just let miimon get cleared automatically; even setting miimon to
302                  * 0 (disabled) clears arp_interval.
303                  */
304         }
305
306         value = nm_setting_bond_get_option_by_name (s_bond, NM_SETTING_BOND_OPTION_ARP_VALIDATE);
307         /* arp_validate > 0 only valid in active-backup mode */
308         if (   value
309             && g_strcmp0 (value, "0") != 0
310             && g_strcmp0 (value, "none") != 0
311             && g_strcmp0 (mode, "active-backup") == 0)
312                 set_bond_attr (device, "arp_validate", value);
313         else
314                 set_bond_attr (device, "arp_validate", "0");
315
316         if (   g_strcmp0 (mode, "active-backup") == 0
317             || g_strcmp0 (mode, "balance-alb") == 0
318             || g_strcmp0 (mode, "balance-tlb") == 0) {
319                 value = nm_setting_bond_get_option_by_name (s_bond, NM_SETTING_BOND_OPTION_PRIMARY);
320                 set_bond_attr (device, "primary", value ? value : "");
321         }
322
323         /* Clear ARP targets */
324         contents = nm_platform_sysctl_master_get_option (NM_PLATFORM_GET, ifindex, "arp_ip_target");
325         set_arp_targets (device, contents, " \n", "-");
326         g_free (contents);
327
328         /* Add new ARP targets */
329         value = nm_setting_bond_get_option_by_name (s_bond, NM_SETTING_BOND_OPTION_ARP_IP_TARGET);
330         set_arp_targets (device, value, ",", "+");
331
332         set_simple_option (device, "primary_reselect", s_bond, NM_SETTING_BOND_OPTION_PRIMARY_RESELECT);
333         set_simple_option (device, "fail_over_mac", s_bond, NM_SETTING_BOND_OPTION_FAIL_OVER_MAC);
334         set_simple_option (device, "use_carrier", s_bond, NM_SETTING_BOND_OPTION_USE_CARRIER);
335         set_simple_option (device, "ad_select", s_bond, NM_SETTING_BOND_OPTION_AD_SELECT);
336         set_simple_option (device, "xmit_hash_policy", s_bond, NM_SETTING_BOND_OPTION_XMIT_HASH_POLICY);
337         set_simple_option (device, "resend_igmp", s_bond, NM_SETTING_BOND_OPTION_RESEND_IGMP);
338
339         if (   g_strcmp0 (mode, "4") == 0
340             || g_strcmp0 (mode, "802.3ad") == 0)
341                 set_simple_option (device, "lacp_rate", s_bond, NM_SETTING_BOND_OPTION_LACP_RATE);
342
343         return NM_ACT_STAGE_RETURN_SUCCESS;
344 }
345
346
347 static NMActStageReturn
348 act_stage1_prepare (NMDevice *dev, NMDeviceStateReason *reason)
349 {
350         NMActStageReturn ret = NM_ACT_STAGE_RETURN_SUCCESS;
351         gboolean no_firmware = FALSE;
352
353         g_return_val_if_fail (reason != NULL, NM_ACT_STAGE_RETURN_FAILURE);
354
355         ret = NM_DEVICE_CLASS (nm_device_bond_parent_class)->act_stage1_prepare (dev, reason);
356         if (ret != NM_ACT_STAGE_RETURN_SUCCESS)
357                 return ret;
358
359         /* Interface must be down to set bond options */
360         nm_device_take_down (dev, TRUE);
361         ret = apply_bonding_config (dev);
362         nm_device_bring_up (dev, TRUE, &no_firmware);
363
364         return ret;
365 }
366
367 static void
368 ip4_config_pre_commit (NMDevice *self, NMIP4Config *config)
369 {
370         NMConnection *connection;
371         NMSettingWired *s_wired;
372         guint32 mtu;
373
374         connection = nm_device_get_applied_connection (self);
375         g_assert (connection);
376         s_wired = nm_connection_get_setting_wired (connection);
377
378         if (s_wired) {
379                 /* MTU override */
380                 mtu = nm_setting_wired_get_mtu (s_wired);
381                 if (mtu)
382                         nm_ip4_config_set_mtu (config, mtu, NM_IP_CONFIG_SOURCE_USER);
383         }
384 }
385
386 static gboolean
387 enslave_slave (NMDevice *device,
388                NMDevice *slave,
389                NMConnection *connection,
390                gboolean configure)
391 {
392         NMDeviceBond *self = NM_DEVICE_BOND (device);
393         gboolean success = TRUE, no_firmware = FALSE;
394         const char *slave_iface = nm_device_get_ip_iface (slave);
395
396         nm_device_master_check_slave_physical_port (device, slave, LOGD_BOND);
397
398         if (configure) {
399                 nm_device_take_down (slave, TRUE);
400                 success = nm_platform_link_enslave (NM_PLATFORM_GET,
401                                                     nm_device_get_ip_ifindex (device),
402                                                     nm_device_get_ip_ifindex (slave));
403                 nm_device_bring_up (slave, TRUE, &no_firmware);
404
405                 if (!success)
406                         return FALSE;
407
408                 _LOGI (LOGD_BOND, "enslaved bond slave %s", slave_iface);
409         } else
410                 _LOGI (LOGD_BOND, "bond slave %s was enslaved", slave_iface);
411
412         return TRUE;
413 }
414
415 static void
416 release_slave (NMDevice *device,
417                NMDevice *slave,
418                gboolean configure)
419 {
420         NMDeviceBond *self = NM_DEVICE_BOND (device);
421         gboolean success, no_firmware = FALSE;
422
423         if (configure) {
424                 success = nm_platform_link_release (NM_PLATFORM_GET,
425                                                     nm_device_get_ip_ifindex (device),
426                                                     nm_device_get_ip_ifindex (slave));
427
428                 if (success) {
429                         _LOGI (LOGD_BOND, "released bond slave %s",
430                                nm_device_get_ip_iface (slave));
431                 } else {
432                         _LOGW (LOGD_BOND, "failed to release bond slave %s",
433                                nm_device_get_ip_iface (slave));
434                 }
435
436                 /* Kernel bonding code "closes" the slave when releasing it, (which clears
437                  * IFF_UP), so we must bring it back up here to ensure carrier changes and
438                  * other state is noticed by the now-released slave.
439                  */
440                 if (!nm_device_bring_up (slave, TRUE, &no_firmware))
441                         _LOGW (LOGD_BOND, "released bond slave could not be brought up.");
442         } else {
443                 _LOGI (LOGD_BOND, "bond slave %s was released",
444                        nm_device_get_ip_iface (slave));
445         }
446 }
447
448 static gboolean
449 create_and_realize (NMDevice *device,
450                     NMConnection *connection,
451                     NMDevice *parent,
452                     const NMPlatformLink **out_plink,
453                     GError **error)
454 {
455         const char *iface = nm_device_get_iface (device);
456         NMPlatformError plerr;
457
458         g_assert (iface);
459
460         plerr = nm_platform_link_bond_add (NM_PLATFORM_GET, iface, out_plink);
461         if (plerr != NM_PLATFORM_ERROR_SUCCESS) {
462                 g_set_error (error, NM_DEVICE_ERROR, NM_DEVICE_ERROR_CREATION_FAILED,
463                              "Failed to create bond interface '%s' for '%s': %s",
464                              iface,
465                              nm_connection_get_id (connection),
466                              nm_platform_error_to_string (plerr));
467                 return FALSE;
468         }
469         return TRUE;
470 }
471
472 /******************************************************************/
473
474 static void
475 nm_device_bond_init (NMDeviceBond * self)
476 {
477 }
478
479 static void
480 nm_device_bond_class_init (NMDeviceBondClass *klass)
481 {
482         GObjectClass *object_class = G_OBJECT_CLASS (klass);
483         NMDeviceClass *parent_class = NM_DEVICE_CLASS (klass);
484
485         g_type_class_add_private (object_class, sizeof (NMDeviceBondPrivate));
486
487         NM_DEVICE_CLASS_DECLARE_TYPES (klass, NM_SETTING_BOND_SETTING_NAME, NM_LINK_TYPE_BOND)
488
489         parent_class->get_generic_capabilities = get_generic_capabilities;
490         parent_class->is_available = is_available;
491         parent_class->check_connection_compatible = check_connection_compatible;
492         parent_class->check_connection_available = check_connection_available;
493         parent_class->complete_connection = complete_connection;
494
495         parent_class->update_connection = update_connection;
496         parent_class->master_update_slave_connection = master_update_slave_connection;
497
498         parent_class->create_and_realize = create_and_realize;
499         parent_class->act_stage1_prepare = act_stage1_prepare;
500         parent_class->ip4_config_pre_commit = ip4_config_pre_commit;
501         parent_class->enslave_slave = enslave_slave;
502         parent_class->release_slave = release_slave;
503
504         nm_exported_object_class_add_interface (NM_EXPORTED_OBJECT_CLASS (klass),
505                                                 NMDBUS_TYPE_DEVICE_BOND_SKELETON,
506                                                 NULL);
507 }
508
509 /*************************************************************/
510
511 #define NM_TYPE_BOND_FACTORY (nm_bond_factory_get_type ())
512 #define NM_BOND_FACTORY(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), NM_TYPE_BOND_FACTORY, NMBondFactory))
513
514 static NMDevice *
515 create_device (NMDeviceFactory *factory,
516                const char *iface,
517                const NMPlatformLink *plink,
518                NMConnection *connection,
519                gboolean *out_ignore)
520 {
521         return (NMDevice *) g_object_new (NM_TYPE_DEVICE_BOND,
522                                           NM_DEVICE_IFACE, iface,
523                                           NM_DEVICE_DRIVER, "bonding",
524                                           NM_DEVICE_TYPE_DESC, "Bond",
525                                           NM_DEVICE_DEVICE_TYPE, NM_DEVICE_TYPE_BOND,
526                                           NM_DEVICE_LINK_TYPE, NM_LINK_TYPE_BOND,
527                                           NM_DEVICE_IS_MASTER, TRUE,
528                                           NULL);
529 }
530
531 NM_DEVICE_FACTORY_DEFINE_INTERNAL (BOND, Bond, bond,
532         NM_DEVICE_FACTORY_DECLARE_LINK_TYPES    (NM_LINK_TYPE_BOND)
533         NM_DEVICE_FACTORY_DECLARE_SETTING_TYPES (NM_SETTING_BOND_SETTING_NAME),
534         factory_iface->create_device = create_device;
535         )
536