bond_sysfs.c 41 KB

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