bond_sysfs.c 46 KB

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