1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
|
/************************************************************************
* RSTP library - Rapid Spanning Tree (802.1t, 802.1w)
* Copyright (C) 2001-2003 Optical Access
* Author: Alex Rozin
*
* This file is part of RSTP library.
*
* RSTP library is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by the
* Free Software Foundation; version 2.1
*
* RSTP library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
* General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with RSTP library; see the file COPYING. If not, write to the Free
* Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
* 02111-1307, USA.
**********************************************************************/
/* Port Role Transitions state machine : 17.24 */
#include "base.h"
#include "stpm.h"
#define STATES { \
CHOOSE(INIT_PORT), \
CHOOSE(BLOCK_PORT), \
CHOOSE(BLOCKED_PORT), \
CHOOSE(BACKUP_PORT), \
CHOOSE(ROOT_PROPOSED), \
CHOOSE(ROOT_AGREED), \
CHOOSE(REROOT), \
CHOOSE(ROOT_PORT), \
CHOOSE(REROOTED), \
CHOOSE(ROOT_LEARN), \
CHOOSE(ROOT_FORWARD), \
CHOOSE(DESIGNATED_PROPOSE), \
CHOOSE(DESIGNATED_SYNCED), \
CHOOSE(DESIGNATED_RETIRED), \
CHOOSE(DESIGNATED_PORT), \
CHOOSE(DESIGNATED_LISTEN), \
CHOOSE(DESIGNATED_LEARN), \
CHOOSE(DESIGNATED_FORWARD) \
}
#define GET_STATE_NAME STP_roletrns_get_state_name
#include "choose.h"
static void
setSyncBridge (STATE_MACH_T *this)
{
register PORT_T* port;
for (port = this->owner.port->owner->ports; port; port = port->next) {
port->sync = True; /* in ROOT_PROPOSED (setSyncBridge) */
}
}
static void
setReRootBridge (STATE_MACH_T *this)
{
register PORT_T* port;
for (port = this->owner.port->owner->ports; port; port = port->next) {
port->reRoot = True; /* In setReRootBridge */
}
}
static Bool
compute_all_synced (PORT_T* this)
{
register PORT_T* port;
for (port = this->owner->ports; port; port = port->next) {
if (port->port_index == this->port_index) continue;
if (! port->synced) {
return False;
}
}
return True;
}
static Bool
compute_re_rooted (PORT_T* this)
{
register PORT_T* port;
for (port = this->owner->ports; port; port = port->next) {
if (port->port_index == this->port_index) continue;
if (port->rrWhile) {
return False;
}
}
return True;
}
void
STP_roletrns_enter_state (STATE_MACH_T* this)
{
register PORT_T* port = this->owner.port;
register STPM_T* stpm;
stpm = port->owner;
switch (this->State) {
case BEGIN:
case INIT_PORT:
#if 0 /* due 802.1y Z.4 */
port->role = DisabledPort;
#else
port->role = port->selectedRole = DisabledPort;
port->reselect = True;
#endif
port->synced = False; /* in INIT */
port->sync = True; /* in INIT */
port->reRoot = True; /* in INIT_PORT */
port->rrWhile = stpm->rootTimes.ForwardDelay;
port->fdWhile = stpm->rootTimes.ForwardDelay;
port->rbWhile = 0;
#ifdef STP_DBG
if (this->debug)
STP_port_trace_flags ("after init", port);
#endif
break;
case BLOCK_PORT:
port->role = port->selectedRole;
port->learn =
port->forward = False;
break;
case BLOCKED_PORT:
port->fdWhile = stpm->rootTimes.ForwardDelay;
port->synced = True; /* In BLOCKED_PORT */
port->rrWhile = 0;
port->sync = port->reRoot = False; /* BLOCKED_PORT */
break;
case BACKUP_PORT:
port->rbWhile = 2 * stpm->rootTimes.HelloTime;
break;
/* 17.23.2 */
case ROOT_PROPOSED:
setSyncBridge (this);
port->proposed = False;
#ifdef STP_DBG
if (this->debug)
STP_port_trace_flags ("ROOT_PROPOSED", port);
#endif
break;
case ROOT_AGREED:
port->proposed = port->sync = False; /* in ROOT_AGREED */
port->synced = True; /* In ROOT_AGREED */
port->newInfo = True;
#ifdef STP_DBG
if (this->debug)
STP_port_trace_flags ("ROOT_AGREED", port);
#endif
break;
case REROOT:
setReRootBridge (this);
#ifdef STP_DBG
if (this->debug)
STP_port_trace_flags ("REROOT", port);
#endif
break;
case ROOT_PORT:
port->role = RootPort;
port->rrWhile = stpm->rootTimes.ForwardDelay;
#ifdef STP_DBG
if (this->debug)
STP_port_trace_flags ("ROOT_PORT", port);
#endif
break;
case REROOTED:
port->reRoot = False; /* In REROOTED */
#ifdef STP_DBG
if (this->debug)
STP_port_trace_flags ("REROOTED", port);
#endif
break;
case ROOT_LEARN:
port->fdWhile = stpm->rootTimes.ForwardDelay;
port->learn = True;
#ifdef STP_DBG
if (this->debug)
STP_port_trace_flags ("ROOT_LEARN", port);
#endif
break;
case ROOT_FORWARD:
port->fdWhile = 0;
port->forward = True;
#ifdef STP_DBG
if (this->debug)
STP_port_trace_flags ("ROOT_FORWARD", port);
#endif
break;
/* 17.23.3 */
case DESIGNATED_PROPOSE:
port->proposing = True; /* in DESIGNATED_PROPOSE */
port->newInfo = True;
#ifdef STP_DBG
if (this->debug)
STP_port_trace_flags ("DESIGNATED_PROPOSE", port);
#endif
break;
case DESIGNATED_SYNCED:
port->rrWhile = 0;
port->synced = True; /* DESIGNATED_SYNCED */
port->sync = False; /* DESIGNATED_SYNCED */
#ifdef STP_DBG
if (this->debug)
STP_port_trace_flags ("DESIGNATED_SYNCED", port);
#endif
break;
case DESIGNATED_RETIRED:
port->reRoot = False; /* DESIGNATED_RETIRED */
#ifdef STP_DBG
if (this->debug)
STP_port_trace_flags ("DESIGNATED_RETIRED", port);
#endif
break;
case DESIGNATED_PORT:
port->role = DesignatedPort;
#ifdef STP_DBG
if (this->debug)
STP_port_trace_flags ("DESIGNATED_PORT", port);
#endif
break;
case DESIGNATED_LISTEN:
port->learn = port->forward = False;
port->fdWhile = stpm->rootTimes.ForwardDelay;
#ifdef STP_DBG
if (this->debug)
STP_port_trace_flags ("DESIGNATED_LISTEN", port);
#endif
break;
case DESIGNATED_LEARN:
port->learn = True;
port->fdWhile = stpm->rootTimes.ForwardDelay;
#ifdef STP_DBG
if (this->debug)
STP_port_trace_flags ("DESIGNATED_LEARN", port);
#endif
break;
case DESIGNATED_FORWARD:
port->forward = True;
port->fdWhile = 0;
#ifdef STP_DBG
if (this->debug)
STP_port_trace_flags ("DESIGNATED_FORWARD", port);
#endif
break;
};
}
Bool
STP_roletrns_check_conditions (STATE_MACH_T* this)
{
register PORT_T *port = this->owner.port;
register STPM_T *stpm;
Bool allSynced;
Bool allReRooted;
stpm = port->owner;
if (BEGIN == this->State) {
return STP_hop_2_state (this, INIT_PORT);
}
if (port->role != port->selectedRole &&
port->selected &&
! port->updtInfo) {
switch (port->selectedRole) {
case DisabledPort:
case AlternatePort:
case BackupPort:
#if 0 /* def STP_DBG */
if (this->debug) {
stp_trace ("hop to BLOCK_PORT role=%d selectedRole=%d",
(int) port->role, (int) port->selectedRole);
}
#endif
return STP_hop_2_state (this, BLOCK_PORT);
case RootPort:
return STP_hop_2_state (this, ROOT_PORT);
case DesignatedPort:
return STP_hop_2_state (this, DESIGNATED_PORT);
default:
return False;
}
}
switch (this->State) {
/* 17.23.1 */
case INIT_PORT:
return STP_hop_2_state (this, BLOCK_PORT);
case BLOCK_PORT:
if (!port->selected || port->updtInfo) break;
if (!port->learning && !port->forwarding) {
return STP_hop_2_state (this, BLOCKED_PORT);
}
break;
case BLOCKED_PORT:
if (!port->selected || port->updtInfo) break;
if (port->fdWhile != stpm->rootTimes.ForwardDelay ||
port->sync ||
port->reRoot ||
!port->synced) {
return STP_hop_2_state (this, BLOCKED_PORT);
}
if (port->rbWhile != 2 * stpm->rootTimes.HelloTime &&
port->role == BackupPort) {
return STP_hop_2_state (this, BACKUP_PORT);
}
break;
case BACKUP_PORT:
return STP_hop_2_state (this, BLOCKED_PORT);
/* 17.23.2 */
case ROOT_PROPOSED:
return STP_hop_2_state (this, ROOT_PORT);
case ROOT_AGREED:
return STP_hop_2_state (this, ROOT_PORT);
case REROOT:
return STP_hop_2_state (this, ROOT_PORT);
case ROOT_PORT:
if (!port->selected || port->updtInfo) break;
if (!port->forward && !port->reRoot) {
return STP_hop_2_state (this, REROOT);
}
allSynced = compute_all_synced (port);
if ((port->proposed && allSynced) ||
(!port->synced && allSynced)) {
return STP_hop_2_state (this, ROOT_AGREED);
}
if (port->proposed && !port->synced) {
return STP_hop_2_state (this, ROOT_PROPOSED);
}
allReRooted = compute_re_rooted (port);
if ((!port->fdWhile ||
((allReRooted && !port->rbWhile) && stpm->ForceVersion >=2)) &&
port->learn && !port->forward) {
return STP_hop_2_state (this, ROOT_FORWARD);
}
if ((!port->fdWhile ||
((allReRooted && !port->rbWhile) && stpm->ForceVersion >=2)) &&
!port->learn) {
return STP_hop_2_state (this, ROOT_LEARN);
}
if (port->reRoot && port->forward) {
return STP_hop_2_state (this, REROOTED);
}
if (port->rrWhile != stpm->rootTimes.ForwardDelay) {
return STP_hop_2_state (this, ROOT_PORT);
}
break;
case REROOTED:
return STP_hop_2_state (this, ROOT_PORT);
case ROOT_LEARN:
return STP_hop_2_state (this, ROOT_PORT);
case ROOT_FORWARD:
return STP_hop_2_state (this, ROOT_PORT);
/* 17.23.3 */
case DESIGNATED_PROPOSE:
return STP_hop_2_state (this, DESIGNATED_PORT);
case DESIGNATED_SYNCED:
return STP_hop_2_state (this, DESIGNATED_PORT);
case DESIGNATED_RETIRED:
return STP_hop_2_state (this, DESIGNATED_PORT);
case DESIGNATED_PORT:
if (!port->selected || port->updtInfo) break;
if (!port->forward && !port->agreed && !port->proposing && !port->operEdge) {
return STP_hop_2_state (this, DESIGNATED_PROPOSE);
}
if (!port->rrWhile && port->reRoot) {
return STP_hop_2_state (this, DESIGNATED_RETIRED);
}
if (!port->learning && !port->forwarding && !port->synced) {
return STP_hop_2_state (this, DESIGNATED_SYNCED);
}
if (port->agreed && !port->synced) {
return STP_hop_2_state (this, DESIGNATED_SYNCED);
}
if (port->operEdge && !port->synced) {
return STP_hop_2_state (this, DESIGNATED_SYNCED);
}
if (port->sync && port->synced) {
return STP_hop_2_state (this, DESIGNATED_SYNCED);
}
if ((!port->fdWhile || port->agreed || port->operEdge) &&
(!port->rrWhile || !port->reRoot) &&
!port->sync &&
(port->learn && !port->forward)) {
return STP_hop_2_state (this, DESIGNATED_FORWARD);
}
if ((!port->fdWhile || port->agreed || port->operEdge) &&
(!port->rrWhile || !port->reRoot) &&
!port->sync && !port->learn) {
return STP_hop_2_state (this, DESIGNATED_LEARN);
}
if (((port->sync && !port->synced) ||
(port->reRoot && port->rrWhile)) &&
!port->operEdge && (port->learn || port->forward)) {
return STP_hop_2_state (this, DESIGNATED_LISTEN);
}
break;
case DESIGNATED_LISTEN:
return STP_hop_2_state (this, DESIGNATED_PORT);
case DESIGNATED_LEARN:
return STP_hop_2_state (this, DESIGNATED_PORT);
case DESIGNATED_FORWARD:
return STP_hop_2_state (this, DESIGNATED_PORT);
};
return False;
}
|