br_sysfs_br.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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 <linux/times.h>
  20. #include "br_private.h"
  21. #define to_class_dev(obj) container_of(obj,struct class_device,kobj)
  22. #define to_net_dev(class) container_of(class, struct net_device, class_dev)
  23. #define to_bridge(cd) ((struct net_bridge *)(to_net_dev(cd)->priv))
  24. /*
  25. * Common code for storing bridge parameters.
  26. */
  27. static ssize_t store_bridge_parm(struct class_device *cd,
  28. const char *buf, size_t len,
  29. void (*set)(struct net_bridge *, unsigned long))
  30. {
  31. struct net_bridge *br = to_bridge(cd);
  32. char *endp;
  33. unsigned long val;
  34. if (!capable(CAP_NET_ADMIN))
  35. return -EPERM;
  36. val = simple_strtoul(buf, &endp, 0);
  37. if (endp == buf)
  38. return -EINVAL;
  39. spin_lock_bh(&br->lock);
  40. (*set)(br, val);
  41. spin_unlock_bh(&br->lock);
  42. return len;
  43. }
  44. static ssize_t show_forward_delay(struct class_device *cd, char *buf)
  45. {
  46. struct net_bridge *br = to_bridge(cd);
  47. return sprintf(buf, "%lu\n", jiffies_to_clock_t(br->forward_delay));
  48. }
  49. static void set_forward_delay(struct net_bridge *br, unsigned long val)
  50. {
  51. unsigned long delay = clock_t_to_jiffies(val);
  52. br->forward_delay = delay;
  53. if (br_is_root_bridge(br))
  54. br->bridge_forward_delay = delay;
  55. }
  56. static ssize_t store_forward_delay(struct class_device *cd, const char *buf,
  57. size_t len)
  58. {
  59. return store_bridge_parm(cd, buf, len, set_forward_delay);
  60. }
  61. static CLASS_DEVICE_ATTR(forward_delay, S_IRUGO | S_IWUSR,
  62. show_forward_delay, store_forward_delay);
  63. static ssize_t show_hello_time(struct class_device *cd, char *buf)
  64. {
  65. return sprintf(buf, "%lu\n",
  66. jiffies_to_clock_t(to_bridge(cd)->hello_time));
  67. }
  68. static void set_hello_time(struct net_bridge *br, unsigned long val)
  69. {
  70. unsigned long t = clock_t_to_jiffies(val);
  71. br->hello_time = t;
  72. if (br_is_root_bridge(br))
  73. br->bridge_hello_time = t;
  74. }
  75. static ssize_t store_hello_time(struct class_device *cd, const char *buf,
  76. size_t len)
  77. {
  78. return store_bridge_parm(cd, buf, len, set_hello_time);
  79. }
  80. static CLASS_DEVICE_ATTR(hello_time, S_IRUGO | S_IWUSR, show_hello_time,
  81. store_hello_time);
  82. static ssize_t show_max_age(struct class_device *cd, char *buf)
  83. {
  84. return sprintf(buf, "%lu\n",
  85. jiffies_to_clock_t(to_bridge(cd)->max_age));
  86. }
  87. static void set_max_age(struct net_bridge *br, unsigned long val)
  88. {
  89. unsigned long t = clock_t_to_jiffies(val);
  90. br->max_age = t;
  91. if (br_is_root_bridge(br))
  92. br->bridge_max_age = t;
  93. }
  94. static ssize_t store_max_age(struct class_device *cd, const char *buf,
  95. size_t len)
  96. {
  97. return store_bridge_parm(cd, buf, len, set_max_age);
  98. }
  99. static CLASS_DEVICE_ATTR(max_age, S_IRUGO | S_IWUSR, show_max_age,
  100. store_max_age);
  101. static ssize_t show_ageing_time(struct class_device *cd, char *buf)
  102. {
  103. struct net_bridge *br = to_bridge(cd);
  104. return sprintf(buf, "%lu\n", jiffies_to_clock_t(br->ageing_time));
  105. }
  106. static void set_ageing_time(struct net_bridge *br, unsigned long val)
  107. {
  108. br->ageing_time = clock_t_to_jiffies(val);
  109. }
  110. static ssize_t store_ageing_time(struct class_device *cd, const char *buf,
  111. size_t len)
  112. {
  113. return store_bridge_parm(cd, buf, len, set_ageing_time);
  114. }
  115. static CLASS_DEVICE_ATTR(ageing_time, S_IRUGO | S_IWUSR, show_ageing_time,
  116. store_ageing_time);
  117. static ssize_t show_stp_state(struct class_device *cd, char *buf)
  118. {
  119. struct net_bridge *br = to_bridge(cd);
  120. return sprintf(buf, "%d\n", br->stp_enabled);
  121. }
  122. static void set_stp_state(struct net_bridge *br, unsigned long val)
  123. {
  124. br->stp_enabled = val;
  125. }
  126. static ssize_t store_stp_state(struct class_device *cd,
  127. const char *buf, size_t len)
  128. {
  129. return store_bridge_parm(cd, buf, len, set_stp_state);
  130. }
  131. static CLASS_DEVICE_ATTR(stp_state, S_IRUGO | S_IWUSR, show_stp_state,
  132. store_stp_state);
  133. static ssize_t show_priority(struct class_device *cd, char *buf)
  134. {
  135. struct net_bridge *br = to_bridge(cd);
  136. return sprintf(buf, "%d\n",
  137. (br->bridge_id.prio[0] << 8) | br->bridge_id.prio[1]);
  138. }
  139. static void set_priority(struct net_bridge *br, unsigned long val)
  140. {
  141. br_stp_set_bridge_priority(br, (u16) val);
  142. }
  143. static ssize_t store_priority(struct class_device *cd,
  144. const char *buf, size_t len)
  145. {
  146. return store_bridge_parm(cd, buf, len, set_priority);
  147. }
  148. static CLASS_DEVICE_ATTR(priority, S_IRUGO | S_IWUSR, show_priority,
  149. store_priority);
  150. static ssize_t show_root_id(struct class_device *cd, char *buf)
  151. {
  152. return br_show_bridge_id(buf, &to_bridge(cd)->designated_root);
  153. }
  154. static CLASS_DEVICE_ATTR(root_id, S_IRUGO, show_root_id, NULL);
  155. static ssize_t show_bridge_id(struct class_device *cd, char *buf)
  156. {
  157. return br_show_bridge_id(buf, &to_bridge(cd)->bridge_id);
  158. }
  159. static CLASS_DEVICE_ATTR(bridge_id, S_IRUGO, show_bridge_id, NULL);
  160. static ssize_t show_root_port(struct class_device *cd, char *buf)
  161. {
  162. return sprintf(buf, "%d\n", to_bridge(cd)->root_port);
  163. }
  164. static CLASS_DEVICE_ATTR(root_port, S_IRUGO, show_root_port, NULL);
  165. static ssize_t show_root_path_cost(struct class_device *cd, char *buf)
  166. {
  167. return sprintf(buf, "%d\n", to_bridge(cd)->root_path_cost);
  168. }
  169. static CLASS_DEVICE_ATTR(root_path_cost, S_IRUGO, show_root_path_cost, NULL);
  170. static ssize_t show_topology_change(struct class_device *cd, char *buf)
  171. {
  172. return sprintf(buf, "%d\n", to_bridge(cd)->topology_change);
  173. }
  174. static CLASS_DEVICE_ATTR(topology_change, S_IRUGO, show_topology_change, NULL);
  175. static ssize_t show_topology_change_detected(struct class_device *cd, char *buf)
  176. {
  177. struct net_bridge *br = to_bridge(cd);
  178. return sprintf(buf, "%d\n", br->topology_change_detected);
  179. }
  180. static CLASS_DEVICE_ATTR(topology_change_detected, S_IRUGO, show_topology_change_detected, NULL);
  181. static ssize_t show_hello_timer(struct class_device *cd, char *buf)
  182. {
  183. struct net_bridge *br = to_bridge(cd);
  184. return sprintf(buf, "%ld\n", br_timer_value(&br->hello_timer));
  185. }
  186. static CLASS_DEVICE_ATTR(hello_timer, S_IRUGO, show_hello_timer, NULL);
  187. static ssize_t show_tcn_timer(struct class_device *cd, char *buf)
  188. {
  189. struct net_bridge *br = to_bridge(cd);
  190. return sprintf(buf, "%ld\n", br_timer_value(&br->tcn_timer));
  191. }
  192. static CLASS_DEVICE_ATTR(tcn_timer, S_IRUGO, show_tcn_timer, NULL);
  193. static ssize_t show_topology_change_timer(struct class_device *cd, char *buf)
  194. {
  195. struct net_bridge *br = to_bridge(cd);
  196. return sprintf(buf, "%ld\n", br_timer_value(&br->topology_change_timer));
  197. }
  198. static CLASS_DEVICE_ATTR(topology_change_timer, S_IRUGO, show_topology_change_timer, NULL);
  199. static ssize_t show_gc_timer(struct class_device *cd, char *buf)
  200. {
  201. struct net_bridge *br = to_bridge(cd);
  202. return sprintf(buf, "%ld\n", br_timer_value(&br->gc_timer));
  203. }
  204. static CLASS_DEVICE_ATTR(gc_timer, S_IRUGO, show_gc_timer, NULL);
  205. static struct attribute *bridge_attrs[] = {
  206. &class_device_attr_forward_delay.attr,
  207. &class_device_attr_hello_time.attr,
  208. &class_device_attr_max_age.attr,
  209. &class_device_attr_ageing_time.attr,
  210. &class_device_attr_stp_state.attr,
  211. &class_device_attr_priority.attr,
  212. &class_device_attr_bridge_id.attr,
  213. &class_device_attr_root_id.attr,
  214. &class_device_attr_root_path_cost.attr,
  215. &class_device_attr_root_port.attr,
  216. &class_device_attr_topology_change.attr,
  217. &class_device_attr_topology_change_detected.attr,
  218. &class_device_attr_hello_timer.attr,
  219. &class_device_attr_tcn_timer.attr,
  220. &class_device_attr_topology_change_timer.attr,
  221. &class_device_attr_gc_timer.attr,
  222. NULL
  223. };
  224. static struct attribute_group bridge_group = {
  225. .name = SYSFS_BRIDGE_ATTR,
  226. .attrs = bridge_attrs,
  227. };
  228. /*
  229. * Export the forwarding information table as a binary file
  230. * The records are struct __fdb_entry.
  231. *
  232. * Returns the number of bytes read.
  233. */
  234. static ssize_t brforward_read(struct kobject *kobj, char *buf,
  235. loff_t off, size_t count)
  236. {
  237. struct class_device *cdev = to_class_dev(kobj);
  238. struct net_bridge *br = to_bridge(cdev);
  239. int n;
  240. /* must read whole records */
  241. if (off % sizeof(struct __fdb_entry) != 0)
  242. return -EINVAL;
  243. n = br_fdb_fillbuf(br, buf,
  244. count / sizeof(struct __fdb_entry),
  245. off / sizeof(struct __fdb_entry));
  246. if (n > 0)
  247. n *= sizeof(struct __fdb_entry);
  248. return n;
  249. }
  250. static struct bin_attribute bridge_forward = {
  251. .attr = { .name = SYSFS_BRIDGE_FDB,
  252. .mode = S_IRUGO,
  253. .owner = THIS_MODULE, },
  254. .read = brforward_read,
  255. };
  256. /*
  257. * Add entries in sysfs onto the existing network class device
  258. * for the bridge.
  259. * Adds a attribute group "bridge" containing tuning parameters.
  260. * Binary attribute containing the forward table
  261. * Sub directory to hold links to interfaces.
  262. *
  263. * Note: the ifobj exists only to be a subdirectory
  264. * to hold links. The ifobj exists in same data structure
  265. * as it's parent the bridge so reference counting works.
  266. */
  267. int br_sysfs_addbr(struct net_device *dev)
  268. {
  269. struct kobject *brobj = &dev->class_dev.kobj;
  270. struct net_bridge *br = netdev_priv(dev);
  271. int err;
  272. err = sysfs_create_group(brobj, &bridge_group);
  273. if (err) {
  274. pr_info("%s: can't create group %s/%s\n",
  275. __FUNCTION__, dev->name, bridge_group.name);
  276. goto out1;
  277. }
  278. err = sysfs_create_bin_file(brobj, &bridge_forward);
  279. if (err) {
  280. pr_info("%s: can't create attribue file %s/%s\n",
  281. __FUNCTION__, dev->name, bridge_forward.attr.name);
  282. goto out2;
  283. }
  284. kobject_set_name(&br->ifobj, SYSFS_BRIDGE_PORT_SUBDIR);
  285. br->ifobj.ktype = NULL;
  286. br->ifobj.kset = NULL;
  287. br->ifobj.parent = brobj;
  288. err = kobject_register(&br->ifobj);
  289. if (err) {
  290. pr_info("%s: can't add kobject (directory) %s/%s\n",
  291. __FUNCTION__, dev->name, br->ifobj.name);
  292. goto out3;
  293. }
  294. return 0;
  295. out3:
  296. sysfs_remove_bin_file(&dev->class_dev.kobj, &bridge_forward);
  297. out2:
  298. sysfs_remove_group(&dev->class_dev.kobj, &bridge_group);
  299. out1:
  300. return err;
  301. }
  302. void br_sysfs_delbr(struct net_device *dev)
  303. {
  304. struct kobject *kobj = &dev->class_dev.kobj;
  305. struct net_bridge *br = netdev_priv(dev);
  306. kobject_unregister(&br->ifobj);
  307. sysfs_remove_bin_file(kobj, &bridge_forward);
  308. sysfs_remove_group(kobj, &bridge_group);
  309. }