bond_sysfs.c 45 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 (!rtnl_trylock())
  276. return restart_syscall();
  277. if (bond->dev->flags & IFF_UP) {
  278. pr_err("unable to update mode of %s because interface is up.\n",
  279. bond->dev->name);
  280. ret = -EPERM;
  281. goto out;
  282. }
  283. if (bond->slave_cnt > 0) {
  284. pr_err("unable to update mode of %s because it has slaves.\n",
  285. bond->dev->name);
  286. ret = -EPERM;
  287. goto out;
  288. }
  289. new_value = bond_parse_parm(buf, bond_mode_tbl);
  290. if (new_value < 0) {
  291. pr_err("%s: Ignoring invalid mode value %.*s.\n",
  292. bond->dev->name, (int)strlen(buf) - 1, buf);
  293. ret = -EINVAL;
  294. goto out;
  295. }
  296. if ((new_value == BOND_MODE_ALB ||
  297. new_value == BOND_MODE_TLB) &&
  298. bond->params.arp_interval) {
  299. pr_err("%s: %s mode is incompatible with arp monitoring.\n",
  300. bond->dev->name, bond_mode_tbl[new_value].modename);
  301. ret = -EINVAL;
  302. goto out;
  303. }
  304. bond->params.mode = new_value;
  305. bond_set_mode_ops(bond, bond->params.mode);
  306. pr_info("%s: setting mode to %s (%d).\n",
  307. bond->dev->name, bond_mode_tbl[new_value].modename,
  308. new_value);
  309. out:
  310. rtnl_unlock();
  311. return ret;
  312. }
  313. static DEVICE_ATTR(mode, S_IRUGO | S_IWUSR,
  314. bonding_show_mode, bonding_store_mode);
  315. /*
  316. * Show and set the bonding transmit hash method.
  317. * The bond interface must be down to change the xmit hash policy.
  318. */
  319. static ssize_t bonding_show_xmit_hash(struct device *d,
  320. struct device_attribute *attr,
  321. char *buf)
  322. {
  323. struct bonding *bond = to_bond(d);
  324. return sprintf(buf, "%s %d\n",
  325. xmit_hashtype_tbl[bond->params.xmit_policy].modename,
  326. bond->params.xmit_policy);
  327. }
  328. static ssize_t bonding_store_xmit_hash(struct device *d,
  329. struct device_attribute *attr,
  330. const char *buf, size_t count)
  331. {
  332. int new_value, ret = count;
  333. struct bonding *bond = to_bond(d);
  334. if (bond->dev->flags & IFF_UP) {
  335. pr_err("%s: Interface is up. Unable to update xmit policy.\n",
  336. bond->dev->name);
  337. ret = -EPERM;
  338. goto out;
  339. }
  340. new_value = bond_parse_parm(buf, xmit_hashtype_tbl);
  341. if (new_value < 0) {
  342. pr_err("%s: Ignoring invalid xmit hash policy value %.*s.\n",
  343. bond->dev->name,
  344. (int)strlen(buf) - 1, buf);
  345. ret = -EINVAL;
  346. goto out;
  347. } else {
  348. bond->params.xmit_policy = new_value;
  349. bond_set_mode_ops(bond, bond->params.mode);
  350. pr_info("%s: setting xmit hash policy to %s (%d).\n",
  351. bond->dev->name,
  352. xmit_hashtype_tbl[new_value].modename, new_value);
  353. }
  354. out:
  355. return ret;
  356. }
  357. static DEVICE_ATTR(xmit_hash_policy, S_IRUGO | S_IWUSR,
  358. bonding_show_xmit_hash, bonding_store_xmit_hash);
  359. /*
  360. * Show and set arp_validate.
  361. */
  362. static ssize_t bonding_show_arp_validate(struct device *d,
  363. struct device_attribute *attr,
  364. char *buf)
  365. {
  366. struct bonding *bond = to_bond(d);
  367. return sprintf(buf, "%s %d\n",
  368. arp_validate_tbl[bond->params.arp_validate].modename,
  369. bond->params.arp_validate);
  370. }
  371. static ssize_t bonding_store_arp_validate(struct device *d,
  372. struct device_attribute *attr,
  373. const char *buf, size_t count)
  374. {
  375. int new_value;
  376. struct bonding *bond = to_bond(d);
  377. new_value = bond_parse_parm(buf, arp_validate_tbl);
  378. if (new_value < 0) {
  379. pr_err("%s: Ignoring invalid arp_validate value %s\n",
  380. bond->dev->name, buf);
  381. return -EINVAL;
  382. }
  383. if (new_value && (bond->params.mode != BOND_MODE_ACTIVEBACKUP)) {
  384. pr_err("%s: arp_validate only supported in active-backup mode.\n",
  385. bond->dev->name);
  386. return -EINVAL;
  387. }
  388. pr_info("%s: setting arp_validate to %s (%d).\n",
  389. bond->dev->name, arp_validate_tbl[new_value].modename,
  390. new_value);
  391. bond->params.arp_validate = new_value;
  392. return count;
  393. }
  394. static DEVICE_ATTR(arp_validate, S_IRUGO | S_IWUSR, bonding_show_arp_validate,
  395. bonding_store_arp_validate);
  396. /*
  397. * Show and store fail_over_mac. User only allowed to change the
  398. * value when there are no slaves.
  399. */
  400. static ssize_t bonding_show_fail_over_mac(struct device *d,
  401. struct device_attribute *attr,
  402. char *buf)
  403. {
  404. struct bonding *bond = to_bond(d);
  405. return sprintf(buf, "%s %d\n",
  406. fail_over_mac_tbl[bond->params.fail_over_mac].modename,
  407. bond->params.fail_over_mac);
  408. }
  409. static ssize_t bonding_store_fail_over_mac(struct device *d,
  410. struct device_attribute *attr,
  411. const char *buf, size_t count)
  412. {
  413. int new_value;
  414. struct bonding *bond = to_bond(d);
  415. if (bond->slave_cnt != 0) {
  416. pr_err("%s: Can't alter fail_over_mac with slaves in bond.\n",
  417. bond->dev->name);
  418. return -EPERM;
  419. }
  420. new_value = bond_parse_parm(buf, fail_over_mac_tbl);
  421. if (new_value < 0) {
  422. pr_err("%s: Ignoring invalid fail_over_mac value %s.\n",
  423. bond->dev->name, buf);
  424. return -EINVAL;
  425. }
  426. bond->params.fail_over_mac = new_value;
  427. pr_info("%s: Setting fail_over_mac to %s (%d).\n",
  428. bond->dev->name, fail_over_mac_tbl[new_value].modename,
  429. new_value);
  430. return count;
  431. }
  432. static DEVICE_ATTR(fail_over_mac, S_IRUGO | S_IWUSR,
  433. bonding_show_fail_over_mac, bonding_store_fail_over_mac);
  434. /*
  435. * Show and set the arp timer interval. There are two tricky bits
  436. * here. First, if ARP monitoring is activated, then we must disable
  437. * MII monitoring. Second, if the ARP timer isn't running, we must
  438. * start it.
  439. */
  440. static ssize_t bonding_show_arp_interval(struct device *d,
  441. struct device_attribute *attr,
  442. char *buf)
  443. {
  444. struct bonding *bond = to_bond(d);
  445. return sprintf(buf, "%d\n", bond->params.arp_interval);
  446. }
  447. static ssize_t bonding_store_arp_interval(struct device *d,
  448. struct device_attribute *attr,
  449. const char *buf, size_t count)
  450. {
  451. int new_value, ret = count;
  452. struct bonding *bond = to_bond(d);
  453. if (!rtnl_trylock())
  454. return restart_syscall();
  455. if (sscanf(buf, "%d", &new_value) != 1) {
  456. pr_err("%s: no arp_interval value specified.\n",
  457. bond->dev->name);
  458. ret = -EINVAL;
  459. goto out;
  460. }
  461. if (new_value < 0) {
  462. pr_err("%s: Invalid arp_interval value %d not in range 0-%d; rejected.\n",
  463. bond->dev->name, new_value, INT_MAX);
  464. ret = -EINVAL;
  465. goto out;
  466. }
  467. if (bond->params.mode == BOND_MODE_ALB ||
  468. bond->params.mode == BOND_MODE_TLB) {
  469. pr_info("%s: ARP monitoring cannot be used with ALB/TLB. Only MII monitoring is supported on %s.\n",
  470. bond->dev->name, bond->dev->name);
  471. ret = -EINVAL;
  472. goto out;
  473. }
  474. pr_info("%s: Setting ARP monitoring interval to %d.\n",
  475. bond->dev->name, new_value);
  476. bond->params.arp_interval = new_value;
  477. if (new_value) {
  478. if (bond->params.miimon) {
  479. pr_info("%s: ARP monitoring cannot be used with MII monitoring. %s Disabling MII monitoring.\n",
  480. bond->dev->name, bond->dev->name);
  481. bond->params.miimon = 0;
  482. }
  483. if (!bond->params.arp_targets[0])
  484. pr_info("%s: ARP monitoring has been set up, but no ARP targets have been specified.\n",
  485. bond->dev->name);
  486. }
  487. if (bond->dev->flags & IFF_UP) {
  488. /* If the interface is up, we may need to fire off
  489. * the ARP timer. If the interface is down, the
  490. * timer will get fired off when the open function
  491. * is called.
  492. */
  493. if (!new_value) {
  494. cancel_delayed_work_sync(&bond->arp_work);
  495. } else {
  496. cancel_delayed_work_sync(&bond->mii_work);
  497. queue_delayed_work(bond->wq, &bond->arp_work, 0);
  498. }
  499. }
  500. out:
  501. rtnl_unlock();
  502. return ret;
  503. }
  504. static DEVICE_ATTR(arp_interval, S_IRUGO | S_IWUSR,
  505. bonding_show_arp_interval, bonding_store_arp_interval);
  506. /*
  507. * Show and set the arp targets.
  508. */
  509. static ssize_t bonding_show_arp_targets(struct device *d,
  510. struct device_attribute *attr,
  511. char *buf)
  512. {
  513. int i, res = 0;
  514. struct bonding *bond = to_bond(d);
  515. for (i = 0; i < BOND_MAX_ARP_TARGETS; i++) {
  516. if (bond->params.arp_targets[i])
  517. res += sprintf(buf + res, "%pI4 ",
  518. &bond->params.arp_targets[i]);
  519. }
  520. if (res)
  521. buf[res-1] = '\n'; /* eat the leftover space */
  522. return res;
  523. }
  524. static ssize_t bonding_store_arp_targets(struct device *d,
  525. struct device_attribute *attr,
  526. const char *buf, size_t count)
  527. {
  528. __be32 newtarget;
  529. int i = 0, done = 0, ret = count;
  530. struct bonding *bond = to_bond(d);
  531. __be32 *targets;
  532. targets = bond->params.arp_targets;
  533. newtarget = in_aton(buf + 1);
  534. /* look for adds */
  535. if (buf[0] == '+') {
  536. if ((newtarget == 0) || (newtarget == htonl(INADDR_BROADCAST))) {
  537. pr_err("%s: invalid ARP target %pI4 specified for addition\n",
  538. bond->dev->name, &newtarget);
  539. ret = -EINVAL;
  540. goto out;
  541. }
  542. /* look for an empty slot to put the target in, and check for dupes */
  543. for (i = 0; (i < BOND_MAX_ARP_TARGETS) && !done; i++) {
  544. if (targets[i] == newtarget) { /* duplicate */
  545. pr_err("%s: ARP target %pI4 is already present\n",
  546. bond->dev->name, &newtarget);
  547. ret = -EINVAL;
  548. goto out;
  549. }
  550. if (targets[i] == 0) {
  551. pr_info("%s: adding ARP target %pI4.\n",
  552. bond->dev->name, &newtarget);
  553. done = 1;
  554. targets[i] = newtarget;
  555. }
  556. }
  557. if (!done) {
  558. pr_err("%s: ARP target table is full!\n",
  559. bond->dev->name);
  560. ret = -EINVAL;
  561. goto out;
  562. }
  563. } else if (buf[0] == '-') {
  564. if ((newtarget == 0) || (newtarget == htonl(INADDR_BROADCAST))) {
  565. pr_err("%s: invalid ARP target %pI4 specified for removal\n",
  566. bond->dev->name, &newtarget);
  567. ret = -EINVAL;
  568. goto out;
  569. }
  570. for (i = 0; (i < BOND_MAX_ARP_TARGETS) && !done; i++) {
  571. if (targets[i] == newtarget) {
  572. int j;
  573. pr_info("%s: removing ARP target %pI4.\n",
  574. bond->dev->name, &newtarget);
  575. for (j = i; (j < (BOND_MAX_ARP_TARGETS-1)) && targets[j+1]; j++)
  576. targets[j] = targets[j+1];
  577. targets[j] = 0;
  578. done = 1;
  579. }
  580. }
  581. if (!done) {
  582. pr_info("%s: unable to remove nonexistent ARP target %pI4.\n",
  583. bond->dev->name, &newtarget);
  584. ret = -EINVAL;
  585. goto out;
  586. }
  587. } else {
  588. pr_err("no command found in arp_ip_targets file for bond %s. Use +<addr> or -<addr>.\n",
  589. bond->dev->name);
  590. ret = -EPERM;
  591. goto out;
  592. }
  593. out:
  594. return ret;
  595. }
  596. static DEVICE_ATTR(arp_ip_target, S_IRUGO | S_IWUSR , bonding_show_arp_targets, bonding_store_arp_targets);
  597. /*
  598. * Show and set the up and down delays. These must be multiples of the
  599. * MII monitoring value, and are stored internally as the multiplier.
  600. * Thus, we must translate to MS for the real world.
  601. */
  602. static ssize_t bonding_show_downdelay(struct device *d,
  603. struct device_attribute *attr,
  604. char *buf)
  605. {
  606. struct bonding *bond = to_bond(d);
  607. return sprintf(buf, "%d\n", bond->params.downdelay * bond->params.miimon);
  608. }
  609. static ssize_t bonding_store_downdelay(struct device *d,
  610. struct device_attribute *attr,
  611. const char *buf, size_t count)
  612. {
  613. int new_value, ret = count;
  614. struct bonding *bond = to_bond(d);
  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. return ret;
  646. }
  647. static DEVICE_ATTR(downdelay, S_IRUGO | S_IWUSR,
  648. bonding_show_downdelay, bonding_store_downdelay);
  649. static ssize_t bonding_show_updelay(struct device *d,
  650. struct device_attribute *attr,
  651. char *buf)
  652. {
  653. struct bonding *bond = to_bond(d);
  654. return sprintf(buf, "%d\n", bond->params.updelay * bond->params.miimon);
  655. }
  656. static ssize_t bonding_store_updelay(struct device *d,
  657. struct device_attribute *attr,
  658. const char *buf, size_t count)
  659. {
  660. int new_value, ret = count;
  661. struct bonding *bond = to_bond(d);
  662. if (!(bond->params.miimon)) {
  663. pr_err("%s: Unable to set up delay as MII monitoring is disabled\n",
  664. bond->dev->name);
  665. ret = -EPERM;
  666. goto out;
  667. }
  668. if (sscanf(buf, "%d", &new_value) != 1) {
  669. pr_err("%s: no up delay value specified.\n",
  670. bond->dev->name);
  671. ret = -EINVAL;
  672. goto out;
  673. }
  674. if (new_value < 0) {
  675. pr_err("%s: Invalid up delay value %d not in range %d-%d; rejected.\n",
  676. bond->dev->name, new_value, 0, INT_MAX);
  677. ret = -EINVAL;
  678. goto out;
  679. } else {
  680. if ((new_value % bond->params.miimon) != 0) {
  681. pr_warning("%s: Warning: up delay (%d) is not a multiple of miimon (%d), updelay rounded to %d ms\n",
  682. bond->dev->name, new_value,
  683. bond->params.miimon,
  684. (new_value / bond->params.miimon) *
  685. bond->params.miimon);
  686. }
  687. bond->params.updelay = new_value / bond->params.miimon;
  688. pr_info("%s: Setting up delay to %d.\n",
  689. bond->dev->name,
  690. bond->params.updelay * bond->params.miimon);
  691. }
  692. out:
  693. return ret;
  694. }
  695. static DEVICE_ATTR(updelay, S_IRUGO | S_IWUSR,
  696. bonding_show_updelay, bonding_store_updelay);
  697. /*
  698. * Show and set the LACP interval. Interface must be down, and the mode
  699. * must be set to 802.3ad mode.
  700. */
  701. static ssize_t bonding_show_lacp(struct device *d,
  702. struct device_attribute *attr,
  703. char *buf)
  704. {
  705. struct bonding *bond = to_bond(d);
  706. return sprintf(buf, "%s %d\n",
  707. bond_lacp_tbl[bond->params.lacp_fast].modename,
  708. bond->params.lacp_fast);
  709. }
  710. static ssize_t bonding_store_lacp(struct device *d,
  711. struct device_attribute *attr,
  712. const char *buf, size_t count)
  713. {
  714. int new_value, ret = count;
  715. struct bonding *bond = to_bond(d);
  716. if (bond->dev->flags & IFF_UP) {
  717. pr_err("%s: Unable to update LACP rate because interface is up.\n",
  718. bond->dev->name);
  719. ret = -EPERM;
  720. goto out;
  721. }
  722. if (bond->params.mode != BOND_MODE_8023AD) {
  723. pr_err("%s: Unable to update LACP rate because bond is not in 802.3ad mode.\n",
  724. bond->dev->name);
  725. ret = -EPERM;
  726. goto out;
  727. }
  728. new_value = bond_parse_parm(buf, bond_lacp_tbl);
  729. if ((new_value == 1) || (new_value == 0)) {
  730. bond->params.lacp_fast = new_value;
  731. bond_3ad_update_lacp_rate(bond);
  732. pr_info("%s: Setting LACP rate to %s (%d).\n",
  733. bond->dev->name, bond_lacp_tbl[new_value].modename,
  734. new_value);
  735. } else {
  736. pr_err("%s: Ignoring invalid LACP rate value %.*s.\n",
  737. bond->dev->name, (int)strlen(buf) - 1, buf);
  738. ret = -EINVAL;
  739. }
  740. out:
  741. return ret;
  742. }
  743. static DEVICE_ATTR(lacp_rate, S_IRUGO | S_IWUSR,
  744. bonding_show_lacp, bonding_store_lacp);
  745. static ssize_t bonding_show_min_links(struct device *d,
  746. struct device_attribute *attr,
  747. char *buf)
  748. {
  749. struct bonding *bond = to_bond(d);
  750. return sprintf(buf, "%d\n", bond->params.min_links);
  751. }
  752. static ssize_t bonding_store_min_links(struct device *d,
  753. struct device_attribute *attr,
  754. const char *buf, size_t count)
  755. {
  756. struct bonding *bond = to_bond(d);
  757. int ret;
  758. unsigned int new_value;
  759. ret = kstrtouint(buf, 0, &new_value);
  760. if (ret < 0) {
  761. pr_err("%s: Ignoring invalid min links value %s.\n",
  762. bond->dev->name, buf);
  763. return ret;
  764. }
  765. pr_info("%s: Setting min links value to %u\n",
  766. bond->dev->name, new_value);
  767. bond->params.min_links = new_value;
  768. return count;
  769. }
  770. static DEVICE_ATTR(min_links, S_IRUGO | S_IWUSR,
  771. bonding_show_min_links, bonding_store_min_links);
  772. static ssize_t bonding_show_ad_select(struct device *d,
  773. struct device_attribute *attr,
  774. char *buf)
  775. {
  776. struct bonding *bond = to_bond(d);
  777. return sprintf(buf, "%s %d\n",
  778. ad_select_tbl[bond->params.ad_select].modename,
  779. bond->params.ad_select);
  780. }
  781. static ssize_t bonding_store_ad_select(struct device *d,
  782. struct device_attribute *attr,
  783. const char *buf, size_t count)
  784. {
  785. int new_value, ret = count;
  786. struct bonding *bond = to_bond(d);
  787. if (bond->dev->flags & IFF_UP) {
  788. pr_err("%s: Unable to update ad_select because interface is up.\n",
  789. bond->dev->name);
  790. ret = -EPERM;
  791. goto out;
  792. }
  793. new_value = bond_parse_parm(buf, ad_select_tbl);
  794. if (new_value != -1) {
  795. bond->params.ad_select = new_value;
  796. pr_info("%s: Setting ad_select to %s (%d).\n",
  797. bond->dev->name, ad_select_tbl[new_value].modename,
  798. new_value);
  799. } else {
  800. pr_err("%s: Ignoring invalid ad_select value %.*s.\n",
  801. bond->dev->name, (int)strlen(buf) - 1, buf);
  802. ret = -EINVAL;
  803. }
  804. out:
  805. return ret;
  806. }
  807. static DEVICE_ATTR(ad_select, S_IRUGO | S_IWUSR,
  808. bonding_show_ad_select, bonding_store_ad_select);
  809. /*
  810. * Show and set the number of peer notifications to send after a failover event.
  811. */
  812. static ssize_t bonding_show_num_peer_notif(struct device *d,
  813. struct device_attribute *attr,
  814. char *buf)
  815. {
  816. struct bonding *bond = to_bond(d);
  817. return sprintf(buf, "%d\n", bond->params.num_peer_notif);
  818. }
  819. static ssize_t bonding_store_num_peer_notif(struct device *d,
  820. struct device_attribute *attr,
  821. const char *buf, size_t count)
  822. {
  823. struct bonding *bond = to_bond(d);
  824. int err = kstrtou8(buf, 10, &bond->params.num_peer_notif);
  825. return err ? err : count;
  826. }
  827. static DEVICE_ATTR(num_grat_arp, S_IRUGO | S_IWUSR,
  828. bonding_show_num_peer_notif, bonding_store_num_peer_notif);
  829. static DEVICE_ATTR(num_unsol_na, S_IRUGO | S_IWUSR,
  830. bonding_show_num_peer_notif, bonding_store_num_peer_notif);
  831. /*
  832. * Show and set the MII monitor interval. There are two tricky bits
  833. * here. First, if MII monitoring is activated, then we must disable
  834. * ARP monitoring. Second, if the timer isn't running, we must
  835. * start it.
  836. */
  837. static ssize_t bonding_show_miimon(struct device *d,
  838. struct device_attribute *attr,
  839. char *buf)
  840. {
  841. struct bonding *bond = to_bond(d);
  842. return sprintf(buf, "%d\n", bond->params.miimon);
  843. }
  844. static ssize_t bonding_store_miimon(struct device *d,
  845. struct device_attribute *attr,
  846. const char *buf, size_t count)
  847. {
  848. int new_value, ret = count;
  849. struct bonding *bond = to_bond(d);
  850. if (!rtnl_trylock())
  851. return restart_syscall();
  852. if (sscanf(buf, "%d", &new_value) != 1) {
  853. pr_err("%s: no miimon value specified.\n",
  854. bond->dev->name);
  855. ret = -EINVAL;
  856. goto out;
  857. }
  858. if (new_value < 0) {
  859. pr_err("%s: Invalid miimon value %d not in range %d-%d; rejected.\n",
  860. bond->dev->name, new_value, 0, INT_MAX);
  861. ret = -EINVAL;
  862. goto out;
  863. }
  864. pr_info("%s: Setting MII monitoring interval to %d.\n",
  865. bond->dev->name, new_value);
  866. bond->params.miimon = new_value;
  867. if (bond->params.updelay)
  868. pr_info("%s: Note: Updating updelay (to %d) since it is a multiple of the miimon value.\n",
  869. bond->dev->name,
  870. bond->params.updelay * bond->params.miimon);
  871. if (bond->params.downdelay)
  872. pr_info("%s: Note: Updating downdelay (to %d) since it is a multiple of the miimon value.\n",
  873. bond->dev->name,
  874. bond->params.downdelay * bond->params.miimon);
  875. if (new_value && bond->params.arp_interval) {
  876. pr_info("%s: MII monitoring cannot be used with ARP monitoring. Disabling ARP monitoring...\n",
  877. bond->dev->name);
  878. bond->params.arp_interval = 0;
  879. if (bond->params.arp_validate)
  880. bond->params.arp_validate = BOND_ARP_VALIDATE_NONE;
  881. }
  882. if (bond->dev->flags & IFF_UP) {
  883. /* If the interface is up, we may need to fire off
  884. * the MII timer. If the interface is down, the
  885. * timer will get fired off when the open function
  886. * is called.
  887. */
  888. if (!new_value) {
  889. cancel_delayed_work_sync(&bond->mii_work);
  890. } else {
  891. cancel_delayed_work_sync(&bond->arp_work);
  892. queue_delayed_work(bond->wq, &bond->mii_work, 0);
  893. }
  894. }
  895. out:
  896. rtnl_unlock();
  897. return ret;
  898. }
  899. static DEVICE_ATTR(miimon, S_IRUGO | S_IWUSR,
  900. bonding_show_miimon, bonding_store_miimon);
  901. /*
  902. * Show and set the primary slave. The store function is much
  903. * simpler than bonding_store_slaves function because it only needs to
  904. * handle one interface name.
  905. * The bond must be a mode that supports a primary for this be
  906. * set.
  907. */
  908. static ssize_t bonding_show_primary(struct device *d,
  909. struct device_attribute *attr,
  910. char *buf)
  911. {
  912. int count = 0;
  913. struct bonding *bond = to_bond(d);
  914. if (bond->primary_slave)
  915. count = sprintf(buf, "%s\n", bond->primary_slave->dev->name);
  916. return count;
  917. }
  918. static ssize_t bonding_store_primary(struct device *d,
  919. struct device_attribute *attr,
  920. const char *buf, size_t count)
  921. {
  922. int i;
  923. struct slave *slave;
  924. struct bonding *bond = to_bond(d);
  925. char ifname[IFNAMSIZ];
  926. if (!rtnl_trylock())
  927. return restart_syscall();
  928. block_netpoll_tx();
  929. read_lock(&bond->lock);
  930. write_lock_bh(&bond->curr_slave_lock);
  931. if (!USES_PRIMARY(bond->params.mode)) {
  932. pr_info("%s: Unable to set primary slave; %s is in mode %d\n",
  933. bond->dev->name, bond->dev->name, bond->params.mode);
  934. goto out;
  935. }
  936. sscanf(buf, "%15s", ifname); /* IFNAMSIZ */
  937. /* check to see if we are clearing primary */
  938. if (!strlen(ifname) || buf[0] == '\n') {
  939. pr_info("%s: Setting primary slave to None.\n",
  940. bond->dev->name);
  941. bond->primary_slave = NULL;
  942. memset(bond->params.primary, 0, sizeof(bond->params.primary));
  943. bond_select_active_slave(bond);
  944. goto out;
  945. }
  946. bond_for_each_slave(bond, slave, i) {
  947. if (strncmp(slave->dev->name, ifname, IFNAMSIZ) == 0) {
  948. pr_info("%s: Setting %s as primary slave.\n",
  949. bond->dev->name, slave->dev->name);
  950. bond->primary_slave = slave;
  951. strcpy(bond->params.primary, slave->dev->name);
  952. bond_select_active_slave(bond);
  953. goto out;
  954. }
  955. }
  956. strncpy(bond->params.primary, ifname, IFNAMSIZ);
  957. bond->params.primary[IFNAMSIZ - 1] = 0;
  958. pr_info("%s: Recording %s as primary, "
  959. "but it has not been enslaved to %s yet.\n",
  960. bond->dev->name, ifname, bond->dev->name);
  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, "%15s", 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->dev->real_num_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. }