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 static int nbd_handle_reply_err(uint32_t opt, uint32_t type, Error **errp)
78 if (!(type & (1 << 31))) {
83 case NBD_REP_ERR_UNSUP:
84 error_setg(errp, "Unsupported option type %x", opt);
87 case NBD_REP_ERR_POLICY:
88 error_setg(errp, "Denied by server for option %x", opt);
91 case NBD_REP_ERR_INVALID:
92 error_setg(errp, "Invalid data length for option %x", opt);
95 case NBD_REP_ERR_TLS_REQD:
96 error_setg(errp, "TLS negotiation required before option %x", opt);
100 error_setg(errp, "Unknown error code when asking for option %x", opt);
107 static int nbd_receive_list(QIOChannel *ioc, char **name, Error **errp)
116 if (read_sync(ioc, &magic, sizeof(magic)) != sizeof(magic)) {
117 error_setg(errp, "failed to read list option magic");
120 magic = be64_to_cpu(magic);
121 if (magic != NBD_REP_MAGIC) {
122 error_setg(errp, "Unexpected option list magic");
125 if (read_sync(ioc, &opt, sizeof(opt)) != sizeof(opt)) {
126 error_setg(errp, "failed to read list option");
129 opt = be32_to_cpu(opt);
130 if (opt != NBD_OPT_LIST) {
131 error_setg(errp, "Unexpected option type %x expected %x",
136 if (read_sync(ioc, &type, sizeof(type)) != sizeof(type)) {
137 error_setg(errp, "failed to read list option type");
140 type = be32_to_cpu(type);
141 if (type == NBD_REP_ERR_UNSUP) {
144 if (nbd_handle_reply_err(opt, type, errp) < 0) {
148 if (read_sync(ioc, &len, sizeof(len)) != sizeof(len)) {
149 error_setg(errp, "failed to read option length");
152 len = be32_to_cpu(len);
154 if (type == NBD_REP_ACK) {
156 error_setg(errp, "length too long for option end");
159 } else if (type == NBD_REP_SERVER) {
160 if (read_sync(ioc, &namelen, sizeof(namelen)) != sizeof(namelen)) {
161 error_setg(errp, "failed to read option name length");
164 namelen = be32_to_cpu(namelen);
165 if (len != (namelen + sizeof(namelen))) {
166 error_setg(errp, "incorrect option mame length");
170 error_setg(errp, "export name length too long %d", namelen);
174 *name = g_new0(char, namelen + 1);
175 if (read_sync(ioc, *name, namelen) != namelen) {
176 error_setg(errp, "failed to read export name");
181 (*name)[namelen] = '\0';
183 error_setg(errp, "Unexpected reply type %x expected %x",
184 type, NBD_REP_SERVER);
191 static int nbd_receive_query_exports(QIOChannel *ioc,
192 const char *wantname,
195 uint64_t magic = cpu_to_be64(NBD_OPTS_MAGIC);
196 uint32_t opt = cpu_to_be32(NBD_OPT_LIST);
198 bool foundExport = false;
200 TRACE("Querying export list");
201 if (write_sync(ioc, &magic, sizeof(magic)) != sizeof(magic)) {
202 error_setg(errp, "Failed to send list option magic");
206 if (write_sync(ioc, &opt, sizeof(opt)) != sizeof(opt)) {
207 error_setg(errp, "Failed to send list option number");
211 if (write_sync(ioc, &length, sizeof(length)) != sizeof(length)) {
212 error_setg(errp, "Failed to send list option length");
216 TRACE("Reading available export names");
219 int ret = nbd_receive_list(ioc, &name, errp);
227 /* Server doesn't support export listing, so
228 * we will just assume an export with our
229 * wanted name exists */
234 TRACE("End of export name list");
237 if (g_str_equal(name, wantname)) {
239 TRACE("Found desired export name '%s'", name);
241 TRACE("Ignored export name '%s'", name);
247 error_setg(errp, "No export with name '%s' available", wantname);
254 static QIOChannel *nbd_receive_starttls(QIOChannel *ioc,
255 QCryptoTLSCreds *tlscreds,
256 const char *hostname, Error **errp)
258 uint64_t magic = cpu_to_be64(NBD_OPTS_MAGIC);
259 uint32_t opt = cpu_to_be32(NBD_OPT_STARTTLS);
263 struct NBDTLSHandshakeData data = { 0 };
265 TRACE("Requesting TLS from server");
266 if (write_sync(ioc, &magic, sizeof(magic)) != sizeof(magic)) {
267 error_setg(errp, "Failed to send option magic");
271 if (write_sync(ioc, &opt, sizeof(opt)) != sizeof(opt)) {
272 error_setg(errp, "Failed to send option number");
276 if (write_sync(ioc, &length, sizeof(length)) != sizeof(length)) {
277 error_setg(errp, "Failed to send option length");
281 TRACE("Getting TLS reply from server1");
282 if (read_sync(ioc, &magic, sizeof(magic)) != sizeof(magic)) {
283 error_setg(errp, "failed to read option magic");
286 magic = be64_to_cpu(magic);
287 if (magic != NBD_REP_MAGIC) {
288 error_setg(errp, "Unexpected option magic");
291 TRACE("Getting TLS reply from server2");
292 if (read_sync(ioc, &opt, sizeof(opt)) != sizeof(opt)) {
293 error_setg(errp, "failed to read option");
296 opt = be32_to_cpu(opt);
297 if (opt != NBD_OPT_STARTTLS) {
298 error_setg(errp, "Unexpected option type %x expected %x",
299 opt, NBD_OPT_STARTTLS);
303 TRACE("Getting TLS reply from server");
304 if (read_sync(ioc, &type, sizeof(type)) != sizeof(type)) {
305 error_setg(errp, "failed to read option type");
308 type = be32_to_cpu(type);
309 if (type != NBD_REP_ACK) {
310 error_setg(errp, "Server rejected request to start TLS %x",
315 TRACE("Getting TLS reply from server");
316 if (read_sync(ioc, &length, sizeof(length)) != sizeof(length)) {
317 error_setg(errp, "failed to read option length");
320 length = be32_to_cpu(length);
322 error_setg(errp, "Start TLS reponse was not zero %x",
327 TRACE("TLS request approved, setting up TLS");
328 tioc = qio_channel_tls_new_client(ioc, tlscreds, hostname, errp);
332 data.loop = g_main_loop_new(g_main_context_default(), FALSE);
333 TRACE("Starting TLS hanshake");
334 qio_channel_tls_handshake(tioc,
339 if (!data.complete) {
340 g_main_loop_run(data.loop);
342 g_main_loop_unref(data.loop);
344 error_propagate(errp, data.error);
345 object_unref(OBJECT(tioc));
349 return QIO_CHANNEL(tioc);
353 int nbd_receive_negotiate(QIOChannel *ioc, const char *name, uint32_t *flags,
354 QCryptoTLSCreds *tlscreds, const char *hostname,
356 off_t *size, Error **errp)
362 TRACE("Receiving negotiation tlscreds=%p hostname=%s.",
363 tlscreds, hostname ? hostname : "<null>");
370 if (tlscreds && !outioc) {
371 error_setg(errp, "Output I/O channel required for TLS");
375 if (read_sync(ioc, buf, 8) != 8) {
376 error_setg(errp, "Failed to read data");
381 if (strlen(buf) == 0) {
382 error_setg(errp, "Server connection closed unexpectedly");
386 TRACE("Magic is %c%c%c%c%c%c%c%c",
387 qemu_isprint(buf[0]) ? buf[0] : '.',
388 qemu_isprint(buf[1]) ? buf[1] : '.',
389 qemu_isprint(buf[2]) ? buf[2] : '.',
390 qemu_isprint(buf[3]) ? buf[3] : '.',
391 qemu_isprint(buf[4]) ? buf[4] : '.',
392 qemu_isprint(buf[5]) ? buf[5] : '.',
393 qemu_isprint(buf[6]) ? buf[6] : '.',
394 qemu_isprint(buf[7]) ? buf[7] : '.');
396 if (memcmp(buf, "NBDMAGIC", 8) != 0) {
397 error_setg(errp, "Invalid magic received");
401 if (read_sync(ioc, &magic, sizeof(magic)) != sizeof(magic)) {
402 error_setg(errp, "Failed to read magic");
405 magic = be64_to_cpu(magic);
406 TRACE("Magic is 0x%" PRIx64, magic);
408 if (magic == NBD_OPTS_MAGIC) {
409 uint32_t clientflags = 0;
412 uint16_t globalflags;
413 uint16_t exportflags;
414 bool fixedNewStyle = false;
416 if (read_sync(ioc, &globalflags, sizeof(globalflags)) !=
417 sizeof(globalflags)) {
418 error_setg(errp, "Failed to read server flags");
421 globalflags = be16_to_cpu(globalflags);
422 *flags = globalflags << 16;
423 TRACE("Global flags are %x", globalflags);
424 if (globalflags & NBD_FLAG_FIXED_NEWSTYLE) {
425 fixedNewStyle = true;
426 TRACE("Server supports fixed new style");
427 clientflags |= NBD_FLAG_C_FIXED_NEWSTYLE;
429 /* client requested flags */
430 clientflags = cpu_to_be32(clientflags);
431 if (write_sync(ioc, &clientflags, sizeof(clientflags)) !=
432 sizeof(clientflags)) {
433 error_setg(errp, "Failed to send clientflags field");
438 *outioc = nbd_receive_starttls(ioc, tlscreds, hostname, errp);
444 error_setg(errp, "Server does not support STARTTLS");
449 TRACE("Using default NBD export name \"\"");
453 /* Check our desired export is present in the
454 * server export list. Since NBD_OPT_EXPORT_NAME
455 * cannot return an error message, running this
456 * query gives us good error reporting if the
457 * server required TLS
459 if (nbd_receive_query_exports(ioc, name, errp) < 0) {
463 /* write the export name */
464 magic = cpu_to_be64(magic);
465 if (write_sync(ioc, &magic, sizeof(magic)) != sizeof(magic)) {
466 error_setg(errp, "Failed to send export name magic");
469 opt = cpu_to_be32(NBD_OPT_EXPORT_NAME);
470 if (write_sync(ioc, &opt, sizeof(opt)) != sizeof(opt)) {
471 error_setg(errp, "Failed to send export name option number");
474 namesize = cpu_to_be32(strlen(name));
475 if (write_sync(ioc, &namesize, sizeof(namesize)) !=
477 error_setg(errp, "Failed to send export name length");
480 if (write_sync(ioc, (char *)name, strlen(name)) != strlen(name)) {
481 error_setg(errp, "Failed to send export name");
485 if (read_sync(ioc, &s, sizeof(s)) != sizeof(s)) {
486 error_setg(errp, "Failed to read export length");
489 *size = be64_to_cpu(s);
490 TRACE("Size is %" PRIu64, *size);
492 if (read_sync(ioc, &exportflags, sizeof(exportflags)) !=
493 sizeof(exportflags)) {
494 error_setg(errp, "Failed to read export flags");
497 exportflags = be16_to_cpu(exportflags);
498 *flags |= exportflags;
499 TRACE("Export flags are %x", exportflags);
500 } else if (magic == NBD_CLIENT_MAGIC) {
502 error_setg(errp, "Server does not support export names");
506 error_setg(errp, "Server does not support STARTTLS");
510 if (read_sync(ioc, &s, sizeof(s)) != sizeof(s)) {
511 error_setg(errp, "Failed to read export length");
514 *size = be64_to_cpu(s);
515 TRACE("Size is %" PRIu64, *size);
517 if (read_sync(ioc, flags, sizeof(*flags)) != sizeof(*flags)) {
518 error_setg(errp, "Failed to read export flags");
521 *flags = be32_to_cpup(flags);
523 error_setg(errp, "Bad magic received");
527 if (read_sync(ioc, &buf, 124) != 124) {
528 error_setg(errp, "Failed to read reserved block");
538 int nbd_init(int fd, QIOChannelSocket *sioc, uint32_t flags, off_t size)
540 TRACE("Setting NBD socket");
542 if (ioctl(fd, NBD_SET_SOCK, sioc->fd) < 0) {
544 LOG("Failed to set NBD socket");
548 TRACE("Setting block size to %lu", (unsigned long)BDRV_SECTOR_SIZE);
550 if (ioctl(fd, NBD_SET_BLKSIZE, (size_t)BDRV_SECTOR_SIZE) < 0) {
552 LOG("Failed setting NBD block size");
556 TRACE("Setting size to %zd block(s)", (size_t)(size / BDRV_SECTOR_SIZE));
558 if (ioctl(fd, NBD_SET_SIZE_BLOCKS, (size_t)(size / BDRV_SECTOR_SIZE)) < 0) {
560 LOG("Failed setting size (in blocks)");
564 if (ioctl(fd, NBD_SET_FLAGS, flags) < 0) {
565 if (errno == ENOTTY) {
566 int read_only = (flags & NBD_FLAG_READ_ONLY) != 0;
567 TRACE("Setting readonly attribute");
569 if (ioctl(fd, BLKROSET, (unsigned long) &read_only) < 0) {
571 LOG("Failed setting read-only attribute");
576 LOG("Failed setting flags");
581 TRACE("Negotiation ended");
586 int nbd_client(int fd)
591 TRACE("Doing NBD loop");
593 ret = ioctl(fd, NBD_DO_IT);
594 if (ret < 0 && errno == EPIPE) {
595 /* NBD_DO_IT normally returns EPIPE when someone has disconnected
596 * the socket via NBD_DISCONNECT. We do not want to return 1 in
603 TRACE("NBD loop returned %d: %s", ret, strerror(serrno));
605 TRACE("Clearing NBD queue");
606 ioctl(fd, NBD_CLEAR_QUE);
608 TRACE("Clearing NBD socket");
609 ioctl(fd, NBD_CLEAR_SOCK);
615 int nbd_init(int fd, QIOChannelSocket *ioc, uint32_t flags, off_t size)
620 int nbd_client(int fd)
626 ssize_t nbd_send_request(QIOChannel *ioc, struct nbd_request *request)
628 uint8_t buf[NBD_REQUEST_SIZE];
631 cpu_to_be32w((uint32_t*)buf, NBD_REQUEST_MAGIC);
632 cpu_to_be32w((uint32_t*)(buf + 4), request->type);
633 cpu_to_be64w((uint64_t*)(buf + 8), request->handle);
634 cpu_to_be64w((uint64_t*)(buf + 16), request->from);
635 cpu_to_be32w((uint32_t*)(buf + 24), request->len);
637 TRACE("Sending request to server: "
638 "{ .from = %" PRIu64", .len = %u, .handle = %" PRIu64", .type=%i}",
639 request->from, request->len, request->handle, request->type);
641 ret = write_sync(ioc, buf, sizeof(buf));
646 if (ret != sizeof(buf)) {
647 LOG("writing to socket failed");
653 ssize_t nbd_receive_reply(QIOChannel *ioc, struct nbd_reply *reply)
655 uint8_t buf[NBD_REPLY_SIZE];
659 ret = read_sync(ioc, buf, sizeof(buf));
664 if (ret != sizeof(buf)) {
670 [ 0 .. 3] magic (NBD_REPLY_MAGIC)
671 [ 4 .. 7] error (0 == no error)
675 magic = be32_to_cpup((uint32_t*)buf);
676 reply->error = be32_to_cpup((uint32_t*)(buf + 4));
677 reply->handle = be64_to_cpup((uint64_t*)(buf + 8));
679 reply->error = nbd_errno_to_system_errno(reply->error);
682 "{ magic = 0x%x, .error = %d, handle = %" PRIu64" }",
683 magic, reply->error, reply->handle);
685 if (magic != NBD_REPLY_MAGIC) {
686 LOG("invalid magic (got 0x%x)", magic);