bond_sysfs.c 44 KB

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