* [Qemu-devel] [PATCH 1/7] slirp: adapt a subset of QEMU vmstate code
2019-02-12 16:25 [Qemu-devel] [PATCH 0/7] slirp: make it a standalone project Marc-André Lureau
@ 2019-02-12 16:25 ` Marc-André Lureau
2019-02-12 16:25 ` [Qemu-devel] [PATCH 2/7] slirp: use libslirp migration code Marc-André Lureau
` (6 subsequent siblings)
7 siblings, 0 replies; 9+ messages in thread
From: Marc-André Lureau @ 2019-02-12 16:25 UTC (permalink / raw)
To: qemu-devel; +Cc: samuel.thibault, peter.maydell
Add vmstate serialization code adapted from QEMU.
Keep only the bits that are required for libslirp.
Introduce a IStream/OStream interface to replace QEMU QFile
abstraction.
Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
---
slirp/libslirp.h | 1 +
slirp/stream.h | 34 ++++
slirp/vmstate.h | 396 +++++++++++++++++++++++++++++++++++++++++++
slirp/stream.c | 119 +++++++++++++
slirp/vmstate.c | 401 ++++++++++++++++++++++++++++++++++++++++++++
slirp/Makefile.objs | 2 +
6 files changed, 953 insertions(+)
create mode 100644 slirp/stream.h
create mode 100644 slirp/vmstate.h
create mode 100644 slirp/stream.c
create mode 100644 slirp/vmstate.c
diff --git a/slirp/libslirp.h b/slirp/libslirp.h
index fccab42518..18c50bd7ba 100644
--- a/slirp/libslirp.h
+++ b/slirp/libslirp.h
@@ -26,6 +26,7 @@ enum {
SLIRP_POLL_HUP = 1 << 4,
};
+typedef ssize_t (*SlirpReadCb)(void *buf, size_t len, void *opaque);
typedef ssize_t (*SlirpWriteCb)(const void *buf, size_t len, void *opaque);
typedef void (*SlirpTimerCb)(void *opaque);
typedef int (*SlirpAddPollCb)(int fd, int events, void *opaque);
diff --git a/slirp/stream.h b/slirp/stream.h
new file mode 100644
index 0000000000..985334c043
--- /dev/null
+++ b/slirp/stream.h
@@ -0,0 +1,34 @@
+#ifndef STREAM_H_
+#define STREAM_H_
+
+#include "libslirp.h"
+
+typedef struct SlirpIStream {
+ SlirpReadCb read_cb;
+ void *opaque;
+} SlirpIStream;
+
+typedef struct SlirpOStream {
+ SlirpWriteCb write_cb;
+ void *opaque;
+} SlirpOStream;
+
+bool slirp_istream_read(SlirpIStream *f, void *buf, size_t size);
+bool slirp_ostream_write(SlirpOStream *f, const void *buf, size_t size);
+
+uint8_t slirp_istream_read_u8(SlirpIStream *f);
+bool slirp_ostream_write_u8(SlirpOStream *f, uint8_t b);
+
+uint16_t slirp_istream_read_u16(SlirpIStream *f);
+bool slirp_ostream_write_u16(SlirpOStream *f, uint16_t b);
+
+uint32_t slirp_istream_read_u32(SlirpIStream *f);
+bool slirp_ostream_write_u32(SlirpOStream *f, uint32_t b);
+
+int16_t slirp_istream_read_i16(SlirpIStream *f);
+bool slirp_ostream_write_i16(SlirpOStream *f, int16_t b);
+
+int32_t slirp_istream_read_i32(SlirpIStream *f);
+bool slirp_ostream_write_i32(SlirpOStream *f, int32_t b);
+
+#endif /* STREAM_H_ */
diff --git a/slirp/vmstate.h b/slirp/vmstate.h
new file mode 100644
index 0000000000..6ffad27de9
--- /dev/null
+++ b/slirp/vmstate.h
@@ -0,0 +1,396 @@
+/*
+ * QEMU migration/snapshot declarations
+ *
+ * Copyright (c) 2009-2011 Red Hat, Inc.
+ *
+ * Original author: Juan Quintela <quintela@redhat.com>
+ *
+ * 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.
+ */
+#ifndef VMSTATE_H_
+#define VMSTATE_H_
+
+#include <unistd.h>
+#include <stdint.h>
+#include <stdbool.h>
+#include "slirp.h"
+#include "stream.h"
+
+#define stringify(s) tostring(s)
+#define tostring(s) #s
+
+typedef struct VMStateInfo VMStateInfo;
+typedef struct VMStateDescription VMStateDescription;
+typedef struct VMStateField VMStateField;
+
+int slirp_vmstate_save_state(SlirpOStream *f, const VMStateDescription *vmsd,
+ void *opaque);
+int slirp_vmstate_load_state(SlirpIStream *f, const VMStateDescription *vmsd,
+ void *opaque, int version_id);
+
+/* VMStateInfo allows customized migration of objects that don't fit in
+ * any category in VMStateFlags. Additional information is always passed
+ * into get and put in terms of field and vmdesc parameters. However
+ * these two parameters should only be used in cases when customized
+ * handling is needed, such as QTAILQ. For primitive data types such as
+ * integer, field and vmdesc parameters should be ignored inside get/put.
+ */
+struct VMStateInfo {
+ const char *name;
+ int (*get)(SlirpIStream *f, void *pv, size_t size, VMStateField *field);
+ int (*put)(SlirpOStream *f, void *pv, size_t size, VMStateField *field);
+};
+
+enum VMStateFlags {
+ /* Ignored */
+ VMS_SINGLE = 0x001,
+
+ /* The struct member at opaque + VMStateField.offset is a pointer
+ * to the actual field (e.g. struct a { uint8_t *b;
+ * }). Dereference the pointer before using it as basis for
+ * further pointer arithmetic (see e.g. VMS_ARRAY). Does not
+ * affect the meaning of VMStateField.num_offset or
+ * VMStateField.size_offset; see VMS_VARRAY* and VMS_VBUFFER for
+ * those. */
+ VMS_POINTER = 0x002,
+
+ /* The field is an array of fixed size. VMStateField.num contains
+ * the number of entries in the array. The size of each entry is
+ * given by VMStateField.size and / or opaque +
+ * VMStateField.size_offset; see VMS_VBUFFER and
+ * VMS_MULTIPLY. Each array entry will be processed individually
+ * (VMStateField.info.get()/put() if VMS_STRUCT is not set,
+ * recursion into VMStateField.vmsd if VMS_STRUCT is set). May not
+ * be combined with VMS_VARRAY*. */
+ VMS_ARRAY = 0x004,
+
+ /* The field is itself a struct, containing one or more
+ * fields. Recurse into VMStateField.vmsd. Most useful in
+ * combination with VMS_ARRAY / VMS_VARRAY*, recursing into each
+ * array entry. */
+ VMS_STRUCT = 0x008,
+
+ /* The field is an array of variable size. The int32_t at opaque +
+ * VMStateField.num_offset contains the number of entries in the
+ * array. See the VMS_ARRAY description regarding array handling
+ * in general. May not be combined with VMS_ARRAY or any other
+ * VMS_VARRAY*. */
+ VMS_VARRAY_INT32 = 0x010,
+
+ /* Ignored */
+ VMS_BUFFER = 0x020,
+
+ /* The field is a (fixed-size or variable-size) array of pointers
+ * (e.g. struct a { uint8_t *b[]; }). Dereference each array entry
+ * before using it. Note: Does not imply any one of VMS_ARRAY /
+ * VMS_VARRAY*; these need to be set explicitly. */
+ VMS_ARRAY_OF_POINTER = 0x040,
+
+ /* The field is an array of variable size. The uint16_t at opaque
+ * + VMStateField.num_offset (subject to VMS_MULTIPLY_ELEMENTS)
+ * contains the number of entries in the array. See the VMS_ARRAY
+ * description regarding array handling in general. May not be
+ * combined with VMS_ARRAY or any other VMS_VARRAY*. */
+ VMS_VARRAY_UINT16 = 0x080,
+
+ /* The size of the individual entries (a single array entry if
+ * VMS_ARRAY or any of VMS_VARRAY* are set, or the field itself if
+ * neither is set) is variable (i.e. not known at compile-time),
+ * but the same for all entries. Use the int32_t at opaque +
+ * VMStateField.size_offset (subject to VMS_MULTIPLY) to determine
+ * the size of each (and every) entry. */
+ VMS_VBUFFER = 0x100,
+
+ /* Multiply the entry size given by the int32_t at opaque +
+ * VMStateField.size_offset (see VMS_VBUFFER description) with
+ * VMStateField.size to determine the number of bytes to be
+ * allocated. Only valid in combination with VMS_VBUFFER. */
+ VMS_MULTIPLY = 0x200,
+
+ /* The field is an array of variable size. The uint8_t at opaque +
+ * VMStateField.num_offset (subject to VMS_MULTIPLY_ELEMENTS)
+ * contains the number of entries in the array. See the VMS_ARRAY
+ * description regarding array handling in general. May not be
+ * combined with VMS_ARRAY or any other VMS_VARRAY*. */
+ VMS_VARRAY_UINT8 = 0x400,
+
+ /* The field is an array of variable size. The uint32_t at opaque
+ * + VMStateField.num_offset (subject to VMS_MULTIPLY_ELEMENTS)
+ * contains the number of entries in the array. See the VMS_ARRAY
+ * description regarding array handling in general. May not be
+ * combined with VMS_ARRAY or any other VMS_VARRAY*. */
+ VMS_VARRAY_UINT32 = 0x800,
+
+ /* Fail loading the serialised VM state if this field is missing
+ * from the input. */
+ VMS_MUST_EXIST = 0x1000,
+
+ /* When loading serialised VM state, allocate memory for the
+ * (entire) field. Only valid in combination with
+ * VMS_POINTER. Note: Not all combinations with other flags are
+ * currently supported, e.g. VMS_ALLOC|VMS_ARRAY_OF_POINTER won't
+ * cause the individual entries to be allocated. */
+ VMS_ALLOC = 0x2000,
+
+ /* Multiply the number of entries given by the integer at opaque +
+ * VMStateField.num_offset (see VMS_VARRAY*) with VMStateField.num
+ * to determine the number of entries in the array. Only valid in
+ * combination with one of VMS_VARRAY*. */
+ VMS_MULTIPLY_ELEMENTS = 0x4000,
+
+ /* A structure field that is like VMS_STRUCT, but uses
+ * VMStateField.struct_version_id to tell which version of the
+ * structure we are referencing to use. */
+ VMS_VSTRUCT = 0x8000,
+};
+
+struct VMStateField {
+ const char *name;
+ size_t offset;
+ size_t size;
+ size_t start;
+ int num;
+ size_t num_offset;
+ size_t size_offset;
+ const VMStateInfo *info;
+ enum VMStateFlags flags;
+ const VMStateDescription *vmsd;
+ int version_id;
+ int struct_version_id;
+ bool (*field_exists)(void *opaque, int version_id);
+};
+
+struct VMStateDescription {
+ const char *name;
+ int version_id;
+ int (*pre_load)(void *opaque);
+ int (*post_load)(void *opaque, int version_id);
+ int (*pre_save)(void *opaque);
+ VMStateField *fields;
+};
+
+
+extern const VMStateInfo slirp_vmstate_info_int16;
+extern const VMStateInfo slirp_vmstate_info_int32;
+extern const VMStateInfo slirp_vmstate_info_uint8;
+extern const VMStateInfo slirp_vmstate_info_uint16;
+extern const VMStateInfo slirp_vmstate_info_uint32;
+
+/** Put this in the stream when migrating a null pointer.*/
+#define VMS_NULLPTR_MARKER (0x30U) /* '0' */
+extern const VMStateInfo slirp_vmstate_info_nullptr;
+
+extern const VMStateInfo slirp_vmstate_info_buffer;
+extern const VMStateInfo slirp_vmstate_info_tmp;
+
+#define type_check_array(t1,t2,n) ((t1(*)[n])0 - (t2*)0)
+#define type_check_pointer(t1,t2) ((t1**)0 - (t2*)0)
+#define typeof_field(type, field) typeof(((type *)0)->field)
+#define type_check(t1,t2) ((t1*)0 - (t2*)0)
+
+#define vmstate_offset_value(_state, _field, _type) \
+ (offsetof(_state, _field) + \
+ type_check(_type, typeof_field(_state, _field)))
+
+#define vmstate_offset_pointer(_state, _field, _type) \
+ (offsetof(_state, _field) + \
+ type_check_pointer(_type, typeof_field(_state, _field)))
+
+#define vmstate_offset_array(_state, _field, _type, _num) \
+ (offsetof(_state, _field) + \
+ type_check_array(_type, typeof_field(_state, _field), _num))
+
+#define vmstate_offset_buffer(_state, _field) \
+ vmstate_offset_array(_state, _field, uint8_t, \
+ sizeof(typeof_field(_state, _field)))
+
+/* In the macros below, if there is a _version, that means the macro's
+ * field will be processed only if the version being received is >=
+ * the _version specified. In general, if you add a new field, you
+ * would increment the structure's version and put that version
+ * number into the new field so it would only be processed with the
+ * new version.
+ *
+ * In particular, for VMSTATE_STRUCT() and friends the _version does
+ * *NOT* pick the version of the sub-structure. It works just as
+ * specified above. The version of the top-level structure received
+ * is passed down to all sub-structures. This means that the
+ * sub-structures must have version that are compatible with all the
+ * structures that use them.
+ *
+ * If you want to specify the version of the sub-structure, use
+ * VMSTATE_VSTRUCT(), which allows the specific sub-structure version
+ * to be directly specified.
+ */
+
+#define VMSTATE_SINGLE_TEST(_field, _state, _test, _version, _info, _type) { \
+ .name = (stringify(_field)), \
+ .version_id = (_version), \
+ .field_exists = (_test), \
+ .size = sizeof(_type), \
+ .info = &(_info), \
+ .flags = VMS_SINGLE, \
+ .offset = vmstate_offset_value(_state, _field, _type), \
+}
+
+#define VMSTATE_ARRAY(_field, _state, _num, _version, _info, _type) {\
+ .name = (stringify(_field)), \
+ .version_id = (_version), \
+ .num = (_num), \
+ .info = &(_info), \
+ .size = sizeof(_type), \
+ .flags = VMS_ARRAY, \
+ .offset = vmstate_offset_array(_state, _field, _type, _num), \
+}
+
+#define VMSTATE_STRUCT_TEST(_field, _state, _test, _version, _vmsd, _type) { \
+ .name = (stringify(_field)), \
+ .version_id = (_version), \
+ .field_exists = (_test), \
+ .vmsd = &(_vmsd), \
+ .size = sizeof(_type), \
+ .flags = VMS_STRUCT, \
+ .offset = vmstate_offset_value(_state, _field, _type), \
+}
+
+#define VMSTATE_STRUCT_POINTER_V(_field, _state, _version, _vmsd, _type) { \
+ .name = (stringify(_field)), \
+ .version_id = (_version), \
+ .vmsd = &(_vmsd), \
+ .size = sizeof(_type *), \
+ .flags = VMS_STRUCT|VMS_POINTER, \
+ .offset = vmstate_offset_pointer(_state, _field, _type), \
+}
+
+#define VMSTATE_STRUCT_ARRAY_TEST(_field, _state, _num, _test, _version, _vmsd, _type) { \
+ .name = (stringify(_field)), \
+ .num = (_num), \
+ .field_exists = (_test), \
+ .version_id = (_version), \
+ .vmsd = &(_vmsd), \
+ .size = sizeof(_type), \
+ .flags = VMS_STRUCT|VMS_ARRAY, \
+ .offset = vmstate_offset_array(_state, _field, _type, _num),\
+}
+
+#define VMSTATE_STATIC_BUFFER(_field, _state, _version, _test, _start, _size) { \
+ .name = (stringify(_field)), \
+ .version_id = (_version), \
+ .field_exists = (_test), \
+ .size = (_size - _start), \
+ .info = &slirp_vmstate_info_buffer, \
+ .flags = VMS_BUFFER, \
+ .offset = vmstate_offset_buffer(_state, _field) + _start, \
+}
+
+#define VMSTATE_VBUFFER_UINT32(_field, _state, _version, _test, _field_size) { \
+ .name = (stringify(_field)), \
+ .version_id = (_version), \
+ .field_exists = (_test), \
+ .size_offset = vmstate_offset_value(_state, _field_size, uint32_t),\
+ .info = &slirp_vmstate_info_buffer, \
+ .flags = VMS_VBUFFER|VMS_POINTER, \
+ .offset = offsetof(_state, _field), \
+}
+
+#define QEMU_BUILD_BUG_ON_STRUCT(x) \
+ struct { \
+ int:(x) ? -1 : 1; \
+ }
+
+#define QEMU_BUILD_BUG_ON_ZERO(x) (sizeof(QEMU_BUILD_BUG_ON_STRUCT(x)) - \
+ sizeof(QEMU_BUILD_BUG_ON_STRUCT(x)))
+
+/* 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 = "tmp", \
+ .size = sizeof(_tmp_type) + \
+ QEMU_BUILD_BUG_ON_ZERO(offsetof(_tmp_type, parent) != 0) + \
+ type_check_pointer(_state, \
+ typeof_field(_tmp_type, parent)), \
+ .vmsd = &(_vmsd), \
+ .info = &slirp_vmstate_info_tmp, \
+}
+
+#define VMSTATE_SINGLE(_field, _state, _version, _info, _type) \
+ VMSTATE_SINGLE_TEST(_field, _state, NULL, _version, _info, _type)
+
+#define VMSTATE_STRUCT(_field, _state, _version, _vmsd, _type) \
+ VMSTATE_STRUCT_TEST(_field, _state, NULL, _version, _vmsd, _type)
+
+#define VMSTATE_STRUCT_POINTER(_field, _state, _vmsd, _type) \
+ VMSTATE_STRUCT_POINTER_V(_field, _state, 0, _vmsd, _type)
+
+#define VMSTATE_STRUCT_ARRAY(_field, _state, _num, _version, _vmsd, _type) \
+ VMSTATE_STRUCT_ARRAY_TEST(_field, _state, _num, NULL, _version, \
+ _vmsd, _type)
+
+#define VMSTATE_INT16_V(_f, _s, _v) \
+ VMSTATE_SINGLE(_f, _s, _v, slirp_vmstate_info_int16, int16_t)
+#define VMSTATE_INT32_V(_f, _s, _v) \
+ VMSTATE_SINGLE(_f, _s, _v, slirp_vmstate_info_int32, int32_t)
+
+#define VMSTATE_UINT8_V(_f, _s, _v) \
+ VMSTATE_SINGLE(_f, _s, _v, slirp_vmstate_info_uint8, uint8_t)
+#define VMSTATE_UINT16_V(_f, _s, _v) \
+ VMSTATE_SINGLE(_f, _s, _v, slirp_vmstate_info_uint16, uint16_t)
+#define VMSTATE_UINT32_V(_f, _s, _v) \
+ VMSTATE_SINGLE(_f, _s, _v, slirp_vmstate_info_uint32, uint32_t)
+
+#define VMSTATE_INT16(_f, _s) \
+ VMSTATE_INT16_V(_f, _s, 0)
+#define VMSTATE_INT32(_f, _s) \
+ VMSTATE_INT32_V(_f, _s, 0)
+
+#define VMSTATE_UINT8(_f, _s) \
+ VMSTATE_UINT8_V(_f, _s, 0)
+#define VMSTATE_UINT16(_f, _s) \
+ VMSTATE_UINT16_V(_f, _s, 0)
+#define VMSTATE_UINT32(_f, _s) \
+ VMSTATE_UINT32_V(_f, _s, 0)
+
+#define VMSTATE_UINT16_TEST(_f, _s, _t) \
+ VMSTATE_SINGLE_TEST(_f, _s, _t, 0, slirp_vmstate_info_uint16, uint16_t)
+
+#define VMSTATE_UINT32_TEST(_f, _s, _t) \
+ VMSTATE_SINGLE_TEST(_f, _s, _t, 0, slirp_vmstate_info_uint32, uint32_t)
+
+#define VMSTATE_INT16_ARRAY_V(_f, _s, _n, _v) \
+ VMSTATE_ARRAY(_f, _s, _n, _v, slirp_vmstate_info_int16, int16_t)
+
+#define VMSTATE_INT16_ARRAY(_f, _s, _n) \
+ VMSTATE_INT16_ARRAY_V(_f, _s, _n, 0)
+
+#define VMSTATE_BUFFER_V(_f, _s, _v) \
+ VMSTATE_STATIC_BUFFER(_f, _s, _v, NULL, 0, sizeof(typeof_field(_s, _f)))
+
+#define VMSTATE_BUFFER(_f, _s) \
+ VMSTATE_BUFFER_V(_f, _s, 0)
+
+#define VMSTATE_END_OF_LIST() \
+ {}
+
+#endif
diff --git a/slirp/stream.c b/slirp/stream.c
new file mode 100644
index 0000000000..d114dde334
--- /dev/null
+++ b/slirp/stream.c
@@ -0,0 +1,119 @@
+/*
+ * libslirp io streams
+ *
+ * Copyright (c) 2018 Red Hat, Inc.
+ *
+ * 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 "stream.h"
+#include <glib.h>
+
+bool slirp_istream_read(SlirpIStream *f, void *buf, size_t size)
+{
+ return f->read_cb(buf, size, f->opaque) == size;
+}
+
+bool slirp_ostream_write(SlirpOStream *f, const void *buf, size_t size)
+{
+ return f->write_cb(buf, size, f->opaque) == size;
+}
+
+uint8_t slirp_istream_read_u8(SlirpIStream *f)
+{
+ uint8_t b;
+
+ if (slirp_istream_read(f, &b, sizeof(b))) {
+ return b;
+ }
+
+ return 0;
+}
+
+bool slirp_ostream_write_u8(SlirpOStream *f, uint8_t b)
+{
+ return slirp_ostream_write(f, &b, sizeof(b));
+}
+
+uint16_t slirp_istream_read_u16(SlirpIStream *f)
+{
+ uint16_t b;
+
+ if (slirp_istream_read(f, &b, sizeof(b))) {
+ return GUINT16_FROM_BE(b);
+ }
+
+ return 0;
+}
+
+bool slirp_ostream_write_u16(SlirpOStream *f, uint16_t b)
+{
+ b = GUINT16_TO_BE(b);
+ return slirp_ostream_write(f, &b, sizeof(b));
+}
+
+uint32_t slirp_istream_read_u32(SlirpIStream *f)
+{
+ uint32_t b;
+
+ if (slirp_istream_read(f, &b, sizeof(b))) {
+ return GUINT32_FROM_BE(b);
+ }
+
+ return 0;
+}
+
+bool slirp_ostream_write_u32(SlirpOStream *f, uint32_t b)
+{
+ b = GUINT32_TO_BE(b);
+ return slirp_ostream_write(f, &b, sizeof(b));
+}
+
+int16_t slirp_istream_read_i16(SlirpIStream *f)
+{
+ int16_t b;
+
+ if (slirp_istream_read(f, &b, sizeof(b))) {
+ return GINT16_FROM_BE(b);
+ }
+
+ return 0;
+}
+
+bool slirp_ostream_write_i16(SlirpOStream *f, int16_t b)
+{
+ b = GINT16_TO_BE(b);
+ return slirp_ostream_write(f, &b, sizeof(b));
+}
+
+int32_t slirp_istream_read_i32(SlirpIStream *f)
+{
+ int32_t b;
+
+ if (slirp_istream_read(f, &b, sizeof(b))) {
+ return GINT32_FROM_BE(b);
+ }
+
+ return 0;
+}
+
+bool slirp_ostream_write_i32(SlirpOStream *f, int32_t b)
+{
+ b = GINT32_TO_BE(b);
+ return slirp_ostream_write(f, &b, sizeof(b));
+}
diff --git a/slirp/vmstate.c b/slirp/vmstate.c
new file mode 100644
index 0000000000..26b6399ec7
--- /dev/null
+++ b/slirp/vmstate.c
@@ -0,0 +1,401 @@
+/*
+ * VMState interpreter
+ *
+ * Copyright (c) 2009-2018 Red Hat Inc
+ *
+ * Authors:
+ * Juan Quintela <quintela@redhat.com>
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or later.
+ * See the COPYING file in the top-level directory.
+ */
+#include <assert.h>
+#include <errno.h>
+#include <string.h>
+#include <glib.h>
+
+#include "stream.h"
+#include "vmstate.h"
+
+static int get_nullptr(SlirpIStream *f, void *pv, size_t size,
+ VMStateField *field)
+{
+ if (slirp_istream_read_u8(f) == VMS_NULLPTR_MARKER) {
+ return 0;
+ }
+ g_warning("vmstate: get_nullptr expected VMS_NULLPTR_MARKER");
+ return -EINVAL;
+}
+
+static int put_nullptr(SlirpOStream *f, void *pv, size_t size,
+ VMStateField *field)
+
+{
+ if (pv == NULL) {
+ slirp_ostream_write_u8(f, VMS_NULLPTR_MARKER);
+ return 0;
+ }
+ g_warning("vmstate: put_nullptr must be called with pv == NULL");
+ return -EINVAL;
+}
+
+const VMStateInfo slirp_vmstate_info_nullptr = {
+ .name = "uint64",
+ .get = get_nullptr,
+ .put = put_nullptr,
+};
+
+static int get_uint8(SlirpIStream *f, void *pv, size_t size, VMStateField *field)
+{
+ uint8_t *v = pv;
+ *v = slirp_istream_read_u8(f);
+ return 0;
+}
+
+static int put_uint8(SlirpOStream *f, void *pv, size_t size, VMStateField *field)
+{
+ uint8_t *v = pv;
+ slirp_ostream_write_u8(f, *v);
+ return 0;
+}
+
+const VMStateInfo slirp_vmstate_info_uint8 = {
+ .name = "uint8",
+ .get = get_uint8,
+ .put = put_uint8,
+};
+
+static int get_uint16(SlirpIStream *f, void *pv, size_t size,
+ VMStateField *field)
+{
+ uint16_t *v = pv;
+ *v = slirp_istream_read_u16(f);
+ return 0;
+}
+
+static int put_uint16(SlirpOStream *f, void *pv, size_t size,
+ VMStateField *field)
+{
+ uint16_t *v = pv;
+ slirp_ostream_write_u16(f, *v);
+ return 0;
+}
+
+const VMStateInfo slirp_vmstate_info_uint16 = {
+ .name = "uint16",
+ .get = get_uint16,
+ .put = put_uint16,
+};
+
+static int get_uint32(SlirpIStream *f, void *pv, size_t size,
+ VMStateField *field)
+{
+ uint32_t *v = pv;
+ *v = slirp_istream_read_u32(f);
+ return 0;
+}
+
+static int put_uint32(SlirpOStream *f, void *pv, size_t size,
+ VMStateField *field)
+{
+ uint32_t *v = pv;
+ slirp_ostream_write_u32(f, *v);
+ return 0;
+}
+
+const VMStateInfo slirp_vmstate_info_uint32 = {
+ .name = "uint32",
+ .get = get_uint32,
+ .put = put_uint32,
+};
+
+static int get_int16(SlirpIStream *f, void *pv, size_t size, VMStateField *field)
+{
+ int16_t *v = pv;
+ *v = slirp_istream_read_i16(f);
+ return 0;
+}
+
+static int put_int16(SlirpOStream *f, void *pv, size_t size, VMStateField *field)
+{
+ int16_t *v = pv;
+ slirp_ostream_write_i16(f, *v);
+ return 0;
+}
+
+const VMStateInfo slirp_vmstate_info_int16 = {
+ .name = "int16",
+ .get = get_int16,
+ .put = put_int16,
+};
+
+static int get_int32(SlirpIStream *f, void *pv, size_t size, VMStateField *field)
+{
+ int32_t *v = pv;
+ *v = slirp_istream_read_i32(f);
+ return 0;
+}
+
+static int put_int32(SlirpOStream *f, void *pv, size_t size, VMStateField *field)
+{
+ int32_t *v = pv;
+ slirp_ostream_write_i32(f, *v);
+ return 0;
+}
+
+const VMStateInfo slirp_vmstate_info_int32 = {
+ .name = "int32",
+ .get = get_int32,
+ .put = put_int32,
+};
+
+/* 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(SlirpIStream *f, void *pv, size_t size, VMStateField *field)
+{
+ int ret;
+ const VMStateDescription *vmsd = field->vmsd;
+ int version_id = field->version_id;
+ void *tmp = g_malloc(size);
+
+ /* Writes the parent field which is at the start of the tmp */
+ *(void **)tmp = pv;
+ ret = slirp_vmstate_load_state(f, vmsd, tmp, version_id);
+ g_free(tmp);
+ return ret;
+}
+
+static int put_tmp(SlirpOStream *f, void *pv, size_t size, VMStateField *field)
+{
+ const VMStateDescription *vmsd = field->vmsd;
+ void *tmp = g_malloc(size);
+ int ret;
+
+ /* Writes the parent field which is at the start of the tmp */
+ *(void **)tmp = pv;
+ ret = slirp_vmstate_save_state(f, vmsd, tmp);
+ g_free(tmp);
+
+ return ret;
+}
+
+const VMStateInfo slirp_vmstate_info_tmp = {
+ .name = "tmp",
+ .get = get_tmp,
+ .put = put_tmp,
+};
+
+static int get_buffer(SlirpIStream *f, void *pv, size_t size,
+ VMStateField *field)
+{
+ slirp_istream_read(f, pv, size);
+ return 0;
+}
+
+static int put_buffer(SlirpOStream *f, void *pv, size_t size,
+ VMStateField *field)
+{
+ slirp_ostream_write(f, pv, size);
+ return 0;
+}
+
+const VMStateInfo slirp_vmstate_info_buffer = {
+ .name = "buffer",
+ .get = get_buffer,
+ .put = put_buffer,
+};
+
+static int vmstate_n_elems(void *opaque, VMStateField *field)
+{
+ int n_elems = 1;
+
+ if (field->flags & VMS_ARRAY) {
+ n_elems = field->num;
+ } else if (field->flags & VMS_VARRAY_INT32) {
+ n_elems = *(int32_t *)(opaque + field->num_offset);
+ } else if (field->flags & VMS_VARRAY_UINT32) {
+ n_elems = *(uint32_t *)(opaque + field->num_offset);
+ } else if (field->flags & VMS_VARRAY_UINT16) {
+ n_elems = *(uint16_t *)(opaque + field->num_offset);
+ } else if (field->flags & VMS_VARRAY_UINT8) {
+ n_elems = *(uint8_t *)(opaque + field->num_offset);
+ }
+
+ if (field->flags & VMS_MULTIPLY_ELEMENTS) {
+ n_elems *= field->num;
+ }
+
+ return n_elems;
+}
+
+static int vmstate_size(void *opaque, VMStateField *field)
+{
+ int size = field->size;
+
+ if (field->flags & VMS_VBUFFER) {
+ size = *(int32_t *)(opaque + field->size_offset);
+ if (field->flags & VMS_MULTIPLY) {
+ size *= field->size;
+ }
+ }
+
+ return size;
+}
+
+static int
+vmstate_save_state_v(SlirpOStream *f, const VMStateDescription *vmsd,
+ void *opaque, int version_id)
+{
+ int ret = 0;
+ VMStateField *field = vmsd->fields;
+
+ if (vmsd->pre_save) {
+ ret = vmsd->pre_save(opaque);
+ if (ret) {
+ g_warning("pre-save failed: %s", vmsd->name);
+ return ret;
+ }
+ }
+
+ while (field->name) {
+ if ((field->field_exists &&
+ field->field_exists(opaque, version_id)) ||
+ (!field->field_exists &&
+ field->version_id <= version_id)) {
+ void *first_elem = opaque + field->offset;
+ int i, n_elems = vmstate_n_elems(opaque, field);
+ int size = vmstate_size(opaque, field);
+
+ if (field->flags & VMS_POINTER) {
+ first_elem = *(void **)first_elem;
+ assert(first_elem || !n_elems || !size);
+ }
+ for (i = 0; i < n_elems; i++) {
+ void *curr_elem = first_elem + size * i;
+ ret = 0;
+
+ if (field->flags & VMS_ARRAY_OF_POINTER) {
+ assert(curr_elem);
+ curr_elem = *(void **)curr_elem;
+ }
+ if (!curr_elem && size) {
+ /* if null pointer write placeholder and do not follow */
+ assert(field->flags & VMS_ARRAY_OF_POINTER);
+ ret = slirp_vmstate_info_nullptr.put(f, curr_elem, size, NULL);
+ } else if (field->flags & VMS_STRUCT) {
+ ret = slirp_vmstate_save_state(f, field->vmsd, curr_elem);
+ } else if (field->flags & VMS_VSTRUCT) {
+ ret = vmstate_save_state_v(f, field->vmsd, curr_elem,
+ field->struct_version_id);
+ } else {
+ ret = field->info->put(f, curr_elem, size, field);
+ }
+ if (ret) {
+ g_warning("Save of field %s/%s failed",
+ vmsd->name, field->name);
+ return ret;
+ }
+ }
+ } else {
+ if (field->flags & VMS_MUST_EXIST) {
+ g_warning("Output state validation failed: %s/%s",
+ vmsd->name, field->name);
+ assert(!(field->flags & VMS_MUST_EXIST));
+ }
+ }
+ field++;
+ }
+
+ return 0;
+}
+
+int slirp_vmstate_save_state(SlirpOStream *f, const VMStateDescription *vmsd,
+ void *opaque)
+{
+ return vmstate_save_state_v(f, vmsd, opaque, vmsd->version_id);
+}
+
+static void vmstate_handle_alloc(void *ptr, VMStateField *field, void *opaque)
+{
+ if (field->flags & VMS_POINTER && field->flags & VMS_ALLOC) {
+ size_t size = vmstate_size(opaque, field);
+ size *= vmstate_n_elems(opaque, field);
+ if (size) {
+ *(void **)ptr = g_malloc(size);
+ }
+ }
+}
+
+int slirp_vmstate_load_state(SlirpIStream *f, const VMStateDescription *vmsd,
+ void *opaque, int version_id)
+{
+ VMStateField *field = vmsd->fields;
+ int ret = 0;
+
+ if (version_id > vmsd->version_id) {
+ g_warning("%s: incoming version_id %d is too new "
+ "for local version_id %d",
+ vmsd->name, version_id, vmsd->version_id);
+ return -EINVAL;
+ }
+ if (vmsd->pre_load) {
+ int ret = vmsd->pre_load(opaque);
+ if (ret) {
+ return ret;
+ }
+ }
+ while (field->name) {
+ if ((field->field_exists &&
+ field->field_exists(opaque, version_id)) ||
+ (!field->field_exists &&
+ field->version_id <= version_id)) {
+ void *first_elem = opaque + field->offset;
+ int i, n_elems = vmstate_n_elems(opaque, field);
+ int size = vmstate_size(opaque, field);
+
+ vmstate_handle_alloc(first_elem, field, opaque);
+ if (field->flags & VMS_POINTER) {
+ first_elem = *(void **)first_elem;
+ assert(first_elem || !n_elems || !size);
+ }
+ for (i = 0; i < n_elems; i++) {
+ void *curr_elem = first_elem + size * i;
+
+ if (field->flags & VMS_ARRAY_OF_POINTER) {
+ curr_elem = *(void **)curr_elem;
+ }
+ if (!curr_elem && size) {
+ /* if null pointer check placeholder and do not follow */
+ assert(field->flags & VMS_ARRAY_OF_POINTER);
+ ret = slirp_vmstate_info_nullptr.get(f, curr_elem, size, NULL);
+ } else if (field->flags & VMS_STRUCT) {
+ ret = slirp_vmstate_load_state(f, field->vmsd, curr_elem,
+ field->vmsd->version_id);
+ } else if (field->flags & VMS_VSTRUCT) {
+ ret = slirp_vmstate_load_state(f, field->vmsd, curr_elem,
+ field->struct_version_id);
+ } else {
+ ret = field->info->get(f, curr_elem, size, field);
+ }
+ if (ret < 0) {
+ g_warning("Failed to load %s:%s", vmsd->name,
+ field->name);
+ return ret;
+ }
+ }
+ } else if (field->flags & VMS_MUST_EXIST) {
+ g_warning("Input validation failed: %s/%s",
+ vmsd->name, field->name);
+ return -1;
+ }
+ field++;
+ }
+ if (vmsd->post_load) {
+ ret = vmsd->post_load(opaque, version_id);
+ }
+ return ret;
+}
diff --git a/slirp/Makefile.objs b/slirp/Makefile.objs
index 88340a583b..69e140f965 100644
--- a/slirp/Makefile.objs
+++ b/slirp/Makefile.objs
@@ -21,6 +21,7 @@ slirp.mo-objs = \
slirp.o \
socket.o \
state.o \
+ stream.o \
tcp_input.o \
tcp_output.o \
tcp_subr.o \
@@ -29,6 +30,7 @@ slirp.mo-objs = \
udp.o \
udp6.o \
util.o \
+ vmstate.o \
$(NULL)
slirp.mo-cflags = -DG_LOG_DOMAIN=\"Slirp\" -DWITH_QEMU
--
2.21.0.rc0.1.g036caf7885
^ permalink raw reply related [flat|nested] 9+ messages in thread* [Qemu-devel] [PATCH 2/7] slirp: use libslirp migration code
2019-02-12 16:25 [Qemu-devel] [PATCH 0/7] slirp: make it a standalone project Marc-André Lureau
2019-02-12 16:25 ` [Qemu-devel] [PATCH 1/7] slirp: adapt a subset of QEMU vmstate code Marc-André Lureau
@ 2019-02-12 16:25 ` Marc-André Lureau
2019-02-12 16:25 ` [Qemu-devel] [PATCH 3/7] slirp: use "slirp_" prefix for inet_aton() win32 implementation Marc-André Lureau
` (5 subsequent siblings)
7 siblings, 0 replies; 9+ messages in thread
From: Marc-André Lureau @ 2019-02-12 16:25 UTC (permalink / raw)
To: qemu-devel; +Cc: samuel.thibault, peter.maydell
slirp migration code uses QEMU vmstate so far, when building WITH_QEMU.
Introduce slirp_state_{load,save,version}() functions to move the
state saving handling to libslirp side.
So far, the bitstream compatibility should remain equal with current
QEMU, as this is effectively using the same code, with the same format
etc. When libslirp is made standalone, we will need some mechanism to
ensure bitstream compatibility regardless of the libslirp version
installed. See the FIXME note in the code.
Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
---
include/migration/qemu-file-types.h | 2 ++
migration/qemu-file.h | 1 -
slirp/libslirp.h | 8 +++++
slirp/state.h | 9 -----
net/slirp.c | 55 +++++++++++++++++++++++++++++
slirp/slirp.c | 9 -----
slirp/state.c | 52 ++++++++++++---------------
7 files changed, 88 insertions(+), 48 deletions(-)
diff --git a/include/migration/qemu-file-types.h b/include/migration/qemu-file-types.h
index bd6d7dd7f9..bbe04d4484 100644
--- a/include/migration/qemu-file-types.h
+++ b/include/migration/qemu-file-types.h
@@ -25,6 +25,8 @@
#ifndef QEMU_FILE_H
#define QEMU_FILE_H
+int qemu_file_get_error(QEMUFile *f);
+
void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, size_t size);
void qemu_put_byte(QEMUFile *f, int v);
diff --git a/migration/qemu-file.h b/migration/qemu-file.h
index 2ccfcfb2a8..13baf896bd 100644
--- a/migration/qemu-file.h
+++ b/migration/qemu-file.h
@@ -149,7 +149,6 @@ void qemu_update_position(QEMUFile *f, size_t size);
void qemu_file_reset_rate_limit(QEMUFile *f);
void qemu_file_set_rate_limit(QEMUFile *f, int64_t new_rate);
int64_t qemu_file_get_rate_limit(QEMUFile *f);
-int qemu_file_get_error(QEMUFile *f);
void qemu_file_set_error(QEMUFile *f, int ret);
int qemu_file_shutdown(QEMUFile *f);
QEMUFile *qemu_file_get_return_path(QEMUFile *f);
diff --git a/slirp/libslirp.h b/slirp/libslirp.h
index 18c50bd7ba..7ce1e4d0d4 100644
--- a/slirp/libslirp.h
+++ b/slirp/libslirp.h
@@ -101,6 +101,14 @@ void slirp_socket_recv(Slirp *slirp, struct in_addr guest_addr,
int guest_port, const uint8_t *buf, int size);
size_t slirp_socket_can_recv(Slirp *slirp, struct in_addr guest_addr,
int guest_port);
+
+void slirp_state_save(Slirp *s, SlirpWriteCb write_cb, void *opaque);
+
+int slirp_state_load(Slirp *s, int version_id,
+ SlirpReadCb read_cb, void *opaque);
+
+int slirp_state_version(void);
+
#ifdef __cplusplus
} /* extern "C" */
#endif
diff --git a/slirp/state.h b/slirp/state.h
index 154866898f..e69de29bb2 100644
--- a/slirp/state.h
+++ b/slirp/state.h
@@ -1,9 +0,0 @@
-#ifndef SLIRP_STATE_H_
-#define SLIRP_STATE_H_
-
-#include "libslirp.h"
-
-void slirp_state_register(Slirp *slirp);
-void slirp_state_unregister(Slirp *slirp);
-
-#endif /* SLIRP_STATE_H_ */
diff --git a/net/slirp.c b/net/slirp.c
index 7a16d8d615..bcd91475d2 100644
--- a/net/slirp.c
+++ b/net/slirp.c
@@ -44,6 +44,8 @@
#include "qapi/error.h"
#include "qapi/qmp/qdict.h"
#include "util.h"
+#include "migration/register.h"
+#include "migration/qemu-file-types.h"
static int get_str_sep(char *buf, int buf_size, const char **pp, int sep)
{
@@ -146,6 +148,7 @@ static void net_slirp_cleanup(NetClientState *nc)
g_slist_free_full(s->fwd, slirp_free_fwd);
main_loop_poll_remove_notifier(&s->poll_notifier);
+ unregister_savevm(NULL, "slirp", s->slirp);
slirp_cleanup(s->slirp);
if (s->exit_notifier.notify) {
qemu_remove_exit_notifier(&s->exit_notifier);
@@ -303,6 +306,46 @@ static void net_slirp_poll_notify(Notifier *notifier, void *data)
}
}
+static ssize_t
+net_slirp_stream_read(void *buf, size_t size, void *opaque)
+{
+ QEMUFile *f = opaque;
+
+ return qemu_get_buffer(f, buf, size);
+}
+
+static ssize_t
+net_slirp_stream_write(const void *buf, size_t size, void *opaque)
+{
+ QEMUFile *f = opaque;
+
+ qemu_put_buffer(f, buf, size);
+ if (qemu_file_get_error(f)) {
+ return -1;
+ }
+
+ return size;
+}
+
+static int net_slirp_state_load(QEMUFile *f, void *opaque, int version_id)
+{
+ Slirp *slirp = opaque;
+
+ return slirp_state_load(slirp, version_id, net_slirp_stream_read, f);
+}
+
+static void net_slirp_state_save(QEMUFile *f, void *opaque)
+{
+ Slirp *slirp = opaque;
+
+ slirp_state_save(slirp, net_slirp_stream_write, f);
+}
+
+static SaveVMHandlers savevm_slirp_state = {
+ .save_state = net_slirp_state_save,
+ .load_state = net_slirp_state_load,
+};
+
static int net_slirp_init(NetClientState *peer, const char *model,
const char *name, int restricted,
bool ipv4, const char *vnetwork, const char *vhost,
@@ -523,6 +566,18 @@ static int net_slirp_init(NetClientState *peer, const char *model,
&slirp_cb, s);
QTAILQ_INSERT_TAIL(&slirp_stacks, s, entry);
+ /*
+ * Make sure the current bitstream version of slirp is 4, to avoid
+ * QEMU migration incompatibilities, if upstream slirp bumped the
+ * version.
+ *
+ * FIXME: use bitfileds of features? teach libslirp to save with
+ * specific version?
+ */
+ g_assert(slirp_state_version() == 4);
+ register_savevm_live(NULL, "slirp", 0, slirp_state_version(),
+ &savevm_slirp_state, s->slirp);
+
s->poll_notifier.notify = net_slirp_poll_notify;
main_loop_poll_add_notifier(&s->poll_notifier);
diff --git a/slirp/slirp.c b/slirp/slirp.c
index 55591430dc..2d8d5d34e4 100644
--- a/slirp/slirp.c
+++ b/slirp/slirp.c
@@ -23,9 +23,6 @@
*/
#include "slirp.h"
-#ifdef WITH_QEMU
-#include "state.h"
-#endif
#ifndef _WIN32
#include <net/if.h>
@@ -326,9 +323,6 @@ Slirp *slirp_init(int restricted, bool in_enabled, struct in_addr vnetwork,
translate_dnssearch(slirp, vdnssearch);
}
-#ifdef WITH_QEMU
- slirp_state_register(slirp);
-#endif
return slirp;
}
@@ -342,9 +336,6 @@ void slirp_cleanup(Slirp *slirp)
g_free(e);
}
-#ifdef WITH_QEMU
- slirp_state_unregister(slirp);
-#endif
ip_cleanup(slirp);
ip6_cleanup(slirp);
m_cleanup(slirp);
diff --git a/slirp/state.c b/slirp/state.c
index 0e5a706e87..f5dd80cdc8 100644
--- a/slirp/state.c
+++ b/slirp/state.c
@@ -21,13 +21,10 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
-#include "qemu/osdep.h"
-
#include "slirp.h"
+#include "vmstate.h"
#include "state.h"
-#include "migration/vmstate.h"
-#include "migration/qemu-file-types.h"
-#include "migration/register.h"
+#include "stream.h"
static int slirp_tcp_post_load(void *opaque, int version)
{
@@ -180,7 +177,7 @@ static int slirp_socket_pre_load(void *opaque)
#else
/* Win uses u_long rather than uint32_t - but it's still 32bits long */
#define VMSTATE_SIN4_ADDR(f, s, t) VMSTATE_SINGLE_TEST(f, s, t, 0, \
- vmstate_info_uint32, u_long)
+ slirp_vmstate_info_uint32, u_long)
#endif
/* The OS provided ss_family field isn't that portable; it's size
@@ -322,10 +319,13 @@ static const VMStateDescription vmstate_slirp = {
}
};
-static void slirp_state_save(QEMUFile *f, void *opaque)
+void slirp_state_save(Slirp *slirp, SlirpWriteCb write_cb, void *opaque)
{
- Slirp *slirp = opaque;
struct gfwd_list *ex_ptr;
+ SlirpOStream f = {
+ .write_cb = write_cb,
+ .opaque = opaque,
+ };
for (ex_ptr = slirp->guestfwd_list; ex_ptr; ex_ptr = ex_ptr->ex_next)
if (ex_ptr->write_cb) {
@@ -336,25 +336,29 @@ static void slirp_state_save(QEMUFile *f, void *opaque)
continue;
}
- qemu_put_byte(f, 42);
- vmstate_save_state(f, &vmstate_slirp_socket, so, NULL);
+ slirp_ostream_write_u8(&f, 42);
+ slirp_vmstate_save_state(&f, &vmstate_slirp_socket, so);
}
- qemu_put_byte(f, 0);
+ slirp_ostream_write_u8(&f, 0);
- vmstate_save_state(f, &vmstate_slirp, slirp, NULL);
+ slirp_vmstate_save_state(&f, &vmstate_slirp, slirp);
}
-static int slirp_state_load(QEMUFile *f, void *opaque, int version_id)
+int slirp_state_load(Slirp *slirp, int version_id,
+ SlirpReadCb read_cb, void *opaque)
{
- Slirp *slirp = opaque;
struct gfwd_list *ex_ptr;
+ SlirpIStream f = {
+ .read_cb = read_cb,
+ .opaque = opaque,
+ };
- while (qemu_get_byte(f)) {
+ while (slirp_istream_read_u8(&f)) {
int ret;
struct socket *so = socreate(slirp);
- ret = vmstate_load_state(f, &vmstate_slirp_socket, so, version_id);
+ ret = slirp_vmstate_load_state(&f, &vmstate_slirp_socket, so, version_id);
if (ret < 0) {
return ret;
}
@@ -375,20 +379,10 @@ static int slirp_state_load(QEMUFile *f, void *opaque, int version_id)
}
}
- return vmstate_load_state(f, &vmstate_slirp, slirp, version_id);
-}
-
-void slirp_state_register(Slirp *slirp)
-{
- static SaveVMHandlers savevm_slirp_state = {
- .save_state = slirp_state_save,
- .load_state = slirp_state_load,
- };
-
- register_savevm_live(NULL, "slirp", 0, 4, &savevm_slirp_state, slirp);
+ return slirp_vmstate_load_state(&f, &vmstate_slirp, slirp, version_id);
}
-void slirp_state_unregister(Slirp *slirp)
+int slirp_state_version(void)
{
- unregister_savevm(NULL, "slirp", slirp);
+ return 4;
}
--
2.21.0.rc0.1.g036caf7885
^ permalink raw reply related [flat|nested] 9+ messages in thread