2 * Copyright (C) 2005 Anthony Liguori <anthony@codemonkey.ws>
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, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include <sys/ioctl.h>
27 #include <sys/socket.h>
28 #include <netinet/in.h>
29 #include <netinet/tcp.h>
30 #include <arpa/inet.h>
35 #define LOG(msg, ...) do { \
36 fprintf(stderr, "%s:%s():L%d: " msg "\n", \
37 __FILE__, __FUNCTION__, __LINE__, ## __VA_ARGS__); \
40 #define TRACE(msg, ...) do { \
41 if (verbose) LOG(msg, ## __VA_ARGS__); \
44 /* This is all part of the "official" NBD API */
46 #define NBD_REQUEST_MAGIC 0x25609513
47 #define NBD_REPLY_MAGIC 0x67446698
49 #define NBD_SET_SOCK _IO(0xab, 0)
50 #define NBD_SET_BLKSIZE _IO(0xab, 1)
51 #define NBD_SET_SIZE _IO(0xab, 2)
52 #define NBD_DO_IT _IO(0xab, 3)
53 #define NBD_CLEAR_SOCK _IO(0xab, 4)
54 #define NBD_CLEAR_QUE _IO(0xab, 5)
55 #define NBD_PRINT_DEBUG _IO(0xab, 6)
56 #define NBD_SET_SIZE_BLOCKS _IO(0xab, 7)
57 #define NBD_DISCONNECT _IO(0xab, 8)
59 /* That's all folks */
61 #define read_sync(fd, buffer, size) wr_sync(fd, buffer, size, true)
62 #define write_sync(fd, buffer, size) wr_sync(fd, buffer, size, false)
64 static size_t wr_sync(int fd
, void *buffer
, size_t size
, bool do_read
)
68 while (offset
< size
) {
72 len
= read(fd
, buffer
+ offset
, size
- offset
);
74 len
= write(fd
, buffer
+ offset
, size
- offset
);
77 /* recoverable error */
78 if (len
== -1 && errno
== EAGAIN
) {
87 /* unrecoverable error */
98 static int tcp_socket_outgoing(const char *address
, uint16_t port
)
102 struct sockaddr_in addr
;
105 s
= socket(PF_INET
, SOCK_STREAM
, 0);
110 if (inet_aton(address
, &in
) == 0) {
113 ent
= gethostbyname(address
);
118 memcpy(&in
, ent
->h_addr
, sizeof(in
));
121 addr
.sin_family
= AF_INET
;
122 addr
.sin_port
= htons(port
);
123 memcpy(&addr
.sin_addr
.s_addr
, &in
, sizeof(in
));
125 if (connect(s
, (struct sockaddr
*)&addr
, sizeof(addr
)) == -1) {
137 int tcp_socket_incoming(const char *address
, uint16_t port
)
141 struct sockaddr_in addr
;
145 s
= socket(PF_INET
, SOCK_STREAM
, 0);
150 if (inet_aton(address
, &in
) == 0) {
153 ent
= gethostbyname(address
);
158 memcpy(&in
, ent
->h_addr
, sizeof(in
));
161 addr
.sin_family
= AF_INET
;
162 addr
.sin_port
= htons(port
);
163 memcpy(&addr
.sin_addr
.s_addr
, &in
, sizeof(in
));
166 if (setsockopt(s
, SOL_SOCKET
, SO_REUSEADDR
, &opt
, sizeof(opt
)) == -1) {
170 if (bind(s
, (struct sockaddr
*)&addr
, sizeof(addr
)) == -1) {
174 if (listen(s
, 128) == -1) {
200 int nbd_negotiate(BlockDriverState
*bs
, int csock
, off_t size
)
202 char buf
[8 + 8 + 8 + 128];
205 [ 0 .. 7] passwd ("NBDMAGIC")
206 [ 8 .. 15] magic (0x00420281861253)
208 [24 .. 151] reserved (0)
211 TRACE("Beginning negotiation.");
212 memcpy(buf
, "NBDMAGIC", 8);
213 cpu_to_be64w((uint64_t*)(buf
+ 8), 0x00420281861253LL
);
214 cpu_to_be64w((uint64_t*)(buf
+ 16), size
);
215 memset(buf
+ 24, 0, 128);
217 if (write_sync(csock
, buf
, sizeof(buf
)) != sizeof(buf
)) {
223 TRACE("Negotation succeeded.");
228 int nbd_receive_negotiate(int fd
, int csock
)
230 char buf
[8 + 8 + 8 + 128];
235 TRACE("Receiving negotation.");
237 if (read_sync(csock
, buf
, sizeof(buf
)) != sizeof(buf
)) {
243 magic
= be64_to_cpup((uint64_t*)(buf
+ 8));
244 size
= be64_to_cpup((uint64_t*)(buf
+ 16));
247 TRACE("Magic is %c%c%c%c%c%c%c%c",
248 isprint(buf
[0]) ? buf
[0] : '.',
249 isprint(buf
[1]) ? buf
[1] : '.',
250 isprint(buf
[2]) ? buf
[2] : '.',
251 isprint(buf
[3]) ? buf
[3] : '.',
252 isprint(buf
[4]) ? buf
[4] : '.',
253 isprint(buf
[5]) ? buf
[5] : '.',
254 isprint(buf
[6]) ? buf
[6] : '.',
255 isprint(buf
[7]) ? buf
[7] : '.');
256 TRACE("Magic is 0x%" PRIx64
, magic
);
257 TRACE("Size is %" PRIu64
, size
);
259 if (memcmp(buf
, "NBDMAGIC", 8) != 0) {
260 LOG("Invalid magic received");
265 TRACE("Checking magic");
267 if (magic
!= 0x00420281861253LL
) {
268 LOG("Bad magic received");
273 TRACE("Setting block size to %lu", (unsigned long)blocksize
);
275 if (ioctl(fd
, NBD_SET_BLKSIZE
, blocksize
) == -1) {
277 LOG("Failed setting NBD block size");
282 TRACE("Setting size to %llu block(s)",
283 (unsigned long long)(size
/ blocksize
));
285 if (ioctl(fd
, NBD_SET_SIZE_BLOCKS
, size
/ blocksize
) == -1) {
287 LOG("Failed setting size (in blocks)");
292 TRACE("Clearing NBD socket");
294 if (ioctl(fd
, NBD_CLEAR_SOCK
) == -1) {
296 LOG("Failed clearing NBD socket");
301 TRACE("Setting NBD socket");
303 if (ioctl(fd
, NBD_SET_SOCK
, csock
) == -1) {
305 LOG("Failed to set NBD socket");
310 TRACE("Negotiation ended");
315 int nbd_disconnect(int fd
)
317 ioctl(fd
, NBD_CLEAR_QUE
);
318 ioctl(fd
, NBD_DISCONNECT
);
319 ioctl(fd
, NBD_CLEAR_SOCK
);
323 int nbd_client(int fd
, int csock
)
328 TRACE("Doing NBD loop");
330 ret
= ioctl(fd
, NBD_DO_IT
);
333 TRACE("NBD loop returned %d: %s", ret
, strerror(serrno
));
335 TRACE("Clearing NBD queue");
336 ioctl(fd
, NBD_CLEAR_QUE
);
338 TRACE("Clearing NBD socket");
339 ioctl(fd
, NBD_CLEAR_SOCK
);
345 int nbd_trip(BlockDriverState
*bs
, int csock
, off_t size
, uint64_t dev_offset
, off_t
*offset
, bool readonly
)
348 static uint8_t data
[1024 * 1024]; // keep this off of the stack
350 uint8_t data
[1024 * 1024];
352 uint8_t buf
[4 + 4 + 8 + 8 + 4];
358 TRACE("Reading request.");
360 if (read_sync(csock
, buf
, sizeof(buf
)) != sizeof(buf
)) {
367 [ 0 .. 3] magic (NBD_REQUEST_MAGIC)
368 [ 4 .. 7] type (0 == READ, 1 == WRITE)
374 magic
= be32_to_cpup((uint32_t*)buf
);
375 type
= be32_to_cpup((uint32_t*)(buf
+ 4));
376 from
= be64_to_cpup((uint64_t*)(buf
+ 16));
377 len
= be32_to_cpup((uint32_t*)(buf
+ 24));
379 TRACE("Got request: "
380 "{ magic = 0x%x, .type = %d, from = %" PRIu64
" , len = %u }",
381 magic
, type
, from
, len
);
384 if (magic
!= NBD_REQUEST_MAGIC
) {
385 LOG("invalid magic (got 0x%x)", magic
);
390 if (len
> sizeof(data
)) {
391 LOG("len (%u) is larger than max len (%lu)",
392 len
, (unsigned long)sizeof(data
));
397 if ((from
+ len
) < from
) {
398 LOG("integer overflow detected! "
399 "you're probably being attacked");
404 if ((from
+ len
) > size
) {
405 LOG("From: %" PRIu64
", Len: %u, Size: %" PRIu64
406 ", Offset: %" PRIu64
"\n",
407 from
, len
, size
, dev_offset
);
408 LOG("requested operation past EOF--bad client?");
414 [ 0 .. 3] magic (NBD_REPLY_MAGIC)
415 [ 4 .. 7] error (0 == no error)
418 cpu_to_be32w((uint32_t*)buf
, NBD_REPLY_MAGIC
);
419 cpu_to_be32w((uint32_t*)(buf
+ 4), 0);
421 TRACE("Decoding type");
425 TRACE("Request type is READ");
427 if (bdrv_read(bs
, (from
+ dev_offset
) / 512, data
, len
/ 512) == -1) {
428 LOG("reading from file failed");
434 TRACE("Read %u byte(s)", len
);
436 TRACE("Sending OK response");
438 if (write_sync(csock
, buf
, 16) != 16) {
439 LOG("writing to socket failed");
444 TRACE("Sending data to client");
446 if (write_sync(csock
, data
, len
) != len
) {
447 LOG("writing to socket failed");
453 TRACE("Request type is WRITE");
455 TRACE("Reading %u byte(s)", len
);
457 if (read_sync(csock
, data
, len
) != len
) {
458 LOG("reading from socket failed");
464 TRACE("Server is read-only, return error");
466 cpu_to_be32w((uint32_t*)(buf
+ 4), 1);
468 TRACE("Writing to device");
470 if (bdrv_write(bs
, (from
+ dev_offset
) / 512, data
, len
/ 512) == -1) {
471 LOG("writing to file failed");
479 TRACE("Sending response to client");
481 if (write_sync(csock
, buf
, 16) != 16) {
482 LOG("writing to socket failed");
488 TRACE("Request type is DISCONNECT");
492 LOG("invalid request type (%u) received", type
);
497 TRACE("Request/Reply complete");