bond_sysfs.c 23 KB

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