bond_sysfs.c 41 KB

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