bond_sysfs.c 44 KB

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