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