bond_sysfs.c 44 KB

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