bond_sysfs.c 42 KB

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