2 * Copyright (C) 2016 Red Hat, Inc.
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)
47 TRACE("Squashing unexpected error %d to EINVAL", err);
56 /* Definitions for opaque data types */
58 static QTAILQ_HEAD(, NBDExport) exports = QTAILQ_HEAD_INITIALIZER(exports);
60 /* That's all folks */
62 /* Basic flow for negotiation
89 /* Discard length bytes from channel. Return -errno on failure and 0 on
91 static int nbd_drop(QIOChannel *ioc, size_t size, Error **errp)
97 buffer = sizeof(small) >= size ? small : g_malloc(MIN(65536, size));
99 ssize_t count = MIN(65536, size);
100 ret = nbd_read(ioc, buffer, MIN(65536, size), errp);
109 if (buffer != small) {
115 /* Send an option request.
117 * The request is for option @opt, with @data containing @len bytes of
118 * additional payload for the request (@len may be -1 to treat @data as
119 * a C string; and @data may be NULL if @len is 0).
120 * Return 0 if successful, -1 with errp set if it is impossible to
122 static int nbd_send_option_request(QIOChannel *ioc, uint32_t opt,
123 uint32_t len, const char *data,
127 QEMU_BUILD_BUG_ON(sizeof(req) != 16);
130 req.length = len = strlen(data);
132 TRACE("Sending option request %" PRIu32", len %" PRIu32, opt, len);
134 stq_be_p(&req.magic, NBD_OPTS_MAGIC);
135 stl_be_p(&req.option, opt);
136 stl_be_p(&req.length, len);
138 if (nbd_write(ioc, &req, sizeof(req), errp) < 0) {
139 error_prepend(errp, "Failed to send option request header");
143 if (len && nbd_write(ioc, (char *) data, len, errp) < 0) {
144 error_prepend(errp, "Failed to send option request data");
151 /* Send NBD_OPT_ABORT as a courtesy to let the server know that we are
152 * not going to attempt further negotiation. */
153 static void nbd_send_opt_abort(QIOChannel *ioc)
155 /* Technically, a compliant server is supposed to reply to us; but
156 * older servers disconnected instead. At any rate, we're allowed
157 * to disconnect without waiting for the server reply, so we don't
158 * even care if the request makes it to the server, let alone
159 * waiting around for whether the server replies. */
160 nbd_send_option_request(ioc, NBD_OPT_ABORT, 0, NULL, NULL);
164 /* Receive the header of an option reply, which should match the given
165 * opt. Read through the length field, but NOT the length bytes of
166 * payload. Return 0 if successful, -1 with errp set if it is
167 * impossible to continue. */
168 static int nbd_receive_option_reply(QIOChannel *ioc, uint32_t opt,
169 nbd_opt_reply *reply, Error **errp)
171 QEMU_BUILD_BUG_ON(sizeof(*reply) != 20);
172 if (nbd_read(ioc, reply, sizeof(*reply), errp) < 0) {
173 error_prepend(errp, "failed to read option reply");
174 nbd_send_opt_abort(ioc);
177 be64_to_cpus(&reply->magic);
178 be32_to_cpus(&reply->option);
179 be32_to_cpus(&reply->type);
180 be32_to_cpus(&reply->length);
182 TRACE("Received option reply %" PRIx32", type %" PRIx32", len %" PRIu32,
183 reply->option, reply->type, reply->length);
185 if (reply->magic != NBD_REP_MAGIC) {
186 error_setg(errp, "Unexpected option reply magic");
187 nbd_send_opt_abort(ioc);
190 if (reply->option != opt) {
191 error_setg(errp, "Unexpected option type %x expected %x",
193 nbd_send_opt_abort(ioc);
199 /* If reply represents success, return 1 without further action.
200 * If reply represents an error, consume the optional payload of
201 * the packet on ioc. Then return 0 for unsupported (so the client
202 * can fall back to other approaches), or -1 with errp set for other
205 static int nbd_handle_reply_err(QIOChannel *ioc, nbd_opt_reply *reply,
211 if (!(reply->type & (1 << 31))) {
216 if (reply->length > NBD_MAX_BUFFER_SIZE) {
217 error_setg(errp, "server's error message is too long");
220 msg = g_malloc(reply->length + 1);
221 if (nbd_read(ioc, msg, reply->length, errp) < 0) {
222 error_prepend(errp, "failed to read option error message");
225 msg[reply->length] = '\0';
228 switch (reply->type) {
229 case NBD_REP_ERR_UNSUP:
230 TRACE("server doesn't understand request %" PRIx32
231 ", attempting fallback", reply->option);
235 case NBD_REP_ERR_POLICY:
236 error_setg(errp, "Denied by server for option %" PRIx32,
240 case NBD_REP_ERR_INVALID:
241 error_setg(errp, "Invalid data length for option %" PRIx32,
245 case NBD_REP_ERR_PLATFORM:
246 error_setg(errp, "Server lacks support for option %" PRIx32,
250 case NBD_REP_ERR_TLS_REQD:
251 error_setg(errp, "TLS negotiation required before option %" PRIx32,
255 case NBD_REP_ERR_SHUTDOWN:
256 error_setg(errp, "Server shutting down before option %" PRIx32,
261 error_setg(errp, "Unknown error code when asking for option %" PRIx32,
267 error_append_hint(errp, "%s\n", msg);
273 nbd_send_opt_abort(ioc);
278 /* Process another portion of the NBD_OPT_LIST reply. Set *@match if
279 * the current reply matches @want or if the server does not support
280 * NBD_OPT_LIST, otherwise leave @match alone. Return 0 if iteration
281 * is complete, positive if more replies are expected, or negative
282 * with @errp set if an unrecoverable error occurred. */
283 static int nbd_receive_list(QIOChannel *ioc, const char *want, bool *match,
289 char name[NBD_MAX_NAME_SIZE + 1];
292 if (nbd_receive_option_reply(ioc, NBD_OPT_LIST, &reply, errp) < 0) {
295 error = nbd_handle_reply_err(ioc, &reply, errp);
297 /* The server did not support NBD_OPT_LIST, so set *match on
298 * the assumption that any name will be accepted. */
304 if (reply.type == NBD_REP_ACK) {
306 error_setg(errp, "length too long for option end");
307 nbd_send_opt_abort(ioc);
311 } else if (reply.type != NBD_REP_SERVER) {
312 error_setg(errp, "Unexpected reply type %" PRIx32 " expected %x",
313 reply.type, NBD_REP_SERVER);
314 nbd_send_opt_abort(ioc);
318 if (len < sizeof(namelen) || len > NBD_MAX_BUFFER_SIZE) {
319 error_setg(errp, "incorrect option length %" PRIu32, len);
320 nbd_send_opt_abort(ioc);
323 if (nbd_read(ioc, &namelen, sizeof(namelen), errp) < 0) {
324 error_prepend(errp, "failed to read option name length");
325 nbd_send_opt_abort(ioc);
328 namelen = be32_to_cpu(namelen);
329 len -= sizeof(namelen);
331 error_setg(errp, "incorrect option name length");
332 nbd_send_opt_abort(ioc);
335 if (namelen != strlen(want)) {
336 if (nbd_drop(ioc, len, errp) < 0) {
337 error_prepend(errp, "failed to skip export name with wrong length");
338 nbd_send_opt_abort(ioc);
344 assert(namelen < sizeof(name));
345 if (nbd_read(ioc, name, namelen, errp) < 0) {
346 error_prepend(errp, "failed to read export name");
347 nbd_send_opt_abort(ioc);
350 name[namelen] = '\0';
352 if (nbd_drop(ioc, len, errp) < 0) {
353 error_prepend(errp, "failed to read export description");
354 nbd_send_opt_abort(ioc);
357 if (!strcmp(name, want)) {
364 /* Return -1 on failure, 0 if wantname is an available export. */
365 static int nbd_receive_query_exports(QIOChannel *ioc,
366 const char *wantname,
369 bool foundExport = false;
371 TRACE("Querying export list for '%s'", wantname);
372 if (nbd_send_option_request(ioc, NBD_OPT_LIST, 0, NULL, errp) < 0) {
376 TRACE("Reading available export names");
378 int ret = nbd_receive_list(ioc, wantname, &foundExport, errp);
381 /* Server gave unexpected reply */
383 } else if (ret == 0) {
384 /* Done iterating. */
386 error_setg(errp, "No export with name '%s' available",
388 nbd_send_opt_abort(ioc);
391 TRACE("Found desired export name '%s'", wantname);
397 static QIOChannel *nbd_receive_starttls(QIOChannel *ioc,
398 QCryptoTLSCreds *tlscreds,
399 const char *hostname, Error **errp)
403 struct NBDTLSHandshakeData data = { 0 };
405 TRACE("Requesting TLS from server");
406 if (nbd_send_option_request(ioc, NBD_OPT_STARTTLS, 0, NULL, errp) < 0) {
410 TRACE("Getting TLS reply from server");
411 if (nbd_receive_option_reply(ioc, NBD_OPT_STARTTLS, &reply, errp) < 0) {
415 if (reply.type != NBD_REP_ACK) {
416 error_setg(errp, "Server rejected request to start TLS %" PRIx32,
418 nbd_send_opt_abort(ioc);
422 if (reply.length != 0) {
423 error_setg(errp, "Start TLS response was not zero %" PRIu32,
425 nbd_send_opt_abort(ioc);
429 TRACE("TLS request approved, setting up TLS");
430 tioc = qio_channel_tls_new_client(ioc, tlscreds, hostname, errp);
434 qio_channel_set_name(QIO_CHANNEL(tioc), "nbd-client-tls");
435 data.loop = g_main_loop_new(g_main_context_default(), FALSE);
436 TRACE("Starting TLS handshake");
437 qio_channel_tls_handshake(tioc,
442 if (!data.complete) {
443 g_main_loop_run(data.loop);
445 g_main_loop_unref(data.loop);
447 error_propagate(errp, data.error);
448 object_unref(OBJECT(tioc));
452 return QIO_CHANNEL(tioc);
456 int nbd_receive_negotiate(QIOChannel *ioc, const char *name, uint16_t *flags,
457 QCryptoTLSCreds *tlscreds, const char *hostname,
459 off_t *size, Error **errp)
466 TRACE("Receiving negotiation tlscreds=%p hostname=%s.",
467 tlscreds, hostname ? hostname : "<null>");
474 if (tlscreds && !outioc) {
475 error_setg(errp, "Output I/O channel required for TLS");
479 if (nbd_read(ioc, buf, 8, errp) < 0) {
480 error_prepend(errp, "Failed to read data");
485 if (strlen(buf) == 0) {
486 error_setg(errp, "Server connection closed unexpectedly");
490 TRACE("Magic is %c%c%c%c%c%c%c%c",
491 qemu_isprint(buf[0]) ? buf[0] : '.',
492 qemu_isprint(buf[1]) ? buf[1] : '.',
493 qemu_isprint(buf[2]) ? buf[2] : '.',
494 qemu_isprint(buf[3]) ? buf[3] : '.',
495 qemu_isprint(buf[4]) ? buf[4] : '.',
496 qemu_isprint(buf[5]) ? buf[5] : '.',
497 qemu_isprint(buf[6]) ? buf[6] : '.',
498 qemu_isprint(buf[7]) ? buf[7] : '.');
500 if (memcmp(buf, "NBDMAGIC", 8) != 0) {
501 error_setg(errp, "Invalid magic received");
505 if (nbd_read(ioc, &magic, sizeof(magic), errp) < 0) {
506 error_prepend(errp, "Failed to read magic");
509 magic = be64_to_cpu(magic);
510 TRACE("Magic is 0x%" PRIx64, magic);
512 if (magic == NBD_OPTS_MAGIC) {
513 uint32_t clientflags = 0;
514 uint16_t globalflags;
515 bool fixedNewStyle = false;
517 if (nbd_read(ioc, &globalflags, sizeof(globalflags), errp) < 0) {
518 error_prepend(errp, "Failed to read server flags");
521 globalflags = be16_to_cpu(globalflags);
522 TRACE("Global flags are %" PRIx32, globalflags);
523 if (globalflags & NBD_FLAG_FIXED_NEWSTYLE) {
524 fixedNewStyle = true;
525 TRACE("Server supports fixed new style");
526 clientflags |= NBD_FLAG_C_FIXED_NEWSTYLE;
528 if (globalflags & NBD_FLAG_NO_ZEROES) {
530 TRACE("Server supports no zeroes");
531 clientflags |= NBD_FLAG_C_NO_ZEROES;
533 /* client requested flags */
534 clientflags = cpu_to_be32(clientflags);
535 if (nbd_write(ioc, &clientflags, sizeof(clientflags), errp) < 0) {
536 error_prepend(errp, "Failed to send clientflags field");
541 *outioc = nbd_receive_starttls(ioc, tlscreds, hostname, errp);
547 error_setg(errp, "Server does not support STARTTLS");
552 TRACE("Using default NBD export name \"\"");
556 /* Check our desired export is present in the
557 * server export list. Since NBD_OPT_EXPORT_NAME
558 * cannot return an error message, running this
559 * query gives us good error reporting if the
560 * server required TLS
562 if (nbd_receive_query_exports(ioc, name, errp) < 0) {
566 /* write the export name request */
567 if (nbd_send_option_request(ioc, NBD_OPT_EXPORT_NAME, -1, name,
572 /* Read the response */
573 if (nbd_read(ioc, &s, sizeof(s), errp) < 0) {
574 error_prepend(errp, "Failed to read export length");
577 *size = be64_to_cpu(s);
579 if (nbd_read(ioc, flags, sizeof(*flags), errp) < 0) {
580 error_prepend(errp, "Failed to read export flags");
584 } else if (magic == NBD_CLIENT_MAGIC) {
588 error_setg(errp, "Server does not support export names");
592 error_setg(errp, "Server does not support STARTTLS");
596 if (nbd_read(ioc, &s, sizeof(s), errp) < 0) {
597 error_prepend(errp, "Failed to read export length");
600 *size = be64_to_cpu(s);
601 TRACE("Size is %" PRIu64, *size);
603 if (nbd_read(ioc, &oldflags, sizeof(oldflags), errp) < 0) {
604 error_prepend(errp, "Failed to read export flags");
607 be32_to_cpus(&oldflags);
608 if (oldflags & ~0xffff) {
609 error_setg(errp, "Unexpected export flags %0x" PRIx32, oldflags);
614 error_setg(errp, "Bad magic received");
618 TRACE("Size is %" PRIu64 ", export flags %" PRIx16, *size, *flags);
619 if (zeroes && nbd_drop(ioc, 124, errp) < 0) {
620 error_prepend(errp, "Failed to read reserved block");
630 int nbd_init(int fd, QIOChannelSocket *sioc, uint16_t flags, off_t size,
633 unsigned long sectors = size / BDRV_SECTOR_SIZE;
634 if (size / BDRV_SECTOR_SIZE != sectors) {
635 error_setg(errp, "Export size %lld too large for 32-bit kernel",
640 TRACE("Setting NBD socket");
642 if (ioctl(fd, NBD_SET_SOCK, (unsigned long) sioc->fd) < 0) {
644 error_setg(errp, "Failed to set NBD socket");
648 TRACE("Setting block size to %lu", (unsigned long)BDRV_SECTOR_SIZE);
650 if (ioctl(fd, NBD_SET_BLKSIZE, (unsigned long)BDRV_SECTOR_SIZE) < 0) {
652 error_setg(errp, "Failed setting NBD block size");
656 TRACE("Setting size to %lu block(s)", sectors);
657 if (size % BDRV_SECTOR_SIZE) {
658 TRACE("Ignoring trailing %d bytes of export",
659 (int) (size % BDRV_SECTOR_SIZE));
662 if (ioctl(fd, NBD_SET_SIZE_BLOCKS, sectors) < 0) {
664 error_setg(errp, "Failed setting size (in blocks)");
668 if (ioctl(fd, NBD_SET_FLAGS, (unsigned long) flags) < 0) {
669 if (errno == ENOTTY) {
670 int read_only = (flags & NBD_FLAG_READ_ONLY) != 0;
671 TRACE("Setting readonly attribute");
673 if (ioctl(fd, BLKROSET, (unsigned long) &read_only) < 0) {
675 error_setg(errp, "Failed setting read-only attribute");
680 error_setg(errp, "Failed setting flags");
685 TRACE("Negotiation ended");
690 int nbd_client(int fd)
695 TRACE("Doing NBD loop");
697 ret = ioctl(fd, NBD_DO_IT);
698 if (ret < 0 && errno == EPIPE) {
699 /* NBD_DO_IT normally returns EPIPE when someone has disconnected
700 * the socket via NBD_DISCONNECT. We do not want to return 1 in
707 TRACE("NBD loop returned %d: %s", ret, strerror(serrno));
709 TRACE("Clearing NBD queue");
710 ioctl(fd, NBD_CLEAR_QUE);
712 TRACE("Clearing NBD socket");
713 ioctl(fd, NBD_CLEAR_SOCK);
719 int nbd_disconnect(int fd)
721 ioctl(fd, NBD_CLEAR_QUE);
722 ioctl(fd, NBD_DISCONNECT);
723 ioctl(fd, NBD_CLEAR_SOCK);
728 int nbd_init(int fd, QIOChannelSocket *ioc, uint16_t flags, off_t size,
731 error_setg(errp, "nbd_init is only supported on Linux");
735 int nbd_client(int fd)
739 int nbd_disconnect(int fd)
745 ssize_t nbd_send_request(QIOChannel *ioc, NBDRequest *request)
747 uint8_t buf[NBD_REQUEST_SIZE];
749 TRACE("Sending request to server: "
750 "{ .from = %" PRIu64", .len = %" PRIu32 ", .handle = %" PRIu64
751 ", .flags = %" PRIx16 ", .type = %" PRIu16 " }",
752 request->from, request->len, request->handle,
753 request->flags, request->type);
755 stl_be_p(buf, NBD_REQUEST_MAGIC);
756 stw_be_p(buf + 4, request->flags);
757 stw_be_p(buf + 6, request->type);
758 stq_be_p(buf + 8, request->handle);
759 stq_be_p(buf + 16, request->from);
760 stl_be_p(buf + 24, request->len);
762 return nbd_write(ioc, buf, sizeof(buf), NULL);
765 ssize_t nbd_receive_reply(QIOChannel *ioc, NBDReply *reply, Error **errp)
767 uint8_t buf[NBD_REPLY_SIZE];
771 ret = nbd_read_eof(ioc, buf, sizeof(buf), errp);
776 if (ret != sizeof(buf)) {
777 error_setg(errp, "read failed");
782 [ 0 .. 3] magic (NBD_REPLY_MAGIC)
783 [ 4 .. 7] error (0 == no error)
787 magic = ldl_be_p(buf);
788 reply->error = ldl_be_p(buf + 4);
789 reply->handle = ldq_be_p(buf + 8);
791 reply->error = nbd_errno_to_system_errno(reply->error);
793 if (reply->error == ESHUTDOWN) {
794 /* This works even on mingw which lacks a native ESHUTDOWN */
795 error_setg(errp, "server shutting down");
798 TRACE("Got reply: { magic = 0x%" PRIx32 ", .error = % " PRId32
799 ", handle = %" PRIu64" }",
800 magic, reply->error, reply->handle);
802 if (magic != NBD_REPLY_MAGIC) {
803 error_setg(errp, "invalid magic (got 0x%" PRIx32 ")", magic);