nbd

package
v1.1.0-alpha-7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 11, 2017 License: Apache-2.0, MIT Imports: 30 Imported by: 0

Documentation

Index

Constants

View Source
const (
	EnvConfigFile = "_GONBDSERVER_CONFFILE"
	EnvPIDFile    = "_GONBDSERVER_PIDFILE"
)

Environment variables that can be used to overwrite some of the flags.

View Source
const (
	NBD_CMD_READ         = 0
	NBD_CMD_WRITE        = 1
	NBD_CMD_DISC         = 2
	NBD_CMD_FLUSH        = 3
	NBD_CMD_TRIM         = 4
	NBD_CMD_WRITE_ZEROES = 6
)

NBD commands

View Source
const (
	NBD_CMD_FLAG_FUA = uint16(1 << 0)
	NBD_CMD_MAY_TRIM = uint16(1 << 1)
	NBD_CMD_FLAG_DF  = uint16(1 << 2)
)

NBD command flags

View Source
const (
	NBD_FLAG_HAS_FLAGS         = uint16(1 << 0)
	NBD_FLAG_READ_ONLY         = uint16(1 << 1)
	NBD_FLAG_SEND_FLUSH        = uint16(1 << 2)
	NBD_FLAG_SEND_FUA          = uint16(1 << 3)
	NBD_FLAG_ROTATIONAL        = uint16(1 << 4)
	NBD_FLAG_SEND_TRIM         = uint16(1 << 5)
	NBD_FLAG_SEND_WRITE_ZEROES = uint16(1 << 6)
	NBD_FLAG_SEND_DF           = uint16(1 << 7)
	NBD_FLAG_SEND_CLOSE        = uint16(1 << 8)
)

NBD negotiation flags

View Source
const (
	NBD_MAGIC                  = 0x4e42444d41474943
	NBD_REQUEST_MAGIC          = 0x25609513
	NBD_REPLY_MAGIC            = 0x67446698
	NBD_CLISERV_MAGIC          = 0x00420281861253
	NBD_OPTS_MAGIC             = 0x49484156454F5054
	NBD_REP_MAGIC              = 0x3e889045565a9
	NBD_STRUCTURED_REPLY_MAGIC = 0x668e33ef
)

NBD magic numbers

View Source
const (
	NBD_OPT_EXPORT_NAME      = 1
	NBD_OPT_ABORT            = 2
	NBD_OPT_LIST             = 3
	NBD_OPT_PEEK_EXPORT      = 4
	NBD_OPT_STARTTLS         = 5
	NBD_OPT_INFO             = 6
	NBD_OPT_GO               = 7
	NBD_OPT_STRUCTURED_REPLY = 8
)

NBD options

View Source
const (
	NBD_REP_ACK                 = uint32(1)
	NBD_REP_SERVER              = uint32(2)
	NBD_REP_INFO                = uint32(3)
	NBD_REP_FLAG_ERROR          = uint32(1 << 31)
	NBD_REP_ERR_UNSUP           = uint32(1 | NBD_REP_FLAG_ERROR)
	NBD_REP_ERR_POLICY          = uint32(2 | NBD_REP_FLAG_ERROR)
	NBD_REP_ERR_INVALID         = uint32(3 | NBD_REP_FLAG_ERROR)
	NBD_REP_ERR_PLATFORM        = uint32(4 | NBD_REP_FLAG_ERROR)
	NBD_REP_ERR_TLS_REQD        = uint32(5 | NBD_REP_FLAG_ERROR)
	NBD_REP_ERR_UNKNOWN         = uint32(6 | NBD_REP_FLAG_ERROR)
	NBD_REP_ERR_SHUTDOWN        = uint32(7 | NBD_REP_FLAG_ERROR)
	NBD_REP_ERR_BLOCK_SIZE_REQD = uint32(8 | NBD_REP_FLAG_ERROR)
)

NBD option reply types

View Source
const (
	NBD_REPLY_TYPE_NONE         = 0
	NBD_REPLY_TYPE_ERROR        = 1
	NBD_REPLY_TYPE_ERROR_OFFSET = 2
	NBD_REPLY_TYPE_OFFSET_DATA  = 3
	NBD_REPLY_TYPE_OFFSET_HOLE  = 4
)

