]> git.proxmox.com Git - qemu.git/blobdiff - qemu-io.c
LAN9118 improvements
[qemu.git] / qemu-io.c
index 029ee0b6e09d6e33e62e7981c8141cb1a360f2c6..aa26e36b85bec15ef22b3c8153254c3650d5c134 100644 (file)
--- a/qemu-io.c
+++ b/qemu-io.c
@@ -7,10 +7,12 @@
  * This work is licensed under the terms of the GNU GPL, version 2 or later.
  * See the COPYING file in the top-level directory.
  */
+#include <sys/time.h>
 #include <sys/types.h>
 #include <stdarg.h>
 #include <stdio.h>
 #include <getopt.h>
+#include <libgen.h>
 
 #include "qemu-common.h"
 #include "block_int.h"
@@ -127,7 +129,8 @@ create_iovec(QEMUIOVector *qiov, char **argv, int nr_iov, int pattern)
 {
        size_t *sizes = calloc(nr_iov, sizeof(size_t));
        size_t count = 0;
-       void *buf, *p;
+       void *buf = NULL;
+       void *p;
        int i;
 
        for (i = 0; i < nr_iov; i++) {
@@ -137,19 +140,19 @@ create_iovec(QEMUIOVector *qiov, char **argv, int nr_iov, int pattern)
                len = cvtnum(arg);
                if (len < 0) {
                        printf("non-numeric length argument -- %s\n", arg);
-                       return NULL;
+                       goto fail;
                }
 
                /* should be SIZE_T_MAX, but that doesn't exist */
                if (len > UINT_MAX) {
                        printf("too large length argument -- %s\n", arg);
-                       return NULL;
+                       goto fail;
                }
 
                if (len & 0x1ff) {
                        printf("length argument %lld is not sector aligned\n",
                                len);
-                       return NULL;
+                       goto fail;
                }
 
                sizes[i] = len;
@@ -165,6 +168,7 @@ create_iovec(QEMUIOVector *qiov, char **argv, int nr_iov, int pattern)
                p += sizes[i];
        }
 
+fail:
        free(sizes);
        return buf;
 }
@@ -264,8 +268,6 @@ static int do_aio_writev(QEMUIOVector *qiov, int64_t offset, int *total)
 }
 
 
-static const cmdinfo_t read_cmd;
-
 static void
 read_help(void)
 {
@@ -289,6 +291,19 @@ read_help(void)
 "\n");
 }
 
+static int read_f(int argc, char **argv);
+
+static const cmdinfo_t read_cmd = {
+       .name           = "read",
+       .altname        = "r",
+       .cfunc          = read_f,
+       .argmin         = 2,
+       .argmax         = -1,
+       .args           = "[-abCpqv] [-P pattern [-s off] [-l len]] off len",
+       .oneline        = "reads a number of bytes at a specified offset",
+       .help           = read_help,
+};
+
 static int
 read_f(int argc, char **argv)
 {
@@ -437,19 +452,6 @@ out:
        return 0;
 }
 
-static const cmdinfo_t read_cmd = {
-       .name           = "read",
-       .altname        = "r",
-       .cfunc          = read_f,
-       .argmin         = 2,
-       .argmax         = -1,
-       .args           = "[-abCpqv] [-P pattern [-s off] [-l len]] off len",
-       .oneline        = "reads a number of bytes at a specified offset",
-       .help           = read_help,
-};
-
-static const cmdinfo_t readv_cmd;
-
 static void
 readv_help(void)
 {
@@ -470,6 +472,18 @@ readv_help(void)
 "\n");
 }
 
+static int readv_f(int argc, char **argv);
+
+static const cmdinfo_t readv_cmd = {
+       .name           = "readv",
+       .cfunc          = readv_f,
+       .argmin         = 2,
+       .argmax         = -1,
+       .args           = "[-Cqv] [-P pattern ] off len [len..]",
+       .oneline        = "reads a number of bytes at a specified offset",
+       .help           = readv_help,
+};
+
 static int
 readv_f(int argc, char **argv)
 {
@@ -561,18 +575,6 @@ out:
        return 0;
 }
 
