public inbox for passt-dev@passt.top
 help / color / mirror / code / Atom feed
blob 6707c029a88e58d166a9a359f5dfd4e64aa2e6ed 5920 bytes (raw)

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
 
// SPDX-License-Identifier: GPL-2.0-or-later

/* PASST - Plug A Simple Socket Transport
 *  for qemu/UNIX domain socket mode
 *
 * PASTA - Pack A Subtle Tap Abstraction
 *  for network namespace/tap device mode
 *
 * migrate.c - Migration sections, layout, and routines
 *
 * Copyright (c) 2025 Red Hat GmbH
 * Author: Stefano Brivio <sbrivio@redhat.com>
 */

#include <errno.h>
#include <sys/uio.h>

#include "util.h"
#include "ip.h"
#include "passt.h"
#include "inany.h"
#include "flow.h"
#include "flow_table.h"

#include "migrate.h"

/* Current version of migration data */
#define MIGRATE_VERSION		1

/* Magic as we see it and as seen with reverse endianness */
#define MIGRATE_MAGIC		0xB1BB1D1B0BB1D1B0
#define MIGRATE_MAGIC_SWAPPED	0xB0D1B1B01B1DBBB1

/* Migration header to send from source */
static union migrate_header header = {
	.magic		= MIGRATE_MAGIC,
	.version	= htonl_constant(MIGRATE_VERSION),
	.time_t_size	= htonl_constant(sizeof(time_t)),
	.flow_size	= htonl_constant(sizeof(union flow)),
	.flow_sidx_size	= htonl_constant(sizeof(struct flow_sidx)),
	.voidp_size	= htonl_constant(sizeof(void *)),
};

/* Data sections for version 1 */
static struct iovec sections_v1[] = {
	{ &header,	sizeof(header) },
};

/* Set of data versions */
static struct migrate_data data_versions[] = {
	{
		1,	sections_v1,
	},
	{ 0 },
};

/* Handlers to call in source before sending data */
struct migrate_handler handlers_source_pre[] = {
	{ flow_migrate_source_pre },
	{ 0 },
};

/* Handlers to call in source after sending data */
struct migrate_handler handlers_source_post[] = {
	{ 0 },
};

/* Handlers to call in target before receiving data with version 1 */
struct migrate_handler handlers_target_pre_v1[] = {
	{ 0 },
};

/* Handlers to call in target after receiving data with version 1 */
struct migrate_handler handlers_target_post_v1[] = {
	{ 0 },
};

/* Versioned sets of migration handlers */
struct migrate_target_handlers target_handlers[] = {
	{
		1,
		handlers_target_pre_v1,
		handlers_target_post_v1,
	},
	{ 0 },
};

/**
 * migrate_source_pre() - Pre-migration tasks as source
 * @c:		Execution context
 * @m:		Migration metadata
 *
 * Return: 0 on success, error code on failure
 */
int migrate_source_pre(struct ctx *c, struct migrate_meta *m)
{
	struct migrate_handler *h;

	for (h = handlers_source_pre; h->fn; h++) {
		int rc;

		if ((rc = h->fn(c, m)))
			return rc;
	}

	return 0;
}

/**
 * migrate_source() - Perform migration as source: send state to hypervisor
 * @fd:		Descriptor for state transfer
 * @m:		Migration metadata
 *
 * Return: 0 on success, error code on failure
 */
int migrate_source(int fd, const struct migrate_meta *m)
{
	static struct migrate_data *d;
	int count, rc;

	(void)m;

	for (d = data_versions; d->v != MIGRATE_VERSION; d++);

	for (count = 0; d->sections[count].iov_len; count++);

	debug("Writing %u migration sections", count - 1 /* minus header */);
	rc = write_remainder(fd, d->sections, count, 0);
	if (rc < 0)
		return errno;

	return 0;
}

/**
 * migrate_source_post() - Post-migration tasks as source
 * @c:		Execution context
 * @m:		Migration metadata
 *
 * Return: 0 on success, error code on failure
 */
void migrate_source_post(struct ctx *c, struct migrate_meta *m)
{
	struct migrate_handler *h;

	for (h = handlers_source_post; h->fn; h++)
		h->fn(c, m);
}

