passt: Relicense to GPL 2.0, or any later version
In practical terms, passt doesn't benefit from the additional
protection offered by the AGPL over the GPL, because it's not
suitable to be executed over a computer network.
Further, restricting the distribution under the version 3 of the GPL
wouldn't provide any practical advantage either, as long as the passt
codebase is concerned, and might cause unnecessary compatibility
dilemmas.
Change licensing terms to the GNU General Public License Version 2,
or any later version, with written permission from all current and
past contributors, namely: myself, David Gibson, Laine Stump, Andrea
Bolognani, Paul Holzinger, Richard W.M. Jones, Chris Kuhn, Florian
Weimer, Giuseppe Scrivano, Stefan Hajnoczi, and Vasiliy Ulyanov.
Signed-off-by: Stefano Brivio <sbrivio@redhat.com>
2023-04-05 20:11:44 +02:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
passt: New design and implementation with native Layer 4 sockets
This is a reimplementation, partially building on the earlier draft,
that uses L4 sockets (SOCK_DGRAM, SOCK_STREAM) instead of SOCK_RAW,
providing L4-L2 translation functionality without requiring any
security capability.
Conceptually, this follows the design presented at:
https://gitlab.com/abologna/kubevirt-and-kvm/-/blob/master/Networking.md
The most significant novelty here comes from TCP and UDP translation
layers. In particular, the TCP state and translation logic follows
the intent of being minimalistic, without reimplementing a full TCP
stack in either direction, and synchronising as much as possible the
TCP dynamic and flows between guest and host kernel.
Another important introduction concerns addressing, port translation
and forwarding. The Layer 4 implementations now attempt to bind on
all unbound ports, in order to forward connections in a transparent
way.
While at it:
- the qemu 'tap' back-end can't be used as-is by qrap anymore,
because of explicit checks now introduced in qemu to ensure that
the corresponding file descriptor is actually a tap device. For
this reason, qrap now operates on a 'socket' back-end type,
accounting for and building the additional header reporting
frame length
- provide a demo script that sets up namespaces, addresses and
routes, and starts the daemon. A virtual machine started in the
network namespace, wrapped by qrap, will now directly interface
with passt and communicate using Layer 4 sockets provided by the
host kernel.
Signed-off-by: Stefano Brivio <sbrivio@redhat.com>
2021-02-16 07:25:09 +01:00
|
|
|
|
2020-07-20 16:41:49 +02:00
|
|
|
/* PASST - Plug A Simple Socket Transport
|
2020-07-18 01:02:39 +02:00
|
|
|
*
|
2021-08-19 19:48:59 +02:00
|
|
|
* qrap.c - qemu wrapper connecting UNIX domain socket to file descriptor
|
2020-07-18 01:02:39 +02:00
|
|
|
*
|
passt: New design and implementation with native Layer 4 sockets
This is a reimplementation, partially building on the earlier draft,
that uses L4 sockets (SOCK_DGRAM, SOCK_STREAM) instead of SOCK_RAW,
providing L4-L2 translation functionality without requiring any
security capability.
Conceptually, this follows the design presented at:
https://gitlab.com/abologna/kubevirt-and-kvm/-/blob/master/Networking.md
The most significant novelty here comes from TCP and UDP translation
layers. In particular, the TCP state and translation logic follows
the intent of being minimalistic, without reimplementing a full TCP
stack in either direction, and synchronising as much as possible the
TCP dynamic and flows between guest and host kernel.
Another important introduction concerns addressing, port translation
and forwarding. The Layer 4 implementations now attempt to bind on
all unbound ports, in order to forward connections in a transparent
way.
While at it:
- the qemu 'tap' back-end can't be used as-is by qrap anymore,
because of explicit checks now introduced in qemu to ensure that
the corresponding file descriptor is actually a tap device. For
this reason, qrap now operates on a 'socket' back-end type,
accounting for and building the additional header reporting
frame length
- provide a demo script that sets up namespaces, addresses and
routes, and starts the daemon. A virtual machine started in the
network namespace, wrapped by qrap, will now directly interface
with passt and communicate using Layer 4 sockets provided by the
host kernel.
Signed-off-by: Stefano Brivio <sbrivio@redhat.com>
2021-02-16 07:25:09 +01:00
|
|
|
* Copyright (c) 2020-2021 Red Hat GmbH
|
2020-07-18 01:02:39 +02:00
|
|
|
* Author: Stefano Brivio <sbrivio@redhat.com>
|
|
|
|
*
|
2022-11-04 02:38:31 +01:00
|
|
|
* 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.
|
2020-07-18 01:02:39 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
2020-07-20 16:41:49 +02:00
|
|
|
#include <stdint.h>
|
2020-07-18 01:02:39 +02:00
|
|
|
#include <stdlib.h>
|
2021-05-21 11:14:52 +02:00
|
|
|
#include <string.h>
|
2020-07-18 01:02:39 +02:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <errno.h>
|
2021-05-10 12:38:50 +02:00
|
|
|
#include <linux/limits.h>
|
|
|
|
#include <limits.h>
|
2021-05-21 11:14:52 +02:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <net/if_arp.h>
|
2021-08-12 15:42:43 +02:00
|
|
|
#include <netinet/in.h>
|
2022-07-20 18:19:57 +02:00
|
|
|
#include <netinet/ip6.h>
|
2021-10-21 04:26:08 +02:00
|
|
|
#include <netinet/if_ether.h>
|
2022-06-16 15:00:06 +02:00
|
|
|
#include <time.h>
|
2021-10-21 04:26:08 +02:00
|
|
|
|
2022-07-20 18:19:57 +02:00
|
|
|
#include <linux/icmpv6.h>
|
|
|
|
|
2021-05-21 11:14:52 +02:00
|
|
|
#include "util.h"
|
2024-03-06 06:58:33 +01:00
|
|
|
#include "ip.h"
|
2021-07-17 08:28:38 +02:00
|
|
|
#include "passt.h"
|
2021-05-21 11:14:52 +02:00
|
|
|
#include "arp.h"
|
2021-05-12 08:35:36 +02:00
|
|
|
|
2021-05-10 12:38:50 +02:00
|
|
|
static char *qemu_names[] = {
|
|
|
|
"kvm",
|
|
|
|
"qemu-kvm",
|
|
|
|
#ifdef ARCH
|
2021-10-21 12:09:25 +02:00
|
|
|
( "qemu-system-" ARCH ),
|
2021-05-10 12:38:50 +02:00
|
|
|
#endif
|
2021-05-10 14:00:30 +02:00
|
|
|
"/usr/libexec/qemu-kvm",
|
2021-05-10 12:38:50 +02:00
|
|
|
NULL,
|
|
|
|
};
|
|
|
|
|
2021-07-17 08:28:38 +02:00
|
|
|
/**
|
|
|
|
* struct drop_arg - Drop matching arguments on command line
|
|
|
|
* @name: Option name
|
|
|
|
* @val: Substring in option value, NULL matches any value
|
|
|
|
*/
|
2021-05-12 08:35:36 +02:00
|
|
|
static const struct drop_arg {
|
|
|
|
char *name;
|
|
|
|
char *val;
|
|
|
|
} drop_args[] = {
|
|
|
|
{ "-netdev", NULL },
|
|
|
|
{ "-net", NULL },
|
|
|
|
{ "-device", "virtio-net-pci," },
|
2023-02-24 19:49:47 +01:00
|
|
|
{ "-device", "{\"driver\":\"virtio-net-pci\"," },
|
2021-05-12 08:35:36 +02:00
|
|
|
{ "-device", "virtio-net-ccw," },
|
2023-02-24 19:49:47 +01:00
|
|
|
{ "-device", "{\"driver\":\"virtio-net-ccw\"," },
|
2021-05-12 08:35:36 +02:00
|
|
|
{ "-device", "e1000," },
|
2023-02-24 19:49:47 +01:00
|
|
|
{ "-device", "{\"driver\":\"e1000\"," },
|
2021-05-21 11:14:48 +02:00
|
|
|
{ "-device", "e1000e," },
|
2023-02-24 19:49:47 +01:00
|
|
|
{ "-device", "{\"driver\":\"e1000e\"," },
|
2021-05-12 08:35:36 +02:00
|
|
|
{ "-device", "rtl8139," },
|
2023-02-24 19:49:47 +01:00
|
|
|
{ "-device", "{\"driver\":\"rtl8139\"," },
|
2021-05-12 08:35:36 +02:00
|
|
|
{ 0 },
|
|
|
|
};
|
|
|
|
|
2021-07-17 08:28:38 +02:00
|
|
|
/**
|
|
|
|
* 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)
|
2022-10-20 11:04:19 +02:00
|
|
|
* @template_json: Device prefix for when JSON is used
|
|
|
|
* @template_json_post: Device suffix for when JSON is used
|
qrap: Introduce machine-specific PCI address base
For pc machines, devices are placed directly on pci.0 with
addresses like
bus=pci.0,addr=0xa
and in this case the existing code works correctly.
For q35 machines, however, a separate PCI bus is created for
each devices using a pcie-root-port, and the resulting
addresses look like
bus=pci.9,addr=0x0
In this case, we need to treat PCI addresses as decimal, not
hexadecimal, both when parsing and generating them.
This issue has gone unnoticed for a long time because it only
shows up when enough PCI devices are present: for small
numbers, decimal and hexadecimal overlap, masking the issue.
Reported-by: Alona Paz <alkaplan@redhat.com>
Fixes: 5307faa05997 ("qrap: Strip network devices from command line, set them up according to machine")
Signed-off-by: Andrea Bolognani <abologna@redhat.com>
Reviewed-by: David Gibson <david@gibson.dropbear.id.au>
2023-02-24 19:49:48 +01:00
|
|
|
* @base: Base used for PCI addresses
|
2021-07-17 08:28:38 +02:00
|
|
|
* @first: First usable PCI address
|
|
|
|
* @last: Last usable PCI address
|
|
|
|
*/
|
2021-05-12 08:35:36 +02:00
|
|
|
static const struct pci_dev {
|
|
|
|
char *mach;
|
|
|
|
char *name;
|
|
|
|
char *template;
|
|
|
|
char *template_post;
|
2022-10-20 11:04:19 +02:00
|
|
|
char *template_json;
|
|
|
|
char *template_json_post;
|
qrap: Introduce machine-specific PCI address base
For pc machines, devices are placed directly on pci.0 with
addresses like
bus=pci.0,addr=0xa
and in this case the existing code works correctly.
For q35 machines, however, a separate PCI bus is created for
each devices using a pcie-root-port, and the resulting
addresses look like
bus=pci.9,addr=0x0
In this case, we need to treat PCI addresses as decimal, not
hexadecimal, both when parsing and generating them.
This issue has gone unnoticed for a long time because it only
shows up when enough PCI devices are present: for small
numbers, decimal and hexadecimal overlap, masking the issue.
Reported-by: Alona Paz <alkaplan@redhat.com>
Fixes: 5307faa05997 ("qrap: Strip network devices from command line, set them up according to machine")
Signed-off-by: Andrea Bolognani <abologna@redhat.com>
Reviewed-by: David Gibson <david@gibson.dropbear.id.au>
2023-02-24 19:49:48 +01:00
|
|
|
int base;
|
2021-05-12 08:35:36 +02:00
|
|
|
int first;
|
|
|
|
int last;
|
|
|
|
} pci_devs[] = {
|
2022-10-20 11:04:19 +02:00
|
|
|
{
|
|
|
|
"pc-q35", "virtio-net-pci",
|
|
|
|
"bus=pci.", ",addr=0x0",
|
|
|
|
"\"bus\":\"pci.", ",\"addr\":\"0x0\"",
|
qrap: Introduce machine-specific PCI address base
For pc machines, devices are placed directly on pci.0 with
addresses like
bus=pci.0,addr=0xa
and in this case the existing code works correctly.
For q35 machines, however, a separate PCI bus is created for
each devices using a pcie-root-port, and the resulting
addresses look like
bus=pci.9,addr=0x0
In this case, we need to treat PCI addresses as decimal, not
hexadecimal, both when parsing and generating them.
This issue has gone unnoticed for a long time because it only
shows up when enough PCI devices are present: for small
numbers, decimal and hexadecimal overlap, masking the issue.
Reported-by: Alona Paz <alkaplan@redhat.com>
Fixes: 5307faa05997 ("qrap: Strip network devices from command line, set them up according to machine")
Signed-off-by: Andrea Bolognani <abologna@redhat.com>
Reviewed-by: David Gibson <david@gibson.dropbear.id.au>
2023-02-24 19:49:48 +01:00
|
|
|
10, 3, /* 2: hotplug bus */ 31
|
2022-10-20 11:04:19 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"pc-", "virtio-net-pci",
|
|
|
|
"bus=pci.0,addr=0x", "",
|
2023-02-24 19:49:46 +01:00
|
|
|
"\"bus\":\"pci.0\",\"addr\":\"0x", "",
|
qrap: Introduce machine-specific PCI address base
For pc machines, devices are placed directly on pci.0 with
addresses like
bus=pci.0,addr=0xa
and in this case the existing code works correctly.
For q35 machines, however, a separate PCI bus is created for
each devices using a pcie-root-port, and the resulting
addresses look like
bus=pci.9,addr=0x0
In this case, we need to treat PCI addresses as decimal, not
hexadecimal, both when parsing and generating them.
This issue has gone unnoticed for a long time because it only
shows up when enough PCI devices are present: for small
numbers, decimal and hexadecimal overlap, masking the issue.
Reported-by: Alona Paz <alkaplan@redhat.com>
Fixes: 5307faa05997 ("qrap: Strip network devices from command line, set them up according to machine")
Signed-off-by: Andrea Bolognani <abologna@redhat.com>
Reviewed-by: David Gibson <david@gibson.dropbear.id.au>
2023-02-24 19:49:48 +01:00
|
|
|
16, 2, /* 1: ISA bridge */ 31
|
2022-10-20 11:04:19 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"s390-ccw", "virtio-net-ccw",
|
|
|
|
"devno=fe.0.", "",
|
|
|
|
"\"devno\":\"fe.0.", "",
|
qrap: Introduce machine-specific PCI address base
For pc machines, devices are placed directly on pci.0 with
addresses like
bus=pci.0,addr=0xa
and in this case the existing code works correctly.
For q35 machines, however, a separate PCI bus is created for
each devices using a pcie-root-port, and the resulting
addresses look like
bus=pci.9,addr=0x0
In this case, we need to treat PCI addresses as decimal, not
hexadecimal, both when parsing and generating them.
This issue has gone unnoticed for a long time because it only
shows up when enough PCI devices are present: for small
numbers, decimal and hexadecimal overlap, masking the issue.
Reported-by: Alona Paz <alkaplan@redhat.com>
Fixes: 5307faa05997 ("qrap: Strip network devices from command line, set them up according to machine")
Signed-off-by: Andrea Bolognani <abologna@redhat.com>
Reviewed-by: David Gibson <david@gibson.dropbear.id.au>
2023-02-24 19:49:48 +01:00
|
|
|
16, 1, 16
|
2022-10-20 11:04:19 +02:00
|
|
|
},
|
2021-05-12 08:35:36 +02:00
|
|
|
{ 0 },
|
|
|
|
};
|
|
|
|
|
2021-05-10 12:38:50 +02:00
|
|
|
#define DEFAULT_FD 5
|
|
|
|
|
2020-07-18 01:02:39 +02:00
|
|
|
/**
|
|
|
|
* usage() - Print usage and exit
|
|
|
|
* @name: Executable name
|
|
|
|
*/
|
|
|
|
void usage(const char *name)
|
|
|
|
{
|
2021-08-19 19:48:59 +02:00
|
|
|
fprintf(stderr, "Usage: %s [FDNUM QEMU_CMD] [QEMU_ARG]...\n", name);
|
2021-05-10 12:38:50 +02:00
|
|
|
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);
|
2020-07-18 01:02:39 +02:00
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
2022-10-20 11:04:19 +02:00
|
|
|
int i, s, qemu_argc = 0, addr_map = 0, has_dev = 0, has_json = 0, retry_on_reset, rc;
|
2022-01-30 02:59:12 +01:00
|
|
|
struct timeval tv = { .tv_sec = 0, .tv_usec = (long)(500 * 1000) };
|
2021-07-17 08:28:38 +02:00
|
|
|
char *qemu_argv[ARG_MAX], dev_str[ARG_MAX];
|
2020-07-18 01:02:39 +02:00
|
|
|
struct sockaddr_un addr = {
|
|
|
|
.sun_family = AF_UNIX,
|
|
|
|
};
|
2021-05-12 08:35:36 +02:00
|
|
|
const struct pci_dev *dev = NULL;
|
2020-07-18 01:02:39 +02:00
|
|
|
long fd;
|
2021-05-21 11:14:52 +02:00
|
|
|
struct {
|
2022-07-20 18:19:57 +02:00
|
|
|
uint32_t vnet_len4;
|
|
|
|
struct ethhdr eh4;
|
2021-05-21 11:14:52 +02:00
|
|
|
struct arphdr ah;
|
|
|
|
struct arpmsg am;
|
2022-07-20 18:19:57 +02:00
|
|
|
|
|
|
|
uint32_t vnet_len6;
|
|
|
|
struct ethhdr eh6;
|
|
|
|
struct ipv6hdr ip6hr;
|
|
|
|
struct icmp6hdr ihr;
|
|
|
|
struct in6_addr target;
|
|
|
|
} __attribute__((__packed__)) probe = {
|
|
|
|
.vnet_len4 = htonl(42),
|
2021-05-21 11:14:52 +02:00
|
|
|
{
|
|
|
|
.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 },
|
|
|
|
},
|
2022-07-20 18:19:57 +02:00
|
|
|
.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,
|
2021-05-21 11:14:52 +02:00
|
|
|
};
|
|
|
|
char probe_r;
|
2020-07-18 01:02:39 +02:00
|
|
|
|
2021-05-10 12:38:50 +02:00
|
|
|
if (argc >= 3) {
|
2022-09-28 06:33:30 +02:00
|
|
|
const char *path = getenv("PATH");
|
2021-07-17 08:28:38 +02:00
|
|
|
errno = 0;
|
2021-05-10 12:38:50 +02:00
|
|
|
fd = strtol(argv[1], NULL, 0);
|
2022-09-28 06:33:30 +02:00
|
|
|
if (fd >= 3 && fd < INT_MAX && !errno && path) {
|
2021-05-21 11:14:49 +02:00
|
|
|
char env_path[ARG_MAX + 1], *p, command[ARG_MAX];
|
2021-05-10 12:38:50 +02:00
|
|
|
|
2022-09-28 06:33:30 +02:00
|
|
|
strncpy(env_path, path, ARG_MAX);
|
2022-09-28 06:33:25 +02:00
|
|
|
/* cppcheck-suppress strtokCalled */
|
2021-05-10 12:38:50 +02:00
|
|
|
p = strtok(env_path, ":");
|
|
|
|
while (p) {
|
|
|
|
snprintf(command, ARG_MAX, "%s/%s", p, argv[2]);
|
|
|
|
if (!access(command, X_OK))
|
|
|
|
goto valid_args;
|
|
|
|
|
2022-09-28 06:33:25 +02:00
|
|
|
/* cppcheck-suppress strtokCalled */
|
2021-05-10 12:38:50 +02:00
|
|
|
p = strtok(NULL, ":");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fd = DEFAULT_FD;
|
2021-05-12 08:35:36 +02:00
|
|
|
|
|
|
|
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;
|
|
|
|
|
2021-05-10 12:38:50 +02:00
|
|
|
for (qemu_argc = 1, i = 1; i < argc; i++) {
|
2021-05-12 08:35:36 +02:00
|
|
|
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;
|
2021-05-10 12:38:50 +02:00
|
|
|
}
|
2020-07-18 01:02:39 +02:00
|
|
|
|
2021-05-12 08:35:36 +02:00
|
|
|
if (!strcmp(argv[i], "-device") && i + 1 < argc) {
|
2024-01-15 07:39:43 +01:00
|
|
|
const char *template = NULL;
|
|
|
|
const char *p;
|
2021-05-12 08:35:36 +02:00
|
|
|
|
|
|
|
has_dev = 1;
|
|
|
|
|
|
|
|
if ((p = strstr(argv[i + 1], dev->template))) {
|
2022-10-20 11:04:19 +02:00
|
|
|
template = dev->template;
|
|
|
|
} else if ((p = strstr(argv[i + 1], dev->template_json))) {
|
|
|
|
template = dev->template_json;
|
|
|
|
has_json = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (template) {
|
2021-10-21 09:41:13 +02:00
|
|
|
long n;
|
|
|
|
|
qrap: Introduce machine-specific PCI address base
For pc machines, devices are placed directly on pci.0 with
addresses like
bus=pci.0,addr=0xa
and in this case the existing code works correctly.
For q35 machines, however, a separate PCI bus is created for
each devices using a pcie-root-port, and the resulting
addresses look like
bus=pci.9,addr=0x0
In this case, we need to treat PCI addresses as decimal, not
hexadecimal, both when parsing and generating them.
This issue has gone unnoticed for a long time because it only
shows up when enough PCI devices are present: for small
numbers, decimal and hexadecimal overlap, masking the issue.
Reported-by: Alona Paz <alkaplan@redhat.com>
Fixes: 5307faa05997 ("qrap: Strip network devices from command line, set them up according to machine")
Signed-off-by: Andrea Bolognani <abologna@redhat.com>
Reviewed-by: David Gibson <david@gibson.dropbear.id.au>
2023-02-24 19:49:48 +01:00
|
|
|
n = strtol(p + strlen(template), NULL, dev->base);
|
2021-05-12 08:35:36 +02:00
|
|
|
if (!errno)
|
|
|
|
addr_map |= (1 << n);
|
|
|
|
}
|
|
|
|
}
|
2020-07-18 01:02:39 +02:00
|
|
|
|
2021-05-10 12:38:50 +02:00
|
|
|
qemu_argv[qemu_argc++] = argv[i];
|
|
|
|
}
|
|
|
|
|
2021-05-12 08:35:36 +02:00
|
|
|
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]);
|
|
|
|
}
|
2021-05-10 12:38:50 +02:00
|
|
|
|
2021-05-12 08:35:36 +02:00
|
|
|
if (has_dev) {
|
|
|
|
qemu_argv[qemu_argc++] = "-device";
|
2022-10-20 11:04:19 +02:00
|
|
|
if (!has_json) {
|
qrap: Introduce machine-specific PCI address base
For pc machines, devices are placed directly on pci.0 with
addresses like
bus=pci.0,addr=0xa
and in this case the existing code works correctly.
For q35 machines, however, a separate PCI bus is created for
each devices using a pcie-root-port, and the resulting
addresses look like
bus=pci.9,addr=0x0
In this case, we need to treat PCI addresses as decimal, not
hexadecimal, both when parsing and generating them.
This issue has gone unnoticed for a long time because it only
shows up when enough PCI devices are present: for small
numbers, decimal and hexadecimal overlap, masking the issue.
Reported-by: Alona Paz <alkaplan@redhat.com>
Fixes: 5307faa05997 ("qrap: Strip network devices from command line, set them up according to machine")
Signed-off-by: Andrea Bolognani <abologna@redhat.com>
Reviewed-by: David Gibson <david@gibson.dropbear.id.au>
2023-02-24 19:49:48 +01:00
|
|
|
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);
|
|
|
|
}
|
2022-10-20 11:04:19 +02:00
|
|
|
} else {
|
qrap: Introduce machine-specific PCI address base
For pc machines, devices are placed directly on pci.0 with
addresses like
bus=pci.0,addr=0xa
and in this case the existing code works correctly.
For q35 machines, however, a separate PCI bus is created for
each devices using a pcie-root-port, and the resulting
addresses look like
bus=pci.9,addr=0x0
In this case, we need to treat PCI addresses as decimal, not
hexadecimal, both when parsing and generating them.
This issue has gone unnoticed for a long time because it only
shows up when enough PCI devices are present: for small
numbers, decimal and hexadecimal overlap, masking the issue.
Reported-by: Alona Paz <alkaplan@redhat.com>
Fixes: 5307faa05997 ("qrap: Strip network devices from command line, set them up according to machine")
Signed-off-by: Andrea Bolognani <abologna@redhat.com>
Reviewed-by: David Gibson <david@gibson.dropbear.id.au>
2023-02-24 19:49:48 +01:00
|
|
|
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);
|
|
|
|
}
|
2022-10-20 11:04:19 +02:00
|
|
|
}
|
2021-05-12 08:35:36 +02:00
|
|
|
qemu_argv[qemu_argc++] = dev_str;
|
2021-05-11 17:08:15 +02:00
|
|
|
}
|
2021-05-10 12:38:50 +02:00
|
|
|
|
2021-05-12 08:35:36 +02:00
|
|
|
qemu_argv[qemu_argc++] = "-netdev";
|
2023-02-24 19:49:49 +01:00
|
|
|
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\"}";
|
|
|
|
}
|
2021-05-12 08:35:36 +02:00
|
|
|
qemu_argv[qemu_argc] = NULL;
|
|
|
|
|
2021-05-10 12:38:50 +02:00
|
|
|
valid_args:
|
2021-05-21 11:14:52 +02:00
|
|
|
for (i = 1; i < UNIX_SOCK_MAX; i++) {
|
2022-06-27 15:05:45 +02:00
|
|
|
retry_on_reset = 50;
|
2022-06-16 15:00:06 +02:00
|
|
|
|
|
|
|
retry:
|
2021-05-21 11:14:52 +02:00
|
|
|
s = socket(AF_UNIX, SOCK_STREAM, 0);
|
|
|
|
if (s < 0) {
|
|
|
|
perror("socket");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
2022-04-05 11:51:47 +02:00
|
|
|
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");
|
|
|
|
|
2021-05-21 11:14:52 +02:00
|
|
|
snprintf(addr.sun_path, UNIX_PATH_MAX, UNIX_SOCK_PATH, i);
|
2022-07-06 08:02:24 +02:00
|
|
|
|
|
|
|
errno = 0;
|
|
|
|
|
|
|
|
if (connect(s, (const struct sockaddr *)&addr, sizeof(addr))) {
|
2022-09-28 06:33:21 +02:00
|
|
|
rc = errno;
|
2021-07-17 08:28:38 +02:00
|
|
|
perror("connect");
|
2022-07-06 08:02:24 +02:00
|
|
|
} else if (send(s, &probe, sizeof(probe), 0) != sizeof(probe)) {
|
2022-09-28 06:33:21 +02:00
|
|
|
rc = errno;
|
2021-07-17 08:28:38 +02:00
|
|
|
perror("send");
|
2022-07-06 08:02:24 +02:00
|
|
|
} else if (recv(s, &probe_r, 1, MSG_PEEK) <= 0) {
|
2022-09-28 06:33:21 +02:00
|
|
|
rc = errno;
|
2021-07-17 08:28:38 +02:00
|
|
|
perror("recv");
|
2022-07-06 08:02:24 +02:00
|
|
|
} else {
|
2021-05-21 11:14:52 +02:00
|
|
|
break;
|
2022-07-06 08:02:24 +02:00
|
|
|
}
|
2021-05-21 11:14:52 +02:00
|
|
|
|
2022-06-16 15:00:06 +02:00
|
|
|
/* 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.
|
|
|
|
*
|
2022-06-27 12:11:07 +02:00
|
|
|
* 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.
|
2022-06-16 15:00:06 +02:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
2022-09-28 06:33:21 +02:00
|
|
|
if (retry_on_reset && rc == ECONNRESET) {
|
2022-06-27 12:11:07 +02:00
|
|
|
retry_on_reset--;
|
2022-09-28 06:33:25 +02:00
|
|
|
/* cppcheck-suppress usleepCalled */
|
2022-06-27 12:11:07 +02:00
|
|
|
usleep(50 * 1000);
|
2022-06-16 15:00:06 +02:00
|
|
|
goto retry;
|
|
|
|
}
|
|
|
|
|
2021-07-17 08:28:38 +02:00
|
|
|
fprintf(stderr, "Probe of %s failed\n", addr.sun_path);
|
2021-05-21 11:14:52 +02:00
|
|
|
|
|
|
|
close(s);
|
2020-07-18 01:02:39 +02:00
|
|
|
}
|
|
|
|
|
2021-05-21 11:14:52 +02:00
|
|
|
if (i == UNIX_SOCK_MAX) {
|
2020-07-18 01:02:39 +02:00
|
|
|
perror("connect");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
2021-07-17 08:28:38 +02:00
|
|
|
tv.tv_usec = 0;
|
2022-04-05 07:10:30 +02:00
|
|
|
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");
|
|
|
|
|
2021-07-17 08:28:38 +02:00
|
|
|
fprintf(stderr, "Connected to %s\n", addr.sun_path);
|
|
|
|
|
2020-07-18 01:02:39 +02:00
|
|
|
if (dup2(s, (int)fd) < 0) {
|
|
|
|
perror("dup");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
close(s);
|
|
|
|
|
2021-05-10 12:38:50 +02:00
|
|
|
if (qemu_argc) {
|
2021-05-10 14:00:30 +02:00
|
|
|
char **name;
|
2021-05-10 12:38:50 +02:00
|
|
|
|
2021-05-10 14:00:30 +02:00
|
|
|
for (name = qemu_names; *name; name++) {
|
|
|
|
qemu_argv[0] = *name;
|
|
|
|
execvp(*name, qemu_argv);
|
2021-05-10 12:38:50 +02:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2020-07-18 01:02:39 +02:00
|
|
|
perror("execvp");
|
|
|
|
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|