bond_sysfs.c 40 KB

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