bond_sysfs.c 46 KB

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