bond_sysfs.c 44 KB

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