NBD reply types

View Source
const (
	NBD_FLAG_FIXED_NEWSTYLE = 1 << 0
	NBD_FLAG_NO_ZEROES      = 1 << 1
)

NBD hanshake flags

View Source
const (
	NBD_FLAG_C_FIXED_NEWSTYLE = 1 << 0
	NBD_FLAG_C_NO_ZEROES      = 1 << 1
)

NBD client flags

View Source
const (
	NBD_EPERM     = 1
	NBD_EIO       = 5
	NBD_ENOMEM    = 12
	NBD_EINVAL    = 22
	NBD_ENOSPC    = 28
	NBD_EOVERFLOW = 75
)

NBD errors

View Source
const (
	NBD_INFO_EXPORT      = 0
	NBD_INFO_NAME        = 1
	NBD_INFO_DESCRIPTION = 2
	NBD_INFO_BLOCK_SIZE  = 3
)

NBD info types

View Source
const (
	CMDT_CHECK_LENGTH_OFFSET     = 1 << iota // length and offset must be valid
	CMDT_REQ_PAYLOAD                         // request carries a payload
	CMDT_REQ_FAKE_PAYLOAD                    // request does not carry a payload, but we'll make a zero payload up
	CMDT_REP_PAYLOAD                         // reply carries a payload
	CMDT_CHECK_NOT_READ_ONLY                 // not valid on read-only media
	CMDT_SET_DISCONNECT_RECEIVED             // a disconnect - don't process any further commands
)

Our internal flags to characterize commands

View Source
const (
	NBD_DEFAULT_PORT = 10809
)

NBD default port

View Source
const (
	NBD_REPLY_FLAG_DONE = 1 << 0
)

NBD reply flags

Variables

CmdTypeMap is a map specifying each command

Functions

func ContainsBackend

func ContainsBackend(name string) bool

ContainsBackend allows you to check if a backend is already available, even though you can still overwrite it with `RegisterBackend` in case you want.

func GetBackendNames

func GetBackendNames() []string

GetBackendNames returns the names of all registered backends

func RegisterBackend

func RegisterBackend(name string, generator BackendGenerator)

RegisterBackend allows you to register a backend with a name, overwriting any existing backend for that name

func RegisterFlags

func RegisterFlags()

RegisterFlags registers all NBD-Specific flags

func Run

func Run(control *Control)

Run defines the entry point of this nbd module. It creates the server in the foreground or as a deamon. It will create the listeners and server based on the config and defaults. Once that's all up and running, the service is ready to receive and reply to NBD Requests.

func RunConfig

func RunConfig(control *Control)

RunConfig - this is effectively the main entry point of the program

We parse the config, then start each of the listeners, restarting them when we get SIGHUP, but being sure not to kill the sessions

func StartServer

func StartServer(parentCtx context.Context, sessionParentCtx context.Context, sessionWaitGroup *sync.WaitGroup, logger log.Logger, s ServerConfig)

StartServer starts a single server.

A parent context is given in which the listener runs, as well as a session context in which the sessions (connections) themselves run. This enables the sessions to be retained when the listener is cancelled on a SIGHUP

Types

type Backend

type Backend interface {
	WriteAt(ctx context.Context, b []byte, offset int64) (int64, error)     // write data to w at offset
	WriteZeroesAt(ctx context.Context, offset, length int64) (int64, error) // write zeroes to w at offset
	ReadAt(ctx context.Context, offset, length int64) ([]byte, error)       // read from o b at offset
	TrimAt(ctx context.Context, offset, length int64) (int64, error)        // trim
	Flush(ctx context.Context) error                                        // flush
	Close(ctx context.Context) error                                        // close
	Geometry(ctx context.Context) (Geometry, error)                         // size, minimum BS, preferred BS, maximum BS
	HasFua(ctx context.Context) bool                                        // does the driver support FUA?
	HasFlush(ctx context.Context) bool                                      // does the driver support flush?
	GoBackground(ctx context.Context)                                       // optional background thread
}

