bond_sysfs_slave.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /* Sysfs attributes of bond slaves
  3. *
  4. * Copyright (c) 2014 Scott Feldman <[email protected]>
  5. */
  6. #include <linux/capability.h>
  7. #include <linux/kernel.h>
  8. #include <linux/netdevice.h>
  9. #include <net/bonding.h>
  10. struct slave_attribute {
  11. struct attribute attr;
  12. ssize_t (*show)(struct slave *, char *);
  13. };
  14. #define SLAVE_ATTR_RO(_name) \
  15. const struct slave_attribute slave_attr_##_name = __ATTR_RO(_name)
  16. static ssize_t state_show(struct slave *slave, char *buf)
  17. {
  18. switch (bond_slave_state(slave)) {
  19. case BOND_STATE_ACTIVE:
  20. return sysfs_emit(buf, "active\n");
  21. case BOND_STATE_BACKUP:
  22. return sysfs_emit(buf, "backup\n");
  23. default:
  24. return sysfs_emit(buf, "UNKNOWN\n");
  25. }
  26. }
  27. static SLAVE_ATTR_RO(state);
  28. static ssize_t mii_status_show(struct slave *slave, char *buf)
  29. {
  30. return sysfs_emit(buf, "%s\n", bond_slave_link_status(slave->link));
  31. }
  32. static SLAVE_ATTR_RO(mii_status);
  33. static ssize_t link_failure_count_show(struct slave *slave, char *buf)
  34. {
  35. return sysfs_emit(buf, "%d\n", slave->link_failure_count);
  36. }
  37. static SLAVE_ATTR_RO(link_failure_count);
  38. static ssize_t perm_hwaddr_show(struct slave *slave, char *buf)
  39. {
  40. return sysfs_emit(buf, "%*phC\n",
  41. slave->dev->addr_len,
  42. slave->perm_hwaddr);
  43. }
  44. static SLAVE_ATTR_RO(perm_hwaddr);
  45. static ssize_t queue_id_show(struct slave *slave, char *buf)
  46. {
  47. return sysfs_emit(buf, "%d\n", slave->queue_id);
  48. }
  49. static SLAVE_ATTR_RO(queue_id);
  50. static ssize_t ad_aggregator_id_show(struct slave *slave, char *buf)
  51. {
  52. const struct aggregator *agg;
  53. if (BOND_MODE(slave->bond) == BOND_MODE_8023AD) {
  54. agg = SLAVE_AD_INFO(slave)->port.aggregator;
  55. if (agg)
  56. return sysfs_emit(buf, "%d\n",
  57. agg->aggregator_identifier);
  58. }
  59. return sysfs_emit(buf, "N/A\n");
  60. }
  61. static SLAVE_ATTR_RO(ad_aggregator_id);
  62. static ssize_t ad_actor_oper_port_state_show(struct slave *slave, char *buf)
  63. {
  64. const struct port *ad_port;
  65. if (BOND_MODE(slave->bond) == BOND_MODE_8023AD) {
  66. ad_port = &SLAVE_AD_INFO(slave)->port;
  67. if (ad_port->aggregator)
  68. return sysfs_emit(buf, "%u\n",
  69. ad_port->actor_oper_port_state);
  70. }
  71. return sysfs_emit(buf, "N/A\n");
  72. }
  73. static SLAVE_ATTR_RO(ad_actor_oper_port_state);
  74. static ssize_t ad_partner_oper_port_state_show(struct slave *slave, char *buf)
  75. {
  76. const struct port *ad_port;
  77. if (BOND_MODE(slave->bond) == BOND_MODE_8023AD) {
  78. ad_port = &SLAVE_AD_INFO(slave)->port;
  79. if (ad_port->aggregator)
  80. return sysfs_emit(buf, "%u\n",
  81. ad_port->partner_oper.port_state);
  82. }
  83. return sysfs_emit(buf, "N/A\n");
  84. }
  85. static SLAVE_ATTR_RO(ad_partner_oper_port_state);
  86. static const struct attribute *slave_attrs[] = {
  87. &slave_attr_state.attr,
  88. &slave_attr_mii_status.attr,
  89. &slave_attr_link_failure_count.attr,
  90. &slave_attr_perm_hwaddr.attr,
  91. &slave_attr_queue_id.attr,
  92. &slave_attr_ad_aggregator_id.attr,
  93. &slave_attr_ad_actor_oper_port_state.attr,
  94. &slave_attr_ad_partner_oper_port_state.attr,
  95. NULL
  96. };
  97. #define to_slave_attr(_at) container_of(_at, struct slave_attribute, attr)
  98. static ssize_t slave_show(struct kobject *kobj,
  99. struct attribute *attr, char *buf)
  100. {
  101. struct slave_attribute *slave_attr = to_slave_attr(attr);
  102. struct slave *slave = to_slave(kobj);
  103. return slave_attr->show(slave, buf);
  104. }
  105. const struct sysfs_ops slave_sysfs_ops = {
  106. .show = slave_show,
  107. };
  108. int bond_sysfs_slave_add(struct slave *slave)
  109. {
  110. return sysfs_create_files(&slave->kobj, slave_attrs);
  111. }
  112. void bond_sysfs_slave_del(struct slave *slave)
  113. {
  114. sysfs_remove_files(&slave->kobj, slave_attrs);
  115. }