bond_sysfs.c 45 KB

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