23 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
25 #include <linux/kernel.h>
26 #include <linux/module.h>
27 #include <linux/device.h>
28 #include <linux/sched.h>
30 #include <linux/types.h>
31 #include <linux/string.h>
32 #include <linux/netdevice.h>
36 #include <linux/ctype.h>
38 #include <linux/rtnetlink.h>
46 #define to_dev(obj) container_of(obj, struct device, kobj)
47 #define to_bond(cd) ((struct bonding *)(netdev_priv(to_net_dev(cd))))
53 static ssize_t bonding_show_bonds(
struct class *cls,
69 res +=
sprintf(buf + res,
"++more++ ");
72 res +=
sprintf(buf + res,
"%s ", bond->
dev->name);
100 static ssize_t bonding_store_bonds(
struct class *cls,
110 sscanf(buffer,
"%16s", command);
111 ifname = command + 1;
112 if ((
strlen(command) <= 1) ||
116 if (command[0] ==
'+') {
117 pr_info(
"%s is being created...\n", ifname);
121 pr_info(
"%s already exists.\n", ifname);
123 pr_info(
"%s creation failed.\n", ifname);
126 }
else if (command[0] ==
'-') {
130 bond_dev = bond_get_by_name(bn, ifname);
132 pr_info(
"%s is being deleted...\n", ifname);
133 unregister_netdevice(bond_dev);
135 pr_err(
"unable to delete non-existent %s\n", ifname);
148 pr_err(
"no command found in bonding_masters. Use +ifname or -ifname.\n");
152 static const void *bonding_namespace(
struct class *cls,
163 .name =
"bonding_masters",
166 .show = bonding_show_bonds,
167 .store = bonding_store_bonds,
168 .namespace = bonding_namespace,
217 res +=
sprintf(buf + res,
"++more++ ");
220 res +=
sprintf(buf + res,
"%s ", slave->
dev->name);
236 const char *buffer,
size_t count)
238 char command[
IFNAMSIZ + 1] = { 0, };
245 return restart_syscall();
247 sscanf(buffer,
"%16s", command);
248 ifname = command + 1;
249 if ((
strlen(command) <= 1) ||
255 pr_info(
"%s: Interface %s does not exist!\n",
256 bond->
dev->name, ifname);
261 switch (command[0]) {
281 pr_err(
"no command found in slaves file for bond %s. Use +ifname or -ifname.\n",
291 bonding_store_slaves);
309 const char *buf,
size_t count)
311 int new_value, ret =
count;
315 pr_err(
"unable to update mode of %s because interface is up.\n",
322 pr_err(
"unable to update mode of %s because it has slaves.\n",
330 pr_err(
"%s: Ignoring invalid mode value %.*s.\n",
331 bond->
dev->name, (
int)
strlen(buf) - 1, buf);
337 bond->
params.arp_interval) {
338 pr_err(
"%s: %s mode is incompatible with arp monitoring.\n",
344 bond->
params.mode = new_value;
346 pr_info(
"%s: setting mode to %s (%d).\n",
353 bonding_show_mode, bonding_store_mode);
367 bond->
params.xmit_policy);
372 const char *buf,
size_t count)
374 int new_value, ret =
count;
378 pr_err(
"%s: Interface is up. Unable to update xmit policy.\n",
386 pr_err(
"%s: Ignoring invalid xmit hash policy value %.*s.\n",
388 (
int)
strlen(buf) - 1, buf);
392 bond->
params.xmit_policy = new_value;
394 pr_info(
"%s: setting xmit hash policy to %s (%d).\n",
402 bonding_show_xmit_hash, bonding_store_xmit_hash);
415 bond->
params.arp_validate);
420 const char *buf,
size_t count)
427 pr_err(
"%s: Ignoring invalid arp_validate value %s\n",
428 bond->
dev->name, buf);
432 pr_err(
"%s: arp_validate only supported in active-backup mode.\n",
436 pr_info(
"%s: setting arp_validate to %s (%d).\n",
440 bond->
params.arp_validate = new_value;
446 bonding_store_arp_validate);
460 bond->
params.fail_over_mac);
465 const char *buf,
size_t count)
471 pr_err(
"%s: Can't alter fail_over_mac with slaves in bond.\n",
478 pr_err(
"%s: Ignoring invalid fail_over_mac value %s.\n",
479 bond->
dev->name, buf);
483 bond->
params.fail_over_mac = new_value;
484 pr_info(
"%s: Setting fail_over_mac to %s (%d).\n",
492 bonding_show_fail_over_mac, bonding_store_fail_over_mac);
511 const char *buf,
size_t count)
513 int new_value, ret =
count;
517 return restart_syscall();
518 if (
sscanf(buf,
"%d", &new_value) != 1) {
519 pr_err(
"%s: no arp_interval value specified.\n",
525 pr_err(
"%s: Invalid arp_interval value %d not in range 1-%d; rejected.\n",
532 pr_info(
"%s: ARP monitoring cannot be used with ALB/TLB. Only MII monitoring is supported on %s.\n",
533 bond->
dev->name, bond->
dev->name);
537 pr_info(
"%s: Setting ARP monitoring interval to %d.\n",
538 bond->
dev->name, new_value);
539 bond->
params.arp_interval = new_value;
540 if (bond->
params.miimon) {
541 pr_info(
"%s: ARP monitoring cannot be used with MII monitoring. %s Disabling MII monitoring.\n",
542 bond->
dev->name, bond->
dev->name);
545 if (!bond->
params.arp_targets[0]) {
546 pr_info(
"%s: ARP monitoring has been set up, but no ARP targets have been specified.\n",
564 bonding_show_arp_interval, bonding_store_arp_interval);
577 if (bond->
params.arp_targets[i])
578 res +=
sprintf(buf + res,
"%pI4 ",
579 &bond->
params.arp_targets[i]);
588 const char *buf,
size_t count)
595 targets = bond->
params.arp_targets;
600 pr_err(
"%s: invalid ARP target %pI4 specified for addition\n",
601 bond->
dev->name, &newtarget);
607 if (targets[i] == newtarget) {
608 pr_err(
"%s: ARP target %pI4 is already present\n",
609 bond->
dev->name, &newtarget);
613 if (targets[i] == 0) {
614 pr_info(
"%s: adding ARP target %pI4.\n",
615 bond->
dev->name, &newtarget);
617 targets[
i] = newtarget;
621 pr_err(
"%s: ARP target table is full!\n",
627 }
else if (buf[0] ==
'-') {
629 pr_err(
"%s: invalid ARP target %pI4 specified for removal\n",
630 bond->
dev->name, &newtarget);
636 if (targets[i] == newtarget) {
638 pr_info(
"%s: removing ARP target %pI4.\n",
639 bond->
dev->name, &newtarget);
640 for (j = i; (j < (BOND_MAX_ARP_TARGETS-1)) && targets[j+1]; j++)
641 targets[j] = targets[j+1];
648 pr_info(
"%s: unable to remove nonexistent ARP target %pI4.\n",
649 bond->
dev->name, &newtarget);
654 pr_err(
"no command found in arp_ip_targets file for bond %s. Use +<addr> or -<addr>.\n",
681 const char *buf,
size_t count)
683 int new_value, ret =
count;
686 if (!(bond->
params.miimon)) {
687 pr_err(
"%s: Unable to set down delay as MII monitoring is disabled\n",
693 if (
sscanf(buf,
"%d", &new_value) != 1) {
694 pr_err(
"%s: no down delay value specified.\n", bond->
dev->name);
699 pr_err(
"%s: Invalid down delay value %d not in range %d-%d; rejected.\n",
704 if ((new_value % bond->
params.miimon) != 0) {
705 pr_warning(
"%s: Warning: down delay (%d) is not a multiple of miimon (%d), delay rounded to %d ms\n",
706 bond->
dev->name, new_value,
708 (new_value / bond->
params.miimon) *
711 bond->
params.downdelay = new_value / bond->
params.miimon;
712 pr_info(
"%s: Setting down delay to %d.\n",
722 bonding_show_downdelay, bonding_store_downdelay);
736 const char *buf,
size_t count)
738 int new_value, ret =
count;
741 if (!(bond->
params.miimon)) {
742 pr_err(
"%s: Unable to set up delay as MII monitoring is disabled\n",
748 if (
sscanf(buf,
"%d", &new_value) != 1) {
749 pr_err(
"%s: no up delay value specified.\n",
755 pr_err(
"%s: Invalid down delay value %d not in range %d-%d; rejected.\n",
760 if ((new_value % bond->
params.miimon) != 0) {
761 pr_warning(
"%s: Warning: up delay (%d) is not a multiple of miimon (%d), updelay rounded to %d ms\n",
762 bond->
dev->name, new_value,
764 (new_value / bond->
params.miimon) *
768 pr_info(
"%s: Setting up delay to %d.\n",
777 bonding_show_updelay, bonding_store_updelay);
796 const char *buf,
size_t count)
798 int new_value, ret =
count;
802 pr_err(
"%s: Unable to update LACP rate because interface is up.\n",
809 pr_err(
"%s: Unable to update LACP rate because bond is not in 802.3ad mode.\n",
817 if ((new_value == 1) || (new_value == 0)) {
818 bond->
params.lacp_fast = new_value;
820 pr_info(
"%s: Setting LACP rate to %s (%d).\n",
824 pr_err(
"%s: Ignoring invalid LACP rate value %.*s.\n",
825 bond->
dev->name, (
int)
strlen(buf) - 1, buf);
832 bonding_show_lacp, bonding_store_lacp);
845 const char *buf,
size_t count)
849 unsigned int new_value;
853 pr_err(
"%s: Ignoring invalid min links value %s.\n",
854 bond->
dev->name, buf);
858 pr_info(
"%s: Setting min links value to %u\n",
859 bond->
dev->name, new_value);
860 bond->
params.min_links = new_value;
864 bonding_show_min_links, bonding_store_min_links);
880 const char *buf,
size_t count)
882 int new_value, ret =
count;
886 pr_err(
"%s: Unable to update ad_select because interface is up.\n",
894 if (new_value != -1) {
895 bond->
params.ad_select = new_value;
896 pr_info(
"%s: Setting ad_select to %s (%d).\n",
900 pr_err(
"%s: Ignoring invalid ad_select value %.*s.\n",
901 bond->
dev->name, (
int)
strlen(buf) - 1, buf);
908 bonding_show_ad_select, bonding_store_ad_select);
921 static ssize_t bonding_store_num_peer_notif(
struct device *d,
923 const char *buf,
size_t count)
927 return err ? err :
count;
930 bonding_show_num_peer_notif, bonding_store_num_peer_notif);
932 bonding_show_num_peer_notif, bonding_store_num_peer_notif);
951 const char *buf,
size_t count)
953 int new_value, ret =
count;
957 return restart_syscall();
958 if (
sscanf(buf,
"%d", &new_value) != 1) {
959 pr_err(
"%s: no miimon value specified.\n",
965 pr_err(
"%s: Invalid miimon value %d not in range %d-%d; rejected.\n",
970 pr_info(
"%s: Setting MII monitoring interval to %d.\n",
971 bond->
dev->name, new_value);
972 bond->
params.miimon = new_value;
974 pr_info(
"%s: Note: Updating updelay (to %d) since it is a multiple of the miimon value.\n",
977 if (bond->
params.downdelay)
978 pr_info(
"%s: Note: Updating downdelay (to %d) since it is a multiple of the miimon value.\n",
981 if (bond->
params.arp_interval) {
982 pr_info(
"%s: MII monitoring cannot be used with ARP monitoring. Disabling ARP monitoring...\n",
984 bond->
params.arp_interval = 0;
985 if (bond->
params.arp_validate) {
986 bond->
params.arp_validate =
1006 bonding_show_miimon, bonding_store_miimon);
1030 const char *buf,
size_t count)
1033 struct slave *slave;
1038 return restart_syscall();
1044 pr_info(
"%s: Unable to set primary slave; %s is in mode %d\n",
1049 sscanf(buf,
"%15s", ifname);
1052 if (!
strlen(ifname) || buf[0] ==
'\n') {
1053 pr_info(
"%s: Setting primary slave to None.\n",
1062 pr_info(
"%s: Setting %s as primary slave.\n",
1063 bond->
dev->name, slave->
dev->name);
1074 pr_info(
"%s: Recording %s as primary, "
1075 "but it has not been enslaved to %s yet.\n",
1076 bond->
dev->name, ifname, bond->
dev->name);
1086 bonding_show_primary, bonding_store_primary);
1091 static ssize_t bonding_show_primary_reselect(
struct device *d,
1097 return sprintf(buf,
"%s %d\n",
1099 bond->
params.primary_reselect);
1102 static ssize_t bonding_store_primary_reselect(
struct device *d,
1104 const char *buf,
size_t count)
1106 int new_value, ret =
count;
1110 return restart_syscall();
1113 if (new_value < 0) {
1114 pr_err(
"%s: Ignoring invalid primary_reselect value %.*s.\n",
1116 (
int)
strlen(buf) - 1, buf);
1121 bond->
params.primary_reselect = new_value;
1122 pr_info(
"%s: setting primary_reselect to %s (%d).\n",
1138 bonding_show_primary_reselect,
1139 bonding_store_primary_reselect);
1155 const char *buf,
size_t count)
1157 int new_value, ret =
count;
1161 if (
sscanf(buf,
"%d", &new_value) != 1) {
1162 pr_err(
"%s: no use_carrier value specified.\n",
1167 if ((new_value == 0) || (new_value == 1)) {
1168 bond->
params.use_carrier = new_value;
1169 pr_info(
"%s: Setting use_carrier to %d.\n",
1170 bond->
dev->name, new_value);
1172 pr_info(
"%s: Ignoring invalid use_carrier value %d.\n",
1173 bond->
dev->name, new_value);
1179 bonding_show_carrier, bonding_store_carrier);
1198 count =
sprintf(buf,
"%s\n", curr->
dev->name);
1204 const char *buf,
size_t count)
1207 struct slave *slave;
1208 struct slave *old_active =
NULL;
1209 struct slave *new_active =
NULL;
1214 return restart_syscall();
1221 pr_info(
"%s: Unable to change active slave; %s is in mode %d\n",
1226 sscanf(buf,
"%15s", ifname);
1229 if (!
strlen(ifname) || buf[0] ==
'\n') {
1230 pr_info(
"%s: Clearing current active slave.\n",
1241 if (new_active == old_active) {
1243 pr_info(
"%s: %s is already the current"
1254 pr_info(
"%s: Setting %s as active"
1262 pr_info(
"%s: Could not set %s as"
1263 " active slave; either %s is"
1264 " down or the link is down.\n",
1274 pr_info(
"%s: Unable to set %.*s as active slave.\n",
1275 bond->
dev->name, (
int)
strlen(buf) - 1, buf);
1287 bonding_show_active_slave, bonding_store_active_slave);
1304 return sprintf(buf,
"%s\n", curr ?
"up" :
"down");
1378 static ssize_t bonding_show_ad_partner_key(
struct device *d,
1400 static ssize_t bonding_show_ad_partner_mac(
struct device *d,
1424 struct slave *slave;
1429 return restart_syscall();
1437 res +=
sprintf(buf + res,
"++more++ ");
1440 res +=
sprintf(buf + res,
"%s:%d ",
1456 const char *buffer,
size_t count)
1458 struct slave *slave, *update_slave;
1466 return restart_syscall();
1469 delim =
strchr(buffer,
':');
1478 if (
sscanf(++delim,
"%hd\n", &qid) != 1)
1484 qid > bond->
dev->real_num_tx_queues)
1495 update_slave =
NULL;
1497 if (sdev == slave->
dev)
1502 update_slave = slave;
1503 else if (qid && qid == slave->
queue_id) {
1504 goto err_no_cmd_unlock;
1509 goto err_no_cmd_unlock;
1522 pr_info(
"invalid input for queue_id set for %s.\n",
1529 bonding_store_queue_id);
1541 return sprintf(buf,
"%d\n", bond->
params.all_slaves_active);
1544 static ssize_t bonding_store_slaves_active(
struct device *d,
1546 const char *buf,
size_t count)
1548 int i, new_value, ret =
count;
1550 struct slave *slave;
1552 if (
sscanf(buf,
"%d", &new_value) != 1) {
1553 pr_err(
"%s: no all_slaves_active value specified.\n",
1559 if (new_value == bond->
params.all_slaves_active)
1562 if ((new_value == 0) || (new_value == 1)) {
1563 bond->
params.all_slaves_active = new_value;
1565 pr_info(
"%s: Ignoring invalid all_slaves_active value %d.\n",
1566 bond->
dev->name, new_value);
1573 if (!bond_is_active_slave(slave)) {
1585 bonding_show_slaves_active, bonding_store_slaves_active);
1601 const char *buf,
size_t count)
1603 int new_value, ret =
count;
1606 if (
sscanf(buf,
"%d", &new_value) != 1) {
1607 pr_err(
"%s: no resend_igmp value specified.\n",
1613 if (new_value < 0 || new_value > 255) {
1614 pr_err(
"%s: Invalid resend_igmp value %d not in range 0-255; rejected.\n",
1615 bond->
dev->name, new_value);
1620 pr_info(
"%s: Setting resend_igmp to %d.\n",
1621 bond->
dev->name, new_value);
1622 bond->
params.resend_igmp = new_value;
1628 bonding_show_resend_igmp, bonding_store_resend_igmp);
1630 static struct attribute *per_bond_attrs[] = {
1631 &dev_attr_slaves.attr,
1632 &dev_attr_mode.attr,
1633 &dev_attr_fail_over_mac.attr,
1634 &dev_attr_arp_validate.attr,
1635 &dev_attr_arp_interval.attr,
1636 &dev_attr_arp_ip_target.attr,
1637 &dev_attr_downdelay.attr,
1638 &dev_attr_updelay.attr,
1639 &dev_attr_lacp_rate.attr,
1640 &dev_attr_ad_select.attr,
1641 &dev_attr_xmit_hash_policy.attr,
1642 &dev_attr_num_grat_arp.attr,
1643 &dev_attr_num_unsol_na.attr,
1644 &dev_attr_miimon.attr,
1645 &dev_attr_primary.attr,
1646 &dev_attr_primary_reselect.attr,
1647 &dev_attr_use_carrier.attr,
1648 &dev_attr_active_slave.attr,
1649 &dev_attr_mii_status.attr,
1650 &dev_attr_ad_aggregator.attr,
1651 &dev_attr_ad_num_ports.attr,
1652 &dev_attr_ad_actor_key.attr,
1653 &dev_attr_ad_partner_key.attr,
1654 &dev_attr_ad_partner_mac.attr,
1655 &dev_attr_queue_id.attr,
1656 &dev_attr_all_slaves_active.attr,
1657 &dev_attr_resend_igmp.attr,
1658 &dev_attr_min_links.attr,
1664 .attrs = per_bond_attrs,
1693 class_attr_bonding_masters.
attr.name))
1694 pr_err(
"network device named %s already exists in sysfs",
1695 class_attr_bonding_masters.
attr.name);
1717 bond->
dev->sysfs_groups[0] = &bonding_group;