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