bond_sysfs.c 41 KB

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