nbd: More debug typo fixes, use correct formats
[qemu.git] / nbd / client.c
1 /*
2 * Copyright (C) 2005 Anthony Liguori <anthony@codemonkey.ws>
3 *
4 * Network Block Device Client Side
5 *
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.
9 *
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.
14 *
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/>.
17 */
18
19 #include "qemu/osdep.h"
20 #include "qapi/error.h"
21 #include "nbd-internal.h"
22
23 static int nbd_errno_to_system_errno(int err)
24 {
25 switch (err) {
26 case NBD_SUCCESS:
27 return 0;
28 case NBD_EPERM:
29 return EPERM;
30 case NBD_EIO:
31 return EIO;
32 case NBD_ENOMEM:
33 return ENOMEM;
34 case NBD_ENOSPC:
35 return ENOSPC;
36 case NBD_EINVAL:
37 default:
38 return EINVAL;
39 }
40 }
41
42 /* Definitions for opaque data types */
43
44 static QTAILQ_HEAD(, NBDExport) exports = QTAILQ_HEAD_INITIALIZER(exports);
45
46 /* That's all folks */
47
48 /* Basic flow for negotiation
49
50 Server Client
51 Negotiate
52
53 or
54
55 Server Client
56 Negotiate #1
57 Option
58 Negotiate #2
59
60 ----
61
62 followed by
63
64 Server Client
65 Request
66 Response
67 Request
68 Response
69 ...
70 ...
71 Request (type == 2)
72
73 */
74
75
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.
80 */
81 static int nbd_handle_reply_err(QIOChannel *ioc, uint32_t opt, uint32_t type,
82 Error **errp)
83 {
84 uint32_t len;
85 char *msg = NULL;
86 int result = -1;
87
88 if (!(type & (1 << 31))) {
89 return 1;
90 }
91
92 if (read_sync(ioc, &len, sizeof(len)) != sizeof(len)) {
93 error_setg(errp, "failed to read option length");
94 return -1;
95 }
96 len = be32_to_cpu(len);
97 if (len) {
98 if (len > NBD_MAX_BUFFER_SIZE) {
99 error_setg(errp, "server's error message is too long");
100 goto cleanup;
101 }
102 msg = g_malloc(len + 1);
103 if (read_sync(ioc, msg, len) != len) {
104 error_setg(errp, "failed to read option error message");
105 goto cleanup;
106 }
107 msg[len] = '\0';
108 }
109
110 switch (type) {
111 case NBD_REP_ERR_UNSUP:
112 TRACE("server doesn't understand request %" PRIx32
113 ", attempting fallback", opt);
114 result = 0;
115 goto cleanup;
116
117 case NBD_REP_ERR_POLICY:
118 error_setg(errp, "Denied by server for option %" PRIx32, opt);
119 break;
120
121 case NBD_REP_ERR_INVALID:
122 error_setg(errp, "Invalid data length for option %" PRIx32, opt);
123 break;
124
125 case NBD_REP_ERR_TLS_REQD:
126 error_setg(errp, "TLS negotiation required before option %" PRIx32,
127 opt);
128 break;
129
130 default:
131 error_setg(errp, "Unknown error code when asking for option %" PRIx32,
132 opt);
133 break;
134 }
135
136 if (msg) {
137 error_append_hint(errp, "%s\n", msg);
138 }
139
140 cleanup:
141 g_free(msg);
142 return result;
143 }
144
145 static int nbd_receive_list(QIOChannel *ioc, char **name, Error **errp)
146 {
147 uint64_t magic;
148 uint32_t opt;
149 uint32_t type;
150 uint32_t len;
151 uint32_t namelen;
152 int error;
153
154 *name = NULL;
155 if (read_sync(ioc, &magic, sizeof(magic)) != sizeof(magic)) {
156 error_setg(errp, "failed to read list option magic");
157 return -1;
158 }
159 magic = be64_to_cpu(magic);
160 if (magic != NBD_REP_MAGIC) {
161 error_setg(errp, "Unexpected option list magic");
162 return -1;
163 }
164 if (read_sync(ioc, &opt, sizeof(opt)) != sizeof(opt)) {
165 error_setg(errp, "failed to read list option");
166 return -1;
167 }
168 opt = be32_to_cpu(opt);
169 if (opt != NBD_OPT_LIST) {
170 error_setg(errp, "Unexpected option type %" PRIx32 " expected %x",
171 opt, NBD_OPT_LIST);
172 return -1;
173 }
174
175 if (read_sync(ioc, &type, sizeof(type)) != sizeof(type)) {
176 error_setg(errp, "failed to read list option type");
177 return -1;
178 }
179 type = be32_to_cpu(type);
180 error = nbd_handle_reply_err(ioc, opt, type, errp);
181 if (error <= 0) {
182 return error;
183 }
184
185 if (read_sync(ioc, &len, sizeof(len)) != sizeof(len)) {
186 error_setg(errp, "failed to read option length");
187 return -1;
188 }
189 len = be32_to_cpu(len);
190
191 if (type == NBD_REP_ACK) {
192 if (len != 0) {
193 error_setg(errp, "length too long for option end");
194 return -1;
195 }
196 } else if (type == NBD_REP_SERVER) {
197 if (len < sizeof(namelen) || len > NBD_MAX_BUFFER_SIZE) {
198 error_setg(errp, "incorrect option length");
199 return -1;
200 }
201 if (read_sync(ioc, &namelen, sizeof(namelen)) != sizeof(namelen)) {
202 error_setg(errp, "failed to read option name length");
203 return -1;
204 }
205 namelen = be32_to_cpu(namelen);
206 len -= sizeof(namelen);
207 if (len < namelen) {
208 error_setg(errp, "incorrect option name length");
209 return -1;
210 }
211 if (namelen > 255) {
212 error_setg(errp, "export name length too long %" PRIu32, namelen);
213 return -1;
214 }
215
216 *name = g_new0(char, namelen + 1);
217 if (read_sync(ioc, *name, namelen) != namelen) {
218 error_setg(errp, "failed to read export name");
219 g_free(*name);
220 *name = NULL;
221 return -1;
222 }
223 (*name)[namelen] = '\0';
224 len -= namelen;
225 if (len) {
226 char *buf = g_malloc(len + 1);
227 if (read_sync(ioc, buf, len) != len) {
228 error_setg(errp, "failed to read export description");
229 g_free(*name);
230 g_free(buf);
231 *name = NULL;
232 return -1;
233 }
234 buf[len] = '\0';
235 TRACE("Ignoring export description: %s", buf);
236 g_free(buf);
237 }
238 } else {
239 error_setg(errp, "Unexpected reply type %" PRIx32 " expected %x",
240 type, NBD_REP_SERVER);
241 return -1;
242 }
243 return 1;
244 }
245
246
247 static int nbd_receive_query_exports(QIOChannel *ioc,
248 const char *wantname,
249 Error **errp)
250 {
251 uint64_t magic = cpu_to_be64(NBD_OPTS_MAGIC);
252 uint32_t opt = cpu_to_be32(NBD_OPT_LIST);
253 uint32_t length = 0;
254 bool foundExport = false;
255
256 TRACE("Querying export list");
257 if (write_sync(ioc, &magic, sizeof(magic)) != sizeof(magic)) {
258 error_setg(errp, "Failed to send list option magic");
259 return -1;
260 }
261
262 if (write_sync(ioc, &opt, sizeof(opt)) != sizeof(opt)) {
263 error_setg(errp, "Failed to send list option number");
264 return -1;
265 }
266
267 if (write_sync(ioc, &length, sizeof(length)) != sizeof(length)) {
268 error_setg(errp, "Failed to send list option length");
269 return -1;
270 }
271
272 TRACE("Reading available export names");
273 while (1) {
274 char *name = NULL;
275 int ret = nbd_receive_list(ioc, &name, errp);
276
277 if (ret < 0) {
278 g_free(name);
279 name = NULL;
280 return -1;
281 }
282 if (ret == 0) {
283 /* Server doesn't support export listing, so
284 * we will just assume an export with our
285 * wanted name exists */
286 foundExport = true;
287 break;
288 }
289 if (name == NULL) {
290 TRACE("End of export name list");
291 break;
292 }
293 if (g_str_equal(name, wantname)) {
294 foundExport = true;
295 TRACE("Found desired export name '%s'", name);
296 } else {
297 TRACE("Ignored export name '%s'", name);
298 }
299 g_free(name);
300 }
301
302 if (!foundExport) {
303 error_setg(errp, "No export with name '%s' available", wantname);
304 return -1;
305 }
306
307 return 0;
308 }
309
310 static QIOChannel *nbd_receive_starttls(QIOChannel *ioc,
311 QCryptoTLSCreds *tlscreds,
312 const char *hostname, Error **errp)
313 {
314 uint64_t magic = cpu_to_be64(NBD_OPTS_MAGIC);
315 uint32_t opt = cpu_to_be32(NBD_OPT_STARTTLS);
316 uint32_t length = 0;
317 uint32_t type;
318 QIOChannelTLS *tioc;
319 struct NBDTLSHandshakeData data = { 0 };
320
321 TRACE("Requesting TLS from server");
322 if (write_sync(ioc, &magic, sizeof(magic)) != sizeof(magic)) {
323 error_setg(errp, "Failed to send option magic");
324 return NULL;
325 }
326
327 if (write_sync(ioc, &opt, sizeof(opt)) != sizeof(opt)) {
328 error_setg(errp, "Failed to send option number");
329 return NULL;
330 }
331
332 if (write_sync(ioc, &length, sizeof(length)) != sizeof(length)) {
333 error_setg(errp, "Failed to send option length");
334 return NULL;
335 }
336
337 TRACE("Getting TLS reply from server1");
338 if (read_sync(ioc, &magic, sizeof(magic)) != sizeof(magic)) {
339 error_setg(errp, "failed to read option magic");
340 return NULL;
341 }
342 magic = be64_to_cpu(magic);
343 if (magic != NBD_REP_MAGIC) {
344 error_setg(errp, "Unexpected option magic");
345 return NULL;
346 }
347 TRACE("Getting TLS reply from server2");
348 if (read_sync(ioc, &opt, sizeof(opt)) != sizeof(opt)) {
349 error_setg(errp, "failed to read option");
350 return NULL;
351 }
352 opt = be32_to_cpu(opt);
353 if (opt != NBD_OPT_STARTTLS) {
354 error_setg(errp, "Unexpected option type %" PRIx32 " expected %x",
355 opt, NBD_OPT_STARTTLS);
356 return NULL;
357 }
358
359 TRACE("Getting TLS reply from server");
360 if (read_sync(ioc, &type, sizeof(type)) != sizeof(type)) {
361 error_setg(errp, "failed to read option type");
362 return NULL;
363 }
364 type = be32_to_cpu(type);
365 if (type != NBD_REP_ACK) {
366 error_setg(errp, "Server rejected request to start TLS %" PRIx32,
367 type);
368 return NULL;
369 }
370
371 TRACE("Getting TLS reply from server");
372 if (read_sync(ioc, &length, sizeof(length)) != sizeof(length)) {
373 error_setg(errp, "failed to read option length");
374 return NULL;
375 }
376 length = be32_to_cpu(length);
377 if (length != 0) {
378 error_setg(errp, "Start TLS response was not zero %" PRIu32,
379 length);
380 return NULL;
381 }
382
383 TRACE("TLS request approved, setting up TLS");
384 tioc = qio_channel_tls_new_client(ioc, tlscreds, hostname, errp);
385 if (!tioc) {
386 return NULL;
387 }
388 data.loop = g_main_loop_new(g_main_context_default(), FALSE);
389 TRACE("Starting TLS handshake");
390 qio_channel_tls_handshake(tioc,
391 nbd_tls_handshake,
392 &data,
393 NULL);
394
395 if (!data.complete) {
396 g_main_loop_run(data.loop);
397 }
398 g_main_loop_unref(data.loop);
399 if (data.error) {
400 error_propagate(errp, data.error);
401 object_unref(OBJECT(tioc));
402 return NULL;
403 }
404
405 return QIO_CHANNEL(tioc);
406 }
407
408
409 int nbd_receive_negotiate(QIOChannel *ioc, const char *name, uint32_t *flags,
410 QCryptoTLSCreds *tlscreds, const char *hostname,
411 QIOChannel **outioc,
412 off_t *size, Error **errp)
413 {
414 char buf[256];
415 uint64_t magic, s;
416 int rc;
417
418 TRACE("Receiving negotiation tlscreds=%p hostname=%s.",
419 tlscreds, hostname ? hostname : "<null>");
420
421 rc = -EINVAL;
422
423 if (outioc) {
424 *outioc = NULL;
425 }
426 if (tlscreds && !outioc) {
427 error_setg(errp, "Output I/O channel required for TLS");
428 goto fail;
429 }
430
431 if (read_sync(ioc, buf, 8) != 8) {
432 error_setg(errp, "Failed to read data");
433 goto fail;
434 }
435
436 buf[8] = '\0';
437 if (strlen(buf) == 0) {
438 error_setg(errp, "Server connection closed unexpectedly");
439 goto fail;
440 }
441
442 TRACE("Magic is %c%c%c%c%c%c%c%c",
443 qemu_isprint(buf[0]) ? buf[0] : '.',
444 qemu_isprint(buf[1]) ? buf[1] : '.',
445 qemu_isprint(buf[2]) ? buf[2] : '.',
446 qemu_isprint(buf[3]) ? buf[3] : '.',
447 qemu_isprint(buf[4]) ? buf[4] : '.',
448 qemu_isprint(buf[5]) ? buf[5] : '.',
449 qemu_isprint(buf[6]) ? buf[6] : '.',
450 qemu_isprint(buf[7]) ? buf[7] : '.');
451
452 if (memcmp(buf, "NBDMAGIC", 8) != 0) {
453 error_setg(errp, "Invalid magic received");
454 goto fail;
455 }
456
457 if (read_sync(ioc, &magic, sizeof(magic)) != sizeof(magic)) {
458 error_setg(errp, "Failed to read magic");
459 goto fail;
460 }
461 magic = be64_to_cpu(magic);
462 TRACE("Magic is 0x%" PRIx64, magic);
463
464 if (magic == NBD_OPTS_MAGIC) {
465 uint32_t clientflags = 0;
466 uint32_t opt;
467 uint32_t namesize;
468 uint16_t globalflags;
469 uint16_t exportflags;
470 bool fixedNewStyle = false;
471
472 if (read_sync(ioc, &globalflags, sizeof(globalflags)) !=
473 sizeof(globalflags)) {
474 error_setg(errp, "Failed to read server flags");
475 goto fail;
476 }
477 globalflags = be16_to_cpu(globalflags);
478 *flags = globalflags << 16;
479 TRACE("Global flags are %" PRIx32, globalflags);
480 if (globalflags & NBD_FLAG_FIXED_NEWSTYLE) {
481 fixedNewStyle = true;
482 TRACE("Server supports fixed new style");
483 clientflags |= NBD_FLAG_C_FIXED_NEWSTYLE;
484 }
485 /* client requested flags */
486 clientflags = cpu_to_be32(clientflags);
487 if (write_sync(ioc, &clientflags, sizeof(clientflags)) !=
488 sizeof(clientflags)) {
489 error_setg(errp, "Failed to send clientflags field");
490 goto fail;
491 }
492 if (tlscreds) {
493 if (fixedNewStyle) {
494 *outioc = nbd_receive_starttls(ioc, tlscreds, hostname, errp);
495 if (!*outioc) {
496 goto fail;
497 }
498 ioc = *outioc;
499 } else {
500 error_setg(errp, "Server does not support STARTTLS");
501 goto fail;
502 }
503 }
504 if (!name) {
505 TRACE("Using default NBD export name \"\"");
506 name = "";
507 }
508 if (fixedNewStyle) {
509 /* Check our desired export is present in the
510 * server export list. Since NBD_OPT_EXPORT_NAME
511 * cannot return an error message, running this
512 * query gives us good error reporting if the
513 * server required TLS
514 */
515 if (nbd_receive_query_exports(ioc, name, errp) < 0) {
516 goto fail;
517 }
518 }
519 /* write the export name */
520 magic = cpu_to_be64(magic);
521 if (write_sync(ioc, &magic, sizeof(magic)) != sizeof(magic)) {
522 error_setg(errp, "Failed to send export name magic");
523 goto fail;
524 }
525 opt = cpu_to_be32(NBD_OPT_EXPORT_NAME);
526 if (write_sync(ioc, &opt, sizeof(opt)) != sizeof(opt)) {
527 error_setg(errp, "Failed to send export name option number");
528 goto fail;
529 }
530 namesize = cpu_to_be32(strlen(name));
531 if (write_sync(ioc, &namesize, sizeof(namesize)) !=
532 sizeof(namesize)) {
533 error_setg(errp, "Failed to send export name length");
534 goto fail;
535 }
536 if (write_sync(ioc, (char *)name, strlen(name)) != strlen(name)) {
537 error_setg(errp, "Failed to send export name");
538 goto fail;
539 }
540
541 if (read_sync(ioc, &s, sizeof(s)) != sizeof(s)) {
542 error_setg(errp, "Failed to read export length");
543 goto fail;
544 }
545 *size = be64_to_cpu(s);
546 TRACE("Size is %" PRIu64, *size);
547
548 if (read_sync(ioc, &exportflags, sizeof(exportflags)) !=
549 sizeof(exportflags)) {
550 error_setg(errp, "Failed to read export flags");
551 goto fail;
552 }
553 exportflags = be16_to_cpu(exportflags);
554 *flags |= exportflags;
555 TRACE("Export flags are %" PRIx16, exportflags);
556 } else if (magic == NBD_CLIENT_MAGIC) {
557 if (name) {
558 error_setg(errp, "Server does not support export names");
559 goto fail;
560 }
561 if (tlscreds) {
562 error_setg(errp, "Server does not support STARTTLS");
563 goto fail;
564 }
565
566 if (read_sync(ioc, &s, sizeof(s)) != sizeof(s)) {
567 error_setg(errp, "Failed to read export length");
568 goto fail;
569 }
570 *size = be64_to_cpu(s);
571 TRACE("Size is %" PRIu64, *size);
572
573 if (read_sync(ioc, flags, sizeof(*flags)) != sizeof(*flags)) {
574 error_setg(errp, "Failed to read export flags");
575 goto fail;
576 }
577 *flags = be32_to_cpu(*flags);
578 } else {
579 error_setg(errp, "Bad magic received");
580 goto fail;
581 }
582
583 if (read_sync(ioc, &buf, 124) != 124) {
584 error_setg(errp, "Failed to read reserved block");
585 goto fail;
586 }
587 rc = 0;
588
589 fail:
590 return rc;
591 }
592
593 #ifdef __linux__
594 int nbd_init(int fd, QIOChannelSocket *sioc, uint32_t flags, off_t size)
595 {
596 TRACE("Setting NBD socket");
597
598 if (ioctl(fd, NBD_SET_SOCK, sioc->fd) < 0) {
599 int serrno = errno;
600 LOG("Failed to set NBD socket");
601 return -serrno;
602 }
603
604 TRACE("Setting block size to %lu", (unsigned long)BDRV_SECTOR_SIZE);
605
606 if (ioctl(fd, NBD_SET_BLKSIZE, (size_t)BDRV_SECTOR_SIZE) < 0) {
607 int serrno = errno;
608 LOG("Failed setting NBD block size");
609 return -serrno;
610 }
611
612 TRACE("Setting size to %zd block(s)", (size_t)(size / BDRV_SECTOR_SIZE));
613
614 if (ioctl(fd, NBD_SET_SIZE_BLOCKS, (size_t)(size / BDRV_SECTOR_SIZE)) < 0) {
615 int serrno = errno;
616 LOG("Failed setting size (in blocks)");
617 return -serrno;
618 }
619
620 if (ioctl(fd, NBD_SET_FLAGS, flags) < 0) {
621 if (errno == ENOTTY) {
622 int read_only = (flags & NBD_FLAG_READ_ONLY) != 0;
623 TRACE("Setting readonly attribute");
624
625 if (ioctl(fd, BLKROSET, (unsigned long) &read_only) < 0) {
626 int serrno = errno;
627 LOG("Failed setting read-only attribute");
628 return -serrno;
629 }
630 } else {
631 int serrno = errno;
632 LOG("Failed setting flags");
633 return -serrno;
634 }
635 }
636
637 TRACE("Negotiation ended");
638
639 return 0;
640 }
641
642 int nbd_client(int fd)
643 {
644 int ret;
645 int serrno;
646
647 TRACE("Doing NBD loop");
648
649 ret = ioctl(fd, NBD_DO_IT);
650 if (ret < 0 && errno == EPIPE) {
651 /* NBD_DO_IT normally returns EPIPE when someone has disconnected
652 * the socket via NBD_DISCONNECT. We do not want to return 1 in
653 * that case.
654 */
655 ret = 0;
656 }
657 serrno = errno;
658
659 TRACE("NBD loop returned %d: %s", ret, strerror(serrno));
660
661 TRACE("Clearing NBD queue");
662 ioctl(fd, NBD_CLEAR_QUE);
663
664 TRACE("Clearing NBD socket");
665 ioctl(fd, NBD_CLEAR_SOCK);
666
667 errno = serrno;
668 return ret;
669 }
670 #else
671 int nbd_init(int fd, QIOChannelSocket *ioc, uint32_t flags, off_t size)
672 {
673 return -ENOTSUP;
674 }
675
676 int nbd_client(int fd)
677 {
678 return -ENOTSUP;
679 }
680 #endif
681
682 ssize_t nbd_send_request(QIOChannel *ioc, struct nbd_request *request)
683 {
684 uint8_t buf[NBD_REQUEST_SIZE];
685 ssize_t ret;
686
687 TRACE("Sending request to server: "
688 "{ .from = %" PRIu64", .len = %" PRIu32 ", .handle = %" PRIu64
689 ", .type=%" PRIu16 " }",
690 request->from, request->len, request->handle, request->type);
691
692 stl_be_p(buf, NBD_REQUEST_MAGIC);
693 stl_be_p(buf + 4, request->type);
694 stq_be_p(buf + 8, request->handle);
695 stq_be_p(buf + 16, request->from);
696 stl_be_p(buf + 24, request->len);
697
698 ret = write_sync(ioc, buf, sizeof(buf));
699 if (ret < 0) {
700 return ret;
701 }
702
703 if (ret != sizeof(buf)) {
704 LOG("writing to socket failed");
705 return -EINVAL;
706 }
707 return 0;
708 }
709
710 ssize_t nbd_receive_reply(QIOChannel *ioc, struct nbd_reply *reply)
711 {
712 uint8_t buf[NBD_REPLY_SIZE];
713 uint32_t magic;
714 ssize_t ret;
715
716 ret = read_sync(ioc, buf, sizeof(buf));
717 if (ret < 0) {
718 return ret;
719 }
720
721 if (ret != sizeof(buf)) {
722 LOG("read failed");
723 return -EINVAL;
724 }
725
726 /* Reply
727 [ 0 .. 3] magic (NBD_REPLY_MAGIC)
728 [ 4 .. 7] error (0 == no error)
729 [ 7 .. 15] handle
730 */
731
732 magic = ldl_be_p(buf);
733 reply->error = ldl_be_p(buf + 4);
734 reply->handle = ldq_be_p(buf + 8);
735
736 reply->error = nbd_errno_to_system_errno(reply->error);
737
738 TRACE("Got reply: { magic = 0x%" PRIx32 ", .error = % " PRId32
739 ", handle = %" PRIu64" }",
740 magic, reply->error, reply->handle);
741
742 if (magic != NBD_REPLY_MAGIC) {
743 LOG("invalid magic (got 0x%" PRIx32 ")", magic);
744 return -EINVAL;
745 }
746 return 0;
747 }
748