bond_sysfs.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848
  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. /* use_carrier is obsolete, but print value for compatibility */
  395. static ssize_t bonding_show_carrier(struct device *d,
  396. struct device_attribute *attr,
  397. char *buf)
  398. {
  399. return sysfs_emit(buf, "1\n");
  400. }
  401. static DEVICE_ATTR(use_carrier, 0644,
  402. bonding_show_carrier, bonding_sysfs_store_option);
  403. /* Show currently active_slave. */
  404. static ssize_t bonding_show_active_slave(struct device *d,
  405. struct device_attribute *attr,
  406. char *buf)
  407. {
  408. struct bonding *bond = to_bond(d);
  409. struct net_device *slave_dev;
  410. int count = 0;
  411. rcu_read_lock();
  412. slave_dev = bond_option_active_slave_get_rcu(bond);
  413. if (slave_dev)
  414. count = sysfs_emit(buf, "%s\n", slave_dev->name);
  415. rcu_read_unlock();
  416. return count;
  417. }
  418. static DEVICE_ATTR(active_slave, 0644,
  419. bonding_show_active_slave, bonding_sysfs_store_option);
  420. /* Show link status of the bond interface. */
  421. static ssize_t bonding_show_mii_status(struct device *d,
  422. struct device_attribute *attr,
  423. char *buf)
  424. {
  425. struct bonding *bond = to_bond(d);
  426. bool active = netif_carrier_ok(bond->dev);
  427. return sysfs_emit(buf, "%s\n", active ? "up" : "down");
  428. }
  429. static DEVICE_ATTR(mii_status, 0444, bonding_show_mii_status, NULL);
  430. /* Show current 802.3ad aggregator ID. */
  431. static ssize_t bonding_show_ad_aggregator(struct device *d,
  432. struct device_attribute *attr,
  433. char *buf)
  434. {
  435. int count = 0;
  436. struct bonding *bond = to_bond(d);
  437. if (BOND_MODE(bond) == BOND_MODE_8023AD) {
  438. struct ad_info ad_info;
  439. count = sysfs_emit(buf, "%d\n",
  440. bond_3ad_get_active_agg_info(bond, &ad_info)
  441. ? 0 : ad_info.aggregator_id);
  442. }
  443. return count;
  444. }
  445. static DEVICE_ATTR(ad_aggregator, 0444, bonding_show_ad_aggregator, NULL);
  446. /* Show number of active 802.3ad ports. */
  447. static ssize_t bonding_show_ad_num_ports(struct device *d,
  448. struct device_attribute *attr,
  449. char *buf)
  450. {
  451. int count = 0;
  452. struct bonding *bond = to_bond(d);
  453. if (BOND_MODE(bond) == BOND_MODE_8023AD) {
  454. struct ad_info ad_info;
  455. count = sysfs_emit(buf, "%d\n",
  456. bond_3ad_get_active_agg_info(bond, &ad_info)
  457. ? 0 : ad_info.ports);
  458. }
  459. return count;
  460. }
  461. static DEVICE_ATTR(ad_num_ports, 0444, bonding_show_ad_num_ports, NULL);
  462. /* Show current 802.3ad actor key. */
  463. static ssize_t bonding_show_ad_actor_key(struct device *d,
  464. struct device_attribute *attr,
  465. char *buf)
  466. {
  467. int count = 0;
  468. struct bonding *bond = to_bond(d);
  469. if (BOND_MODE(bond) == BOND_MODE_8023AD && capable(CAP_NET_ADMIN)) {
  470. struct ad_info ad_info;
  471. count = sysfs_emit(buf, "%d\n",
  472. bond_3ad_get_active_agg_info(bond, &ad_info)
  473. ? 0 : ad_info.actor_key);
  474. }
  475. return count;
  476. }
  477. static DEVICE_ATTR(ad_actor_key, 0444, bonding_show_ad_actor_key, NULL);
  478. /* Show current 802.3ad partner key. */
  479. static ssize_t bonding_show_ad_partner_key(struct device *d,
  480. struct device_attribute *attr,
  481. char *buf)
  482. {
  483. int count = 0;
  484. struct bonding *bond = to_bond(d);
  485. if (BOND_MODE(bond) == BOND_MODE_8023AD && capable(CAP_NET_ADMIN)) {
  486. struct ad_info ad_info;
  487. count = sysfs_emit(buf, "%d\n",
  488. bond_3ad_get_active_agg_info(bond, &ad_info)
  489. ? 0 : ad_info.partner_key);
  490. }
  491. return count;
  492. }
  493. static DEVICE_ATTR(ad_partner_key, 0444, bonding_show_ad_partner_key, NULL);
  494. /* Show current 802.3ad partner mac. */
  495. static ssize_t bonding_show_ad_partner_mac(struct device *d,
  496. struct device_attribute *attr,
  497. char *buf)
  498. {
  499. int count = 0;
  500. struct bonding *bond = to_bond(d);
  501. if (BOND_MODE(bond) == BOND_MODE_8023AD && capable(CAP_NET_ADMIN)) {
  502. struct ad_info ad_info;
  503. if (!bond_3ad_get_active_agg_info(bond, &ad_info))
  504. count = sysfs_emit(buf, "%pM\n", ad_info.partner_system);
  505. }
  506. return count;
  507. }
  508. static DEVICE_ATTR(ad_partner_mac, 0444, bonding_show_ad_partner_mac, NULL);
  509. /* Show the queue_ids of the slaves in the current bond. */
  510. static ssize_t bonding_show_queue_id(struct device *d,
  511. struct device_attribute *attr,
  512. char *buf)
  513. {
  514. struct bonding *bond = to_bond(d);
  515. struct list_head *iter;
  516. struct slave *slave;
  517. int res = 0;
  518. rcu_read_lock();
  519. bond_for_each_slave_rcu(bond, slave, iter) {
  520. if (res > (PAGE_SIZE - IFNAMSIZ - 6)) {
  521. /* not enough space for another interface_name:queue_id pair */
  522. if ((PAGE_SIZE - res) > 10)
  523. res = PAGE_SIZE - 10;
  524. res += sysfs_emit_at(buf, res, "++more++ ");
  525. break;
  526. }
  527. res += sysfs_emit_at(buf, res, "%s:%d ",
  528. slave->dev->name,
  529. READ_ONCE(slave->queue_id));
  530. }
  531. if (res)
  532. buf[res-1] = '\n'; /* eat the leftover space */
  533. rcu_read_unlock();
  534. return res;
  535. }
  536. static DEVICE_ATTR(queue_id, 0644, bonding_show_queue_id,
  537. bonding_sysfs_store_option);
  538. /* Show the all_slaves_active flag. */
  539. static ssize_t bonding_show_slaves_active(struct device *d,
  540. struct device_attribute *attr,
  541. char *buf)
  542. {
  543. struct bonding *bond = to_bond(d);
  544. return sysfs_emit(buf, "%d\n", bond->params.all_slaves_active);
  545. }
  546. static DEVICE_ATTR(all_slaves_active, 0644,
  547. bonding_show_slaves_active, bonding_sysfs_store_option);
  548. /* Show the number of IGMP membership reports to send on link failure */
  549. static ssize_t bonding_show_resend_igmp(struct device *d,
  550. struct device_attribute *attr,
  551. char *buf)
  552. {
  553. struct bonding *bond = to_bond(d);
  554. return sysfs_emit(buf, "%d\n", bond->params.resend_igmp);
  555. }
  556. static DEVICE_ATTR(resend_igmp, 0644,
  557. bonding_show_resend_igmp, bonding_sysfs_store_option);
  558. static ssize_t bonding_show_lp_interval(struct device *d,
  559. struct device_attribute *attr,
  560. char *buf)
  561. {
  562. struct bonding *bond = to_bond(d);
  563. return sysfs_emit(buf, "%d\n", bond->params.lp_interval);
  564. }
  565. static DEVICE_ATTR(lp_interval, 0644,
  566. bonding_show_lp_interval, bonding_sysfs_store_option);
  567. static ssize_t bonding_show_tlb_dynamic_lb(struct device *d,
  568. struct device_attribute *attr,
  569. char *buf)
  570. {
  571. struct bonding *bond = to_bond(d);
  572. return sysfs_emit(buf, "%d\n", bond->params.tlb_dynamic_lb);
  573. }
  574. static DEVICE_ATTR(tlb_dynamic_lb, 0644,
  575. bonding_show_tlb_dynamic_lb, bonding_sysfs_store_option);
  576. static ssize_t bonding_show_packets_per_slave(struct device *d,
  577. struct device_attribute *attr,
  578. char *buf)
  579. {
  580. struct bonding *bond = to_bond(d);
  581. unsigned int packets_per_slave = bond->params.packets_per_slave;
  582. return sysfs_emit(buf, "%u\n", packets_per_slave);
  583. }
  584. static DEVICE_ATTR(packets_per_slave, 0644,
  585. bonding_show_packets_per_slave, bonding_sysfs_store_option);
  586. static ssize_t bonding_show_ad_actor_sys_prio(struct device *d,
  587. struct device_attribute *attr,
  588. char *buf)
  589. {
  590. struct bonding *bond = to_bond(d);
  591. if (BOND_MODE(bond) == BOND_MODE_8023AD && capable(CAP_NET_ADMIN))
  592. return sysfs_emit(buf, "%hu\n", bond->params.ad_actor_sys_prio);
  593. return 0;
  594. }
  595. static DEVICE_ATTR(ad_actor_sys_prio, 0644,
  596. bonding_show_ad_actor_sys_prio, bonding_sysfs_store_option);
  597. static ssize_t bonding_show_ad_actor_system(struct device *d,
  598. struct device_attribute *attr,
  599. char *buf)
  600. {
  601. struct bonding *bond = to_bond(d);
  602. if (BOND_MODE(bond) == BOND_MODE_8023AD && capable(CAP_NET_ADMIN))
  603. return sysfs_emit(buf, "%pM\n", bond->params.ad_actor_system);
  604. return 0;
  605. }
  606. static DEVICE_ATTR(ad_actor_system, 0644,
  607. bonding_show_ad_actor_system, bonding_sysfs_store_option);
  608. static ssize_t bonding_show_ad_user_port_key(struct device *d,
  609. struct device_attribute *attr,
  610. char *buf)
  611. {
  612. struct bonding *bond = to_bond(d);
  613. if (BOND_MODE(bond) == BOND_MODE_8023AD && capable(CAP_NET_ADMIN))
  614. return sysfs_emit(buf, "%hu\n", bond->params.ad_user_port_key);
  615. return 0;
  616. }
  617. static DEVICE_ATTR(ad_user_port_key, 0644,
  618. bonding_show_ad_user_port_key, bonding_sysfs_store_option);
  619. static struct attribute *per_bond_attrs[] = {
  620. &dev_attr_slaves.attr,
  621. &dev_attr_mode.attr,
  622. &dev_attr_fail_over_mac.attr,
  623. &dev_attr_arp_validate.attr,
  624. &dev_attr_arp_all_targets.attr,
  625. &dev_attr_arp_interval.attr,
  626. &dev_attr_arp_ip_target.attr,
  627. &dev_attr_downdelay.attr,
  628. &dev_attr_updelay.attr,
  629. &dev_attr_peer_notif_delay.attr,
  630. &dev_attr_lacp_active.attr,
  631. &dev_attr_lacp_rate.attr,
  632. &dev_attr_ad_select.attr,
  633. &dev_attr_xmit_hash_policy.attr,
  634. &dev_attr_num_grat_arp.attr,
  635. &dev_attr_num_unsol_na.attr,
  636. &dev_attr_miimon.attr,
  637. &dev_attr_primary.attr,
  638. &dev_attr_primary_reselect.attr,
  639. &dev_attr_use_carrier.attr,
  640. &dev_attr_active_slave.attr,
  641. &dev_attr_mii_status.attr,
  642. &dev_attr_ad_aggregator.attr,
  643. &dev_attr_ad_num_ports.attr,
  644. &dev_attr_ad_actor_key.attr,
  645. &dev_attr_ad_partner_key.attr,
  646. &dev_attr_ad_partner_mac.attr,
  647. &dev_attr_queue_id.attr,
  648. &dev_attr_all_slaves_active.attr,
  649. &dev_attr_resend_igmp.attr,
  650. &dev_attr_min_links.attr,
  651. &dev_attr_lp_interval.attr,
  652. &dev_attr_packets_per_slave.attr,
  653. &dev_attr_tlb_dynamic_lb.attr,
  654. &dev_attr_ad_actor_sys_prio.attr,
  655. &dev_attr_ad_actor_system.attr,
  656. &dev_attr_ad_user_port_key.attr,
  657. &dev_attr_arp_missed_max.attr,
  658. NULL,
  659. };
  660. static const struct attribute_group bonding_group = {
  661. .name = "bonding",
  662. .attrs = per_bond_attrs,
  663. };
  664. /* Initialize sysfs. This sets up the bonding_masters file in
  665. * /sys/class/net.
  666. */
  667. int __net_init bond_create_sysfs(struct bond_net *bn)
  668. {
  669. int ret;
  670. bn->class_attr_bonding_masters = class_attr_bonding_masters;
  671. sysfs_attr_init(&bn->class_attr_bonding_masters.attr);
  672. ret = netdev_class_create_file_ns(&bn->class_attr_bonding_masters,
  673. to_ns_common(bn->net));
  674. /* Permit multiple loads of the module by ignoring failures to
  675. * create the bonding_masters sysfs file. Bonding devices
  676. * created by second or subsequent loads of the module will
  677. * not be listed in, or controllable by, bonding_masters, but
  678. * will have the usual "bonding" sysfs directory.
  679. *
  680. * This is done to preserve backwards compatibility for
  681. * initscripts/sysconfig, which load bonding multiple times to
  682. * configure multiple bonding devices.
  683. */
  684. if (ret == -EEXIST) {
  685. /* Is someone being kinky and naming a device bonding_master? */
  686. if (netdev_name_in_use(bn->net,
  687. class_attr_bonding_masters.attr.name))
  688. pr_err("network device named %s already exists in sysfs\n",
  689. class_attr_bonding_masters.attr.name);
  690. ret = 0;
  691. }
  692. return ret;
  693. }
  694. /* Remove /sys/class/net/bonding_masters. */
  695. void __net_exit bond_destroy_sysfs(struct bond_net *bn)
  696. {
  697. netdev_class_remove_file_ns(&bn->class_attr_bonding_masters, to_ns_common(bn->net));
  698. }
  699. /* Initialize sysfs for each bond. This sets up and registers
  700. * the 'bondctl' directory for each individual bond under /sys/class/net.
  701. */
  702. void bond_prepare_sysfs_group(struct bonding *bond)
  703. {
  704. bond->dev->sysfs_groups[0] = &bonding_group;
  705. }