bond_sysfs.c 46 KB

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