bond_sysfs.c 37 KB

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