Index


NAME

Linux::NBD::Server - server (data provider) side of a network block device

SYNOPSIS

 use Linux::NBD::Server;

DESCRIPTION

You must subclass Linux::NBD::Server to get meaningful results. You should overwrite req_read and/or req_write. The default implementations just return EIO.

METHODS

$server = new Linux::NBD::Server socket => $fd, ...;

Create a new server. All arguments are put into a hashref which is blessed and returned. The only required argument is socket which should be connected to a Linux::NBD::Client.

$server->run

Enters a server loop, serving requests. Only returns when the socket is closed or a disconnect message is received.

If you need non-blocking access your best bet is to use $server-one_request>, but note that it still might block to read the write data from the socket.

$server->one_request

Waits for and handles a single request and returns, returning true if no further requests will arrive.

$server->format_reply ($handle[, $error, [, $data]])

Formats (and returns as a string) a reply message. If the request was a read request and $error is zero (or missing, both meaning no error), you should append the read data to it.

If $data is given and $error is zero, it is appended to the string before it is returned.

$server->reply ($handle[, $error[, $data]])

Formats and sends a reply to the server.

$server->send ($data)

Writes the given data to the client socket.

$server->recv ($bytes)

Reads the specified number of bytes and returns them.

$server->req_read ($handle, $offset, $length)

This callback is called for every read request. It should send back a reply and the requested number of bytes, e.g.:

  my ($self, $handle, $ofs, $len) = @_;
  $self->reply($handle, 0, "\xff" x $len);

$server->req_write ($handle, $offset, $length)

Same as req_read, but you should use recv to read $len bytes from the socket (even if you intend to return an error, of course).

  my ($self, $handle, $ofs, $len) = @_;
  my $data = $self->recv($len);
  $self->reply($handle);

NBD TOOL NEGOTIATION PROTOCOL

Standard NBD tools add a handshake phase before starting to serve/send requests. The server-side implementation looks like this:

    $size = new Math::BigInt $size;
    my $size_hi = $size->copy->brsft(32)->blsft(32);
    my $size_lo = $size->copy->bsub($size_hi)->numify;
    $size_hi = $size_hi->brsft(32)->numify;

    $s->send ("NBDMAGIC\x00\x00\x42\x02\x81\x86\x12\x53");
    $s->send (pack "NN", $size_hi, $size_lo);
    $s->send ("\x00" x 128);

    # now serve
    my $server = new ServerClass socket => $s;
    $server->run;

BUGS

Should support AnyEvent or something similar for non-blocking behaviour.

AUTHOR

 Marc Lehmann <schmorp@schmorp.de>
 http://home.schmorp.de/