bond_sysfs.c 40 KB

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