Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Storage: Add NVMe and SDC storage connectors #14710

Draft
wants to merge 13 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 76 additions & 0 deletions lxd/storage/connectors/connector.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package connectors

import (
"context"
)

const (
// TypeUnknown represents an unknown storage connector.
TypeUnknown string = "unknown"

// TypeNVME represents an NVMe/TCP storage connector.
TypeNVME string = "nvme"

// TypeSDC represents Dell SDC storage connector.
TypeSDC string = "sdc"
)

// Connector represents a storage connector that handles connections through
// appropriate storage subsystem.
type Connector interface {
Type() string
Version() (string, error)
QualifiedName() (string, error)
LoadModules() bool
SessionID(targetQN string) (string, error)
Connect(ctx context.Context, targetAddr string, targetQN string) error
ConnectAll(ctx context.Context, targetAddr string) error
Disconnect(targetQN string) error
DisconnectAll() error
}

// NewConnector instantiates a new connector of the given type.
// The caller needs to ensure connector type is validated before calling this
// function, as common (empty) connector is returned for unknown type.
func NewConnector(connectorType string, serverUUID string) Connector {
common := common{
serverUUID: serverUUID,
}

switch connectorType {
case TypeNVME:
return &connectorNVMe{
common: common,
}

case TypeSDC:
return &connectorNVMe{
common: common,
}

default:
// Return common connector if the type is unknown. This removes
// the need to check for nil or handle the error in the caller.
return &common
}
}

// GetSupportedVersions returns the versions for the given connector types
// ignoring those that produce an error when version is being retrieved
// (e.g. due to a missing required tools).
func GetSupportedVersions(connectorTypes []string) []string {
versions := make([]string, 0, len(connectorTypes))

// Iterate over the supported connectors, extracting version and loading
// kernel module for each of them.
for _, connectorType := range connectorTypes {
version, err := NewConnector(connectorType, "").Version()
if err != nil {
continue
}

versions = append(versions, version)
}

return versions
}
58 changes: 58 additions & 0 deletions lxd/storage/connectors/connector_common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package connectors

import (
"context"
"fmt"
)

var _ Connector = &common{}

type common struct {
serverUUID string
}

// Type returns the name of the connector.
func (c *common) Type() string {
return TypeUnknown
}

// Version returns the version of the connector.
func (c *common) Version() (string, error) {
return "", fmt.Errorf("Version not implemented")
}

// QualifiedName returns the qualified name of the connector.
func (c *common) QualifiedName() (string, error) {
return "", fmt.Errorf("QualifiedName not implemented")
}

// LoadModules loads the necessary kernel modules.
func (c *common) LoadModules() bool {
return true
}

// SessionID returns the identifier of a session that matches the connector's qualified name.
// If there is no such session, an empty string is returned.
func (c *common) SessionID(targetQN string) (string, error) {
return "", fmt.Errorf("ExistingSession not implemented")
}

// Connect establishes a connection with the target on the given address.
func (c common) Connect(ctx context.Context, targetAddr string, targetQN string) error {
return fmt.Errorf("Connect not implemented")
}

// ConnectAll establishes a connection with all targets available on the given address.
func (c common) ConnectAll(ctx context.Context, targetAddr string) error {
return fmt.Errorf("ConnectAll not implemented")
}

// Disconnect terminates a connection with the target.
func (c common) Disconnect(targetQN string) error {
return fmt.Errorf("Disconnect not implemented")
}

// DisconnectAll terminates all connections with all targets.
func (c common) DisconnectAll() error {
return fmt.Errorf("DisconnectAll not implemented")
}
171 changes: 171 additions & 0 deletions lxd/storage/connectors/connector_nvme.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
package connectors

import (
"context"
"fmt"
"os"
"path/filepath"
"strings"

"github.com/canonical/lxd/lxd/util"
"github.com/canonical/lxd/shared"
)

var _ Connector = &connectorNVMe{}

type connectorNVMe struct {
common
}

// Type returns the type of the connector.
func (c *connectorNVMe) Type() string {
return TypeNVME
}

// Version returns the version of the NVMe CLI.
func (c *connectorNVMe) Version() (string, error) {
// Detect and record the version of the NVMe CLI.
out, err := shared.RunCommand("nvme", "version")
if err != nil {
return "", fmt.Errorf("Failed to get nvme-cli version: %w", err)
}

fields := strings.Split(strings.TrimSpace(out), " ")
if strings.HasPrefix(out, "nvme version ") && len(fields) > 2 {
return fmt.Sprintf("%s (nvme-cli)", fields[2]), nil
}

return "", fmt.Errorf("Failed to get nvme-cli version: Unexpected output %q", out)
}

// LoadModules loads the NVMe/TCP kernel modules.
// Returns true if the modules can be loaded.
func (c *connectorNVMe) LoadModules() bool {
err := util.LoadModule("nvme_fabrics")
if err != nil {
return false
}

err = util.LoadModule("nvme_tcp")
return err == nil
}

// QualifiedName returns a custom NQN generated from the server UUID.
// Getting the NQN from /etc/nvme/hostnqn would require the nvme-cli
// package to be installed on the host.
func (c *connectorNVMe) QualifiedName() (string, error) {
return fmt.Sprintf("nqn.2014-08.org.nvmexpress:uuid:%s", c.serverUUID), nil
}

