Block jobs & NBD patches

v2: - add arguments to QEMUMachine constructor in test, to make it work
       on arm in gitlab pipeline
     - use bdrv_inc_in_flight() / bdrv_dec_in_flight() instead of direct
       manipulation with bs->in_flight
 
 - add new options for copy-before-write filter
 - new trace points for NBD
 - prefer unsigned type for some 'in_flight' fields
 -----BEGIN PGP SIGNATURE-----
 
 iQIzBAABCgAdFiEEi5wmzbL9FHyIDoahVh8kwfGfefsFAmK8BqkACgkQVh8kwfGf
 efuiMw/+P9FFLfGFSjVP+LYeT0Ah6kN1ypCMQzIk3Qq/J6qgMZhtRqpQoZOfIFQL
 U9fGmEtQZ7gvEMD/nJToL6uOYlnQfPxDcA6GrRwWWE3rcFiPK4J0q2LlnPLn4QaU
 W/qag5l/QnZfLlj/iV6neWOEvqdnvY1L8IS+T8xV6N0iBYlwgMC/6FGshQwehzcV
 T5F1qPGB0vjFDjf92LFPEsvsFFHjHIVPwOyJMvF64QtSk57utikq/la9PI/yA9AH
 Ll4mNQuZKx6rSI5wE6b21jc8iOUvaoHdPSEDQZfNILSdgGdiKvFwE51y+baGnIAD
 TpjxfG59q0jyGxMjQVxMRSFaxAC4+Mqi82diSPv4xbiUdsE4byJH0oENn4z7+wAv
 EvjuU9yx4FfHHltoUNwfn2pv00o/ELdZIoBNmW36rPxSGZMvfVfRtuBL7XWNUFbW
 Fr4BwsjC4KfIxb16QTBGhXVv6grxdlwoU9N23npdi0YpW1ftZzXGDa85+gINQ807
 eK/gP/OtYPwIql0bgmLiuaRNzC9psmQOO6vQbdvd/e4BEVWkxiez37+e+zFMStmL
 OAL8rS6jckUoxVZjCYFEWg97XOobLUIhQxt9Fwh2omMDGKTwv861ghUAivxSWs93
 IRNxfwqNPxnpDDXjnK1ayZgU08IL98AUYVKcPN1y8JvEhB4Hr1k=
 =ndKk
 -----END PGP SIGNATURE-----

Merge tag 'pull-block-2022-06-14-v2' of https://gitlab.com/vsementsov/qemu into staging

Block jobs & NBD patches

v2: - add arguments to QEMUMachine constructor in test, to make it work
      on arm in gitlab pipeline
    - use bdrv_inc_in_flight() / bdrv_dec_in_flight() instead of direct
      manipulation with bs->in_flight

- add new options for copy-before-write filter
- new trace points for NBD
- prefer unsigned type for some 'in_flight' fields

# -----BEGIN PGP SIGNATURE-----
#
# iQIzBAABCgAdFiEEi5wmzbL9FHyIDoahVh8kwfGfefsFAmK8BqkACgkQVh8kwfGf
# efuiMw/+P9FFLfGFSjVP+LYeT0Ah6kN1ypCMQzIk3Qq/J6qgMZhtRqpQoZOfIFQL
# U9fGmEtQZ7gvEMD/nJToL6uOYlnQfPxDcA6GrRwWWE3rcFiPK4J0q2LlnPLn4QaU
# W/qag5l/QnZfLlj/iV6neWOEvqdnvY1L8IS+T8xV6N0iBYlwgMC/6FGshQwehzcV
# T5F1qPGB0vjFDjf92LFPEsvsFFHjHIVPwOyJMvF64QtSk57utikq/la9PI/yA9AH
# Ll4mNQuZKx6rSI5wE6b21jc8iOUvaoHdPSEDQZfNILSdgGdiKvFwE51y+baGnIAD
# TpjxfG59q0jyGxMjQVxMRSFaxAC4+Mqi82diSPv4xbiUdsE4byJH0oENn4z7+wAv
# EvjuU9yx4FfHHltoUNwfn2pv00o/ELdZIoBNmW36rPxSGZMvfVfRtuBL7XWNUFbW
# Fr4BwsjC4KfIxb16QTBGhXVv6grxdlwoU9N23npdi0YpW1ftZzXGDa85+gINQ807
# eK/gP/OtYPwIql0bgmLiuaRNzC9psmQOO6vQbdvd/e4BEVWkxiez37+e+zFMStmL
# OAL8rS6jckUoxVZjCYFEWg97XOobLUIhQxt9Fwh2omMDGKTwv861ghUAivxSWs93
# IRNxfwqNPxnpDDXjnK1ayZgU08IL98AUYVKcPN1y8JvEhB4Hr1k=
# =ndKk
# -----END PGP SIGNATURE-----
# gpg: Signature made Wed 29 Jun 2022 01:30:41 PM +0530
# gpg:                using RSA key 8B9C26CDB2FD147C880E86A1561F24C1F19F79FB
# gpg: Good signature from "Vladimir Sementsov-Ogievskiy <vsementsov@yandex-team.ru>" [unknown]
# gpg:                 aka "Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>" [unknown]
# gpg: WARNING: This key is not certified with a trusted signature!
# gpg:          There is no indication that the signature belongs to the owner.
# Primary key fingerprint: 8B9C 26CD B2FD 147C 880E  86A1 561F 24C1 F19F 79FB

