xref: /openbmc/qemu/block/throttle.c (revision af175e85f92c870386ad74f466e29537b79611d3)
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"
21d8e7d87eSManos Pitsidianakis #include "block/throttle-groups.h"
220b8fa32fSMarkus Armbruster #include "qemu/module.h"
23922a01a0SMarkus Armbruster #include "qemu/option.h"
24d8e7d87eSManos Pitsidianakis #include "qemu/throttle-options.h"
25d8e7d87eSManos Pitsidianakis #include "qapi/error.h"
26d8e7d87eSManos Pitsidianakis 
27d8e7d87eSManos Pitsidianakis static QemuOptsList throttle_opts = {
28d8e7d87eSManos Pitsidianakis     .name = "throttle",
29d8e7d87eSManos Pitsidianakis     .head = QTAILQ_HEAD_INITIALIZER(throttle_opts.head),
30d8e7d87eSManos Pitsidianakis     .desc = {
31d8e7d87eSManos Pitsidianakis         {
32d8e7d87eSManos Pitsidianakis             .name = QEMU_OPT_THROTTLE_GROUP_NAME,
33d8e7d87eSManos Pitsidianakis             .type = QEMU_OPT_STRING,
34d8e7d87eSManos Pitsidianakis             .help = "Name of the throttle group",
35d8e7d87eSManos Pitsidianakis         },
36d8e7d87eSManos Pitsidianakis         { /* end of list */ }
37d8e7d87eSManos Pitsidianakis     },
38d8e7d87eSManos Pitsidianakis };
39d8e7d87eSManos Pitsidianakis 
40bc33c047SAlberto Garcia /*
41bc33c047SAlberto Garcia  * If this function succeeds then the throttle group name is stored in
42bc33c047SAlberto Garcia  * @group and must be freed by the caller.
43bc33c047SAlberto Garcia  * If there's an error then @group remains unmodified.
44bc33c047SAlberto Garcia  */
45bc33c047SAlberto Garcia static int throttle_parse_options(QDict *options, char **group, Error **errp)
46d8e7d87eSManos Pitsidianakis {
47d8e7d87eSManos Pitsidianakis     int ret;
48d8e7d87eSManos Pitsidianakis     const char *group_name;
49d8e7d87eSManos Pitsidianakis     QemuOpts *opts = qemu_opts_create(&throttle_opts, NULL, 0, &error_abort);
50d8e7d87eSManos Pitsidianakis 
51*af175e85SMarkus Armbruster     if (!qemu_opts_absorb_qdict(opts, options, errp)) {
52d8e7d87eSManos Pitsidianakis         ret = -EINVAL;
53d8e7d87eSManos Pitsidianakis         goto fin;
54d8e7d87eSManos Pitsidianakis     }
55d8e7d87eSManos Pitsidianakis 
56d8e7d87eSManos Pitsidianakis     group_name = qemu_opt_get(opts, QEMU_OPT_THROTTLE_GROUP_NAME);
57d8e7d87eSManos Pitsidianakis     if (!group_name) {
58d8e7d87eSManos Pitsidianakis         error_setg(errp, "Please specify a throttle group");
59d8e7d87eSManos Pitsidianakis         ret = -EINVAL;
60d8e7d87eSManos Pitsidianakis         goto fin;
61d8e7d87eSManos Pitsidianakis     } else if (!throttle_group_exists(group_name)) {
62d8e7d87eSManos Pitsidianakis         error_setg(errp, "Throttle group '%s' does not exist", group_name);
63d8e7d87eSManos Pitsidianakis         ret = -EINVAL;
64d8e7d87eSManos Pitsidianakis         goto fin;
65d8e7d87eSManos Pitsidianakis     }
66d8e7d87eSManos Pitsidianakis 
67bc33c047SAlberto Garcia     *group = g_strdup(group_name);
68d8e7d87eSManos Pitsidianakis     ret = 0;
69d8e7d87eSManos Pitsidianakis fin:
70d8e7d87eSManos Pitsidianakis     qemu_opts_del(opts);
71d8e7d87eSManos Pitsidianakis     return ret;
72d8e7d87eSManos Pitsidianakis }
73d8e7d87eSManos Pitsidianakis 
74d8e7d87eSManos Pitsidianakis static int throttle_open(BlockDriverState *bs, QDict *options,
75d8e7d87eSManos Pitsidianakis                          int flags, Error **errp)
76d8e7d87eSManos Pitsidianakis {
77d8e7d87eSManos Pitsidianakis     ThrottleGroupMember *tgm = bs->opaque;
78bc33c047SAlberto Garcia     char *group;
79bc33c047SAlberto Garcia     int ret;
80d8e7d87eSManos Pitsidianakis 
81b3af2af4SMax Reitz     bs->file = bdrv_open_child(NULL, options, "file", bs, &child_of_bds,
82b3af2af4SMax Reitz                                BDRV_CHILD_FILTERED | BDRV_CHILD_PRIMARY,
83b3af2af4SMax Reitz                                false, errp);
84d8e7d87eSManos Pitsidianakis     if (!bs->file) {
85d8e7d87eSManos Pitsidianakis         return -EINVAL;
86d8e7d87eSManos Pitsidianakis     }
87228345bfSMax Reitz     bs->supported_write_flags = bs->file->bs->supported_write_flags |
88228345bfSMax Reitz                                 BDRV_REQ_WRITE_UNCHANGED;
89228345bfSMax Reitz     bs->supported_zero_flags = bs->file->bs->supported_zero_flags |
90228345bfSMax Reitz                                BDRV_REQ_WRITE_UNCHANGED;
91d8e7d87eSManos Pitsidianakis 
92bc33c047SAlberto Garcia     ret = throttle_parse_options(options, &group, errp);
93bc33c047SAlberto Garcia     if (ret == 0) {
94bc33c047SAlberto Garcia         /* Register membership to group with name group_name */
95bc33c047SAlberto Garcia         throttle_group_register_tgm(tgm, group, bdrv_get_aio_context(bs));
96bc33c047SAlberto Garcia         g_free(group);
97bc33c047SAlberto Garcia     }
98bc33c047SAlberto Garcia 
99bc33c047SAlberto Garcia     return ret;
100d8e7d87eSManos Pitsidianakis }
101d8e7d87eSManos Pitsidianakis 
102d8e7d87eSManos Pitsidianakis static void throttle_close(BlockDriverState *bs)
103d8e7d87eSManos Pitsidianakis {
104d8e7d87eSManos Pitsidianakis     ThrottleGroupMember *tgm = bs->opaque;
105d8e7d87eSManos Pitsidianakis     throttle_group_unregister_tgm(tgm);
106d8e7d87eSManos Pitsidianakis }
107d8e7d87eSManos Pitsidianakis 
108d8e7d87eSManos Pitsidianakis 
109d8e7d87eSManos Pitsidianakis static int64_t throttle_getlength(BlockDriverState *bs)
110d8e7d87eSManos Pitsidianakis {
111d8e7d87eSManos Pitsidianakis     return bdrv_getlength(bs->file->bs);
112d8e7d87eSManos Pitsidianakis }
113d8e7d87eSManos Pitsidianakis 
114d8e7d87eSManos Pitsidianakis static int coroutine_fn throttle_co_preadv(BlockDriverState *bs,
115d8e7d87eSManos Pitsidianakis                                            uint64_t offset, uint64_t bytes,
116d8e7d87eSManos Pitsidianakis                                            QEMUIOVector *qiov, int flags)
117d8e7d87eSManos Pitsidianakis {
118d8e7d87eSManos Pitsidianakis 
119d8e7d87eSManos Pitsidianakis     ThrottleGroupMember *tgm = bs->opaque;
120d8e7d87eSManos Pitsidianakis     throttle_group_co_io_limits_intercept(tgm, bytes, false);
121d8e7d87eSManos Pitsidianakis 
122d8e7d87eSManos Pitsidianakis     return bdrv_co_preadv(bs->file, offset, bytes, qiov, flags);
123d8e7d87eSManos Pitsidianakis }
124d8e7d87eSManos Pitsidianakis 
125d8e7d87eSManos Pitsidianakis static int coroutine_fn throttle_co_pwritev(BlockDriverState *bs,
126d8e7d87eSManos Pitsidianakis                                             uint64_t offset, uint64_t bytes,
127d8e7d87eSManos Pitsidianakis                                             QEMUIOVector *qiov, int flags)
128d8e7d87eSManos Pitsidianakis {
129d8e7d87eSManos Pitsidianakis     ThrottleGroupMember *tgm = bs->opaque;
130d8e7d87eSManos Pitsidianakis     throttle_group_co_io_limits_intercept(tgm, bytes, true);
131d8e7d87eSManos Pitsidianakis 
132d8e7d87eSManos Pitsidianakis     return bdrv_co_pwritev(bs->file, offset, bytes, qiov, flags);
133d8e7d87eSManos Pitsidianakis }
134d8e7d87eSManos Pitsidianakis 
135d8e7d87eSManos Pitsidianakis static int coroutine_fn throttle_co_pwrite_zeroes(BlockDriverState *bs,
136d8e7d87eSManos Pitsidianakis                                                   int64_t offset, int bytes,
137d8e7d87eSManos Pitsidianakis                                                   BdrvRequestFlags flags)
138d8e7d87eSManos Pitsidianakis {
139d8e7d87eSManos Pitsidianakis     ThrottleGroupMember *tgm = bs->opaque;
140d8e7d87eSManos Pitsidianakis     throttle_group_co_io_limits_intercept(tgm, bytes, true);
141d8e7d87eSManos Pitsidianakis 
142d8e7d87eSManos Pitsidianakis     return bdrv_co_pwrite_zeroes(bs->file, offset, bytes, flags);
143d8e7d87eSManos Pitsidianakis }
144d8e7d87eSManos Pitsidianakis 
145d8e7d87eSManos Pitsidianakis static int coroutine_fn throttle_co_pdiscard(BlockDriverState *bs,
146d8e7d87eSManos Pitsidianakis                                              int64_t offset, int bytes)
147d8e7d87eSManos Pitsidianakis {
148d8e7d87eSManos Pitsidianakis     ThrottleGroupMember *tgm = bs->opaque;
149d8e7d87eSManos Pitsidianakis     throttle_group_co_io_limits_intercept(tgm, bytes, true);
150d8e7d87eSManos Pitsidianakis 
1510b9fd3f4SFam Zheng     return bdrv_co_pdiscard(bs->file, offset, bytes);
152d8e7d87eSManos Pitsidianakis }
153d8e7d87eSManos Pitsidianakis 
154d8e7d87eSManos Pitsidianakis static int throttle_co_flush(BlockDriverState *bs)
155d8e7d87eSManos Pitsidianakis {
156d8e7d87eSManos Pitsidianakis     return bdrv_co_flush(bs->file->bs);
157d8e7d87eSManos Pitsidianakis }
158d8e7d87eSManos Pitsidianakis 
159d8e7d87eSManos Pitsidianakis static void throttle_detach_aio_context(BlockDriverState *bs)
160d8e7d87eSManos Pitsidianakis {
161d8e7d87eSManos Pitsidianakis     ThrottleGroupMember *tgm = bs->opaque;
162d8e7d87eSManos Pitsidianakis     throttle_group_detach_aio_context(tgm);
163d8e7d87eSManos Pitsidianakis }
164d8e7d87eSManos Pitsidianakis 
165d8e7d87eSManos Pitsidianakis static void throttle_attach_aio_context(BlockDriverState *bs,
166d8e7d87eSManos Pitsidianakis                                         AioContext *new_context)
167d8e7d87eSManos Pitsidianakis {
168d8e7d87eSManos Pitsidianakis     ThrottleGroupMember *tgm = bs->opaque;
169d8e7d87eSManos Pitsidianakis     throttle_group_attach_aio_context(tgm, new_context);
170d8e7d87eSManos Pitsidianakis }
171d8e7d87eSManos Pitsidianakis 
172d8e7d87eSManos Pitsidianakis static int throttle_reopen_prepare(BDRVReopenState *reopen_state,
173d8e7d87eSManos Pitsidianakis                                    BlockReopenQueue *queue, Error **errp)
174d8e7d87eSManos Pitsidianakis {
175bc33c047SAlberto Garcia     int ret;
176bc33c047SAlberto Garcia     char *group = NULL;
177d8e7d87eSManos Pitsidianakis 
178d8e7d87eSManos Pitsidianakis     assert(reopen_state != NULL);
179d8e7d87eSManos Pitsidianakis     assert(reopen_state->bs != NULL);
180d8e7d87eSManos Pitsidianakis 
181bc33c047SAlberto Garcia     ret = throttle_parse_options(reopen_state->options, &group, errp);
182bc33c047SAlberto Garcia     reopen_state->opaque = group;
183bc33c047SAlberto Garcia     return ret;
184d8e7d87eSManos Pitsidianakis }
185d8e7d87eSManos Pitsidianakis 
186d8e7d87eSManos Pitsidianakis static void throttle_reopen_commit(BDRVReopenState *reopen_state)
187d8e7d87eSManos Pitsidianakis {
188bc33c047SAlberto Garcia     BlockDriverState *bs = reopen_state->bs;
189bc33c047SAlberto Garcia     ThrottleGroupMember *tgm = bs->opaque;
190bc33c047SAlberto Garcia     char *group = reopen_state->opaque;
191d8e7d87eSManos Pitsidianakis 
192bc33c047SAlberto Garcia     assert(group);
193bc33c047SAlberto Garcia 
194bc33c047SAlberto Garcia     if (strcmp(group, throttle_group_get_name(tgm))) {
195bc33c047SAlberto Garcia         throttle_group_unregister_tgm(tgm);
196bc33c047SAlberto Garcia         throttle_group_register_tgm(tgm, group, bdrv_get_aio_context(bs));
197bc33c047SAlberto Garcia     }
198bc33c047SAlberto Garcia     g_free(reopen_state->opaque);
199d8e7d87eSManos Pitsidianakis     reopen_state->opaque = NULL;
200d8e7d87eSManos Pitsidianakis }
201d8e7d87eSManos Pitsidianakis 
202d8e7d87eSManos Pitsidianakis static void throttle_reopen_abort(BDRVReopenState *reopen_state)
203d8e7d87eSManos Pitsidianakis {
204bc33c047SAlberto Garcia     g_free(reopen_state->opaque);
205d8e7d87eSManos Pitsidianakis     reopen_state->opaque = NULL;
206d8e7d87eSManos Pitsidianakis }
207d8e7d87eSManos Pitsidianakis 
208b867eaa1SManos Pitsidianakis static void coroutine_fn throttle_co_drain_begin(BlockDriverState *bs)
209b867eaa1SManos Pitsidianakis {
210b867eaa1SManos Pitsidianakis     ThrottleGroupMember *tgm = bs->opaque;
211b867eaa1SManos Pitsidianakis     if (atomic_fetch_inc(&tgm->io_limits_disabled) == 0) {
212b867eaa1SManos Pitsidianakis         throttle_group_restart_tgm(tgm);
213b867eaa1SManos Pitsidianakis     }
214b867eaa1SManos Pitsidianakis }
215b867eaa1SManos Pitsidianakis 
216b867eaa1SManos Pitsidianakis static void coroutine_fn throttle_co_drain_end(BlockDriverState *bs)
217b867eaa1SManos Pitsidianakis {
218b867eaa1SManos Pitsidianakis     ThrottleGroupMember *tgm = bs->opaque;
219b867eaa1SManos Pitsidianakis     assert(tgm->io_limits_disabled);
220b867eaa1SManos Pitsidianakis     atomic_dec(&tgm->io_limits_disabled);
221b867eaa1SManos Pitsidianakis }
222b867eaa1SManos Pitsidianakis 
2232654267cSMax Reitz static const char *const throttle_strong_runtime_opts[] = {
2242654267cSMax Reitz     QEMU_OPT_THROTTLE_GROUP_NAME,
2252654267cSMax Reitz 
2262654267cSMax Reitz     NULL
2272654267cSMax Reitz };
2282654267cSMax Reitz 
229d8e7d87eSManos Pitsidianakis static BlockDriver bdrv_throttle = {
230d8e7d87eSManos Pitsidianakis     .format_name                        =   "throttle",
231d8e7d87eSManos Pitsidianakis     .instance_size                      =   sizeof(ThrottleGroupMember),
232d8e7d87eSManos Pitsidianakis 
233a7328ba5SFabiano Rosas     .bdrv_open                          =   throttle_open,
234d8e7d87eSManos Pitsidianakis     .bdrv_close                         =   throttle_close,
235d8e7d87eSManos Pitsidianakis     .bdrv_co_flush                      =   throttle_co_flush,
236d8e7d87eSManos Pitsidianakis 
23769dca43dSMax Reitz     .bdrv_child_perm                    =   bdrv_default_perms,
238d8e7d87eSManos Pitsidianakis 
239d8e7d87eSManos Pitsidianakis     .bdrv_getlength                     =   throttle_getlength,
240d8e7d87eSManos Pitsidianakis 
241d8e7d87eSManos Pitsidianakis     .bdrv_co_preadv                     =   throttle_co_preadv,
242d8e7d87eSManos Pitsidianakis     .bdrv_co_pwritev                    =   throttle_co_pwritev,
243d8e7d87eSManos Pitsidianakis 
244d8e7d87eSManos Pitsidianakis     .bdrv_co_pwrite_zeroes              =   throttle_co_pwrite_zeroes,
245d8e7d87eSManos Pitsidianakis     .bdrv_co_pdiscard                   =   throttle_co_pdiscard,
246d8e7d87eSManos Pitsidianakis 
247d8e7d87eSManos Pitsidianakis     .bdrv_attach_aio_context            =   throttle_attach_aio_context,
248d8e7d87eSManos Pitsidianakis     .bdrv_detach_aio_context            =   throttle_detach_aio_context,
249d8e7d87eSManos Pitsidianakis 
250d8e7d87eSManos Pitsidianakis     .bdrv_reopen_prepare                =   throttle_reopen_prepare,
251d8e7d87eSManos Pitsidianakis     .bdrv_reopen_commit                 =   throttle_reopen_commit,
252d8e7d87eSManos Pitsidianakis     .bdrv_reopen_abort                  =   throttle_reopen_abort,
2533e4d0e72SEric Blake     .bdrv_co_block_status               =   bdrv_co_block_status_from_file,
254d8e7d87eSManos Pitsidianakis 
255b867eaa1SManos Pitsidianakis     .bdrv_co_drain_begin                =   throttle_co_drain_begin,
256b867eaa1SManos Pitsidianakis     .bdrv_co_drain_end                  =   throttle_co_drain_end,
257b867eaa1SManos Pitsidianakis 
258d8e7d87eSManos Pitsidianakis     .is_filter                          =   true,
2592654267cSMax Reitz     .strong_runtime_opts                =   throttle_strong_runtime_opts,
260d8e7d87eSManos Pitsidianakis };
261d8e7d87eSManos Pitsidianakis 
262d8e7d87eSManos Pitsidianakis static void bdrv_throttle_init(void)
263d8e7d87eSManos Pitsidianakis {
264d8e7d87eSManos Pitsidianakis     bdrv_register(&bdrv_throttle);
265d8e7d87eSManos Pitsidianakis }
266d8e7d87eSManos Pitsidianakis 
267d8e7d87eSManos Pitsidianakis block_init(bdrv_throttle_init);
268