1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
2 /* NetworkManager -- Network link manager
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.
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.
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.
18 * Copyright 2011 - 2012 Red Hat, Inc.
21 #include "nm-default.h"
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"
35 #include "nmdbus-device-bond.h"
37 #include "nm-device-logging.h"
38 _LOG_DECLARE_SELF(NMDeviceBond);
40 G_DEFINE_TYPE (NMDeviceBond, nm_device_bond, NM_TYPE_DEVICE)
42 #define NM_DEVICE_BOND_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), NM_TYPE_DEVICE_BOND, NMDeviceBondPrivate))
46 } NMDeviceBondPrivate;
48 /******************************************************************/
50 static NMDeviceCapabilities
51 get_generic_capabilities (NMDevice *dev)
53 return NM_DEVICE_CAP_CARRIER_DETECT | NM_DEVICE_CAP_IS_SOFTWARE;
57 is_available (NMDevice *dev, NMDeviceCheckDevAvailableFlags flags)
63 check_connection_available (NMDevice *device,
64 NMConnection *connection,
65 NMDeviceCheckConAvailableFlags flags,
66 const char *specific_object)
68 /* Connections are always available because the carrier state is determined
69 * by the slave carrier states, not the bonds's state.
75 check_connection_compatible (NMDevice *device, NMConnection *connection)
77 NMSettingBond *s_bond;
79 if (!NM_DEVICE_CLASS (nm_device_bond_parent_class)->check_connection_compatible (device, connection))
82 s_bond = nm_connection_get_setting_bond (connection);
83 if (!s_bond || !nm_connection_is_type (connection, NM_SETTING_BOND_SETTING_NAME))
86 /* FIXME: match bond properties like mode, etc? */
92 complete_connection (NMDevice *device,
93 NMConnection *connection,
94 const char *specific_object,
95 const GSList *existing_connections,
98 NMSettingBond *s_bond;
100 nm_utils_complete_generic (NM_PLATFORM_GET,
102 NM_SETTING_BOND_SETTING_NAME,
103 existing_connections,
105 _("Bond connection"),
109 s_bond = nm_connection_get_setting_bond (connection);
111 s_bond = (NMSettingBond *) nm_setting_bond_new ();
112 nm_connection_add_setting (connection, NM_SETTING (s_bond));
118 /******************************************************************/
121 set_bond_attr (NMDevice *device, const char *attr, const char *value)
123 NMDeviceBond *self = NM_DEVICE_BOND (device);
125 int ifindex = nm_device_get_ifindex (device);
127 ret = nm_platform_sysctl_master_set_option (NM_PLATFORM_GET, ifindex, attr, value);
129 _LOGW (LOGD_HW, "failed to set bonding attribute '%s' to '%s'", attr, value);
133 /* Ignore certain bond options if they are zero (off/disabled) */
135 ignore_if_zero (const char *option, const char *value)
137 if (strcmp (option, "arp_interval") &&
138 strcmp (option, "miimon") &&
139 strcmp (option, "downdelay") &&
140 strcmp (option, "updelay"))
143 return g_strcmp0 (value, "0") == 0 ? TRUE : FALSE;
147 update_connection (NMDevice *device, NMConnection *connection)
149 NMSettingBond *s_bond = nm_connection_get_setting_bond (connection);
150 int ifindex = nm_device_get_ifindex (device);
151 const char **options;
154 s_bond = (NMSettingBond *) nm_setting_bond_new ();
155 nm_connection_add_setting (connection, (NMSetting *) s_bond);
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);
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) {
176 nm_setting_bond_add_option (s_bond, *options, value);
183 master_update_slave_connection (NMDevice *self,
185 NMConnection *connection,
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,
196 set_arp_targets (NMDevice *device,
201 char **items, **iter, *tmp;
203 if (!value || !*value)
206 items = g_strsplit_set (value, delim, 0);
207 for (iter = items; iter && *iter; iter++) {
209 tmp = g_strdup_printf ("%s%s", prefix, *iter);
210 set_bond_attr (device, "arp_ip_target", tmp);
218 set_simple_option (NMDevice *device,
220 NMSettingBond *s_bond,
225 value = nm_setting_bond_get_option_by_name (s_bond, opt);
227 value = nm_setting_bond_get_option_default (s_bond, opt);
228 set_bond_attr (device, attr, value);
231 static NMActStageReturn
232 apply_bonding_config (NMDevice *device)
234 NMConnection *connection;
235 NMSettingBond *s_bond;
236 int ifindex = nm_device_get_ifindex (device);
237 const char *mode, *value;
239 gboolean set_arp_interval = TRUE;
241 /* Option restrictions:
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 ]
250 * clearing miimon requires that arp_interval be 0, but clearing
251 * arp_interval doesn't require miimon to be 0
254 connection = nm_device_get_applied_connection (device);
255 g_assert (connection);
256 s_bond = nm_connection_get_setting_bond (connection);
259 mode = nm_setting_bond_get_option_by_name (s_bond, NM_SETTING_BOND_OPTION_MODE);
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;
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);
273 /* If not given, and arp_interval is not given, default to 100 */
277 value = nm_setting_bond_get_option_by_name (s_bond, NM_SETTING_BOND_OPTION_ARP_INTERVAL);
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");
284 /* The stuff after 'mode' requires the given mode or doesn't care */
285 set_bond_attr (device, "mode", mode);
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;
291 if (set_arp_interval) {
292 set_simple_option (device, "arp_interval", s_bond, NM_SETTING_BOND_OPTION_ARP_INTERVAL);
294 /* Just let miimon get cleared automatically; even setting miimon to
295 * 0 (disabled) clears arp_interval.
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 */
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);
307 set_bond_attr (device, "arp_validate", "0");
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 : "");
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", "-");
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, ",", "+");
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);
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);
336 return NM_ACT_STAGE_RETURN_SUCCESS;
340 static NMActStageReturn
341 act_stage1_prepare (NMDevice *dev, NMDeviceStateReason *reason)
343 NMActStageReturn ret = NM_ACT_STAGE_RETURN_SUCCESS;
344 gboolean no_firmware = FALSE;
346 g_return_val_if_fail (reason != NULL, NM_ACT_STAGE_RETURN_FAILURE);
348 ret = NM_DEVICE_CLASS (nm_device_bond_parent_class)->act_stage1_prepare (dev, reason);
349 if (ret != NM_ACT_STAGE_RETURN_SUCCESS)
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);
361 ip4_config_pre_commit (NMDevice *self, NMIP4Config *config)
363 NMConnection *connection;
364 NMSettingWired *s_wired;
367 connection = nm_device_get_applied_connection (self);
368 g_assert (connection);
369 s_wired = nm_connection_get_setting_wired (connection);
373 mtu = nm_setting_wired_get_mtu (s_wired);
375 nm_ip4_config_set_mtu (config, mtu, NM_IP_CONFIG_SOURCE_USER);
380 enslave_slave (NMDevice *device,
382 NMConnection *connection,
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);
389 nm_device_master_check_slave_physical_port (device, slave, LOGD_BOND);
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);
401 _LOGI (LOGD_BOND, "enslaved bond slave %s", slave_iface);
403 _LOGI (LOGD_BOND, "bond slave %s was enslaved", slave_iface);
409 release_slave (NMDevice *device,
413 NMDeviceBond *self = NM_DEVICE_BOND (device);
414 gboolean success, no_firmware = FALSE;
417 success = nm_platform_link_release (NM_PLATFORM_GET,
418 nm_device_get_ip_ifindex (device),
419 nm_device_get_ip_ifindex (slave));
422 _LOGI (LOGD_BOND, "released bond slave %s",
423 nm_device_get_ip_iface (slave));
425 _LOGW (LOGD_BOND, "failed to release bond slave %s",
426 nm_device_get_ip_iface (slave));
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.
433 if (!nm_device_bring_up (slave, TRUE, &no_firmware))
434 _LOGW (LOGD_BOND, "released bond slave could not be brought up.");
436 _LOGI (LOGD_BOND, "bond slave %s was released",
437 nm_device_get_ip_iface (slave));
442 create_and_realize (NMDevice *device,
443 NMConnection *connection,
445 const NMPlatformLink **out_plink,
448 const char *iface = nm_device_get_iface (device);
449 NMPlatformError plerr;
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",
458 nm_connection_get_id (connection),
459 nm_platform_error_to_string (plerr));
465 /******************************************************************/
468 nm_device_bond_init (NMDeviceBond * self)
473 nm_device_bond_class_init (NMDeviceBondClass *klass)
475 GObjectClass *object_class = G_OBJECT_CLASS (klass);
476 NMDeviceClass *parent_class = NM_DEVICE_CLASS (klass);
478 g_type_class_add_private (object_class, sizeof (NMDeviceBondPrivate));
480 NM_DEVICE_CLASS_DECLARE_TYPES (klass, NM_SETTING_BOND_SETTING_NAME, NM_LINK_TYPE_BOND)
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;
488 parent_class->update_connection = update_connection;
489 parent_class->master_update_slave_connection = master_update_slave_connection;
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;
497 nm_exported_object_class_add_interface (NM_EXPORTED_OBJECT_CLASS (klass),
498 NMDBUS_TYPE_DEVICE_BOND_SKELETON,
502 /*************************************************************/
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))
508 create_device (NMDeviceFactory *factory,
510 const NMPlatformLink *plink,
511 NMConnection *connection,
512 gboolean *out_ignore)
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,
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;