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
)
42 /* Definitions for opaque data types */
44 static QTAILQ_HEAD(, NBDExport
) exports
= QTAILQ_HEAD_INITIALIZER(exports
);
46 /* That's all folks */
48 /* Basic flow for negotiation
76 /* If type represents success, return 1 without further action.
77 * If type represents an error reply, consume the rest of the packet on ioc.
78 * Then return 0 for unsupported (so the client can fall back to
79 * other approaches), or -1 with errp set for other errors.
81 static int nbd_handle_reply_err(QIOChannel
*ioc
, uint32_t opt
, uint32_t type
,
88 if (!(type
& (1 << 31))) {
92 if (read_sync(ioc
, &len
, sizeof(len
)) != sizeof(len
)) {
93 error_setg(errp
, "failed to read option length");
96 len
= be32_to_cpu(len
);
98 if (len
> NBD_MAX_BUFFER_SIZE
) {
99 error_setg(errp
, "server's error message is too long");
102 msg
= g_malloc(len
+ 1);
103 if (read_sync(ioc
, msg
, len
) != len
) {
104 error_setg(errp
, "failed to read option error message");
111 case NBD_REP_ERR_UNSUP
:
112 TRACE("server doesn't understand request %" PRIx32
113 ", attempting fallback", opt
);
117 case NBD_REP_ERR_POLICY
:
118 error_setg(errp
, "Denied by server for option %" PRIx32
, opt
);
121 case NBD_REP_ERR_INVALID
:
122 error_setg(errp
, "Invalid data length for option %" PRIx32
, opt
);
125 case NBD_REP_ERR_TLS_REQD
:
126 error_setg(errp
, "TLS negotiation required before option %" PRIx32
,
131 error_setg(errp
, "Unknown error code when asking for option %" PRIx32
,
137 error_append_hint(errp
, "%s\n", msg
);
145 static int nbd_receive_list(QIOChannel
*ioc
, char **name
, Error
**errp
)
155 if (read_sync(ioc
, &magic
, sizeof(magic
)) != sizeof(magic
)) {
156 error_setg(errp
, "failed to read list option magic");
159 magic
= be64_to_cpu(magic
);
160 if (magic
!= NBD_REP_MAGIC
) {
161 error_setg(errp
, "Unexpected option list magic");
164 if (read_sync(ioc
, &opt
, sizeof(opt
)) != sizeof(opt
)) {
165 error_setg(errp
, "failed to read list option");
168 opt
= be32_to_cpu(opt
);
169 if (opt
!= NBD_OPT_LIST
) {
170 error_setg(errp
, "Unexpected option type %" PRIx32
" expected %x",
175 if (read_sync(ioc
, &type
, sizeof(type
)) != sizeof(type
)) {
176 error_setg(errp
, "failed to read list option type");
179 type
= be32_to_cpu(type
);
180 error
= nbd_handle_reply_err(ioc
, opt
, type
, errp
);
185 if (read_sync(ioc
, &len
, sizeof(len
)) != sizeof(len
)) {
186 error_setg(errp
, "failed to read option length");
189 len
= be32_to_cpu(len
);
191 if (type
== NBD_REP_ACK
) {
193 error_setg(errp
, "length too long for option end");
196 } else if (type
== NBD_REP_SERVER
) {
197 if (len
< sizeof(namelen
) || len
> NBD_MAX_BUFFER_SIZE
) {
198 error_setg(errp
, "incorrect option length");
201 if (read_sync(ioc
, &namelen
, sizeof(namelen
)) != sizeof(namelen
)) {
202 error_setg(errp
, "failed to read option name length");
205 namelen
= be32_to_cpu(namelen
);
206 len
-= sizeof(namelen
);
208 error_setg(errp
, "incorrect option name length");
212 error_setg(errp
, "export name length too long %" PRIu32
, namelen
);
216 *name
= g_new0(char, namelen
+ 1);
217 if (read_sync(ioc
, *name
, namelen
) != namelen
) {
218 error_setg(errp
, "failed to read export name");
223 (*name
)[namelen
] = '\0';
226 char *buf
= g_malloc(len
+ 1);
227 if (read_sync(ioc
, buf
, len
) != len
) {
228 error_setg(errp
, "failed to read export description");
235 TRACE("Ignoring export description: %s", buf
);
239 error_setg(errp
, "Unexpected reply type %" PRIx32
" expected %x",
240 type
, NBD_REP_SERVER
);
247 static int nbd_receive_query_exports(QIOChannel
*ioc
,
248 const char *wantname
,
251 uint64_t magic
= cpu_to_be64(NBD_OPTS_MAGIC
);
252 uint32_t opt
= cpu_to_be32(NBD_OPT_LIST
);
254 bool foundExport
= false;
256 TRACE("Querying export list");
257 if (write_sync(ioc
, &magic
, sizeof(magic
)) != sizeof(magic
)) {
258 error_setg(errp
, "Failed to send list option magic");
262 if (write_sync(ioc
, &opt
, sizeof(opt
)) != sizeof(opt
)) {
263 error_setg(errp
, "Failed to send list option number");
267 if (write_sync(ioc
, &length
, sizeof(length
)) != sizeof(length
)) {
268 error_setg(errp
, "Failed to send list option length");
272 TRACE("Reading available export names");
275 int ret
= nbd_receive_list(ioc
, &name
, errp
);
283 /* Server doesn't support export listing, so
284 * we will just assume an export with our
285 * wanted name exists */
290 TRACE("End of export name list");
293 if (g_str_equal(name
, wantname
)) {
295 TRACE("Found desired export name '%s'", name
);
297 TRACE("Ignored export name '%s'", name
);
303 error_setg(errp
, "No export with name '%s' available", wantname
);
310 static QIOChannel
*nbd_receive_starttls(QIOChannel
*ioc
,
311 QCryptoTLSCreds
*tlscreds
,
312 const char *hostname
, Error
**errp
)
314 uint64_t magic
= cpu_to_be64(NBD_OPTS_MAGIC
);
315 uint32_t opt
= cpu_to_be32(NBD_OPT_STARTTLS
);
319 struct NBDTLSHandshakeData data
= { 0 };
321 TRACE("Requesting TLS from server");
322 if (write_sync(ioc
, &magic
, sizeof(magic
)) != sizeof(magic
)) {
323 error_setg(errp
, "Failed to send option magic");
327 if (write_sync(ioc
, &opt
, sizeof(opt
)) != sizeof(opt
)) {
328 error_setg(errp
, "Failed to send option number");
332 if (write_sync(ioc
, &length
, sizeof(length
)) != sizeof(length
)) {
333 error_setg(errp
, "Failed to send option length");
337 TRACE("Getting TLS reply from server1");
338 if (read_sync(ioc
, &magic
, sizeof(magic
)) != sizeof(magic
)) {
339 error_setg(errp
, "failed to read option magic");
342 magic
= be64_to_cpu(magic
);
343 if (magic
!= NBD_REP_MAGIC
) {
344 error_setg(errp
, "Unexpected option magic");
347 TRACE("Getting TLS reply from server2");
348 if (read_sync(ioc
, &opt
, sizeof(opt
)) != sizeof(opt
)) {
349 error_setg(errp
, "failed to read option");
352 opt
= be32_to_cpu(opt
);
353 if (opt
!= NBD_OPT_STARTTLS
) {
354 error_setg(errp
, "Unexpected option type %" PRIx32
" expected %x",
355 opt
, NBD_OPT_STARTTLS
);
359 TRACE("Getting TLS reply from server");
360 if (read_sync(ioc
, &type
, sizeof(type
)) != sizeof(type
)) {
361 error_setg(errp
, "failed to read option type");
364 type
= be32_to_cpu(type
);
365 if (type
!= NBD_REP_ACK
) {
366 error_setg(errp
, "Server rejected request to start TLS %" PRIx32
,
371 TRACE("Getting TLS reply from server");
372 if (read_sync(ioc
, &length
, sizeof(length
)) != sizeof(length
)) {
373 error_setg(errp
, "failed to read option length");
376 length
= be32_to_cpu(length
);
378 error_setg(errp
, "Start TLS response was not zero %" PRIu32
,
383 TRACE("TLS request approved, setting up TLS");
384 tioc
= qio_channel_tls_new_client(ioc
, tlscreds
, hostname
, errp
);
388 data
.loop
= g_main_loop_new(g_main_context_default(), FALSE
);
389 TRACE("Starting TLS handshake");
390 qio_channel_tls_handshake(tioc
,
395 if (!data
.complete
) {
396 g_main_loop_run(data
.loop
);
398 g_main_loop_unref(data
.loop
);
400 error_propagate(errp
, data
.error
);
401 object_unref(OBJECT(tioc
));
405 return QIO_CHANNEL(tioc
);
409 int nbd_receive_negotiate(QIOChannel
*ioc
, const char *name
, uint32_t *flags
,
410 QCryptoTLSCreds
*tlscreds
, const char *hostname
,
412 off_t
*size
, Error
**errp
)
418 TRACE("Receiving negotiation tlscreds=%p hostname=%s.",
419 tlscreds
, hostname
? hostname
: "<null>");
426 if (tlscreds
&& !outioc
) {
427 error_setg(errp
, "Output I/O channel required for TLS");
431 if (read_sync(ioc
, buf
, 8) != 8) {
432 error_setg(errp
, "Failed to read data");
437 if (strlen(buf
) == 0) {
438 error_setg(errp
, "Server connection closed unexpectedly");
442 TRACE("Magic is %c%c%c%c%c%c%c%c",
443 qemu_isprint(buf
[0]) ? buf
[0] : '.',
444 qemu_isprint(buf
[1]) ? buf
[1] : '.',
445 qemu_isprint(buf
[2]) ? buf
[2] : '.',
446 qemu_isprint(buf
[3]) ? buf
[3] : '.',
447 qemu_isprint(buf
[4]) ? buf
[4] : '.',
448 qemu_isprint(buf
[5]) ? buf
[5] : '.',
449 qemu_isprint(buf
[6]) ? buf
[6] : '.',
450 qemu_isprint(buf
[7]) ? buf
[7] : '.');
452 if (memcmp(buf
, "NBDMAGIC", 8) != 0) {
453 error_setg(errp
, "Invalid magic received");
457 if (read_sync(ioc
, &magic
, sizeof(magic
)) != sizeof(magic
)) {
458 error_setg(errp
, "Failed to read magic");
461 magic
= be64_to_cpu(magic
);
462 TRACE("Magic is 0x%" PRIx64
, magic
);
464 if (magic
== NBD_OPTS_MAGIC
) {
465 uint32_t clientflags
= 0;
468 uint16_t globalflags
;
469 uint16_t exportflags
;
470 bool fixedNewStyle
= false;
472 if (read_sync(ioc
, &globalflags
, sizeof(globalflags
)) !=
473 sizeof(globalflags
)) {
474 error_setg(errp
, "Failed to read server flags");
477 globalflags
= be16_to_cpu(globalflags
);
478 *flags
= globalflags
<< 16;
479 TRACE("Global flags are %" PRIx32
, globalflags
);
480 if (globalflags
& NBD_FLAG_FIXED_NEWSTYLE
) {
481 fixedNewStyle
= true;
482 TRACE("Server supports fixed new style");
483 clientflags
|= NBD_FLAG_C_FIXED_NEWSTYLE
;
485 /* client requested flags */
486 clientflags
= cpu_to_be32(clientflags
);
487 if (write_sync(ioc
, &clientflags
, sizeof(clientflags
)) !=
488 sizeof(clientflags
)) {
489 error_setg(errp
, "Failed to send clientflags field");
494 *outioc
= nbd_receive_starttls(ioc
, tlscreds
, hostname
, errp
);
500 error_setg(errp
, "Server does not support STARTTLS");
505 TRACE("Using default NBD export name \"\"");
509 /* Check our desired export is present in the
510 * server export list. Since NBD_OPT_EXPORT_NAME
511 * cannot return an error message, running this
512 * query gives us good error reporting if the
513 * server required TLS
515 if (nbd_receive_query_exports(ioc
, name
, errp
) < 0) {
519 /* write the export name */
520 magic
= cpu_to_be64(magic
);
521 if (write_sync(ioc
, &magic
, sizeof(magic
)) != sizeof(magic
)) {
522 error_setg(errp
, "Failed to send export name magic");
525 opt
= cpu_to_be32(NBD_OPT_EXPORT_NAME
);
526 if (write_sync(ioc
, &opt
, sizeof(opt
)) != sizeof(opt
)) {
527 error_setg(errp
, "Failed to send export name option number");
530 namesize
= cpu_to_be32(strlen(name
));
531 if (write_sync(ioc
, &namesize
, sizeof(namesize
)) !=
533 error_setg(errp
, "Failed to send export name length");
536 if (write_sync(ioc
, (char *)name
, strlen(name
)) != strlen(name
)) {
537 error_setg(errp
, "Failed to send export name");
541 if (read_sync(ioc
, &s
, sizeof(s
)) != sizeof(s
)) {
542 error_setg(errp
, "Failed to read export length");
545 *size
= be64_to_cpu(s
);
546 TRACE("Size is %" PRIu64
, *size
);
548 if (read_sync(ioc
, &exportflags
, sizeof(exportflags
)) !=
549 sizeof(exportflags
)) {
550 error_setg(errp
, "Failed to read export flags");
553 exportflags
= be16_to_cpu(exportflags
);
554 *flags
|= exportflags
;
555 TRACE("Export flags are %" PRIx16
, exportflags
);
556 } else if (magic
== NBD_CLIENT_MAGIC
) {
558 error_setg(errp
, "Server does not support export names");
562 error_setg(errp
, "Server does not support STARTTLS");
566 if (read_sync(ioc
, &s
, sizeof(s
)) != sizeof(s
)) {
567 error_setg(errp
, "Failed to read export length");
570 *size
= be64_to_cpu(s
);
571 TRACE("Size is %" PRIu64
, *size
);
573 if (read_sync(ioc
, flags
, sizeof(*flags
)) != sizeof(*flags
)) {
574 error_setg(errp
, "Failed to read export flags");
577 *flags
= be32_to_cpu(*flags
);
579 error_setg(errp
, "Bad magic received");
583 if (read_sync(ioc
, &buf
, 124) != 124) {
584 error_setg(errp
, "Failed to read reserved block");
594 int nbd_init(int fd
, QIOChannelSocket
*sioc
, uint32_t flags
, off_t size
)
596 TRACE("Setting NBD socket");
598 if (ioctl(fd
, NBD_SET_SOCK
, sioc
->fd
) < 0) {
600 LOG("Failed to set NBD socket");
604 TRACE("Setting block size to %lu", (unsigned long)BDRV_SECTOR_SIZE
);
606 if (ioctl(fd
, NBD_SET_BLKSIZE
, (size_t)BDRV_SECTOR_SIZE
) < 0) {
608 LOG("Failed setting NBD block size");
612 TRACE("Setting size to %zd block(s)", (size_t)(size
/ BDRV_SECTOR_SIZE
));
614 if (ioctl(fd
, NBD_SET_SIZE_BLOCKS
, (size_t)(size
/ BDRV_SECTOR_SIZE
)) < 0) {
616 LOG("Failed setting size (in blocks)");
620 if (ioctl(fd
, NBD_SET_FLAGS
, flags
) < 0) {
621 if (errno
== ENOTTY
) {
622 int read_only
= (flags
& NBD_FLAG_READ_ONLY
) != 0;
623 TRACE("Setting readonly attribute");
625 if (ioctl(fd
, BLKROSET
, (unsigned long) &read_only
) < 0) {
627 LOG("Failed setting read-only attribute");
632 LOG("Failed setting flags");
637 TRACE("Negotiation ended");
642 int nbd_client(int fd
)
647 TRACE("Doing NBD loop");
649 ret
= ioctl(fd
, NBD_DO_IT
);
650 if (ret
< 0 && errno
== EPIPE
) {
651 /* NBD_DO_IT normally returns EPIPE when someone has disconnected
652 * the socket via NBD_DISCONNECT. We do not want to return 1 in
659 TRACE("NBD loop returned %d: %s", ret
, strerror(serrno
));
661 TRACE("Clearing NBD queue");
662 ioctl(fd
, NBD_CLEAR_QUE
);
664 TRACE("Clearing NBD socket");
665 ioctl(fd
, NBD_CLEAR_SOCK
);
671 int nbd_init(int fd
, QIOChannelSocket
*ioc
, uint32_t flags
, off_t size
)
676 int nbd_client(int fd
)
682 ssize_t
nbd_send_request(QIOChannel
*ioc
, struct nbd_request
*request
)
684 uint8_t buf
[NBD_REQUEST_SIZE
];
687 TRACE("Sending request to server: "
688 "{ .from = %" PRIu64
", .len = %" PRIu32
", .handle = %" PRIu64
689 ", .type=%" PRIu16
" }",
690 request
->from
, request
->len
, request
->handle
, request
->type
);
692 stl_be_p(buf
, NBD_REQUEST_MAGIC
);
693 stl_be_p(buf
+ 4, request
->type
);
694 stq_be_p(buf
+ 8, request
->handle
);
695 stq_be_p(buf
+ 16, request
->from
);
696 stl_be_p(buf
+ 24, request
->len
);
698 ret
= write_sync(ioc
, buf
, sizeof(buf
));
703 if (ret
!= sizeof(buf
)) {
704 LOG("writing to socket failed");
710 ssize_t
nbd_receive_reply(QIOChannel
*ioc
, struct nbd_reply
*reply
)
712 uint8_t buf
[NBD_REPLY_SIZE
];
716 ret
= read_sync(ioc
, buf
, sizeof(buf
));
721 if (ret
!= sizeof(buf
)) {
727 [ 0 .. 3] magic (NBD_REPLY_MAGIC)
728 [ 4 .. 7] error (0 == no error)
732 magic
= ldl_be_p(buf
);
733 reply
->error
= ldl_be_p(buf
+ 4);
734 reply
->handle
= ldq_be_p(buf
+ 8);
736 reply
->error
= nbd_errno_to_system_errno(reply
->error
);
738 TRACE("Got reply: { magic = 0x%" PRIx32
", .error = % " PRId32
739 ", handle = %" PRIu64
" }",
740 magic
, reply
->error
, reply
->handle
);
742 if (magic
!= NBD_REPLY_MAGIC
) {
743 LOG("invalid magic (got 0x%" PRIx32
")", magic
);