bond_sysfs.c 40 KB

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