| 1 | /* |
| 2 | * QTest testcase for CPU plugging |
| 3 | * |
| 4 | * Copyright (c) 2015 SUSE Linux GmbH |
| 5 | * |
| 6 | * This work is licensed under the terms of the GNU GPL, version 2 or later. |
| 7 | * See the COPYING file in the top-level directory. |
| 8 | */ |
| 9 | |
| 10 | #include "qemu/osdep.h" |
| 11 | |
| 12 | #include "libqtest-single.h" |
| 13 | #include "qobject/qdict.h" |
| 14 | #include "qobject/qlist.h" |
| 15 | |
| 16 | struct PlugTestData { |
| 17 | const char *machine; |
| 18 | const char *cpu_model; |
| 19 | char *device_model; |
| 20 | unsigned sockets; |
| 21 | unsigned cores; |
| 22 | unsigned threads; |
| 23 | unsigned maxcpus; |
| 24 | }; |
| 25 | typedef struct PlugTestData PlugTestData; |
| 26 | |
| 27 | static void test_plug_with_device_add(gconstpointer data) |
| 28 | { |
| 29 | const PlugTestData *td = data; |
| 30 | char *args; |
| 31 | QTestState *qts; |
| 32 | QDict *resp; |
| 33 | QList *cpus; |
| 34 | QObject *e; |
| 35 | int hotplugged = 0; |
| 36 | |
| 37 | args = g_strdup_printf("-machine %s -cpu %s " |
| 38 | "-smp 1,sockets=%u,cores=%u,threads=%u,maxcpus=%u", |
| 39 | td->machine, td->cpu_model, |
| 40 | td->sockets, td->cores, td->threads, td->maxcpus); |
| 41 | qts = qtest_init(args); |
| 42 | |
| 43 | resp = qtest_qmp(qts, "{ 'execute': 'query-hotpluggable-cpus'}"); |
| 44 | g_assert(qdict_haskey(resp, "return")); |
| 45 | cpus = qdict_get_qlist(resp, "return"); |
| 46 | g_assert(cpus); |
| 47 | |
| 48 | while ((e = qlist_pop(cpus))) { |
| 49 | const QDict *cpu, *props; |
| 50 | |
| 51 | cpu = qobject_to(QDict, e); |
| 52 | if (qdict_haskey(cpu, "qom-path")) { |
| 53 | qobject_unref(e); |
| 54 | continue; |
| 55 | } |
| 56 | |
| 57 | g_assert(qdict_haskey(cpu, "props")); |
| 58 | props = qdict_get_qdict(cpu, "props"); |
| 59 | |
| 60 | qtest_qmp_device_add_qdict(qts, td->device_model, props); |
| 61 | hotplugged++; |
| 62 | qobject_unref(e); |
| 63 | } |
| 64 | |
| 65 | /* make sure that there were hotplugged CPUs */ |
| 66 | g_assert(hotplugged); |
| 67 | qobject_unref(resp); |
| 68 | qtest_quit(qts); |
| 69 | g_free(args); |
| 70 | } |
| 71 | |
| 72 | static void test_data_free(gpointer data) |
| 73 | { |
| 74 | PlugTestData *pc = data; |
| 75 | |
| 76 | g_free(pc->device_model); |
| 77 | g_free(pc); |
| 78 | } |
| 79 | |
| 80 | static void add_pc_test_case(const char *mname) |
| 81 | { |
| 82 | char *path; |
| 83 | PlugTestData *data; |
| 84 | |
| 85 | if (!g_str_has_prefix(mname, "pc-")) { |
| 86 | return; |
| 87 | } |
| 88 | data = g_new(PlugTestData, 1); |
| 89 | data->machine = mname; |
| 90 | data->cpu_model = "Haswell"; /* 1.3+ theoretically */ |
| 91 | data->device_model = g_strdup_printf("%s-%s-cpu", data->cpu_model, |
| 92 | qtest_get_arch()); |
| 93 | data->sockets = 1; |
| 94 | data->cores = 3; |
| 95 | data->threads = 2; |
| 96 | data->maxcpus = data->sockets * data->cores * data->threads; |
| 97 | |
| 98 | path = g_strdup_printf("cpu-plug/%s/device-add/%ux%ux%u&maxcpus=%u", |
| 99 | mname, data->sockets, data->cores, |
| 100 | data->threads, data->maxcpus); |
| 101 | qtest_add_data_func_full(path, data, test_plug_with_device_add, |
| 102 | test_data_free); |
| 103 | g_free(path); |
| 104 | } |
| 105 | |
| 106 | static void add_pseries_test_case(const char *mname) |
| 107 | { |
| 108 | char *path; |
| 109 | PlugTestData *data; |
| 110 | |
| 111 | if (!g_str_has_prefix(mname, "pseries-") || |
| 112 | (g_str_has_prefix(mname, "pseries-2.") && atoi(&mname[10]) < 7)) { |
| 113 | return; |
| 114 | } |
| 115 | data = g_new(PlugTestData, 1); |
| 116 | data->machine = mname; |
| 117 | data->cpu_model = "power8_v2.0"; |
| 118 | data->device_model = g_strdup("power8_v2.0-spapr-cpu-core"); |
| 119 | data->sockets = 2; |
| 120 | data->cores = 3; |
| 121 | data->threads = 1; |
| 122 | data->maxcpus = data->sockets * data->cores * data->threads; |
| 123 | |
| 124 | path = g_strdup_printf("cpu-plug/%s/device-add/%ux%ux%u&maxcpus=%u", |
| 125 | mname, data->sockets, data->cores, |
| 126 | data->threads, data->maxcpus); |
| 127 | qtest_add_data_func_full(path, data, test_plug_with_device_add, |
| 128 | test_data_free); |
| 129 | g_free(path); |
| 130 | } |
| 131 | |
| 132 | static void add_s390x_test_case(const char *mname) |
| 133 | { |
| 134 | char *path; |
| 135 | PlugTestData *data; |
| 136 | |
| 137 | if (!g_str_has_prefix(mname, "s390-ccw-virtio-")) { |
| 138 | return; |
| 139 | } |
| 140 | |
| 141 | data = g_new(PlugTestData, 1); |
| 142 | data->machine = mname; |
| 143 | data->cpu_model = "qemu"; |
| 144 | data->device_model = g_strdup("qemu-s390x-cpu"); |
| 145 | data->sockets = 1; |
| 146 | data->cores = 3; |
| 147 | data->threads = 1; |
| 148 | data->maxcpus = data->sockets * data->cores * data->threads; |
| 149 | |
| 150 | path = g_strdup_printf("cpu-plug/%s/device-add/%ux%ux%u&maxcpus=%u", |
| 151 | mname, data->sockets, data->cores, |
| 152 | data->threads, data->maxcpus); |
| 153 | qtest_add_data_func_full(path, data, test_plug_with_device_add, |
| 154 | test_data_free); |
| 155 | g_free(path); |
| 156 | } |
| 157 | |
| 158 | static void add_loongarch_test_case(const char *mname) |
| 159 | { |
| 160 | char *path; |
| 161 | PlugTestData *data; |
| 162 | |
| 163 | data = g_new(PlugTestData, 1); |
| 164 | data->machine = mname; |
| 165 | data->cpu_model = "la464"; |
| 166 | data->device_model = g_strdup("la464-loongarch-cpu"); |
| 167 | data->sockets = 1; |
| 168 | data->cores = 3; |
| 169 | data->threads = 1; |
| 170 | data->maxcpus = data->sockets * data->cores * data->threads; |
| 171 | |
| 172 | path = g_strdup_printf("cpu-plug/%s/device-add/%ux%ux%u&maxcpus=%u", |
| 173 | mname, data->sockets, data->cores, |
| 174 | data->threads, data->maxcpus); |
| 175 | qtest_add_data_func_full(path, data, test_plug_with_device_add, |
| 176 | test_data_free); |
| 177 | g_free(path); |
| 178 | } |
| 179 | |
| 180 | int main(int argc, char **argv) |
| 181 | { |
| 182 | const char *arch = qtest_get_arch(); |
| 183 | |
| 184 | g_test_init(&argc, &argv, NULL); |
| 185 | |
| 186 | if (strcmp(arch, "i386") == 0 || strcmp(arch, "x86_64") == 0) { |
| 187 | qtest_cb_for_every_machine(add_pc_test_case, g_test_quick()); |
| 188 | } else if (g_str_equal(arch, "ppc64")) { |
| 189 | qtest_cb_for_every_machine(add_pseries_test_case, g_test_quick()); |
| 190 | } else if (g_str_equal(arch, "s390x")) { |
| 191 | qtest_cb_for_every_machine(add_s390x_test_case, g_test_quick()); |
| 192 | } else if (g_str_equal(arch, "loongarch64") && qtest_has_machine("virt")) { |
| 193 | add_loongarch_test_case("virt"); |
| 194 | } |
| 195 | |
| 196 | return g_test_run(); |
| 197 | } |