br_sysfs_if.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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 struct brport_attribute *brport_attrs[] = {
  121. &brport_attr_path_cost,
  122. &brport_attr_priority,
  123. &brport_attr_port_id,
  124. &brport_attr_port_no,
  125. &brport_attr_designated_root,
  126. &brport_attr_designated_bridge,
  127. &brport_attr_designated_port,
  128. &brport_attr_designated_cost,
  129. &brport_attr_state,
  130. &brport_attr_change_ack,
  131. &brport_attr_config_pending,
  132. &brport_attr_message_age_timer,
  133. &brport_attr_forward_delay_timer,
  134. &brport_attr_hold_timer,
  135. NULL
  136. };
  137. #define to_brport_attr(_at) container_of(_at, struct brport_attribute, attr)
  138. #define to_brport(obj) container_of(obj, struct net_bridge_port, kobj)
  139. static ssize_t brport_show(struct kobject * kobj,
  140. struct attribute * attr, char * buf)
  141. {
  142. struct brport_attribute * brport_attr = to_brport_attr(attr);
  143. struct net_bridge_port * p = to_brport(kobj);
  144. return brport_attr->show(p, buf);
  145. }
  146. static ssize_t brport_store(struct kobject * kobj,
  147. struct attribute * attr,
  148. const char * buf, size_t count)
  149. {
  150. struct brport_attribute * brport_attr = to_brport_attr(attr);
  151. struct net_bridge_port * p = to_brport(kobj);
  152. ssize_t ret = -EINVAL;
  153. char *endp;
  154. unsigned long val;
  155. if (!capable(CAP_NET_ADMIN))
  156. return -EPERM;
  157. val = simple_strtoul(buf, &endp, 0);
  158. if (endp != buf) {
  159. rtnl_lock();
  160. if (p->dev && p->br && brport_attr->store) {
  161. spin_lock_bh(&p->br->lock);
  162. ret = brport_attr->store(p, val);
  163. spin_unlock_bh(&p->br->lock);
  164. if (ret == 0)
  165. ret = count;
  166. }
  167. rtnl_unlock();
  168. }
  169. return ret;
  170. }
  171. struct sysfs_ops brport_sysfs_ops = {
  172. .show = brport_show,
  173. .store = brport_store,
  174. };
  175. /*
  176. * Add sysfs entries to ethernet device added to a bridge.
  177. * Creates a brport subdirectory with bridge attributes.
  178. * Puts symlink in bridge's brport subdirectory
  179. */
  180. int br_sysfs_addif(struct net_bridge_port *p)
  181. {
  182. struct net_bridge *br = p->br;
  183. struct brport_attribute **a;
  184. int err;
  185. err = sysfs_create_link(&p->kobj, &br->dev->dev.kobj,
  186. SYSFS_BRIDGE_PORT_LINK);
  187. if (err)
  188. goto out2;
  189. for (a = brport_attrs; *a; ++a) {
  190. err = sysfs_create_file(&p->kobj, &((*a)->attr));
  191. if (err)
  192. goto out2;
  193. }
  194. err= sysfs_create_link(&br->ifobj, &p->kobj, p->dev->name);
  195. out2:
  196. return err;
  197. }