]> Git Repo - qemu.git/blob - tests/test-blockjob.c
hw/dma: Do not build the xlnx_dpdma device for the MicroBlaze machines
[qemu.git] / tests / test-blockjob.c
1 /*
2  * Blockjob tests
3  *
4  * Copyright Igalia, S.L. 2016
5  *
6  * Authors:
7  *  Alberto Garcia   <[email protected]>
8  *
9  * This work is licensed under the terms of the GNU LGPL, version 2 or later.
10  * See the COPYING.LIB file in the top-level directory.
11  */
12
13 #include "qemu/osdep.h"
14 #include "qapi/error.h"
15 #include "qemu/main-loop.h"
16 #include "block/blockjob_int.h"
17 #include "sysemu/block-backend.h"
18 #include "qapi/qmp/qdict.h"
19
20 static const BlockJobDriver test_block_job_driver = {
21     .job_driver = {
22         .instance_size = sizeof(BlockJob),
23         .free          = block_job_free,
24         .user_resume   = block_job_user_resume,
25         .drain         = block_job_drain,
26     },
27 };
28
29 static void block_job_cb(void *opaque, int ret)
30 {
31 }
32
33 static BlockJob *mk_job(BlockBackend *blk, const char *id,
34                         const BlockJobDriver *drv, bool should_succeed,
35                         int flags)
36 {
37     BlockJob *job;
38     Error *errp = NULL;
39
40     job = block_job_create(id, drv, NULL, blk_bs(blk),
41                            0, BLK_PERM_ALL, 0, flags, block_job_cb,
42                            NULL, &errp);
43     if (should_succeed) {
44         g_assert_null(errp);
45         g_assert_nonnull(job);
46         if (id) {
47             g_assert_cmpstr(job->job.id, ==, id);
48         } else {
49             g_assert_cmpstr(job->job.id, ==, blk_name(blk));
50         }
51     } else {
52         g_assert_nonnull(errp);
53         g_assert_null(job);
54         error_free(errp);
55     }
56
57     return job;
58 }
59
60 static BlockJob *do_test_id(BlockBackend *blk, const char *id,
61                             bool should_succeed)
62 {
63     return mk_job(blk, id, &test_block_job_driver,
64                   should_succeed, JOB_DEFAULT);
65 }
66
67 /* This creates a BlockBackend (optionally with a name) with a
68  * BlockDriverState inserted. */
69 static BlockBackend *create_blk(const char *name)
70 {
71     /* No I/O is performed on this device */
72     BlockBackend *blk = blk_new(qemu_get_aio_context(), 0, BLK_PERM_ALL);
73     BlockDriverState *bs;
74
75     QDict *opt = qdict_new();
76     qdict_put_str(opt, "file.read-zeroes", "on");
77     bs = bdrv_open("null-co://", NULL, opt, 0, &error_abort);
78     g_assert_nonnull(bs);
79
80     blk_insert_bs(blk, bs, &error_abort);
81     bdrv_unref(bs);
82
83     if (name) {
84         Error *errp = NULL;
85         monitor_add_blk(blk, name, &errp);
86         g_assert_null(errp);
87     }
88
89     return blk;
90 }
91
92 /* This destroys the backend */
93 static void destroy_blk(BlockBackend *blk)
94 {
95     if (blk_name(blk)[0] != '\0') {
96         monitor_remove_blk(blk);
97     }
98
99     blk_remove_bs(blk);
100     blk_unref(blk);
101 }
102
103 static void test_job_ids(void)
104 {
105     BlockBackend *blk[3];
106     BlockJob *job[3];
107
108     blk[0] = create_blk(NULL);
109     blk[1] = create_blk("drive1");
110     blk[2] = create_blk("drive2");
111
112     /* No job ID provided and the block backend has no name */
113     job[0] = do_test_id(blk[0], NULL, false);
114
115     /* These are all invalid job IDs */
116     job[0] = do_test_id(blk[0], "0id", false);
117     job[0] = do_test_id(blk[0], "",    false);
118     job[0] = do_test_id(blk[0], "   ", false);
119     job[0] = do_test_id(blk[0], "123", false);
120     job[0] = do_test_id(blk[0], "_id", false);
121     job[0] = do_test_id(blk[0], "-id", false);
122     job[0] = do_test_id(blk[0], ".id", false);
123     job[0] = do_test_id(blk[0], "#id", false);
124
125     /* This one is valid */
126     job[0] = do_test_id(blk[0], "id0", true);
127
128     /* We can have two jobs in the same BDS */
129     job[1] = do_test_id(blk[0], "id1", true);
130     job_early_fail(&job[1]->job);
131
132     /* Duplicate job IDs are not allowed */
133     job[1] = do_test_id(blk[1], "id0", false);
134
135     /* But once job[0] finishes we can reuse its ID */
136     job_early_fail(&job[0]->job);
137     job[1] = do_test_id(blk[1], "id0", true);
138
139     /* No job ID specified, defaults to the backend name ('drive1') */
140     job_early_fail(&job[1]->job);
141     job[1] = do_test_id(blk[1], NULL, true);
142
143     /* Duplicate job ID */
144     job[2] = do_test_id(blk[2], "drive1", false);
145
146     /* The ID of job[2] would default to 'drive2' but it is already in use */
147     job[0] = do_test_id(blk[0], "drive2", true);
148     job[2] = do_test_id(blk[2], NULL, false);
149
150     /* This one is valid */
151     job[2] = do_test_id(blk[2], "id_2", true);
152
153     job_early_fail(&job[0]->job);
154     job_early_fail(&job[1]->job);
155     job_early_fail(&job[2]->job);
156
157     destroy_blk(blk[0]);
158     destroy_blk(blk[1]);
159     destroy_blk(blk[2]);
160 }
161
162 typedef struct CancelJob {
163     BlockJob common;
164     BlockBackend *blk;
165     bool should_converge;
166     bool should_complete;
167 } CancelJob;
168
169 static void cancel_job_complete(Job *job, Error **errp)
170 {
171     CancelJob *s = container_of(job, CancelJob, common.job);
172     s->should_complete = true;
173 }
174
175 static int coroutine_fn cancel_job_run(Job *job, Error **errp)
176 {
177     CancelJob *s = container_of(job, CancelJob, common.job);
178
179     while (!s->should_complete) {
180         if (job_is_cancelled(&s->common.job)) {
181             return 0;
182         }
183
184         if (!job_is_ready(&s->common.job) && s->should_converge) {
185             job_transition_to_ready(&s->common.job);
186         }
187
188         job_sleep_ns(&s->common.job, 100000);
189     }
190
191     return 0;
192 }
193
194 static const BlockJobDriver test_cancel_driver = {
195     .job_driver = {
196         .instance_size = sizeof(CancelJob),
197         .free          = block_job_free,
198         .user_resume   = block_job_user_resume,
199         .drain         = block_job_drain,
200         .run           = cancel_job_run,
201         .complete      = cancel_job_complete,
202     },
203 };
204
205 static CancelJob *create_common(Job **pjob)
206 {
207     BlockBackend *blk;
208     Job *job;
209     BlockJob *bjob;
210     CancelJob *s;
211
212     blk = create_blk(NULL);
213     bjob = mk_job(blk, "Steve", &test_cancel_driver, true,
214                   JOB_MANUAL_FINALIZE | JOB_MANUAL_DISMISS);
215     job = &bjob->job;
216     job_ref(job);
217     assert(job->status == JOB_STATUS_CREATED);
218     s = container_of(bjob, CancelJob, common);
219     s->blk = blk;
220
221     *pjob = job;
222     return s;
223 }
224
225 static void cancel_common(CancelJob *s)
226 {
227     BlockJob *job = &s->common;
228     BlockBackend *blk = s->blk;
229     JobStatus sts = job->job.status;
230     AioContext *ctx;
231
232     ctx = job->job.aio_context;
233     aio_context_acquire(ctx);
234
235     job_cancel_sync(&job->job);
236     if (sts != JOB_STATUS_CREATED && sts != JOB_STATUS_CONCLUDED) {
237         Job *dummy = &job->job;
238         job_dismiss(&dummy, &error_abort);
239     }
240     assert(job->job.status == JOB_STATUS_NULL);
241     job_unref(&job->job);
242     destroy_blk(blk);
243
244     aio_context_release(ctx);
245 }
246
247 static void test_cancel_created(void)
248 {
249     Job *job;
250     CancelJob *s;
251
252     s = create_common(&job);
253     cancel_common(s);
254 }
255
256 static void test_cancel_running(void)
257 {
258     Job *job;
259     CancelJob *s;
260
261     s = create_common(&job);
262
263     job_start(job);
264     assert(job->status == JOB_STATUS_RUNNING);
265
266     cancel_common(s);
267 }
268
269 static void test_cancel_paused(void)
270 {
271     Job *job;
272     CancelJob *s;
273
274     s = create_common(&job);
275
276     job_start(job);
277     assert(job->status == JOB_STATUS_RUNNING);
278
279     job_user_pause(job, &error_abort);
280     job_enter(job);
281     assert(job->status == JOB_STATUS_PAUSED);
282
283     cancel_common(s);
284 }
285
286 static void test_cancel_ready(void)
287 {
288     Job *job;
289     CancelJob *s;
290
291     s = create_common(&job);
292
293     job_start(job);
294     assert(job->status == JOB_STATUS_RUNNING);
295
296     s->should_converge = true;
297     job_enter(job);
298     assert(job->status == JOB_STATUS_READY);
299
300     cancel_common(s);
301 }
302
303 static void test_cancel_standby(void)
304 {
305     Job *job;
306     CancelJob *s;
307
308     s = create_common(&job);
309
310     job_start(job);
311     assert(job->status == JOB_STATUS_RUNNING);
312
313     s->should_converge = true;
314     job_enter(job);
315     assert(job->status == JOB_STATUS_READY);
316
317     job_user_pause(job, &error_abort);
318     job_enter(job);
319     assert(job->status == JOB_STATUS_STANDBY);
320
321     cancel_common(s);
322 }
323
324 static void test_cancel_pending(void)
325 {
326     Job *job;
327     CancelJob *s;
328
329     s = create_common(&job);
330
331     job_start(job);
332     assert(job->status == JOB_STATUS_RUNNING);
333
334     s->should_converge = true;
335     job_enter(job);
336     assert(job->status == JOB_STATUS_READY);
337
338     job_complete(job, &error_abort);
339     job_enter(job);
340     while (!job->deferred_to_main_loop) {
341         aio_poll(qemu_get_aio_context(), true);
342     }
343     assert(job->status == JOB_STATUS_READY);
344     aio_poll(qemu_get_aio_context(), true);
345     assert(job->status == JOB_STATUS_PENDING);
346
347     cancel_common(s);
348 }
349
350 static void test_cancel_concluded(void)
351 {
352     Job *job;
353     CancelJob *s;
354
355     s = create_common(&job);
356
357     job_start(job);
358     assert(job->status == JOB_STATUS_RUNNING);
359
360     s->should_converge = true;
361     job_enter(job);
362     assert(job->status == JOB_STATUS_READY);
363
364     job_complete(job, &error_abort);
365     job_enter(job);
366     while (!job->deferred_to_main_loop) {
367         aio_poll(qemu_get_aio_context(), true);
368     }
369     assert(job->status == JOB_STATUS_READY);
370     aio_poll(qemu_get_aio_context(), true);
371     assert(job->status == JOB_STATUS_PENDING);
372
373     job_finalize(job, &error_abort);
374     assert(job->status == JOB_STATUS_CONCLUDED);
375
376     cancel_common(s);
377 }
378
379 int main(int argc, char **argv)
380 {
381     qemu_init_main_loop(&error_abort);
382     bdrv_init();
383
384     g_test_init(&argc, &argv, NULL);
385     g_test_add_func("/blockjob/ids", test_job_ids);
386     g_test_add_func("/blockjob/cancel/created", test_cancel_created);
387     g_test_add_func("/blockjob/cancel/running", test_cancel_running);
388     g_test_add_func("/blockjob/cancel/paused", test_cancel_paused);
389     g_test_add_func("/blockjob/cancel/ready", test_cancel_ready);
390     g_test_add_func("/blockjob/cancel/standby", test_cancel_standby);
391     g_test_add_func("/blockjob/cancel/pending", test_cancel_pending);
392     g_test_add_func("/blockjob/cancel/concluded", test_cancel_concluded);
393     return g_test_run();
394 }
This page took 0.057985 seconds and 4 git commands to generate.