bond_sysfs.c 44 KB

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