bond_sysfs.c 40 KB

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