bond_sysfs.c 47 KB

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