2 * Copyright (C) 2016-2017 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
, nbd_opt_lookup(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
, nbd_opt_lookup(reply
->option
),
158 reply
->type
, nbd_rep_lookup(reply
->type
),
161 if (reply
->magic
!= NBD_REP_MAGIC
) {
162 error_setg(errp
, "Unexpected option reply magic");
163 nbd_send_opt_abort(ioc
);
166 if (reply
->option
!= opt
) {
167 error_setg(errp
, "Unexpected option type %x expected %x",
169 nbd_send_opt_abort(ioc
);
175 /* If reply represents success, return 1 without further action.
176 * If reply represents an error, consume the optional payload of
177 * the packet on ioc. Then return 0 for unsupported (so the client
178 * can fall back to other approaches), or -1 with errp set for other
181 static int nbd_handle_reply_err(QIOChannel
*ioc
, nbd_opt_reply
*reply
,
187 if (!(reply
->type
& (1 << 31))) {
192 if (reply
->length
> NBD_MAX_BUFFER_SIZE
) {
193 error_setg(errp
, "server error 0x%" PRIx32
194 " (%s) message is too long",
195 reply
->type
, nbd_rep_lookup(reply
->type
));
198 msg
= g_malloc(reply
->length
+ 1);
199 if (nbd_read(ioc
, msg
, reply
->length
, errp
) < 0) {
200 error_prepend(errp
, "failed to read option error 0x%" PRIx32
202 reply
->type
, nbd_rep_lookup(reply
->type
));
205 msg
[reply
->length
] = '\0';
208 switch (reply
->type
) {
209 case NBD_REP_ERR_UNSUP
:
210 trace_nbd_reply_err_unsup(reply
->option
, nbd_opt_lookup(reply
->option
));
214 case NBD_REP_ERR_POLICY
:
215 error_setg(errp
, "Denied by server for option %" PRIx32
" (%s)",
216 reply
->option
, nbd_opt_lookup(reply
->option
));
219 case NBD_REP_ERR_INVALID
:
220 error_setg(errp
, "Invalid data length for option %" PRIx32
" (%s)",
221 reply
->option
, nbd_opt_lookup(reply
->option
));
224 case NBD_REP_ERR_PLATFORM
:
225 error_setg(errp
, "Server lacks support for option %" PRIx32
" (%s)",
226 reply
->option
, nbd_opt_lookup(reply
->option
));
229 case NBD_REP_ERR_TLS_REQD
:
230 error_setg(errp
, "TLS negotiation required before option %" PRIx32
231 " (%s)", reply
->option
, nbd_opt_lookup(reply
->option
));
234 case NBD_REP_ERR_UNKNOWN
:
235 error_setg(errp
, "Requested export not available for option %" PRIx32
236 " (%s)", reply
->option
, nbd_opt_lookup(reply
->option
));
239 case NBD_REP_ERR_SHUTDOWN
:
240 error_setg(errp
, "Server shutting down before option %" PRIx32
" (%s)",
241 reply
->option
, nbd_opt_lookup(reply
->option
));
244 case NBD_REP_ERR_BLOCK_SIZE_REQD
:
245 error_setg(errp
, "Server requires INFO_BLOCK_SIZE for option %" PRIx32
246 " (%s)", reply
->option
, nbd_opt_lookup(reply
->option
));
250 error_setg(errp
, "Unknown error code when asking for option %" PRIx32
251 " (%s)", reply
->option
, nbd_opt_lookup(reply
->option
));
256 error_append_hint(errp
, "%s\n", msg
);
262 nbd_send_opt_abort(ioc
);
267 /* Process another portion of the NBD_OPT_LIST reply. Set *@match if
268 * the current reply matches @want or if the server does not support
269 * NBD_OPT_LIST, otherwise leave @match alone. Return 0 if iteration
270 * is complete, positive if more replies are expected, or negative
271 * with @errp set if an unrecoverable error occurred. */
272 static int nbd_receive_list(QIOChannel
*ioc
, const char *want
, bool *match
,
278 char name
[NBD_MAX_NAME_SIZE
+ 1];
281 if (nbd_receive_option_reply(ioc
, NBD_OPT_LIST
, &reply
, errp
) < 0) {
284 error
= nbd_handle_reply_err(ioc
, &reply
, errp
);
286 /* The server did not support NBD_OPT_LIST, so set *match on
287 * the assumption that any name will be accepted. */
293 if (reply
.type
== NBD_REP_ACK
) {
295 error_setg(errp
, "length too long for option end");
296 nbd_send_opt_abort(ioc
);
300 } else if (reply
.type
!= NBD_REP_SERVER
) {
301 error_setg(errp
, "Unexpected reply type %" PRIx32
" expected %x",
302 reply
.type
, NBD_REP_SERVER
);
303 nbd_send_opt_abort(ioc
);
307 if (len
< sizeof(namelen
) || len
> NBD_MAX_BUFFER_SIZE
) {
308 error_setg(errp
, "incorrect option length %" PRIu32
, len
);
309 nbd_send_opt_abort(ioc
);
312 if (nbd_read(ioc
, &namelen
, sizeof(namelen
), errp
) < 0) {
313 error_prepend(errp
, "failed to read option name length");
314 nbd_send_opt_abort(ioc
);
317 namelen
= be32_to_cpu(namelen
);
318 len
-= sizeof(namelen
);
320 error_setg(errp
, "incorrect option name length");
321 nbd_send_opt_abort(ioc
);
324 if (namelen
!= strlen(want
)) {
325 if (nbd_drop(ioc
, len
, errp
) < 0) {
326 error_prepend(errp
, "failed to skip export name with wrong length");
327 nbd_send_opt_abort(ioc
);
333 assert(namelen
< sizeof(name
));
334 if (nbd_read(ioc
, name
, namelen
, errp
) < 0) {
335 error_prepend(errp
, "failed to read export name");
336 nbd_send_opt_abort(ioc
);
339 name
[namelen
] = '\0';
341 if (nbd_drop(ioc
, len
, errp
) < 0) {
342 error_prepend(errp
, "failed to read export description");
343 nbd_send_opt_abort(ioc
);
346 if (!strcmp(name
, want
)) {
353 /* Return -1 on failure, 0 if wantname is an available export. */
354 static int nbd_receive_query_exports(QIOChannel
*ioc
,
355 const char *wantname
,
358 bool foundExport
= false;
360 trace_nbd_receive_query_exports_start(wantname
);
361 if (nbd_send_option_request(ioc
, NBD_OPT_LIST
, 0, NULL
, errp
) < 0) {
366 int ret
= nbd_receive_list(ioc
, wantname
, &foundExport
, errp
);
369 /* Server gave unexpected reply */
371 } else if (ret
== 0) {
372 /* Done iterating. */
374 error_setg(errp
, "No export with name '%s' available",
376 nbd_send_opt_abort(ioc
);
379 trace_nbd_receive_query_exports_success(wantname
);
385 static QIOChannel
*nbd_receive_starttls(QIOChannel
*ioc
,
386 QCryptoTLSCreds
*tlscreds
,
387 const char *hostname
, Error
**errp
)
391 struct NBDTLSHandshakeData data
= { 0 };
393 trace_nbd_receive_starttls_request();
394 if (nbd_send_option_request(ioc
, NBD_OPT_STARTTLS
, 0, NULL
, errp
) < 0) {
398 trace_nbd_receive_starttls_reply();
399 if (nbd_receive_option_reply(ioc
, NBD_OPT_STARTTLS
, &reply
, errp
) < 0) {
403 if (reply
.type
!= NBD_REP_ACK
) {
404 error_setg(errp
, "Server rejected request to start TLS %" PRIx32
,
406 nbd_send_opt_abort(ioc
);
410 if (reply
.length
!= 0) {
411 error_setg(errp
, "Start TLS response was not zero %" PRIu32
,
413 nbd_send_opt_abort(ioc
);
417 trace_nbd_receive_starttls_new_client();
418 tioc
= qio_channel_tls_new_client(ioc
, tlscreds
, hostname
, errp
);
422 qio_channel_set_name(QIO_CHANNEL(tioc
), "nbd-client-tls");
423 data
.loop
= g_main_loop_new(g_main_context_default(), FALSE
);
424 trace_nbd_receive_starttls_tls_handshake();
425 qio_channel_tls_handshake(tioc
,
430 if (!data
.complete
) {
431 g_main_loop_run(data
.loop
);
433 g_main_loop_unref(data
.loop
);
435 error_propagate(errp
, data
.error
);
436 object_unref(OBJECT(tioc
));
440 return QIO_CHANNEL(tioc
);
444 int nbd_receive_negotiate(QIOChannel
*ioc
, const char *name
,
445 QCryptoTLSCreds
*tlscreds
, const char *hostname
,
446 QIOChannel
**outioc
, NBDExportInfo
*info
,
454 trace_nbd_receive_negotiate(tlscreds
, hostname
? hostname
: "<null>");
461 if (tlscreds
&& !outioc
) {
462 error_setg(errp
, "Output I/O channel required for TLS");
466 if (nbd_read(ioc
, buf
, 8, errp
) < 0) {
467 error_prepend(errp
, "Failed to read data");
472 if (strlen(buf
) == 0) {
473 error_setg(errp
, "Server connection closed unexpectedly");
477 magic
= ldq_be_p(buf
);
478 trace_nbd_receive_negotiate_magic(magic
);
480 if (memcmp(buf
, "NBDMAGIC", 8) != 0) {
481 error_setg(errp
, "Invalid magic received");
485 if (nbd_read(ioc
, &magic
, sizeof(magic
), errp
) < 0) {
486 error_prepend(errp
, "Failed to read magic");
489 magic
= be64_to_cpu(magic
);
490 trace_nbd_receive_negotiate_magic(magic
);
492 if (magic
== NBD_OPTS_MAGIC
) {
493 uint32_t clientflags
= 0;
494 uint16_t globalflags
;
495 bool fixedNewStyle
= false;
497 if (nbd_read(ioc
, &globalflags
, sizeof(globalflags
), errp
) < 0) {
498 error_prepend(errp
, "Failed to read server flags");
501 globalflags
= be16_to_cpu(globalflags
);
502 trace_nbd_receive_negotiate_server_flags(globalflags
);
503 if (globalflags
& NBD_FLAG_FIXED_NEWSTYLE
) {
504 fixedNewStyle
= true;
505 clientflags
|= NBD_FLAG_C_FIXED_NEWSTYLE
;
507 if (globalflags
& NBD_FLAG_NO_ZEROES
) {
509 clientflags
|= NBD_FLAG_C_NO_ZEROES
;
511 /* client requested flags */
512 clientflags
= cpu_to_be32(clientflags
);
513 if (nbd_write(ioc
, &clientflags
, sizeof(clientflags
), errp
) < 0) {
514 error_prepend(errp
, "Failed to send clientflags field");
519 *outioc
= nbd_receive_starttls(ioc
, tlscreds
, hostname
, errp
);
525 error_setg(errp
, "Server does not support STARTTLS");
530 trace_nbd_receive_negotiate_default_name();
534 /* Check our desired export is present in the
535 * server export list. Since NBD_OPT_EXPORT_NAME
536 * cannot return an error message, running this
537 * query gives us good error reporting if the
538 * server required TLS
540 if (nbd_receive_query_exports(ioc
, name
, errp
) < 0) {
544 /* write the export name request */
545 if (nbd_send_option_request(ioc
, NBD_OPT_EXPORT_NAME
, -1, name
,
550 /* Read the response */
551 if (nbd_read(ioc
, &info
->size
, sizeof(info
->size
), errp
) < 0) {
552 error_prepend(errp
, "Failed to read export length");
555 be64_to_cpus(&info
->size
);
557 if (nbd_read(ioc
, &info
->flags
, sizeof(info
->flags
), errp
) < 0) {
558 error_prepend(errp
, "Failed to read export flags");
561 be16_to_cpus(&info
->flags
);
562 } else if (magic
== NBD_CLIENT_MAGIC
) {
566 error_setg(errp
, "Server does not support export names");
570 error_setg(errp
, "Server does not support STARTTLS");
574 if (nbd_read(ioc
, &info
->size
, sizeof(info
->size
), errp
) < 0) {
575 error_prepend(errp
, "Failed to read export length");
578 be64_to_cpus(&info
->size
);
580 if (nbd_read(ioc
, &oldflags
, sizeof(oldflags
), errp
) < 0) {
581 error_prepend(errp
, "Failed to read export flags");
584 be32_to_cpus(&oldflags
);
585 if (oldflags
& ~0xffff) {
586 error_setg(errp
, "Unexpected export flags %0x" PRIx32
, oldflags
);
589 info
->flags
= oldflags
;
591 error_setg(errp
, "Bad magic received");
595 trace_nbd_receive_negotiate_size_flags(info
->size
, info
->flags
);
596 if (zeroes
&& nbd_drop(ioc
, 124, errp
) < 0) {
597 error_prepend(errp
, "Failed to read reserved block");
607 int nbd_init(int fd
, QIOChannelSocket
*sioc
, NBDExportInfo
*info
,
610 unsigned long sectors
= info
->size
/ BDRV_SECTOR_SIZE
;
611 if (info
->size
/ BDRV_SECTOR_SIZE
!= sectors
) {
612 error_setg(errp
, "Export size %" PRIu64
" too large for 32-bit kernel",
617 trace_nbd_init_set_socket();
619 if (ioctl(fd
, NBD_SET_SOCK
, (unsigned long) sioc
->fd
) < 0) {
621 error_setg(errp
, "Failed to set NBD socket");
625 trace_nbd_init_set_block_size(BDRV_SECTOR_SIZE
);
627 if (ioctl(fd
, NBD_SET_BLKSIZE
, (unsigned long)BDRV_SECTOR_SIZE
) < 0) {
629 error_setg(errp
, "Failed setting NBD block size");
633 trace_nbd_init_set_size(sectors
);
634 if (info
->size
% BDRV_SECTOR_SIZE
) {
635 trace_nbd_init_trailing_bytes(info
->size
% BDRV_SECTOR_SIZE
);
638 if (ioctl(fd
, NBD_SET_SIZE_BLOCKS
, sectors
) < 0) {
640 error_setg(errp
, "Failed setting size (in blocks)");
644 if (ioctl(fd
, NBD_SET_FLAGS
, (unsigned long) info
->flags
) < 0) {
645 if (errno
== ENOTTY
) {
646 int read_only
= (info
->flags
& NBD_FLAG_READ_ONLY
) != 0;
647 trace_nbd_init_set_readonly();
649 if (ioctl(fd
, BLKROSET
, (unsigned long) &read_only
) < 0) {
651 error_setg(errp
, "Failed setting read-only attribute");
656 error_setg(errp
, "Failed setting flags");
661 trace_nbd_init_finish();
666 int nbd_client(int fd
)
671 trace_nbd_client_loop();
673 ret
= ioctl(fd
, NBD_DO_IT
);
674 if (ret
< 0 && errno
== EPIPE
) {
675 /* NBD_DO_IT normally returns EPIPE when someone has disconnected
676 * the socket via NBD_DISCONNECT. We do not want to return 1 in
683 trace_nbd_client_loop_ret(ret
, strerror(serrno
));
685 trace_nbd_client_clear_queue();
686 ioctl(fd
, NBD_CLEAR_QUE
);
688 trace_nbd_client_clear_socket();
689 ioctl(fd
, NBD_CLEAR_SOCK
);
695 int nbd_disconnect(int fd
)
697 ioctl(fd
, NBD_CLEAR_QUE
);
698 ioctl(fd
, NBD_DISCONNECT
);
699 ioctl(fd
, NBD_CLEAR_SOCK
);
704 int nbd_init(int fd
, QIOChannelSocket
*ioc
, NBDExportInfo
*info
,
707 error_setg(errp
, "nbd_init is only supported on Linux");
711 int nbd_client(int fd
)
715 int nbd_disconnect(int fd
)
721 ssize_t
nbd_send_request(QIOChannel
*ioc
, NBDRequest
*request
)
723 uint8_t buf
[NBD_REQUEST_SIZE
];
725 trace_nbd_send_request(request
->from
, request
->len
, request
->handle
,
726 request
->flags
, request
->type
);
728 stl_be_p(buf
, NBD_REQUEST_MAGIC
);
729 stw_be_p(buf
+ 4, request
->flags
);
730 stw_be_p(buf
+ 6, request
->type
);
731 stq_be_p(buf
+ 8, request
->handle
);
732 stq_be_p(buf
+ 16, request
->from
);
733 stl_be_p(buf
+ 24, request
->len
);
735 return nbd_write(ioc
, buf
, sizeof(buf
), NULL
);
738 ssize_t
nbd_receive_reply(QIOChannel
*ioc
, NBDReply
*reply
, Error
**errp
)
740 uint8_t buf
[NBD_REPLY_SIZE
];
744 ret
= nbd_read_eof(ioc
, buf
, sizeof(buf
), errp
);
749 if (ret
!= sizeof(buf
)) {
750 error_setg(errp
, "read failed");
755 [ 0 .. 3] magic (NBD_REPLY_MAGIC)
756 [ 4 .. 7] error (0 == no error)
760 magic
= ldl_be_p(buf
);
761 reply
->error
= ldl_be_p(buf
+ 4);
762 reply
->handle
= ldq_be_p(buf
+ 8);
764 reply
->error
= nbd_errno_to_system_errno(reply
->error
);
766 if (reply
->error
== ESHUTDOWN
) {
767 /* This works even on mingw which lacks a native ESHUTDOWN */
768 error_setg(errp
, "server shutting down");
771 trace_nbd_receive_reply(magic
, reply
->error
, reply
->handle
);
773 if (magic
!= NBD_REPLY_MAGIC
) {
774 error_setg(errp
, "invalid magic (got 0x%" PRIx32
")", magic
);