bond_sysfs.c 44 KB

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