-
Notifications
You must be signed in to change notification settings - Fork 0
/
subsys.go
338 lines (306 loc) · 8.04 KB
/
subsys.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
package main
import (
"errors"
"fmt"
"net"
"os"
"sort"
"sync"
"time"
)
// Subsys is the virtual node that represents a subsystem,
// it communicates with outside real subsystem and pass packets to TSN switches
type Subsys struct {
name string
position [2]int
id int
// recv conn from other node
inConn *net.UDPConn
recvCnt int
fwdCnt int
portsIn [PORT_NUM_SUBSYS]*Port
portsOut [PORT_NUM_SUBSYS]*Port
portsInIdx int
portsOutIdx int
Priority int // will overwrite priority in packets
RoutingTable map[string][]RoutingEntry
SeqRecoverHistory map[int32]bool // for frer
SeqRecoverHistoryMutex sync.Mutex
logMutex sync.Mutex
stopSig chan bool
}
// returns a Subsys pointer
func NewSubsys(name string, position [2]int) *Subsys {
var portsIn [PORT_NUM_SUBSYS]*Port
var portsOut [PORT_NUM_SUBSYS]*Port
for i := 0; i < PORT_NUM_SUBSYS; i++ {
portsIn[i] = NewPort(i, name)
portsOut[i] = NewPort(i, name)
}
s := &Subsys{
name: name,
position: position,
id: subsysName2ID(name),
Priority: 1,
portsIn: portsIn,
portsOut: portsOut,
portsInIdx: -1,
portsOutIdx: -1,
RoutingTable: make(map[string][]RoutingEntry),
SeqRecoverHistory: make(map[int32]bool),
stopSig: make(chan bool),
}
for subsys := range SUBSYS_MAP {
if subsys == name {
continue
}
paths := Graph.FindAllPaths(name, subsys)
table := []RoutingEntry{}
for _, p := range paths {
entry := RoutingEntry{NextHop: p[1], HopCount: len(p) - 1}
found := false
for _, e := range table {
if e.NextHop == entry.NextHop {
found = true
if e.HopCount > entry.HopCount {
e.HopCount = entry.HopCount
}
}
}
if !found {
table = append(table, entry)
}
}
sort.SliceStable(table, func(i, j int) bool {
return table[i].HopCount < table[j].HopCount
})
s.RoutingTable[subsys] = table
}
go s.Start()
Subsystems = append(Subsystems, s)
return s
}
// implement Node interface
func (s *Subsys) Name() string {
return s.name
}
// implement Node interface
func (s *Subsys) OutPort() *Port {
s.portsOutIdx++
return s.portsOut[s.portsOutIdx]
}
// implement Node interface
func (s *Subsys) InPort() *Port {
s.portsInIdx++
return s.portsIn[s.portsInIdx]
}
// Start the Subsys
func (s *Subsys) Start() {
fmt.Printf("Start virtual subsys (id: %d) - %s: local_addr: %s, remote_addr: %s\n",
s.id, s.name, os.Getenv("ADDR_LOCAL_"+s.name), os.Getenv("ADDR_REMOTE_"+s.name))
udpAddr, err := net.ResolveUDPAddr("udp", os.Getenv("ADDR_LOCAL_"+s.name))
if err != nil {
fmt.Println("invalid address")
return
}
s.inConn, err = net.ListenUDP("udp", udpAddr)
if err != nil {
fmt.Println(err)
return
}
go s.handlePacket()
for _, p := range s.portsIn {
go s.handleMessage(p)
}
}
// Stop the Subsys
func (s *Subsys) Stop() {
for range s.portsIn {
s.stopSig <- true // stop inports
}
s.inConn.Close() // stop udp server
// fmt.Println(s.name, "stopped")
}
// receive external UDP packets
func (s *Subsys) handlePacket() {
for {
var buf [PKT_BUF_LEN]byte
n, err := s.inConn.Read(buf[0:])
if err != nil {
// fmt.Println(err)
return
}
go func(buffer []byte) {
pkt := new(Packet)
err = pkt.FromBuf(buf[0:n])
if err != nil {
fmt.Println(err)
return
}
pkt.Priority = uint8(s.Priority)
pkt.Seq = getSeqNum()
pkt.UID = getUID()
pkt.RxTimestamp = time.Now().UnixNano()
// fmt.Println("packet recv #", pkt.Seq, pkt.RxTimestamp)
if pkt.Src != uint8(s.id) {
fmt.Printf("[%s]WARNING! SRC doesn't match\n", s.name)
}
if ANIMATION_ENABLED {
WSLog <- Log{
Type: WSLOG_PKT_TX,
PktTx: PktTx{Node: s.name, UID: pkt.UID},
}
}
if p, err := s.routing(pkt); err == nil {
s.send(pkt, p)
} else {
fmt.Println(err)
}
}(buf[0:n])
}
}
// send internal messages from switches to outside
func (s *Subsys) handleMessage(inPort *Port) {
// fmt.Println(s.name, "waiting msg from switches")
for {
select {
case <-s.stopSig:
// fmt.Println(s.name, "terminate an inport goroutine")
return
case pkt := <-inPort.Channel:
s.recvCnt++
if FRER_ENABLED || DUP_ELI_ENABLED {
// eliminate dup
s.SeqRecoverHistoryMutex.Lock()
if _, ok := s.SeqRecoverHistory[pkt.Seq]; ok {
// fmt.Println(sw.name, "eliminate dup from", pkt.Path[len(pkt.Path)-1])
s.SeqRecoverHistoryMutex.Unlock()
continue
}
s.SeqRecoverHistory[pkt.Seq] = true
s.SeqRecoverHistoryMutex.Unlock()
}
pkt.Path = append(pkt.Path, s.name)
pkt.TxTimestamp = time.Now().UnixNano()
if s.name == "HMS" &&
(pkt.Dst == uint8(subsysName2ID("GCC")) ||
(pkt.Src == uint8(subsysName2ID("GCC")) && pkt.Dst != uint8(subsysName2ID("HMS")))) {
if p, err := s.routing(pkt); err == nil {
s.send(pkt, p)
} else {
fmt.Println(err)
}
} else {
// fmt.Println(s.name, "packet send out #", pkt.Seq, pkt.TxTimestamp)
// fmt.Println(pkt.RawBytes)
// if !pkt.IsSim {
go func() {
outConn, err := net.Dial("udp", os.Getenv("ADDR_REMOTE_"+s.name))
if err != nil {
fmt.Println(err)
return
}
_, err = outConn.Write(pkt.RawBytes)
if err != nil {
fmt.Printf("[%s] sending UDP to remote error %v\n", s.name, err)
}
}()
// }
if ANIMATION_ENABLED {
WSLog <- Log{
Type: WSLOG_PKT_TX,
PktTx: PktTx{Node: s.name, UID: pkt.UID},
}
go func() {
time.Sleep(1 * time.Second)
WSLog <- Log{
Type: WSLOG_PKT_TX,
PktTx: PktTx{Node: s.name, UID: pkt.UID, Finished: true},
}
}()
}
if SAVE_STATS {
go saveStatsDelay(s.name, subsysID2Name(pkt.Src), pkt.Seq, float64(pkt.TxTimestamp-pkt.RxTimestamp)/1000) // pkt.Delay)
}
if pkt.Delay < 1 {
pkt.Delay *= 1000000
// fmt.Printf("Pkt #%d: %d bytes, %v, delay: %.3f us\n", pkt.Seq, len(pkt.RawBytes), pkt.Path, pkt.Delay)
// fmt.Printf("Pkt #%d: %d bytes, %v, delay: %v us\n", pkt.Seq, len(pkt.RawBytes), pkt.Path, (pkt.TxTimestamp-pkt.RxTimestamp)/1000)
if CONSOLE_ENABLED {
WSLog <- Log{
Type: WSLOG_MSG,
Msg: fmt.Sprintf("Pkt #%d: %d bytes, %v, delay: %v us", pkt.Seq, len(pkt.RawBytes), pkt.Path, (pkt.TxTimestamp-pkt.RxTimestamp)/1000),
// Msg: fmt.Sprintf("Pkt #%d: %d bytes, %v, delay: %.3f us", pkt.Seq, len(pkt.RawBytes), pkt.Path, pkt.Delay),
}
}
} else {
// fmt.Printf("Pkt #%d: %d bytes, %v, delay: %.3f us\n", pkt.Seq, len(pkt.RawBytes), pkt.Path, pkt.Delay)
if CONSOLE_ENABLED {
WSLog <- Log{
Type: WSLOG_MSG,
Msg: fmt.Sprintf("Pkt #%d: %d bytes, %v, delay: %.2f s", pkt.Seq, len(pkt.RawBytes), pkt.Path, pkt.Delay),
}
}
}
}
}
}
}
// create a simulated internal packet flow
func (s *Subsys) CreateFlow(dst int) {
pkt := &Packet{
Src: uint8(s.id),
Dst: uint8(dst),
Priority: uint8(s.Priority),
IsSim: true,
}
var buf [64]byte
buf[0] = pkt.Src
buf[1] = pkt.Dst
pkt.RawBytes = buf[:]
pkt.RxTimestamp = time.Now().UnixNano()
pkt.Seq = getSeqNum()
pkt.UID = getUID()
if ANIMATION_ENABLED {
WSLog <- Log{
Type: WSLOG_PKT_TX,
PktTx: PktTx{Node: s.name, UID: pkt.UID},
}
}
if p, err := s.routing(pkt); err == nil {
s.send(pkt, p)
} else {
fmt.Println(err)
}
}
// find the right port to send this packet
func (s *Subsys) routing(pkt *Packet) (*Port, error) {
L1:
for _, entry := range s.RoutingTable[subsysID2Name(pkt.Dst)] {
if entry.NextHop[:2] == "SW" {
for _, swww := range Switches {
if REROUTE_ENABLED {
if swww.name == entry.NextHop &&
swww.Faults[FAULT_FAILURE].Happening {
continue L1
}
}
}
}
for _, p := range s.portsOut {
if p.Neighbor == entry.NextHop {
return p, nil
}
}
}
return nil, errors.New("[" + s.name + "] cannot found next hop to " + string(pkt.Dst))
}
// send a packet out from a port
func (s *Subsys) send(pkt *Packet, port *Port) {
// fmt.Println("sent to", port.Neighbor)
pkt.Path = append(pkt.Path, s.name)
port.Channel <- pkt
s.logMutex.Lock()
s.fwdCnt++
s.logMutex.Unlock()
}