bond_sysfs.c 42 KB

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