-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
221 lines (187 loc) · 4.37 KB
/
server.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
package nvme
import (
"fmt"
"log"
"net"
"sync"
"github.com/thirdmartini/go-nvme/internal/sys"
"github.com/thirdmartini/go-nvme/pkg/tracer"
"github.com/thirdmartini/go-nvme/protocol"
)
var serialAccess sync.Mutex
type SessionInfo struct {
Source string
Ctrl *Controller
}
type Server struct {
Address string
SubSystems map[string]Subsystem
SessionInfo map[string]SessionInfo
Lock sync.Mutex
listen net.Listener
wg sync.WaitGroup
quit chan bool
debugLevel uint64
}
func (s *Server) RegisterSession(name string, ctrl *Controller) {
s.Lock.Lock()
defer s.Lock.Unlock()
s.SessionInfo[name] = SessionInfo{
Source: name,
Ctrl: ctrl,
}
}
func (s *Server) UnRegisterSession(name string) {
s.Lock.Lock()
defer s.Lock.Unlock()
delete(s.SessionInfo, name)
}
func (s *Server) GetSessions() []SessionInfo {
s.Lock.Lock()
defer s.Lock.Unlock()
sil := make([]SessionInfo, 0, len(s.SessionInfo))
for k := range s.SessionInfo {
sil = append(sil, s.SessionInfo[k])
}
return sil
}
func (s *Server) AddSubSystem(subsys Subsystem) {
s.Lock.Lock()
defer s.Lock.Unlock()
s.SubSystems[subsys.GetNQN()] = subsys
}
func (s *Server) GetSubSystem(nqn string) Subsystem {
s.Lock.Lock()
defer s.Lock.Unlock()
subsys, ok := s.SubSystems[nqn]
if !ok {
return nil
}
return subsys
}
func (s *Server) ListSubSystems() []Subsystem {
s.Lock.Lock()
defer s.Lock.Unlock()
ls := make([]Subsystem, 0, len(s.SubSystems))
for k := range s.SubSystems {
ls = append(ls, s.SubSystems[k])
}
return ls
}
func (s *Server) RemoveSubSystem(nqn string) error {
s.Lock.Lock()
defer s.Lock.Unlock()
subsys, ok := s.SubSystems[nqn]
if !ok {
return fmt.Errorf("nqn:%s does not exists", nqn)
}
delete(s.SubSystems, subsys.GetNQN())
for id := range s.SessionInfo {
session := s.SessionInfo[id]
if session.Ctrl.Subsystem != subsys {
continue
}
session.Ctrl.Close()
}
return nil
}
func (s *Server) startController(conn *sys.Conn) {
sessionId := conn.RemoteAddr().String()
ctrl := Controller{
REGCtrlCaps: uint64(1)<<37 | uint64(15)<<24 | (uint64(protocol.NVMECtrlAttrMaxQueueSize) - 1),
// REGCtrlStatus: 0x1, // Controller is ready
REGCtrlStatus: 0x0,
Version: 1<<16 | 3<<8 | 0, // 1.3.0
ControllerID: 0, // was 1
Subsystem: &InitSubsystem{},
QueueID: 0,
QueueSize: protocol.NVMECtrlAttrMaxQueueSize, // default queue size
Queue: make([]NVMERequest, protocol.NVMECtrlAttrMaxQueueSize, protocol.NVMECtrlAttrMaxQueueSize),
SQCUR: 0,
SQHD: 0,
Server: s,
SessionID: sessionId,
FlowControlDisabled: false,
waiting: make(chan *NVMERequest, protocol.NVMECtrlAttrMaxQueueSize),
completions: make(chan *NVMERequest, protocol.NVMECtrlAttrMaxQueueSize),
}
for i, _ := range ctrl.Queue {
req := &ctrl.Queue[i]
ctrl.waiting <- req
}
if s.debugLevel == 0 {
ctrl.Log = &tracer.NullTracer{}
} else {
ctrl.Log = &tracer.LogTracer{
Id: sessionId,
Level: s.debugLevel,
}
}
ctrl.Log.Begin(tracer.TraceController, "Session Started")
s.RegisterSession(sessionId, &ctrl)
defer func() {
s.UnRegisterSession(sessionId)
ctrl.Log.Trace(tracer.TraceController, "Session Terminated")
conn.Close()
}()
if true {
ctrl.Serve(conn)
return
} else {
panic(false)
//ctrl.ServeDeprecated(conn)
}
}
func (s *Server) Serve() error {
defer s.wg.Done()
for {
conn, err := s.listen.Accept()
if err != nil {
select {
case <-s.quit:
return nil
default:
log.Println("accept error", err)
}
} else {
sconn := sys.NewConn(conn)
fmt.Printf("Server Starded Queue\n")
s.wg.Add(1)
go func() {
s.startController(sconn)
s.wg.Done()
}()
}
}
}
func (s *Server) Close() error {
close(s.quit)
s.listen.Close()
fmt.Printf("Listen Closed\n")
s.wg.Wait()
fmt.Printf("Server Closed\n")
return nil
}
func (s *Server) SetDebugLevel(level uint64) {
s.debugLevel = level
}
func New(addr string) (*Server, error) {
listen, err := net.Listen("tcp4", addr)
if err != nil {
return nil, err
}
s := &Server{
SubSystems: make(map[string]Subsystem),
SessionInfo: make(map[string]SessionInfo),
Address: addr,
listen: listen,
quit: make(chan bool),
}
s.wg.Add(1)
// We always have a discovery subsystem
discovery := &DiscoverySubsystem{
Server: s,
}
s.AddSubSystem(discovery)
return s, nil
}