bond_options.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  1. /*
  2. * drivers/net/bond/bond_options.c - bonding options
  3. * Copyright (c) 2013 Jiri Pirko <jiri@resnulli.us>
  4. * Copyright (c) 2013 Scott Feldman <sfeldma@cumulusnetworks.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. */
  11. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  12. #include <linux/errno.h>
  13. #include <linux/if.h>
  14. #include <linux/netdevice.h>
  15. #include <linux/rwlock.h>
  16. #include <linux/rcupdate.h>
  17. #include "bonding.h"
  18. static bool bond_mode_is_valid(int mode)
  19. {
  20. int i;
  21. for (i = 0; bond_mode_tbl[i].modename; i++);
  22. return mode >= 0 && mode < i;
  23. }
  24. int bond_option_mode_set(struct bonding *bond, int mode)
  25. {
  26. if (!bond_mode_is_valid(mode)) {
  27. pr_err("invalid mode value %d.\n", mode);
  28. return -EINVAL;
  29. }
  30. if (bond->dev->flags & IFF_UP) {
  31. pr_err("%s: unable to update mode because interface is up.\n",
  32. bond->dev->name);
  33. return -EPERM;
  34. }
  35. if (bond_has_slaves(bond)) {
  36. pr_err("%s: unable to update mode because bond has slaves.\n",
  37. bond->dev->name);
  38. return -EPERM;
  39. }
  40. if (BOND_NO_USES_ARP(mode) && bond->params.arp_interval) {
  41. pr_info("%s: %s mode is incompatible with arp monitoring, start mii monitoring\n",
  42. bond->dev->name, bond_mode_tbl[mode].modename);
  43. /* disable arp monitoring */
  44. bond->params.arp_interval = 0;
  45. /* set miimon to default value */
  46. bond->params.miimon = BOND_DEFAULT_MIIMON;
  47. pr_info("%s: Setting MII monitoring interval to %d.\n",
  48. bond->dev->name, bond->params.miimon);
  49. }
  50. /* don't cache arp_validate between modes */
  51. bond->params.arp_validate = BOND_ARP_VALIDATE_NONE;
  52. bond->params.mode = mode;
  53. return 0;
  54. }
  55. static struct net_device *__bond_option_active_slave_get(struct bonding *bond,
  56. struct slave *slave)
  57. {
  58. return USES_PRIMARY(bond->params.mode) && slave ? slave->dev : NULL;
  59. }
  60. struct net_device *bond_option_active_slave_get_rcu(struct bonding *bond)
  61. {
  62. struct slave *slave = rcu_dereference(bond->curr_active_slave);
  63. return __bond_option_active_slave_get(bond, slave);
  64. }
  65. struct net_device *bond_option_active_slave_get(struct bonding *bond)
  66. {
  67. return __bond_option_active_slave_get(bond, bond->curr_active_slave);
  68. }
  69. int bond_option_active_slave_set(struct bonding *bond,
  70. struct net_device *slave_dev)
  71. {
  72. int ret = 0;
  73. if (slave_dev) {
  74. if (!netif_is_bond_slave(slave_dev)) {
  75. pr_err("Device %s is not bonding slave.\n",
  76. slave_dev->name);
  77. return -EINVAL;
  78. }
  79. if (bond->dev != netdev_master_upper_dev_get(slave_dev)) {
  80. pr_err("%s: Device %s is not our slave.\n",
  81. bond->dev->name, slave_dev->name);
  82. return -EINVAL;
  83. }
  84. }
  85. if (!USES_PRIMARY(bond->params.mode)) {
  86. pr_err("%s: Unable to change active slave; %s is in mode %d\n",
  87. bond->dev->name, bond->dev->name, bond->params.mode);
  88. return -EINVAL;
  89. }
  90. block_netpoll_tx();
  91. write_lock_bh(&bond->curr_slave_lock);
  92. /* check to see if we are clearing active */
  93. if (!slave_dev) {
  94. pr_info("%s: Clearing current active slave.\n",
  95. bond->dev->name);
  96. rcu_assign_pointer(bond->curr_active_slave, NULL);
  97. bond_select_active_slave(bond);
  98. } else {
  99. struct slave *old_active = bond->curr_active_slave;
  100. struct slave *new_active = bond_slave_get_rtnl(slave_dev);
  101. BUG_ON(!new_active);
  102. if (new_active == old_active) {
  103. /* do nothing */
  104. pr_info("%s: %s is already the current active slave.\n",
  105. bond->dev->name, new_active->dev->name);
  106. } else {
  107. if (old_active && (new_active->link == BOND_LINK_UP) &&
  108. IS_UP(new_active->dev)) {
  109. pr_info("%s: Setting %s as active slave.\n",
  110. bond->dev->name, new_active->dev->name);
  111. bond_change_active_slave(bond, new_active);
  112. } else {
  113. pr_err("%s: Could not set %s as active slave; either %s is down or the link is down.\n",
  114. bond->dev->name, new_active->dev->name,
  115. new_active->dev->name);
  116. ret = -EINVAL;
  117. }
  118. }
  119. }
  120. write_unlock_bh(&bond->curr_slave_lock);
  121. unblock_netpoll_tx();
  122. return ret;
  123. }
  124. int bond_option_miimon_set(struct bonding *bond, int miimon)
  125. {
  126. if (miimon < 0) {
  127. pr_err("%s: Invalid miimon value %d not in range %d-%d; rejected.\n",
  128. bond->dev->name, miimon, 0, INT_MAX);
  129. return -EINVAL;
  130. }
  131. pr_info("%s: Setting MII monitoring interval to %d.\n",
  132. bond->dev->name, miimon);
  133. bond->params.miimon = miimon;
  134. if (bond->params.updelay)
  135. pr_info("%s: Note: Updating updelay (to %d) since it is a multiple of the miimon value.\n",
  136. bond->dev->name,
  137. bond->params.updelay * bond->params.miimon);
  138. if (bond->params.downdelay)
  139. pr_info("%s: Note: Updating downdelay (to %d) since it is a multiple of the miimon value.\n",
  140. bond->dev->name,
  141. bond->params.downdelay * bond->params.miimon);
  142. if (miimon && bond->params.arp_interval) {
  143. pr_info("%s: MII monitoring cannot be used with ARP monitoring. Disabling ARP monitoring...\n",
  144. bond->dev->name);
  145. bond->params.arp_interval = 0;
  146. if (bond->params.arp_validate)
  147. bond->params.arp_validate = BOND_ARP_VALIDATE_NONE;
  148. }
  149. if (bond->dev->flags & IFF_UP) {
  150. /* If the interface is up, we may need to fire off
  151. * the MII timer. If the interface is down, the
  152. * timer will get fired off when the open function
  153. * is called.
  154. */
  155. if (!miimon) {
  156. cancel_delayed_work_sync(&bond->mii_work);
  157. } else {
  158. cancel_delayed_work_sync(&bond->arp_work);
  159. queue_delayed_work(bond->wq, &bond->mii_work, 0);
  160. }
  161. }
  162. return 0;
  163. }
  164. int bond_option_updelay_set(struct bonding *bond, int updelay)
  165. {
  166. if (!(bond->params.miimon)) {
  167. pr_err("%s: Unable to set up delay as MII monitoring is disabled\n",
  168. bond->dev->name);
  169. return -EPERM;
  170. }
  171. if (updelay < 0) {
  172. pr_err("%s: Invalid up delay value %d not in range %d-%d; rejected.\n",
  173. bond->dev->name, updelay, 0, INT_MAX);
  174. return -EINVAL;
  175. } else {
  176. if ((updelay % bond->params.miimon) != 0) {
  177. pr_warn("%s: Warning: up delay (%d) is not a multiple of miimon (%d), updelay rounded to %d ms\n",
  178. bond->dev->name, updelay,
  179. bond->params.miimon,
  180. (updelay / bond->params.miimon) *
  181. bond->params.miimon);
  182. }
  183. bond->params.updelay = updelay / bond->params.miimon;
  184. pr_info("%s: Setting up delay to %d.\n",
  185. bond->dev->name,
  186. bond->params.updelay * bond->params.miimon);
  187. }
  188. return 0;
  189. }
  190. int bond_option_downdelay_set(struct bonding *bond, int downdelay)
  191. {
  192. if (!(bond->params.miimon)) {
  193. pr_err("%s: Unable to set down delay as MII monitoring is disabled\n",
  194. bond->dev->name);
  195. return -EPERM;
  196. }
  197. if (downdelay < 0) {
  198. pr_err("%s: Invalid down delay value %d not in range %d-%d; rejected.\n",
  199. bond->dev->name, downdelay, 0, INT_MAX);
  200. return -EINVAL;
  201. } else {
  202. if ((downdelay % bond->params.miimon) != 0) {
  203. pr_warn("%s: Warning: down delay (%d) is not a multiple of miimon (%d), delay rounded to %d ms\n",
  204. bond->dev->name, downdelay,
  205. bond->params.miimon,
  206. (downdelay / bond->params.miimon) *
  207. bond->params.miimon);
  208. }
  209. bond->params.downdelay = downdelay / bond->params.miimon;
  210. pr_info("%s: Setting down delay to %d.\n",
  211. bond->dev->name,
  212. bond->params.downdelay * bond->params.miimon);
  213. }
  214. return 0;
  215. }
  216. int bond_option_use_carrier_set(struct bonding *bond, int use_carrier)
  217. {
  218. if ((use_carrier == 0) || (use_carrier == 1)) {
  219. bond->params.use_carrier = use_carrier;
  220. pr_info("%s: Setting use_carrier to %d.\n",
  221. bond->dev->name, use_carrier);
  222. } else {
  223. pr_info("%s: Ignoring invalid use_carrier value %d.\n",
  224. bond->dev->name, use_carrier);
  225. }
  226. return 0;
  227. }
  228. int bond_option_arp_interval_set(struct bonding *bond, int arp_interval)
  229. {
  230. if (arp_interval < 0) {
  231. pr_err("%s: Invalid arp_interval value %d not in range 0-%d; rejected.\n",
  232. bond->dev->name, arp_interval, INT_MAX);
  233. return -EINVAL;
  234. }
  235. if (BOND_NO_USES_ARP(bond->params.mode)) {
  236. pr_info("%s: ARP monitoring cannot be used with ALB/TLB/802.3ad. Only MII monitoring is supported on %s.\n",
  237. bond->dev->name, bond->dev->name);
  238. return -EINVAL;
  239. }
  240. pr_info("%s: Setting ARP monitoring interval to %d.\n",
  241. bond->dev->name, arp_interval);
  242. bond->params.arp_interval = arp_interval;
  243. if (arp_interval) {
  244. if (bond->params.miimon) {
  245. pr_info("%s: ARP monitoring cannot be used with MII monitoring. %s Disabling MII monitoring.\n",
  246. bond->dev->name, bond->dev->name);
  247. bond->params.miimon = 0;
  248. }
  249. if (!bond->params.arp_targets[0])
  250. pr_info("%s: ARP monitoring has been set up, but no ARP targets have been specified.\n",
  251. bond->dev->name);
  252. }
  253. if (bond->dev->flags & IFF_UP) {
  254. /* If the interface is up, we may need to fire off
  255. * the ARP timer. If the interface is down, the
  256. * timer will get fired off when the open function
  257. * is called.
  258. */
  259. if (!arp_interval) {
  260. if (bond->params.arp_validate)
  261. bond->recv_probe = NULL;
  262. cancel_delayed_work_sync(&bond->arp_work);
  263. } else {
  264. /* arp_validate can be set only in active-backup mode */
  265. if (bond->params.arp_validate)
  266. bond->recv_probe = bond_arp_rcv;
  267. cancel_delayed_work_sync(&bond->mii_work);
  268. queue_delayed_work(bond->wq, &bond->arp_work, 0);
  269. }
  270. }
  271. return 0;
  272. }
  273. static void _bond_options_arp_ip_target_set(struct bonding *bond, int slot,
  274. __be32 target,
  275. unsigned long last_rx)
  276. {
  277. __be32 *targets = bond->params.arp_targets;
  278. struct list_head *iter;
  279. struct slave *slave;
  280. if (slot >= 0 && slot < BOND_MAX_ARP_TARGETS) {
  281. bond_for_each_slave(bond, slave, iter)
  282. slave->target_last_arp_rx[slot] = last_rx;
  283. targets[slot] = target;
  284. }
  285. }
  286. static int _bond_option_arp_ip_target_add(struct bonding *bond, __be32 target)
  287. {
  288. __be32 *targets = bond->params.arp_targets;
  289. int ind;
  290. if (IS_IP_TARGET_UNUSABLE_ADDRESS(target)) {
  291. pr_err("%s: invalid ARP target %pI4 specified for addition\n",
  292. bond->dev->name, &target);
  293. return -EINVAL;
  294. }
  295. if (bond_get_targets_ip(targets, target) != -1) { /* dup */
  296. pr_err("%s: ARP target %pI4 is already present\n",
  297. bond->dev->name, &target);
  298. return -EINVAL;
  299. }
  300. ind = bond_get_targets_ip(targets, 0); /* first free slot */
  301. if (ind == -1) {
  302. pr_err("%s: ARP target table is full!\n",
  303. bond->dev->name);
  304. return -EINVAL;
  305. }
  306. pr_info("%s: adding ARP target %pI4.\n", bond->dev->name, &target);
  307. _bond_options_arp_ip_target_set(bond, ind, target, jiffies);
  308. return 0;
  309. }
  310. int bond_option_arp_ip_target_add(struct bonding *bond, __be32 target)
  311. {
  312. int ret;
  313. /* not to race with bond_arp_rcv */
  314. write_lock_bh(&bond->lock);
  315. ret = _bond_option_arp_ip_target_add(bond, target);
  316. write_unlock_bh(&bond->lock);
  317. return ret;
  318. }
  319. int bond_option_arp_ip_target_rem(struct bonding *bond, __be32 target)
  320. {
  321. __be32 *targets = bond->params.arp_targets;
  322. struct list_head *iter;
  323. struct slave *slave;
  324. unsigned long *targets_rx;
  325. int ind, i;
  326. if (IS_IP_TARGET_UNUSABLE_ADDRESS(target)) {
  327. pr_err("%s: invalid ARP target %pI4 specified for removal\n",
  328. bond->dev->name, &target);
  329. return -EINVAL;
  330. }
  331. ind = bond_get_targets_ip(targets, target);
  332. if (ind == -1) {
  333. pr_err("%s: unable to remove nonexistent ARP target %pI4.\n",
  334. bond->dev->name, &target);
  335. return -EINVAL;
  336. }
  337. if (ind == 0 && !targets[1] && bond->params.arp_interval)
  338. pr_warn("%s: removing last arp target with arp_interval on\n",
  339. bond->dev->name);
  340. pr_info("%s: removing ARP target %pI4.\n", bond->dev->name,
  341. &target);
  342. /* not to race with bond_arp_rcv */
  343. write_lock_bh(&bond->lock);
  344. bond_for_each_slave(bond, slave, iter) {
  345. targets_rx = slave->target_last_arp_rx;
  346. for (i = ind; (i < BOND_MAX_ARP_TARGETS-1) && targets[i+1]; i++)
  347. targets_rx[i] = targets_rx[i+1];
  348. targets_rx[i] = 0;
  349. }
  350. for (i = ind; (i < BOND_MAX_ARP_TARGETS-1) && targets[i+1]; i++)
  351. targets[i] = targets[i+1];
  352. targets[i] = 0;
  353. write_unlock_bh(&bond->lock);
  354. return 0;
  355. }
  356. int bond_option_arp_ip_targets_set(struct bonding *bond, __be32 *targets,
  357. int count)
  358. {
  359. int i, ret = 0;
  360. /* not to race with bond_arp_rcv */
  361. write_lock_bh(&bond->lock);
  362. /* clear table */
  363. for (i = 0; i < BOND_MAX_ARP_TARGETS; i++)
  364. _bond_options_arp_ip_target_set(bond, i, 0, 0);
  365. if (count == 0 && bond->params.arp_interval)
  366. pr_warn("%s: removing last arp target with arp_interval on\n",
  367. bond->dev->name);
  368. for (i = 0; i < count; i++) {
  369. ret = _bond_option_arp_ip_target_add(bond, targets[i]);
  370. if (ret)
  371. break;
  372. }
  373. write_unlock_bh(&bond->lock);
  374. return ret;
  375. }
  376. int bond_option_arp_validate_set(struct bonding *bond, int arp_validate)
  377. {
  378. if (bond->params.mode != BOND_MODE_ACTIVEBACKUP) {
  379. pr_err("%s: arp_validate only supported in active-backup mode.\n",
  380. bond->dev->name);
  381. return -EINVAL;
  382. }
  383. pr_info("%s: setting arp_validate to %s (%d).\n",
  384. bond->dev->name, arp_validate_tbl[arp_validate].modename,
  385. arp_validate);
  386. if (bond->dev->flags & IFF_UP) {
  387. if (!arp_validate)
  388. bond->recv_probe = NULL;
  389. else if (bond->params.arp_interval)
  390. bond->recv_probe = bond_arp_rcv;
  391. }
  392. bond->params.arp_validate = arp_validate;
  393. return 0;
  394. }
  395. int bond_option_arp_all_targets_set(struct bonding *bond, int arp_all_targets)
  396. {
  397. pr_info("%s: setting arp_all_targets to %s (%d).\n",
  398. bond->dev->name, arp_all_targets_tbl[arp_all_targets].modename,
  399. arp_all_targets);
  400. bond->params.arp_all_targets = arp_all_targets;
  401. return 0;
  402. }
  403. int bond_option_primary_set(struct bonding *bond, const char *primary)
  404. {
  405. struct list_head *iter;
  406. struct slave *slave;
  407. int err = 0;
  408. block_netpoll_tx();
  409. read_lock(&bond->lock);
  410. write_lock_bh(&bond->curr_slave_lock);
  411. if (!USES_PRIMARY(bond->params.mode)) {
  412. pr_err("%s: Unable to set primary slave; %s is in mode %d\n",
  413. bond->dev->name, bond->dev->name, bond->params.mode);
  414. err = -EINVAL;
  415. goto out;
  416. }
  417. /* check to see if we are clearing primary */
  418. if (!strlen(primary)) {
  419. pr_info("%s: Setting primary slave to None.\n",
  420. bond->dev->name);
  421. bond->primary_slave = NULL;
  422. memset(bond->params.primary, 0, sizeof(bond->params.primary));
  423. bond_select_active_slave(bond);
  424. goto out;
  425. }
  426. bond_for_each_slave(bond, slave, iter) {
  427. if (strncmp(slave->dev->name, primary, IFNAMSIZ) == 0) {
  428. pr_info("%s: Setting %s as primary slave.\n",
  429. bond->dev->name, slave->dev->name);
  430. bond->primary_slave = slave;
  431. strcpy(bond->params.primary, slave->dev->name);
  432. bond_select_active_slave(bond);
  433. goto out;
  434. }
  435. }
  436. strncpy(bond->params.primary, primary, IFNAMSIZ);
  437. bond->params.primary[IFNAMSIZ - 1] = 0;
  438. pr_info("%s: Recording %s as primary, but it has not been enslaved to %s yet.\n",
  439. bond->dev->name, primary, bond->dev->name);
  440. out:
  441. write_unlock_bh(&bond->curr_slave_lock);
  442. read_unlock(&bond->lock);
  443. unblock_netpoll_tx();
  444. return err;
  445. }
  446. int bond_option_primary_reselect_set(struct bonding *bond, int primary_reselect)
  447. {
  448. bond->params.primary_reselect = primary_reselect;
  449. pr_info("%s: setting primary_reselect to %s (%d).\n",
  450. bond->dev->name, pri_reselect_tbl[primary_reselect].modename,
  451. primary_reselect);
  452. block_netpoll_tx();
  453. write_lock_bh(&bond->curr_slave_lock);
  454. bond_select_active_slave(bond);
  455. write_unlock_bh(&bond->curr_slave_lock);
  456. unblock_netpoll_tx();
  457. return 0;
  458. }
  459. int bond_option_fail_over_mac_set(struct bonding *bond, int fail_over_mac)
  460. {
  461. if (bond_has_slaves(bond)) {
  462. pr_err("%s: Can't alter fail_over_mac with slaves in bond.\n",
  463. bond->dev->name);
  464. return -EPERM;
  465. }
  466. bond->params.fail_over_mac = fail_over_mac;
  467. pr_info("%s: Setting fail_over_mac to %s (%d).\n",
  468. bond->dev->name, fail_over_mac_tbl[fail_over_mac].modename,
  469. fail_over_mac);
  470. return 0;
  471. }
  472. int bond_option_xmit_hash_policy_set(struct bonding *bond, int xmit_hash_policy)
  473. {
  474. bond->params.xmit_policy = xmit_hash_policy;
  475. pr_info("%s: setting xmit hash policy to %s (%d).\n",
  476. bond->dev->name,
  477. xmit_hashtype_tbl[xmit_hash_policy].modename, xmit_hash_policy);
  478. return 0;
  479. }
  480. int bond_option_resend_igmp_set(struct bonding *bond, int resend_igmp)
  481. {
  482. if (resend_igmp < 0 || resend_igmp > 255) {
  483. pr_err("%s: Invalid resend_igmp value %d not in range 0-255; rejected.\n",
  484. bond->dev->name, resend_igmp);
  485. return -EINVAL;
  486. }
  487. bond->params.resend_igmp = resend_igmp;
  488. pr_info("%s: Setting resend_igmp to %d.\n",
  489. bond->dev->name, resend_igmp);
  490. return 0;
  491. }