bond_sysfs.c 43 KB

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