]> git.proxmox.com Git - qemu.git/blob - nbd.c
Merge NBD client/server, by Laurent Vivier.
[qemu.git] / nbd.c
1 /*\
2 * Copyright (C) 2005 Anthony Liguori <anthony@codemonkey.ws>
3 *
4 * Network Block Device
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, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 \*/
19
20 #include "nbd.h"
21
22 #include <errno.h>
23 #include <string.h>
24 #include <sys/ioctl.h>
25 #include <ctype.h>
26 #include <inttypes.h>
27 #include <sys/socket.h>
28 #include <sys/un.h>
29 #include <netinet/in.h>
30 #include <netinet/tcp.h>
31 #include <arpa/inet.h>
32 #include <netdb.h>
33
34 extern int verbose;
35
36 #define LOG(msg, ...) do { \
37 fprintf(stderr, "%s:%s():L%d: " msg "\n", \
38 __FILE__, __FUNCTION__, __LINE__, ## __VA_ARGS__); \
39 } while(0)
40
41 #define TRACE(msg, ...) do { \
42 if (verbose) LOG(msg, ## __VA_ARGS__); \
43 } while(0)
44
45 /* This is all part of the "official" NBD API */
46
47 #define NBD_REQUEST_MAGIC 0x25609513
48 #define NBD_REPLY_MAGIC 0x67446698
49
50 #define NBD_SET_SOCK _IO(0xab, 0)
51 #define NBD_SET_BLKSIZE _IO(0xab, 1)
52 #define NBD_SET_SIZE _IO(0xab, 2)
53 #define NBD_DO_IT _IO(0xab, 3)
54 #define NBD_CLEAR_SOCK _IO(0xab, 4)
55 #define NBD_CLEAR_QUE _IO(0xab, 5)
56 #define NBD_PRINT_DEBUG _IO(0xab, 6)
57 #define NBD_SET_SIZE_BLOCKS _IO(0xab, 7)
58 #define NBD_DISCONNECT _IO(0xab, 8)
59
60 /* That's all folks */
61
62 #define read_sync(fd, buffer, size) wr_sync(fd, buffer, size, true)
63 #define write_sync(fd, buffer, size) wr_sync(fd, buffer, size, false)
64
65 static size_t wr_sync(int fd, void *buffer, size_t size, bool do_read)
66 {
67 size_t offset = 0;
68
69 while (offset < size) {
70 ssize_t len;
71
72 if (do_read) {
73 len = read(fd, buffer + offset, size - offset);
74 } else {
75 len = write(fd, buffer + offset, size - offset);
76 }
77
78 /* recoverable error */
79 if (len == -1 && errno == EAGAIN) {
80 continue;
81 }
82
83 /* eof */
84 if (len == 0) {
85 break;
86 }
87
88 /* unrecoverable error */
89 if (len == -1) {
90 return 0;
91 }
92
93 offset += len;
94 }
95
96 return offset;
97 }
98
99 static int tcp_socket_outgoing(const char *address, uint16_t port)
100 {
101 int s;
102 struct in_addr in;
103 struct sockaddr_in addr;
104 int serrno;
105
106 s = socket(PF_INET, SOCK_STREAM, 0);
107 if (s == -1) {
108 return -1;
109 }
110
111 if (inet_aton(address, &in) == 0) {
112 struct hostent *ent;
113
114 ent = gethostbyname(address);
115 if (ent == NULL) {
116 goto error;
117 }
118
119 memcpy(&in, ent->h_addr, sizeof(in));
120 }
121
122 addr.sin_family = AF_INET;
123 addr.sin_port = htons(port);
124 memcpy(&addr.sin_addr.s_addr, &in, sizeof(in));
125
126 if (connect(s, (struct sockaddr *)&addr, sizeof(addr)) == -1) {
127 goto error;
128 }
129
130 return s;
131 error:
132 serrno = errno;
133 close(s);
134 errno = serrno;
135 return -1;
136 }
137
138 int tcp_socket_incoming(const char *address, uint16_t port)
139 {
140 int s;
141 struct in_addr in;
142 struct sockaddr_in addr;
143 int serrno;
144 int opt;
145
146 s = socket(PF_INET, SOCK_STREAM, 0);
147 if (s == -1) {
148 return -1;
149 }
150
151 if (inet_aton(address, &in) == 0) {
152 struct hostent *ent;
153
154 ent = gethostbyname(address);
155 if (ent == NULL) {
156 goto error;
157 }
158
159 memcpy(&in, ent->h_addr, sizeof(in));
160 }
161
162 addr.sin_family = AF_INET;
163 addr.sin_port = htons(port);
164 memcpy(&addr.sin_addr.s_addr, &in, sizeof(in));
165
166 opt = 1;
167 if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) == -1) {
168 goto error;
169 }
170
171 if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) == -1) {
172 goto error;
173 }
174
175 if (listen(s, 128) == -1) {
176 goto error;
177 }
178
179 return s;
180 error:
181 serrno = errno;
182 close(s);
183 errno = serrno;
184 return -1;
185 }
186
187 int unix_socket_incoming(const char *path)
188 {
189 int s;
190 struct sockaddr_un addr;
191 int serrno;
192
193 s = socket(PF_UNIX, SOCK_STREAM, 0);
194 if (s == -1) {
195 return -1;
196 }
197
198 memset(&addr, 0, sizeof(addr));
199 addr.sun_family = AF_UNIX;
200 pstrcpy(addr.sun_path, sizeof(addr.sun_path), path);
201
202 if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) == -1) {
203 goto error;
204 }
205
206 if (listen(s, 128) == -1) {
207 goto error;
208 }
209
210 return s;
211 error:
212 serrno = errno;
213 close(s);
214 errno = serrno;
215 return -1;
216 }
217
218 int unix_socket_outgoing(const char *path)
219 {
220 int s;
221 struct sockaddr_un addr;
222 int serrno;
223
224 s = socket(PF_UNIX, SOCK_STREAM, 0);
225 if (s == -1) {
226 return -1;
227 }
228
229 memset(&addr, 0, sizeof(addr));
230 addr.sun_family = AF_UNIX;
231 pstrcpy(addr.sun_path, sizeof(addr.sun_path), path);
232
233 if (connect(s, (struct sockaddr *)&addr, sizeof(addr)) == -1) {
234 goto error;
235 }
236
237 return s;
238 error:
239 serrno = errno;
240 close(s);
241 errno = serrno;
242 return -1;
243 }
244
245
246 /* Basic flow
247
248 Server Client
249
250 Negotiate
251 Request
252 Response
253 Request
254 Response
255 ...
256 ...
257 Request (type == 2)
258 */
259
260 int nbd_negotiate(BlockDriverState *bs, int csock, off_t size)
261 {
262 char buf[8 + 8 + 8 + 128];
263
264 /* Negotiate
265 [ 0 .. 7] passwd ("NBDMAGIC")
266 [ 8 .. 15] magic (0x00420281861253)
267 [16 .. 23] size
268 [24 .. 151] reserved (0)
269 */
270
271 TRACE("Beginning negotiation.");
272 memcpy(buf, "NBDMAGIC", 8);
273 cpu_to_be64w((uint64_t*)(buf + 8), 0x00420281861253LL);
274 cpu_to_be64w((uint64_t*)(buf + 16), size);
275 memset(buf + 24, 0, 128);
276
277 if (write_sync(csock, buf, sizeof(buf)) != sizeof(buf)) {
278 LOG("write failed");
279 errno = EINVAL;
280 return -1;
281 }
282
283 TRACE("Negotation succeeded.");
284
285 return 0;
286 }
287
288 int nbd_receive_negotiate(int csock, off_t *size, size_t *blocksize)
289 {
290 char buf[8 + 8 + 8 + 128];
291 uint64_t magic;
292
293 TRACE("Receiving negotation.");
294
295 if (read_sync(csock, buf, sizeof(buf)) != sizeof(buf)) {
296 LOG("read failed");
297 errno = EINVAL;
298 return -1;
299 }
300
301 magic = be64_to_cpup((uint64_t*)(buf + 8));
302 *size = be64_to_cpup((uint64_t*)(buf + 16));
303 *blocksize = 1024;
304
305 TRACE("Magic is %c%c%c%c%c%c%c%c",
306 isprint(buf[0]) ? buf[0] : '.',
307 isprint(buf[1]) ? buf[1] : '.',
308 isprint(buf[2]) ? buf[2] : '.',
309 isprint(buf[3]) ? buf[3] : '.',
310 isprint(buf[4]) ? buf[4] : '.',
311 isprint(buf[5]) ? buf[5] : '.',
312 isprint(buf[6]) ? buf[6] : '.',
313 isprint(buf[7]) ? buf[7] : '.');
314 TRACE("Magic is 0x%" PRIx64, magic);
315 TRACE("Size is %" PRIu64, *size);
316
317 if (memcmp(buf, "NBDMAGIC", 8) != 0) {
318 LOG("Invalid magic received");
319 errno = EINVAL;
320 return -1;
321 }
322
323 TRACE("Checking magic");
324
325 if (magic != 0x00420281861253LL) {
326 LOG("Bad magic received");
327 errno = EINVAL;
328 return -1;
329 }
330 return 0;
331 }
332
333 int nbd_init(int fd, int csock, off_t size, size_t blocksize)
334 {
335 TRACE("Setting block size to %lu", (unsigned long)blocksize);
336
337 if (ioctl(fd, NBD_SET_BLKSIZE, blocksize) == -1) {
338 int serrno = errno;
339 LOG("Failed setting NBD block size");
340 errno = serrno;
341 return -1;
342 }
343
344 TRACE("Setting size to %llu block(s)",
345 (unsigned long long)(size / blocksize));
346
347 if (ioctl(fd, NBD_SET_SIZE_BLOCKS, size / blocksize) == -1) {
348 int serrno = errno;
349 LOG("Failed setting size (in blocks)");
350 errno = serrno;
351 return -1;
352 }
353
354 TRACE("Clearing NBD socket");
355
356 if (ioctl(fd, NBD_CLEAR_SOCK) == -1) {
357 int serrno = errno;
358 LOG("Failed clearing NBD socket");
359 errno = serrno;
360 return -1;
361 }
362
363 TRACE("Setting NBD socket");
364
365 if (ioctl(fd, NBD_SET_SOCK, csock) == -1) {
366 int serrno = errno;
367 LOG("Failed to set NBD socket");
368 errno = serrno;
369 return -1;
370 }
371
372 TRACE("Negotiation ended");
373
374 return 0;
375 }
376
377 int nbd_disconnect(int fd)
378 {
379 ioctl(fd, NBD_CLEAR_QUE);
380 ioctl(fd, NBD_DISCONNECT);
381 ioctl(fd, NBD_CLEAR_SOCK);
382 return 0;
383 }
384
385 int nbd_client(int fd, int csock)
386 {
387 int ret;
388 int serrno;
389
390 TRACE("Doing NBD loop");
391
392 ret = ioctl(fd, NBD_DO_IT);
393 serrno = errno;
394
395 TRACE("NBD loop returned %d: %s", ret, strerror(serrno));
396
397 TRACE("Clearing NBD queue");
398 ioctl(fd, NBD_CLEAR_QUE);
399
400 TRACE("Clearing NBD socket");
401 ioctl(fd, NBD_CLEAR_SOCK);
402
403 errno = serrno;
404 return ret;
405 }
406
407 int nbd_trip(BlockDriverState *bs, int csock, off_t size, uint64_t dev_offset, off_t *offset, bool readonly)
408 {
409 #ifndef _REENTRANT
410 static uint8_t data[1024 * 1024]; // keep this off of the stack
411 #else
412 uint8_t data[1024 * 1024];
413 #endif
414 uint8_t buf[4 + 4 + 8 + 8 + 4];
415 uint32_t magic;
416 uint32_t type;
417 uint64_t from;
418 uint32_t len;
419
420 TRACE("Reading request.");
421
422 if (read_sync(csock, buf, sizeof(buf)) != sizeof(buf)) {
423 LOG("read failed");
424 errno = EINVAL;
425 return -1;
426 }
427
428 /* Request
429 [ 0 .. 3] magic (NBD_REQUEST_MAGIC)
430 [ 4 .. 7] type (0 == READ, 1 == WRITE)
431 [ 8 .. 15] handle
432 [16 .. 23] from
433 [24 .. 27] len
434 */
435
436 magic = be32_to_cpup((uint32_t*)buf);
437 type = be32_to_cpup((uint32_t*)(buf + 4));
438 from = be64_to_cpup((uint64_t*)(buf + 16));
439 len = be32_to_cpup((uint32_t*)(buf + 24));
440
441 TRACE("Got request: "
442 "{ magic = 0x%x, .type = %d, from = %" PRIu64" , len = %u }",
443 magic, type, from, len);
444
445
446 if (magic != NBD_REQUEST_MAGIC) {
447 LOG("invalid magic (got 0x%x)", magic);
448 errno = EINVAL;
449 return -1;
450 }
451
452 if (len > sizeof(data)) {
453 LOG("len (%u) is larger than max len (%lu)",
454 len, (unsigned long)sizeof(data));
455 errno = EINVAL;
456 return -1;
457 }
458
459 if ((from + len) < from) {
460 LOG("integer overflow detected! "
461 "you're probably being attacked");
462 errno = EINVAL;
463 return -1;
464 }
465
466 if ((from + len) > size) {
467 LOG("From: %" PRIu64 ", Len: %u, Size: %" PRIu64
468 ", Offset: %" PRIu64 "\n",
469 from, len, size, dev_offset);
470 LOG("requested operation past EOF--bad client?");
471 errno = EINVAL;
472 return -1;
473 }
474
475 /* Reply
476 [ 0 .. 3] magic (NBD_REPLY_MAGIC)
477 [ 4 .. 7] error (0 == no error)
478 [ 7 .. 15] handle
479 */
480 cpu_to_be32w((uint32_t*)buf, NBD_REPLY_MAGIC);
481 cpu_to_be32w((uint32_t*)(buf + 4), 0);
482
483 TRACE("Decoding type");
484
485 switch (type) {
486 case 0:
487 TRACE("Request type is READ");
488
489 if (bdrv_read(bs, (from + dev_offset) / 512, data, len / 512) == -1) {
490 LOG("reading from file failed");
491 errno = EINVAL;
492 return -1;
493 }
494 *offset += len;
495
496 TRACE("Read %u byte(s)", len);
497
498 TRACE("Sending OK response");
499
500 if (write_sync(csock, buf, 16) != 16) {
501 LOG("writing to socket failed");
502 errno = EINVAL;
503 return -1;
504 }
505
506 TRACE("Sending data to client");
507
508 if (write_sync(csock, data, len) != len) {
509 LOG("writing to socket failed");
510 errno = EINVAL;
511 return -1;
512 }
513 break;
514 case 1:
515 TRACE("Request type is WRITE");
516
517 TRACE("Reading %u byte(s)", len);
518
519 if (read_sync(csock, data, len) != len) {
520 LOG("reading from socket failed");
521 errno = EINVAL;
522 return -1;
523 }
524
525 if (readonly) {
526 TRACE("Server is read-only, return error");
527
528 cpu_to_be32w((uint32_t*)(buf + 4), 1);
529 } else {
530 TRACE("Writing to device");
531
532 if (bdrv_write(bs, (from + dev_offset) / 512, data, len / 512) == -1) {
533 LOG("writing to file failed");
534 errno = EINVAL;
535 return -1;
536 }
537
538 *offset += len;
539 }
540
541 TRACE("Sending response to client");
542
543 if (write_sync(csock, buf, 16) != 16) {
544 LOG("writing to socket failed");
545 errno = EINVAL;
546 return -1;
547 }
548 break;
549 case 2:
550 TRACE("Request type is DISCONNECT");
551 errno = 0;
552 return 1;
553 default:
554 LOG("invalid request type (%u) received", type);
555 errno = EINVAL;
556 return -1;
557 }
558
559 TRACE("Request/Reply complete");
560
561 return 0;
562 }