bond_sysfs.c 42 KB

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