patches and low-level development discussion
 help / color / mirror / code / Atom feed
blob 6600385648989e243184eedd2a695306570760ce 3759 bytes (raw)
name: release/checks/integration/networking.c 	 # note: path name is non-authoritative(*)

  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
 
// SPDX-License-Identifier: EUPL-1.2+
// SPDX-FileCopyrightText: 2025 Alyssa Ross <hi@alyssa.is>
// SPDX-FileCopyrightText: 2025 Yureka Lilian <yureka@cyberchaos.dev>

#include "lib.h"

#include <poll.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include <arpa/inet.h>
#include <net/if.h>

#include <sys/ioctl.h>
#include <linux/ipv6.h>

static int setup_server(void)
{
	int fd;
	struct ifreq ifr;
	struct in6_ifreq ifr6;

	struct sockaddr_in6 addr = {
		.sin6_family = AF_INET6,
		.sin6_port = htons(1234),
		.sin6_addr = { .s6_addr = { 0xfd, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02 } },
	};

	sprintf(ifr.ifr_name, "lo");

	if ((fd = socket(AF_INET6, SOCK_STREAM|SOCK_CLOEXEC, 0)) == -1) {
		perror("socket");
		exit(EXIT_FAILURE);
	}

	if (ioctl(fd, SIOCGIFFLAGS, &ifr) == -1) {
		perror("SIOCGIFFLAGS");
		exit(EXIT_FAILURE);
	}

	ifr.ifr_flags |= IFF_UP;
	if (ioctl(fd, SIOCSIFFLAGS, &ifr) == -1) {
		perror("SIOCSIFFLAGS");
		exit(EXIT_FAILURE);
	}

	ifr6.ifr6_ifindex = 1;
	ifr6.ifr6_addr = addr.sin6_addr;
	ifr6.ifr6_prefixlen = 128;
	if (ioctl(fd, SIOCSIFADDR, &ifr6) == -1) {
		perror("SIOCSIFADDR");
		exit(EXIT_FAILURE);
	}

	if ((fd = socket(AF_INET6, SOCK_STREAM|SOCK_CLOEXEC, 0)) == -1) {
		perror("socket");
		exit(EXIT_FAILURE);
	}

	int tries = 0;
	while (bind(fd, &addr, sizeof addr) == -1) {
		perror("bind");
		if (tries++ >= 5)
			exit(EXIT_FAILURE);
	}

	if (listen(fd, 1) == -1) {
		perror("listen");
		exit(EXIT_FAILURE);
	}

	return fd;
}

static void expect_connection(int listener)
{
	int conn_fd;
	FILE *conn;
	char msg[7];
	size_t len;

	fputs("waiting for server connection\n", stderr);
	if ((conn_fd = accept(listener, nullptr, nullptr)) == -1) {
		perror("accept");
		exit(EXIT_FAILURE);
	}
	fputs("accepted connection!\n", stderr);
	if (!(conn = fdopen(conn_fd, "r"))) {
		perror("fdopen(server connection)");
		exit(EXIT_FAILURE);
	}

	len = fread(msg, 1, sizeof msg, conn);
	if (len != 6 || memcmp("hello\n", msg, 6)) {
		if (ferror(conn))
			perror("fread(server connection)");
		else
			fprintf(stderr, "unexpected connection data: %.*s",
			        (int)len, msg);
		exit(EXIT_FAILURE);
	}
	fclose(conn);
}

static void drain_connections(int listener)
{
	int r;
	struct pollfd pollfd = { .fd = listener, .events = POLLIN };

	for (;;) {
		switch (poll(&pollfd, 1, 0)) {
		case -1:
			perror("poll");
			exit(EXIT_FAILURE);
		case 0:
			return;
		}

		if ((r = accept4(listener, nullptr, nullptr, SOCK_CLOEXEC)) == -1) {
			perror("accept");
			exit(EXIT_FAILURE);
		}

		close(r);
	}
}

void test(struct config c)
{
	int server = setup_server();

	struct vm *vm = start_qemu(c);
	start_console_thread(vm, "~ # ");
	wait_for_prompt(vm);

	if (fputs("set -euxo pipefail && "
	          "mkdir /run/mnt && "
	          "mount \"$(findfs UUID=a7834806-2f82-4faf-8ac4-4f8fd8a474ca)\" /run/mnt && "
	          "s6-rc -bu change vmm-env && "
	          "vm-import user /run/mnt/vms && "
	          "vm-start \"$(basename \"$(readlink /run/vm/by-name/user.nc)\")\" && "
	          "tail -Fc +0 /run/log/current /run/*.log &\n",
	          vm_console_writer(vm)) == EOF) {
		fputs("error writing to console\n", stderr);
		exit(EXIT_FAILURE);
	}

	expect_connection(server);

	wait_for_prompt(vm);

	if (fputs("s6-svc -wR -r /run/vm/by-name/sys.netvm/service\n",
	          vm_console_writer(vm)) == EOF) {
		fputs("error writing to console\n", stderr);
		exit(EXIT_FAILURE);
	}

	wait_for_prompt(vm);

	drain_connections(server);

	if (fputs("vm-start \"$(basename \"$(readlink /run/vm/by-name/sys.netvm)\")\"\n",
	          vm_console_writer(vm)) == EOF) {
		fputs("error writing to console\n", stderr);
		exit(EXIT_FAILURE);
	}

	expect_connection(server);
}

debug log:

solving 6600385 ...
found 6600385 in https://inbox.spectrum-os.org/spectrum-devel/20251128223038.97536-8-yureka@cyberchaos.dev/
found 97d7895 in https://spectrum-os.org/git/spectrum
preparing index
index prepared:
100644 97d789502eacff67ee7c231bbd495a2eef0af829	release/checks/integration/networking.c

applying [1/1] https://inbox.spectrum-os.org/spectrum-devel/20251128223038.97536-8-yureka@cyberchaos.dev/
diff --git a/release/checks/integration/networking.c b/release/checks/integration/networking.c
index 97d7895..6600385 100644

Checking patch release/checks/integration/networking.c...
Applied patch release/checks/integration/networking.c cleanly.

index at:
100644 6600385648989e243184eedd2a695306570760ce	release/checks/integration/networking.c

(*) Git path names are given by the tree(s) the blob belongs to.
    Blobs themselves have no identifier aside from the hash of its contents.^

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

	https://spectrum-os.org/git/doc
	https://spectrum-os.org/git/mktuntap
	https://spectrum-os.org/git/spectrum
	https://spectrum-os.org/git/ucspi-vsock

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 NNTP newsgroup(s).