bond_sysfs.c 40 KB

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