/
controller.go
316 lines (275 loc) · 9.07 KB
/
controller.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
package ctrl_mgr
import (
"context"
"fmt"
"time"
"github.com/horus-scheduler/horus_controller/core"
"github.com/horus-scheduler/horus_controller/core/model"
horus_net "github.com/horus-scheduler/horus_controller/core/net"
horus_pb "github.com/horus-scheduler/horus_controller/protobuf"
bfrtC "github.com/khaledmdiab/bfrt-go-client/pkg/client"
"github.com/khaledmdiab/bfrt-go-client/pkg/pb"
"github.com/sirupsen/logrus"
)
type controller struct {
ID uint16
Address string
cfg *rootConfig
topology *model.Topology
vcm *core.VCManager
// Controller components
pipeID uint32
// Communicating with the ASIC
asicIngress chan []byte // recv-from the ASIC
asicEgress chan []byte // send-to the ASIC
}
// Leaf-specific logic
type leafController struct {
*controller
cp LeafCP
// Components
ch *LeafBusChan
bus *LeafBus // Main leaf logic
rpcEndPoint *horus_net.LeafRpcEndpoint // RPC server (Horus messages)
healthMgr *core.LeafHealthManager // Tracking the health of downstream nodes
}
// Spine-specific logic
type spineController struct {
*controller
cp SpineCP
// Components
bus *SpineBus // Main spine logic
rpcEndPoint *horus_net.SpineRpcEndpoint // RPC server (Horus messages)
healthMgr *core.LeafHealthManager // Tracking the health of downstream nodes
}
type TopologyOption func(*model.Topology) error
type BfrtUpdateFn func(context.Context, *pb.TableEntry) error
func WithExtraLeaf(leafInfo *horus_pb.LeafInfo) TopologyOption {
return func(topo *model.Topology) error {
_, err := topo.AddLeafToSpine(leafInfo)
return err
}
}
func WithoutLeaves() TopologyOption {
return func(topo *model.Topology) error {
topo.ClearLeaves()
return nil
}
}
func NewBareLeafController(ctrlID uint16, pipeID uint32, cfg *rootConfig, opts ...TopologyOption) *leafController {
asicEgress := make(chan []byte, horus_net.DefaultUnixSockSendSize)
asicIngress := make(chan []byte, horus_net.DefaultUnixSockRecvSize)
hmEgress := make(chan *core.LeafHealthMsg, horus_net.DefaultUnixSockRecvSize)
updatedServersRPC := make(chan *core.LeafHealthMsg, horus_net.DefaultUnixSockRecvSize)
newServersRPC := make(chan *horus_net.ServerAddedMessage, horus_net.DefaultUnixSockRecvSize)
newVCsRPC := make(chan *horus_net.VCUpdatedMessage, horus_net.DefaultUnixSockRecvSize)
rpcEndPoint := horus_net.NewBareLeafRpcEndpoint(ctrlID,
cfg.RemoteSrvServer,
updatedServersRPC,
newServersRPC,
newVCsRPC)
ch := NewLeafBusChan(hmEgress, updatedServersRPC, newServersRPC, newVCsRPC, asicIngress, asicEgress)
return &leafController{
rpcEndPoint: rpcEndPoint,
ch: ch,
controller: &controller{
ID: ctrlID,
cfg: cfg,
pipeID: pipeID,
asicIngress: asicIngress,
asicEgress: asicEgress,
},
}
}
func NewSpineController(ctrlID uint16, pipeID uint32, topoFp string, cfg *rootConfig) *spineController {
// topoCfg := model.ReadTopologyFile(topoFp)
rpcLite := horus_net.NewSpineRpcEndpointLite(cfg.RemoteSrvServer)
topoInfo, err := rpcLite.GetTopology()
if err != nil {
logrus.Fatal(err)
}
topology := model.NewDCNFromTopoInfo(topoInfo)
vcm := core.NewVCManager(topology)
asicEgress := make(chan []byte, horus_net.DefaultUnixSockSendSize)
asicIngress := make(chan []byte, horus_net.DefaultUnixSockRecvSize)
activeNode := make(chan *core.LeafHealthMsg, horus_net.DefaultUnixSockRecvSize)
failedLeaves := make(chan *horus_net.LeafFailedMessage, horus_net.DefaultRpcRecvSize)
failedServers := make(chan *horus_net.ServerFailedMessage, horus_net.DefaultRpcRecvSize)
newLeaves := make(chan *horus_net.LeafAddedMessage, horus_net.DefaultRpcRecvSize)
newServers := make(chan *horus_net.ServerAddedMessage, horus_net.DefaultRpcRecvSize)
newVCs := make(chan *horus_net.VCUpdatedMessage, horus_net.DefaultRpcRecvSize)
spine := topology.GetNode(ctrlID, model.NodeType_Spine)
target := bfrtC.NewTarget(bfrtC.WithDeviceId(cfg.DeviceID), bfrtC.WithPipeId(pipeID))
client := bfrtC.NewClient(cfg.BfrtAddress, cfg.P4Name, uint32(ctrlID), target)
var cp SpineCP
if cfg.ControlAPI == "fake" {
cp = NewFakeSpineCP(spine, topology)
} else if cfg.ControlAPI == "v1" {
cp = NewBfrtSpineCP_V1(client, spine, topology)
} else if cfg.ControlAPI == "v2" {
cp = NewBfrtSpineCP_V2(client, spine, topology)
} else {
logrus.Fatal("[Spine] Control API is invalid!")
}
rpcEndPoint := horus_net.NewSpineRpcEndpoint(spine.Address, cfg.RemoteSrvServer,
topology, vcm, failedLeaves, failedServers, newLeaves, newServers, newVCs)
ch := NewSpineBusChan(activeNode, failedLeaves, failedServers, newLeaves, newServers, newVCs,
asicIngress, asicEgress)
bus := NewSpineBus(ctrlID, ch, topology, vcm, nil, cp)
return &spineController{
rpcEndPoint: rpcEndPoint,
healthMgr: nil,
bus: bus,
cp: cp,
controller: &controller{
ID: ctrlID,
topology: topology,
vcm: vcm,
cfg: cfg,
asicIngress: asicIngress,
asicEgress: asicEgress,
},
}
}
// Common controller init. logic goes here
func (c *controller) Start() {
}
func (c *leafController) FetchTopology() error {
go c.rpcEndPoint.StartClient()
logrus.Debugf("[Leaf-%d] Fetching the current topology from %s", c.ID, c.rpcEndPoint.SrvCentralAddr)
time.Sleep(time.Second)
topoInfo, err := c.rpcEndPoint.GetTopology()
if topoInfo == nil {
logrus.Errorf("[Leaf-%d] No topology was fetched from %s", c.ID, c.rpcEndPoint.SrvCentralAddr)
}
if err != nil {
logrus.Errorf(err.Error())
}
topo := model.NewDCNFromTopoInfo(topoInfo)
c.topology = topo
leaf, _ := c.topology.Leaves.Load(c.ID)
if leaf == nil {
logrus.Fatalf("[Leaf-%d] Leaf %d doesn't exist in the topology", c.ID, c.ID)
return fmt.Errorf("leaf %d doesn't exist in the topology", c.ID)
}
// c.topology.Debug()
c.vcm = core.NewVCManager(c.topology)
c.rpcEndPoint.SetVCManager(c.vcm)
c.rpcEndPoint.SetLocalAddress(leaf.Address)
c.rpcEndPoint.SetTopology(c.topology)
c.rpcEndPoint.StartServer()
return nil
}
func (c *leafController) FetchVCs() error {
logrus.Debugf("[Leaf-%d] Fetching all VCs from %s", c.ID, c.rpcEndPoint.SrvCentralAddr)
time.Sleep(time.Second)
vcs, err := c.rpcEndPoint.GetVCs()
if len(vcs) == 0 {
logrus.Warnf("[Leaf-%d] No VCs were fetched from %s", c.ID, c.rpcEndPoint.SrvCentralAddr)
} else {
logrus.Debugf("[Leaf-%d] %d VCs were fetched", c.ID, len(vcs))
}
if err != nil {
logrus.Error(err)
return err
}
for _, vcConf := range vcs {
vc, err := model.NewVC(vcConf, c.topology)
if err != nil {
logrus.Error(err)
} else {
c.vcm.AddVC(vc)
}
}
return nil
}
func (c *leafController) CreateHealthManager() error {
healthMgr, err := core.NewLeafHealthManager(c.ID, c.bus.hmMsg, c.topology, c.vcm, c.cfg.Timeout)
if err != nil {
return err
}
c.healthMgr = healthMgr
return nil
}
func (c *leafController) StartBare(fetchTopo bool) {
logrus.
WithFields(logrus.Fields{"ID": c.ID}).
Infof("[Leaf] Starting leaf switch controller")
c.controller.Start()
if fetchTopo {
err := c.FetchTopology()
if err != nil {
logrus.Fatal(err)
}
}
err := c.FetchVCs()
if err != nil {
logrus.Error(err)
}
leaf := c.topology.GetNode(c.ID, model.NodeType_Leaf)
target := bfrtC.NewTarget(bfrtC.WithDeviceId(c.cfg.DeviceID), bfrtC.WithPipeId(c.pipeID))
client := bfrtC.NewClient(c.cfg.BfrtAddress, c.cfg.P4Name, uint32(c.ID), target)
if c.cfg.ControlAPI == "fake" {
c.cp = NewFakeLeafCP(leaf, c.cfg.Spines, c.topology)
} else if c.cfg.ControlAPI == "v1" {
c.cp = NewBfrtLeafCP_V1(client, leaf, c.cfg.Spines, c.topology)
} else if c.cfg.ControlAPI == "v2" {
c.cp = NewBfrtLeafCP_V2(client, leaf, c.cfg.Spines, c.topology)
} else {
logrus.Fatal("[Leaf] Control API is invalid!")
}
c.bus = NewBareLeafBus(c.ID, c.ch, c.cp)
err = c.CreateHealthManager()
if err != nil {
logrus.Fatal(err)
}
c.bus.SetHealthManager(c.healthMgr)
c.bus.SetTopology(c.topology)
c.bus.SetVCManager(c.vcm)
// Init Table/register entries for each leaf
c.cp.Init()
go c.healthMgr.Start()
go c.bus.Start()
}
func (c *leafController) Shutdown() {
logrus.
WithFields(logrus.Fields{"ID": c.ID}).
Infof("[Leaf] Shutting down leaf switch controller")
c.bus.DoneChan <- true
c.healthMgr.DoneChan <- true
c.rpcEndPoint.DoneChan <- true
// Calls the cleanup function for the emulated leaf and sends idleRemove pkt to the spine
c.bus.Shutdown()
close(c.asicEgress)
close(c.asicIngress)
close(c.bus.hmMsg)
}
func (c *spineController) Start() {
logrus.
WithFields(logrus.Fields{"ID": c.ID}).
Infof("[Spine] Starting spine switch controller")
c.controller.Start()
go c.rpcEndPoint.Start()
logrus.Debugf("[Spine] Fetching all VCs from %s", c.rpcEndPoint.SrvCentralAddr)
time.Sleep(time.Second)
vcs, err := c.rpcEndPoint.GetVCs()
if len(vcs) == 0 {
logrus.Warnf("[Spine] No VCs were fetched from %s", c.rpcEndPoint.SrvCentralAddr)
} else {
logrus.Debugf("[Spine] %d VCs were fetched", len(vcs))
}
if err != nil {
logrus.Error(err)
} else {
for _, vcConf := range vcs {
vc, err := model.NewVC(vcConf, c.topology)
if err != nil {
logrus.Error(err)
} else {
c.vcm.AddVC(vc)
}
}
}
go c.cp.Init()
go c.bus.Start()
}