br_sysfs_if.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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. int (*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 BRPORT_ATTR(path_cost, S_IRUGO | S_IWUSR,
  37. show_path_cost, br_stp_set_path_cost);
  38. static ssize_t show_priority(struct net_bridge_port *p, char *buf)
  39. {
  40. return sprintf(buf, "%d\n", p->priority);
  41. }
  42. static BRPORT_ATTR(priority, S_IRUGO | S_IWUSR,
  43. show_priority, br_stp_set_port_priority);
  44. static ssize_t show_designated_root(struct net_bridge_port *p, char *buf)
  45. {
  46. return br_show_bridge_id(buf, &p->designated_root);
  47. }
  48. static BRPORT_ATTR(designated_root, S_IRUGO, show_designated_root, NULL);
  49. static ssize_t show_designated_bridge(struct net_bridge_port *p, char *buf)
  50. {
  51. return br_show_bridge_id(buf, &p->designated_bridge);
  52. }
  53. static BRPORT_ATTR(designated_bridge, S_IRUGO, show_designated_bridge, NULL);
  54. static ssize_t show_designated_port(struct net_bridge_port *p, char *buf)
  55. {
  56. return sprintf(buf, "%d\n", p->designated_port);
  57. }
  58. static BRPORT_ATTR(designated_port, S_IRUGO, show_designated_port, NULL);
  59. static ssize_t show_designated_cost(struct net_bridge_port *p, char *buf)
  60. {
  61. return sprintf(buf, "%d\n", p->designated_cost);
  62. }
  63. static BRPORT_ATTR(designated_cost, S_IRUGO, show_designated_cost, NULL);
  64. static ssize_t show_port_id(struct net_bridge_port *p, char *buf)
  65. {
  66. return sprintf(buf, "0x%x\n", p->port_id);
  67. }
  68. static BRPORT_ATTR(port_id, S_IRUGO, show_port_id, NULL);
  69. static ssize_t show_port_no(struct net_bridge_port *p, char *buf)
  70. {
  71. return sprintf(buf, "0x%x\n", p->port_no);
  72. }
  73. static BRPORT_ATTR(port_no, S_IRUGO, show_port_no, NULL);
  74. static ssize_t show_change_ack(struct net_bridge_port *p, char *buf)
  75. {
  76. return sprintf(buf, "%d\n", p->topology_change_ack);
  77. }
  78. static BRPORT_ATTR(change_ack, S_IRUGO, show_change_ack, NULL);
  79. static ssize_t show_config_pending(struct net_bridge_port *p, char *buf)
  80. {
  81. return sprintf(buf, "%d\n", p->config_pending);
  82. }
  83. static BRPORT_ATTR(config_pending, S_IRUGO, show_config_pending, NULL);
  84. static ssize_t show_port_state(struct net_bridge_port *p, char *buf)
  85. {
  86. return sprintf(buf, "%d\n", p->state);
  87. }
  88. static BRPORT_ATTR(state, S_IRUGO, show_port_state, NULL);
  89. static ssize_t show_message_age_timer(struct net_bridge_port *p,
  90. char *buf)
  91. {
  92. return sprintf(buf, "%ld\n", br_timer_value(&p->message_age_timer));
  93. }
  94. static BRPORT_ATTR(message_age_timer, S_IRUGO, show_message_age_timer, NULL);
  95. static ssize_t show_forward_delay_timer(struct net_bridge_port *p,
  96. char *buf)
  97. {
  98. return sprintf(buf, "%ld\n", br_timer_value(&p->forward_delay_timer));
  99. }
  100. static BRPORT_ATTR(forward_delay_timer, S_IRUGO, show_forward_delay_timer, NULL);
  101. static ssize_t show_hold_timer(struct net_bridge_port *p,
  102. char *buf)
  103. {
  104. return sprintf(buf, "%ld\n", br_timer_value(&p->hold_timer));
  105. }
  106. static BRPORT_ATTR(hold_timer, S_IRUGO, show_hold_timer, NULL);
  107. static int store_flush(struct net_bridge_port *p, unsigned long v)
  108. {
  109. br_fdb_delete_by_port(p->br, p, 0); // Don't delete local entry
  110. return 0;
  111. }
  112. static BRPORT_ATTR(flush, S_IWUSR, NULL, store_flush);
  113. static ssize_t show_hairpin_mode(struct net_bridge_port *p, char *buf)
  114. {
  115. int hairpin_mode = (p->flags & BR_HAIRPIN_MODE) ? 1 : 0;
  116. return sprintf(buf, "%d\n", hairpin_mode);
  117. }
  118. static int store_hairpin_mode(struct net_bridge_port *p, unsigned long v)
  119. {
  120. if (v)
  121. p->flags |= BR_HAIRPIN_MODE;
  122. else
  123. p->flags &= ~BR_HAIRPIN_MODE;
  124. return 0;
  125. }
  126. static BRPORT_ATTR(hairpin_mode, S_IRUGO | S_IWUSR,
  127. show_hairpin_mode, store_hairpin_mode);
  128. #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
  129. static ssize_t show_multicast_router(struct net_bridge_port *p, char *buf)
  130. {
  131. return sprintf(buf, "%d\n", p->multicast_router);
  132. }
  133. static int store_multicast_router(struct net_bridge_port *p,
  134. unsigned long v)
  135. {
  136. return br_multicast_set_port_router(p, v);
  137. }
  138. static BRPORT_ATTR(multicast_router, S_IRUGO | S_IWUSR, show_multicast_router,
  139. store_multicast_router);
  140. #endif
  141. static struct brport_attribute *brport_attrs[] = {
  142. &brport_attr_path_cost,
  143. &brport_attr_priority,
  144. &brport_attr_port_id,
  145. &brport_attr_port_no,
  146. &brport_attr_designated_root,
  147. &brport_attr_designated_bridge,
  148. &brport_attr_designated_port,
  149. &brport_attr_designated_cost,
  150. &brport_attr_state,
  151. &brport_attr_change_ack,
  152. &brport_attr_config_pending,
  153. &brport_attr_message_age_timer,
  154. &brport_attr_forward_delay_timer,
  155. &brport_attr_hold_timer,
  156. &brport_attr_flush,
  157. &brport_attr_hairpin_mode,
  158. #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
  159. &brport_attr_multicast_router,
  160. #endif
  161. NULL
  162. };
  163. #define to_brport_attr(_at) container_of(_at, struct brport_attribute, attr)
  164. #define to_brport(obj) container_of(obj, struct net_bridge_port, kobj)
  165. static ssize_t brport_show(struct kobject * kobj,
  166. struct attribute * attr, char * buf)
  167. {
  168. struct brport_attribute * brport_attr = to_brport_attr(attr);
  169. struct net_bridge_port * p = to_brport(kobj);
  170. return brport_attr->show(p, buf);
  171. }
  172. static ssize_t brport_store(struct kobject * kobj,
  173. struct attribute * attr,
  174. const char * buf, size_t count)
  175. {
  176. struct brport_attribute * brport_attr = to_brport_attr(attr);
  177. struct net_bridge_port * p = to_brport(kobj);
  178. ssize_t ret = -EINVAL;
  179. char *endp;
  180. unsigned long val;
  181. if (!capable(CAP_NET_ADMIN))
  182. return -EPERM;
  183. val = simple_strtoul(buf, &endp, 0);
  184. if (endp != buf) {
  185. if (!rtnl_trylock())
  186. return restart_syscall();
  187. if (p->dev && p->br && brport_attr->store) {
  188. spin_lock_bh(&p->br->lock);
  189. ret = brport_attr->store(p, val);
  190. spin_unlock_bh(&p->br->lock);
  191. if (ret == 0)
  192. ret = count;
  193. }
  194. rtnl_unlock();
  195. }
  196. return ret;
  197. }
  198. const struct sysfs_ops brport_sysfs_ops = {
  199. .show = brport_show,
  200. .store = brport_store,
  201. };
  202. /*
  203. * Add sysfs entries to ethernet device added to a bridge.
  204. * Creates a brport subdirectory with bridge attributes.
  205. * Puts symlink in bridge's brif subdirectory
  206. */
  207. int br_sysfs_addif(struct net_bridge_port *p)
  208. {
  209. struct net_bridge *br = p->br;
  210. struct brport_attribute **a;
  211. int err;
  212. err = sysfs_create_link(&p->kobj, &br->dev->dev.kobj,
  213. SYSFS_BRIDGE_PORT_LINK);
  214. if (err)
  215. return err;
  216. for (a = brport_attrs; *a; ++a) {
  217. err = sysfs_create_file(&p->kobj, &((*a)->attr));
  218. if (err)
  219. return err;
  220. }
  221. strlcpy(p->sysfs_name, p->dev->name, IFNAMSIZ);
  222. return sysfs_create_link(br->ifobj, &p->kobj, p->sysfs_name);
  223. }
  224. /* Rename bridge's brif symlink */
  225. int br_sysfs_renameif(struct net_bridge_port *p)
  226. {
  227. struct net_bridge *br = p->br;
  228. int err;
  229. /* If a rename fails, the rollback will cause another
  230. * rename call with the existing name.
  231. */
  232. if (!strncmp(p->sysfs_name, p->dev->name, IFNAMSIZ))
  233. return 0;
  234. err = sysfs_rename_link(br->ifobj, &p->kobj,
  235. p->sysfs_name, p->dev->name);
  236. if (err)
  237. netdev_notice(br->dev, "unable to rename link %s to %s",
  238. p->sysfs_name, p->dev->name);
  239. else
  240. strlcpy(p->sysfs_name, p->dev->name, IFNAMSIZ);
  241. return err;
  242. }