bond_sysfs.c 43 KB

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