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" 21 #include "block/throttle-groups.h" 22 #include "qemu/option.h" 23 #include "qemu/throttle-options.h" 24 #include "qapi/error.h" 25 26 static QemuOptsList throttle_opts = { 27 .name = "throttle", 28 .head = QTAILQ_HEAD_INITIALIZER(throttle_opts.head), 29 .desc = { 30 { 31 .name = QEMU_OPT_THROTTLE_GROUP_NAME, 32 .type = QEMU_OPT_STRING, 33 .help = "Name of the throttle group", 34 }, 35 { /* end of list */ } 36 }, 37 }; 38 39 static int throttle_configure_tgm(BlockDriverState *bs, 40 ThrottleGroupMember *tgm, 41 QDict *options, Error **errp) 42 { 43 int ret; 44 const char *group_name; 45 Error *local_err = NULL; 46 QemuOpts *opts = qemu_opts_create(&throttle_opts, NULL, 0, &error_abort); 47 48 qemu_opts_absorb_qdict(opts, options, &local_err); 49 if (local_err) { 50 error_propagate(errp, local_err); 51 ret = -EINVAL; 52 goto fin; 53 } 54 55 group_name = qemu_opt_get(opts, QEMU_OPT_THROTTLE_GROUP_NAME); 56 if (!group_name) { 57 error_setg(errp, "Please specify a throttle group"); 58 ret = -EINVAL; 59 goto fin; 60 } else if (!throttle_group_exists(group_name)) { 61 error_setg(errp, "Throttle group '%s' does not exist", group_name); 62 ret = -EINVAL; 63 goto fin; 64 } 65 66 /* Register membership to group with name group_name */ 67 throttle_group_register_tgm(tgm, group_name, bdrv_get_aio_context(bs)); 68 ret = 0; 69 fin: 70 qemu_opts_del(opts); 71 return ret; 72 } 73 74 static int throttle_open(BlockDriverState *bs, QDict *options, 75 int flags, Error **errp) 76 { 77 ThrottleGroupMember *tgm = bs->opaque; 78 79 bs->file = bdrv_open_child(NULL, options, "file", bs, 80 &child_file, false, errp); 81 if (!bs->file) { 82 return -EINVAL; 83 } 84 bs->supported_write_flags = bs->file->bs->supported_write_flags | 85 BDRV_REQ_WRITE_UNCHANGED; 86 bs->supported_zero_flags = bs->file->bs->supported_zero_flags | 87 BDRV_REQ_WRITE_UNCHANGED; 88 89 return throttle_configure_tgm(bs, tgm, options, errp); 90 } 91 92 static void throttle_close(BlockDriverState *bs) 93 { 94 ThrottleGroupMember *tgm = bs->opaque; 95 throttle_group_unregister_tgm(tgm); 96 } 97 98 99 static int64_t throttle_getlength(BlockDriverState *bs) 100 { 101 return bdrv_getlength(bs->file->bs); 102 } 103 104 static int coroutine_fn throttle_co_preadv(BlockDriverState *bs, 105 uint64_t offset, uint64_t bytes, 106 QEMUIOVector *qiov, int flags) 107 { 108 109 ThrottleGroupMember *tgm = bs->opaque; 110 throttle_group_co_io_limits_intercept(tgm, bytes, false); 111 112 return bdrv_co_preadv(bs->file, offset, bytes, qiov, flags); 113 } 114 115 static int coroutine_fn throttle_co_pwritev(BlockDriverState *bs, 116 uint64_t offset, uint64_t bytes, 117 QEMUIOVector *qiov, int flags) 118 { 119 ThrottleGroupMember *tgm = bs->opaque; 120 throttle_group_co_io_limits_intercept(tgm, bytes, true); 121 122 return bdrv_co_pwritev(bs->file, offset, bytes, qiov, flags); 123 } 124 125 static int coroutine_fn throttle_co_pwrite_zeroes(BlockDriverState *bs, 126 int64_t offset, int bytes, 127 BdrvRequestFlags flags) 128 { 129 ThrottleGroupMember *tgm = bs->opaque; 130 throttle_group_co_io_limits_intercept(tgm, bytes, true); 131 132 return bdrv_co_pwrite_zeroes(bs->file, offset, bytes, flags); 133 } 134 135 static int coroutine_fn throttle_co_pdiscard(BlockDriverState *bs, 136 int64_t offset, int bytes) 137 { 138 ThrottleGroupMember *tgm = bs->opaque; 139 throttle_group_co_io_limits_intercept(tgm, bytes, true); 140 141 return bdrv_co_pdiscard(bs->file->bs, offset, bytes); 142 } 143 144 static int throttle_co_flush(BlockDriverState *bs) 145 { 146 return bdrv_co_flush(bs->file->bs); 147 } 148 149 static void throttle_detach_aio_context(BlockDriverState *bs) 150 { 151 ThrottleGroupMember *tgm = bs->opaque; 152 throttle_group_detach_aio_context(tgm); 153 } 154 155 static void throttle_attach_aio_context(BlockDriverState *bs, 156 AioContext *new_context) 157 { 158 ThrottleGroupMember *tgm = bs->opaque; 159 throttle_group_attach_aio_context(tgm, new_context); 160 } 161 162 static int throttle_reopen_prepare(BDRVReopenState *reopen_state, 163 BlockReopenQueue *queue, Error **errp) 164 { 165 ThrottleGroupMember *tgm; 166 167 assert(reopen_state != NULL); 168 assert(reopen_state->bs != NULL); 169 170 reopen_state->opaque = g_new0(ThrottleGroupMember, 1); 171 tgm = reopen_state->opaque; 172 173 return throttle_configure_tgm(reopen_state->bs, tgm, reopen_state->options, 174 errp); 175 } 176 177 static void throttle_reopen_commit(BDRVReopenState *reopen_state) 178 { 179 ThrottleGroupMember *old_tgm = reopen_state->bs->opaque; 180 ThrottleGroupMember *new_tgm = reopen_state->opaque; 181 182 throttle_group_unregister_tgm(old_tgm); 183 g_free(old_tgm); 184 reopen_state->bs->opaque = new_tgm; 185 reopen_state->opaque = NULL; 186 } 187 188 static void throttle_reopen_abort(BDRVReopenState *reopen_state) 189 { 190 ThrottleGroupMember *tgm = reopen_state->opaque; 191 192 throttle_group_unregister_tgm(tgm); 193 g_free(tgm); 194 reopen_state->opaque = NULL; 195 } 196 197 static bool throttle_recurse_is_first_non_filter(BlockDriverState *bs, 198 BlockDriverState *candidate) 199 { 200 return bdrv_recurse_is_first_non_filter(bs->file->bs, candidate); 201 } 202 203 static void coroutine_fn throttle_co_drain_begin(BlockDriverState *bs) 204 { 205 ThrottleGroupMember *tgm = bs->opaque; 206 if (atomic_fetch_inc(&tgm->io_limits_disabled) == 0) { 207 throttle_group_restart_tgm(tgm); 208 } 209 } 210 211 static void coroutine_fn throttle_co_drain_end(BlockDriverState *bs) 212 { 213 ThrottleGroupMember *tgm = bs->opaque; 214 assert(tgm->io_limits_disabled); 215 atomic_dec(&tgm->io_limits_disabled); 216 } 217 218 static BlockDriver bdrv_throttle = { 219 .format_name = "throttle", 220 .instance_size = sizeof(ThrottleGroupMember), 221 222 .bdrv_open = throttle_open, 223 .bdrv_close = throttle_close, 224 .bdrv_co_flush = throttle_co_flush, 225 226 .bdrv_child_perm = bdrv_filter_default_perms, 227 228 .bdrv_getlength = throttle_getlength, 229 230 .bdrv_co_preadv = throttle_co_preadv, 231 .bdrv_co_pwritev = throttle_co_pwritev, 232 233 .bdrv_co_pwrite_zeroes = throttle_co_pwrite_zeroes, 234 .bdrv_co_pdiscard = throttle_co_pdiscard, 235 236 .bdrv_recurse_is_first_non_filter = throttle_recurse_is_first_non_filter, 237 238 .bdrv_attach_aio_context = throttle_attach_aio_context, 239 .bdrv_detach_aio_context = throttle_detach_aio_context, 240 241 .bdrv_reopen_prepare = throttle_reopen_prepare, 242 .bdrv_reopen_commit = throttle_reopen_commit, 243 .bdrv_reopen_abort = throttle_reopen_abort, 244 .bdrv_co_block_status = bdrv_co_block_status_from_file, 245 246 .bdrv_co_drain_begin = throttle_co_drain_begin, 247 .bdrv_co_drain_end = throttle_co_drain_end, 248 249 .is_filter = true, 250 }; 251 252 static void bdrv_throttle_init(void) 253 { 254 bdrv_register(&bdrv_throttle); 255 } 256 257 block_init(bdrv_throttle_init); 258