1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * kvm_binary_stats_test
4  *
5  * Copyright (C) 2021, Google LLC.
6  *
7  * Test the fd-based interface for KVM statistics.
8  */
9 
10 #define _GNU_SOURCE /* for program_invocation_short_name */
11 #include <fcntl.h>
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <errno.h>
16 
17 #include "test_util.h"
18 
19 #include "kvm_util.h"
20 #include "asm/kvm.h"
21 #include "linux/kvm.h"
22 
23 static void stats_test(int stats_fd)
24 {
25 	ssize_t ret;
26 	int i;
27 	size_t size_desc;
28 	size_t size_data = 0;
29 	struct kvm_stats_header header;
30 	char *id;
31 	struct kvm_stats_desc *stats_desc;
32 	u64 *stats_data;
33 	struct kvm_stats_desc *pdesc;
34 
35 	/* Read kvm stats header */
36 	ret = read(stats_fd, &header, sizeof(header));
37 	TEST_ASSERT(ret == sizeof(header), "Read stats header");
38 	size_desc = sizeof(*stats_desc) + header.name_size;
39 
40 	/* Read kvm stats id string */
41 	id = malloc(header.name_size);
42 	TEST_ASSERT(id, "Allocate memory for id string");
43 	ret = read(stats_fd, id, header.name_size);
44 	TEST_ASSERT(ret == header.name_size, "Read id string");
45 
46 	/* Check id string, that should start with "kvm" */
47 	TEST_ASSERT(!strncmp(id, "kvm", 3) && strlen(id) < header.name_size,
48 				"Invalid KVM stats type, id: %s", id);
49 
50 	/* Sanity check for other fields in header */
51 	if (header.num_desc == 0) {
52 		printf("No KVM stats defined!");
53 		return;
54 	}
55 	/* Check overlap */
56 	TEST_ASSERT(header.desc_offset > 0 && header.data_offset > 0
57 			&& header.desc_offset >= sizeof(header)
58 			&& header.data_offset >= sizeof(header),
59 			"Invalid offset fields in header");
60 	TEST_ASSERT(header.desc_offset > header.data_offset ||
61 			(header.desc_offset + size_desc * header.num_desc <=
62 							header.data_offset),
63 			"Descriptor block is overlapped with data block");
64 
65 	/* Allocate memory for stats descriptors */
66 	stats_desc = calloc(header.num_desc, size_desc);
67 	TEST_ASSERT(stats_desc, "Allocate memory for stats descriptors");
68 	/* Read kvm stats descriptors */
69 	ret = pread(stats_fd, stats_desc,
70 			size_desc * header.num_desc, header.desc_offset);
71 	TEST_ASSERT(ret == size_desc * header.num_desc,
72 			"Read KVM stats descriptors");
73 
74 	/* Sanity check for fields in descriptors */
75 	for (i = 0; i < header.num_desc; ++i) {
76 		pdesc = (void *)stats_desc + i * size_desc;
77 		/* Check type,unit,base boundaries */
78 		TEST_ASSERT((pdesc->flags & KVM_STATS_TYPE_MASK)
79 				<= KVM_STATS_TYPE_MAX, "Unknown KVM stats type");
80 		TEST_ASSERT((pdesc->flags & KVM_STATS_UNIT_MASK)
81 				<= KVM_STATS_UNIT_MAX, "Unknown KVM stats unit");
82 		TEST_ASSERT((pdesc->flags & KVM_STATS_BASE_MASK)
83 				<= KVM_STATS_BASE_MAX, "Unknown KVM stats base");
84 		/* Check exponent for stats unit
85 		 * Exponent for counter should be greater than or equal to 0
86 		 * Exponent for unit bytes should be greater than or equal to 0
87 		 * Exponent for unit seconds should be less than or equal to 0
88 		 * Exponent for unit clock cycles should be greater than or
89 		 * equal to 0
90 		 */
91 		switch (pdesc->flags & KVM_STATS_UNIT_MASK) {
92 		case KVM_STATS_UNIT_NONE:
93 		case KVM_STATS_UNIT_BYTES:
94 		case KVM_STATS_UNIT_CYCLES:
95 			TEST_ASSERT(pdesc->exponent >= 0,
96 					"Unsupported KVM stats unit");
97 			break;
98 		case KVM_STATS_UNIT_SECONDS:
99 			TEST_ASSERT(pdesc->exponent <= 0,
100 					"Unsupported KVM stats unit");
101 			break;
102 		}
103 		/* Check name string */
104 		TEST_ASSERT(strlen(pdesc->name) < header.name_size,
105 				"KVM stats name(%s) too long", pdesc->name);
106 		/* Check size field, which should not be zero */
107 		TEST_ASSERT(pdesc->size, "KVM descriptor(%s) with size of 0",
108 				pdesc->name);
109 		/* Check bucket_size field */
110 		switch (pdesc->flags & KVM_STATS_TYPE_MASK) {
111 		case KVM_STATS_TYPE_LINEAR_HIST:
112 			TEST_ASSERT(pdesc->bucket_size,
113 			    "Bucket size of Linear Histogram stats (%s) is zero",
114 			    pdesc->name);
115 			break;
116 		default:
117 			TEST_ASSERT(!pdesc->bucket_size,
118 			    "Bucket size of stats (%s) is not zero",
119 			    pdesc->name);
120 		}
121 		size_data += pdesc->size * sizeof(*stats_data);
122 	}
123 	/* Check overlap */
124 	TEST_ASSERT(header.data_offset >= header.desc_offset
125 		|| header.data_offset + size_data <= header.desc_offset,
126 		"Data block is overlapped with Descriptor block");
127 	/* Check validity of all stats data size */
128 	TEST_ASSERT(size_data >= header.num_desc * sizeof(*stats_data),
129 			"Data size is not correct");
130 	/* Check stats offset */
131 	for (i = 0; i < header.num_desc; ++i) {
132 		pdesc = (void *)stats_desc + i * size_desc;
133 		TEST_ASSERT(pdesc->offset < size_data,
134 			"Invalid offset (%u) for stats: %s",
135 			pdesc->offset, pdesc->name);
136 	}
137 
138 	/* Allocate memory for stats data */
139 	stats_data = malloc(size_data);
140 	TEST_ASSERT(stats_data, "Allocate memory for stats data");
141 	/* Read kvm stats data as a bulk */
142 	ret = pread(stats_fd, stats_data, size_data, header.data_offset);
143 	TEST_ASSERT(ret == size_data, "Read KVM stats data");
144 	/* Read kvm stats data one by one */
145 	size_data = 0;
146 	for (i = 0; i < header.num_desc; ++i) {
147 		pdesc = (void *)stats_desc + i * size_desc;
148 		ret = pread(stats_fd, stats_data,
149 				pdesc->size * sizeof(*stats_data),
150 				header.data_offset + size_data);
151 		TEST_ASSERT(ret == pdesc->size * sizeof(*stats_data),
152 				"Read data of KVM stats: %s", pdesc->name);
153 		size_data += pdesc->size * sizeof(*stats_data);
154 	}
155 
156 	free(stats_data);
157 	free(stats_desc);
158 	free(id);
159 }
160 
161 
162 static void vm_stats_test(struct kvm_vm *vm)
163 {
164 	int stats_fd = vm_get_stats_fd(vm);
165 
166 	stats_test(stats_fd);
167 	close(stats_fd);
168 	TEST_ASSERT(fcntl(stats_fd, F_GETFD) == -1, "Stats fd not freed");
169 }
170 
171 static void vcpu_stats_test(struct kvm_vcpu *vcpu)
172 {
173 	int stats_fd = vcpu_get_stats_fd(vcpu);
174 
175 	stats_test(stats_fd);
176 	close(stats_fd);
177 	TEST_ASSERT(fcntl(stats_fd, F_GETFD) == -1, "Stats fd not freed");
178 }
179 
180 #define DEFAULT_NUM_VM		4
181 #define DEFAULT_NUM_VCPU	4
182 
183 /*
184  * Usage: kvm_bin_form_stats [#vm] [#vcpu]
185  * The first parameter #vm set the number of VMs being created.
186  * The second parameter #vcpu set the number of VCPUs being created.
187  * By default, DEFAULT_NUM_VM VM and DEFAULT_NUM_VCPU VCPU for the VM would be
188  * created for testing.
189  */
190 
191 int main(int argc, char *argv[])
192 {
193 	int i, j;
194 	struct kvm_vcpu **vcpus;
195 	struct kvm_vm **vms;
196 	int max_vm = DEFAULT_NUM_VM;
197 	int max_vcpu = DEFAULT_NUM_VCPU;
198 
199 	/* Get the number of VMs and VCPUs that would be created for testing. */
200 	if (argc > 1) {
201 		max_vm = strtol(argv[1], NULL, 0);
202 		if (max_vm <= 0)
203 			max_vm = DEFAULT_NUM_VM;
204 	}
205 	if (argc > 2) {
206 		max_vcpu = strtol(argv[2], NULL, 0);
207 		if (max_vcpu <= 0)
208 			max_vcpu = DEFAULT_NUM_VCPU;
209 	}
210 
211 	/* Check the extension for binary stats */
212 	TEST_REQUIRE(kvm_has_cap(KVM_CAP_BINARY_STATS_FD));
213 
214 	/* Create VMs and VCPUs */
215 	vms = malloc(sizeof(vms[0]) * max_vm);
216 	TEST_ASSERT(vms, "Allocate memory for storing VM pointers");
217 
218 	vcpus = malloc(sizeof(struct kvm_vcpu *) * max_vm * max_vcpu);
219 	TEST_ASSERT(vcpus, "Allocate memory for storing vCPU pointers");
220 
221 	for (i = 0; i < max_vm; ++i) {
222 		vms[i] = vm_create_barebones();
223 		for (j = 0; j < max_vcpu; ++j)
224 			vcpus[i * max_vcpu + j] = __vm_vcpu_add(vms[i], j);
225 	}
226 
227 	/* Check stats read for every VM and VCPU */
228 	for (i = 0; i < max_vm; ++i) {
229 		vm_stats_test(vms[i]);
230 		for (j = 0; j < max_vcpu; ++j)
231 			vcpu_stats_test(vcpus[i * max_vcpu + j]);
232 	}
233 
234 	for (i = 0; i < max_vm; ++i)
235 		kvm_vm_free(vms[i]);
236 	free(vms);
237 	return 0;
238 }
239