br_sysfs_br.c 10 KB

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