bond_sysfs.c 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721
  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/sysdev.h>
  28. #include <linux/fs.h>
  29. #include <linux/types.h>
  30. #include <linux/string.h>
  31. #include <linux/netdevice.h>
  32. #include <linux/inetdevice.h>
  33. #include <linux/in.h>
  34. #include <linux/sysfs.h>
  35. #include <linux/ctype.h>
  36. #include <linux/inet.h>
  37. #include <linux/rtnetlink.h>
  38. #include <linux/etherdevice.h>
  39. #include <net/net_namespace.h>
  40. #include <net/netns/generic.h>
  41. #include <linux/nsproxy.h>
  42. #include "bonding.h"
  43. #define to_dev(obj) container_of(obj, struct device, kobj)
  44. #define to_bond(cd) ((struct bonding *)(netdev_priv(to_net_dev(cd))))
  45. /*
  46. * "show" function for the bond_masters attribute.
  47. * The class parameter is ignored.
  48. */
  49. static ssize_t bonding_show_bonds(struct class *cls,
  50. struct class_attribute *attr,
  51. char *buf)
  52. {
  53. struct bond_net *bn =
  54. container_of(attr, struct bond_net, class_attr_bonding_masters);
  55. int res = 0;
  56. struct bonding *bond;
  57. rtnl_lock();
  58. list_for_each_entry(bond, &bn->dev_list, bond_list) {
  59. if (res > (PAGE_SIZE - IFNAMSIZ)) {
  60. /* not enough space for another interface name */
  61. if ((PAGE_SIZE - res) > 10)
  62. res = PAGE_SIZE - 10;
  63. res += sprintf(buf + res, "++more++ ");
  64. break;
  65. }
  66. res += sprintf(buf + res, "%s ", bond->dev->name);
  67. }
  68. if (res)
  69. buf[res-1] = '\n'; /* eat the leftover space */
  70. rtnl_unlock();
  71. return res;
  72. }
  73. static struct net_device *bond_get_by_name(struct bond_net *bn, const char *ifname)
  74. {
  75. struct bonding *bond;
  76. list_for_each_entry(bond, &bn->dev_list, bond_list) {
  77. if (strncmp(bond->dev->name, ifname, IFNAMSIZ) == 0)
  78. return bond->dev;
  79. }
  80. return NULL;
  81. }
  82. /*
  83. * "store" function for the bond_masters attribute. This is what
  84. * creates and deletes entire bonds.
  85. *
  86. * The class parameter is ignored.
  87. *
  88. */
  89. static ssize_t bonding_store_bonds(struct class *cls,
  90. struct class_attribute *attr,
  91. const char *buffer, size_t count)
  92. {
  93. struct bond_net *bn =
  94. container_of(attr, struct bond_net, class_attr_bonding_masters);
  95. char command[IFNAMSIZ + 1] = {0, };
  96. char *ifname;
  97. int rv, res = count;
  98. sscanf(buffer, "%16s", command); /* IFNAMSIZ*/
  99. ifname = command + 1;
  100. if ((strlen(command) <= 1) ||
  101. !dev_valid_name(ifname))
  102. goto err_no_cmd;
  103. if (command[0] == '+') {
  104. pr_info("%s is being created...\n", ifname);
  105. rv = bond_create(bn->net, ifname);
  106. if (rv) {
  107. if (rv == -EEXIST)
  108. pr_info("%s already exists.\n", ifname);
  109. else
  110. pr_info("%s creation failed.\n", ifname);
  111. res = rv;
  112. }
  113. } else if (command[0] == '-') {
  114. struct net_device *bond_dev;
  115. rtnl_lock();
  116. bond_dev = bond_get_by_name(bn, ifname);
  117. if (bond_dev) {
  118. pr_info("%s is being deleted...\n", ifname);
  119. unregister_netdevice(bond_dev);
  120. } else {
  121. pr_err("unable to delete non-existent %s\n", ifname);
  122. res = -ENODEV;
  123. }
  124. rtnl_unlock();
  125. } else
  126. goto err_no_cmd;
  127. /* Always return either count or an error. If you return 0, you'll
  128. * get called forever, which is bad.
  129. */
  130. return res;
  131. err_no_cmd:
  132. pr_err("no command found in bonding_masters. Use +ifname or -ifname.\n");
  133. return -EPERM;
  134. }
  135. static const void *bonding_namespace(struct class *cls,
  136. const struct class_attribute *attr)
  137. {
  138. const struct bond_net *bn =
  139. container_of(attr, struct bond_net, class_attr_bonding_masters);
  140. return bn->net;
  141. }
  142. /* class attribute for bond_masters file. This ends up in /sys/class/net */
  143. static const struct class_attribute class_attr_bonding_masters = {
  144. .attr = {
  145. .name = "bonding_masters",
  146. .mode = S_IWUSR | S_IRUGO,
  147. },
  148. .show = bonding_show_bonds,
  149. .store = bonding_store_bonds,
  150. .namespace = bonding_namespace,
  151. };
  152. int bond_create_slave_symlinks(struct net_device *master,
  153. struct net_device *slave)
  154. {
  155. char linkname[IFNAMSIZ+7];
  156. int ret = 0;
  157. /* first, create a link from the slave back to the master */
  158. ret = sysfs_create_link(&(slave->dev.kobj), &(master->dev.kobj),
  159. "master");
  160. if (ret)
  161. return ret;
  162. /* next, create a link from the master to the slave */
  163. sprintf(linkname, "slave_%s", slave->name);
  164. ret = sysfs_create_link(&(master->dev.kobj), &(slave->dev.kobj),
  165. linkname);
  166. return ret;
  167. }
  168. void bond_destroy_slave_symlinks(struct net_device *master,
  169. struct net_device *slave)
  170. {
  171. char linkname[IFNAMSIZ+7];
  172. sysfs_remove_link(&(slave->dev.kobj), "master");
  173. sprintf(linkname, "slave_%s", slave->name);
  174. sysfs_remove_link(&(master->dev.kobj), linkname);
  175. }
  176. /*
  177. * Show the slaves in the current bond.
  178. */
  179. static ssize_t bonding_show_slaves(struct device *d,
  180. struct device_attribute *attr, char *buf)
  181. {
  182. struct slave *slave;
  183. int i, res = 0;
  184. struct bonding *bond = to_bond(d);
  185. read_lock(&bond->lock);
  186. bond_for_each_slave(bond, slave, i) {
  187. if (res > (PAGE_SIZE - IFNAMSIZ)) {
  188. /* not enough space for another interface name */
  189. if ((PAGE_SIZE - res) > 10)
  190. res = PAGE_SIZE - 10;
  191. res += sprintf(buf + res, "++more++ ");
  192. break;
  193. }
  194. res += sprintf(buf + res, "%s ", slave->dev->name);
  195. }
  196. read_unlock(&bond->lock);
  197. if (res)
  198. buf[res-1] = '\n'; /* eat the leftover space */
  199. return res;
  200. }
  201. /*
  202. * Set the slaves in the current bond. The bond interface must be
  203. * up for this to succeed.
  204. * This is supposed to be only thin wrapper for bond_enslave and bond_release.
  205. * All hard work should be done there.
  206. */
  207. static ssize_t bonding_store_slaves(struct device *d,
  208. struct device_attribute *attr,
  209. const char *buffer, size_t count)
  210. {
  211. char command[IFNAMSIZ + 1] = { 0, };
  212. char *ifname;
  213. int res, ret = count;
  214. struct net_device *dev;
  215. struct bonding *bond = to_bond(d);
  216. if (!rtnl_trylock())
  217. return restart_syscall();
  218. sscanf(buffer, "%16s", command); /* IFNAMSIZ*/
  219. ifname = command + 1;
  220. if ((strlen(command) <= 1) ||
  221. !dev_valid_name(ifname))
  222. goto err_no_cmd;
  223. dev = __dev_get_by_name(dev_net(bond->dev), ifname);
  224. if (!dev) {
  225. pr_info("%s: Interface %s does not exist!\n",
  226. bond->dev->name, ifname);
  227. ret = -ENODEV;
  228. goto out;
  229. }
  230. switch (command[0]) {
  231. case '+':
  232. pr_info("%s: Adding slave %s.\n", bond->dev->name, dev->name);
  233. res = bond_enslave(bond->dev, dev);
  234. break;
  235. case '-':
  236. pr_info("%s: Removing slave %s.\n", bond->dev->name, dev->name);
  237. res = bond_release(bond->dev, dev);
  238. break;
  239. default:
  240. goto err_no_cmd;
  241. }
  242. if (res)
  243. ret = res;
  244. goto out;
  245. err_no_cmd:
  246. pr_err("no command found in slaves file for bond %s. Use +ifname or -ifname.\n",
  247. bond->dev->name);
  248. ret = -EPERM;
  249. out:
  250. rtnl_unlock();
  251. return ret;
  252. }
  253. static DEVICE_ATTR(slaves, S_IRUGO | S_IWUSR, bonding_show_slaves,
  254. bonding_store_slaves);
  255. /*
  256. * Show and set the bonding mode. The bond interface must be down to
  257. * change the mode.
  258. */
  259. static ssize_t bonding_show_mode(struct device *d,
  260. struct device_attribute *attr, char *buf)
  261. {
  262. struct bonding *bond = to_bond(d);
  263. return sprintf(buf, "%s %d\n",
  264. bond_mode_tbl[bond->params.mode].modename,
  265. bond->params.mode);
  266. }
  267. static ssize_t bonding_store_mode(struct device *d,
  268. struct device_attribute *attr,
  269. const char *buf, size_t count)
  270. {
  271. int new_value, ret = count;
  272. struct bonding *bond = to_bond(d);
  273. if (bond->dev->flags & IFF_UP) {
  274. pr_err("unable to update mode of %s because interface is up.\n",
  275. bond->dev->name);
  276. ret = -EPERM;
  277. goto out;
  278. }
  279. new_value = bond_parse_parm(buf, bond_mode_tbl);
  280. if (new_value < 0) {
  281. pr_err("%s: Ignoring invalid mode value %.*s.\n",
  282. bond->dev->name, (int)strlen(buf) - 1, buf);
  283. ret = -EINVAL;
  284. goto out;
  285. }
  286. if ((new_value == BOND_MODE_ALB ||
  287. new_value == BOND_MODE_TLB) &&
  288. bond->params.arp_interval) {
  289. pr_err("%s: %s mode is incompatible with arp monitoring.\n",
  290. bond->dev->name, bond_mode_tbl[new_value].modename);
  291. ret = -EINVAL;
  292. goto out;
  293. }
  294. bond->params.mode = new_value;
  295. bond_set_mode_ops(bond, bond->params.mode);
  296. pr_info("%s: setting mode to %s (%d).\n",
  297. bond->dev->name, bond_mode_tbl[new_value].modename,
  298. new_value);
  299. out:
  300. return ret;
  301. }
  302. static DEVICE_ATTR(mode, S_IRUGO | S_IWUSR,
  303. bonding_show_mode, bonding_store_mode);
  304. /*
  305. * Show and set the bonding transmit hash method.
  306. * The bond interface must be down to change the xmit hash policy.
  307. */
  308. static ssize_t bonding_show_xmit_hash(struct device *d,
  309. struct device_attribute *attr,
  310. char *buf)
  311. {
  312. struct bonding *bond = to_bond(d);
  313. return sprintf(buf, "%s %d\n",
  314. xmit_hashtype_tbl[bond->params.xmit_policy].modename,
  315. bond->params.xmit_policy);
  316. }
  317. static ssize_t bonding_store_xmit_hash(struct device *d,
  318. struct device_attribute *attr,
  319. const char *buf, size_t count)
  320. {
  321. int new_value, ret = count;
  322. struct bonding *bond = to_bond(d);
  323. if (bond->dev->flags & IFF_UP) {
  324. pr_err("%s: Interface is up. Unable to update xmit policy.\n",
  325. bond->dev->name);
  326. ret = -EPERM;
  327. goto out;
  328. }
  329. new_value = bond_parse_parm(buf, xmit_hashtype_tbl);
  330. if (new_value < 0) {
  331. pr_err("%s: Ignoring invalid xmit hash policy value %.*s.\n",
  332. bond->dev->name,
  333. (int)strlen(buf) - 1, buf);
  334. ret = -EINVAL;
  335. goto out;
  336. } else {
  337. bond->params.xmit_policy = new_value;
  338. bond_set_mode_ops(bond, bond->params.mode);
  339. pr_info("%s: setting xmit hash policy to %s (%d).\n",
  340. bond->dev->name,
  341. xmit_hashtype_tbl[new_value].modename, new_value);
  342. }
  343. out:
  344. return ret;
  345. }
  346. static DEVICE_ATTR(xmit_hash_policy, S_IRUGO | S_IWUSR,
  347. bonding_show_xmit_hash, bonding_store_xmit_hash);
  348. /*
  349. * Show and set arp_validate.
  350. */
  351. static ssize_t bonding_show_arp_validate(struct device *d,
  352. struct device_attribute *attr,
  353. char *buf)
  354. {
  355. struct bonding *bond = to_bond(d);
  356. return sprintf(buf, "%s %d\n",
  357. arp_validate_tbl[bond->params.arp_validate].modename,
  358. bond->params.arp_validate);
  359. }
  360. static ssize_t bonding_store_arp_validate(struct device *d,
  361. struct device_attribute *attr,
  362. const char *buf, size_t count)
  363. {
  364. int new_value;
  365. struct bonding *bond = to_bond(d);
  366. new_value = bond_parse_parm(buf, arp_validate_tbl);
  367. if (new_value < 0) {
  368. pr_err("%s: Ignoring invalid arp_validate value %s\n",
  369. bond->dev->name, buf);
  370. return -EINVAL;
  371. }
  372. if (new_value && (bond->params.mode != BOND_MODE_ACTIVEBACKUP)) {
  373. pr_err("%s: arp_validate only supported in active-backup mode.\n",
  374. bond->dev->name);
  375. return -EINVAL;
  376. }
  377. pr_info("%s: setting arp_validate to %s (%d).\n",
  378. bond->dev->name, arp_validate_tbl[new_value].modename,
  379. new_value);
  380. bond->params.arp_validate = new_value;
  381. return count;
  382. }
  383. static DEVICE_ATTR(arp_validate, S_IRUGO | S_IWUSR, bonding_show_arp_validate,
  384. bonding_store_arp_validate);
  385. /*
  386. * Show and store fail_over_mac. User only allowed to change the
  387. * value when there are no slaves.
  388. */
  389. static ssize_t bonding_show_fail_over_mac(struct device *d,
  390. struct device_attribute *attr,
  391. char *buf)
  392. {
  393. struct bonding *bond = to_bond(d);
  394. return sprintf(buf, "%s %d\n",
  395. fail_over_mac_tbl[bond->params.fail_over_mac].modename,
  396. bond->params.fail_over_mac);
  397. }
  398. static ssize_t bonding_store_fail_over_mac(struct device *d,
  399. struct device_attribute *attr,
  400. const char *buf, size_t count)
  401. {
  402. int new_value;
  403. struct bonding *bond = to_bond(d);
  404. if (bond->slave_cnt != 0) {
  405. pr_err("%s: Can't alter fail_over_mac with slaves in bond.\n",
  406. bond->dev->name);
  407. return -EPERM;
  408. }
  409. new_value = bond_parse_parm(buf, fail_over_mac_tbl);
  410. if (new_value < 0) {
  411. pr_err("%s: Ignoring invalid fail_over_mac value %s.\n",
  412. bond->dev->name, buf);
  413. return -EINVAL;
  414. }
  415. bond->params.fail_over_mac = new_value;
  416. pr_info("%s: Setting fail_over_mac to %s (%d).\n",
  417. bond->dev->name, fail_over_mac_tbl[new_value].modename,
  418. new_value);
  419. return count;
  420. }
  421. static DEVICE_ATTR(fail_over_mac, S_IRUGO | S_IWUSR,
  422. bonding_show_fail_over_mac, bonding_store_fail_over_mac);
  423. /*
  424. * Show and set the arp timer interval. There are two tricky bits
  425. * here. First, if ARP monitoring is activated, then we must disable
  426. * MII monitoring. Second, if the ARP timer isn't running, we must
  427. * start it.
  428. */
  429. static ssize_t bonding_show_arp_interval(struct device *d,
  430. struct device_attribute *attr,
  431. char *buf)
  432. {
  433. struct bonding *bond = to_bond(d);
  434. return sprintf(buf, "%d\n", bond->params.arp_interval);
  435. }
  436. static ssize_t bonding_store_arp_interval(struct device *d,
  437. struct device_attribute *attr,
  438. const char *buf, size_t count)
  439. {
  440. int new_value, ret = count;
  441. struct bonding *bond = to_bond(d);
  442. if (sscanf(buf, "%d", &new_value) != 1) {
  443. pr_err("%s: no arp_interval value specified.\n",
  444. bond->dev->name);
  445. ret = -EINVAL;
  446. goto out;
  447. }
  448. if (new_value < 0) {
  449. pr_err("%s: Invalid arp_interval value %d not in range 1-%d; rejected.\n",
  450. bond->dev->name, new_value, INT_MAX);
  451. ret = -EINVAL;
  452. goto out;
  453. }
  454. if (bond->params.mode == BOND_MODE_ALB ||
  455. bond->params.mode == BOND_MODE_TLB) {
  456. pr_info("%s: ARP monitoring cannot be used with ALB/TLB. Only MII monitoring is supported on %s.\n",
  457. bond->dev->name, bond->dev->name);
  458. ret = -EINVAL;
  459. goto out;
  460. }
  461. pr_info("%s: Setting ARP monitoring interval to %d.\n",
  462. bond->dev->name, new_value);
  463. bond->params.arp_interval = new_value;
  464. if (bond->params.miimon) {
  465. pr_info("%s: ARP monitoring cannot be used with MII monitoring. %s Disabling MII monitoring.\n",
  466. bond->dev->name, bond->dev->name);
  467. bond->params.miimon = 0;
  468. if (delayed_work_pending(&bond->mii_work)) {
  469. cancel_delayed_work(&bond->mii_work);
  470. flush_workqueue(bond->wq);
  471. }
  472. }
  473. if (!bond->params.arp_targets[0]) {
  474. pr_info("%s: ARP monitoring has been set up, but no ARP targets have been specified.\n",
  475. bond->dev->name);
  476. }
  477. if (bond->dev->flags & IFF_UP) {
  478. /* If the interface is up, we may need to fire off
  479. * the ARP timer. If the interface is down, the
  480. * timer will get fired off when the open function
  481. * is called.
  482. */
  483. if (!delayed_work_pending(&bond->arp_work)) {
  484. if (bond->params.mode == BOND_MODE_ACTIVEBACKUP)
  485. INIT_DELAYED_WORK(&bond->arp_work,
  486. bond_activebackup_arp_mon);
  487. else
  488. INIT_DELAYED_WORK(&bond->arp_work,
  489. bond_loadbalance_arp_mon);
  490. queue_delayed_work(bond->wq, &bond->arp_work, 0);
  491. }
  492. }
  493. out:
  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, 1, 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 down delay value %d not in range %d-%d; rejected.\n",
  668. bond->dev->name, new_value, 1, 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 (sscanf(buf, "%d", &new_value) != 1) {
  843. pr_err("%s: no miimon value specified.\n",
  844. bond->dev->name);
  845. ret = -EINVAL;
  846. goto out;
  847. }
  848. if (new_value < 0) {
  849. pr_err("%s: Invalid miimon value %d not in range %d-%d; rejected.\n",
  850. bond->dev->name, new_value, 1, INT_MAX);
  851. ret = -EINVAL;
  852. goto out;
  853. } else {
  854. pr_info("%s: Setting MII monitoring interval to %d.\n",
  855. bond->dev->name, new_value);
  856. bond->params.miimon = new_value;
  857. if (bond->params.updelay)
  858. pr_info("%s: Note: Updating updelay (to %d) since it is a multiple of the miimon value.\n",
  859. bond->dev->name,
  860. bond->params.updelay * bond->params.miimon);
  861. if (bond->params.downdelay)
  862. pr_info("%s: Note: Updating downdelay (to %d) since it is a multiple of the miimon value.\n",
  863. bond->dev->name,
  864. bond->params.downdelay * bond->params.miimon);
  865. if (bond->params.arp_interval) {
  866. pr_info("%s: MII monitoring cannot be used with ARP monitoring. Disabling ARP monitoring...\n",
  867. bond->dev->name);
  868. bond->params.arp_interval = 0;
  869. if (bond->params.arp_validate) {
  870. bond->params.arp_validate =
  871. BOND_ARP_VALIDATE_NONE;
  872. }
  873. if (delayed_work_pending(&bond->arp_work)) {
  874. cancel_delayed_work(&bond->arp_work);
  875. flush_workqueue(bond->wq);
  876. }
  877. }
  878. if (bond->dev->flags & IFF_UP) {
  879. /* If the interface is up, we may need to fire off
  880. * the MII timer. If the interface is down, the
  881. * timer will get fired off when the open function
  882. * is called.
  883. */
  884. if (!delayed_work_pending(&bond->mii_work)) {
  885. INIT_DELAYED_WORK(&bond->mii_work,
  886. bond_mii_monitor);
  887. queue_delayed_work(bond->wq,
  888. &bond->mii_work, 0);
  889. }
  890. }
  891. }
  892. out:
  893. return ret;
  894. }
  895. static DEVICE_ATTR(miimon, S_IRUGO | S_IWUSR,
  896. bonding_show_miimon, bonding_store_miimon);
  897. /*
  898. * Show and set the primary slave. The store function is much
  899. * simpler than bonding_store_slaves function because it only needs to
  900. * handle one interface name.
  901. * The bond must be a mode that supports a primary for this be
  902. * set.
  903. */
  904. static ssize_t bonding_show_primary(struct device *d,
  905. struct device_attribute *attr,
  906. char *buf)
  907. {
  908. int count = 0;
  909. struct bonding *bond = to_bond(d);
  910. if (bond->primary_slave)
  911. count = sprintf(buf, "%s\n", bond->primary_slave->dev->name);
  912. return count;
  913. }
  914. static ssize_t bonding_store_primary(struct device *d,
  915. struct device_attribute *attr,
  916. const char *buf, size_t count)
  917. {
  918. int i;
  919. struct slave *slave;
  920. struct bonding *bond = to_bond(d);
  921. char ifname[IFNAMSIZ];
  922. if (!rtnl_trylock())
  923. return restart_syscall();
  924. block_netpoll_tx();
  925. read_lock(&bond->lock);
  926. write_lock_bh(&bond->curr_slave_lock);
  927. if (!USES_PRIMARY(bond->params.mode)) {
  928. pr_info("%s: Unable to set primary slave; %s is in mode %d\n",
  929. bond->dev->name, bond->dev->name, bond->params.mode);
  930. goto out;
  931. }
  932. sscanf(buf, "%16s", ifname); /* IFNAMSIZ */
  933. /* check to see if we are clearing primary */
  934. if (!strlen(ifname) || buf[0] == '\n') {
  935. pr_info("%s: Setting primary slave to None.\n",
  936. bond->dev->name);
  937. bond->primary_slave = NULL;
  938. bond_select_active_slave(bond);
  939. goto out;
  940. }
  941. bond_for_each_slave(bond, slave, i) {
  942. if (strncmp(slave->dev->name, ifname, IFNAMSIZ) == 0) {
  943. pr_info("%s: Setting %s as primary slave.\n",
  944. bond->dev->name, slave->dev->name);
  945. bond->primary_slave = slave;
  946. strcpy(bond->params.primary, slave->dev->name);
  947. bond_select_active_slave(bond);
  948. goto out;
  949. }
  950. }
  951. pr_info("%s: Unable to set %.*s as primary slave.\n",
  952. bond->dev->name, (int)strlen(buf) - 1, buf);
  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, "%16s", 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->params.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. bond_for_each_slave(bond, slave, i) {
  1372. if (!bond_is_active_slave(slave)) {
  1373. if (new_value)
  1374. slave->inactive = 0;
  1375. else
  1376. slave->inactive = 1;
  1377. }
  1378. }
  1379. out:
  1380. return ret;
  1381. }
  1382. static DEVICE_ATTR(all_slaves_active, S_IRUGO | S_IWUSR,
  1383. bonding_show_slaves_active, bonding_store_slaves_active);
  1384. /*
  1385. * Show and set the number of IGMP membership reports to send on link failure
  1386. */
  1387. static ssize_t bonding_show_resend_igmp(struct device *d,
  1388. struct device_attribute *attr,
  1389. char *buf)
  1390. {
  1391. struct bonding *bond = to_bond(d);
  1392. return sprintf(buf, "%d\n", bond->params.resend_igmp);
  1393. }
  1394. static ssize_t bonding_store_resend_igmp(struct device *d,
  1395. struct device_attribute *attr,
  1396. const char *buf, size_t count)
  1397. {
  1398. int new_value, ret = count;
  1399. struct bonding *bond = to_bond(d);
  1400. if (sscanf(buf, "%d", &new_value) != 1) {
  1401. pr_err("%s: no resend_igmp value specified.\n",
  1402. bond->dev->name);
  1403. ret = -EINVAL;
  1404. goto out;
  1405. }
  1406. if (new_value < 0 || new_value > 255) {
  1407. pr_err("%s: Invalid resend_igmp value %d not in range 0-255; rejected.\n",
  1408. bond->dev->name, new_value);
  1409. ret = -EINVAL;
  1410. goto out;
  1411. }
  1412. pr_info("%s: Setting resend_igmp to %d.\n",
  1413. bond->dev->name, new_value);
  1414. bond->params.resend_igmp = new_value;
  1415. out:
  1416. return ret;
  1417. }
  1418. static DEVICE_ATTR(resend_igmp, S_IRUGO | S_IWUSR,
  1419. bonding_show_resend_igmp, bonding_store_resend_igmp);
  1420. static struct attribute *per_bond_attrs[] = {
  1421. &dev_attr_slaves.attr,
  1422. &dev_attr_mode.attr,
  1423. &dev_attr_fail_over_mac.attr,
  1424. &dev_attr_arp_validate.attr,
  1425. &dev_attr_arp_interval.attr,
  1426. &dev_attr_arp_ip_target.attr,
  1427. &dev_attr_downdelay.attr,
  1428. &dev_attr_updelay.attr,
  1429. &dev_attr_lacp_rate.attr,
  1430. &dev_attr_ad_select.attr,
  1431. &dev_attr_xmit_hash_policy.attr,
  1432. &dev_attr_num_grat_arp.attr,
  1433. &dev_attr_num_unsol_na.attr,
  1434. &dev_attr_miimon.attr,
  1435. &dev_attr_primary.attr,
  1436. &dev_attr_primary_reselect.attr,
  1437. &dev_attr_use_carrier.attr,
  1438. &dev_attr_active_slave.attr,
  1439. &dev_attr_mii_status.attr,
  1440. &dev_attr_ad_aggregator.attr,
  1441. &dev_attr_ad_num_ports.attr,
  1442. &dev_attr_ad_actor_key.attr,
  1443. &dev_attr_ad_partner_key.attr,
  1444. &dev_attr_ad_partner_mac.attr,
  1445. &dev_attr_queue_id.attr,
  1446. &dev_attr_all_slaves_active.attr,
  1447. &dev_attr_resend_igmp.attr,
  1448. &dev_attr_min_links.attr,
  1449. NULL,
  1450. };
  1451. static struct attribute_group bonding_group = {
  1452. .name = "bonding",
  1453. .attrs = per_bond_attrs,
  1454. };
  1455. /*
  1456. * Initialize sysfs. This sets up the bonding_masters file in
  1457. * /sys/class/net.
  1458. */
  1459. int bond_create_sysfs(struct bond_net *bn)
  1460. {
  1461. int ret;
  1462. bn->class_attr_bonding_masters = class_attr_bonding_masters;
  1463. sysfs_attr_init(&bn->class_attr_bonding_masters.attr);
  1464. ret = netdev_class_create_file(&bn->class_attr_bonding_masters);
  1465. /*
  1466. * Permit multiple loads of the module by ignoring failures to
  1467. * create the bonding_masters sysfs file. Bonding devices
  1468. * created by second or subsequent loads of the module will
  1469. * not be listed in, or controllable by, bonding_masters, but
  1470. * will have the usual "bonding" sysfs directory.
  1471. *
  1472. * This is done to preserve backwards compatibility for
  1473. * initscripts/sysconfig, which load bonding multiple times to
  1474. * configure multiple bonding devices.
  1475. */
  1476. if (ret == -EEXIST) {
  1477. /* Is someone being kinky and naming a device bonding_master? */
  1478. if (__dev_get_by_name(bn->net,
  1479. class_attr_bonding_masters.attr.name))
  1480. pr_err("network device named %s already exists in sysfs",
  1481. class_attr_bonding_masters.attr.name);
  1482. ret = 0;
  1483. }
  1484. return ret;
  1485. }
  1486. /*
  1487. * Remove /sys/class/net/bonding_masters.
  1488. */
  1489. void bond_destroy_sysfs(struct bond_net *bn)
  1490. {
  1491. netdev_class_remove_file(&bn->class_attr_bonding_masters);
  1492. }
  1493. /*
  1494. * Initialize sysfs for each bond. This sets up and registers
  1495. * the 'bondctl' directory for each individual bond under /sys/class/net.
  1496. */
  1497. void bond_prepare_sysfs_group(struct bonding *bond)
  1498. {
  1499. bond->dev->sysfs_groups[0] = &bonding_group;
  1500. }