2 * Copyright (C) 2005 Anthony Liguori <anthony@codemonkey.ws>
4 * Network Block Device Client Side
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; under version 2 of the License.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, see <http://www.gnu.org/licenses/>.
19 #include "qemu/osdep.h"
20 #include "qapi/error.h"
21 #include "nbd-internal.h"
23 static int nbd_errno_to_system_errno(int err
)
37 TRACE("Squashing unexpected error %d to EINVAL", err
);
44 /* Definitions for opaque data types */
46 static QTAILQ_HEAD(, NBDExport
) exports
= QTAILQ_HEAD_INITIALIZER(exports
);
48 /* That's all folks */
50 /* Basic flow for negotiation
78 /* If type represents success, return 1 without further action.
79 * If type represents an error reply, consume the rest of the packet on ioc.
80 * Then return 0 for unsupported (so the client can fall back to
81 * other approaches), or -1 with errp set for other errors.
83 static int nbd_handle_reply_err(QIOChannel
*ioc
, uint32_t opt
, uint32_t type
,
90 if (!(type
& (1 << 31))) {
94 if (read_sync(ioc
, &len
, sizeof(len
)) != sizeof(len
)) {
95 error_setg(errp
, "failed to read option length");
98 len
= be32_to_cpu(len
);
100 if (len
> NBD_MAX_BUFFER_SIZE
) {
101 error_setg(errp
, "server's error message is too long");
104 msg
= g_malloc(len
+ 1);
105 if (read_sync(ioc
, msg
, len
) != len
) {
106 error_setg(errp
, "failed to read option error message");
113 case NBD_REP_ERR_UNSUP
:
114 TRACE("server doesn't understand request %" PRIx32
115 ", attempting fallback", opt
);
119 case NBD_REP_ERR_POLICY
:
120 error_setg(errp
, "Denied by server for option %" PRIx32
, opt
);
123 case NBD_REP_ERR_INVALID
:
124 error_setg(errp
, "Invalid data length for option %" PRIx32
, opt
);
127 case NBD_REP_ERR_TLS_REQD
:
128 error_setg(errp
, "TLS negotiation required before option %" PRIx32
,
133 error_setg(errp
, "Unknown error code when asking for option %" PRIx32
,
139 error_append_hint(errp
, "%s\n", msg
);
147 static int nbd_receive_list(QIOChannel
*ioc
, char **name
, Error
**errp
)
157 if (read_sync(ioc
, &magic
, sizeof(magic
)) != sizeof(magic
)) {
158 error_setg(errp
, "failed to read list option magic");
161 magic
= be64_to_cpu(magic
);
162 if (magic
!= NBD_REP_MAGIC
) {
163 error_setg(errp
, "Unexpected option list magic");
166 if (read_sync(ioc
, &opt
, sizeof(opt
)) != sizeof(opt
)) {
167 error_setg(errp
, "failed to read list option");
170 opt
= be32_to_cpu(opt
);
171 if (opt
!= NBD_OPT_LIST
) {
172 error_setg(errp
, "Unexpected option type %" PRIx32
" expected %x",
177 if (read_sync(ioc
, &type
, sizeof(type
)) != sizeof(type
)) {
178 error_setg(errp
, "failed to read list option type");
181 type
= be32_to_cpu(type
);
182 error
= nbd_handle_reply_err(ioc
, opt
, type
, errp
);
187 if (read_sync(ioc
, &len
, sizeof(len
)) != sizeof(len
)) {
188 error_setg(errp
, "failed to read option length");
191 len
= be32_to_cpu(len
);
193 if (type
== NBD_REP_ACK
) {
195 error_setg(errp
, "length too long for option end");
198 } else if (type
== NBD_REP_SERVER
) {
199 if (len
< sizeof(namelen
) || len
> NBD_MAX_BUFFER_SIZE
) {
200 error_setg(errp
, "incorrect option length");
203 if (read_sync(ioc
, &namelen
, sizeof(namelen
)) != sizeof(namelen
)) {
204 error_setg(errp
, "failed to read option name length");
207 namelen
= be32_to_cpu(namelen
);
208 len
-= sizeof(namelen
);
210 error_setg(errp
, "incorrect option name length");
213 if (namelen
> NBD_MAX_NAME_SIZE
) {
214 error_setg(errp
, "export name length too long %" PRIu32
, namelen
);
218 *name
= g_new0(char, namelen
+ 1);
219 if (read_sync(ioc
, *name
, namelen
) != namelen
) {
220 error_setg(errp
, "failed to read export name");
225 (*name
)[namelen
] = '\0';
228 char *buf
= g_malloc(len
+ 1);
229 if (read_sync(ioc
, buf
, len
) != len
) {
230 error_setg(errp
, "failed to read export description");
237 TRACE("Ignoring export description: %s", buf
);
241 error_setg(errp
, "Unexpected reply type %" PRIx32
" expected %x",
242 type
, NBD_REP_SERVER
);
249 static int nbd_receive_query_exports(QIOChannel
*ioc
,
250 const char *wantname
,
253 uint64_t magic
= cpu_to_be64(NBD_OPTS_MAGIC
);
254 uint32_t opt
= cpu_to_be32(NBD_OPT_LIST
);
256 bool foundExport
= false;
258 TRACE("Querying export list");
259 if (write_sync(ioc
, &magic
, sizeof(magic
)) != sizeof(magic
)) {
260 error_setg(errp
, "Failed to send list option magic");
264 if (write_sync(ioc
, &opt
, sizeof(opt
)) != sizeof(opt
)) {
265 error_setg(errp
, "Failed to send list option number");
269 if (write_sync(ioc
, &length
, sizeof(length
)) != sizeof(length
)) {
270 error_setg(errp
, "Failed to send list option length");
274 TRACE("Reading available export names");
277 int ret
= nbd_receive_list(ioc
, &name
, errp
);
285 /* Server doesn't support export listing, so
286 * we will just assume an export with our
287 * wanted name exists */
292 TRACE("End of export name list");
295 if (g_str_equal(name
, wantname
)) {
297 TRACE("Found desired export name '%s'", name
);
299 TRACE("Ignored export name '%s'", name
);
305 error_setg(errp
, "No export with name '%s' available", wantname
);
312 static QIOChannel
*nbd_receive_starttls(QIOChannel
*ioc
,
313 QCryptoTLSCreds
*tlscreds
,
314 const char *hostname
, Error
**errp
)
316 uint64_t magic
= cpu_to_be64(NBD_OPTS_MAGIC
);
317 uint32_t opt
= cpu_to_be32(NBD_OPT_STARTTLS
);
321 struct NBDTLSHandshakeData data
= { 0 };
323 TRACE("Requesting TLS from server");
324 if (write_sync(ioc
, &magic
, sizeof(magic
)) != sizeof(magic
)) {
325 error_setg(errp
, "Failed to send option magic");
329 if (write_sync(ioc
, &opt
, sizeof(opt
)) != sizeof(opt
)) {
330 error_setg(errp
, "Failed to send option number");
334 if (write_sync(ioc
, &length
, sizeof(length
)) != sizeof(length
)) {
335 error_setg(errp
, "Failed to send option length");
339 TRACE("Getting TLS reply from server1");
340 if (read_sync(ioc
, &magic
, sizeof(magic
)) != sizeof(magic
)) {
341 error_setg(errp
, "failed to read option magic");
344 magic
= be64_to_cpu(magic
);
345 if (magic
!= NBD_REP_MAGIC
) {
346 error_setg(errp
, "Unexpected option magic");
349 TRACE("Getting TLS reply from server2");
350 if (read_sync(ioc
, &opt
, sizeof(opt
)) != sizeof(opt
)) {
351 error_setg(errp
, "failed to read option");
354 opt
= be32_to_cpu(opt
);
355 if (opt
!= NBD_OPT_STARTTLS
) {
356 error_setg(errp
, "Unexpected option type %" PRIx32
" expected %x",
357 opt
, NBD_OPT_STARTTLS
);
361 TRACE("Getting TLS reply from server");
362 if (read_sync(ioc
, &type
, sizeof(type
)) != sizeof(type
)) {
363 error_setg(errp
, "failed to read option type");
366 type
= be32_to_cpu(type
);
367 if (type
!= NBD_REP_ACK
) {
368 error_setg(errp
, "Server rejected request to start TLS %" PRIx32
,
373 TRACE("Getting TLS reply from server");
374 if (read_sync(ioc
, &length
, sizeof(length
)) != sizeof(length
)) {
375 error_setg(errp
, "failed to read option length");
378 length
= be32_to_cpu(length
);
380 error_setg(errp
, "Start TLS response was not zero %" PRIu32
,
385 TRACE("TLS request approved, setting up TLS");
386 tioc
= qio_channel_tls_new_client(ioc
, tlscreds
, hostname
, errp
);
390 data
.loop
= g_main_loop_new(g_main_context_default(), FALSE
);
391 TRACE("Starting TLS handshake");
392 qio_channel_tls_handshake(tioc
,
397 if (!data
.complete
) {
398 g_main_loop_run(data
.loop
);
400 g_main_loop_unref(data
.loop
);
402 error_propagate(errp
, data
.error
);
403 object_unref(OBJECT(tioc
));
407 return QIO_CHANNEL(tioc
);
411 int nbd_receive_negotiate(QIOChannel
*ioc
, const char *name
, uint32_t *flags
,
412 QCryptoTLSCreds
*tlscreds
, const char *hostname
,
414 off_t
*size
, Error
**errp
)
420 TRACE("Receiving negotiation tlscreds=%p hostname=%s.",
421 tlscreds
, hostname
? hostname
: "<null>");
428 if (tlscreds
&& !outioc
) {
429 error_setg(errp
, "Output I/O channel required for TLS");
433 if (read_sync(ioc
, buf
, 8) != 8) {
434 error_setg(errp
, "Failed to read data");
439 if (strlen(buf
) == 0) {
440 error_setg(errp
, "Server connection closed unexpectedly");
444 TRACE("Magic is %c%c%c%c%c%c%c%c",
445 qemu_isprint(buf
[0]) ? buf
[0] : '.',
446 qemu_isprint(buf
[1]) ? buf
[1] : '.',
447 qemu_isprint(buf
[2]) ? buf
[2] : '.',
448 qemu_isprint(buf
[3]) ? buf
[3] : '.',
449 qemu_isprint(buf
[4]) ? buf
[4] : '.',
450 qemu_isprint(buf
[5]) ? buf
[5] : '.',
451 qemu_isprint(buf
[6]) ? buf
[6] : '.',
452 qemu_isprint(buf
[7]) ? buf
[7] : '.');
454 if (memcmp(buf
, "NBDMAGIC", 8) != 0) {
455 error_setg(errp
, "Invalid magic received");
459 if (read_sync(ioc
, &magic
, sizeof(magic
)) != sizeof(magic
)) {
460 error_setg(errp
, "Failed to read magic");
463 magic
= be64_to_cpu(magic
);
464 TRACE("Magic is 0x%" PRIx64
, magic
);
466 if (magic
== NBD_OPTS_MAGIC
) {
467 uint32_t clientflags
= 0;
470 uint16_t globalflags
;
471 uint16_t exportflags
;
472 bool fixedNewStyle
= false;
474 if (read_sync(ioc
, &globalflags
, sizeof(globalflags
)) !=
475 sizeof(globalflags
)) {
476 error_setg(errp
, "Failed to read server flags");
479 globalflags
= be16_to_cpu(globalflags
);
480 *flags
= globalflags
<< 16;
481 TRACE("Global flags are %" PRIx32
, globalflags
);
482 if (globalflags
& NBD_FLAG_FIXED_NEWSTYLE
) {
483 fixedNewStyle
= true;
484 TRACE("Server supports fixed new style");
485 clientflags
|= NBD_FLAG_C_FIXED_NEWSTYLE
;
487 /* client requested flags */
488 clientflags
= cpu_to_be32(clientflags
);
489 if (write_sync(ioc
, &clientflags
, sizeof(clientflags
)) !=
490 sizeof(clientflags
)) {
491 error_setg(errp
, "Failed to send clientflags field");
496 *outioc
= nbd_receive_starttls(ioc
, tlscreds
, hostname
, errp
);
502 error_setg(errp
, "Server does not support STARTTLS");
507 TRACE("Using default NBD export name \"\"");
511 /* Check our desired export is present in the
512 * server export list. Since NBD_OPT_EXPORT_NAME
513 * cannot return an error message, running this
514 * query gives us good error reporting if the
515 * server required TLS
517 if (nbd_receive_query_exports(ioc
, name
, errp
) < 0) {
521 /* write the export name */
522 magic
= cpu_to_be64(magic
);
523 if (write_sync(ioc
, &magic
, sizeof(magic
)) != sizeof(magic
)) {
524 error_setg(errp
, "Failed to send export name magic");
527 opt
= cpu_to_be32(NBD_OPT_EXPORT_NAME
);
528 if (write_sync(ioc
, &opt
, sizeof(opt
)) != sizeof(opt
)) {
529 error_setg(errp
, "Failed to send export name option number");
532 namesize
= cpu_to_be32(strlen(name
));
533 if (write_sync(ioc
, &namesize
, sizeof(namesize
)) !=
535 error_setg(errp
, "Failed to send export name length");
538 if (write_sync(ioc
, (char *)name
, strlen(name
)) != strlen(name
)) {
539 error_setg(errp
, "Failed to send export name");
543 if (read_sync(ioc
, &s
, sizeof(s
)) != sizeof(s
)) {
544 error_setg(errp
, "Failed to read export length");
547 *size
= be64_to_cpu(s
);
548 TRACE("Size is %" PRIu64
, *size
);
550 if (read_sync(ioc
, &exportflags
, sizeof(exportflags
)) !=
551 sizeof(exportflags
)) {
552 error_setg(errp
, "Failed to read export flags");
555 exportflags
= be16_to_cpu(exportflags
);
556 *flags
|= exportflags
;
557 TRACE("Export flags are %" PRIx16
, exportflags
);
558 } else if (magic
== NBD_CLIENT_MAGIC
) {
560 error_setg(errp
, "Server does not support export names");
564 error_setg(errp
, "Server does not support STARTTLS");
568 if (read_sync(ioc
, &s
, sizeof(s
)) != sizeof(s
)) {
569 error_setg(errp
, "Failed to read export length");
572 *size
= be64_to_cpu(s
);
573 TRACE("Size is %" PRIu64
, *size
);
575 if (read_sync(ioc
, flags
, sizeof(*flags
)) != sizeof(*flags
)) {
576 error_setg(errp
, "Failed to read export flags");
579 *flags
= be32_to_cpu(*flags
);
581 error_setg(errp
, "Bad magic received");
585 if (read_sync(ioc
, &buf
, 124) != 124) {
586 error_setg(errp
, "Failed to read reserved block");
596 int nbd_init(int fd
, QIOChannelSocket
*sioc
, uint32_t flags
, off_t size
)
598 unsigned long sectors
= size
/ BDRV_SECTOR_SIZE
;
599 if (size
/ BDRV_SECTOR_SIZE
!= sectors
) {
600 LOG("Export size %lld too large for 32-bit kernel", (long long) size
);
604 TRACE("Setting NBD socket");
606 if (ioctl(fd
, NBD_SET_SOCK
, (unsigned long) sioc
->fd
) < 0) {
608 LOG("Failed to set NBD socket");
612 TRACE("Setting block size to %lu", (unsigned long)BDRV_SECTOR_SIZE
);
614 if (ioctl(fd
, NBD_SET_BLKSIZE
, (unsigned long)BDRV_SECTOR_SIZE
) < 0) {
616 LOG("Failed setting NBD block size");
620 TRACE("Setting size to %lu block(s)", sectors
);
621 if (size
% BDRV_SECTOR_SIZE
) {
622 TRACE("Ignoring trailing %d bytes of export",
623 (int) (size
% BDRV_SECTOR_SIZE
));
626 if (ioctl(fd
, NBD_SET_SIZE_BLOCKS
, sectors
) < 0) {
628 LOG("Failed setting size (in blocks)");
632 if (ioctl(fd
, NBD_SET_FLAGS
, (unsigned long) flags
) < 0) {
633 if (errno
== ENOTTY
) {
634 int read_only
= (flags
& NBD_FLAG_READ_ONLY
) != 0;
635 TRACE("Setting readonly attribute");
637 if (ioctl(fd
, BLKROSET
, (unsigned long) &read_only
) < 0) {
639 LOG("Failed setting read-only attribute");
644 LOG("Failed setting flags");
649 TRACE("Negotiation ended");
654 int nbd_client(int fd
)
659 TRACE("Doing NBD loop");
661 ret
= ioctl(fd
, NBD_DO_IT
);
662 if (ret
< 0 && errno
== EPIPE
) {
663 /* NBD_DO_IT normally returns EPIPE when someone has disconnected
664 * the socket via NBD_DISCONNECT. We do not want to return 1 in
671 TRACE("NBD loop returned %d: %s", ret
, strerror(serrno
));
673 TRACE("Clearing NBD queue");
674 ioctl(fd
, NBD_CLEAR_QUE
);
676 TRACE("Clearing NBD socket");
677 ioctl(fd
, NBD_CLEAR_SOCK
);
683 int nbd_disconnect(int fd
)
685 ioctl(fd
, NBD_CLEAR_QUE
);
686 ioctl(fd
, NBD_DISCONNECT
);
687 ioctl(fd
, NBD_CLEAR_SOCK
);
692 int nbd_init(int fd
, QIOChannelSocket
*ioc
, uint32_t flags
, off_t size
)
697 int nbd_client(int fd
)
701 int nbd_disconnect(int fd
)
707 ssize_t
nbd_send_request(QIOChannel
*ioc
, struct nbd_request
*request
)
709 uint8_t buf
[NBD_REQUEST_SIZE
];
712 TRACE("Sending request to server: "
713 "{ .from = %" PRIu64
", .len = %" PRIu32
", .handle = %" PRIu64
714 ", .type=%" PRIu32
" }",
715 request
->from
, request
->len
, request
->handle
, request
->type
);
717 stl_be_p(buf
, NBD_REQUEST_MAGIC
);
718 stl_be_p(buf
+ 4, request
->type
);
719 stq_be_p(buf
+ 8, request
->handle
);
720 stq_be_p(buf
+ 16, request
->from
);
721 stl_be_p(buf
+ 24, request
->len
);
723 ret
= write_sync(ioc
, buf
, sizeof(buf
));
728 if (ret
!= sizeof(buf
)) {
729 LOG("writing to socket failed");
735 ssize_t
nbd_receive_reply(QIOChannel
*ioc
, struct nbd_reply
*reply
)
737 uint8_t buf
[NBD_REPLY_SIZE
];
741 ret
= read_sync(ioc
, buf
, sizeof(buf
));
746 if (ret
!= sizeof(buf
)) {
752 [ 0 .. 3] magic (NBD_REPLY_MAGIC)
753 [ 4 .. 7] error (0 == no error)
757 magic
= ldl_be_p(buf
);
758 reply
->error
= ldl_be_p(buf
+ 4);
759 reply
->handle
= ldq_be_p(buf
+ 8);
761 reply
->error
= nbd_errno_to_system_errno(reply
->error
);
763 TRACE("Got reply: { magic = 0x%" PRIx32
", .error = % " PRId32
764 ", handle = %" PRIu64
" }",
765 magic
, reply
->error
, reply
->handle
);
767 if (magic
!= NBD_REPLY_MAGIC
) {
768 LOG("invalid magic (got 0x%" PRIx32
")", magic
);