b88b2511b9df53598aa7f0e9852c5091be5aa6e0
[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
164                 if (value && !ignore_if_zero (*options, value) && (g_strcmp0 (value, defvalue) != 0)) {
165                         /* Replace " " with "," for arp_ip_targets from the kernel */
166                         if (strcmp (*options, "arp_ip_target") == 0) {
167                                 char *p = value;
168
169                                 while (p && *p) {
170                                         if (*p == ' ')
171                                                 *p = ',';
172                                         p++;
173                                 }
174                         }
175
176                         nm_setting_bond_add_option (s_bond, *options, value);
177                 }
178                 options++;
179         }
180 }
181
182 static gboolean
183 master_update_slave_connection (NMDevice *self,
184                                 NMDevice *slave,
185                                 NMConnection *connection,
186                                 GError **error)
187 {
188         g_object_set (nm_connection_get_setting_connection (connection),
189                       NM_SETTING_CONNECTION_MASTER, nm_device_get_iface (self),
190                       NM_SETTING_CONNECTION_SLAVE_TYPE, NM_SETTING_BOND_SETTING_NAME,
191                       NULL);
192         return TRUE;
193 }
194
195 static void
196 set_arp_targets (NMDevice *device,
197                  const char *value,
198                  const char *delim,
199                  const char *prefix)
200 {
201         char **items, **iter, *tmp;
202
203         if (!value || !*value)
204                 return;
205
206         items = g_strsplit_set (value, delim, 0);
207         for (iter = items; iter && *iter; iter++) {
208                 if (*iter[0]) {
209                         tmp = g_strdup_printf ("%s%s", prefix, *iter);
210                         set_bond_attr (device, "arp_ip_target", tmp);
211                         g_free (tmp);
212                 }
213         }
214         g_strfreev (items);
215 }
216
217 static void
218 set_simple_option (NMDevice *device,
219                    const char *attr,
220                    NMSettingBond *s_bond,
221                    const char *opt)
222 {
223         const char *value;
224
225         value = nm_setting_bond_get_option_by_name (s_bond, opt);
226         if (!value)
227                 value = nm_setting_bond_get_option_default (s_bond, opt);
228         set_bond_attr (device, attr, value);
229 }
230
231 static NMActStageReturn
232 apply_bonding_config (NMDevice *device)
233 {
234         NMConnection *connection;
235         NMSettingBond *s_bond;
236         int ifindex = nm_device_get_ifindex (device);
237         const char *mode, *value;
238         char *contents;
239         gboolean set_arp_interval = TRUE;
240
241         /* Option restrictions:
242          *
243          * arp_interval conflicts miimon > 0
244          * arp_interval conflicts [ alb, tlb ]
245          * arp_validate needs [ active-backup ]
246          * downdelay needs miimon
247          * updelay needs miimon
248          * primary needs [ active-backup, tlb, alb ]
249          *
250          * clearing miimon requires that arp_interval be 0, but clearing
251          *     arp_interval doesn't require miimon to be 0
252          */
253
254         connection = nm_device_get_applied_connection (device);
255         g_assert (connection);
256         s_bond = nm_connection_get_setting_bond (connection);
257         g_assert (s_bond);
258
259         mode = nm_setting_bond_get_option_by_name (s_bond, NM_SETTING_BOND_OPTION_MODE);
260         if (mode == NULL)
261                 mode = "balance-rr";
262
263         value = nm_setting_bond_get_option_by_name (s_bond, NM_SETTING_BOND_OPTION_MIIMON);
264         if (value && atoi (value)) {
265                 /* clear arp interval */
266                 set_bond_attr (device, "arp_interval", "0");
267                 set_arp_interval = FALSE;
268
269                 set_bond_attr (device, "miimon", value);
270                 set_simple_option (device, "updelay", s_bond, NM_SETTING_BOND_OPTION_UPDELAY);
271                 set_simple_option (device, "downdelay", s_bond, NM_SETTING_BOND_OPTION_DOWNDELAY);
272         } else if (!value) {
273                 /* If not given, and arp_interval is not given, default to 100 */
274                 long int val_int;
275                 char *end;
276
277                 value = nm_setting_bond_get_option_by_name (s_bond, NM_SETTING_BOND_OPTION_ARP_INTERVAL);
278                 errno = 0;
279                 val_int = strtol (value ? value : "0", &end, 10);
280                 if (!value || (val_int == 0 && errno == 0 && *end == '\0'))
281                         set_bond_attr (device, "miimon", "100");
282         }
283
284         /* The stuff after 'mode' requires the given mode or doesn't care */
285         set_bond_attr (device, "mode", mode);
286
287         /* arp_interval not compatible with ALB, TLB */
288         if (g_strcmp0 (mode, "balance-alb") == 0 || g_strcmp0 (mode, "balance-tlb") == 0)
289                 set_arp_interval = FALSE;
290
291         if (set_arp_interval) {
292                 set_simple_option (device, "arp_interval", s_bond, NM_SETTING_BOND_OPTION_ARP_INTERVAL);
293
294                 /* Just let miimon get cleared automatically; even setting miimon to
295                  * 0 (disabled) clears arp_interval.
296                  */
297         }
298
299         value = nm_setting_bond_get_option_by_name (s_bond, NM_SETTING_BOND_OPTION_ARP_VALIDATE);
300         /* arp_validate > 0 only valid in active-backup mode */
301         if (   value
302             && g_strcmp0 (value, "0") != 0
303             && g_strcmp0 (value, "none") != 0
304             && g_strcmp0 (mode, "active-backup") == 0)
305                 set_bond_attr (device, "arp_validate", value);
306         else
307                 set_bond_attr (device, "arp_validate", "0");
308
309         if (   g_strcmp0 (mode, "active-backup") == 0
310             || g_strcmp0 (mode, "balance-alb") == 0
311             || g_strcmp0 (mode, "balance-tlb") == 0) {
312                 value = nm_setting_bond_get_option_by_name (s_bond, NM_SETTING_BOND_OPTION_PRIMARY);
313                 set_bond_attr (device, "primary", value ? value : "");
314         }
315
316         /* Clear ARP targets */
317         contents = nm_platform_sysctl_master_get_option (NM_PLATFORM_GET, ifindex, "arp_ip_target");
318         set_arp_targets (device, contents, " \n", "-");
319         g_free (contents);
320
321         /* Add new ARP targets */
322         value = nm_setting_bond_get_option_by_name (s_bond, NM_SETTING_BOND_OPTION_ARP_IP_TARGET);
323         set_arp_targets (device, value, ",", "+");
324
325         set_simple_option (device, "primary_reselect", s_bond, NM_SETTING_BOND_OPTION_PRIMARY_RESELECT);
326         set_simple_option (device, "fail_over_mac", s_bond, NM_SETTING_BOND_OPTION_FAIL_OVER_MAC);
327         set_simple_option (device, "use_carrier", s_bond, NM_SETTING_BOND_OPTION_USE_CARRIER);
328         set_simple_option (device, "ad_select", s_bond, NM_SETTING_BOND_OPTION_AD_SELECT);
329         set_simple_option (device, "xmit_hash_policy", s_bond, NM_SETTING_BOND_OPTION_XMIT_HASH_POLICY);
330         set_simple_option (device, "resend_igmp", s_bond, NM_SETTING_BOND_OPTION_RESEND_IGMP);
331
332         if (   g_strcmp0 (mode, "4") == 0
333             || g_strcmp0 (mode, "802.3ad") == 0)
334                 set_simple_option (device, "lacp_rate", s_bond, NM_SETTING_BOND_OPTION_LACP_RATE);
335
336         return NM_ACT_STAGE_RETURN_SUCCESS;
337 }
338
339
340 static NMActStageReturn
341 act_stage1_prepare (NMDevice *dev, NMDeviceStateReason *reason)
342 {
343         NMActStageReturn ret = NM_ACT_STAGE_RETURN_SUCCESS;
344         gboolean no_firmware = FALSE;
345
346         g_return_val_if_fail (reason != NULL, NM_ACT_STAGE_RETURN_FAILURE);
347
348         ret = NM_DEVICE_CLASS (nm_device_bond_parent_class)->act_stage1_prepare (dev, reason);
349         if (ret != NM_ACT_STAGE_RETURN_SUCCESS)
350                 return ret;
351
352         /* Interface must be down to set bond options */
353         nm_device_take_down (dev, TRUE);
354         ret = apply_bonding_config (dev);
355         nm_device_bring_up (dev, TRUE, &no_firmware);
356
357         return ret;
358 }
359
360 static void
361 ip4_config_pre_commit (NMDevice *self, NMIP4Config *config)
362 {
363         NMConnection *connection;
364         NMSettingWired *s_wired;
365         guint32 mtu;
366
367         connection = nm_device_get_applied_connection (self);
368         g_assert (connection);
369         s_wired = nm_connection_get_setting_wired (connection);
370
371         if (s_wired) {
372                 /* MTU override */
373                 mtu = nm_setting_wired_get_mtu (s_wired);
374                 if (mtu)
375                         nm_ip4_config_set_mtu (config, mtu, NM_IP_CONFIG_SOURCE_USER);
376         }
377 }
378
379 static gboolean
380 enslave_slave (NMDevice *device,
381                NMDevice *slave,
382                NMConnection *connection,
383                gboolean configure)
384 {
385         NMDeviceBond *self = NM_DEVICE_BOND (device);
386         gboolean success = TRUE, no_firmware = FALSE;
387         const char *slave_iface = nm_device_get_ip_iface (slave);
388
389         nm_device_master_check_slave_physical_port (device, slave, LOGD_BOND);
390
391         if (configure) {
392                 nm_device_take_down (slave, TRUE);
393                 success = nm_platform_link_enslave (NM_PLATFORM_GET,
394                                                     nm_device_get_ip_ifindex (device),
395                                                     nm_device_get_ip_ifindex (slave));
396                 nm_device_bring_up (slave, TRUE, &no_firmware);
397
398                 if (!success)
399                         return FALSE;
400
401                 _LOGI (LOGD_BOND, "enslaved bond slave %s", slave_iface);
402         } else
403                 _LOGI (LOGD_BOND, "bond slave %s was enslaved", slave_iface);
404
405         return TRUE;
406 }
407
408 static void
409 release_slave (NMDevice *device,
410                NMDevice *slave,
411                gboolean configure)
412 {
413         NMDeviceBond *self = NM_DEVICE_BOND (device);
414         gboolean success, no_firmware = FALSE;
415
416         if (configure) {
417                 success = nm_platform_link_release (NM_PLATFORM_GET,
418                                                     nm_device_get_ip_ifindex (device),
419                                                     nm_device_get_ip_ifindex (slave));
420
421                 if (success) {
422                         _LOGI (LOGD_BOND, "released bond slave %s",
423                                nm_device_get_ip_iface (slave));
424                 } else {
425                         _LOGW (LOGD_BOND, "failed to release bond slave %s",
426                                nm_device_get_ip_iface (slave));
427                 }
428
429                 /* Kernel bonding code "closes" the slave when releasing it, (which clears
430                  * IFF_UP), so we must bring it back up here to ensure carrier changes and
431                  * other state is noticed by the now-released slave.
432                  */
433                 if (!nm_device_bring_up (slave, TRUE, &no_firmware))
434                         _LOGW (LOGD_BOND, "released bond slave could not be brought up.");
435         } else {
436                 _LOGI (LOGD_BOND, "bond slave %s was released",
437                        nm_device_get_ip_iface (slave));
438         }
439 }
440
441 static gboolean
442 create_and_realize (NMDevice *device,
443                     NMConnection *connection,
444                     NMDevice *parent,
445                     const NMPlatformLink **out_plink,
446                     GError **error)
447 {
448         const char *iface = nm_device_get_iface (device);
449         NMPlatformError plerr;
450
451         g_assert (iface);
452
453         plerr = nm_platform_link_bond_add (NM_PLATFORM_GET, iface, out_plink);
454         if (plerr != NM_PLATFORM_ERROR_SUCCESS) {
455                 g_set_error (error, NM_DEVICE_ERROR, NM_DEVICE_ERROR_CREATION_FAILED,
456                              "Failed to create bond interface '%s' for '%s': %s",
457                              iface,
458                              nm_connection_get_id (connection),
459                              nm_platform_error_to_string (plerr));
460                 return FALSE;
461         }
462         return TRUE;
463 }
464
465 /******************************************************************/
466
467 static void
468 nm_device_bond_init (NMDeviceBond * self)
469 {
470 }
471
472 static void
473 nm_device_bond_class_init (NMDeviceBondClass *klass)
474 {
475         GObjectClass *object_class = G_OBJECT_CLASS (klass);
476         NMDeviceClass *parent_class = NM_DEVICE_CLASS (klass);
477
478         g_type_class_add_private (object_class, sizeof (NMDeviceBondPrivate));
479
480         NM_DEVICE_CLASS_DECLARE_TYPES (klass, NM_SETTING_BOND_SETTING_NAME, NM_LINK_TYPE_BOND)
481
482         parent_class->get_generic_capabilities = get_generic_capabilities;
483         parent_class->is_available = is_available;
484         parent_class->check_connection_compatible = check_connection_compatible;
485         parent_class->check_connection_available = check_connection_available;
486         parent_class->complete_connection = complete_connection;
487
488         parent_class->update_connection = update_connection;
489         parent_class->master_update_slave_connection = master_update_slave_connection;
490
491         parent_class->create_and_realize = create_and_realize;
492         parent_class->act_stage1_prepare = act_stage1_prepare;
493         parent_class->ip4_config_pre_commit = ip4_config_pre_commit;
494         parent_class->enslave_slave = enslave_slave;
495         parent_class->release_slave = release_slave;
496
497         nm_exported_object_class_add_interface (NM_EXPORTED_OBJECT_CLASS (klass),
498                                                 NMDBUS_TYPE_DEVICE_BOND_SKELETON,
499                                                 NULL);
500 }
501
502 /*************************************************************/
503
504 #define NM_TYPE_BOND_FACTORY (nm_bond_factory_get_type ())
505 #define NM_BOND_FACTORY(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), NM_TYPE_BOND_FACTORY, NMBondFactory))
506
507 static NMDevice *
508 create_device (NMDeviceFactory *factory,
509                const char *iface,
510                const NMPlatformLink *plink,
511                NMConnection *connection,
512                gboolean *out_ignore)
513 {
514         return (NMDevice *) g_object_new (NM_TYPE_DEVICE_BOND,
515                                           NM_DEVICE_IFACE, iface,
516                                           NM_DEVICE_DRIVER, "bonding",
517                                           NM_DEVICE_TYPE_DESC, "Bond",
518                                           NM_DEVICE_DEVICE_TYPE, NM_DEVICE_TYPE_BOND,
519                                           NM_DEVICE_LINK_TYPE, NM_LINK_TYPE_BOND,
520                                           NM_DEVICE_IS_MASTER, TRUE,
521                                           NULL);
522 }
523
524 NM_DEVICE_FACTORY_DEFINE_INTERNAL (BOND, Bond, bond,
525         NM_DEVICE_FACTORY_DECLARE_LINK_TYPES    (NM_LINK_TYPE_BOND)
526         NM_DEVICE_FACTORY_DECLARE_SETTING_TYPES (NM_SETTING_BOND_SETTING_NAME),
527         factory_iface->create_device = create_device;
528         )
529