-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathnvme.go
712 lines (636 loc) · 21.1 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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
/*
*
* Copyright © 2022 Dell Inc. or its subsidiaries. All Rights Reserved.
*
* 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 gobrick
import (
"context"
"errors"
"fmt"
"path"
"strings"
"sync"
"time"
"github.com/dell/gobrick/internal/logger"
intmultipath "github.com/dell/gobrick/internal/multipath"
intpowerpath "github.com/dell/gobrick/internal/powerpath"
intscsi "github.com/dell/gobrick/internal/scsi"
"github.com/dell/gobrick/internal/tracer"
wrp "github.com/dell/gobrick/internal/wrappers"
"github.com/dell/gobrick/pkg/multipath"
"github.com/dell/gobrick/pkg/powerpath"
"github.com/dell/gobrick/pkg/scsi"
"github.com/dell/gonvme"
log "github.com/sirupsen/logrus"
"golang.org/x/sync/semaphore"
"golang.org/x/sync/singleflight"
)
const (
// NVMeWaitDeviceTimeoutDefault - NVMe default device time out
NVMeWaitDeviceTimeoutDefault = time.Second * 30
// NVMeWaitDeviceRegisterTimeoutDefault - NVMe default device register timeout
NVMeWaitDeviceRegisterTimeoutDefault = time.Second * 10
// NVMeMaxParallelOperationsDefault - max parallen NVMe operations
NVMeMaxParallelOperationsDefault = 5
// NVMePortDefault - NVMe TCP port
NVMePortDefault = ":4420"
// PowerMaxOUIPrefix - PowerMax format 6 OUI prefix
PowerMaxOUIPrefix = "6000097"
// PowerStoreOUIPrefix - PowerStore format 6 OUI prefix
PowerStoreOUIPrefix = "68ccf09"
)
// NVMeConnectorParams - type definition for NVMe connector params
type NVMeConnectorParams struct {
// nvmeLib command will run from this chroot
Chroot string
// timeouts
// how long to wait for nvme session to become active after login
WaitDeviceTimeout time.Duration
WaitDeviceRegisterTimeout time.Duration
FailedSessionMinimumLoginRetryInterval time.Duration
MultipathFlushTimeout time.Duration
MultipathFlushRetryTimeout time.Duration
MultipathFlushRetries int
MaxParallelOperations int
}
// DevicePathResult - placeholder for nvme devicepaths
type DevicePathResult struct {
devicePaths []string
nguid string
}
// FCHBAInfo holds information about host NVMe/FC ports
type FCHBAInfo struct {
PortName string
NodeName string
}
// NewNVMeConnector - get new NVMeConnector
func NewNVMeConnector(params NVMeConnectorParams) *NVMeConnector {
mp := multipath.NewMultipath(params.Chroot)
pp := powerpath.NewPowerpath(params.Chroot)
s := scsi.NewSCSI(params.Chroot)
conn := &NVMeConnector{
multipath: mp,
powerpath: pp,
scsi: s,
filePath: &wrp.FilepathWrapper{},
os: &wrp.OSWrapper{},
baseConnector: newBaseConnector(mp, pp, s,
baseConnectorParams{
MultipathFlushTimeout: params.MultipathFlushTimeout,
MultipathFlushRetryTimeout: params.MultipathFlushRetryTimeout,
MultipathFlushRetries: params.MultipathFlushRetries,
}),
}
nvmeOpts := make(map[string]string)
nvmeOpts["chrootDirectory"] = params.Chroot
conn.nvmeLib = gonvme.NewNVMe(nvmeOpts)
// always try to use manual session management first
conn.manualSessionManagement = true
// timeouts
setTimeouts(&conn.waitDeviceTimeout,
params.WaitDeviceTimeout, NVMeWaitDeviceTimeoutDefault)
setTimeouts(&conn.waitDeviceRegisterTimeout,
params.WaitDeviceRegisterTimeout, NVMeWaitDeviceRegisterTimeoutDefault)
conn.loginLock = newRateLock()
maxParallelOperations := params.MaxParallelOperations
if maxParallelOperations == 0 {
maxParallelOperations = NVMeMaxParallelOperationsDefault
}
conn.limiter = semaphore.NewWeighted(int64(maxParallelOperations))
conn.singleCall = &singleflight.Group{}
return conn
}
// NVMeConnector - type defenition for NVMe connector
type NVMeConnector struct {
baseConnector *baseConnector
multipath intmultipath.Multipath
powerpath intpowerpath.Powerpath
scsi intscsi.SCSI
nvmeLib wrp.NVMe
manualSessionManagement bool
// timeouts
waitDeviceTimeout time.Duration
waitDeviceRegisterTimeout time.Duration
loginLock *rateLock
limiter *semaphore.Weighted
singleCall *singleflight.Group
// wrappers
filePath wrp.LimitedFilepath
os wrp.LimitedOS
}
// NVMeTargetInfo - Placeholder for NVMe targets
type NVMeTargetInfo struct {
Portal string
Target string
}
// NVMeVolumeInfo - placeholder for NVMe volume
type NVMeVolumeInfo struct {
Targets []NVMeTargetInfo
WWN string
}
func singleCallKeyForNVMeTargets(info NVMeVolumeInfo) string {
data := make([]string, len(info.Targets))
for i, t := range info.Targets {
target := strings.Join([]string{t.Portal, t.Target}, ":")
data[i] = target
}
return strings.Join(data, ",")
}
// ConnectVolume - connect to nvme volume
func (c *NVMeConnector) ConnectVolume(ctx context.Context, info NVMeVolumeInfo, useFC bool) (Device, error) {
defer tracer.TraceFuncCall(ctx, "NVMeConnector.ConnectVolume")()
if err := c.limiter.Acquire(ctx, 1); err != nil {
return Device{}, errors.New("too many parallel operations. try later")
}
defer c.limiter.Release(1)
addDefaultNVMePortToVolumeInfoPortals(&info)
if err := c.validateNVMeVolumeInfo(ctx, info); err != nil {
return Device{}, err
}
ret, err, _ := c.singleCall.Do(
singleCallKeyForNVMeTargets(info),
func() (interface{}, error) { return c.checkNVMeSessions(ctx, info) })
if err != nil {
return Device{}, err
}
sessions := ret.([]gonvme.NVMESession)
ret, _, _ = c.singleCall.Do(
"IsDaemonRunning",
func() (interface{}, error) { return c.multipath.IsDaemonRunning(ctx), nil })
multipathIsEnabled := ret.(bool)
var d Device
if multipathIsEnabled {
logger.Info(ctx, "start multipath device connection")
d, err = c.connectMultipathDevice(ctx, sessions, info, useFC)
if err != nil {
logger.Info(ctx, "start single device connection")
d, err = c.connectSingleDevice(ctx, info, useFC)
}
} else {
logger.Info(ctx, "start single device connection")
d, err = c.connectSingleDevice(ctx, info, useFC)
}
if err == nil {
if c.scsi.CheckDeviceIsValid(ctx, path.Join("/dev/", d.Name)) {
return d, nil
}
msg := fmt.Sprintf("device %s found but failed to read data from it", d.Name)
logger.Error(ctx, msg)
err = errors.New(msg)
}
logger.Error(ctx, "failed to connect volume, try to cleanup: %s", err.Error())
_ = c.cleanConnection(ctx, true, info)
return Device{}, err
}
// DisconnectVolume - disconnect a given nvme volume
func (c *NVMeConnector) DisconnectVolume(ctx context.Context, info NVMeVolumeInfo) error {
defer tracer.TraceFuncCall(ctx, "NVMeConnector.DisconnectVolume")()
if err := c.limiter.Acquire(ctx, 1); err != nil {
return errors.New("too many parallel operations. try later")
}
defer c.limiter.Release(1)
addDefaultNVMePortToVolumeInfoPortals(&info)
return c.cleanConnection(ctx, false, info)
}
// DisconnectVolumeByDeviceName - disconnect from a given device
func (c *NVMeConnector) DisconnectVolumeByDeviceName(ctx context.Context, name string) error {
defer tracer.TraceFuncCall(ctx, "NVMeConnector.DisconnectVolumeByDeviceName")()
if err := c.limiter.Acquire(ctx, 1); err != nil {
return errors.New("too many parallel operations. try later")
}
defer c.limiter.Release(1)
return c.baseConnector.disconnectNVMEDevicesByDeviceName(ctx, name)
}
// GetInitiatorName - returns nqn
func (c *NVMeConnector) GetInitiatorName(ctx context.Context) ([]string, error) {
defer tracer.TraceFuncCall(ctx, "NVMeConnector.GetInitiatorName")()
logger.Info(ctx, "get initiator name")
data, err := c.nvmeLib.GetInitiators("")
if err != nil {
logger.Error(ctx, "failed to read initiator name: %s", err.Error())
}
logger.Info(ctx, "initiator name is: %s", data)
return data, nil
}
func addDefaultNVMePortToVolumeInfoPortals(info *NVMeVolumeInfo) {
for i, t := range info.Targets {
if !strings.Contains(t.Portal, ":") {
info.Targets[i].Portal += NVMePortDefault
}
}
}
func (c *NVMeConnector) cleanConnection(ctx context.Context, force bool, info NVMeVolumeInfo) error {
defer tracer.TraceFuncCall(ctx, "NVMeConnector.cleanConnection")()
var devices []string
wwn := info.WWN
DevicePathsAndNamespaces, err := c.nvmeLib.ListNVMeDeviceAndNamespace()
if err != nil {
log.Errorf("Couldn't find the nvme namespaces %s", err.Error())
}
var devicePath string
var namespace string
log.Debugf("DevicePathsAndNamespaces: %s", DevicePathsAndNamespaces)
for _, DevicePathAndNamespace := range DevicePathsAndNamespaces {
devicePath = DevicePathAndNamespace.DevicePath
namespace = DevicePathAndNamespace.Namespace
nguid, newnamespace, _ := c.nvmeLib.GetNVMeDeviceData(devicePath)
log.Debugf("nguid: %s, wwn: %s, newnamespace: %s, namespace: %s", nguid, wwn, newnamespace, namespace)
if c.wwnMatches(nguid, wwn) && namespace == newnamespace {
devices = append(devices, devicePath)
}
}
if len(devices) == 0 {
return nil
}
return c.baseConnector.cleanNVMeDevices(ctx, force, devices, wwn)
}
func (c *NVMeConnector) connectSingleDevice(ctx context.Context, info NVMeVolumeInfo, useFC bool) (Device, error) {
defer tracer.TraceFuncCall(ctx, "NVMeConnector.connectSingleDevice")()
devCH := make(chan DevicePathResult)
wg := sync.WaitGroup{}
discoveryCtx, cFunc := context.WithTimeout(ctx, c.waitDeviceTimeout)
defer cFunc()
wg.Add(1)
go c.discoverDevice(discoveryCtx, &wg, devCH, info, useFC)
// for non blocking wg wait
wgCH := make(chan struct{})
go func() {
wg.Wait()
close(wgCH)
}()
var devices []string
var discoveryComplete, lastTry bool
var endTime time.Time
wwn := info.WWN
for {
// get discovered devices
select {
case <-ctx.Done():
return Device{}, errors.New("connectSingleDevice canceled")
default:
}
devices, nguid := readNVMeDevicesFromResultCH(devCH, devices)
// check all discovery gorutines finished
if !discoveryComplete {
select {
case <-wgCH:
discoveryComplete = true
logger.Info(ctx, "all discovery goroutines complete")
default:
logger.Info(ctx, "discovery goroutines are still running")
}
}
if discoveryComplete && len(devices) == 0 {
msg := "discovery complete but devices not found"
logger.Error(ctx, msg)
return Device{}, errors.New(msg)
}
if wwn == "" && len(devices) != 0 {
msg := "invalid WWN provided"
logger.Error(ctx, msg)
return Device{}, errors.New(msg)
}
if wwn != "" && nguid != "" {
if len(devices) > 1 {
logger.Debug(ctx, "Multiple nvme devices found for the given wwn %s", wwn)
}
return Device{Name: devices[0], WWN: wwn}, nil
}
if discoveryComplete && !lastTry {
logger.Info(ctx, "discovery finished, wait %f seconds for device registration",
c.waitDeviceRegisterTimeout.Seconds())
lastTry = true
endTime = time.Now().Add(c.waitDeviceRegisterTimeout)
}
if lastTry && time.Now().After(endTime) {
msg := "registered device not found"
logger.Error(ctx, msg)
return Device{}, errors.New(msg)
}
time.Sleep(time.Second)
}
}
func (c *NVMeConnector) connectMultipathDevice(
ctx context.Context, _ []gonvme.NVMESession, info NVMeVolumeInfo, useFC bool,
) (Device, error) {
defer tracer.TraceFuncCall(ctx, "NVMeConnector.connectMultipathDevice")()
devCH := make(chan DevicePathResult)
wg := sync.WaitGroup{}
discoveryCtx, cFunc := context.WithTimeout(ctx, c.waitDeviceTimeout)
defer cFunc()
wg.Add(1)
go c.discoverDevice(discoveryCtx, &wg, devCH, info, useFC)
// for non blocking wg wait
wgCH := make(chan struct{})
go func() {
wg.Wait()
close(wgCH)
}()
var devices []string
var mpath string
wwn := info.WWN
var wwnAdded, discoveryComplete, lastTry bool
var endTime time.Time
nguid := ""
for {
// get discovered devices
select {
case <-ctx.Done():
return Device{}, errors.New("connectMultipathDevice canceled")
default:
}
if nguid == "" {
devices, nguid = readNVMeDevicesFromResultCH(devCH, devices)
}
// check all discovery gorutines finished
if !discoveryComplete {
select {
case <-wgCH:
discoveryComplete = true
logger.Info(ctx, "all discover goroutines complete")
default:
logger.Info(ctx, "discover goroutines are still running")
}
}
if discoveryComplete && len(devices) == 0 {
msg := "discover complete but devices not found"
logger.Error(ctx, msg)
return Device{}, errors.New(msg)
}
if wwn == "" && len(devices) != 0 {
logger.Info(ctx, "Invalid WWN provided ")
}
if wwn != "" && mpath == "" {
var err error
mpath, err = c.scsi.GetNVMEDMDeviceByChildren(ctx, devices)
if err != nil {
logger.Debug(ctx, "failed to get DM by children: %s", err.Error())
}
if mpath == "" && !wwnAdded {
if err := c.multipath.AddWWID(ctx, wwn); err == nil {
wwnAdded = true
} else {
logger.Info(ctx, err.Error())
}
}
}
if mpath != "" {
// use nguid as wwn for nvme devices
var err error
if err = c.scsi.WaitUdevSymlinkNVMe(ctx, mpath, nguid); err == nil {
logger.Info(ctx, "multipath device found: %s", mpath)
return Device{WWN: wwn, Name: mpath, MultipathID: wwn}, nil
}
}
if discoveryComplete && !lastTry {
logger.Info(ctx, "discovery finished, wait %f seconds for DM to appear",
c.waitDeviceRegisterTimeout.Seconds())
lastTry = true
for _, d := range devices {
if err := c.multipath.AddPath(ctx, path.Join("/dev/", d)); err != nil {
logger.Error(ctx, err.Error())
}
}
endTime = time.Now().Add(c.waitDeviceRegisterTimeout)
}
if lastTry && time.Now().After(endTime) {
msg := "registered multipath device not found"
logger.Error(ctx, msg)
return Device{}, errors.New(msg)
}
time.Sleep(time.Second)
}
}
func (c *NVMeConnector) validateNVMeVolumeInfo(ctx context.Context, info NVMeVolumeInfo) error {
defer tracer.TraceFuncCall(ctx, "NVMeConnector.validateNVMeVolumeInfo")()
if len(info.Targets) == 0 {
return errors.New("at least one NVMe target required")
}
for _, t := range info.Targets {
if t.Target == "" || t.Portal == "" {
return errors.New("invalid target info")
}
}
if info.WWN == "" {
return errors.New("invalid volume wwn")
}
return nil
}
func (c *NVMeConnector) discoverDevice(ctx context.Context, wg *sync.WaitGroup, result chan DevicePathResult, info NVMeVolumeInfo, useFC bool) {
defer tracer.TraceFuncCall(ctx, "NVMeConnector.discoverDevice")()
defer wg.Done()
wwn := info.WWN
var devicePathResult DevicePathResult
retryCount := 0
for {
nguidResult := ""
DevicePathsAndNamespaces, err := c.nvmeLib.ListNVMeDeviceAndNamespace()
if err != nil {
log.Errorf("Couldn't find the nvme namespaces %s", err.Error())
}
var devicePaths []string
var devicePath string
var namespace string
log.Debugf("DevicePathsAndNamespaces %+v retryCount %d", DevicePathsAndNamespaces, retryCount)
for _, DevicePathAndNamespace := range DevicePathsAndNamespaces {
devicePath = DevicePathAndNamespace.DevicePath
namespace = DevicePathAndNamespace.Namespace
nguid, newnamespace, _ := c.nvmeLib.GetNVMeDeviceData(devicePath)
log.Debugf("nguid %s, wwn %s, newnamespace %s, namespace %s", nguid, wwn, newnamespace, namespace)
if c.wwnMatches(nguid, wwn) && namespace == newnamespace {
devicePaths = append(devicePaths, devicePath)
nguidResult = nguid
// using two nvme devices for each volume for the multipath discovery
if len(devicePaths) >= 2 {
break
}
}
}
devicePathResult = DevicePathResult{devicePaths: devicePaths, nguid: nguidResult}
logger.Debug(ctx, "devicePathResult", devicePathResult, "nguidResult", nguidResult, "retryCount", retryCount)
if nguidResult != "" || retryCount == 1 {
break
}
err = c.tryNVMeConnect(ctx, info, useFC)
if err != nil {
log.Errorf("Couldn't perform duplicate NVMe connect")
}
retryCount = retryCount + 1
}
result <- devicePathResult
}
// wwnMatches checks if the given nguid and wwn match.
//
// Parameters:
// - nguid: a string representing the nguid.
// - wwn: a string representing the wwn.
//
// Returns:
// - a boolean indicating whether the wwn matches the nguid.
func (c *NVMeConnector) wwnMatches(nguid, wwn string) bool {
/*
// PowerStore
Sample wwn : naa.68ccf098001111a2222b3d4444a1b23c
token1: 1111a2222b3d4444
token2: a1b23c
Sample nguid : 1111a2222b3d44448ccf096800a1b23c
// PowerMax
nguid: 12635330303134340000976000012000
wwn: 60000970000120001263533030313434
11aaa111111111a11a111a1111aa1111
1a111a1111aa1111 1aaa11 1 1111111a1
nguid: wwn[last16] + wwn[1:6] + wwn[0] + wwn[7:15]
1263533030313434 + 000097 + 6 + 000012000
*/
if len(wwn) < 32 {
return false
}
wwn = strings.ToLower(wwn)
if strings.HasPrefix(wwn, "naa.") {
wwn = wwn[4:]
}
var token1, token2 string
if strings.HasPrefix(wwn, PowerStoreOUIPrefix) {
token1 = wwn[13 : len(wwn)-7]
token2 = wwn[len(wwn)-6 : len(wwn)-1]
log.Infof("PowerStore: %s %s %s %t", token1, token2, nguid, strings.Contains(nguid, token2))
if strings.Contains(nguid, token1) && strings.Contains(nguid, token2) {
return true
}
} else if strings.HasPrefix(wwn, PowerMaxOUIPrefix) {
token1 = wwn[16:]
token2 = wwn[1:7]
log.Infof("Powermax: %s %s %s %t", token1, token2, nguid, strings.HasPrefix(nguid, token1+token2))
if strings.HasPrefix(nguid, token1+token2) {
return true
}
}
return false
}
func (c *NVMeConnector) tryNVMeConnect(ctx context.Context, info NVMeVolumeInfo, useFC bool) error {
defer tracer.TraceFuncCall(ctx, "NVMeConnector.tryNVMeConnect")()
targets := info.Targets
if useFC {
FCHostsInfo, err := c.getFCHostInfo(ctx)
if err != nil {
log.Errorf("Error gathering NVMe/FC Hosts on the host side: %v", err)
return err
}
for _, t := range targets {
for _, FCHostInfo := range FCHostsInfo {
hostAddress := strings.Replace(fmt.Sprintf("nn-%s:pn-%s", FCHostInfo.NodeName, FCHostInfo.PortName), "\n", "", -1)
tgt := gonvme.NVMeTarget{Portal: t.Portal, TargetNqn: t.Target, HostAdr: hostAddress}
err = c.nvmeLib.NVMeFCConnect(tgt, true)
if err != nil {
log.Errorf("Couldn't connect to NVMeFC target")
continue
}
return nil
}
}
}
return nil
}
func readNVMeDevicesFromResultCH(ch chan DevicePathResult, _ []string) ([]string, string) {
devicePathResult := <-ch
var devicePaths []string
for _, dpath := range devicePathResult.devicePaths {
// modify dpath /dev/nvme0n1 -> nvme0n1
newpath := strings.ReplaceAll(dpath, "/dev/", "")
devicePaths = append(devicePaths, newpath)
}
return devicePaths, devicePathResult.nguid
}
func (c *NVMeConnector) checkNVMeSessions(
ctx context.Context, info NVMeVolumeInfo,
) ([]gonvme.NVMESession, error) {
defer tracer.TraceFuncCall(ctx, "NVMeConnector.checkNVMeSessions")()
var activeSessions []gonvme.NVMESession
// var targetsToLogin []NVMeTargetInfo
for _, t := range info.Targets {
logger.Info(ctx,
"check NVMe session for %s %s", t.Portal, t.Target)
session, _, err := c.getSessionByTargetInfo(ctx, t)
if err != nil {
logger.Error(ctx,
"unable to get nvme session info: %s", err.Error())
continue
}
activeSessions = append(activeSessions, session)
}
errMsg := "can't find active nvme session"
if len(activeSessions) == 0 {
logger.Error(ctx, errMsg)
return nil, errors.New(errMsg)
}
logger.Info(ctx, "found active nvme sessions")
return activeSessions, nil
}
func (c *NVMeConnector) getSessionByTargetInfo(ctx context.Context,
target NVMeTargetInfo,
) (gonvme.NVMESession, bool, error) {
defer tracer.TraceFuncCall(ctx, "NVMeConnector.getSessionByTargetInfo")()
r := gonvme.NVMESession{}
logPrefix := fmt.Sprintf("Portal: %s, Target: %s :", target.Portal, target.Target)
sessions, err := c.nvmeLib.GetSessions()
if err != nil {
logger.Error(ctx, logPrefix+"unable to get nvme sessions: %s", err.Error())
return r, false, err
}
var found bool
// TODO: check if comparision needs contains check
for _, s := range sessions {
if s.Target == target.Target && s.Portal == target.Portal {
r = s
found = true
break
}
}
if found {
logger.Info(ctx, logPrefix+"nvme session found")
} else {
logger.Info(ctx, logPrefix+"nvme session not found")
}
return r, found, nil
}
func (c *NVMeConnector) getFCHostInfo(ctx context.Context) ([]FCHBAInfo, error) {
defer tracer.TraceFuncCall(ctx, "NVMeConnector.getFCHostInfo")()
logger.Info(ctx, "get FC hbas info")
match, err := c.filePath.Glob("/sys/class/fc_host/host*")
if err != nil {
logger.Error(ctx, err.Error())
return nil, err
}
var FCHostsInfo []FCHBAInfo
for _, m := range match {
var FCHostInfo FCHBAInfo
data, err := c.os.ReadFile(path.Join(m, "port_name"))
if err != nil {
log.Errorf("match: %s failed to read port_name file: %s", match, err.Error())
continue
}
FCHostInfo.PortName = strings.TrimSpace(string(data))
data, err = c.os.ReadFile(path.Join(m, "node_name"))
if err != nil {
log.Errorf("match: %s failed to read node_name file: %s", match, err.Error())
continue
}
FCHostInfo.NodeName = strings.TrimSpace(string(data))
FCHostsInfo = append(FCHostsInfo, FCHostInfo)
}
logger.Info(ctx, "FC has been found: %s", FCHostsInfo)
return FCHostsInfo, nil
}