bond_sysfs.c 44 KB

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