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