From f9f33f35d659c15bae83051ca8f264cf3bec2b26 Mon Sep 17 00:00:00 2001 From: Don Khan Date: Tue, 17 Sep 2024 08:56:52 -0500 Subject: [PATCH] Fix detection and matching of wwn to nguid. --- nvme.go | 52 ++++++++++++++++++++++++++++++++++++---------------- nvme_test.go | 28 ++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+), 16 deletions(-) diff --git a/nvme.go b/nvme.go index 82f82ac..2224fbc 100644 --- a/nvme.go +++ b/nvme.go @@ -54,6 +54,12 @@ const ( // 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 @@ -526,15 +532,23 @@ func (c *NVMeConnector) discoverDevice(ctx context.Context, wg *sync.WaitGroup, 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 - wwn1 : 1111a2222b3d4444 - wwn2 : a1b23c - + token1: 1111a2222b3d4444 + token2: a1b23c Sample nguid : 1111a2222b3d44448ccf096800a1b23c - / pmax: + // PowerMax nguid: 12635330303134340000976000012000 wwn: 60000970000120001263533030313434 11aaa111111111a11a111a1111aa1111 @@ -545,23 +559,29 @@ func (c *NVMeConnector) wwnMatches(nguid, wwn string) bool { if len(wwn) < 32 { return false } - var wwn1, wwn2 string - if strings.Contains(wwn, "naa") { - // powerstore - wwn1 = wwn[13 : len(wwn)-7] - wwn2 = wwn[len(wwn)-6 : len(wwn)-1] - if strings.Contains(nguid, wwn1) && strings.Contains(nguid, wwn2) { + + 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 { - // pmax - wwn1 = wwn[16:] - wwn2 = wwn[1:7] - log.Infof("Powermax: %s %s %s %t", wwn1, wwn2, nguid, strings.HasPrefix(nguid, wwn1+wwn2)) - if strings.HasPrefix(nguid, wwn1+wwn2) { + } 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 } diff --git a/nvme_test.go b/nvme_test.go index fe3297b..ccf4392 100644 --- a/nvme_test.go +++ b/nvme_test.go @@ -17,6 +17,7 @@ package gobrick import ( "context" + "fmt" "reflect" "testing" "time" @@ -191,3 +192,30 @@ func TestNVME_Connector_ConnectVolume(t *testing.T) { }) } } + +func TestNVME_wwnMatches(t *testing.T) { + tests := []struct { + nguid string + wwn string + want bool + }{ + {nguid: "0f8da909812540628ccf09680039914f", wwn: "naa.68ccf098000f8da9098125406239914f", want: true}, + {nguid: "0f8da909812540628ccf09680039914f", wwn: "NAA.68ccf098000f8da9098125406239914f", want: true}, + {nguid: "0f8da909812540628ccf09680039914f", wwn: "68ccf098000f8da9098125406239914f", want: true}, + {nguid: "0f8da909812540628ccf09680039914f", wwn: "68CCF098000F8DA9098125406239914F", want: true}, + {nguid: "0f8da909812540628ccf09680039914f", wwn: "60000978000f8da9098125406239914f", want: false}, + {nguid: "12635330303134340000976000012000", wwn: "60000970000120001263533030313434", want: true}, + {nguid: "12635330303134340000976000012000", wwn: "68ccf070000120001263533030313434", want: false}, + {nguid: "12635330303134340000976000012000", wwn: "8ccf070000120001263533030313434", want: false}, + {nguid: "12635330303134340000976000012000", wwn: "68CCF070000120001263533030313434", want: false}, + } + + for _, tt := range tests { + t.Run(fmt.Sprintf("(%s,%s) should be %v", tt.nguid, tt.wwn, tt.want), func(t *testing.T) { + c := &NVMeConnector{} + if c.wwnMatches(tt.nguid, tt.wwn) != tt.want { + t.Errorf("wwnMatches(%v, %v) = %v, want %v", tt.nguid, tt.wwn, !tt.want, tt.want) + } + }) + } +}