bond_sysfs.c 46 KB

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