Manos Pitsidianakis | d8e7d87 | 2017-08-25 16:20:27 +0300 | [diff] [blame] | 1 | /* |
| 2 | * QEMU block throttling filter driver infrastructure |
| 3 | * |
| 4 | * Copyright (c) 2017 Manos Pitsidianakis |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License as |
| 8 | * published by the Free Software Foundation; either version 2 or |
| 9 | * (at your option) version 3 of the License. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, see <http://www.gnu.org/licenses/>. |
| 18 | */ |
| 19 | |
| 20 | #include "qemu/osdep.h" |
Markus Armbruster | e2c1c34 | 2022-12-21 14:35:49 +0100 | [diff] [blame] | 21 | #include "block/block-io.h" |
| 22 | #include "block/block_int.h" |
Manos Pitsidianakis | d8e7d87 | 2017-08-25 16:20:27 +0300 | [diff] [blame] | 23 | #include "block/throttle-groups.h" |
Markus Armbruster | 0b8fa32 | 2019-05-23 16:35:07 +0200 | [diff] [blame] | 24 | #include "qemu/module.h" |
Markus Armbruster | 922a01a | 2018-02-01 12:18:46 +0100 | [diff] [blame] | 25 | #include "qemu/option.h" |
Manos Pitsidianakis | d8e7d87 | 2017-08-25 16:20:27 +0300 | [diff] [blame] | 26 | #include "qemu/throttle-options.h" |
| 27 | #include "qapi/error.h" |
| 28 | |
| 29 | static QemuOptsList throttle_opts = { |
| 30 | .name = "throttle", |
| 31 | .head = QTAILQ_HEAD_INITIALIZER(throttle_opts.head), |
| 32 | .desc = { |
| 33 | { |
| 34 | .name = QEMU_OPT_THROTTLE_GROUP_NAME, |
| 35 | .type = QEMU_OPT_STRING, |
| 36 | .help = "Name of the throttle group", |
| 37 | }, |
| 38 | { /* end of list */ } |
| 39 | }, |
| 40 | }; |
| 41 | |
Alberto Garcia | bc33c04 | 2018-06-08 18:15:36 +0300 | [diff] [blame] | 42 | /* |
| 43 | * If this function succeeds then the throttle group name is stored in |
| 44 | * @group and must be freed by the caller. |
| 45 | * If there's an error then @group remains unmodified. |
| 46 | */ |
| 47 | static int throttle_parse_options(QDict *options, char **group, Error **errp) |
Manos Pitsidianakis | d8e7d87 | 2017-08-25 16:20:27 +0300 | [diff] [blame] | 48 | { |
| 49 | int ret; |
| 50 | const char *group_name; |
Manos Pitsidianakis | d8e7d87 | 2017-08-25 16:20:27 +0300 | [diff] [blame] | 51 | QemuOpts *opts = qemu_opts_create(&throttle_opts, NULL, 0, &error_abort); |
| 52 | |
Markus Armbruster | af175e8 | 2020-07-07 18:06:03 +0200 | [diff] [blame] | 53 | if (!qemu_opts_absorb_qdict(opts, options, errp)) { |
Manos Pitsidianakis | d8e7d87 | 2017-08-25 16:20:27 +0300 | [diff] [blame] | 54 | ret = -EINVAL; |
| 55 | goto fin; |
| 56 | } |
| 57 | |
| 58 | group_name = qemu_opt_get(opts, QEMU_OPT_THROTTLE_GROUP_NAME); |
| 59 | if (!group_name) { |
| 60 | error_setg(errp, "Please specify a throttle group"); |
| 61 | ret = -EINVAL; |
| 62 | goto fin; |
| 63 | } else if (!throttle_group_exists(group_name)) { |
| 64 | error_setg(errp, "Throttle group '%s' does not exist", group_name); |
| 65 | ret = -EINVAL; |
| 66 | goto fin; |
| 67 | } |
| 68 | |
Alberto Garcia | bc33c04 | 2018-06-08 18:15:36 +0300 | [diff] [blame] | 69 | *group = g_strdup(group_name); |
Manos Pitsidianakis | d8e7d87 | 2017-08-25 16:20:27 +0300 | [diff] [blame] | 70 | ret = 0; |
| 71 | fin: |
| 72 | qemu_opts_del(opts); |
| 73 | return ret; |
| 74 | } |
| 75 | |
| 76 | static int throttle_open(BlockDriverState *bs, QDict *options, |
| 77 | int flags, Error **errp) |
| 78 | { |
| 79 | ThrottleGroupMember *tgm = bs->opaque; |
Alberto Garcia | bc33c04 | 2018-06-08 18:15:36 +0300 | [diff] [blame] | 80 | char *group; |
| 81 | int ret; |
Manos Pitsidianakis | d8e7d87 | 2017-08-25 16:20:27 +0300 | [diff] [blame] | 82 | |
Vladimir Sementsov-Ogievskiy | 8393078 | 2022-07-26 23:11:21 +0300 | [diff] [blame] | 83 | ret = bdrv_open_file_child(NULL, options, "file", bs, errp); |
| 84 | if (ret < 0) { |
| 85 | return ret; |
Manos Pitsidianakis | d8e7d87 | 2017-08-25 16:20:27 +0300 | [diff] [blame] | 86 | } |
Max Reitz | 228345b | 2018-04-21 15:29:26 +0200 | [diff] [blame] | 87 | bs->supported_write_flags = bs->file->bs->supported_write_flags | |
| 88 | BDRV_REQ_WRITE_UNCHANGED; |
| 89 | bs->supported_zero_flags = bs->file->bs->supported_zero_flags | |
| 90 | BDRV_REQ_WRITE_UNCHANGED; |
Manos Pitsidianakis | d8e7d87 | 2017-08-25 16:20:27 +0300 | [diff] [blame] | 91 | |
Alberto Garcia | bc33c04 | 2018-06-08 18:15:36 +0300 | [diff] [blame] | 92 | ret = throttle_parse_options(options, &group, errp); |
| 93 | if (ret == 0) { |
| 94 | /* Register membership to group with name group_name */ |
| 95 | throttle_group_register_tgm(tgm, group, bdrv_get_aio_context(bs)); |
| 96 | g_free(group); |
| 97 | } |
| 98 | |
| 99 | return ret; |
Manos Pitsidianakis | d8e7d87 | 2017-08-25 16:20:27 +0300 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | static void throttle_close(BlockDriverState *bs) |
| 103 | { |
| 104 | ThrottleGroupMember *tgm = bs->opaque; |
| 105 | throttle_group_unregister_tgm(tgm); |
| 106 | } |
| 107 | |
| 108 | |
Kevin Wolf | 8ab8140 | 2023-02-03 16:22:02 +0100 | [diff] [blame] | 109 | static int64_t coroutine_fn GRAPH_RDLOCK |
| 110 | throttle_co_getlength(BlockDriverState *bs) |
Manos Pitsidianakis | d8e7d87 | 2017-08-25 16:20:27 +0300 | [diff] [blame] | 111 | { |
Emanuele Giuseppe Esposito | c86422c | 2023-01-13 21:42:04 +0100 | [diff] [blame] | 112 | return bdrv_co_getlength(bs->file->bs); |
Manos Pitsidianakis | d8e7d87 | 2017-08-25 16:20:27 +0300 | [diff] [blame] | 113 | } |
| 114 | |
Kevin Wolf | b9b10c3 | 2023-02-03 16:21:50 +0100 | [diff] [blame] | 115 | static int coroutine_fn GRAPH_RDLOCK |
| 116 | throttle_co_preadv(BlockDriverState *bs, int64_t offset, int64_t bytes, |
| 117 | QEMUIOVector *qiov, BdrvRequestFlags flags) |
Manos Pitsidianakis | d8e7d87 | 2017-08-25 16:20:27 +0300 | [diff] [blame] | 118 | { |
| 119 | |
| 120 | ThrottleGroupMember *tgm = bs->opaque; |
| 121 | throttle_group_co_io_limits_intercept(tgm, bytes, false); |
| 122 | |
| 123 | return bdrv_co_preadv(bs->file, offset, bytes, qiov, flags); |
| 124 | } |
| 125 | |
Kevin Wolf | b9b10c3 | 2023-02-03 16:21:50 +0100 | [diff] [blame] | 126 | static int coroutine_fn GRAPH_RDLOCK |
| 127 | throttle_co_pwritev(BlockDriverState *bs, int64_t offset, int64_t bytes, |
| 128 | QEMUIOVector *qiov, BdrvRequestFlags flags) |
Manos Pitsidianakis | d8e7d87 | 2017-08-25 16:20:27 +0300 | [diff] [blame] | 129 | { |
| 130 | ThrottleGroupMember *tgm = bs->opaque; |
| 131 | throttle_group_co_io_limits_intercept(tgm, bytes, true); |
| 132 | |
| 133 | return bdrv_co_pwritev(bs->file, offset, bytes, qiov, flags); |
| 134 | } |
| 135 | |
Kevin Wolf | abaf8b7 | 2023-02-03 16:21:48 +0100 | [diff] [blame] | 136 | static int coroutine_fn GRAPH_RDLOCK |
| 137 | throttle_co_pwrite_zeroes(BlockDriverState *bs, int64_t offset, int64_t bytes, |
| 138 | BdrvRequestFlags flags) |
Manos Pitsidianakis | d8e7d87 | 2017-08-25 16:20:27 +0300 | [diff] [blame] | 139 | { |
| 140 | ThrottleGroupMember *tgm = bs->opaque; |
| 141 | throttle_group_co_io_limits_intercept(tgm, bytes, true); |
| 142 | |
| 143 | return bdrv_co_pwrite_zeroes(bs->file, offset, bytes, flags); |
| 144 | } |
| 145 | |
Emanuele Giuseppe Esposito | 9a5a1c6 | 2023-02-03 16:21:47 +0100 | [diff] [blame] | 146 | static int coroutine_fn GRAPH_RDLOCK |
| 147 | throttle_co_pdiscard(BlockDriverState *bs, int64_t offset, int64_t bytes) |
Manos Pitsidianakis | d8e7d87 | 2017-08-25 16:20:27 +0300 | [diff] [blame] | 148 | { |
| 149 | ThrottleGroupMember *tgm = bs->opaque; |
| 150 | throttle_group_co_io_limits_intercept(tgm, bytes, true); |
| 151 | |
Fam Zheng | 0b9fd3f | 2018-07-10 14:31:17 +0800 | [diff] [blame] | 152 | return bdrv_co_pdiscard(bs->file, offset, bytes); |
Manos Pitsidianakis | d8e7d87 | 2017-08-25 16:20:27 +0300 | [diff] [blame] | 153 | } |
| 154 | |
Kevin Wolf | b9b10c3 | 2023-02-03 16:21:50 +0100 | [diff] [blame] | 155 | static int coroutine_fn GRAPH_RDLOCK |
| 156 | throttle_co_pwritev_compressed(BlockDriverState *bs, int64_t offset, |
| 157 | int64_t bytes, QEMUIOVector *qiov) |
Max Reitz | e7e754a | 2019-06-12 22:52:27 +0200 | [diff] [blame] | 158 | { |
| 159 | return throttle_co_pwritev(bs, offset, bytes, qiov, |
| 160 | BDRV_REQ_WRITE_COMPRESSED); |
| 161 | } |
| 162 | |
Emanuele Giuseppe Esposito | 8809534 | 2023-02-03 16:21:46 +0100 | [diff] [blame] | 163 | static int coroutine_fn GRAPH_RDLOCK throttle_co_flush(BlockDriverState *bs) |
Manos Pitsidianakis | d8e7d87 | 2017-08-25 16:20:27 +0300 | [diff] [blame] | 164 | { |
| 165 | return bdrv_co_flush(bs->file->bs); |
| 166 | } |
| 167 | |
| 168 | static void throttle_detach_aio_context(BlockDriverState *bs) |
| 169 | { |
| 170 | ThrottleGroupMember *tgm = bs->opaque; |
| 171 | throttle_group_detach_aio_context(tgm); |
| 172 | } |
| 173 | |
| 174 | static void throttle_attach_aio_context(BlockDriverState *bs, |
| 175 | AioContext *new_context) |
| 176 | { |
| 177 | ThrottleGroupMember *tgm = bs->opaque; |
| 178 | throttle_group_attach_aio_context(tgm, new_context); |
| 179 | } |
| 180 | |
| 181 | static int throttle_reopen_prepare(BDRVReopenState *reopen_state, |
| 182 | BlockReopenQueue *queue, Error **errp) |
| 183 | { |
Alberto Garcia | bc33c04 | 2018-06-08 18:15:36 +0300 | [diff] [blame] | 184 | int ret; |
| 185 | char *group = NULL; |
Manos Pitsidianakis | d8e7d87 | 2017-08-25 16:20:27 +0300 | [diff] [blame] | 186 | |
| 187 | assert(reopen_state != NULL); |
| 188 | assert(reopen_state->bs != NULL); |
| 189 | |
Alberto Garcia | bc33c04 | 2018-06-08 18:15:36 +0300 | [diff] [blame] | 190 | ret = throttle_parse_options(reopen_state->options, &group, errp); |
| 191 | reopen_state->opaque = group; |
| 192 | return ret; |
Manos Pitsidianakis | d8e7d87 | 2017-08-25 16:20:27 +0300 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | static void throttle_reopen_commit(BDRVReopenState *reopen_state) |
| 196 | { |
Alberto Garcia | bc33c04 | 2018-06-08 18:15:36 +0300 | [diff] [blame] | 197 | BlockDriverState *bs = reopen_state->bs; |
| 198 | ThrottleGroupMember *tgm = bs->opaque; |
| 199 | char *group = reopen_state->opaque; |
Manos Pitsidianakis | d8e7d87 | 2017-08-25 16:20:27 +0300 | [diff] [blame] | 200 | |
Alberto Garcia | bc33c04 | 2018-06-08 18:15:36 +0300 | [diff] [blame] | 201 | assert(group); |
| 202 | |
| 203 | if (strcmp(group, throttle_group_get_name(tgm))) { |
| 204 | throttle_group_unregister_tgm(tgm); |
| 205 | throttle_group_register_tgm(tgm, group, bdrv_get_aio_context(bs)); |
| 206 | } |
| 207 | g_free(reopen_state->opaque); |
Manos Pitsidianakis | d8e7d87 | 2017-08-25 16:20:27 +0300 | [diff] [blame] | 208 | reopen_state->opaque = NULL; |
| 209 | } |
| 210 | |
| 211 | static void throttle_reopen_abort(BDRVReopenState *reopen_state) |
| 212 | { |
Alberto Garcia | bc33c04 | 2018-06-08 18:15:36 +0300 | [diff] [blame] | 213 | g_free(reopen_state->opaque); |
Manos Pitsidianakis | d8e7d87 | 2017-08-25 16:20:27 +0300 | [diff] [blame] | 214 | reopen_state->opaque = NULL; |
| 215 | } |
| 216 | |
Kevin Wolf | 5e8ac21 | 2022-11-18 18:40:58 +0100 | [diff] [blame] | 217 | static void throttle_drain_begin(BlockDriverState *bs) |
Manos Pitsidianakis | b867eaa | 2017-09-23 14:14:11 +0300 | [diff] [blame] | 218 | { |
| 219 | ThrottleGroupMember *tgm = bs->opaque; |
Stefan Hajnoczi | d73415a | 2020-09-23 11:56:46 +0100 | [diff] [blame] | 220 | if (qatomic_fetch_inc(&tgm->io_limits_disabled) == 0) { |
Manos Pitsidianakis | b867eaa | 2017-09-23 14:14:11 +0300 | [diff] [blame] | 221 | throttle_group_restart_tgm(tgm); |
| 222 | } |
| 223 | } |
| 224 | |
Kevin Wolf | 5e8ac21 | 2022-11-18 18:40:58 +0100 | [diff] [blame] | 225 | static void throttle_drain_end(BlockDriverState *bs) |
Manos Pitsidianakis | b867eaa | 2017-09-23 14:14:11 +0300 | [diff] [blame] | 226 | { |
| 227 | ThrottleGroupMember *tgm = bs->opaque; |
| 228 | assert(tgm->io_limits_disabled); |
Stefan Hajnoczi | d73415a | 2020-09-23 11:56:46 +0100 | [diff] [blame] | 229 | qatomic_dec(&tgm->io_limits_disabled); |
Manos Pitsidianakis | b867eaa | 2017-09-23 14:14:11 +0300 | [diff] [blame] | 230 | } |
| 231 | |
Max Reitz | 2654267 | 2019-02-01 20:29:25 +0100 | [diff] [blame] | 232 | static const char *const throttle_strong_runtime_opts[] = { |
| 233 | QEMU_OPT_THROTTLE_GROUP_NAME, |
| 234 | |
| 235 | NULL |
| 236 | }; |
| 237 | |
Manos Pitsidianakis | d8e7d87 | 2017-08-25 16:20:27 +0300 | [diff] [blame] | 238 | static BlockDriver bdrv_throttle = { |
| 239 | .format_name = "throttle", |
Manos Pitsidianakis | d8e7d87 | 2017-08-25 16:20:27 +0300 | [diff] [blame] | 240 | .instance_size = sizeof(ThrottleGroupMember), |
| 241 | |
Fabiano Rosas | a7328ba | 2018-03-12 19:07:51 -0300 | [diff] [blame] | 242 | .bdrv_open = throttle_open, |
Manos Pitsidianakis | d8e7d87 | 2017-08-25 16:20:27 +0300 | [diff] [blame] | 243 | .bdrv_close = throttle_close, |
| 244 | .bdrv_co_flush = throttle_co_flush, |
| 245 | |
Max Reitz | 69dca43 | 2020-05-13 13:05:39 +0200 | [diff] [blame] | 246 | .bdrv_child_perm = bdrv_default_perms, |
Manos Pitsidianakis | d8e7d87 | 2017-08-25 16:20:27 +0300 | [diff] [blame] | 247 | |
Emanuele Giuseppe Esposito | c86422c | 2023-01-13 21:42:04 +0100 | [diff] [blame] | 248 | .bdrv_co_getlength = throttle_co_getlength, |
Manos Pitsidianakis | d8e7d87 | 2017-08-25 16:20:27 +0300 | [diff] [blame] | 249 | |
| 250 | .bdrv_co_preadv = throttle_co_preadv, |
| 251 | .bdrv_co_pwritev = throttle_co_pwritev, |
| 252 | |
| 253 | .bdrv_co_pwrite_zeroes = throttle_co_pwrite_zeroes, |
| 254 | .bdrv_co_pdiscard = throttle_co_pdiscard, |
Max Reitz | e7e754a | 2019-06-12 22:52:27 +0200 | [diff] [blame] | 255 | .bdrv_co_pwritev_compressed = throttle_co_pwritev_compressed, |
Manos Pitsidianakis | d8e7d87 | 2017-08-25 16:20:27 +0300 | [diff] [blame] | 256 | |
Manos Pitsidianakis | d8e7d87 | 2017-08-25 16:20:27 +0300 | [diff] [blame] | 257 | .bdrv_attach_aio_context = throttle_attach_aio_context, |
| 258 | .bdrv_detach_aio_context = throttle_detach_aio_context, |
| 259 | |
| 260 | .bdrv_reopen_prepare = throttle_reopen_prepare, |
| 261 | .bdrv_reopen_commit = throttle_reopen_commit, |
| 262 | .bdrv_reopen_abort = throttle_reopen_abort, |
Manos Pitsidianakis | d8e7d87 | 2017-08-25 16:20:27 +0300 | [diff] [blame] | 263 | |
Kevin Wolf | 5e8ac21 | 2022-11-18 18:40:58 +0100 | [diff] [blame] | 264 | .bdrv_drain_begin = throttle_drain_begin, |
| 265 | .bdrv_drain_end = throttle_drain_end, |
Manos Pitsidianakis | b867eaa | 2017-09-23 14:14:11 +0300 | [diff] [blame] | 266 | |
Manos Pitsidianakis | d8e7d87 | 2017-08-25 16:20:27 +0300 | [diff] [blame] | 267 | .is_filter = true, |
Max Reitz | 2654267 | 2019-02-01 20:29:25 +0100 | [diff] [blame] | 268 | .strong_runtime_opts = throttle_strong_runtime_opts, |
Manos Pitsidianakis | d8e7d87 | 2017-08-25 16:20:27 +0300 | [diff] [blame] | 269 | }; |
| 270 | |
| 271 | static void bdrv_throttle_init(void) |
| 272 | { |
| 273 | bdrv_register(&bdrv_throttle); |
| 274 | } |
| 275 | |
| 276 | block_init(bdrv_throttle_init); |