]> git.proxmox.com Git - mirror_qemu.git/blame - qemu-io.c
spapr: Move RMA memory region registration code
[mirror_qemu.git] / qemu-io.c
CommitLineData
e3aff4f6
AL
1/*
2 * Command line utility to exercise the QEMU I/O path.
3 *
4 * Copyright (C) 2009 Red Hat, Inc.
5 * Copyright (c) 2003-2005 Silicon Graphics, Inc.
6 *
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 */
c32d766a 10#include <sys/time.h>
e3aff4f6
AL
11#include <sys/types.h>
12#include <stdarg.h>
13#include <stdio.h>
14#include <getopt.h>
c32d766a 15#include <libgen.h>
e3aff4f6 16
3d21994f 17#include "qemu-io.h"
1de7afc9 18#include "qemu/main-loop.h"
b543c5cd
HR
19#include "qemu/option.h"
20#include "qemu/config-file.h"
0cf17e18 21#include "qemu/readline.h"
737e150e 22#include "block/block_int.h"
d7bb72c8 23#include "trace/control.h"
e3aff4f6 24
43642b38 25#define CMD_NOFILE_OK 0x01
e3aff4f6 26
f9883880 27static char *progname;
e3aff4f6 28
f9883880 29static BlockDriverState *qemuio_bs;
191c2890 30
d1174f13
KW
31/* qemu-io commands passed using -c */
32static int ncmdline;
33static char **cmdline;
34
0cf17e18
SH
35static ReadLineState *readline_state;
36
734c3b85 37static int close_f(BlockDriverState *bs, int argc, char **argv)
e3aff4f6 38{
4f6fd349 39 bdrv_unref(bs);
734c3b85 40 qemuio_bs = NULL;
43642b38 41 return 0;
e3aff4f6
AL
42}
43
44static const cmdinfo_t close_cmd = {
43642b38
DN
45 .name = "close",
46 .altname = "c",
47 .cfunc = close_f,
48 .oneline = "close the current open file",
e3aff4f6
AL
49};
50
b543c5cd 51static int openfile(char *name, int flags, int growable, QDict *opts)
e3aff4f6 52{
34b5d2c6
HR
53 Error *local_err = NULL;
54
734c3b85 55 if (qemuio_bs) {
43642b38 56 fprintf(stderr, "file open already, try 'help close'\n");
29f2601a 57 QDECREF(opts);
43642b38
DN
58 return 1;
59 }
60
61 if (growable) {
2e40134b
HR
62 if (bdrv_open(&qemuio_bs, name, NULL, opts, flags | BDRV_O_PROTOCOL,
63 NULL, &local_err))
64 {
543f7bef
MA
65 fprintf(stderr, "%s: can't open%s%s: %s\n", progname,
66 name ? " device " : "", name ?: "",
34b5d2c6
HR
67 error_get_pretty(local_err));
68 error_free(local_err);
43642b38
DN
69 return 1;
70 }
71 } else {
98522f63 72 qemuio_bs = bdrv_new("hda", &error_abort);
43642b38 73
ddf5636d
HR
74 if (bdrv_open(&qemuio_bs, name, NULL, opts, flags, NULL, &local_err)
75 < 0)
76 {
543f7bef
MA
77 fprintf(stderr, "%s: can't open%s%s: %s\n", progname,
78 name ? " device " : "", name ?: "",
34b5d2c6
HR
79 error_get_pretty(local_err));
80 error_free(local_err);
4f6fd349 81 bdrv_unref(qemuio_bs);
734c3b85 82 qemuio_bs = NULL;
43642b38
DN
83 return 1;
84 }
85 }
86
87 return 0;
e3aff4f6
AL
88}
89
43642b38 90static void open_help(void)
e3aff4f6 91{
43642b38 92 printf(
e3aff4f6
AL
93"\n"
94" opens a new file in the requested mode\n"
95"\n"
96" Example:\n"
97" 'open -Cn /tmp/data' - creates/opens data file read-write and uncached\n"
98"\n"
99" Opens a file for subsequent use by all of the other qemu-io commands.\n"
e3aff4f6
AL
100" -r, -- open file read-only\n"
101" -s, -- use snapshot file\n"
102" -n, -- disable host cache\n"
b543c5cd
HR
103" -g, -- allow file to grow (only applies to protocols)\n"
104" -o, -- options to be given to the block driver"
e3aff4f6
AL
105"\n");
106}
107
734c3b85 108static int open_f(BlockDriverState *bs, int argc, char **argv);
22a2bdcb
BS
109
110static const cmdinfo_t open_cmd = {
43642b38
DN
111 .name = "open",
112 .altname = "o",
113 .cfunc = open_f,
114 .argmin = 1,
115 .argmax = -1,
116 .flags = CMD_NOFILE_OK,
b543c5cd 117 .args = "[-Crsn] [-o options] [path]",
43642b38
DN
118 .oneline = "open the file specified by path",
119 .help = open_help,
22a2bdcb 120};
e3aff4f6 121
b543c5cd
HR
122static QemuOptsList empty_opts = {
123 .name = "drive",
443422fd 124 .merge_lists = true,
b543c5cd
HR
125 .head = QTAILQ_HEAD_INITIALIZER(empty_opts.head),
126 .desc = {
127 /* no elements => accept any params */
128 { /* end of list */ }
129 },
130};
131
734c3b85 132static int open_f(BlockDriverState *bs, int argc, char **argv)
e3aff4f6 133{
43642b38
DN
134 int flags = 0;
135 int readonly = 0;
136 int growable = 0;
137 int c;
b543c5cd 138 QemuOpts *qopts;
443422fd 139 QDict *opts;
43642b38 140
b543c5cd 141 while ((c = getopt(argc, argv, "snrgo:")) != EOF) {
43642b38
DN
142 switch (c) {
143 case 's':
144 flags |= BDRV_O_SNAPSHOT;
145 break;
146 case 'n':
147 flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB;
148 break;
149 case 'r':
150 readonly = 1;
151 break;
152 case 'g':
153 growable = 1;
154 break;
b543c5cd 155 case 'o':
443422fd 156 if (!qemu_opts_parse(&empty_opts, optarg, 0)) {
b543c5cd 157 printf("could not parse option list -- %s\n", optarg);
443422fd 158 qemu_opts_reset(&empty_opts);
b543c5cd
HR
159 return 0;
160 }
b543c5cd 161 break;
43642b38 162 default:
443422fd 163 qemu_opts_reset(&empty_opts);
c2cdf5c5 164 return qemuio_command_usage(&open_cmd);
f5edb014 165 }
43642b38
DN
166 }
167
168 if (!readonly) {
169 flags |= BDRV_O_RDWR;
170 }
e3aff4f6 171
443422fd
MA
172 qopts = qemu_opts_find(&empty_opts, NULL);
173 opts = qopts ? qemu_opts_to_qdict(qopts, NULL) : NULL;
174 qemu_opts_reset(&empty_opts);
175
fd0fee34
HR
176 if (optind == argc - 1) {
177 return openfile(argv[optind], flags, growable, opts);
178 } else if (optind == argc) {
179 return openfile(NULL, flags, growable, opts);
180 } else {
29f2601a 181 QDECREF(opts);
c2cdf5c5 182 return qemuio_command_usage(&open_cmd);
43642b38 183 }
e3aff4f6
AL
184}
185
e681be7e
KW
186static int quit_f(BlockDriverState *bs, int argc, char **argv)
187{
188 return 1;
189}
190
191static const cmdinfo_t quit_cmd = {
192 .name = "quit",
193 .altname = "q",
194 .cfunc = quit_f,
195 .argmin = -1,
196 .argmax = -1,
197 .flags = CMD_FLAG_GLOBAL,
198 .oneline = "exit the program",
199};
200
e3aff4f6
AL
201static void usage(const char *name)
202{
43642b38 203 printf(
d208cc35 204"Usage: %s [-h] [-V] [-rsnm] [-c STRING] ... [file]\n"
84844a20 205"QEMU Disk exerciser\n"
e3aff4f6 206"\n"
d208cc35
MK
207" -c, --cmd STRING execute command with its arguments\n"
208" from the given string\n"
e3aff4f6
AL
209" -r, --read-only export read-only\n"
210" -s, --snapshot use snapshot file\n"
211" -n, --nocache disable host cache\n"
1db6947d 212" -g, --growable allow file to grow (only applies to protocols)\n"
e3aff4f6 213" -m, --misalign misalign allocations for O_DIRECT\n"
5c6c3a6c 214" -k, --native-aio use kernel AIO implementation (on Linux only)\n"
592fa070 215" -t, --cache=MODE use the given cache mode for the image\n"
d7bb72c8 216" -T, --trace FILE enable trace events listed in the given file\n"
e3aff4f6
AL
217" -h, --help display this help and exit\n"
218" -V, --version output version information and exit\n"
d208cc35
MK
219"\n"
220"See '%s -c help' for information on available commands."
e3aff4f6 221"\n",
d208cc35 222 name, name);
e3aff4f6
AL
223}
224
d1174f13
KW
225static char *get_prompt(void)
226{
227 static char prompt[FILENAME_MAX + 2 /*"> "*/ + 1 /*"\0"*/ ];
228
229 if (!prompt[0]) {
230 snprintf(prompt, sizeof(prompt), "%s> ", progname);
231 }
232
233 return prompt;
234}
235
d5d1507b
SW
236static void GCC_FMT_ATTR(2, 3) readline_printf_func(void *opaque,
237 const char *fmt, ...)
d1174f13 238{
0cf17e18
SH
239 va_list ap;
240 va_start(ap, fmt);
241 vprintf(fmt, ap);
242 va_end(ap);
d1174f13 243}
0cf17e18
SH
244
245static void readline_flush_func(void *opaque)
d1174f13 246{
0cf17e18 247 fflush(stdout);
d1174f13
KW
248}
249
0cf17e18 250static void readline_func(void *opaque, const char *str, void *readline_opaque)
d1174f13 251{
0cf17e18
SH
252 char **line = readline_opaque;
253 *line = g_strdup(str);
254}
255
4694020d
SH
256static void completion_match(const char *cmd, void *opaque)
257{
258 readline_add_completion(readline_state, cmd);
259}
260
0cf17e18
SH
261static void readline_completion_func(void *opaque, const char *str)
262{
4694020d
SH
263 readline_set_completion_index(readline_state, strlen(str));
264 qemuio_complete_command(str, completion_match, NULL);
0cf17e18
SH
265}
266
267static char *fetchline_readline(void)
268{
269 char *line = NULL;
270
271 readline_start(readline_state, get_prompt(), 0, readline_func, &line);
272 while (!line) {
273 int ch = getchar();
274 if (ch == EOF) {
275 break;
d1174f13 276 }
0cf17e18 277 readline_handle_byte(readline_state, ch);
d1174f13
KW
278 }
279 return line;
280}
0cf17e18
SH
281
282#define MAXREADLINESZ 1024
283static char *fetchline_fgets(void)
d1174f13
KW
284{
285 char *p, *line = g_malloc(MAXREADLINESZ);
286
287 if (!fgets(line, MAXREADLINESZ, stdin)) {
288 g_free(line);
289 return NULL;
290 }
291
292 p = line + strlen(line);
293 if (p != line && p[-1] == '\n') {
294 p[-1] = '\0';
295 }
296
297 return line;
298}
0cf17e18
SH
299
300static char *fetchline(void)
301{
302 if (readline_state) {
303 return fetchline_readline();
304 } else {
305 return fetchline_fgets();
306 }
307}
d1174f13
KW
308
309static void prep_fetchline(void *opaque)
310{
311 int *fetchable = opaque;
312
313 qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL);
314 *fetchable= 1;
315}
316
317static void command_loop(void)
318{
319 int i, done = 0, fetchable = 0, prompted = 0;
320 char *input;
321
322 for (i = 0; !done && i < ncmdline; i++) {
3d21994f 323 done = qemuio_command(qemuio_bs, cmdline[i]);
d1174f13
KW
324 }
325 if (cmdline) {
326 g_free(cmdline);
327 return;
328 }
329
330 while (!done) {
331 if (!prompted) {
332 printf("%s", get_prompt());
333 fflush(stdout);
334 qemu_set_fd_handler(STDIN_FILENO, prep_fetchline, NULL, &fetchable);
335 prompted = 1;
336 }
337
338 main_loop_wait(false);
339
340 if (!fetchable) {
341 continue;
342 }
343
344 input = fetchline();
345 if (input == NULL) {
346 break;
347 }
3d21994f 348 done = qemuio_command(qemuio_bs, input);
d1174f13
KW
349 g_free(input);
350
351 prompted = 0;
352 fetchable = 0;
353 }
354 qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL);
355}
356
357static void add_user_command(char *optarg)
358{
359 cmdline = g_realloc(cmdline, ++ncmdline * sizeof(char *));
360 cmdline[ncmdline-1] = optarg;
361}
362
0cf17e18
SH
363static void reenable_tty_echo(void)
364{
365 qemu_set_tty_echo(STDIN_FILENO, true);
366}
367
e3aff4f6
AL
368int main(int argc, char **argv)
369{
43642b38
DN
370 int readonly = 0;
371 int growable = 0;
9e8f1835 372 const char *sopt = "hVc:d:rsnmgkt:T:";
43642b38
DN
373 const struct option lopt[] = {
374 { "help", 0, NULL, 'h' },
375 { "version", 0, NULL, 'V' },
376 { "offset", 1, NULL, 'o' },
377 { "cmd", 1, NULL, 'c' },
378 { "read-only", 0, NULL, 'r' },
379 { "snapshot", 0, NULL, 's' },
380 { "nocache", 0, NULL, 'n' },
381 { "misalign", 0, NULL, 'm' },
382 { "growable", 0, NULL, 'g' },
383 { "native-aio", 0, NULL, 'k' },
9e8f1835 384 { "discard", 1, NULL, 'd' },
592fa070 385 { "cache", 1, NULL, 't' },
d7bb72c8 386 { "trace", 1, NULL, 'T' },
43642b38
DN
387 { NULL, 0, NULL, 0 }
388 };
389 int c;
390 int opt_index = 0;
9e8f1835 391 int flags = BDRV_O_UNMAP;
43642b38 392
526eda14
MK
393#ifdef CONFIG_POSIX
394 signal(SIGPIPE, SIG_IGN);
395#endif
396
43642b38 397 progname = basename(argv[0]);
10f5bff6 398 qemu_init_exec_dir(argv[0]);
43642b38
DN
399
400 while ((c = getopt_long(argc, argv, sopt, lopt, &opt_index)) != -1) {
401 switch (c) {
402 case 's':
403 flags |= BDRV_O_SNAPSHOT;
404 break;
405 case 'n':
406 flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB;
407 break;
9e8f1835
PB
408 case 'd':
409 if (bdrv_parse_discard_flags(optarg, &flags) < 0) {
410 error_report("Invalid discard option: %s", optarg);
411 exit(1);
412 }
413 break;
43642b38
DN
414 case 'c':
415 add_user_command(optarg);
416 break;
417 case 'r':
418 readonly = 1;
419 break;
420 case 'm':
f9883880 421 qemuio_misalign = true;
43642b38
DN
422 break;
423 case 'g':
424 growable = 1;
425 break;
426 case 'k':
427 flags |= BDRV_O_NATIVE_AIO;
428 break;
592fa070
KW
429 case 't':
430 if (bdrv_parse_cache_flags(optarg, &flags) < 0) {
431 error_report("Invalid cache option: %s", optarg);
432 exit(1);
433 }
434 break;
d7bb72c8 435 case 'T':
5b808275 436 if (!trace_init_backends(optarg, NULL)) {
d7bb72c8
SH
437 exit(1); /* error message will have been printed */
438 }
439 break;
43642b38 440 case 'V':
02da386a 441 printf("%s version %s\n", progname, QEMU_VERSION);
43642b38
DN
442 exit(0);
443 case 'h':
444 usage(progname);
445 exit(0);
446 default:
447 usage(progname);
448 exit(1);
f5edb014 449 }
43642b38
DN
450 }
451
452 if ((argc - optind) > 1) {
453 usage(progname);
454 exit(1);
455 }
e3aff4f6 456
a57d1143 457 qemu_init_main_loop();
2592c59a 458 bdrv_init();
a57d1143 459
43642b38 460 /* initialize commands */
c2cdf5c5
KW
461 qemuio_add_command(&quit_cmd);
462 qemuio_add_command(&open_cmd);
463 qemuio_add_command(&close_cmd);
43642b38 464
0cf17e18
SH
465 if (isatty(STDIN_FILENO)) {
466 readline_state = readline_init(readline_printf_func,
467 readline_flush_func,
468 NULL,
469 readline_completion_func);
470 qemu_set_tty_echo(STDIN_FILENO, false);
471 atexit(reenable_tty_echo);
472 }
473
43642b38
DN
474 /* open the device */
475 if (!readonly) {
476 flags |= BDRV_O_RDWR;
477 }
478
479 if ((argc - optind) == 1) {
b543c5cd 480 openfile(argv[optind], flags, growable, NULL);
43642b38
DN
481 }
482 command_loop();
e3aff4f6 483
43642b38 484 /*
922453bc 485 * Make sure all outstanding requests complete before the program exits.
43642b38 486 */
922453bc 487 bdrv_drain_all();
95533d5f 488
734c3b85 489 if (qemuio_bs) {
4f6fd349 490 bdrv_unref(qemuio_bs);
43642b38 491 }
0cf17e18 492 g_free(readline_state);
43642b38 493 return 0;
e3aff4f6 494}