bond_sysfs.c 44 KB

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