bond_sysfs.c 47 KB

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