bond_sysfs.c 40 KB

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