-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontroller.go
298 lines (250 loc) · 6.61 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
package nvme
import (
"fmt"
"io"
"net"
"sync"
"time"
"github.com/thirdmartini/go-nvme/internal/buffers"
"github.com/thirdmartini/go-nvme/pkg/tracer"
"github.com/thirdmartini/go-nvme/protocol"
"github.com/thirdmartini/go-nvme/stream"
"github.com/thirdmartini/go-nvme/targets"
)
var controllersAvailable = uint64(0)
type Controller struct {
SessionID string
// Controller registers
REGCtrlCaps uint64
REGCtrlConfig uint32
// Bit.5 Processing Paused
// Bit.4 Reset Occurred
// Bit.2-3 Shutdown Status
// Bit.1 Controller Fatal Status
// Bit.0 Ready
REGCtrlStatus uint32
Version uint32
ConnectedHostNQN string
ConnectedSubNQN string
ControllerID uint16
AEC uint32
NCQR uint16
NSQR uint16
// FlowControlDisabled manages whether the controller will perform flow control
// through use of SQHD/SQCUR. This is set in the FabricConnect command
// Preference: we would prefer this is always disabled
FlowControlDisabled bool
SQHD uint16 // head of queue (next available slot)
SQCUR uint16 // current slot being processed
QueueID uint16
QueueSize uint16
Queue []NVMERequest
Server *Server
Subsystem Subsystem
RequestTime time.Duration
RequestCount uint64
conn net.Conn
in *stream.Reader
out *stream.Writer
Log tracer.Tracer
bufferManager *buffers.Buffers
// queue for handling bottom half
waiting chan *NVMERequest
completions chan *NVMERequest
// close needs to wait for all sun routines to exit
wg sync.WaitGroup
}
func (c *Controller) ProcessResponse(w *NVMEResponse) (error, bool) {
// TODO: we really need to deal with this in the SGL style
sgl := w.sgl
offset := uint32(0)
for idx := 0; idx < w.sglc; idx++ {
flags := uint8(0)
if idx+1 == len(sgl) {
flags = 0x1 << 2 // last transfer
}
data := sgl[idx].Data
dataLen := uint32(len(data))
w.C2H.CCCID = w.CID
w.C2H.DATAO = offset
w.C2H.DATAL = dataLen
offset += dataLen
c.Log.TraceProtocol(tracer.TraceData, &w.C2H)
c.out.MarshalWithData2(protocol.C2HData, flags, &w.C2H, 16, data)
err := c.out.Flush()
if err != nil {
tracer.Fatal(err.Error())
}
}
if c.FlowControlDisabled {
w.Response.SQHD = 0xFFFF
}
c.Log.TraceProtocol(tracer.TraceCapsule, &w.Response)
if w.NoReply {
return nil, true
}
c.out.Send(protocol.CapsuleResp, &w.Response, 16)
return c.out.Flush(), true
}
func (c *Controller) Serve(conn net.Conn) error {
c.conn = conn
c.in = stream.NewReader(conn)
c.out = stream.NewWriter(conn)
quit := make(chan bool, 10)
c.wg.Add(2)
go func() {
err := c.CompletionHandler()
fmt.Printf("Completion Handler Exiting %v\n", err)
conn.Close()
c.wg.Done()
}()
defer func() {
// need to wait for all outstanding commands to drain
fmt.Printf("Signal Controler(%d:%s/%s).Serve exit\n", c.ControllerID, c.Subsystem.GetNQN(), c.SessionID)
fmt.Printf("Controler(%d).WaitDrain(%d/%d)\n", c.ControllerID, len(c.waiting), cap(c.waiting))
count := 0
for _ = range c.waiting {
count++
//fmt.Printf("Controler(%d).Draining %d/%d\n", c.ControllerID, count, cap(c.waiting))
if count == cap(c.waiting) {
break
}
}
fmt.Printf("Controler(%d).Serve request queue drained\n", c.ControllerID)
close(c.completions)
c.wg.Done()
}()
c.bufferManager = buffers.New(128, protocol.MaxH2CPDUSize*8)
for {
hdr, err := c.in.Dequeue()
if err != nil {
if err != io.EOF {
c.Log.Trace(0x01, "Session Error: %s", err.Error())
}
break
}
stime := time.Now()
c.Log.TraceProtocol(tracer.TraceCommands, hdr)
switch hdr.Type {
case protocol.ICReq:
req := protocol.ICRequest{}
err = c.in.Receive(&req)
if err != nil {
c.Log.Trace(tracer.TraceCommands, "Session Error: %s", err.Error())
return err
}
c.Log.TraceProtocol(tracer.TraceCommands, &req)
rsp := protocol.ICResponse{
PDUDataDigest: 0, // no digests for now
MaxH2CDataLength: 0x8000, // 32K
}
err = c.out.Send(protocol.ICResp, &rsp, 120)
if err != nil {
c.Log.Trace(tracer.TraceCommands, "Session Error: %s", err.Error())
return err
}
c.Log.TraceProtocol(tracer.TraceCommands, &rsp)
err = c.out.Flush()
if err != nil {
c.Log.Trace(tracer.TraceCommands, "Session Error: %s", err.Error())
return err
}
case protocol.CapsuleCmd:
err = c.HandleCapsule(conn, quit)
if err != nil {
c.Log.Trace(tracer.TraceCommands, "Session Error: %s", err.Error())
return err
}
case protocol.H2CTermReq:
term := protocol.C2HTermRequest{}
err = c.in.Receive(&term)
if err != nil {
return err
}
return fmt.Errorf("controller terminated connection: %x:%x", term.FatalErrorStatus, term.FatalErrorInformation)
default:
c.Log.Todo("Unknown h.Type: 0x%x", hdr.Type)
break
}
c.RequestTime += time.Now().Sub(stime)
c.RequestCount++
c.Log.End()
}
return nil
}
func (c *Controller) HandleCapsule(conn net.Conn, quit chan bool) error {
var req *NVMERequest
select {
case <-quit:
return nil
case req = <-c.waiting:
}
c.SQHD++
if c.SQHD > c.QueueSize {
c.SQHD = 0
}
req.completion = c.completions
err := c.in.Receive(&req.capsule)
if err != nil {
c.Log.Trace(0x01, "Session Error: %s", err.Error())
return err
}
dataLen := c.in.Length()
if dataLen != 0 {
req.payload = c.bufferManager.Get()
req.payloadLength = int(dataLen)
c.in.ReceiveData(req.payload[0:dataLen])
}
// we can do all this in dequeue
w := &req.response
w.Response.CID = req.capsule.CID
w.Response.SQHD = c.SQHD
w.SetStatus(protocol.SCSuccess)
w.State = 0
capsule := req.Capsule()
w.Type = protocol.CapsuleResp
w.CID = capsule.CID
// QueueID:0 is reserved for admin commands
if c.QueueID == 0 {
c.Log.TraceCapsule(true, capsule)
err = c.handleAdminCapsule(w, req)
req.Complete(targets.TargetErrorNone)
} else {
c.Log.TraceCapsule(false, capsule)
err = c.handleIOCapsule(w, req)
}
return err
}
// CompletionHandler sends the request responses back to the initiator
func (c *Controller) CompletionHandler() error {
var req *NVMERequest
for req = range c.completions {
err, done := c.ProcessResponse(&req.response)
if err != nil {
fmt.Printf("CompletionError: %s\n", err.Error())
if req.payload != nil {
c.bufferManager.Put(req.payload)
req.payload = nil
}
req.active = false
req.response.Reset()
c.waiting <- req
continue
}
if done {
if req.payload != nil {
c.bufferManager.Put(req.payload)
req.payload = nil
}
req.active = false
req.response.Reset()
c.waiting <- req
} // else we are waiting for an R2T to requeue this command
}
return nil
}
func (c *Controller) Close() error {
c.conn.Close()
c.wg.Wait()
return nil
}