bond_sysfs.c 46 KB

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