bond_sysfs.c 46 KB

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