bond_sysfs.c 42 KB

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