bond_sysfs.c 44 KB

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