bond_sysfs.c 43 KB

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