bond_sysfs.c 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507
  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.miimon) {
  544. printk(KERN_INFO DRV_NAME
  545. ": %s: ARP monitoring cannot be used with MII monitoring. "
  546. "%s Disabling MII monitoring.\n",
  547. bond->dev->name, bond->dev->name);
  548. bond->params.miimon = 0;
  549. if (delayed_work_pending(&bond->mii_work)) {
  550. cancel_delayed_work(&bond->mii_work);
  551. flush_workqueue(bond->wq);
  552. }
  553. }
  554. if (!bond->params.arp_targets[0]) {
  555. printk(KERN_INFO DRV_NAME
  556. ": %s: ARP monitoring has been set up, "
  557. "but no ARP targets have been specified.\n",
  558. bond->dev->name);
  559. }
  560. if (bond->dev->flags & IFF_UP) {
  561. /* If the interface is up, we may need to fire off
  562. * the ARP timer. If the interface is down, the
  563. * timer will get fired off when the open function
  564. * is called.
  565. */
  566. if (!delayed_work_pending(&bond->arp_work)) {
  567. if (bond->params.mode == BOND_MODE_ACTIVEBACKUP)
  568. INIT_DELAYED_WORK(&bond->arp_work,
  569. bond_activebackup_arp_mon);
  570. else
  571. INIT_DELAYED_WORK(&bond->arp_work,
  572. bond_loadbalance_arp_mon);
  573. queue_delayed_work(bond->wq, &bond->arp_work, 0);
  574. }
  575. }
  576. out:
  577. return ret;
  578. }
  579. static DEVICE_ATTR(arp_interval, S_IRUGO | S_IWUSR , bonding_show_arp_interval, bonding_store_arp_interval);
  580. /*
  581. * Show and set the arp targets.
  582. */
  583. static ssize_t bonding_show_arp_targets(struct device *d,
  584. struct device_attribute *attr,
  585. char *buf)
  586. {
  587. int i, res = 0;
  588. struct bonding *bond = to_bond(d);
  589. for (i = 0; i < BOND_MAX_ARP_TARGETS; i++) {
  590. if (bond->params.arp_targets[i])
  591. res += sprintf(buf + res, "%u.%u.%u.%u ",
  592. NIPQUAD(bond->params.arp_targets[i]));
  593. }
  594. if (res)
  595. buf[res-1] = '\n'; /* eat the leftover space */
  596. return res;
  597. }
  598. static ssize_t bonding_store_arp_targets(struct device *d,
  599. struct device_attribute *attr,
  600. const char *buf, size_t count)
  601. {
  602. __be32 newtarget;
  603. int i = 0, done = 0, ret = count;
  604. struct bonding *bond = to_bond(d);
  605. __be32 *targets;
  606. targets = bond->params.arp_targets;
  607. newtarget = in_aton(buf + 1);
  608. /* look for adds */
  609. if (buf[0] == '+') {
  610. if ((newtarget == 0) || (newtarget == htonl(INADDR_BROADCAST))) {
  611. printk(KERN_ERR DRV_NAME
  612. ": %s: invalid ARP target %u.%u.%u.%u specified for addition\n",
  613. bond->dev->name, NIPQUAD(newtarget));
  614. ret = -EINVAL;
  615. goto out;
  616. }
  617. /* look for an empty slot to put the target in, and check for dupes */
  618. for (i = 0; (i < BOND_MAX_ARP_TARGETS); i++) {
  619. if (targets[i] == newtarget) { /* duplicate */
  620. printk(KERN_ERR DRV_NAME
  621. ": %s: ARP target %u.%u.%u.%u is already present\n",
  622. bond->dev->name, NIPQUAD(newtarget));
  623. if (done)
  624. targets[i] = 0;
  625. ret = -EINVAL;
  626. goto out;
  627. }
  628. if (targets[i] == 0 && !done) {
  629. printk(KERN_INFO DRV_NAME
  630. ": %s: adding ARP target %d.%d.%d.%d.\n",
  631. bond->dev->name, NIPQUAD(newtarget));
  632. done = 1;
  633. targets[i] = newtarget;
  634. }
  635. }
  636. if (!done) {
  637. printk(KERN_ERR DRV_NAME
  638. ": %s: ARP target table is full!\n",
  639. bond->dev->name);
  640. ret = -EINVAL;
  641. goto out;
  642. }
  643. }
  644. else if (buf[0] == '-') {
  645. if ((newtarget == 0) || (newtarget == htonl(INADDR_BROADCAST))) {
  646. printk(KERN_ERR DRV_NAME
  647. ": %s: invalid ARP target %d.%d.%d.%d specified for removal\n",
  648. bond->dev->name, NIPQUAD(newtarget));
  649. ret = -EINVAL;
  650. goto out;
  651. }
  652. for (i = 0; (i < BOND_MAX_ARP_TARGETS); i++) {
  653. if (targets[i] == newtarget) {
  654. printk(KERN_INFO DRV_NAME
  655. ": %s: removing ARP target %d.%d.%d.%d.\n",
  656. bond->dev->name, NIPQUAD(newtarget));
  657. targets[i] = 0;
  658. done = 1;
  659. }
  660. }
  661. if (!done) {
  662. printk(KERN_INFO DRV_NAME
  663. ": %s: unable to remove nonexistent ARP target %d.%d.%d.%d.\n",
  664. bond->dev->name, NIPQUAD(newtarget));
  665. ret = -EINVAL;
  666. goto out;
  667. }
  668. }
  669. else {
  670. printk(KERN_ERR DRV_NAME ": no command found in arp_ip_targets file for bond %s. Use +<addr> or -<addr>.\n",
  671. bond->dev->name);
  672. ret = -EPERM;
  673. goto out;
  674. }
  675. out:
  676. return ret;
  677. }
  678. static DEVICE_ATTR(arp_ip_target, S_IRUGO | S_IWUSR , bonding_show_arp_targets, bonding_store_arp_targets);
  679. /*
  680. * Show and set the up and down delays. These must be multiples of the
  681. * MII monitoring value, and are stored internally as the multiplier.
  682. * Thus, we must translate to MS for the real world.
  683. */
  684. static ssize_t bonding_show_downdelay(struct device *d,
  685. struct device_attribute *attr,
  686. char *buf)
  687. {
  688. struct bonding *bond = to_bond(d);
  689. return sprintf(buf, "%d\n", bond->params.downdelay * bond->params.miimon);
  690. }
  691. static ssize_t bonding_store_downdelay(struct device *d,
  692. struct device_attribute *attr,
  693. const char *buf, size_t count)
  694. {
  695. int new_value, ret = count;
  696. struct bonding *bond = to_bond(d);
  697. if (!(bond->params.miimon)) {
  698. printk(KERN_ERR DRV_NAME
  699. ": %s: Unable to set down delay as MII monitoring is disabled\n",
  700. bond->dev->name);
  701. ret = -EPERM;
  702. goto out;
  703. }
  704. if (sscanf(buf, "%d", &new_value) != 1) {
  705. printk(KERN_ERR DRV_NAME
  706. ": %s: no down delay value specified.\n",
  707. bond->dev->name);
  708. ret = -EINVAL;
  709. goto out;
  710. }
  711. if (new_value < 0) {
  712. printk(KERN_ERR DRV_NAME
  713. ": %s: Invalid down delay value %d not in range %d-%d; rejected.\n",
  714. bond->dev->name, new_value, 1, INT_MAX);
  715. ret = -EINVAL;
  716. goto out;
  717. } else {
  718. if ((new_value % bond->params.miimon) != 0) {
  719. printk(KERN_WARNING DRV_NAME
  720. ": %s: Warning: down delay (%d) is not a multiple "
  721. "of miimon (%d), delay rounded to %d ms\n",
  722. bond->dev->name, new_value, bond->params.miimon,
  723. (new_value / bond->params.miimon) *
  724. bond->params.miimon);
  725. }
  726. bond->params.downdelay = new_value / bond->params.miimon;
  727. printk(KERN_INFO DRV_NAME ": %s: Setting down delay to %d.\n",
  728. bond->dev->name, bond->params.downdelay * bond->params.miimon);
  729. }
  730. out:
  731. return ret;
  732. }
  733. static DEVICE_ATTR(downdelay, S_IRUGO | S_IWUSR , bonding_show_downdelay, bonding_store_downdelay);
  734. static ssize_t bonding_show_updelay(struct device *d,
  735. struct device_attribute *attr,
  736. char *buf)
  737. {
  738. struct bonding *bond = to_bond(d);
  739. return sprintf(buf, "%d\n", bond->params.updelay * bond->params.miimon);
  740. }
  741. static ssize_t bonding_store_updelay(struct device *d,
  742. struct device_attribute *attr,
  743. const char *buf, size_t count)
  744. {
  745. int new_value, ret = count;
  746. struct bonding *bond = to_bond(d);
  747. if (!(bond->params.miimon)) {
  748. printk(KERN_ERR DRV_NAME
  749. ": %s: Unable to set up delay as MII monitoring is disabled\n",
  750. bond->dev->name);
  751. ret = -EPERM;
  752. goto out;
  753. }
  754. if (sscanf(buf, "%d", &new_value) != 1) {
  755. printk(KERN_ERR DRV_NAME
  756. ": %s: no up delay value specified.\n",
  757. bond->dev->name);
  758. ret = -EINVAL;
  759. goto out;
  760. }
  761. if (new_value < 0) {
  762. printk(KERN_ERR DRV_NAME
  763. ": %s: Invalid down delay value %d not in range %d-%d; rejected.\n",
  764. bond->dev->name, new_value, 1, INT_MAX);
  765. ret = -EINVAL;
  766. goto out;
  767. } else {
  768. if ((new_value % bond->params.miimon) != 0) {
  769. printk(KERN_WARNING DRV_NAME
  770. ": %s: Warning: up delay (%d) is not a multiple "
  771. "of miimon (%d), updelay rounded to %d ms\n",
  772. bond->dev->name, new_value, bond->params.miimon,
  773. (new_value / bond->params.miimon) *
  774. bond->params.miimon);
  775. }
  776. bond->params.updelay = new_value / bond->params.miimon;
  777. printk(KERN_INFO DRV_NAME ": %s: Setting up delay to %d.\n",
  778. bond->dev->name, bond->params.updelay * bond->params.miimon);
  779. }
  780. out:
  781. return ret;
  782. }
  783. static DEVICE_ATTR(updelay, S_IRUGO | S_IWUSR , bonding_show_updelay, bonding_store_updelay);
  784. /*
  785. * Show and set the LACP interval. Interface must be down, and the mode
  786. * must be set to 802.3ad mode.
  787. */
  788. static ssize_t bonding_show_lacp(struct device *d,
  789. struct device_attribute *attr,
  790. char *buf)
  791. {
  792. struct bonding *bond = to_bond(d);
  793. return sprintf(buf, "%s %d\n",
  794. bond_lacp_tbl[bond->params.lacp_fast].modename,
  795. bond->params.lacp_fast);
  796. }
  797. static ssize_t bonding_store_lacp(struct device *d,
  798. struct device_attribute *attr,
  799. const char *buf, size_t count)
  800. {
  801. int new_value, ret = count;
  802. struct bonding *bond = to_bond(d);
  803. if (bond->dev->flags & IFF_UP) {
  804. printk(KERN_ERR DRV_NAME
  805. ": %s: Unable to update LACP rate because interface is up.\n",
  806. bond->dev->name);
  807. ret = -EPERM;
  808. goto out;
  809. }
  810. if (bond->params.mode != BOND_MODE_8023AD) {
  811. printk(KERN_ERR DRV_NAME
  812. ": %s: Unable to update LACP rate because bond is not in 802.3ad mode.\n",
  813. bond->dev->name);
  814. ret = -EPERM;
  815. goto out;
  816. }
  817. new_value = bond_parse_parm(buf, bond_lacp_tbl);
  818. if ((new_value == 1) || (new_value == 0)) {
  819. bond->params.lacp_fast = new_value;
  820. printk(KERN_INFO DRV_NAME
  821. ": %s: Setting LACP rate to %s (%d).\n",
  822. bond->dev->name, bond_lacp_tbl[new_value].modename, new_value);
  823. } else {
  824. printk(KERN_ERR DRV_NAME
  825. ": %s: Ignoring invalid LACP rate value %.*s.\n",
  826. bond->dev->name, (int)strlen(buf) - 1, buf);
  827. ret = -EINVAL;
  828. }
  829. out:
  830. return ret;
  831. }
  832. static DEVICE_ATTR(lacp_rate, S_IRUGO | S_IWUSR, bonding_show_lacp, bonding_store_lacp);
  833. /*
  834. * Show and set the number of grat ARP to send after a failover event.
  835. */
  836. static ssize_t bonding_show_n_grat_arp(struct device *d,
  837. struct device_attribute *attr,
  838. char *buf)
  839. {
  840. struct bonding *bond = to_bond(d);
  841. return sprintf(buf, "%d\n", bond->params.num_grat_arp);
  842. }
  843. static ssize_t bonding_store_n_grat_arp(struct device *d,
  844. struct device_attribute *attr,
  845. const char *buf, size_t count)
  846. {
  847. int new_value, ret = count;
  848. struct bonding *bond = to_bond(d);
  849. if (sscanf(buf, "%d", &new_value) != 1) {
  850. printk(KERN_ERR DRV_NAME
  851. ": %s: no num_grat_arp value specified.\n",
  852. bond->dev->name);
  853. ret = -EINVAL;
  854. goto out;
  855. }
  856. if (new_value < 0 || new_value > 255) {
  857. printk(KERN_ERR DRV_NAME
  858. ": %s: Invalid num_grat_arp value %d not in range 0-255; rejected.\n",
  859. bond->dev->name, new_value);
  860. ret = -EINVAL;
  861. goto out;
  862. } else {
  863. bond->params.num_grat_arp = new_value;
  864. }
  865. out:
  866. return ret;
  867. }
  868. static DEVICE_ATTR(num_grat_arp, S_IRUGO | S_IWUSR, bonding_show_n_grat_arp, bonding_store_n_grat_arp);
  869. /*
  870. * Show and set the MII monitor interval. There are two tricky bits
  871. * here. First, if MII monitoring is activated, then we must disable
  872. * ARP monitoring. Second, if the timer isn't running, we must
  873. * start it.
  874. */
  875. static ssize_t bonding_show_miimon(struct device *d,
  876. struct device_attribute *attr,
  877. char *buf)
  878. {
  879. struct bonding *bond = to_bond(d);
  880. return sprintf(buf, "%d\n", bond->params.miimon);
  881. }
  882. static ssize_t bonding_store_miimon(struct device *d,
  883. struct device_attribute *attr,
  884. const char *buf, size_t count)
  885. {
  886. int new_value, ret = count;
  887. struct bonding *bond = to_bond(d);
  888. if (sscanf(buf, "%d", &new_value) != 1) {
  889. printk(KERN_ERR DRV_NAME
  890. ": %s: no miimon value specified.\n",
  891. bond->dev->name);
  892. ret = -EINVAL;
  893. goto out;
  894. }
  895. if (new_value < 0) {
  896. printk(KERN_ERR DRV_NAME
  897. ": %s: Invalid miimon value %d not in range %d-%d; rejected.\n",
  898. bond->dev->name, new_value, 1, INT_MAX);
  899. ret = -EINVAL;
  900. goto out;
  901. } else {
  902. printk(KERN_INFO DRV_NAME
  903. ": %s: Setting MII monitoring interval to %d.\n",
  904. bond->dev->name, new_value);
  905. bond->params.miimon = new_value;
  906. if(bond->params.updelay)
  907. printk(KERN_INFO DRV_NAME
  908. ": %s: Note: Updating updelay (to %d) "
  909. "since it is a multiple of the miimon value.\n",
  910. bond->dev->name,
  911. bond->params.updelay * bond->params.miimon);
  912. if(bond->params.downdelay)
  913. printk(KERN_INFO DRV_NAME
  914. ": %s: Note: Updating downdelay (to %d) "
  915. "since it is a multiple of the miimon value.\n",
  916. bond->dev->name,
  917. bond->params.downdelay * bond->params.miimon);
  918. if (bond->params.arp_interval) {
  919. printk(KERN_INFO DRV_NAME
  920. ": %s: MII monitoring cannot be used with "
  921. "ARP monitoring. Disabling ARP monitoring...\n",
  922. bond->dev->name);
  923. bond->params.arp_interval = 0;
  924. if (bond->params.arp_validate) {
  925. bond_unregister_arp(bond);
  926. bond->params.arp_validate =
  927. BOND_ARP_VALIDATE_NONE;
  928. }
  929. if (delayed_work_pending(&bond->arp_work)) {
  930. cancel_delayed_work(&bond->arp_work);
  931. flush_workqueue(bond->wq);
  932. }
  933. }
  934. if (bond->dev->flags & IFF_UP) {
  935. /* If the interface is up, we may need to fire off
  936. * the MII timer. If the interface is down, the
  937. * timer will get fired off when the open function
  938. * is called.
  939. */
  940. if (!delayed_work_pending(&bond->mii_work)) {
  941. INIT_DELAYED_WORK(&bond->mii_work,
  942. bond_mii_monitor);
  943. queue_delayed_work(bond->wq,
  944. &bond->mii_work, 0);
  945. }
  946. }
  947. }
  948. out:
  949. return ret;
  950. }
  951. static DEVICE_ATTR(miimon, S_IRUGO | S_IWUSR, bonding_show_miimon, bonding_store_miimon);
  952. /*
  953. * Show and set the primary slave. The store function is much
  954. * simpler than bonding_store_slaves function because it only needs to
  955. * handle one interface name.
  956. * The bond must be a mode that supports a primary for this be
  957. * set.
  958. */
  959. static ssize_t bonding_show_primary(struct device *d,
  960. struct device_attribute *attr,
  961. char *buf)
  962. {
  963. int count = 0;
  964. struct bonding *bond = to_bond(d);
  965. if (bond->primary_slave)
  966. count = sprintf(buf, "%s\n", bond->primary_slave->dev->name);
  967. return count;
  968. }
  969. static ssize_t bonding_store_primary(struct device *d,
  970. struct device_attribute *attr,
  971. const char *buf, size_t count)
  972. {
  973. int i;
  974. struct slave *slave;
  975. struct bonding *bond = to_bond(d);
  976. rtnl_lock();
  977. read_lock(&bond->lock);
  978. write_lock_bh(&bond->curr_slave_lock);
  979. if (!USES_PRIMARY(bond->params.mode)) {
  980. printk(KERN_INFO DRV_NAME
  981. ": %s: Unable to set primary slave; %s is in mode %d\n",
  982. bond->dev->name, bond->dev->name, bond->params.mode);
  983. } else {
  984. bond_for_each_slave(bond, slave, i) {
  985. if (strnicmp
  986. (slave->dev->name, buf,
  987. strlen(slave->dev->name)) == 0) {
  988. printk(KERN_INFO DRV_NAME
  989. ": %s: Setting %s as primary slave.\n",
  990. bond->dev->name, slave->dev->name);
  991. bond->primary_slave = slave;
  992. bond_select_active_slave(bond);
  993. goto out;
  994. }
  995. }
  996. /* if we got here, then we didn't match the name of any slave */
  997. if (strlen(buf) == 0 || buf[0] == '\n') {
  998. printk(KERN_INFO DRV_NAME
  999. ": %s: Setting primary slave to None.\n",
  1000. bond->dev->name);
  1001. bond->primary_slave = NULL;
  1002. bond_select_active_slave(bond);
  1003. } else {
  1004. printk(KERN_INFO DRV_NAME
  1005. ": %s: Unable to set %.*s as primary slave as it is not a slave.\n",
  1006. bond->dev->name, (int)strlen(buf) - 1, buf);
  1007. }
  1008. }
  1009. out:
  1010. write_unlock_bh(&bond->curr_slave_lock);
  1011. read_unlock(&bond->lock);
  1012. rtnl_unlock();
  1013. return count;
  1014. }
  1015. static DEVICE_ATTR(primary, S_IRUGO | S_IWUSR, bonding_show_primary, bonding_store_primary);
  1016. /*
  1017. * Show and set the use_carrier flag.
  1018. */
  1019. static ssize_t bonding_show_carrier(struct device *d,
  1020. struct device_attribute *attr,
  1021. char *buf)
  1022. {
  1023. struct bonding *bond = to_bond(d);
  1024. return sprintf(buf, "%d\n", bond->params.use_carrier);
  1025. }
  1026. static ssize_t bonding_store_carrier(struct device *d,
  1027. struct device_attribute *attr,
  1028. const char *buf, size_t count)
  1029. {
  1030. int new_value, ret = count;
  1031. struct bonding *bond = to_bond(d);
  1032. if (sscanf(buf, "%d", &new_value) != 1) {
  1033. printk(KERN_ERR DRV_NAME
  1034. ": %s: no use_carrier value specified.\n",
  1035. bond->dev->name);
  1036. ret = -EINVAL;
  1037. goto out;
  1038. }
  1039. if ((new_value == 0) || (new_value == 1)) {
  1040. bond->params.use_carrier = new_value;
  1041. printk(KERN_INFO DRV_NAME ": %s: Setting use_carrier to %d.\n",
  1042. bond->dev->name, new_value);
  1043. } else {
  1044. printk(KERN_INFO DRV_NAME
  1045. ": %s: Ignoring invalid use_carrier value %d.\n",
  1046. bond->dev->name, new_value);
  1047. }
  1048. out:
  1049. return count;
  1050. }
  1051. static DEVICE_ATTR(use_carrier, S_IRUGO | S_IWUSR, bonding_show_carrier, bonding_store_carrier);
  1052. /*
  1053. * Show and set currently active_slave.
  1054. */
  1055. static ssize_t bonding_show_active_slave(struct device *d,
  1056. struct device_attribute *attr,
  1057. char *buf)
  1058. {
  1059. struct slave *curr;
  1060. struct bonding *bond = to_bond(d);
  1061. int count = 0;
  1062. read_lock(&bond->curr_slave_lock);
  1063. curr = bond->curr_active_slave;
  1064. read_unlock(&bond->curr_slave_lock);
  1065. if (USES_PRIMARY(bond->params.mode) && curr)
  1066. count = sprintf(buf, "%s\n", curr->dev->name);
  1067. return count;
  1068. }
  1069. static ssize_t bonding_store_active_slave(struct device *d,
  1070. struct device_attribute *attr,
  1071. const char *buf, size_t count)
  1072. {
  1073. int i;
  1074. struct slave *slave;
  1075. struct slave *old_active = NULL;
  1076. struct slave *new_active = NULL;
  1077. struct bonding *bond = to_bond(d);
  1078. rtnl_lock();
  1079. read_lock(&bond->lock);
  1080. write_lock_bh(&bond->curr_slave_lock);
  1081. if (!USES_PRIMARY(bond->params.mode)) {
  1082. printk(KERN_INFO DRV_NAME
  1083. ": %s: Unable to change active slave; %s is in mode %d\n",
  1084. bond->dev->name, bond->dev->name, bond->params.mode);
  1085. } else {
  1086. bond_for_each_slave(bond, slave, i) {
  1087. if (strnicmp
  1088. (slave->dev->name, buf,
  1089. strlen(slave->dev->name)) == 0) {
  1090. old_active = bond->curr_active_slave;
  1091. new_active = slave;
  1092. if (new_active == old_active) {
  1093. /* do nothing */
  1094. printk(KERN_INFO DRV_NAME
  1095. ": %s: %s is already the current active slave.\n",
  1096. bond->dev->name, slave->dev->name);
  1097. goto out;
  1098. }
  1099. else {
  1100. if ((new_active) &&
  1101. (old_active) &&
  1102. (new_active->link == BOND_LINK_UP) &&
  1103. IS_UP(new_active->dev)) {
  1104. printk(KERN_INFO DRV_NAME
  1105. ": %s: Setting %s as active slave.\n",
  1106. bond->dev->name, slave->dev->name);
  1107. bond_change_active_slave(bond, new_active);
  1108. }
  1109. else {
  1110. printk(KERN_INFO DRV_NAME
  1111. ": %s: Could not set %s as active slave; "
  1112. "either %s is down or the link is down.\n",
  1113. bond->dev->name, slave->dev->name,
  1114. slave->dev->name);
  1115. }
  1116. goto out;
  1117. }
  1118. }
  1119. }
  1120. /* if we got here, then we didn't match the name of any slave */
  1121. if (strlen(buf) == 0 || buf[0] == '\n') {
  1122. printk(KERN_INFO DRV_NAME
  1123. ": %s: Setting active slave to None.\n",
  1124. bond->dev->name);
  1125. bond->primary_slave = NULL;
  1126. bond_select_active_slave(bond);
  1127. } else {
  1128. printk(KERN_INFO DRV_NAME
  1129. ": %s: Unable to set %.*s as active slave as it is not a slave.\n",
  1130. bond->dev->name, (int)strlen(buf) - 1, buf);
  1131. }
  1132. }
  1133. out:
  1134. write_unlock_bh(&bond->curr_slave_lock);
  1135. read_unlock(&bond->lock);
  1136. rtnl_unlock();
  1137. return count;
  1138. }
  1139. static DEVICE_ATTR(active_slave, S_IRUGO | S_IWUSR, bonding_show_active_slave, bonding_store_active_slave);
  1140. /*
  1141. * Show link status of the bond interface.
  1142. */
  1143. static ssize_t bonding_show_mii_status(struct device *d,
  1144. struct device_attribute *attr,
  1145. char *buf)
  1146. {
  1147. struct slave *curr;
  1148. struct bonding *bond = to_bond(d);
  1149. read_lock(&bond->curr_slave_lock);
  1150. curr = bond->curr_active_slave;
  1151. read_unlock(&bond->curr_slave_lock);
  1152. return sprintf(buf, "%s\n", (curr) ? "up" : "down");
  1153. }
  1154. static DEVICE_ATTR(mii_status, S_IRUGO, bonding_show_mii_status, NULL);
  1155. /*
  1156. * Show current 802.3ad aggregator ID.
  1157. */
  1158. static ssize_t bonding_show_ad_aggregator(struct device *d,
  1159. struct device_attribute *attr,
  1160. char *buf)
  1161. {
  1162. int count = 0;
  1163. struct bonding *bond = to_bond(d);
  1164. if (bond->params.mode == BOND_MODE_8023AD) {
  1165. struct ad_info ad_info;
  1166. count = sprintf(buf, "%d\n", (bond_3ad_get_active_agg_info(bond, &ad_info)) ? 0 : ad_info.aggregator_id);
  1167. }
  1168. return count;
  1169. }
  1170. static DEVICE_ATTR(ad_aggregator, S_IRUGO, bonding_show_ad_aggregator, NULL);
  1171. /*
  1172. * Show number of active 802.3ad ports.
  1173. */
  1174. static ssize_t bonding_show_ad_num_ports(struct device *d,
  1175. struct device_attribute *attr,
  1176. char *buf)
  1177. {
  1178. int count = 0;
  1179. struct bonding *bond = to_bond(d);
  1180. if (bond->params.mode == BOND_MODE_8023AD) {
  1181. struct ad_info ad_info;
  1182. count = sprintf(buf, "%d\n", (bond_3ad_get_active_agg_info(bond, &ad_info)) ? 0: ad_info.ports);
  1183. }
  1184. return count;
  1185. }
  1186. static DEVICE_ATTR(ad_num_ports, S_IRUGO, bonding_show_ad_num_ports, NULL);
  1187. /*
  1188. * Show current 802.3ad actor key.
  1189. */
  1190. static ssize_t bonding_show_ad_actor_key(struct device *d,
  1191. struct device_attribute *attr,
  1192. char *buf)
  1193. {
  1194. int count = 0;
  1195. struct bonding *bond = to_bond(d);
  1196. if (bond->params.mode == BOND_MODE_8023AD) {
  1197. struct ad_info ad_info;
  1198. count = sprintf(buf, "%d\n", (bond_3ad_get_active_agg_info(bond, &ad_info)) ? 0 : ad_info.actor_key);
  1199. }
  1200. return count;
  1201. }
  1202. static DEVICE_ATTR(ad_actor_key, S_IRUGO, bonding_show_ad_actor_key, NULL);
  1203. /*
  1204. * Show current 802.3ad partner key.
  1205. */
  1206. static ssize_t bonding_show_ad_partner_key(struct device *d,
  1207. struct device_attribute *attr,
  1208. char *buf)
  1209. {
  1210. int count = 0;
  1211. struct bonding *bond = to_bond(d);
  1212. if (bond->params.mode == BOND_MODE_8023AD) {
  1213. struct ad_info ad_info;
  1214. count = sprintf(buf, "%d\n", (bond_3ad_get_active_agg_info(bond, &ad_info)) ? 0 : ad_info.partner_key);
  1215. }
  1216. return count;
  1217. }
  1218. static DEVICE_ATTR(ad_partner_key, S_IRUGO, bonding_show_ad_partner_key, NULL);
  1219. /*
  1220. * Show current 802.3ad partner mac.
  1221. */
  1222. static ssize_t bonding_show_ad_partner_mac(struct device *d,
  1223. struct device_attribute *attr,
  1224. char *buf)
  1225. {
  1226. int count = 0;
  1227. struct bonding *bond = to_bond(d);
  1228. if (bond->params.mode == BOND_MODE_8023AD) {
  1229. struct ad_info ad_info;
  1230. if (!bond_3ad_get_active_agg_info(bond, &ad_info)) {
  1231. count = sprintf(buf, "%pM\n", ad_info.partner_system);
  1232. }
  1233. }
  1234. return count;
  1235. }
  1236. static DEVICE_ATTR(ad_partner_mac, S_IRUGO, bonding_show_ad_partner_mac, NULL);
  1237. static struct attribute *per_bond_attrs[] = {
  1238. &dev_attr_slaves.attr,
  1239. &dev_attr_mode.attr,
  1240. &dev_attr_fail_over_mac.attr,
  1241. &dev_attr_arp_validate.attr,
  1242. &dev_attr_arp_interval.attr,
  1243. &dev_attr_arp_ip_target.attr,
  1244. &dev_attr_downdelay.attr,
  1245. &dev_attr_updelay.attr,
  1246. &dev_attr_lacp_rate.attr,
  1247. &dev_attr_xmit_hash_policy.attr,
  1248. &dev_attr_num_grat_arp.attr,
  1249. &dev_attr_miimon.attr,
  1250. &dev_attr_primary.attr,
  1251. &dev_attr_use_carrier.attr,
  1252. &dev_attr_active_slave.attr,
  1253. &dev_attr_mii_status.attr,
  1254. &dev_attr_ad_aggregator.attr,
  1255. &dev_attr_ad_num_ports.attr,
  1256. &dev_attr_ad_actor_key.attr,
  1257. &dev_attr_ad_partner_key.attr,
  1258. &dev_attr_ad_partner_mac.attr,
  1259. NULL,
  1260. };
  1261. static struct attribute_group bonding_group = {
  1262. .name = "bonding",
  1263. .attrs = per_bond_attrs,
  1264. };
  1265. /*
  1266. * Initialize sysfs. This sets up the bonding_masters file in
  1267. * /sys/class/net.
  1268. */
  1269. int bond_create_sysfs(void)
  1270. {
  1271. int ret;
  1272. ret = netdev_class_create_file(&class_attr_bonding_masters);
  1273. /*
  1274. * Permit multiple loads of the module by ignoring failures to
  1275. * create the bonding_masters sysfs file. Bonding devices
  1276. * created by second or subsequent loads of the module will
  1277. * not be listed in, or controllable by, bonding_masters, but
  1278. * will have the usual "bonding" sysfs directory.
  1279. *
  1280. * This is done to preserve backwards compatibility for
  1281. * initscripts/sysconfig, which load bonding multiple times to
  1282. * configure multiple bonding devices.
  1283. */
  1284. if (ret == -EEXIST) {
  1285. /* Is someone being kinky and naming a device bonding_master? */
  1286. if (__dev_get_by_name(&init_net,
  1287. class_attr_bonding_masters.attr.name))
  1288. printk(KERN_ERR
  1289. "network device named %s already exists in sysfs",
  1290. class_attr_bonding_masters.attr.name);
  1291. }
  1292. return ret;
  1293. }
  1294. /*
  1295. * Remove /sys/class/net/bonding_masters.
  1296. */
  1297. void bond_destroy_sysfs(void)
  1298. {
  1299. netdev_class_remove_file(&class_attr_bonding_masters);
  1300. }
  1301. /*
  1302. * Initialize sysfs for each bond. This sets up and registers
  1303. * the 'bondctl' directory for each individual bond under /sys/class/net.
  1304. */
  1305. int bond_create_sysfs_entry(struct bonding *bond)
  1306. {
  1307. struct net_device *dev = bond->dev;
  1308. int err;
  1309. err = sysfs_create_group(&(dev->dev.kobj), &bonding_group);
  1310. if (err) {
  1311. printk(KERN_EMERG "eek! didn't create group!\n");
  1312. }
  1313. if (expected_refcount < 1)
  1314. expected_refcount = atomic_read(&bond->dev->dev.kobj.kref.refcount);
  1315. return err;
  1316. }
  1317. /*
  1318. * Remove sysfs entries for each bond.
  1319. */
  1320. void bond_destroy_sysfs_entry(struct bonding *bond)
  1321. {
  1322. struct net_device *dev = bond->dev;
  1323. sysfs_remove_group(&(dev->dev.kobj), &bonding_group);
  1324. }