bond_sysfs.c 46 KB

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