forked from daos-stack/daos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nvme.go
340 lines (278 loc) · 9.04 KB
/
nvme.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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
//
// (C) Copyright 2018-2020 Intel Corporation.
//
// 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.
//
// GOVERNMENT LICENSE RIGHTS-OPEN SOURCE SOFTWARE
// The Government's rights to use, modify, reproduce, release, perform, display,
// or disclose this software are subject to the terms of the Apache License as
// provided in Contract No. 8F-30005.
// Any reproduction of computer software, computer software documentation, or
// portions thereof marked with this legend must also reproduce the markings.
//
package spdk
// CGO_CFLAGS & CGO_LDFLAGS env vars can be used
// to specify additional dirs.
/*
#cgo CFLAGS: -I .
#cgo LDFLAGS: -L . -lnvme_control -lspdk
#include "stdlib.h"
#include "spdk/stdinc.h"
#include "spdk/nvme.h"
#include "spdk/env.h"
#include "include/nvme_control.h"
#include "include/nvme_control_common.h"
*/
import "C"
import (
"os"
"unsafe"
"github.com/pkg/errors"
"github.com/daos-stack/daos/src/control/logging"
)
const lockfilePathPrefix = "/tmp/spdk_pci_lock_"
// NVME is the interface that provides SPDK NVMe functionality.
type NVME interface {
// Discover NVMe controllers and namespaces, and device health info
Discover(logging.Logger) ([]Controller, error)
// Format NVMe controller namespaces
Format(logging.Logger, string) error
// Cleanup NVMe object references
Cleanup()
// CleanLockfiles removes SPDK lockfiles for specific PCI addresses
CleanLockfiles(logging.Logger, ...string)
}
// Nvme is an NVME interface implementation.
type Nvme struct{}
// Controller struct mirrors C.struct_ctrlr_t and
// describes a NVMe controller.
//
// TODO: populate implicitly using inner member:
// +inner C.struct_ctrlr_t
type Controller struct {
Model string
Serial string
PCIAddr string
FWRev string
SocketID int32
Namespaces []*Namespace
HealthStats *DeviceHealth
}
// Namespace struct mirrors C.struct_ns_t and
// describes a NVMe Namespace tied to a controller.
//
// TODO: populate implicitly using inner member:
// +inner C.struct_ns_t
type Namespace struct {
ID uint32
Size uint64
}
// DeviceHealth struct mirrors C.struct_dev_health_t
// and describes the raw SPDK device health stats
// of a controller (NVMe SSD).
type DeviceHealth struct {
Temp uint32
TempWarnTime uint32
TempCritTime uint32
CtrlBusyTime uint64
PowerCycles uint64
PowerOnHours uint64
UnsafeShutdowns uint64
MediaErrors uint64
ErrorLogEntries uint64
TempWarn bool
AvailSpareWarn bool
ReliabilityWarn bool
ReadOnlyWarn bool
VolatileWarn bool
}
type remFunc func(name string) error
func realRemove(name string) error {
return os.Remove(name)
}
func cleanLockfiles(log logging.Logger, remove remFunc, pciAddrs ...string) error {
removed := make([]string, 0, len(pciAddrs))
for _, pciAddr := range pciAddrs {
fName := lockfilePathPrefix + pciAddr
if err := remove(fName); err != nil {
if os.IsNotExist(err) {
continue
}
return errors.Wrapf(err, "remove %s", fName)
}
removed = append(removed, fName)
}
log.Debugf("removed lockfiles: %v", removed)
return nil
}
// wrapCleanError encapsulates inErr inside any cleanErr.
func wrapCleanError(inErr error, cleanErr error) (outErr error) {
outErr = inErr
if cleanErr != nil {
outErr = errors.Wrap(inErr, cleanErr.Error())
if outErr == nil {
outErr = cleanErr
}
}
return
}
// CleanLockfiles removes SPDK lockfiles after binding operations.
func (n *Nvme) CleanLockfiles(log logging.Logger, pciAddrs ...string) error {
return cleanLockfiles(log, realRemove, pciAddrs...)
}
func pciAddressList(ctrlrs []Controller) []string {
pciAddrs := make([]string, 0, len(ctrlrs))
for _, c := range ctrlrs {
pciAddrs = append(pciAddrs, c.PCIAddr)
}
return pciAddrs
}
// Discover NVMe devices accessible by SPDK on a given host.
//
// Calls C.nvme_discover which returns pointers to single linked list of
// ctrlr_t structs. These are converted and returned as Controller slices
// containing any Namespace and DeviceHealth structs. Afterwards remove
// lockfile for each discovered device.
func (n *Nvme) Discover(log logging.Logger) ([]Controller, error) {
ctrlrs, err := processReturn(C.nvme_discover(), "NVMe Discover(): C.nvme_discover")
pciAddrs := pciAddressList(ctrlrs)
log.Debugf("discovered nvme ssds: %v", pciAddrs)
return ctrlrs, wrapCleanError(err, n.CleanLockfiles(log, pciAddrs...))
}
// Format device at given pci address, destructive operation!
//
// Attempt wipe of namespace #1 LBA-0 and falls back to full controller
// format if quick format failed. Afterwards remove lockfile for formatted
// device.
func (n *Nvme) Format(log logging.Logger, ctrlrPciAddr string) (err error) {
defer func() {
err = wrapCleanError(err, n.CleanLockfiles(log, ctrlrPciAddr))
}()
csPci := C.CString(ctrlrPciAddr)
defer C.free(unsafe.Pointer(csPci))
failMsg := "NVMe Format(): C.nvme_"
wipeMsg := failMsg + "wipe_first_ns()"
if _, err = processReturn(C.nvme_wipe_first_ns(csPci), wipeMsg); err == nil {
return // quick format succeeded
}
log.Debugf("%s: %s", wipeMsg, err.Error())
log.Infof("falling back to full format on %s\n", ctrlrPciAddr)
_, err = processReturn(C.nvme_format(csPci), failMsg+"format()")
return
}
// Update calls C.nvme_fwupdate to update controller firmware image.
//
// Retrieves image from path and updates given firmware slot/register
// then remove lockfile for updated device.
func (n *Nvme) Update(log logging.Logger, ctrlrPciAddr string, path string, slot int32) (ctrlrs []Controller, err error) {
csPath := C.CString(path)
defer C.free(unsafe.Pointer(csPath))
csPci := C.CString(ctrlrPciAddr)
defer C.free(unsafe.Pointer(csPci))
ctrlrs, err = processReturn(C.nvme_fwupdate(csPci, csPath, C.uint(slot)),
"NVMe Update(): C.nvme_fwupdate")
err = wrapCleanError(err, n.CleanLockfiles(log, ctrlrPciAddr))
return
}
// Cleanup unlinks and detaches any controllers or namespaces,
// as well as cleans up optional device health information.
func (n *Nvme) Cleanup() {
C.nvme_cleanup()
}
// c2GoController is a private translation function
func c2GoController(ctrlr *C.struct_ctrlr_t) Controller {
return Controller{
Model: C.GoString(&ctrlr.model[0]),
Serial: C.GoString(&ctrlr.serial[0]),
PCIAddr: C.GoString(&ctrlr.pci_addr[0]),
FWRev: C.GoString(&ctrlr.fw_rev[0]),
SocketID: int32(ctrlr.socket_id),
}
}
// c2GoDeviceHealth is a private translation function
func c2GoDeviceHealth(health *C.struct_dev_health_t) *DeviceHealth {
return &DeviceHealth{
Temp: uint32(health.temperature),
TempWarnTime: uint32(health.warn_temp_time),
TempCritTime: uint32(health.crit_temp_time),
CtrlBusyTime: uint64(health.ctrl_busy_time),
PowerCycles: uint64(health.power_cycles),
PowerOnHours: uint64(health.power_on_hours),
UnsafeShutdowns: uint64(health.unsafe_shutdowns),
MediaErrors: uint64(health.media_errors),
ErrorLogEntries: uint64(health.error_log_entries),
TempWarn: bool(health.temp_warning),
AvailSpareWarn: bool(health.avail_spare_warning),
ReliabilityWarn: bool(health.dev_reliabilty_warning),
ReadOnlyWarn: bool(health.read_only_warning),
VolatileWarn: bool(health.volatile_mem_warning),
}
}
// c2GoNamespace is a private translation function
func c2GoNamespace(ns *C.struct_ns_t) *Namespace {
return &Namespace{
ID: uint32(ns.id),
Size: uint64(ns.size),
}
}
// processReturn parses return structs
func processReturn(retPtr *C.struct_ret_t, failMsg string) (ctrlrs []Controller, err error) {
if retPtr == nil {
return nil, errors.Wrap(FaultBindingRetNull, failMsg)
}
defer freeReturn(retPtr)
ctrlrPtr := retPtr.ctrlrs
if retPtr.rc != 0 {
err = errors.Wrap(FaultBindingFailed(int(retPtr.rc), C.GoString(&retPtr.err[0])),
failMsg)
return
}
for ctrlrPtr != nil {
ctrlr := c2GoController(ctrlrPtr)
if nsPtr := ctrlrPtr.nss; nsPtr != nil {
for nsPtr != nil {
ctrlr.Namespaces = append(ctrlr.Namespaces, c2GoNamespace(nsPtr))
nsPtr = nsPtr.next
}
}
healthPtr := ctrlrPtr.dev_health
if healthPtr == nil {
err = FaultCtrlrNoHealth
return
}
ctrlr.HealthStats = c2GoDeviceHealth(healthPtr)
ctrlrs = append(ctrlrs, ctrlr)
ctrlrPtr = ctrlrPtr.next
}
return
}
// freeReturn frees memory that was allocated in C
func freeReturn(retPtr *C.struct_ret_t) {
ctrlr := retPtr.ctrlrs
for ctrlr != nil {
ctrlrNext := ctrlr.next
ns := ctrlr.nss
for ns != nil {
nsNext := ns.next
C.free(unsafe.Pointer(ns))
ns = nsNext
}
if ctrlr.dev_health != nil {
C.free(unsafe.Pointer(ctrlr.dev_health))
}
C.free(unsafe.Pointer(ctrlr))
ctrlr = ctrlrNext
}
C.free(unsafe.Pointer(retPtr))
retPtr = nil
}