Backend is an interface implemented by the various backend drivers

func NewFileBackend

func NewFileBackend(ctx context.Context, ec *ExportConfig) (Backend, error)

NewFileBackend generates a new file backend

type BackendGenerator

type BackendGenerator func(ctx context.Context, e *ExportConfig) (Backend, error)

BackendGenerator is a generator function type that generates a backend

type Config

type Config struct {
	Servers []ServerConfig // array of server configs
	Logging LogConfig      // Configuration for logging
}

Config holds the config that applies to all servers (currently just logging), and an array of server configs

func ParseConfig

func ParseConfig() (*Config, error)

ParseConfig parses the YAML configuration provided

func (*Config) GetLogger

func (c *Config) GetLogger() (log.Logger, error)

GetLogger returns the configured logger linked to this config

type Connection

type Connection struct {
	// contains filtered or unexported fields
}

Connection holds the details for each connection

func NewConnection

func NewConnection(listener *Listener, logger log.Logger, conn net.Conn) (*Connection, error)

NewConnection returns a new Connection object

func (*Connection) Serve

func (c *Connection) Serve(parentCtx context.Context)

Serve the two phases of an NBD connection. The first phase is the Negotiation between Server and Client. The second phase is the transmition of data, replies based on requests.

type ConnectionParameters

type ConnectionParameters struct {
	ConnectionTimeout time.Duration // maximum time to complete negotiation
}

ConnectionParameters holds parameters for each inbound connection

type Control

type Control struct {
	// contains filtered or unexported fields
}

Control structure is used to sync an async event

type DeadlineListener

type DeadlineListener interface {
	SetDeadline(t time.Time) error
	net.Listener
}

DeadlineListener defines a listener type that does what we want

type DriverParametersConfig

type DriverParametersConfig map[string]string

DriverParametersConfig is an arbitrary map of other parameters in string format

type Export

type Export struct {
	// contains filtered or unexported fields
}

Export contains the details of an export

type ExportConfig

type ExportConfig struct {
	Name               string                 // name of the export
	Description        string                 // description of export
	Driver             string                 // name of the driver
	ReadOnly           bool                   // true of the export should be opened readonly
	TLSOnly            bool                   // true if the export should only be served over TLS
	MinimumBlockSize   uint64                 // minimum block size
	PreferredBlockSize uint64                 // preferred block size
	MaximumBlockSize   uint64                 // maximum block size
	DriverParameters   DriverParametersConfig `yaml:",inline"` // driver parameters. These are an arbitrary map. Inline means they go aside teh foregoing
}

ExportConfig holds the config for one exported item

type ExportConfigManager

type ExportConfigManager interface {
	// List Config Names that this manager has available,
	// none can be returned in case this manager
	// does not support such a feature.
	ListConfigNames() []string
	// GetConfig returns, if possible,
	// an export Config linked to a given name
	GetConfig(name string) (*ExportConfig, error)
}

ExportConfigManager is the interface, that allows you to dynamically list and generate export configs, which have priority over the static configs predefined on the server

type FileBackend

type FileBackend struct {
	// contains filtered or unexported fields
}

FileBackend implements Backend

func (*FileBackend) Close

func (fb *FileBackend) Close(ctx context.Context) error

Close implements Backend.Close

func (*FileBackend) Flush

func (fb *FileBackend) Flush(ctx context.Context) error

Flush implements Backend.Flush

func (*FileBackend) Geometry

func (fb *FileBackend) Geometry(ctx context.Context) (Geometry, error)

Geometry implements Backend.Geometry

func (*FileBackend) GoBackground

func (fb *FileBackend) GoBackground(ctx context.Context)

GoBackground implements Backend.GoBackground

func (*FileBackend) HasFlush

func (fb *FileBackend) HasFlush(ctx context.Context) bool

HasFlush implements Backend.HasFlush

func (*FileBackend) HasFua

func (fb *FileBackend) HasFua(ctx context.Context) bool

HasFua implements Backend.HasFua

func (*FileBackend) ReadAt

