public inbox for passt-dev@passt.top
 help / color / mirror / code / Atom feed
blob 0a90fba3b23f72ddbb3602d28af326a6b18fe5f5 7708 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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
 
// 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"
#include "repair.h"

/* Magic identifier for migration data */
#define MIGRATE_MAGIC		0xB1BB1D1B0BB1D1B0

/**
 * struct migrate_seen_addrs_v1 - Migratable guest addresses for v1 state stream
 * @addr6:	Observed guest IPv6 address
 * @addr6_ll:	Observed guest IPv6 link-local address
 * @addr4:	Observed guest IPv4 address
 * @mac:	Observed guest MAC address
 */
struct migrate_seen_addrs_v1 {
	struct in6_addr addr6;
	struct in6_addr addr6_ll;
	struct in_addr addr4;
	unsigned char mac[ETH_ALEN];
} __attribute__((packed));

/**
 * seen_addrs_source_v1() - Copy and send guest observed addresses from source
 * @c:		Execution context
 * @stage:	Migration stage, unused
 * @fd:		File descriptor for state transfer
 *
 * Return: 0 on success, positive error code on failure
 */
/* cppcheck-suppress [constParameterCallback, unmatchedSuppression] */
static int seen_addrs_source_v1(struct ctx *c,
				const struct migrate_stage *stage, int fd)
{
	struct migrate_seen_addrs_v1 addrs = {
		.addr6 = c->ip6.addr_seen,
		.addr6_ll = c->ip6.addr_ll_seen,
		.addr4 = c->ip4.addr_seen,
	};

	(void)stage;

	memcpy(addrs.mac, c->guest_mac, sizeof(addrs.mac));

	if (write_all_buf(fd, &addrs, sizeof(addrs)))
		return errno;

	return 0;
}

/**
 * seen_addrs_target_v1() - Receive and use guest observed addresses on target
 * @c:		Execution context
 * @stage:	Migration stage, unused
 * @fd:		File descriptor for state transfer
 *
 * Return: 0 on success, positive error code on failure
 */
static int seen_addrs_target_v1(struct ctx *c,
				const struct migrate_stage *stage, int fd)
{
	struct migrate_seen_addrs_v1 addrs;

	(void)stage;

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

	c->ip6.addr_seen = addrs.addr6;
	c->ip6.addr_ll_seen = addrs.addr6_ll;
	c->ip4.addr_seen = addrs.addr4;
	memcpy(c->guest_mac, addrs.mac, sizeof(c->guest_mac));

	return 0;
}

/** no_rollback() - dummy callback indicating a stage can't be rolled back
 * TODO
 */
static int no_rollback(struct ctx *c, const struct migrate_stage *stage, int fd)
{
	(void)c;
	(void)stage;
	(void)fd;
	return ENXIO;
}

/* Stages for version 1 */
static const struct migrate_stage stages_v1[] = {
	{
		.name = "freeze flows",
		.source = flow_freeze,
		.rollback = flow_thaw,
		.target = NULL,
	},
	/* FIXME: With this step, close() in tcp_flow_migrate_source_ext()
	 * *sometimes* closes the connection for real.
	 */
/*	{
		.name = "shrink TCP windows",
		.source = flow_migrate_source_early,
		.target = NULL,
	},
*/
	{
		.name = "observed addresses",
		.source = seen_addrs_source_v1,
		.target = seen_addrs_target_v1,
	},
	{
		.name = "transfer flows",
		.source = flow_migrate_source,
		/* This closes sockets, so can't be rolled back */
		.rollback = no_rollback,
		.target = flow_migrate_target,
	},
	{
		.name = "thaw flows",
		.source = NULL,
		.target = flow_thaw,
	},
	{ 0 },
};

/* Supported encoding versions, from latest (most preferred) to oldest */
static const struct migrate_version versions[] = {
	{ 1,	stages_v1, },
	{ 0 },
};

/* Current encoding version */
#define CURRENT_VERSION		(&versions[0])

/**
 * migrate_source() - Migration as source, send state to hypervisor
 * @c:		Execution context
 * @fd:		File descriptor for state transfer
 *
 * Return: 0 on success, positive error code on failure
 */
static int migrate_source(struct ctx *c, int fd)
{
	const struct migrate_version *v = CURRENT_VERSION;
	const struct migrate_header header = {
		.magic		= htonll_constant(MIGRATE_MAGIC),
		.version	= htonl(v->id),
		.compat_version	= htonl(v->id),
	};
	const struct migrate_stage *s;
	int ret;

	if (write_all_buf(fd, &header, sizeof(header))) {
		ret = errno;
		err("Can't send migration header: %s, abort", strerror_(ret));
		return ret;
	}

	for (s = v->s; s->name; s++) {
		if (!s->source)
			continue;

		debug("Source side migration stage: %s", s->name);

		if ((ret = s->source(c, s, fd)))
			goto rollback;
	}

	return 0;

rollback:
	err("Source migration stage: %s: %s, aborting", s->name,
	    strerror_(ret));

	while (s-- > v->s) {
		debug("Rolling back migration: %s", s->name);
		if (s->rollback && s->rollback(c, s, fd))
			die("Unable to roll back migration");
	}

	return ret;
}

