bond_sysfs.c 44 KB

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