From 3ca2d9a6ddffdc0b01fa78c1bcf447141c3cf4c3 Mon Sep 17 00:00:00 2001
From: Jeffrey Cody <jcody@redhat.com>
Date: Wed, 21 Mar 2012 21:55:09 +0100
Subject: [PATCH 42/55] qmp: add block_job_set_speed command

RH-Author: Jeffrey Cody <jcody@redhat.com>
Message-id: <f73d5b1e37b2d52cf41444426a4ad50f610b88e7.1332362400.git.jcody@redhat.com>
Patchwork-id: 38891
O-Subject: [RHEL6.3 qemu-kvm PATCH v8 42/54] qmp: add block_job_set_speed command
Bugzilla: 582475
RH-Acked-by: Paolo Bonzini <pbonzini@redhat.com>
RH-Acked-by: Marcelo Tosatti <mtosatti@redhat.com>
RH-Acked-by: Kevin Wolf <kwolf@redhat.com>

From: Stefan Hajnoczi <stefanha@linux.vnet.ibm.com>

Add block_job_set_speed, which sets the maximum speed for a background
block operation.

Signed-off-by: Stefan Hajnoczi <stefanha@linux.vnet.ibm.com>
Acked-by: Luiz Capitulino <lcapitulino@redhat.com>
Signed-off-by: Kevin Wolf <kwolf@redhat.com>

(cherry picked from commit 2d47c6e9aa2475807913bd46dfca55980cca9fb4)

Signed-off-by: Stefan Hajnoczi <stefanha@linux.vnet.ibm.com>
Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>
Signed-off-by: Jeff Cody <jcody@redhat.com>
---
 blockdev.c      |   30 ++++++++++++++++++++++++++++++
 blockdev.h      |    2 ++
 qemu-monitor.hx |   42 ++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 74 insertions(+), 0 deletions(-)

Signed-off-by: Michal Novotny <minovotn@redhat.com>
---
 blockdev.c      |   30 ++++++++++++++++++++++++++++++
 blockdev.h      |    2 ++
 qemu-monitor.hx |   42 ++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 74 insertions(+), 0 deletions(-)

diff --git a/blockdev.c b/blockdev.c
index d60ee40..3d7e3a7 100644
--- a/blockdev.c
+++ b/blockdev.c
@@ -1067,3 +1067,33 @@ int do_block_stream(Monitor *mon, const QDict *params, QObject **ret_data)
     trace_do_block_stream(bs, bs->job);
     return 0;
 }
+
+static BlockJob *find_block_job(const char *device)
+{
+    BlockDriverState *bs;
+
+    bs = bdrv_find(device);
+    if (!bs || !bs->job) {
+        return NULL;
+    }
+    return bs->job;
+}
+
+int do_block_job_set_speed(Monitor *mon, const QDict *params,
+                           QObject **ret_data)
+{
+    const char *device = qdict_get_str(params, "device");
+    int64_t value = qdict_get_int(params, "value");
+    BlockJob *job = find_block_job(device);
+
+    if (!job) {
+        qerror_report(QERR_DEVICE_NOT_ACTIVE, device);
+        return -1;
+    }
+
+    if (block_job_set_speed(job, value) < 0) {
+        qerror_report(QERR_NOT_SUPPORTED);
+        return -1;
+    }
+    return 0;
+}
diff --git a/blockdev.h b/blockdev.h
index 6194bda..32e516b 100644
--- a/blockdev.h
+++ b/blockdev.h
@@ -72,5 +72,7 @@ int do_change_block(Monitor *mon, const char *device,
 int simple_drive_add(Monitor *mon, const QDict *qdict, QObject **ret_data);
 int do_drive_del(Monitor *mon, const QDict *qdict, QObject **ret_data);
 int do_block_stream(Monitor *mon, const QDict *qdict, QObject **ret_data);
+int do_block_job_set_speed(Monitor *mon, const QDict *params,
+                           QObject **ret_data);
 
 #endif
diff --git a/qemu-monitor.hx b/qemu-monitor.hx
index 839e77b..af9a69e 100644
--- a/qemu-monitor.hx
+++ b/qemu-monitor.hx
@@ -2079,6 +2079,48 @@ If image streaming is not supported by this device, NotSupported.
 
 EQMP
 
+    {
+        .name       = "block_job_set_speed",
+        .args_type  = "device:B,value:o",
+        .params     = "device value",
+        .help       = "set maximum speed for a background block operation",
+        .user_print = monitor_user_noop,
+        .mhandler.cmd_new = do_block_job_set_speed,
+    },
+
+STEXI
+@item block_job_set_stream
+@findex block_job_set_stream
+Set maximum speed for a background block operation.
+ETEXI
+
+SQMP
+block_job_set_speed
+-------------------
+
+Set maximum speed for a background block operation.
+
+This command can only be issued when there is an active block job.
+
+Throttling can be disabled by setting the speed to 0.
+
+Arguments:
+
+- device: the device name
+- value:  the maximum speed, in bytes per second
+
+Returns:
+
+Nothing on success
+If the job type does not support throttling, NotSupported
+If streaming is not active on this device, DeviceNotActive
+
+Example:
+
+-> { "execute": "block_job_set_speed",
+    "arguments": { "device": "virtio0", "value": 1024 } }
+EQMP
+
 HXCOMM Keep the 'info' command at the end!
 HXCOMM This is required for the QMP documentation layout.
 
-- 
1.7.7.6

