bond_sysfs.c 45 KB

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