bond_sysfs.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534
  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, see <http://www.gnu.org/licenses/>.
  16. *
  17. * The full GNU General Public License is included in this distribution in the
  18. * file called LICENSE.
  19. *
  20. */
  21. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  22. #include <linux/kernel.h>
  23. #include <linux/module.h>
  24. #include <linux/device.h>
  25. #include <linux/sched.h>
  26. #include <linux/fs.h>
  27. #include <linux/types.h>
  28. #include <linux/string.h>
  29. #include <linux/netdevice.h>
  30. #include <linux/inetdevice.h>
  31. #include <linux/in.h>
  32. #include <linux/sysfs.h>
  33. #include <linux/ctype.h>
  34. #include <linux/inet.h>
  35. #include <linux/rtnetlink.h>
  36. #include <linux/etherdevice.h>
  37. #include <net/net_namespace.h>
  38. #include <net/netns/generic.h>
  39. #include <linux/nsproxy.h>
  40. #include <linux/reciprocal_div.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. /* class attribute for bond_masters file. This ends up in /sys/class/net */
  135. static const struct class_attribute class_attr_bonding_masters = {
  136. .attr = {
  137. .name = "bonding_masters",
  138. .mode = S_IWUSR | S_IRUGO,
  139. },
  140. .show = bonding_show_bonds,
  141. .store = bonding_store_bonds,
  142. };
  143. /*
  144. * Show the slaves in the current bond.
  145. */
  146. static ssize_t bonding_show_slaves(struct device *d,
  147. struct device_attribute *attr, char *buf)
  148. {
  149. struct bonding *bond = to_bond(d);
  150. struct list_head *iter;
  151. struct slave *slave;
  152. int res = 0;
  153. if (!rtnl_trylock())
  154. return restart_syscall();
  155. bond_for_each_slave(bond, slave, iter) {
  156. if (res > (PAGE_SIZE - IFNAMSIZ)) {
  157. /* not enough space for another interface name */
  158. if ((PAGE_SIZE - res) > 10)
  159. res = PAGE_SIZE - 10;
  160. res += sprintf(buf + res, "++more++ ");
  161. break;
  162. }
  163. res += sprintf(buf + res, "%s ", slave->dev->name);
  164. }
  165. rtnl_unlock();
  166. if (res)
  167. buf[res-1] = '\n'; /* eat the leftover space */
  168. return res;
  169. }
  170. /*
  171. * Set the slaves in the current bond.
  172. * This is supposed to be only thin wrapper for bond_enslave and bond_release.
  173. * All hard work should be done there.
  174. */
  175. static ssize_t bonding_store_slaves(struct device *d,
  176. struct device_attribute *attr,
  177. const char *buffer, size_t count)
  178. {
  179. char command[IFNAMSIZ + 1] = { 0, };
  180. char *ifname;
  181. int res, ret = count;
  182. struct net_device *dev;
  183. struct bonding *bond = to_bond(d);
  184. if (!rtnl_trylock())
  185. return restart_syscall();
  186. sscanf(buffer, "%16s", command); /* IFNAMSIZ*/
  187. ifname = command + 1;
  188. if ((strlen(command) <= 1) ||
  189. !dev_valid_name(ifname))
  190. goto err_no_cmd;
  191. dev = __dev_get_by_name(dev_net(bond->dev), ifname);
  192. if (!dev) {
  193. pr_info("%s: Interface %s does not exist!\n",
  194. bond->dev->name, ifname);
  195. ret = -ENODEV;
  196. goto out;
  197. }
  198. switch (command[0]) {
  199. case '+':
  200. pr_info("%s: Adding slave %s.\n", bond->dev->name, dev->name);
  201. res = bond_enslave(bond->dev, dev);
  202. break;
  203. case '-':
  204. pr_info("%s: Removing slave %s.\n", bond->dev->name, dev->name);
  205. res = bond_release(bond->dev, dev);
  206. break;
  207. default:
  208. goto err_no_cmd;
  209. }
  210. if (res)
  211. ret = res;
  212. goto out;
  213. err_no_cmd:
  214. pr_err("no command found in slaves file for bond %s. Use +ifname or -ifname.\n",
  215. bond->dev->name);
  216. ret = -EPERM;
  217. out:
  218. rtnl_unlock();
  219. return ret;
  220. }
  221. static DEVICE_ATTR(slaves, S_IRUGO | S_IWUSR, bonding_show_slaves,
  222. bonding_store_slaves);
  223. /*
  224. * Show and set the bonding mode. The bond interface must be down to
  225. * change the mode.
  226. */
  227. static ssize_t bonding_show_mode(struct device *d,
  228. struct device_attribute *attr, char *buf)
  229. {
  230. struct bonding *bond = to_bond(d);
  231. return sprintf(buf, "%s %d\n",
  232. bond_mode_tbl[bond->params.mode].modename,
  233. bond->params.mode);
  234. }
  235. static ssize_t bonding_store_mode(struct device *d,
  236. struct device_attribute *attr,
  237. const char *buf, size_t count)
  238. {
  239. int new_value, ret;
  240. struct bonding *bond = to_bond(d);
  241. new_value = bond_parse_parm(buf, bond_mode_tbl);
  242. if (new_value < 0) {
  243. pr_err("%s: Ignoring invalid mode value %.*s.\n",
  244. bond->dev->name, (int)strlen(buf) - 1, buf);
  245. return -EINVAL;
  246. }
  247. if (!rtnl_trylock())
  248. return restart_syscall();
  249. ret = bond_option_mode_set(bond, new_value);
  250. if (!ret) {
  251. pr_info("%s: setting mode to %s (%d).\n",
  252. bond->dev->name, bond_mode_tbl[new_value].modename,
  253. new_value);
  254. ret = count;
  255. }
  256. rtnl_unlock();
  257. return ret;
  258. }
  259. static DEVICE_ATTR(mode, S_IRUGO | S_IWUSR,
  260. bonding_show_mode, bonding_store_mode);
  261. /*
  262. * Show and set the bonding transmit hash method.
  263. */
  264. static ssize_t bonding_show_xmit_hash(struct device *d,
  265. struct device_attribute *attr,
  266. char *buf)
  267. {
  268. struct bonding *bond = to_bond(d);
  269. return sprintf(buf, "%s %d\n",
  270. xmit_hashtype_tbl[bond->params.xmit_policy].modename,
  271. bond->params.xmit_policy);
  272. }
  273. static ssize_t bonding_store_xmit_hash(struct device *d,
  274. struct device_attribute *attr,
  275. const char *buf, size_t count)
  276. {
  277. int new_value, ret;
  278. struct bonding *bond = to_bond(d);
  279. new_value = bond_parse_parm(buf, xmit_hashtype_tbl);
  280. if (new_value < 0) {
  281. pr_err("%s: Ignoring invalid xmit hash policy value %.*s.\n",
  282. bond->dev->name,
  283. (int)strlen(buf) - 1, buf);
  284. return -EINVAL;
  285. }
  286. if (!rtnl_trylock())
  287. return restart_syscall();
  288. ret = bond_option_xmit_hash_policy_set(bond, new_value);
  289. if (!ret)
  290. ret = count;
  291. rtnl_unlock();
  292. return ret;
  293. }
  294. static DEVICE_ATTR(xmit_hash_policy, S_IRUGO | S_IWUSR,
  295. bonding_show_xmit_hash, bonding_store_xmit_hash);
  296. /*
  297. * Show and set arp_validate.
  298. */
  299. static ssize_t bonding_show_arp_validate(struct device *d,
  300. struct device_attribute *attr,
  301. char *buf)
  302. {
  303. struct bonding *bond = to_bond(d);
  304. return sprintf(buf, "%s %d\n",
  305. arp_validate_tbl[bond->params.arp_validate].modename,
  306. bond->params.arp_validate);
  307. }
  308. static ssize_t bonding_store_arp_validate(struct device *d,
  309. struct device_attribute *attr,
  310. const char *buf, size_t count)
  311. {
  312. struct bonding *bond = to_bond(d);
  313. int new_value, ret;
  314. new_value = bond_parse_parm(buf, arp_validate_tbl);
  315. if (new_value < 0) {
  316. pr_err("%s: Ignoring invalid arp_validate value %s\n",
  317. bond->dev->name, buf);
  318. return -EINVAL;
  319. }
  320. if (!rtnl_trylock())
  321. return restart_syscall();
  322. ret = bond_option_arp_validate_set(bond, new_value);
  323. if (!ret)
  324. ret = count;
  325. rtnl_unlock();
  326. return ret;
  327. }
  328. static DEVICE_ATTR(arp_validate, S_IRUGO | S_IWUSR, bonding_show_arp_validate,
  329. bonding_store_arp_validate);
  330. /*
  331. * Show and set arp_all_targets.
  332. */
  333. static ssize_t bonding_show_arp_all_targets(struct device *d,
  334. struct device_attribute *attr,
  335. char *buf)
  336. {
  337. struct bonding *bond = to_bond(d);
  338. int value = bond->params.arp_all_targets;
  339. return sprintf(buf, "%s %d\n", arp_all_targets_tbl[value].modename,
  340. value);
  341. }
  342. static ssize_t bonding_store_arp_all_targets(struct device *d,
  343. struct device_attribute *attr,
  344. const char *buf, size_t count)
  345. {
  346. struct bonding *bond = to_bond(d);
  347. int new_value, ret;
  348. new_value = bond_parse_parm(buf, arp_all_targets_tbl);
  349. if (new_value < 0) {
  350. pr_err("%s: Ignoring invalid arp_all_targets value %s\n",
  351. bond->dev->name, buf);
  352. return -EINVAL;
  353. }
  354. if (!rtnl_trylock())
  355. return restart_syscall();
  356. ret = bond_option_arp_all_targets_set(bond, new_value);
  357. if (!ret)
  358. ret = count;
  359. rtnl_unlock();
  360. return ret;
  361. }
  362. static DEVICE_ATTR(arp_all_targets, S_IRUGO | S_IWUSR,
  363. bonding_show_arp_all_targets, bonding_store_arp_all_targets);
  364. /*
  365. * Show and store fail_over_mac. User only allowed to change the
  366. * value when there are no slaves.
  367. */
  368. static ssize_t bonding_show_fail_over_mac(struct device *d,
  369. struct device_attribute *attr,
  370. char *buf)
  371. {
  372. struct bonding *bond = to_bond(d);
  373. return sprintf(buf, "%s %d\n",
  374. fail_over_mac_tbl[bond->params.fail_over_mac].modename,
  375. bond->params.fail_over_mac);
  376. }
  377. static ssize_t bonding_store_fail_over_mac(struct device *d,
  378. struct device_attribute *attr,
  379. const char *buf, size_t count)
  380. {
  381. int new_value, ret;
  382. struct bonding *bond = to_bond(d);
  383. new_value = bond_parse_parm(buf, fail_over_mac_tbl);
  384. if (new_value < 0) {
  385. pr_err("%s: Ignoring invalid fail_over_mac value %s.\n",
  386. bond->dev->name, buf);
  387. return -EINVAL;
  388. }
  389. if (!rtnl_trylock())
  390. return restart_syscall();
  391. ret = bond_option_fail_over_mac_set(bond, new_value);
  392. if (!ret)
  393. ret = count;
  394. rtnl_unlock();
  395. return ret;
  396. }
  397. static DEVICE_ATTR(fail_over_mac, S_IRUGO | S_IWUSR,
  398. bonding_show_fail_over_mac, bonding_store_fail_over_mac);
  399. /*
  400. * Show and set the arp timer interval. There are two tricky bits
  401. * here. First, if ARP monitoring is activated, then we must disable
  402. * MII monitoring. Second, if the ARP timer isn't running, we must
  403. * start it.
  404. */
  405. static ssize_t bonding_show_arp_interval(struct device *d,
  406. struct device_attribute *attr,
  407. char *buf)
  408. {
  409. struct bonding *bond = to_bond(d);
  410. return sprintf(buf, "%d\n", bond->params.arp_interval);
  411. }
  412. static ssize_t bonding_store_arp_interval(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, ret;
  418. if (sscanf(buf, "%d", &new_value) != 1) {
  419. pr_err("%s: no arp_interval value specified.\n",
  420. bond->dev->name);
  421. return -EINVAL;
  422. }
  423. if (!rtnl_trylock())
  424. return restart_syscall();
  425. ret = bond_option_arp_interval_set(bond, new_value);
  426. if (!ret)
  427. ret = count;
  428. rtnl_unlock();
  429. return ret;
  430. }
  431. static DEVICE_ATTR(arp_interval, S_IRUGO | S_IWUSR,
  432. bonding_show_arp_interval, bonding_store_arp_interval);
  433. /*
  434. * Show and set the arp targets.
  435. */
  436. static ssize_t bonding_show_arp_targets(struct device *d,
  437. struct device_attribute *attr,
  438. char *buf)
  439. {
  440. int i, res = 0;
  441. struct bonding *bond = to_bond(d);
  442. for (i = 0; i < BOND_MAX_ARP_TARGETS; i++) {
  443. if (bond->params.arp_targets[i])
  444. res += sprintf(buf + res, "%pI4 ",
  445. &bond->params.arp_targets[i]);
  446. }
  447. if (res)
  448. buf[res-1] = '\n'; /* eat the leftover space */
  449. return res;
  450. }
  451. static ssize_t bonding_store_arp_targets(struct device *d,
  452. struct device_attribute *attr,
  453. const char *buf, size_t count)
  454. {
  455. struct bonding *bond = to_bond(d);
  456. __be32 target;
  457. int ret = -EPERM;
  458. if (!in4_pton(buf + 1, -1, (u8 *)&target, -1, NULL)) {
  459. pr_err("%s: invalid ARP target %pI4 specified\n",
  460. bond->dev->name, &target);
  461. return -EPERM;
  462. }
  463. if (!rtnl_trylock())
  464. return restart_syscall();
  465. if (buf[0] == '+')
  466. ret = bond_option_arp_ip_target_add(bond, target);
  467. else if (buf[0] == '-')
  468. ret = bond_option_arp_ip_target_rem(bond, target);
  469. else
  470. pr_err("no command found in arp_ip_targets file for bond %s. Use +<addr> or -<addr>.\n",
  471. bond->dev->name);
  472. if (!ret)
  473. ret = count;
  474. rtnl_unlock();
  475. return ret;
  476. }
  477. static DEVICE_ATTR(arp_ip_target, S_IRUGO | S_IWUSR , bonding_show_arp_targets, bonding_store_arp_targets);
  478. /*
  479. * Show and set the up and down delays. These must be multiples of the
  480. * MII monitoring value, and are stored internally as the multiplier.
  481. * Thus, we must translate to MS for the real world.
  482. */
  483. static ssize_t bonding_show_downdelay(struct device *d,
  484. struct device_attribute *attr,
  485. char *buf)
  486. {
  487. struct bonding *bond = to_bond(d);
  488. return sprintf(buf, "%d\n", bond->params.downdelay * bond->params.miimon);
  489. }
  490. static ssize_t bonding_store_downdelay(struct device *d,
  491. struct device_attribute *attr,
  492. const char *buf, size_t count)
  493. {
  494. int new_value, ret;
  495. struct bonding *bond = to_bond(d);
  496. if (sscanf(buf, "%d", &new_value) != 1) {
  497. pr_err("%s: no down delay value specified.\n", bond->dev->name);
  498. return -EINVAL;
  499. }
  500. if (!rtnl_trylock())
  501. return restart_syscall();
  502. ret = bond_option_downdelay_set(bond, new_value);
  503. if (!ret)
  504. ret = count;
  505. rtnl_unlock();
  506. return ret;
  507. }
  508. static DEVICE_ATTR(downdelay, S_IRUGO | S_IWUSR,
  509. bonding_show_downdelay, bonding_store_downdelay);
  510. static ssize_t bonding_show_updelay(struct device *d,
  511. struct device_attribute *attr,
  512. char *buf)
  513. {
  514. struct bonding *bond = to_bond(d);
  515. return sprintf(buf, "%d\n", bond->params.updelay * bond->params.miimon);
  516. }
  517. static ssize_t bonding_store_updelay(struct device *d,
  518. struct device_attribute *attr,
  519. const char *buf, size_t count)
  520. {
  521. int new_value, ret;
  522. struct bonding *bond = to_bond(d);
  523. if (sscanf(buf, "%d", &new_value) != 1) {
  524. pr_err("%s: no up delay value specified.\n",
  525. bond->dev->name);
  526. return -EINVAL;
  527. }
  528. if (!rtnl_trylock())
  529. return restart_syscall();
  530. ret = bond_option_updelay_set(bond, new_value);
  531. if (!ret)
  532. ret = count;
  533. rtnl_unlock();
  534. return ret;
  535. }
  536. static DEVICE_ATTR(updelay, S_IRUGO | S_IWUSR,
  537. bonding_show_updelay, bonding_store_updelay);
  538. /*
  539. * Show and set the LACP interval. Interface must be down, and the mode
  540. * must be set to 802.3ad mode.
  541. */
  542. static ssize_t bonding_show_lacp(struct device *d,
  543. struct device_attribute *attr,
  544. char *buf)
  545. {
  546. struct bonding *bond = to_bond(d);
  547. return sprintf(buf, "%s %d\n",
  548. bond_lacp_tbl[bond->params.lacp_fast].modename,
  549. bond->params.lacp_fast);
  550. }
  551. static ssize_t bonding_store_lacp(struct device *d,
  552. struct device_attribute *attr,
  553. const char *buf, size_t count)
  554. {
  555. struct bonding *bond = to_bond(d);
  556. int new_value, ret = count;
  557. if (!rtnl_trylock())
  558. return restart_syscall();
  559. if (bond->dev->flags & IFF_UP) {
  560. pr_err("%s: Unable to update LACP rate because interface is up.\n",
  561. bond->dev->name);
  562. ret = -EPERM;
  563. goto out;
  564. }
  565. if (bond->params.mode != BOND_MODE_8023AD) {
  566. pr_err("%s: Unable to update LACP rate because bond is not in 802.3ad mode.\n",
  567. bond->dev->name);
  568. ret = -EPERM;
  569. goto out;
  570. }
  571. new_value = bond_parse_parm(buf, bond_lacp_tbl);
  572. if ((new_value == 1) || (new_value == 0)) {
  573. bond->params.lacp_fast = new_value;
  574. bond_3ad_update_lacp_rate(bond);
  575. pr_info("%s: Setting LACP rate to %s (%d).\n",
  576. bond->dev->name, bond_lacp_tbl[new_value].modename,
  577. new_value);
  578. } else {
  579. pr_err("%s: Ignoring invalid LACP rate value %.*s.\n",
  580. bond->dev->name, (int)strlen(buf) - 1, buf);
  581. ret = -EINVAL;
  582. }
  583. out:
  584. rtnl_unlock();
  585. return ret;
  586. }
  587. static DEVICE_ATTR(lacp_rate, S_IRUGO | S_IWUSR,
  588. bonding_show_lacp, bonding_store_lacp);
  589. static ssize_t bonding_show_min_links(struct device *d,
  590. struct device_attribute *attr,
  591. char *buf)
  592. {
  593. struct bonding *bond = to_bond(d);
  594. return sprintf(buf, "%d\n", bond->params.min_links);
  595. }
  596. static ssize_t bonding_store_min_links(struct device *d,
  597. struct device_attribute *attr,
  598. const char *buf, size_t count)
  599. {
  600. struct bonding *bond = to_bond(d);
  601. int ret;
  602. unsigned int new_value;
  603. ret = kstrtouint(buf, 0, &new_value);
  604. if (ret < 0) {
  605. pr_err("%s: Ignoring invalid min links value %s.\n",
  606. bond->dev->name, buf);
  607. return ret;
  608. }
  609. pr_info("%s: Setting min links value to %u\n",
  610. bond->dev->name, new_value);
  611. bond->params.min_links = new_value;
  612. return count;
  613. }
  614. static DEVICE_ATTR(min_links, S_IRUGO | S_IWUSR,
  615. bonding_show_min_links, bonding_store_min_links);
  616. static ssize_t bonding_show_ad_select(struct device *d,
  617. struct device_attribute *attr,
  618. char *buf)
  619. {
  620. struct bonding *bond = to_bond(d);
  621. return sprintf(buf, "%s %d\n",
  622. ad_select_tbl[bond->params.ad_select].modename,
  623. bond->params.ad_select);
  624. }
  625. static ssize_t bonding_store_ad_select(struct device *d,
  626. struct device_attribute *attr,
  627. const char *buf, size_t count)
  628. {
  629. int new_value, ret = count;
  630. struct bonding *bond = to_bond(d);
  631. if (bond->dev->flags & IFF_UP) {
  632. pr_err("%s: Unable to update ad_select because interface is up.\n",
  633. bond->dev->name);
  634. ret = -EPERM;
  635. goto out;
  636. }
  637. new_value = bond_parse_parm(buf, ad_select_tbl);
  638. if (new_value != -1) {
  639. bond->params.ad_select = new_value;
  640. pr_info("%s: Setting ad_select to %s (%d).\n",
  641. bond->dev->name, ad_select_tbl[new_value].modename,
  642. new_value);
  643. } else {
  644. pr_err("%s: Ignoring invalid ad_select value %.*s.\n",
  645. bond->dev->name, (int)strlen(buf) - 1, buf);
  646. ret = -EINVAL;
  647. }
  648. out:
  649. return ret;
  650. }
  651. static DEVICE_ATTR(ad_select, S_IRUGO | S_IWUSR,
  652. bonding_show_ad_select, bonding_store_ad_select);
  653. /*
  654. * Show and set the number of peer notifications to send after a failover event.
  655. */
  656. static ssize_t bonding_show_num_peer_notif(struct device *d,
  657. struct device_attribute *attr,
  658. char *buf)
  659. {
  660. struct bonding *bond = to_bond(d);
  661. return sprintf(buf, "%d\n", bond->params.num_peer_notif);
  662. }
  663. static ssize_t bonding_store_num_peer_notif(struct device *d,
  664. struct device_attribute *attr,
  665. const char *buf, size_t count)
  666. {
  667. struct bonding *bond = to_bond(d);
  668. int err = kstrtou8(buf, 10, &bond->params.num_peer_notif);
  669. return err ? err : count;
  670. }
  671. static DEVICE_ATTR(num_grat_arp, S_IRUGO | S_IWUSR,
  672. bonding_show_num_peer_notif, bonding_store_num_peer_notif);
  673. static DEVICE_ATTR(num_unsol_na, S_IRUGO | S_IWUSR,
  674. bonding_show_num_peer_notif, bonding_store_num_peer_notif);
  675. /*
  676. * Show and set the MII monitor interval. There are two tricky bits
  677. * here. First, if MII monitoring is activated, then we must disable
  678. * ARP monitoring. Second, if the timer isn't running, we must
  679. * start it.
  680. */
  681. static ssize_t bonding_show_miimon(struct device *d,
  682. struct device_attribute *attr,
  683. char *buf)
  684. {
  685. struct bonding *bond = to_bond(d);
  686. return sprintf(buf, "%d\n", bond->params.miimon);
  687. }
  688. static ssize_t bonding_store_miimon(struct device *d,
  689. struct device_attribute *attr,
  690. const char *buf, size_t count)
  691. {
  692. int new_value, ret;
  693. struct bonding *bond = to_bond(d);
  694. if (sscanf(buf, "%d", &new_value) != 1) {
  695. pr_err("%s: no miimon value specified.\n",
  696. bond->dev->name);
  697. return -EINVAL;
  698. }
  699. if (!rtnl_trylock())
  700. return restart_syscall();
  701. ret = bond_option_miimon_set(bond, new_value);
  702. if (!ret)
  703. ret = count;
  704. rtnl_unlock();
  705. return ret;
  706. }
  707. static DEVICE_ATTR(miimon, S_IRUGO | S_IWUSR,
  708. bonding_show_miimon, bonding_store_miimon);
  709. /*
  710. * Show and set the primary slave. The store function is much
  711. * simpler than bonding_store_slaves function because it only needs to
  712. * handle one interface name.
  713. * The bond must be a mode that supports a primary for this be
  714. * set.
  715. */
  716. static ssize_t bonding_show_primary(struct device *d,
  717. struct device_attribute *attr,
  718. char *buf)
  719. {
  720. int count = 0;
  721. struct bonding *bond = to_bond(d);
  722. if (bond->primary_slave)
  723. count = sprintf(buf, "%s\n", bond->primary_slave->dev->name);
  724. return count;
  725. }
  726. static ssize_t bonding_store_primary(struct device *d,
  727. struct device_attribute *attr,
  728. const char *buf, size_t count)
  729. {
  730. struct bonding *bond = to_bond(d);
  731. char ifname[IFNAMSIZ];
  732. int ret;
  733. sscanf(buf, "%15s", ifname); /* IFNAMSIZ */
  734. if (ifname[0] == '\n')
  735. ifname[0] = '\0';
  736. if (!rtnl_trylock())
  737. return restart_syscall();
  738. ret = bond_option_primary_set(bond, ifname);
  739. if (!ret)
  740. ret = count;
  741. rtnl_unlock();
  742. return ret;
  743. }
  744. static DEVICE_ATTR(primary, S_IRUGO | S_IWUSR,
  745. bonding_show_primary, bonding_store_primary);
  746. /*
  747. * Show and set the primary_reselect flag.
  748. */
  749. static ssize_t bonding_show_primary_reselect(struct device *d,
  750. struct device_attribute *attr,
  751. char *buf)
  752. {
  753. struct bonding *bond = to_bond(d);
  754. return sprintf(buf, "%s %d\n",
  755. pri_reselect_tbl[bond->params.primary_reselect].modename,
  756. bond->params.primary_reselect);
  757. }
  758. static ssize_t bonding_store_primary_reselect(struct device *d,
  759. struct device_attribute *attr,
  760. const char *buf, size_t count)
  761. {
  762. int new_value, ret;
  763. struct bonding *bond = to_bond(d);
  764. new_value = bond_parse_parm(buf, pri_reselect_tbl);
  765. if (new_value < 0) {
  766. pr_err("%s: Ignoring invalid primary_reselect value %.*s.\n",
  767. bond->dev->name,
  768. (int) strlen(buf) - 1, buf);
  769. return -EINVAL;
  770. }
  771. if (!rtnl_trylock())
  772. return restart_syscall();
  773. ret = bond_option_primary_reselect_set(bond, new_value);
  774. if (!ret)
  775. ret = count;
  776. rtnl_unlock();
  777. return ret;
  778. }
  779. static DEVICE_ATTR(primary_reselect, S_IRUGO | S_IWUSR,
  780. bonding_show_primary_reselect,
  781. bonding_store_primary_reselect);
  782. /*
  783. * Show and set the use_carrier flag.
  784. */
  785. static ssize_t bonding_show_carrier(struct device *d,
  786. struct device_attribute *attr,
  787. char *buf)
  788. {
  789. struct bonding *bond = to_bond(d);
  790. return sprintf(buf, "%d\n", bond->params.use_carrier);
  791. }
  792. static ssize_t bonding_store_carrier(struct device *d,
  793. struct device_attribute *attr,
  794. const char *buf, size_t count)
  795. {
  796. int new_value, ret;
  797. struct bonding *bond = to_bond(d);
  798. if (sscanf(buf, "%d", &new_value) != 1) {
  799. pr_err("%s: no use_carrier value specified.\n",
  800. bond->dev->name);
  801. return -EINVAL;
  802. }
  803. if (!rtnl_trylock())
  804. return restart_syscall();
  805. ret = bond_option_use_carrier_set(bond, new_value);
  806. if (!ret)
  807. ret = count;
  808. rtnl_unlock();
  809. return ret;
  810. }
  811. static DEVICE_ATTR(use_carrier, S_IRUGO | S_IWUSR,
  812. bonding_show_carrier, bonding_store_carrier);
  813. /*
  814. * Show and set currently active_slave.
  815. */
  816. static ssize_t bonding_show_active_slave(struct device *d,
  817. struct device_attribute *attr,
  818. char *buf)
  819. {
  820. struct bonding *bond = to_bond(d);
  821. struct net_device *slave_dev;
  822. int count = 0;
  823. rcu_read_lock();
  824. slave_dev = bond_option_active_slave_get_rcu(bond);
  825. if (slave_dev)
  826. count = sprintf(buf, "%s\n", slave_dev->name);
  827. rcu_read_unlock();
  828. return count;
  829. }
  830. static ssize_t bonding_store_active_slave(struct device *d,
  831. struct device_attribute *attr,
  832. const char *buf, size_t count)
  833. {
  834. int ret;
  835. struct bonding *bond = to_bond(d);
  836. char ifname[IFNAMSIZ];
  837. struct net_device *dev;
  838. if (!rtnl_trylock())
  839. return restart_syscall();
  840. sscanf(buf, "%15s", ifname); /* IFNAMSIZ */
  841. if (!strlen(ifname) || buf[0] == '\n') {
  842. dev = NULL;
  843. } else {
  844. dev = __dev_get_by_name(dev_net(bond->dev), ifname);
  845. if (!dev) {
  846. ret = -ENODEV;
  847. goto out;
  848. }
  849. }
  850. ret = bond_option_active_slave_set(bond, dev);
  851. if (!ret)
  852. ret = count;
  853. out:
  854. rtnl_unlock();
  855. return ret;
  856. }
  857. static DEVICE_ATTR(active_slave, S_IRUGO | S_IWUSR,
  858. bonding_show_active_slave, bonding_store_active_slave);
  859. /*
  860. * Show link status of the bond interface.
  861. */
  862. static ssize_t bonding_show_mii_status(struct device *d,
  863. struct device_attribute *attr,
  864. char *buf)
  865. {
  866. struct bonding *bond = to_bond(d);
  867. return sprintf(buf, "%s\n", bond->curr_active_slave ? "up" : "down");
  868. }
  869. static DEVICE_ATTR(mii_status, S_IRUGO, bonding_show_mii_status, NULL);
  870. /*
  871. * Show current 802.3ad aggregator ID.
  872. */
  873. static ssize_t bonding_show_ad_aggregator(struct device *d,
  874. struct device_attribute *attr,
  875. char *buf)
  876. {
  877. int count = 0;
  878. struct bonding *bond = to_bond(d);
  879. if (bond->params.mode == BOND_MODE_8023AD) {
  880. struct ad_info ad_info;
  881. count = sprintf(buf, "%d\n",
  882. bond_3ad_get_active_agg_info(bond, &ad_info)
  883. ? 0 : ad_info.aggregator_id);
  884. }
  885. return count;
  886. }
  887. static DEVICE_ATTR(ad_aggregator, S_IRUGO, bonding_show_ad_aggregator, NULL);
  888. /*
  889. * Show number of active 802.3ad ports.
  890. */
  891. static ssize_t bonding_show_ad_num_ports(struct device *d,
  892. struct device_attribute *attr,
  893. char *buf)
  894. {
  895. int count = 0;
  896. struct bonding *bond = to_bond(d);
  897. if (bond->params.mode == BOND_MODE_8023AD) {
  898. struct ad_info ad_info;
  899. count = sprintf(buf, "%d\n",
  900. bond_3ad_get_active_agg_info(bond, &ad_info)
  901. ? 0 : ad_info.ports);
  902. }
  903. return count;
  904. }
  905. static DEVICE_ATTR(ad_num_ports, S_IRUGO, bonding_show_ad_num_ports, NULL);
  906. /*
  907. * Show current 802.3ad actor key.
  908. */
  909. static ssize_t bonding_show_ad_actor_key(struct device *d,
  910. struct device_attribute *attr,
  911. char *buf)
  912. {
  913. int count = 0;
  914. struct bonding *bond = to_bond(d);
  915. if (bond->params.mode == BOND_MODE_8023AD) {
  916. struct ad_info ad_info;
  917. count = sprintf(buf, "%d\n",
  918. bond_3ad_get_active_agg_info(bond, &ad_info)
  919. ? 0 : ad_info.actor_key);
  920. }
  921. return count;
  922. }
  923. static DEVICE_ATTR(ad_actor_key, S_IRUGO, bonding_show_ad_actor_key, NULL);
  924. /*
  925. * Show current 802.3ad partner key.
  926. */
  927. static ssize_t bonding_show_ad_partner_key(struct device *d,
  928. struct device_attribute *attr,
  929. char *buf)
  930. {
  931. int count = 0;
  932. struct bonding *bond = to_bond(d);
  933. if (bond->params.mode == BOND_MODE_8023AD) {
  934. struct ad_info ad_info;
  935. count = sprintf(buf, "%d\n",
  936. bond_3ad_get_active_agg_info(bond, &ad_info)
  937. ? 0 : ad_info.partner_key);
  938. }
  939. return count;
  940. }
  941. static DEVICE_ATTR(ad_partner_key, S_IRUGO, bonding_show_ad_partner_key, NULL);
  942. /*
  943. * Show current 802.3ad partner mac.
  944. */
  945. static ssize_t bonding_show_ad_partner_mac(struct device *d,
  946. struct device_attribute *attr,
  947. char *buf)
  948. {
  949. int count = 0;
  950. struct bonding *bond = to_bond(d);
  951. if (bond->params.mode == BOND_MODE_8023AD) {
  952. struct ad_info ad_info;
  953. if (!bond_3ad_get_active_agg_info(bond, &ad_info))
  954. count = sprintf(buf, "%pM\n", ad_info.partner_system);
  955. }
  956. return count;
  957. }
  958. static DEVICE_ATTR(ad_partner_mac, S_IRUGO, bonding_show_ad_partner_mac, NULL);
  959. /*
  960. * Show the queue_ids of the slaves in the current bond.
  961. */
  962. static ssize_t bonding_show_queue_id(struct device *d,
  963. struct device_attribute *attr,
  964. char *buf)
  965. {
  966. struct bonding *bond = to_bond(d);
  967. struct list_head *iter;
  968. struct slave *slave;
  969. int res = 0;
  970. if (!rtnl_trylock())
  971. return restart_syscall();
  972. bond_for_each_slave(bond, slave, iter) {
  973. if (res > (PAGE_SIZE - IFNAMSIZ - 6)) {
  974. /* not enough space for another interface_name:queue_id pair */
  975. if ((PAGE_SIZE - res) > 10)
  976. res = PAGE_SIZE - 10;
  977. res += sprintf(buf + res, "++more++ ");
  978. break;
  979. }
  980. res += sprintf(buf + res, "%s:%d ",
  981. slave->dev->name, slave->queue_id);
  982. }
  983. if (res)
  984. buf[res-1] = '\n'; /* eat the leftover space */
  985. rtnl_unlock();
  986. return res;
  987. }
  988. /*
  989. * Set the queue_ids of the slaves in the current bond. The bond
  990. * interface must be enslaved for this to work.
  991. */
  992. static ssize_t bonding_store_queue_id(struct device *d,
  993. struct device_attribute *attr,
  994. const char *buffer, size_t count)
  995. {
  996. struct slave *slave, *update_slave;
  997. struct bonding *bond = to_bond(d);
  998. struct list_head *iter;
  999. u16 qid;
  1000. int ret = count;
  1001. char *delim;
  1002. struct net_device *sdev = NULL;
  1003. if (!rtnl_trylock())
  1004. return restart_syscall();
  1005. /* delim will point to queue id if successful */
  1006. delim = strchr(buffer, ':');
  1007. if (!delim)
  1008. goto err_no_cmd;
  1009. /*
  1010. * Terminate string that points to device name and bump it
  1011. * up one, so we can read the queue id there.
  1012. */
  1013. *delim = '\0';
  1014. if (sscanf(++delim, "%hd\n", &qid) != 1)
  1015. goto err_no_cmd;
  1016. /* Check buffer length, valid ifname and queue id */
  1017. if (strlen(buffer) > IFNAMSIZ ||
  1018. !dev_valid_name(buffer) ||
  1019. qid > bond->dev->real_num_tx_queues)
  1020. goto err_no_cmd;
  1021. /* Get the pointer to that interface if it exists */
  1022. sdev = __dev_get_by_name(dev_net(bond->dev), buffer);
  1023. if (!sdev)
  1024. goto err_no_cmd;
  1025. /* Search for thes slave and check for duplicate qids */
  1026. update_slave = NULL;
  1027. bond_for_each_slave(bond, slave, iter) {
  1028. if (sdev == slave->dev)
  1029. /*
  1030. * We don't need to check the matching
  1031. * slave for dups, since we're overwriting it
  1032. */
  1033. update_slave = slave;
  1034. else if (qid && qid == slave->queue_id) {
  1035. goto err_no_cmd;
  1036. }
  1037. }
  1038. if (!update_slave)
  1039. goto err_no_cmd;
  1040. /* Actually set the qids for the slave */
  1041. update_slave->queue_id = qid;
  1042. out:
  1043. rtnl_unlock();
  1044. return ret;
  1045. err_no_cmd:
  1046. pr_info("invalid input for queue_id set for %s.\n",
  1047. bond->dev->name);
  1048. ret = -EPERM;
  1049. goto out;
  1050. }
  1051. static DEVICE_ATTR(queue_id, S_IRUGO | S_IWUSR, bonding_show_queue_id,
  1052. bonding_store_queue_id);
  1053. /*
  1054. * Show and set the all_slaves_active flag.
  1055. */
  1056. static ssize_t bonding_show_slaves_active(struct device *d,
  1057. struct device_attribute *attr,
  1058. char *buf)
  1059. {
  1060. struct bonding *bond = to_bond(d);
  1061. return sprintf(buf, "%d\n", bond->params.all_slaves_active);
  1062. }
  1063. static ssize_t bonding_store_slaves_active(struct device *d,
  1064. struct device_attribute *attr,
  1065. const char *buf, size_t count)
  1066. {
  1067. struct bonding *bond = to_bond(d);
  1068. int new_value, ret = count;
  1069. struct list_head *iter;
  1070. struct slave *slave;
  1071. if (!rtnl_trylock())
  1072. return restart_syscall();
  1073. if (sscanf(buf, "%d", &new_value) != 1) {
  1074. pr_err("%s: no all_slaves_active value specified.\n",
  1075. bond->dev->name);
  1076. ret = -EINVAL;
  1077. goto out;
  1078. }
  1079. if (new_value == bond->params.all_slaves_active)
  1080. goto out;
  1081. if ((new_value == 0) || (new_value == 1)) {
  1082. bond->params.all_slaves_active = new_value;
  1083. } else {
  1084. pr_info("%s: Ignoring invalid all_slaves_active value %d.\n",
  1085. bond->dev->name, new_value);
  1086. ret = -EINVAL;
  1087. goto out;
  1088. }
  1089. bond_for_each_slave(bond, slave, iter) {
  1090. if (!bond_is_active_slave(slave)) {
  1091. if (new_value)
  1092. slave->inactive = 0;
  1093. else
  1094. slave->inactive = 1;
  1095. }
  1096. }
  1097. out:
  1098. rtnl_unlock();
  1099. return ret;
  1100. }
  1101. static DEVICE_ATTR(all_slaves_active, S_IRUGO | S_IWUSR,
  1102. bonding_show_slaves_active, bonding_store_slaves_active);
  1103. /*
  1104. * Show and set the number of IGMP membership reports to send on link failure
  1105. */
  1106. static ssize_t bonding_show_resend_igmp(struct device *d,
  1107. struct device_attribute *attr,
  1108. char *buf)
  1109. {
  1110. struct bonding *bond = to_bond(d);
  1111. return sprintf(buf, "%d\n", bond->params.resend_igmp);
  1112. }
  1113. static ssize_t bonding_store_resend_igmp(struct device *d,
  1114. struct device_attribute *attr,
  1115. const char *buf, size_t count)
  1116. {
  1117. int new_value, ret = count;
  1118. struct bonding *bond = to_bond(d);
  1119. if (sscanf(buf, "%d", &new_value) != 1) {
  1120. pr_err("%s: no resend_igmp value specified.\n",
  1121. bond->dev->name);
  1122. return -EINVAL;
  1123. }
  1124. if (!rtnl_trylock())
  1125. return restart_syscall();
  1126. ret = bond_option_resend_igmp_set(bond, new_value);
  1127. if (!ret)
  1128. ret = count;
  1129. rtnl_unlock();
  1130. return ret;
  1131. }
  1132. static DEVICE_ATTR(resend_igmp, S_IRUGO | S_IWUSR,
  1133. bonding_show_resend_igmp, bonding_store_resend_igmp);
  1134. static ssize_t bonding_show_lp_interval(struct device *d,
  1135. struct device_attribute *attr,
  1136. char *buf)
  1137. {
  1138. struct bonding *bond = to_bond(d);
  1139. return sprintf(buf, "%d\n", bond->params.lp_interval);
  1140. }
  1141. static ssize_t bonding_store_lp_interval(struct device *d,
  1142. struct device_attribute *attr,
  1143. const char *buf, size_t count)
  1144. {
  1145. struct bonding *bond = to_bond(d);
  1146. int new_value, ret = count;
  1147. if (sscanf(buf, "%d", &new_value) != 1) {
  1148. pr_err("%s: no lp interval value specified.\n",
  1149. bond->dev->name);
  1150. ret = -EINVAL;
  1151. goto out;
  1152. }
  1153. if (new_value <= 0) {
  1154. pr_err ("%s: lp_interval must be between 1 and %d\n",
  1155. bond->dev->name, INT_MAX);
  1156. ret = -EINVAL;
  1157. goto out;
  1158. }
  1159. bond->params.lp_interval = new_value;
  1160. out:
  1161. return ret;
  1162. }
  1163. static DEVICE_ATTR(lp_interval, S_IRUGO | S_IWUSR,
  1164. bonding_show_lp_interval, bonding_store_lp_interval);
  1165. static ssize_t bonding_show_packets_per_slave(struct device *d,
  1166. struct device_attribute *attr,
  1167. char *buf)
  1168. {
  1169. struct bonding *bond = to_bond(d);
  1170. unsigned int packets_per_slave = bond->params.packets_per_slave;
  1171. if (packets_per_slave > 1)
  1172. packets_per_slave = reciprocal_value(packets_per_slave);
  1173. return sprintf(buf, "%u\n", packets_per_slave);
  1174. }
  1175. static ssize_t bonding_store_packets_per_slave(struct device *d,
  1176. struct device_attribute *attr,
  1177. const char *buf, size_t count)
  1178. {
  1179. struct bonding *bond = to_bond(d);
  1180. int new_value, ret = count;
  1181. if (sscanf(buf, "%d", &new_value) != 1) {
  1182. pr_err("%s: no packets_per_slave value specified.\n",
  1183. bond->dev->name);
  1184. ret = -EINVAL;
  1185. goto out;
  1186. }
  1187. if (new_value < 0 || new_value > USHRT_MAX) {
  1188. pr_err("%s: packets_per_slave must be between 0 and %u\n",
  1189. bond->dev->name, USHRT_MAX);
  1190. ret = -EINVAL;
  1191. goto out;
  1192. }
  1193. if (bond->params.mode != BOND_MODE_ROUNDROBIN)
  1194. pr_warn("%s: Warning: packets_per_slave has effect only in balance-rr mode\n",
  1195. bond->dev->name);
  1196. if (new_value > 1)
  1197. bond->params.packets_per_slave = reciprocal_value(new_value);
  1198. else
  1199. bond->params.packets_per_slave = new_value;
  1200. out:
  1201. return ret;
  1202. }
  1203. static DEVICE_ATTR(packets_per_slave, S_IRUGO | S_IWUSR,
  1204. bonding_show_packets_per_slave,
  1205. bonding_store_packets_per_slave);
  1206. static struct attribute *per_bond_attrs[] = {
  1207. &dev_attr_slaves.attr,
  1208. &dev_attr_mode.attr,
  1209. &dev_attr_fail_over_mac.attr,
  1210. &dev_attr_arp_validate.attr,
  1211. &dev_attr_arp_all_targets.attr,
  1212. &dev_attr_arp_interval.attr,
  1213. &dev_attr_arp_ip_target.attr,
  1214. &dev_attr_downdelay.attr,
  1215. &dev_attr_updelay.attr,
  1216. &dev_attr_lacp_rate.attr,
  1217. &dev_attr_ad_select.attr,
  1218. &dev_attr_xmit_hash_policy.attr,
  1219. &dev_attr_num_grat_arp.attr,
  1220. &dev_attr_num_unsol_na.attr,
  1221. &dev_attr_miimon.attr,
  1222. &dev_attr_primary.attr,
  1223. &dev_attr_primary_reselect.attr,
  1224. &dev_attr_use_carrier.attr,
  1225. &dev_attr_active_slave.attr,
  1226. &dev_attr_mii_status.attr,
  1227. &dev_attr_ad_aggregator.attr,
  1228. &dev_attr_ad_num_ports.attr,
  1229. &dev_attr_ad_actor_key.attr,
  1230. &dev_attr_ad_partner_key.attr,
  1231. &dev_attr_ad_partner_mac.attr,
  1232. &dev_attr_queue_id.attr,
  1233. &dev_attr_all_slaves_active.attr,
  1234. &dev_attr_resend_igmp.attr,
  1235. &dev_attr_min_links.attr,
  1236. &dev_attr_lp_interval.attr,
  1237. &dev_attr_packets_per_slave.attr,
  1238. NULL,
  1239. };
  1240. static struct attribute_group bonding_group = {
  1241. .name = "bonding",
  1242. .attrs = per_bond_attrs,
  1243. };
  1244. /*
  1245. * Initialize sysfs. This sets up the bonding_masters file in
  1246. * /sys/class/net.
  1247. */
  1248. int bond_create_sysfs(struct bond_net *bn)
  1249. {
  1250. int ret;
  1251. bn->class_attr_bonding_masters = class_attr_bonding_masters;
  1252. sysfs_attr_init(&bn->class_attr_bonding_masters.attr);
  1253. ret = netdev_class_create_file_ns(&bn->class_attr_bonding_masters,
  1254. bn->net);
  1255. /*
  1256. * Permit multiple loads of the module by ignoring failures to
  1257. * create the bonding_masters sysfs file. Bonding devices
  1258. * created by second or subsequent loads of the module will
  1259. * not be listed in, or controllable by, bonding_masters, but
  1260. * will have the usual "bonding" sysfs directory.
  1261. *
  1262. * This is done to preserve backwards compatibility for
  1263. * initscripts/sysconfig, which load bonding multiple times to
  1264. * configure multiple bonding devices.
  1265. */
  1266. if (ret == -EEXIST) {
  1267. /* Is someone being kinky and naming a device bonding_master? */
  1268. if (__dev_get_by_name(bn->net,
  1269. class_attr_bonding_masters.attr.name))
  1270. pr_err("network device named %s already exists in sysfs",
  1271. class_attr_bonding_masters.attr.name);
  1272. ret = 0;
  1273. }
  1274. return ret;
  1275. }
  1276. /*
  1277. * Remove /sys/class/net/bonding_masters.
  1278. */
  1279. void bond_destroy_sysfs(struct bond_net *bn)
  1280. {
  1281. netdev_class_remove_file_ns(&bn->class_attr_bonding_masters, bn->net);
  1282. }
  1283. /*
  1284. * Initialize sysfs for each bond. This sets up and registers
  1285. * the 'bondctl' directory for each individual bond under /sys/class/net.
  1286. */
  1287. void bond_prepare_sysfs_group(struct bonding *bond)
  1288. {
  1289. bond->dev->sysfs_groups[0] = &bonding_group;
  1290. }