bond_sysfs.c 40 KB

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