bond_sysfs.c 43 KB

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