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"
22 #include "nbd-internal.h"
24 static int nbd_errno_to_system_errno(int err
)
38 TRACE("Squashing unexpected error %d to EINVAL", err
);
45 /* Definitions for opaque data types */
47 static QTAILQ_HEAD(, NBDExport
) exports
= QTAILQ_HEAD_INITIALIZER(exports
);
49 /* That's all folks */
51 /* Basic flow for negotiation
79 /* If type represents success, return 1 without further action.
80 * If type represents an error reply, consume the rest of the packet on ioc.
81 * Then return 0 for unsupported (so the client can fall back to
82 * other approaches), or -1 with errp set for other errors.
84 static int nbd_handle_reply_err(QIOChannel
*ioc
, uint32_t opt
, uint32_t type
,
91 if (!(type
& (1 << 31))) {
95 if (read_sync(ioc
, &len
, sizeof(len
)) != sizeof(len
)) {
96 error_setg(errp
, "failed to read option length");
99 len
= be32_to_cpu(len
);
101 if (len
> NBD_MAX_BUFFER_SIZE
) {
102 error_setg(errp
, "server's error message is too long");
105 msg
= g_malloc(len
+ 1);
106 if (read_sync(ioc
, msg
, len
) != len
) {
107 error_setg(errp
, "failed to read option error message");
114 case NBD_REP_ERR_UNSUP
:
115 TRACE("server doesn't understand request %" PRIx32
116 ", attempting fallback", opt
);
120 case NBD_REP_ERR_POLICY
:
121 error_setg(errp
, "Denied by server for option %" PRIx32
, opt
);
124 case NBD_REP_ERR_INVALID
:
125 error_setg(errp
, "Invalid data length for option %" PRIx32
, opt
);
128 case NBD_REP_ERR_TLS_REQD
:
129 error_setg(errp
, "TLS negotiation required before option %" PRIx32
,
134 error_setg(errp
, "Unknown error code when asking for option %" PRIx32
,
140 error_append_hint(errp
, "%s\n", msg
);
148 static int nbd_receive_list(QIOChannel
*ioc
, char **name
, Error
**errp
)
158 if (read_sync(ioc
, &magic
, sizeof(magic
)) != sizeof(magic
)) {
159 error_setg(errp
, "failed to read list option magic");
162 magic
= be64_to_cpu(magic
);
163 if (magic
!= NBD_REP_MAGIC
) {
164 error_setg(errp
, "Unexpected option list magic");
167 if (read_sync(ioc
, &opt
, sizeof(opt
)) != sizeof(opt
)) {
168 error_setg(errp
, "failed to read list option");
171 opt
= be32_to_cpu(opt
);
172 if (opt
!= NBD_OPT_LIST
) {
173 error_setg(errp
, "Unexpected option type %" PRIx32
" expected %x",
178 if (read_sync(ioc
, &type
, sizeof(type
)) != sizeof(type
)) {
179 error_setg(errp
, "failed to read list option type");
182 type
= be32_to_cpu(type
);
183 error
= nbd_handle_reply_err(ioc
, opt
, type
, errp
);
188 if (read_sync(ioc
, &len
, sizeof(len
)) != sizeof(len
)) {
189 error_setg(errp
, "failed to read option length");
192 len
= be32_to_cpu(len
);
194 if (type
== NBD_REP_ACK
) {
196 error_setg(errp
, "length too long for option end");
199 } else if (type
== NBD_REP_SERVER
) {
200 if (len
< sizeof(namelen
) || len
> NBD_MAX_BUFFER_SIZE
) {
201 error_setg(errp
, "incorrect option length");
204 if (read_sync(ioc
, &namelen
, sizeof(namelen
)) != sizeof(namelen
)) {
205 error_setg(errp
, "failed to read option name length");
208 namelen
= be32_to_cpu(namelen
);
209 len
-= sizeof(namelen
);
211 error_setg(errp
, "incorrect option name length");
214 if (namelen
> NBD_MAX_NAME_SIZE
) {
215 error_setg(errp
, "export name length too long %" PRIu32
, namelen
);
219 *name
= g_new0(char, namelen
+ 1);
220 if (read_sync(ioc
, *name
, namelen
) != namelen
) {
221 error_setg(errp
, "failed to read export name");
226 (*name
)[namelen
] = '\0';
229 char *buf
= g_malloc(len
+ 1);
230 if (read_sync(ioc
, buf
, len
) != len
) {
231 error_setg(errp
, "failed to read export description");
238 TRACE("Ignoring export description: %s", buf
);
242 error_setg(errp
, "Unexpected reply type %" PRIx32
" expected %x",
243 type
, NBD_REP_SERVER
);
250 static int nbd_receive_query_exports(QIOChannel
*ioc
,
251 const char *wantname
,
254 uint64_t magic
= cpu_to_be64(NBD_OPTS_MAGIC
);
255 uint32_t opt
= cpu_to_be32(NBD_OPT_LIST
);
257 bool foundExport
= false;
259 TRACE("Querying export list");
260 if (write_sync(ioc
, &magic
, sizeof(magic
)) != sizeof(magic
)) {
261 error_setg(errp
, "Failed to send list option magic");
265 if (write_sync(ioc
, &opt
, sizeof(opt
)) != sizeof(opt
)) {
266 error_setg(errp
, "Failed to send list option number");
270 if (write_sync(ioc
, &length
, sizeof(length
)) != sizeof(length
)) {
271 error_setg(errp
, "Failed to send list option length");
275 TRACE("Reading available export names");
278 int ret
= nbd_receive_list(ioc
, &name
, errp
);
286 /* Server doesn't support export listing, so
287 * we will just assume an export with our
288 * wanted name exists */
293 TRACE("End of export name list");
296 if (g_str_equal(name
, wantname
)) {
298 TRACE("Found desired export name '%s'", name
);
300 TRACE("Ignored export name '%s'", name
);
306 error_setg(errp
, "No export with name '%s' available", wantname
);
313 static QIOChannel
*nbd_receive_starttls(QIOChannel
*ioc
,
314 QCryptoTLSCreds
*tlscreds
,
315 const char *hostname
, Error
**errp
)
317 uint64_t magic
= cpu_to_be64(NBD_OPTS_MAGIC
);
318 uint32_t opt
= cpu_to_be32(NBD_OPT_STARTTLS
);
322 struct NBDTLSHandshakeData data
= { 0 };
324 TRACE("Requesting TLS from server");
325 if (write_sync(ioc
, &magic
, sizeof(magic
)) != sizeof(magic
)) {
326 error_setg(errp
, "Failed to send option magic");
330 if (write_sync(ioc
, &opt
, sizeof(opt
)) != sizeof(opt
)) {
331 error_setg(errp
, "Failed to send option number");
335 if (write_sync(ioc
, &length
, sizeof(length
)) != sizeof(length
)) {
336 error_setg(errp
, "Failed to send option length");
340 TRACE("Getting TLS reply from server1");
341 if (read_sync(ioc
, &magic
, sizeof(magic
)) != sizeof(magic
)) {
342 error_setg(errp
, "failed to read option magic");
345 magic
= be64_to_cpu(magic
);
346 if (magic
!= NBD_REP_MAGIC
) {
347 error_setg(errp
, "Unexpected option magic");
350 TRACE("Getting TLS reply from server2");
351 if (read_sync(ioc
, &opt
, sizeof(opt
)) != sizeof(opt
)) {
352 error_setg(errp
, "failed to read option");
355 opt
= be32_to_cpu(opt
);
356 if (opt
!= NBD_OPT_STARTTLS
) {
357 error_setg(errp
, "Unexpected option type %" PRIx32
" expected %x",
358 opt
, NBD_OPT_STARTTLS
);
362 TRACE("Getting TLS reply from server");
363 if (read_sync(ioc
, &type
, sizeof(type
)) != sizeof(type
)) {
364 error_setg(errp
, "failed to read option type");
367 type
= be32_to_cpu(type
);
368 if (type
!= NBD_REP_ACK
) {
369 error_setg(errp
, "Server rejected request to start TLS %" PRIx32
,
374 TRACE("Getting TLS reply from server");
375 if (read_sync(ioc
, &length
, sizeof(length
)) != sizeof(length
)) {
376 error_setg(errp
, "failed to read option length");
379 length
= be32_to_cpu(length
);
381 error_setg(errp
, "Start TLS response was not zero %" PRIu32
,
386 TRACE("TLS request approved, setting up TLS");
387 tioc
= qio_channel_tls_new_client(ioc
, tlscreds
, hostname
, errp
);
391 qio_channel_set_name(QIO_CHANNEL(tioc
), "nbd-client-tls");
392 data
.loop
= g_main_loop_new(g_main_context_default(), FALSE
);
393 TRACE("Starting TLS handshake");
394 qio_channel_tls_handshake(tioc
,
399 if (!data
.complete
) {
400 g_main_loop_run(data
.loop
);
402 g_main_loop_unref(data
.loop
);
404 error_propagate(errp
, data
.error
);
405 object_unref(OBJECT(tioc
));
409 return QIO_CHANNEL(tioc
);
413 int nbd_receive_negotiate(QIOChannel
*ioc
, const char *name
, uint16_t *flags
,
414 QCryptoTLSCreds
*tlscreds
, const char *hostname
,
416 off_t
*size
, Error
**errp
)
422 TRACE("Receiving negotiation tlscreds=%p hostname=%s.",
423 tlscreds
, hostname
? hostname
: "<null>");
430 if (tlscreds
&& !outioc
) {
431 error_setg(errp
, "Output I/O channel required for TLS");
435 if (read_sync(ioc
, buf
, 8) != 8) {
436 error_setg(errp
, "Failed to read data");
441 if (strlen(buf
) == 0) {
442 error_setg(errp
, "Server connection closed unexpectedly");
446 TRACE("Magic is %c%c%c%c%c%c%c%c",
447 qemu_isprint(buf
[0]) ? buf
[0] : '.',
448 qemu_isprint(buf
[1]) ? buf
[1] : '.',
449 qemu_isprint(buf
[2]) ? buf
[2] : '.',
450 qemu_isprint(buf
[3]) ? buf
[3] : '.',
451 qemu_isprint(buf
[4]) ? buf
[4] : '.',
452 qemu_isprint(buf
[5]) ? buf
[5] : '.',
453 qemu_isprint(buf
[6]) ? buf
[6] : '.',
454 qemu_isprint(buf
[7]) ? buf
[7] : '.');
456 if (memcmp(buf
, "NBDMAGIC", 8) != 0) {
457 error_setg(errp
, "Invalid magic received");
461 if (read_sync(ioc
, &magic
, sizeof(magic
)) != sizeof(magic
)) {
462 error_setg(errp
, "Failed to read magic");
465 magic
= be64_to_cpu(magic
);
466 TRACE("Magic is 0x%" PRIx64
, magic
);
468 if (magic
== NBD_OPTS_MAGIC
) {
469 uint32_t clientflags
= 0;
472 uint16_t globalflags
;
473 bool fixedNewStyle
= false;
475 if (read_sync(ioc
, &globalflags
, sizeof(globalflags
)) !=
476 sizeof(globalflags
)) {
477 error_setg(errp
, "Failed to read server flags");
480 globalflags
= be16_to_cpu(globalflags
);
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
);
549 if (read_sync(ioc
, flags
, sizeof(*flags
)) != sizeof(*flags
)) {
550 error_setg(errp
, "Failed to read export flags");
554 } 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
, &oldflags
, sizeof(oldflags
)) != sizeof(oldflags
)) {
574 error_setg(errp
, "Failed to read export flags");
577 be32_to_cpus(&oldflags
);
578 if (oldflags
& ~0xffff) {
579 error_setg(errp
, "Unexpected export flags %0x" PRIx32
, oldflags
);
584 error_setg(errp
, "Bad magic received");
588 TRACE("Size is %" PRIu64
", export flags %" PRIx16
, *size
, *flags
);
589 if (read_sync(ioc
, &buf
, 124) != 124) {
590 error_setg(errp
, "Failed to read reserved block");
600 int nbd_init(int fd
, QIOChannelSocket
*sioc
, uint16_t flags
, off_t size
)
602 unsigned long sectors
= size
/ BDRV_SECTOR_SIZE
;
603 if (size
/ BDRV_SECTOR_SIZE
!= sectors
) {
604 LOG("Export size %lld too large for 32-bit kernel", (long long) size
);
608 TRACE("Setting NBD socket");
610 if (ioctl(fd
, NBD_SET_SOCK
, (unsigned long) sioc
->fd
) < 0) {
612 LOG("Failed to set NBD socket");
616 TRACE("Setting block size to %lu", (unsigned long)BDRV_SECTOR_SIZE
);
618 if (ioctl(fd
, NBD_SET_BLKSIZE
, (unsigned long)BDRV_SECTOR_SIZE
) < 0) {
620 LOG("Failed setting NBD block size");
624 TRACE("Setting size to %lu block(s)", sectors
);
625 if (size
% BDRV_SECTOR_SIZE
) {
626 TRACE("Ignoring trailing %d bytes of export",
627 (int) (size
% BDRV_SECTOR_SIZE
));
630 if (ioctl(fd
, NBD_SET_SIZE_BLOCKS
, sectors
) < 0) {
632 LOG("Failed setting size (in blocks)");
636 if (ioctl(fd
, NBD_SET_FLAGS
, (unsigned long) flags
) < 0) {
637 if (errno
== ENOTTY
) {
638 int read_only
= (flags
& NBD_FLAG_READ_ONLY
) != 0;
639 TRACE("Setting readonly attribute");
641 if (ioctl(fd
, BLKROSET
, (unsigned long) &read_only
) < 0) {
643 LOG("Failed setting read-only attribute");
648 LOG("Failed setting flags");
653 TRACE("Negotiation ended");
658 int nbd_client(int fd
)
663 TRACE("Doing NBD loop");
665 ret
= ioctl(fd
, NBD_DO_IT
);
666 if (ret
< 0 && errno
== EPIPE
) {
667 /* NBD_DO_IT normally returns EPIPE when someone has disconnected
668 * the socket via NBD_DISCONNECT. We do not want to return 1 in
675 TRACE("NBD loop returned %d: %s", ret
, strerror(serrno
));
677 TRACE("Clearing NBD queue");
678 ioctl(fd
, NBD_CLEAR_QUE
);
680 TRACE("Clearing NBD socket");
681 ioctl(fd
, NBD_CLEAR_SOCK
);
687 int nbd_disconnect(int fd
)
689 ioctl(fd
, NBD_CLEAR_QUE
);
690 ioctl(fd
, NBD_DISCONNECT
);
691 ioctl(fd
, NBD_CLEAR_SOCK
);
696 int nbd_init(int fd
, QIOChannelSocket
*ioc
, uint16_t flags
, off_t size
)
701 int nbd_client(int fd
)
705 int nbd_disconnect(int fd
)
711 ssize_t
nbd_send_request(QIOChannel
*ioc
, struct nbd_request
*request
)
713 uint8_t buf
[NBD_REQUEST_SIZE
];
716 TRACE("Sending request to server: "
717 "{ .from = %" PRIu64
", .len = %" PRIu32
", .handle = %" PRIu64
718 ", .flags = %" PRIx16
", .type = %" PRIu16
" }",
719 request
->from
, request
->len
, request
->handle
,
720 request
->flags
, request
->type
);
722 stl_be_p(buf
, NBD_REQUEST_MAGIC
);
723 stw_be_p(buf
+ 4, request
->flags
);
724 stw_be_p(buf
+ 6, request
->type
);
725 stq_be_p(buf
+ 8, request
->handle
);
726 stq_be_p(buf
+ 16, request
->from
);
727 stl_be_p(buf
+ 24, request
->len
);
729 ret
= write_sync(ioc
, buf
, sizeof(buf
));
734 if (ret
!= sizeof(buf
)) {
735 LOG("writing to socket failed");
741 ssize_t
nbd_receive_reply(QIOChannel
*ioc
, struct nbd_reply
*reply
)
743 uint8_t buf
[NBD_REPLY_SIZE
];
747 ret
= read_sync(ioc
, buf
, sizeof(buf
));
752 if (ret
!= sizeof(buf
)) {
758 [ 0 .. 3] magic (NBD_REPLY_MAGIC)
759 [ 4 .. 7] error (0 == no error)
763 magic
= ldl_be_p(buf
);
764 reply
->error
= ldl_be_p(buf
+ 4);
765 reply
->handle
= ldq_be_p(buf
+ 8);
767 reply
->error
= nbd_errno_to_system_errno(reply
->error
);
769 TRACE("Got reply: { magic = 0x%" PRIx32
", .error = % " PRId32
770 ", handle = %" PRIu64
" }",
771 magic
, reply
->error
, reply
->handle
);
773 if (magic
!= NBD_REPLY_MAGIC
) {
774 LOG("invalid magic (got 0x%" PRIx32
")", magic
);