bond_sysfs.c 41 KB

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