bond_sysfs.c 44 KB

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