Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * vsock_test - vsock.ko test suite
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Copyright (C) 2017 Red Hat, Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  * Author: Stefan Hajnoczi <stefanha@redhat.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <getopt.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <stdio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <stdlib.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <unistd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include "timeout.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include "control.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #include "util.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) static void test_stream_connection_reset(const struct test_opts *opts)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) 	union {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 		struct sockaddr sa;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 		struct sockaddr_vm svm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 	} addr = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 		.svm = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 			.svm_family = AF_VSOCK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 			.svm_port = 1234,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 			.svm_cid = opts->peer_cid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 		},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 	int fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 	fd = socket(AF_VSOCK, SOCK_STREAM, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 	timeout_begin(TIMEOUT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 		ret = connect(fd, &addr.sa, sizeof(addr.svm));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 		timeout_check("connect");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 	} while (ret < 0 && errno == EINTR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 	timeout_end();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 	if (ret != -1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 		fprintf(stderr, "expected connect(2) failure, got %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 		exit(EXIT_FAILURE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 	if (errno != ECONNRESET) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 		fprintf(stderr, "unexpected connect(2) errno %d\n", errno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 		exit(EXIT_FAILURE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 	close(fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) static void test_stream_bind_only_client(const struct test_opts *opts)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 	union {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 		struct sockaddr sa;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 		struct sockaddr_vm svm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 	} addr = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 		.svm = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 			.svm_family = AF_VSOCK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 			.svm_port = 1234,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 			.svm_cid = opts->peer_cid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 		},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 	int fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 	/* Wait for the server to be ready */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	control_expectln("BIND");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	fd = socket(AF_VSOCK, SOCK_STREAM, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 	timeout_begin(TIMEOUT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 		ret = connect(fd, &addr.sa, sizeof(addr.svm));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 		timeout_check("connect");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	} while (ret < 0 && errno == EINTR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	timeout_end();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	if (ret != -1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 		fprintf(stderr, "expected connect(2) failure, got %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 		exit(EXIT_FAILURE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 	if (errno != ECONNRESET) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 		fprintf(stderr, "unexpected connect(2) errno %d\n", errno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 		exit(EXIT_FAILURE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	/* Notify the server that the client has finished */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	control_writeln("DONE");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 	close(fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) static void test_stream_bind_only_server(const struct test_opts *opts)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	union {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 		struct sockaddr sa;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 		struct sockaddr_vm svm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 	} addr = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 		.svm = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 			.svm_family = AF_VSOCK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 			.svm_port = 1234,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 			.svm_cid = VMADDR_CID_ANY,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 		},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	int fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	fd = socket(AF_VSOCK, SOCK_STREAM, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	if (bind(fd, &addr.sa, sizeof(addr.svm)) < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 		perror("bind");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 		exit(EXIT_FAILURE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	/* Notify the client that the server is ready */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	control_writeln("BIND");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	/* Wait for the client to finish */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 	control_expectln("DONE");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	close(fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) static void test_stream_client_close_client(const struct test_opts *opts)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 	int fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	fd = vsock_stream_connect(opts->peer_cid, 1234);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	if (fd < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 		perror("connect");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 		exit(EXIT_FAILURE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 	send_byte(fd, 1, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 	close(fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) static void test_stream_client_close_server(const struct test_opts *opts)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 	int fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 	fd = vsock_stream_accept(VMADDR_CID_ANY, 1234, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 	if (fd < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 		perror("accept");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 		exit(EXIT_FAILURE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 	/* Wait for the remote to close the connection, before check
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 	 * -EPIPE error on send.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 	vsock_wait_remote_close(fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 	send_byte(fd, -EPIPE, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 	recv_byte(fd, 1, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 	recv_byte(fd, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 	close(fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) static void test_stream_server_close_client(const struct test_opts *opts)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 	int fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 	fd = vsock_stream_connect(opts->peer_cid, 1234);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 	if (fd < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 		perror("connect");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 		exit(EXIT_FAILURE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 	/* Wait for the remote to close the connection, before check
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 	 * -EPIPE error on send.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 	vsock_wait_remote_close(fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 	send_byte(fd, -EPIPE, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 	recv_byte(fd, 1, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 	recv_byte(fd, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 	close(fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) static void test_stream_server_close_server(const struct test_opts *opts)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 	int fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 	fd = vsock_stream_accept(VMADDR_CID_ANY, 1234, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 	if (fd < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 		perror("accept");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 		exit(EXIT_FAILURE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 	send_byte(fd, 1, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 	close(fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) /* With the standard socket sizes, VMCI is able to support about 100
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201)  * concurrent stream connections.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) #define MULTICONN_NFDS 100
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) static void test_stream_multiconn_client(const struct test_opts *opts)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 	int fds[MULTICONN_NFDS];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 	for (i = 0; i < MULTICONN_NFDS; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 		fds[i] = vsock_stream_connect(opts->peer_cid, 1234);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 		if (fds[i] < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 			perror("connect");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 			exit(EXIT_FAILURE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 	for (i = 0; i < MULTICONN_NFDS; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 		if (i % 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 			recv_byte(fds[i], 1, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 			send_byte(fds[i], 1, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 	for (i = 0; i < MULTICONN_NFDS; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 		close(fds[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) static void test_stream_multiconn_server(const struct test_opts *opts)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 	int fds[MULTICONN_NFDS];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 	for (i = 0; i < MULTICONN_NFDS; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 		fds[i] = vsock_stream_accept(VMADDR_CID_ANY, 1234, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 		if (fds[i] < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 			perror("accept");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 			exit(EXIT_FAILURE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 	for (i = 0; i < MULTICONN_NFDS; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 		if (i % 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 			send_byte(fds[i], 1, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 			recv_byte(fds[i], 1, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 	for (i = 0; i < MULTICONN_NFDS; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 		close(fds[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) static void test_stream_msg_peek_client(const struct test_opts *opts)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 	int fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 	fd = vsock_stream_connect(opts->peer_cid, 1234);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 	if (fd < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 		perror("connect");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 		exit(EXIT_FAILURE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 	send_byte(fd, 1, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 	close(fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) static void test_stream_msg_peek_server(const struct test_opts *opts)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 	int fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 	fd = vsock_stream_accept(VMADDR_CID_ANY, 1234, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 	if (fd < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 		perror("accept");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 		exit(EXIT_FAILURE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 	recv_byte(fd, 1, MSG_PEEK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 	recv_byte(fd, 1, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 	close(fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) static struct test_case test_cases[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 	{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 		.name = "SOCK_STREAM connection reset",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 		.run_client = test_stream_connection_reset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 	{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 		.name = "SOCK_STREAM bind only",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 		.run_client = test_stream_bind_only_client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 		.run_server = test_stream_bind_only_server,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 	{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 		.name = "SOCK_STREAM client close",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 		.run_client = test_stream_client_close_client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 		.run_server = test_stream_client_close_server,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 	{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 		.name = "SOCK_STREAM server close",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 		.run_client = test_stream_server_close_client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 		.run_server = test_stream_server_close_server,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 	{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 		.name = "SOCK_STREAM multiple connections",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 		.run_client = test_stream_multiconn_client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 		.run_server = test_stream_multiconn_server,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 	{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 		.name = "SOCK_STREAM MSG_PEEK",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 		.run_client = test_stream_msg_peek_client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 		.run_server = test_stream_msg_peek_server,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 	{},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) static const char optstring[] = "";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) static const struct option longopts[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 	{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 		.name = "control-host",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 		.has_arg = required_argument,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 		.val = 'H',
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 	{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 		.name = "control-port",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 		.has_arg = required_argument,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 		.val = 'P',
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 	{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 		.name = "mode",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 		.has_arg = required_argument,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 		.val = 'm',
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 	{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 		.name = "peer-cid",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 		.has_arg = required_argument,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 		.val = 'p',
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 	{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 		.name = "list",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 		.has_arg = no_argument,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 		.val = 'l',
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 	{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 		.name = "skip",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 		.has_arg = required_argument,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 		.val = 's',
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 	{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 		.name = "help",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 		.has_arg = no_argument,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 		.val = '?',
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 	{},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) static void usage(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 	fprintf(stderr, "Usage: vsock_test [--help] [--control-host=<host>] --control-port=<port> --mode=client|server --peer-cid=<cid> [--list] [--skip=<test_id>]\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 		"\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 		"  Server: vsock_test --control-port=1234 --mode=server --peer-cid=3\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 		"  Client: vsock_test --control-host=192.168.0.1 --control-port=1234 --mode=client --peer-cid=2\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 		"\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 		"Run vsock.ko tests.  Must be launched in both guest\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 		"and host.  One side must use --mode=client and\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 		"the other side must use --mode=server.\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 		"\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 		"A TCP control socket connection is used to coordinate tests\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 		"between the client and the server.  The server requires a\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 		"listen address and the client requires an address to\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 		"connect to.\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 		"\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 		"The CID of the other side must be given with --peer-cid=<cid>.\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 		"\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 		"Options:\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 		"  --help                 This help message\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 		"  --control-host <host>  Server IP address to connect to\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 		"  --control-port <port>  Server port to listen on/connect to\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 		"  --mode client|server   Server or client mode\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 		"  --peer-cid <cid>       CID of the other side\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 		"  --list                 List of tests that will be executed\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 		"  --skip <test_id>       Test ID to skip;\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 		"                         use multiple --skip options to skip more tests\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 		);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 	exit(EXIT_FAILURE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) int main(int argc, char **argv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 	const char *control_host = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 	const char *control_port = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 	struct test_opts opts = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 		.mode = TEST_MODE_UNSET,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 		.peer_cid = VMADDR_CID_ANY,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 	init_signals();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 	for (;;) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 		int opt = getopt_long(argc, argv, optstring, longopts, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 		if (opt == -1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 		switch (opt) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 		case 'H':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) 			control_host = optarg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) 		case 'm':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) 			if (strcmp(optarg, "client") == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) 				opts.mode = TEST_MODE_CLIENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) 			else if (strcmp(optarg, "server") == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) 				opts.mode = TEST_MODE_SERVER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) 			else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) 				fprintf(stderr, "--mode must be \"client\" or \"server\"\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) 				return EXIT_FAILURE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) 		case 'p':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) 			opts.peer_cid = parse_cid(optarg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) 		case 'P':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) 			control_port = optarg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) 		case 'l':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) 			list_tests(test_cases);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) 		case 's':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) 			skip_test(test_cases, ARRAY_SIZE(test_cases) - 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) 				  optarg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) 		case '?':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) 		default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) 			usage();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) 	if (!control_port)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) 		usage();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) 	if (opts.mode == TEST_MODE_UNSET)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) 		usage();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) 	if (opts.peer_cid == VMADDR_CID_ANY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) 		usage();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) 	if (!control_host) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) 		if (opts.mode != TEST_MODE_SERVER)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) 			usage();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) 		control_host = "0.0.0.0";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) 	control_init(control_host, control_port,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) 		     opts.mode == TEST_MODE_SERVER);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) 	run_tests(test_cases, &opts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) 	control_cleanup();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) 	return EXIT_SUCCESS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) }