pc: Convert GSIState::i8259_irq into array
[qemu/wangdongxu.git] / nbd.c
blobfb5e424abe9d2120a353049e87970d7176ad36fc
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"
20 #include "block.h"
22 #include <errno.h>
23 #include <string.h>
24 #ifndef _WIN32
25 #include <sys/ioctl.h>
26 #endif
27 #if defined(__sun__) || defined(__HAIKU__)
28 #include <sys/ioccom.h>
29 #endif
30 #include <ctype.h>
31 #include <inttypes.h>
33 #ifdef __linux__
34 #include <linux/fs.h>
35 #endif
37 #include "qemu_socket.h"
39 //#define DEBUG_NBD
41 #ifdef DEBUG_NBD
42 #define TRACE(msg, ...) do { \
43 LOG(msg, ## __VA_ARGS__); \
44 } while(0)
45 #else
46 #define TRACE(msg, ...) \
47 do { } while (0)
48 #endif
50 #define LOG(msg, ...) do { \
51 fprintf(stderr, "%s:%s():L%d: " msg "\n", \
52 __FILE__, __FUNCTION__, __LINE__, ## __VA_ARGS__); \
53 } while(0)
55 /* This is all part of the "official" NBD API */
57 #define NBD_REPLY_SIZE (4 + 4 + 8)
58 #define NBD_REQUEST_MAGIC 0x25609513
59 #define NBD_REPLY_MAGIC 0x67446698
61 #define NBD_SET_SOCK _IO(0xab, 0)
62 #define NBD_SET_BLKSIZE _IO(0xab, 1)
63 #define NBD_SET_SIZE _IO(0xab, 2)
64 #define NBD_DO_IT _IO(0xab, 3)
65 #define NBD_CLEAR_SOCK _IO(0xab, 4)
66 #define NBD_CLEAR_QUE _IO(0xab, 5)
67 #define NBD_PRINT_DEBUG _IO(0xab, 6)
68 #define NBD_SET_SIZE_BLOCKS _IO(0xab, 7)
69 #define NBD_DISCONNECT _IO(0xab, 8)
70 #define NBD_SET_TIMEOUT _IO(0xab, 9)
71 #define NBD_SET_FLAGS _IO(0xab, 10)
73 #define NBD_OPT_EXPORT_NAME (1 << 0)
75 /* That's all folks */
77 #define read_sync(fd, buffer, size) nbd_wr_sync(fd, buffer, size, true)
78 #define write_sync(fd, buffer, size) nbd_wr_sync(fd, buffer, size, false)
80 size_t nbd_wr_sync(int fd, void *buffer, size_t size, bool do_read)
82 size_t offset = 0;
84 while (offset < size) {
85 ssize_t len;
87 if (do_read) {
88 len = qemu_recv(fd, buffer + offset, size - offset, 0);
89 } else {
90 len = send(fd, buffer + offset, size - offset, 0);
93 if (len == -1)
94 errno = socket_error();
96 /* recoverable error */
97 if (len == -1 && (errno == EAGAIN || errno == EINTR)) {
98 continue;
101 /* eof */
102 if (len == 0) {
103 break;
106 /* unrecoverable error */
107 if (len == -1) {
108 return 0;
111 offset += len;
114 return offset;
117 static void combine_addr(char *buf, size_t len, const char* address,
118 uint16_t port)
120 /* If the address-part contains a colon, it's an IPv6 IP so needs [] */
121 if (strstr(address, ":")) {
122 snprintf(buf, len, "[%s]:%u", address, port);
123 } else {
124 snprintf(buf, len, "%s:%u", address, port);
128 int tcp_socket_outgoing(const char *address, uint16_t port)
130 char address_and_port[128];
131 combine_addr(address_and_port, 128, address, port);
132 return tcp_socket_outgoing_spec(address_and_port);
135 int tcp_socket_outgoing_spec(const char *address_and_port)
137 return inet_connect(address_and_port, SOCK_STREAM);
140 int tcp_socket_incoming(const char *address, uint16_t port)
142 char address_and_port[128];
143 combine_addr(address_and_port, 128, address, port);
144 return tcp_socket_incoming_spec(address_and_port);
147 int tcp_socket_incoming_spec(const char *address_and_port)
149 char *ostr = NULL;
150 int olen = 0;
151 return inet_listen(address_and_port, ostr, olen, SOCK_STREAM, 0);
154 int unix_socket_incoming(const char *path)
156 char *ostr = NULL;
157 int olen = 0;
159 return unix_listen(path, ostr, olen);
162 int unix_socket_outgoing(const char *path)
164 return unix_connect(path);
167 /* Basic flow
169 Server Client
171 Negotiate
172 Request
173 Response
174 Request
175 Response
178 Request (type == 2)
181 int nbd_negotiate(int csock, off_t size, uint32_t flags)
183 char buf[8 + 8 + 8 + 128];
185 /* Negotiate
186 [ 0 .. 7] passwd ("NBDMAGIC")
187 [ 8 .. 15] magic (0x00420281861253)
188 [16 .. 23] size
189 [24 .. 27] flags
190 [28 .. 151] reserved (0)
193 TRACE("Beginning negotiation.");
194 memcpy(buf, "NBDMAGIC", 8);
195 cpu_to_be64w((uint64_t*)(buf + 8), 0x00420281861253LL);
196 cpu_to_be64w((uint64_t*)(buf + 16), size);
197 cpu_to_be32w((uint32_t*)(buf + 24), flags | NBD_FLAG_HAS_FLAGS);
198 memset(buf + 28, 0, 124);
200 if (write_sync(csock, buf, sizeof(buf)) != sizeof(buf)) {
201 LOG("write failed");
202 errno = EINVAL;
203 return -1;
206 TRACE("Negotation succeeded.");
208 return 0;
211 int nbd_receive_negotiate(int csock, const char *name, uint32_t *flags,
212 off_t *size, size_t *blocksize)
214 char buf[256];
215 uint64_t magic, s;
216 uint16_t tmp;
218 TRACE("Receiving negotation.");
220 if (read_sync(csock, buf, 8) != 8) {
221 LOG("read failed");
222 errno = EINVAL;
223 return -1;
226 buf[8] = '\0';
227 if (strlen(buf) == 0) {
228 LOG("server connection closed");
229 errno = EINVAL;
230 return -1;
233 TRACE("Magic is %c%c%c%c%c%c%c%c",
234 qemu_isprint(buf[0]) ? buf[0] : '.',
235 qemu_isprint(buf[1]) ? buf[1] : '.',
236 qemu_isprint(buf[2]) ? buf[2] : '.',
237 qemu_isprint(buf[3]) ? buf[3] : '.',
238 qemu_isprint(buf[4]) ? buf[4] : '.',
239 qemu_isprint(buf[5]) ? buf[5] : '.',
240 qemu_isprint(buf[6]) ? buf[6] : '.',
241 qemu_isprint(buf[7]) ? buf[7] : '.');
243 if (memcmp(buf, "NBDMAGIC", 8) != 0) {
244 LOG("Invalid magic received");
245 errno = EINVAL;
246 return -1;
249 if (read_sync(csock, &magic, sizeof(magic)) != sizeof(magic)) {
250 LOG("read failed");
251 errno = EINVAL;
252 return -1;
254 magic = be64_to_cpu(magic);
255 TRACE("Magic is 0x%" PRIx64, magic);
257 if (name) {
258 uint32_t reserved = 0;
259 uint32_t opt;
260 uint32_t namesize;
262 TRACE("Checking magic (opts_magic)");
263 if (magic != 0x49484156454F5054LL) {
264 LOG("Bad magic received");
265 errno = EINVAL;
266 return -1;
268 if (read_sync(csock, &tmp, sizeof(tmp)) != sizeof(tmp)) {
269 LOG("flags read failed");
270 errno = EINVAL;
271 return -1;
273 *flags = be16_to_cpu(tmp) << 16;
274 /* reserved for future use */
275 if (write_sync(csock, &reserved, sizeof(reserved)) !=
276 sizeof(reserved)) {
277 LOG("write failed (reserved)");
278 errno = EINVAL;
279 return -1;
281 /* write the export name */
282 magic = cpu_to_be64(magic);
283 if (write_sync(csock, &magic, sizeof(magic)) != sizeof(magic)) {
284 LOG("write failed (magic)");
285 errno = EINVAL;
286 return -1;
288 opt = cpu_to_be32(NBD_OPT_EXPORT_NAME);
289 if (write_sync(csock, &opt, sizeof(opt)) != sizeof(opt)) {
290 LOG("write failed (opt)");
291 errno = EINVAL;
292 return -1;
294 namesize = cpu_to_be32(strlen(name));
295 if (write_sync(csock, &namesize, sizeof(namesize)) !=
296 sizeof(namesize)) {
297 LOG("write failed (namesize)");
298 errno = EINVAL;
299 return -1;
301 if (write_sync(csock, (char*)name, strlen(name)) != strlen(name)) {
302 LOG("write failed (name)");
303 errno = EINVAL;
304 return -1;
306 } else {
307 TRACE("Checking magic (cli_magic)");
309 if (magic != 0x00420281861253LL) {
310 LOG("Bad magic received");
311 errno = EINVAL;
312 return -1;
316 if (read_sync(csock, &s, sizeof(s)) != sizeof(s)) {
317 LOG("read failed");
318 errno = EINVAL;
319 return -1;
321 *size = be64_to_cpu(s);
322 *blocksize = 1024;
323 TRACE("Size is %" PRIu64, *size);
325 if (!name) {
326 if (read_sync(csock, flags, sizeof(*flags)) != sizeof(*flags)) {
327 LOG("read failed (flags)");
328 errno = EINVAL;
329 return -1;
331 *flags = be32_to_cpup(flags);
332 } else {
333 if (read_sync(csock, &tmp, sizeof(tmp)) != sizeof(tmp)) {
334 LOG("read failed (tmp)");
335 errno = EINVAL;
336 return -1;
338 *flags |= be32_to_cpu(tmp);
340 if (read_sync(csock, &buf, 124) != 124) {
341 LOG("read failed (buf)");
342 errno = EINVAL;
343 return -1;
345 return 0;
348 #ifdef __linux__
349 int nbd_init(int fd, int csock, uint32_t flags, off_t size, size_t blocksize)
351 TRACE("Setting block size to %lu", (unsigned long)blocksize);
353 if (ioctl(fd, NBD_SET_BLKSIZE, blocksize) == -1) {
354 int serrno = errno;
355 LOG("Failed setting NBD block size");
356 errno = serrno;
357 return -1;
360 TRACE("Setting size to %zd block(s)", (size_t)(size / blocksize));
362 if (ioctl(fd, NBD_SET_SIZE_BLOCKS, size / blocksize) == -1) {
363 int serrno = errno;
364 LOG("Failed setting size (in blocks)");
365 errno = serrno;
366 return -1;
369 if (flags & NBD_FLAG_READ_ONLY) {
370 int read_only = 1;
371 TRACE("Setting readonly attribute");
373 if (ioctl(fd, BLKROSET, (unsigned long) &read_only) < 0) {
374 int serrno = errno;
375 LOG("Failed setting read-only attribute");
376 errno = serrno;
377 return -1;
381 if (ioctl(fd, NBD_SET_FLAGS, flags) < 0
382 && errno != ENOTTY) {
383 int serrno = errno;
384 LOG("Failed setting flags");
385 errno = serrno;
386 return -1;
389 TRACE("Clearing NBD socket");
391 if (ioctl(fd, NBD_CLEAR_SOCK) == -1) {
392 int serrno = errno;
393 LOG("Failed clearing NBD socket");
394 errno = serrno;
395 return -1;
398 TRACE("Setting NBD socket");
400 if (ioctl(fd, NBD_SET_SOCK, csock) == -1) {
401 int serrno = errno;
402 LOG("Failed to set NBD socket");
403 errno = serrno;
404 return -1;
407 TRACE("Negotiation ended");
409 return 0;
412 int nbd_disconnect(int fd)
414 ioctl(fd, NBD_CLEAR_QUE);
415 ioctl(fd, NBD_DISCONNECT);
416 ioctl(fd, NBD_CLEAR_SOCK);
417 return 0;
420 int nbd_client(int fd)
422 int ret;
423 int serrno;
425 TRACE("Doing NBD loop");
427 ret = ioctl(fd, NBD_DO_IT);
428 serrno = errno;
430 TRACE("NBD loop returned %d: %s", ret, strerror(serrno));
432 TRACE("Clearing NBD queue");
433 ioctl(fd, NBD_CLEAR_QUE);
435 TRACE("Clearing NBD socket");
436 ioctl(fd, NBD_CLEAR_SOCK);
438 errno = serrno;
439 return ret;
441 #else
442 int nbd_init(int fd, int csock, uint32_t flags, off_t size, size_t blocksize)
444 errno = ENOTSUP;
445 return -1;
448 int nbd_disconnect(int fd)
450 errno = ENOTSUP;
451 return -1;
454 int nbd_client(int fd)
456 errno = ENOTSUP;
457 return -1;
459 #endif
461 int nbd_send_request(int csock, struct nbd_request *request)
463 uint8_t buf[4 + 4 + 8 + 8 + 4];
465 cpu_to_be32w((uint32_t*)buf, NBD_REQUEST_MAGIC);
466 cpu_to_be32w((uint32_t*)(buf + 4), request->type);
467 cpu_to_be64w((uint64_t*)(buf + 8), request->handle);
468 cpu_to_be64w((uint64_t*)(buf + 16), request->from);
469 cpu_to_be32w((uint32_t*)(buf + 24), request->len);
471 TRACE("Sending request to client: "
472 "{ .from = %" PRIu64", .len = %u, .handle = %" PRIu64", .type=%i}",
473 request->from, request->len, request->handle, request->type);
475 if (write_sync(csock, buf, sizeof(buf)) != sizeof(buf)) {
476 LOG("writing to socket failed");
477 errno = EINVAL;
478 return -1;
480 return 0;
483 static int nbd_receive_request(int csock, struct nbd_request *request)
485 uint8_t buf[4 + 4 + 8 + 8 + 4];
486 uint32_t magic;
488 if (read_sync(csock, buf, sizeof(buf)) != sizeof(buf)) {
489 LOG("read failed");
490 errno = EINVAL;
491 return -1;
494 /* Request
495 [ 0 .. 3] magic (NBD_REQUEST_MAGIC)
496 [ 4 .. 7] type (0 == READ, 1 == WRITE)
497 [ 8 .. 15] handle
498 [16 .. 23] from
499 [24 .. 27] len
502 magic = be32_to_cpup((uint32_t*)buf);
503 request->type = be32_to_cpup((uint32_t*)(buf + 4));
504 request->handle = be64_to_cpup((uint64_t*)(buf + 8));
505 request->from = be64_to_cpup((uint64_t*)(buf + 16));
506 request->len = be32_to_cpup((uint32_t*)(buf + 24));
508 TRACE("Got request: "
509 "{ magic = 0x%x, .type = %d, from = %" PRIu64" , len = %u }",
510 magic, request->type, request->from, request->len);
512 if (magic != NBD_REQUEST_MAGIC) {
513 LOG("invalid magic (got 0x%x)", magic);
514 errno = EINVAL;
515 return -1;
517 return 0;
520 int nbd_receive_reply(int csock, struct nbd_reply *reply)
522 uint8_t buf[NBD_REPLY_SIZE];
523 uint32_t magic;
525 memset(buf, 0xAA, sizeof(buf));
527 if (read_sync(csock, buf, sizeof(buf)) != sizeof(buf)) {
528 LOG("read failed");
529 errno = EINVAL;
530 return -1;
533 /* Reply
534 [ 0 .. 3] magic (NBD_REPLY_MAGIC)
535 [ 4 .. 7] error (0 == no error)
536 [ 7 .. 15] handle
539 magic = be32_to_cpup((uint32_t*)buf);
540 reply->error = be32_to_cpup((uint32_t*)(buf + 4));
541 reply->handle = be64_to_cpup((uint64_t*)(buf + 8));
543 TRACE("Got reply: "
544 "{ magic = 0x%x, .error = %d, handle = %" PRIu64" }",
545 magic, reply->error, reply->handle);
547 if (magic != NBD_REPLY_MAGIC) {
548 LOG("invalid magic (got 0x%x)", magic);
549 errno = EINVAL;
550 return -1;
552 return 0;
555 static int nbd_send_reply(int csock, struct nbd_reply *reply)
557 uint8_t buf[4 + 4 + 8];
559 /* Reply
560 [ 0 .. 3] magic (NBD_REPLY_MAGIC)
561 [ 4 .. 7] error (0 == no error)
562 [ 7 .. 15] handle
564 cpu_to_be32w((uint32_t*)buf, NBD_REPLY_MAGIC);
565 cpu_to_be32w((uint32_t*)(buf + 4), reply->error);
566 cpu_to_be64w((uint64_t*)(buf + 8), reply->handle);
568 TRACE("Sending response to client");
570 if (write_sync(csock, buf, sizeof(buf)) != sizeof(buf)) {
571 LOG("writing to socket failed");
572 errno = EINVAL;
573 return -1;
575 return 0;
578 int nbd_trip(BlockDriverState *bs, int csock, off_t size, uint64_t dev_offset,
579 off_t *offset, uint32_t nbdflags, uint8_t *data, int data_size)
581 struct nbd_request request;
582 struct nbd_reply reply;
584 TRACE("Reading request.");
586 if (nbd_receive_request(csock, &request) == -1)
587 return -1;
589 if (request.len + NBD_REPLY_SIZE > data_size) {
590 LOG("len (%u) is larger than max len (%u)",
591 request.len + NBD_REPLY_SIZE, data_size);
592 errno = EINVAL;
593 return -1;
596 if ((request.from + request.len) < request.from) {
597 LOG("integer overflow detected! "
598 "you're probably being attacked");
599 errno = EINVAL;
600 return -1;
603 if ((request.from + request.len) > size) {
604 LOG("From: %" PRIu64 ", Len: %u, Size: %" PRIu64
605 ", Offset: %" PRIu64 "\n",
606 request.from, request.len, (uint64_t)size, dev_offset);
607 LOG("requested operation past EOF--bad client?");
608 errno = EINVAL;
609 return -1;
612 TRACE("Decoding type");
614 reply.handle = request.handle;
615 reply.error = 0;
617 switch (request.type) {
618 case NBD_CMD_READ:
619 TRACE("Request type is READ");
621 if (bdrv_read(bs, (request.from + dev_offset) / 512,
622 data + NBD_REPLY_SIZE,
623 request.len / 512) == -1) {
624 LOG("reading from file failed");
625 errno = EINVAL;
626 return -1;
628 *offset += request.len;
630 TRACE("Read %u byte(s)", request.len);
632 /* Reply
633 [ 0 .. 3] magic (NBD_REPLY_MAGIC)
634 [ 4 .. 7] error (0 == no error)
635 [ 7 .. 15] handle
638 cpu_to_be32w((uint32_t*)data, NBD_REPLY_MAGIC);
639 cpu_to_be32w((uint32_t*)(data + 4), reply.error);
640 cpu_to_be64w((uint64_t*)(data + 8), reply.handle);
642 TRACE("Sending data to client");
644 if (write_sync(csock, data,
645 request.len + NBD_REPLY_SIZE) !=
646 request.len + NBD_REPLY_SIZE) {
647 LOG("writing to socket failed");
648 errno = EINVAL;
649 return -1;
651 break;
652 case NBD_CMD_WRITE:
653 TRACE("Request type is WRITE");
655 TRACE("Reading %u byte(s)", request.len);
657 if (read_sync(csock, data, request.len) != request.len) {
658 LOG("reading from socket failed");
659 errno = EINVAL;
660 return -1;
663 if (nbdflags & NBD_FLAG_READ_ONLY) {
664 TRACE("Server is read-only, return error");
665 reply.error = 1;
666 } else {
667 TRACE("Writing to device");
669 if (bdrv_write(bs, (request.from + dev_offset) / 512,
670 data, request.len / 512) == -1) {
671 LOG("writing to file failed");
672 errno = EINVAL;
673 return -1;
676 *offset += request.len;
679 if (nbd_send_reply(csock, &reply) == -1)
680 return -1;
681 break;
682 case NBD_CMD_DISC:
683 TRACE("Request type is DISCONNECT");
684 errno = 0;
685 return 1;
686 default:
687 LOG("invalid request type (%u) received", request.type);
688 errno = EINVAL;
689 return -1;
692 TRACE("Request/Reply complete");
694 return 0;