func (fb *FileBackend) ReadAt(ctx context.Context, offset, length int64) ([]byte, error)

ReadAt implements Backend.ReadAt

func (*FileBackend) TrimAt

func (fb *FileBackend) TrimAt(ctx context.Context, offset, length int64) (int64, error)

TrimAt implements Backend.TrimAt

func (*FileBackend) WriteAt

func (fb *FileBackend) WriteAt(ctx context.Context, b []byte, offset int64) (int64, error)

WriteAt implements Backend.WriteAt

func (*FileBackend) WriteZeroesAt

func (fb *FileBackend) WriteZeroesAt(ctx context.Context, offset, length int64) (int64, error)

WriteZeroesAt implements Backend.WriteZeroesAt

type Geometry

type Geometry struct {
	Size               uint64
	MinimumBlockSize   uint64
	PreferredBlockSize uint64
	MaximumBlockSize   uint64
}

Geometry information for a backend

type Listener

type Listener struct {
	// contains filtered or unexported fields
}

Listener defines a single listener on a given net.Conn address

func NewListener

func NewListener(logger log.Logger, s ServerConfig) (*Listener, error)

NewListener returns a new listener object

func (*Listener) GetExportConfig

func (l *Listener) GetExportConfig(name string) (cfg *ExportConfig, err error)

GetExportConfig returns a config based on a given name. If the ExportConfigGenerator is set and it can return a config, using the given name, that config will be returned. Otherwise it will try to find the config in the statically defined list. If it can't find it in that list either, or the static list is empty, an error will be returned.

func (*Listener) ListExportConfigNames

func (l *Listener) ListExportConfigNames() (names []string)

ListExportConfigNames returns a list of available exportNames. NOTE: the returned list might not be complete,

as it is possible that a export config manager is being used,
that does not support the listing of available export config names.

func (*Listener) Listen

func (l *Listener) Listen(parentCtx context.Context, sessionParentCtx context.Context, sessionWaitGroup *sync.WaitGroup)

Listen listens on an given address for incoming connections

When sessions come in they are started on a separate context (sessionParentCtx), so that the listener can be killed without killing the sessions

func (*Listener) SetExportConfigManager

func (l *Listener) SetExportConfigManager(m ExportConfigManager)

SetExportConfigManager sets the manager used to dynamically, manage export configs, which has priority over the statically defined export configs.

type LogConfig

type LogConfig struct {
	File           string // a file to log to
	FileMode       string // file mode
	SyslogFacility string // a syslog facility name - set to enable syslog
	Debug          bool   // log debug statements
}

LogConfig specifies configuration for logging

type Reply

type Reply struct {
	// contains filtered or unexported fields
}

Reply is an internal structure for propagating replies onto the reply goroutine to be sent from there

type ServerConfig

type ServerConfig struct {
	Protocol        string         // protocol it should listen on (in net.Conn form)
	Address         string         // address to listen on
	DefaultExport   string         // name of default export
	Exports         []ExportConfig // array of configurations of exported items
	TLS             TLSConfig      // TLS configuration
	DisableNoZeroes bool           // Disable NoZereos extension
}

ServerConfig holds the config that applies to each server (i.e. listener)

type SyslogWriter

type SyslogWriter struct {
	// contains filtered or unexported fields
}

SyslogWriter is a WriterCloser that logs to syslog with an extracted priority

func NewSyslogWriter

func NewSyslogWriter(facility string) (*SyslogWriter, error)

NewSyslogWriter creates a new syslog writer

func (*SyslogWriter) Close

func (s *SyslogWriter) Close() error

Close the channel

func (*SyslogWriter) Write

func (s *SyslogWriter) Write(p []byte) (n int, err error)

Write to the syslog, removing the prefix and setting the appropriate level

type TLSConfig

type TLSConfig struct {
	KeyFile    string // path to TLS key file
	CertFile   string // path to TLS cert file
	ServerName string // server name
	CaCertFile string // path to certificate file
	ClientAuth string // client authentication strategy
	MinVersion string // minimum TLS version
	MaxVersion string // maximum TLS version
}

TLSConfig has the configuration for TLS

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL