forked from FlexSNR/l2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
txmachine.go
613 lines (535 loc) · 22.7 KB
/
txmachine.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
//
//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 / | (----` \ \/ \/ / | | `---| |----`| ,----'| |__| |
// | __| | | | __| > < \ \ \ / | | | | | | | __ |
// | | | `----.| |____ / . \ .----) | \ /\ / | | | | | `----.| | | |
// |__| |_______||_______/__/ \__\ |_______/ \__/ \__/ |__| |__| \______||__| |__|
//
// TX MACHINE, this is not really a State machine but going to create a sort of
// State machine to processes events
// TX Machine is described in 802.1ax-2014 6.4.16
package drcp
import (
"fmt"
"l2/lacp/protocol/utils"
"math"
"strconv"
"strings"
"utils/fsm"
"github.com/google/gopacket/layers"
)
const TxMachineModuleStr = "DRCP Tx Machine"
const (
TxmStateNone = iota + 1
TxmStateOn
TxmStateOff
)
var TxmStateStrMap map[fsm.State]string
func TxMachineStrStateMapCreate() {
TxmStateStrMap = make(map[fsm.State]string)
TxmStateStrMap[TxmStateNone] = "None"
TxmStateStrMap[TxmStateOn] = "On"
TxmStateStrMap[TxmStateOff] = "Off"
}
const (
TxmEventBegin = iota + 1
TxmEventUnconditionalFallThrough
TxmEventNtt
TxmEventDrcpDisabled
TxmEventDrcpEnabled
)
// TxMachine holds FSM and current State
// and event channels for State transitions
type TxMachine struct {
// for debugging
PreviousState fsm.State
Machine *fsm.Machine
// Port this Machine is associated with
p *DRCPIpp
// machine specific events
TxmEvents chan utils.MachineEvent
}
// PrevState will get the previous State from the State transitions
func (txm *TxMachine) PrevState() fsm.State { return txm.PreviousState }
// PrevStateSet will set the previous State
func (txm *TxMachine) PrevStateSet(s fsm.State) { txm.PreviousState = s }
// Stop will stop all timers and close all channels
func (txm *TxMachine) Stop() {
close(txm.TxmEvents)
}
// NewDrcpTxMachine will create a new instance of the TxMachine
func NewDrcpTxMachine(port *DRCPIpp) *TxMachine {
txm := &TxMachine{
p: port,
PreviousState: TxmStateNone,
TxmEvents: make(chan utils.MachineEvent, 1000)}
port.TxMachineFsm = txm
return txm
}
// A helpful function that lets us apply arbitrary rulesets to this
// instances State machine without reallocating the machine.
func (txm *TxMachine) Apply(r *fsm.Ruleset) *fsm.Machine {
if txm.Machine == nil {
txm.Machine = &fsm.Machine{}
}
// Assign the ruleset to be used for this machine
txm.Machine.Rules = r
txm.Machine.Curr = &utils.StateEvent{
StrStateMap: TxmStateStrMap,
LogEna: false,
Logger: txm.DrcpTxmLog,
Owner: TxMachineModuleStr,
}
return txm.Machine
}
// DrcpTxMachineOn will either send a packet out
func (txm *TxMachine) DrcpTxMachineOn(m fsm.Machine, data interface{}) fsm.State {
p := txm.p
dr := p.dr
// NTT must be set to tx
if p.NTTDRCPDU &&
p.DRCPEnabled {
dr.DRFHomeOperDRCPState.SetState(layers.DRCPStateIPPActivity)
numPkts := 1
if p.GatewayConversationTransmit &&
p.PortConversationTransmit &&
!dr.DrniCommonMethods {
numPkts = 2
}
for pkt := 0; pkt < numPkts; pkt++ {
// increment tx counter
p.DRCPDUsTX++
// Ethernet + ethertype
pktLength := uint32(14)
drcp := layers.DRCP{
SubType: layers.DRCPSubProtocolDRCP,
Version: layers.DRCPVersion1,
}
// Lets fill the in the various TLV's
drcp.PortalInfo = layers.DRCPPortalInfoTlv{
AggPriority: dr.DrniAggregatorPriority,
AggId: dr.DrniAggregatorId,
PortalPriority: dr.DrniPortalPriority,
PortalAddr: [6]uint8{
dr.DrniPortalAddr[0],
dr.DrniPortalAddr[1],
dr.DrniPortalAddr[2],
dr.DrniPortalAddr[3],
dr.DrniPortalAddr[4],
dr.DrniPortalAddr[5],
},
}
drcp.PortalInfo.TlvTypeLength.SetTlv(uint16(layers.DRCPTLVTypePortalInfo))
drcp.PortalInfo.TlvTypeLength.SetLength(uint16(layers.DRCPTLVPortalInfoLength))
pktLength += uint32(layers.DRCPTLVPortalInfoLength) + uint32(layers.DRCPTlvAndLengthSize)
drcp.PortalConfigInfo = layers.DRCPPortalConfigurationInfoTlv{
OperAggKey: dr.DRFHomeOperAggregatorKey,
PortAlgorithm: dr.DRFHomePortAlgorithm,
GatewayAlgorithm: dr.DRFHomeGatewayAlgorithm,
GatewayDigest: dr.DRFHomeConversationGatewayListDigest.get16Bytes(),
PortDigest: dr.DRFHomeConversationPortListDigest.get16Bytes(),
}
drcp.PortalConfigInfo.TopologyState.SetState(layers.DRCPTopologyStatePortalSystemNum, dr.DrniPortalSystemNumber)
drcp.PortalConfigInfo.TopologyState.SetState(layers.DRCPTopologyStateNeighborConfPortalSystemNumber, p.DRFHomeConfNeighborPortalSystemNumber)
val := uint8(0)
if dr.DrniThreeSystemPortal {
val = 1
}
drcp.PortalConfigInfo.TopologyState.SetState(layers.DRCPTopologyState3SystemPortal, val)
val = 0
if dr.DrniPortConversationControl {
val = 1
}
drcp.PortalConfigInfo.TopologyState.SetState(layers.DRCPTopologyStateCommonMethods, val)
drcp.PortalConfigInfo.TlvTypeLength.SetTlv(uint16(layers.DRCPTLVTypePortalConfigInfo))
drcp.PortalConfigInfo.TlvTypeLength.SetLength(uint16(layers.DRCPTLVPortalConfigurationInfoLength))
// TODO set Port Digest and Gateway Digest
pktLength += uint32(layers.DRCPTLVPortalConfigurationInfoLength) + uint32(layers.DRCPTlvAndLengthSize)
if p.GatewayConversationTransmit &&
p.PortConversationTransmit &&
dr.DrniCommonMethods {
if !dr.DrniThreeSystemPortal {
drcp.TwoPortalPortConversationVector.TlvTypeLength.SetTlv(uint16(layers.DRCPTLV2PPortConversationVector))
drcp.TwoPortalPortConversationVector.TlvTypeLength.SetLength(uint16(layers.DRCPTLV2PPortConversationVectorLength))
pktLength += uint32(layers.DRCPTLV2PPortConversationVectorLength) + uint32(layers.DRCPTlvAndLengthSize)
// lets only send the port conversation vector
for i, j := 0, 0; i < 512; i, j = i+1, j+8 {
if dr.DrniPortalSystemPortConversation[j] {
drcp.TwoPortalPortConversationVector.Vector[i] = 1 << 7
}
if dr.DrniPortalSystemPortConversation[j+1] {
drcp.TwoPortalPortConversationVector.Vector[i] = 1 << 6
}
if dr.DrniPortalSystemPortConversation[j+2] {
drcp.TwoPortalPortConversationVector.Vector[i] = 1 << 5
}
if dr.DrniPortalSystemPortConversation[j+3] {
drcp.TwoPortalPortConversationVector.Vector[i] = 1 << 4
}
if dr.DrniPortalSystemPortConversation[j+4] {
drcp.TwoPortalPortConversationVector.Vector[i] = 1 << 3
}
if dr.DrniPortalSystemPortConversation[j+5] {
drcp.TwoPortalPortConversationVector.Vector[i] = 1 << 2
}
if dr.DrniPortalSystemPortConversation[j+6] {
drcp.TwoPortalPortConversationVector.Vector[i] = 1 << 1
}
if dr.DrniPortalSystemPortConversation[j+7] {
drcp.TwoPortalPortConversationVector.Vector[i] = 1 << 0
}
}
} else {
// TODO 3P Not supported
}
} else if p.GatewayConversationTransmit &&
p.PortConversationTransmit &&
!dr.DrniCommonMethods {
if pkt == 0 {
if !dr.DrniThreeSystemPortal {
drcp.TwoPortalGatewayConversationVector.TlvTypeLength.SetTlv(uint16(layers.DRCPTLV2PGatewayConversationVector))
drcp.TwoPortalGatewayConversationVector.TlvTypeLength.SetLength(uint16(layers.DRCPTLV2PGatewayConversationVectorLength))
pktLength += uint32(layers.DRCPTLV2PGatewayConversationVectorLength) + uint32(layers.DRCPTlvAndLengthSize)
for i, j := 0, 0; i < 512; i, j = i+1, j+8 {
if dr.DrniPortalSystemGatewayConversation[j] {
drcp.TwoPortalGatewayConversationVector.Vector[i] = 1 << 7
}
if dr.DrniPortalSystemGatewayConversation[j+1] {
drcp.TwoPortalGatewayConversationVector.Vector[i] = 1 << 6
}
if dr.DrniPortalSystemGatewayConversation[j+2] {
drcp.TwoPortalGatewayConversationVector.Vector[i] = 1 << 5
}
if dr.DrniPortalSystemGatewayConversation[j+3] {
drcp.TwoPortalGatewayConversationVector.Vector[i] = 1 << 4
}
if dr.DrniPortalSystemGatewayConversation[j+4] {
drcp.TwoPortalGatewayConversationVector.Vector[i] = 1 << 3
}
if dr.DrniPortalSystemGatewayConversation[j+5] {
drcp.TwoPortalGatewayConversationVector.Vector[i] = 1 << 2
}
if dr.DrniPortalSystemGatewayConversation[j+6] {
drcp.TwoPortalGatewayConversationVector.Vector[i] = 1 << 1
}
if dr.DrniPortalSystemGatewayConversation[j+7] {
drcp.TwoPortalGatewayConversationVector.Vector[i] = 1 << 0
}
}
} else {
// TODO 3P Not supported
}
} else {
drcp.TwoPortalPortConversationVector.TlvTypeLength.SetTlv(uint16(layers.DRCPTLV2PPortConversationVector))
drcp.TwoPortalPortConversationVector.TlvTypeLength.SetLength(uint16(layers.DRCPTLV2PPortConversationVectorLength))
pktLength += uint32(layers.DRCPTLV2PPortConversationVectorLength) + uint32(layers.DRCPTlvAndLengthSize)
// lets only send the port conversation vector
if !dr.DrniThreeSystemPortal {
for i, j := 0, 0; i < 512; i, j = i+1, j+8 {
if dr.DrniPortalSystemPortConversation[j] {
drcp.TwoPortalPortConversationVector.Vector[i] = 1 << 7
}
if dr.DrniPortalSystemPortConversation[j+1] {
drcp.TwoPortalPortConversationVector.Vector[i] = 1 << 6
}
if dr.DrniPortalSystemPortConversation[j+2] {
drcp.TwoPortalPortConversationVector.Vector[i] = 1 << 5
}
if dr.DrniPortalSystemPortConversation[j+3] {
drcp.TwoPortalPortConversationVector.Vector[i] = 1 << 4
}
if dr.DrniPortalSystemPortConversation[j+4] {
drcp.TwoPortalPortConversationVector.Vector[i] = 1 << 3
}
if dr.DrniPortalSystemPortConversation[j+5] {
drcp.TwoPortalPortConversationVector.Vector[i] = 1 << 2
}
if dr.DrniPortalSystemPortConversation[j+6] {
drcp.TwoPortalPortConversationVector.Vector[i] = 1 << 1
}
if dr.DrniPortalSystemPortConversation[j+7] {
drcp.TwoPortalPortConversationVector.Vector[i] = 1 << 0
}
}
} else {
// TODO 3P Not supported
}
}
} else if p.GatewayConversationTransmit {
if !dr.DrniThreeSystemPortal {
drcp.TwoPortalGatewayConversationVector.TlvTypeLength.SetTlv(uint16(layers.DRCPTLV2PGatewayConversationVector))
drcp.TwoPortalGatewayConversationVector.TlvTypeLength.SetLength(uint16(layers.DRCPTLV2PGatewayConversationVectorLength))
pktLength += uint32(layers.DRCPTLV2PGatewayConversationVectorLength) + uint32(layers.DRCPTlvAndLengthSize)
drcp.TwoPortalGatewayConversationVector.Vector = make([]uint8, 512)
for i, j := 0, 0; i < 512; i, j = i+1, j+8 {
if dr.DrniPortalSystemGatewayConversation[j] {
drcp.TwoPortalGatewayConversationVector.Vector[i] = 1 << 7
}
if dr.DrniPortalSystemGatewayConversation[j+1] {
drcp.TwoPortalGatewayConversationVector.Vector[i] = 1 << 6
}
if dr.DrniPortalSystemGatewayConversation[j+2] {
drcp.TwoPortalGatewayConversationVector.Vector[i] = 1 << 5
}
if dr.DrniPortalSystemGatewayConversation[j+3] {
drcp.TwoPortalGatewayConversationVector.Vector[i] = 1 << 4
}
if dr.DrniPortalSystemGatewayConversation[j+4] {
drcp.TwoPortalGatewayConversationVector.Vector[i] = 1 << 3
}
if dr.DrniPortalSystemGatewayConversation[j+5] {
drcp.TwoPortalGatewayConversationVector.Vector[i] = 1 << 2
}
if dr.DrniPortalSystemGatewayConversation[j+6] {
drcp.TwoPortalGatewayConversationVector.Vector[i] = 1 << 1
}
if dr.DrniPortalSystemGatewayConversation[j+7] {
drcp.TwoPortalGatewayConversationVector.Vector[i] = 1 << 0
}
}
} else {
// TODO 3P Not supported
}
} else if p.PortConversationTransmit {
if !dr.DrniThreeSystemPortal {
drcp.TwoPortalGatewayConversationVector.TlvTypeLength.SetTlv(uint16(layers.DRCPTLV2PPortConversationVector))
drcp.TwoPortalGatewayConversationVector.TlvTypeLength.SetLength(uint16(layers.DRCPTLV2PPortConversationVectorLength))
pktLength += uint32(layers.DRCPTLV2PPortConversationVectorLength) + uint32(layers.DRCPTlvAndLengthSize)
for i, j := 0, 0; i < 512; i, j = i+1, j+8 {
if dr.DrniPortalSystemPortConversation[j] {
drcp.TwoPortalGatewayConversationVector.Vector[i] = 1 << 7
}
if dr.DrniPortalSystemPortConversation[j+1] {
drcp.TwoPortalGatewayConversationVector.Vector[i] = 1 << 6
}
if dr.DrniPortalSystemPortConversation[j+2] {
drcp.TwoPortalGatewayConversationVector.Vector[i] = 1 << 5
}
if dr.DrniPortalSystemPortConversation[j+3] {
drcp.TwoPortalGatewayConversationVector.Vector[i] = 1 << 4
}
if dr.DrniPortalSystemPortConversation[j+4] {
drcp.TwoPortalGatewayConversationVector.Vector[i] = 1 << 3
}
if dr.DrniPortalSystemPortConversation[j+5] {
drcp.TwoPortalGatewayConversationVector.Vector[i] = 1 << 2
}
if dr.DrniPortalSystemPortConversation[j+6] {
drcp.TwoPortalGatewayConversationVector.Vector[i] = 1 << 1
}
if dr.DrniPortalSystemPortConversation[j+7] {
drcp.TwoPortalGatewayConversationVector.Vector[i] = 1 << 0
}
}
} else {
// TODO 3P Not supported
}
}
drcp.State.TlvTypeLength.SetTlv(uint16(layers.DRCPTLVTypeDRCPState))
drcp.State.TlvTypeLength.SetLength(uint16(layers.DRCPTLVStateLength))
drcp.State.State = dr.DRFHomeOperDRCPState
pktLength += uint32(layers.DRCPTLVStateLength) + uint32(layers.DRCPTlvAndLengthSize)
drcp.HomePortsInfo = layers.DRCPHomePortsInfoTlv{
AdminAggKey: dr.DRFHomeAdminAggregatorKey,
OperPartnerAggKey: dr.DRFHomeOperAggregatorKey,
}
drcp.HomePortsInfo.TlvTypeLength.SetTlv(uint16(layers.DRCPTLVTypeHomePortsInfo))
// length is determine by the number of active ports + 6
homePortsInfoLength := uint32(4)
// TODO protect this list with a lock
if dr.a != nil && dr.DRAggregatorDistributedList != nil {
for _, ifindex := range dr.DRAggregatorDistributedList {
if ifindex > 0 {
drcp.HomePortsInfo.ActiveHomePorts = append(drcp.HomePortsInfo.ActiveHomePorts, uint32(ifindex))
}
}
}
homePortsInfoLength += uint32(4 * len(drcp.HomePortsInfo.ActiveHomePorts))
drcp.HomePortsInfo.TlvTypeLength.SetLength(uint16(homePortsInfoLength))
pktLength += uint32(homePortsInfoLength) + uint32(layers.DRCPTlvAndLengthSize)
drcp.NeighborPortsInfo = layers.DRCPNeighborPortsInfoTlv{
AdminAggKey: p.DRFNeighborAdminAggregatorKey,
OperPartnerAggKey: p.DRFNeighborOperPartnerAggregatorKey,
}
dr.DrniPortalSystemState[p.DRFNeighborPortalSystemNumber].mutex.Lock()
//txm.DrcpTxmLog(fmt.Sprintf("TX: homeportal[%d] neighbor portal[%d]\n", dr.DrniPortalSystemNumber, p.DRFNeighborPortalSystemNumber))
if dr.DrniPortalSystemState[p.DRFNeighborPortalSystemNumber].OpState {
for _, portId := range dr.DrniPortalSystemState[p.DRFNeighborPortalSystemNumber].PortIdList {
drcp.NeighborPortsInfo.ActiveNeighborPorts = append(drcp.NeighborPortsInfo.ActiveNeighborPorts, portId)
}
}
dr.DrniPortalSystemState[p.DRFNeighborPortalSystemNumber].mutex.Unlock()
drcp.NeighborPortsInfo.TlvTypeLength.SetTlv(uint16(layers.DRCPTLVTypeNeighborPortsInfo))
drcp.NeighborPortsInfo.TlvTypeLength.SetLength(uint16(4 + (4 * len(drcp.NeighborPortsInfo.ActiveNeighborPorts))))
//portMtu := uint32(utils.PortConfigMap[int32(p.Id)].Mtu)
portMtu := uint32(32768)
/*
fmt.Printf("TX: HomeGatewayVectorTransmit %t OtherGatewayVectorTRansmit %t pktlength %d mtu %d\n",
dr.HomeGatewayVectorTransmit,
dr.OtherGatewayVectorTransmit,
pktLength,
portMtu)
*/
if (dr.HomeGatewayVectorTransmit ||
dr.OtherGatewayVectorTransmit) &&
pktLength < portMtu {
// TODO WTF is the standard trying to say is supposed to happen here
// Only include it if it does not make the packet exceed the MTU? But other parts of standard say
// the field is manditory
drcp.HomeGatewayVector = layers.DRCPHomeGatewayVectorTlv{}
gatewayvector := dr.DrniPortalSystemState[dr.DrniPortalSystemNumber].getGatewayVectorByIndex(0)
if gatewayvector != nil {
// TODO this should actually be the DRFHomeGatewayConversationMask, which should be the same as below but
// lets follow the standard
if gatewayvector.Vector != nil {
drcp.HomeGatewayVector.Sequence = gatewayvector.Sequence
j := 0
for i, vector := range gatewayvector.Vector {
index := uint(math.Mod(float64(i), 8))
if index == 0 && i != 0 {
j++
}
if vector {
if drcp.HomeGatewayVector.Vector == nil {
drcp.HomeGatewayVector.Vector = make([]uint8, 512)
}
drcp.HomeGatewayVector.Vector[j] |= uint8(1 << (7 - index))
//fmt.Printf("Adding Vector[%d] pkt %d = %d index %d\n", i, j, drcp.HomeGatewayVector.Vector[j], index)
}
}
}
}
drcp.HomeGatewayVector.TlvTypeLength.SetTlv(uint16(layers.DRCPTLVTypeHomeGatewayVector))
if len(drcp.HomeGatewayVector.Vector) > 0 {
drcp.HomeGatewayVector.TlvTypeLength.SetLength(uint16(layers.DRCPTLVHomeGatewayVectorLength_2))
} else {
drcp.HomeGatewayVector.TlvTypeLength.SetLength(uint16(layers.DRCPTLVHomeGatewayVectorLength_1))
}
// 3P system not supported, thus other should not be set
drcp.OtherGatewayVector = layers.DRCPOtherGatewayVectorTlv{}
drcp.OtherGatewayVector.TlvTypeLength.SetTlv(uint16(layers.DRCPTLVTypeOtherGatewayVector))
drcp.OtherGatewayVector.TlvTypeLength.SetLength(uint16(layers.DRCPTLVOtherGatewayVectorLength_1))
} else if (dr.HomeGatewayVectorTransmit ||
dr.OtherGatewayVectorTransmit) &&
pktLength > portMtu {
txm.DrcpTxmLog(fmt.Sprintf("Unable to send packet pkt size %d exceeds MTU %d of IPP %d", pktLength, portMtu, p.Id))
}
drcp.NeighborGatewayVector = layers.DRCPNeighborGatewayVectorTlv{
Sequence: uint32(p.DRFNeighborGatewaySequence),
}
drcp.NeighborGatewayVector.TlvTypeLength.SetTlv(uint16(layers.DRCPTLVTypeNeighborGatewayVector))
drcp.NeighborGatewayVector.TlvTypeLength.SetLength(uint16(layers.DRCPTLVNeighborGatewayVectorLength))
if p.DRFHomeNetworkIPLSharingMethod != ENCAP_METHOD_SHARING_NULL {
drcp.NetworkIPLMethod.TlvTypeLength.SetTlv(uint16(layers.DRCPTLVNetworkIPLSharingMethod))
drcp.NetworkIPLMethod.TlvTypeLength.SetLength(uint16(layers.DRCPTLVNetworkIPLSharingMethodLength))
drcp.NetworkIPLMethod.Method = p.DRFHomeNetworkIPLSharingMethod
pktLength += uint32(layers.DRCPTLVNetworkIPLSharingMethodLength) + uint32(layers.DRCPTlvAndLengthSize)
if p.DRFHomeNetworkIPLSharingMethod != ENCAP_METHOD_SHARING_BY_TIME {
drcp.NetworkIPLEncapsulation.TlvTypeLength.SetTlv(uint16(layers.DRCPTLVNetworkIPLSharingEncapsulation))
drcp.NetworkIPLEncapsulation.TlvTypeLength.SetLength(uint16(layers.DRCPTLVNetworkIPLSharingEncapsulationLength))
for i := 0; i < 16; i++ {
drcp.NetworkIPLEncapsulation.IplEncapDigest[i] = p.DRFHomeNetworkIPLIPLEncapDigest[i]
drcp.NetworkIPLEncapsulation.NetEncapDigest[i] = p.DRFHomeNetworkIPLIPLNetEncapDigest[i]
}
pktLength += uint32(layers.DRCPTLVNetworkIPLSharingEncapsulationLength) + uint32(layers.DRCPTlvAndLengthSize)
}
}
drcp.Terminator.TlvTypeLength.SetTlv(uint16(layers.DRCPTLVTypeTerminator))
drcp.Terminator.TlvTypeLength.SetLength(uint16(layers.DRCPTLVTerminatorLength))
key := IppDbKey{
Name: p.Name,
DrName: p.dr.DrniName,
}
//txm.DrcpTxmLog(fmt.Sprintf("\nTX:%d %+v\n", dr.DrniPortalSystemNumber, drcp))
// transmit the packet(s)
for _, txfunc := range DRGlobalSystem.TxCallbacks[key] {
txfunc(key, p.dr.DrniPortalPortProtocolIDA, &drcp)
}
}
}
// Clear NTT
p.NTTDRCPDU = false
return TxmStateOn
}
// DrcpTxMachineOff will ensure that no packets are transmitted, typically means that
// drcp has been disabled or a packet was just transmitted
func (txm *TxMachine) DrcpTxMachineOff(m fsm.Machine, data interface{}) fsm.State {
p := txm.p
p.NTTDRCPDU = false
return TxmStateOff
}
// DrcpTxMachineFSMBuild will build the State machine with callbacks
func DrcpTxMachineFSMBuild(p *DRCPIpp) *TxMachine {
rules := fsm.Ruleset{}
TxMachineStrStateMapCreate()
// Instantiate a new TxMachine
// Initial State will be a psuedo State known as "begin" so that
// we can transition to the initalize State
txm := NewDrcpTxMachine(p)
//BEGIN -> TX OFF
rules.AddRule(TxmStateNone, TxmEventBegin, txm.DrcpTxMachineOff)
rules.AddRule(TxmStateOn, TxmEventBegin, txm.DrcpTxMachineOff)
rules.AddRule(TxmStateOff, TxmEventBegin, txm.DrcpTxMachineOff)
// NTT -> TX ON
rules.AddRule(TxmStateOff, TxmEventNtt, txm.DrcpTxMachineOn)
// UNCONDITIONAL -> TX OFF
rules.AddRule(TxmStateOn, TxmEventUnconditionalFallThrough, txm.DrcpTxMachineOff)
// Create a new FSM and apply the rules
txm.Apply(&rules)
return txm
}
// TxMachineMain: 802.1ax-2014 Section 9.4.19 DRCPDU Transmit machine
// Creation of Tx State Machine State transitions and callbacks
// and create go routine to pend on events
func (p *DRCPIpp) TxMachineMain() {
// Build the State machine for Transmit Machine according to
// 802.1ax Section 9.4.19 DRCPDU Transmit machine
txm := DrcpTxMachineFSMBuild(p)
p.wg.Add(1)
// set the inital State
txm.Machine.Start(txm.PrevState())
// lets create a go routing which will wait for the specific events
// that the TxMachine should handle.
go func(m *TxMachine) {
m.DrcpTxmLog("Machine Start")
defer m.p.wg.Done()
for {
select {
case event, ok := <-m.TxmEvents:
if ok {
if event.E == TxmEventNtt {
p.NTTDRCPDU = true
}
rv := m.Machine.ProcessEvent(event.Src, event.E, nil)
if rv == nil && m.Machine.Curr.CurrentState() == TxmStateOn {
rv = m.Machine.ProcessEvent(TxMachineModuleStr, TxmEventUnconditionalFallThrough, nil)
}
if event.ResponseChan != nil {
utils.SendResponse(TxMachineModuleStr, event.ResponseChan)
}
if rv != nil {
m.DrcpTxmLog(strings.Join([]string{error.Error(rv), event.Src, TxmStateStrMap[m.Machine.Curr.CurrentState()], strconv.Itoa(int(event.E))}, ":"))
}
} else {
m.DrcpTxmLog("Machine End")
return
}
}
}
}(txm)
}