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