bond_sysfs.c 39 KB

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