slirp: Fix port comparision in slirp_remove_hostfwd
[qemu/ar7.git] / qemu-io.c
blob566ca79fc8381a5abe622e68aa026e5bd34ee4bc
1 /*
2 * Command line utility to exercise the QEMU I/O path.
4 * Copyright (C) 2009 Red Hat, Inc.
5 * Copyright (c) 2003-2005 Silicon Graphics, Inc.
7 * This work is licensed under the terms of the GNU GPL, version 2 or later.
8 * See the COPYING file in the top-level directory.
9 */
10 #include <sys/types.h>
11 #include <stdarg.h>
12 #include <stdio.h>
13 #include <getopt.h>
15 #include "qemu-common.h"
16 #include "block_int.h"
17 #include "cmd.h"
19 #define VERSION "0.0.1"
21 #define CMD_NOFILE_OK 0x01
23 char *progname;
24 static BlockDriverState *bs;
26 static int misalign;
29 * Memory allocation helpers.
31 * Make sure memory is aligned by default, or purposefully misaligned if
32 * that is specified on the command line.
35 #define MISALIGN_OFFSET 16
36 static void *qemu_io_alloc(size_t len, int pattern)
38 void *buf;
40 if (misalign)
41 len += MISALIGN_OFFSET;
42 buf = qemu_memalign(512, len);
43 memset(buf, pattern, len);
44 if (misalign)
45 buf += MISALIGN_OFFSET;
46 return buf;
49 static void qemu_io_free(void *p)
51 if (misalign)
52 p -= MISALIGN_OFFSET;
53 qemu_vfree(p);
56 static void
57 dump_buffer(const void *buffer, int64_t offset, int len)
59 int i, j;
60 const uint8_t *p;
62 for (i = 0, p = buffer; i < len; i += 16) {
63 const uint8_t *s = p;
65 printf("%08llx: ", (unsigned long long)offset + i);
66 for (j = 0; j < 16 && i + j < len; j++, p++)
67 printf("%02x ", *p);
68 printf(" ");
69 for (j = 0; j < 16 && i + j < len; j++, s++) {
70 if (isalnum(*s))
71 printf("%c", *s);
72 else
73 printf(".");
75 printf("\n");
79 static void
80 print_report(const char *op, struct timeval *t, int64_t offset,
81 int count, int total, int cnt, int Cflag)
83 char s1[64], s2[64], ts[64];
85 timestr(t, ts, sizeof(ts), Cflag ? VERBOSE_FIXED_TIME : 0);
86 if (!Cflag) {
87 cvtstr((double)total, s1, sizeof(s1));
88 cvtstr(tdiv((double)total, *t), s2, sizeof(s2));
89 printf("%s %d/%d bytes at offset %lld\n",
90 op, total, count, (long long)offset);
91 printf("%s, %d ops; %s (%s/sec and %.4f ops/sec)\n",
92 s1, cnt, ts, s2, tdiv((double)cnt, *t));
93 } else {/* bytes,ops,time,bytes/sec,ops/sec */
94 printf("%d,%d,%s,%.3f,%.3f\n",
95 total, cnt, ts,
96 tdiv((double)total, *t),
97 tdiv((double)cnt, *t));
101 static int do_read(char *buf, int64_t offset, int count, int *total)
103 int ret;
105 ret = bdrv_read(bs, offset >> 9, (uint8_t *)buf, count >> 9);
106 if (ret < 0)
107 return ret;
108 *total = count;
109 return 1;
112 static int do_write(char *buf, int64_t offset, int count, int *total)
114 int ret;
116 ret = bdrv_write(bs, offset >> 9, (uint8_t *)buf, count >> 9);
117 if (ret < 0)
118 return ret;
119 *total = count;
120 return 1;
123 static int do_pread(char *buf, int64_t offset, int count, int *total)
125 *total = bdrv_pread(bs, offset, (uint8_t *)buf, count);
126 if (*total < 0)
127 return *total;
128 return 1;
131 static int do_pwrite(char *buf, int64_t offset, int count, int *total)
133 *total = bdrv_pwrite(bs, offset, (uint8_t *)buf, count);
134 if (*total < 0)
135 return *total;
136 return 1;
139 #define NOT_DONE 0x7fffffff
140 static void aio_rw_done(void *opaque, int ret)
142 *(int *)opaque = ret;
145 static int do_aio_readv(QEMUIOVector *qiov, int64_t offset, int *total)
147 BlockDriverAIOCB *acb;
148 int async_ret = NOT_DONE;
150 acb = bdrv_aio_readv(bs, offset >> 9, qiov, qiov->size >> 9,
151 aio_rw_done, &async_ret);
152 if (!acb)
153 return -EIO;
155 while (async_ret == NOT_DONE)
156 qemu_aio_wait();
158 *total = qiov->size;
159 return async_ret < 0 ? async_ret : 1;
162 static int do_aio_writev(QEMUIOVector *qiov, int64_t offset, int *total)
164 BlockDriverAIOCB *acb;
165 int async_ret = NOT_DONE;
167 acb = bdrv_aio_writev(bs, offset >> 9, qiov, qiov->size >> 9,
168 aio_rw_done, &async_ret);
169 if (!acb)
170 return -EIO;
172 while (async_ret == NOT_DONE)
173 qemu_aio_wait();
175 *total = qiov->size;
176 return async_ret < 0 ? async_ret : 1;
180 static const cmdinfo_t read_cmd;
182 static void
183 read_help(void)
185 printf(
186 "\n"
187 " reads a range of bytes from the given offset\n"
188 "\n"
189 " Example:\n"
190 " 'read -v 512 1k' - dumps 1 kilobyte read from 512 bytes into the file\n"
191 "\n"
192 " Reads a segment of the currently open file, optionally dumping it to the\n"
193 " standard output stream (with -v option) for subsequent inspection.\n"
194 " -C, -- report statistics in a machine parsable format\n"
195 " -l, -- length for pattern verification (only with -P)\n"
196 " -p, -- use bdrv_pread to read the file\n"
197 " -P, -- use a pattern to verify read data\n"
198 " -q, -- quite mode, do not show I/O statistics\n"
199 " -s, -- start offset for pattern verification (only with -P)\n"
200 " -v, -- dump buffer to standard output\n"
201 "\n");
204 static int
205 read_f(int argc, char **argv)
207 struct timeval t1, t2;
208 int Cflag = 0, pflag = 0, qflag = 0, vflag = 0;
209 int Pflag = 0, sflag = 0, lflag = 0;
210 int c, cnt;
211 char *buf;
212 int64_t offset;
213 int count;
214 /* Some compilers get confused and warn if this is not initialized. */
215 int total = 0;
216 int pattern = 0, pattern_offset = 0, pattern_count = 0;
218 while ((c = getopt(argc, argv, "Cl:pP:qs:v")) != EOF) {
219 switch (c) {
220 case 'C':
221 Cflag = 1;
222 break;
223 case 'l':
224 lflag = 1;
225 pattern_count = cvtnum(optarg);
226 if (pattern_count < 0) {
227 printf("non-numeric length argument -- %s\n", optarg);
228 return 0;
230 break;
231 case 'p':
232 pflag = 1;
233 break;
234 case 'P':
235 Pflag = 1;
236 pattern = atoi(optarg);
237 break;
238 case 'q':
239 qflag = 1;
240 break;
241 case 's':
242 sflag = 1;
243 pattern_offset = cvtnum(optarg);
244 if (pattern_offset < 0) {
245 printf("non-numeric length argument -- %s\n", optarg);
246 return 0;
248 break;
249 case 'v':
250 vflag = 1;
251 break;
252 default:
253 return command_usage(&read_cmd);
257 if (optind != argc - 2)
258 return command_usage(&read_cmd);
260 offset = cvtnum(argv[optind]);
261 if (offset < 0) {
262 printf("non-numeric length argument -- %s\n", argv[optind]);
263 return 0;
266 optind++;
267 count = cvtnum(argv[optind]);
268 if (count < 0) {
269 printf("non-numeric length argument -- %s\n", argv[optind]);
270 return 0;
273 if (!Pflag && (lflag || sflag)) {
274 return command_usage(&read_cmd);
277 if (!lflag) {
278 pattern_count = count - pattern_offset;
281 if ((pattern_count < 0) || (pattern_count + pattern_offset > count)) {
282 printf("pattern verfication range exceeds end of read data\n");
283 return 0;
286 if (!pflag)
287 if (offset & 0x1ff) {
288 printf("offset %lld is not sector aligned\n",
289 (long long)offset);
290 return 0;
292 if (count & 0x1ff) {
293 printf("count %d is not sector aligned\n",
294 count);
295 return 0;
299 buf = qemu_io_alloc(count, 0xab);
301 gettimeofday(&t1, NULL);
302 if (pflag)
303 cnt = do_pread(buf, offset, count, &total);
304 else
305 cnt = do_read(buf, offset, count, &total);
306 gettimeofday(&t2, NULL);
308 if (cnt < 0) {
309 printf("read failed: %s\n", strerror(-cnt));
310 return 0;
313 if (Pflag) {
314 void* cmp_buf = malloc(pattern_count);
315 memset(cmp_buf, pattern, pattern_count);
316 if (memcmp(buf + pattern_offset, cmp_buf, pattern_count)) {
317 printf("Pattern verification failed at offset %lld, "
318 "%d bytes\n",
319 (long long) offset + pattern_offset, pattern_count);
321 free(cmp_buf);
324 if (qflag)
325 return 0;
327 if (vflag)
328 dump_buffer(buf, offset, count);
330 /* Finally, report back -- -C gives a parsable format */
331 t2 = tsub(t2, t1);
332 print_report("read", &t2, offset, count, total, cnt, Cflag);
334 qemu_io_free(buf);
336 return 0;
339 static const cmdinfo_t read_cmd = {
340 .name = "read",
341 .altname = "r",
342 .cfunc = read_f,
343 .argmin = 2,
344 .argmax = -1,
345 .args = "[-aCpqv] [-P pattern [-s off] [-l len]] off len",
346 .oneline = "reads a number of bytes at a specified offset",
347 .help = read_help,
350 static const cmdinfo_t readv_cmd;
352 static void
353 readv_help(void)
355 printf(
356 "\n"
357 " reads a range of bytes from the given offset into multiple buffers\n"
358 "\n"
359 " Example:\n"
360 " 'readv -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n"
361 "\n"
362 " Reads a segment of the currently open file, optionally dumping it to the\n"
363 " standard output stream (with -v option) for subsequent inspection.\n"
364 " Uses multiple iovec buffers if more than one byte range is specified.\n"
365 " -C, -- report statistics in a machine parsable format\n"
366 " -P, -- use a pattern to verify read data\n"
367 " -v, -- dump buffer to standard output\n"
368 " -q, -- quite mode, do not show I/O statistics\n"
369 "\n");
372 static int
373 readv_f(int argc, char **argv)
375 struct timeval t1, t2;
376 int Cflag = 0, qflag = 0, vflag = 0;
377 int c, cnt;
378 char *buf, *p;
379 int64_t offset;
380 int count = 0, total;
381 int nr_iov, i;
382 QEMUIOVector qiov;
383 int pattern = 0;
384 int Pflag = 0;
386 while ((c = getopt(argc, argv, "CP:qv")) != EOF) {
387 switch (c) {
388 case 'C':
389 Cflag = 1;
390 break;
391 case 'P':
392 Pflag = 1;
393 pattern = atoi(optarg);
394 break;
395 case 'q':
396 qflag = 1;
397 break;
398 case 'v':
399 vflag = 1;
400 break;
401 default:
402 return command_usage(&readv_cmd);
406 if (optind > argc - 2)
407 return command_usage(&readv_cmd);
410 offset = cvtnum(argv[optind]);
411 if (offset < 0) {
412 printf("non-numeric length argument -- %s\n", argv[optind]);
413 return 0;
415 optind++;
417 if (offset & 0x1ff) {
418 printf("offset %lld is not sector aligned\n",
419 (long long)offset);
420 return 0;
423 if (count & 0x1ff) {
424 printf("count %d is not sector aligned\n",
425 count);
426 return 0;
429 for (i = optind; i < argc; i++) {
430 size_t len;
432 len = cvtnum(argv[i]);
433 if (len < 0) {
434 printf("non-numeric length argument -- %s\n", argv[i]);
435 return 0;
437 count += len;
440 nr_iov = argc - optind;
441 qemu_iovec_init(&qiov, nr_iov);
442 buf = p = qemu_io_alloc(count, 0xab);
443 for (i = 0; i < nr_iov; i++) {
444 size_t len;
446 len = cvtnum(argv[optind]);
447 if (len < 0) {
448 printf("non-numeric length argument -- %s\n",
449 argv[optind]);
450 return 0;
453 qemu_iovec_add(&qiov, p, len);
454 p += len;
455 optind++;
458 gettimeofday(&t1, NULL);
459 cnt = do_aio_readv(&qiov, offset, &total);
460 gettimeofday(&t2, NULL);
462 if (cnt < 0) {
463 printf("readv failed: %s\n", strerror(-cnt));
464 return 0;
467 if (Pflag) {
468 void* cmp_buf = malloc(count);
469 memset(cmp_buf, pattern, count);
470 if (memcmp(buf, cmp_buf, count)) {
471 printf("Pattern verification failed at offset %lld, "
472 "%d bytes\n",
473 (long long) offset, count);
475 free(cmp_buf);
478 if (qflag)
479 return 0;
481 if (vflag)
482 dump_buffer(buf, offset, qiov.size);
484 /* Finally, report back -- -C gives a parsable format */
485 t2 = tsub(t2, t1);
486 print_report("read", &t2, offset, qiov.size, total, cnt, Cflag);
488 qemu_io_free(buf);
490 return 0;
493 static const cmdinfo_t readv_cmd = {
494 .name = "readv",
495 .cfunc = readv_f,
496 .argmin = 2,
497 .argmax = -1,
498 .args = "[-Cqv] [-P pattern ] off len [len..]",
499 .oneline = "reads a number of bytes at a specified offset",
500 .help = readv_help,
503 static const cmdinfo_t write_cmd;
505 static void
506 write_help(void)
508 printf(
509 "\n"
510 " writes a range of bytes from the given offset\n"
511 "\n"
512 " Example:\n"
513 " 'write 512 1k' - writes 1 kilobyte at 512 bytes into the open file\n"
514 "\n"
515 " Writes into a segment of the currently open file, using a buffer\n"
516 " filled with a set pattern (0xcdcdcdcd).\n"
517 " -p, -- use bdrv_pwrite to write the file\n"
518 " -P, -- use different pattern to fill file\n"
519 " -C, -- report statistics in a machine parsable format\n"
520 " -q, -- quite mode, do not show I/O statistics\n"
521 "\n");
524 static int
525 write_f(int argc, char **argv)
527 struct timeval t1, t2;
528 int Cflag = 0, pflag = 0, qflag = 0;
529 int c, cnt;
530 char *buf;
531 int64_t offset;
532 int count;
533 /* Some compilers get confused and warn if this is not initialized. */
534 int total = 0;
535 int pattern = 0xcd;
537 while ((c = getopt(argc, argv, "CpP:q")) != EOF) {
538 switch (c) {
539 case 'C':
540 Cflag = 1;
541 break;
542 case 'p':
543 pflag = 1;
544 break;
545 case 'P':
546 pattern = atoi(optarg);
547 break;
548 case 'q':
549 qflag = 1;
550 break;
551 default:
552 return command_usage(&write_cmd);
556 if (optind != argc - 2)
557 return command_usage(&write_cmd);
559 offset = cvtnum(argv[optind]);
560 if (offset < 0) {
561 printf("non-numeric length argument -- %s\n", argv[optind]);
562 return 0;
565 optind++;
566 count = cvtnum(argv[optind]);
567 if (count < 0) {
568 printf("non-numeric length argument -- %s\n", argv[optind]);
569 return 0;
572 if (!pflag) {
573 if (offset & 0x1ff) {
574 printf("offset %lld is not sector aligned\n",
575 (long long)offset);
576 return 0;
579 if (count & 0x1ff) {
580 printf("count %d is not sector aligned\n",
581 count);
582 return 0;
586 buf = qemu_io_alloc(count, pattern);
588 gettimeofday(&t1, NULL);
589 if (pflag)
590 cnt = do_pwrite(buf, offset, count, &total);
591 else
592 cnt = do_write(buf, offset, count, &total);
593 gettimeofday(&t2, NULL);
595 if (cnt < 0) {
596 printf("write failed: %s\n", strerror(-cnt));
597 return 0;
600 if (qflag)
601 return 0;
603 /* Finally, report back -- -C gives a parsable format */
604 t2 = tsub(t2, t1);
605 print_report("wrote", &t2, offset, count, total, cnt, Cflag);
607 qemu_io_free(buf);
609 return 0;
612 static const cmdinfo_t write_cmd = {
613 .name = "write",
614 .altname = "w",
615 .cfunc = write_f,
616 .argmin = 2,
617 .argmax = -1,
618 .args = "[-aCpq] [-P pattern ] off len",
619 .oneline = "writes a number of bytes at a specified offset",
620 .help = write_help,
623 static const cmdinfo_t writev_cmd;
625 static void
626 writev_help(void)
628 printf(
629 "\n"
630 " writes a range of bytes from the given offset source from multiple buffers\n"
631 "\n"
632 " Example:\n"
633 " 'write 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n"
634 "\n"
635 " Writes into a segment of the currently open file, using a buffer\n"
636 " filled with a set pattern (0xcdcdcdcd).\n"
637 " -P, -- use different pattern to fill file\n"
638 " -C, -- report statistics in a machine parsable format\n"
639 " -q, -- quite mode, do not show I/O statistics\n"
640 "\n");
643 static int
644 writev_f(int argc, char **argv)
646 struct timeval t1, t2;
647 int Cflag = 0, qflag = 0;
648 int c, cnt;
649 char *buf, *p;
650 int64_t offset;
651 int count = 0, total;
652 int nr_iov, i;
653 int pattern = 0xcd;
654 QEMUIOVector qiov;
656 while ((c = getopt(argc, argv, "CqP:")) != EOF) {
657 switch (c) {
658 case 'C':
659 Cflag = 1;
660 break;
661 case 'q':
662 qflag = 1;
663 break;
664 case 'P':
665 pattern = atoi(optarg);
666 break;
667 default:
668 return command_usage(&writev_cmd);
672 if (optind > argc - 2)
673 return command_usage(&writev_cmd);
675 offset = cvtnum(argv[optind]);
676 if (offset < 0) {
677 printf("non-numeric length argument -- %s\n", argv[optind]);
678 return 0;
680 optind++;
682 if (offset & 0x1ff) {
683 printf("offset %lld is not sector aligned\n",
684 (long long)offset);
685 return 0;
688 if (count & 0x1ff) {
689 printf("count %d is not sector aligned\n",
690 count);
691 return 0;
695 for (i = optind; i < argc; i++) {
696 size_t len;
698 len = cvtnum(argv[optind]);
699 if (len < 0) {
700 printf("non-numeric length argument -- %s\n", argv[i]);
701 return 0;
703 count += len;
706 nr_iov = argc - optind;
707 qemu_iovec_init(&qiov, nr_iov);
708 buf = p = qemu_io_alloc(count, pattern);
709 for (i = 0; i < nr_iov; i++) {
710 size_t len;
712 len = cvtnum(argv[optind]);
713 if (len < 0) {
714 printf("non-numeric length argument -- %s\n",
715 argv[optind]);
716 return 0;
719 qemu_iovec_add(&qiov, p, len);
720 p += len;
721 optind++;
724 gettimeofday(&t1, NULL);
725 cnt = do_aio_writev(&qiov, offset, &total);
726 gettimeofday(&t2, NULL);
728 if (cnt < 0) {
729 printf("writev failed: %s\n", strerror(-cnt));
730 return 0;
733 if (qflag)
734 return 0;
736 /* Finally, report back -- -C gives a parsable format */
737 t2 = tsub(t2, t1);
738 print_report("wrote", &t2, offset, qiov.size, total, cnt, Cflag);
740 qemu_io_free(buf);
742 return 0;
745 static const cmdinfo_t writev_cmd = {
746 .name = "writev",
747 .cfunc = writev_f,
748 .argmin = 2,
749 .argmax = -1,
750 .args = "[-Cq] [-P pattern ] off len [len..]",
751 .oneline = "writes a number of bytes at a specified offset",
752 .help = writev_help,
755 struct aio_ctx {
756 QEMUIOVector qiov;
757 int64_t offset;
758 char *buf;
759 int qflag;
760 int vflag;
761 int Cflag;
762 int Pflag;
763 int pattern;
764 struct timeval t1;
767 static void
768 aio_write_done(void *opaque, int ret)
770 struct aio_ctx *ctx = opaque;
771 struct timeval t2;
772 int total;
773 int cnt = 1;
775 gettimeofday(&t2, NULL);
777 total = ctx->qiov.size;
779 if (ret < 0) {
780 printf("aio_write failed: %s\n", strerror(-ret));
781 return;
784 if (ctx->qflag)
785 return;
787 /* Finally, report back -- -C gives a parsable format */
788 t2 = tsub(t2, ctx->t1);
789 print_report("wrote", &t2, ctx->offset, ctx->qiov.size, total, cnt,
790 ctx->Cflag);
792 qemu_io_free(ctx->buf);
793 free(ctx);
796 static const cmdinfo_t aio_read_cmd;
798 static void
799 aio_read_done(void *opaque, int ret)
801 struct aio_ctx *ctx = opaque;
802 struct timeval t2;
803 int total;
804 int cnt = 1;
806 gettimeofday(&t2, NULL);
808 total = ctx->qiov.size;
810 if (ret < 0) {
811 printf("readv failed: %s\n", strerror(-ret));
812 return;
815 if (ctx->Pflag) {
816 void *cmp_buf = malloc(total);
818 memset(cmp_buf, ctx->pattern, total);
819 if (memcmp(ctx->buf, cmp_buf, total)) {
820 printf("Pattern verification failed at offset %lld, "
821 "%d bytes\n",
822 (long long) ctx->offset, total);
824 free(cmp_buf);
827 if (ctx->qflag)
828 return;
830 if (ctx->vflag)
831 dump_buffer(ctx->buf, ctx->offset, total);
833 /* Finally, report back -- -C gives a parsable format */
834 t2 = tsub(t2, ctx->t1);
835 print_report("read", &t2, ctx->offset, ctx->qiov.size, total, cnt,
836 ctx->Cflag);
838 qemu_io_free(ctx->buf);
839 free(ctx);
843 static void
844 aio_read_help(void)
846 printf(
847 "\n"
848 " asynchronously reads a range of bytes from the given offset\n"
849 "\n"
850 " Example:\n"
851 " 'aio_read -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n"
852 "\n"
853 " Reads a segment of the currently open file, optionally dumping it to the\n"
854 " standard output stream (with -v option) for subsequent inspection.\n"
855 " The read is performed asynchronously and should the aio_flush command \n"
856 " should be used to ensure all outstanding aio requests have been completed\n"
857 " -C, -- report statistics in a machine parsable format\n"
858 " -P, -- use a pattern to verify read data\n"
859 " -v, -- dump buffer to standard output\n"
860 " -q, -- quite mode, do not show I/O statistics\n"
861 "\n");
864 static int
865 aio_read_f(int argc, char **argv)
867 char *p;
868 int count = 0;
869 int nr_iov, i, c;
870 struct aio_ctx *ctx = calloc(1, sizeof(struct aio_ctx));
871 BlockDriverAIOCB *acb;
873 ctx->pattern = 0xcd;
875 while ((c = getopt(argc, argv, "CP:qv")) != EOF) {
876 switch (c) {
877 case 'C':
878 ctx->Cflag = 1;
879 break;
880 case 'P':
881 ctx->Pflag = 1;
882 ctx->pattern = atoi(optarg);
883 break;
884 case 'q':
885 ctx->qflag = 1;
886 break;
887 case 'v':
888 ctx->vflag = 1;
889 break;
890 default:
891 return command_usage(&aio_read_cmd);
895 if (optind > argc - 2)
896 return command_usage(&aio_read_cmd);
899 ctx->offset = cvtnum(argv[optind]);
900 if (ctx->offset < 0) {
901 printf("non-numeric length argument -- %s\n", argv[optind]);
902 return 0;
904 optind++;
906 if (ctx->offset & 0x1ff) {
907 printf("offset %lld is not sector aligned\n",
908 (long long)ctx->offset);
909 return 0;
912 if (count & 0x1ff) {
913 printf("count %d is not sector aligned\n",
914 count);
915 return 0;
918 for (i = optind; i < argc; i++) {
919 size_t len;
921 len = cvtnum(argv[i]);
922 if (len < 0) {
923 printf("non-numeric length argument -- %s\n", argv[i]);
924 return 0;
926 count += len;
929 nr_iov = argc - optind;
930 qemu_iovec_init(&ctx->qiov, nr_iov);
931 ctx->buf = p = qemu_io_alloc(count, 0xab);
932 for (i = 0; i < nr_iov; i++) {
933 size_t len;
935 len = cvtnum(argv[optind]);
936 if (len < 0) {
937 printf("non-numeric length argument -- %s\n",
938 argv[optind]);
939 return 0;
942 qemu_iovec_add(&ctx->qiov, p, len);
943 p += len;
944 optind++;
947 gettimeofday(&ctx->t1, NULL);
948 acb = bdrv_aio_readv(bs, ctx->offset >> 9, &ctx->qiov,
949 ctx->qiov.size >> 9, aio_read_done, ctx);
950 if (!acb)
951 return -EIO;
953 return 0;
956 static const cmdinfo_t aio_read_cmd = {
957 .name = "aio_read",
958 .cfunc = aio_read_f,
959 .argmin = 2,
960 .argmax = -1,
961 .args = "[-Cqv] [-P pattern ] off len [len..]",
962 .oneline = "asynchronously reads a number of bytes",
963 .help = aio_read_help,
966 static const cmdinfo_t aio_write_cmd;
968 static void
969 aio_write_help(void)
971 printf(
972 "\n"
973 " asynchronously writes a range of bytes from the given offset source \n"
974 " from multiple buffers\n"
975 "\n"
976 " Example:\n"
977 " 'aio_write 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n"
978 "\n"
979 " Writes into a segment of the currently open file, using a buffer\n"
980 " filled with a set pattern (0xcdcdcdcd).\n"
981 " The write is performed asynchronously and should the aio_flush command \n"
982 " should be used to ensure all outstanding aio requests have been completed\n"
983 " -P, -- use different pattern to fill file\n"
984 " -C, -- report statistics in a machine parsable format\n"
985 " -q, -- quite mode, do not show I/O statistics\n"
986 "\n");
990 static int
991 aio_write_f(int argc, char **argv)
993 char *p;
994 int count = 0;
995 int nr_iov, i, c;
996 int pattern = 0xcd;
997 struct aio_ctx *ctx = calloc(1, sizeof(struct aio_ctx));
998 BlockDriverAIOCB *acb;
1000 while ((c = getopt(argc, argv, "CqP:")) != EOF) {
1001 switch (c) {
1002 case 'C':
1003 ctx->Cflag = 1;
1004 break;
1005 case 'q':
1006 ctx->qflag = 1;
1007 break;
1008 case 'P':
1009 pattern = atoi(optarg);
1010 break;
1011 default:
1012 return command_usage(&aio_write_cmd);
1016 if (optind > argc - 2)
1017 return command_usage(&aio_write_cmd);
1019 ctx->offset = cvtnum(argv[optind]);
1020 if (ctx->offset < 0) {
1021 printf("non-numeric length argument -- %s\n", argv[optind]);
1022 return 0;
1024 optind++;
1026 if (ctx->offset & 0x1ff) {
1027 printf("offset %lld is not sector aligned\n",
1028 (long long)ctx->offset);
1029 return 0;
1032 if (count & 0x1ff) {
1033 printf("count %d is not sector aligned\n",
1034 count);
1035 return 0;
1039 for (i = optind; i < argc; i++) {
1040 size_t len;
1042 len = cvtnum(argv[optind]);
1043 if (len < 0) {
1044 printf("non-numeric length argument -- %s\n", argv[i]);
1045 return 0;
1047 count += len;
1050 nr_iov = argc - optind;
1051 qemu_iovec_init(&ctx->qiov, nr_iov);
1052 ctx->buf = p = qemu_io_alloc(count, pattern);
1053 for (i = 0; i < nr_iov; i++) {
1054 size_t len;
1056 len = cvtnum(argv[optind]);
1057 if (len < 0) {
1058 printf("non-numeric length argument -- %s\n",
1059 argv[optind]);
1060 return 0;
1063 qemu_iovec_add(&ctx->qiov, p, len);
1064 p += len;
1065 optind++;
1068 gettimeofday(&ctx->t1, NULL);
1069 acb = bdrv_aio_writev(bs, ctx->offset >> 9, &ctx->qiov,
1070 ctx->qiov.size >> 9, aio_write_done, ctx);
1071 if (!acb)
1072 return -EIO;
1074 return 0;
1077 static const cmdinfo_t aio_write_cmd = {
1078 .name = "aio_write",
1079 .cfunc = aio_write_f,
1080 .argmin = 2,
1081 .argmax = -1,
1082 .args = "[-Cq] [-P pattern ] off len [len..]",
1083 .oneline = "asynchronously writes a number of bytes",
1084 .help = aio_write_help,
1087 static int
1088 aio_flush_f(int argc, char **argv)
1090 qemu_aio_flush();
1091 return 0;
1094 static const cmdinfo_t aio_flush_cmd = {
1095 .name = "aio_flush",
1096 .cfunc = aio_flush_f,
1097 .oneline = "completes all outstanding aio requets"
1100 static int
1101 flush_f(int argc, char **argv)
1103 bdrv_flush(bs);
1104 return 0;
1107 static const cmdinfo_t flush_cmd = {
1108 .name = "flush",
1109 .altname = "f",
1110 .cfunc = flush_f,
1111 .oneline = "flush all in-core file state to disk",
1114 static int
1115 truncate_f(int argc, char **argv)
1117 int64_t offset;
1118 int ret;
1120 offset = cvtnum(argv[1]);
1121 if (offset < 0) {
1122 printf("non-numeric truncate argument -- %s\n", argv[1]);
1123 return 0;
1126 ret = bdrv_truncate(bs, offset);
1127 if (ret < 0) {
1128 printf("truncate: %s", strerror(ret));
1129 return 0;
1132 return 0;
1135 static const cmdinfo_t truncate_cmd = {
1136 .name = "truncate",
1137 .altname = "t",
1138 .cfunc = truncate_f,
1139 .argmin = 1,
1140 .argmax = 1,
1141 .args = "off",
1142 .oneline = "truncates the current file at the given offset",
1145 static int
1146 length_f(int argc, char **argv)
1148 int64_t size;
1149 char s1[64];
1151 size = bdrv_getlength(bs);
1152 if (size < 0) {
1153 printf("getlength: %s", strerror(size));
1154 return 0;
1157 cvtstr(size, s1, sizeof(s1));
1158 printf("%s\n", s1);
1159 return 0;
1163 static const cmdinfo_t length_cmd = {
1164 .name = "length",
1165 .altname = "l",
1166 .cfunc = length_f,
1167 .oneline = "gets the length of the current file",
1171 static int
1172 info_f(int argc, char **argv)
1174 BlockDriverInfo bdi;
1175 char s1[64], s2[64];
1176 int ret;
1178 if (bs->drv && bs->drv->format_name)
1179 printf("format name: %s\n", bs->drv->format_name);
1180 if (bs->drv && bs->drv->protocol_name)
1181 printf("format name: %s\n", bs->drv->protocol_name);
1183 ret = bdrv_get_info(bs, &bdi);
1184 if (ret)
1185 return 0;
1187 cvtstr(bdi.cluster_size, s1, sizeof(s1));
1188 cvtstr(bdi.vm_state_offset, s2, sizeof(s2));
1190 printf("cluster size: %s\n", s1);
1191 printf("vm state offset: %s\n", s2);
1193 return 0;
1198 static const cmdinfo_t info_cmd = {
1199 .name = "info",
1200 .altname = "i",
1201 .cfunc = info_f,
1202 .oneline = "prints information about the current file",
1205 static int
1206 alloc_f(int argc, char **argv)
1208 int64_t offset;
1209 int nb_sectors;
1210 char s1[64];
1211 int num;
1212 int ret;
1213 const char *retstr;
1215 offset = cvtnum(argv[1]);
1216 if (offset & 0x1ff) {
1217 printf("offset %lld is not sector aligned\n",
1218 (long long)offset);
1219 return 0;
1222 if (argc == 3)
1223 nb_sectors = cvtnum(argv[2]);
1224 else
1225 nb_sectors = 1;
1227 ret = bdrv_is_allocated(bs, offset >> 9, nb_sectors, &num);
1229 cvtstr(offset, s1, sizeof(s1));
1231 retstr = ret ? "allocated" : "not allocated";
1232 if (nb_sectors == 1)
1233 printf("sector %s at offset %s\n", retstr, s1);
1234 else
1235 printf("%d/%d sectors %s at offset %s\n",
1236 num, nb_sectors, retstr, s1);
1237 return 0;
1240 static const cmdinfo_t alloc_cmd = {
1241 .name = "alloc",
1242 .altname = "a",
1243 .argmin = 1,
1244 .argmax = 2,
1245 .cfunc = alloc_f,
1246 .args = "off [sectors]",
1247 .oneline = "checks if a sector is present in the file",
1250 static int
1251 close_f(int argc, char **argv)
1253 bdrv_close(bs);
1254 bs = NULL;
1255 return 0;
1258 static const cmdinfo_t close_cmd = {
1259 .name = "close",
1260 .altname = "c",
1261 .cfunc = close_f,
1262 .oneline = "close the current open file",
1265 static int openfile(char *name, int flags)
1267 if (bs) {
1268 fprintf(stderr, "file open already, try 'help close'\n");
1269 return 1;
1272 bs = bdrv_new("hda");
1273 if (!bs)
1274 return 1;
1276 if (bdrv_open(bs, name, flags) == -1) {
1277 fprintf(stderr, "%s: can't open device %s\n", progname, name);
1278 bs = NULL;
1279 return 1;
1282 return 0;
1285 static void
1286 open_help(void)
1288 printf(
1289 "\n"
1290 " opens a new file in the requested mode\n"
1291 "\n"
1292 " Example:\n"
1293 " 'open -Cn /tmp/data' - creates/opens data file read-write and uncached\n"
1294 "\n"
1295 " Opens a file for subsequent use by all of the other qemu-io commands.\n"
1296 " -C, -- create new file if it doesn't exist\n"
1297 " -r, -- open file read-only\n"
1298 " -s, -- use snapshot file\n"
1299 " -n, -- disable host cache\n"
1300 "\n");
1303 static const cmdinfo_t open_cmd;
1305 static int
1306 open_f(int argc, char **argv)
1308 int flags = 0;
1309 int readonly = 0;
1310 int c;
1312 while ((c = getopt(argc, argv, "snCr")) != EOF) {
1313 switch (c) {
1314 case 's':
1315 flags |= BDRV_O_SNAPSHOT;
1316 break;
1317 case 'n':
1318 flags |= BDRV_O_NOCACHE;
1319 break;
1320 case 'C':
1321 flags |= BDRV_O_CREAT;
1322 break;
1323 case 'r':
1324 readonly = 1;
1325 break;
1326 default:
1327 return command_usage(&open_cmd);
1331 if (readonly)
1332 flags |= BDRV_O_RDONLY;
1333 else
1334 flags |= BDRV_O_RDWR;
1336 if (optind != argc - 1)
1337 return command_usage(&open_cmd);
1339 return openfile(argv[optind], flags);
1342 static const cmdinfo_t open_cmd = {
1343 .name = "open",
1344 .altname = "o",
1345 .cfunc = open_f,
1346 .argmin = 1,
1347 .argmax = -1,
1348 .flags = CMD_NOFILE_OK,
1349 .args = "[-Crsn] [path]",
1350 .oneline = "open the file specified by path",
1351 .help = open_help,
1354 static int
1355 init_args_command(
1356 int index)
1358 /* only one device allowed so far */
1359 if (index >= 1)
1360 return 0;
1361 return ++index;
1364 static int
1365 init_check_command(
1366 const cmdinfo_t *ct)
1368 if (ct->flags & CMD_FLAG_GLOBAL)
1369 return 1;
1370 if (!(ct->flags & CMD_NOFILE_OK) && !bs) {
1371 fprintf(stderr, "no file open, try 'help open'\n");
1372 return 0;
1374 return 1;
1377 static void usage(const char *name)
1379 printf(
1380 "Usage: %s [-h] [-V] [-Crsnm] [-c cmd] ... [file]\n"
1381 "QEMU Disk exerciser\n"
1382 "\n"
1383 " -C, --create create new file if it doesn't exist\n"
1384 " -c, --cmd command to execute\n"
1385 " -r, --read-only export read-only\n"
1386 " -s, --snapshot use snapshot file\n"
1387 " -n, --nocache disable host cache\n"
1388 " -m, --misalign misalign allocations for O_DIRECT\n"
1389 " -h, --help display this help and exit\n"
1390 " -V, --version output version information and exit\n"
1391 "\n",
1392 name);
1396 int main(int argc, char **argv)
1398 int readonly = 0;
1399 const char *sopt = "hVc:Crsnm";
1400 struct option lopt[] = {
1401 { "help", 0, 0, 'h' },
1402 { "version", 0, 0, 'V' },
1403 { "offset", 1, 0, 'o' },
1404 { "cmd", 1, 0, 'c' },
1405 { "create", 0, 0, 'C' },
1406 { "read-only", 0, 0, 'r' },
1407 { "snapshot", 0, 0, 's' },
1408 { "nocache", 0, 0, 'n' },
1409 { "misalign", 0, 0, 'm' },
1410 { NULL, 0, 0, 0 }
1412 int c;
1413 int opt_index = 0;
1414 int flags = 0;
1416 progname = basename(argv[0]);
1418 while ((c = getopt_long(argc, argv, sopt, lopt, &opt_index)) != -1) {
1419 switch (c) {
1420 case 's':
1421 flags |= BDRV_O_SNAPSHOT;
1422 break;
1423 case 'n':
1424 flags |= BDRV_O_NOCACHE;
1425 break;
1426 case 'c':
1427 add_user_command(optarg);
1428 break;
1429 case 'C':
1430 flags |= BDRV_O_CREAT;
1431 break;
1432 case 'r':
1433 readonly = 1;
1434 break;
1435 case 'm':
1436 misalign = 1;
1437 break;
1438 case 'V':
1439 printf("%s version %s\n", progname, VERSION);
1440 exit(0);
1441 case 'h':
1442 usage(progname);
1443 exit(0);
1444 default:
1445 usage(progname);
1446 exit(1);
1450 if ((argc - optind) > 1) {
1451 usage(progname);
1452 exit(1);
1455 bdrv_init();
1457 /* initialize commands */
1458 quit_init();
1459 help_init();
1460 add_command(&open_cmd);
1461 add_command(&close_cmd);
1462 add_command(&read_cmd);
1463 add_command(&readv_cmd);
1464 add_command(&write_cmd);
1465 add_command(&writev_cmd);
1466 add_command(&aio_read_cmd);
1467 add_command(&aio_write_cmd);
1468 add_command(&aio_flush_cmd);
1469 add_command(&flush_cmd);
1470 add_command(&truncate_cmd);
1471 add_command(&length_cmd);
1472 add_command(&info_cmd);
1473 add_command(&alloc_cmd);
1475 add_args_command(init_args_command);
1476 add_check_command(init_check_command);
1478 /* open the device */
1479 if (readonly)
1480 flags |= BDRV_O_RDONLY;
1481 else
1482 flags |= BDRV_O_RDWR;
1484 if ((argc - optind) == 1)
1485 openfile(argv[optind], flags);
1486 command_loop();
1489 * Make sure all outstanding requests get flushed the program exits.
1491 qemu_aio_flush();
1493 if (bs)
1494 bdrv_close(bs);
1495 return 0;