bond_sysfs.c 41 KB

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