bond_sysfs.c 45 KB

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