bond_sysfs.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363
  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/sched.h>
  25. #include <linux/device.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/string.h>
  35. #include <linux/ctype.h>
  36. #include <linux/inet.h>
  37. #include <linux/rtnetlink.h>
  38. /* #define BONDING_DEBUG 1 */
  39. #include "bonding.h"
  40. #define to_class_dev(obj) container_of(obj,struct class_device,kobj)
  41. #define to_net_dev(class) container_of(class, struct net_device, class_dev)
  42. #define to_bond(cd) ((struct bonding *)(to_net_dev(cd)->priv))
  43. /*---------------------------- Declarations -------------------------------*/
  44. extern struct list_head bond_dev_list;
  45. extern struct bond_params bonding_defaults;
  46. extern struct bond_parm_tbl bond_mode_tbl[];
  47. extern struct bond_parm_tbl bond_lacp_tbl[];
  48. extern struct bond_parm_tbl xmit_hashtype_tbl[];
  49. static int expected_refcount = -1;
  50. static struct class *netdev_class;
  51. /*--------------------------- Data Structures -----------------------------*/
  52. /* Bonding sysfs lock. Why can't we just use the subsytem lock?
  53. * Because kobject_register tries to acquire the subsystem lock. If
  54. * we already hold the lock (which we would if the user was creating
  55. * a new bond through the sysfs interface), we deadlock.
  56. * This lock is only needed when deleting a bond - we need to make sure
  57. * that we don't collide with an ongoing ioctl.
  58. */
  59. struct rw_semaphore bonding_rwsem;
  60. /*------------------------------ Functions --------------------------------*/
  61. /*
  62. * "show" function for the bond_masters attribute.
  63. * The class parameter is ignored.
  64. */
  65. static ssize_t bonding_show_bonds(struct class *cls, char *buffer)
  66. {
  67. int res = 0;
  68. struct bonding *bond;
  69. down_read(&(bonding_rwsem));
  70. list_for_each_entry(bond, &bond_dev_list, bond_list) {
  71. if (res > (PAGE_SIZE - IFNAMSIZ)) {
  72. /* not enough space for another interface name */
  73. if ((PAGE_SIZE - res) > 10)
  74. res = PAGE_SIZE - 10;
  75. res += sprintf(buffer + res, "++more++");
  76. break;
  77. }
  78. res += sprintf(buffer + res, "%s ",
  79. bond->dev->name);
  80. }
  81. res += sprintf(buffer + res, "\n");
  82. res++;
  83. up_read(&(bonding_rwsem));
  84. return res;
  85. }
  86. /*
  87. * "store" function for the bond_masters attribute. This is what
  88. * creates and deletes entire bonds.
  89. *
  90. * The class parameter is ignored.
  91. *
  92. */
  93. static ssize_t bonding_store_bonds(struct class *cls, const char *buffer, size_t count)
  94. {
  95. char command[IFNAMSIZ + 1] = {0, };
  96. char *ifname;
  97. int res = count;
  98. struct bonding *bond;
  99. struct bonding *nxt;
  100. down_write(&(bonding_rwsem));
  101. sscanf(buffer, "%16s", command); /* IFNAMSIZ*/
  102. ifname = command + 1;
  103. if ((strlen(command) <= 1) ||
  104. !dev_valid_name(ifname))
  105. goto err_no_cmd;
  106. if (command[0] == '+') {
  107. /* Check to see if the bond already exists. */
  108. list_for_each_entry_safe(bond, nxt, &bond_dev_list, bond_list)
  109. if (strnicmp(bond->dev->name, ifname, IFNAMSIZ) == 0) {
  110. printk(KERN_ERR DRV_NAME
  111. ": cannot add bond %s; it already exists\n",
  112. ifname);
  113. res = -EPERM;
  114. goto out;
  115. }
  116. printk(KERN_INFO DRV_NAME
  117. ": %s is being created...\n", ifname);
  118. if (bond_create(ifname, &bonding_defaults, &bond)) {
  119. printk(KERN_INFO DRV_NAME
  120. ": %s interface already exists. Bond creation failed.\n",
  121. ifname);
  122. res = -EPERM;
  123. }
  124. goto out;
  125. }
  126. if (command[0] == '-') {
  127. list_for_each_entry_safe(bond, nxt, &bond_dev_list, bond_list)
  128. if (strnicmp(bond->dev->name, ifname, IFNAMSIZ) == 0) {
  129. rtnl_lock();
  130. /* check the ref count on the bond's kobject.
  131. * If it's > expected, then there's a file open,
  132. * and we have to fail.
  133. */
  134. if (atomic_read(&bond->dev->class_dev.kobj.kref.refcount)
  135. > expected_refcount){
  136. rtnl_unlock();
  137. printk(KERN_INFO DRV_NAME
  138. ": Unable remove bond %s due to open references.\n",
  139. ifname);
  140. res = -EPERM;
  141. goto out;
  142. }
  143. printk(KERN_INFO DRV_NAME
  144. ": %s is being deleted...\n",
  145. bond->dev->name);
  146. unregister_netdevice(bond->dev);
  147. bond_deinit(bond->dev);
  148. bond_destroy_sysfs_entry(bond);
  149. rtnl_unlock();
  150. goto out;
  151. }
  152. printk(KERN_ERR DRV_NAME
  153. ": unable to delete non-existent bond %s\n", ifname);
  154. res = -ENODEV;
  155. goto out;
  156. }
  157. err_no_cmd:
  158. printk(KERN_ERR DRV_NAME
  159. ": no command found in bonding_masters. Use +ifname or -ifname.\n");
  160. res = -EPERM;
  161. /* Always return either count or an error. If you return 0, you'll
  162. * get called forever, which is bad.
  163. */
  164. out:
  165. up_write(&(bonding_rwsem));
  166. return res;
  167. }
  168. /* class attribute for bond_masters file. This ends up in /sys/class/net */
  169. static CLASS_ATTR(bonding_masters, S_IWUSR | S_IRUGO,
  170. bonding_show_bonds, bonding_store_bonds);
  171. int bond_create_slave_symlinks(struct net_device *master, struct net_device *slave)
  172. {
  173. char linkname[IFNAMSIZ+7];
  174. int ret = 0;
  175. /* first, create a link from the slave back to the master */
  176. ret = sysfs_create_link(&(slave->class_dev.kobj), &(master->class_dev.kobj),
  177. "master");
  178. if (ret)
  179. return ret;
  180. /* next, create a link from the master to the slave */
  181. sprintf(linkname,"slave_%s",slave->name);
  182. ret = sysfs_create_link(&(master->class_dev.kobj), &(slave->class_dev.kobj),
  183. linkname);
  184. return ret;
  185. }
  186. void bond_destroy_slave_symlinks(struct net_device *master, struct net_device *slave)
  187. {
  188. char linkname[IFNAMSIZ+7];
  189. sysfs_remove_link(&(slave->class_dev.kobj), "master");
  190. sprintf(linkname,"slave_%s",slave->name);
  191. sysfs_remove_link(&(master->class_dev.kobj), linkname);
  192. }
  193. /*
  194. * Show the slaves in the current bond.
  195. */
  196. static ssize_t bonding_show_slaves(struct class_device *cd, char *buf)
  197. {
  198. struct slave *slave;
  199. int i, res = 0;
  200. struct bonding *bond = to_bond(cd);
  201. read_lock_bh(&bond->lock);
  202. bond_for_each_slave(bond, slave, i) {
  203. if (res > (PAGE_SIZE - IFNAMSIZ)) {
  204. /* not enough space for another interface name */
  205. if ((PAGE_SIZE - res) > 10)
  206. res = PAGE_SIZE - 10;
  207. res += sprintf(buf + res, "++more++");
  208. break;
  209. }
  210. res += sprintf(buf + res, "%s ", slave->dev->name);
  211. }
  212. read_unlock_bh(&bond->lock);
  213. res += sprintf(buf + res, "\n");
  214. res++;
  215. return res;
  216. }
  217. /*
  218. * Set the slaves in the current bond. The bond interface must be
  219. * up for this to succeed.
  220. * This function is largely the same flow as bonding_update_bonds().
  221. */
  222. static ssize_t bonding_store_slaves(struct class_device *cd, const char *buffer, size_t count)
  223. {
  224. char command[IFNAMSIZ + 1] = { 0, };
  225. char *ifname;
  226. int i, res, found, ret = count;
  227. struct slave *slave;
  228. struct net_device *dev = NULL;
  229. struct bonding *bond = to_bond(cd);
  230. /* Quick sanity check -- is the bond interface up? */
  231. if (!(bond->dev->flags & IFF_UP)) {
  232. printk(KERN_ERR DRV_NAME
  233. ": %s: Unable to update slaves because interface is down.\n",
  234. bond->dev->name);
  235. ret = -EPERM;
  236. goto out;
  237. }
  238. /* Note: We can't hold bond->lock here, as bond_create grabs it. */
  239. sscanf(buffer, "%16s", command); /* IFNAMSIZ*/
  240. ifname = command + 1;
  241. if ((strlen(command) <= 1) ||
  242. !dev_valid_name(ifname))
  243. goto err_no_cmd;
  244. if (command[0] == '+') {
  245. /* Got a slave name in ifname. Is it already in the list? */
  246. found = 0;
  247. read_lock_bh(&bond->lock);
  248. bond_for_each_slave(bond, slave, i)
  249. if (strnicmp(slave->dev->name, ifname, IFNAMSIZ) == 0) {
  250. printk(KERN_ERR DRV_NAME
  251. ": %s: Interface %s is already enslaved!\n",
  252. bond->dev->name, ifname);
  253. ret = -EPERM;
  254. read_unlock_bh(&bond->lock);
  255. goto out;
  256. }
  257. read_unlock_bh(&bond->lock);
  258. printk(KERN_INFO DRV_NAME ": %s: Adding slave %s.\n",
  259. bond->dev->name, ifname);
  260. dev = dev_get_by_name(ifname);
  261. if (!dev) {
  262. printk(KERN_INFO DRV_NAME
  263. ": %s: Interface %s does not exist!\n",
  264. bond->dev->name, ifname);
  265. ret = -EPERM;
  266. goto out;
  267. }
  268. else
  269. dev_put(dev);
  270. if (dev->flags & IFF_UP) {
  271. printk(KERN_ERR DRV_NAME
  272. ": %s: Error: Unable to enslave %s "
  273. "because it is already up.\n",
  274. bond->dev->name, dev->name);
  275. ret = -EPERM;
  276. goto out;
  277. }
  278. /* If this is the first slave, then we need to set
  279. the master's hardware address to be the same as the
  280. slave's. */
  281. if (!(*((u32 *) & (bond->dev->dev_addr[0])))) {
  282. memcpy(bond->dev->dev_addr, dev->dev_addr,
  283. dev->addr_len);
  284. }
  285. /* Set the slave's MTU to match the bond */
  286. if (dev->mtu != bond->dev->mtu) {
  287. if (dev->change_mtu) {
  288. res = dev->change_mtu(dev,
  289. bond->dev->mtu);
  290. if (res) {
  291. ret = res;
  292. goto out;
  293. }
  294. } else {
  295. dev->mtu = bond->dev->mtu;
  296. }
  297. }
  298. rtnl_lock();
  299. res = bond_enslave(bond->dev, dev);
  300. rtnl_unlock();
  301. if (res) {
  302. ret = res;
  303. }
  304. goto out;
  305. }
  306. if (command[0] == '-') {
  307. dev = NULL;
  308. bond_for_each_slave(bond, slave, i)
  309. if (strnicmp(slave->dev->name, ifname, IFNAMSIZ) == 0) {
  310. dev = slave->dev;
  311. break;
  312. }
  313. if (dev) {
  314. printk(KERN_INFO DRV_NAME ": %s: Removing slave %s\n",
  315. bond->dev->name, dev->name);
  316. rtnl_lock();
  317. res = bond_release(bond->dev, dev);
  318. rtnl_unlock();
  319. if (res) {
  320. ret = res;
  321. goto out;
  322. }
  323. /* set the slave MTU to the default */
  324. if (dev->change_mtu) {
  325. dev->change_mtu(dev, 1500);
  326. } else {
  327. dev->mtu = 1500;
  328. }
  329. }
  330. else {
  331. printk(KERN_ERR DRV_NAME ": unable to remove non-existent slave %s for bond %s.\n",
  332. ifname, bond->dev->name);
  333. ret = -ENODEV;
  334. }
  335. goto out;
  336. }
  337. err_no_cmd:
  338. printk(KERN_ERR DRV_NAME ": no command found in slaves file for bond %s. Use +ifname or -ifname.\n", bond->dev->name);
  339. ret = -EPERM;
  340. out:
  341. return ret;
  342. }
  343. static CLASS_DEVICE_ATTR(slaves, S_IRUGO | S_IWUSR, bonding_show_slaves, bonding_store_slaves);
  344. /*
  345. * Show and set the bonding mode. The bond interface must be down to
  346. * change the mode.
  347. */
  348. static ssize_t bonding_show_mode(struct class_device *cd, char *buf)
  349. {
  350. struct bonding *bond = to_bond(cd);
  351. return sprintf(buf, "%s %d\n",
  352. bond_mode_tbl[bond->params.mode].modename,
  353. bond->params.mode) + 1;
  354. }
  355. static ssize_t bonding_store_mode(struct class_device *cd, const char *buf, size_t count)
  356. {
  357. int new_value, ret = count;
  358. struct bonding *bond = to_bond(cd);
  359. if (bond->dev->flags & IFF_UP) {
  360. printk(KERN_ERR DRV_NAME
  361. ": unable to update mode of %s because interface is up.\n",
  362. bond->dev->name);
  363. ret = -EPERM;
  364. goto out;
  365. }
  366. new_value = bond_parse_parm((char *)buf, bond_mode_tbl);
  367. if (new_value < 0) {
  368. printk(KERN_ERR DRV_NAME
  369. ": %s: Ignoring invalid mode value %.*s.\n",
  370. bond->dev->name,
  371. (int)strlen(buf) - 1, buf);
  372. ret = -EINVAL;
  373. goto out;
  374. } else {
  375. if (bond->params.mode == BOND_MODE_8023AD)
  376. bond_unset_master_3ad_flags(bond);
  377. if (bond->params.mode == BOND_MODE_ALB)
  378. bond_unset_master_alb_flags(bond);
  379. bond->params.mode = new_value;
  380. bond_set_mode_ops(bond, bond->params.mode);
  381. printk(KERN_INFO DRV_NAME ": %s: setting mode to %s (%d).\n",
  382. bond->dev->name, bond_mode_tbl[new_value].modename, new_value);
  383. }
  384. out:
  385. return ret;
  386. }
  387. static CLASS_DEVICE_ATTR(mode, S_IRUGO | S_IWUSR, bonding_show_mode, bonding_store_mode);
  388. /*
  389. * Show and set the bonding transmit hash method. The bond interface must be down to
  390. * change the xmit hash policy.
  391. */
  392. static ssize_t bonding_show_xmit_hash(struct class_device *cd, char *buf)
  393. {
  394. int count;
  395. struct bonding *bond = to_bond(cd);
  396. if ((bond->params.mode != BOND_MODE_XOR) &&
  397. (bond->params.mode != BOND_MODE_8023AD)) {
  398. // Not Applicable
  399. count = sprintf(buf, "NA\n") + 1;
  400. } else {
  401. count = sprintf(buf, "%s %d\n",
  402. xmit_hashtype_tbl[bond->params.xmit_policy].modename,
  403. bond->params.xmit_policy) + 1;
  404. }
  405. return count;
  406. }
  407. static ssize_t bonding_store_xmit_hash(struct class_device *cd, const char *buf, size_t count)
  408. {
  409. int new_value, ret = count;
  410. struct bonding *bond = to_bond(cd);
  411. if (bond->dev->flags & IFF_UP) {
  412. printk(KERN_ERR DRV_NAME
  413. "%s: Interface is up. Unable to update xmit policy.\n",
  414. bond->dev->name);
  415. ret = -EPERM;
  416. goto out;
  417. }
  418. if ((bond->params.mode != BOND_MODE_XOR) &&
  419. (bond->params.mode != BOND_MODE_8023AD)) {
  420. printk(KERN_ERR DRV_NAME
  421. "%s: Transmit hash policy is irrelevant in this mode.\n",
  422. bond->dev->name);
  423. ret = -EPERM;
  424. goto out;
  425. }
  426. new_value = bond_parse_parm((char *)buf, xmit_hashtype_tbl);
  427. if (new_value < 0) {
  428. printk(KERN_ERR DRV_NAME
  429. ": %s: Ignoring invalid xmit hash policy value %.*s.\n",
  430. bond->dev->name,
  431. (int)strlen(buf) - 1, buf);
  432. ret = -EINVAL;
  433. goto out;
  434. } else {
  435. bond->params.xmit_policy = new_value;
  436. bond_set_mode_ops(bond, bond->params.mode);
  437. printk(KERN_INFO DRV_NAME ": %s: setting xmit hash policy to %s (%d).\n",
  438. bond->dev->name, xmit_hashtype_tbl[new_value].modename, new_value);
  439. }
  440. out:
  441. return ret;
  442. }
  443. static CLASS_DEVICE_ATTR(xmit_hash_policy, S_IRUGO | S_IWUSR, bonding_show_xmit_hash, bonding_store_xmit_hash);
  444. /*
  445. * Show and set the arp timer interval. There are two tricky bits
  446. * here. First, if ARP monitoring is activated, then we must disable
  447. * MII monitoring. Second, if the ARP timer isn't running, we must
  448. * start it.
  449. */
  450. static ssize_t bonding_show_arp_interval(struct class_device *cd, char *buf)
  451. {
  452. struct bonding *bond = to_bond(cd);
  453. return sprintf(buf, "%d\n", bond->params.arp_interval) + 1;
  454. }
  455. static ssize_t bonding_store_arp_interval(struct class_device *cd, const char *buf, size_t count)
  456. {
  457. int new_value, ret = count;
  458. struct bonding *bond = to_bond(cd);
  459. if (sscanf(buf, "%d", &new_value) != 1) {
  460. printk(KERN_ERR DRV_NAME
  461. ": %s: no arp_interval value specified.\n",
  462. bond->dev->name);
  463. ret = -EINVAL;
  464. goto out;
  465. }
  466. if (new_value < 0) {
  467. printk(KERN_ERR DRV_NAME
  468. ": %s: Invalid arp_interval value %d not in range 1-%d; rejected.\n",
  469. bond->dev->name, new_value, INT_MAX);
  470. ret = -EINVAL;
  471. goto out;
  472. }
  473. printk(KERN_INFO DRV_NAME
  474. ": %s: Setting ARP monitoring interval to %d.\n",
  475. bond->dev->name, new_value);
  476. bond->params.arp_interval = new_value;
  477. if (bond->params.miimon) {
  478. printk(KERN_INFO DRV_NAME
  479. ": %s: ARP monitoring cannot be used with MII monitoring. "
  480. "%s Disabling MII monitoring.\n",
  481. bond->dev->name, bond->dev->name);
  482. bond->params.miimon = 0;
  483. /* Kill MII timer, else it brings bond's link down */
  484. if (bond->arp_timer.function) {
  485. printk(KERN_INFO DRV_NAME
  486. ": %s: Kill MII timer, else it brings bond's link down...\n",
  487. bond->dev->name);
  488. del_timer_sync(&bond->mii_timer);
  489. }
  490. }
  491. if (!bond->params.arp_targets[0]) {
  492. printk(KERN_INFO DRV_NAME
  493. ": %s: ARP monitoring has been set up, "
  494. "but no ARP targets have been specified.\n",
  495. bond->dev->name);
  496. }
  497. if (bond->dev->flags & IFF_UP) {
  498. /* If the interface is up, we may need to fire off
  499. * the ARP timer. If the interface is down, the
  500. * timer will get fired off when the open function
  501. * is called.
  502. */
  503. if (bond->arp_timer.function) {
  504. /* The timer's already set up, so fire it off */
  505. mod_timer(&bond->arp_timer, jiffies + 1);
  506. } else {
  507. /* Set up the timer. */
  508. init_timer(&bond->arp_timer);
  509. bond->arp_timer.expires = jiffies + 1;
  510. bond->arp_timer.data =
  511. (unsigned long) bond->dev;
  512. if (bond->params.mode == BOND_MODE_ACTIVEBACKUP) {
  513. bond->arp_timer.function =
  514. (void *)
  515. &bond_activebackup_arp_mon;
  516. } else {
  517. bond->arp_timer.function =
  518. (void *)
  519. &bond_loadbalance_arp_mon;
  520. }
  521. add_timer(&bond->arp_timer);
  522. }
  523. }
  524. out:
  525. return ret;
  526. }
  527. static CLASS_DEVICE_ATTR(arp_interval, S_IRUGO | S_IWUSR , bonding_show_arp_interval, bonding_store_arp_interval);
  528. /*
  529. * Show and set the arp targets.
  530. */
  531. static ssize_t bonding_show_arp_targets(struct class_device *cd, char *buf)
  532. {
  533. int i, res = 0;
  534. struct bonding *bond = to_bond(cd);
  535. for (i = 0; i < BOND_MAX_ARP_TARGETS; i++) {
  536. if (bond->params.arp_targets[i])
  537. res += sprintf(buf + res, "%u.%u.%u.%u ",
  538. NIPQUAD(bond->params.arp_targets[i]));
  539. }
  540. if (res)
  541. res--; /* eat the leftover space */
  542. res += sprintf(buf + res, "\n");
  543. res++;
  544. return res;
  545. }
  546. static ssize_t bonding_store_arp_targets(struct class_device *cd, const char *buf, size_t count)
  547. {
  548. u32 newtarget;
  549. int i = 0, done = 0, ret = count;
  550. struct bonding *bond = to_bond(cd);
  551. u32 *targets;
  552. targets = bond->params.arp_targets;
  553. newtarget = in_aton(buf + 1);
  554. /* look for adds */
  555. if (buf[0] == '+') {
  556. if ((newtarget == 0) || (newtarget == INADDR_BROADCAST)) {
  557. printk(KERN_ERR DRV_NAME
  558. ": %s: invalid ARP target %u.%u.%u.%u specified for addition\n",
  559. bond->dev->name, NIPQUAD(newtarget));
  560. ret = -EINVAL;
  561. goto out;
  562. }
  563. /* look for an empty slot to put the target in, and check for dupes */
  564. for (i = 0; (i < BOND_MAX_ARP_TARGETS); i++) {
  565. if (targets[i] == newtarget) { /* duplicate */
  566. printk(KERN_ERR DRV_NAME
  567. ": %s: ARP target %u.%u.%u.%u is already present\n",
  568. bond->dev->name, NIPQUAD(newtarget));
  569. if (done)
  570. targets[i] = 0;
  571. ret = -EINVAL;
  572. goto out;
  573. }
  574. if (targets[i] == 0 && !done) {
  575. printk(KERN_INFO DRV_NAME
  576. ": %s: adding ARP target %d.%d.%d.%d.\n",
  577. bond->dev->name, NIPQUAD(newtarget));
  578. done = 1;
  579. targets[i] = newtarget;
  580. }
  581. }
  582. if (!done) {
  583. printk(KERN_ERR DRV_NAME
  584. ": %s: ARP target table is full!\n",
  585. bond->dev->name);
  586. ret = -EINVAL;
  587. goto out;
  588. }
  589. }
  590. else if (buf[0] == '-') {
  591. if ((newtarget == 0) || (newtarget == INADDR_BROADCAST)) {
  592. printk(KERN_ERR DRV_NAME
  593. ": %s: invalid ARP target %d.%d.%d.%d specified for removal\n",
  594. bond->dev->name, NIPQUAD(newtarget));
  595. ret = -EINVAL;
  596. goto out;
  597. }
  598. for (i = 0; (i < BOND_MAX_ARP_TARGETS); i++) {
  599. if (targets[i] == newtarget) {
  600. printk(KERN_INFO DRV_NAME
  601. ": %s: removing ARP target %d.%d.%d.%d.\n",
  602. bond->dev->name, NIPQUAD(newtarget));
  603. targets[i] = 0;
  604. done = 1;
  605. }
  606. }
  607. if (!done) {
  608. printk(KERN_INFO DRV_NAME
  609. ": %s: unable to remove nonexistent ARP target %d.%d.%d.%d.\n",
  610. bond->dev->name, NIPQUAD(newtarget));
  611. ret = -EINVAL;
  612. goto out;
  613. }
  614. }
  615. else {
  616. printk(KERN_ERR DRV_NAME ": no command found in arp_ip_targets file for bond %s. Use +<addr> or -<addr>.\n",
  617. bond->dev->name);
  618. ret = -EPERM;
  619. goto out;
  620. }
  621. out:
  622. return ret;
  623. }
  624. static CLASS_DEVICE_ATTR(arp_ip_target, S_IRUGO | S_IWUSR , bonding_show_arp_targets, bonding_store_arp_targets);
  625. /*
  626. * Show and set the up and down delays. These must be multiples of the
  627. * MII monitoring value, and are stored internally as the multiplier.
  628. * Thus, we must translate to MS for the real world.
  629. */
  630. static ssize_t bonding_show_downdelay(struct class_device *cd, char *buf)
  631. {
  632. struct bonding *bond = to_bond(cd);
  633. return sprintf(buf, "%d\n", bond->params.downdelay * bond->params.miimon) + 1;
  634. }
  635. static ssize_t bonding_store_downdelay(struct class_device *cd, const char *buf, size_t count)
  636. {
  637. int new_value, ret = count;
  638. struct bonding *bond = to_bond(cd);
  639. if (!(bond->params.miimon)) {
  640. printk(KERN_ERR DRV_NAME
  641. ": %s: Unable to set down delay as MII monitoring is disabled\n",
  642. bond->dev->name);
  643. ret = -EPERM;
  644. goto out;
  645. }
  646. if (sscanf(buf, "%d", &new_value) != 1) {
  647. printk(KERN_ERR DRV_NAME
  648. ": %s: no down delay value specified.\n",
  649. bond->dev->name);
  650. ret = -EINVAL;
  651. goto out;
  652. }
  653. if (new_value < 0) {
  654. printk(KERN_ERR DRV_NAME
  655. ": %s: Invalid down delay value %d not in range %d-%d; rejected.\n",
  656. bond->dev->name, new_value, 1, INT_MAX);
  657. ret = -EINVAL;
  658. goto out;
  659. } else {
  660. if ((new_value % bond->params.miimon) != 0) {
  661. printk(KERN_WARNING DRV_NAME
  662. ": %s: Warning: down delay (%d) is not a multiple "
  663. "of miimon (%d), delay rounded to %d ms\n",
  664. bond->dev->name, new_value, bond->params.miimon,
  665. (new_value / bond->params.miimon) *
  666. bond->params.miimon);
  667. }
  668. bond->params.downdelay = new_value / bond->params.miimon;
  669. printk(KERN_INFO DRV_NAME ": %s: Setting down delay to %d.\n",
  670. bond->dev->name, bond->params.downdelay * bond->params.miimon);
  671. }
  672. out:
  673. return ret;
  674. }
  675. static CLASS_DEVICE_ATTR(downdelay, S_IRUGO | S_IWUSR , bonding_show_downdelay, bonding_store_downdelay);
  676. static ssize_t bonding_show_updelay(struct class_device *cd, char *buf)
  677. {
  678. struct bonding *bond = to_bond(cd);
  679. return sprintf(buf, "%d\n", bond->params.updelay * bond->params.miimon) + 1;
  680. }
  681. static ssize_t bonding_store_updelay(struct class_device *cd, const char *buf, size_t count)
  682. {
  683. int new_value, ret = count;
  684. struct bonding *bond = to_bond(cd);
  685. if (!(bond->params.miimon)) {
  686. printk(KERN_ERR DRV_NAME
  687. ": %s: Unable to set up delay as MII monitoring is disabled\n",
  688. bond->dev->name);
  689. ret = -EPERM;
  690. goto out;
  691. }
  692. if (sscanf(buf, "%d", &new_value) != 1) {
  693. printk(KERN_ERR DRV_NAME
  694. ": %s: no up delay value specified.\n",
  695. bond->dev->name);
  696. ret = -EINVAL;
  697. goto out;
  698. }
  699. if (new_value < 0) {
  700. printk(KERN_ERR DRV_NAME
  701. ": %s: Invalid down delay value %d not in range %d-%d; rejected.\n",
  702. bond->dev->name, new_value, 1, INT_MAX);
  703. ret = -EINVAL;
  704. goto out;
  705. } else {
  706. if ((new_value % bond->params.miimon) != 0) {
  707. printk(KERN_WARNING DRV_NAME
  708. ": %s: Warning: up delay (%d) is not a multiple "
  709. "of miimon (%d), updelay rounded to %d ms\n",
  710. bond->dev->name, new_value, bond->params.miimon,
  711. (new_value / bond->params.miimon) *
  712. bond->params.miimon);
  713. }
  714. bond->params.updelay = new_value / bond->params.miimon;
  715. printk(KERN_INFO DRV_NAME ": %s: Setting up delay to %d.\n",
  716. bond->dev->name, bond->params.updelay * bond->params.miimon);
  717. }
  718. out:
  719. return ret;
  720. }
  721. static CLASS_DEVICE_ATTR(updelay, S_IRUGO | S_IWUSR , bonding_show_updelay, bonding_store_updelay);
  722. /*
  723. * Show and set the LACP interval. Interface must be down, and the mode
  724. * must be set to 802.3ad mode.
  725. */
  726. static ssize_t bonding_show_lacp(struct class_device *cd, char *buf)
  727. {
  728. struct bonding *bond = to_bond(cd);
  729. return sprintf(buf, "%s %d\n",
  730. bond_lacp_tbl[bond->params.lacp_fast].modename,
  731. bond->params.lacp_fast) + 1;
  732. }
  733. static ssize_t bonding_store_lacp(struct class_device *cd, const char *buf, size_t count)
  734. {
  735. int new_value, ret = count;
  736. struct bonding *bond = to_bond(cd);
  737. if (bond->dev->flags & IFF_UP) {
  738. printk(KERN_ERR DRV_NAME
  739. ": %s: Unable to update LACP rate because interface is up.\n",
  740. bond->dev->name);
  741. ret = -EPERM;
  742. goto out;
  743. }
  744. if (bond->params.mode != BOND_MODE_8023AD) {
  745. printk(KERN_ERR DRV_NAME
  746. ": %s: Unable to update LACP rate because bond is not in 802.3ad mode.\n",
  747. bond->dev->name);
  748. ret = -EPERM;
  749. goto out;
  750. }
  751. new_value = bond_parse_parm((char *)buf, bond_lacp_tbl);
  752. if ((new_value == 1) || (new_value == 0)) {
  753. bond->params.lacp_fast = new_value;
  754. printk(KERN_INFO DRV_NAME
  755. ": %s: Setting LACP rate to %s (%d).\n",
  756. bond->dev->name, bond_lacp_tbl[new_value].modename, new_value);
  757. } else {
  758. printk(KERN_ERR DRV_NAME
  759. ": %s: Ignoring invalid LACP rate value %.*s.\n",
  760. bond->dev->name, (int)strlen(buf) - 1, buf);
  761. ret = -EINVAL;
  762. }
  763. out:
  764. return ret;
  765. }
  766. static CLASS_DEVICE_ATTR(lacp_rate, S_IRUGO | S_IWUSR, bonding_show_lacp, bonding_store_lacp);
  767. /*
  768. * Show and set the MII monitor interval. There are two tricky bits
  769. * here. First, if MII monitoring is activated, then we must disable
  770. * ARP monitoring. Second, if the timer isn't running, we must
  771. * start it.
  772. */
  773. static ssize_t bonding_show_miimon(struct class_device *cd, char *buf)
  774. {
  775. struct bonding *bond = to_bond(cd);
  776. return sprintf(buf, "%d\n", bond->params.miimon) + 1;
  777. }
  778. static ssize_t bonding_store_miimon(struct class_device *cd, const char *buf, size_t count)
  779. {
  780. int new_value, ret = count;
  781. struct bonding *bond = to_bond(cd);
  782. if (sscanf(buf, "%d", &new_value) != 1) {
  783. printk(KERN_ERR DRV_NAME
  784. ": %s: no miimon value specified.\n",
  785. bond->dev->name);
  786. ret = -EINVAL;
  787. goto out;
  788. }
  789. if (new_value < 0) {
  790. printk(KERN_ERR DRV_NAME
  791. ": %s: Invalid miimon value %d not in range %d-%d; rejected.\n",
  792. bond->dev->name, new_value, 1, INT_MAX);
  793. ret = -EINVAL;
  794. goto out;
  795. } else {
  796. printk(KERN_INFO DRV_NAME
  797. ": %s: Setting MII monitoring interval to %d.\n",
  798. bond->dev->name, new_value);
  799. bond->params.miimon = new_value;
  800. if(bond->params.updelay)
  801. printk(KERN_INFO DRV_NAME
  802. ": %s: Note: Updating updelay (to %d) "
  803. "since it is a multiple of the miimon value.\n",
  804. bond->dev->name,
  805. bond->params.updelay * bond->params.miimon);
  806. if(bond->params.downdelay)
  807. printk(KERN_INFO DRV_NAME
  808. ": %s: Note: Updating downdelay (to %d) "
  809. "since it is a multiple of the miimon value.\n",
  810. bond->dev->name,
  811. bond->params.downdelay * bond->params.miimon);
  812. if (bond->params.arp_interval) {
  813. printk(KERN_INFO DRV_NAME
  814. ": %s: MII monitoring cannot be used with "
  815. "ARP monitoring. Disabling ARP monitoring...\n",
  816. bond->dev->name);
  817. bond->params.arp_interval = 0;
  818. /* Kill ARP timer, else it brings bond's link down */
  819. if (bond->mii_timer.function) {
  820. printk(KERN_INFO DRV_NAME
  821. ": %s: Kill ARP timer, else it brings bond's link down...\n",
  822. bond->dev->name);
  823. del_timer_sync(&bond->arp_timer);
  824. }
  825. }
  826. if (bond->dev->flags & IFF_UP) {
  827. /* If the interface is up, we may need to fire off
  828. * the MII timer. If the interface is down, the
  829. * timer will get fired off when the open function
  830. * is called.
  831. */
  832. if (bond->mii_timer.function) {
  833. /* The timer's already set up, so fire it off */
  834. mod_timer(&bond->mii_timer, jiffies + 1);
  835. } else {
  836. /* Set up the timer. */
  837. init_timer(&bond->mii_timer);
  838. bond->mii_timer.expires = jiffies + 1;
  839. bond->mii_timer.data =
  840. (unsigned long) bond->dev;
  841. bond->mii_timer.function =
  842. (void *) &bond_mii_monitor;
  843. add_timer(&bond->mii_timer);
  844. }
  845. }
  846. }
  847. out:
  848. return ret;
  849. }
  850. static CLASS_DEVICE_ATTR(miimon, S_IRUGO | S_IWUSR, bonding_show_miimon, bonding_store_miimon);
  851. /*
  852. * Show and set the primary slave. The store function is much
  853. * simpler than bonding_store_slaves function because it only needs to
  854. * handle one interface name.
  855. * The bond must be a mode that supports a primary for this be
  856. * set.
  857. */
  858. static ssize_t bonding_show_primary(struct class_device *cd, char *buf)
  859. {
  860. int count = 0;
  861. struct bonding *bond = to_bond(cd);
  862. if (bond->primary_slave)
  863. count = sprintf(buf, "%s\n", bond->primary_slave->dev->name) + 1;
  864. else
  865. count = sprintf(buf, "\n") + 1;
  866. return count;
  867. }
  868. static ssize_t bonding_store_primary(struct class_device *cd, const char *buf, size_t count)
  869. {
  870. int i;
  871. struct slave *slave;
  872. struct bonding *bond = to_bond(cd);
  873. write_lock_bh(&bond->lock);
  874. if (!USES_PRIMARY(bond->params.mode)) {
  875. printk(KERN_INFO DRV_NAME
  876. ": %s: Unable to set primary slave; %s is in mode %d\n",
  877. bond->dev->name, bond->dev->name, bond->params.mode);
  878. } else {
  879. bond_for_each_slave(bond, slave, i) {
  880. if (strnicmp
  881. (slave->dev->name, buf,
  882. strlen(slave->dev->name)) == 0) {
  883. printk(KERN_INFO DRV_NAME
  884. ": %s: Setting %s as primary slave.\n",
  885. bond->dev->name, slave->dev->name);
  886. bond->primary_slave = slave;
  887. bond_select_active_slave(bond);
  888. goto out;
  889. }
  890. }
  891. /* if we got here, then we didn't match the name of any slave */
  892. if (strlen(buf) == 0 || buf[0] == '\n') {
  893. printk(KERN_INFO DRV_NAME
  894. ": %s: Setting primary slave to None.\n",
  895. bond->dev->name);
  896. bond->primary_slave = NULL;
  897. bond_select_active_slave(bond);
  898. } else {
  899. printk(KERN_INFO DRV_NAME
  900. ": %s: Unable to set %.*s as primary slave as it is not a slave.\n",
  901. bond->dev->name, (int)strlen(buf) - 1, buf);
  902. }
  903. }
  904. out:
  905. write_unlock_bh(&bond->lock);
  906. return count;
  907. }
  908. static CLASS_DEVICE_ATTR(primary, S_IRUGO | S_IWUSR, bonding_show_primary, bonding_store_primary);
  909. /*
  910. * Show and set the use_carrier flag.
  911. */
  912. static ssize_t bonding_show_carrier(struct class_device *cd, char *buf)
  913. {
  914. struct bonding *bond = to_bond(cd);
  915. return sprintf(buf, "%d\n", bond->params.use_carrier) + 1;
  916. }
  917. static ssize_t bonding_store_carrier(struct class_device *cd, const char *buf, size_t count)
  918. {
  919. int new_value, ret = count;
  920. struct bonding *bond = to_bond(cd);
  921. if (sscanf(buf, "%d", &new_value) != 1) {
  922. printk(KERN_ERR DRV_NAME
  923. ": %s: no use_carrier value specified.\n",
  924. bond->dev->name);
  925. ret = -EINVAL;
  926. goto out;
  927. }
  928. if ((new_value == 0) || (new_value == 1)) {
  929. bond->params.use_carrier = new_value;
  930. printk(KERN_INFO DRV_NAME ": %s: Setting use_carrier to %d.\n",
  931. bond->dev->name, new_value);
  932. } else {
  933. printk(KERN_INFO DRV_NAME
  934. ": %s: Ignoring invalid use_carrier value %d.\n",
  935. bond->dev->name, new_value);
  936. }
  937. out:
  938. return count;
  939. }
  940. static CLASS_DEVICE_ATTR(use_carrier, S_IRUGO | S_IWUSR, bonding_show_carrier, bonding_store_carrier);
  941. /*
  942. * Show and set currently active_slave.
  943. */
  944. static ssize_t bonding_show_active_slave(struct class_device *cd, char *buf)
  945. {
  946. struct slave *curr;
  947. struct bonding *bond = to_bond(cd);
  948. int count;
  949. read_lock(&bond->curr_slave_lock);
  950. curr = bond->curr_active_slave;
  951. read_unlock(&bond->curr_slave_lock);
  952. if (USES_PRIMARY(bond->params.mode) && curr)
  953. count = sprintf(buf, "%s\n", curr->dev->name) + 1;
  954. else
  955. count = sprintf(buf, "\n") + 1;
  956. return count;
  957. }
  958. static ssize_t bonding_store_active_slave(struct class_device *cd, const char *buf, size_t count)
  959. {
  960. int i;
  961. struct slave *slave;
  962. struct slave *old_active = NULL;
  963. struct slave *new_active = NULL;
  964. struct bonding *bond = to_bond(cd);
  965. write_lock_bh(&bond->lock);
  966. if (!USES_PRIMARY(bond->params.mode)) {
  967. printk(KERN_INFO DRV_NAME
  968. ": %s: Unable to change active slave; %s is in mode %d\n",
  969. bond->dev->name, bond->dev->name, bond->params.mode);
  970. } else {
  971. bond_for_each_slave(bond, slave, i) {
  972. if (strnicmp
  973. (slave->dev->name, buf,
  974. strlen(slave->dev->name)) == 0) {
  975. old_active = bond->curr_active_slave;
  976. new_active = slave;
  977. if (new_active && (new_active == old_active)) {
  978. /* do nothing */
  979. printk(KERN_INFO DRV_NAME
  980. ": %s: %s is already the current active slave.\n",
  981. bond->dev->name, slave->dev->name);
  982. goto out;
  983. }
  984. else {
  985. if ((new_active) &&
  986. (old_active) &&
  987. (new_active->link == BOND_LINK_UP) &&
  988. IS_UP(new_active->dev)) {
  989. printk(KERN_INFO DRV_NAME
  990. ": %s: Setting %s as active slave.\n",
  991. bond->dev->name, slave->dev->name);
  992. bond_change_active_slave(bond, new_active);
  993. }
  994. else {
  995. printk(KERN_INFO DRV_NAME
  996. ": %s: Could not set %s as active slave; "
  997. "either %s is down or the link is down.\n",
  998. bond->dev->name, slave->dev->name,
  999. slave->dev->name);
  1000. }
  1001. goto out;
  1002. }
  1003. }
  1004. }
  1005. /* if we got here, then we didn't match the name of any slave */
  1006. if (strlen(buf) == 0 || buf[0] == '\n') {
  1007. printk(KERN_INFO DRV_NAME
  1008. ": %s: Setting active slave to None.\n",
  1009. bond->dev->name);
  1010. bond->primary_slave = NULL;
  1011. bond_select_active_slave(bond);
  1012. } else {
  1013. printk(KERN_INFO DRV_NAME
  1014. ": %s: Unable to set %.*s as active slave as it is not a slave.\n",
  1015. bond->dev->name, (int)strlen(buf) - 1, buf);
  1016. }
  1017. }
  1018. out:
  1019. write_unlock_bh(&bond->lock);
  1020. return count;
  1021. }
  1022. static CLASS_DEVICE_ATTR(active_slave, S_IRUGO | S_IWUSR, bonding_show_active_slave, bonding_store_active_slave);
  1023. /*
  1024. * Show link status of the bond interface.
  1025. */
  1026. static ssize_t bonding_show_mii_status(struct class_device *cd, char *buf)
  1027. {
  1028. struct slave *curr;
  1029. struct bonding *bond = to_bond(cd);
  1030. read_lock(&bond->curr_slave_lock);
  1031. curr = bond->curr_active_slave;
  1032. read_unlock(&bond->curr_slave_lock);
  1033. return sprintf(buf, "%s\n", (curr) ? "up" : "down") + 1;
  1034. }
  1035. static CLASS_DEVICE_ATTR(mii_status, S_IRUGO, bonding_show_mii_status, NULL);
  1036. /*
  1037. * Show current 802.3ad aggregator ID.
  1038. */
  1039. static ssize_t bonding_show_ad_aggregator(struct class_device *cd, char *buf)
  1040. {
  1041. int count = 0;
  1042. struct bonding *bond = to_bond(cd);
  1043. if (bond->params.mode == BOND_MODE_8023AD) {
  1044. struct ad_info ad_info;
  1045. count = sprintf(buf, "%d\n", (bond_3ad_get_active_agg_info(bond, &ad_info)) ? 0 : ad_info.aggregator_id) + 1;
  1046. }
  1047. else
  1048. count = sprintf(buf, "\n") + 1;
  1049. return count;
  1050. }
  1051. static CLASS_DEVICE_ATTR(ad_aggregator, S_IRUGO, bonding_show_ad_aggregator, NULL);
  1052. /*
  1053. * Show number of active 802.3ad ports.
  1054. */
  1055. static ssize_t bonding_show_ad_num_ports(struct class_device *cd, char *buf)
  1056. {
  1057. int count = 0;
  1058. struct bonding *bond = to_bond(cd);
  1059. if (bond->params.mode == BOND_MODE_8023AD) {
  1060. struct ad_info ad_info;
  1061. count = sprintf(buf, "%d\n", (bond_3ad_get_active_agg_info(bond, &ad_info)) ? 0: ad_info.ports) + 1;
  1062. }
  1063. else
  1064. count = sprintf(buf, "\n") + 1;
  1065. return count;
  1066. }
  1067. static CLASS_DEVICE_ATTR(ad_num_ports, S_IRUGO, bonding_show_ad_num_ports, NULL);
  1068. /*
  1069. * Show current 802.3ad actor key.
  1070. */
  1071. static ssize_t bonding_show_ad_actor_key(struct class_device *cd, char *buf)
  1072. {
  1073. int count = 0;
  1074. struct bonding *bond = to_bond(cd);
  1075. if (bond->params.mode == BOND_MODE_8023AD) {
  1076. struct ad_info ad_info;
  1077. count = sprintf(buf, "%d\n", (bond_3ad_get_active_agg_info(bond, &ad_info)) ? 0 : ad_info.actor_key) + 1;
  1078. }
  1079. else
  1080. count = sprintf(buf, "\n") + 1;
  1081. return count;
  1082. }
  1083. static CLASS_DEVICE_ATTR(ad_actor_key, S_IRUGO, bonding_show_ad_actor_key, NULL);
  1084. /*
  1085. * Show current 802.3ad partner key.
  1086. */
  1087. static ssize_t bonding_show_ad_partner_key(struct class_device *cd, char *buf)
  1088. {
  1089. int count = 0;
  1090. struct bonding *bond = to_bond(cd);
  1091. if (bond->params.mode == BOND_MODE_8023AD) {
  1092. struct ad_info ad_info;
  1093. count = sprintf(buf, "%d\n", (bond_3ad_get_active_agg_info(bond, &ad_info)) ? 0 : ad_info.partner_key) + 1;
  1094. }
  1095. else
  1096. count = sprintf(buf, "\n") + 1;
  1097. return count;
  1098. }
  1099. static CLASS_DEVICE_ATTR(ad_partner_key, S_IRUGO, bonding_show_ad_partner_key, NULL);
  1100. /*
  1101. * Show current 802.3ad partner mac.
  1102. */
  1103. static ssize_t bonding_show_ad_partner_mac(struct class_device *cd, char *buf)
  1104. {
  1105. int count = 0;
  1106. struct bonding *bond = to_bond(cd);
  1107. if (bond->params.mode == BOND_MODE_8023AD) {
  1108. struct ad_info ad_info;
  1109. if (!bond_3ad_get_active_agg_info(bond, &ad_info)) {
  1110. count = sprintf(buf,"%02x:%02x:%02x:%02x:%02x:%02x\n",
  1111. ad_info.partner_system[0],
  1112. ad_info.partner_system[1],
  1113. ad_info.partner_system[2],
  1114. ad_info.partner_system[3],
  1115. ad_info.partner_system[4],
  1116. ad_info.partner_system[5]) + 1;
  1117. }
  1118. }
  1119. else
  1120. count = sprintf(buf, "\n") + 1;
  1121. return count;
  1122. }
  1123. static CLASS_DEVICE_ATTR(ad_partner_mac, S_IRUGO, bonding_show_ad_partner_mac, NULL);
  1124. static struct attribute *per_bond_attrs[] = {
  1125. &class_device_attr_slaves.attr,
  1126. &class_device_attr_mode.attr,
  1127. &class_device_attr_arp_interval.attr,
  1128. &class_device_attr_arp_ip_target.attr,
  1129. &class_device_attr_downdelay.attr,
  1130. &class_device_attr_updelay.attr,
  1131. &class_device_attr_lacp_rate.attr,
  1132. &class_device_attr_xmit_hash_policy.attr,
  1133. &class_device_attr_miimon.attr,
  1134. &class_device_attr_primary.attr,
  1135. &class_device_attr_use_carrier.attr,
  1136. &class_device_attr_active_slave.attr,
  1137. &class_device_attr_mii_status.attr,
  1138. &class_device_attr_ad_aggregator.attr,
  1139. &class_device_attr_ad_num_ports.attr,
  1140. &class_device_attr_ad_actor_key.attr,
  1141. &class_device_attr_ad_partner_key.attr,
  1142. &class_device_attr_ad_partner_mac.attr,
  1143. NULL,
  1144. };
  1145. static struct attribute_group bonding_group = {
  1146. .name = "bonding",
  1147. .attrs = per_bond_attrs,
  1148. };
  1149. /*
  1150. * Initialize sysfs. This sets up the bonding_masters file in
  1151. * /sys/class/net.
  1152. */
  1153. int bond_create_sysfs(void)
  1154. {
  1155. int ret = 0;
  1156. struct bonding *firstbond;
  1157. init_rwsem(&bonding_rwsem);
  1158. /* get the netdev class pointer */
  1159. firstbond = container_of(bond_dev_list.next, struct bonding, bond_list);
  1160. if (!firstbond)
  1161. return -ENODEV;
  1162. netdev_class = firstbond->dev->class_dev.class;
  1163. if (!netdev_class)
  1164. return -ENODEV;
  1165. ret = class_create_file(netdev_class, &class_attr_bonding_masters);
  1166. return ret;
  1167. }
  1168. /*
  1169. * Remove /sys/class/net/bonding_masters.
  1170. */
  1171. void bond_destroy_sysfs(void)
  1172. {
  1173. if (netdev_class)
  1174. class_remove_file(netdev_class, &class_attr_bonding_masters);
  1175. }
  1176. /*
  1177. * Initialize sysfs for each bond. This sets up and registers
  1178. * the 'bondctl' directory for each individual bond under /sys/class/net.
  1179. */
  1180. int bond_create_sysfs_entry(struct bonding *bond)
  1181. {
  1182. struct net_device *dev = bond->dev;
  1183. int err;
  1184. err = sysfs_create_group(&(dev->class_dev.kobj), &bonding_group);
  1185. if (err) {
  1186. printk(KERN_EMERG "eek! didn't create group!\n");
  1187. }
  1188. if (expected_refcount < 1)
  1189. expected_refcount = atomic_read(&bond->dev->class_dev.kobj.kref.refcount);
  1190. return err;
  1191. }
  1192. /*
  1193. * Remove sysfs entries for each bond.
  1194. */
  1195. void bond_destroy_sysfs_entry(struct bonding *bond)
  1196. {
  1197. struct net_device *dev = bond->dev;
  1198. sysfs_remove_group(&(dev->class_dev.kobj), &bonding_group);
  1199. }