// SessionID returns the target's qualified name (NQN) if a corresponding
// session is found. Otherwise, an empty string is returned.
func (c *connectorNVMe) SessionID(targetQN string) (string, error) {
// Base path for NVMe sessions/subsystems.
basePath := "/sys/devices/virtual/nvme-subsystem"

// Retrieve list of existing NVMe sessions on this host.
directories, err := os.ReadDir(basePath)
if err != nil {
if os.IsNotExist(err) {
// No active sessions because NVMe subsystems directory does not exist.
return "", nil
}

return "", fmt.Errorf("Failed getting a list of existing NVMe subsystems: %w", err)
}

for _, directory := range directories {
subsystemName := directory.Name()

// Get the target NQN.
nqnBytes, err := os.ReadFile(filepath.Join(basePath, subsystemName, "subsysnqn"))
if err != nil {
return "", fmt.Errorf("Failed getting the target NQN for subystem %q: %w", subsystemName, err)
}

if strings.Contains(string(nqnBytes), targetQN) {
// Already connected.
return targetQN, nil
}
}

return "", nil
}

// Connect establishes a connection with the target on the given address.
func (c *connectorNVMe) Connect(ctx context.Context, targetAddr string, targetQN string) error {
hostNQN, err := c.QualifiedName()
if err != nil {
return err
}

// Try to find an existing NVMe session.
targetNQN, err := c.SessionID(targetQN)
if err != nil {
return err
}

if targetNQN != "" {
// Already connected.
return nil
}

_, stderr, err := shared.RunCommandSplit(ctx, nil, nil, "nvme", "connect", "--transport", "tcp", "--traddr", targetAddr, "--nqn", targetQN, "--hostnqn", hostNQN, "--hostid", c.serverUUID)
if err != nil {
return fmt.Errorf("Failed to connect to target %q on %q via NVMe: %w", targetQN, targetAddr, err)
}

if stderr != "" {
return fmt.Errorf("Failed to connect to target %q on %q via NVMe: %s", targetQN, targetAddr, stderr)
}

return nil
}

// ConnectAll establishes a connection with all targets available on the given address.
func (c *connectorNVMe) ConnectAll(ctx context.Context, targetAddr string) error {
MusicDin marked this conversation as resolved.
Show resolved Hide resolved
hostNQN, err := c.QualifiedName()
if err != nil {
return err
}

_, stderr, err := shared.RunCommandSplit(ctx, nil, nil, "nvme", "connect-all", "--transport", "tcp", "--traddr", targetAddr, "--hostnqn", hostNQN, "--hostid", c.serverUUID)
if err != nil {
return fmt.Errorf("Failed to connect to any target on %q via NVMe: %w", targetAddr, err)
}

if stderr != "" {
return fmt.Errorf("Failed to connect to any target on %q via NVMe: %s", targetAddr, stderr)
}

return nil
}

// Disconnect terminates a connection with the target.
func (c *connectorNVMe) Disconnect(targetQN string) error {
// Find an existing NVMe session.
targetNQN, err := c.SessionID(targetQN)
if err != nil {
return err
}

// Disconnect from the NVMe target if there is an existing session.
if targetNQN != "" {
_, err := shared.RunCommand("nvme", "disconnect", "--nqn", targetNQN)
if err != nil {
return fmt.Errorf("Failed disconnecting from NVMe target %q: %w", targetNQN, err)
}
}

return nil
}

// DisconnectAll terminates all connections with all targets.
func (c *connectorNVMe) DisconnectAll() error {
_, err := shared.RunCommand("nvme", "disconnect-all")
if err != nil {
return fmt.Errorf("Failed disconnecting from NVMe targets: %w", err)
}

return nil
}
53 changes: 53 additions & 0 deletions lxd/storage/connectors/connector_sdc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package connectors

import (
"context"
)

var _ Connector = &connectorSDC{}

type connectorSDC struct {
common
}

// Type returns the type of the connector.
func (c *connectorSDC) Type() string {
return TypeSDC
}

// LoadModules returns true. SDC does not require any kernel modules to be loaded.
func (c *connectorSDC) LoadModules() bool {
return true
}

// QualifiedName returns an empty string and no error. SDC has no qualified name.
func (c *connectorSDC) QualifiedName() (string, error) {
return "", nil
}

// SessionID returns an empty string and no error, as connections are handled by SDC.
func (c *connectorSDC) SessionID(targetQN string) (string, error) {
return "", nil
}

// Connect does nothing. Connections are fully handled by SDC.
func (c *connectorSDC) Connect(ctx context.Context, targetAddr string, targetQN string) error {
// Nothing to do. Connection is handled by Dell SDC.
return nil
}

// ConnectAll does nothing. Connections are fully handled by SDC.
func (c *connectorSDC) ConnectAll(ctx context.Context, targetAddr string) error {
// Nothing to do. Connection is handled by Dell SDC.
return nil
}

// Disconnect does nothing. Connections are fully handled by SDC.
func (c *connectorSDC) Disconnect(targetQN string) error {
return nil
}

// DisconnectAll does nothing. Connections are fully handled by SDC.
func (c *connectorSDC) DisconnectAll() error {
return nil
}
Loading
Loading