2 * Copyright (C) 2016 Red Hat, Inc.
3 * Copyright (C) 2005 Anthony Liguori <anthony@codemonkey.ws>
5 * Network Block Device Client Side
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; under version 2 of the License.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
20 #include "qemu/osdep.h"
21 #include "qapi/error.h"
23 #include "nbd-internal.h"
25 static int nbd_errno_to_system_errno(int err
)
48 trace_nbd_unknown_error(err
);
57 /* Definitions for opaque data types */
59 static QTAILQ_HEAD(, NBDExport
) exports
= QTAILQ_HEAD_INITIALIZER(exports
);
61 /* That's all folks */
63 /* Basic flow for negotiation
90 /* Send an option request.
92 * The request is for option @opt, with @data containing @len bytes of
93 * additional payload for the request (@len may be -1 to treat @data as
94 * a C string; and @data may be NULL if @len is 0).
95 * Return 0 if successful, -1 with errp set if it is impossible to
97 static int nbd_send_option_request(QIOChannel
*ioc
, uint32_t opt
,
98 uint32_t len
, const char *data
,
102 QEMU_BUILD_BUG_ON(sizeof(req
) != 16);
105 req
.length
= len
= strlen(data
);
107 trace_nbd_send_option_request(opt
, len
);
109 stq_be_p(&req
.magic
, NBD_OPTS_MAGIC
);
110 stl_be_p(&req
.option
, opt
);
111 stl_be_p(&req
.length
, len
);
113 if (nbd_write(ioc
, &req
, sizeof(req
), errp
) < 0) {
114 error_prepend(errp
, "Failed to send option request header");
118 if (len
&& nbd_write(ioc
, (char *) data
, len
, errp
) < 0) {
119 error_prepend(errp
, "Failed to send option request data");
126 /* Send NBD_OPT_ABORT as a courtesy to let the server know that we are
127 * not going to attempt further negotiation. */
128 static void nbd_send_opt_abort(QIOChannel
*ioc
)
130 /* Technically, a compliant server is supposed to reply to us; but
131 * older servers disconnected instead. At any rate, we're allowed
132 * to disconnect without waiting for the server reply, so we don't
133 * even care if the request makes it to the server, let alone
134 * waiting around for whether the server replies. */
135 nbd_send_option_request(ioc
, NBD_OPT_ABORT
, 0, NULL
, NULL
);
139 /* Receive the header of an option reply, which should match the given
140 * opt. Read through the length field, but NOT the length bytes of
141 * payload. Return 0 if successful, -1 with errp set if it is
142 * impossible to continue. */
143 static int nbd_receive_option_reply(QIOChannel
*ioc
, uint32_t opt
,
144 nbd_opt_reply
*reply
, Error
**errp
)
146 QEMU_BUILD_BUG_ON(sizeof(*reply
) != 20);
147 if (nbd_read(ioc
, reply
, sizeof(*reply
), errp
) < 0) {
148 error_prepend(errp
, "failed to read option reply");
149 nbd_send_opt_abort(ioc
);
152 be64_to_cpus(&reply
->magic
);
153 be32_to_cpus(&reply
->option
);
154 be32_to_cpus(&reply
->type
);
155 be32_to_cpus(&reply
->length
);
157 trace_nbd_receive_option_reply(reply
->option
, reply
->type
, reply
->length
);
159 if (reply
->magic
!= NBD_REP_MAGIC
) {
160 error_setg(errp
, "Unexpected option reply magic");
161 nbd_send_opt_abort(ioc
);
164 if (reply
->option
!= opt
) {
165 error_setg(errp
, "Unexpected option type %x expected %x",
167 nbd_send_opt_abort(ioc
);
173 /* If reply represents success, return 1 without further action.
174 * If reply represents an error, consume the optional payload of
175 * the packet on ioc. Then return 0 for unsupported (so the client
176 * can fall back to other approaches), or -1 with errp set for other
179 static int nbd_handle_reply_err(QIOChannel
*ioc
, nbd_opt_reply
*reply
,
185 if (!(reply
->type
& (1 << 31))) {
190 if (reply
->length
> NBD_MAX_BUFFER_SIZE
) {
191 error_setg(errp
, "server's error message is too long");
194 msg
= g_malloc(reply
->length
+ 1);
195 if (nbd_read(ioc
, msg
, reply
->length
, errp
) < 0) {
196 error_prepend(errp
, "failed to read option error message");
199 msg
[reply
->length
] = '\0';
202 switch (reply
->type
) {
203 case NBD_REP_ERR_UNSUP
:
204 trace_nbd_reply_err_unsup(reply
->option
);
208 case NBD_REP_ERR_POLICY
:
209 error_setg(errp
, "Denied by server for option %" PRIx32
,
213 case NBD_REP_ERR_INVALID
:
214 error_setg(errp
, "Invalid data length for option %" PRIx32
,
218 case NBD_REP_ERR_PLATFORM
:
219 error_setg(errp
, "Server lacks support for option %" PRIx32
,
223 case NBD_REP_ERR_TLS_REQD
:
224 error_setg(errp
, "TLS negotiation required before option %" PRIx32
,
228 case NBD_REP_ERR_SHUTDOWN
:
229 error_setg(errp
, "Server shutting down before option %" PRIx32
,
234 error_setg(errp
, "Unknown error code when asking for option %" PRIx32
,
240 error_append_hint(errp
, "%s\n", msg
);
246 nbd_send_opt_abort(ioc
);
251 /* Process another portion of the NBD_OPT_LIST reply. Set *@match if
252 * the current reply matches @want or if the server does not support
253 * NBD_OPT_LIST, otherwise leave @match alone. Return 0 if iteration
254 * is complete, positive if more replies are expected, or negative
255 * with @errp set if an unrecoverable error occurred. */
256 static int nbd_receive_list(QIOChannel
*ioc
, const char *want
, bool *match
,
262 char name
[NBD_MAX_NAME_SIZE
+ 1];
265 if (nbd_receive_option_reply(ioc
, NBD_OPT_LIST
, &reply
, errp
) < 0) {
268 error
= nbd_handle_reply_err(ioc
, &reply
, errp
);
270 /* The server did not support NBD_OPT_LIST, so set *match on
271 * the assumption that any name will be accepted. */
277 if (reply
.type
== NBD_REP_ACK
) {
279 error_setg(errp
, "length too long for option end");
280 nbd_send_opt_abort(ioc
);
284 } else if (reply
.type
!= NBD_REP_SERVER
) {
285 error_setg(errp
, "Unexpected reply type %" PRIx32
" expected %x",
286 reply
.type
, NBD_REP_SERVER
);
287 nbd_send_opt_abort(ioc
);
291 if (len
< sizeof(namelen
) || len
> NBD_MAX_BUFFER_SIZE
) {
292 error_setg(errp
, "incorrect option length %" PRIu32
, len
);
293 nbd_send_opt_abort(ioc
);
296 if (nbd_read(ioc
, &namelen
, sizeof(namelen
), errp
) < 0) {
297 error_prepend(errp
, "failed to read option name length");
298 nbd_send_opt_abort(ioc
);
301 namelen
= be32_to_cpu(namelen
);
302 len
-= sizeof(namelen
);
304 error_setg(errp
, "incorrect option name length");
305 nbd_send_opt_abort(ioc
);
308 if (namelen
!= strlen(want
)) {
309 if (nbd_drop(ioc
, len
, errp
) < 0) {
310 error_prepend(errp
, "failed to skip export name with wrong length");
311 nbd_send_opt_abort(ioc
);
317 assert(namelen
< sizeof(name
));
318 if (nbd_read(ioc
, name
, namelen
, errp
) < 0) {
319 error_prepend(errp
, "failed to read export name");
320 nbd_send_opt_abort(ioc
);
323 name
[namelen
] = '\0';
325 if (nbd_drop(ioc
, len
, errp
) < 0) {
326 error_prepend(errp
, "failed to read export description");
327 nbd_send_opt_abort(ioc
);
330 if (!strcmp(name
, want
)) {
337 /* Return -1 on failure, 0 if wantname is an available export. */
338 static int nbd_receive_query_exports(QIOChannel
*ioc
,
339 const char *wantname
,
342 bool foundExport
= false;
344 trace_nbd_receive_query_exports_start(wantname
);
345 if (nbd_send_option_request(ioc
, NBD_OPT_LIST
, 0, NULL
, errp
) < 0) {
350 int ret
= nbd_receive_list(ioc
, wantname
, &foundExport
, errp
);
353 /* Server gave unexpected reply */
355 } else if (ret
== 0) {
356 /* Done iterating. */
358 error_setg(errp
, "No export with name '%s' available",
360 nbd_send_opt_abort(ioc
);
363 trace_nbd_receive_query_exports_success(wantname
);
369 static QIOChannel
*nbd_receive_starttls(QIOChannel
*ioc
,
370 QCryptoTLSCreds
*tlscreds
,
371 const char *hostname
, Error
**errp
)
375 struct NBDTLSHandshakeData data
= { 0 };
377 trace_nbd_receive_starttls_request();
378 if (nbd_send_option_request(ioc
, NBD_OPT_STARTTLS
, 0, NULL
, errp
) < 0) {
382 trace_nbd_receive_starttls_reply();
383 if (nbd_receive_option_reply(ioc
, NBD_OPT_STARTTLS
, &reply
, errp
) < 0) {
387 if (reply
.type
!= NBD_REP_ACK
) {
388 error_setg(errp
, "Server rejected request to start TLS %" PRIx32
,
390 nbd_send_opt_abort(ioc
);
394 if (reply
.length
!= 0) {
395 error_setg(errp
, "Start TLS response was not zero %" PRIu32
,
397 nbd_send_opt_abort(ioc
);
401 trace_nbd_receive_starttls_new_client();
402 tioc
= qio_channel_tls_new_client(ioc
, tlscreds
, hostname
, errp
);
406 qio_channel_set_name(QIO_CHANNEL(tioc
), "nbd-client-tls");
407 data
.loop
= g_main_loop_new(g_main_context_default(), FALSE
);
408 trace_nbd_receive_starttls_tls_handshake();
409 qio_channel_tls_handshake(tioc
,
414 if (!data
.complete
) {
415 g_main_loop_run(data
.loop
);
417 g_main_loop_unref(data
.loop
);
419 error_propagate(errp
, data
.error
);
420 object_unref(OBJECT(tioc
));
424 return QIO_CHANNEL(tioc
);
428 int nbd_receive_negotiate(QIOChannel
*ioc
, const char *name
, uint16_t *flags
,
429 QCryptoTLSCreds
*tlscreds
, const char *hostname
,
431 off_t
*size
, Error
**errp
)
438 trace_nbd_receive_negotiate(tlscreds
, hostname
? hostname
: "<null>");
445 if (tlscreds
&& !outioc
) {
446 error_setg(errp
, "Output I/O channel required for TLS");
450 if (nbd_read(ioc
, buf
, 8, errp
) < 0) {
451 error_prepend(errp
, "Failed to read data");
456 if (strlen(buf
) == 0) {
457 error_setg(errp
, "Server connection closed unexpectedly");
461 magic
= ldq_be_p(buf
);
462 trace_nbd_receive_negotiate_magic(magic
);
464 if (memcmp(buf
, "NBDMAGIC", 8) != 0) {
465 error_setg(errp
, "Invalid magic received");
469 if (nbd_read(ioc
, &magic
, sizeof(magic
), errp
) < 0) {
470 error_prepend(errp
, "Failed to read magic");
473 magic
= be64_to_cpu(magic
);
474 trace_nbd_receive_negotiate_magic(magic
);
476 if (magic
== NBD_OPTS_MAGIC
) {
477 uint32_t clientflags
= 0;
478 uint16_t globalflags
;
479 bool fixedNewStyle
= false;
481 if (nbd_read(ioc
, &globalflags
, sizeof(globalflags
), errp
) < 0) {
482 error_prepend(errp
, "Failed to read server flags");
485 globalflags
= be16_to_cpu(globalflags
);
486 trace_nbd_receive_negotiate_server_flags(globalflags
);
487 if (globalflags
& NBD_FLAG_FIXED_NEWSTYLE
) {
488 fixedNewStyle
= true;
489 clientflags
|= NBD_FLAG_C_FIXED_NEWSTYLE
;
491 if (globalflags
& NBD_FLAG_NO_ZEROES
) {
493 clientflags
|= NBD_FLAG_C_NO_ZEROES
;
495 /* client requested flags */
496 clientflags
= cpu_to_be32(clientflags
);
497 if (nbd_write(ioc
, &clientflags
, sizeof(clientflags
), errp
) < 0) {
498 error_prepend(errp
, "Failed to send clientflags field");
503 *outioc
= nbd_receive_starttls(ioc
, tlscreds
, hostname
, errp
);
509 error_setg(errp
, "Server does not support STARTTLS");
514 trace_nbd_receive_negotiate_default_name();
518 /* Check our desired export is present in the
519 * server export list. Since NBD_OPT_EXPORT_NAME
520 * cannot return an error message, running this
521 * query gives us good error reporting if the
522 * server required TLS
524 if (nbd_receive_query_exports(ioc
, name
, errp
) < 0) {
528 /* write the export name request */
529 if (nbd_send_option_request(ioc
, NBD_OPT_EXPORT_NAME
, -1, name
,
534 /* Read the response */
535 if (nbd_read(ioc
, &s
, sizeof(s
), errp
) < 0) {
536 error_prepend(errp
, "Failed to read export length");
539 *size
= be64_to_cpu(s
);
541 if (nbd_read(ioc
, flags
, sizeof(*flags
), errp
) < 0) {
542 error_prepend(errp
, "Failed to read export flags");
546 } else if (magic
== NBD_CLIENT_MAGIC
) {
550 error_setg(errp
, "Server does not support export names");
554 error_setg(errp
, "Server does not support STARTTLS");
558 if (nbd_read(ioc
, &s
, sizeof(s
), errp
) < 0) {
559 error_prepend(errp
, "Failed to read export length");
562 *size
= be64_to_cpu(s
);
564 if (nbd_read(ioc
, &oldflags
, sizeof(oldflags
), errp
) < 0) {
565 error_prepend(errp
, "Failed to read export flags");
568 be32_to_cpus(&oldflags
);
569 if (oldflags
& ~0xffff) {
570 error_setg(errp
, "Unexpected export flags %0x" PRIx32
, oldflags
);
575 error_setg(errp
, "Bad magic received");
579 trace_nbd_receive_negotiate_size_flags(*size
, *flags
);
580 if (zeroes
&& nbd_drop(ioc
, 124, errp
) < 0) {
581 error_prepend(errp
, "Failed to read reserved block");
591 int nbd_init(int fd
, QIOChannelSocket
*sioc
, uint16_t flags
, off_t size
,
594 unsigned long sectors
= size
/ BDRV_SECTOR_SIZE
;
595 if (size
/ BDRV_SECTOR_SIZE
!= sectors
) {
596 error_setg(errp
, "Export size %lld too large for 32-bit kernel",
601 trace_nbd_init_set_socket();
603 if (ioctl(fd
, NBD_SET_SOCK
, (unsigned long) sioc
->fd
) < 0) {
605 error_setg(errp
, "Failed to set NBD socket");
609 trace_nbd_init_set_block_size(BDRV_SECTOR_SIZE
);
611 if (ioctl(fd
, NBD_SET_BLKSIZE
, (unsigned long)BDRV_SECTOR_SIZE
) < 0) {
613 error_setg(errp
, "Failed setting NBD block size");
617 trace_nbd_init_set_size(sectors
);
618 if (size
% BDRV_SECTOR_SIZE
) {
619 trace_nbd_init_trailing_bytes(size
% BDRV_SECTOR_SIZE
);
622 if (ioctl(fd
, NBD_SET_SIZE_BLOCKS
, sectors
) < 0) {
624 error_setg(errp
, "Failed setting size (in blocks)");
628 if (ioctl(fd
, NBD_SET_FLAGS
, (unsigned long) flags
) < 0) {
629 if (errno
== ENOTTY
) {
630 int read_only
= (flags
& NBD_FLAG_READ_ONLY
) != 0;
631 trace_nbd_init_set_readonly();
633 if (ioctl(fd
, BLKROSET
, (unsigned long) &read_only
) < 0) {
635 error_setg(errp
, "Failed setting read-only attribute");
640 error_setg(errp
, "Failed setting flags");
645 trace_nbd_init_finish();
650 int nbd_client(int fd
)
655 trace_nbd_client_loop();
657 ret
= ioctl(fd
, NBD_DO_IT
);
658 if (ret
< 0 && errno
== EPIPE
) {
659 /* NBD_DO_IT normally returns EPIPE when someone has disconnected
660 * the socket via NBD_DISCONNECT. We do not want to return 1 in
667 trace_nbd_client_loop_ret(ret
, strerror(serrno
));
669 trace_nbd_client_clear_queue();
670 ioctl(fd
, NBD_CLEAR_QUE
);
672 trace_nbd_client_clear_socket();
673 ioctl(fd
, NBD_CLEAR_SOCK
);
679 int nbd_disconnect(int fd
)
681 ioctl(fd
, NBD_CLEAR_QUE
);
682 ioctl(fd
, NBD_DISCONNECT
);
683 ioctl(fd
, NBD_CLEAR_SOCK
);
688 int nbd_init(int fd
, QIOChannelSocket
*ioc
, uint16_t flags
, off_t size
,
691 error_setg(errp
, "nbd_init is only supported on Linux");
695 int nbd_client(int fd
)
699 int nbd_disconnect(int fd
)
705 ssize_t
nbd_send_request(QIOChannel
*ioc
, NBDRequest
*request
)
707 uint8_t buf
[NBD_REQUEST_SIZE
];
709 trace_nbd_send_request(request
->from
, request
->len
, request
->handle
,
710 request
->flags
, request
->type
);
712 stl_be_p(buf
, NBD_REQUEST_MAGIC
);
713 stw_be_p(buf
+ 4, request
->flags
);
714 stw_be_p(buf
+ 6, request
->type
);
715 stq_be_p(buf
+ 8, request
->handle
);
716 stq_be_p(buf
+ 16, request
->from
);
717 stl_be_p(buf
+ 24, request
->len
);
719 return nbd_write(ioc
, buf
, sizeof(buf
), NULL
);
722 ssize_t
nbd_receive_reply(QIOChannel
*ioc
, NBDReply
*reply
, Error
**errp
)
724 uint8_t buf
[NBD_REPLY_SIZE
];
728 ret
= nbd_read_eof(ioc
, buf
, sizeof(buf
), errp
);
733 if (ret
!= sizeof(buf
)) {
734 error_setg(errp
, "read failed");
739 [ 0 .. 3] magic (NBD_REPLY_MAGIC)
740 [ 4 .. 7] error (0 == no error)
744 magic
= ldl_be_p(buf
);
745 reply
->error
= ldl_be_p(buf
+ 4);
746 reply
->handle
= ldq_be_p(buf
+ 8);
748 reply
->error
= nbd_errno_to_system_errno(reply
->error
);
750 if (reply
->error
== ESHUTDOWN
) {
751 /* This works even on mingw which lacks a native ESHUTDOWN */
752 error_setg(errp
, "server shutting down");
755 trace_nbd_receive_reply(magic
, reply
->error
, reply
->handle
);
757 if (magic
!= NBD_REPLY_MAGIC
) {
758 error_setg(errp
, "invalid magic (got 0x%" PRIx32
")", magic
);