/drivers/net/bonding/bond_sysfs.c

https://bitbucket.org/ndreys/linux-sunxi · C · 1669 lines · 1265 code · 214 blank · 190 comment · 199 complexity · 31fe7d3014d3f11ea84981d56a85a7ef MD5 · raw file

  1. /*
  2. * Copyright(c) 2004-2005 Intel Corporation. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms of the GNU General Public License as published by the
  6. * 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, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  11. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  12. * 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. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  17. *
  18. * The full GNU General Public License is included in this distribution in the
  19. * file called LICENSE.
  20. *
  21. */
  22. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  23. #include <linux/kernel.h>
  24. #include <linux/module.h>
  25. #include <linux/device.h>
  26. #include <linux/sched.h>
  27. #include <linux/sysdev.h>
  28. #include <linux/fs.h>
  29. #include <linux/types.h>
  30. #include <linux/string.h>
  31. #include <linux/netdevice.h>
  32. #include <linux/inetdevice.h>
  33. #include <linux/in.h>
  34. #include <linux/sysfs.h>
  35. #include <linux/ctype.h>
  36. #include <linux/inet.h>
  37. #include <linux/rtnetlink.h>
  38. #include <linux/etherdevice.h>
  39. #include <net/net_namespace.h>
  40. #include <net/netns/generic.h>
  41. #include <linux/nsproxy.h>
  42. #include "bonding.h"
  43. #define to_dev(obj) container_of(obj, struct device, kobj)
  44. #define to_bond(cd) ((struct bonding *)(netdev_priv(to_net_dev(cd))))
  45. /*
  46. * "show" function for the bond_masters attribute.
  47. * The class parameter is ignored.
  48. */
  49. static ssize_t bonding_show_bonds(struct class *cls,
  50. struct class_attribute *attr,
  51. char *buf)
  52. {
  53. struct net *net = current->nsproxy->net_ns;
  54. struct bond_net *bn = net_generic(net, bond_net_id);
  55. int res = 0;
  56. struct bonding *bond;
  57. rtnl_lock();
  58. list_for_each_entry(bond, &bn->dev_list, bond_list) {
  59. if (res > (PAGE_SIZE - IFNAMSIZ)) {
  60. /* not enough space for another interface name */
  61. if ((PAGE_SIZE - res) > 10)
  62. res = PAGE_SIZE - 10;
  63. res += sprintf(buf + res, "++more++ ");
  64. break;
  65. }
  66. res += sprintf(buf + res, "%s ", bond->dev->name);
  67. }
  68. if (res)
  69. buf[res-1] = '\n'; /* eat the leftover space */
  70. rtnl_unlock();
  71. return res;
  72. }
  73. static struct net_device *bond_get_by_name(struct net *net, const char *ifname)
  74. {
  75. struct bond_net *bn = net_generic(net, bond_net_id);
  76. struct bonding *bond;
  77. list_for_each_entry(bond, &bn->dev_list, bond_list) {
  78. if (strncmp(bond->dev->name, ifname, IFNAMSIZ) == 0)
  79. return bond->dev;
  80. }
  81. return NULL;
  82. }
  83. /*
  84. * "store" function for the bond_masters attribute. This is what
  85. * creates and deletes entire bonds.
  86. *
  87. * The class parameter is ignored.
  88. *
  89. */
  90. static ssize_t bonding_store_bonds(struct class *cls,
  91. struct class_attribute *attr,
  92. const char *buffer, size_t count)
  93. {
  94. struct net *net = current->nsproxy->net_ns;
  95. char command[IFNAMSIZ + 1] = {0, };
  96. char *ifname;
  97. int rv, res = count;
  98. sscanf(buffer, "%16s", command); /* IFNAMSIZ*/
  99. ifname = command + 1;
  100. if ((strlen(command) <= 1) ||
  101. !dev_valid_name(ifname))
  102. goto err_no_cmd;
  103. if (command[0] == '+') {
  104. pr_info("%s is being created...\n", ifname);
  105. rv = bond_create(net, ifname);
  106. if (rv) {
  107. if (rv == -EEXIST)
  108. pr_info("%s already exists.\n", ifname);
  109. else
  110. pr_info("%s creation failed.\n", ifname);
  111. res = rv;
  112. }
  113. } else if (command[0] == '-') {
  114. struct net_device *bond_dev;
  115. rtnl_lock();
  116. bond_dev = bond_get_by_name(net, ifname);
  117. if (bond_dev) {
  118. pr_info("%s is being deleted...\n", ifname);
  119. unregister_netdevice(bond_dev);
  120. } else {
  121. pr_err("unable to delete non-existent %s\n", ifname);
  122. res = -ENODEV;
  123. }
  124. rtnl_unlock();
  125. } else
  126. goto err_no_cmd;
  127. /* Always return either count or an error. If you return 0, you'll
  128. * get called forever, which is bad.
  129. */
  130. return res;
  131. err_no_cmd:
  132. pr_err("no command found in bonding_masters. Use +ifname or -ifname.\n");
  133. return -EPERM;
  134. }
  135. /* class attribute for bond_masters file. This ends up in /sys/class/net */
  136. static CLASS_ATTR(bonding_masters, S_IWUSR | S_IRUGO,
  137. bonding_show_bonds, bonding_store_bonds);
  138. int bond_create_slave_symlinks(struct net_device *master,
  139. struct net_device *slave)
  140. {
  141. char linkname[IFNAMSIZ+7];
  142. int ret = 0;
  143. /* first, create a link from the slave back to the master */
  144. ret = sysfs_create_link(&(slave->dev.kobj), &(master->dev.kobj),
  145. "master");
  146. if (ret)
  147. return ret;
  148. /* next, create a link from the master to the slave */
  149. sprintf(linkname, "slave_%s", slave->name);
  150. ret = sysfs_create_link(&(master->dev.kobj), &(slave->dev.kobj),
  151. linkname);
  152. return ret;
  153. }
  154. void bond_destroy_slave_symlinks(struct net_device *master,
  155. struct net_device *slave)
  156. {
  157. char linkname[IFNAMSIZ+7];
  158. sysfs_remove_link(&(slave->dev.kobj), "master");
  159. sprintf(linkname, "slave_%s", slave->name);
  160. sysfs_remove_link(&(master->dev.kobj), linkname);
  161. }
  162. /*
  163. * Show the slaves in the current bond.
  164. */
  165. static ssize_t bonding_show_slaves(struct device *d,
  166. struct device_attribute *attr, char *buf)
  167. {
  168. struct slave *slave;
  169. int i, res = 0;
  170. struct bonding *bond = to_bond(d);
  171. read_lock(&bond->lock);
  172. bond_for_each_slave(bond, slave, i) {
  173. if (res > (PAGE_SIZE - IFNAMSIZ)) {
  174. /* not enough space for another interface name */
  175. if ((PAGE_SIZE - res) > 10)
  176. res = PAGE_SIZE - 10;
  177. res += sprintf(buf + res, "++more++ ");
  178. break;
  179. }
  180. res += sprintf(buf + res, "%s ", slave->dev->name);
  181. }
  182. read_unlock(&bond->lock);
  183. if (res)
  184. buf[res-1] = '\n'; /* eat the leftover space */
  185. return res;
  186. }
  187. /*
  188. * Set the slaves in the current bond. The bond interface must be
  189. * up for this to succeed.
  190. * This is supposed to be only thin wrapper for bond_enslave and bond_release.
  191. * All hard work should be done there.
  192. */
  193. static ssize_t bonding_store_slaves(struct device *d,
  194. struct device_attribute *attr,
  195. const char *buffer, size_t count)
  196. {
  197. char command[IFNAMSIZ + 1] = { 0, };
  198. char *ifname;
  199. int res, ret = count;
  200. struct net_device *dev;
  201. struct bonding *bond = to_bond(d);
  202. if (!rtnl_trylock())
  203. return restart_syscall();
  204. sscanf(buffer, "%16s", command); /* IFNAMSIZ*/
  205. ifname = command + 1;
  206. if ((strlen(command) <= 1) ||
  207. !dev_valid_name(ifname))
  208. goto err_no_cmd;
  209. dev = __dev_get_by_name(dev_net(bond->dev), ifname);
  210. if (!dev) {
  211. pr_info("%s: Interface %s does not exist!\n",
  212. bond->dev->name, ifname);
  213. ret = -ENODEV;
  214. goto out;
  215. }
  216. switch (command[0]) {
  217. case '+':
  218. pr_info("%s: Adding slave %s.\n", bond->dev->name, dev->name);
  219. res = bond_enslave(bond->dev, dev);
  220. break;
  221. case '-':
  222. pr_info("%s: Removing slave %s.\n", bond->dev->name, dev->name);
  223. res = bond_release(bond->dev, dev);
  224. break;
  225. default:
  226. goto err_no_cmd;
  227. }
  228. if (res)
  229. ret = res;
  230. goto out;
  231. err_no_cmd:
  232. pr_err("no command found in slaves file for bond %s. Use +ifname or -ifname.\n",
  233. bond->dev->name);
  234. ret = -EPERM;
  235. out:
  236. rtnl_unlock();
  237. return ret;
  238. }
  239. static DEVICE_ATTR(slaves, S_IRUGO | S_IWUSR, bonding_show_slaves,
  240. bonding_store_slaves);
  241. /*
  242. * Show and set the bonding mode. The bond interface must be down to
  243. * change the mode.
  244. */
  245. static ssize_t bonding_show_mode(struct device *d,
  246. struct device_attribute *attr, char *buf)
  247. {
  248. struct bonding *bond = to_bond(d);
  249. return sprintf(buf, "%s %d\n",
  250. bond_mode_tbl[bond->params.mode].modename,
  251. bond->params.mode);
  252. }
  253. static ssize_t bonding_store_mode(struct device *d,
  254. struct device_attribute *attr,
  255. const char *buf, size_t count)
  256. {
  257. int new_value, ret = count;
  258. struct bonding *bond = to_bond(d);
  259. if (bond->dev->flags & IFF_UP) {
  260. pr_err("unable to update mode of %s because interface is up.\n",
  261. bond->dev->name);
  262. ret = -EPERM;
  263. goto out;
  264. }
  265. new_value = bond_parse_parm(buf, bond_mode_tbl);
  266. if (new_value < 0) {
  267. pr_err("%s: Ignoring invalid mode value %.*s.\n",
  268. bond->dev->name, (int)strlen(buf) - 1, buf);
  269. ret = -EINVAL;
  270. goto out;
  271. }
  272. if ((new_value == BOND_MODE_ALB ||
  273. new_value == BOND_MODE_TLB) &&
  274. bond->params.arp_interval) {
  275. pr_err("%s: %s mode is incompatible with arp monitoring.\n",
  276. bond->dev->name, bond_mode_tbl[new_value].modename);
  277. ret = -EINVAL;
  278. goto out;
  279. }
  280. bond->params.mode = new_value;
  281. bond_set_mode_ops(bond, bond->params.mode);
  282. pr_info("%s: setting mode to %s (%d).\n",
  283. bond->dev->name, bond_mode_tbl[new_value].modename,
  284. new_value);
  285. out:
  286. return ret;
  287. }
  288. static DEVICE_ATTR(mode, S_IRUGO | S_IWUSR,
  289. bonding_show_mode, bonding_store_mode);
  290. /*
  291. * Show and set the bonding transmit hash method.
  292. * The bond interface must be down to change the xmit hash policy.
  293. */
  294. static ssize_t bonding_show_xmit_hash(struct device *d,
  295. struct device_attribute *attr,
  296. char *buf)
  297. {
  298. struct bonding *bond = to_bond(d);
  299. return sprintf(buf, "%s %d\n",
  300. xmit_hashtype_tbl[bond->params.xmit_policy].modename,
  301. bond->params.xmit_policy);
  302. }
  303. static ssize_t bonding_store_xmit_hash(struct device *d,
  304. struct device_attribute *attr,
  305. const char *buf, size_t count)
  306. {
  307. int new_value, ret = count;
  308. struct bonding *bond = to_bond(d);
  309. if (bond->dev->flags & IFF_UP) {
  310. pr_err("%s: Interface is up. Unable to update xmit policy.\n",
  311. bond->dev->name);
  312. ret = -EPERM;
  313. goto out;
  314. }
  315. new_value = bond_parse_parm(buf, xmit_hashtype_tbl);
  316. if (new_value < 0) {
  317. pr_err("%s: Ignoring invalid xmit hash policy value %.*s.\n",
  318. bond->dev->name,
  319. (int)strlen(buf) - 1, buf);
  320. ret = -EINVAL;
  321. goto out;
  322. } else {
  323. bond->params.xmit_policy = new_value;
  324. bond_set_mode_ops(bond, bond->params.mode);
  325. pr_info("%s: setting xmit hash policy to %s (%d).\n",
  326. bond->dev->name,
  327. xmit_hashtype_tbl[new_value].modename, new_value);
  328. }
  329. out:
  330. return ret;
  331. }
  332. static DEVICE_ATTR(xmit_hash_policy, S_IRUGO | S_IWUSR,
  333. bonding_show_xmit_hash, bonding_store_xmit_hash);
  334. /*
  335. * Show and set arp_validate.
  336. */
  337. static ssize_t bonding_show_arp_validate(struct device *d,
  338. struct device_attribute *attr,
  339. char *buf)
  340. {
  341. struct bonding *bond = to_bond(d);
  342. return sprintf(buf, "%s %d\n",
  343. arp_validate_tbl[bond->params.arp_validate].modename,
  344. bond->params.arp_validate);
  345. }
  346. static ssize_t bonding_store_arp_validate(struct device *d,
  347. struct device_attribute *attr,
  348. const char *buf, size_t count)
  349. {
  350. int new_value;
  351. struct bonding *bond = to_bond(d);
  352. new_value = bond_parse_parm(buf, arp_validate_tbl);
  353. if (new_value < 0) {
  354. pr_err("%s: Ignoring invalid arp_validate value %s\n",
  355. bond->dev->name, buf);
  356. return -EINVAL;
  357. }
  358. if (new_value && (bond->params.mode != BOND_MODE_ACTIVEBACKUP)) {
  359. pr_err("%s: arp_validate only supported in active-backup mode.\n",
  360. bond->dev->name);
  361. return -EINVAL;
  362. }
  363. pr_info("%s: setting arp_validate to %s (%d).\n",
  364. bond->dev->name, arp_validate_tbl[new_value].modename,
  365. new_value);
  366. bond->params.arp_validate = new_value;
  367. return count;
  368. }
  369. static DEVICE_ATTR(arp_validate, S_IRUGO | S_IWUSR, bonding_show_arp_validate,
  370. bonding_store_arp_validate);
  371. /*
  372. * Show and store fail_over_mac. User only allowed to change the
  373. * value when there are no slaves.
  374. */
  375. static ssize_t bonding_show_fail_over_mac(struct device *d,
  376. struct device_attribute *attr,
  377. char *buf)
  378. {
  379. struct bonding *bond = to_bond(d);
  380. return sprintf(buf, "%s %d\n",
  381. fail_over_mac_tbl[bond->params.fail_over_mac].modename,
  382. bond->params.fail_over_mac);
  383. }
  384. static ssize_t bonding_store_fail_over_mac(struct device *d,
  385. struct device_attribute *attr,
  386. const char *buf, size_t count)
  387. {
  388. int new_value;
  389. struct bonding *bond = to_bond(d);
  390. if (bond->slave_cnt != 0) {
  391. pr_err("%s: Can't alter fail_over_mac with slaves in bond.\n",
  392. bond->dev->name);
  393. return -EPERM;
  394. }
  395. new_value = bond_parse_parm(buf, fail_over_mac_tbl);
  396. if (new_value < 0) {
  397. pr_err("%s: Ignoring invalid fail_over_mac value %s.\n",
  398. bond->dev->name, buf);
  399. return -EINVAL;
  400. }
  401. bond->params.fail_over_mac = new_value;
  402. pr_info("%s: Setting fail_over_mac to %s (%d).\n",
  403. bond->dev->name, fail_over_mac_tbl[new_value].modename,
  404. new_value);
  405. return count;
  406. }
  407. static DEVICE_ATTR(fail_over_mac, S_IRUGO | S_IWUSR,
  408. bonding_show_fail_over_mac, bonding_store_fail_over_mac);
  409. /*
  410. * Show and set the arp timer interval. There are two tricky bits
  411. * here. First, if ARP monitoring is activated, then we must disable
  412. * MII monitoring. Second, if the ARP timer isn't running, we must
  413. * start it.
  414. */
  415. static ssize_t bonding_show_arp_interval(struct device *d,
  416. struct device_attribute *attr,
  417. char *buf)
  418. {
  419. struct bonding *bond = to_bond(d);
  420. return sprintf(buf, "%d\n", bond->params.arp_interval);
  421. }
  422. static ssize_t bonding_store_arp_interval(struct device *d,
  423. struct device_attribute *attr,
  424. const char *buf, size_t count)
  425. {
  426. int new_value, ret = count;
  427. struct bonding *bond = to_bond(d);
  428. if (sscanf(buf, "%d", &new_value) != 1) {
  429. pr_err("%s: no arp_interval value specified.\n",
  430. bond->dev->name);
  431. ret = -EINVAL;
  432. goto out;
  433. }
  434. if (new_value < 0) {
  435. pr_err("%s: Invalid arp_interval value %d not in range 1-%d; rejected.\n",
  436. bond->dev->name, new_value, INT_MAX);
  437. ret = -EINVAL;
  438. goto out;
  439. }
  440. if (bond->params.mode == BOND_MODE_ALB ||
  441. bond->params.mode == BOND_MODE_TLB) {
  442. pr_info("%s: ARP monitoring cannot be used with ALB/TLB. Only MII monitoring is supported on %s.\n",
  443. bond->dev->name, bond->dev->name);
  444. ret = -EINVAL;
  445. goto out;
  446. }
  447. pr_info("%s: Setting ARP monitoring interval to %d.\n",
  448. bond->dev->name, new_value);
  449. bond->params.arp_interval = new_value;
  450. if (bond->params.miimon) {
  451. pr_info("%s: ARP monitoring cannot be used with MII monitoring. %s Disabling MII monitoring.\n",
  452. bond->dev->name, bond->dev->name);
  453. bond->params.miimon = 0;
  454. if (delayed_work_pending(&bond->mii_work)) {
  455. cancel_delayed_work(&bond->mii_work);
  456. flush_workqueue(bond->wq);
  457. }
  458. }
  459. if (!bond->params.arp_targets[0]) {
  460. pr_info("%s: ARP monitoring has been set up, but no ARP targets have been specified.\n",
  461. bond->dev->name);
  462. }
  463. if (bond->dev->flags & IFF_UP) {
  464. /* If the interface is up, we may need to fire off
  465. * the ARP timer. If the interface is down, the
  466. * timer will get fired off when the open function
  467. * is called.
  468. */
  469. if (!delayed_work_pending(&bond->arp_work)) {
  470. if (bond->params.mode == BOND_MODE_ACTIVEBACKUP)
  471. INIT_DELAYED_WORK(&bond->arp_work,
  472. bond_activebackup_arp_mon);
  473. else
  474. INIT_DELAYED_WORK(&bond->arp_work,
  475. bond_loadbalance_arp_mon);
  476. queue_delayed_work(bond->wq, &bond->arp_work, 0);
  477. }
  478. }
  479. out:
  480. return ret;
  481. }
  482. static DEVICE_ATTR(arp_interval, S_IRUGO | S_IWUSR,
  483. bonding_show_arp_interval, bonding_store_arp_interval);
  484. /*
  485. * Show and set the arp targets.
  486. */
  487. static ssize_t bonding_show_arp_targets(struct device *d,
  488. struct device_attribute *attr,
  489. char *buf)
  490. {
  491. int i, res = 0;
  492. struct bonding *bond = to_bond(d);
  493. for (i = 0; i < BOND_MAX_ARP_TARGETS; i++) {
  494. if (bond->params.arp_targets[i])
  495. res += sprintf(buf + res, "%pI4 ",
  496. &bond->params.arp_targets[i]);
  497. }
  498. if (res)
  499. buf[res-1] = '\n'; /* eat the leftover space */
  500. return res;
  501. }
  502. static ssize_t bonding_store_arp_targets(struct device *d,
  503. struct device_attribute *attr,
  504. const char *buf, size_t count)
  505. {
  506. __be32 newtarget;
  507. int i = 0, done = 0, ret = count;
  508. struct bonding *bond = to_bond(d);
  509. __be32 *targets;
  510. targets = bond->params.arp_targets;
  511. newtarget = in_aton(buf + 1);
  512. /* look for adds */
  513. if (buf[0] == '+') {
  514. if ((newtarget == 0) || (newtarget == htonl(INADDR_BROADCAST))) {
  515. pr_err("%s: invalid ARP target %pI4 specified for addition\n",
  516. bond->dev->name, &newtarget);
  517. ret = -EINVAL;
  518. goto out;
  519. }
  520. /* look for an empty slot to put the target in, and check for dupes */
  521. for (i = 0; (i < BOND_MAX_ARP_TARGETS) && !done; i++) {
  522. if (targets[i] == newtarget) { /* duplicate */
  523. pr_err("%s: ARP target %pI4 is already present\n",
  524. bond->dev->name, &newtarget);
  525. ret = -EINVAL;
  526. goto out;
  527. }
  528. if (targets[i] == 0) {
  529. pr_info("%s: adding ARP target %pI4.\n",
  530. bond->dev->name, &newtarget);
  531. done = 1;
  532. targets[i] = newtarget;
  533. }
  534. }
  535. if (!done) {
  536. pr_err("%s: ARP target table is full!\n",
  537. bond->dev->name);
  538. ret = -EINVAL;
  539. goto out;
  540. }
  541. } else if (buf[0] == '-') {
  542. if ((newtarget == 0) || (newtarget == htonl(INADDR_BROADCAST))) {
  543. pr_err("%s: invalid ARP target %pI4 specified for removal\n",
  544. bond->dev->name, &newtarget);
  545. ret = -EINVAL;
  546. goto out;
  547. }
  548. for (i = 0; (i < BOND_MAX_ARP_TARGETS) && !done; i++) {
  549. if (targets[i] == newtarget) {
  550. int j;
  551. pr_info("%s: removing ARP target %pI4.\n",
  552. bond->dev->name, &newtarget);
  553. for (j = i; (j < (BOND_MAX_ARP_TARGETS-1)) && targets[j+1]; j++)
  554. targets[j] = targets[j+1];
  555. targets[j] = 0;
  556. done = 1;
  557. }
  558. }
  559. if (!done) {
  560. pr_info("%s: unable to remove nonexistent ARP target %pI4.\n",
  561. bond->dev->name, &newtarget);
  562. ret = -EINVAL;
  563. goto out;
  564. }
  565. } else {
  566. pr_err("no command found in arp_ip_targets file for bond %s. Use +<addr> or -<addr>.\n",
  567. bond->dev->name);
  568. ret = -EPERM;
  569. goto out;
  570. }
  571. out:
  572. return ret;
  573. }
  574. static DEVICE_ATTR(arp_ip_target, S_IRUGO | S_IWUSR , bonding_show_arp_targets, bonding_store_arp_targets);
  575. /*
  576. * Show and set the up and down delays. These must be multiples of the
  577. * MII monitoring value, and are stored internally as the multiplier.
  578. * Thus, we must translate to MS for the real world.
  579. */
  580. static ssize_t bonding_show_downdelay(struct device *d,
  581. struct device_attribute *attr,
  582. char *buf)
  583. {
  584. struct bonding *bond = to_bond(d);
  585. return sprintf(buf, "%d\n", bond->params.downdelay * bond->params.miimon);
  586. }
  587. static ssize_t bonding_store_downdelay(struct device *d,
  588. struct device_attribute *attr,
  589. const char *buf, size_t count)
  590. {
  591. int new_value, ret = count;
  592. struct bonding *bond = to_bond(d);
  593. if (!(bond->params.miimon)) {
  594. pr_err("%s: Unable to set down delay as MII monitoring is disabled\n",
  595. bond->dev->name);
  596. ret = -EPERM;
  597. goto out;
  598. }
  599. if (sscanf(buf, "%d", &new_value) != 1) {
  600. pr_err("%s: no down delay value specified.\n", bond->dev->name);
  601. ret = -EINVAL;
  602. goto out;
  603. }
  604. if (new_value < 0) {
  605. pr_err("%s: Invalid down delay value %d not in range %d-%d; rejected.\n",
  606. bond->dev->name, new_value, 1, INT_MAX);
  607. ret = -EINVAL;
  608. goto out;
  609. } else {
  610. if ((new_value % bond->params.miimon) != 0) {
  611. pr_warning("%s: Warning: down delay (%d) is not a multiple of miimon (%d), delay rounded to %d ms\n",
  612. bond->dev->name, new_value,
  613. bond->params.miimon,
  614. (new_value / bond->params.miimon) *
  615. bond->params.miimon);
  616. }
  617. bond->params.downdelay = new_value / bond->params.miimon;
  618. pr_info("%s: Setting down delay to %d.\n",
  619. bond->dev->name,
  620. bond->params.downdelay * bond->params.miimon);
  621. }
  622. out:
  623. return ret;
  624. }
  625. static DEVICE_ATTR(downdelay, S_IRUGO | S_IWUSR,
  626. bonding_show_downdelay, bonding_store_downdelay);
  627. static ssize_t bonding_show_updelay(struct device *d,
  628. struct device_attribute *attr,
  629. char *buf)
  630. {
  631. struct bonding *bond = to_bond(d);
  632. return sprintf(buf, "%d\n", bond->params.updelay * bond->params.miimon);
  633. }
  634. static ssize_t bonding_store_updelay(struct device *d,
  635. struct device_attribute *attr,
  636. const char *buf, size_t count)
  637. {
  638. int new_value, ret = count;
  639. struct bonding *bond = to_bond(d);
  640. if (!(bond->params.miimon)) {
  641. pr_err("%s: Unable to set up delay as MII monitoring is disabled\n",
  642. bond->dev->name);
  643. ret = -EPERM;
  644. goto out;
  645. }
  646. if (sscanf(buf, "%d", &new_value) != 1) {
  647. pr_err("%s: no up delay value specified.\n",
  648. bond->dev->name);
  649. ret = -EINVAL;
  650. goto out;
  651. }
  652. if (new_value < 0) {
  653. pr_err("%s: Invalid down delay value %d not in range %d-%d; rejected.\n",
  654. bond->dev->name, new_value, 1, INT_MAX);
  655. ret = -EINVAL;
  656. goto out;
  657. } else {
  658. if ((new_value % bond->params.miimon) != 0) {
  659. pr_warning("%s: Warning: up delay (%d) is not a multiple of miimon (%d), updelay rounded to %d ms\n",
  660. bond->dev->name, new_value,
  661. bond->params.miimon,
  662. (new_value / bond->params.miimon) *
  663. bond->params.miimon);
  664. }
  665. bond->params.updelay = new_value / bond->params.miimon;
  666. pr_info("%s: Setting up delay to %d.\n",
  667. bond->dev->name,
  668. bond->params.updelay * bond->params.miimon);
  669. }
  670. out:
  671. return ret;
  672. }
  673. static DEVICE_ATTR(updelay, S_IRUGO | S_IWUSR,
  674. bonding_show_updelay, bonding_store_updelay);
  675. /*
  676. * Show and set the LACP interval. Interface must be down, and the mode
  677. * must be set to 802.3ad mode.
  678. */
  679. static ssize_t bonding_show_lacp(struct device *d,
  680. struct device_attribute *attr,
  681. char *buf)
  682. {
  683. struct bonding *bond = to_bond(d);
  684. return sprintf(buf, "%s %d\n",
  685. bond_lacp_tbl[bond->params.lacp_fast].modename,
  686. bond->params.lacp_fast);
  687. }
  688. static ssize_t bonding_store_lacp(struct device *d,
  689. struct device_attribute *attr,
  690. const char *buf, size_t count)
  691. {
  692. int new_value, ret = count;
  693. struct bonding *bond = to_bond(d);
  694. if (bond->dev->flags & IFF_UP) {
  695. pr_err("%s: Unable to update LACP rate because interface is up.\n",
  696. bond->dev->name);
  697. ret = -EPERM;
  698. goto out;
  699. }
  700. if (bond->params.mode != BOND_MODE_8023AD) {
  701. pr_err("%s: Unable to update LACP rate because bond is not in 802.3ad mode.\n",
  702. bond->dev->name);
  703. ret = -EPERM;
  704. goto out;
  705. }
  706. new_value = bond_parse_parm(buf, bond_lacp_tbl);
  707. if ((new_value == 1) || (new_value == 0)) {
  708. bond->params.lacp_fast = new_value;
  709. pr_info("%s: Setting LACP rate to %s (%d).\n",
  710. bond->dev->name, bond_lacp_tbl[new_value].modename,
  711. new_value);
  712. } else {
  713. pr_err("%s: Ignoring invalid LACP rate value %.*s.\n",
  714. bond->dev->name, (int)strlen(buf) - 1, buf);
  715. ret = -EINVAL;
  716. }
  717. out:
  718. return ret;
  719. }
  720. static DEVICE_ATTR(lacp_rate, S_IRUGO | S_IWUSR,
  721. bonding_show_lacp, bonding_store_lacp);
  722. static ssize_t bonding_show_ad_select(struct device *d,
  723. struct device_attribute *attr,
  724. char *buf)
  725. {
  726. struct bonding *bond = to_bond(d);
  727. return sprintf(buf, "%s %d\n",
  728. ad_select_tbl[bond->params.ad_select].modename,
  729. bond->params.ad_select);
  730. }
  731. static ssize_t bonding_store_ad_select(struct device *d,
  732. struct device_attribute *attr,
  733. const char *buf, size_t count)
  734. {
  735. int new_value, ret = count;
  736. struct bonding *bond = to_bond(d);
  737. if (bond->dev->flags & IFF_UP) {
  738. pr_err("%s: Unable to update ad_select because interface is up.\n",
  739. bond->dev->name);
  740. ret = -EPERM;
  741. goto out;
  742. }
  743. new_value = bond_parse_parm(buf, ad_select_tbl);
  744. if (new_value != -1) {
  745. bond->params.ad_select = new_value;
  746. pr_info("%s: Setting ad_select to %s (%d).\n",
  747. bond->dev->name, ad_select_tbl[new_value].modename,
  748. new_value);
  749. } else {
  750. pr_err("%s: Ignoring invalid ad_select value %.*s.\n",
  751. bond->dev->name, (int)strlen(buf) - 1, buf);
  752. ret = -EINVAL;
  753. }
  754. out:
  755. return ret;
  756. }
  757. static DEVICE_ATTR(ad_select, S_IRUGO | S_IWUSR,
  758. bonding_show_ad_select, bonding_store_ad_select);
  759. /*
  760. * Show and set the number of peer notifications to send after a failover event.
  761. */
  762. static ssize_t bonding_show_num_peer_notif(struct device *d,
  763. struct device_attribute *attr,
  764. char *buf)
  765. {
  766. struct bonding *bond = to_bond(d);
  767. return sprintf(buf, "%d\n", bond->params.num_peer_notif);
  768. }
  769. static ssize_t bonding_store_num_peer_notif(struct device *d,
  770. struct device_attribute *attr,
  771. const char *buf, size_t count)
  772. {
  773. struct bonding *bond = to_bond(d);
  774. int err = kstrtou8(buf, 10, &bond->params.num_peer_notif);
  775. return err ? err : count;
  776. }
  777. static DEVICE_ATTR(num_grat_arp, S_IRUGO | S_IWUSR,
  778. bonding_show_num_peer_notif, bonding_store_num_peer_notif);
  779. static DEVICE_ATTR(num_unsol_na, S_IRUGO | S_IWUSR,
  780. bonding_show_num_peer_notif, bonding_store_num_peer_notif);
  781. /*
  782. * Show and set the MII monitor interval. There are two tricky bits
  783. * here. First, if MII monitoring is activated, then we must disable
  784. * ARP monitoring. Second, if the timer isn't running, we must
  785. * start it.
  786. */
  787. static ssize_t bonding_show_miimon(struct device *d,
  788. struct device_attribute *attr,
  789. char *buf)
  790. {
  791. struct bonding *bond = to_bond(d);
  792. return sprintf(buf, "%d\n", bond->params.miimon);
  793. }
  794. static ssize_t bonding_store_miimon(struct device *d,
  795. struct device_attribute *attr,
  796. const char *buf, size_t count)
  797. {
  798. int new_value, ret = count;
  799. struct bonding *bond = to_bond(d);
  800. if (sscanf(buf, "%d", &new_value) != 1) {
  801. pr_err("%s: no miimon value specified.\n",
  802. bond->dev->name);
  803. ret = -EINVAL;
  804. goto out;
  805. }
  806. if (new_value < 0) {
  807. pr_err("%s: Invalid miimon value %d not in range %d-%d; rejected.\n",
  808. bond->dev->name, new_value, 1, INT_MAX);
  809. ret = -EINVAL;
  810. goto out;
  811. } else {
  812. pr_info("%s: Setting MII monitoring interval to %d.\n",
  813. bond->dev->name, new_value);
  814. bond->params.miimon = new_value;
  815. if (bond->params.updelay)
  816. pr_info("%s: Note: Updating updelay (to %d) since it is a multiple of the miimon value.\n",
  817. bond->dev->name,
  818. bond->params.updelay * bond->params.miimon);
  819. if (bond->params.downdelay)
  820. pr_info("%s: Note: Updating downdelay (to %d) since it is a multiple of the miimon value.\n",
  821. bond->dev->name,
  822. bond->params.downdelay * bond->params.miimon);
  823. if (bond->params.arp_interval) {
  824. pr_info("%s: MII monitoring cannot be used with ARP monitoring. Disabling ARP monitoring...\n",
  825. bond->dev->name);
  826. bond->params.arp_interval = 0;
  827. if (bond->params.arp_validate) {
  828. bond->params.arp_validate =
  829. BOND_ARP_VALIDATE_NONE;
  830. }
  831. if (delayed_work_pending(&bond->arp_work)) {
  832. cancel_delayed_work(&bond->arp_work);
  833. flush_workqueue(bond->wq);
  834. }
  835. }
  836. if (bond->dev->flags & IFF_UP) {
  837. /* If the interface is up, we may need to fire off
  838. * the MII timer. If the interface is down, the
  839. * timer will get fired off when the open function
  840. * is called.
  841. */
  842. if (!delayed_work_pending(&bond->mii_work)) {
  843. INIT_DELAYED_WORK(&bond->mii_work,
  844. bond_mii_monitor);
  845. queue_delayed_work(bond->wq,
  846. &bond->mii_work, 0);
  847. }
  848. }
  849. }
  850. out:
  851. return ret;
  852. }
  853. static DEVICE_ATTR(miimon, S_IRUGO | S_IWUSR,
  854. bonding_show_miimon, bonding_store_miimon);
  855. /*
  856. * Show and set the primary slave. The store function is much
  857. * simpler than bonding_store_slaves function because it only needs to
  858. * handle one interface name.
  859. * The bond must be a mode that supports a primary for this be
  860. * set.
  861. */
  862. static ssize_t bonding_show_primary(struct device *d,
  863. struct device_attribute *attr,
  864. char *buf)
  865. {
  866. int count = 0;
  867. struct bonding *bond = to_bond(d);
  868. if (bond->primary_slave)
  869. count = sprintf(buf, "%s\n", bond->primary_slave->dev->name);
  870. return count;
  871. }
  872. static ssize_t bonding_store_primary(struct device *d,
  873. struct device_attribute *attr,
  874. const char *buf, size_t count)
  875. {
  876. int i;
  877. struct slave *slave;
  878. struct bonding *bond = to_bond(d);
  879. char ifname[IFNAMSIZ];
  880. if (!rtnl_trylock())
  881. return restart_syscall();
  882. block_netpoll_tx();
  883. read_lock(&bond->lock);
  884. write_lock_bh(&bond->curr_slave_lock);
  885. if (!USES_PRIMARY(bond->params.mode)) {
  886. pr_info("%s: Unable to set primary slave; %s is in mode %d\n",
  887. bond->dev->name, bond->dev->name, bond->params.mode);
  888. goto out;
  889. }
  890. sscanf(buf, "%16s", ifname); /* IFNAMSIZ */
  891. /* check to see if we are clearing primary */
  892. if (!strlen(ifname) || buf[0] == '\n') {
  893. pr_info("%s: Setting primary slave to None.\n",
  894. bond->dev->name);
  895. bond->primary_slave = NULL;
  896. bond_select_active_slave(bond);
  897. goto out;
  898. }
  899. bond_for_each_slave(bond, slave, i) {
  900. if (strncmp(slave->dev->name, ifname, IFNAMSIZ) == 0) {
  901. pr_info("%s: Setting %s as primary slave.\n",
  902. bond->dev->name, slave->dev->name);
  903. bond->primary_slave = slave;
  904. strcpy(bond->params.primary, slave->dev->name);
  905. bond_select_active_slave(bond);
  906. goto out;
  907. }
  908. }
  909. pr_info("%s: Unable to set %.*s as primary slave.\n",
  910. bond->dev->name, (int)strlen(buf) - 1, buf);
  911. out:
  912. write_unlock_bh(&bond->curr_slave_lock);
  913. read_unlock(&bond->lock);
  914. unblock_netpoll_tx();
  915. rtnl_unlock();
  916. return count;
  917. }
  918. static DEVICE_ATTR(primary, S_IRUGO | S_IWUSR,
  919. bonding_show_primary, bonding_store_primary);
  920. /*
  921. * Show and set the primary_reselect flag.
  922. */
  923. static ssize_t bonding_show_primary_reselect(struct device *d,
  924. struct device_attribute *attr,
  925. char *buf)
  926. {
  927. struct bonding *bond = to_bond(d);
  928. return sprintf(buf, "%s %d\n",
  929. pri_reselect_tbl[bond->params.primary_reselect].modename,
  930. bond->params.primary_reselect);
  931. }
  932. static ssize_t bonding_store_primary_reselect(struct device *d,
  933. struct device_attribute *attr,
  934. const char *buf, size_t count)
  935. {
  936. int new_value, ret = count;
  937. struct bonding *bond = to_bond(d);
  938. if (!rtnl_trylock())
  939. return restart_syscall();
  940. new_value = bond_parse_parm(buf, pri_reselect_tbl);
  941. if (new_value < 0) {
  942. pr_err("%s: Ignoring invalid primary_reselect value %.*s.\n",
  943. bond->dev->name,
  944. (int) strlen(buf) - 1, buf);
  945. ret = -EINVAL;
  946. goto out;
  947. }
  948. bond->params.primary_reselect = new_value;
  949. pr_info("%s: setting primary_reselect to %s (%d).\n",
  950. bond->dev->name, pri_reselect_tbl[new_value].modename,
  951. new_value);
  952. block_netpoll_tx();
  953. read_lock(&bond->lock);
  954. write_lock_bh(&bond->curr_slave_lock);
  955. bond_select_active_slave(bond);
  956. write_unlock_bh(&bond->curr_slave_lock);
  957. read_unlock(&bond->lock);
  958. unblock_netpoll_tx();
  959. out:
  960. rtnl_unlock();
  961. return ret;
  962. }
  963. static DEVICE_ATTR(primary_reselect, S_IRUGO | S_IWUSR,
  964. bonding_show_primary_reselect,
  965. bonding_store_primary_reselect);
  966. /*
  967. * Show and set the use_carrier flag.
  968. */
  969. static ssize_t bonding_show_carrier(struct device *d,
  970. struct device_attribute *attr,
  971. char *buf)
  972. {
  973. struct bonding *bond = to_bond(d);
  974. return sprintf(buf, "%d\n", bond->params.use_carrier);
  975. }
  976. static ssize_t bonding_store_carrier(struct device *d,
  977. struct device_attribute *attr,
  978. const char *buf, size_t count)
  979. {
  980. int new_value, ret = count;
  981. struct bonding *bond = to_bond(d);
  982. if (sscanf(buf, "%d", &new_value) != 1) {
  983. pr_err("%s: no use_carrier value specified.\n",
  984. bond->dev->name);
  985. ret = -EINVAL;
  986. goto out;
  987. }
  988. if ((new_value == 0) || (new_value == 1)) {
  989. bond->params.use_carrier = new_value;
  990. pr_info("%s: Setting use_carrier to %d.\n",
  991. bond->dev->name, new_value);
  992. } else {
  993. pr_info("%s: Ignoring invalid use_carrier value %d.\n",
  994. bond->dev->name, new_value);
  995. }
  996. out:
  997. return ret;
  998. }
  999. static DEVICE_ATTR(use_carrier, S_IRUGO | S_IWUSR,
  1000. bonding_show_carrier, bonding_store_carrier);
  1001. /*
  1002. * Show and set currently active_slave.
  1003. */
  1004. static ssize_t bonding_show_active_slave(struct device *d,
  1005. struct device_attribute *attr,
  1006. char *buf)
  1007. {
  1008. struct slave *curr;
  1009. struct bonding *bond = to_bond(d);
  1010. int count = 0;
  1011. read_lock(&bond->curr_slave_lock);
  1012. curr = bond->curr_active_slave;
  1013. read_unlock(&bond->curr_slave_lock);
  1014. if (USES_PRIMARY(bond->params.mode) && curr)
  1015. count = sprintf(buf, "%s\n", curr->dev->name);
  1016. return count;
  1017. }
  1018. static ssize_t bonding_store_active_slave(struct device *d,
  1019. struct device_attribute *attr,
  1020. const char *buf, size_t count)
  1021. {
  1022. int i;
  1023. struct slave *slave;
  1024. struct slave *old_active = NULL;
  1025. struct slave *new_active = NULL;
  1026. struct bonding *bond = to_bond(d);
  1027. char ifname[IFNAMSIZ];
  1028. if (!rtnl_trylock())
  1029. return restart_syscall();
  1030. block_netpoll_tx();
  1031. read_lock(&bond->lock);
  1032. write_lock_bh(&bond->curr_slave_lock);
  1033. if (!USES_PRIMARY(bond->params.mode)) {
  1034. pr_info("%s: Unable to change active slave; %s is in mode %d\n",
  1035. bond->dev->name, bond->dev->name, bond->params.mode);
  1036. goto out;
  1037. }
  1038. sscanf(buf, "%16s", ifname); /* IFNAMSIZ */
  1039. /* check to see if we are clearing active */
  1040. if (!strlen(ifname) || buf[0] == '\n') {
  1041. pr_info("%s: Clearing current active slave.\n",
  1042. bond->dev->name);
  1043. bond->curr_active_slave = NULL;
  1044. bond_select_active_slave(bond);
  1045. goto out;
  1046. }
  1047. bond_for_each_slave(bond, slave, i) {
  1048. if (strncmp(slave->dev->name, ifname, IFNAMSIZ) == 0) {
  1049. old_active = bond->curr_active_slave;
  1050. new_active = slave;
  1051. if (new_active == old_active) {
  1052. /* do nothing */
  1053. pr_info("%s: %s is already the current"
  1054. " active slave.\n",
  1055. bond->dev->name,
  1056. slave->dev->name);
  1057. goto out;
  1058. }
  1059. else {
  1060. if ((new_active) &&
  1061. (old_active) &&
  1062. (new_active->link == BOND_LINK_UP) &&
  1063. IS_UP(new_active->dev)) {
  1064. pr_info("%s: Setting %s as active"
  1065. " slave.\n",
  1066. bond->dev->name,
  1067. slave->dev->name);
  1068. bond_change_active_slave(bond,
  1069. new_active);
  1070. }
  1071. else {
  1072. pr_info("%s: Could not set %s as"
  1073. " active slave; either %s is"
  1074. " down or the link is down.\n",
  1075. bond->dev->name,
  1076. slave->dev->name,
  1077. slave->dev->name);
  1078. }
  1079. goto out;
  1080. }
  1081. }
  1082. }
  1083. pr_info("%s: Unable to set %.*s as active slave.\n",
  1084. bond->dev->name, (int)strlen(buf) - 1, buf);
  1085. out:
  1086. write_unlock_bh(&bond->curr_slave_lock);
  1087. read_unlock(&bond->lock);
  1088. unblock_netpoll_tx();
  1089. rtnl_unlock();
  1090. return count;
  1091. }
  1092. static DEVICE_ATTR(active_slave, S_IRUGO | S_IWUSR,
  1093. bonding_show_active_slave, bonding_store_active_slave);
  1094. /*
  1095. * Show link status of the bond interface.
  1096. */
  1097. static ssize_t bonding_show_mii_status(struct device *d,
  1098. struct device_attribute *attr,
  1099. char *buf)
  1100. {
  1101. struct slave *curr;
  1102. struct bonding *bond = to_bond(d);
  1103. read_lock(&bond->curr_slave_lock);
  1104. curr = bond->curr_active_slave;
  1105. read_unlock(&bond->curr_slave_lock);
  1106. return sprintf(buf, "%s\n", curr ? "up" : "down");
  1107. }
  1108. static DEVICE_ATTR(mii_status, S_IRUGO, bonding_show_mii_status, NULL);
  1109. /*
  1110. * Show current 802.3ad aggregator ID.
  1111. */
  1112. static ssize_t bonding_show_ad_aggregator(struct device *d,
  1113. struct device_attribute *attr,
  1114. char *buf)
  1115. {
  1116. int count = 0;
  1117. struct bonding *bond = to_bond(d);
  1118. if (bond->params.mode == BOND_MODE_8023AD) {
  1119. struct ad_info ad_info;
  1120. count = sprintf(buf, "%d\n",
  1121. (bond_3ad_get_active_agg_info(bond, &ad_info))
  1122. ? 0 : ad_info.aggregator_id);
  1123. }
  1124. return count;
  1125. }
  1126. static DEVICE_ATTR(ad_aggregator, S_IRUGO, bonding_show_ad_aggregator, NULL);
  1127. /*
  1128. * Show number of active 802.3ad ports.
  1129. */
  1130. static ssize_t bonding_show_ad_num_ports(struct device *d,
  1131. struct device_attribute *attr,
  1132. char *buf)
  1133. {
  1134. int count = 0;
  1135. struct bonding *bond = to_bond(d);
  1136. if (bond->params.mode == BOND_MODE_8023AD) {
  1137. struct ad_info ad_info;
  1138. count = sprintf(buf, "%d\n",
  1139. (bond_3ad_get_active_agg_info(bond, &ad_info))
  1140. ? 0 : ad_info.ports);
  1141. }
  1142. return count;
  1143. }
  1144. static DEVICE_ATTR(ad_num_ports, S_IRUGO, bonding_show_ad_num_ports, NULL);
  1145. /*
  1146. * Show current 802.3ad actor key.
  1147. */
  1148. static ssize_t bonding_show_ad_actor_key(struct device *d,
  1149. struct device_attribute *attr,
  1150. char *buf)
  1151. {
  1152. int count = 0;
  1153. struct bonding *bond = to_bond(d);
  1154. if (bond->params.mode == BOND_MODE_8023AD) {
  1155. struct ad_info ad_info;
  1156. count = sprintf(buf, "%d\n",
  1157. (bond_3ad_get_active_agg_info(bond, &ad_info))
  1158. ? 0 : ad_info.actor_key);
  1159. }
  1160. return count;
  1161. }
  1162. static DEVICE_ATTR(ad_actor_key, S_IRUGO, bonding_show_ad_actor_key, NULL);
  1163. /*
  1164. * Show current 802.3ad partner key.
  1165. */
  1166. static ssize_t bonding_show_ad_partner_key(struct device *d,
  1167. struct device_attribute *attr,
  1168. char *buf)
  1169. {
  1170. int count = 0;
  1171. struct bonding *bond = to_bond(d);
  1172. if (bond->params.mode == BOND_MODE_8023AD) {
  1173. struct ad_info ad_info;
  1174. count = sprintf(buf, "%d\n",
  1175. (bond_3ad_get_active_agg_info(bond, &ad_info))
  1176. ? 0 : ad_info.partner_key);
  1177. }
  1178. return count;
  1179. }
  1180. static DEVICE_ATTR(ad_partner_key, S_IRUGO, bonding_show_ad_partner_key, NULL);
  1181. /*
  1182. * Show current 802.3ad partner mac.
  1183. */
  1184. static ssize_t bonding_show_ad_partner_mac(struct device *d,
  1185. struct device_attribute *attr,
  1186. char *buf)
  1187. {
  1188. int count = 0;
  1189. struct bonding *bond = to_bond(d);
  1190. if (bond->params.mode == BOND_MODE_8023AD) {
  1191. struct ad_info ad_info;
  1192. if (!bond_3ad_get_active_agg_info(bond, &ad_info))
  1193. count = sprintf(buf, "%pM\n", ad_info.partner_system);
  1194. }
  1195. return count;
  1196. }
  1197. static DEVICE_ATTR(ad_partner_mac, S_IRUGO, bonding_show_ad_partner_mac, NULL);
  1198. /*
  1199. * Show the queue_ids of the slaves in the current bond.
  1200. */
  1201. static ssize_t bonding_show_queue_id(struct device *d,
  1202. struct device_attribute *attr,
  1203. char *buf)
  1204. {
  1205. struct slave *slave;
  1206. int i, res = 0;
  1207. struct bonding *bond = to_bond(d);
  1208. if (!rtnl_trylock())
  1209. return restart_syscall();
  1210. read_lock(&bond->lock);
  1211. bond_for_each_slave(bond, slave, i) {
  1212. if (res > (PAGE_SIZE - IFNAMSIZ - 6)) {
  1213. /* not enough space for another interface_name:queue_id pair */
  1214. if ((PAGE_SIZE - res) > 10)
  1215. res = PAGE_SIZE - 10;
  1216. res += sprintf(buf + res, "++more++ ");
  1217. break;
  1218. }
  1219. res += sprintf(buf + res, "%s:%d ",
  1220. slave->dev->name, slave->queue_id);
  1221. }
  1222. read_unlock(&bond->lock);
  1223. if (res)
  1224. buf[res-1] = '\n'; /* eat the leftover space */
  1225. rtnl_unlock();
  1226. return res;
  1227. }
  1228. /*
  1229. * Set the queue_ids of the slaves in the current bond. The bond
  1230. * interface must be enslaved for this to work.
  1231. */
  1232. static ssize_t bonding_store_queue_id(struct device *d,
  1233. struct device_attribute *attr,
  1234. const char *buffer, size_t count)
  1235. {
  1236. struct slave *slave, *update_slave;
  1237. struct bonding *bond = to_bond(d);
  1238. u16 qid;
  1239. int i, ret = count;
  1240. char *delim;
  1241. struct net_device *sdev = NULL;
  1242. if (!rtnl_trylock())
  1243. return restart_syscall();
  1244. /* delim will point to queue id if successful */
  1245. delim = strchr(buffer, ':');
  1246. if (!delim)
  1247. goto err_no_cmd;
  1248. /*
  1249. * Terminate string that points to device name and bump it
  1250. * up one, so we can read the queue id there.
  1251. */
  1252. *delim = '\0';
  1253. if (sscanf(++delim, "%hd\n", &qid) != 1)
  1254. goto err_no_cmd;
  1255. /* Check buffer length, valid ifname and queue id */
  1256. if (strlen(buffer) > IFNAMSIZ ||
  1257. !dev_valid_name(buffer) ||
  1258. qid > bond->params.tx_queues)
  1259. goto err_no_cmd;
  1260. /* Get the pointer to that interface if it exists */
  1261. sdev = __dev_get_by_name(dev_net(bond->dev), buffer);
  1262. if (!sdev)
  1263. goto err_no_cmd;
  1264. read_lock(&bond->lock);
  1265. /* Search for thes slave and check for duplicate qids */
  1266. update_slave = NULL;
  1267. bond_for_each_slave(bond, slave, i) {
  1268. if (sdev == slave->dev)
  1269. /*
  1270. * We don't need to check the matching
  1271. * slave for dups, since we're overwriting it
  1272. */
  1273. update_slave = slave;
  1274. else if (qid && qid == slave->queue_id) {
  1275. goto err_no_cmd_unlock;
  1276. }
  1277. }
  1278. if (!update_slave)
  1279. goto err_no_cmd_unlock;
  1280. /* Actually set the qids for the slave */
  1281. update_slave->queue_id = qid;
  1282. read_unlock(&bond->lock);
  1283. out:
  1284. rtnl_unlock();
  1285. return ret;
  1286. err_no_cmd_unlock:
  1287. read_unlock(&bond->lock);
  1288. err_no_cmd:
  1289. pr_info("invalid input for queue_id set for %s.\n",
  1290. bond->dev->name);
  1291. ret = -EPERM;
  1292. goto out;
  1293. }
  1294. static DEVICE_ATTR(queue_id, S_IRUGO | S_IWUSR, bonding_show_queue_id,
  1295. bonding_store_queue_id);
  1296. /*
  1297. * Show and set the all_slaves_active flag.
  1298. */
  1299. static ssize_t bonding_show_slaves_active(struct device *d,
  1300. struct device_attribute *attr,
  1301. char *buf)
  1302. {
  1303. struct bonding *bond = to_bond(d);
  1304. return sprintf(buf, "%d\n", bond->params.all_slaves_active);
  1305. }
  1306. static ssize_t bonding_store_slaves_active(struct device *d,
  1307. struct device_attribute *attr,
  1308. const char *buf, size_t count)
  1309. {
  1310. int i, new_value, ret = count;
  1311. struct bonding *bond = to_bond(d);
  1312. struct slave *slave;
  1313. if (sscanf(buf, "%d", &new_value) != 1) {
  1314. pr_err("%s: no all_slaves_active value specified.\n",
  1315. bond->dev->name);
  1316. ret = -EINVAL;
  1317. goto out;
  1318. }
  1319. if (new_value == bond->params.all_slaves_active)
  1320. goto out;
  1321. if ((new_value == 0) || (new_value == 1)) {
  1322. bond->params.all_slaves_active = new_value;
  1323. } else {
  1324. pr_info("%s: Ignoring invalid all_slaves_active value %d.\n",
  1325. bond->dev->name, new_value);
  1326. ret = -EINVAL;
  1327. goto out;
  1328. }
  1329. bond_for_each_slave(bond, slave, i) {
  1330. if (!bond_is_active_slave(slave)) {
  1331. if (new_value)
  1332. slave->inactive = 0;
  1333. else
  1334. slave->inactive = 1;
  1335. }
  1336. }
  1337. out:
  1338. return ret;
  1339. }
  1340. static DEVICE_ATTR(all_slaves_active, S_IRUGO | S_IWUSR,
  1341. bonding_show_slaves_active, bonding_store_slaves_active);
  1342. /*
  1343. * Show and set the number of IGMP membership reports to send on link failure
  1344. */
  1345. static ssize_t bonding_show_resend_igmp(struct device *d,
  1346. struct device_attribute *attr,
  1347. char *buf)
  1348. {
  1349. struct bonding *bond = to_bond(d);
  1350. return sprintf(buf, "%d\n", bond->params.resend_igmp);
  1351. }
  1352. static ssize_t bonding_store_resend_igmp(struct device *d,
  1353. struct device_attribute *attr,
  1354. const char *buf, size_t count)
  1355. {
  1356. int new_value, ret = count;
  1357. struct bonding *bond = to_bond(d);
  1358. if (sscanf(buf, "%d", &new_value) != 1) {
  1359. pr_err("%s: no resend_igmp value specified.\n",
  1360. bond->dev->name);
  1361. ret = -EINVAL;
  1362. goto out;
  1363. }
  1364. if (new_value < 0 || new_value > 255) {
  1365. pr_err("%s: Invalid resend_igmp value %d not in range 0-255; rejected.\n",
  1366. bond->dev->name, new_value);
  1367. ret = -EINVAL;
  1368. goto out;
  1369. }
  1370. pr_info("%s: Setting resend_igmp to %d.\n",
  1371. bond->dev->name, new_value);
  1372. bond->params.resend_igmp = new_value;
  1373. out:
  1374. return ret;
  1375. }
  1376. static DEVICE_ATTR(resend_igmp, S_IRUGO | S_IWUSR,
  1377. bonding_show_resend_igmp, bonding_store_resend_igmp);
  1378. static struct attribute *per_bond_attrs[] = {
  1379. &dev_attr_slaves.attr,
  1380. &dev_attr_mode.attr,
  1381. &dev_attr_fail_over_mac.attr,
  1382. &dev_attr_arp_validate.attr,
  1383. &dev_attr_arp_interval.attr,
  1384. &dev_attr_arp_ip_target.attr,
  1385. &dev_attr_downdelay.attr,
  1386. &dev_attr_updelay.attr,
  1387. &dev_attr_lacp_rate.attr,
  1388. &dev_attr_ad_select.attr,
  1389. &dev_attr_xmit_hash_policy.attr,
  1390. &dev_attr_num_grat_arp.attr,
  1391. &dev_attr_num_unsol_na.attr,
  1392. &dev_attr_miimon.attr,
  1393. &dev_attr_primary.attr,
  1394. &dev_attr_primary_reselect.attr,
  1395. &dev_attr_use_carrier.attr,
  1396. &dev_attr_active_slave.attr,
  1397. &dev_attr_mii_status.attr,
  1398. &dev_attr_ad_aggregator.attr,
  1399. &dev_attr_ad_num_ports.attr,
  1400. &dev_attr_ad_actor_key.attr,
  1401. &dev_attr_ad_partner_key.attr,
  1402. &dev_attr_ad_partner_mac.attr,
  1403. &dev_attr_queue_id.attr,
  1404. &dev_attr_all_slaves_active.attr,
  1405. &dev_attr_resend_igmp.attr,
  1406. NULL,
  1407. };
  1408. static struct attribute_group bonding_group = {
  1409. .name = "bonding",
  1410. .attrs = per_bond_attrs,
  1411. };
  1412. /*
  1413. * Initialize sysfs. This sets up the bonding_masters file in
  1414. * /sys/class/net.
  1415. */
  1416. int bond_create_sysfs(void)
  1417. {
  1418. int ret;
  1419. ret = netdev_class_create_file(&class_attr_bonding_masters);
  1420. /*
  1421. * Permit multiple loads of the module by ignoring failures to
  1422. * create the bonding_masters sysfs file. Bonding devices
  1423. * created by second or subsequent loads of the module will
  1424. * not be listed in, or controllable by, bonding_masters, but
  1425. * will have the usual "bonding" sysfs directory.
  1426. *
  1427. * This is done to preserve backwards compatibility for
  1428. * initscripts/sysconfig, which load bonding multiple times to
  1429. * configure multiple bonding devices.
  1430. */
  1431. if (ret == -EEXIST) {
  1432. /* Is someone being kinky and naming a device bonding_master? */
  1433. if (__dev_get_by_name(&init_net,
  1434. class_attr_bonding_masters.attr.name))
  1435. pr_err("network device named %s already exists in sysfs",
  1436. class_attr_bonding_masters.attr.name);
  1437. ret = 0;
  1438. }
  1439. return ret;
  1440. }
  1441. /*
  1442. * Remove /sys/class/net/bonding_masters.
  1443. */
  1444. void bond_destroy_sysfs(void)
  1445. {
  1446. netdev_class_remove_file(&class_attr_bonding_masters);
  1447. }
  1448. /*
  1449. * Initialize sysfs for each bond. This sets up and registers
  1450. * the 'bondctl' directory for each individual bond under /sys/class/net.
  1451. */
  1452. void bond_prepare_sysfs_group(struct bonding *bond)
  1453. {
  1454. bond->dev->sysfs_groups[0] = &bonding_group;
  1455. }