bond_sysfs.c 40 KB

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