bond_sysfs.c 46 KB

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