bond_sysfs.c 41 KB

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