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 "nbd-internal.h"
22 static int nbd_errno_to_system_errno(int err
)
41 /* Definitions for opaque data types */
43 static QTAILQ_HEAD(, NBDExport
) exports
= QTAILQ_HEAD_INITIALIZER(exports
);
45 /* That's all folks */
47 /* Basic flow for negotiation
75 static int nbd_handle_reply_err(uint32_t opt
, uint32_t type
, Error
**errp
)
77 if (!(type
& (1 << 31))) {
82 case NBD_REP_ERR_UNSUP
:
83 error_setg(errp
, "Unsupported option type %x", opt
);
86 case NBD_REP_ERR_POLICY
:
87 error_setg(errp
, "Denied by server for option %x", opt
);
90 case NBD_REP_ERR_INVALID
:
91 error_setg(errp
, "Invalid data length for option %x", opt
);
94 case NBD_REP_ERR_TLS_REQD
:
95 error_setg(errp
, "TLS negotiation required before option %x", opt
);
99 error_setg(errp
, "Unknown error code when asking for option %x", opt
);
106 static int nbd_receive_list(QIOChannel
*ioc
, char **name
, Error
**errp
)
115 if (read_sync(ioc
, &magic
, sizeof(magic
)) != sizeof(magic
)) {
116 error_setg(errp
, "failed to read list option magic");
119 magic
= be64_to_cpu(magic
);
120 if (magic
!= NBD_REP_MAGIC
) {
121 error_setg(errp
, "Unexpected option list magic");
124 if (read_sync(ioc
, &opt
, sizeof(opt
)) != sizeof(opt
)) {
125 error_setg(errp
, "failed to read list option");
128 opt
= be32_to_cpu(opt
);
129 if (opt
!= NBD_OPT_LIST
) {
130 error_setg(errp
, "Unexpected option type %x expected %x",
135 if (read_sync(ioc
, &type
, sizeof(type
)) != sizeof(type
)) {
136 error_setg(errp
, "failed to read list option type");
139 type
= be32_to_cpu(type
);
140 if (type
== NBD_REP_ERR_UNSUP
) {
143 if (nbd_handle_reply_err(opt
, type
, errp
) < 0) {
147 if (read_sync(ioc
, &len
, sizeof(len
)) != sizeof(len
)) {
148 error_setg(errp
, "failed to read option length");
151 len
= be32_to_cpu(len
);
153 if (type
== NBD_REP_ACK
) {
155 error_setg(errp
, "length too long for option end");
158 } else if (type
== NBD_REP_SERVER
) {
159 if (read_sync(ioc
, &namelen
, sizeof(namelen
)) != sizeof(namelen
)) {
160 error_setg(errp
, "failed to read option name length");
163 namelen
= be32_to_cpu(namelen
);
164 if (len
!= (namelen
+ sizeof(namelen
))) {
165 error_setg(errp
, "incorrect option mame length");
169 error_setg(errp
, "export name length too long %d", namelen
);
173 *name
= g_new0(char, namelen
+ 1);
174 if (read_sync(ioc
, *name
, namelen
) != namelen
) {
175 error_setg(errp
, "failed to read export name");
180 (*name
)[namelen
] = '\0';
182 error_setg(errp
, "Unexpected reply type %x expected %x",
183 type
, NBD_REP_SERVER
);
190 static int nbd_receive_query_exports(QIOChannel
*ioc
,
191 const char *wantname
,
194 uint64_t magic
= cpu_to_be64(NBD_OPTS_MAGIC
);
195 uint32_t opt
= cpu_to_be32(NBD_OPT_LIST
);
197 bool foundExport
= false;
199 TRACE("Querying export list");
200 if (write_sync(ioc
, &magic
, sizeof(magic
)) != sizeof(magic
)) {
201 error_setg(errp
, "Failed to send list option magic");
205 if (write_sync(ioc
, &opt
, sizeof(opt
)) != sizeof(opt
)) {
206 error_setg(errp
, "Failed to send list option number");
210 if (write_sync(ioc
, &length
, sizeof(length
)) != sizeof(length
)) {
211 error_setg(errp
, "Failed to send list option length");
215 TRACE("Reading available export names");
218 int ret
= nbd_receive_list(ioc
, &name
, errp
);
226 /* Server doesn't support export listing, so
227 * we will just assume an export with our
228 * wanted name exists */
233 TRACE("End of export name list");
236 if (g_str_equal(name
, wantname
)) {
238 TRACE("Found desired export name '%s'", name
);
240 TRACE("Ignored export name '%s'", name
);
246 error_setg(errp
, "No export with name '%s' available", wantname
);
253 static QIOChannel
*nbd_receive_starttls(QIOChannel
*ioc
,
254 QCryptoTLSCreds
*tlscreds
,
255 const char *hostname
, Error
**errp
)
257 uint64_t magic
= cpu_to_be64(NBD_OPTS_MAGIC
);
258 uint32_t opt
= cpu_to_be32(NBD_OPT_STARTTLS
);
262 struct NBDTLSHandshakeData data
= { 0 };
264 TRACE("Requesting TLS from server");
265 if (write_sync(ioc
, &magic
, sizeof(magic
)) != sizeof(magic
)) {
266 error_setg(errp
, "Failed to send option magic");
270 if (write_sync(ioc
, &opt
, sizeof(opt
)) != sizeof(opt
)) {
271 error_setg(errp
, "Failed to send option number");
275 if (write_sync(ioc
, &length
, sizeof(length
)) != sizeof(length
)) {
276 error_setg(errp
, "Failed to send option length");
280 TRACE("Getting TLS reply from server1");
281 if (read_sync(ioc
, &magic
, sizeof(magic
)) != sizeof(magic
)) {
282 error_setg(errp
, "failed to read option magic");
285 magic
= be64_to_cpu(magic
);
286 if (magic
!= NBD_REP_MAGIC
) {
287 error_setg(errp
, "Unexpected option magic");
290 TRACE("Getting TLS reply from server2");
291 if (read_sync(ioc
, &opt
, sizeof(opt
)) != sizeof(opt
)) {
292 error_setg(errp
, "failed to read option");
295 opt
= be32_to_cpu(opt
);
296 if (opt
!= NBD_OPT_STARTTLS
) {
297 error_setg(errp
, "Unexpected option type %x expected %x",
298 opt
, NBD_OPT_STARTTLS
);
302 TRACE("Getting TLS reply from server");
303 if (read_sync(ioc
, &type
, sizeof(type
)) != sizeof(type
)) {
304 error_setg(errp
, "failed to read option type");
307 type
= be32_to_cpu(type
);
308 if (type
!= NBD_REP_ACK
) {
309 error_setg(errp
, "Server rejected request to start TLS %x",
314 TRACE("Getting TLS reply from server");
315 if (read_sync(ioc
, &length
, sizeof(length
)) != sizeof(length
)) {
316 error_setg(errp
, "failed to read option length");
319 length
= be32_to_cpu(length
);
321 error_setg(errp
, "Start TLS reponse was not zero %x",
326 TRACE("TLS request approved, setting up TLS");
327 tioc
= qio_channel_tls_new_client(ioc
, tlscreds
, hostname
, errp
);
331 data
.loop
= g_main_loop_new(g_main_context_default(), FALSE
);
332 TRACE("Starting TLS hanshake");
333 qio_channel_tls_handshake(tioc
,
338 if (!data
.complete
) {
339 g_main_loop_run(data
.loop
);
341 g_main_loop_unref(data
.loop
);
343 error_propagate(errp
, data
.error
);
344 object_unref(OBJECT(tioc
));
348 return QIO_CHANNEL(tioc
);
352 int nbd_receive_negotiate(QIOChannel
*ioc
, const char *name
, uint32_t *flags
,
353 QCryptoTLSCreds
*tlscreds
, const char *hostname
,
355 off_t
*size
, Error
**errp
)
361 TRACE("Receiving negotiation tlscreds=%p hostname=%s.",
362 tlscreds
, hostname
? hostname
: "<null>");
369 if (tlscreds
&& !outioc
) {
370 error_setg(errp
, "Output I/O channel required for TLS");
374 if (read_sync(ioc
, buf
, 8) != 8) {
375 error_setg(errp
, "Failed to read data");
380 if (strlen(buf
) == 0) {
381 error_setg(errp
, "Server connection closed unexpectedly");
385 TRACE("Magic is %c%c%c%c%c%c%c%c",
386 qemu_isprint(buf
[0]) ? buf
[0] : '.',
387 qemu_isprint(buf
[1]) ? buf
[1] : '.',
388 qemu_isprint(buf
[2]) ? buf
[2] : '.',
389 qemu_isprint(buf
[3]) ? buf
[3] : '.',
390 qemu_isprint(buf
[4]) ? buf
[4] : '.',
391 qemu_isprint(buf
[5]) ? buf
[5] : '.',
392 qemu_isprint(buf
[6]) ? buf
[6] : '.',
393 qemu_isprint(buf
[7]) ? buf
[7] : '.');
395 if (memcmp(buf
, "NBDMAGIC", 8) != 0) {
396 error_setg(errp
, "Invalid magic received");
400 if (read_sync(ioc
, &magic
, sizeof(magic
)) != sizeof(magic
)) {
401 error_setg(errp
, "Failed to read magic");
404 magic
= be64_to_cpu(magic
);
405 TRACE("Magic is 0x%" PRIx64
, magic
);
407 if (magic
== NBD_OPTS_MAGIC
) {
408 uint32_t clientflags
= 0;
411 uint16_t globalflags
;
412 uint16_t exportflags
;
413 bool fixedNewStyle
= false;
415 if (read_sync(ioc
, &globalflags
, sizeof(globalflags
)) !=
416 sizeof(globalflags
)) {
417 error_setg(errp
, "Failed to read server flags");
420 globalflags
= be16_to_cpu(globalflags
);
421 *flags
= globalflags
<< 16;
422 TRACE("Global flags are %x", globalflags
);
423 if (globalflags
& NBD_FLAG_FIXED_NEWSTYLE
) {
424 fixedNewStyle
= true;
425 TRACE("Server supports fixed new style");
426 clientflags
|= NBD_FLAG_C_FIXED_NEWSTYLE
;
428 /* client requested flags */
429 clientflags
= cpu_to_be32(clientflags
);
430 if (write_sync(ioc
, &clientflags
, sizeof(clientflags
)) !=
431 sizeof(clientflags
)) {
432 error_setg(errp
, "Failed to send clientflags field");
437 *outioc
= nbd_receive_starttls(ioc
, tlscreds
, hostname
, errp
);
443 error_setg(errp
, "Server does not support STARTTLS");
448 TRACE("Using default NBD export name \"\"");
452 /* Check our desired export is present in the
453 * server export list. Since NBD_OPT_EXPORT_NAME
454 * cannot return an error message, running this
455 * query gives us good error reporting if the
456 * server required TLS
458 if (nbd_receive_query_exports(ioc
, name
, errp
) < 0) {
462 /* write the export name */
463 magic
= cpu_to_be64(magic
);
464 if (write_sync(ioc
, &magic
, sizeof(magic
)) != sizeof(magic
)) {
465 error_setg(errp
, "Failed to send export name magic");
468 opt
= cpu_to_be32(NBD_OPT_EXPORT_NAME
);
469 if (write_sync(ioc
, &opt
, sizeof(opt
)) != sizeof(opt
)) {
470 error_setg(errp
, "Failed to send export name option number");
473 namesize
= cpu_to_be32(strlen(name
));
474 if (write_sync(ioc
, &namesize
, sizeof(namesize
)) !=
476 error_setg(errp
, "Failed to send export name length");
479 if (write_sync(ioc
, (char *)name
, strlen(name
)) != strlen(name
)) {
480 error_setg(errp
, "Failed to send export name");
484 if (read_sync(ioc
, &s
, sizeof(s
)) != sizeof(s
)) {
485 error_setg(errp
, "Failed to read export length");
488 *size
= be64_to_cpu(s
);
489 TRACE("Size is %" PRIu64
, *size
);
491 if (read_sync(ioc
, &exportflags
, sizeof(exportflags
)) !=
492 sizeof(exportflags
)) {
493 error_setg(errp
, "Failed to read export flags");
496 exportflags
= be16_to_cpu(exportflags
);
497 *flags
|= exportflags
;
498 TRACE("Export flags are %x", exportflags
);
499 } else if (magic
== NBD_CLIENT_MAGIC
) {
501 error_setg(errp
, "Server does not support export names");
505 error_setg(errp
, "Server does not support STARTTLS");
509 if (read_sync(ioc
, &s
, sizeof(s
)) != sizeof(s
)) {
510 error_setg(errp
, "Failed to read export length");
513 *size
= be64_to_cpu(s
);
514 TRACE("Size is %" PRIu64
, *size
);
516 if (read_sync(ioc
, flags
, sizeof(*flags
)) != sizeof(*flags
)) {
517 error_setg(errp
, "Failed to read export flags");
520 *flags
= be32_to_cpup(flags
);
522 error_setg(errp
, "Bad magic received");
526 if (read_sync(ioc
, &buf
, 124) != 124) {
527 error_setg(errp
, "Failed to read reserved block");
537 int nbd_init(int fd
, QIOChannelSocket
*sioc
, uint32_t flags
, off_t size
)
539 TRACE("Setting NBD socket");
541 if (ioctl(fd
, NBD_SET_SOCK
, sioc
->fd
) < 0) {
543 LOG("Failed to set NBD socket");
547 TRACE("Setting block size to %lu", (unsigned long)BDRV_SECTOR_SIZE
);
549 if (ioctl(fd
, NBD_SET_BLKSIZE
, (size_t)BDRV_SECTOR_SIZE
) < 0) {
551 LOG("Failed setting NBD block size");
555 TRACE("Setting size to %zd block(s)", (size_t)(size
/ BDRV_SECTOR_SIZE
));
557 if (ioctl(fd
, NBD_SET_SIZE_BLOCKS
, (size_t)(size
/ BDRV_SECTOR_SIZE
)) < 0) {
559 LOG("Failed setting size (in blocks)");
563 if (ioctl(fd
, NBD_SET_FLAGS
, flags
) < 0) {
564 if (errno
== ENOTTY
) {
565 int read_only
= (flags
& NBD_FLAG_READ_ONLY
) != 0;
566 TRACE("Setting readonly attribute");
568 if (ioctl(fd
, BLKROSET
, (unsigned long) &read_only
) < 0) {
570 LOG("Failed setting read-only attribute");
575 LOG("Failed setting flags");
580 TRACE("Negotiation ended");
585 int nbd_client(int fd
)
590 TRACE("Doing NBD loop");
592 ret
= ioctl(fd
, NBD_DO_IT
);
593 if (ret
< 0 && errno
== EPIPE
) {
594 /* NBD_DO_IT normally returns EPIPE when someone has disconnected
595 * the socket via NBD_DISCONNECT. We do not want to return 1 in
602 TRACE("NBD loop returned %d: %s", ret
, strerror(serrno
));
604 TRACE("Clearing NBD queue");
605 ioctl(fd
, NBD_CLEAR_QUE
);
607 TRACE("Clearing NBD socket");
608 ioctl(fd
, NBD_CLEAR_SOCK
);
614 int nbd_init(int fd
, QIOChannelSocket
*ioc
, uint32_t flags
, off_t size
)
619 int nbd_client(int fd
)
625 ssize_t
nbd_send_request(QIOChannel
*ioc
, struct nbd_request
*request
)
627 uint8_t buf
[NBD_REQUEST_SIZE
];
630 cpu_to_be32w((uint32_t*)buf
, NBD_REQUEST_MAGIC
);
631 cpu_to_be32w((uint32_t*)(buf
+ 4), request
->type
);
632 cpu_to_be64w((uint64_t*)(buf
+ 8), request
->handle
);
633 cpu_to_be64w((uint64_t*)(buf
+ 16), request
->from
);
634 cpu_to_be32w((uint32_t*)(buf
+ 24), request
->len
);
636 TRACE("Sending request to client: "
637 "{ .from = %" PRIu64
", .len = %u, .handle = %" PRIu64
", .type=%i}",
638 request
->from
, request
->len
, request
->handle
, request
->type
);
640 ret
= write_sync(ioc
, buf
, sizeof(buf
));
645 if (ret
!= sizeof(buf
)) {
646 LOG("writing to socket failed");
652 ssize_t
nbd_receive_reply(QIOChannel
*ioc
, struct nbd_reply
*reply
)
654 uint8_t buf
[NBD_REPLY_SIZE
];
658 ret
= read_sync(ioc
, buf
, sizeof(buf
));
663 if (ret
!= sizeof(buf
)) {
669 [ 0 .. 3] magic (NBD_REPLY_MAGIC)
670 [ 4 .. 7] error (0 == no error)
674 magic
= be32_to_cpup((uint32_t*)buf
);
675 reply
->error
= be32_to_cpup((uint32_t*)(buf
+ 4));
676 reply
->handle
= be64_to_cpup((uint64_t*)(buf
+ 8));
678 reply
->error
= nbd_errno_to_system_errno(reply
->error
);
681 "{ magic = 0x%x, .error = %d, handle = %" PRIu64
" }",
682 magic
, reply
->error
, reply
->handle
);
684 if (magic
!= NBD_REPLY_MAGIC
) {
685 LOG("invalid magic (got 0x%x)", magic
);