public inbox for passt-dev@passt.top
 help / color / mirror / code / Atom feed
blob d59670621731ad66f2c8e08240b710c694c03784 10714 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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
 
// SPDX-License-Identifier: GPL-2.0-or-later

/* PASST - Plug A Simple Socket Transport
 *
 * qrap.c - qemu wrapper connecting UNIX domain socket to file descriptor
 *
 * Copyright (c) 2020-2021 Red Hat GmbH
 * Author: Stefano Brivio <sbrivio@redhat.com>
 *
 * TODO: Drop this implementation once qemu commit 13c6be96618c ("net: stream:
 * add unix socket") is included in a release (7.2), and once we can reasonably
 * assume existing users switched to it.
 */

#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <errno.h>
#include <linux/limits.h>
#include <limits.h>
#include <fcntl.h>
#include <net/if_arp.h>
#include <netinet/in.h>
#include <netinet/ip6.h>
#include <netinet/if_ether.h>
#include <time.h>

#include <linux/icmpv6.h>

#include "util.h"
#include "ip.h"
#include "passt.h"
#include "arp.h"

static char *qemu_names[] = {
	"kvm",
	"qemu-kvm",
#ifdef ARCH
	( "qemu-system-" ARCH ),
#endif
	"/usr/libexec/qemu-kvm",
	NULL,
};

/**
 * struct drop_arg - Drop matching arguments on command line
 * @name:	Option name
 * @val:	Substring in option value, NULL matches any value
 */
static const struct drop_arg {
	char *name;
	char *val;
} drop_args[] = {
	{ "-netdev",	NULL },
	{ "-net",	NULL },
	{ "-device",	"virtio-net-pci," },
	{ "-device",	"{\"driver\":\"virtio-net-pci\"," },
	{ "-device",	"virtio-net-ccw," },
	{ "-device",	"{\"driver\":\"virtio-net-ccw\"," },
	{ "-device",	"e1000," },
	{ "-device",	"{\"driver\":\"e1000\"," },
	{ "-device",	"e1000e," },
	{ "-device",	"{\"driver\":\"e1000e\"," },
	{ "-device",	"rtl8139," },
	{ "-device",	"{\"driver\":\"rtl8139\"," },
	{ 0 },
};

/**
 * struct pci_dev - PCI devices to add on command line depending on machine name
 * @mach:		Machine name
 * @name:		Device ("-device") name to insert
 * @template:		Prefix for device specification (first part of address)
 * @template_post:	Suffix for device specification (last part of address)
 * @template_json:		Device prefix for when JSON is used
 * @template_json_post:	Device suffix for when JSON is used
 * @base:		Base used for PCI addresses
 * @first:		First usable PCI address
 * @last:		Last usable PCI address
 */
static const struct pci_dev {
	char *mach;
	char *name;
	char *template;
	char *template_post;
	char *template_json;
	char *template_json_post;
	int base;
	int first;
	int last;
} pci_devs[] = {
	{
		"pc-q35", "virtio-net-pci",
		"bus=pci.", ",addr=0x0",
		"\"bus\":\"pci.", ",\"addr\":\"0x0\"",
		10, 3, /* 2: hotplug bus */ 31
	},
	{
		"pc-", "virtio-net-pci",
		"bus=pci.0,addr=0x", "",
		"\"bus\":\"pci.0\",\"addr\":\"0x", "",
		16, 2, /* 1: ISA bridge */ 31
	},
	{
		"s390-ccw", "virtio-net-ccw",
		"devno=fe.0.", "",
		"\"devno\":\"fe.0.", "",
		16, 1, 16
	},
	{ 0 },
};

#define DEFAULT_FD	5

/**
 * usage() - Print usage and exit
 * @name:	Executable name
 */
void usage(const char *name)
{
	fprintf(stderr, "Usage: %s [FDNUM QEMU_CMD] [QEMU_ARG]...\n", name);
	fprintf(stderr, "\n");
	fprintf(stderr, "If first and second arguments aren't a socket number\n"
			"and a path, %s will try to locate a qemu binary\n"
			"and directly patch the command line\n", name);

	exit(EXIT_FAILURE);
}

/**
 * main() - Entry point and main loop
 * @argc:	Argument count
 * @argv:	File descriptor number, then qemu with arguments
 *
 * Return: 0 once interrupted, non-zero on failure
 */
