bond_sysfs_slave.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /* Sysfs attributes of bond slaves
  2. *
  3. * Copyright (c) 2014 Scott Feldman <sfeldma@cumulusnetworks.com>
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License
  7. * as published by the Free Software Foundation; either version
  8. * 2 of the License, or (at your option) any later version.
  9. */
  10. #include <linux/capability.h>
  11. #include <linux/kernel.h>
  12. #include <linux/netdevice.h>
  13. #include "bonding.h"
  14. struct slave_attribute {
  15. struct attribute attr;
  16. ssize_t (*show)(struct slave *, char *);
  17. };
  18. #define SLAVE_ATTR(_name, _mode, _show) \
  19. const struct slave_attribute slave_attr_##_name = { \
  20. .attr = {.name = __stringify(_name), \
  21. .mode = _mode }, \
  22. .show = _show, \
  23. };
  24. #define SLAVE_ATTR_RO(_name) \
  25. SLAVE_ATTR(_name, S_IRUGO, _name##_show)
  26. static ssize_t state_show(struct slave *slave, char *buf)
  27. {
  28. switch (bond_slave_state(slave)) {
  29. case BOND_STATE_ACTIVE:
  30. return sprintf(buf, "active\n");
  31. case BOND_STATE_BACKUP:
  32. return sprintf(buf, "backup\n");
  33. default:
  34. return sprintf(buf, "UNKONWN\n");
  35. }
  36. }
  37. static SLAVE_ATTR_RO(state);
  38. static ssize_t mii_status_show(struct slave *slave, char *buf)
  39. {
  40. return sprintf(buf, "%s\n", bond_slave_link_status(slave->link));
  41. }
  42. static SLAVE_ATTR_RO(mii_status);
  43. static ssize_t link_failure_count_show(struct slave *slave, char *buf)
  44. {
  45. return sprintf(buf, "%d\n", slave->link_failure_count);
  46. }
  47. static SLAVE_ATTR_RO(link_failure_count);
  48. static ssize_t perm_hwaddr_show(struct slave *slave, char *buf)
  49. {
  50. return sprintf(buf, "%pM\n", slave->perm_hwaddr);
  51. }
  52. static SLAVE_ATTR_RO(perm_hwaddr);
  53. static ssize_t queue_id_show(struct slave *slave, char *buf)
  54. {
  55. return sprintf(buf, "%d\n", slave->queue_id);
  56. }
  57. static SLAVE_ATTR_RO(queue_id);
  58. static ssize_t ad_aggregator_id_show(struct slave *slave, char *buf)
  59. {
  60. const struct aggregator *agg;
  61. if (BOND_MODE(slave->bond) == BOND_MODE_8023AD) {
  62. agg = SLAVE_AD_INFO(slave)->port.aggregator;
  63. if (agg)
  64. return sprintf(buf, "%d\n",
  65. agg->aggregator_identifier);
  66. }
  67. return sprintf(buf, "N/A\n");
  68. }
  69. static SLAVE_ATTR_RO(ad_aggregator_id);
  70. static const struct slave_attribute *slave_attrs[] = {
  71. &slave_attr_state,
  72. &slave_attr_mii_status,
  73. &slave_attr_link_failure_count,
  74. &slave_attr_perm_hwaddr,
  75. &slave_attr_queue_id,
  76. &slave_attr_ad_aggregator_id,
  77. NULL
  78. };
  79. #define to_slave_attr(_at) container_of(_at, struct slave_attribute, attr)
  80. #define to_slave(obj) container_of(obj, struct slave, kobj)
  81. static ssize_t slave_show(struct kobject *kobj,
  82. struct attribute *attr, char *buf)
  83. {
  84. struct slave_attribute *slave_attr = to_slave_attr(attr);
  85. struct slave *slave = to_slave(kobj);
  86. return slave_attr->show(slave, buf);
  87. }
  88. static const struct sysfs_ops slave_sysfs_ops = {
  89. .show = slave_show,
  90. };
  91. static struct kobj_type slave_ktype = {
  92. #ifdef CONFIG_SYSFS
  93. .sysfs_ops = &slave_sysfs_ops,
  94. #endif
  95. };
  96. int bond_sysfs_slave_add(struct slave *slave)
  97. {
  98. const struct slave_attribute **a;
  99. int err;
  100. err = kobject_init_and_add(&slave->kobj, &slave_ktype,
  101. &(slave->dev->dev.kobj), "bonding_slave");
  102. if (err)
  103. return err;
  104. for (a = slave_attrs; *a; ++a) {
  105. err = sysfs_create_file(&slave->kobj, &((*a)->attr));
  106. if (err) {
  107. kobject_del(&slave->kobj);
  108. return err;
  109. }
  110. }
  111. return 0;
  112. }
  113. void bond_sysfs_slave_del(struct slave *slave)
  114. {
  115. const struct slave_attribute **a;
  116. for (a = slave_attrs; *a; ++a)
  117. sysfs_remove_file(&slave->kobj, &((*a)->attr));
  118. kobject_del(&slave->kobj);
  119. }