/
asic.go
143 lines (126 loc) · 3.5 KB
/
asic.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
package ctrl_mgr
import (
"github.com/google/gopacket"
"github.com/google/gopacket/layers"
"github.com/horus-scheduler/horus_controller/core/model"
horus_net "github.com/horus-scheduler/horus_controller/core/net"
"github.com/sirupsen/logrus"
)
type asicEndPoint struct {
ifName string
client *horus_net.RawSockClient
// Leaf controllers channels
leafIngress *model.ByteChanMap
leafEgress *model.ByteChanMap
// Spine controllers channels
// TODO: spine controllers *currently* don't talk with the ASIC
spineIngress *model.ByteChanMap
spineEgress *model.ByteChanMap
// asic channels
asicIngress chan []byte
asicEgress chan []byte
aggEgress chan []byte
doneChan chan bool
}
func NewAsicEndPoint(ifName string,
leaves []*leafController,
spines []*spineController,
asicIngress chan []byte,
asicEgress chan []byte) *asicEndPoint {
client := horus_net.NewRawSockClient(ifName, asicEgress, asicIngress)
client.Connect()
horus_net.TestHorusPkt(horus_net.PKT_TYPE_KEEP_ALIVE, 0, 12, 16, 123, []byte("Dummy"))
leafIngress := model.NewByteChanMap()
leafEgress := model.NewByteChanMap()
spineIngress := model.NewByteChanMap()
spineEgress := model.NewByteChanMap()
aggEgress := make(chan []byte)
a := &asicEndPoint{ifName: ifName,
client: client,
asicIngress: asicIngress,
asicEgress: asicEgress,
aggEgress: aggEgress,
leafIngress: leafIngress,
leafEgress: leafEgress,
spineIngress: spineIngress,
spineEgress: spineEgress,
doneChan: make(chan bool, 1),
}
for _, l := range leaves {
a.AddLeafCtrl(l)
}
for _, s := range spines {
spineIngress.Store(s.ID, s.asicIngress)
spineEgress.Store(s.ID, s.asicEgress)
}
return a
}
func (a *asicEndPoint) Start() {
go a.client.Start()
go a.read()
go a.write()
<-a.doneChan
}
func (a *asicEndPoint) AddLeafCtrl(leaf *leafController) {
logrus.Infof("[ASIC] Adding a leaf controller %d", leaf.ID)
a.leafIngress.Store(leaf.ID, leaf.asicIngress)
a.leafEgress.Store(leaf.ID, leaf.asicEgress)
go func(c chan []byte) {
for msg := range c {
a.aggEgress <- msg
}
}(leaf.asicEgress)
}
func (a *asicEndPoint) RemoveLeafCtrl(leaf *leafController) {
logrus.Infof("[ASIC] Removing a leaf controller %d", leaf.ID)
logrus.Warn("[ASIC] RemoveLeafCtrl is experimental")
a.leafIngress.Delete(leaf.ID)
a.leafEgress.Delete(leaf.ID)
}
// Reads pkts from the ASIC
func (a *asicEndPoint) read() {
for {
select {
case msg := <-a.asicIngress:
//logrus.Debug("[ASIC] Received msg: %s", msg)
pkt := gopacket.NewPacket(msg, layers.LayerTypeEthernet, gopacket.Default)
if horusLayer := pkt.Layer(horus_net.LayerTypeHorus); horusLayer != nil {
horus, _ := horusLayer.(*horus_net.HorusPacket)
dstID := horus.DstID
//logrus.Debug("[ASIC] Received Horus pkt type ", horus.PktType)
ch, found := a.leafIngress.Load(dstID)
//logrus.Debug("[ASIC] Dst ID ", dstID)
if found {
ch <- pkt.Data()
} else {
logrus.Warnf("[ASIC] Leaf controller %d does not exist", dstID)
}
} else {
logrus.Warnf("[ASIC] Ctrl pkt from ASIC not in correct format")
}
}
}
}
// Writes pkts to the ASIC
func (a *asicEndPoint) write() {
// for _, ch := range a.leafEgress.Internal() {
// go func(c chan []byte) {
// for msg := range c {
// a.AggEgress <- msg
// }
// }(ch)
// }
// for _, ch := range a.spineEgress.Internal() {
// go func(c chan []byte) {
// for msg := range c {
// a.AggEgress <- msg
// }
// }(ch)
// }
for {
select {
case msg := <-a.aggEgress:
a.asicEgress <- msg
}
}
}