bond_sysfs.c 42 KB

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