bond_sysfs.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851
  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(struct class *cls,
  30. struct class_attribute *attr,
  31. char *buf)
  32. {
  33. struct bond_net *bn =
  34. container_of(attr, struct bond_net, class_attr_bonding_masters);
  35. int res = 0;
  36. struct bonding *bond;
  37. rtnl_lock();
  38. list_for_each_entry(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. rtnl_unlock();
  51. return res;
  52. }
  53. static struct net_device *bond_get_by_name(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(struct class *cls,
  68. struct class_attribute *attr,
  69. const char *buffer, size_t count)
  70. {
  71. struct bond_net *bn =
  72. container_of(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. if (!rtnl_trylock())
  152. return restart_syscall();
  153. bond_for_each_slave(bond, slave, iter) {
  154. if (res > (PAGE_SIZE - IFNAMSIZ)) {
  155. /* not enough space for another interface name */
  156. if ((PAGE_SIZE - res) > 10)
  157. res = PAGE_SIZE - 10;
  158. res += sysfs_emit_at(buf, res, "++more++ ");
  159. break;
  160. }
  161. res += sysfs_emit_at(buf, res, "%s ", slave->dev->name);
  162. }
  163. rtnl_unlock();
  164. if (res)
  165. buf[res-1] = '\n'; /* eat the leftover space */
  166. return res;
  167. }
  168. static DEVICE_ATTR(slaves, 0644, bonding_show_slaves,
  169. bonding_sysfs_store_option);
  170. /* Show the bonding mode. */
  171. static ssize_t bonding_show_mode(struct device *d,
  172. struct device_attribute *attr, char *buf)
  173. {
  174. struct bonding *bond = to_bond(d);
  175. const struct bond_opt_value *val;
  176. val = bond_opt_get_val(BOND_OPT_MODE, BOND_MODE(bond));
  177. return sysfs_emit(buf, "%s %d\n", val->string, BOND_MODE(bond));
  178. }
  179. static DEVICE_ATTR(mode, 0644, bonding_show_mode, bonding_sysfs_store_option);
  180. /* Show the bonding transmit hash method. */
  181. static ssize_t bonding_show_xmit_hash(struct device *d,
  182. struct device_attribute *attr,
  183. 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_XMIT_HASH, bond->params.xmit_policy);
  188. return sysfs_emit(buf, "%s %d\n", val->string, bond->params.xmit_policy);
  189. }
  190. static DEVICE_ATTR(xmit_hash_policy, 0644,
  191. bonding_show_xmit_hash, bonding_sysfs_store_option);
  192. /* Show arp_validate. */
  193. static ssize_t bonding_show_arp_validate(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_ARP_VALIDATE,
  200. bond->params.arp_validate);
  201. return sysfs_emit(buf, "%s %d\n", val->string, bond->params.arp_validate);
  202. }
  203. static DEVICE_ATTR(arp_validate, 0644, bonding_show_arp_validate,
  204. bonding_sysfs_store_option);
  205. /* Show arp_all_targets. */
  206. static ssize_t bonding_show_arp_all_targets(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_ALL_TARGETS,
  213. bond->params.arp_all_targets);
  214. return sysfs_emit(buf, "%s %d\n",
  215. val->string, bond->params.arp_all_targets);
  216. }
  217. static DEVICE_ATTR(arp_all_targets, 0644,
  218. bonding_show_arp_all_targets, bonding_sysfs_store_option);
  219. /* Show fail_over_mac. */
  220. static ssize_t bonding_show_fail_over_mac(struct device *d,
  221. struct device_attribute *attr,
  222. char *buf)
  223. {
  224. struct bonding *bond = to_bond(d);
  225. const struct bond_opt_value *val;
  226. val = bond_opt_get_val(BOND_OPT_FAIL_OVER_MAC,
  227. bond->params.fail_over_mac);
  228. return sysfs_emit(buf, "%s %d\n", val->string, bond->params.fail_over_mac);
  229. }
  230. static DEVICE_ATTR(fail_over_mac, 0644,
  231. bonding_show_fail_over_mac, bonding_sysfs_store_option);
  232. /* Show the arp timer interval. */
  233. static ssize_t bonding_show_arp_interval(struct device *d,
  234. struct device_attribute *attr,
  235. char *buf)
  236. {
  237. struct bonding *bond = to_bond(d);
  238. return sysfs_emit(buf, "%d\n", bond->params.arp_interval);
  239. }
  240. static DEVICE_ATTR(arp_interval, 0644,
  241. bonding_show_arp_interval, bonding_sysfs_store_option);
  242. /* Show the arp targets. */
  243. static ssize_t bonding_show_arp_targets(struct device *d,
  244. struct device_attribute *attr,
  245. char *buf)
  246. {
  247. struct bonding *bond = to_bond(d);
  248. int i, res = 0;
  249. for (i = 0; i < BOND_MAX_ARP_TARGETS; i++) {
  250. if (bond->params.arp_targets[i])
  251. res += sysfs_emit_at(buf, res, "%pI4 ",
  252. &bond->params.arp_targets[i]);
  253. }
  254. if (res)
  255. buf[res-1] = '\n'; /* eat the leftover space */
  256. return res;
  257. }
  258. static DEVICE_ATTR(arp_ip_target, 0644,
  259. bonding_show_arp_targets, bonding_sysfs_store_option);
  260. /* Show the arp missed max. */
  261. static ssize_t bonding_show_missed_max(struct device *d,
  262. struct device_attribute *attr,
  263. char *buf)
  264. {
  265. struct bonding *bond = to_bond(d);
  266. return sysfs_emit(buf, "%u\n", bond->params.missed_max);
  267. }
  268. static DEVICE_ATTR(arp_missed_max, 0644,
  269. bonding_show_missed_max, bonding_sysfs_store_option);
  270. /* Show the up and down delays. */
  271. static ssize_t bonding_show_downdelay(struct device *d,
  272. struct device_attribute *attr,
  273. char *buf)
  274. {
  275. struct bonding *bond = to_bond(d);
  276. return sysfs_emit(buf, "%d\n", bond->params.downdelay * bond->params.miimon);
  277. }
  278. static DEVICE_ATTR(downdelay, 0644,
  279. bonding_show_downdelay, bonding_sysfs_store_option);
  280. static ssize_t bonding_show_updelay(struct device *d,
  281. struct device_attribute *attr,
  282. char *buf)
  283. {
  284. struct bonding *bond = to_bond(d);
  285. return sysfs_emit(buf, "%d\n", bond->params.updelay * bond->params.miimon);
  286. }
  287. static DEVICE_ATTR(updelay, 0644,
  288. bonding_show_updelay, bonding_sysfs_store_option);
  289. static ssize_t bonding_show_peer_notif_delay(struct device *d,
  290. struct device_attribute *attr,
  291. char *buf)
  292. {
  293. struct bonding *bond = to_bond(d);
  294. return sysfs_emit(buf, "%d\n",
  295. bond->params.peer_notif_delay * bond->params.miimon);
  296. }
  297. static DEVICE_ATTR(peer_notif_delay, 0644,
  298. bonding_show_peer_notif_delay, bonding_sysfs_store_option);
  299. /* Show the LACP activity and interval. */
  300. static ssize_t bonding_show_lacp_active(struct device *d,
  301. struct device_attribute *attr,
  302. char *buf)
  303. {
  304. struct bonding *bond = to_bond(d);
  305. const struct bond_opt_value *val;
  306. val = bond_opt_get_val(BOND_OPT_LACP_ACTIVE, bond->params.lacp_active);
  307. return sysfs_emit(buf, "%s %d\n", val->string, bond->params.lacp_active);
  308. }
  309. static DEVICE_ATTR(lacp_active, 0644,
  310. bonding_show_lacp_active, bonding_sysfs_store_option);
  311. static ssize_t bonding_show_lacp_rate(struct device *d,
  312. struct device_attribute *attr,
  313. char *buf)
  314. {
  315. struct bonding *bond = to_bond(d);
  316. const struct bond_opt_value *val;
  317. val = bond_opt_get_val(BOND_OPT_LACP_RATE, bond->params.lacp_fast);
  318. return sysfs_emit(buf, "%s %d\n", val->string, bond->params.lacp_fast);
  319. }
  320. static DEVICE_ATTR(lacp_rate, 0644,
  321. bonding_show_lacp_rate, bonding_sysfs_store_option);
  322. static ssize_t bonding_show_min_links(struct device *d,
  323. struct device_attribute *attr,
  324. char *buf)
  325. {
  326. struct bonding *bond = to_bond(d);
  327. return sysfs_emit(buf, "%u\n", bond->params.min_links);
  328. }
  329. static DEVICE_ATTR(min_links, 0644,
  330. bonding_show_min_links, bonding_sysfs_store_option);
  331. static ssize_t bonding_show_ad_select(struct device *d,
  332. struct device_attribute *attr,
  333. char *buf)
  334. {
  335. struct bonding *bond = to_bond(d);
  336. const struct bond_opt_value *val;
  337. val = bond_opt_get_val(BOND_OPT_AD_SELECT, bond->params.ad_select);
  338. return sysfs_emit(buf, "%s %d\n", val->string, bond->params.ad_select);
  339. }
  340. static DEVICE_ATTR(ad_select, 0644,
  341. bonding_show_ad_select, bonding_sysfs_store_option);
  342. /* Show the number of peer notifications to send after a failover event. */
  343. static ssize_t bonding_show_num_peer_notif(struct device *d,
  344. struct device_attribute *attr,
  345. char *buf)
  346. {
  347. struct bonding *bond = to_bond(d);
  348. return sysfs_emit(buf, "%d\n", bond->params.num_peer_notif);
  349. }
  350. static DEVICE_ATTR(num_grat_arp, 0644,
  351. bonding_show_num_peer_notif, bonding_sysfs_store_option);
  352. static DEVICE_ATTR(num_unsol_na, 0644,
  353. bonding_show_num_peer_notif, bonding_sysfs_store_option);
  354. /* Show the MII monitor interval. */
  355. static ssize_t bonding_show_miimon(struct device *d,
  356. struct device_attribute *attr,
  357. char *buf)
  358. {
  359. struct bonding *bond = to_bond(d);
  360. return sysfs_emit(buf, "%d\n", bond->params.miimon);
  361. }
  362. static DEVICE_ATTR(miimon, 0644,
  363. bonding_show_miimon, bonding_sysfs_store_option);
  364. /* Show the primary slave. */
  365. static ssize_t bonding_show_primary(struct device *d,
  366. struct device_attribute *attr,
  367. char *buf)
  368. {
  369. struct bonding *bond = to_bond(d);
  370. struct slave *primary;
  371. int count = 0;
  372. rcu_read_lock();
  373. primary = rcu_dereference(bond->primary_slave);
  374. if (primary)
  375. count = sysfs_emit(buf, "%s\n", primary->dev->name);
  376. rcu_read_unlock();
  377. return count;
  378. }
  379. static DEVICE_ATTR(primary, 0644,
  380. bonding_show_primary, bonding_sysfs_store_option);
  381. /* Show the primary_reselect flag. */
  382. static ssize_t bonding_show_primary_reselect(struct device *d,
  383. struct device_attribute *attr,
  384. char *buf)
  385. {
  386. struct bonding *bond = to_bond(d);
  387. const struct bond_opt_value *val;
  388. val = bond_opt_get_val(BOND_OPT_PRIMARY_RESELECT,
  389. bond->params.primary_reselect);
  390. return sysfs_emit(buf, "%s %d\n",
  391. val->string, bond->params.primary_reselect);
  392. }
  393. static DEVICE_ATTR(primary_reselect, 0644,
  394. bonding_show_primary_reselect, bonding_sysfs_store_option);
  395. /* Show the use_carrier flag. */
  396. static ssize_t bonding_show_carrier(struct device *d,
  397. struct device_attribute *attr,
  398. char *buf)
  399. {
  400. struct bonding *bond = to_bond(d);
  401. return sysfs_emit(buf, "%d\n", bond->params.use_carrier);
  402. }
  403. static DEVICE_ATTR(use_carrier, 0644,
  404. bonding_show_carrier, bonding_sysfs_store_option);
  405. /* Show currently active_slave. */
  406. static ssize_t bonding_show_active_slave(struct device *d,
  407. struct device_attribute *attr,
  408. char *buf)
  409. {
  410. struct bonding *bond = to_bond(d);
  411. struct net_device *slave_dev;
  412. int count = 0;
  413. rcu_read_lock();
  414. slave_dev = bond_option_active_slave_get_rcu(bond);
  415. if (slave_dev)
  416. count = sysfs_emit(buf, "%s\n", slave_dev->name);
  417. rcu_read_unlock();
  418. return count;
  419. }
  420. static DEVICE_ATTR(active_slave, 0644,
  421. bonding_show_active_slave, bonding_sysfs_store_option);
  422. /* Show link status of the bond interface. */
  423. static ssize_t bonding_show_mii_status(struct device *d,
  424. struct device_attribute *attr,
  425. char *buf)
  426. {
  427. struct bonding *bond = to_bond(d);
  428. bool active = netif_carrier_ok(bond->dev);
  429. return sysfs_emit(buf, "%s\n", active ? "up" : "down");
  430. }
  431. static DEVICE_ATTR(mii_status, 0444, bonding_show_mii_status, NULL);
  432. /* Show current 802.3ad aggregator ID. */
  433. static ssize_t bonding_show_ad_aggregator(struct device *d,
  434. struct device_attribute *attr,
  435. char *buf)
  436. {
  437. int count = 0;
  438. struct bonding *bond = to_bond(d);
  439. if (BOND_MODE(bond) == BOND_MODE_8023AD) {
  440. struct ad_info ad_info;
  441. count = sysfs_emit(buf, "%d\n",
  442. bond_3ad_get_active_agg_info(bond, &ad_info)
  443. ? 0 : ad_info.aggregator_id);
  444. }
  445. return count;
  446. }
  447. static DEVICE_ATTR(ad_aggregator, 0444, bonding_show_ad_aggregator, NULL);
  448. /* Show number of active 802.3ad ports. */
  449. static ssize_t bonding_show_ad_num_ports(struct device *d,
  450. struct device_attribute *attr,
  451. char *buf)
  452. {
  453. int count = 0;
  454. struct bonding *bond = to_bond(d);
  455. if (BOND_MODE(bond) == BOND_MODE_8023AD) {
  456. struct ad_info ad_info;
  457. count = sysfs_emit(buf, "%d\n",
  458. bond_3ad_get_active_agg_info(bond, &ad_info)
  459. ? 0 : ad_info.ports);
  460. }
  461. return count;
  462. }
  463. static DEVICE_ATTR(ad_num_ports, 0444, bonding_show_ad_num_ports, NULL);
  464. /* Show current 802.3ad actor key. */
  465. static ssize_t bonding_show_ad_actor_key(struct device *d,
  466. struct device_attribute *attr,
  467. char *buf)
  468. {
  469. int count = 0;
  470. struct bonding *bond = to_bond(d);
  471. if (BOND_MODE(bond) == BOND_MODE_8023AD && capable(CAP_NET_ADMIN)) {
  472. struct ad_info ad_info;
  473. count = sysfs_emit(buf, "%d\n",
  474. bond_3ad_get_active_agg_info(bond, &ad_info)
  475. ? 0 : ad_info.actor_key);
  476. }
  477. return count;
  478. }
  479. static DEVICE_ATTR(ad_actor_key, 0444, bonding_show_ad_actor_key, NULL);
  480. /* Show current 802.3ad partner key. */
  481. static ssize_t bonding_show_ad_partner_key(struct device *d,
  482. struct device_attribute *attr,
  483. char *buf)
  484. {
  485. int count = 0;
  486. struct bonding *bond = to_bond(d);
  487. if (BOND_MODE(bond) == BOND_MODE_8023AD && capable(CAP_NET_ADMIN)) {
  488. struct ad_info ad_info;
  489. count = sysfs_emit(buf, "%d\n",
  490. bond_3ad_get_active_agg_info(bond, &ad_info)
  491. ? 0 : ad_info.partner_key);
  492. }
  493. return count;
  494. }
  495. static DEVICE_ATTR(ad_partner_key, 0444, bonding_show_ad_partner_key, NULL);
  496. /* Show current 802.3ad partner mac. */
  497. static ssize_t bonding_show_ad_partner_mac(struct device *d,
  498. struct device_attribute *attr,
  499. char *buf)
  500. {
  501. int count = 0;
  502. struct bonding *bond = to_bond(d);
  503. if (BOND_MODE(bond) == BOND_MODE_8023AD && capable(CAP_NET_ADMIN)) {
  504. struct ad_info ad_info;
  505. if (!bond_3ad_get_active_agg_info(bond, &ad_info))
  506. count = sysfs_emit(buf, "%pM\n", ad_info.partner_system);
  507. }
  508. return count;
  509. }
  510. static DEVICE_ATTR(ad_partner_mac, 0444, bonding_show_ad_partner_mac, NULL);
  511. /* Show the queue_ids of the slaves in the current bond. */
  512. static ssize_t bonding_show_queue_id(struct device *d,
  513. struct device_attribute *attr,
  514. char *buf)
  515. {
  516. struct bonding *bond = to_bond(d);
  517. struct list_head *iter;
  518. struct slave *slave;
  519. int res = 0;
  520. if (!rtnl_trylock())
  521. return restart_syscall();
  522. bond_for_each_slave(bond, slave, iter) {
  523. if (res > (PAGE_SIZE - IFNAMSIZ - 6)) {
  524. /* not enough space for another interface_name:queue_id pair */
  525. if ((PAGE_SIZE - res) > 10)
  526. res = PAGE_SIZE - 10;
  527. res += sysfs_emit_at(buf, res, "++more++ ");
  528. break;
  529. }
  530. res += sysfs_emit_at(buf, res, "%s:%d ",
  531. slave->dev->name, slave->queue_id);
  532. }
  533. if (res)
  534. buf[res-1] = '\n'; /* eat the leftover space */
  535. rtnl_unlock();
  536. return res;
  537. }
  538. static DEVICE_ATTR(queue_id, 0644, bonding_show_queue_id,
  539. bonding_sysfs_store_option);
  540. /* Show the all_slaves_active flag. */
  541. static ssize_t bonding_show_slaves_active(struct device *d,
  542. struct device_attribute *attr,
  543. char *buf)
  544. {
  545. struct bonding *bond = to_bond(d);
  546. return sysfs_emit(buf, "%d\n", bond->params.all_slaves_active);
  547. }
  548. static DEVICE_ATTR(all_slaves_active, 0644,
  549. bonding_show_slaves_active, bonding_sysfs_store_option);
  550. /* Show the number of IGMP membership reports to send on link failure */
  551. static ssize_t bonding_show_resend_igmp(struct device *d,
  552. struct device_attribute *attr,
  553. char *buf)
  554. {
  555. struct bonding *bond = to_bond(d);
  556. return sysfs_emit(buf, "%d\n", bond->params.resend_igmp);
  557. }
  558. static DEVICE_ATTR(resend_igmp, 0644,
  559. bonding_show_resend_igmp, bonding_sysfs_store_option);
  560. static ssize_t bonding_show_lp_interval(struct device *d,
  561. struct device_attribute *attr,
  562. char *buf)
  563. {
  564. struct bonding *bond = to_bond(d);
  565. return sysfs_emit(buf, "%d\n", bond->params.lp_interval);
  566. }
  567. static DEVICE_ATTR(lp_interval, 0644,
  568. bonding_show_lp_interval, bonding_sysfs_store_option);
  569. static ssize_t bonding_show_tlb_dynamic_lb(struct device *d,
  570. struct device_attribute *attr,
  571. char *buf)
  572. {
  573. struct bonding *bond = to_bond(d);
  574. return sysfs_emit(buf, "%d\n", bond->params.tlb_dynamic_lb);
  575. }
  576. static DEVICE_ATTR(tlb_dynamic_lb, 0644,
  577. bonding_show_tlb_dynamic_lb, bonding_sysfs_store_option);
  578. static ssize_t bonding_show_packets_per_slave(struct device *d,
  579. struct device_attribute *attr,
  580. char *buf)
  581. {
  582. struct bonding *bond = to_bond(d);
  583. unsigned int packets_per_slave = bond->params.packets_per_slave;
  584. return sysfs_emit(buf, "%u\n", packets_per_slave);
  585. }
  586. static DEVICE_ATTR(packets_per_slave, 0644,
  587. bonding_show_packets_per_slave, bonding_sysfs_store_option);
  588. static ssize_t bonding_show_ad_actor_sys_prio(struct device *d,
  589. struct device_attribute *attr,
  590. char *buf)
  591. {
  592. struct bonding *bond = to_bond(d);
  593. if (BOND_MODE(bond) == BOND_MODE_8023AD && capable(CAP_NET_ADMIN))
  594. return sysfs_emit(buf, "%hu\n", bond->params.ad_actor_sys_prio);
  595. return 0;
  596. }
  597. static DEVICE_ATTR(ad_actor_sys_prio, 0644,
  598. bonding_show_ad_actor_sys_prio, bonding_sysfs_store_option);
  599. static ssize_t bonding_show_ad_actor_system(struct device *d,
  600. struct device_attribute *attr,
  601. char *buf)
  602. {
  603. struct bonding *bond = to_bond(d);
  604. if (BOND_MODE(bond) == BOND_MODE_8023AD && capable(CAP_NET_ADMIN))
  605. return sysfs_emit(buf, "%pM\n", bond->params.ad_actor_system);
  606. return 0;
  607. }
  608. static DEVICE_ATTR(ad_actor_system, 0644,
  609. bonding_show_ad_actor_system, bonding_sysfs_store_option);
  610. static ssize_t bonding_show_ad_user_port_key(struct device *d,
  611. struct device_attribute *attr,
  612. char *buf)
  613. {
  614. struct bonding *bond = to_bond(d);
  615. if (BOND_MODE(bond) == BOND_MODE_8023AD && capable(CAP_NET_ADMIN))
  616. return sysfs_emit(buf, "%hu\n", bond->params.ad_user_port_key);
  617. return 0;
  618. }
  619. static DEVICE_ATTR(ad_user_port_key, 0644,
  620. bonding_show_ad_user_port_key, bonding_sysfs_store_option);
  621. static struct attribute *per_bond_attrs[] = {
  622. &dev_attr_slaves.attr,
  623. &dev_attr_mode.attr,
  624. &dev_attr_fail_over_mac.attr,
  625. &dev_attr_arp_validate.attr,
  626. &dev_attr_arp_all_targets.attr,
  627. &dev_attr_arp_interval.attr,
  628. &dev_attr_arp_ip_target.attr,
  629. &dev_attr_downdelay.attr,
  630. &dev_attr_updelay.attr,
  631. &dev_attr_peer_notif_delay.attr,
  632. &dev_attr_lacp_active.attr,
  633. &dev_attr_lacp_rate.attr,
  634. &dev_attr_ad_select.attr,
  635. &dev_attr_xmit_hash_policy.attr,
  636. &dev_attr_num_grat_arp.attr,
  637. &dev_attr_num_unsol_na.attr,
  638. &dev_attr_miimon.attr,
  639. &dev_attr_primary.attr,
  640. &dev_attr_primary_reselect.attr,
  641. &dev_attr_use_carrier.attr,
  642. &dev_attr_active_slave.attr,
  643. &dev_attr_mii_status.attr,
  644. &dev_attr_ad_aggregator.attr,
  645. &dev_attr_ad_num_ports.attr,
  646. &dev_attr_ad_actor_key.attr,
  647. &dev_attr_ad_partner_key.attr,
  648. &dev_attr_ad_partner_mac.attr,
  649. &dev_attr_queue_id.attr,
  650. &dev_attr_all_slaves_active.attr,
  651. &dev_attr_resend_igmp.attr,
  652. &dev_attr_min_links.attr,
  653. &dev_attr_lp_interval.attr,
  654. &dev_attr_packets_per_slave.attr,
  655. &dev_attr_tlb_dynamic_lb.attr,
  656. &dev_attr_ad_actor_sys_prio.attr,
  657. &dev_attr_ad_actor_system.attr,
  658. &dev_attr_ad_user_port_key.attr,
  659. &dev_attr_arp_missed_max.attr,
  660. NULL,
  661. };
  662. static const struct attribute_group bonding_group = {
  663. .name = "bonding",
  664. .attrs = per_bond_attrs,
  665. };
  666. /* Initialize sysfs. This sets up the bonding_masters file in
  667. * /sys/class/net.
  668. */
  669. int bond_create_sysfs(struct bond_net *bn)
  670. {
  671. int ret;
  672. bn->class_attr_bonding_masters = class_attr_bonding_masters;
  673. sysfs_attr_init(&bn->class_attr_bonding_masters.attr);
  674. ret = netdev_class_create_file_ns(&bn->class_attr_bonding_masters,
  675. bn->net);
  676. /* Permit multiple loads of the module by ignoring failures to
  677. * create the bonding_masters sysfs file. Bonding devices
  678. * created by second or subsequent loads of the module will
  679. * not be listed in, or controllable by, bonding_masters, but
  680. * will have the usual "bonding" sysfs directory.
  681. *
  682. * This is done to preserve backwards compatibility for
  683. * initscripts/sysconfig, which load bonding multiple times to
  684. * configure multiple bonding devices.
  685. */
  686. if (ret == -EEXIST) {
  687. /* Is someone being kinky and naming a device bonding_master? */
  688. if (netdev_name_in_use(bn->net,
  689. class_attr_bonding_masters.attr.name))
  690. pr_err("network device named %s already exists in sysfs\n",
  691. class_attr_bonding_masters.attr.name);
  692. ret = 0;
  693. }
  694. return ret;
  695. }
  696. /* Remove /sys/class/net/bonding_masters. */
  697. void bond_destroy_sysfs(struct bond_net *bn)
  698. {
  699. netdev_class_remove_file_ns(&bn->class_attr_bonding_masters, bn->net);
  700. }
  701. /* Initialize sysfs for each bond. This sets up and registers
  702. * the 'bondctl' directory for each individual bond under /sys/class/net.
  703. */
  704. void bond_prepare_sysfs_group(struct bonding *bond)
  705. {
  706. bond->dev->sysfs_groups[0] = &bonding_group;
  707. }