xref: /openbmc/qemu/block/throttle.c (revision e7bbc9b1)
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 /*
40  * If this function succeeds then the throttle group name is stored in
41  * @group and must be freed by the caller.
42  * If there's an error then @group remains unmodified.
43  */
44 static int throttle_parse_options(QDict *options, char **group, Error **errp)
45 {
46     int ret;
47     const char *group_name;
48     Error *local_err = NULL;
49     QemuOpts *opts = qemu_opts_create(&throttle_opts, NULL, 0, &error_abort);
50 
51     qemu_opts_absorb_qdict(opts, options, &local_err);
52     if (local_err) {
53         error_propagate(errp, local_err);
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 
69     *group = g_strdup(group_name);
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;
80     char *group;
81     int ret;
82 
83     bs->file = bdrv_open_child(NULL, options, "file", bs,
84                                &child_file, false, errp);
85     if (!bs->file) {
86         return -EINVAL;
87     }
88     bs->supported_write_flags = bs->file->bs->supported_write_flags |
89                                 BDRV_REQ_WRITE_UNCHANGED;
90     bs->supported_zero_flags = bs->file->bs->supported_zero_flags |
91                                BDRV_REQ_WRITE_UNCHANGED;
92 
93     ret = throttle_parse_options(options, &group, errp);
94     if (ret == 0) {
95         /* Register membership to group with name group_name */
96         throttle_group_register_tgm(tgm, group, bdrv_get_aio_context(bs));
97         g_free(group);
98     }
99 
100     return ret;
101 }
102 
103 static void throttle_close(BlockDriverState *bs)
104 {
105     ThrottleGroupMember *tgm = bs->opaque;
106     throttle_group_unregister_tgm(tgm);
107 }
108 
109 
110 static int64_t throttle_getlength(BlockDriverState *bs)
111 {
112     return bdrv_getlength(bs->file->bs);
113 }
114 
115 static int coroutine_fn throttle_co_preadv(BlockDriverState *bs,
116                                            uint64_t offset, uint64_t bytes,
117                                            QEMUIOVector *qiov, int flags)
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 
126 static int coroutine_fn throttle_co_pwritev(BlockDriverState *bs,
127                                             uint64_t offset, uint64_t bytes,
128                                             QEMUIOVector *qiov, int flags)
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 
136 static int coroutine_fn throttle_co_pwrite_zeroes(BlockDriverState *bs,
137                                                   int64_t offset, int bytes,
138                                                   BdrvRequestFlags flags)
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 
146 static int coroutine_fn throttle_co_pdiscard(BlockDriverState *bs,
147                                              int64_t offset, int bytes)
148 {
149     ThrottleGroupMember *tgm = bs->opaque;
150     throttle_group_co_io_limits_intercept(tgm, bytes, true);
151 
152     return bdrv_co_pdiscard(bs->file, offset, bytes);
153 }
154 
155 static int throttle_co_flush(BlockDriverState *bs)
156 {
157     return bdrv_co_flush(bs->file->bs);
158 }
159 
160 static void throttle_detach_aio_context(BlockDriverState *bs)
161 {
162     ThrottleGroupMember *tgm = bs->opaque;
163     throttle_group_detach_aio_context(tgm);
164 }
165 
166 static void throttle_attach_aio_context(BlockDriverState *bs,
167                                         AioContext *new_context)
168 {
169     ThrottleGroupMember *tgm = bs->opaque;
170     throttle_group_attach_aio_context(tgm, new_context);
171 }
172 
173 static int throttle_reopen_prepare(BDRVReopenState *reopen_state,
174                                    BlockReopenQueue *queue, Error **errp)
175 {
176     int ret;
177     char *group = NULL;
178 
179     assert(reopen_state != NULL);
180     assert(reopen_state->bs != NULL);
181 
182     ret = throttle_parse_options(reopen_state->options, &group, errp);
183     reopen_state->opaque = group;
184     return ret;
185 }
186 
187 static void throttle_reopen_commit(BDRVReopenState *reopen_state)
188 {
189     BlockDriverState *bs = reopen_state->bs;
190     ThrottleGroupMember *tgm = bs->opaque;
191     char *group = reopen_state->opaque;
192 
193     assert(group);
194 
195     if (strcmp(group, throttle_group_get_name(tgm))) {
196         throttle_group_unregister_tgm(tgm);
197         throttle_group_register_tgm(tgm, group, bdrv_get_aio_context(bs));
198     }
199     g_free(reopen_state->opaque);
200     reopen_state->opaque = NULL;
201 }
202 
203 static void throttle_reopen_abort(BDRVReopenState *reopen_state)
204 {
205     g_free(reopen_state->opaque);
206     reopen_state->opaque = NULL;
207 }
208 
209 static bool throttle_recurse_is_first_non_filter(BlockDriverState *bs,
210                                                  BlockDriverState *candidate)
211 {
212     return bdrv_recurse_is_first_non_filter(bs->file->bs, candidate);
213 }
214 
215 static void coroutine_fn throttle_co_drain_begin(BlockDriverState *bs)
216 {
217     ThrottleGroupMember *tgm = bs->opaque;
218     if (atomic_fetch_inc(&tgm->io_limits_disabled) == 0) {
219         throttle_group_restart_tgm(tgm);
220     }
221 }
222 
223 static void coroutine_fn throttle_co_drain_end(BlockDriverState *bs)
224 {
225     ThrottleGroupMember *tgm = bs->opaque;
226     assert(tgm->io_limits_disabled);
227     atomic_dec(&tgm->io_limits_disabled);
228 }
229 
230 static BlockDriver bdrv_throttle = {
231     .format_name                        =   "throttle",
232     .instance_size                      =   sizeof(ThrottleGroupMember),
233 
234     .bdrv_open                          =   throttle_open,
235     .bdrv_close                         =   throttle_close,
236     .bdrv_co_flush                      =   throttle_co_flush,
237 
238     .bdrv_child_perm                    =   bdrv_filter_default_perms,
239 
240     .bdrv_getlength                     =   throttle_getlength,
241 
242     .bdrv_co_preadv                     =   throttle_co_preadv,
243     .bdrv_co_pwritev                    =   throttle_co_pwritev,
244 
245     .bdrv_co_pwrite_zeroes              =   throttle_co_pwrite_zeroes,
246     .bdrv_co_pdiscard                   =   throttle_co_pdiscard,
247 
248     .bdrv_recurse_is_first_non_filter   =   throttle_recurse_is_first_non_filter,
249 
250     .bdrv_attach_aio_context            =   throttle_attach_aio_context,
251     .bdrv_detach_aio_context            =   throttle_detach_aio_context,
252 
253     .bdrv_reopen_prepare                =   throttle_reopen_prepare,
254     .bdrv_reopen_commit                 =   throttle_reopen_commit,
255     .bdrv_reopen_abort                  =   throttle_reopen_abort,
256     .bdrv_co_block_status               =   bdrv_co_block_status_from_file,
257 
258     .bdrv_co_drain_begin                =   throttle_co_drain_begin,
259     .bdrv_co_drain_end                  =   throttle_co_drain_end,
260 
261     .is_filter                          =   true,
262 };
263 
264 static void bdrv_throttle_init(void)
265 {
266     bdrv_register(&bdrv_throttle);
267 }
268 
269 block_init(bdrv_throttle_init);
270