/**
 * migrate_target_read_header() - Set metadata in target from source header
 * @fd:		Descriptor for state transfer
 * @m:		Migration metadata, filled on return
 *
 * Return: 0 on success, error code on failure
 */
int migrate_target_read_header(int fd, struct migrate_meta *m)
{
	static struct migrate_data *d;
	union migrate_header h;

	if (read_all_buf(fd, &h, sizeof(h)))
		return errno;

	debug("Source magic: 0x%016" PRIx64 ", sizeof(void *): %u, version: %u",
	      h.magic, ntohl(h.voidp_size), ntohl(h.version));

	for (d = data_versions; d->v != ntohl(h.version) && d->v; d++);
	if (!d->v)
		return ENOTSUP;
	m->v = d->v;

	if (h.magic == MIGRATE_MAGIC)
		m->bswap = false;
	else if (h.magic == MIGRATE_MAGIC_SWAPPED)
		m->bswap = true;
	else
		return ENOTSUP;

	if (ntohl(h.voidp_size) == 4)
		m->source_64b = false;
	else if (ntohl(h.voidp_size) == 8)
		m->source_64b = true;
	else
		return ENOTSUP;

	if (ntohl(h.time_t_size) == 4)
		m->time_64b = false;
	else if (ntohl(h.time_t_size) == 8)
		m->time_64b = true;
	else
		return ENOTSUP;

	m->flow_size = ntohl(h.flow_size);
	m->flow_sidx_size = ntohl(h.flow_sidx_size);

	return 0;
}

/**
 * migrate_target_pre() - Pre-migration tasks as target
 * @c:		Execution context
 * @m:		Migration metadata
 *
 * Return: 0 on success, error code on failure
 */
int migrate_target_pre(struct ctx *c, struct migrate_meta *m)
{
	struct migrate_target_handlers *th;
	struct migrate_handler *h;

	for (th = target_handlers; th->v != m->v && th->v; th++);

	for (h = th->pre; h->fn; h++) {
		int rc;

		if ((rc = h->fn(c, m)))
			return rc;
	}

	return 0;
}

/**
 * migrate_target() - Perform migration as target: receive state from hypervisor
 * @fd:		Descriptor for state transfer
 * @m:		Migration metadata
 *
 * Return: 0 on success, error code on failure
 *
 * #syscalls:vu readv
 */
int migrate_target(int fd, const struct migrate_meta *m)
{
	static struct migrate_data *d;
	unsigned cnt;
	int rc;

	for (d = data_versions; d->v != m->v && d->v; d++);

	for (cnt = 0; d->sections[cnt + 1 /* skip header */].iov_len; cnt++);

	debug("Reading %u migration sections", cnt);
	rc = read_remainder(fd, d->sections + 1, cnt, 0);
	if (rc < 0)
		return errno;

	return 0;
}

/**
 * migrate_target_post() - Post-migration tasks as target
 * @c:		Execution context
 * @m:		Migration metadata
 */
void migrate_target_post(struct ctx *c, struct migrate_meta *m)
{
	struct migrate_target_handlers *th;
	struct migrate_handler *h;

	for (th = target_handlers; th->v != m->v && th->v; th++);

	for (h = th->post; h->fn; h++)
		h->fn(c, m);
}

debug log:

solving 6707c02 ...
found 6707c02 in https://archives.passt.top/passt-dev/20250128233940.1235855-9-sbrivio@redhat.com/
found b8b79e0 in https://archives.passt.top/passt-dev/20250128233940.1235855-6-sbrivio@redhat.com/

applying [1/2] https://archives.passt.top/passt-dev/20250128233940.1235855-6-sbrivio@redhat.com/
diff --git a/migrate.c b/migrate.c
new file mode 100644
index 0000000..b8b79e0


applying [2/2] https://archives.passt.top/passt-dev/20250128233940.1235855-9-sbrivio@redhat.com/
diff --git a/migrate.c b/migrate.c
index b8b79e0..6707c02 100644

Checking patch migrate.c...
Applied patch migrate.c cleanly.
Checking patch migrate.c...
Applied patch migrate.c cleanly.

index at:
100644 6707c029a88e58d166a9a359f5dfd4e64aa2e6ed	migrate.c

Code repositories for project(s) associated with this public inbox

	https://passt.top/passt

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for IMAP folder(s).