bond_sysfs.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831
  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 <net/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. /* "show" function for the bond_masters attribute.
  44. * The class parameter is ignored.
  45. */
  46. static ssize_t bonding_show_bonds(struct class *cls,
  47. struct class_attribute *attr,
  48. char *buf)
  49. {
  50. struct bond_net *bn =
  51. container_of(attr, struct bond_net, class_attr_bonding_masters);
  52. int res = 0;
  53. struct bonding *bond;
  54. rtnl_lock();
  55. list_for_each_entry(bond, &bn->dev_list, bond_list) {
  56. if (res > (PAGE_SIZE - IFNAMSIZ)) {
  57. /* not enough space for another interface name */
  58. if ((PAGE_SIZE - res) > 10)
  59. res = PAGE_SIZE - 10;
  60. res += sprintf(buf + res, "++more++ ");
  61. break;
  62. }
  63. res += sprintf(buf + res, "%s ", bond->dev->name);
  64. }
  65. if (res)
  66. buf[res-1] = '\n'; /* eat the leftover space */
  67. rtnl_unlock();
  68. return res;
  69. }
  70. static struct net_device *bond_get_by_name(struct bond_net *bn, const char *ifname)
  71. {
  72. struct bonding *bond;
  73. list_for_each_entry(bond, &bn->dev_list, bond_list) {
  74. if (strncmp(bond->dev->name, ifname, IFNAMSIZ) == 0)
  75. return bond->dev;
  76. }
  77. return NULL;
  78. }
  79. /* "store" function for the bond_masters attribute. This is what
  80. * creates and deletes entire bonds.
  81. *
  82. * The class parameter is ignored.
  83. */
  84. static ssize_t bonding_store_bonds(struct class *cls,
  85. struct class_attribute *attr,
  86. const char *buffer, size_t count)
  87. {
  88. struct bond_net *bn =
  89. container_of(attr, struct bond_net, class_attr_bonding_masters);
  90. char command[IFNAMSIZ + 1] = {0, };
  91. char *ifname;
  92. int rv, res = count;
  93. sscanf(buffer, "%16s", command); /* IFNAMSIZ*/
  94. ifname = command + 1;
  95. if ((strlen(command) <= 1) ||
  96. !dev_valid_name(ifname))
  97. goto err_no_cmd;
  98. if (command[0] == '+') {
  99. pr_info("%s is being created...\n", ifname);
  100. rv = bond_create(bn->net, ifname);
  101. if (rv) {
  102. if (rv == -EEXIST)
  103. pr_info("%s already exists\n", ifname);
  104. else
  105. pr_info("%s creation failed\n", ifname);
  106. res = rv;
  107. }
  108. } else if (command[0] == '-') {
  109. struct net_device *bond_dev;
  110. rtnl_lock();
  111. bond_dev = bond_get_by_name(bn, ifname);
  112. if (bond_dev) {
  113. pr_info("%s is being deleted...\n", ifname);
  114. unregister_netdevice(bond_dev);
  115. } else {
  116. pr_err("unable to delete non-existent %s\n", ifname);
  117. res = -ENODEV;
  118. }
  119. rtnl_unlock();
  120. } else
  121. goto err_no_cmd;
  122. /* Always return either count or an error. If you return 0, you'll
  123. * get called forever, which is bad.
  124. */
  125. return res;
  126. err_no_cmd:
  127. pr_err("no command found in bonding_masters - use +ifname or -ifname\n");
  128. return -EPERM;
  129. }
  130. /* class attribute for bond_masters file. This ends up in /sys/class/net */
  131. static const struct class_attribute class_attr_bonding_masters = {
  132. .attr = {
  133. .name = "bonding_masters",
  134. .mode = S_IWUSR | S_IRUGO,
  135. },
  136. .show = bonding_show_bonds,
  137. .store = bonding_store_bonds,
  138. };
  139. /* Generic "store" method for bonding sysfs option setting */
  140. static ssize_t bonding_sysfs_store_option(struct device *d,
  141. struct device_attribute *attr,
  142. const char *buffer, size_t count)
  143. {
  144. struct bonding *bond = to_bond(d);
  145. const struct bond_option *opt;
  146. int ret;
  147. opt = bond_opt_get_by_name(attr->attr.name);
  148. if (WARN_ON(!opt))
  149. return -ENOENT;
  150. ret = bond_opt_tryset_rtnl(bond, opt->id, (char *)buffer);
  151. if (!ret)
  152. ret = count;
  153. return ret;
  154. }
  155. /* Show the slaves in the current bond. */
  156. static ssize_t bonding_show_slaves(struct device *d,
  157. struct device_attribute *attr, char *buf)
  158. {
  159. struct bonding *bond = to_bond(d);
  160. struct list_head *iter;
  161. struct slave *slave;
  162. int res = 0;
  163. if (!rtnl_trylock())
  164. return restart_syscall();
  165. bond_for_each_slave(bond, slave, iter) {
  166. if (res > (PAGE_SIZE - IFNAMSIZ)) {
  167. /* not enough space for another interface name */
  168. if ((PAGE_SIZE - res) > 10)
  169. res = PAGE_SIZE - 10;
  170. res += sprintf(buf + res, "++more++ ");
  171. break;
  172. }
  173. res += sprintf(buf + res, "%s ", slave->dev->name);
  174. }
  175. rtnl_unlock();
  176. if (res)
  177. buf[res-1] = '\n'; /* eat the leftover space */
  178. return res;
  179. }
  180. static DEVICE_ATTR(slaves, S_IRUGO | S_IWUSR, bonding_show_slaves,
  181. bonding_sysfs_store_option);
  182. /* Show the bonding mode. */
  183. static ssize_t bonding_show_mode(struct device *d,
  184. struct device_attribute *attr, char *buf)
  185. {
  186. struct bonding *bond = to_bond(d);
  187. const struct bond_opt_value *val;
  188. val = bond_opt_get_val(BOND_OPT_MODE, BOND_MODE(bond));
  189. return sprintf(buf, "%s %d\n", val->string, BOND_MODE(bond));
  190. }
  191. static DEVICE_ATTR(mode, S_IRUGO | S_IWUSR,
  192. bonding_show_mode, bonding_sysfs_store_option);
  193. /* Show the bonding transmit hash method. */
  194. static ssize_t bonding_show_xmit_hash(struct device *d,
  195. struct device_attribute *attr,
  196. char *buf)
  197. {
  198. struct bonding *bond = to_bond(d);
  199. const struct bond_opt_value *val;
  200. val = bond_opt_get_val(BOND_OPT_XMIT_HASH, bond->params.xmit_policy);
  201. return sprintf(buf, "%s %d\n", val->string, bond->params.xmit_policy);
  202. }
  203. static DEVICE_ATTR(xmit_hash_policy, S_IRUGO | S_IWUSR,
  204. bonding_show_xmit_hash, bonding_sysfs_store_option);
  205. /* Show arp_validate. */
  206. static ssize_t bonding_show_arp_validate(struct device *d,
  207. struct device_attribute *attr,
  208. char *buf)
  209. {
  210. struct bonding *bond = to_bond(d);
  211. const struct bond_opt_value *val;
  212. val = bond_opt_get_val(BOND_OPT_ARP_VALIDATE,
  213. bond->params.arp_validate);
  214. return sprintf(buf, "%s %d\n", val->string, bond->params.arp_validate);
  215. }
  216. static DEVICE_ATTR(arp_validate, S_IRUGO | S_IWUSR, bonding_show_arp_validate,
  217. bonding_sysfs_store_option);
  218. /* Show arp_all_targets. */
  219. static ssize_t bonding_show_arp_all_targets(struct device *d,
  220. struct device_attribute *attr,
  221. char *buf)
  222. {
  223. struct bonding *bond = to_bond(d);
  224. const struct bond_opt_value *val;
  225. val = bond_opt_get_val(BOND_OPT_ARP_ALL_TARGETS,
  226. bond->params.arp_all_targets);
  227. return sprintf(buf, "%s %d\n",
  228. val->string, bond->params.arp_all_targets);
  229. }
  230. static DEVICE_ATTR(arp_all_targets, S_IRUGO | S_IWUSR,
  231. bonding_show_arp_all_targets, bonding_sysfs_store_option);
  232. /* Show fail_over_mac. */
  233. static ssize_t bonding_show_fail_over_mac(struct device *d,
  234. struct device_attribute *attr,
  235. char *buf)
  236. {
  237. struct bonding *bond = to_bond(d);
  238. const struct bond_opt_value *val;
  239. val = bond_opt_get_val(BOND_OPT_FAIL_OVER_MAC,
  240. bond->params.fail_over_mac);
  241. return sprintf(buf, "%s %d\n", val->string, bond->params.fail_over_mac);
  242. }
  243. static DEVICE_ATTR(fail_over_mac, S_IRUGO | S_IWUSR,
  244. bonding_show_fail_over_mac, bonding_sysfs_store_option);
  245. /* Show the arp timer interval. */
  246. static ssize_t bonding_show_arp_interval(struct device *d,
  247. struct device_attribute *attr,
  248. char *buf)
  249. {
  250. struct bonding *bond = to_bond(d);
  251. return sprintf(buf, "%d\n", bond->params.arp_interval);
  252. }
  253. static DEVICE_ATTR(arp_interval, S_IRUGO | S_IWUSR,
  254. bonding_show_arp_interval, bonding_sysfs_store_option);
  255. /* Show the arp targets. */
  256. static ssize_t bonding_show_arp_targets(struct device *d,
  257. struct device_attribute *attr,
  258. char *buf)
  259. {
  260. struct bonding *bond = to_bond(d);
  261. int i, res = 0;
  262. for (i = 0; i < BOND_MAX_ARP_TARGETS; i++) {
  263. if (bond->params.arp_targets[i])
  264. res += sprintf(buf + res, "%pI4 ",
  265. &bond->params.arp_targets[i]);
  266. }
  267. if (res)
  268. buf[res-1] = '\n'; /* eat the leftover space */
  269. return res;
  270. }
  271. static DEVICE_ATTR(arp_ip_target, S_IRUGO | S_IWUSR,
  272. bonding_show_arp_targets, bonding_sysfs_store_option);
  273. /* Show the up and down delays. */
  274. static ssize_t bonding_show_downdelay(struct device *d,
  275. struct device_attribute *attr,
  276. char *buf)
  277. {
  278. struct bonding *bond = to_bond(d);
  279. return sprintf(buf, "%d\n", bond->params.downdelay * bond->params.miimon);
  280. }
  281. static DEVICE_ATTR(downdelay, S_IRUGO | S_IWUSR,
  282. bonding_show_downdelay, bonding_sysfs_store_option);
  283. static ssize_t bonding_show_updelay(struct device *d,
  284. struct device_attribute *attr,
  285. char *buf)
  286. {
  287. struct bonding *bond = to_bond(d);
  288. return sprintf(buf, "%d\n", bond->params.updelay * bond->params.miimon);
  289. }
  290. static DEVICE_ATTR(updelay, S_IRUGO | S_IWUSR,
  291. bonding_show_updelay, bonding_sysfs_store_option);
  292. /* Show the LACP interval. */
  293. static ssize_t bonding_show_lacp(struct device *d,
  294. struct device_attribute *attr,
  295. char *buf)
  296. {
  297. struct bonding *bond = to_bond(d);
  298. const struct bond_opt_value *val;
  299. val = bond_opt_get_val(BOND_OPT_LACP_RATE, bond->params.lacp_fast);
  300. return sprintf(buf, "%s %d\n", val->string, bond->params.lacp_fast);
  301. }
  302. static DEVICE_ATTR(lacp_rate, S_IRUGO | S_IWUSR,
  303. bonding_show_lacp, bonding_sysfs_store_option);
  304. static ssize_t bonding_show_min_links(struct device *d,
  305. struct device_attribute *attr,
  306. char *buf)
  307. {
  308. struct bonding *bond = to_bond(d);
  309. return sprintf(buf, "%u\n", bond->params.min_links);
  310. }
  311. static DEVICE_ATTR(min_links, S_IRUGO | S_IWUSR,
  312. bonding_show_min_links, bonding_sysfs_store_option);
  313. static ssize_t bonding_show_ad_select(struct device *d,
  314. struct device_attribute *attr,
  315. char *buf)
  316. {
  317. struct bonding *bond = to_bond(d);
  318. const struct bond_opt_value *val;
  319. val = bond_opt_get_val(BOND_OPT_AD_SELECT, bond->params.ad_select);
  320. return sprintf(buf, "%s %d\n", val->string, bond->params.ad_select);
  321. }
  322. static DEVICE_ATTR(ad_select, S_IRUGO | S_IWUSR,
  323. bonding_show_ad_select, bonding_sysfs_store_option);
  324. /* Show and set the number of peer notifications to send after a failover event. */
  325. static ssize_t bonding_show_num_peer_notif(struct device *d,
  326. struct device_attribute *attr,
  327. char *buf)
  328. {
  329. struct bonding *bond = to_bond(d);
  330. return sprintf(buf, "%d\n", bond->params.num_peer_notif);
  331. }
  332. static ssize_t bonding_store_num_peer_notif(struct device *d,
  333. struct device_attribute *attr,
  334. const char *buf, size_t count)
  335. {
  336. struct bonding *bond = to_bond(d);
  337. int ret;
  338. ret = bond_opt_tryset_rtnl(bond, BOND_OPT_NUM_PEER_NOTIF, (char *)buf);
  339. if (!ret)
  340. ret = count;
  341. return ret;
  342. }
  343. static DEVICE_ATTR(num_grat_arp, S_IRUGO | S_IWUSR,
  344. bonding_show_num_peer_notif, bonding_store_num_peer_notif);
  345. static DEVICE_ATTR(num_unsol_na, S_IRUGO | S_IWUSR,
  346. bonding_show_num_peer_notif, bonding_store_num_peer_notif);
  347. /* Show the MII monitor interval. */
  348. static ssize_t bonding_show_miimon(struct device *d,
  349. struct device_attribute *attr,
  350. char *buf)
  351. {
  352. struct bonding *bond = to_bond(d);
  353. return sprintf(buf, "%d\n", bond->params.miimon);
  354. }
  355. static DEVICE_ATTR(miimon, S_IRUGO | S_IWUSR,
  356. bonding_show_miimon, bonding_sysfs_store_option);
  357. /* Show the primary slave. */
  358. static ssize_t bonding_show_primary(struct device *d,
  359. struct device_attribute *attr,
  360. char *buf)
  361. {
  362. struct bonding *bond = to_bond(d);
  363. struct slave *primary;
  364. int count = 0;
  365. rcu_read_lock();
  366. primary = rcu_dereference(bond->primary_slave);
  367. if (primary)
  368. count = sprintf(buf, "%s\n", primary->dev->name);
  369. rcu_read_unlock();
  370. return count;
  371. }
  372. static DEVICE_ATTR(primary, S_IRUGO | S_IWUSR,
  373. bonding_show_primary, bonding_sysfs_store_option);
  374. /* Show the primary_reselect flag. */
  375. static ssize_t bonding_show_primary_reselect(struct device *d,
  376. struct device_attribute *attr,
  377. char *buf)
  378. {
  379. struct bonding *bond = to_bond(d);
  380. const struct bond_opt_value *val;
  381. val = bond_opt_get_val(BOND_OPT_PRIMARY_RESELECT,
  382. bond->params.primary_reselect);
  383. return sprintf(buf, "%s %d\n",
  384. val->string, bond->params.primary_reselect);
  385. }
  386. static DEVICE_ATTR(primary_reselect, S_IRUGO | S_IWUSR,
  387. bonding_show_primary_reselect, bonding_sysfs_store_option);
  388. /* Show the use_carrier flag. */
  389. static ssize_t bonding_show_carrier(struct device *d,
  390. struct device_attribute *attr,
  391. char *buf)
  392. {
  393. struct bonding *bond = to_bond(d);
  394. return sprintf(buf, "%d\n", bond->params.use_carrier);
  395. }
  396. static DEVICE_ATTR(use_carrier, S_IRUGO | S_IWUSR,
  397. bonding_show_carrier, bonding_sysfs_store_option);
  398. /* Show currently active_slave. */
  399. static ssize_t bonding_show_active_slave(struct device *d,
  400. struct device_attribute *attr,
  401. char *buf)
  402. {
  403. struct bonding *bond = to_bond(d);
  404. struct net_device *slave_dev;
  405. int count = 0;
  406. rcu_read_lock();
  407. slave_dev = bond_option_active_slave_get_rcu(bond);
  408. if (slave_dev)
  409. count = sprintf(buf, "%s\n", slave_dev->name);
  410. rcu_read_unlock();
  411. return count;
  412. }
  413. static DEVICE_ATTR(active_slave, S_IRUGO | S_IWUSR,
  414. bonding_show_active_slave, bonding_sysfs_store_option);
  415. /* Show link status of the bond interface. */
  416. static ssize_t bonding_show_mii_status(struct device *d,
  417. struct device_attribute *attr,
  418. char *buf)
  419. {
  420. struct bonding *bond = to_bond(d);
  421. bool active = !!rcu_access_pointer(bond->curr_active_slave);
  422. return sprintf(buf, "%s\n", active ? "up" : "down");
  423. }
  424. static DEVICE_ATTR(mii_status, S_IRUGO, bonding_show_mii_status, NULL);
  425. /* Show current 802.3ad aggregator ID. */
  426. static ssize_t bonding_show_ad_aggregator(struct device *d,
  427. struct device_attribute *attr,
  428. char *buf)
  429. {
  430. int count = 0;
  431. struct bonding *bond = to_bond(d);
  432. if (BOND_MODE(bond) == BOND_MODE_8023AD) {
  433. struct ad_info ad_info;
  434. count = sprintf(buf, "%d\n",
  435. bond_3ad_get_active_agg_info(bond, &ad_info)
  436. ? 0 : ad_info.aggregator_id);
  437. }
  438. return count;
  439. }
  440. static DEVICE_ATTR(ad_aggregator, S_IRUGO, bonding_show_ad_aggregator, NULL);
  441. /* Show number of active 802.3ad ports. */
  442. static ssize_t bonding_show_ad_num_ports(struct device *d,
  443. struct device_attribute *attr,
  444. char *buf)
  445. {
  446. int count = 0;
  447. struct bonding *bond = to_bond(d);
  448. if (BOND_MODE(bond) == BOND_MODE_8023AD) {
  449. struct ad_info ad_info;
  450. count = sprintf(buf, "%d\n",
  451. bond_3ad_get_active_agg_info(bond, &ad_info)
  452. ? 0 : ad_info.ports);
  453. }
  454. return count;
  455. }
  456. static DEVICE_ATTR(ad_num_ports, S_IRUGO, bonding_show_ad_num_ports, NULL);
  457. /* Show current 802.3ad actor key. */
  458. static ssize_t bonding_show_ad_actor_key(struct device *d,
  459. struct device_attribute *attr,
  460. char *buf)
  461. {
  462. int count = 0;
  463. struct bonding *bond = to_bond(d);
  464. if (BOND_MODE(bond) == BOND_MODE_8023AD && capable(CAP_NET_ADMIN)) {
  465. struct ad_info ad_info;
  466. count = sprintf(buf, "%d\n",
  467. bond_3ad_get_active_agg_info(bond, &ad_info)
  468. ? 0 : ad_info.actor_key);
  469. }
  470. return count;
  471. }
  472. static DEVICE_ATTR(ad_actor_key, S_IRUGO, bonding_show_ad_actor_key, NULL);
  473. /* Show current 802.3ad partner key. */
  474. static ssize_t bonding_show_ad_partner_key(struct device *d,
  475. struct device_attribute *attr,
  476. char *buf)
  477. {
  478. int count = 0;
  479. struct bonding *bond = to_bond(d);
  480. if (BOND_MODE(bond) == BOND_MODE_8023AD && capable(CAP_NET_ADMIN)) {
  481. struct ad_info ad_info;
  482. count = sprintf(buf, "%d\n",
  483. bond_3ad_get_active_agg_info(bond, &ad_info)
  484. ? 0 : ad_info.partner_key);
  485. }
  486. return count;
  487. }
  488. static DEVICE_ATTR(ad_partner_key, S_IRUGO, bonding_show_ad_partner_key, NULL);
  489. /* Show current 802.3ad partner mac. */
  490. static ssize_t bonding_show_ad_partner_mac(struct device *d,
  491. struct device_attribute *attr,
  492. char *buf)
  493. {
  494. int count = 0;
  495. struct bonding *bond = to_bond(d);
  496. if (BOND_MODE(bond) == BOND_MODE_8023AD && capable(CAP_NET_ADMIN)) {
  497. struct ad_info ad_info;
  498. if (!bond_3ad_get_active_agg_info(bond, &ad_info))
  499. count = sprintf(buf, "%pM\n", ad_info.partner_system);
  500. }
  501. return count;
  502. }
  503. static DEVICE_ATTR(ad_partner_mac, S_IRUGO, bonding_show_ad_partner_mac, NULL);
  504. /* Show the queue_ids of the slaves in the current bond. */
  505. static ssize_t bonding_show_queue_id(struct device *d,
  506. struct device_attribute *attr,
  507. char *buf)
  508. {
  509. struct bonding *bond = to_bond(d);
  510. struct list_head *iter;
  511. struct slave *slave;
  512. int res = 0;
  513. if (!rtnl_trylock())
  514. return restart_syscall();
  515. bond_for_each_slave(bond, slave, iter) {
  516. if (res > (PAGE_SIZE - IFNAMSIZ - 6)) {
  517. /* not enough space for another interface_name:queue_id pair */
  518. if ((PAGE_SIZE - res) > 10)
  519. res = PAGE_SIZE - 10;
  520. res += sprintf(buf + res, "++more++ ");
  521. break;
  522. }
  523. res += sprintf(buf + res, "%s:%d ",
  524. slave->dev->name, slave->queue_id);
  525. }
  526. if (res)
  527. buf[res-1] = '\n'; /* eat the leftover space */
  528. rtnl_unlock();
  529. return res;
  530. }
  531. static DEVICE_ATTR(queue_id, S_IRUGO | S_IWUSR, bonding_show_queue_id,
  532. bonding_sysfs_store_option);
  533. /* Show the all_slaves_active flag. */
  534. static ssize_t bonding_show_slaves_active(struct device *d,
  535. struct device_attribute *attr,
  536. char *buf)
  537. {
  538. struct bonding *bond = to_bond(d);
  539. return sprintf(buf, "%d\n", bond->params.all_slaves_active);
  540. }
  541. static DEVICE_ATTR(all_slaves_active, S_IRUGO | S_IWUSR,
  542. bonding_show_slaves_active, bonding_sysfs_store_option);
  543. /* Show the number of IGMP membership reports to send on link failure */
  544. static ssize_t bonding_show_resend_igmp(struct device *d,
  545. struct device_attribute *attr,
  546. char *buf)
  547. {
  548. struct bonding *bond = to_bond(d);
  549. return sprintf(buf, "%d\n", bond->params.resend_igmp);
  550. }
  551. static DEVICE_ATTR(resend_igmp, S_IRUGO | S_IWUSR,
  552. bonding_show_resend_igmp, bonding_sysfs_store_option);
  553. static ssize_t bonding_show_lp_interval(struct device *d,
  554. struct device_attribute *attr,
  555. char *buf)
  556. {
  557. struct bonding *bond = to_bond(d);
  558. return sprintf(buf, "%d\n", bond->params.lp_interval);
  559. }
  560. static DEVICE_ATTR(lp_interval, S_IRUGO | S_IWUSR,
  561. bonding_show_lp_interval, bonding_sysfs_store_option);
  562. static ssize_t bonding_show_tlb_dynamic_lb(struct device *d,
  563. struct device_attribute *attr,
  564. char *buf)
  565. {
  566. struct bonding *bond = to_bond(d);
  567. return sprintf(buf, "%d\n", bond->params.tlb_dynamic_lb);
  568. }
  569. static DEVICE_ATTR(tlb_dynamic_lb, S_IRUGO | S_IWUSR,
  570. bonding_show_tlb_dynamic_lb, bonding_sysfs_store_option);
  571. static ssize_t bonding_show_packets_per_slave(struct device *d,
  572. struct device_attribute *attr,
  573. char *buf)
  574. {
  575. struct bonding *bond = to_bond(d);
  576. unsigned int packets_per_slave = bond->params.packets_per_slave;
  577. return sprintf(buf, "%u\n", packets_per_slave);
  578. }
  579. static DEVICE_ATTR(packets_per_slave, S_IRUGO | S_IWUSR,
  580. bonding_show_packets_per_slave, bonding_sysfs_store_option);
  581. static ssize_t bonding_show_ad_actor_sys_prio(struct device *d,
  582. struct device_attribute *attr,
  583. char *buf)
  584. {
  585. struct bonding *bond = to_bond(d);
  586. if (BOND_MODE(bond) == BOND_MODE_8023AD && capable(CAP_NET_ADMIN))
  587. return sprintf(buf, "%hu\n", bond->params.ad_actor_sys_prio);
  588. return 0;
  589. }
  590. static DEVICE_ATTR(ad_actor_sys_prio, S_IRUGO | S_IWUSR,
  591. bonding_show_ad_actor_sys_prio, bonding_sysfs_store_option);
  592. static ssize_t bonding_show_ad_actor_system(struct device *d,
  593. struct device_attribute *attr,
  594. char *buf)
  595. {
  596. struct bonding *bond = to_bond(d);
  597. if (BOND_MODE(bond) == BOND_MODE_8023AD && capable(CAP_NET_ADMIN))
  598. return sprintf(buf, "%pM\n", bond->params.ad_actor_system);
  599. return 0;
  600. }
  601. static DEVICE_ATTR(ad_actor_system, S_IRUGO | S_IWUSR,
  602. bonding_show_ad_actor_system, bonding_sysfs_store_option);
  603. static ssize_t bonding_show_ad_user_port_key(struct device *d,
  604. struct device_attribute *attr,
  605. char *buf)
  606. {
  607. struct bonding *bond = to_bond(d);
  608. if (BOND_MODE(bond) == BOND_MODE_8023AD && capable(CAP_NET_ADMIN))
  609. return sprintf(buf, "%hu\n", bond->params.ad_user_port_key);
  610. return 0;
  611. }
  612. static DEVICE_ATTR(ad_user_port_key, S_IRUGO | S_IWUSR,
  613. bonding_show_ad_user_port_key, bonding_sysfs_store_option);
  614. static struct attribute *per_bond_attrs[] = {
  615. &dev_attr_slaves.attr,
  616. &dev_attr_mode.attr,
  617. &dev_attr_fail_over_mac.attr,
  618. &dev_attr_arp_validate.attr,
  619. &dev_attr_arp_all_targets.attr,
  620. &dev_attr_arp_interval.attr,
  621. &dev_attr_arp_ip_target.attr,
  622. &dev_attr_downdelay.attr,
  623. &dev_attr_updelay.attr,
  624. &dev_attr_lacp_rate.attr,
  625. &dev_attr_ad_select.attr,
  626. &dev_attr_xmit_hash_policy.attr,
  627. &dev_attr_num_grat_arp.attr,
  628. &dev_attr_num_unsol_na.attr,
  629. &dev_attr_miimon.attr,
  630. &dev_attr_primary.attr,
  631. &dev_attr_primary_reselect.attr,
  632. &dev_attr_use_carrier.attr,
  633. &dev_attr_active_slave.attr,
  634. &dev_attr_mii_status.attr,
  635. &dev_attr_ad_aggregator.attr,
  636. &dev_attr_ad_num_ports.attr,
  637. &dev_attr_ad_actor_key.attr,
  638. &dev_attr_ad_partner_key.attr,
  639. &dev_attr_ad_partner_mac.attr,
  640. &dev_attr_queue_id.attr,
  641. &dev_attr_all_slaves_active.attr,
  642. &dev_attr_resend_igmp.attr,
  643. &dev_attr_min_links.attr,
  644. &dev_attr_lp_interval.attr,
  645. &dev_attr_packets_per_slave.attr,
  646. &dev_attr_tlb_dynamic_lb.attr,
  647. &dev_attr_ad_actor_sys_prio.attr,
  648. &dev_attr_ad_actor_system.attr,
  649. &dev_attr_ad_user_port_key.attr,
  650. NULL,
  651. };
  652. static struct attribute_group bonding_group = {
  653. .name = "bonding",
  654. .attrs = per_bond_attrs,
  655. };
  656. /* Initialize sysfs. This sets up the bonding_masters file in
  657. * /sys/class/net.
  658. */
  659. int bond_create_sysfs(struct bond_net *bn)
  660. {
  661. int ret;
  662. bn->class_attr_bonding_masters = class_attr_bonding_masters;
  663. sysfs_attr_init(&bn->class_attr_bonding_masters.attr);
  664. ret = netdev_class_create_file_ns(&bn->class_attr_bonding_masters,
  665. bn->net);
  666. /* Permit multiple loads of the module by ignoring failures to
  667. * create the bonding_masters sysfs file. Bonding devices
  668. * created by second or subsequent loads of the module will
  669. * not be listed in, or controllable by, bonding_masters, but
  670. * will have the usual "bonding" sysfs directory.
  671. *
  672. * This is done to preserve backwards compatibility for
  673. * initscripts/sysconfig, which load bonding multiple times to
  674. * configure multiple bonding devices.
  675. */
  676. if (ret == -EEXIST) {
  677. /* Is someone being kinky and naming a device bonding_master? */
  678. if (__dev_get_by_name(bn->net,
  679. class_attr_bonding_masters.attr.name))
  680. pr_err("network device named %s already exists in sysfs\n",
  681. class_attr_bonding_masters.attr.name);
  682. ret = 0;
  683. }
  684. return ret;
  685. }
  686. /* Remove /sys/class/net/bonding_masters. */
  687. void bond_destroy_sysfs(struct bond_net *bn)
  688. {
  689. netdev_class_remove_file_ns(&bn->class_attr_bonding_masters, bn->net);
  690. }
  691. /* Initialize sysfs for each bond. This sets up and registers
  692. * the 'bondctl' directory for each individual bond under /sys/class/net.
  693. */
  694. void bond_prepare_sysfs_group(struct bonding *bond)
  695. {
  696. bond->dev->sysfs_groups[0] = &bonding_group;
  697. }