* tag 'pull-block-2022-06-14-v2' of https://gitlab.com/vsementsov/qemu:
  block: use 'unsigned' for in_flight field on driver state
  nbd: trace long NBD operations
  iotests: copy-before-write: add cases for cbw-timeout option
  block/copy-before-write: implement cbw-timeout option
  block/block-copy: block_copy(): add timeout_ns parameter
  util: add qemu-co-timeout
  iotests: add copy-before-write: on-cbw-error tests
  block/copy-before-write: add on-cbw-error open parameter
  block/copy-before-write: refactor option parsing

Signed-off-by: Richard Henderson <richard.henderson@linaro.org>
This commit is contained in:
Richard Henderson 2022-06-29 21:35:27 +05:30
commit f96d4e0f60
15 changed files with 483 additions and 43 deletions

View File

@ -883,23 +883,42 @@ static int coroutine_fn block_copy_common(BlockCopyCallState *call_state)
return ret;
}
int coroutine_fn block_copy(BlockCopyState *s, int64_t start, int64_t bytes,
bool ignore_ratelimit)
static void coroutine_fn block_copy_async_co_entry(void *opaque)
{
BlockCopyCallState call_state = {
block_copy_common(opaque);
}
int coroutine_fn block_copy(BlockCopyState *s, int64_t start, int64_t bytes,
bool ignore_ratelimit, uint64_t timeout_ns,
BlockCopyAsyncCallbackFunc cb,
void *cb_opaque)
{
int ret;
BlockCopyCallState *call_state = g_new(BlockCopyCallState, 1);
*call_state = (BlockCopyCallState) {
.s = s,
.offset = start,
.bytes = bytes,
.ignore_ratelimit = ignore_ratelimit,
.max_workers = BLOCK_COPY_MAX_WORKERS,
.cb = cb,
.cb_opaque = cb_opaque,
};
return block_copy_common(&call_state);
}
ret = qemu_co_timeout(block_copy_async_co_entry, call_state, timeout_ns,
g_free);
if (ret < 0) {
assert(ret == -ETIMEDOUT);
block_copy_call_cancel(call_state);
/* call_state will be freed by running coroutine. */
return ret;
}
static void coroutine_fn block_copy_async_co_entry(void *opaque)
{
block_copy_common(opaque);
ret = call_state->ret;
g_free(call_state);
return ret;
}
BlockCopyCallState *block_copy_async(BlockCopyState *s,

View File

@ -24,6 +24,7 @@
*/
#include "qemu/osdep.h"
#include "qapi/qmp/qjson.h"
#include "sysemu/block-backend.h"
#include "qemu/cutils.h"
@ -40,6 +41,8 @@
typedef struct BDRVCopyBeforeWriteState {
BlockCopyState *bcs;
BdrvChild *target;
OnCbwError on_cbw_error;
uint32_t cbw_timeout_ns;
/*
* @lock: protects access to @access_bitmap, @done_bitmap and
@ -64,6 +67,14 @@ typedef struct BDRVCopyBeforeWriteState {
* node. These areas must not be rewritten by guest.
*/
BlockReqList frozen_read_reqs;
/*
* @snapshot_error is normally zero. But on first copy-before-write failure
* when @on_cbw_error == ON_CBW_ERROR_BREAK_SNAPSHOT, @snapshot_error takes
* value of this error (<0). After that all in-flight and further
* snapshot-API requests will fail with that error.
*/
int snapshot_error;
} BDRVCopyBeforeWriteState;
static coroutine_fn int cbw_co_preadv(
@ -73,6 +84,13 @@ static coroutine_fn int cbw_co_preadv(
return bdrv_co_preadv(bs->file, offset, bytes, qiov, flags);
}
static void block_copy_cb(void *opaque)
{
BlockDriverState *bs = opaque;
bdrv_dec_in_flight(bs);
}
/*
* Do copy-before-write operation.
*
@ -94,16 +112,36 @@ static coroutine_fn int cbw_do_copy_before_write(BlockDriverState *bs,
return 0;
}
if (s->snapshot_error) {
return 0;
}
off = QEMU_ALIGN_DOWN(offset, cluster_size);
end = QEMU_ALIGN_UP(offset + bytes, cluster_size);
ret = block_copy(s->bcs, off, end - off, true);
if (ret < 0) {
/*
* Increase in_flight, so that in case of timed-out block-copy, the
* remaining background block_copy() request (which can't be immediately
* cancelled by timeout) is presented in bs->in_flight. This way we are
* sure that on bs close() we'll previously wait for all timed-out but yet
* running block_copy calls.
*/
bdrv_inc_in_flight(bs);
ret = block_copy(s->bcs, off, end - off, true, s->cbw_timeout_ns,
block_copy_cb, bs);
if (ret < 0 && s->on_cbw_error == ON_CBW_ERROR_BREAK_GUEST_WRITE) {
return ret;
}
WITH_QEMU_LOCK_GUARD(&s->lock) {
bdrv_set_dirty_bitmap(s->done_bitmap, off, end - off);
if (ret < 0) {
assert(s->on_cbw_error == ON_CBW_ERROR_BREAK_SNAPSHOT);
if (!s->snapshot_error) {
s->snapshot_error = ret;
}
} else {
bdrv_set_dirty_bitmap(s->done_bitmap, off, end - off);
}
reqlist_wait_all(&s->frozen_read_reqs, off, end - off, &s->lock);
}
@ -175,6 +213,11 @@ static BlockReq *cbw_snapshot_read_lock(BlockDriverState *bs,
QEMU_LOCK_GUARD(&s->lock);
if (s->snapshot_error) {
g_free(req);
return NULL;
}
if (bdrv_dirty_bitmap_next_zero(s->access_bitmap, offset, bytes) != -1) {
g_free(req);
return NULL;
@ -328,46 +371,36 @@ static void cbw_child_perm(BlockDriverState *bs, BdrvChild *c,
}
}
static bool cbw_parse_bitmap_option(QDict *options, BdrvDirtyBitmap **bitmap,
Error **errp)
static BlockdevOptions *cbw_parse_options(QDict *options, Error **errp)
{
QDict *bitmap_qdict = NULL;
BlockDirtyBitmap *bmp_param = NULL;
BlockdevOptions *opts = NULL;
Visitor *v = NULL;
bool ret = false;
*bitmap = NULL;
qdict_put_str(options, "driver", "copy-before-write");
qdict_extract_subqdict(options, &bitmap_qdict, "bitmap.");
if (!qdict_size(bitmap_qdict)) {
ret = true;
goto out;
}
v = qobject_input_visitor_new_flat_confused(bitmap_qdict, errp);
v = qobject_input_visitor_new_flat_confused(options, errp);
if (!v) {
goto out;
}
visit_type_BlockDirtyBitmap(v, NULL, &bmp_param, errp);
if (!bmp_param) {
visit_type_BlockdevOptions(v, NULL, &opts, errp);
if (!opts) {
goto out;
}
*bitmap = block_dirty_bitmap_lookup(bmp_param->node, bmp_param->name, NULL,
errp);
if (!*bitmap) {
goto out;
}
ret = true;
/*
* Delete options which we are going to parse through BlockdevOptions
* object for original options.
*/
qdict_extract_subqdict(options, NULL, "bitmap");
qdict_del(options, "on-cbw-error");
qdict_del(options, "cbw-timeout");
out:
qapi_free_BlockDirtyBitmap(bmp_param);
visit_free(v);
qobject_unref(bitmap_qdict);
qdict_del(options, "driver");
return ret;
return opts;
}
static int cbw_open(BlockDriverState *bs, QDict *options, int flags,
@ -376,6 +409,15 @@ static int cbw_open(BlockDriverState *bs, QDict *options, int flags,
BDRVCopyBeforeWriteState *s = bs->opaque;
BdrvDirtyBitmap *bitmap = NULL;
int64_t cluster_size;
g_autoptr(BlockdevOptions) full_opts = NULL;
BlockdevOptionsCbw *opts;
full_opts = cbw_parse_options(options, errp);
if (!full_opts) {
return -EINVAL;
}
assert(full_opts->driver == BLOCKDEV_DRIVER_COPY_BEFORE_WRITE);
opts = &full_opts->u.copy_before_write;
bs->file = bdrv_open_child(NULL, options, "file", bs, &child_of_bds,
BDRV_CHILD_FILTERED | BDRV_CHILD_PRIMARY,
@ -390,9 +432,17 @@ static int cbw_open(BlockDriverState *bs, QDict *options, int flags,
return -EINVAL;
}
if (!cbw_parse_bitmap_option(options, &bitmap, errp)) {
return -EINVAL;
if (opts->has_bitmap) {
bitmap = block_dirty_bitmap_lookup(opts->bitmap->node,
opts->bitmap->name, NULL, errp);
if (!bitmap) {
return -EINVAL;
}
}
s->on_cbw_error = opts->has_on_cbw_error ? opts->on_cbw_error :
ON_CBW_ERROR_BREAK_GUEST_WRITE;
s->cbw_timeout_ns = opts->has_cbw_timeout ?
opts->cbw_timeout * NANOSECONDS_PER_SECOND : 0;
bs->total_sectors = bs->file->bs->total_sectors;
bs->supported_write_flags = BDRV_REQ_WRITE_UNCHANGED |

View File

@ -73,7 +73,7 @@ typedef struct MirrorBlockJob {
uint64_t last_pause_ns;
unsigned long *in_flight_bitmap;
int in_flight;
unsigned in_flight;
int64_t bytes_in_flight;
QTAILQ_HEAD(, MirrorOp) ops_in_flight;
int ret;

View File

@ -77,7 +77,7 @@ typedef struct BDRVNBDState {
QemuMutex requests_lock;
NBDClientState state;
CoQueue free_sema;
int in_flight;
unsigned in_flight;
NBDClientRequest requests[MAX_NBD_REQUESTS];
QEMUTimer *reconnect_delay_timer;
@ -371,6 +371,7 @@ static bool nbd_client_connecting(BDRVNBDState *s)
/* Called with s->requests_lock taken. */
static coroutine_fn void nbd_reconnect_attempt(BDRVNBDState *s)
{
int ret;
bool blocking = s->state == NBD_CLIENT_CONNECTING_WAIT;
/*
@ -380,6 +381,8 @@ static coroutine_fn void nbd_reconnect_attempt(BDRVNBDState *s)
assert(nbd_client_connecting(s));
assert(s->in_flight == 1);
trace_nbd_reconnect_attempt(s->bs->in_flight);
if (blocking && !s->reconnect_delay_timer) {
/*
* It's the first reconnect attempt after switching to
@ -401,7 +404,8 @@ static coroutine_fn void nbd_reconnect_attempt(BDRVNBDState *s)
}
qemu_mutex_unlock(&s->requests_lock);
nbd_co_do_establish_connection(s->bs, blocking, NULL);
ret = nbd_co_do_establish_connection(s->bs, blocking, NULL);
trace_nbd_reconnect_attempt_result(ret, s->bs->in_flight);
qemu_mutex_lock(&s->requests_lock);
/*

View File

@ -172,6 +172,8 @@ nbd_read_reply_entry_fail(int ret, const char *err) "ret = %d, err: %s"
nbd_co_request_fail(uint64_t from, uint32_t len, uint64_t handle, uint16_t flags, uint16_t type, const char *name, int ret, const char *err) "Request failed { .from = %" PRIu64", .len = %" PRIu32 ", .handle = %" PRIu64 ", .flags = 0x%" PRIx16 ", .type = %" PRIu16 " (%s) } ret = %d, err: %s"
nbd_client_handshake(const char *export_name) "export '%s'"
nbd_client_handshake_success(const char *export_name) "export '%s'"
nbd_reconnect_attempt(unsigned in_flight) "in_flight %u"
nbd_reconnect_attempt_result(int ret, unsigned in_flight) "ret %d in_flight %u"
# ssh.c
ssh_restart_coroutine(void *co) "co=%p"

View File

@ -40,7 +40,9 @@ int64_t block_copy_reset_unallocated(BlockCopyState *s,
int64_t offset, int64_t *count);
int coroutine_fn block_copy(BlockCopyState *s, int64_t offset, int64_t bytes,
bool ignore_ratelimit);
bool ignore_ratelimit, uint64_t timeout_ns,
BlockCopyAsyncCallbackFunc cb,
void *cb_opaque);
/*
* Run block-copy in a coroutine, create corresponding BlockCopyCallState

View File

@ -331,6 +331,19 @@ static inline void coroutine_fn qemu_co_sleep_ns(QEMUClockType type, int64_t ns)
qemu_co_sleep_ns_wakeable(&w, type, ns);
}
typedef void CleanupFunc(void *opaque);
/**
* Run entry in a coroutine and start timer. Wait for entry to finish or for
* timer to elapse, what happen first. If entry finished, return 0, if timer
* elapsed earlier, return -ETIMEDOUT.
*
* Be careful, entry execution is not canceled, user should handle it somehow.
* If @clean is provided, it's called after coroutine finish if timeout
* happened.
*/
int coroutine_fn qemu_co_timeout(CoroutineEntry *entry, void *opaque,
uint64_t timeout_ns, CleanupFunc clean);
/**
* Wake a coroutine if it is sleeping in qemu_co_sleep_ns. The timer will be
* deleted. @sleep_state must be the variable whose address was given to

View File

@ -23,6 +23,7 @@
*/
#include "qemu/osdep.h"
#include "trace.h"
#include "block/nbd.h"
@ -210,6 +211,7 @@ static void *connect_thread_func(void *opaque)
object_unref(OBJECT(conn->sioc));
conn->sioc = NULL;
if (conn->do_retry && !conn->detached) {
trace_nbd_connect_thread_sleep(timeout);
qemu_mutex_unlock(&conn->mutex);
sleep(timeout);

View File

@ -73,3 +73,6 @@ nbd_co_receive_request_decode_type(uint64_t handle, uint16_t type, const char *n
nbd_co_receive_request_payload_received(uint64_t handle, uint32_t len) "Payload received: handle = %" PRIu64 ", len = %" PRIu32
nbd_co_receive_align_compliance(const char *op, uint64_t from, uint32_t len, uint32_t align) "client sent non-compliant unaligned %s request: from=0x%" PRIx64 ", len=0x%" PRIx32 ", align=0x%" PRIx32
nbd_trip(void) "Reading request"
# client-connection.c
nbd_connect_thread_sleep(uint64_t timeout) "timeout %" PRIu64

View File

@ -4184,6 +4184,25 @@
'base': 'BlockdevOptionsGenericFormat',
'data': { '*bottom': 'str' } }
##
# @OnCbwError:
#
# An enumeration of possible behaviors for copy-before-write operation
# failures.
#
# @break-guest-write: report the error to the guest. This way, the guest
# will not be able to overwrite areas that cannot be
# backed up, so the backup process remains valid.
#
# @break-snapshot: continue guest write. Doing so will make the provided
# snapshot state invalid and any backup or export
# process based on it will finally fail.
#
# Since: 7.1
##
{ 'enum': 'OnCbwError',
'data': [ 'break-guest-write', 'break-snapshot' ] }
##
# @BlockdevOptionsCbw:
#
@ -4205,11 +4224,21 @@
# modifications (or removing) of specified bitmap doesn't
# influence the filter. (Since 7.0)
#
# @on-cbw-error: Behavior on failure of copy-before-write operation.
# Default is @break-guest-write. (Since 7.1)
#
# @cbw-timeout: Zero means no limit. Non-zero sets the timeout in seconds
# for copy-before-write operation. When a timeout occurs,
# the respective copy-before-write operation will fail, and
# the @on-cbw-error parameter will decide how this failure
# is handled. Default 0. (Since 7.1)
#
# Since: 6.2
##
{ 'struct': 'BlockdevOptionsCbw',
'base': 'BlockdevOptionsGenericFormat',
'data': { 'target': 'BlockdevRef', '*bitmap': 'BlockDirtyBitmap' } }
'data': { 'target': 'BlockdevRef', '*bitmap': 'BlockDirtyBitmap',
'*on-cbw-error': 'OnCbwError', '*cbw-timeout': 'uint32' } }
##
# @BlockdevOptions:

View File

@ -51,3 +51,8 @@ notes=FIXME,
# Maximum number of characters on a single line.
max-line-length=79
[SIMILARITIES]
min-similarity-lines=6

View File

@ -0,0 +1,216 @@
#!/usr/bin/env python3
# group: auto backup
#
# Copyright (c) 2022 Virtuozzo International GmbH
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
import os
import re
from qemu.machine import QEMUMachine
import iotests
from iotests import qemu_img_create, qemu_io
temp_img = os.path.join(iotests.test_dir, 'temp')
source_img = os.path.join(iotests.test_dir, 'source')
size = '1M'
class TestCbwError(iotests.QMPTestCase):
def tearDown(self):
self.vm.shutdown()
os.remove(temp_img)
os.remove(source_img)
def setUp(self):
qemu_img_create('-f', iotests.imgfmt, source_img, size)
qemu_img_create('-f', iotests.imgfmt, temp_img, size)
qemu_io('-c', 'write 0 1M', source_img)
opts = ['-nodefaults', '-display', 'none', '-machine', 'none']
self.vm = QEMUMachine(iotests.qemu_prog, opts,
base_temp_dir=iotests.test_dir,
sock_dir=iotests.sock_dir)
self.vm.launch()
def do_cbw_error(self, on_cbw_error):
result = self.vm.qmp('blockdev-add', {
'node-name': 'cbw',
'driver': 'copy-before-write',
'on-cbw-error': on_cbw_error,
'file': {
'driver': iotests.imgfmt,
'file': {
'driver': 'file',
'filename': source_img,
}
},
'target': {
'driver': iotests.imgfmt,
'file': {
'driver': 'blkdebug',
'image': {
'driver': 'file',
'filename': temp_img
},
'inject-error': [
{
'event': 'write_aio',
'errno': 5,
'immediately': False,
'once': True
}
]
}
}
})
self.assert_qmp(result, 'return', {})
result = self.vm.qmp('blockdev-add', {
'node-name': 'access',
'driver': 'snapshot-access',
'file': 'cbw'
})
self.assert_qmp(result, 'return', {})
result = self.vm.qmp('human-monitor-command',
command_line='qemu-io cbw "write 0 1M"')
self.assert_qmp(result, 'return', '')
result = self.vm.qmp('human-monitor-command',
command_line='qemu-io access "read 0 1M"')
self.assert_qmp(result, 'return', '')
self.vm.shutdown()
log = self.vm.get_log()
log = re.sub(r'^\[I \d+\.\d+\] OPENED\n', '', log)
log = re.sub(r'\[I \+\d+\.\d+\] CLOSED\n?$', '', log)
log = iotests.filter_qemu_io(log)
return log
def test_break_snapshot_on_cbw_error(self):
"""break-snapshot behavior:
Guest write succeed, but further snapshot-read fails, as snapshot is
broken.
"""
log = self.do_cbw_error('break-snapshot')
self.assertEqual(log, """\
wrote 1048576/1048576 bytes at offset 0
1 MiB, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec)
read failed: Permission denied
""")
def test_break_guest_write_on_cbw_error(self):
"""break-guest-write behavior:
Guest write fails, but snapshot-access continues working and further
snapshot-read succeeds.
"""
log = self.do_cbw_error('break-guest-write')
self.assertEqual(log, """\
write failed: Input/output error
read 1048576/1048576 bytes at offset 0
1 MiB, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec)
""")
def do_cbw_timeout(self, on_cbw_error):
result = self.vm.qmp('object-add', {
'qom-type': 'throttle-group',
'id': 'group0',
'limits': {'bps-write': 300 * 1024}
})
self.assert_qmp(result, 'return', {})
result = self.vm.qmp('blockdev-add', {
'node-name': 'cbw',
'driver': 'copy-before-write',
'on-cbw-error': on_cbw_error,
'cbw-timeout': 1,
'file': {
'driver': iotests.imgfmt,
'file': {
'driver': 'file',
'filename': source_img,
}
},
'target': {
'driver': 'throttle',
'throttle-group': 'group0',
'file': {
'driver': 'qcow2',
'file': {
'driver': 'file',
'filename': temp_img
}
}
}
})
self.assert_qmp(result, 'return', {})
result = self.vm.qmp('blockdev-add', {
'node-name': 'access',
'driver': 'snapshot-access',
'file': 'cbw'
})
self.assert_qmp(result, 'return', {})
result = self.vm.qmp('human-monitor-command',
command_line='qemu-io cbw "write 0 512K"')
self.assert_qmp(result, 'return', '')
# We need second write to trigger throttling
result = self.vm.qmp('human-monitor-command',
command_line='qemu-io cbw "write 512K 512K"')
self.assert_qmp(result, 'return', '')
result = self.vm.qmp('human-monitor-command',
command_line='qemu-io access "read 0 1M"')
self.assert_qmp(result, 'return', '')
self.vm.shutdown()
log = self.vm.get_log()
log = re.sub(r'^\[I \d+\.\d+\] OPENED\n', '', log)
log = re.sub(r'\[I \+\d+\.\d+\] CLOSED\n?$', '', log)
log = iotests.filter_qemu_io(log)
return log
def test_timeout_break_guest(self):
log = self.do_cbw_timeout('break-guest-write')
self.assertEqual(log, """\
wrote 524288/524288 bytes at offset 0
512 KiB, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec)
write failed: Connection timed out
read 1048576/1048576 bytes at offset 0
1 MiB, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec)
""")
def test_timeout_break_snapshot(self):
log = self.do_cbw_timeout('break-snapshot')
self.assertEqual(log, """\
wrote 524288/524288 bytes at offset 0
512 KiB, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec)
wrote 524288/524288 bytes at offset 524288
512 KiB, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec)
read failed: Permission denied
""")
if __name__ == '__main__':
iotests.main(supported_fmts=['qcow2'],
supported_protocols=['file'])

View File

@ -0,0 +1,5 @@
....
----------------------------------------------------------------------
Ran 4 tests
OK

View File

@ -85,6 +85,7 @@ if have_block
util_ss.add(files('block-helpers.c'))
util_ss.add(files('qemu-coroutine-sleep.c'))
util_ss.add(files('qemu-co-shared-resource.c'))
util_ss.add(files('qemu-co-timeout.c'))
util_ss.add(files('thread-pool.c', 'qemu-timer.c'))
util_ss.add(files('readline.c'))
util_ss.add(files('throttle.c'))

89
util/qemu-co-timeout.c Normal file
View File

@ -0,0 +1,89 @@
/*
* Helper functionality for distributing a fixed total amount of
* an abstract resource among multiple coroutines.
*
* Copyright (c) 2022 Virtuozzo International GmbH
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include "qemu/osdep.h"
#include "qemu/coroutine.h"
#include "block/aio.h"
typedef struct QemuCoTimeoutState {
CoroutineEntry *entry;
void *opaque;
QemuCoSleep sleep_state;
bool marker;
CleanupFunc *clean;
} QemuCoTimeoutState;
static void coroutine_fn qemu_co_timeout_entry(void *opaque)
{
QemuCoTimeoutState *s = opaque;
s->entry(s->opaque);
if (s->marker) {
assert(!s->sleep_state.to_wake);
/* .marker set by qemu_co_timeout, it have been failed */
if (s->clean) {
s->clean(s->opaque);
}
g_free(s);
} else {
s->marker = true;
qemu_co_sleep_wake(&s->sleep_state);
}
}
int coroutine_fn qemu_co_timeout(CoroutineEntry *entry, void *opaque,
uint64_t timeout_ns, CleanupFunc clean)
{
QemuCoTimeoutState *s;
Coroutine *co;
if (timeout_ns == 0) {
entry(opaque);
return 0;
}
s = g_new(QemuCoTimeoutState, 1);
*s = (QemuCoTimeoutState) {
.entry = entry,
.opaque = opaque,
.clean = clean
};
co = qemu_coroutine_create(qemu_co_timeout_entry, s);
aio_co_enter(qemu_get_current_aio_context(), co);
qemu_co_sleep_ns_wakeable(&s->sleep_state, QEMU_CLOCK_REALTIME, timeout_ns);
if (s->marker) {
/* .marker set by qemu_co_timeout_entry, success */
g_free(s);
return 0;
}
/* Don't free s, as we can't cancel qemu_co_timeout_entry execution */
s->marker = true;
return -ETIMEDOUT;
}