bond_sysfs.c 40 KB

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