bond_sysfs.c 45 KB

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