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 %d, attempting fallback",
117 case NBD_REP_ERR_POLICY
:
118 error_setg(errp
, "Denied by server for option %x", opt
);
121 case NBD_REP_ERR_INVALID
:
122 error_setg(errp
, "Invalid data length for option %x", opt
);
125 case NBD_REP_ERR_TLS_REQD
:
126 error_setg(errp
, "TLS negotiation required before option %x", opt
);
130 error_setg(errp
, "Unknown error code when asking for option %x", opt
);
135 error_append_hint(errp
, "%s\n", msg
);
143 static int nbd_receive_list(QIOChannel
*ioc
, char **name
, Error
**errp
)
153 if (read_sync(ioc
, &magic
, sizeof(magic
)) != sizeof(magic
)) {
154 error_setg(errp
, "failed to read list option magic");
157 magic
= be64_to_cpu(magic
);
158 if (magic
!= NBD_REP_MAGIC
) {
159 error_setg(errp
, "Unexpected option list magic");
162 if (read_sync(ioc
, &opt
, sizeof(opt
)) != sizeof(opt
)) {
163 error_setg(errp
, "failed to read list option");
166 opt
= be32_to_cpu(opt
);
167 if (opt
!= NBD_OPT_LIST
) {
168 error_setg(errp
, "Unexpected option type %x expected %x",
173 if (read_sync(ioc
, &type
, sizeof(type
)) != sizeof(type
)) {
174 error_setg(errp
, "failed to read list option type");
177 type
= be32_to_cpu(type
);
178 error
= nbd_handle_reply_err(ioc
, opt
, type
, errp
);
183 if (read_sync(ioc
, &len
, sizeof(len
)) != sizeof(len
)) {
184 error_setg(errp
, "failed to read option length");
187 len
= be32_to_cpu(len
);
189 if (type
== NBD_REP_ACK
) {
191 error_setg(errp
, "length too long for option end");
194 } else if (type
== NBD_REP_SERVER
) {
195 if (read_sync(ioc
, &namelen
, sizeof(namelen
)) != sizeof(namelen
)) {
196 error_setg(errp
, "failed to read option name length");
199 namelen
= be32_to_cpu(namelen
);
200 if (len
!= (namelen
+ sizeof(namelen
))) {
201 error_setg(errp
, "incorrect option mame length");
205 error_setg(errp
, "export name length too long %d", namelen
);
209 *name
= g_new0(char, namelen
+ 1);
210 if (read_sync(ioc
, *name
, namelen
) != namelen
) {
211 error_setg(errp
, "failed to read export name");
216 (*name
)[namelen
] = '\0';
218 error_setg(errp
, "Unexpected reply type %x expected %x",
219 type
, NBD_REP_SERVER
);
226 static int nbd_receive_query_exports(QIOChannel
*ioc
,
227 const char *wantname
,
230 uint64_t magic
= cpu_to_be64(NBD_OPTS_MAGIC
);
231 uint32_t opt
= cpu_to_be32(NBD_OPT_LIST
);
233 bool foundExport
= false;
235 TRACE("Querying export list");
236 if (write_sync(ioc
, &magic
, sizeof(magic
)) != sizeof(magic
)) {
237 error_setg(errp
, "Failed to send list option magic");
241 if (write_sync(ioc
, &opt
, sizeof(opt
)) != sizeof(opt
)) {
242 error_setg(errp
, "Failed to send list option number");
246 if (write_sync(ioc
, &length
, sizeof(length
)) != sizeof(length
)) {
247 error_setg(errp
, "Failed to send list option length");
251 TRACE("Reading available export names");
254 int ret
= nbd_receive_list(ioc
, &name
, errp
);
262 /* Server doesn't support export listing, so
263 * we will just assume an export with our
264 * wanted name exists */
269 TRACE("End of export name list");
272 if (g_str_equal(name
, wantname
)) {
274 TRACE("Found desired export name '%s'", name
);
276 TRACE("Ignored export name '%s'", name
);
282 error_setg(errp
, "No export with name '%s' available", wantname
);
289 static QIOChannel
*nbd_receive_starttls(QIOChannel
*ioc
,
290 QCryptoTLSCreds
*tlscreds
,
291 const char *hostname
, Error
**errp
)
293 uint64_t magic
= cpu_to_be64(NBD_OPTS_MAGIC
);
294 uint32_t opt
= cpu_to_be32(NBD_OPT_STARTTLS
);
298 struct NBDTLSHandshakeData data
= { 0 };
300 TRACE("Requesting TLS from server");
301 if (write_sync(ioc
, &magic
, sizeof(magic
)) != sizeof(magic
)) {
302 error_setg(errp
, "Failed to send option magic");
306 if (write_sync(ioc
, &opt
, sizeof(opt
)) != sizeof(opt
)) {
307 error_setg(errp
, "Failed to send option number");
311 if (write_sync(ioc
, &length
, sizeof(length
)) != sizeof(length
)) {
312 error_setg(errp
, "Failed to send option length");
316 TRACE("Getting TLS reply from server1");
317 if (read_sync(ioc
, &magic
, sizeof(magic
)) != sizeof(magic
)) {
318 error_setg(errp
, "failed to read option magic");
321 magic
= be64_to_cpu(magic
);
322 if (magic
!= NBD_REP_MAGIC
) {
323 error_setg(errp
, "Unexpected option magic");
326 TRACE("Getting TLS reply from server2");
327 if (read_sync(ioc
, &opt
, sizeof(opt
)) != sizeof(opt
)) {
328 error_setg(errp
, "failed to read option");
331 opt
= be32_to_cpu(opt
);
332 if (opt
!= NBD_OPT_STARTTLS
) {
333 error_setg(errp
, "Unexpected option type %x expected %x",
334 opt
, NBD_OPT_STARTTLS
);
338 TRACE("Getting TLS reply from server");
339 if (read_sync(ioc
, &type
, sizeof(type
)) != sizeof(type
)) {
340 error_setg(errp
, "failed to read option type");
343 type
= be32_to_cpu(type
);
344 if (type
!= NBD_REP_ACK
) {
345 error_setg(errp
, "Server rejected request to start TLS %x",
350 TRACE("Getting TLS reply from server");
351 if (read_sync(ioc
, &length
, sizeof(length
)) != sizeof(length
)) {
352 error_setg(errp
, "failed to read option length");
355 length
= be32_to_cpu(length
);
357 error_setg(errp
, "Start TLS reponse was not zero %x",
362 TRACE("TLS request approved, setting up TLS");
363 tioc
= qio_channel_tls_new_client(ioc
, tlscreds
, hostname
, errp
);
367 data
.loop
= g_main_loop_new(g_main_context_default(), FALSE
);
368 TRACE("Starting TLS hanshake");
369 qio_channel_tls_handshake(tioc
,
374 if (!data
.complete
) {
375 g_main_loop_run(data
.loop
);
377 g_main_loop_unref(data
.loop
);
379 error_propagate(errp
, data
.error
);
380 object_unref(OBJECT(tioc
));
384 return QIO_CHANNEL(tioc
);
388 int nbd_receive_negotiate(QIOChannel
*ioc
, const char *name
, uint32_t *flags
,
389 QCryptoTLSCreds
*tlscreds
, const char *hostname
,
391 off_t
*size
, Error
**errp
)
397 TRACE("Receiving negotiation tlscreds=%p hostname=%s.",
398 tlscreds
, hostname
? hostname
: "<null>");
405 if (tlscreds
&& !outioc
) {
406 error_setg(errp
, "Output I/O channel required for TLS");
410 if (read_sync(ioc
, buf
, 8) != 8) {
411 error_setg(errp
, "Failed to read data");
416 if (strlen(buf
) == 0) {
417 error_setg(errp
, "Server connection closed unexpectedly");
421 TRACE("Magic is %c%c%c%c%c%c%c%c",
422 qemu_isprint(buf
[0]) ? buf
[0] : '.',
423 qemu_isprint(buf
[1]) ? buf
[1] : '.',
424 qemu_isprint(buf
[2]) ? buf
[2] : '.',
425 qemu_isprint(buf
[3]) ? buf
[3] : '.',
426 qemu_isprint(buf
[4]) ? buf
[4] : '.',
427 qemu_isprint(buf
[5]) ? buf
[5] : '.',
428 qemu_isprint(buf
[6]) ? buf
[6] : '.',
429 qemu_isprint(buf
[7]) ? buf
[7] : '.');
431 if (memcmp(buf
, "NBDMAGIC", 8) != 0) {
432 error_setg(errp
, "Invalid magic received");
436 if (read_sync(ioc
, &magic
, sizeof(magic
)) != sizeof(magic
)) {
437 error_setg(errp
, "Failed to read magic");
440 magic
= be64_to_cpu(magic
);
441 TRACE("Magic is 0x%" PRIx64
, magic
);
443 if (magic
== NBD_OPTS_MAGIC
) {
444 uint32_t clientflags
= 0;
447 uint16_t globalflags
;
448 uint16_t exportflags
;
449 bool fixedNewStyle
= false;
451 if (read_sync(ioc
, &globalflags
, sizeof(globalflags
)) !=
452 sizeof(globalflags
)) {
453 error_setg(errp
, "Failed to read server flags");
456 globalflags
= be16_to_cpu(globalflags
);
457 *flags
= globalflags
<< 16;
458 TRACE("Global flags are %x", globalflags
);
459 if (globalflags
& NBD_FLAG_FIXED_NEWSTYLE
) {
460 fixedNewStyle
= true;
461 TRACE("Server supports fixed new style");
462 clientflags
|= NBD_FLAG_C_FIXED_NEWSTYLE
;
464 /* client requested flags */
465 clientflags
= cpu_to_be32(clientflags
);
466 if (write_sync(ioc
, &clientflags
, sizeof(clientflags
)) !=
467 sizeof(clientflags
)) {
468 error_setg(errp
, "Failed to send clientflags field");
473 *outioc
= nbd_receive_starttls(ioc
, tlscreds
, hostname
, errp
);
479 error_setg(errp
, "Server does not support STARTTLS");
484 TRACE("Using default NBD export name \"\"");
488 /* Check our desired export is present in the
489 * server export list. Since NBD_OPT_EXPORT_NAME
490 * cannot return an error message, running this
491 * query gives us good error reporting if the
492 * server required TLS
494 if (nbd_receive_query_exports(ioc
, name
, errp
) < 0) {
498 /* write the export name */
499 magic
= cpu_to_be64(magic
);
500 if (write_sync(ioc
, &magic
, sizeof(magic
)) != sizeof(magic
)) {
501 error_setg(errp
, "Failed to send export name magic");
504 opt
= cpu_to_be32(NBD_OPT_EXPORT_NAME
);
505 if (write_sync(ioc
, &opt
, sizeof(opt
)) != sizeof(opt
)) {
506 error_setg(errp
, "Failed to send export name option number");
509 namesize
= cpu_to_be32(strlen(name
));
510 if (write_sync(ioc
, &namesize
, sizeof(namesize
)) !=
512 error_setg(errp
, "Failed to send export name length");
515 if (write_sync(ioc
, (char *)name
, strlen(name
)) != strlen(name
)) {
516 error_setg(errp
, "Failed to send export name");
520 if (read_sync(ioc
, &s
, sizeof(s
)) != sizeof(s
)) {
521 error_setg(errp
, "Failed to read export length");
524 *size
= be64_to_cpu(s
);
525 TRACE("Size is %" PRIu64
, *size
);
527 if (read_sync(ioc
, &exportflags
, sizeof(exportflags
)) !=
528 sizeof(exportflags
)) {
529 error_setg(errp
, "Failed to read export flags");
532 exportflags
= be16_to_cpu(exportflags
);
533 *flags
|= exportflags
;
534 TRACE("Export flags are %x", exportflags
);
535 } else if (magic
== NBD_CLIENT_MAGIC
) {
537 error_setg(errp
, "Server does not support export names");
541 error_setg(errp
, "Server does not support STARTTLS");
545 if (read_sync(ioc
, &s
, sizeof(s
)) != sizeof(s
)) {
546 error_setg(errp
, "Failed to read export length");
549 *size
= be64_to_cpu(s
);
550 TRACE("Size is %" PRIu64
, *size
);
552 if (read_sync(ioc
, flags
, sizeof(*flags
)) != sizeof(*flags
)) {
553 error_setg(errp
, "Failed to read export flags");
556 *flags
= be32_to_cpup(flags
);
558 error_setg(errp
, "Bad magic received");
562 if (read_sync(ioc
, &buf
, 124) != 124) {
563 error_setg(errp
, "Failed to read reserved block");
573 int nbd_init(int fd
, QIOChannelSocket
*sioc
, uint32_t flags
, off_t size
)
575 TRACE("Setting NBD socket");
577 if (ioctl(fd
, NBD_SET_SOCK
, sioc
->fd
) < 0) {
579 LOG("Failed to set NBD socket");
583 TRACE("Setting block size to %lu", (unsigned long)BDRV_SECTOR_SIZE
);
585 if (ioctl(fd
, NBD_SET_BLKSIZE
, (size_t)BDRV_SECTOR_SIZE
) < 0) {
587 LOG("Failed setting NBD block size");
591 TRACE("Setting size to %zd block(s)", (size_t)(size
/ BDRV_SECTOR_SIZE
));
593 if (ioctl(fd
, NBD_SET_SIZE_BLOCKS
, (size_t)(size
/ BDRV_SECTOR_SIZE
)) < 0) {
595 LOG("Failed setting size (in blocks)");
599 if (ioctl(fd
, NBD_SET_FLAGS
, flags
) < 0) {
600 if (errno
== ENOTTY
) {
601 int read_only
= (flags
& NBD_FLAG_READ_ONLY
) != 0;
602 TRACE("Setting readonly attribute");
604 if (ioctl(fd
, BLKROSET
, (unsigned long) &read_only
) < 0) {
606 LOG("Failed setting read-only attribute");
611 LOG("Failed setting flags");
616 TRACE("Negotiation ended");
621 int nbd_client(int fd
)
626 TRACE("Doing NBD loop");
628 ret
= ioctl(fd
, NBD_DO_IT
);
629 if (ret
< 0 && errno
== EPIPE
) {
630 /* NBD_DO_IT normally returns EPIPE when someone has disconnected
631 * the socket via NBD_DISCONNECT. We do not want to return 1 in
638 TRACE("NBD loop returned %d: %s", ret
, strerror(serrno
));
640 TRACE("Clearing NBD queue");
641 ioctl(fd
, NBD_CLEAR_QUE
);
643 TRACE("Clearing NBD socket");
644 ioctl(fd
, NBD_CLEAR_SOCK
);
650 int nbd_init(int fd
, QIOChannelSocket
*ioc
, uint32_t flags
, off_t size
)
655 int nbd_client(int fd
)
661 ssize_t
nbd_send_request(QIOChannel
*ioc
, struct nbd_request
*request
)
663 uint8_t buf
[NBD_REQUEST_SIZE
];
666 TRACE("Sending request to server: "
667 "{ .from = %" PRIu64
", .len = %u, .handle = %" PRIu64
", .type=%i}",
668 request
->from
, request
->len
, request
->handle
, request
->type
);
670 cpu_to_be32w((uint32_t*)buf
, NBD_REQUEST_MAGIC
);
671 cpu_to_be32w((uint32_t*)(buf
+ 4), request
->type
);
672 cpu_to_be64w((uint64_t*)(buf
+ 8), request
->handle
);
673 cpu_to_be64w((uint64_t*)(buf
+ 16), request
->from
);
674 cpu_to_be32w((uint32_t*)(buf
+ 24), request
->len
);
676 ret
= write_sync(ioc
, buf
, sizeof(buf
));
681 if (ret
!= sizeof(buf
)) {
682 LOG("writing to socket failed");
688 ssize_t
nbd_receive_reply(QIOChannel
*ioc
, struct nbd_reply
*reply
)
690 uint8_t buf
[NBD_REPLY_SIZE
];
694 ret
= read_sync(ioc
, buf
, sizeof(buf
));
699 if (ret
!= sizeof(buf
)) {
705 [ 0 .. 3] magic (NBD_REPLY_MAGIC)
706 [ 4 .. 7] error (0 == no error)
710 magic
= be32_to_cpup((uint32_t*)buf
);
711 reply
->error
= be32_to_cpup((uint32_t*)(buf
+ 4));
712 reply
->handle
= be64_to_cpup((uint64_t*)(buf
+ 8));
714 reply
->error
= nbd_errno_to_system_errno(reply
->error
);
717 "{ magic = 0x%x, .error = %d, handle = %" PRIu64
" }",
718 magic
, reply
->error
, reply
->handle
);
720 if (magic
!= NBD_REPLY_MAGIC
) {
721 LOG("invalid magic (got 0x%x)", magic
);