Commit 0b025033 authored by Stefan Hajnoczi's avatar Stefan Hajnoczi Committed by David S. Miller

VSOCK: add tools/testing/vsock/vsock_diag_test

This patch adds tests for the vsock_diag.ko module.

These tests are not self-tests because they require manual set up of a
KVM or VMware guest.  Please see tools/testing/vsock/README for
instructions.

The control.h and timeout.h infrastructure can be used for additional
AF_VSOCK tests in the future.
Signed-off-by: default avatarStefan Hajnoczi <stefanha@redhat.com>
Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
parent 413a4317
......@@ -14294,6 +14294,7 @@ F: net/vmw_vsock/virtio_transport.c
F: drivers/net/vsockmon.c
F: drivers/vhost/vsock.c
F: drivers/vhost/vsock.h
F: tools/testing/vsock/
VIRTIO CONSOLE DRIVER
M: Amit Shah <amit@kernel.org>
......
all: test
test: vsock_diag_test
vsock_diag_test: vsock_diag_test.o timeout.o control.o
CFLAGS += -g -O2 -Werror -Wall -I. -I../../include/uapi -I../../include -Wno-pointer-sign -fno-strict-overflow -fno-strict-aliasing -fno-common -MMD -U_FORTIFY_SOURCE -D_GNU_SOURCE
.PHONY: all test clean
clean:
${RM} *.o *.d vsock_diag_test
-include *.d
AF_VSOCK test suite
-------------------
These tests exercise net/vmw_vsock/ host<->guest sockets for VMware, KVM, and
Hyper-V.
The following tests are available:
* vsock_diag_test - vsock_diag.ko module for listing open sockets
The following prerequisite steps are not automated and must be performed prior
to running tests:
1. Build the kernel and these tests.
2. Install the kernel and tests on the host.
3. Install the kernel and tests inside the guest.
4. Boot the guest and ensure that the AF_VSOCK transport is enabled.
Invoke test binaries in both directions as follows:
# host=server, guest=client
(host)# $TEST_BINARY --mode=server \
--control-port=1234 \
--peer-cid=3
(guest)# $TEST_BINARY --mode=client \
--control-host=$HOST_IP \
--control-port=1234 \
--peer-cid=2
# host=client, guest=server
(guest)# $TEST_BINARY --mode=server \
--control-port=1234 \
--peer-cid=2
(host)# $TEST_BINARY --mode=client \
--control-port=$GUEST_IP \
--control-port=1234 \
--peer-cid=3
/* Control socket for client/server test execution
*
* Copyright (C) 2017 Red Hat, Inc.
*
* Author: Stefan Hajnoczi <stefanha@redhat.com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; version 2
* of the License.
*/
/* The client and server may need to coordinate to avoid race conditions like
* the client attempting to connect to a socket that the server is not
* listening on yet. The control socket offers a communications channel for
* such coordination tasks.
*
* If the client calls control_expectln("LISTENING"), then it will block until
* the server calls control_writeln("LISTENING"). This provides a simple
* mechanism for coordinating between the client and the server.
*/
#include <errno.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include "timeout.h"
#include "control.h"
static int control_fd = -1;
/* Open the control socket, either in server or client mode */
void control_init(const char *control_host,
const char *control_port,
bool server)
{
struct addrinfo hints = {
.ai_socktype = SOCK_STREAM,
};
struct addrinfo *result = NULL;
struct addrinfo *ai;
int ret;
ret = getaddrinfo(control_host, control_port, &hints, &result);
if (ret != 0) {
fprintf(stderr, "%s\n", gai_strerror(ret));
exit(EXIT_FAILURE);
}
for (ai = result; ai; ai = ai->ai_next) {
int fd;
int val = 1;
fd = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
if (fd < 0)
continue;
if (!server) {
if (connect(fd, ai->ai_addr, ai->ai_addrlen) < 0)
goto next;
control_fd = fd;
printf("Control socket connected to %s:%s.\n",
control_host, control_port);
break;
}
if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR,
&val, sizeof(val)) < 0) {
perror("setsockopt");
exit(EXIT_FAILURE);
}
if (bind(fd, ai->ai_addr, ai->ai_addrlen) < 0)
goto next;
if (listen(fd, 1) < 0)
goto next;
printf("Control socket listening on %s:%s\n",
control_host, control_port);
fflush(stdout);
control_fd = accept(fd, NULL, 0);
close(fd);
if (control_fd < 0) {
perror("accept");
exit(EXIT_FAILURE);
}
printf("Control socket connection accepted...\n");
break;
next:
close(fd);
}
if (control_fd < 0) {
fprintf(stderr, "Control socket initialization failed. Invalid address %s:%s?\n",
control_host, control_port);
exit(EXIT_FAILURE);
}
freeaddrinfo(result);
}
/* Free resources */
void control_cleanup(void)
{
close(control_fd);
control_fd = -1;
}
/* Write a line to the control socket */
void control_writeln(const char *str)
{
ssize_t len = strlen(str);
ssize_t ret;
timeout_begin(TIMEOUT);
do {
ret = send(control_fd, str, len, MSG_MORE);
timeout_check("send");
} while (ret < 0 && errno == EINTR);
if (ret != len) {
perror("send");
exit(EXIT_FAILURE);
}
do {
ret = send(control_fd, "\n", 1, 0);
timeout_check("send");
} while (ret < 0 && errno == EINTR);
if (ret != 1) {
perror("send");
exit(EXIT_FAILURE);
}
timeout_end();
}
/* Return the next line from the control socket (without the trailing newline).
*
* The program terminates if a timeout occurs.
*
* The caller must free() the returned string.
*/
char *control_readln(void)
{
char *buf = NULL;
size_t idx = 0;
size_t buflen = 0;
timeout_begin(TIMEOUT);
for (;;) {
ssize_t ret;
if (idx >= buflen) {
char *new_buf;
new_buf = realloc(buf, buflen + 80);
if (!new_buf) {
perror("realloc");
exit(EXIT_FAILURE);
}
buf = new_buf;
buflen += 80;
}
do {
ret = recv(control_fd, &buf[idx], 1, 0);
timeout_check("recv");
} while (ret < 0 && errno == EINTR);
if (ret == 0) {
fprintf(stderr, "unexpected EOF on control socket\n");
exit(EXIT_FAILURE);
}
if (ret != 1) {
perror("recv");
exit(EXIT_FAILURE);
}
if (buf[idx] == '\n') {
buf[idx] = '\0';
break;
}
idx++;
}
timeout_end();
return buf;
}
/* Wait until a given line is received or a timeout occurs */
void control_expectln(const char *str)
{
char *line;
line = control_readln();
if (strcmp(str, line) != 0) {
fprintf(stderr, "expected \"%s\" on control socket, got \"%s\"\n",
str, line);
exit(EXIT_FAILURE);
}
free(line);
}
#ifndef CONTROL_H
#define CONTROL_H
#include <stdbool.h>
void control_init(const char *control_host, const char *control_port,
bool server);
void control_cleanup(void);
void control_writeln(const char *str);
char *control_readln(void);
void control_expectln(const char *str);
#endif /* CONTROL_H */
/* Timeout API for single-threaded programs that use blocking
* syscalls (read/write/send/recv/connect/accept).
*
* Copyright (C) 2017 Red Hat, Inc.
*
* Author: Stefan Hajnoczi <stefanha@redhat.com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; version 2
* of the License.
*/
/* Use the following pattern:
*
* timeout_begin(TIMEOUT);
* do {
* ret = accept(...);
* timeout_check("accept");
* } while (ret < 0 && ret == EINTR);
* timeout_end();
*/
#include <stdlib.h>
#include <stdbool.h>
#include <unistd.h>
#include <stdio.h>
#include "timeout.h"
static volatile bool timeout;
/* SIGALRM handler function. Do not use sleep(2), alarm(2), or
* setitimer(2) while using this API - they may interfere with each
* other.
*/
void sigalrm(int signo)
{
timeout = true;
}
/* Start a timeout. Call timeout_check() to verify that the timeout hasn't
* expired. timeout_end() must be called to stop the timeout. Timeouts cannot
* be nested.
*/
void timeout_begin(unsigned int seconds)
{
alarm(seconds);
}
/* Exit with an error message if the timeout has expired */
void timeout_check(const char *operation)
{
if (timeout) {
fprintf(stderr, "%s timed out\n", operation);
exit(EXIT_FAILURE);
}
}
/* Stop a timeout */
void timeout_end(void)
{
alarm(0);
timeout = false;
}
#ifndef TIMEOUT_H
#define TIMEOUT_H
enum {
/* Default timeout */
TIMEOUT = 10 /* seconds */
};
void sigalrm(int signo);
void timeout_begin(unsigned int seconds);
void timeout_check(const char *operation);
void timeout_end(void);
#endif /* TIMEOUT_H */
This diff is collapsed.
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment