From 71c82bb283a44ece9e75029bb00e42797586976c Mon Sep 17 00:00:00 2001 From: Derek Su Date: Tue, 20 Feb 2024 09:19:07 +0000 Subject: [PATCH] vendor: update go-spdk-helper Longhorn 7672 Signed-off-by: Derek Su --- go.mod | 2 + go.sum | 4 +- .../go-spdk-helper/pkg/spdk/client/basic.go | 49 +++++++++ .../go-spdk-helper/pkg/spdk/setup/setup.go | 104 ++++++++++++++++++ .../go-spdk-helper/pkg/spdk/types/aio.go | 2 +- .../go-spdk-helper/pkg/spdk/types/bdev.go | 10 +- .../go-spdk-helper/pkg/spdk/types/nvme.go | 9 +- .../go-spdk-helper/pkg/spdk/types/virtio.go | 12 ++ vendor/modules.txt | 3 +- 9 files changed, 183 insertions(+), 12 deletions(-) create mode 100644 vendor/github.com/longhorn/go-spdk-helper/pkg/spdk/setup/setup.go create mode 100644 vendor/github.com/longhorn/go-spdk-helper/pkg/spdk/types/virtio.go diff --git a/go.mod b/go.mod index b8512ca11..d691dbefe 100644 --- a/go.mod +++ b/go.mod @@ -53,3 +53,5 @@ require ( k8s.io/mount-utils v0.26.0 // indirect k8s.io/utils v0.0.0-20221107191617-1a15be271d1d // indirect ) + +replace github.com/longhorn/go-spdk-helper v0.0.0-20240219160403-5fe20e7f7542 => github.com/derekbit/go-spdk-helper v0.0.0-20240222011249-55a70b724e31 diff --git a/go.sum b/go.sum index 8c71a9fbf..d9cd95059 100644 --- a/go.sum +++ b/go.sum @@ -16,6 +16,8 @@ github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM= github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/derekbit/go-spdk-helper v0.0.0-20240222011249-55a70b724e31 h1:niHOcSamWA2imdq8KcWZ/zQIiqL/kB9Iu2d6tbVDN+M= +github.com/derekbit/go-spdk-helper v0.0.0-20240222011249-55a70b724e31/go.mod h1:GlzdjhUZn8iH/TzCfy8O+WXP4kxxnPvwFAG/uwuKmso= github.com/gammazero/deque v0.1.0 h1:f9LnNmq66VDeuAlSAapemq/U7hJ2jpIWa4c09q8Dlik= github.com/gammazero/deque v0.1.0/go.mod h1:KQw7vFau1hHuM8xmI9RbgKFbAsQFWmBpqQ2KenFLk6M= github.com/gammazero/workerpool v1.1.2 h1:vuioDQbgrz4HoaCi2q1HLlOXdpbap5AET7xu5/qj87g= @@ -46,8 +48,6 @@ github.com/longhorn/backupstore v0.0.0-20240110081942-bd231cfb0c7b h1:euBbfDb6bn github.com/longhorn/backupstore v0.0.0-20240110081942-bd231cfb0c7b/go.mod h1:4cbJWtlrD2cGTQxQLtdlPTYopiJiusXH7CpOBrn/s3k= github.com/longhorn/go-common-libs v0.0.0-20240219094750-e7176c332156 h1:Jv1+UlfrzSodyIobaioQn8Vh4RQ+23FpN2Q60iFdqCU= github.com/longhorn/go-common-libs v0.0.0-20240219094750-e7176c332156/go.mod h1:nIECQARppamt2zwFSdzADRTVKo/7izFwWIS3VWi7D/s= -github.com/longhorn/go-spdk-helper v0.0.0-20240219160403-5fe20e7f7542 h1:IrYKv4aIz7ncNOzzU4wadvmMGLUZjRVu34IZd8EmcBw= -github.com/longhorn/go-spdk-helper v0.0.0-20240219160403-5fe20e7f7542/go.mod h1:GlzdjhUZn8iH/TzCfy8O+WXP4kxxnPvwFAG/uwuKmso= github.com/longhorn/nsfilelock v0.0.0-20200723175406-fa7c83ad0003 h1:Jw9uANsGcHTxp6HcC++/vN17LfeuDmozHI2j6DoZf5E= github.com/longhorn/nsfilelock v0.0.0-20200723175406-fa7c83ad0003/go.mod h1:0CLeXlf59Lg6C0kjLSDf47ft73Dh37CwymYRKWwAn04= github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0/go.mod h1:zJYVVT2jmtg6P3p1VtQj7WsuWi/y4VnjVBn7F8KPB3I= diff --git a/vendor/github.com/longhorn/go-spdk-helper/pkg/spdk/client/basic.go b/vendor/github.com/longhorn/go-spdk-helper/pkg/spdk/client/basic.go index e649d4736..78196ade9 100644 --- a/vendor/github.com/longhorn/go-spdk-helper/pkg/spdk/client/basic.go +++ b/vendor/github.com/longhorn/go-spdk-helper/pkg/spdk/client/basic.go @@ -4,6 +4,8 @@ import ( "encoding/json" "strings" + "github.com/pkg/errors" + spdktypes "github.com/longhorn/go-spdk-helper/pkg/spdk/types" ) @@ -1045,3 +1047,50 @@ func (c *Client) LogGetPrintLevel() (string, error) { return strings.Trim(string(level), "\"\n"), nil } + +// BdevVirtioAttachController creates new initiator Virtio SCSI or Virtio Block and expose all found bdevs. +// +// "name": Required. Use this name as base for new created bdevs. +// +// "trtype": Required. Transport type, "user" or "pci". +// +// "traddr": Required. Transport type specific target address: e.g. UNIX domain socket path or BDF. +// +// "devType": Required. Device type, "scsi" or "blk". +func (c *Client) BdevVirtioAttachController(name, trtype, traddr, devType string) ([]string, error) { + req := spdktypes.BdevVirtioAttachControllerRequest{ + Name: name, + Trtype: trtype, + Traddr: traddr, + DevType: devType, + } + + cmdOutput, err := c.jsonCli.SendCommand("bdev_virtio_attach_controller", req) + if err != nil { + return nil, err + } + + var disks []string + err = json.Unmarshal([]byte(cmdOutput), &disks) + if err != nil { + return nil, errors.Wrapf(err, "failed to unmarshal disks: %s", cmdOutput) + } + + return disks, nil +} + +// BdevVirtioDetachController removes a Virtio device. +// +// "name": Required. Use this name as base for new created bdevs. +func (c *Client) BdevVirtioDetachController(name string) (deleted bool, err error) { + req := spdktypes.BdevVirtioDetachControllerRequest{ + Name: name, + } + + cmdOutput, err := c.jsonCli.SendCommand("bdev_virtio_detach_controller", req) + if err != nil { + return false, err + } + + return deleted, json.Unmarshal(cmdOutput, &deleted) +} diff --git a/vendor/github.com/longhorn/go-spdk-helper/pkg/spdk/setup/setup.go b/vendor/github.com/longhorn/go-spdk-helper/pkg/spdk/setup/setup.go new file mode 100644 index 000000000..a0ba9d327 --- /dev/null +++ b/vendor/github.com/longhorn/go-spdk-helper/pkg/spdk/setup/setup.go @@ -0,0 +1,104 @@ +package setup + +import ( + "fmt" + "strings" + + commonNs "github.com/longhorn/go-common-libs/ns" + + "github.com/longhorn/go-spdk-helper/pkg/types" +) + +const ( + spdkSetupPath = "/usr/src/spdk/scripts/setup.sh" +) + +func Bind(deviceAddr, deviceDriver string, executor *commonNs.Executor) (string, error) { + if deviceAddr == "" { + return "", fmt.Errorf("device address is empty") + } + + envs := []string{ + fmt.Sprintf("%s=%s", "PCI_ALLOWED", deviceAddr), + fmt.Sprintf("%s=%s", "DRIVER_OVERRIDE", deviceDriver), + } + + cmdArgs := []string{ + spdkSetupPath, + "bind", + } + + outputStr, err := executor.Execute(envs, "bash", cmdArgs, types.ExecuteTimeout) + if err != nil { + return "", err + } + + return outputStr, nil +} + +func Unbind(deviceAddr string, executor *commonNs.Executor) (string, error) { + if deviceAddr == "" { + return "", fmt.Errorf("device address is empty") + } + + cmdArgs := []string{ + spdkSetupPath, + "unbind", + deviceAddr, + } + + outputStr, err := executor.Execute(nil, "bash", cmdArgs, types.ExecuteTimeout) + if err != nil { + return "", err + } + + return outputStr, nil +} + +func GetDiskDriver(deviceAddr string, executor *commonNs.Executor) (string, error) { + if deviceAddr == "" { + return "", fmt.Errorf("device address is empty") + } + + cmdArgs := []string{ + spdkSetupPath, + "get-device-driver", + deviceAddr, + } + + outputStr, err := executor.Execute(nil, "bash", cmdArgs, types.ExecuteTimeout) + if err != nil { + return "", err + } + + return extractJSON(outputStr) +} + +func GetDiskStatus(deviceAddr string, executor *commonNs.Executor) (string, error) { + if deviceAddr == "" { + return "", fmt.Errorf("device address is empty") + } + + cmdArgs := []string{ + spdkSetupPath, + "disk-status", + deviceAddr, + } + + outputStr, err := executor.Execute(nil, "bash", cmdArgs, types.ExecuteTimeout) + if err != nil { + return "", err + } + + return extractJSON(outputStr) +} + +func extractJSON(outputStr string) (string, error) { + // Find the first '{' and last '}' characters, assuming valid JSON format + start := strings.Index(outputStr, "{") + end := strings.LastIndex(outputStr, "}") + if start != -1 && end != -1 { + return outputStr[start : end+1], nil + } + return "", fmt.Errorf("failed to extract JSON from output: %s", outputStr) +} diff --git a/vendor/github.com/longhorn/go-spdk-helper/pkg/spdk/types/aio.go b/vendor/github.com/longhorn/go-spdk-helper/pkg/spdk/types/aio.go index 015ade311..404e305c2 100644 --- a/vendor/github.com/longhorn/go-spdk-helper/pkg/spdk/types/aio.go +++ b/vendor/github.com/longhorn/go-spdk-helper/pkg/spdk/types/aio.go @@ -15,7 +15,7 @@ type BdevAioDriverSpecificInfo struct { type BdevAioCreateRequest struct { Name string `json:"name"` Filename string `json:"filename"` - BlockSize uint64 `json:"block_size"` + BlockSize uint64 `json:"block_size,omitzero"` } type BdevAioDeleteRequest struct { diff --git a/vendor/github.com/longhorn/go-spdk-helper/pkg/spdk/types/bdev.go b/vendor/github.com/longhorn/go-spdk-helper/pkg/spdk/types/bdev.go index 27c40da85..3ae95f72d 100644 --- a/vendor/github.com/longhorn/go-spdk-helper/pkg/spdk/types/bdev.go +++ b/vendor/github.com/longhorn/go-spdk-helper/pkg/spdk/types/bdev.go @@ -3,10 +3,12 @@ package types type BdevProductName string const ( - BdevProductNameAio = BdevProductName("AIO disk") - BdevProductNameLvol = BdevProductName("Logical Volume") - BdevProductNameRaid = BdevProductName("Raid Volume") - BdevProductNameNvme = BdevProductName("NVMe disk") + BdevProductNameAio = BdevProductName("AIO disk") + BdevProductNameLvol = BdevProductName("Logical Volume") + BdevProductNameRaid = BdevProductName("Raid Volume") + BdevProductNameNvme = BdevProductName("NVMe disk") + BdevProductNameVirtioBlk = BdevProductName("VirtioBlk Disk") + BdevProductNameVirtioScsi = BdevProductName("Virtio SCSI Disk") ) type BdevType string diff --git a/vendor/github.com/longhorn/go-spdk-helper/pkg/spdk/types/nvme.go b/vendor/github.com/longhorn/go-spdk-helper/pkg/spdk/types/nvme.go index 76afe25df..b7fe46911 100644 --- a/vendor/github.com/longhorn/go-spdk-helper/pkg/spdk/types/nvme.go +++ b/vendor/github.com/longhorn/go-spdk-helper/pkg/spdk/types/nvme.go @@ -21,10 +21,11 @@ const ( type BdevDriverSpecificNvme []NvmeNamespaceInfo type NvmeNamespaceInfo struct { - CtrlrData NvmeCtrlrData `json:"ctrlr_data"` - NsData NvmeNsData `json:"ns_data"` - Trid NvmeTransportID `json:"trid"` - VS NvmeVendorSpecific `json:"vs"` + PciAddress string `json:"pci_address,omitempty"` + CtrlrData NvmeCtrlrData `json:"ctrlr_data"` + NsData NvmeNsData `json:"ns_data"` + Trid NvmeTransportID `json:"trid"` + VS NvmeVendorSpecific `json:"vs"` } type NvmeCtrlrData struct { diff --git a/vendor/github.com/longhorn/go-spdk-helper/pkg/spdk/types/virtio.go b/vendor/github.com/longhorn/go-spdk-helper/pkg/spdk/types/virtio.go new file mode 100644 index 000000000..2d240aac9 --- /dev/null +++ b/vendor/github.com/longhorn/go-spdk-helper/pkg/spdk/types/virtio.go @@ -0,0 +1,12 @@ +package types + +type BdevVirtioAttachControllerRequest struct { + Name string `json:"name"` + Trtype string `json:"trtype"` + Traddr string `json:"traddr"` + DevType string `json:"dev_type"` +} + +type BdevVirtioDetachControllerRequest struct { + Name string `json:"name"` +} diff --git a/vendor/modules.txt b/vendor/modules.txt index 334ed3b00..62a56af23 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -64,11 +64,12 @@ github.com/longhorn/go-common-libs/sync github.com/longhorn/go-common-libs/sys github.com/longhorn/go-common-libs/types github.com/longhorn/go-common-libs/utils -# github.com/longhorn/go-spdk-helper v0.0.0-20240219160403-5fe20e7f7542 +# github.com/longhorn/go-spdk-helper v0.0.0-20240219160403-5fe20e7f7542 => github.com/derekbit/go-spdk-helper v0.0.0-20240222011249-55a70b724e31 ## explicit; go 1.21 github.com/longhorn/go-spdk-helper/pkg/jsonrpc github.com/longhorn/go-spdk-helper/pkg/nvme github.com/longhorn/go-spdk-helper/pkg/spdk/client +github.com/longhorn/go-spdk-helper/pkg/spdk/setup github.com/longhorn/go-spdk-helper/pkg/spdk/target github.com/longhorn/go-spdk-helper/pkg/spdk/types github.com/longhorn/go-spdk-helper/pkg/types