bond_sysfs.c 43 KB

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