bond_sysfs.c 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493
  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 subsytem lock?
  52. * Because kobject_register tries to acquire the subsystem lock. If
  53. * we already hold the lock (which we would if the user was creating
  54. * a new bond through the sysfs interface), we deadlock.
  55. * This lock is only needed when deleting a bond - we need to make sure
  56. * that we don't collide with an ongoing ioctl.
  57. */
  58. struct rw_semaphore bonding_rwsem;
  59. /*------------------------------ Functions --------------------------------*/
  60. /*
  61. * "show" function for the bond_masters attribute.
  62. * The class parameter is ignored.
  63. */
  64. static ssize_t bonding_show_bonds(struct class *cls, char *buffer)
  65. {
  66. int res = 0;
  67. struct bonding *bond;
  68. down_read(&(bonding_rwsem));
  69. list_for_each_entry(bond, &bond_dev_list, bond_list) {
  70. if (res > (PAGE_SIZE - IFNAMSIZ)) {
  71. /* not enough space for another interface name */
  72. if ((PAGE_SIZE - res) > 10)
  73. res = PAGE_SIZE - 10;
  74. res += sprintf(buffer + res, "++more++");
  75. break;
  76. }
  77. res += sprintf(buffer + res, "%s ",
  78. bond->dev->name);
  79. }
  80. res += sprintf(buffer + res, "\n");
  81. res++;
  82. up_read(&(bonding_rwsem));
  83. return res;
  84. }
  85. /*
  86. * "store" function for the bond_masters attribute. This is what
  87. * creates and deletes entire bonds.
  88. *
  89. * The class parameter is ignored.
  90. *
  91. */
  92. static ssize_t bonding_store_bonds(struct class *cls, const char *buffer, size_t count)
  93. {
  94. char command[IFNAMSIZ + 1] = {0, };
  95. char *ifname;
  96. int res = count;
  97. struct bonding *bond;
  98. struct bonding *nxt;
  99. down_write(&(bonding_rwsem));
  100. sscanf(buffer, "%16s", command); /* IFNAMSIZ*/
  101. ifname = command + 1;
  102. if ((strlen(command) <= 1) ||
  103. !dev_valid_name(ifname))
  104. goto err_no_cmd;
  105. if (command[0] == '+') {
  106. /* Check to see if the bond already exists. */
  107. list_for_each_entry_safe(bond, nxt, &bond_dev_list, bond_list)
  108. if (strnicmp(bond->dev->name, ifname, IFNAMSIZ) == 0) {
  109. printk(KERN_ERR DRV_NAME
  110. ": cannot add bond %s; it already exists\n",
  111. ifname);
  112. res = -EPERM;
  113. goto out;
  114. }
  115. printk(KERN_INFO DRV_NAME
  116. ": %s is being created...\n", ifname);
  117. if (bond_create(ifname, &bonding_defaults, &bond)) {
  118. printk(KERN_INFO DRV_NAME
  119. ": %s interface already exists. Bond creation failed.\n",
  120. ifname);
  121. res = -EPERM;
  122. }
  123. goto out;
  124. }
  125. if (command[0] == '-') {
  126. list_for_each_entry_safe(bond, nxt, &bond_dev_list, bond_list)
  127. if (strnicmp(bond->dev->name, ifname, IFNAMSIZ) == 0) {
  128. rtnl_lock();
  129. /* check the ref count on the bond's kobject.
  130. * If it's > expected, then there's a file open,
  131. * and we have to fail.
  132. */
  133. if (atomic_read(&bond->dev->dev.kobj.kref.refcount)
  134. > expected_refcount){
  135. rtnl_unlock();
  136. printk(KERN_INFO DRV_NAME
  137. ": Unable remove bond %s due to open references.\n",
  138. ifname);
  139. res = -EPERM;
  140. goto out;
  141. }
  142. printk(KERN_INFO DRV_NAME
  143. ": %s is being deleted...\n",
  144. bond->dev->name);
  145. bond_deinit(bond->dev);
  146. bond_destroy_sysfs_entry(bond);
  147. unregister_netdevice(bond->dev);
  148. rtnl_unlock();
  149. goto out;
  150. }
  151. printk(KERN_ERR DRV_NAME
  152. ": unable to delete non-existent bond %s\n", ifname);
  153. res = -ENODEV;
  154. goto out;
  155. }
  156. err_no_cmd:
  157. printk(KERN_ERR DRV_NAME
  158. ": no command found in bonding_masters. Use +ifname or -ifname.\n");
  159. res = -EPERM;
  160. /* Always return either count or an error. If you return 0, you'll
  161. * get called forever, which is bad.
  162. */
  163. out:
  164. up_write(&(bonding_rwsem));
  165. return res;
  166. }
  167. /* class attribute for bond_masters file. This ends up in /sys/class/net */
  168. static CLASS_ATTR(bonding_masters, S_IWUSR | S_IRUGO,
  169. bonding_show_bonds, bonding_store_bonds);
  170. int bond_create_slave_symlinks(struct net_device *master, struct net_device *slave)
  171. {
  172. char linkname[IFNAMSIZ+7];
  173. int ret = 0;
  174. /* first, create a link from the slave back to the master */
  175. ret = sysfs_create_link(&(slave->dev.kobj), &(master->dev.kobj),
  176. "master");
  177. if (ret)
  178. return ret;
  179. /* next, create a link from the master to the slave */
  180. sprintf(linkname,"slave_%s",slave->name);
  181. ret = sysfs_create_link(&(master->dev.kobj), &(slave->dev.kobj),
  182. linkname);
  183. return ret;
  184. }
  185. void bond_destroy_slave_symlinks(struct net_device *master, struct net_device *slave)
  186. {
  187. char linkname[IFNAMSIZ+7];
  188. sysfs_remove_link(&(slave->dev.kobj), "master");
  189. sprintf(linkname,"slave_%s",slave->name);
  190. sysfs_remove_link(&(master->dev.kobj), linkname);
  191. }
  192. /*
  193. * Show the slaves in the current bond.
  194. */
  195. static ssize_t bonding_show_slaves(struct device *d,
  196. struct device_attribute *attr, char *buf)
  197. {
  198. struct slave *slave;
  199. int i, res = 0;
  200. struct bonding *bond = to_bond(d);
  201. read_lock_bh(&bond->lock);
  202. bond_for_each_slave(bond, slave, i) {
  203. if (res > (PAGE_SIZE - IFNAMSIZ)) {
  204. /* not enough space for another interface name */
  205. if ((PAGE_SIZE - res) > 10)
  206. res = PAGE_SIZE - 10;
  207. res += sprintf(buf + res, "++more++");
  208. break;
  209. }
  210. res += sprintf(buf + res, "%s ", slave->dev->name);
  211. }
  212. read_unlock_bh(&bond->lock);
  213. res += sprintf(buf + res, "\n");
  214. res++;
  215. return res;
  216. }
  217. /*
  218. * Set the slaves in the current bond. The bond interface must be
  219. * up for this to succeed.
  220. * This function is largely the same flow as bonding_update_bonds().
  221. */
  222. static ssize_t bonding_store_slaves(struct device *d,
  223. struct device_attribute *attr,
  224. const char *buffer, size_t count)
  225. {
  226. char command[IFNAMSIZ + 1] = { 0, };
  227. char *ifname;
  228. int i, res, found, ret = count;
  229. u32 original_mtu;
  230. struct slave *slave;
  231. struct net_device *dev = NULL;
  232. struct bonding *bond = to_bond(d);
  233. /* Quick sanity check -- is the bond interface up? */
  234. if (!(bond->dev->flags & IFF_UP)) {
  235. printk(KERN_WARNING DRV_NAME
  236. ": %s: doing slave updates when interface is down.\n",
  237. bond->dev->name);
  238. }
  239. /* Note: We can't hold bond->lock here, as bond_create grabs it. */
  240. sscanf(buffer, "%16s", command); /* IFNAMSIZ*/
  241. ifname = command + 1;
  242. if ((strlen(command) <= 1) ||
  243. !dev_valid_name(ifname))
  244. goto err_no_cmd;
  245. if (command[0] == '+') {
  246. /* Got a slave name in ifname. Is it already in the list? */
  247. found = 0;
  248. read_lock_bh(&bond->lock);
  249. bond_for_each_slave(bond, slave, i)
  250. if (strnicmp(slave->dev->name, ifname, IFNAMSIZ) == 0) {
  251. printk(KERN_ERR DRV_NAME
  252. ": %s: Interface %s is already enslaved!\n",
  253. bond->dev->name, ifname);
  254. ret = -EPERM;
  255. read_unlock_bh(&bond->lock);
  256. goto out;
  257. }
  258. read_unlock_bh(&bond->lock);
  259. printk(KERN_INFO DRV_NAME ": %s: Adding slave %s.\n",
  260. bond->dev->name, ifname);
  261. dev = dev_get_by_name(&init_net, ifname);
  262. if (!dev) {
  263. printk(KERN_INFO DRV_NAME
  264. ": %s: Interface %s does not exist!\n",
  265. bond->dev->name, ifname);
  266. ret = -EPERM;
  267. goto out;
  268. }
  269. else
  270. dev_put(dev);
  271. if (dev->flags & IFF_UP) {
  272. printk(KERN_ERR DRV_NAME
  273. ": %s: Error: Unable to enslave %s "
  274. "because it is already up.\n",
  275. bond->dev->name, dev->name);
  276. ret = -EPERM;
  277. goto out;
  278. }
  279. /* If this is the first slave, then we need to set
  280. the master's hardware address to be the same as the
  281. slave's. */
  282. if (!(*((u32 *) & (bond->dev->dev_addr[0])))) {
  283. memcpy(bond->dev->dev_addr, dev->dev_addr,
  284. dev->addr_len);
  285. }
  286. /* Set the slave's MTU to match the bond */
  287. original_mtu = dev->mtu;
  288. if (dev->mtu != bond->dev->mtu) {
  289. if (dev->change_mtu) {
  290. res = dev->change_mtu(dev,
  291. bond->dev->mtu);
  292. if (res) {
  293. ret = res;
  294. goto out;
  295. }
  296. } else {
  297. dev->mtu = bond->dev->mtu;
  298. }
  299. }
  300. rtnl_lock();
  301. res = bond_enslave(bond->dev, dev);
  302. bond_for_each_slave(bond, slave, i)
  303. if (strnicmp(slave->dev->name, ifname, IFNAMSIZ) == 0)
  304. slave->original_mtu = original_mtu;
  305. rtnl_unlock();
  306. if (res) {
  307. ret = res;
  308. }
  309. goto out;
  310. }
  311. if (command[0] == '-') {
  312. dev = NULL;
  313. bond_for_each_slave(bond, slave, i)
  314. if (strnicmp(slave->dev->name, ifname, IFNAMSIZ) == 0) {
  315. dev = slave->dev;
  316. original_mtu = slave->original_mtu;
  317. break;
  318. }
  319. if (dev) {
  320. printk(KERN_INFO DRV_NAME ": %s: Removing slave %s\n",
  321. bond->dev->name, dev->name);
  322. rtnl_lock();
  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) + 1;
  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. int count;
  406. struct bonding *bond = to_bond(d);
  407. if ((bond->params.mode != BOND_MODE_XOR) &&
  408. (bond->params.mode != BOND_MODE_8023AD)) {
  409. // Not Applicable
  410. count = sprintf(buf, "NA\n") + 1;
  411. } else {
  412. count = sprintf(buf, "%s %d\n",
  413. xmit_hashtype_tbl[bond->params.xmit_policy].modename,
  414. bond->params.xmit_policy) + 1;
  415. }
  416. return count;
  417. }
  418. static ssize_t bonding_store_xmit_hash(struct device *d,
  419. struct device_attribute *attr,
  420. const char *buf, size_t count)
  421. {
  422. int new_value, ret = count;
  423. struct bonding *bond = to_bond(d);
  424. if (bond->dev->flags & IFF_UP) {
  425. printk(KERN_ERR DRV_NAME
  426. "%s: Interface is up. Unable to update xmit policy.\n",
  427. bond->dev->name);
  428. ret = -EPERM;
  429. goto out;
  430. }
  431. if ((bond->params.mode != BOND_MODE_XOR) &&
  432. (bond->params.mode != BOND_MODE_8023AD)) {
  433. printk(KERN_ERR DRV_NAME
  434. "%s: Transmit hash policy is irrelevant in this mode.\n",
  435. bond->dev->name);
  436. ret = -EPERM;
  437. goto out;
  438. }
  439. new_value = bond_parse_parm((char *)buf, xmit_hashtype_tbl);
  440. if (new_value < 0) {
  441. printk(KERN_ERR DRV_NAME
  442. ": %s: Ignoring invalid xmit hash policy value %.*s.\n",
  443. bond->dev->name,
  444. (int)strlen(buf) - 1, buf);
  445. ret = -EINVAL;
  446. goto out;
  447. } else {
  448. bond->params.xmit_policy = new_value;
  449. bond_set_mode_ops(bond, bond->params.mode);
  450. printk(KERN_INFO DRV_NAME ": %s: setting xmit hash policy to %s (%d).\n",
  451. bond->dev->name, xmit_hashtype_tbl[new_value].modename, new_value);
  452. }
  453. out:
  454. return ret;
  455. }
  456. static DEVICE_ATTR(xmit_hash_policy, S_IRUGO | S_IWUSR, bonding_show_xmit_hash, bonding_store_xmit_hash);
  457. /*
  458. * Show and set arp_validate.
  459. */
  460. static ssize_t bonding_show_arp_validate(struct device *d,
  461. struct device_attribute *attr,
  462. char *buf)
  463. {
  464. struct bonding *bond = to_bond(d);
  465. return sprintf(buf, "%s %d\n",
  466. arp_validate_tbl[bond->params.arp_validate].modename,
  467. bond->params.arp_validate) + 1;
  468. }
  469. static ssize_t bonding_store_arp_validate(struct device *d,
  470. struct device_attribute *attr,
  471. const char *buf, size_t count)
  472. {
  473. int new_value;
  474. struct bonding *bond = to_bond(d);
  475. new_value = bond_parse_parm((char *)buf, arp_validate_tbl);
  476. if (new_value < 0) {
  477. printk(KERN_ERR DRV_NAME
  478. ": %s: Ignoring invalid arp_validate value %s\n",
  479. bond->dev->name, buf);
  480. return -EINVAL;
  481. }
  482. if (new_value && (bond->params.mode != BOND_MODE_ACTIVEBACKUP)) {
  483. printk(KERN_ERR DRV_NAME
  484. ": %s: arp_validate only supported in active-backup mode.\n",
  485. bond->dev->name);
  486. return -EINVAL;
  487. }
  488. printk(KERN_INFO DRV_NAME ": %s: setting arp_validate to %s (%d).\n",
  489. bond->dev->name, arp_validate_tbl[new_value].modename,
  490. new_value);
  491. if (!bond->params.arp_validate && new_value) {
  492. bond_register_arp(bond);
  493. } else if (bond->params.arp_validate && !new_value) {
  494. bond_unregister_arp(bond);
  495. }
  496. bond->params.arp_validate = new_value;
  497. return count;
  498. }
  499. static DEVICE_ATTR(arp_validate, S_IRUGO | S_IWUSR, bonding_show_arp_validate, bonding_store_arp_validate);
  500. /*
  501. * Show and set the arp timer interval. There are two tricky bits
  502. * here. First, if ARP monitoring is activated, then we must disable
  503. * MII monitoring. Second, if the ARP timer isn't running, we must
  504. * start it.
  505. */
  506. static ssize_t bonding_show_arp_interval(struct device *d,
  507. struct device_attribute *attr,
  508. char *buf)
  509. {
  510. struct bonding *bond = to_bond(d);
  511. return sprintf(buf, "%d\n", bond->params.arp_interval) + 1;
  512. }
  513. static ssize_t bonding_store_arp_interval(struct device *d,
  514. struct device_attribute *attr,
  515. const char *buf, size_t count)
  516. {
  517. int new_value, ret = count;
  518. struct bonding *bond = to_bond(d);
  519. if (sscanf(buf, "%d", &new_value) != 1) {
  520. printk(KERN_ERR DRV_NAME
  521. ": %s: no arp_interval value specified.\n",
  522. bond->dev->name);
  523. ret = -EINVAL;
  524. goto out;
  525. }
  526. if (new_value < 0) {
  527. printk(KERN_ERR DRV_NAME
  528. ": %s: Invalid arp_interval value %d not in range 1-%d; rejected.\n",
  529. bond->dev->name, new_value, INT_MAX);
  530. ret = -EINVAL;
  531. goto out;
  532. }
  533. printk(KERN_INFO DRV_NAME
  534. ": %s: Setting ARP monitoring interval to %d.\n",
  535. bond->dev->name, new_value);
  536. bond->params.arp_interval = new_value;
  537. if (bond->params.miimon) {
  538. printk(KERN_INFO DRV_NAME
  539. ": %s: ARP monitoring cannot be used with MII monitoring. "
  540. "%s Disabling MII monitoring.\n",
  541. bond->dev->name, bond->dev->name);
  542. bond->params.miimon = 0;
  543. /* Kill MII timer, else it brings bond's link down */
  544. if (bond->arp_timer.function) {
  545. printk(KERN_INFO DRV_NAME
  546. ": %s: Kill MII timer, else it brings bond's link down...\n",
  547. bond->dev->name);
  548. del_timer_sync(&bond->mii_timer);
  549. }
  550. }
  551. if (!bond->params.arp_targets[0]) {
  552. printk(KERN_INFO DRV_NAME
  553. ": %s: ARP monitoring has been set up, "
  554. "but no ARP targets have been specified.\n",
  555. bond->dev->name);
  556. }
  557. if (bond->dev->flags & IFF_UP) {
  558. /* If the interface is up, we may need to fire off
  559. * the ARP timer. If the interface is down, the
  560. * timer will get fired off when the open function
  561. * is called.
  562. */
  563. if (bond->arp_timer.function) {
  564. /* The timer's already set up, so fire it off */
  565. mod_timer(&bond->arp_timer, jiffies + 1);
  566. } else {
  567. /* Set up the timer. */
  568. init_timer(&bond->arp_timer);
  569. bond->arp_timer.expires = jiffies + 1;
  570. bond->arp_timer.data =
  571. (unsigned long) bond->dev;
  572. if (bond->params.mode == BOND_MODE_ACTIVEBACKUP) {
  573. bond->arp_timer.function =
  574. (void *)
  575. &bond_activebackup_arp_mon;
  576. } else {
  577. bond->arp_timer.function =
  578. (void *)
  579. &bond_loadbalance_arp_mon;
  580. }
  581. add_timer(&bond->arp_timer);
  582. }
  583. }
  584. out:
  585. return ret;
  586. }
  587. static DEVICE_ATTR(arp_interval, S_IRUGO | S_IWUSR , bonding_show_arp_interval, bonding_store_arp_interval);
  588. /*
  589. * Show and set the arp targets.
  590. */
  591. static ssize_t bonding_show_arp_targets(struct device *d,
  592. struct device_attribute *attr,
  593. char *buf)
  594. {
  595. int i, res = 0;
  596. struct bonding *bond = to_bond(d);
  597. for (i = 0; i < BOND_MAX_ARP_TARGETS; i++) {
  598. if (bond->params.arp_targets[i])
  599. res += sprintf(buf + res, "%u.%u.%u.%u ",
  600. NIPQUAD(bond->params.arp_targets[i]));
  601. }
  602. if (res)
  603. res--; /* eat the leftover space */
  604. res += sprintf(buf + res, "\n");
  605. res++;
  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) + 1;
  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) + 1;
  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) + 1;
  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((char *)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) + 1;
  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. /* Kill ARP timer, else it brings bond's link down */
  904. if (bond->mii_timer.function) {
  905. printk(KERN_INFO DRV_NAME
  906. ": %s: Kill ARP timer, else it brings bond's link down...\n",
  907. bond->dev->name);
  908. del_timer_sync(&bond->arp_timer);
  909. }
  910. }
  911. if (bond->dev->flags & IFF_UP) {
  912. /* If the interface is up, we may need to fire off
  913. * the MII timer. If the interface is down, the
  914. * timer will get fired off when the open function
  915. * is called.
  916. */
  917. if (bond->mii_timer.function) {
  918. /* The timer's already set up, so fire it off */
  919. mod_timer(&bond->mii_timer, jiffies + 1);
  920. } else {
  921. /* Set up the timer. */
  922. init_timer(&bond->mii_timer);
  923. bond->mii_timer.expires = jiffies + 1;
  924. bond->mii_timer.data =
  925. (unsigned long) bond->dev;
  926. bond->mii_timer.function =
  927. (void *) &bond_mii_monitor;
  928. add_timer(&bond->mii_timer);
  929. }
  930. }
  931. }
  932. out:
  933. return ret;
  934. }
  935. static DEVICE_ATTR(miimon, S_IRUGO | S_IWUSR, bonding_show_miimon, bonding_store_miimon);
  936. /*
  937. * Show and set the primary slave. The store function is much
  938. * simpler than bonding_store_slaves function because it only needs to
  939. * handle one interface name.
  940. * The bond must be a mode that supports a primary for this be
  941. * set.
  942. */
  943. static ssize_t bonding_show_primary(struct device *d,
  944. struct device_attribute *attr,
  945. char *buf)
  946. {
  947. int count = 0;
  948. struct bonding *bond = to_bond(d);
  949. if (bond->primary_slave)
  950. count = sprintf(buf, "%s\n", bond->primary_slave->dev->name) + 1;
  951. else
  952. count = sprintf(buf, "\n") + 1;
  953. return count;
  954. }
  955. static ssize_t bonding_store_primary(struct device *d,
  956. struct device_attribute *attr,
  957. const char *buf, size_t count)
  958. {
  959. int i;
  960. struct slave *slave;
  961. struct bonding *bond = to_bond(d);
  962. write_lock_bh(&bond->lock);
  963. if (!USES_PRIMARY(bond->params.mode)) {
  964. printk(KERN_INFO DRV_NAME
  965. ": %s: Unable to set primary slave; %s is in mode %d\n",
  966. bond->dev->name, bond->dev->name, bond->params.mode);
  967. } else {
  968. bond_for_each_slave(bond, slave, i) {
  969. if (strnicmp
  970. (slave->dev->name, buf,
  971. strlen(slave->dev->name)) == 0) {
  972. printk(KERN_INFO DRV_NAME
  973. ": %s: Setting %s as primary slave.\n",
  974. bond->dev->name, slave->dev->name);
  975. bond->primary_slave = slave;
  976. bond_select_active_slave(bond);
  977. goto out;
  978. }
  979. }
  980. /* if we got here, then we didn't match the name of any slave */
  981. if (strlen(buf) == 0 || buf[0] == '\n') {
  982. printk(KERN_INFO DRV_NAME
  983. ": %s: Setting primary slave to None.\n",
  984. bond->dev->name);
  985. bond->primary_slave = NULL;
  986. bond_select_active_slave(bond);
  987. } else {
  988. printk(KERN_INFO DRV_NAME
  989. ": %s: Unable to set %.*s as primary slave as it is not a slave.\n",
  990. bond->dev->name, (int)strlen(buf) - 1, buf);
  991. }
  992. }
  993. out:
  994. write_unlock_bh(&bond->lock);
  995. return count;
  996. }
  997. static DEVICE_ATTR(primary, S_IRUGO | S_IWUSR, bonding_show_primary, bonding_store_primary);
  998. /*
  999. * Show and set the use_carrier flag.
  1000. */
  1001. static ssize_t bonding_show_carrier(struct device *d,
  1002. struct device_attribute *attr,
  1003. char *buf)
  1004. {
  1005. struct bonding *bond = to_bond(d);
  1006. return sprintf(buf, "%d\n", bond->params.use_carrier) + 1;
  1007. }
  1008. static ssize_t bonding_store_carrier(struct device *d,
  1009. struct device_attribute *attr,
  1010. const char *buf, size_t count)
  1011. {
  1012. int new_value, ret = count;
  1013. struct bonding *bond = to_bond(d);
  1014. if (sscanf(buf, "%d", &new_value) != 1) {
  1015. printk(KERN_ERR DRV_NAME
  1016. ": %s: no use_carrier value specified.\n",
  1017. bond->dev->name);
  1018. ret = -EINVAL;
  1019. goto out;
  1020. }
  1021. if ((new_value == 0) || (new_value == 1)) {
  1022. bond->params.use_carrier = new_value;
  1023. printk(KERN_INFO DRV_NAME ": %s: Setting use_carrier to %d.\n",
  1024. bond->dev->name, new_value);
  1025. } else {
  1026. printk(KERN_INFO DRV_NAME
  1027. ": %s: Ignoring invalid use_carrier value %d.\n",
  1028. bond->dev->name, new_value);
  1029. }
  1030. out:
  1031. return count;
  1032. }
  1033. static DEVICE_ATTR(use_carrier, S_IRUGO | S_IWUSR, bonding_show_carrier, bonding_store_carrier);
  1034. /*
  1035. * Show and set currently active_slave.
  1036. */
  1037. static ssize_t bonding_show_active_slave(struct device *d,
  1038. struct device_attribute *attr,
  1039. char *buf)
  1040. {
  1041. struct slave *curr;
  1042. struct bonding *bond = to_bond(d);
  1043. int count;
  1044. read_lock(&bond->curr_slave_lock);
  1045. curr = bond->curr_active_slave;
  1046. read_unlock(&bond->curr_slave_lock);
  1047. if (USES_PRIMARY(bond->params.mode) && curr)
  1048. count = sprintf(buf, "%s\n", curr->dev->name) + 1;
  1049. else
  1050. count = sprintf(buf, "\n") + 1;
  1051. return count;
  1052. }
  1053. static ssize_t bonding_store_active_slave(struct device *d,
  1054. struct device_attribute *attr,
  1055. const char *buf, size_t count)
  1056. {
  1057. int i;
  1058. struct slave *slave;
  1059. struct slave *old_active = NULL;
  1060. struct slave *new_active = NULL;
  1061. struct bonding *bond = to_bond(d);
  1062. write_lock_bh(&bond->lock);
  1063. if (!USES_PRIMARY(bond->params.mode)) {
  1064. printk(KERN_INFO DRV_NAME
  1065. ": %s: Unable to change active slave; %s is in mode %d\n",
  1066. bond->dev->name, bond->dev->name, bond->params.mode);
  1067. } else {
  1068. bond_for_each_slave(bond, slave, i) {
  1069. if (strnicmp
  1070. (slave->dev->name, buf,
  1071. strlen(slave->dev->name)) == 0) {
  1072. old_active = bond->curr_active_slave;
  1073. new_active = slave;
  1074. if (new_active == old_active) {
  1075. /* do nothing */
  1076. printk(KERN_INFO DRV_NAME
  1077. ": %s: %s is already the current active slave.\n",
  1078. bond->dev->name, slave->dev->name);
  1079. goto out;
  1080. }
  1081. else {
  1082. if ((new_active) &&
  1083. (old_active) &&
  1084. (new_active->link == BOND_LINK_UP) &&
  1085. IS_UP(new_active->dev)) {
  1086. printk(KERN_INFO DRV_NAME
  1087. ": %s: Setting %s as active slave.\n",
  1088. bond->dev->name, slave->dev->name);
  1089. bond_change_active_slave(bond, new_active);
  1090. }
  1091. else {
  1092. printk(KERN_INFO DRV_NAME
  1093. ": %s: Could not set %s as active slave; "
  1094. "either %s is down or the link is down.\n",
  1095. bond->dev->name, slave->dev->name,
  1096. slave->dev->name);
  1097. }
  1098. goto out;
  1099. }
  1100. }
  1101. }
  1102. /* if we got here, then we didn't match the name of any slave */
  1103. if (strlen(buf) == 0 || buf[0] == '\n') {
  1104. printk(KERN_INFO DRV_NAME
  1105. ": %s: Setting active slave to None.\n",
  1106. bond->dev->name);
  1107. bond->primary_slave = NULL;
  1108. bond_select_active_slave(bond);
  1109. } else {
  1110. printk(KERN_INFO DRV_NAME
  1111. ": %s: Unable to set %.*s as active slave as it is not a slave.\n",
  1112. bond->dev->name, (int)strlen(buf) - 1, buf);
  1113. }
  1114. }
  1115. out:
  1116. write_unlock_bh(&bond->lock);
  1117. return count;
  1118. }
  1119. static DEVICE_ATTR(active_slave, S_IRUGO | S_IWUSR, bonding_show_active_slave, bonding_store_active_slave);
  1120. /*
  1121. * Show link status of the bond interface.
  1122. */
  1123. static ssize_t bonding_show_mii_status(struct device *d,
  1124. struct device_attribute *attr,
  1125. char *buf)
  1126. {
  1127. struct slave *curr;
  1128. struct bonding *bond = to_bond(d);
  1129. read_lock(&bond->curr_slave_lock);
  1130. curr = bond->curr_active_slave;
  1131. read_unlock(&bond->curr_slave_lock);
  1132. return sprintf(buf, "%s\n", (curr) ? "up" : "down") + 1;
  1133. }
  1134. static DEVICE_ATTR(mii_status, S_IRUGO, bonding_show_mii_status, NULL);
  1135. /*
  1136. * Show current 802.3ad aggregator ID.
  1137. */
  1138. static ssize_t bonding_show_ad_aggregator(struct device *d,
  1139. struct device_attribute *attr,
  1140. char *buf)
  1141. {
  1142. int count = 0;
  1143. struct bonding *bond = to_bond(d);
  1144. if (bond->params.mode == BOND_MODE_8023AD) {
  1145. struct ad_info ad_info;
  1146. count = sprintf(buf, "%d\n", (bond_3ad_get_active_agg_info(bond, &ad_info)) ? 0 : ad_info.aggregator_id) + 1;
  1147. }
  1148. else
  1149. count = sprintf(buf, "\n") + 1;
  1150. return count;
  1151. }
  1152. static DEVICE_ATTR(ad_aggregator, S_IRUGO, bonding_show_ad_aggregator, NULL);
  1153. /*
  1154. * Show number of active 802.3ad ports.
  1155. */
  1156. static ssize_t bonding_show_ad_num_ports(struct device *d,
  1157. struct device_attribute *attr,
  1158. char *buf)
  1159. {
  1160. int count = 0;
  1161. struct bonding *bond = to_bond(d);
  1162. if (bond->params.mode == BOND_MODE_8023AD) {
  1163. struct ad_info ad_info;
  1164. count = sprintf(buf, "%d\n", (bond_3ad_get_active_agg_info(bond, &ad_info)) ? 0: ad_info.ports) + 1;
  1165. }
  1166. else
  1167. count = sprintf(buf, "\n") + 1;
  1168. return count;
  1169. }
  1170. static DEVICE_ATTR(ad_num_ports, S_IRUGO, bonding_show_ad_num_ports, NULL);
  1171. /*
  1172. * Show current 802.3ad actor key.
  1173. */
  1174. static ssize_t bonding_show_ad_actor_key(struct device *d,
  1175. struct device_attribute *attr,
  1176. char *buf)
  1177. {
  1178. int count = 0;
  1179. struct bonding *bond = to_bond(d);
  1180. if (bond->params.mode == BOND_MODE_8023AD) {
  1181. struct ad_info ad_info;
  1182. count = sprintf(buf, "%d\n", (bond_3ad_get_active_agg_info(bond, &ad_info)) ? 0 : ad_info.actor_key) + 1;
  1183. }
  1184. else
  1185. count = sprintf(buf, "\n") + 1;
  1186. return count;
  1187. }
  1188. static DEVICE_ATTR(ad_actor_key, S_IRUGO, bonding_show_ad_actor_key, NULL);
  1189. /*
  1190. * Show current 802.3ad partner key.
  1191. */
  1192. static ssize_t bonding_show_ad_partner_key(struct device *d,
  1193. struct device_attribute *attr,
  1194. char *buf)
  1195. {
  1196. int count = 0;
  1197. struct bonding *bond = to_bond(d);
  1198. if (bond->params.mode == BOND_MODE_8023AD) {
  1199. struct ad_info ad_info;
  1200. count = sprintf(buf, "%d\n", (bond_3ad_get_active_agg_info(bond, &ad_info)) ? 0 : ad_info.partner_key) + 1;
  1201. }
  1202. else
  1203. count = sprintf(buf, "\n") + 1;
  1204. return count;
  1205. }
  1206. static DEVICE_ATTR(ad_partner_key, S_IRUGO, bonding_show_ad_partner_key, NULL);
  1207. /*
  1208. * Show current 802.3ad partner mac.
  1209. */
  1210. static ssize_t bonding_show_ad_partner_mac(struct device *d,
  1211. struct device_attribute *attr,
  1212. char *buf)
  1213. {
  1214. int count = 0;
  1215. struct bonding *bond = to_bond(d);
  1216. DECLARE_MAC_BUF(mac);
  1217. if (bond->params.mode == BOND_MODE_8023AD) {
  1218. struct ad_info ad_info;
  1219. if (!bond_3ad_get_active_agg_info(bond, &ad_info)) {
  1220. count = sprintf(buf,"%s\n",
  1221. print_mac(mac, ad_info.partner_system))
  1222. + 1;
  1223. }
  1224. }
  1225. else
  1226. count = sprintf(buf, "\n") + 1;
  1227. return count;
  1228. }
  1229. static DEVICE_ATTR(ad_partner_mac, S_IRUGO, bonding_show_ad_partner_mac, NULL);
  1230. static struct attribute *per_bond_attrs[] = {
  1231. &dev_attr_slaves.attr,
  1232. &dev_attr_mode.attr,
  1233. &dev_attr_arp_validate.attr,
  1234. &dev_attr_arp_interval.attr,
  1235. &dev_attr_arp_ip_target.attr,
  1236. &dev_attr_downdelay.attr,
  1237. &dev_attr_updelay.attr,
  1238. &dev_attr_lacp_rate.attr,
  1239. &dev_attr_xmit_hash_policy.attr,
  1240. &dev_attr_miimon.attr,
  1241. &dev_attr_primary.attr,
  1242. &dev_attr_use_carrier.attr,
  1243. &dev_attr_active_slave.attr,
  1244. &dev_attr_mii_status.attr,
  1245. &dev_attr_ad_aggregator.attr,
  1246. &dev_attr_ad_num_ports.attr,
  1247. &dev_attr_ad_actor_key.attr,
  1248. &dev_attr_ad_partner_key.attr,
  1249. &dev_attr_ad_partner_mac.attr,
  1250. NULL,
  1251. };
  1252. static struct attribute_group bonding_group = {
  1253. .name = "bonding",
  1254. .attrs = per_bond_attrs,
  1255. };
  1256. /*
  1257. * Initialize sysfs. This sets up the bonding_masters file in
  1258. * /sys/class/net.
  1259. */
  1260. int bond_create_sysfs(void)
  1261. {
  1262. int ret = 0;
  1263. struct bonding *firstbond;
  1264. init_rwsem(&bonding_rwsem);
  1265. /* get the netdev class pointer */
  1266. firstbond = container_of(bond_dev_list.next, struct bonding, bond_list);
  1267. if (!firstbond)
  1268. return -ENODEV;
  1269. netdev_class = firstbond->dev->dev.class;
  1270. if (!netdev_class)
  1271. return -ENODEV;
  1272. ret = class_create_file(netdev_class, &class_attr_bonding_masters);
  1273. /*
  1274. * Permit multiple loads of the module by ignoring failures to
  1275. * create the bonding_masters sysfs file. Bonding devices
  1276. * created by second or subsequent loads of the module will
  1277. * not be listed in, or controllable by, bonding_masters, but
  1278. * will have the usual "bonding" sysfs directory.
  1279. *
  1280. * This is done to preserve backwards compatibility for
  1281. * initscripts/sysconfig, which load bonding multiple times to
  1282. * configure multiple bonding devices.
  1283. */
  1284. if (ret == -EEXIST) {
  1285. netdev_class = NULL;
  1286. return 0;
  1287. }
  1288. return ret;
  1289. }
  1290. /*
  1291. * Remove /sys/class/net/bonding_masters.
  1292. */
  1293. void bond_destroy_sysfs(void)
  1294. {
  1295. if (netdev_class)
  1296. class_remove_file(netdev_class, &class_attr_bonding_masters);
  1297. }
  1298. /*
  1299. * Initialize sysfs for each bond. This sets up and registers
  1300. * the 'bondctl' directory for each individual bond under /sys/class/net.
  1301. */
  1302. int bond_create_sysfs_entry(struct bonding *bond)
  1303. {
  1304. struct net_device *dev = bond->dev;
  1305. int err;
  1306. err = sysfs_create_group(&(dev->dev.kobj), &bonding_group);
  1307. if (err) {
  1308. printk(KERN_EMERG "eek! didn't create group!\n");
  1309. }
  1310. if (expected_refcount < 1)
  1311. expected_refcount = atomic_read(&bond->dev->dev.kobj.kref.refcount);
  1312. return err;
  1313. }
  1314. /*
  1315. * Remove sysfs entries for each bond.
  1316. */
  1317. void bond_destroy_sysfs_entry(struct bonding *bond)
  1318. {
  1319. struct net_device *dev = bond->dev;
  1320. sysfs_remove_group(&(dev->dev.kobj), &bonding_group);
  1321. }