bond_sysfs.c 41 KB

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