/
tcmfsm.go
693 lines (603 loc) · 23.7 KB
/
tcmfsm.go
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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
//
//Copyright [2016] [SnapRoute Inc]
//
//Licensed under the Apache License, Version 2.0 (the "License");
//you may not use this file except in compliance with the License.
//You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// _______ __ __________ ___ _______.____ __ ____ __ .___________. ______ __ __
// | ____|| | | ____\ \ / / / |\ \ / \ / / | | | | / || | | |
// | |__ | | | |__ \ V / | (----` \ \/ \/ / | | `---| |----`| ,----'| |__| |
// | __| | | | __| > < \ \ \ / | | | | | | | __ |
// | | | `----.| |____ / . \ .----) | \ /\ / | | | | | `----.| | | |
// |__| |_______||_______/__/ \__\ |_______/ \__/ \__/ |__| |__| \______||__| |__|
//
// 802.1D-2004 17.30 Topology Change State Machine
//The Topology Change state machine shall implement the function specified by the state diagram in Figure
//17-25, the definitions in 17.13, 17.16, 17.20, and 17.21, and variable declarations in 17.17, 17.18, and 17.19
//This state machine is responsible for topology change detection, notification, and propagation, and for
//instructing the Filtering Database to remove Dynamic Filtering Entries for certain ports (17.11).
package stp
import (
"fmt"
//"time"
"utils/fsm"
)
const TcMachineModuleStr = "TCM"
const (
TcStateNone = iota + 1
TcStateInactive
TcStateLearning
TcStateDetected
TcStateActive
TcStateNotifiedTcn
TcStateNotifiedTc
TcStatePropagating
TcStateAcknowledged
)
var TcStateStrMap map[fsm.State]string
func TcMachineStrStateMapInit() {
TcStateStrMap = make(map[fsm.State]string)
TcStateStrMap[PrsStateNone] = "None"
TcStateStrMap[TcStateInactive] = "Inactive"
TcStateStrMap[TcStateLearning] = "Learning"
TcStateStrMap[TcStateDetected] = "Detected"
TcStateStrMap[TcStateActive] = "Active"
TcStateStrMap[TcStateNotifiedTcn] = "NotifiedTcn"
TcStateStrMap[TcStateNotifiedTc] = "NotifiedTc"
TcStateStrMap[TcStatePropagating] = "Propagating"
TcStateStrMap[TcStateAcknowledged] = "Acknowledged"
}
const (
TcEventBegin = iota + 1
TcEventUnconditionalFallThrough
TcEventRoleNotEqualRootPortAndRoleNotEqualDesignatedPortAndNotLearnAndNotLearningAndNotRcvdTcAndNotRcvdTcnAndNotRcvdTcAckAndNotTcProp
TcEventLearnAndNotFdbFlush
TcEventRcvdTc
TcEventRcvdTcn
TcEventRcvdTcAck
TcEventTcProp
TcEventRoleEqualRootPortAndForwardAndNotOperEdge
TcEventRoleEqualDesignatedPortAndForwardAndNotOperEdge
TcEventTcPropAndNotOperEdge
TcEventRoleNotEqualRootPortAndRoleNotEqualDesignatedPort
TcEventOperEdge
)
// TcMachine holds FSM and current State
// and event channels for State transitions
type TcMachine struct {
Machine *fsm.Machine
// State transition log
log chan string
// Reference to StpPort
p *StpPort
// machine specific events
TcEvents chan MachineEvent
// enable logging
TcLogEnableEvent chan bool
}
func (m *TcMachine) GetCurrStateStr() string {
return TcStateStrMap[m.Machine.Curr.CurrentState()]
}
func (m *TcMachine) GetPrevStateStr() string {
return TcStateStrMap[m.Machine.Curr.PreviousState()]
}
// NewStpTcMachine will create a new instance of the LacpRxMachine
func NewStpTcMachine(p *StpPort) *TcMachine {
tcm := &TcMachine{
p: p,
TcEvents: make(chan MachineEvent, 10),
TcLogEnableEvent: make(chan bool)}
p.TcMachineFsm = tcm
return tcm
}
func (tcm *TcMachine) TcmLogger(s string) {
StpMachineLogger("DEBUG", PtxmMachineModuleStr, tcm.p.IfIndex, tcm.p.BrgIfIndex, s)
}
// A helpful function that lets us apply arbitrary rulesets to this
// instances State machine without reallocating the machine.
func (tcm *TcMachine) Apply(r *fsm.Ruleset) *fsm.Machine {
if tcm.Machine == nil {
tcm.Machine = &fsm.Machine{}
}
// Assign the ruleset to be used for this machine
tcm.Machine.Rules = r
tcm.Machine.Curr = &StpStateEvent{
strStateMap: TcStateStrMap,
logEna: true,
logger: tcm.TcmLogger,
owner: TcMachineModuleStr,
ps: TcStateNone,
s: TcStateNone,
}
return tcm.Machine
}
// Stop should clean up all resources
func (tcm *TcMachine) Stop() {
close(tcm.TcEvents)
close(tcm.TcLogEnableEvent)
}
// TcmMachineInactive
func (tcm *TcMachine) TcMachineInactive(m fsm.Machine, data interface{}) fsm.State {
p := tcm.p
defer tcm.NotifyFdbFlush()
p.TcWhileTimer.count = 0
p.TcAck = false
return TcStateInactive
}
// TcMachineLearning
func (tcm *TcMachine) TcMachineLearning(m fsm.Machine, data interface{}) fsm.State {
p := tcm.p
p.RcvdTc = false
p.RcvdTcn = false
p.RcvdTcAck = false
p.TcProp = false
return TcStateLearning
}
// TcMachineDetected
func (tcm *TcMachine) TcMachineDetected(m fsm.Machine, data interface{}) fsm.State {
p := tcm.p
newinfonotificationsent := tcm.newTcWhile()
tcm.setTcPropTree()
if !newinfonotificationsent {
defer tcm.NotifyNewInfoChanged(p.NewInfo, true)
}
p.NewInfo = true
return TcStateDetected
}
// TcMachineActive
func (tcm *TcMachine) TcMachineActive(m fsm.Machine, data interface{}) fsm.State {
return TcStateActive
}
// TcMachineNotifyTcn
func (tcm *TcMachine) TcMachineNotifiedTcn(m fsm.Machine, data interface{}) fsm.State {
tcm.newTcWhile()
return TcStateNotifiedTcn
}
// TcMachineNotifyTc
func (tcm *TcMachine) TcMachineNotifiedTc(m fsm.Machine, data interface{}) fsm.State {
p := tcm.p
p.RcvdTcn = false
p.RcvdTc = false
if p.Role == PortRoleDesignatedPort {
defer tcm.NotifyTcAckChanged(true)
p.TcAck = true
}
// Figure 17-25 says setTcPropBridge, but this is the only mention of this in
// the standard, assuming it should be Tree
tcm.setTcPropTree()
return TcStateNotifiedTc
}
// TcMachinePropagating
func (tcm *TcMachine) TcMachinePropagating(m fsm.Machine, data interface{}) fsm.State {
p := tcm.p
tcm.newTcWhile()
defer tcm.NotifyFdbFlush()
p.TcProp = false
return TcStatePropagating
}
// TcMachineAcknowledged
func (tcm *TcMachine) TcMachineAcknowledged(m fsm.Machine, data interface{}) fsm.State {
p := tcm.p
defer tcm.NotifyTcWhileChanged(0)
p.RcvdTcAck = false
return TcStateAcknowledged
}
func TcMachineFSMBuild(p *StpPort) *TcMachine {
rules := fsm.Ruleset{}
// Instantiate a new PrxmMachine
// Initial State will be a psuedo State known as "begin" so that
// we can transition to the DISCARD State
tcm := NewStpTcMachine(p)
/*
TcStateInactive
TcStateLearning
TcStateDetected
TcStateActive
TcStateNotifiedTcn
TcStateNotifiedTc
TcStatePropagating
TcStateAcknowledged
*/
// BEGIN -> INACTIVE
rules.AddRule(TcStateNone, TcEventBegin, tcm.TcMachineInactive)
rules.AddRule(TcStateInactive, TcEventBegin, tcm.TcMachineInactive)
rules.AddRule(TcStateLearning, TcEventBegin, tcm.TcMachineInactive)
rules.AddRule(TcStateDetected, TcEventBegin, tcm.TcMachineInactive)
rules.AddRule(TcStateActive, TcEventBegin, tcm.TcMachineInactive)
rules.AddRule(TcStateNotifiedTcn, TcEventBegin, tcm.TcMachineInactive)
rules.AddRule(TcStateNotifiedTc, TcEventBegin, tcm.TcMachineInactive)
rules.AddRule(TcStatePropagating, TcEventBegin, tcm.TcMachineInactive)
rules.AddRule(TcStateAcknowledged, TcEventBegin, tcm.TcMachineInactive)
// ROLE != ROOTPORT && ROLE != DESIGNATEDPORT and !(LEARN || LEARNING) and !(RCVDTC || RCVDTCN || RCVDTCACK || TCPROP) -> INACTIVE
rules.AddRule(TcStateLearning, TcEventRoleNotEqualRootPortAndRoleNotEqualDesignatedPortAndNotLearnAndNotLearningAndNotRcvdTcAndNotRcvdTcnAndNotRcvdTcAckAndNotTcProp, tcm.TcMachineInactive)
// LEARN and NOTFLUSH -> LEARNING
rules.AddRule(TcStateInactive, TcEventLearnAndNotFdbFlush, tcm.TcMachineLearning)
// RCVDTC -> LEARNING or NOTIFIED_TC
rules.AddRule(TcStateLearning, TcEventRcvdTc, tcm.TcMachineLearning)
rules.AddRule(TcStateActive, TcEventRcvdTc, tcm.TcMachineNotifiedTc)
// RCVDTCN -> LEARNING or NOTIFIED_TCN
rules.AddRule(TcStateLearning, TcEventRcvdTcn, tcm.TcMachineLearning)
rules.AddRule(TcStateActive, TcEventRcvdTcn, tcm.TcMachineNotifiedTcn)
// RCVDTCACK -> LEARNING or ACKNOWLEDGED
rules.AddRule(TcStateLearning, TcEventRcvdTcAck, tcm.TcMachineLearning)
rules.AddRule(TcStateActive, TcEventRcvdTcAck, tcm.TcMachineAcknowledged)
// TCPROP -> LEARNING
rules.AddRule(TcStateLearning, TcEventTcProp, tcm.TcMachineLearning)
// (ROLE == ROOTPORT or ROLE == DESIGNATEDPORT) and FORWARD and NOT OPEREDGE -> DETECTED
rules.AddRule(TcStateLearning, TcEventRoleEqualRootPortAndForwardAndNotOperEdge, tcm.TcMachineDetected)
rules.AddRule(TcStateLearning, TcEventRoleEqualDesignatedPortAndForwardAndNotOperEdge, tcm.TcMachineDetected)
// UNCONDITIONAL FALL THROUGH -> ACTIVE or NOTIFIED TC
rules.AddRule(TcStateDetected, TcEventUnconditionalFallThrough, tcm.TcMachineActive)
rules.AddRule(TcStateNotifiedTcn, TcEventUnconditionalFallThrough, tcm.TcMachineNotifiedTc)
rules.AddRule(TcStateNotifiedTc, TcEventUnconditionalFallThrough, tcm.TcMachineActive)
rules.AddRule(TcStatePropagating, TcEventUnconditionalFallThrough, tcm.TcMachineActive)
rules.AddRule(TcStateAcknowledged, TcEventUnconditionalFallThrough, tcm.TcMachineActive)
// ROLE != ROOT PORT and ROLE != DESIGNATEDPORT -> LEARNING
rules.AddRule(TcStateActive, TcEventRoleNotEqualRootPortAndRoleNotEqualDesignatedPort, tcm.TcMachineLearning)
// OPEREDGE -> LEARNING
rules.AddRule(TcStateActive, TcEventOperEdge, tcm.TcMachineLearning)
// TCPROP and NOT OPEREDGE -> PROPAGATING
rules.AddRule(TcStateActive, TcEventTcPropAndNotOperEdge, tcm.TcMachinePropagating)
// Create a new FSM and apply the rules
tcm.Apply(&rules)
return tcm
}
// PimMachineMain:
func (p *StpPort) TcMachineMain() {
// Build the State machine for STP Bridge Detection State Machine according to
// 802.1d Section 17.25
tcm := TcMachineFSMBuild(p)
p.wg.Add(1)
// set the inital State
tcm.Machine.Start(tcm.Machine.Curr.PreviousState())
// lets create a go routing which will wait for the specific events
// that the Port Timer State Machine should handle
go func(m *TcMachine) {
StpMachineLogger("DEBUG", PtxmMachineModuleStr, p.IfIndex, p.BrgIfIndex, "Machine Start")
defer m.p.wg.Done()
for {
select {
case event, ok := <-m.TcEvents:
if ok {
if m.Machine.Curr.CurrentState() == TcStateNone && event.e != TcEventBegin {
m.TcEvents <- event
break
}
//fmt.Println("Event Rx", event.src, event.e)
rv := m.Machine.ProcessEvent(event.src, event.e, nil)
if rv != nil {
StpMachineLogger("ERROR", PtxmMachineModuleStr, p.IfIndex, p.BrgIfIndex, fmt.Sprintf("%s event[%d] currState[%s]\n", rv, event.e, TcStateStrMap[m.Machine.Curr.CurrentState()]))
} else {
// for faster transitions lets check all state events
m.ProcessPostStateProcessing()
}
if event.responseChan != nil {
SendResponse(TcMachineModuleStr, event.responseChan)
}
} else {
StpMachineLogger("DEBUG", PtxmMachineModuleStr, p.IfIndex, p.BrgIfIndex, "Machine End")
return
}
case ena := <-m.TcLogEnableEvent:
m.Machine.Curr.EnableLogging(ena)
}
}
}(tcm)
}
func (tcm *TcMachine) ProcessPostStateInactive() {
p := tcm.p
if tcm.Machine.Curr.CurrentState() == TcStateInactive &&
p.Learn &&
!p.FdbFlush {
rv := tcm.Machine.ProcessEvent(TcMachineModuleStr, TcEventLearnAndNotFdbFlush, nil)
if rv != nil {
StpMachineLogger("ERROR", PtxmMachineModuleStr, p.IfIndex, p.BrgIfIndex, fmt.Sprintf("%s event[%d] currState[%s]\n", rv, TcEventLearnAndNotFdbFlush, TcStateStrMap[tcm.Machine.Curr.CurrentState()]))
} else {
tcm.ProcessPostStateProcessing()
}
}
}
func (tcm *TcMachine) ProcessPostStateLearning() {
p := tcm.p
if tcm.Machine.Curr.CurrentState() == TcStateLearning {
if (p.Role == PortRoleRootPort || p.Role == PortRoleDesignatedPort) &&
p.Forward &&
!p.OperEdge {
if p.Role == PortRoleRootPort {
rv := tcm.Machine.ProcessEvent(TcMachineModuleStr, TcEventRoleEqualRootPortAndForwardAndNotOperEdge, nil)
if rv != nil {
StpMachineLogger("ERROR", PtxmMachineModuleStr, p.IfIndex, p.BrgIfIndex, fmt.Sprintf("%s event[%d] currState[%s]\n", rv, TcEventRoleEqualRootPortAndForwardAndNotOperEdge, TcStateStrMap[tcm.Machine.Curr.CurrentState()]))
} else {
tcm.ProcessPostStateProcessing()
}
} else {
rv := tcm.Machine.ProcessEvent(TcMachineModuleStr, TcEventRoleEqualDesignatedPortAndForwardAndNotOperEdge, nil)
if rv != nil {
StpMachineLogger("ERROR", PtxmMachineModuleStr, p.IfIndex, p.BrgIfIndex, fmt.Sprintf("%s event[%d] currState[%s]\n", rv, TcEventRoleEqualDesignatedPortAndForwardAndNotOperEdge, TcStateStrMap[tcm.Machine.Curr.CurrentState()]))
} else {
tcm.ProcessPostStateProcessing()
}
}
} else if p.RcvdTc {
rv := tcm.Machine.ProcessEvent(TcMachineModuleStr, TcEventRcvdTc, nil)
if rv != nil {
StpMachineLogger("ERROR", PtxmMachineModuleStr, p.IfIndex, p.BrgIfIndex, fmt.Sprintf("%s event[%d] currState[%s]\n", rv, TcEventRoleEqualDesignatedPortAndForwardAndNotOperEdge, TcStateStrMap[tcm.Machine.Curr.CurrentState()]))
} else {
tcm.ProcessPostStateProcessing()
}
} else if p.RcvdTcn {
rv := tcm.Machine.ProcessEvent(TcMachineModuleStr, TcEventRcvdTcn, nil)
if rv != nil {
StpMachineLogger("ERROR", PtxmMachineModuleStr, p.IfIndex, p.BrgIfIndex, fmt.Sprintf("%s event[%d] currState[%s]\n", rv, TcEventRoleEqualDesignatedPortAndForwardAndNotOperEdge, TcStateStrMap[tcm.Machine.Curr.CurrentState()]))
} else {
tcm.ProcessPostStateProcessing()
}
} else if p.RcvdTcAck {
rv := tcm.Machine.ProcessEvent(TcMachineModuleStr, TcEventRcvdTcAck, nil)
if rv != nil {
StpMachineLogger("ERROR", PtxmMachineModuleStr, p.IfIndex, p.BrgIfIndex, fmt.Sprintf("%s event[%d] currState[%s]\n", rv, TcEventRoleEqualDesignatedPortAndForwardAndNotOperEdge, TcStateStrMap[tcm.Machine.Curr.CurrentState()]))
} else {
tcm.ProcessPostStateProcessing()
}
} else if p.TcProp {
rv := tcm.Machine.ProcessEvent(TcMachineModuleStr, TcEventTcProp, nil)
if rv != nil {
StpMachineLogger("ERROR", PtxmMachineModuleStr, p.IfIndex, p.BrgIfIndex, fmt.Sprintf("%s event[%d] currState[%s]\n", rv, TcEventRoleEqualDesignatedPortAndForwardAndNotOperEdge, TcStateStrMap[tcm.Machine.Curr.CurrentState()]))
} else {
tcm.ProcessPostStateProcessing()
}
}
}
}
func (tcm *TcMachine) ProcessPostStateActive() {
p := tcm.p
if tcm.Machine.Curr.CurrentState() == TcStateActive {
if p.Role != PortRoleRootPort &&
p.Role != PortRoleDesignatedPort {
rv := tcm.Machine.ProcessEvent(TcMachineModuleStr, TcEventRoleNotEqualRootPortAndRoleNotEqualDesignatedPort, nil)
if rv != nil {
StpMachineLogger("ERROR", PtxmMachineModuleStr, p.IfIndex, p.BrgIfIndex, fmt.Sprintf("%s event[%d] currState[%s]\n", rv, TcEventRoleNotEqualRootPortAndRoleNotEqualDesignatedPort, TcStateStrMap[tcm.Machine.Curr.CurrentState()]))
} else {
tcm.ProcessPostStateProcessing()
}
} else if p.OperEdge {
rv := tcm.Machine.ProcessEvent(TcMachineModuleStr, TcEventOperEdge, nil)
if rv != nil {
StpMachineLogger("ERROR", PtxmMachineModuleStr, p.IfIndex, p.BrgIfIndex, fmt.Sprintf("%s event[%d] currState[%s]\n", rv, TcEventOperEdge, TcStateStrMap[tcm.Machine.Curr.CurrentState()]))
} else {
tcm.ProcessPostStateProcessing()
}
} else if p.RcvdTcn {
rv := tcm.Machine.ProcessEvent(TcMachineModuleStr, TcEventRcvdTcn, nil)
if rv != nil {
StpMachineLogger("ERROR", PtxmMachineModuleStr, p.IfIndex, p.BrgIfIndex, fmt.Sprintf("%s event[%d] currState[%s]\n", rv, TcEventRcvdTcn, TcStateStrMap[tcm.Machine.Curr.CurrentState()]))
} else {
tcm.ProcessPostStateProcessing()
}
} else if p.RcvdTc {
rv := tcm.Machine.ProcessEvent(TcMachineModuleStr, TcEventRcvdTc, nil)
if rv != nil {
StpMachineLogger("ERROR", PtxmMachineModuleStr, p.IfIndex, p.BrgIfIndex, fmt.Sprintf("%s event[%d] currState[%s]\n", rv, TcEventRcvdTc, TcStateStrMap[tcm.Machine.Curr.CurrentState()]))
} else {
tcm.ProcessPostStateProcessing()
}
} else if p.TcProp && !p.OperEdge {
rv := tcm.Machine.ProcessEvent(TcMachineModuleStr, TcEventTcPropAndNotOperEdge, nil)
if rv != nil {
StpMachineLogger("ERROR", PtxmMachineModuleStr, p.IfIndex, p.BrgIfIndex, fmt.Sprintf("%s event[%d] currState[%s]\n", rv, TcEventTcPropAndNotOperEdge, TcStateStrMap[tcm.Machine.Curr.CurrentState()]))
} else {
tcm.ProcessPostStateProcessing()
}
} else if p.RcvdTcAck {
rv := tcm.Machine.ProcessEvent(TcMachineModuleStr, TcEventRcvdTcAck, nil)
if rv != nil {
StpMachineLogger("ERROR", PtxmMachineModuleStr, p.IfIndex, p.BrgIfIndex, fmt.Sprintf("%s event[%d] currState[%s]\n", rv, TcEventRcvdTcAck, TcStateStrMap[tcm.Machine.Curr.CurrentState()]))
} else {
tcm.ProcessPostStateProcessing()
}
}
}
}
func (tcm *TcMachine) ProcessPostStateDetected() {
p := tcm.p
if tcm.Machine.Curr.CurrentState() == TcStateDetected {
rv := tcm.Machine.ProcessEvent(TcMachineModuleStr, TcEventUnconditionalFallThrough, nil)
if rv != nil {
StpMachineLogger("ERROR", PtxmMachineModuleStr, p.IfIndex, p.BrgIfIndex, fmt.Sprintf("%s event[%d] currState[%s]\n", rv, TcEventUnconditionalFallThrough, TcStateStrMap[tcm.Machine.Curr.CurrentState()]))
} else {
tcm.ProcessPostStateProcessing()
}
}
}
func (tcm *TcMachine) ProcessPostStateNotifiedTcn() {
p := tcm.p
if tcm.Machine.Curr.CurrentState() == TcStateNotifiedTcn {
rv := tcm.Machine.ProcessEvent(TcMachineModuleStr, TcEventUnconditionalFallThrough, nil)
if rv != nil {
StpMachineLogger("ERROR", PtxmMachineModuleStr, p.IfIndex, p.BrgIfIndex, fmt.Sprintf("%s event[%d] currState[%s]\n", rv, TcEventUnconditionalFallThrough, TcStateStrMap[tcm.Machine.Curr.CurrentState()]))
} else {
tcm.ProcessPostStateProcessing()
}
}
}
func (tcm *TcMachine) ProcessPostStateNotifiedTc() {
p := tcm.p
if tcm.Machine.Curr.CurrentState() == TcStateNotifiedTc {
rv := tcm.Machine.ProcessEvent(TcMachineModuleStr, TcEventUnconditionalFallThrough, nil)
if rv != nil {
StpMachineLogger("ERROR", PtxmMachineModuleStr, p.IfIndex, p.BrgIfIndex, fmt.Sprintf("%s event[%d] currState[%s]\n", rv, TcEventUnconditionalFallThrough, TcStateStrMap[tcm.Machine.Curr.CurrentState()]))
} else {
tcm.ProcessPostStateProcessing()
}
}
}
func (tcm *TcMachine) ProcessPostStatePropagating() {
p := tcm.p
if tcm.Machine.Curr.CurrentState() == TcStatePropagating {
rv := tcm.Machine.ProcessEvent(TcMachineModuleStr, TcEventUnconditionalFallThrough, nil)
if rv != nil {
StpMachineLogger("ERROR", PtxmMachineModuleStr, p.IfIndex, p.BrgIfIndex, fmt.Sprintf("%s event[%d] currState[%s]\n", rv, TcEventUnconditionalFallThrough, TcStateStrMap[tcm.Machine.Curr.CurrentState()]))
} else {
tcm.ProcessPostStateProcessing()
}
}
}
func (tcm *TcMachine) ProcessPostStateAcknowledged() {
p := tcm.p
if tcm.Machine.Curr.CurrentState() == TcStateAcknowledged {
rv := tcm.Machine.ProcessEvent(TcMachineModuleStr, TcEventUnconditionalFallThrough, nil)
if rv != nil {
StpMachineLogger("ERROR", TcMachineModuleStr, p.IfIndex, p.BrgIfIndex, fmt.Sprintf("%s event[%d] currState[%s]\n", rv, TcEventUnconditionalFallThrough, TcStateStrMap[tcm.Machine.Curr.CurrentState()]))
} else {
tcm.ProcessPostStateProcessing()
}
}
}
// ProcessPostStateProcessing:
// advance states after a given event for faster state transitions
func (tcm *TcMachine) ProcessPostStateProcessing() {
tcm.ProcessPostStateInactive()
tcm.ProcessPostStateLearning()
tcm.ProcessPostStateActive()
tcm.ProcessPostStateDetected()
tcm.ProcessPostStateNotifiedTcn()
tcm.ProcessPostStateNotifiedTc()
tcm.ProcessPostStatePropagating()
tcm.ProcessPostStateAcknowledged()
}
// 17.19.7
//A boolean. Set by the topology change state machine to instruct the filtering database to remove all entries
//for this Port, immediately if rstpVersion (17.20.11) is TRUE, or by rapid ageing (17.19.1) if stpVersion
//(17.20.12) is TRUE. Reset by the filtering database once the entries are removed if rstpVersion is TRUE, and
//immediately if stpVersion is TRUE.
func (tcm *TcMachine) FlushFdb() {
p := tcm.p
// standard allows for imidiate flush
// or adjust timer to flush once flushing
// is complete lets clear FdbFlush and
// send event to TCM
for _, client := range GetAsicDPluginList() {
client.FlushStgFdb(p.b.StgId, p.IfIndex)
}
StpMachineLogger("DEBUG", TcMachineModuleStr, p.IfIndex, p.BrgIfIndex, "FDB Flush")
p.FdbFlush = false
if p.Learn &&
p.TcMachineFsm != nil &&
p.TcMachineFsm.Machine.Curr.CurrentState() == TcStateInactive {
p.TcMachineFsm.TcEvents <- MachineEvent{
e: TcEventLearnAndNotFdbFlush,
src: "ASICD",
}
}
}
func (tcm *TcMachine) NotifyFdbFlush() {
p := tcm.p
p.FdbFlush = true
// spawn go routine to flush and wait for flush completion
// but allow processing to continue
go tcm.FlushFdb()
}
func (tcm *TcMachine) NotifyTcAckChanged(val bool) {
// lets force transmit, this should have come via state path
// Detected -> Active -> Notified Tc
// New info is set in Detected
tcm.NotifyNewInfoChanged(false, true)
}
func (tcm *TcMachine) NotifyTcWhileChanged(val int32) {
p := tcm.p
p.TcWhileTimer.count = val
if val == 0 {
// this should stop transmit of tcn messages
}
}
func (tcm *TcMachine) NotifyTcPropChanged(oldtcprop bool, newtcprop bool) {
p := tcm.p
if oldtcprop != newtcprop {
if tcm.Machine.Curr.CurrentState() == TcStateActive {
if newtcprop &&
!p.OperEdge {
tcm.TcEvents <- MachineEvent{
e: TcEventTcPropAndNotOperEdge,
src: TcMachineModuleStr,
}
}
} else if tcm.Machine.Curr.CurrentState() == TcStateLearning {
if newtcprop {
tcm.TcEvents <- MachineEvent{
e: TcEventTcProp,
src: TcMachineModuleStr,
}
}
}
}
}
func (tcm *TcMachine) NotifyNewInfoChanged(oldnewinfo bool, newnewinfo bool) {
p := tcm.p
if oldnewinfo != newnewinfo {
if p.PtxmMachineFsm.Machine.Curr.CurrentState() == PtxmStateIdle {
if p.SendRSTP &&
p.NewInfo &&
p.TxCount < p.b.TxHoldCount &&
p.HelloWhenTimer.count != 0 {
p.PtxmMachineFsm.PtxmEvents <- MachineEvent{
e: PtxmEventSendRSTPAndNewInfoAndTxCountLessThanTxHoldCoundAndHelloWhenNotEqualZeroAndSelectedAndNotUpdtInfo,
src: TcMachineModuleStr,
}
} else if !p.SendRSTP &&
p.NewInfo && p.Role == PortRoleRootPort &&
p.TxCount < p.b.TxHoldCount &&
p.HelloWhenTimer.count != 0 {
p.PtxmMachineFsm.PtxmEvents <- MachineEvent{
e: PtxmEventNotSendRSTPAndNewInfoAndRootPortAndTxCountLessThanTxHoldCountAndHellWhenNotEqualZeroAndSelectedAndNotUpdtInfo,
src: TcMachineModuleStr,
}
} else if !p.SendRSTP &&
p.NewInfo && p.Role == PortRoleDesignatedPort &&
p.TxCount < p.b.TxHoldCount &&
p.HelloWhenTimer.count != 0 {
p.PtxmMachineFsm.PtxmEvents <- MachineEvent{
e: PtxmEventNotSendRSTPAndNewInfoAndDesignatedPortAndTxCountLessThanTxHoldCountAndHellWhenNotEqualZeroAndSelectedAndNotUpdtInfo,
src: TcMachineModuleStr,
}
}
}
}
}
// newTcWhile: 17.21.7
func (tcm *TcMachine) newTcWhile() (newinfonotificationsent bool) {
p := tcm.p
newinfonotificationsent = false
if p.TcWhileTimer.count == 0 {
if p.SendRSTP {
p.TcWhileTimer.count = BridgeHelloTimeDefault + 1
defer tcm.NotifyNewInfoChanged(p.NewInfo, true)
newinfonotificationsent = true
p.NewInfo = true
} else {
p.TcWhileTimer.count = int32(p.PortTimes.MaxAge + p.PortTimes.ForwardingDelay)
}
}
return newinfonotificationsent
}
// setTcPropTree: 17.21.18
func (tcm *TcMachine) setTcPropTree() {
p := tcm.p
b := p.b
var port *StpPort
for _, pId := range b.StpPorts {
if pId != p.IfIndex &&
StpFindPortByIfIndex(pId, b.BrgIfIndex, &port) {
port.TcMachineFsm.NotifyTcPropChanged(port.TcProp, true)
port.TcProp = false
}
}
}