-static const cmdinfo_t readv_cmd = {
-       .name           = "readv",
-       .cfunc          = readv_f,
-       .argmin         = 2,
-       .argmax         = -1,
-       .args           = "[-Cqv] [-P pattern ] off len [len..]",
-       .oneline        = "reads a number of bytes at a specified offset",
-       .help           = readv_help,
-};
-
-static const cmdinfo_t write_cmd;
-
 static void
 write_help(void)
 {
@@ -593,6 +595,19 @@ write_help(void)
 "\n");
 }
 
+static int write_f(int argc, char **argv);
+
+static const cmdinfo_t write_cmd = {
+       .name           = "write",
+       .altname        = "w",
+       .cfunc          = write_f,
+       .argmin         = 2,
+       .argmax         = -1,
+       .args           = "[-abCpq] [-P pattern ] off len",
+       .oneline        = "writes a number of bytes at a specified offset",
+       .help           = write_help,
+};
+
 static int
 write_f(int argc, char **argv)
 {
@@ -694,19 +709,6 @@ out:
        return 0;
 }
 
-static const cmdinfo_t write_cmd = {
-       .name           = "write",
-       .altname        = "w",
-       .cfunc          = write_f,
-       .argmin         = 2,
-       .argmax         = -1,
-       .args           = "[-abCpq] [-P pattern ] off len",
-       .oneline        = "writes a number of bytes at a specified offset",
-       .help           = write_help,
-};
-
-static const cmdinfo_t writev_cmd;
-
 static void
 writev_help(void)
 {
@@ -725,6 +727,18 @@ writev_help(void)
 "\n");
 }
 
+static int writev_f(int argc, char **argv);
+
+static const cmdinfo_t writev_cmd = {
+       .name           = "writev",
+       .cfunc          = writev_f,
+       .argmin         = 2,
+       .argmax         = -1,
+       .args           = "[-Cq] [-P pattern ] off len [len..]",
+       .oneline        = "writes a number of bytes at a specified offset",
+       .help           = writev_help,
+};
+
 static int
 writev_f(int argc, char **argv)
 {
@@ -795,16 +809,6 @@ out:
        return 0;
 }
 
-static const cmdinfo_t writev_cmd = {
-       .name           = "writev",
-       .cfunc          = writev_f,
-       .argmin         = 2,
-       .argmax         = -1,
-       .args           = "[-Cq] [-P pattern ] off len [len..]",
-       .oneline        = "writes a number of bytes at a specified offset",
-       .help           = writev_help,
-};
-
 struct aio_ctx {
        QEMUIOVector qiov;
        int64_t offset;
@@ -844,8 +848,6 @@ out:
        free(ctx);
 }
 
-static const cmdinfo_t aio_read_cmd;
-
 static void
 aio_read_done(void *opaque, int ret)
 {
@@ -909,6 +911,18 @@ aio_read_help(void)
 "\n");
 }
 
+static int aio_read_f(int argc, char **argv);
+
+static const cmdinfo_t aio_read_cmd = {
+       .name           = "aio_read",
+       .cfunc          = aio_read_f,
+       .argmin         = 2,
+       .argmax         = -1,
+       .args           = "[-Cqv] [-P pattern ] off len [len..]",
+       .oneline        = "asynchronously reads a number of bytes",
+       .help           = aio_read_help,
+};
+
 static int
 aio_read_f(int argc, char **argv)
 {
@@ -974,18 +988,6 @@ aio_read_f(int argc, char **argv)
        return 0;
 }
 
-static const cmdinfo_t aio_read_cmd = {
-       .name           = "aio_read",
-       .cfunc          = aio_read_f,
-       .argmin         = 2,
-       .argmax         = -1,
-       .args           = "[-Cqv] [-P pattern ] off len [len..]",
-       .oneline        = "asynchronously reads a number of bytes",
-       .help           = aio_read_help,
-};
-
-static const cmdinfo_t aio_write_cmd;
-
 static void
 aio_write_help(void)
 {
@@ -1007,6 +1009,17 @@ aio_write_help(void)
 "\n");
 }
 
+static int aio_write_f(int argc, char **argv);
+
+static const cmdinfo_t aio_write_cmd = {
+       .name           = "aio_write",
+       .cfunc          = aio_write_f,
+       .argmin         = 2,
+       .argmax         = -1,
+       .args           = "[-Cq] [-P pattern ] off len [len..]",
+       .oneline        = "asynchronously writes a number of bytes",
+       .help           = aio_write_help,
+};
 
 static int
 aio_write_f(int argc, char **argv)
