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 (len
< sizeof(namelen
) || len
> NBD_MAX_BUFFER_SIZE
) {
196 error_setg(errp
, "incorrect option length");
199 if (read_sync(ioc
, &namelen
, sizeof(namelen
)) != sizeof(namelen
)) {
200 error_setg(errp
, "failed to read option name length");
203 namelen
= be32_to_cpu(namelen
);
204 len
-= sizeof(namelen
);
206 error_setg(errp
, "incorrect option name length");
210 error_setg(errp
, "export name length too long %d", namelen
);
214 *name
= g_new0(char, namelen
+ 1);
215 if (read_sync(ioc
, *name
, namelen
) != namelen
) {
216 error_setg(errp
, "failed to read export name");
221 (*name
)[namelen
] = '\0';
224 char *buf
= g_malloc(len
+ 1);
225 if (read_sync(ioc
, buf
, len
) != len
) {
226 error_setg(errp
, "failed to read export description");
233 TRACE("Ignoring export description: %s", buf
);
237 error_setg(errp
, "Unexpected reply type %x expected %x",
238 type
, NBD_REP_SERVER
);
245 static int nbd_receive_query_exports(QIOChannel
*ioc
,
246 const char *wantname
,
249 uint64_t magic
= cpu_to_be64(NBD_OPTS_MAGIC
);
250 uint32_t opt
= cpu_to_be32(NBD_OPT_LIST
);
252 bool foundExport
= false;
254 TRACE("Querying export list");
255 if (write_sync(ioc
, &magic
, sizeof(magic
)) != sizeof(magic
)) {
256 error_setg(errp
, "Failed to send list option magic");
260 if (write_sync(ioc
, &opt
, sizeof(opt
)) != sizeof(opt
)) {
261 error_setg(errp
, "Failed to send list option number");
265 if (write_sync(ioc
, &length
, sizeof(length
)) != sizeof(length
)) {
266 error_setg(errp
, "Failed to send list option length");
270 TRACE("Reading available export names");
273 int ret
= nbd_receive_list(ioc
, &name
, errp
);
281 /* Server doesn't support export listing, so
282 * we will just assume an export with our
283 * wanted name exists */
288 TRACE("End of export name list");
291 if (g_str_equal(name
, wantname
)) {
293 TRACE("Found desired export name '%s'", name
);
295 TRACE("Ignored export name '%s'", name
);
301 error_setg(errp
, "No export with name '%s' available", wantname
);
308 static QIOChannel
*nbd_receive_starttls(QIOChannel
*ioc
,
309 QCryptoTLSCreds
*tlscreds
,
310 const char *hostname
, Error
**errp
)
312 uint64_t magic
= cpu_to_be64(NBD_OPTS_MAGIC
);
313 uint32_t opt
= cpu_to_be32(NBD_OPT_STARTTLS
);
317 struct NBDTLSHandshakeData data
= { 0 };
319 TRACE("Requesting TLS from server");
320 if (write_sync(ioc
, &magic
, sizeof(magic
)) != sizeof(magic
)) {
321 error_setg(errp
, "Failed to send option magic");
325 if (write_sync(ioc
, &opt
, sizeof(opt
)) != sizeof(opt
)) {
326 error_setg(errp
, "Failed to send option number");
330 if (write_sync(ioc
, &length
, sizeof(length
)) != sizeof(length
)) {
331 error_setg(errp
, "Failed to send option length");
335 TRACE("Getting TLS reply from server1");
336 if (read_sync(ioc
, &magic
, sizeof(magic
)) != sizeof(magic
)) {
337 error_setg(errp
, "failed to read option magic");
340 magic
= be64_to_cpu(magic
);
341 if (magic
!= NBD_REP_MAGIC
) {
342 error_setg(errp
, "Unexpected option magic");
345 TRACE("Getting TLS reply from server2");
346 if (read_sync(ioc
, &opt
, sizeof(opt
)) != sizeof(opt
)) {
347 error_setg(errp
, "failed to read option");
350 opt
= be32_to_cpu(opt
);
351 if (opt
!= NBD_OPT_STARTTLS
) {
352 error_setg(errp
, "Unexpected option type %x expected %x",
353 opt
, NBD_OPT_STARTTLS
);
357 TRACE("Getting TLS reply from server");
358 if (read_sync(ioc
, &type
, sizeof(type
)) != sizeof(type
)) {
359 error_setg(errp
, "failed to read option type");
362 type
= be32_to_cpu(type
);
363 if (type
!= NBD_REP_ACK
) {
364 error_setg(errp
, "Server rejected request to start TLS %x",
369 TRACE("Getting TLS reply from server");
370 if (read_sync(ioc
, &length
, sizeof(length
)) != sizeof(length
)) {
371 error_setg(errp
, "failed to read option length");
374 length
= be32_to_cpu(length
);
376 error_setg(errp
, "Start TLS response was not zero %x",
381 TRACE("TLS request approved, setting up TLS");
382 tioc
= qio_channel_tls_new_client(ioc
, tlscreds
, hostname
, errp
);
386 data
.loop
= g_main_loop_new(g_main_context_default(), FALSE
);
387 TRACE("Starting TLS hanshake");
388 qio_channel_tls_handshake(tioc
,
393 if (!data
.complete
) {
394 g_main_loop_run(data
.loop
);
396 g_main_loop_unref(data
.loop
);
398 error_propagate(errp
, data
.error
);
399 object_unref(OBJECT(tioc
));
403 return QIO_CHANNEL(tioc
);
407 int nbd_receive_negotiate(QIOChannel
*ioc
, const char *name
, uint32_t *flags
,
408 QCryptoTLSCreds
*tlscreds
, const char *hostname
,
410 off_t
*size
, Error
**errp
)
416 TRACE("Receiving negotiation tlscreds=%p hostname=%s.",
417 tlscreds
, hostname
? hostname
: "<null>");
424 if (tlscreds
&& !outioc
) {
425 error_setg(errp
, "Output I/O channel required for TLS");
429 if (read_sync(ioc
, buf
, 8) != 8) {
430 error_setg(errp
, "Failed to read data");
435 if (strlen(buf
) == 0) {
436 error_setg(errp
, "Server connection closed unexpectedly");
440 TRACE("Magic is %c%c%c%c%c%c%c%c",
441 qemu_isprint(buf
[0]) ? buf
[0] : '.',
442 qemu_isprint(buf
[1]) ? buf
[1] : '.',
443 qemu_isprint(buf
[2]) ? buf
[2] : '.',
444 qemu_isprint(buf
[3]) ? buf
[3] : '.',
445 qemu_isprint(buf
[4]) ? buf
[4] : '.',
446 qemu_isprint(buf
[5]) ? buf
[5] : '.',
447 qemu_isprint(buf
[6]) ? buf
[6] : '.',
448 qemu_isprint(buf
[7]) ? buf
[7] : '.');
450 if (memcmp(buf
, "NBDMAGIC", 8) != 0) {
451 error_setg(errp
, "Invalid magic received");
455 if (read_sync(ioc
, &magic
, sizeof(magic
)) != sizeof(magic
)) {
456 error_setg(errp
, "Failed to read magic");
459 magic
= be64_to_cpu(magic
);
460 TRACE("Magic is 0x%" PRIx64
, magic
);
462 if (magic
== NBD_OPTS_MAGIC
) {
463 uint32_t clientflags
= 0;
466 uint16_t globalflags
;
467 uint16_t exportflags
;
468 bool fixedNewStyle
= false;
470 if (read_sync(ioc
, &globalflags
, sizeof(globalflags
)) !=
471 sizeof(globalflags
)) {
472 error_setg(errp
, "Failed to read server flags");
475 globalflags
= be16_to_cpu(globalflags
);
476 *flags
= globalflags
<< 16;
477 TRACE("Global flags are %x", globalflags
);
478 if (globalflags
& NBD_FLAG_FIXED_NEWSTYLE
) {
479 fixedNewStyle
= true;
480 TRACE("Server supports fixed new style");
481 clientflags
|= NBD_FLAG_C_FIXED_NEWSTYLE
;
483 /* client requested flags */
484 clientflags
= cpu_to_be32(clientflags
);
485 if (write_sync(ioc
, &clientflags
, sizeof(clientflags
)) !=
486 sizeof(clientflags
)) {
487 error_setg(errp
, "Failed to send clientflags field");
492 *outioc
= nbd_receive_starttls(ioc
, tlscreds
, hostname
, errp
);
498 error_setg(errp
, "Server does not support STARTTLS");
503 TRACE("Using default NBD export name \"\"");
507 /* Check our desired export is present in the
508 * server export list. Since NBD_OPT_EXPORT_NAME
509 * cannot return an error message, running this
510 * query gives us good error reporting if the
511 * server required TLS
513 if (nbd_receive_query_exports(ioc
, name
, errp
) < 0) {
517 /* write the export name */
518 magic
= cpu_to_be64(magic
);
519 if (write_sync(ioc
, &magic
, sizeof(magic
)) != sizeof(magic
)) {
520 error_setg(errp
, "Failed to send export name magic");
523 opt
= cpu_to_be32(NBD_OPT_EXPORT_NAME
);
524 if (write_sync(ioc
, &opt
, sizeof(opt
)) != sizeof(opt
)) {
525 error_setg(errp
, "Failed to send export name option number");
528 namesize
= cpu_to_be32(strlen(name
));
529 if (write_sync(ioc
, &namesize
, sizeof(namesize
)) !=
531 error_setg(errp
, "Failed to send export name length");
534 if (write_sync(ioc
, (char *)name
, strlen(name
)) != strlen(name
)) {
535 error_setg(errp
, "Failed to send export name");
539 if (read_sync(ioc
, &s
, sizeof(s
)) != sizeof(s
)) {
540 error_setg(errp
, "Failed to read export length");
543 *size
= be64_to_cpu(s
);
544 TRACE("Size is %" PRIu64
, *size
);
546 if (read_sync(ioc
, &exportflags
, sizeof(exportflags
)) !=
547 sizeof(exportflags
)) {
548 error_setg(errp
, "Failed to read export flags");
551 exportflags
= be16_to_cpu(exportflags
);
552 *flags
|= exportflags
;
553 TRACE("Export flags are %x", exportflags
);
554 } else if (magic
== NBD_CLIENT_MAGIC
) {
556 error_setg(errp
, "Server does not support export names");
560 error_setg(errp
, "Server does not support STARTTLS");
564 if (read_sync(ioc
, &s
, sizeof(s
)) != sizeof(s
)) {
565 error_setg(errp
, "Failed to read export length");
568 *size
= be64_to_cpu(s
);
569 TRACE("Size is %" PRIu64
, *size
);
571 if (read_sync(ioc
, flags
, sizeof(*flags
)) != sizeof(*flags
)) {
572 error_setg(errp
, "Failed to read export flags");
575 *flags
= be32_to_cpup(flags
);
577 error_setg(errp
, "Bad magic received");
581 if (read_sync(ioc
, &buf
, 124) != 124) {
582 error_setg(errp
, "Failed to read reserved block");
592 int nbd_init(int fd
, QIOChannelSocket
*sioc
, uint32_t flags
, off_t size
)
594 TRACE("Setting NBD socket");
596 if (ioctl(fd
, NBD_SET_SOCK
, sioc
->fd
) < 0) {
598 LOG("Failed to set NBD socket");
602 TRACE("Setting block size to %lu", (unsigned long)BDRV_SECTOR_SIZE
);
604 if (ioctl(fd
, NBD_SET_BLKSIZE
, (size_t)BDRV_SECTOR_SIZE
) < 0) {
606 LOG("Failed setting NBD block size");
610 TRACE("Setting size to %zd block(s)", (size_t)(size
/ BDRV_SECTOR_SIZE
));
612 if (ioctl(fd
, NBD_SET_SIZE_BLOCKS
, (size_t)(size
/ BDRV_SECTOR_SIZE
)) < 0) {
614 LOG("Failed setting size (in blocks)");
618 if (ioctl(fd
, NBD_SET_FLAGS
, flags
) < 0) {
619 if (errno
== ENOTTY
) {
620 int read_only
= (flags
& NBD_FLAG_READ_ONLY
) != 0;
621 TRACE("Setting readonly attribute");
623 if (ioctl(fd
, BLKROSET
, (unsigned long) &read_only
) < 0) {
625 LOG("Failed setting read-only attribute");
630 LOG("Failed setting flags");
635 TRACE("Negotiation ended");
640 int nbd_client(int fd
)
645 TRACE("Doing NBD loop");
647 ret
= ioctl(fd
, NBD_DO_IT
);
648 if (ret
< 0 && errno
== EPIPE
) {
649 /* NBD_DO_IT normally returns EPIPE when someone has disconnected
650 * the socket via NBD_DISCONNECT. We do not want to return 1 in
657 TRACE("NBD loop returned %d: %s", ret
, strerror(serrno
));
659 TRACE("Clearing NBD queue");
660 ioctl(fd
, NBD_CLEAR_QUE
);
662 TRACE("Clearing NBD socket");
663 ioctl(fd
, NBD_CLEAR_SOCK
);
669 int nbd_init(int fd
, QIOChannelSocket
*ioc
, uint32_t flags
, off_t size
)
674 int nbd_client(int fd
)
680 ssize_t
nbd_send_request(QIOChannel
*ioc
, struct nbd_request
*request
)
682 uint8_t buf
[NBD_REQUEST_SIZE
];
685 TRACE("Sending request to server: "
686 "{ .from = %" PRIu64
", .len = %u, .handle = %" PRIu64
", .type=%i}",
687 request
->from
, request
->len
, request
->handle
, request
->type
);
689 cpu_to_be32w((uint32_t*)buf
, NBD_REQUEST_MAGIC
);
690 cpu_to_be32w((uint32_t*)(buf
+ 4), request
->type
);
691 cpu_to_be64w((uint64_t*)(buf
+ 8), request
->handle
);
692 cpu_to_be64w((uint64_t*)(buf
+ 16), request
->from
);
693 cpu_to_be32w((uint32_t*)(buf
+ 24), request
->len
);
695 ret
= write_sync(ioc
, buf
, sizeof(buf
));
700 if (ret
!= sizeof(buf
)) {
701 LOG("writing to socket failed");
707 ssize_t
nbd_receive_reply(QIOChannel
*ioc
, struct nbd_reply
*reply
)
709 uint8_t buf
[NBD_REPLY_SIZE
];
713 ret
= read_sync(ioc
, buf
, sizeof(buf
));
718 if (ret
!= sizeof(buf
)) {
724 [ 0 .. 3] magic (NBD_REPLY_MAGIC)
725 [ 4 .. 7] error (0 == no error)
729 magic
= be32_to_cpup((uint32_t*)buf
);
730 reply
->error
= be32_to_cpup((uint32_t*)(buf
+ 4));
731 reply
->handle
= be64_to_cpup((uint64_t*)(buf
+ 8));
733 reply
->error
= nbd_errno_to_system_errno(reply
->error
);
736 "{ magic = 0x%x, .error = %d, handle = %" PRIu64
" }",
737 magic
, reply
->error
, reply
->handle
);
739 if (magic
!= NBD_REPLY_MAGIC
) {
740 LOG("invalid magic (got 0x%x)", magic
);