bond_sysfs.c 44 KB

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