bond_sysfs.c 44 KB

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