bond_sysfs.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163
  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, see <http://www.gnu.org/licenses/>.
  16. *
  17. * The full GNU General Public License is included in this distribution in the
  18. * file called LICENSE.
  19. *
  20. */
  21. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  22. #include <linux/kernel.h>
  23. #include <linux/module.h>
  24. #include <linux/device.h>
  25. #include <linux/sched.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 <linux/etherdevice.h>
  37. #include <net/net_namespace.h>
  38. #include <net/netns/generic.h>
  39. #include <linux/nsproxy.h>
  40. #include "bonding.h"
  41. #define to_dev(obj) container_of(obj, struct device, kobj)
  42. #define to_bond(cd) ((struct bonding *)(netdev_priv(to_net_dev(cd))))
  43. /*
  44. * "show" function for the bond_masters attribute.
  45. * The class parameter is ignored.
  46. */
  47. static ssize_t bonding_show_bonds(struct class *cls,
  48. struct class_attribute *attr,
  49. char *buf)
  50. {
  51. struct bond_net *bn =
  52. container_of(attr, struct bond_net, class_attr_bonding_masters);
  53. int res = 0;
  54. struct bonding *bond;
  55. rtnl_lock();
  56. list_for_each_entry(bond, &bn->dev_list, bond_list) {
  57. if (res > (PAGE_SIZE - IFNAMSIZ)) {
  58. /* not enough space for another interface name */
  59. if ((PAGE_SIZE - res) > 10)
  60. res = PAGE_SIZE - 10;
  61. res += sprintf(buf + res, "++more++ ");
  62. break;
  63. }
  64. res += sprintf(buf + res, "%s ", bond->dev->name);
  65. }
  66. if (res)
  67. buf[res-1] = '\n'; /* eat the leftover space */
  68. rtnl_unlock();
  69. return res;
  70. }
  71. static struct net_device *bond_get_by_name(struct bond_net *bn, const char *ifname)
  72. {
  73. struct bonding *bond;
  74. list_for_each_entry(bond, &bn->dev_list, bond_list) {
  75. if (strncmp(bond->dev->name, ifname, IFNAMSIZ) == 0)
  76. return bond->dev;
  77. }
  78. return NULL;
  79. }
  80. /*
  81. * "store" function for the bond_masters attribute. This is what
  82. * creates and deletes entire bonds.
  83. *
  84. * The class parameter is ignored.
  85. *
  86. */
  87. static ssize_t bonding_store_bonds(struct class *cls,
  88. struct class_attribute *attr,
  89. const char *buffer, size_t count)
  90. {
  91. struct bond_net *bn =
  92. container_of(attr, struct bond_net, class_attr_bonding_masters);
  93. char command[IFNAMSIZ + 1] = {0, };
  94. char *ifname;
  95. int rv, res = count;
  96. sscanf(buffer, "%16s", command); /* IFNAMSIZ*/
  97. ifname = command + 1;
  98. if ((strlen(command) <= 1) ||
  99. !dev_valid_name(ifname))
  100. goto err_no_cmd;
  101. if (command[0] == '+') {
  102. pr_info("%s is being created...\n", ifname);
  103. rv = bond_create(bn->net, ifname);
  104. if (rv) {
  105. if (rv == -EEXIST)
  106. pr_info("%s already exists.\n", ifname);
  107. else
  108. pr_info("%s creation failed.\n", ifname);
  109. res = rv;
  110. }
  111. } else if (command[0] == '-') {
  112. struct net_device *bond_dev;
  113. rtnl_lock();
  114. bond_dev = bond_get_by_name(bn, ifname);
  115. if (bond_dev) {
  116. pr_info("%s is being deleted...\n", ifname);
  117. unregister_netdevice(bond_dev);
  118. } else {
  119. pr_err("unable to delete non-existent %s\n", ifname);
  120. res = -ENODEV;
  121. }
  122. rtnl_unlock();
  123. } else
  124. goto err_no_cmd;
  125. /* Always return either count or an error. If you return 0, you'll
  126. * get called forever, which is bad.
  127. */
  128. return res;
  129. err_no_cmd:
  130. pr_err("no command found in bonding_masters. Use +ifname or -ifname.\n");
  131. return -EPERM;
  132. }
  133. /* class attribute for bond_masters file. This ends up in /sys/class/net */
  134. static const struct class_attribute class_attr_bonding_masters = {
  135. .attr = {
  136. .name = "bonding_masters",
  137. .mode = S_IWUSR | S_IRUGO,
  138. },
  139. .show = bonding_show_bonds,
  140. .store = bonding_store_bonds,
  141. };
  142. /*
  143. * Show the slaves in the current bond.
  144. */
  145. static ssize_t bonding_show_slaves(struct device *d,
  146. struct device_attribute *attr, char *buf)
  147. {
  148. struct bonding *bond = to_bond(d);
  149. struct list_head *iter;
  150. struct slave *slave;
  151. int res = 0;
  152. if (!rtnl_trylock())
  153. return restart_syscall();
  154. bond_for_each_slave(bond, slave, iter) {
  155. if (res > (PAGE_SIZE - IFNAMSIZ)) {
  156. /* not enough space for another interface name */
  157. if ((PAGE_SIZE - res) > 10)
  158. res = PAGE_SIZE - 10;
  159. res += sprintf(buf + res, "++more++ ");
  160. break;
  161. }
  162. res += sprintf(buf + res, "%s ", slave->dev->name);
  163. }
  164. rtnl_unlock();
  165. if (res)
  166. buf[res-1] = '\n'; /* eat the leftover space */
  167. return res;
  168. }
  169. /*
  170. * Set the slaves in the current bond.
  171. * This is supposed to be only thin wrapper for bond_enslave and bond_release.
  172. * All hard work should be done there.
  173. */
  174. static ssize_t bonding_store_slaves(struct device *d,
  175. struct device_attribute *attr,
  176. const char *buffer, size_t count)
  177. {
  178. struct bonding *bond = to_bond(d);
  179. int ret;
  180. ret = bond_opt_tryset_rtnl(bond, BOND_OPT_SLAVES, (char *)buffer);
  181. if (!ret)
  182. ret = count;
  183. return ret;
  184. }
  185. static DEVICE_ATTR(slaves, S_IRUGO | S_IWUSR, bonding_show_slaves,
  186. bonding_store_slaves);
  187. /*
  188. * Show and set the bonding mode. The bond interface must be down to
  189. * change the mode.
  190. */
  191. static ssize_t bonding_show_mode(struct device *d,
  192. struct device_attribute *attr, char *buf)
  193. {
  194. struct bonding *bond = to_bond(d);
  195. struct bond_opt_value *val;
  196. val = bond_opt_get_val(BOND_OPT_MODE, bond->params.mode);
  197. return sprintf(buf, "%s %d\n", val->string, bond->params.mode);
  198. }
  199. static ssize_t bonding_store_mode(struct device *d,
  200. struct device_attribute *attr,
  201. const char *buf, size_t count)
  202. {
  203. struct bonding *bond = to_bond(d);
  204. int ret;
  205. ret = bond_opt_tryset_rtnl(bond, BOND_OPT_MODE, (char *)buf);
  206. if (!ret)
  207. ret = count;
  208. return ret;
  209. }
  210. static DEVICE_ATTR(mode, S_IRUGO | S_IWUSR,
  211. bonding_show_mode, bonding_store_mode);
  212. /*
  213. * Show and set the bonding transmit hash method.
  214. */
  215. static ssize_t bonding_show_xmit_hash(struct device *d,
  216. struct device_attribute *attr,
  217. char *buf)
  218. {
  219. struct bonding *bond = to_bond(d);
  220. struct bond_opt_value *val;
  221. val = bond_opt_get_val(BOND_OPT_XMIT_HASH, bond->params.xmit_policy);
  222. return sprintf(buf, "%s %d\n", val->string, bond->params.xmit_policy);
  223. }
  224. static ssize_t bonding_store_xmit_hash(struct device *d,
  225. struct device_attribute *attr,
  226. const char *buf, size_t count)
  227. {
  228. struct bonding *bond = to_bond(d);
  229. int ret;
  230. ret = bond_opt_tryset_rtnl(bond, BOND_OPT_XMIT_HASH, (char *)buf);
  231. if (!ret)
  232. ret = count;
  233. return ret;
  234. }
  235. static DEVICE_ATTR(xmit_hash_policy, S_IRUGO | S_IWUSR,
  236. bonding_show_xmit_hash, bonding_store_xmit_hash);
  237. /*
  238. * Show and set arp_validate.
  239. */
  240. static ssize_t bonding_show_arp_validate(struct device *d,
  241. struct device_attribute *attr,
  242. char *buf)
  243. {
  244. struct bonding *bond = to_bond(d);
  245. struct bond_opt_value *val;
  246. val = bond_opt_get_val(BOND_OPT_ARP_VALIDATE,
  247. bond->params.arp_validate);
  248. return sprintf(buf, "%s %d\n", val->string, bond->params.arp_validate);
  249. }
  250. static ssize_t bonding_store_arp_validate(struct device *d,
  251. struct device_attribute *attr,
  252. const char *buf, size_t count)
  253. {
  254. struct bonding *bond = to_bond(d);
  255. int ret;
  256. ret = bond_opt_tryset_rtnl(bond, BOND_OPT_ARP_VALIDATE, (char *)buf);
  257. if (!ret)
  258. ret = count;
  259. return ret;
  260. }
  261. static DEVICE_ATTR(arp_validate, S_IRUGO | S_IWUSR, bonding_show_arp_validate,
  262. bonding_store_arp_validate);
  263. /*
  264. * Show and set arp_all_targets.
  265. */
  266. static ssize_t bonding_show_arp_all_targets(struct device *d,
  267. struct device_attribute *attr,
  268. char *buf)
  269. {
  270. struct bonding *bond = to_bond(d);
  271. struct bond_opt_value *val;
  272. val = bond_opt_get_val(BOND_OPT_ARP_ALL_TARGETS,
  273. bond->params.arp_all_targets);
  274. return sprintf(buf, "%s %d\n",
  275. val->string, bond->params.arp_all_targets);
  276. }
  277. static ssize_t bonding_store_arp_all_targets(struct device *d,
  278. struct device_attribute *attr,
  279. const char *buf, size_t count)
  280. {
  281. struct bonding *bond = to_bond(d);
  282. int ret;
  283. ret = bond_opt_tryset_rtnl(bond, BOND_OPT_ARP_ALL_TARGETS, (char *)buf);
  284. if (!ret)
  285. ret = count;
  286. return ret;
  287. }
  288. static DEVICE_ATTR(arp_all_targets, S_IRUGO | S_IWUSR,
  289. bonding_show_arp_all_targets, bonding_store_arp_all_targets);
  290. /*
  291. * Show and store fail_over_mac. User only allowed to change the
  292. * value when there are no slaves.
  293. */
  294. static ssize_t bonding_show_fail_over_mac(struct device *d,
  295. struct device_attribute *attr,
  296. char *buf)
  297. {
  298. struct bonding *bond = to_bond(d);
  299. struct bond_opt_value *val;
  300. val = bond_opt_get_val(BOND_OPT_FAIL_OVER_MAC,
  301. bond->params.fail_over_mac);
  302. return sprintf(buf, "%s %d\n", val->string, bond->params.fail_over_mac);
  303. }
  304. static ssize_t bonding_store_fail_over_mac(struct device *d,
  305. struct device_attribute *attr,
  306. const char *buf, size_t count)
  307. {
  308. struct bonding *bond = to_bond(d);
  309. int ret;
  310. ret = bond_opt_tryset_rtnl(bond, BOND_OPT_FAIL_OVER_MAC, (char *)buf);
  311. if (!ret)
  312. ret = count;
  313. return ret;
  314. }
  315. static DEVICE_ATTR(fail_over_mac, S_IRUGO | S_IWUSR,
  316. bonding_show_fail_over_mac, bonding_store_fail_over_mac);
  317. /*
  318. * Show and set the arp timer interval. There are two tricky bits
  319. * here. First, if ARP monitoring is activated, then we must disable
  320. * MII monitoring. Second, if the ARP timer isn't running, we must
  321. * start it.
  322. */
  323. static ssize_t bonding_show_arp_interval(struct device *d,
  324. struct device_attribute *attr,
  325. char *buf)
  326. {
  327. struct bonding *bond = to_bond(d);
  328. return sprintf(buf, "%d\n", bond->params.arp_interval);
  329. }
  330. static ssize_t bonding_store_arp_interval(struct device *d,
  331. struct device_attribute *attr,
  332. const char *buf, size_t count)
  333. {
  334. struct bonding *bond = to_bond(d);
  335. int ret;
  336. ret = bond_opt_tryset_rtnl(bond, BOND_OPT_ARP_INTERVAL, (char *)buf);
  337. if (!ret)
  338. ret = count;
  339. return ret;
  340. }
  341. static DEVICE_ATTR(arp_interval, S_IRUGO | S_IWUSR,
  342. bonding_show_arp_interval, bonding_store_arp_interval);
  343. /*
  344. * Show and set the arp targets.
  345. */
  346. static ssize_t bonding_show_arp_targets(struct device *d,
  347. struct device_attribute *attr,
  348. char *buf)
  349. {
  350. struct bonding *bond = to_bond(d);
  351. int i, res = 0;
  352. for (i = 0; i < BOND_MAX_ARP_TARGETS; i++) {
  353. if (bond->params.arp_targets[i])
  354. res += sprintf(buf + res, "%pI4 ",
  355. &bond->params.arp_targets[i]);
  356. }
  357. if (res)
  358. buf[res-1] = '\n'; /* eat the leftover space */
  359. return res;
  360. }
  361. static ssize_t bonding_store_arp_targets(struct device *d,
  362. struct device_attribute *attr,
  363. const char *buf, size_t count)
  364. {
  365. struct bonding *bond = to_bond(d);
  366. int ret;
  367. ret = bond_opt_tryset_rtnl(bond, BOND_OPT_ARP_TARGETS, (char *)buf);
  368. if (!ret)
  369. ret = count;
  370. return ret;
  371. }
  372. static DEVICE_ATTR(arp_ip_target, S_IRUGO | S_IWUSR , bonding_show_arp_targets, bonding_store_arp_targets);
  373. /*
  374. * Show and set the up and down delays. These must be multiples of the
  375. * MII monitoring value, and are stored internally as the multiplier.
  376. * Thus, we must translate to MS for the real world.
  377. */
  378. static ssize_t bonding_show_downdelay(struct device *d,
  379. struct device_attribute *attr,
  380. char *buf)
  381. {
  382. struct bonding *bond = to_bond(d);
  383. return sprintf(buf, "%d\n", bond->params.downdelay * bond->params.miimon);
  384. }
  385. static ssize_t bonding_store_downdelay(struct device *d,
  386. struct device_attribute *attr,
  387. const char *buf, size_t count)
  388. {
  389. struct bonding *bond = to_bond(d);
  390. int ret;
  391. ret = bond_opt_tryset_rtnl(bond, BOND_OPT_DOWNDELAY, (char *)buf);
  392. if (!ret)
  393. ret = count;
  394. return ret;
  395. }
  396. static DEVICE_ATTR(downdelay, S_IRUGO | S_IWUSR,
  397. bonding_show_downdelay, bonding_store_downdelay);
  398. static ssize_t bonding_show_updelay(struct device *d,
  399. struct device_attribute *attr,
  400. char *buf)
  401. {
  402. struct bonding *bond = to_bond(d);
  403. return sprintf(buf, "%d\n", bond->params.updelay * bond->params.miimon);
  404. }
  405. static ssize_t bonding_store_updelay(struct device *d,
  406. struct device_attribute *attr,
  407. const char *buf, size_t count)
  408. {
  409. struct bonding *bond = to_bond(d);
  410. int ret;
  411. ret = bond_opt_tryset_rtnl(bond, BOND_OPT_UPDELAY, (char *)buf);
  412. if (!ret)
  413. ret = count;
  414. return ret;
  415. }
  416. static DEVICE_ATTR(updelay, S_IRUGO | S_IWUSR,
  417. bonding_show_updelay, bonding_store_updelay);
  418. /*
  419. * Show and set the LACP interval. Interface must be down, and the mode
  420. * must be set to 802.3ad mode.
  421. */
  422. static ssize_t bonding_show_lacp(struct device *d,
  423. struct device_attribute *attr,
  424. char *buf)
  425. {
  426. struct bonding *bond = to_bond(d);
  427. struct bond_opt_value *val;
  428. val = bond_opt_get_val(BOND_OPT_LACP_RATE, bond->params.lacp_fast);
  429. return sprintf(buf, "%s %d\n", val->string, bond->params.lacp_fast);
  430. }
  431. static ssize_t bonding_store_lacp(struct device *d,
  432. struct device_attribute *attr,
  433. const char *buf, size_t count)
  434. {
  435. struct bonding *bond = to_bond(d);
  436. int ret;
  437. ret = bond_opt_tryset_rtnl(bond, BOND_OPT_LACP_RATE, (char *)buf);
  438. if (!ret)
  439. ret = count;
  440. return ret;
  441. }
  442. static DEVICE_ATTR(lacp_rate, S_IRUGO | S_IWUSR,
  443. bonding_show_lacp, bonding_store_lacp);
  444. static ssize_t bonding_show_min_links(struct device *d,
  445. struct device_attribute *attr,
  446. char *buf)
  447. {
  448. struct bonding *bond = to_bond(d);
  449. return sprintf(buf, "%d\n", bond->params.min_links);
  450. }
  451. static ssize_t bonding_store_min_links(struct device *d,
  452. struct device_attribute *attr,
  453. const char *buf, size_t count)
  454. {
  455. struct bonding *bond = to_bond(d);
  456. int ret;
  457. ret = bond_opt_tryset_rtnl(bond, BOND_OPT_MINLINKS, (char *)buf);
  458. if (!ret)
  459. ret = count;
  460. return ret;
  461. }
  462. static DEVICE_ATTR(min_links, S_IRUGO | S_IWUSR,
  463. bonding_show_min_links, bonding_store_min_links);
  464. static ssize_t bonding_show_ad_select(struct device *d,
  465. struct device_attribute *attr,
  466. char *buf)
  467. {
  468. struct bonding *bond = to_bond(d);
  469. struct bond_opt_value *val;
  470. val = bond_opt_get_val(BOND_OPT_AD_SELECT, bond->params.ad_select);
  471. return sprintf(buf, "%s %d\n", val->string, bond->params.ad_select);
  472. }
  473. static ssize_t bonding_store_ad_select(struct device *d,
  474. struct device_attribute *attr,
  475. const char *buf, size_t count)
  476. {
  477. struct bonding *bond = to_bond(d);
  478. int ret;
  479. ret = bond_opt_tryset_rtnl(bond, BOND_OPT_AD_SELECT, (char *)buf);
  480. if (!ret)
  481. ret = count;
  482. return ret;
  483. }
  484. static DEVICE_ATTR(ad_select, S_IRUGO | S_IWUSR,
  485. bonding_show_ad_select, bonding_store_ad_select);
  486. /*
  487. * Show and set the number of peer notifications to send after a failover event.
  488. */
  489. static ssize_t bonding_show_num_peer_notif(struct device *d,
  490. struct device_attribute *attr,
  491. char *buf)
  492. {
  493. struct bonding *bond = to_bond(d);
  494. return sprintf(buf, "%d\n", bond->params.num_peer_notif);
  495. }
  496. static ssize_t bonding_store_num_peer_notif(struct device *d,
  497. struct device_attribute *attr,
  498. const char *buf, size_t count)
  499. {
  500. struct bonding *bond = to_bond(d);
  501. int ret;
  502. ret = bond_opt_tryset_rtnl(bond, BOND_OPT_NUM_PEER_NOTIF, (char *)buf);
  503. if (!ret)
  504. ret = count;
  505. return ret;
  506. }
  507. static DEVICE_ATTR(num_grat_arp, S_IRUGO | S_IWUSR,
  508. bonding_show_num_peer_notif, bonding_store_num_peer_notif);
  509. static DEVICE_ATTR(num_unsol_na, S_IRUGO | S_IWUSR,
  510. bonding_show_num_peer_notif, bonding_store_num_peer_notif);
  511. /*
  512. * Show and set the MII monitor interval. There are two tricky bits
  513. * here. First, if MII monitoring is activated, then we must disable
  514. * ARP monitoring. Second, if the timer isn't running, we must
  515. * start it.
  516. */
  517. static ssize_t bonding_show_miimon(struct device *d,
  518. struct device_attribute *attr,
  519. char *buf)
  520. {
  521. struct bonding *bond = to_bond(d);
  522. return sprintf(buf, "%d\n", bond->params.miimon);
  523. }
  524. static ssize_t bonding_store_miimon(struct device *d,
  525. struct device_attribute *attr,
  526. const char *buf, size_t count)
  527. {
  528. struct bonding *bond = to_bond(d);
  529. int ret;
  530. ret = bond_opt_tryset_rtnl(bond, BOND_OPT_MIIMON, (char *)buf);
  531. if (!ret)
  532. ret = count;
  533. return ret;
  534. }
  535. static DEVICE_ATTR(miimon, S_IRUGO | S_IWUSR,
  536. bonding_show_miimon, bonding_store_miimon);
  537. /*
  538. * Show and set the primary slave. The store function is much
  539. * simpler than bonding_store_slaves function because it only needs to
  540. * handle one interface name.
  541. * The bond must be a mode that supports a primary for this be
  542. * set.
  543. */
  544. static ssize_t bonding_show_primary(struct device *d,
  545. struct device_attribute *attr,
  546. char *buf)
  547. {
  548. int count = 0;
  549. struct bonding *bond = to_bond(d);
  550. if (bond->primary_slave)
  551. count = sprintf(buf, "%s\n", bond->primary_slave->dev->name);
  552. return count;
  553. }
  554. static ssize_t bonding_store_primary(struct device *d,
  555. struct device_attribute *attr,
  556. const char *buf, size_t count)
  557. {
  558. struct bonding *bond = to_bond(d);
  559. int ret;
  560. ret = bond_opt_tryset_rtnl(bond, BOND_OPT_PRIMARY, (char *)buf);
  561. if (!ret)
  562. ret = count;
  563. return ret;
  564. }
  565. static DEVICE_ATTR(primary, S_IRUGO | S_IWUSR,
  566. bonding_show_primary, bonding_store_primary);
  567. /*
  568. * Show and set the primary_reselect flag.
  569. */
  570. static ssize_t bonding_show_primary_reselect(struct device *d,
  571. struct device_attribute *attr,
  572. char *buf)
  573. {
  574. struct bonding *bond = to_bond(d);
  575. struct bond_opt_value *val;
  576. val = bond_opt_get_val(BOND_OPT_PRIMARY_RESELECT,
  577. bond->params.primary_reselect);
  578. return sprintf(buf, "%s %d\n",
  579. val->string, bond->params.primary_reselect);
  580. }
  581. static ssize_t bonding_store_primary_reselect(struct device *d,
  582. struct device_attribute *attr,
  583. const char *buf, size_t count)
  584. {
  585. struct bonding *bond = to_bond(d);
  586. int ret;
  587. ret = bond_opt_tryset_rtnl(bond, BOND_OPT_PRIMARY_RESELECT,
  588. (char *)buf);
  589. if (!ret)
  590. ret = count;
  591. return ret;
  592. }
  593. static DEVICE_ATTR(primary_reselect, S_IRUGO | S_IWUSR,
  594. bonding_show_primary_reselect,
  595. bonding_store_primary_reselect);
  596. /*
  597. * Show and set the use_carrier flag.
  598. */
  599. static ssize_t bonding_show_carrier(struct device *d,
  600. struct device_attribute *attr,
  601. char *buf)
  602. {
  603. struct bonding *bond = to_bond(d);
  604. return sprintf(buf, "%d\n", bond->params.use_carrier);
  605. }
  606. static ssize_t bonding_store_carrier(struct device *d,
  607. struct device_attribute *attr,
  608. const char *buf, size_t count)
  609. {
  610. struct bonding *bond = to_bond(d);
  611. int ret;
  612. ret = bond_opt_tryset_rtnl(bond, BOND_OPT_USE_CARRIER, (char *)buf);
  613. if (!ret)
  614. ret = count;
  615. return ret;
  616. }
  617. static DEVICE_ATTR(use_carrier, S_IRUGO | S_IWUSR,
  618. bonding_show_carrier, bonding_store_carrier);
  619. /*
  620. * Show and set currently active_slave.
  621. */
  622. static ssize_t bonding_show_active_slave(struct device *d,
  623. struct device_attribute *attr,
  624. char *buf)
  625. {
  626. struct bonding *bond = to_bond(d);
  627. struct net_device *slave_dev;
  628. int count = 0;
  629. rcu_read_lock();
  630. slave_dev = bond_option_active_slave_get_rcu(bond);
  631. if (slave_dev)
  632. count = sprintf(buf, "%s\n", slave_dev->name);
  633. rcu_read_unlock();
  634. return count;
  635. }
  636. static ssize_t bonding_store_active_slave(struct device *d,
  637. struct device_attribute *attr,
  638. const char *buf, size_t count)
  639. {
  640. struct bonding *bond = to_bond(d);
  641. int ret;
  642. ret = bond_opt_tryset_rtnl(bond, BOND_OPT_ACTIVE_SLAVE, (char *)buf);
  643. if (!ret)
  644. ret = count;
  645. return ret;
  646. }
  647. static DEVICE_ATTR(active_slave, S_IRUGO | S_IWUSR,
  648. bonding_show_active_slave, bonding_store_active_slave);
  649. /*
  650. * Show link status of the bond interface.
  651. */
  652. static ssize_t bonding_show_mii_status(struct device *d,
  653. struct device_attribute *attr,
  654. char *buf)
  655. {
  656. struct bonding *bond = to_bond(d);
  657. return sprintf(buf, "%s\n", bond->curr_active_slave ? "up" : "down");
  658. }
  659. static DEVICE_ATTR(mii_status, S_IRUGO, bonding_show_mii_status, NULL);
  660. /*
  661. * Show current 802.3ad aggregator ID.
  662. */
  663. static ssize_t bonding_show_ad_aggregator(struct device *d,
  664. struct device_attribute *attr,
  665. char *buf)
  666. {
  667. int count = 0;
  668. struct bonding *bond = to_bond(d);
  669. if (bond->params.mode == BOND_MODE_8023AD) {
  670. struct ad_info ad_info;
  671. count = sprintf(buf, "%d\n",
  672. bond_3ad_get_active_agg_info(bond, &ad_info)
  673. ? 0 : ad_info.aggregator_id);
  674. }
  675. return count;
  676. }
  677. static DEVICE_ATTR(ad_aggregator, S_IRUGO, bonding_show_ad_aggregator, NULL);
  678. /*
  679. * Show number of active 802.3ad ports.
  680. */
  681. static ssize_t bonding_show_ad_num_ports(struct device *d,
  682. struct device_attribute *attr,
  683. char *buf)
  684. {
  685. int count = 0;
  686. struct bonding *bond = to_bond(d);
  687. if (bond->params.mode == BOND_MODE_8023AD) {
  688. struct ad_info ad_info;
  689. count = sprintf(buf, "%d\n",
  690. bond_3ad_get_active_agg_info(bond, &ad_info)
  691. ? 0 : ad_info.ports);
  692. }
  693. return count;
  694. }
  695. static DEVICE_ATTR(ad_num_ports, S_IRUGO, bonding_show_ad_num_ports, NULL);
  696. /*
  697. * Show current 802.3ad actor key.
  698. */
  699. static ssize_t bonding_show_ad_actor_key(struct device *d,
  700. struct device_attribute *attr,
  701. char *buf)
  702. {
  703. int count = 0;
  704. struct bonding *bond = to_bond(d);
  705. if (bond->params.mode == BOND_MODE_8023AD) {
  706. struct ad_info ad_info;
  707. count = sprintf(buf, "%d\n",
  708. bond_3ad_get_active_agg_info(bond, &ad_info)
  709. ? 0 : ad_info.actor_key);
  710. }
  711. return count;
  712. }
  713. static DEVICE_ATTR(ad_actor_key, S_IRUGO, bonding_show_ad_actor_key, NULL);
  714. /*
  715. * Show current 802.3ad partner key.
  716. */
  717. static ssize_t bonding_show_ad_partner_key(struct device *d,
  718. struct device_attribute *attr,
  719. char *buf)
  720. {
  721. int count = 0;
  722. struct bonding *bond = to_bond(d);
  723. if (bond->params.mode == BOND_MODE_8023AD) {
  724. struct ad_info ad_info;
  725. count = sprintf(buf, "%d\n",
  726. bond_3ad_get_active_agg_info(bond, &ad_info)
  727. ? 0 : ad_info.partner_key);
  728. }
  729. return count;
  730. }
  731. static DEVICE_ATTR(ad_partner_key, S_IRUGO, bonding_show_ad_partner_key, NULL);
  732. /*
  733. * Show current 802.3ad partner mac.
  734. */
  735. static ssize_t bonding_show_ad_partner_mac(struct device *d,
  736. struct device_attribute *attr,
  737. char *buf)
  738. {
  739. int count = 0;
  740. struct bonding *bond = to_bond(d);
  741. if (bond->params.mode == BOND_MODE_8023AD) {
  742. struct ad_info ad_info;
  743. if (!bond_3ad_get_active_agg_info(bond, &ad_info))
  744. count = sprintf(buf, "%pM\n", ad_info.partner_system);
  745. }
  746. return count;
  747. }
  748. static DEVICE_ATTR(ad_partner_mac, S_IRUGO, bonding_show_ad_partner_mac, NULL);
  749. /*
  750. * Show the queue_ids of the slaves in the current bond.
  751. */
  752. static ssize_t bonding_show_queue_id(struct device *d,
  753. struct device_attribute *attr,
  754. char *buf)
  755. {
  756. struct bonding *bond = to_bond(d);
  757. struct list_head *iter;
  758. struct slave *slave;
  759. int res = 0;
  760. if (!rtnl_trylock())
  761. return restart_syscall();
  762. bond_for_each_slave(bond, slave, iter) {
  763. if (res > (PAGE_SIZE - IFNAMSIZ - 6)) {
  764. /* not enough space for another interface_name:queue_id pair */
  765. if ((PAGE_SIZE - res) > 10)
  766. res = PAGE_SIZE - 10;
  767. res += sprintf(buf + res, "++more++ ");
  768. break;
  769. }
  770. res += sprintf(buf + res, "%s:%d ",
  771. slave->dev->name, slave->queue_id);
  772. }
  773. if (res)
  774. buf[res-1] = '\n'; /* eat the leftover space */
  775. rtnl_unlock();
  776. return res;
  777. }
  778. /*
  779. * Set the queue_ids of the slaves in the current bond. The bond
  780. * interface must be enslaved for this to work.
  781. */
  782. static ssize_t bonding_store_queue_id(struct device *d,
  783. struct device_attribute *attr,
  784. const char *buffer, size_t count)
  785. {
  786. struct bonding *bond = to_bond(d);
  787. int ret;
  788. ret = bond_opt_tryset_rtnl(bond, BOND_OPT_QUEUE_ID, (char *)buffer);
  789. if (!ret)
  790. ret = count;
  791. return ret;
  792. }
  793. static DEVICE_ATTR(queue_id, S_IRUGO | S_IWUSR, bonding_show_queue_id,
  794. bonding_store_queue_id);
  795. /*
  796. * Show and set the all_slaves_active flag.
  797. */
  798. static ssize_t bonding_show_slaves_active(struct device *d,
  799. struct device_attribute *attr,
  800. char *buf)
  801. {
  802. struct bonding *bond = to_bond(d);
  803. return sprintf(buf, "%d\n", bond->params.all_slaves_active);
  804. }
  805. static ssize_t bonding_store_slaves_active(struct device *d,
  806. struct device_attribute *attr,
  807. const char *buf, size_t count)
  808. {
  809. struct bonding *bond = to_bond(d);
  810. int ret;
  811. ret = bond_opt_tryset_rtnl(bond, BOND_OPT_ALL_SLAVES_ACTIVE,
  812. (char *)buf);
  813. if (!ret)
  814. ret = count;
  815. return ret;
  816. }
  817. static DEVICE_ATTR(all_slaves_active, S_IRUGO | S_IWUSR,
  818. bonding_show_slaves_active, bonding_store_slaves_active);
  819. /*
  820. * Show and set the number of IGMP membership reports to send on link failure
  821. */
  822. static ssize_t bonding_show_resend_igmp(struct device *d,
  823. struct device_attribute *attr,
  824. char *buf)
  825. {
  826. struct bonding *bond = to_bond(d);
  827. return sprintf(buf, "%d\n", bond->params.resend_igmp);
  828. }
  829. static ssize_t bonding_store_resend_igmp(struct device *d,
  830. struct device_attribute *attr,
  831. const char *buf, size_t count)
  832. {
  833. struct bonding *bond = to_bond(d);
  834. int ret;
  835. ret = bond_opt_tryset_rtnl(bond, BOND_OPT_RESEND_IGMP, (char *)buf);
  836. if (!ret)
  837. ret = count;
  838. return ret;
  839. }
  840. static DEVICE_ATTR(resend_igmp, S_IRUGO | S_IWUSR,
  841. bonding_show_resend_igmp, bonding_store_resend_igmp);
  842. static ssize_t bonding_show_lp_interval(struct device *d,
  843. struct device_attribute *attr,
  844. char *buf)
  845. {
  846. struct bonding *bond = to_bond(d);
  847. return sprintf(buf, "%d\n", bond->params.lp_interval);
  848. }
  849. static ssize_t bonding_store_lp_interval(struct device *d,
  850. struct device_attribute *attr,
  851. const char *buf, size_t count)
  852. {
  853. struct bonding *bond = to_bond(d);
  854. int ret;
  855. ret = bond_opt_tryset_rtnl(bond, BOND_OPT_LP_INTERVAL, (char *)buf);
  856. if (!ret)
  857. ret = count;
  858. return ret;
  859. }
  860. static DEVICE_ATTR(lp_interval, S_IRUGO | S_IWUSR,
  861. bonding_show_lp_interval, bonding_store_lp_interval);
  862. static ssize_t bonding_show_packets_per_slave(struct device *d,
  863. struct device_attribute *attr,
  864. char *buf)
  865. {
  866. struct bonding *bond = to_bond(d);
  867. unsigned int packets_per_slave = bond->params.packets_per_slave;
  868. return sprintf(buf, "%u\n", packets_per_slave);
  869. }
  870. static ssize_t bonding_store_packets_per_slave(struct device *d,
  871. struct device_attribute *attr,
  872. const char *buf, size_t count)
  873. {
  874. struct bonding *bond = to_bond(d);
  875. int ret;
  876. ret = bond_opt_tryset_rtnl(bond, BOND_OPT_PACKETS_PER_SLAVE,
  877. (char *)buf);
  878. if (!ret)
  879. ret = count;
  880. return ret;
  881. }
  882. static DEVICE_ATTR(packets_per_slave, S_IRUGO | S_IWUSR,
  883. bonding_show_packets_per_slave,
  884. bonding_store_packets_per_slave);
  885. static struct attribute *per_bond_attrs[] = {
  886. &dev_attr_slaves.attr,
  887. &dev_attr_mode.attr,
  888. &dev_attr_fail_over_mac.attr,
  889. &dev_attr_arp_validate.attr,
  890. &dev_attr_arp_all_targets.attr,
  891. &dev_attr_arp_interval.attr,
  892. &dev_attr_arp_ip_target.attr,
  893. &dev_attr_downdelay.attr,
  894. &dev_attr_updelay.attr,
  895. &dev_attr_lacp_rate.attr,
  896. &dev_attr_ad_select.attr,
  897. &dev_attr_xmit_hash_policy.attr,
  898. &dev_attr_num_grat_arp.attr,
  899. &dev_attr_num_unsol_na.attr,
  900. &dev_attr_miimon.attr,
  901. &dev_attr_primary.attr,
  902. &dev_attr_primary_reselect.attr,
  903. &dev_attr_use_carrier.attr,
  904. &dev_attr_active_slave.attr,
  905. &dev_attr_mii_status.attr,
  906. &dev_attr_ad_aggregator.attr,
  907. &dev_attr_ad_num_ports.attr,
  908. &dev_attr_ad_actor_key.attr,
  909. &dev_attr_ad_partner_key.attr,
  910. &dev_attr_ad_partner_mac.attr,
  911. &dev_attr_queue_id.attr,
  912. &dev_attr_all_slaves_active.attr,
  913. &dev_attr_resend_igmp.attr,
  914. &dev_attr_min_links.attr,
  915. &dev_attr_lp_interval.attr,
  916. &dev_attr_packets_per_slave.attr,
  917. NULL,
  918. };
  919. static struct attribute_group bonding_group = {
  920. .name = "bonding",
  921. .attrs = per_bond_attrs,
  922. };
  923. /*
  924. * Initialize sysfs. This sets up the bonding_masters file in
  925. * /sys/class/net.
  926. */
  927. int bond_create_sysfs(struct bond_net *bn)
  928. {
  929. int ret;
  930. bn->class_attr_bonding_masters = class_attr_bonding_masters;
  931. sysfs_attr_init(&bn->class_attr_bonding_masters.attr);
  932. ret = netdev_class_create_file_ns(&bn->class_attr_bonding_masters,
  933. bn->net);
  934. /*
  935. * Permit multiple loads of the module by ignoring failures to
  936. * create the bonding_masters sysfs file. Bonding devices
  937. * created by second or subsequent loads of the module will
  938. * not be listed in, or controllable by, bonding_masters, but
  939. * will have the usual "bonding" sysfs directory.
  940. *
  941. * This is done to preserve backwards compatibility for
  942. * initscripts/sysconfig, which load bonding multiple times to
  943. * configure multiple bonding devices.
  944. */
  945. if (ret == -EEXIST) {
  946. /* Is someone being kinky and naming a device bonding_master? */
  947. if (__dev_get_by_name(bn->net,
  948. class_attr_bonding_masters.attr.name))
  949. pr_err("network device named %s already exists in sysfs",
  950. class_attr_bonding_masters.attr.name);
  951. ret = 0;
  952. }
  953. return ret;
  954. }
  955. /*
  956. * Remove /sys/class/net/bonding_masters.
  957. */
  958. void bond_destroy_sysfs(struct bond_net *bn)
  959. {
  960. netdev_class_remove_file_ns(&bn->class_attr_bonding_masters, bn->net);
  961. }
  962. /*
  963. * Initialize sysfs for each bond. This sets up and registers
  964. * the 'bondctl' directory for each individual bond under /sys/class/net.
  965. */
  966. void bond_prepare_sysfs_group(struct bonding *bond)
  967. {
  968. bond->dev->sysfs_groups[0] = &bonding_group;
  969. }