-
Notifications
You must be signed in to change notification settings - Fork 2
/
switchtec_mock.go
326 lines (255 loc) · 8 KB
/
switchtec_mock.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
/*
* Copyright 2020, 2021, 2022 Hewlett Packard Enterprise Development LP
* Other additional copyright holders may be indicated within.
*
* The entirety of this work is 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.
*/
package fabric
import (
"fmt"
"math/rand"
"os"
"strconv"
"time"
"github.com/NearNodeFlash/nnf-ec/internal/switchtec/pkg/switchtec"
sf "github.com/NearNodeFlash/nnf-ec/pkg/rfsf/pkg/models"
)
type MockSwitchtecController struct {
devices []MockSwitchtecDevice
globalPdfid int
}
type MockSwitchtecDevice struct {
ctrl *MockSwitchtecController
createdAt time.Time
id int
path string
open bool
exists bool
config *SwitchConfig
ports []MockSwitchtecPort
}
type MockSwitchtecPort struct {
id int
functions []switchtec.DumpEpPortAttachedDeviceFunction
pdfid uint16
bindings []*switchtec.DumpEpPortAttachedDeviceFunction
config *PortConfig
}
func NewMockSwitchtecController() SwitchtecControllerInterface {
config, err := loadConfig()
if err != nil {
return nil
}
ctrl := &MockSwitchtecController{
globalPdfid: rand.Intn(0xFF) << 8,
}
ctrl.devices = make([]MockSwitchtecDevice, len(config.Switches))
for switchIdx, switchConfig := range config.Switches {
devId, err := strconv.Atoi(switchConfig.Id)
if err != nil {
return nil
}
ctrl.devices[switchIdx] = MockSwitchtecDevice{
ctrl: ctrl,
id: devId,
path: "", // Path becomes valid when opened
config: &config.Switches[switchIdx],
open: false,
exists: true,
createdAt: time.Now(),
}
dev := &ctrl.devices[switchIdx]
dev.ports = make([]MockSwitchtecPort, len(switchConfig.Ports))
for portIdx, portConfig := range switchConfig.Ports {
dev.ports[portIdx] = MockSwitchtecPort{
id: portIdx,
pdfid: uint16(ctrl.allocateNewPDFID()),
config: &switchConfig.Ports[portIdx],
}
port := &dev.ports[portIdx]
switch portConfig.getPortType() {
case sf.INTERSWITCH_PORT_PV130PT:
continue
case sf.MANAGEMENT_PORT_PV130PT:
port.bindings = make([]*switchtec.DumpEpPortAttachedDeviceFunction, switchConfig.DownstreamPortCount)
case sf.UPSTREAM_PORT_PV130PT:
port.bindings = make([]*switchtec.DumpEpPortAttachedDeviceFunction, config.DownstreamPortCount)
case sf.DOWNSTREAM_PORT_PV130PT:
{
port.functions = make([]switchtec.DumpEpPortAttachedDeviceFunction, 1 /* PF */ +1 /*MGMT*/ +config.UpstreamPortCount)
isPF := func(idx int) uint8 {
if idx == 0 {
return 1
}
return 0
}
for idx := range port.functions {
f := switchtec.DumpEpPortAttachedDeviceFunction{
FunctionID: uint16(idx),
PDFID: port.pdfid + uint16(idx),
SRIOVCapPF: isPF(idx),
VFNum: uint8(idx - 1),
Bound: 0,
BoundPAXID: 0,
BoundHVDPhyPID: 0,
BoundHVDLogPID: 0,
}
port.functions[idx] = f
}
}
default:
panic("Unhandled port type")
}
}
}
return ctrl
}
func (c *MockSwitchtecController) Open(path string) (SwitchtecDeviceInterface, error) {
for deviceIdx := range c.devices {
device := &c.devices[deviceIdx]
if device.path == "" || device.path == path {
device.path = path
if !device.exists {
return nil, os.ErrNotExist
}
return device, nil
}
}
return nil, fmt.Errorf("Device %s not found", path)
}
func (c *MockSwitchtecController) SetSwitchNotExists(switchIdx int) {
c.devices[switchIdx].exists = false
}
func (c *MockSwitchtecController) allocateNewPDFID() int {
pdfid := c.globalPdfid
c.globalPdfid += 0x100
return pdfid
}
func (*MockSwitchtecDevice) Device() *switchtec.Device {
panic("Mock Switchtec Device does not support Device getter")
}
func (*MockSwitchtecDevice) Path() *string {
panic("Mock Switchtec Device does not support Path getter")
}
func (d *MockSwitchtecDevice) Close() {
d.path = ""
d.id = -1
}
func (d *MockSwitchtecDevice) Identify() (int32, error) {
return int32(d.id), nil
}
func (d *MockSwitchtecDevice) GetFirmwareVersion() (string, error) {
return "MockFirmware", nil
}
func (d *MockSwitchtecDevice) GetModel() (string, error) {
return "MockModel", nil
}
func (d *MockSwitchtecDevice) GetManufacturer() (string, error) {
return "MockMfg", nil
}
func (d *MockSwitchtecDevice) GetSerialNumber() (string, error) {
return "MockSerialNumber", nil
}
var firstUpstreamPortDisabled bool // For test, record the first upstream port as being down.
var firstDownstreamPortDisabled bool // For test, record the first downstream port as being down.
func (d *MockSwitchtecDevice) GetPortStatus() ([]switchtec.PortLinkStat, error) {
stats := make([]switchtec.PortLinkStat, len(d.ports))
for idx := range stats {
p := d.ports[idx]
stats[idx] = switchtec.PortLinkStat{
PhysPortId: uint8(p.config.Port),
CfgLinkWidth: uint8(p.config.Width),
NegLinkWidth: uint8(p.config.Width),
LinkUp: true,
LinkGen: 4,
LinkState: switchtec.PortLinkState_L0,
CurLinkRateGBps: switchtec.GetDataRateGBps(4) * float64(p.config.Width),
}
}
return stats, nil
}
func (d *MockSwitchtecDevice) GetPortMetrics() (PortMetrics, error) {
counters := make(PortMetrics, len(d.ports))
for idx, p := range d.ports {
fakeTxRxBytes := rand.Uint64()
counters[idx] = PortMetric{
PhysPortId: uint8(p.config.Port),
BandwidthCounter: switchtec.BandwidthCounter{
TimeInMicroseconds: uint64(time.Now().Sub(d.createdAt).Microseconds()),
Egress: switchtec.BandwidthCounterDirectional{
Posted: fakeTxRxBytes, Completion: 0, NonPosted: 0,
},
Ingress: switchtec.BandwidthCounterDirectional{
Posted: fakeTxRxBytes, Completion: 0, NonPosted: 0,
},
},
}
}
return counters, nil
}
func (d *MockSwitchtecDevice) GetEvents() ([]switchtec.GfmsEvent, error) {
return make([]switchtec.GfmsEvent, 0), nil
}
func (d *MockSwitchtecDevice) EnumerateEndpoint(physPortId uint8, handlerFunc func(epPort *switchtec.DumpEpPortDevice) error) error {
for _, port := range d.ports {
if uint8(port.config.Port) == physPortId {
epPort := switchtec.DumpEpPortDevice{
Ep: switchtec.DumpEpPortEp{
Functions: port.functions,
},
}
return handlerFunc(&epPort)
}
}
return nil
}
func (d *MockSwitchtecDevice) ResetEndpoint(pdfid uint16) error {
return nil
}
func (d *MockSwitchtecDevice) Bind(hostPhysPortId, hostLogPortId uint8, pdfid uint16) error {
bindPort := func(hostPort *MockSwitchtecPort, hostLogPortId uint8, pdfid uint16) error {
for deviceIdx := range d.ctrl.devices {
for portIdx := range d.ctrl.devices[deviceIdx].ports {
port := &d.ctrl.devices[deviceIdx].ports[portIdx]
if port.pdfid == pdfid&0xFF00 {
for functionIdx := range port.functions {
function := &port.functions[functionIdx]
if function.PDFID == pdfid {
if function.Bound != 0 {
return fmt.Errorf("Device %#04x already bound", pdfid)
}
function.Bound = 1
function.BoundHVDPhyPID = hostPhysPortId
function.BoundHVDLogPID = hostLogPortId
function.BoundPAXID = uint8(d.id)
hostPort.bindings[hostLogPortId] = function
return nil
}
}
}
}
}
return fmt.Errorf("PDFID %#04x Not Found", pdfid)
}
for portIdx, port := range d.ports {
if port.config.Port == int(hostPhysPortId) {
if port.bindings[hostLogPortId] != nil {
return fmt.Errorf("host Port %d Logical Port ID %d already bound", hostPhysPortId, hostLogPortId)
}
return bindPort(&d.ports[portIdx], hostLogPortId, pdfid)
}
}
return fmt.Errorf("host Physical Port ID %d not found", hostPhysPortId)
}