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