/**
 * migrate_target_read_header() - Read header in target
 * @fd:		Descriptor for state transfer
 *
 * Return: version structure on success, NULL on failure with errno set
 */
static const struct migrate_version *migrate_target_read_header(int fd)
{
	const struct migrate_version *v;
	struct migrate_header h;
	uint32_t id, compat_id;

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

	id = ntohl(h.version);
	compat_id = ntohl(h.compat_version);

	debug("Source magic: 0x%016" PRIx64 ", version: %u, compat: %u",
	      ntohll(h.magic), id, compat_id);

	if (ntohll(h.magic) != MIGRATE_MAGIC || !id || !compat_id) {
		err("Invalid incoming device state");
		errno = EINVAL;
		return NULL;
	}

	for (v = versions; v->id; v++)
		if (v->id <= id && v->id >= compat_id)
			return v;

	errno = ENOTSUP;
	err("Unsupported device state version: %u", id);
	return NULL;
}

/**
 * migrate_target() - Migration as target, receive state from hypervisor
 * @c:		Execution context
 * @fd:		File descriptor for state transfer
 *
 * Return: 0 on success, positive error code on failure
 */
static int migrate_target(struct ctx *c, int fd)
{
	const struct migrate_version *v;
	const struct migrate_stage *s;
	int ret;

	if (!(v = migrate_target_read_header(fd)))
		return errno;

	for (s = v->s; s->name; s++) {
		if (!s->target)
			continue;

		debug("Target side migration stage: %s", s->name);

		if ((ret = s->target(c, s, fd))) {
			/* FIXME: Less brutal failure */
			die("Target migration stage: %s: %s, abort", s->name,
			    strerror_(ret));
		}
	}

	return 0;
}

/**
 * migrate_init() - Set up things necessary for migration
 * @c:		Execution context
 */
void migrate_init(struct ctx *c)
{
	c->device_state_result = -1;
}

/**
 * migrate_close() - Close migration channel and connection to passt-repair
 * @c:		Execution context
 */
void migrate_close(struct ctx *c)
{
	if (c->device_state_fd != -1) {
		debug("Closing migration channel, fd: %d", c->device_state_fd);
		close(c->device_state_fd);
		c->device_state_fd = -1;
		c->device_state_result = -1;
	}

	repair_close(c);
}

/**
 * migrate_request() - Request a migration of device state
 * @c:		Execution context
 * @fd:		fd to transfer state
 * @target:	Are we the target of the migration?
 */
void migrate_request(struct ctx *c, int fd, bool target)
{
	debug("Migration requested, fd: %d (was %d)", fd, c->device_state_fd);

	if (c->device_state_fd != -1)
		migrate_close(c);

	c->device_state_fd = fd;
	c->migrate_target = target;
}

/**
 * migrate_handler() - Send/receive passt internal state to/from hypervisor
 * @c:		Execution context
 */
void migrate_handler(struct ctx *c)
{
	int rc;

	if (c->device_state_fd < 0)
		return;

	debug("Handling migration request from fd: %d, target: %d",
	      c->device_state_fd, c->migrate_target);

	if (c->migrate_target)
		rc = migrate_target(c, c->device_state_fd);
	else
		rc = migrate_source(c, c->device_state_fd);

	migrate_close(c);

	c->device_state_result = rc;
}

debug log:

solving 0a90fba3 ...
found 0a90fba3 in https://archives.passt.top/passt-dev/20250214130845.3475757-5-david@gibson.dropbear.id.au/
found d802e2f9 in https://archives.passt.top/passt-dev/20250214130845.3475757-4-david@gibson.dropbear.id.au/
found 1c590160 in https://passt.top/passt
preparing index
index prepared:
100644 1c590160cc62935367e660d39576ca53fa04d5ff	migrate.c

applying [1/2] https://archives.passt.top/passt-dev/20250214130845.3475757-4-david@gibson.dropbear.id.au/
diff --git a/migrate.c b/migrate.c
index 1c590160..d802e2f9 100644


applying [2/2] https://archives.passt.top/passt-dev/20250214130845.3475757-5-david@gibson.dropbear.id.au/
diff --git a/migrate.c b/migrate.c
index d802e2f9..0a90fba3 100644

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

index at:
100644 0a90fba3b23f72ddbb3602d28af326a6b18fe5f5	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).