bond_sysfs.c 40 KB

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