add pflib: PixelFormat conversion library.
[qemu.git] / nbd.c
blob011b50f166a49b5c211d6e633896e5893ccc90ed
1 /*
2 * Copyright (C) 2005 Anthony Liguori <anthony@codemonkey.ws>
4 * Network Block Device
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 "nbd.h"
21 #include <errno.h>
22 #include <string.h>
23 #ifndef _WIN32
24 #include <sys/ioctl.h>
25 #endif
26 #ifdef __sun__
27 #include <sys/ioccom.h>
28 #endif
29 #include <ctype.h>
30 #include <inttypes.h>
32 #include "qemu_socket.h"
34 //#define DEBUG_NBD
36 #ifdef DEBUG_NBD
37 #define TRACE(msg, ...) do { \
38 LOG(msg, ## __VA_ARGS__); \
39 } while(0)
40 #else
41 #define TRACE(msg, ...) \
42 do { } while (0)
43 #endif
45 #define LOG(msg, ...) do { \
46 fprintf(stderr, "%s:%s():L%d: " msg "\n", \
47 __FILE__, __FUNCTION__, __LINE__, ## __VA_ARGS__); \
48 } while(0)
50 /* This is all part of the "official" NBD API */
52 #define NBD_REQUEST_MAGIC 0x25609513
53 #define NBD_REPLY_MAGIC 0x67446698
55 #define NBD_SET_SOCK _IO(0xab, 0)
56 #define NBD_SET_BLKSIZE _IO(0xab, 1)
57 #define NBD_SET_SIZE _IO(0xab, 2)
58 #define NBD_DO_IT _IO(0xab, 3)
59 #define NBD_CLEAR_SOCK _IO(0xab, 4)
60 #define NBD_CLEAR_QUE _IO(0xab, 5)
61 #define NBD_PRINT_DEBUG _IO(0xab, 6)
62 #define NBD_SET_SIZE_BLOCKS _IO(0xab, 7)
63 #define NBD_DISCONNECT _IO(0xab, 8)
65 #define NBD_OPT_EXPORT_NAME (1 << 0)
67 /* That's all folks */
69 #define read_sync(fd, buffer, size) nbd_wr_sync(fd, buffer, size, true)
70 #define write_sync(fd, buffer, size) nbd_wr_sync(fd, buffer, size, false)
72 size_t nbd_wr_sync(int fd, void *buffer, size_t size, bool do_read)
74 size_t offset = 0;
76 while (offset < size) {
77 ssize_t len;
79 if (do_read) {
80 len = recv(fd, buffer + offset, size - offset, 0);
81 } else {
82 len = send(fd, buffer + offset, size - offset, 0);
85 if (len == -1)
86 errno = socket_error();
88 /* recoverable error */
89 if (len == -1 && (errno == EAGAIN || errno == EINTR)) {
90 continue;
93 /* eof */
94 if (len == 0) {
95 break;
98 /* unrecoverable error */
99 if (len == -1) {
100 return 0;
103 offset += len;
106 return offset;
109 int tcp_socket_outgoing(const char *address, uint16_t port)
111 int s;
112 struct in_addr in;
113 struct sockaddr_in addr;
115 s = socket(PF_INET, SOCK_STREAM, 0);
116 if (s == -1) {
117 return -1;
120 if (inet_aton(address, &in) == 0) {
121 struct hostent *ent;
123 ent = gethostbyname(address);
124 if (ent == NULL) {
125 goto error;
128 memcpy(&in, ent->h_addr, sizeof(in));
131 addr.sin_family = AF_INET;
132 addr.sin_port = htons(port);
133 memcpy(&addr.sin_addr.s_addr, &in, sizeof(in));
135 if (connect(s, (struct sockaddr *)&addr, sizeof(addr)) == -1) {
136 goto error;
139 return s;
140 error:
141 closesocket(s);
142 return -1;
145 int tcp_socket_incoming(const char *address, uint16_t port)
147 int s;
148 struct in_addr in;
149 struct sockaddr_in addr;
150 int opt;
152 s = socket(PF_INET, SOCK_STREAM, 0);
153 if (s == -1) {
154 return -1;
157 if (inet_aton(address, &in) == 0) {
158 struct hostent *ent;
160 ent = gethostbyname(address);
161 if (ent == NULL) {
162 goto error;
165 memcpy(&in, ent->h_addr, sizeof(in));
168 addr.sin_family = AF_INET;
169 addr.sin_port = htons(port);
170 memcpy(&addr.sin_addr.s_addr, &in, sizeof(in));
172 opt = 1;
173 if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR,
174 (const void *) &opt, sizeof(opt)) == -1) {
175 goto error;
178 if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) == -1) {
179 goto error;
182 if (listen(s, 128) == -1) {
183 goto error;
186 return s;
187 error:
188 closesocket(s);
189 return -1;
192 #ifndef _WIN32
193 int unix_socket_incoming(const char *path)
195 int s;
196 struct sockaddr_un addr;
198 s = socket(PF_UNIX, SOCK_STREAM, 0);
199 if (s == -1) {
200 return -1;
203 memset(&addr, 0, sizeof(addr));
204 addr.sun_family = AF_UNIX;
205 pstrcpy(addr.sun_path, sizeof(addr.sun_path), path);
207 if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) == -1) {
208 goto error;
211 if (listen(s, 128) == -1) {
212 goto error;
215 return s;
216 error:
217 closesocket(s);
218 return -1;
221 int unix_socket_outgoing(const char *path)
223 int s;
224 struct sockaddr_un addr;
226 s = socket(PF_UNIX, SOCK_STREAM, 0);
227 if (s == -1) {
228 return -1;
231 memset(&addr, 0, sizeof(addr));
232 addr.sun_family = AF_UNIX;
233 pstrcpy(addr.sun_path, sizeof(addr.sun_path), path);
235 if (connect(s, (struct sockaddr *)&addr, sizeof(addr)) == -1) {
236 goto error;
239 return s;
240 error:
241 closesocket(s);
242 return -1;
244 #else
245 int unix_socket_incoming(const char *path)
247 errno = ENOTSUP;
248 return -1;
251 int unix_socket_outgoing(const char *path)
253 errno = ENOTSUP;
254 return -1;
256 #endif
259 /* Basic flow
261 Server Client
263 Negotiate
264 Request
265 Response
266 Request
267 Response
270 Request (type == 2)
273 int nbd_negotiate(int csock, off_t size)
275 char buf[8 + 8 + 8 + 128];
277 /* Negotiate
278 [ 0 .. 7] passwd ("NBDMAGIC")
279 [ 8 .. 15] magic (0x00420281861253)
280 [16 .. 23] size
281 [24 .. 151] reserved (0)
284 TRACE("Beginning negotiation.");
285 memcpy(buf, "NBDMAGIC", 8);
286 cpu_to_be64w((uint64_t*)(buf + 8), 0x00420281861253LL);
287 cpu_to_be64w((uint64_t*)(buf + 16), size);
288 memset(buf + 24, 0, 128);
290 if (write_sync(csock, buf, sizeof(buf)) != sizeof(buf)) {
291 LOG("write failed");
292 errno = EINVAL;
293 return -1;
296 TRACE("Negotation succeeded.");
298 return 0;
301 int nbd_receive_negotiate(int csock, const char *name, uint32_t *flags,
302 off_t *size, size_t *blocksize)
304 char buf[256];
305 uint64_t magic, s;
306 uint16_t tmp;
308 TRACE("Receiving negotation.");
310 if (read_sync(csock, buf, 8) != 8) {
311 LOG("read failed");
312 errno = EINVAL;
313 return -1;
316 buf[8] = '\0';
317 if (strlen(buf) == 0) {
318 LOG("server connection closed");
319 errno = EINVAL;
320 return -1;
323 TRACE("Magic is %c%c%c%c%c%c%c%c",
324 qemu_isprint(buf[0]) ? buf[0] : '.',
325 qemu_isprint(buf[1]) ? buf[1] : '.',
326 qemu_isprint(buf[2]) ? buf[2] : '.',
327 qemu_isprint(buf[3]) ? buf[3] : '.',
328 qemu_isprint(buf[4]) ? buf[4] : '.',
329 qemu_isprint(buf[5]) ? buf[5] : '.',
330 qemu_isprint(buf[6]) ? buf[6] : '.',
331 qemu_isprint(buf[7]) ? buf[7] : '.');
333 if (memcmp(buf, "NBDMAGIC", 8) != 0) {
334 LOG("Invalid magic received");
335 errno = EINVAL;
336 return -1;
339 if (read_sync(csock, &magic, sizeof(magic)) != sizeof(magic)) {
340 LOG("read failed");
341 errno = EINVAL;
342 return -1;
344 magic = be64_to_cpu(magic);
345 TRACE("Magic is 0x%" PRIx64, magic);
347 if (name) {
348 uint32_t reserved = 0;
349 uint32_t opt;
350 uint32_t namesize;
352 TRACE("Checking magic (opts_magic)");
353 if (magic != 0x49484156454F5054LL) {
354 LOG("Bad magic received");
355 errno = EINVAL;
356 return -1;
358 if (read_sync(csock, &tmp, sizeof(tmp)) != sizeof(tmp)) {
359 LOG("flags read failed");
360 errno = EINVAL;
361 return -1;
363 *flags = be16_to_cpu(tmp) << 16;
364 /* reserved for future use */
365 if (write_sync(csock, &reserved, sizeof(reserved)) !=
366 sizeof(reserved)) {
367 LOG("write failed (reserved)");
368 errno = EINVAL;
369 return -1;
371 /* write the export name */
372 magic = cpu_to_be64(magic);
373 if (write_sync(csock, &magic, sizeof(magic)) != sizeof(magic)) {
374 LOG("write failed (magic)");
375 errno = EINVAL;
376 return -1;
378 opt = cpu_to_be32(NBD_OPT_EXPORT_NAME);
379 if (write_sync(csock, &opt, sizeof(opt)) != sizeof(opt)) {
380 LOG("write failed (opt)");
381 errno = EINVAL;
382 return -1;
384 namesize = cpu_to_be32(strlen(name));
385 if (write_sync(csock, &namesize, sizeof(namesize)) !=
386 sizeof(namesize)) {
387 LOG("write failed (namesize)");
388 errno = EINVAL;
389 return -1;
391 if (write_sync(csock, (char*)name, strlen(name)) != strlen(name)) {
392 LOG("write failed (name)");
393 errno = EINVAL;
394 return -1;
396 } else {
397 TRACE("Checking magic (cli_magic)");
399 if (magic != 0x00420281861253LL) {
400 LOG("Bad magic received");
401 errno = EINVAL;
402 return -1;
406 if (read_sync(csock, &s, sizeof(s)) != sizeof(s)) {
407 LOG("read failed");
408 errno = EINVAL;
409 return -1;
411 *size = be64_to_cpu(s);
412 *blocksize = 1024;
413 TRACE("Size is %" PRIu64, *size);
415 if (!name) {
416 if (read_sync(csock, flags, sizeof(*flags)) != sizeof(*flags)) {
417 LOG("read failed (flags)");
418 errno = EINVAL;
419 return -1;
421 *flags = be32_to_cpup(flags);
422 } else {
423 if (read_sync(csock, &tmp, sizeof(tmp)) != sizeof(tmp)) {
424 LOG("read failed (tmp)");
425 errno = EINVAL;
426 return -1;
428 *flags |= be32_to_cpu(tmp);
430 if (read_sync(csock, &buf, 124) != 124) {
431 LOG("read failed (buf)");
432 errno = EINVAL;
433 return -1;
435 return 0;
438 #ifndef _WIN32
439 int nbd_init(int fd, int csock, off_t size, size_t blocksize)
441 TRACE("Setting block size to %lu", (unsigned long)blocksize);
443 if (ioctl(fd, NBD_SET_BLKSIZE, blocksize) == -1) {
444 int serrno = errno;
445 LOG("Failed setting NBD block size");
446 errno = serrno;
447 return -1;
450 TRACE("Setting size to %zd block(s)", (size_t)(size / blocksize));
452 if (ioctl(fd, NBD_SET_SIZE_BLOCKS, size / blocksize) == -1) {
453 int serrno = errno;
454 LOG("Failed setting size (in blocks)");
455 errno = serrno;
456 return -1;
459 TRACE("Clearing NBD socket");
461 if (ioctl(fd, NBD_CLEAR_SOCK) == -1) {
462 int serrno = errno;
463 LOG("Failed clearing NBD socket");
464 errno = serrno;
465 return -1;
468 TRACE("Setting NBD socket");
470 if (ioctl(fd, NBD_SET_SOCK, csock) == -1) {
471 int serrno = errno;
472 LOG("Failed to set NBD socket");
473 errno = serrno;
474 return -1;
477 TRACE("Negotiation ended");
479 return 0;
482 int nbd_disconnect(int fd)
484 ioctl(fd, NBD_CLEAR_QUE);
485 ioctl(fd, NBD_DISCONNECT);
486 ioctl(fd, NBD_CLEAR_SOCK);
487 return 0;
490 int nbd_client(int fd)
492 int ret;
493 int serrno;
495 TRACE("Doing NBD loop");
497 ret = ioctl(fd, NBD_DO_IT);
498 serrno = errno;
500 TRACE("NBD loop returned %d: %s", ret, strerror(serrno));
502 TRACE("Clearing NBD queue");
503 ioctl(fd, NBD_CLEAR_QUE);
505 TRACE("Clearing NBD socket");
506 ioctl(fd, NBD_CLEAR_SOCK);
508 errno = serrno;
509 return ret;
511 #else
512 int nbd_init(int fd, int csock, off_t size, size_t blocksize)
514 errno = ENOTSUP;
515 return -1;
518 int nbd_disconnect(int fd)
520 errno = ENOTSUP;
521 return -1;
524 int nbd_client(int fd)
526 errno = ENOTSUP;
527 return -1;
529 #endif
531 int nbd_send_request(int csock, struct nbd_request *request)
533 uint8_t buf[4 + 4 + 8 + 8 + 4];
535 cpu_to_be32w((uint32_t*)buf, NBD_REQUEST_MAGIC);
536 cpu_to_be32w((uint32_t*)(buf + 4), request->type);
537 cpu_to_be64w((uint64_t*)(buf + 8), request->handle);
538 cpu_to_be64w((uint64_t*)(buf + 16), request->from);
539 cpu_to_be32w((uint32_t*)(buf + 24), request->len);
541 TRACE("Sending request to client");
543 if (write_sync(csock, buf, sizeof(buf)) != sizeof(buf)) {
544 LOG("writing to socket failed");
545 errno = EINVAL;
546 return -1;
548 return 0;
552 static int nbd_receive_request(int csock, struct nbd_request *request)
554 uint8_t buf[4 + 4 + 8 + 8 + 4];
555 uint32_t magic;
557 if (read_sync(csock, buf, sizeof(buf)) != sizeof(buf)) {
558 LOG("read failed");
559 errno = EINVAL;
560 return -1;
563 /* Request
564 [ 0 .. 3] magic (NBD_REQUEST_MAGIC)
565 [ 4 .. 7] type (0 == READ, 1 == WRITE)
566 [ 8 .. 15] handle
567 [16 .. 23] from
568 [24 .. 27] len
571 magic = be32_to_cpup((uint32_t*)buf);
572 request->type = be32_to_cpup((uint32_t*)(buf + 4));
573 request->handle = be64_to_cpup((uint64_t*)(buf + 8));
574 request->from = be64_to_cpup((uint64_t*)(buf + 16));
575 request->len = be32_to_cpup((uint32_t*)(buf + 24));
577 TRACE("Got request: "
578 "{ magic = 0x%x, .type = %d, from = %" PRIu64" , len = %u }",
579 magic, request->type, request->from, request->len);
581 if (magic != NBD_REQUEST_MAGIC) {
582 LOG("invalid magic (got 0x%x)", magic);
583 errno = EINVAL;
584 return -1;
586 return 0;
589 int nbd_receive_reply(int csock, struct nbd_reply *reply)
591 uint8_t buf[4 + 4 + 8];
592 uint32_t magic;
594 memset(buf, 0xAA, sizeof(buf));
596 if (read_sync(csock, buf, sizeof(buf)) != sizeof(buf)) {
597 LOG("read failed");
598 errno = EINVAL;
599 return -1;
602 /* Reply
603 [ 0 .. 3] magic (NBD_REPLY_MAGIC)
604 [ 4 .. 7] error (0 == no error)
605 [ 7 .. 15] handle
608 magic = be32_to_cpup((uint32_t*)buf);
609 reply->error = be32_to_cpup((uint32_t*)(buf + 4));
610 reply->handle = be64_to_cpup((uint64_t*)(buf + 8));
612 TRACE("Got reply: "
613 "{ magic = 0x%x, .error = %d, handle = %" PRIu64" }",
614 magic, reply->error, reply->handle);
616 if (magic != NBD_REPLY_MAGIC) {
617 LOG("invalid magic (got 0x%x)", magic);
618 errno = EINVAL;
619 return -1;
621 return 0;
624 static int nbd_send_reply(int csock, struct nbd_reply *reply)
626 uint8_t buf[4 + 4 + 8];
628 /* Reply
629 [ 0 .. 3] magic (NBD_REPLY_MAGIC)
630 [ 4 .. 7] error (0 == no error)
631 [ 7 .. 15] handle
633 cpu_to_be32w((uint32_t*)buf, NBD_REPLY_MAGIC);
634 cpu_to_be32w((uint32_t*)(buf + 4), reply->error);
635 cpu_to_be64w((uint64_t*)(buf + 8), reply->handle);
637 TRACE("Sending response to client");
639 if (write_sync(csock, buf, sizeof(buf)) != sizeof(buf)) {
640 LOG("writing to socket failed");
641 errno = EINVAL;
642 return -1;
644 return 0;
647 int nbd_trip(BlockDriverState *bs, int csock, off_t size, uint64_t dev_offset,
648 off_t *offset, bool readonly, uint8_t *data, int data_size)
650 struct nbd_request request;
651 struct nbd_reply reply;
653 TRACE("Reading request.");
655 if (nbd_receive_request(csock, &request) == -1)
656 return -1;
658 if (request.len > data_size) {
659 LOG("len (%u) is larger than max len (%u)",
660 request.len, data_size);
661 errno = EINVAL;
662 return -1;
665 if ((request.from + request.len) < request.from) {
666 LOG("integer overflow detected! "
667 "you're probably being attacked");
668 errno = EINVAL;
669 return -1;
672 if ((request.from + request.len) > size) {
673 LOG("From: %" PRIu64 ", Len: %u, Size: %" PRIu64
674 ", Offset: %" PRIu64 "\n",
675 request.from, request.len, (uint64_t)size, dev_offset);
676 LOG("requested operation past EOF--bad client?");
677 errno = EINVAL;
678 return -1;
681 TRACE("Decoding type");
683 reply.handle = request.handle;
684 reply.error = 0;
686 switch (request.type) {
687 case NBD_CMD_READ:
688 TRACE("Request type is READ");
690 if (bdrv_read(bs, (request.from + dev_offset) / 512, data,
691 request.len / 512) == -1) {
692 LOG("reading from file failed");
693 errno = EINVAL;
694 return -1;
696 *offset += request.len;
698 TRACE("Read %u byte(s)", request.len);
700 if (nbd_send_reply(csock, &reply) == -1)
701 return -1;
703 TRACE("Sending data to client");
705 if (write_sync(csock, data, request.len) != request.len) {
706 LOG("writing to socket failed");
707 errno = EINVAL;
708 return -1;
710 break;
711 case NBD_CMD_WRITE:
712 TRACE("Request type is WRITE");
714 TRACE("Reading %u byte(s)", request.len);
716 if (read_sync(csock, data, request.len) != request.len) {
717 LOG("reading from socket failed");
718 errno = EINVAL;
719 return -1;
722 if (readonly) {
723 TRACE("Server is read-only, return error");
724 reply.error = 1;
725 } else {
726 TRACE("Writing to device");
728 if (bdrv_write(bs, (request.from + dev_offset) / 512,
729 data, request.len / 512) == -1) {
730 LOG("writing to file failed");
731 errno = EINVAL;
732 return -1;
735 *offset += request.len;
738 if (nbd_send_reply(csock, &reply) == -1)
739 return -1;
740 break;
741 case NBD_CMD_DISC:
742 TRACE("Request type is DISCONNECT");
743 errno = 0;
744 return 1;
745 default:
746 LOG("invalid request type (%u) received", request.type);
747 errno = EINVAL;
748 return -1;
751 TRACE("Request/Reply complete");
753 return 0;