bond_sysfs.c 41 KB

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