@@ -1070,16 +1083,6 @@ aio_write_f(int argc, char **argv)
        return 0;
 }
 
-static const cmdinfo_t aio_write_cmd = {
-       .name           = "aio_write",
-       .cfunc          = aio_write_f,
-       .argmin         = 2,
-       .argmax         = -1,
-       .args           = "[-Cq] [-P pattern ] off len [len..]",
-       .oneline        = "asynchronously writes a number of bytes",
-       .help           = aio_write_help,
-};
-
 static int
 aio_flush_f(int argc, char **argv)
 {
@@ -1310,7 +1313,19 @@ open_help(void)
 "\n");
 }
 
-static const cmdinfo_t open_cmd;
+static int open_f(int argc, char **argv);
+
+static const cmdinfo_t open_cmd = {
+       .name           = "open",
+       .altname        = "o",
+       .cfunc          = open_f,
+       .argmin         = 1,
+       .argmax         = -1,
+       .flags          = CMD_NOFILE_OK,
+       .args           = "[-Crsn] [path]",
+       .oneline        = "open the file specified by path",
+       .help           = open_help,
+};
 
 static int
 open_f(int argc, char **argv)
@@ -1353,18 +1368,6 @@ open_f(int argc, char **argv)
        return openfile(argv[optind], flags, growable);
 }
 
-static const cmdinfo_t open_cmd = {
-       .name           = "open",
-       .altname        = "o",
-       .cfunc          = open_f,
-       .argmin         = 1,
-       .argmax         = -1,
-       .flags          = CMD_NOFILE_OK,
-       .args           = "[-Crsn] [path]",
-       .oneline        = "open the file specified by path",
-       .help           = open_help,
-};
-
 static int
 init_args_command(
         int     index)
@@ -1401,6 +1404,7 @@ static void usage(const char *name)
 "  -n, --nocache        disable host cache\n"
 "  -g, --growable       allow file to grow (only applies to protocols)\n"
 "  -m, --misalign       misalign allocations for O_DIRECT\n"
+"  -k, --native-aio     use kernel AIO implementation (on Linux only)\n"
 "  -h, --help           display this help and exit\n"
 "  -V, --version        output version information and exit\n"
 "\n",
@@ -1412,19 +1416,20 @@ int main(int argc, char **argv)
 {
        int readonly = 0;
        int growable = 0;
-       const char *sopt = "hVc:Crsnmg";
-       struct option lopt[] = {
-               { "help", 0, 0, 'h' },
-               { "version", 0, 0, 'V' },
-               { "offset", 1, 0, 'o' },
-               { "cmd", 1, 0, 'c' },
-               { "create", 0, 0, 'C' },
-               { "read-only", 0, 0, 'r' },
-               { "snapshot", 0, 0, 's' },
-               { "nocache", 0, 0, 'n' },
-               { "misalign", 0, 0, 'm' },
-               { "growable", 0, 0, 'g' },
-               { NULL, 0, 0, 0 }
+       const char *sopt = "hVc:Crsnmgk";
+        const struct option lopt[] = {
+               { "help", 0, NULL, 'h' },
+               { "version", 0, NULL, 'V' },
+               { "offset", 1, NULL, 'o' },
+               { "cmd", 1, NULL, 'c' },
+               { "create", 0, NULL, 'C' },
+               { "read-only", 0, NULL, 'r' },
+               { "snapshot", 0, NULL, 's' },
+               { "nocache", 0, NULL, 'n' },
+               { "misalign", 0, NULL, 'm' },
+               { "growable", 0, NULL, 'g' },
+               { "native-aio", 0, NULL, 'k' },
+               { NULL, 0, NULL, 0 }
        };
        int c;
        int opt_index = 0;
@@ -1455,6 +1460,9 @@ int main(int argc, char **argv)
                case 'g':
                        growable = 1;
                        break;
+               case 'k':
+                       flags |= BDRV_O_NATIVE_AIO;
+                       break;
                case 'V':
                        printf("%s version %s\n", progname, VERSION);
                        exit(0);