bond_sysfs.c 41 KB

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