From nobody Mon Apr 29 15:08:05 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zoho.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1486138504905260.1892255428396; Fri, 3 Feb 2017 08:15:04 -0800 (PST) Received: from localhost ([::1]:35545 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cZgVq-0005Kn-5L for importer@patchew.org; Fri, 03 Feb 2017 11:15:02 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:41860) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cZgO3-0007RK-CK for qemu-devel@nongnu.org; Fri, 03 Feb 2017 11:07:00 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1cZgNz-0007Rl-Jl for qemu-devel@nongnu.org; Fri, 03 Feb 2017 11:06:59 -0500 Received: from mx1.redhat.com ([209.132.183.28]:33768) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1cZgNz-0007RH-EL for qemu-devel@nongnu.org; Fri, 03 Feb 2017 11:06:55 -0500 Received: from int-mx13.intmail.prod.int.phx2.redhat.com (int-mx13.intmail.prod.int.phx2.redhat.com [10.5.11.26]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 9F0ABC057FA4 for ; Fri, 3 Feb 2017 16:06:55 +0000 (UTC) Received: from dgilbert-t530.redhat.com (ovpn-117-144.ams2.redhat.com [10.36.117.144]) by int-mx13.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id v13G6r0m014461; Fri, 3 Feb 2017 11:06:54 -0500 From: "Dr. David Alan Gilbert (git)" To: qemu-devel@nongnu.org, mst@redhat.com, quintela@redhat.com Date: Fri, 3 Feb 2017 16:06:48 +0000 Message-Id: <20170203160651.19917-2-dgilbert@redhat.com> In-Reply-To: <20170203160651.19917-1-dgilbert@redhat.com> References: <20170203160651.19917-1-dgilbert@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.26 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.32]); Fri, 03 Feb 2017 16:06:55 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH 1/4] migration: Add VMSTATE_UNUSED_VARRAY_UINT32 X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" From: "Dr. David Alan Gilbert" VMSTATE_UNUSED_VARRAY_UINT32 is used to skip a chunk of the stream that's an n-element array; note the array size and the dynamic value read never get multiplied so there's no overflow risk. Signed-off-by: Dr. David Alan Gilbert Reviewed-by: Michael S. Tsirkin --- include/migration/vmstate.h | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/include/migration/vmstate.h b/include/migration/vmstate.h index 6233fe2..cc66910 100644 --- a/include/migration/vmstate.h +++ b/include/migration/vmstate.h @@ -660,6 +660,17 @@ extern const VMStateInfo vmstate_info_qtailq; .flags =3D VMS_BUFFER, \ } =20 +/* Discard size * field_num bytes, where field_num is a uint32 member */ +#define VMSTATE_UNUSED_VARRAY_UINT32(_state, _test, _version, _field_num, = _size) {\ + .name =3D "unused", \ + .field_exists =3D (_test), \ + .num_offset =3D vmstate_offset_value(_state, _field_num, uint32_t),\ + .version_id =3D (_version), \ + .size =3D (_size), \ + .info =3D &vmstate_info_unused_buffer, \ + .flags =3D VMS_VARRAY_UINT32 | VMS_BUFFER, \ +} + /* _field_size should be a int32_t field in the _state struct giving the * size of the bitmap _field in bits. */ --=20 2.9.3 From nobody Mon Apr 29 15:08:05 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zoho.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 148613978292120.322444038937988; Fri, 3 Feb 2017 08:36:22 -0800 (PST) Received: from localhost ([::1]:35688 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cZgqS-0006BN-Rc for importer@patchew.org; Fri, 03 Feb 2017 11:36:20 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:41844) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cZgO2-0007Qg-Ja for qemu-devel@nongnu.org; Fri, 03 Feb 2017 11:07:00 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1cZgO0-0007S8-QW for qemu-devel@nongnu.org; Fri, 03 Feb 2017 11:06:58 -0500 Received: from mx1.redhat.com ([209.132.183.28]:49118) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1cZgO0-0007Ru-Ii for qemu-devel@nongnu.org; Fri, 03 Feb 2017 11:06:56 -0500 Received: from int-mx13.intmail.prod.int.phx2.redhat.com (int-mx13.intmail.prod.int.phx2.redhat.com [10.5.11.26]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id C50CE369C3 for ; Fri, 3 Feb 2017 16:06:56 +0000 (UTC) Received: from dgilbert-t530.redhat.com (ovpn-117-144.ams2.redhat.com [10.36.117.144]) by int-mx13.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id v13G6r0n014461; Fri, 3 Feb 2017 11:06:55 -0500 From: "Dr. David Alan Gilbert (git)" To: qemu-devel@nongnu.org, mst@redhat.com, quintela@redhat.com Date: Fri, 3 Feb 2017 16:06:49 +0000 Message-Id: <20170203160651.19917-3-dgilbert@redhat.com> In-Reply-To: <20170203160651.19917-1-dgilbert@redhat.com> References: <20170203160651.19917-1-dgilbert@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.26 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.30]); Fri, 03 Feb 2017 16:06:56 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH 2/4] migration: Add VMSTATE_WITH_TMP X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" From: "Dr. David Alan Gilbert" VMSTATE_WITH_TMP is for handling structures where some calculation or rearrangement of the data needs to be performed before the data hits the wire. For example, where the value on the wire is an offset from a non-migrated base, but the data in the structure is the actual pointer. To use it, a temporary type is created and a vmsd used on that type. The first element of the type must be 'parent' a pointer back to the type of the main structure. VMSTATE_WITH_TMP takes care of allocating and freeing the temporary before running the child vmsd. The post_load/pre_save on the child vmsd can copy things from the parent to the temporary using the parent pointer and do any other calculations needed; it can then use normal VMSD entries to do the actual data storage without having to fiddle around with qemu_get_*/qemu_put_* Signed-off-by: Dr. David Alan Gilbert Reviewed-by: David Gibson Reviewed-by: Alex Benn=C3=A9e Reviewed-by: Michael S. Tsirkin --- include/migration/vmstate.h | 19 +++++++++++++++++++ migration/vmstate.c | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+) diff --git a/include/migration/vmstate.h b/include/migration/vmstate.h index cc66910..f2dfb85 100644 --- a/include/migration/vmstate.h +++ b/include/migration/vmstate.h @@ -259,6 +259,7 @@ extern const VMStateInfo vmstate_info_cpudouble; extern const VMStateInfo vmstate_info_timer; extern const VMStateInfo vmstate_info_buffer; extern const VMStateInfo vmstate_info_unused_buffer; +extern const VMStateInfo vmstate_info_tmp; extern const VMStateInfo vmstate_info_bitmap; extern const VMStateInfo vmstate_info_qtailq; =20 @@ -651,6 +652,24 @@ extern const VMStateInfo vmstate_info_qtailq; .offset =3D offsetof(_state, _field), \ } =20 +/* Allocate a temporary of type 'tmp_type', set tmp->parent to _state + * and execute the vmsd on the temporary. Note that we're working with + * the whole of _state here, not a field within it. + * We compile time check that: + * That _tmp_type contains a 'parent' member that's a pointer to the + * '_state' type + * That the pointer is right at the start of _tmp_type. + */ +#define VMSTATE_WITH_TMP(_state, _tmp_type, _vmsd) { \ + .name =3D "tmp", \ + .size =3D sizeof(_tmp_type) + \ + QEMU_BUILD_BUG_ON_ZERO(offsetof(_tmp_type, parent) != =3D 0) + \ + type_check_pointer(_state, \ + typeof_field(_tmp_type, parent)), \ + .vmsd =3D &(_vmsd), \ + .info =3D &vmstate_info_tmp, \ +} + #define VMSTATE_UNUSED_BUFFER(_test, _version, _size) { \ .name =3D "unused", \ .field_exists =3D (_test), \ diff --git a/migration/vmstate.c b/migration/vmstate.c index 2b2b3a5..a3de20f 100644 --- a/migration/vmstate.c +++ b/migration/vmstate.c @@ -935,6 +935,46 @@ const VMStateInfo vmstate_info_unused_buffer =3D { .put =3D put_unused_buffer, }; =20 +/* vmstate_info_tmp, see VMSTATE_WITH_TMP, the idea is that we allocate + * a temporary buffer and the pre_load/pre_save methods in the child vmsd + * copy stuff from the parent into the child and do calculations to fill + * in fields that don't really exist in the parent but need to be in the + * stream. + */ +static int get_tmp(QEMUFile *f, void *pv, size_t size, VMStateField *field) +{ + int ret; + const VMStateDescription *vmsd =3D field->vmsd; + int version_id =3D field->version_id; + void *tmp =3D g_malloc(size); + + /* Writes the parent field which is at the start of the tmp */ + *(void **)tmp =3D pv; + ret =3D vmstate_load_state(f, vmsd, tmp, version_id); + g_free(tmp); + return ret; +} + +static int put_tmp(QEMUFile *f, void *pv, size_t size, VMStateField *field, + QJSON *vmdesc) +{ + const VMStateDescription *vmsd =3D field->vmsd; + void *tmp =3D g_malloc(size); + + /* Writes the parent field which is at the start of the tmp */ + *(void **)tmp =3D pv; + vmstate_save_state(f, vmsd, tmp, vmdesc); + g_free(tmp); + + return 0; +} + +const VMStateInfo vmstate_info_tmp =3D { + .name =3D "tmp", + .get =3D get_tmp, + .put =3D put_tmp, +}; + /* bitmaps (as defined by bitmap.h). Note that size here is the size * of the bitmap in bits. The on-the-wire format of a bitmap is 64 * bit words with the bits in big endian order. The in-memory format --=20 2.9.3 From nobody Mon Apr 29 15:08:05 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zoho.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1486139027759870.1766259480953; Fri, 3 Feb 2017 08:23:47 -0800 (PST) Received: from localhost ([::1]:35608 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cZgeG-0004Mn-RM for importer@patchew.org; Fri, 03 Feb 2017 11:23:44 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:41857) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cZgO3-0007RB-7M for qemu-devel@nongnu.org; Fri, 03 Feb 2017 11:07:00 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1cZgO2-0007SM-0C for qemu-devel@nongnu.org; Fri, 03 Feb 2017 11:06:59 -0500 Received: from mx1.redhat.com ([209.132.183.28]:58062) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1cZgO1-0007SD-Nv for qemu-devel@nongnu.org; Fri, 03 Feb 2017 11:06:57 -0500 Received: from int-mx13.intmail.prod.int.phx2.redhat.com (int-mx13.intmail.prod.int.phx2.redhat.com [10.5.11.26]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id EEC9A83F40 for ; Fri, 3 Feb 2017 16:06:57 +0000 (UTC) Received: from dgilbert-t530.redhat.com (ovpn-117-144.ams2.redhat.com [10.36.117.144]) by int-mx13.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id v13G6r0o014461; Fri, 3 Feb 2017 11:06:57 -0500 From: "Dr. David Alan Gilbert (git)" To: qemu-devel@nongnu.org, mst@redhat.com, quintela@redhat.com Date: Fri, 3 Feb 2017 16:06:50 +0000 Message-Id: <20170203160651.19917-4-dgilbert@redhat.com> In-Reply-To: <20170203160651.19917-1-dgilbert@redhat.com> References: <20170203160651.19917-1-dgilbert@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.26 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.27]); Fri, 03 Feb 2017 16:06:57 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH 3/4] tests/migration: Add test for VMSTATE_WITH_TMP X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" From: "Dr. David Alan Gilbert" Add a test for VMSTATE_WITH_TMP to tests/test-vmstate.c Signed-off-by: Dr. David Alan Gilbert Reviewed-by: Michael S. Tsirkin --- tests/test-vmstate.c | 98 ++++++++++++++++++++++++++++++++++++++++++++++++= ---- 1 file changed, 92 insertions(+), 6 deletions(-) diff --git a/tests/test-vmstate.c b/tests/test-vmstate.c index 9d87faf..d0dd390 100644 --- a/tests/test-vmstate.c +++ b/tests/test-vmstate.c @@ -90,7 +90,7 @@ static void save_buffer(const uint8_t *buf, size_t buf_si= ze) qemu_fclose(fsave); } =20 -static void compare_vmstate(uint8_t *wire, size_t size) +static void compare_vmstate(const uint8_t *wire, size_t size) { QEMUFile *f =3D open_test_file(false); uint8_t result[size]; @@ -113,7 +113,7 @@ static void compare_vmstate(uint8_t *wire, size_t size) } =20 static int load_vmstate_one(const VMStateDescription *desc, void *obj, - int version, uint8_t *wire, size_t size) + int version, const uint8_t *wire, size_t size) { QEMUFile *f; int ret; @@ -137,7 +137,7 @@ static int load_vmstate_one(const VMStateDescription *d= esc, void *obj, static int load_vmstate(const VMStateDescription *desc, void *obj, void *obj_clone, void (*obj_copy)(void *, void*), - int version, uint8_t *wire, size_t size) + int version, const uint8_t *wire, size_t size) { /* We test with zero size */ obj_copy(obj_clone, obj); @@ -289,7 +289,6 @@ static void test_simple_primitive(void) FIELD_EQUAL(i64_1); FIELD_EQUAL(i64_2); } -#undef FIELD_EQUAL =20 typedef struct TestStruct { uint32_t a, b, c, e; @@ -474,7 +473,6 @@ static void test_load_skip(void) qemu_fclose(loading); } =20 - typedef struct { int32_t i; } TestStructTriv; @@ -688,6 +686,94 @@ static void test_load_q(void) qemu_fclose(fload); } =20 +typedef struct TmpTestStruct { + TestStruct *parent; + int64_t diff; +} TmpTestStruct; + +static void tmp_child_pre_save(void *opaque) +{ + struct TmpTestStruct *tts =3D opaque; + + tts->diff =3D tts->parent->b - tts->parent->a; +} + +static int tmp_child_post_load(void *opaque, int version_id) +{ + struct TmpTestStruct *tts =3D opaque; + + tts->parent->b =3D tts->parent->a + tts->diff; + + return 0; +} + +static const VMStateDescription vmstate_tmp_back_to_parent =3D { + .name =3D "test/tmp_child_parent", + .fields =3D (VMStateField[]) { + VMSTATE_UINT64(f, TestStruct), + VMSTATE_END_OF_LIST() + } +}; + +static const VMStateDescription vmstate_tmp_child =3D { + .name =3D "test/tmp_child", + .pre_save =3D tmp_child_pre_save, + .post_load =3D tmp_child_post_load, + .fields =3D (VMStateField[]) { + VMSTATE_INT64(diff, TmpTestStruct), + VMSTATE_STRUCT_POINTER(parent, TmpTestStruct, + vmstate_tmp_back_to_parent, TestStruct), + VMSTATE_END_OF_LIST() + } +}; + +static const VMStateDescription vmstate_with_tmp =3D { + .name =3D "test/with_tmp", + .version_id =3D 1, + .fields =3D (VMStateField[]) { + VMSTATE_UINT32(a, TestStruct), + VMSTATE_UINT64(d, TestStruct), + VMSTATE_WITH_TMP(TestStruct, TmpTestStruct, vmstate_tmp_child), + VMSTATE_END_OF_LIST() + } +}; + +static void obj_tmp_copy(void *target, void *source) +{ + memcpy(target, source, sizeof(TestStruct)); +} + +static void test_tmp_struct(void) +{ + TestStruct obj, obj_clone; + + uint8_t const wire_with_tmp[] =3D { + /* u32 a */ 0x00, 0x00, 0x00, 0x02, + /* u64 d */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, + /* diff */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, + /* u64 f */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, + QEMU_VM_EOF, /* just to ensure we won't get EOF reported premature= ly */ + }; + + memset(&obj, 0, sizeof(obj)); + obj.a =3D 2; + obj.b =3D 4; + obj.d =3D 1; + obj.f =3D 8; + save_vmstate(&vmstate_with_tmp, &obj); + + compare_vmstate(wire_with_tmp, sizeof(wire_with_tmp)); + + memset(&obj, 0, sizeof(obj)); + SUCCESS(load_vmstate(&vmstate_with_tmp, &obj, &obj_clone, + obj_tmp_copy, 1, wire_with_tmp, + sizeof(wire_with_tmp))); + g_assert_cmpint(obj.a, =3D=3D, 2); /* From top level vmsd */ + g_assert_cmpint(obj.b, =3D=3D, 4); /* from the post_load */ + g_assert_cmpint(obj.d, =3D=3D, 1); /* From top level vmsd */ + g_assert_cmpint(obj.f, =3D=3D, 8); /* From the child->parent */ +} + int main(int argc, char **argv) { temp_fd =3D mkstemp(temp_file); @@ -708,7 +794,7 @@ int main(int argc, char **argv) test_arr_ptr_str_no0_load); g_test_add_func("/vmstate/qtailq/save/saveq", test_save_q); g_test_add_func("/vmstate/qtailq/load/loadq", test_load_q); - + g_test_add_func("/vmstate/tmp_struct", test_tmp_struct); g_test_run(); =20 close(temp_fd); --=20 2.9.3 From nobody Mon Apr 29 15:08:05 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zoho.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1486139254364111.60109965569552; Fri, 3 Feb 2017 08:27:34 -0800 (PST) Received: from localhost ([::1]:35631 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cZghv-0007th-R3 for importer@patchew.org; Fri, 03 Feb 2017 11:27:31 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:41883) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cZgO5-0007T4-5Z for qemu-devel@nongnu.org; Fri, 03 Feb 2017 11:07:02 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1cZgO3-0007Sn-7n for qemu-devel@nongnu.org; Fri, 03 Feb 2017 11:07:01 -0500 Received: from mx1.redhat.com ([209.132.183.28]:49128) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1cZgO2-0007SQ-Vk for qemu-devel@nongnu.org; Fri, 03 Feb 2017 11:06:59 -0500 Received: from int-mx13.intmail.prod.int.phx2.redhat.com (int-mx13.intmail.prod.int.phx2.redhat.com [10.5.11.26]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 280B23B731 for ; Fri, 3 Feb 2017 16:06:59 +0000 (UTC) Received: from dgilbert-t530.redhat.com (ovpn-117-144.ams2.redhat.com [10.36.117.144]) by int-mx13.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id v13G6r0p014461; Fri, 3 Feb 2017 11:06:58 -0500 From: "Dr. David Alan Gilbert (git)" To: qemu-devel@nongnu.org, mst@redhat.com, quintela@redhat.com Date: Fri, 3 Feb 2017 16:06:51 +0000 Message-Id: <20170203160651.19917-5-dgilbert@redhat.com> In-Reply-To: <20170203160651.19917-1-dgilbert@redhat.com> References: <20170203160651.19917-1-dgilbert@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.26 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.30]); Fri, 03 Feb 2017 16:06:59 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH 4/4] virtio/migration: Migrate virtio-net to VMState X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" From: "Dr. David Alan Gilbert" Signed-off-by: Dr. David Alan Gilbert Reviewed-by: Michael S. Tsirkin --- hw/net/virtio-net.c | 316 +++++++++++++++++++++++++++----------= ---- include/hw/virtio/virtio-net.h | 4 +- 2 files changed, 213 insertions(+), 107 deletions(-) diff --git a/hw/net/virtio-net.c b/hw/net/virtio-net.c index 7b3ad4a..41723a4 100644 --- a/hw/net/virtio-net.c +++ b/hw/net/virtio-net.c @@ -1557,119 +1557,22 @@ static void virtio_net_set_multiqueue(VirtIONet *n= , int multiqueue) virtio_net_set_queues(n); } =20 -static void virtio_net_save_device(VirtIODevice *vdev, QEMUFile *f) +static int virtio_net_post_load_device(void *opaque, int version_id) { - VirtIONet *n =3D VIRTIO_NET(vdev); - int i; - - qemu_put_buffer(f, n->mac, ETH_ALEN); - qemu_put_be32(f, n->vqs[0].tx_waiting); - qemu_put_be32(f, n->mergeable_rx_bufs); - qemu_put_be16(f, n->status); - qemu_put_byte(f, n->promisc); - qemu_put_byte(f, n->allmulti); - qemu_put_be32(f, n->mac_table.in_use); - qemu_put_buffer(f, n->mac_table.macs, n->mac_table.in_use * ETH_ALEN); - qemu_put_buffer(f, (uint8_t *)n->vlans, MAX_VLAN >> 3); - qemu_put_be32(f, n->has_vnet_hdr); - qemu_put_byte(f, n->mac_table.multi_overflow); - qemu_put_byte(f, n->mac_table.uni_overflow); - qemu_put_byte(f, n->alluni); - qemu_put_byte(f, n->nomulti); - qemu_put_byte(f, n->nouni); - qemu_put_byte(f, n->nobcast); - qemu_put_byte(f, n->has_ufo); - if (n->max_queues > 1) { - qemu_put_be16(f, n->max_queues); - qemu_put_be16(f, n->curr_queues); - for (i =3D 1; i < n->curr_queues; i++) { - qemu_put_be32(f, n->vqs[i].tx_waiting); - } - } - - if (virtio_vdev_has_feature(vdev, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS)) { - qemu_put_be64(f, n->curr_guest_offloads); - } -} - -static int virtio_net_load_device(VirtIODevice *vdev, QEMUFile *f, - int version_id) -{ - VirtIONet *n =3D VIRTIO_NET(vdev); + VirtIONet *n =3D opaque; + VirtIODevice *vdev =3D VIRTIO_DEVICE(n); int i, link_down; =20 - qemu_get_buffer(f, n->mac, ETH_ALEN); - n->vqs[0].tx_waiting =3D qemu_get_be32(f); - - virtio_net_set_mrg_rx_bufs(n, qemu_get_be32(f), + virtio_net_set_mrg_rx_bufs(n, n->mergeable_rx_bufs, virtio_vdev_has_feature(vdev, VIRTIO_F_VERSION_1)= ); =20 - n->status =3D qemu_get_be16(f); - - n->promisc =3D qemu_get_byte(f); - n->allmulti =3D qemu_get_byte(f); - - n->mac_table.in_use =3D qemu_get_be32(f); /* MAC_TABLE_ENTRIES may be different from the saved image */ - if (n->mac_table.in_use <=3D MAC_TABLE_ENTRIES) { - qemu_get_buffer(f, n->mac_table.macs, - n->mac_table.in_use * ETH_ALEN); - } else { - int64_t i; - - /* Overflow detected - can happen if source has a larger MAC table. - * We simply set overflow flag so there's no need to maintain the - * table of addresses, discard them all. - * Note: 64 bit math to avoid integer overflow. - */ - for (i =3D 0; i < (int64_t)n->mac_table.in_use * ETH_ALEN; ++i) { - qemu_get_byte(f); - } - n->mac_table.multi_overflow =3D n->mac_table.uni_overflow =3D 1; + if (n->mac_table.in_use > MAC_TABLE_ENTRIES) { n->mac_table.in_use =3D 0; } -=20 - qemu_get_buffer(f, (uint8_t *)n->vlans, MAX_VLAN >> 3); - - if (qemu_get_be32(f) && !peer_has_vnet_hdr(n)) { - error_report("virtio-net: saved image requires vnet_hdr=3Don"); - return -1; - } - - n->mac_table.multi_overflow =3D qemu_get_byte(f); - n->mac_table.uni_overflow =3D qemu_get_byte(f); - - n->alluni =3D qemu_get_byte(f); - n->nomulti =3D qemu_get_byte(f); - n->nouni =3D qemu_get_byte(f); - n->nobcast =3D qemu_get_byte(f); - - if (qemu_get_byte(f) && !peer_has_ufo(n)) { - error_report("virtio-net: saved image requires TUN_F_UFO support"); - return -1; - } =20 - if (n->max_queues > 1) { - if (n->max_queues !=3D qemu_get_be16(f)) { - error_report("virtio-net: different max_queues "); - return -1; - } - - n->curr_queues =3D qemu_get_be16(f); - if (n->curr_queues > n->max_queues) { - error_report("virtio-net: curr_queues %x > max_queues %x", - n->curr_queues, n->max_queues); - return -1; - } - for (i =3D 1; i < n->curr_queues; i++) { - n->vqs[i].tx_waiting =3D qemu_get_be32(f); - } - } - - if (virtio_vdev_has_feature(vdev, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS)) { - n->curr_guest_offloads =3D qemu_get_be64(f); - } else { + if (!virtio_vdev_has_feature(vdev, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS)) { n->curr_guest_offloads =3D virtio_net_supported_guest_offloads(n); } =20 @@ -1703,6 +1606,210 @@ static int virtio_net_load_device(VirtIODevice *vde= v, QEMUFile *f, return 0; } =20 +/* tx_waiting field of a VirtIONetQueue */ +static const VMStateDescription vmstate_virtio_net_queue_tx_waiting =3D { + .name =3D "virtio-net-queue-tx_waiting", + .fields =3D (VMStateField[]) { + VMSTATE_UINT32(tx_waiting, VirtIONetQueue), + VMSTATE_END_OF_LIST() + }, +}; + +static bool max_queues_gt_1(void *opaque, int version_id) +{ + return VIRTIO_NET(opaque)->max_queues > 1; +} + +static bool has_ctrl_guest_offloads(void *opaque, int version_id) +{ + return virtio_vdev_has_feature(VIRTIO_DEVICE(opaque), + VIRTIO_NET_F_CTRL_GUEST_OFFLOADS); +} + +static bool mac_table_fits(void *opaque, int version_id) +{ + return VIRTIO_NET(opaque)->mac_table.in_use <=3D MAC_TABLE_ENTRIES; +} + +static bool mac_table_doesnt_fit(void *opaque, int version_id) +{ + return !mac_table_fits(opaque, version_id); +} + +/* This temporary type is shared by all the WITH_TMP methods + * although only some fields are used by each. + */ +struct VirtIONetMigTmp { + VirtIONet *parent; + VirtIONetQueue *vqs_1; + uint16_t curr_queues_1; + uint8_t has_ufo; + uint32_t has_vnet_hdr; +}; + +/* The 2nd and subsequent tx_waiting flags are loaded later than + * the 1st entry in the queues and only if there's more than one + * entry. We use the tmp mechanism to calculate a temporary + * pointer and count and also validate the count. + */ + +static void virtio_net_tx_waiting_pre_save(void *opaque) +{ + struct VirtIONetMigTmp *tmp =3D opaque; + + tmp->vqs_1 =3D tmp->parent->vqs + 1; + tmp->curr_queues_1 =3D tmp->parent->curr_queues - 1; + if (tmp->parent->curr_queues =3D=3D 0) { + tmp->curr_queues_1 =3D 0; + } +} + +static int virtio_net_tx_waiting_pre_load(void *opaque) +{ + struct VirtIONetMigTmp *tmp =3D opaque; + + /* Reuse the pointer setup from save */ + virtio_net_tx_waiting_pre_save(opaque); + + if (tmp->parent->curr_queues > tmp->parent->max_queues) { + error_report("virtio-net: curr_queues %x > max_queues %x", + tmp->parent->curr_queues, tmp->parent->max_queues); + + return -EINVAL; + } + + return 0; /* all good */ +} + +static const VMStateDescription vmstate_virtio_net_tx_waiting =3D { + .name =3D "virtio-net-tx_waiting", + .pre_load =3D virtio_net_tx_waiting_pre_load, + .pre_save =3D virtio_net_tx_waiting_pre_save, + .fields =3D (VMStateField[]) { + VMSTATE_STRUCT_VARRAY_POINTER_UINT16(vqs_1, struct VirtIONetMigTmp, + curr_queues_1, + vmstate_virtio_net_queue_tx_waiting, + struct VirtIONetQueue), + VMSTATE_END_OF_LIST() + }, +}; + +/* the 'has_ufo' flag is just tested; if the incoming stream has the + * flag set we need to check that we have it + */ +static int virtio_net_ufo_post_load(void *opaque, int version_id) +{ + struct VirtIONetMigTmp *tmp =3D opaque; + + if (tmp->has_ufo && !peer_has_ufo(tmp->parent)) { + error_report("virtio-net: saved image requires TUN_F_UFO support"); + return -EINVAL; + } + + return 0; +} + +static void virtio_net_ufo_pre_save(void *opaque) +{ + struct VirtIONetMigTmp *tmp =3D opaque; + + tmp->has_ufo =3D tmp->parent->has_ufo; +} + +static const VMStateDescription vmstate_virtio_net_has_ufo =3D { + .name =3D "virtio-net-ufo", + .post_load =3D virtio_net_ufo_post_load, + .pre_save =3D virtio_net_ufo_pre_save, + .fields =3D (VMStateField[]) { + VMSTATE_UINT8(has_ufo, struct VirtIONetMigTmp), + VMSTATE_END_OF_LIST() + }, +}; + +/* the 'has_vnet_hdr' flag is just tested; if the incoming stream has the + * flag set we need to check that we have it + */ +static int virtio_net_vnet_post_load(void *opaque, int version_id) +{ + struct VirtIONetMigTmp *tmp =3D opaque; + + if (tmp->has_vnet_hdr && !peer_has_vnet_hdr(tmp->parent)) { + error_report("virtio-net: saved image requires vnet_hdr=3Don"); + return -EINVAL; + } + + return 0; +} + +static void virtio_net_vnet_pre_save(void *opaque) +{ + struct VirtIONetMigTmp *tmp =3D opaque; + + tmp->has_vnet_hdr =3D tmp->parent->has_vnet_hdr; +} + +static const VMStateDescription vmstate_virtio_net_has_vnet =3D { + .name =3D "virtio-net-vnet", + .post_load =3D virtio_net_vnet_post_load, + .pre_save =3D virtio_net_vnet_pre_save, + .fields =3D (VMStateField[]) { + VMSTATE_UINT32(has_vnet_hdr, struct VirtIONetMigTmp), + VMSTATE_END_OF_LIST() + }, +}; + +static const VMStateDescription vmstate_virtio_net_device =3D { + .name =3D "virtio-net-device", + .version_id =3D VIRTIO_NET_VM_VERSION, + .minimum_version_id =3D VIRTIO_NET_VM_VERSION, + .post_load =3D virtio_net_post_load_device, + .fields =3D (VMStateField[]) { + VMSTATE_UINT8_ARRAY(mac, VirtIONet, ETH_ALEN), + VMSTATE_STRUCT_POINTER(vqs, VirtIONet, + vmstate_virtio_net_queue_tx_waiting, + VirtIONetQueue), + VMSTATE_UINT32(mergeable_rx_bufs, VirtIONet), + VMSTATE_UINT16(status, VirtIONet), + VMSTATE_UINT8(promisc, VirtIONet), + VMSTATE_UINT8(allmulti, VirtIONet), + VMSTATE_UINT32(mac_table.in_use, VirtIONet), + + /* Guarded pair: If it fits we load it, else we throw it away + * - can happen if source has a larger MAC table.; post-load + * sets flags in this case. + */ + VMSTATE_VBUFFER_MULTIPLY(mac_table.macs, VirtIONet, + 0, mac_table_fits, 0, mac_table.in_use, + ETH_ALEN), + VMSTATE_UNUSED_VARRAY_UINT32(VirtIONet, mac_table_doesnt_fit, 0, + mac_table.in_use, ETH_ALEN), + + /* Note: This is an array of uint32's that's always been saved as a + * buffer; hold onto your endiannesses; it's actually used as a bi= tmap + * but based on the uint. + */ + VMSTATE_BUFFER_POINTER_UNSAFE(vlans, VirtIONet, 0, MAX_VLAN >> 3), + VMSTATE_WITH_TMP(VirtIONet, struct VirtIONetMigTmp, + vmstate_virtio_net_has_vnet), + VMSTATE_UINT8(mac_table.multi_overflow, VirtIONet), + VMSTATE_UINT8(mac_table.uni_overflow, VirtIONet), + VMSTATE_UINT8(alluni, VirtIONet), + VMSTATE_UINT8(nomulti, VirtIONet), + VMSTATE_UINT8(nouni, VirtIONet), + VMSTATE_UINT8(nobcast, VirtIONet), + VMSTATE_WITH_TMP(VirtIONet, struct VirtIONetMigTmp, + vmstate_virtio_net_has_ufo), + VMSTATE_SINGLE_TEST(max_queues, VirtIONet, max_queues_gt_1, 0, + vmstate_info_uint16_equal, uint16_t), + VMSTATE_UINT16_TEST(curr_queues, VirtIONet, max_queues_gt_1), + VMSTATE_WITH_TMP(VirtIONet, struct VirtIONetMigTmp, + vmstate_virtio_net_tx_waiting), + VMSTATE_UINT64_TEST(curr_guest_offloads, VirtIONet, + has_ctrl_guest_offloads), + VMSTATE_END_OF_LIST() + }, +}; + static NetClientInfo net_virtio_info =3D { .type =3D NET_CLIENT_DRIVER_NIC, .size =3D sizeof(NICState), @@ -1989,9 +2096,8 @@ static void virtio_net_class_init(ObjectClass *klass,= void *data) vdc->set_status =3D virtio_net_set_status; vdc->guest_notifier_mask =3D virtio_net_guest_notifier_mask; vdc->guest_notifier_pending =3D virtio_net_guest_notifier_pending; - vdc->load =3D virtio_net_load_device; - vdc->save =3D virtio_net_save_device; vdc->legacy_features |=3D (0x1 << VIRTIO_NET_F_GSO); + vdc->vmsd =3D &vmstate_virtio_net_device; } =20 static const TypeInfo virtio_net_info =3D { diff --git a/include/hw/virtio/virtio-net.h b/include/hw/virtio/virtio-net.h index 8ea56a8..1eec9a2 100644 --- a/include/hw/virtio/virtio-net.h +++ b/include/hw/virtio/virtio-net.h @@ -47,7 +47,7 @@ typedef struct VirtIONetQueue { VirtQueue *tx_vq; QEMUTimer *tx_timer; QEMUBH *tx_bh; - int tx_waiting; + uint32_t tx_waiting; struct { VirtQueueElement *elem; } async_tx; @@ -68,7 +68,7 @@ typedef struct VirtIONet { size_t guest_hdr_len; uint32_t host_features; uint8_t has_ufo; - int mergeable_rx_bufs; + uint32_t mergeable_rx_bufs; uint8_t promisc; uint8_t allmulti; uint8_t alluni; --=20 2.9.3