br_sysfs_if.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /*
  2. * Sysfs attributes of bridge ports
  3. * Linux ethernet bridge
  4. *
  5. * Authors:
  6. * Stephen Hemminger <shemminger@osdl.org>
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version
  11. * 2 of the License, or (at your option) any later version.
  12. */
  13. #include <linux/capability.h>
  14. #include <linux/kernel.h>
  15. #include <linux/netdevice.h>
  16. #include <linux/if_bridge.h>
  17. #include <linux/rtnetlink.h>
  18. #include <linux/spinlock.h>
  19. #include "br_private.h"
  20. struct brport_attribute {
  21. struct attribute attr;
  22. ssize_t (*show)(struct net_bridge_port *, char *);
  23. ssize_t (*store)(struct net_bridge_port *, unsigned long);
  24. };
  25. #define BRPORT_ATTR(_name,_mode,_show,_store) \
  26. struct brport_attribute brport_attr_##_name = { \
  27. .attr = {.name = __stringify(_name), \
  28. .mode = _mode }, \
  29. .show = _show, \
  30. .store = _store, \
  31. };
  32. static ssize_t show_path_cost(struct net_bridge_port *p, char *buf)
  33. {
  34. return sprintf(buf, "%d\n", p->path_cost);
  35. }
  36. static ssize_t store_path_cost(struct net_bridge_port *p, unsigned long v)
  37. {
  38. br_stp_set_path_cost(p, v);
  39. return 0;
  40. }
  41. static BRPORT_ATTR(path_cost, S_IRUGO | S_IWUSR,
  42. show_path_cost, store_path_cost);
  43. static ssize_t show_priority(struct net_bridge_port *p, char *buf)
  44. {
  45. return sprintf(buf, "%d\n", p->priority);
  46. }
  47. static ssize_t store_priority(struct net_bridge_port *p, unsigned long v)
  48. {
  49. if (v >= (1<<(16-BR_PORT_BITS)))
  50. return -ERANGE;
  51. br_stp_set_port_priority(p, v);
  52. return 0;
  53. }
  54. static BRPORT_ATTR(priority, S_IRUGO | S_IWUSR,
  55. show_priority, store_priority);
  56. static ssize_t show_designated_root(struct net_bridge_port *p, char *buf)
  57. {
  58. return br_show_bridge_id(buf, &p->designated_root);
  59. }
  60. static BRPORT_ATTR(designated_root, S_IRUGO, show_designated_root, NULL);
  61. static ssize_t show_designated_bridge(struct net_bridge_port *p, char *buf)
  62. {
  63. return br_show_bridge_id(buf, &p->designated_bridge);
  64. }
  65. static BRPORT_ATTR(designated_bridge, S_IRUGO, show_designated_bridge, NULL);
  66. static ssize_t show_designated_port(struct net_bridge_port *p, char *buf)
  67. {
  68. return sprintf(buf, "%d\n", p->designated_port);
  69. }
  70. static BRPORT_ATTR(designated_port, S_IRUGO, show_designated_port, NULL);
  71. static ssize_t show_designated_cost(struct net_bridge_port *p, char *buf)
  72. {
  73. return sprintf(buf, "%d\n", p->designated_cost);
  74. }
  75. static BRPORT_ATTR(designated_cost, S_IRUGO, show_designated_cost, NULL);
  76. static ssize_t show_port_id(struct net_bridge_port *p, char *buf)
  77. {
  78. return sprintf(buf, "0x%x\n", p->port_id);
  79. }
  80. static BRPORT_ATTR(port_id, S_IRUGO, show_port_id, NULL);
  81. static ssize_t show_port_no(struct net_bridge_port *p, char *buf)
  82. {
  83. return sprintf(buf, "0x%x\n", p->port_no);
  84. }
  85. static BRPORT_ATTR(port_no, S_IRUGO, show_port_no, NULL);
  86. static ssize_t show_change_ack(struct net_bridge_port *p, char *buf)
  87. {
  88. return sprintf(buf, "%d\n", p->topology_change_ack);
  89. }
  90. static BRPORT_ATTR(change_ack, S_IRUGO, show_change_ack, NULL);
  91. static ssize_t show_config_pending(struct net_bridge_port *p, char *buf)
  92. {
  93. return sprintf(buf, "%d\n", p->config_pending);
  94. }
  95. static BRPORT_ATTR(config_pending, S_IRUGO, show_config_pending, NULL);
  96. static ssize_t show_port_state(struct net_bridge_port *p, char *buf)
  97. {
  98. return sprintf(buf, "%d\n", p->state);
  99. }
  100. static BRPORT_ATTR(state, S_IRUGO, show_port_state, NULL);
  101. static ssize_t show_message_age_timer(struct net_bridge_port *p,
  102. char *buf)
  103. {
  104. return sprintf(buf, "%ld\n", br_timer_value(&p->message_age_timer));
  105. }
  106. static BRPORT_ATTR(message_age_timer, S_IRUGO, show_message_age_timer, NULL);
  107. static ssize_t show_forward_delay_timer(struct net_bridge_port *p,
  108. char *buf)
  109. {
  110. return sprintf(buf, "%ld\n", br_timer_value(&p->forward_delay_timer));
  111. }
  112. static BRPORT_ATTR(forward_delay_timer, S_IRUGO, show_forward_delay_timer, NULL);
  113. static ssize_t show_hold_timer(struct net_bridge_port *p,
  114. char *buf)
  115. {
  116. return sprintf(buf, "%ld\n", br_timer_value(&p->hold_timer));
  117. }
  118. static BRPORT_ATTR(hold_timer, S_IRUGO, show_hold_timer, NULL);
  119. static ssize_t store_flush(struct net_bridge_port *p, unsigned long v)
  120. {
  121. br_fdb_delete_by_port(p->br, p, 0); // Don't delete local entry
  122. return 0;
  123. }
  124. static BRPORT_ATTR(flush, S_IWUSR, NULL, store_flush);
  125. static struct brport_attribute *brport_attrs[] = {
  126. &brport_attr_path_cost,
  127. &brport_attr_priority,
  128. &brport_attr_port_id,
  129. &brport_attr_port_no,
  130. &brport_attr_designated_root,
  131. &brport_attr_designated_bridge,
  132. &brport_attr_designated_port,
  133. &brport_attr_designated_cost,
  134. &brport_attr_state,
  135. &brport_attr_change_ack,
  136. &brport_attr_config_pending,
  137. &brport_attr_message_age_timer,
  138. &brport_attr_forward_delay_timer,
  139. &brport_attr_hold_timer,
  140. &brport_attr_flush,
  141. NULL
  142. };
  143. #define to_brport_attr(_at) container_of(_at, struct brport_attribute, attr)
  144. #define to_brport(obj) container_of(obj, struct net_bridge_port, kobj)
  145. static ssize_t brport_show(struct kobject * kobj,
  146. struct attribute * attr, char * buf)
  147. {
  148. struct brport_attribute * brport_attr = to_brport_attr(attr);
  149. struct net_bridge_port * p = to_brport(kobj);
  150. return brport_attr->show(p, buf);
  151. }
  152. static ssize_t brport_store(struct kobject * kobj,
  153. struct attribute * attr,
  154. const char * buf, size_t count)
  155. {
  156. struct brport_attribute * brport_attr = to_brport_attr(attr);
  157. struct net_bridge_port * p = to_brport(kobj);
  158. ssize_t ret = -EINVAL;
  159. char *endp;
  160. unsigned long val;
  161. if (!capable(CAP_NET_ADMIN))
  162. return -EPERM;
  163. val = simple_strtoul(buf, &endp, 0);
  164. if (endp != buf) {
  165. rtnl_lock();
  166. if (p->dev && p->br && brport_attr->store) {
  167. spin_lock_bh(&p->br->lock);
  168. ret = brport_attr->store(p, val);
  169. spin_unlock_bh(&p->br->lock);
  170. if (ret == 0)
  171. ret = count;
  172. }
  173. rtnl_unlock();
  174. }
  175. return ret;
  176. }
  177. struct sysfs_ops brport_sysfs_ops = {
  178. .show = brport_show,
  179. .store = brport_store,
  180. };
  181. /*
  182. * Add sysfs entries to ethernet device added to a bridge.
  183. * Creates a brport subdirectory with bridge attributes.
  184. * Puts symlink in bridge's brport subdirectory
  185. */
  186. int br_sysfs_addif(struct net_bridge_port *p)
  187. {
  188. struct net_bridge *br = p->br;
  189. struct brport_attribute **a;
  190. int err;
  191. err = sysfs_create_link(&p->kobj, &br->dev->dev.kobj,
  192. SYSFS_BRIDGE_PORT_LINK);
  193. if (err)
  194. goto out2;
  195. for (a = brport_attrs; *a; ++a) {
  196. err = sysfs_create_file(&p->kobj, &((*a)->attr));
  197. if (err)
  198. goto out2;
  199. }
  200. err= sysfs_create_link(&br->ifobj, &p->kobj, p->dev->name);
  201. out2:
  202. return err;
  203. }