br_sysfs_if.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. .owner = THIS_MODULE, }, \
  30. .show = _show, \
  31. .store = _store, \
  32. };
  33. static ssize_t show_path_cost(struct net_bridge_port *p, char *buf)
  34. {
  35. return sprintf(buf, "%d\n", p->path_cost);
  36. }
  37. static ssize_t store_path_cost(struct net_bridge_port *p, unsigned long v)
  38. {
  39. br_stp_set_path_cost(p, v);
  40. return 0;
  41. }
  42. static BRPORT_ATTR(path_cost, S_IRUGO | S_IWUSR,
  43. show_path_cost, store_path_cost);
  44. static ssize_t show_priority(struct net_bridge_port *p, char *buf)
  45. {
  46. return sprintf(buf, "%d\n", p->priority);
  47. }
  48. static ssize_t store_priority(struct net_bridge_port *p, unsigned long v)
  49. {
  50. if (v >= (1<<(16-BR_PORT_BITS)))
  51. return -ERANGE;
  52. br_stp_set_port_priority(p, v);
  53. return 0;
  54. }
  55. static BRPORT_ATTR(priority, S_IRUGO | S_IWUSR,
  56. show_priority, store_priority);
  57. static ssize_t show_designated_root(struct net_bridge_port *p, char *buf)
  58. {
  59. return br_show_bridge_id(buf, &p->designated_root);
  60. }
  61. static BRPORT_ATTR(designated_root, S_IRUGO, show_designated_root, NULL);
  62. static ssize_t show_designated_bridge(struct net_bridge_port *p, char *buf)
  63. {
  64. return br_show_bridge_id(buf, &p->designated_bridge);
  65. }
  66. static BRPORT_ATTR(designated_bridge, S_IRUGO, show_designated_bridge, NULL);
  67. static ssize_t show_designated_port(struct net_bridge_port *p, char *buf)
  68. {
  69. return sprintf(buf, "%d\n", p->designated_port);
  70. }
  71. static BRPORT_ATTR(designated_port, S_IRUGO, show_designated_port, NULL);
  72. static ssize_t show_designated_cost(struct net_bridge_port *p, char *buf)
  73. {
  74. return sprintf(buf, "%d\n", p->designated_cost);
  75. }
  76. static BRPORT_ATTR(designated_cost, S_IRUGO, show_designated_cost, NULL);
  77. static ssize_t show_port_id(struct net_bridge_port *p, char *buf)
  78. {
  79. return sprintf(buf, "0x%x\n", p->port_id);
  80. }
  81. static BRPORT_ATTR(port_id, S_IRUGO, show_port_id, NULL);
  82. static ssize_t show_port_no(struct net_bridge_port *p, char *buf)
  83. {
  84. return sprintf(buf, "0x%x\n", p->port_no);
  85. }
  86. static BRPORT_ATTR(port_no, S_IRUGO, show_port_no, NULL);
  87. static ssize_t show_change_ack(struct net_bridge_port *p, char *buf)
  88. {
  89. return sprintf(buf, "%d\n", p->topology_change_ack);
  90. }
  91. static BRPORT_ATTR(change_ack, S_IRUGO, show_change_ack, NULL);
  92. static ssize_t show_config_pending(struct net_bridge_port *p, char *buf)
  93. {
  94. return sprintf(buf, "%d\n", p->config_pending);
  95. }
  96. static BRPORT_ATTR(config_pending, S_IRUGO, show_config_pending, NULL);
  97. static ssize_t show_port_state(struct net_bridge_port *p, char *buf)
  98. {
  99. return sprintf(buf, "%d\n", p->state);
  100. }
  101. static BRPORT_ATTR(state, S_IRUGO, show_port_state, NULL);
  102. static ssize_t show_message_age_timer(struct net_bridge_port *p,
  103. char *buf)
  104. {
  105. return sprintf(buf, "%ld\n", br_timer_value(&p->message_age_timer));
  106. }
  107. static BRPORT_ATTR(message_age_timer, S_IRUGO, show_message_age_timer, NULL);
  108. static ssize_t show_forward_delay_timer(struct net_bridge_port *p,
  109. char *buf)
  110. {
  111. return sprintf(buf, "%ld\n", br_timer_value(&p->forward_delay_timer));
  112. }
  113. static BRPORT_ATTR(forward_delay_timer, S_IRUGO, show_forward_delay_timer, NULL);
  114. static ssize_t show_hold_timer(struct net_bridge_port *p,
  115. char *buf)
  116. {
  117. return sprintf(buf, "%ld\n", br_timer_value(&p->hold_timer));
  118. }
  119. static BRPORT_ATTR(hold_timer, S_IRUGO, show_hold_timer, NULL);
  120. static ssize_t store_flush(struct net_bridge_port *p, unsigned long v)
  121. {
  122. br_fdb_delete_by_port(p->br, p, 0); // Don't delete local entry
  123. return 0;
  124. }
  125. static BRPORT_ATTR(flush, S_IWUSR, NULL, store_flush);
  126. static struct brport_attribute *brport_attrs[] = {
  127. &brport_attr_path_cost,
  128. &brport_attr_priority,
  129. &brport_attr_port_id,
  130. &brport_attr_port_no,
  131. &brport_attr_designated_root,
  132. &brport_attr_designated_bridge,
  133. &brport_attr_designated_port,
  134. &brport_attr_designated_cost,
  135. &brport_attr_state,
  136. &brport_attr_change_ack,
  137. &brport_attr_config_pending,
  138. &brport_attr_message_age_timer,
  139. &brport_attr_forward_delay_timer,
  140. &brport_attr_hold_timer,
  141. &brport_attr_flush,
  142. NULL
  143. };
  144. #define to_brport_attr(_at) container_of(_at, struct brport_attribute, attr)
  145. #define to_brport(obj) container_of(obj, struct net_bridge_port, kobj)
  146. static ssize_t brport_show(struct kobject * kobj,
  147. struct attribute * attr, char * buf)
  148. {
  149. struct brport_attribute * brport_attr = to_brport_attr(attr);
  150. struct net_bridge_port * p = to_brport(kobj);
  151. return brport_attr->show(p, buf);
  152. }
  153. static ssize_t brport_store(struct kobject * kobj,
  154. struct attribute * attr,
  155. const char * buf, size_t count)
  156. {
  157. struct brport_attribute * brport_attr = to_brport_attr(attr);
  158. struct net_bridge_port * p = to_brport(kobj);
  159. ssize_t ret = -EINVAL;
  160. char *endp;
  161. unsigned long val;
  162. if (!capable(CAP_NET_ADMIN))
  163. return -EPERM;
  164. val = simple_strtoul(buf, &endp, 0);
  165. if (endp != buf) {
  166. rtnl_lock();
  167. if (p->dev && p->br && brport_attr->store) {
  168. spin_lock_bh(&p->br->lock);
  169. ret = brport_attr->store(p, val);
  170. spin_unlock_bh(&p->br->lock);
  171. if (ret == 0)
  172. ret = count;
  173. }
  174. rtnl_unlock();
  175. }
  176. return ret;
  177. }
  178. struct sysfs_ops brport_sysfs_ops = {
  179. .show = brport_show,
  180. .store = brport_store,
  181. };
  182. /*
  183. * Add sysfs entries to ethernet device added to a bridge.
  184. * Creates a brport subdirectory with bridge attributes.
  185. * Puts symlink in bridge's brport subdirectory
  186. */
  187. int br_sysfs_addif(struct net_bridge_port *p)
  188. {
  189. struct net_bridge *br = p->br;
  190. struct brport_attribute **a;
  191. int err;
  192. err = sysfs_create_link(&p->kobj, &br->dev->dev.kobj,
  193. SYSFS_BRIDGE_PORT_LINK);
  194. if (err)
  195. goto out2;
  196. for (a = brport_attrs; *a; ++a) {
  197. err = sysfs_create_file(&p->kobj, &((*a)->attr));
  198. if (err)
  199. goto out2;
  200. }
  201. err= sysfs_create_link(&br->ifobj, &p->kobj, p->dev->name);
  202. out2:
  203. return err;
  204. }