bond_sysfs.c 42 KB

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