int main(int argc, char **argv)
{
	int i, s, qemu_argc = 0, addr_map = 0, has_dev = 0, has_json = 0, retry_on_reset, rc;
	struct timeval tv = { .tv_sec = 0, .tv_usec = (long)(500 * 1000) };
	char *qemu_argv[ARG_MAX], dev_str[ARG_MAX];
	struct sockaddr_un addr = {
		.sun_family = AF_UNIX,
	};
	const struct pci_dev *dev = NULL;
	long fd;
	struct {
		uint32_t vnet_len4;
		struct ethhdr eh4;
		struct arphdr ah;
		struct arpmsg am;

		uint32_t vnet_len6;
		struct ethhdr eh6;
		struct ipv6hdr ip6hr;
		struct icmp6hdr ihr;
		struct in6_addr target;
	} __attribute__((__packed__)) probe = {
		.vnet_len4 = htonl(42),
		{
			.h_dest   = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff },
			.h_source = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff },
			.h_proto  = htons(ETH_P_ARP),
		},
		{	.ar_hrd = htons(ARPHRD_ETHER),
			.ar_pro = htons(ETH_P_IP),
			.ar_hln = ETH_ALEN,
			.ar_pln = 4,
			.ar_op  = htons(ARPOP_REQUEST),
		},
		{
			.sha = { 0 }, .sip = { 0 }, .tha = { 0 }, .tip = { 0 },
		},
		.vnet_len6 = htonl(78),
		{
			.h_dest   = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff },
			.h_source = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff },
			.h_proto  = htons(ETH_P_IPV6),
		},
		{
			.version = 6,
			.payload_len = htons(24),
			.nexthdr = IPPROTO_ICMPV6,
			.hop_limit = 255,
			.saddr = IN6ADDR_LOOPBACK_INIT,
			.daddr = IN6ADDR_ANY_INIT,
		},
		{
			.icmp6_type = 135,
			.icmp6_code = 0,
		},
		IN6ADDR_ANY_INIT,
	};
	char probe_r;

	if (argc >= 3) {
		const char *path = getenv("PATH");
		errno = 0;
		fd = strtol(argv[1], NULL, 0);
		if (fd >= 3 && fd < INT_MAX && !errno && path) {
			char env_path[ARG_MAX + 1], *p, command[ARG_MAX];

			strncpy(env_path, path, ARG_MAX);
			/* cppcheck-suppress strtokCalled */
			p = strtok(env_path, ":");
			while (p) {
				snprintf(command, ARG_MAX, "%s/%s", p, argv[2]);
				if (!access(command, X_OK))
					goto valid_args;

				/* cppcheck-suppress strtokCalled */
				p = strtok(NULL, ":");
			}
		}
	}

	fd = DEFAULT_FD;

	for (i = 1; i < argc - 1; i++) {
		if (strcmp(argv[i], "-machine"))
			continue;

		for (dev = pci_devs; dev->mach; dev++) {
			if (strstr(argv[i + 1], dev->mach) == argv[i + 1])
				break;
		}
	}

	if (!dev || !dev->mach)
		dev = pci_devs;

	for (qemu_argc = 1, i = 1; i < argc; i++) {
		const struct drop_arg *a;

		for (a = drop_args; a->name; a++) {
			if (!strcmp(argv[i], a->name)) {
				if (!a->val)
					break;

				if (i + 1 < argc &&
				    strstr(argv[i + 1], a->val) == argv[i + 1])
					break;
			}
		}
		if (a->name) {
			i++;
			continue;
		}

		if (!strcmp(argv[i], "-device") && i + 1 < argc) {
			const char *template = NULL;
			const char *p;

			has_dev = 1;

			if ((p = strstr(argv[i + 1], dev->template))) {
				template = dev->template;
			} else if ((p = strstr(argv[i + 1], dev->template_json))) {
				template = dev->template_json;
				has_json = 1;
			}

			if (template) {
				long n;

				n = strtol(p + strlen(template), NULL, dev->base);
				if (!errno)
					addr_map |= (1 << n);
			}
		}

		qemu_argv[qemu_argc++] = argv[i];
	}

	for (i = dev->first; i < dev->last; i++) {
		if (!(addr_map & (1 << i)))
			break;
	}
	if (i == dev->last) {
		fprintf(stderr, "Couldn't find free address for device\n");
		usage(argv[0]);
	}

	if (has_dev) {
		qemu_argv[qemu_argc++] = "-device";
		if (!has_json) {
			if (dev->base == 16) {
				snprintf(dev_str, ARG_MAX,
				         "%s,%s%x%s,netdev=hostnet0,x-txburst=4096",
				         dev->name, dev->template, i, dev->template_post);
			} else if (dev->base == 10) {
				snprintf(dev_str, ARG_MAX,
				         "%s,%s%d%s,netdev=hostnet0,x-txburst=4096",
				         dev->name, dev->template, i, dev->template_post);
			}
		} else {
			if (dev->base == 16) {
				snprintf(dev_str, ARG_MAX,
				         "{\"driver\":\"%s\",%s%x\"%s,\"netdev\":\"hostnet0\",\"x-txburst\":4096}",
				         dev->name, dev->template_json, i, dev->template_json_post);
			} else if (dev->base == 10) {
				snprintf(dev_str, ARG_MAX,
				         "{\"driver\":\"%s\",%s%d\"%s,\"netdev\":\"hostnet0\",\"x-txburst\":4096}",
				         dev->name, dev->template_json, i, dev->template_json_post);
			}
		}
		qemu_argv[qemu_argc++] = dev_str;
	}

	qemu_argv[qemu_argc++] = "-netdev";
	if (!has_json) {
		qemu_argv[qemu_argc++] = "socket,fd=" STR(DEFAULT_FD) ",id=hostnet0";
	} else {
		qemu_argv[qemu_argc++] = "{\"type\":\"socket\",\"fd\":\"" STR(DEFAULT_FD) "\",\"id\":\"hostnet0\"}";
	}
	qemu_argv[qemu_argc] = NULL;

