bond_sysfs.c 45 KB

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