rcu: split list.h and move rcu-protected lists into rculist.h
[linux-3.10.git] / net / bridge / br_stp.c
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/rculist.h>
17
18 #include "br_private.h"
19 #include "br_private_stp.h"
20
21 /* since time values in bpdu are in jiffies and then scaled (1/256)
22  * before sending, make sure that is at least one.
23  */
24 #define MESSAGE_AGE_INCR        ((HZ < 256) ? 1 : (HZ/256))
25
26 static const char *br_port_state_names[] = {
27         [BR_STATE_DISABLED] = "disabled",
28         [BR_STATE_LISTENING] = "listening",
29         [BR_STATE_LEARNING] = "learning",
30         [BR_STATE_FORWARDING] = "forwarding",
31         [BR_STATE_BLOCKING] = "blocking",
32 };
33
34 void br_log_state(const struct net_bridge_port *p)
35 {
36         pr_info("%s: port %d(%s) entering %s state\n",
37                 p->br->dev->name, p->port_no, p->dev->name,
38                 br_port_state_names[p->state]);
39
40 }
41
42 /* called under bridge lock */
43 struct net_bridge_port *br_get_port(struct net_bridge *br, u16 port_no)
44 {
45         struct net_bridge_port *p;
46
47         list_for_each_entry_rcu(p, &br->port_list, list) {
48                 if (p->port_no == port_no)
49                         return p;
50         }
51
52         return NULL;
53 }
54
55 /* called under bridge lock */
56 static int br_should_become_root_port(const struct net_bridge_port *p,
57                                       u16 root_port)
58 {
59         struct net_bridge *br;
60         struct net_bridge_port *rp;
61         int t;
62
63         br = p->br;
64         if (p->state == BR_STATE_DISABLED ||
65             br_is_designated_port(p))
66                 return 0;
67
68         if (memcmp(&br->bridge_id, &p->designated_root, 8) <= 0)
69                 return 0;
70
71         if (!root_port)
72                 return 1;
73
74         rp = br_get_port(br, root_port);
75
76         t = memcmp(&p->designated_root, &rp->designated_root, 8);
77         if (t < 0)
78                 return 1;
79         else if (t > 0)
80                 return 0;
81
82         if (p->designated_cost + p->path_cost <
83             rp->designated_cost + rp->path_cost)
84                 return 1;
85         else if (p->designated_cost + p->path_cost >
86                  rp->designated_cost + rp->path_cost)
87                 return 0;
88
89         t = memcmp(&p->designated_bridge, &rp->designated_bridge, 8);
90         if (t < 0)
91                 return 1;
92         else if (t > 0)
93                 return 0;
94
95         if (p->designated_port < rp->designated_port)
96                 return 1;
97         else if (p->designated_port > rp->designated_port)
98                 return 0;
99
100         if (p->port_id < rp->port_id)
101                 return 1;
102
103         return 0;
104 }
105
106 /* called under bridge lock */
107 static void br_root_selection(struct net_bridge *br)
108 {
109         struct net_bridge_port *p;
110         u16 root_port = 0;
111
112         list_for_each_entry(p, &br->port_list, list) {
113                 if (br_should_become_root_port(p, root_port))
114                         root_port = p->port_no;
115
116         }
117
118         br->root_port = root_port;
119
120         if (!root_port) {
121                 br->designated_root = br->bridge_id;
122                 br->root_path_cost = 0;
123         } else {
124                 p = br_get_port(br, root_port);
125                 br->designated_root = p->designated_root;
126                 br->root_path_cost = p->designated_cost + p->path_cost;
127         }
128 }
129
130 /* called under bridge lock */
131 void br_become_root_bridge(struct net_bridge *br)
132 {
133         br->max_age = br->bridge_max_age;
134         br->hello_time = br->bridge_hello_time;
135         br->forward_delay = br->bridge_forward_delay;
136         br_topology_change_detection(br);
137         del_timer(&br->tcn_timer);
138
139         if (br->dev->flags & IFF_UP) {
140                 br_config_bpdu_generation(br);
141                 mod_timer(&br->hello_timer, jiffies + br->hello_time);
142         }
143 }
144
145 /* called under bridge lock */
146 void br_transmit_config(struct net_bridge_port *p)
147 {
148         struct br_config_bpdu bpdu;
149         struct net_bridge *br;
150
151
152         if (timer_pending(&p->hold_timer)) {
153                 p->config_pending = 1;
154                 return;
155         }
156
157         br = p->br;
158
159         bpdu.topology_change = br->topology_change;
160         bpdu.topology_change_ack = p->topology_change_ack;
161         bpdu.root = br->designated_root;
162         bpdu.root_path_cost = br->root_path_cost;
163         bpdu.bridge_id = br->bridge_id;
164         bpdu.port_id = p->port_id;
165         if (br_is_root_bridge(br))
166                 bpdu.message_age = 0;
167         else {
168                 struct net_bridge_port *root
169                         = br_get_port(br, br->root_port);
170                 bpdu.message_age = br->max_age
171                         - (root->message_age_timer.expires - jiffies)
172                         + MESSAGE_AGE_INCR;
173         }
174         bpdu.max_age = br->max_age;
175         bpdu.hello_time = br->hello_time;
176         bpdu.forward_delay = br->forward_delay;
177
178         if (bpdu.message_age < br->max_age) {
179                 br_send_config_bpdu(p, &bpdu);
180                 p->topology_change_ack = 0;
181                 p->config_pending = 0;
182                 mod_timer(&p->hold_timer,
183                           round_jiffies(jiffies + BR_HOLD_TIME));
184         }
185 }
186
187 /* called under bridge lock */
188 static inline void br_record_config_information(struct net_bridge_port *p,
189                                                 const struct br_config_bpdu *bpdu)
190 {
191         p->designated_root = bpdu->root;
192         p->designated_cost = bpdu->root_path_cost;
193         p->designated_bridge = bpdu->bridge_id;
194         p->designated_port = bpdu->port_id;
195
196         mod_timer(&p->message_age_timer, jiffies
197                   + (p->br->max_age - bpdu->message_age));
198 }
199
200 /* called under bridge lock */
201 static inline void br_record_config_timeout_values(struct net_bridge *br,
202                                             const struct br_config_bpdu *bpdu)
203 {
204         br->max_age = bpdu->max_age;
205         br->hello_time = bpdu->hello_time;
206         br->forward_delay = bpdu->forward_delay;
207         br->topology_change = bpdu->topology_change;
208 }
209
210 /* called under bridge lock */
211 void br_transmit_tcn(struct net_bridge *br)
212 {
213         br_send_tcn_bpdu(br_get_port(br, br->root_port));
214 }
215
216 /* called under bridge lock */
217 static int br_should_become_designated_port(const struct net_bridge_port *p)
218 {
219         struct net_bridge *br;
220         int t;
221
222         br = p->br;
223         if (br_is_designated_port(p))
224                 return 1;
225
226         if (memcmp(&p->designated_root, &br->designated_root, 8))
227                 return 1;
228
229         if (br->root_path_cost < p->designated_cost)
230                 return 1;
231         else if (br->root_path_cost > p->designated_cost)
232                 return 0;
233
234         t = memcmp(&br->bridge_id, &p->designated_bridge, 8);
235         if (t < 0)
236                 return 1;
237         else if (t > 0)
238                 return 0;
239
240         if (p->port_id < p->designated_port)
241                 return 1;
242
243         return 0;
244 }
245
246 /* called under bridge lock */
247 static void br_designated_port_selection(struct net_bridge *br)
248 {
249         struct net_bridge_port *p;
250
251         list_for_each_entry(p, &br->port_list, list) {
252                 if (p->state != BR_STATE_DISABLED &&
253                     br_should_become_designated_port(p))
254                         br_become_designated_port(p);
255
256         }
257 }
258
259 /* called under bridge lock */
260 static int br_supersedes_port_info(struct net_bridge_port *p, struct br_config_bpdu *bpdu)
261 {
262         int t;
263
264         t = memcmp(&bpdu->root, &p->designated_root, 8);
265         if (t < 0)
266                 return 1;
267         else if (t > 0)
268                 return 0;
269
270         if (bpdu->root_path_cost < p->designated_cost)
271                 return 1;
272         else if (bpdu->root_path_cost > p->designated_cost)
273                 return 0;
274
275         t = memcmp(&bpdu->bridge_id, &p->designated_bridge, 8);
276         if (t < 0)
277                 return 1;
278         else if (t > 0)
279                 return 0;
280
281         if (memcmp(&bpdu->bridge_id, &p->br->bridge_id, 8))
282                 return 1;
283
284         if (bpdu->port_id <= p->designated_port)
285                 return 1;
286
287         return 0;
288 }
289
290 /* called under bridge lock */
291 static inline void br_topology_change_acknowledged(struct net_bridge *br)
292 {
293         br->topology_change_detected = 0;
294         del_timer(&br->tcn_timer);
295 }
296
297 /* called under bridge lock */
298 void br_topology_change_detection(struct net_bridge *br)
299 {
300         int isroot = br_is_root_bridge(br);
301
302         pr_info("%s: topology change detected, %s\n", br->dev->name,
303                 isroot ? "propagating" : "sending tcn bpdu");
304
305         if (isroot) {
306                 br->topology_change = 1;
307                 mod_timer(&br->topology_change_timer, jiffies
308                           + br->bridge_forward_delay + br->bridge_max_age);
309         } else if (!br->topology_change_detected) {
310                 br_transmit_tcn(br);
311                 mod_timer(&br->tcn_timer, jiffies + br->bridge_hello_time);
312         }
313
314         br->topology_change_detected = 1;
315 }
316
317 /* called under bridge lock */
318 void br_config_bpdu_generation(struct net_bridge *br)
319 {
320         struct net_bridge_port *p;
321
322         list_for_each_entry(p, &br->port_list, list) {
323                 if (p->state != BR_STATE_DISABLED &&
324                     br_is_designated_port(p))
325                         br_transmit_config(p);
326         }
327 }
328
329 /* called under bridge lock */
330 static inline void br_reply(struct net_bridge_port *p)
331 {
332         br_transmit_config(p);
333 }
334
335 /* called under bridge lock */
336 void br_configuration_update(struct net_bridge *br)
337 {
338         br_root_selection(br);
339         br_designated_port_selection(br);
340 }
341
342 /* called under bridge lock */
343 void br_become_designated_port(struct net_bridge_port *p)
344 {
345         struct net_bridge *br;
346
347         br = p->br;
348         p->designated_root = br->designated_root;
349         p->designated_cost = br->root_path_cost;
350         p->designated_bridge = br->bridge_id;
351         p->designated_port = p->port_id;
352 }
353
354
355 /* called under bridge lock */
356 static void br_make_blocking(struct net_bridge_port *p)
357 {
358         if (p->state != BR_STATE_DISABLED &&
359             p->state != BR_STATE_BLOCKING) {
360                 if (p->state == BR_STATE_FORWARDING ||
361                     p->state == BR_STATE_LEARNING)
362                         br_topology_change_detection(p->br);
363
364                 p->state = BR_STATE_BLOCKING;
365                 br_log_state(p);
366                 del_timer(&p->forward_delay_timer);
367         }
368 }
369
370 /* called under bridge lock */
371 static void br_make_forwarding(struct net_bridge_port *p)
372 {
373         if (p->state == BR_STATE_BLOCKING) {
374                 if (p->br->stp_enabled == BR_KERNEL_STP)
375                         p->state = BR_STATE_LISTENING;
376                 else
377                         p->state = BR_STATE_LEARNING;
378
379                 br_log_state(p);
380                 mod_timer(&p->forward_delay_timer, jiffies + p->br->forward_delay);     }
381 }
382
383 /* called under bridge lock */
384 void br_port_state_selection(struct net_bridge *br)
385 {
386         struct net_bridge_port *p;
387
388         /* Don't change port states if userspace is handling STP */
389         if (br->stp_enabled == BR_USER_STP)
390                 return;
391
392         list_for_each_entry(p, &br->port_list, list) {
393                 if (p->state != BR_STATE_DISABLED) {
394                         if (p->port_no == br->root_port) {
395                                 p->config_pending = 0;
396                                 p->topology_change_ack = 0;
397                                 br_make_forwarding(p);
398                         } else if (br_is_designated_port(p)) {
399                                 del_timer(&p->message_age_timer);
400                                 br_make_forwarding(p);
401                         } else {
402                                 p->config_pending = 0;
403                                 p->topology_change_ack = 0;
404                                 br_make_blocking(p);
405                         }
406                 }
407
408         }
409 }
410
411 /* called under bridge lock */
412 static inline void br_topology_change_acknowledge(struct net_bridge_port *p)
413 {
414         p->topology_change_ack = 1;
415         br_transmit_config(p);
416 }
417
418 /* called under bridge lock */
419 void br_received_config_bpdu(struct net_bridge_port *p, struct br_config_bpdu *bpdu)
420 {
421         struct net_bridge *br;
422         int was_root;
423
424         br = p->br;
425         was_root = br_is_root_bridge(br);
426
427         if (br_supersedes_port_info(p, bpdu)) {
428                 br_record_config_information(p, bpdu);
429                 br_configuration_update(br);
430                 br_port_state_selection(br);
431
432                 if (!br_is_root_bridge(br) && was_root) {
433                         del_timer(&br->hello_timer);
434                         if (br->topology_change_detected) {
435                                 del_timer(&br->topology_change_timer);
436                                 br_transmit_tcn(br);
437
438                                 mod_timer(&br->tcn_timer,
439                                           jiffies + br->bridge_hello_time);
440                         }
441                 }
442
443                 if (p->port_no == br->root_port) {
444                         br_record_config_timeout_values(br, bpdu);
445                         br_config_bpdu_generation(br);
446                         if (bpdu->topology_change_ack)
447                                 br_topology_change_acknowledged(br);
448                 }
449         } else if (br_is_designated_port(p)) {
450                 br_reply(p);
451         }
452 }
453
454 /* called under bridge lock */
455 void br_received_tcn_bpdu(struct net_bridge_port *p)
456 {
457         if (br_is_designated_port(p)) {
458                 pr_info("%s: received tcn bpdu on port %i(%s)\n",
459                        p->br->dev->name, p->port_no, p->dev->name);
460
461                 br_topology_change_detection(p->br);
462                 br_topology_change_acknowledge(p);
463         }
464 }