bond_sysfs.c 42 KB

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