bond_sysfs.c 43 KB

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