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