bond_sysfs.c 23 KB

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