bond_sysfs.c 41 KB

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