bond_sysfs.c 43 KB

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