br_stp.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:11k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  1. /*
  2.  * Spanning tree protocol; generic parts
  3.  * Linux ethernet bridge
  4.  *
  5.  * Authors:
  6.  * Lennert Buytenhek <buytenh@gnu.org>
  7.  *
  8.  * $Id: br_stp.c,v 1.4 2000/06/19 10:13:35 davem Exp $
  9.  *
  10.  * This program is free software; you can redistribute it and/or
  11.  * modify it under the terms of the GNU General Public License
  12.  * as published by the Free Software Foundation; either version
  13.  * 2 of the License, or (at your option) any later version.
  14.  */
  15. #include <linux/kernel.h>
  16. #include <linux/if_bridge.h>
  17. #include <linux/smp_lock.h>
  18. #include <asm/uaccess.h>
  19. #include "br_private.h"
  20. #include "br_private_stp.h"
  21. /* called under ioctl_lock or bridge lock */
  22. int br_is_root_bridge(struct net_bridge *br)
  23. {
  24. return !memcmp(&br->bridge_id, &br->designated_root, 8);
  25. }
  26. /* called under bridge lock */
  27. int br_is_designated_port(struct net_bridge_port *p)
  28. {
  29. return !memcmp(&p->designated_bridge, &p->br->bridge_id, 8) &&
  30. (p->designated_port == p->port_id);
  31. }
  32. /* called under ioctl_lock or bridge lock */
  33. struct net_bridge_port *br_get_port(struct net_bridge *br, int port_no)
  34. {
  35. struct net_bridge_port *p;
  36. p = br->port_list;
  37. while (p != NULL) {
  38. if (p->port_no == port_no)
  39. return p;
  40. p = p->next;
  41. }
  42. return NULL;
  43. }
  44. /* called under bridge lock */
  45. static int br_should_become_root_port(struct net_bridge_port *p, int root_port)
  46. {
  47. struct net_bridge *br;
  48. struct net_bridge_port *rp;
  49. int t;
  50. br = p->br;
  51. if (p->state == BR_STATE_DISABLED ||
  52.     br_is_designated_port(p))
  53. return 0;
  54. if (memcmp(&br->bridge_id, &p->designated_root, 8) <= 0)
  55. return 0;
  56. if (!root_port)
  57. return 1;
  58. rp = br_get_port(br, root_port);
  59. t = memcmp(&p->designated_root, &rp->designated_root, 8);
  60. if (t < 0)
  61. return 1;
  62. else if (t > 0)
  63. return 0;
  64. if (p->designated_cost + p->path_cost <
  65.     rp->designated_cost + rp->path_cost)
  66. return 1;
  67. else if (p->designated_cost + p->path_cost >
  68.  rp->designated_cost + rp->path_cost)
  69. return 0;
  70. t = memcmp(&p->designated_bridge, &rp->designated_bridge, 8);
  71. if (t < 0)
  72. return 1;
  73. else if (t > 0)
  74. return 0;
  75. if (p->designated_port < rp->designated_port)
  76. return 1;
  77. else if (p->designated_port > rp->designated_port)
  78. return 0;
  79. if (p->port_id < rp->port_id)
  80. return 1;
  81. return 0;
  82. }
  83. /* called under bridge lock */
  84. static void br_root_selection(struct net_bridge *br)
  85. {
  86. struct net_bridge_port *p;
  87. int root_port;
  88. root_port = 0;
  89. p = br->port_list;
  90. while (p != NULL) {
  91. if (br_should_become_root_port(p, root_port))
  92. root_port = p->port_no;
  93. p = p->next;
  94. }
  95. br->root_port = root_port;
  96. if (!root_port) {
  97. br->designated_root = br->bridge_id;
  98. br->root_path_cost = 0;
  99. } else {
  100. p = br_get_port(br, root_port);
  101. br->designated_root = p->designated_root;
  102. br->root_path_cost = p->designated_cost + p->path_cost;
  103. }
  104. }
  105. /* called under bridge lock */
  106. void br_become_root_bridge(struct net_bridge *br)
  107. {
  108. br->max_age = br->bridge_max_age;
  109. br->hello_time = br->bridge_hello_time;
  110. br->forward_delay = br->bridge_forward_delay;
  111. br_topology_change_detection(br);
  112. br_timer_clear(&br->tcn_timer);
  113. br_config_bpdu_generation(br);
  114. br_timer_set(&br->hello_timer, jiffies);
  115. }
  116. /* called under bridge lock */
  117. void br_transmit_config(struct net_bridge_port *p)
  118. {
  119. struct br_config_bpdu bpdu;
  120. struct net_bridge *br;
  121. if (br_timer_is_running(&p->hold_timer)) {
  122. p->config_pending = 1;
  123. return;
  124. }
  125. br = p->br;
  126. bpdu.topology_change = br->topology_change;
  127. bpdu.topology_change_ack = p->topology_change_ack;
  128. bpdu.root = br->designated_root;
  129. bpdu.root_path_cost = br->root_path_cost;
  130. bpdu.bridge_id = br->bridge_id;
  131. bpdu.port_id = p->port_id;
  132. bpdu.message_age = 0;
  133. if (!br_is_root_bridge(br)) {
  134. struct net_bridge_port *root;
  135. unsigned long age;
  136. root = br_get_port(br, br->root_port);
  137. age = br_timer_get_residue(&root->message_age_timer) + 1;
  138. bpdu.message_age = age;
  139. }
  140. bpdu.max_age = br->max_age;
  141. bpdu.hello_time = br->hello_time;
  142. bpdu.forward_delay = br->forward_delay;
  143. br_send_config_bpdu(p, &bpdu);
  144. p->topology_change_ack = 0;
  145. p->config_pending = 0;
  146. br_timer_set(&p->hold_timer, jiffies);
  147. }
  148. /* called under bridge lock */
  149. static void br_record_config_information(struct net_bridge_port *p, struct br_config_bpdu *bpdu)
  150. {
  151. p->designated_root = bpdu->root;
  152. p->designated_cost = bpdu->root_path_cost;
  153. p->designated_bridge = bpdu->bridge_id;
  154. p->designated_port = bpdu->port_id;
  155. br_timer_set(&p->message_age_timer, jiffies - bpdu->message_age);
  156. }
  157. /* called under bridge lock */
  158. static void br_record_config_timeout_values(struct net_bridge *br, struct br_config_bpdu *bpdu)
  159. {
  160. br->max_age = bpdu->max_age;
  161. br->hello_time = bpdu->hello_time;
  162. br->forward_delay = bpdu->forward_delay;
  163. br->topology_change = bpdu->topology_change;
  164. }
  165. /* called under bridge lock */
  166. void br_transmit_tcn(struct net_bridge *br)
  167. {
  168. br_send_tcn_bpdu(br_get_port(br, br->root_port));
  169. }
  170. /* called under bridge lock */
  171. static int br_should_become_designated_port(struct net_bridge_port *p)
  172. {
  173. struct net_bridge *br;
  174. int t;
  175. br = p->br;
  176. if (br_is_designated_port(p))
  177. return 1;
  178. if (memcmp(&p->designated_root, &br->designated_root, 8))
  179. return 1;
  180. if (br->root_path_cost < p->designated_cost)
  181. return 1;
  182. else if (br->root_path_cost > p->designated_cost)
  183. return 0;
  184. t = memcmp(&br->bridge_id, &p->designated_bridge, 8);
  185. if (t < 0)
  186. return 1;
  187. else if (t > 0)
  188. return 0;
  189. if (p->port_id < p->designated_port)
  190. return 1;
  191. return 0;
  192. }
  193. /* called under bridge lock */
  194. static void br_designated_port_selection(struct net_bridge *br)
  195. {
  196. struct net_bridge_port *p;
  197. p = br->port_list;
  198. while (p != NULL) {
  199. if (p->state != BR_STATE_DISABLED &&
  200.     br_should_become_designated_port(p))
  201. br_become_designated_port(p);
  202. p = p->next;
  203. }
  204. }
  205. /* called under bridge lock */
  206. static int br_supersedes_port_info(struct net_bridge_port *p, struct br_config_bpdu *bpdu)
  207. {
  208. int t;
  209. t = memcmp(&bpdu->root, &p->designated_root, 8);
  210. if (t < 0)
  211. return 1;
  212. else if (t > 0)
  213. return 0;
  214. if (bpdu->root_path_cost < p->designated_cost)
  215. return 1;
  216. else if (bpdu->root_path_cost > p->designated_cost)
  217. return 0;
  218. t = memcmp(&bpdu->bridge_id, &p->designated_bridge, 8);
  219. if (t < 0)
  220. return 1;
  221. else if (t > 0)
  222. return 0;
  223. if (memcmp(&bpdu->bridge_id, &p->br->bridge_id, 8))
  224. return 1;
  225. if (bpdu->port_id <= p->designated_port)
  226. return 1;
  227. return 0;
  228. }
  229. /* called under bridge lock */
  230. static void br_topology_change_acknowledged(struct net_bridge *br)
  231. {
  232. br->topology_change_detected = 0;
  233. br_timer_clear(&br->tcn_timer);
  234. }
  235. /* called under bridge lock */
  236. void br_topology_change_detection(struct net_bridge *br)
  237. {
  238. printk(KERN_INFO "%s: topology change detected", br->dev.name);
  239. if (br_is_root_bridge(br)) {
  240. printk(", propagating");
  241. br->topology_change = 1;
  242. br_timer_set(&br->topology_change_timer, jiffies);
  243. } else if (!br->topology_change_detected) {
  244. printk(", sending tcn bpdu");
  245. br_transmit_tcn(br);
  246. br_timer_set(&br->tcn_timer, jiffies);
  247. }
  248. printk("n");
  249. br->topology_change_detected = 1;
  250. }
  251. /* called under bridge lock */
  252. void br_config_bpdu_generation(struct net_bridge *br)
  253. {
  254. struct net_bridge_port *p;
  255. p = br->port_list;
  256. while (p != NULL) {
  257. if (p->state != BR_STATE_DISABLED &&
  258.     br_is_designated_port(p))
  259. br_transmit_config(p);
  260. p = p->next;
  261. }
  262. }
  263. /* called under bridge lock */
  264. static void br_reply(struct net_bridge_port *p)
  265. {
  266. br_transmit_config(p);
  267. }
  268. /* called under bridge lock */
  269. void br_configuration_update(struct net_bridge *br)
  270. {
  271. br_root_selection(br);
  272. br_designated_port_selection(br);
  273. }
  274. /* called under bridge lock */
  275. void br_become_designated_port(struct net_bridge_port *p)
  276. {
  277. struct net_bridge *br;
  278. br = p->br;
  279. p->designated_root = br->designated_root;
  280. p->designated_cost = br->root_path_cost;
  281. p->designated_bridge = br->bridge_id;
  282. p->designated_port = p->port_id;
  283. }
  284. /* called under bridge lock */
  285. static void br_make_blocking(struct net_bridge_port *p)
  286. {
  287. if (p->state != BR_STATE_DISABLED &&
  288.     p->state != BR_STATE_BLOCKING) {
  289. if (p->state == BR_STATE_FORWARDING ||
  290.     p->state == BR_STATE_LEARNING)
  291. br_topology_change_detection(p->br);
  292. printk(KERN_INFO "%s: port %i(%s) entering %s staten",
  293.        p->br->dev.name, p->port_no, p->dev->name, "blocking");
  294. p->state = BR_STATE_BLOCKING;
  295. br_timer_clear(&p->forward_delay_timer);
  296. }
  297. }
  298. /* called under bridge lock */
  299. static void br_make_forwarding(struct net_bridge_port *p)
  300. {
  301. if (p->state == BR_STATE_BLOCKING) {
  302. if (p->br->stp_enabled) {
  303. printk(KERN_INFO "%s: port %i(%s) entering %s staten",
  304.        p->br->dev.name, p->port_no, p->dev->name,
  305.        "listening");
  306. p->state = BR_STATE_LISTENING;
  307. } else {
  308. printk(KERN_INFO "%s: port %i(%s) entering %s staten",
  309.        p->br->dev.name, p->port_no, p->dev->name,
  310.        "learning");
  311. p->state = BR_STATE_LEARNING;
  312. }
  313. br_timer_set(&p->forward_delay_timer, jiffies);
  314. }
  315. }
  316. /* called under bridge lock */
  317. void br_port_state_selection(struct net_bridge *br)
  318. {
  319. struct net_bridge_port *p;
  320. p = br->port_list;
  321. while (p != NULL) {
  322. if (p->state != BR_STATE_DISABLED) {
  323. if (p->port_no == br->root_port) {
  324. p->config_pending = 0;
  325. p->topology_change_ack = 0;
  326. br_make_forwarding(p);
  327. } else if (br_is_designated_port(p)) {
  328. br_timer_clear(&p->message_age_timer);
  329. br_make_forwarding(p);
  330. } else {
  331. p->config_pending = 0;
  332. p->topology_change_ack = 0;
  333. br_make_blocking(p);
  334. }
  335. }
  336. p = p->next;
  337. }
  338. }
  339. /* called under bridge lock */
  340. static void br_topology_change_acknowledge(struct net_bridge_port *p)
  341. {
  342. p->topology_change_ack = 1;
  343. br_transmit_config(p);
  344. }
  345. /* lock-safe */
  346. void br_received_config_bpdu(struct net_bridge_port *p, struct br_config_bpdu *bpdu)
  347. {
  348. struct net_bridge *br;
  349. int was_root;
  350. if (p->state == BR_STATE_DISABLED)
  351. return;
  352. br = p->br;
  353. read_lock(&br->lock);
  354. was_root = br_is_root_bridge(br);
  355. if (br_supersedes_port_info(p, bpdu)) {
  356. br_record_config_information(p, bpdu);
  357. br_configuration_update(br);
  358. br_port_state_selection(br);
  359. if (!br_is_root_bridge(br) && was_root) {
  360. br_timer_clear(&br->hello_timer);
  361. if (br->topology_change_detected) {
  362. br_timer_clear(&br->topology_change_timer);
  363. br_transmit_tcn(br);
  364. br_timer_set(&br->tcn_timer, jiffies);
  365. }
  366. }
  367. if (p->port_no == br->root_port) {
  368. br_record_config_timeout_values(br, bpdu);
  369. br_config_bpdu_generation(br);
  370. if (bpdu->topology_change_ack)
  371. br_topology_change_acknowledged(br);
  372. }
  373. } else if (br_is_designated_port(p)) {
  374. br_reply(p);
  375. }
  376. read_unlock(&br->lock);
  377. }
  378. /* lock-safe */
  379. void br_received_tcn_bpdu(struct net_bridge_port *p)
  380. {
  381. read_lock(&p->br->lock);
  382. if (p->state != BR_STATE_DISABLED &&
  383.     br_is_designated_port(p)) {
  384. printk(KERN_INFO "%s: received tcn bpdu on port %i(%s)n",
  385.        p->br->dev.name, p->port_no, p->dev->name);
  386. br_topology_change_detection(p->br);
  387. br_topology_change_acknowledge(p);
  388. }
  389. read_unlock(&p->br->lock);
  390. }