bond_sysfs.c 41 KB

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