0001
0002
0003
0004
0005
0006
0007
0008
0009
0010 #define _GNU_SOURCE
0011 #include <fcntl.h>
0012 #include <stdio.h>
0013 #include <stdlib.h>
0014 #include <string.h>
0015 #include <sys/resource.h>
0016
0017 #include "test_util.h"
0018
0019 #include "kvm_util.h"
0020 #include "asm/kvm.h"
0021 #include "linux/kvm.h"
0022
0023 void test_vcpu_creation(int first_vcpu_id, int num_vcpus)
0024 {
0025 struct kvm_vm *vm;
0026 int i;
0027
0028 pr_info("Testing creating %d vCPUs, with IDs %d...%d.\n",
0029 num_vcpus, first_vcpu_id, first_vcpu_id + num_vcpus - 1);
0030
0031 vm = vm_create_barebones();
0032
0033 for (i = first_vcpu_id; i < first_vcpu_id + num_vcpus; i++)
0034
0035 __vm_vcpu_add(vm, i);
0036
0037 kvm_vm_free(vm);
0038 }
0039
0040 int main(int argc, char *argv[])
0041 {
0042 int kvm_max_vcpu_id = kvm_check_cap(KVM_CAP_MAX_VCPU_ID);
0043 int kvm_max_vcpus = kvm_check_cap(KVM_CAP_MAX_VCPUS);
0044
0045
0046
0047
0048 int nr_fds_wanted = kvm_max_vcpus + 100;
0049 struct rlimit rl;
0050
0051 pr_info("KVM_CAP_MAX_VCPU_ID: %d\n", kvm_max_vcpu_id);
0052 pr_info("KVM_CAP_MAX_VCPUS: %d\n", kvm_max_vcpus);
0053
0054
0055
0056
0057
0058 TEST_ASSERT(!getrlimit(RLIMIT_NOFILE, &rl), "getrlimit() failed!");
0059
0060 if (rl.rlim_cur < nr_fds_wanted) {
0061 rl.rlim_cur = nr_fds_wanted;
0062 if (rl.rlim_max < nr_fds_wanted) {
0063 int old_rlim_max = rl.rlim_max;
0064 rl.rlim_max = nr_fds_wanted;
0065
0066 int r = setrlimit(RLIMIT_NOFILE, &rl);
0067 __TEST_REQUIRE(r >= 0,
0068 "RLIMIT_NOFILE hard limit is too low (%d, wanted %d)\n",
0069 old_rlim_max, nr_fds_wanted);
0070 } else {
0071 TEST_ASSERT(!setrlimit(RLIMIT_NOFILE, &rl), "setrlimit() failed!");
0072 }
0073 }
0074
0075
0076
0077
0078
0079
0080 if (!kvm_max_vcpu_id)
0081 kvm_max_vcpu_id = kvm_max_vcpus;
0082
0083 TEST_ASSERT(kvm_max_vcpu_id >= kvm_max_vcpus,
0084 "KVM_MAX_VCPU_IDS (%d) must be at least as large as KVM_MAX_VCPUS (%d).",
0085 kvm_max_vcpu_id, kvm_max_vcpus);
0086
0087 test_vcpu_creation(0, kvm_max_vcpus);
0088
0089 if (kvm_max_vcpu_id > kvm_max_vcpus)
0090 test_vcpu_creation(
0091 kvm_max_vcpu_id - kvm_max_vcpus, kvm_max_vcpus);
0092
0093 return 0;
0094 }