bond_sysfs.c 41 KB

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