1d8e7d87eSManos Pitsidianakis /*
2d8e7d87eSManos Pitsidianakis * QEMU block throttling filter driver infrastructure
3d8e7d87eSManos Pitsidianakis *
4d8e7d87eSManos Pitsidianakis * Copyright (c) 2017 Manos Pitsidianakis
5d8e7d87eSManos Pitsidianakis *
6d8e7d87eSManos Pitsidianakis * This program is free software; you can redistribute it and/or
7d8e7d87eSManos Pitsidianakis * modify it under the terms of the GNU General Public License as
8d8e7d87eSManos Pitsidianakis * published by the Free Software Foundation; either version 2 or
9d8e7d87eSManos Pitsidianakis * (at your option) version 3 of the License.
10d8e7d87eSManos Pitsidianakis *
11d8e7d87eSManos Pitsidianakis * This program is distributed in the hope that it will be useful,
12d8e7d87eSManos Pitsidianakis * but WITHOUT ANY WARRANTY; without even the implied warranty of
13d8e7d87eSManos Pitsidianakis * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14d8e7d87eSManos Pitsidianakis * GNU General Public License for more details.
15d8e7d87eSManos Pitsidianakis *
16d8e7d87eSManos Pitsidianakis * You should have received a copy of the GNU General Public License
17d8e7d87eSManos Pitsidianakis * along with this program; if not, see <http://www.gnu.org/licenses/>.
18d8e7d87eSManos Pitsidianakis */
19d8e7d87eSManos Pitsidianakis
20d8e7d87eSManos Pitsidianakis #include "qemu/osdep.h"
21e2c1c34fSMarkus Armbruster #include "block/block-io.h"
22e2c1c34fSMarkus Armbruster #include "block/block_int.h"
23d8e7d87eSManos Pitsidianakis #include "block/throttle-groups.h"
240b8fa32fSMarkus Armbruster #include "qemu/module.h"
25922a01a0SMarkus Armbruster #include "qemu/option.h"
26d8e7d87eSManos Pitsidianakis #include "qemu/throttle-options.h"
27d8e7d87eSManos Pitsidianakis #include "qapi/error.h"
28d8e7d87eSManos Pitsidianakis
29d8e7d87eSManos Pitsidianakis static QemuOptsList throttle_opts = {
30d8e7d87eSManos Pitsidianakis .name = "throttle",
31d8e7d87eSManos Pitsidianakis .head = QTAILQ_HEAD_INITIALIZER(throttle_opts.head),
32d8e7d87eSManos Pitsidianakis .desc = {
33d8e7d87eSManos Pitsidianakis {
34d8e7d87eSManos Pitsidianakis .name = QEMU_OPT_THROTTLE_GROUP_NAME,
35d8e7d87eSManos Pitsidianakis .type = QEMU_OPT_STRING,
36d8e7d87eSManos Pitsidianakis .help = "Name of the throttle group",
37d8e7d87eSManos Pitsidianakis },
38d8e7d87eSManos Pitsidianakis { /* end of list */ }
39d8e7d87eSManos Pitsidianakis },
40d8e7d87eSManos Pitsidianakis };
41d8e7d87eSManos Pitsidianakis
42bc33c047SAlberto Garcia /*
43bc33c047SAlberto Garcia * If this function succeeds then the throttle group name is stored in
44bc33c047SAlberto Garcia * @group and must be freed by the caller.
45bc33c047SAlberto Garcia * If there's an error then @group remains unmodified.
46bc33c047SAlberto Garcia */
throttle_parse_options(QDict * options,char ** group,Error ** errp)47bc33c047SAlberto Garcia static int throttle_parse_options(QDict *options, char **group, Error **errp)
48d8e7d87eSManos Pitsidianakis {
49d8e7d87eSManos Pitsidianakis int ret;
50d8e7d87eSManos Pitsidianakis const char *group_name;
51d8e7d87eSManos Pitsidianakis QemuOpts *opts = qemu_opts_create(&throttle_opts, NULL, 0, &error_abort);
52d8e7d87eSManos Pitsidianakis
53af175e85SMarkus Armbruster if (!qemu_opts_absorb_qdict(opts, options, errp)) {
54d8e7d87eSManos Pitsidianakis ret = -EINVAL;
55d8e7d87eSManos Pitsidianakis goto fin;
56d8e7d87eSManos Pitsidianakis }
57d8e7d87eSManos Pitsidianakis
58d8e7d87eSManos Pitsidianakis group_name = qemu_opt_get(opts, QEMU_OPT_THROTTLE_GROUP_NAME);
59d8e7d87eSManos Pitsidianakis if (!group_name) {
60d8e7d87eSManos Pitsidianakis error_setg(errp, "Please specify a throttle group");
61d8e7d87eSManos Pitsidianakis ret = -EINVAL;
62d8e7d87eSManos Pitsidianakis goto fin;
63d8e7d87eSManos Pitsidianakis } else if (!throttle_group_exists(group_name)) {
64d8e7d87eSManos Pitsidianakis error_setg(errp, "Throttle group '%s' does not exist", group_name);
65d8e7d87eSManos Pitsidianakis ret = -EINVAL;
66d8e7d87eSManos Pitsidianakis goto fin;
67d8e7d87eSManos Pitsidianakis }
68d8e7d87eSManos Pitsidianakis
69bc33c047SAlberto Garcia *group = g_strdup(group_name);
70d8e7d87eSManos Pitsidianakis ret = 0;
71d8e7d87eSManos Pitsidianakis fin:
72d8e7d87eSManos Pitsidianakis qemu_opts_del(opts);
73d8e7d87eSManos Pitsidianakis return ret;
74d8e7d87eSManos Pitsidianakis }
75d8e7d87eSManos Pitsidianakis
throttle_open(BlockDriverState * bs,QDict * options,int flags,Error ** errp)76d8e7d87eSManos Pitsidianakis static int throttle_open(BlockDriverState *bs, QDict *options,
77d8e7d87eSManos Pitsidianakis int flags, Error **errp)
78d8e7d87eSManos Pitsidianakis {
79d8e7d87eSManos Pitsidianakis ThrottleGroupMember *tgm = bs->opaque;
80bc33c047SAlberto Garcia char *group;
81bc33c047SAlberto Garcia int ret;
82d8e7d87eSManos Pitsidianakis
8383930780SVladimir Sementsov-Ogievskiy ret = bdrv_open_file_child(NULL, options, "file", bs, errp);
8483930780SVladimir Sementsov-Ogievskiy if (ret < 0) {
8583930780SVladimir Sementsov-Ogievskiy return ret;
86d8e7d87eSManos Pitsidianakis }
87*a4b740dbSKevin Wolf
88*a4b740dbSKevin Wolf GRAPH_RDLOCK_GUARD_MAINLOOP();
89*a4b740dbSKevin Wolf
90228345bfSMax Reitz bs->supported_write_flags = bs->file->bs->supported_write_flags |
91228345bfSMax Reitz BDRV_REQ_WRITE_UNCHANGED;
92228345bfSMax Reitz bs->supported_zero_flags = bs->file->bs->supported_zero_flags |
93228345bfSMax Reitz BDRV_REQ_WRITE_UNCHANGED;
94d8e7d87eSManos Pitsidianakis
95bc33c047SAlberto Garcia ret = throttle_parse_options(options, &group, errp);
96bc33c047SAlberto Garcia if (ret == 0) {
97bc33c047SAlberto Garcia /* Register membership to group with name group_name */
98bc33c047SAlberto Garcia throttle_group_register_tgm(tgm, group, bdrv_get_aio_context(bs));
99bc33c047SAlberto Garcia g_free(group);
100bc33c047SAlberto Garcia }
101bc33c047SAlberto Garcia
102bc33c047SAlberto Garcia return ret;
103d8e7d87eSManos Pitsidianakis }
104d8e7d87eSManos Pitsidianakis
throttle_close(BlockDriverState * bs)105d8e7d87eSManos Pitsidianakis static void throttle_close(BlockDriverState *bs)
106d8e7d87eSManos Pitsidianakis {
107d8e7d87eSManos Pitsidianakis ThrottleGroupMember *tgm = bs->opaque;
108d8e7d87eSManos Pitsidianakis throttle_group_unregister_tgm(tgm);
109d8e7d87eSManos Pitsidianakis }
110d8e7d87eSManos Pitsidianakis
111d8e7d87eSManos Pitsidianakis
1128ab8140aSKevin Wolf static int64_t coroutine_fn GRAPH_RDLOCK
throttle_co_getlength(BlockDriverState * bs)1138ab8140aSKevin Wolf throttle_co_getlength(BlockDriverState *bs)
114d8e7d87eSManos Pitsidianakis {
115c86422c5SEmanuele Giuseppe Esposito return bdrv_co_getlength(bs->file->bs);
116d8e7d87eSManos Pitsidianakis }
117d8e7d87eSManos Pitsidianakis
118b9b10c35SKevin Wolf static int coroutine_fn GRAPH_RDLOCK
throttle_co_preadv(BlockDriverState * bs,int64_t offset,int64_t bytes,QEMUIOVector * qiov,BdrvRequestFlags flags)119b9b10c35SKevin Wolf throttle_co_preadv(BlockDriverState *bs, int64_t offset, int64_t bytes,
120b9b10c35SKevin Wolf QEMUIOVector *qiov, BdrvRequestFlags flags)
121d8e7d87eSManos Pitsidianakis {
122d8e7d87eSManos Pitsidianakis
123d8e7d87eSManos Pitsidianakis ThrottleGroupMember *tgm = bs->opaque;
1243b2337efSzhenwei pi throttle_group_co_io_limits_intercept(tgm, bytes, THROTTLE_READ);
125d8e7d87eSManos Pitsidianakis
126d8e7d87eSManos Pitsidianakis return bdrv_co_preadv(bs->file, offset, bytes, qiov, flags);
127d8e7d87eSManos Pitsidianakis }
128d8e7d87eSManos Pitsidianakis
129b9b10c35SKevin Wolf static int coroutine_fn GRAPH_RDLOCK
throttle_co_pwritev(BlockDriverState * bs,int64_t offset,int64_t bytes,QEMUIOVector * qiov,BdrvRequestFlags flags)130b9b10c35SKevin Wolf throttle_co_pwritev(BlockDriverState *bs, int64_t offset, int64_t bytes,
131b9b10c35SKevin Wolf QEMUIOVector *qiov, BdrvRequestFlags flags)
132d8e7d87eSManos Pitsidianakis {
133d8e7d87eSManos Pitsidianakis ThrottleGroupMember *tgm = bs->opaque;
1343b2337efSzhenwei pi throttle_group_co_io_limits_intercept(tgm, bytes, THROTTLE_WRITE);
135d8e7d87eSManos Pitsidianakis
136d8e7d87eSManos Pitsidianakis return bdrv_co_pwritev(bs->file, offset, bytes, qiov, flags);
137d8e7d87eSManos Pitsidianakis }
138d8e7d87eSManos Pitsidianakis
139abaf8b75SKevin Wolf static int coroutine_fn GRAPH_RDLOCK
throttle_co_pwrite_zeroes(BlockDriverState * bs,int64_t offset,int64_t bytes,BdrvRequestFlags flags)140abaf8b75SKevin Wolf throttle_co_pwrite_zeroes(BlockDriverState *bs, int64_t offset, int64_t bytes,
141d8e7d87eSManos Pitsidianakis BdrvRequestFlags flags)
142d8e7d87eSManos Pitsidianakis {
143d8e7d87eSManos Pitsidianakis ThrottleGroupMember *tgm = bs->opaque;
1443b2337efSzhenwei pi throttle_group_co_io_limits_intercept(tgm, bytes, THROTTLE_WRITE);
145d8e7d87eSManos Pitsidianakis
146d8e7d87eSManos Pitsidianakis return bdrv_co_pwrite_zeroes(bs->file, offset, bytes, flags);
147d8e7d87eSManos Pitsidianakis }
148d8e7d87eSManos Pitsidianakis
1499a5a1c62SEmanuele Giuseppe Esposito static int coroutine_fn GRAPH_RDLOCK
throttle_co_pdiscard(BlockDriverState * bs,int64_t offset,int64_t bytes)1509a5a1c62SEmanuele Giuseppe Esposito throttle_co_pdiscard(BlockDriverState *bs, int64_t offset, int64_t bytes)
151d8e7d87eSManos Pitsidianakis {
152d8e7d87eSManos Pitsidianakis ThrottleGroupMember *tgm = bs->opaque;
1533b2337efSzhenwei pi throttle_group_co_io_limits_intercept(tgm, bytes, THROTTLE_WRITE);
154d8e7d87eSManos Pitsidianakis
1550b9fd3f4SFam Zheng return bdrv_co_pdiscard(bs->file, offset, bytes);
156d8e7d87eSManos Pitsidianakis }
157d8e7d87eSManos Pitsidianakis
158b9b10c35SKevin Wolf static int coroutine_fn GRAPH_RDLOCK
throttle_co_pwritev_compressed(BlockDriverState * bs,int64_t offset,int64_t bytes,QEMUIOVector * qiov)159b9b10c35SKevin Wolf throttle_co_pwritev_compressed(BlockDriverState *bs, int64_t offset,
160b9b10c35SKevin Wolf int64_t bytes, QEMUIOVector *qiov)
161e7e754aeSMax Reitz {
162e7e754aeSMax Reitz return throttle_co_pwritev(bs, offset, bytes, qiov,
163e7e754aeSMax Reitz BDRV_REQ_WRITE_COMPRESSED);
164e7e754aeSMax Reitz }
165e7e754aeSMax Reitz
throttle_co_flush(BlockDriverState * bs)16688095349SEmanuele Giuseppe Esposito static int coroutine_fn GRAPH_RDLOCK throttle_co_flush(BlockDriverState *bs)
167d8e7d87eSManos Pitsidianakis {
168d8e7d87eSManos Pitsidianakis return bdrv_co_flush(bs->file->bs);
169d8e7d87eSManos Pitsidianakis }
170d8e7d87eSManos Pitsidianakis
throttle_detach_aio_context(BlockDriverState * bs)171d8e7d87eSManos Pitsidianakis static void throttle_detach_aio_context(BlockDriverState *bs)
172d8e7d87eSManos Pitsidianakis {
173d8e7d87eSManos Pitsidianakis ThrottleGroupMember *tgm = bs->opaque;
174d8e7d87eSManos Pitsidianakis throttle_group_detach_aio_context(tgm);
175d8e7d87eSManos Pitsidianakis }
176d8e7d87eSManos Pitsidianakis
throttle_attach_aio_context(BlockDriverState * bs,AioContext * new_context)177d8e7d87eSManos Pitsidianakis static void throttle_attach_aio_context(BlockDriverState *bs,
178d8e7d87eSManos Pitsidianakis AioContext *new_context)
179d8e7d87eSManos Pitsidianakis {
180d8e7d87eSManos Pitsidianakis ThrottleGroupMember *tgm = bs->opaque;
181d8e7d87eSManos Pitsidianakis throttle_group_attach_aio_context(tgm, new_context);
182d8e7d87eSManos Pitsidianakis }
183d8e7d87eSManos Pitsidianakis
throttle_reopen_prepare(BDRVReopenState * reopen_state,BlockReopenQueue * queue,Error ** errp)184d8e7d87eSManos Pitsidianakis static int throttle_reopen_prepare(BDRVReopenState *reopen_state,
185d8e7d87eSManos Pitsidianakis BlockReopenQueue *queue, Error **errp)
186d8e7d87eSManos Pitsidianakis {
187bc33c047SAlberto Garcia int ret;
188bc33c047SAlberto Garcia char *group = NULL;
189d8e7d87eSManos Pitsidianakis
190d8e7d87eSManos Pitsidianakis assert(reopen_state != NULL);
191d8e7d87eSManos Pitsidianakis assert(reopen_state->bs != NULL);
192d8e7d87eSManos Pitsidianakis
193bc33c047SAlberto Garcia ret = throttle_parse_options(reopen_state->options, &group, errp);
194bc33c047SAlberto Garcia reopen_state->opaque = group;
195bc33c047SAlberto Garcia return ret;
196d8e7d87eSManos Pitsidianakis }
197d8e7d87eSManos Pitsidianakis
throttle_reopen_commit(BDRVReopenState * reopen_state)198d8e7d87eSManos Pitsidianakis static void throttle_reopen_commit(BDRVReopenState *reopen_state)
199d8e7d87eSManos Pitsidianakis {
200bc33c047SAlberto Garcia BlockDriverState *bs = reopen_state->bs;
201bc33c047SAlberto Garcia ThrottleGroupMember *tgm = bs->opaque;
202bc33c047SAlberto Garcia char *group = reopen_state->opaque;
203d8e7d87eSManos Pitsidianakis
204bc33c047SAlberto Garcia assert(group);
205bc33c047SAlberto Garcia
206bc33c047SAlberto Garcia if (strcmp(group, throttle_group_get_name(tgm))) {
207bc33c047SAlberto Garcia throttle_group_unregister_tgm(tgm);
208bc33c047SAlberto Garcia throttle_group_register_tgm(tgm, group, bdrv_get_aio_context(bs));
209bc33c047SAlberto Garcia }
210bc33c047SAlberto Garcia g_free(reopen_state->opaque);
211d8e7d87eSManos Pitsidianakis reopen_state->opaque = NULL;
212d8e7d87eSManos Pitsidianakis }
213d8e7d87eSManos Pitsidianakis
throttle_reopen_abort(BDRVReopenState * reopen_state)214d8e7d87eSManos Pitsidianakis static void throttle_reopen_abort(BDRVReopenState *reopen_state)
215d8e7d87eSManos Pitsidianakis {
216bc33c047SAlberto Garcia g_free(reopen_state->opaque);
217d8e7d87eSManos Pitsidianakis reopen_state->opaque = NULL;
218d8e7d87eSManos Pitsidianakis }
219d8e7d87eSManos Pitsidianakis
throttle_drain_begin(BlockDriverState * bs)2205e8ac217SKevin Wolf static void throttle_drain_begin(BlockDriverState *bs)
221b867eaa1SManos Pitsidianakis {
222b867eaa1SManos Pitsidianakis ThrottleGroupMember *tgm = bs->opaque;
223d73415a3SStefan Hajnoczi if (qatomic_fetch_inc(&tgm->io_limits_disabled) == 0) {
224b867eaa1SManos Pitsidianakis throttle_group_restart_tgm(tgm);
225b867eaa1SManos Pitsidianakis }
226b867eaa1SManos Pitsidianakis }
227b867eaa1SManos Pitsidianakis
throttle_drain_end(BlockDriverState * bs)2285e8ac217SKevin Wolf static void throttle_drain_end(BlockDriverState *bs)
229b867eaa1SManos Pitsidianakis {
230b867eaa1SManos Pitsidianakis ThrottleGroupMember *tgm = bs->opaque;
231b867eaa1SManos Pitsidianakis assert(tgm->io_limits_disabled);
232d73415a3SStefan Hajnoczi qatomic_dec(&tgm->io_limits_disabled);
233b867eaa1SManos Pitsidianakis }
234b867eaa1SManos Pitsidianakis
2352654267cSMax Reitz static const char *const throttle_strong_runtime_opts[] = {
2362654267cSMax Reitz QEMU_OPT_THROTTLE_GROUP_NAME,
2372654267cSMax Reitz
2382654267cSMax Reitz NULL
2392654267cSMax Reitz };
2402654267cSMax Reitz
241d8e7d87eSManos Pitsidianakis static BlockDriver bdrv_throttle = {
242d8e7d87eSManos Pitsidianakis .format_name = "throttle",
243d8e7d87eSManos Pitsidianakis .instance_size = sizeof(ThrottleGroupMember),
244d8e7d87eSManos Pitsidianakis
245a7328ba5SFabiano Rosas .bdrv_open = throttle_open,
246d8e7d87eSManos Pitsidianakis .bdrv_close = throttle_close,
247d8e7d87eSManos Pitsidianakis .bdrv_co_flush = throttle_co_flush,
248d8e7d87eSManos Pitsidianakis
24969dca43dSMax Reitz .bdrv_child_perm = bdrv_default_perms,
250d8e7d87eSManos Pitsidianakis
251c86422c5SEmanuele Giuseppe Esposito .bdrv_co_getlength = throttle_co_getlength,
252d8e7d87eSManos Pitsidianakis
253d8e7d87eSManos Pitsidianakis .bdrv_co_preadv = throttle_co_preadv,
254d8e7d87eSManos Pitsidianakis .bdrv_co_pwritev = throttle_co_pwritev,
255d8e7d87eSManos Pitsidianakis
256d8e7d87eSManos Pitsidianakis .bdrv_co_pwrite_zeroes = throttle_co_pwrite_zeroes,
257d8e7d87eSManos Pitsidianakis .bdrv_co_pdiscard = throttle_co_pdiscard,
258e7e754aeSMax Reitz .bdrv_co_pwritev_compressed = throttle_co_pwritev_compressed,
259d8e7d87eSManos Pitsidianakis
260d8e7d87eSManos Pitsidianakis .bdrv_attach_aio_context = throttle_attach_aio_context,
261d8e7d87eSManos Pitsidianakis .bdrv_detach_aio_context = throttle_detach_aio_context,
262d8e7d87eSManos Pitsidianakis
263d8e7d87eSManos Pitsidianakis .bdrv_reopen_prepare = throttle_reopen_prepare,
264d8e7d87eSManos Pitsidianakis .bdrv_reopen_commit = throttle_reopen_commit,
265d8e7d87eSManos Pitsidianakis .bdrv_reopen_abort = throttle_reopen_abort,
266d8e7d87eSManos Pitsidianakis
2675e8ac217SKevin Wolf .bdrv_drain_begin = throttle_drain_begin,
2685e8ac217SKevin Wolf .bdrv_drain_end = throttle_drain_end,
269b867eaa1SManos Pitsidianakis
270d8e7d87eSManos Pitsidianakis .is_filter = true,
2712654267cSMax Reitz .strong_runtime_opts = throttle_strong_runtime_opts,
272d8e7d87eSManos Pitsidianakis };
273d8e7d87eSManos Pitsidianakis
bdrv_throttle_init(void)274d8e7d87eSManos Pitsidianakis static void bdrv_throttle_init(void)
275d8e7d87eSManos Pitsidianakis {
276d8e7d87eSManos Pitsidianakis bdrv_register(&bdrv_throttle);
277d8e7d87eSManos Pitsidianakis }
278d8e7d87eSManos Pitsidianakis
279d8e7d87eSManos Pitsidianakis block_init(bdrv_throttle_init);
280