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/>.
25 #include <sys/ioctl.h>
27 #if defined(__sun__) || defined(__HAIKU__)
28 #include <sys/ioccom.h>
37 #include "qemu_socket.h"
42 #define TRACE(msg, ...) do { \
43 LOG(msg, ## __VA_ARGS__); \
46 #define TRACE(msg, ...) \
50 #define LOG(msg, ...) do { \
51 fprintf(stderr, "%s:%s():L%d: " msg "\n", \
52 __FILE__, __FUNCTION__, __LINE__, ## __VA_ARGS__); \
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)
84 if (qemu_in_coroutine()) {
86 return qemu_co_recv(fd, buffer, size);
88 return qemu_co_send(fd, buffer, size);
92 while (offset < size) {
96 len = qemu_recv(fd, buffer + offset, size - offset, 0);
98 len = send(fd, buffer + offset, size - offset, 0);
102 errno = socket_error();
104 /* recoverable error */
105 if (len == -1 && (errno == EAGAIN || errno == EINTR)) {
114 /* unrecoverable error */
125 static void combine_addr(char *buf, size_t len, const char* address,
128 /* If the address-part contains a colon, it's an IPv6 IP so needs [] */
129 if (strstr(address, ":")) {
130 snprintf(buf, len, "[%s]:%u", address, port);
132 snprintf(buf, len, "%s:%u", address, port);
136 int tcp_socket_outgoing(const char *address, uint16_t port)
138 char address_and_port[128];
139 combine_addr(address_and_port, 128, address, port);
140 return tcp_socket_outgoing_spec(address_and_port);
143 int tcp_socket_outgoing_spec(const char *address_and_port)
145 return inet_connect(address_and_port, SOCK_STREAM);
148 int tcp_socket_incoming(const char *address, uint16_t port)
150 char address_and_port[128];
151 combine_addr(address_and_port, 128, address, port);
152 return tcp_socket_incoming_spec(address_and_port);
155 int tcp_socket_incoming_spec(const char *address_and_port)
159 return inet_listen(address_and_port, ostr, olen, SOCK_STREAM, 0);
162 int unix_socket_incoming(const char *path)
167 return unix_listen(path, ostr, olen);
170 int unix_socket_outgoing(const char *path)
172 return unix_connect(path);
189 int nbd_negotiate(int csock, off_t size, uint32_t flags)
191 char buf[8 + 8 + 8 + 128];
194 [ 0 .. 7] passwd ("NBDMAGIC")
195 [ 8 .. 15] magic (0x00420281861253)
198 [28 .. 151] reserved (0)
201 TRACE("Beginning negotiation.");
202 memcpy(buf, "NBDMAGIC", 8);
203 cpu_to_be64w((uint64_t*)(buf + 8), 0x00420281861253LL);
204 cpu_to_be64w((uint64_t*)(buf + 16), size);
205 cpu_to_be32w((uint32_t*)(buf + 24),
206 flags | NBD_FLAG_HAS_FLAGS | NBD_FLAG_SEND_TRIM |
207 NBD_FLAG_SEND_FLUSH | NBD_FLAG_SEND_FUA);
208 memset(buf + 28, 0, 124);
210 if (write_sync(csock, buf, sizeof(buf)) != sizeof(buf)) {
216 TRACE("Negotiation succeeded.");
221 int nbd_receive_negotiate(int csock, const char *name, uint32_t *flags,
222 off_t *size, size_t *blocksize)
228 TRACE("Receiving negotiation.");
230 if (read_sync(csock, buf, 8) != 8) {
237 if (strlen(buf) == 0) {
238 LOG("server connection closed");
243 TRACE("Magic is %c%c%c%c%c%c%c%c",
244 qemu_isprint(buf[0]) ? buf[0] : '.',
245 qemu_isprint(buf[1]) ? buf[1] : '.',
246 qemu_isprint(buf[2]) ? buf[2] : '.',
247 qemu_isprint(buf[3]) ? buf[3] : '.',
248 qemu_isprint(buf[4]) ? buf[4] : '.',
249 qemu_isprint(buf[5]) ? buf[5] : '.',
250 qemu_isprint(buf[6]) ? buf[6] : '.',
251 qemu_isprint(buf[7]) ? buf[7] : '.');
253 if (memcmp(buf, "NBDMAGIC", 8) != 0) {
254 LOG("Invalid magic received");
259 if (read_sync(csock, &magic, sizeof(magic)) != sizeof(magic)) {
264 magic = be64_to_cpu(magic);
265 TRACE("Magic is 0x%" PRIx64, magic);
268 uint32_t reserved = 0;
272 TRACE("Checking magic (opts_magic)");
273 if (magic != 0x49484156454F5054LL) {
274 LOG("Bad magic received");
278 if (read_sync(csock, &tmp, sizeof(tmp)) != sizeof(tmp)) {
279 LOG("flags read failed");
283 *flags = be16_to_cpu(tmp) << 16;
284 /* reserved for future use */
285 if (write_sync(csock, &reserved, sizeof(reserved)) !=
287 LOG("write failed (reserved)");
291 /* write the export name */
292 magic = cpu_to_be64(magic);
293 if (write_sync(csock, &magic, sizeof(magic)) != sizeof(magic)) {
294 LOG("write failed (magic)");
298 opt = cpu_to_be32(NBD_OPT_EXPORT_NAME);
299 if (write_sync(csock, &opt, sizeof(opt)) != sizeof(opt)) {
300 LOG("write failed (opt)");
304 namesize = cpu_to_be32(strlen(name));
305 if (write_sync(csock, &namesize, sizeof(namesize)) !=
307 LOG("write failed (namesize)");
311 if (write_sync(csock, (char*)name, strlen(name)) != strlen(name)) {
312 LOG("write failed (name)");
317 TRACE("Checking magic (cli_magic)");
319 if (magic != 0x00420281861253LL) {
320 LOG("Bad magic received");
326 if (read_sync(csock, &s, sizeof(s)) != sizeof(s)) {
331 *size = be64_to_cpu(s);
333 TRACE("Size is %" PRIu64, *size);
336 if (read_sync(csock, flags, sizeof(*flags)) != sizeof(*flags)) {
337 LOG("read failed (flags)");
341 *flags = be32_to_cpup(flags);
343 if (read_sync(csock, &tmp, sizeof(tmp)) != sizeof(tmp)) {
344 LOG("read failed (tmp)");
348 *flags |= be32_to_cpu(tmp);
350 if (read_sync(csock, &buf, 124) != 124) {
351 LOG("read failed (buf)");
359 int nbd_init(int fd, int csock, uint32_t flags, off_t size, size_t blocksize)
361 TRACE("Setting NBD socket");
363 if (ioctl(fd, NBD_SET_SOCK, csock) == -1) {
365 LOG("Failed to set NBD socket");
370 TRACE("Setting block size to %lu", (unsigned long)blocksize);
372 if (ioctl(fd, NBD_SET_BLKSIZE, blocksize) == -1) {
374 LOG("Failed setting NBD block size");
379 TRACE("Setting size to %zd block(s)", (size_t)(size / blocksize));
381 if (ioctl(fd, NBD_SET_SIZE_BLOCKS, size / blocksize) == -1) {
383 LOG("Failed setting size (in blocks)");
388 if (flags & NBD_FLAG_READ_ONLY) {
390 TRACE("Setting readonly attribute");
392 if (ioctl(fd, BLKROSET, (unsigned long) &read_only) < 0) {
394 LOG("Failed setting read-only attribute");
400 if (ioctl(fd, NBD_SET_FLAGS, flags) < 0
401 && errno != ENOTTY) {
403 LOG("Failed setting flags");
408 TRACE("Negotiation ended");
413 int nbd_disconnect(int fd)
415 ioctl(fd, NBD_CLEAR_QUE);
416 ioctl(fd, NBD_DISCONNECT);
417 ioctl(fd, NBD_CLEAR_SOCK);
421 int nbd_client(int fd)
426 TRACE("Doing NBD loop");
428 ret = ioctl(fd, NBD_DO_IT);
429 if (ret == -1 && errno == EPIPE) {
430 /* NBD_DO_IT normally returns EPIPE when someone has disconnected
431 * the socket via NBD_DISCONNECT. We do not want to return 1 in
438 TRACE("NBD loop returned %d: %s", ret, strerror(serrno));
440 TRACE("Clearing NBD queue");
441 ioctl(fd, NBD_CLEAR_QUE);
443 TRACE("Clearing NBD socket");
444 ioctl(fd, NBD_CLEAR_SOCK);
450 int nbd_init(int fd, int csock, uint32_t flags, off_t size, size_t blocksize)
456 int nbd_disconnect(int fd)
462 int nbd_client(int fd)
469 int nbd_send_request(int csock, struct nbd_request *request)
471 uint8_t buf[4 + 4 + 8 + 8 + 4];
473 cpu_to_be32w((uint32_t*)buf, NBD_REQUEST_MAGIC);
474 cpu_to_be32w((uint32_t*)(buf + 4), request->type);
475 cpu_to_be64w((uint64_t*)(buf + 8), request->handle);
476 cpu_to_be64w((uint64_t*)(buf + 16), request->from);
477 cpu_to_be32w((uint32_t*)(buf + 24), request->len);
479 TRACE("Sending request to client: "
480 "{ .from = %" PRIu64", .len = %u, .handle = %" PRIu64", .type=%i}",
481 request->from, request->len, request->handle, request->type);
483 if (write_sync(csock, buf, sizeof(buf)) != sizeof(buf)) {
484 LOG("writing to socket failed");
491 static int nbd_receive_request(int csock, struct nbd_request *request)
493 uint8_t buf[4 + 4 + 8 + 8 + 4];
496 if (read_sync(csock, buf, sizeof(buf)) != sizeof(buf)) {
503 [ 0 .. 3] magic (NBD_REQUEST_MAGIC)
504 [ 4 .. 7] type (0 == READ, 1 == WRITE)
510 magic = be32_to_cpup((uint32_t*)buf);
511 request->type = be32_to_cpup((uint32_t*)(buf + 4));
512 request->handle = be64_to_cpup((uint64_t*)(buf + 8));
513 request->from = be64_to_cpup((uint64_t*)(buf + 16));
514 request->len = be32_to_cpup((uint32_t*)(buf + 24));
516 TRACE("Got request: "
517 "{ magic = 0x%x, .type = %d, from = %" PRIu64" , len = %u }",
518 magic, request->type, request->from, request->len);
520 if (magic != NBD_REQUEST_MAGIC) {
521 LOG("invalid magic (got 0x%x)", magic);
528 int nbd_receive_reply(int csock, struct nbd_reply *reply)
530 uint8_t buf[NBD_REPLY_SIZE];
533 memset(buf, 0xAA, sizeof(buf));
535 if (read_sync(csock, buf, sizeof(buf)) != sizeof(buf)) {
542 [ 0 .. 3] magic (NBD_REPLY_MAGIC)
543 [ 4 .. 7] error (0 == no error)
547 magic = be32_to_cpup((uint32_t*)buf);
548 reply->error = be32_to_cpup((uint32_t*)(buf + 4));
549 reply->handle = be64_to_cpup((uint64_t*)(buf + 8));
552 "{ magic = 0x%x, .error = %d, handle = %" PRIu64" }",
553 magic, reply->error, reply->handle);
555 if (magic != NBD_REPLY_MAGIC) {
556 LOG("invalid magic (got 0x%x)", magic);
563 static int nbd_send_reply(int csock, struct nbd_reply *reply)
565 uint8_t buf[4 + 4 + 8];
568 [ 0 .. 3] magic (NBD_REPLY_MAGIC)
569 [ 4 .. 7] error (0 == no error)
572 cpu_to_be32w((uint32_t*)buf, NBD_REPLY_MAGIC);
573 cpu_to_be32w((uint32_t*)(buf + 4), reply->error);
574 cpu_to_be64w((uint64_t*)(buf + 8), reply->handle);
576 TRACE("Sending response to client");
578 if (write_sync(csock, buf, sizeof(buf)) != sizeof(buf)) {
579 LOG("writing to socket failed");
586 int nbd_trip(BlockDriverState *bs, int csock, off_t size,
587 uint64_t dev_offset, uint32_t nbdflags,
590 struct nbd_request request;
591 struct nbd_reply reply;
594 TRACE("Reading request.");
596 if (nbd_receive_request(csock, &request) == -1)
599 if (request.len + NBD_REPLY_SIZE > NBD_BUFFER_SIZE) {
600 LOG("len (%u) is larger than max len (%u)",
601 request.len + NBD_REPLY_SIZE, NBD_BUFFER_SIZE);
606 if ((request.from + request.len) < request.from) {
607 LOG("integer overflow detected! "
608 "you're probably being attacked");
613 if ((request.from + request.len) > size) {
614 LOG("From: %" PRIu64 ", Len: %u, Size: %" PRIu64
615 ", Offset: %" PRIu64 "\n",
616 request.from, request.len, (uint64_t)size, dev_offset);
617 LOG("requested operation past EOF--bad client?");
622 TRACE("Decoding type");
624 reply.handle = request.handle;
627 switch (request.type & NBD_CMD_MASK_COMMAND) {
629 TRACE("Request type is READ");
631 ret = bdrv_read(bs, (request.from + dev_offset) / 512,
632 data + NBD_REPLY_SIZE,
635 LOG("reading from file failed");
640 TRACE("Read %u byte(s)", request.len);
643 [ 0 .. 3] magic (NBD_REPLY_MAGIC)
644 [ 4 .. 7] error (0 == no error)
648 cpu_to_be32w((uint32_t*)data, NBD_REPLY_MAGIC);
649 cpu_to_be32w((uint32_t*)(data + 4), reply.error);
650 cpu_to_be64w((uint64_t*)(data + 8), reply.handle);
652 TRACE("Sending data to client");
654 if (write_sync(csock, data,
655 request.len + NBD_REPLY_SIZE) !=
656 request.len + NBD_REPLY_SIZE) {
657 LOG("writing to socket failed");
663 TRACE("Request type is WRITE");
665 TRACE("Reading %u byte(s)", request.len);
667 if (read_sync(csock, data, request.len) != request.len) {
668 LOG("reading from socket failed");
673 if (nbdflags & NBD_FLAG_READ_ONLY) {
674 TRACE("Server is read-only, return error");
677 TRACE("Writing to device");
679 ret = bdrv_write(bs, (request.from + dev_offset) / 512,
680 data, request.len / 512);
682 LOG("writing to file failed");
687 if (request.type & NBD_CMD_FLAG_FUA) {
688 ret = bdrv_flush(bs);
696 if (nbd_send_reply(csock, &reply) == -1)
700 TRACE("Request type is DISCONNECT");
704 TRACE("Request type is FLUSH");
706 ret = bdrv_flush(bs);
712 if (nbd_send_reply(csock, &reply) == -1)
716 TRACE("Request type is TRIM");
717 ret = bdrv_discard(bs, (request.from + dev_offset) / 512,
720 LOG("discard failed");
723 if (nbd_send_reply(csock, &reply) == -1)
727 LOG("invalid request type (%u) received", request.type);
732 TRACE("Request/Reply complete");