bond_sysfs.c 42 KB

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