bond_sysfs.c 43 KB

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