bond_sysfs.c 42 KB

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