bond_sysfs.c 44 KB

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