-
Notifications
You must be signed in to change notification settings - Fork 8
/
periodictxmachine.go
315 lines (257 loc) · 10.7 KB
/
periodictxmachine.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
//
//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.1ax-2014 Section 9.4.15 DRCPDU Periodic Transmission machine
// rxmachine.go
package drcp
import (
"l2/lacp/protocol/utils"
"strconv"
"strings"
"time"
"utils/fsm"
"github.com/google/gopacket/layers"
)
const PtxMachineModuleStr = "DRCP PTX Machine"
// drxm States
const (
PtxmStateNone = iota + 1
PtxmStateNoPeriodic
PtxmStateFastPeriodic
PtxmStateSlowPeriodic
PtxmStatePeriodicTx
)
var PtxmStateStrMap map[fsm.State]string
func PtxMachineStrStateMapCreate() {
PtxmStateStrMap = make(map[fsm.State]string)
PtxmStateStrMap[PtxmStateNone] = "None"
PtxmStateStrMap[PtxmStateNoPeriodic] = "No Periodic"
PtxmStateStrMap[PtxmStateFastPeriodic] = "Fast Periodic"
PtxmStateStrMap[PtxmStateSlowPeriodic] = "Slow Periodic"
PtxmStateStrMap[PtxmStatePeriodicTx] = "Periodic Tx"
}
// rxm events
const (
PtxmEventBegin = iota + 1
PtxmEventNotIPPPortEnabled
PtxmEventUnconditionalFallThrough
PtxmEventDRFNeighborOPerDRCPStateTimeoutEqualLongTimeout
PtxmEventDRFNeighborOPerDRCPStateTimeoutEqualShortTimeout
PtxmEventDRCPPeriodicTimerExpired
)
// PtxMachine holds FSM and current State
// and event channels for State transitions
type PtxMachine struct {
// for debugging
PreviousState fsm.State
Machine *fsm.Machine
p *DRCPIpp
// timer interval
periodicTimerInterval time.Duration
// timers
periodicTimer *time.Timer
// machine specific events
PtxmEvents chan utils.MachineEvent
}
func (ptxm *PtxMachine) PrevState() fsm.State { return ptxm.PreviousState }
// PrevStateSet will set the previous State
func (ptxm *PtxMachine) PrevStateSet(s fsm.State) { ptxm.PreviousState = s }
// Stop should clean up all resources
func (ptxm *PtxMachine) Stop() {
ptxm.PeriodicTimerStop()
close(ptxm.PtxmEvents)
}
// NewDrcpPTxMachine will create a new instance of the PtxMachine
func NewDrcpPTxMachine(port *DRCPIpp) *PtxMachine {
ptxm := &PtxMachine{
p: port,
PreviousState: PtxmStateNone,
PtxmEvents: make(chan utils.MachineEvent, 10),
}
port.PtxMachineFsm = ptxm
// create then stop
ptxm.PeriodicTimerStart()
ptxm.PeriodicTimerStop()
return ptxm
}
// A helpful function that lets us apply arbitrary rulesets to this
// instances State machine without reallocating the machine.
func (ptxm *PtxMachine) Apply(r *fsm.Ruleset) *fsm.Machine {
if ptxm.Machine == nil {
ptxm.Machine = &fsm.Machine{}
}
// Assign the ruleset to be used for this machine
ptxm.Machine.Rules = r
ptxm.Machine.Curr = &utils.StateEvent{
StrStateMap: PtxmStateStrMap,
LogEna: false,
Logger: ptxm.DrcpPtxmLog,
Owner: PtxMachineModuleStr,
}
return ptxm.Machine
}
// DrcpPtxMachineNoPeriodic function to be called after
// State transition to NO_PERIODIC
func (ptxm *PtxMachine) DrcpPtxMachineNoPeriodic(m fsm.Machine, data interface{}) fsm.State {
p := ptxm.p
dr := p.dr
ptxm.PeriodicTimerStop()
// should be no activity since we are not sending on the IPP
dr.DRFHomeOperDRCPState.ClearState(layers.DRCPStateIPPActivity)
// next State
return PtxmStateNoPeriodic
}
// DrcpPtxMachineFastPeriodic function to be called after
// State transition to FAST_PERIODIC
func (ptxm *PtxMachine) DrcpPtxMachineFastPeriodic(m fsm.Machine, data interface{}) fsm.State {
ptxm.PeriodicTimerIntervalSet(DrniFastPeriodicTime)
ptxm.PeriodicTimerStart()
// next State
return PtxmStateFastPeriodic
}
// DrcpPtxMachineSlowPeriodic function to be called after
// State transition to SLOW_PERIODIC
func (ptxm *PtxMachine) DrcpPtxMachineSlowPeriodic(m fsm.Machine, data interface{}) fsm.State {
ptxm.PeriodicTimerIntervalSet(DrniSlowPeriodictime)
ptxm.PeriodicTimerStart()
// next State
return PtxmStateSlowPeriodic
}
// DrcpPtxMachinePeriodicTx function to be called after
// State transition to PERIODIC_TX
func (ptxm *PtxMachine) DrcpPtxMachinePeriodicTx(m fsm.Machine, data interface{}) fsm.State {
p := ptxm.p
dr := p.dr
// should be no activity since we are not sending on the IPP
dr.DRFHomeOperDRCPState.SetState(layers.DRCPStateIPPActivity)
defer p.NotifyNTTDRCPUDChange(PtxMachineModuleStr, p.NTTDRCPDU, true)
p.NTTDRCPDU = true
// next State
return PtxmStatePeriodicTx
}
func DrcpPtxMachineFSMBuild(p *DRCPIpp) *PtxMachine {
PtxMachineStrStateMapCreate()
rules := fsm.Ruleset{}
// Instantiate a new PtxMachine
// Initial State will be a psuedo State known as "begin" so that
// we can transition to the initalize State
ptxm := NewDrcpPTxMachine(p)
//BEGIN -> NO PERIODIC
rules.AddRule(PtxmStateNone, PtxmEventBegin, ptxm.DrcpPtxMachineNoPeriodic)
rules.AddRule(PtxmStateNoPeriodic, PtxmEventBegin, ptxm.DrcpPtxMachineNoPeriodic)
rules.AddRule(PtxmStateFastPeriodic, PtxmEventBegin, ptxm.DrcpPtxMachineNoPeriodic)
rules.AddRule(PtxmStateSlowPeriodic, PtxmEventBegin, ptxm.DrcpPtxMachineNoPeriodic)
rules.AddRule(PtxmStatePeriodicTx, PtxmEventBegin, ptxm.DrcpPtxMachineNoPeriodic)
// NOT IPP PORT ENABLED > NO PERIODIC
rules.AddRule(PtxmStateNone, PtxmEventNotIPPPortEnabled, ptxm.DrcpPtxMachineNoPeriodic)
rules.AddRule(PtxmStateNoPeriodic, PtxmEventNotIPPPortEnabled, ptxm.DrcpPtxMachineNoPeriodic)
rules.AddRule(PtxmStateFastPeriodic, PtxmEventNotIPPPortEnabled, ptxm.DrcpPtxMachineNoPeriodic)
rules.AddRule(PtxmStateSlowPeriodic, PtxmEventNotIPPPortEnabled, ptxm.DrcpPtxMachineNoPeriodic)
rules.AddRule(PtxmStatePeriodicTx, PtxmEventNotIPPPortEnabled, ptxm.DrcpPtxMachineNoPeriodic)
// Unconditional > FAST PERIODIC
rules.AddRule(PtxmStateNoPeriodic, PtxmEventUnconditionalFallThrough, ptxm.DrcpPtxMachineFastPeriodic)
// IPP PORT ENABLED AND DRCP ENABLED -> EXPIRED
rules.AddRule(PtxmStateFastPeriodic, PtxmEventDRFNeighborOPerDRCPStateTimeoutEqualLongTimeout, ptxm.DrcpPtxMachineSlowPeriodic)
// PERIODIC TIMER EXPIRED -> PERIODIC TX
rules.AddRule(PtxmStateFastPeriodic, PtxmEventDRCPPeriodicTimerExpired, ptxm.DrcpPtxMachinePeriodicTx)
rules.AddRule(PtxmStateSlowPeriodic, PtxmEventDRCPPeriodicTimerExpired, ptxm.DrcpPtxMachinePeriodicTx)
// DRF NEIGHBOR OPER DRCP STATE == SHORT TIMEOUT -> FAST PERIODIC OR PERIODIC TX
rules.AddRule(PtxmStatePeriodicTx, PtxmEventDRFNeighborOPerDRCPStateTimeoutEqualShortTimeout, ptxm.DrcpPtxMachineFastPeriodic)
rules.AddRule(PtxmStateSlowPeriodic, PtxmEventDRFNeighborOPerDRCPStateTimeoutEqualShortTimeout, ptxm.DrcpPtxMachinePeriodicTx)
// DRF NEIGHBOR OPER DRCP STATE == LONG TIMEOUT -> SLOW PERIODIC
rules.AddRule(PtxmStatePeriodicTx, PtxmEventDRFNeighborOPerDRCPStateTimeoutEqualLongTimeout, ptxm.DrcpPtxMachineSlowPeriodic)
rules.AddRule(PtxmStateFastPeriodic, PtxmEventDRFNeighborOPerDRCPStateTimeoutEqualLongTimeout, ptxm.DrcpPtxMachineSlowPeriodic)
// Create a new FSM and apply the rules
ptxm.Apply(&rules)
return ptxm
}
// DrcpPtxMachineMain: 802.1ax-2014 Figure 9-24
// Creation of DRCP Periodic Transmit State Machine State transitions and callbacks
// and create go routine to pend on events
func (p *DRCPIpp) DrcpPtxMachineMain() {
// Build the State machine for Lacp Receive Machine according to
// 802.1ax Section 6.4.12 Receive Machine
ptxm := DrcpPtxMachineFSMBuild(p)
p.wg.Add(1)
// set the inital State
ptxm.Machine.Start(ptxm.PrevState())
// lets create a go routing which will wait for the specific events
// that the PtMachine should handle.
go func(m *PtxMachine) {
m.DrcpPtxmLog("Machine Start")
defer m.p.wg.Done()
for {
select {
case <-m.periodicTimer.C:
m.Machine.ProcessEvent(PtxMachineModuleStr, PtxmEventDRCPPeriodicTimerExpired, nil)
if m.Machine.Curr.CurrentState() == PtxmStatePeriodicTx {
if !p.DRFNeighborOperDRCPState.GetState(layers.DRCPStateDRCPTimeout) {
m.Machine.ProcessEvent(PtxMachineModuleStr, PtxmEventDRFNeighborOPerDRCPStateTimeoutEqualLongTimeout, nil)
} else {
m.Machine.ProcessEvent(PtxMachineModuleStr, PtxmEventDRFNeighborOPerDRCPStateTimeoutEqualShortTimeout, nil)
}
}
case event, ok := <-m.PtxmEvents:
if ok {
rv := m.Machine.ProcessEvent(event.Src, event.E, nil)
if rv == nil {
p := m.p
/* continue State transition */
if m.Machine.Curr.CurrentState() == PtxmStateNoPeriodic {
rv = m.Machine.ProcessEvent(PtxMachineModuleStr, PtxmEventUnconditionalFallThrough, nil)
}
// post processing
if rv == nil {
if m.Machine.Curr.CurrentState() == PtxmStateFastPeriodic &&
!p.DRFNeighborOperDRCPState.GetState(layers.DRCPStateDRCPTimeout) {
rv = m.Machine.ProcessEvent(PtxMachineModuleStr, PtxmEventDRFNeighborOPerDRCPStateTimeoutEqualLongTimeout, nil)
}
if rv == nil &&
m.Machine.Curr.CurrentState() == PtxmStateSlowPeriodic &&
p.DRFNeighborOperDRCPState.GetState(layers.DRCPStateDRCPTimeout) {
rv = m.Machine.ProcessEvent(PtxMachineModuleStr, PtxmEventDRFNeighborOPerDRCPStateTimeoutEqualShortTimeout, nil)
}
if rv == nil &&
m.Machine.Curr.CurrentState() == PtxmStatePeriodicTx {
if !p.DRFNeighborOperDRCPState.GetState(layers.DRCPStateDRCPTimeout) {
rv = m.Machine.ProcessEvent(PtxMachineModuleStr, PtxmEventDRFNeighborOPerDRCPStateTimeoutEqualLongTimeout, nil)
} else {
rv = m.Machine.ProcessEvent(PtxMachineModuleStr, PtxmEventDRFNeighborOPerDRCPStateTimeoutEqualShortTimeout, nil)
}
}
}
}
if rv != nil {
m.DrcpPtxmLog(strings.Join([]string{error.Error(rv), event.Src, PtxmStateStrMap[m.Machine.Curr.CurrentState()], strconv.Itoa(int(event.E))}, ":"))
}
// respond to caller if necessary so that we don't have a deadlock
if event.ResponseChan != nil {
utils.SendResponse(PtxMachineModuleStr, event.ResponseChan)
}
} else {
m.DrcpPtxmLog("Machine End")
return
}
}
}
}(ptxm)
}