valid_args:
	for (i = 1; i < UNIX_SOCK_MAX; i++) {
		retry_on_reset = 50;

retry:
		s = socket(AF_UNIX, SOCK_STREAM, 0);
		if (s < 0) {
			perror("socket");
			exit(EXIT_FAILURE);
		}

		if (setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)))
			perror("setsockopt SO_RCVTIMEO");
		if (setsockopt(s, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv)))
			perror("setsockopt SO_SNDTIMEO");

		snprintf(addr.sun_path, UNIX_PATH_MAX, UNIX_SOCK_PATH, i);

		errno = 0;

		if (connect(s, (const struct sockaddr *)&addr, sizeof(addr))) {
			rc = errno;
			perror("connect");
		} else if (send(s, &probe, sizeof(probe), 0) != sizeof(probe)) {
			rc = errno;
			perror("send");
		} else if (recv(s, &probe_r, 1, MSG_PEEK) <= 0) {
			rc = errno;
			perror("recv");
		} else {
			break;
		}

		/* FIXME: in a KubeVirt environment, libvirtd invokes qrap three
		 * times in a strict sequence when a virtual machine needs to
		 * be started, namely, when:
		 * - the domain XML is saved
		 * - the domain is started (for "probing")
		 * - the virtual machine is started for real
		 * and it often happens that the qemu process is still running
		 * when qrap is invoked again, so passt will refuse the new
		 * connection because the previous one is still active. This
		 * overlap seems to be anywhere between 0 and 3ms.
		 *
		 * If we get a connection reset, retry a few times, to allow for
		 * the previous qemu instance to terminate and, in turn, for the
		 * connection to passt to be closed.
		 *
		 * This should be fixed in libvirt instead. It probably makes
		 * sense to check this behaviour once native libvirt support is
		 * there, and this implies native qemu support too, so at that
		 * point qrap will have no reason to exist anymore -- that is,
		 * this FIXME will probably remain until the tool itself is
		 * obsoleted.
		 */
		if (retry_on_reset && rc == ECONNRESET) {
			retry_on_reset--;
			/* cppcheck-suppress usleepCalled */
			usleep(50 * 1000);
			goto retry;
		}

		fprintf(stderr, "Probe of %s failed\n", addr.sun_path);

		close(s);
	}

	if (i == UNIX_SOCK_MAX) {
		perror("connect");
		exit(EXIT_FAILURE);
	}

	tv.tv_usec = 0;
	if (setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)))
		perror("setsockopt, SO_RCVTIMEO reset");
	if (setsockopt(s, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv)))
		perror("setsockopt, SO_SNDTIMEO reset");

	fprintf(stderr, "Connected to %s\n", addr.sun_path);

	if (dup2(s, (int)fd) < 0) {
		perror("dup");
		exit(EXIT_FAILURE);
	}

	close(s);

	if (qemu_argc) {
		char **name;

		for (name = qemu_names; *name; name++) {
			qemu_argv[0] = *name;
			execvp(*name, qemu_argv);
			if (errno != ENOENT) {
				perror("execvp");
				usage(argv[0]);
			}
		}
		if (errno == ENOENT)
			fprintf(stderr, "Couldn't find qemu command\n");
	} else {
		execvp(argv[2], argv + 2);
	}

	perror("execvp");

	return EXIT_FAILURE;
}

debug log:

solving d59670621731 ...
found d59670621731 in https://passt.top/passt

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).