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