1 /*
2 * Copyright © 2016-2017 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
20 * IN THE SOFTWARE.
21 */
22
23 /*
24 * This program is intended for testing of validate GVT-g virtual machine
25 * creation of allow for testing of KVMGT / XenGT (the tool should be root
26 * privilege only).
27 *
28 * TODO:
29 * Enable more GVT-g related test cases.
30 */
31 #include "config.h"
32
33 #include "igt.h"
34 #include <errno.h>
35 #include <getopt.h>
36 #include <math.h>
37 #include <stdint.h>
38 #include <stdbool.h>
39 #include <strings.h>
40 #include <unistd.h>
41 #include <termios.h>
42 #include <sys/poll.h>
43 #include <sys/time.h>
44 #include <sys/ioctl.h>
45 #include <sys/types.h>
46 #include <sys/stat.h>
47 #include <string.h>
48 #include <stdlib.h>
49 #include <signal.h>
50 #include <stdio.h>
51 #include <stdlib.h>
52 #include <time.h>
53 #include <unistd.h>
54 #include <pwd.h>
55 #include <limits.h>
56 #include <dirent.h>
57
58 #define RANDOM(x) (rand() % x)
59
60
61 static char uuid[40];
62 static char guest_ip[32];
63
64 static char mac_addr[20] = {0};
65 static char qemu_path[PATH_MAX] = {0};
66 static char hda_path[PATH_MAX] = {0};
67 static char bios_path[PATH_MAX] = {0};
68
super_system(const char * cmd,char * retmsg,int msg_len)69 static int super_system(const char *cmd, char *retmsg, int msg_len)
70 {
71 FILE *fp;
72 int res = -1;
73 if (cmd == NULL || retmsg == NULL || msg_len < 0) {
74 igt_info("Error: %s system parameter invalid!\n", __func__);
75 return 1;
76 }
77 fp = popen(cmd, "r");
78 if (fp == NULL) {
79 perror("popen");
80 igt_info("Error: %s popen error: %s\n", __func__, strerror(errno));
81 return 2;
82 } else {
83 memset(retmsg, 0, msg_len);
84 while (fgets(retmsg, msg_len, fp)) {
85 ;
86 }
87 res = pclose(fp);
88 if (res == -1) {
89 igt_info("Error:%s close popen file pointer fp error!\n", __func__);
90 return 3;
91 }
92 retmsg[strlen(retmsg) - 1] = '\0';
93 return 0;
94 }
95 }
96
check_gvtg_support(void)97 static int check_gvtg_support(void)
98 {
99 DIR *mdevdir = opendir("/sys/bus/pci/devices/0000:00:02.0/"
100 "mdev_supported_types");
101
102 if (mdevdir == NULL) {
103 return 1;
104 }
105 closedir(mdevdir);
106 return 0;
107 }
108
check_tools(void)109 static int check_tools(void)
110 {
111 if (system("which uuidgen > /dev/null") != 0) {
112 return 1;
113 } else if (system("which arp-scan > /dev/null") != 0) {
114 return 2;
115 } else if (system("which /etc/qemu-ifup > /dev/null") != 0) {
116 return 3;
117 }
118 return 0;
119 }
120
create_guest(void)121 static void create_guest(void)
122 {
123 unsigned int max_size_cmd = 4 * PATH_MAX;
124 char *command;
125
126 command = malloc(max_size_cmd);
127 if (!command)
128 return;
129
130 sprintf(command, "qemu-img create -b %s -f qcow2 %s.qcow2",
131 hda_path, hda_path);
132 igt_assert_eq(system(command), 0);
133
134 sprintf(command, "echo \"%s\" > /sys/bus/pci/devices/0000:00:02.0/"
135 "mdev_supported_types/$(ls /sys/bus/pci/devices/0000:00:02.0/"
136 "mdev_supported_types |awk {'print $1'}|tail -1)/create", uuid);
137 igt_assert_eq(system(command), 0);
138
139 sprintf(command, "%s -m 2048 -smp 2 -M pc -name gvtg_guest"
140 " -hda %s.qcow2 -bios %s -enable-kvm --net nic,macaddr=%s -net"
141 " tap,script=/etc/qemu-ifup -vga cirrus -k en-us"
142 " -serial stdio -vnc :1 -machine kernel_irqchip=on -global"
143 " PIIX4_PM.disable_s3=1 -global PIIX4_PM.disable_s4=1 -cpu host"
144 " -usb -usbdevice tablet -device vfio-pci,sysfsdev="
145 "/sys/bus/pci/devices/0000:00:02.0/%s &",
146 qemu_path, hda_path, bios_path, mac_addr, uuid);
147 igt_assert_eq(system(command), 0);
148
149 free(command);
150 }
151
destroy_all_guest(void)152 static void destroy_all_guest(void)
153 {
154 int ret = system("pkill qemu");
155
156 igt_assert(ret >= 0 && WIFEXITED(ret));
157 igt_assert(WEXITSTATUS(ret) == 0 || WEXITSTATUS(ret) == 1);
158 }
159
remove_vgpu(void)160 static void remove_vgpu(void)
161 {
162 char cmd[128] = {0};
163 sprintf(cmd, "echo 1 > /sys/bus/pci/devices/0000:00:02.0/%s/remove", uuid);
164 igt_assert_eq(system(cmd), 0);
165 }
166
gen_mac_addr(void)167 static void gen_mac_addr(void)
168 {
169 srand(getpid());
170 sprintf(mac_addr, "52:54:00:%02X:%02X:%02X", RANDOM(256), RANDOM(256),
171 RANDOM(256));
172 }
173
gen_uuid(void)174 static void gen_uuid(void)
175 {
176 igt_assert_eq(super_system("uuidgen", uuid, sizeof(uuid)), 0);
177 }
178
fetch_ip_by_mac(char * mac)179 static char *fetch_ip_by_mac(char *mac)
180 {
181 char cmd[1024] = {0};
182 sprintf(cmd, "arp-scan -l -I $(ip addr show|grep inet|grep global|"
183 "awk '{print $NF;}')|grep -i %s|awk '{print $1}'", mac);
184 igt_assert_eq(super_system(cmd, guest_ip, sizeof(guest_ip)), 0);
185 return guest_ip;
186 }
187
check_guest_ip(void)188 static int check_guest_ip(void)
189 {
190 int timeout = 0;
191 while (timeout <= 12) {
192 igt_info("Trying to connect guest, attempt %d.\n", timeout);
193 if (timeout == 12) {
194 igt_info("Cannot connect to guest.\n");
195 return 1;
196 break;
197 }
198
199 fetch_ip_by_mac(mac_addr);
200
201 if (guest_ip[0] != '\0') {
202 igt_info("Fetched guest ip address: %s.\n", guest_ip);
203 break;
204 }
205 timeout++;
206 sleep(5);
207 }
208 return 0;
209 }
210
clear_dmesg(void)211 static void clear_dmesg(void)
212 {
213 igt_assert_eq(system("dmesg -c > /dev/null"), 0);
214 }
215
check_dmesg(void)216 static int check_dmesg(void)
217 {
218 char errorlog[PATH_MAX] = {0};
219
220 igt_assert_eq(super_system("dmesg|grep -E \"GPU HANG|gfx reset|BUG\"",
221 errorlog, sizeof(errorlog)), 0);
222
223 if (errorlog[0] == '\0') {
224 return 0;
225 } else {
226 return 1;
227 }
228 }
229
print_help(void)230 static void print_help(void)
231 {
232 igt_info("\n[options]\n"
233 "-h, --help display usage\n"
234 "-q, --qemu the qemu path\n"
235 "-a, --hda the hda raw image / qcow path\n"
236 "-b, --bios the seabios path\n\n"
237 "[example]\n"
238 " ./intel_gvtg_test -q /usr/bin/qemu-system-x86_64 -a "
239 "/home/img/ubuntu-16.04.img -b /usr/bin/bios.bin\n"
240 );
241 }
242
arg_mismatch(const char * arg)243 static void arg_mismatch(const char *arg)
244 {
245 igt_info("argument mismatch: %s\n", arg);
246 }
247
main(int argc,char * argv[])248 int main(int argc, char *argv[])
249 {
250 int opt = -1;
251 const char *optstring = "hq:a:b:";
252 static struct option long_options[] = {
253 {"help", 0, NULL, 'h'},
254 {"qemu", 1, NULL, 'q'},
255 {"hda", 1, NULL, 'a'},
256 {"bios", 1, NULL, 'b'},
257 {0, 0, 0, 0}
258 };
259
260 int ret = 0;
261 int flag_cnt = 0;
262 int h_flag = 0;
263 int q_flag = 0;
264 int a_flag = 0;
265 int b_flag = 0;
266 int tools_status = 0;
267
268 igt_skip_on_f(check_gvtg_support() == 1,
269 "GVT-g technology is not supported in your system.\n");
270
271 tools_status = check_tools();
272 igt_skip_on_f(tools_status == 1, "Please install the \"uuid-runtime\" tool.\n");
273 igt_skip_on_f(tools_status == 2, "Please install the \"arp-scan\" tool.\n");
274 igt_skip_on_f(tools_status == 3, "Please prepare the \"qemu-ifup\" script.\n");
275
276 if (argc == 1) {
277 print_help();
278 return 0;
279 }
280
281 while ((opt = getopt_long(argc, argv, optstring, long_options, NULL))
282 != -1) {
283 switch (opt) {
284 case 'h':
285 h_flag = 1;
286 break;
287 case 'q':
288 strcpy(qemu_path, optarg);
289 q_flag = 1;
290 break;
291 case 'a':
292 strcpy(hda_path, optarg);
293 a_flag = 1;
294 break;
295 case 'b':
296 strcpy(bios_path, optarg);
297 b_flag = 1;
298 break;
299 case ':':
300 ret = -1;
301 break;
302 case '?':
303 ret = -1;
304 break;
305 }
306
307 if (ret) {
308 break;
309 }
310 };
311
312 if (ret != 0) {
313 return ret;
314 }
315
316 flag_cnt = h_flag + q_flag + a_flag + b_flag;
317
318 if (flag_cnt == 1) {
319 if (h_flag == 1) {
320 print_help();
321 return 0;
322 } else {
323 arg_mismatch(argv[1]);
324 return -1;
325 }
326 } else if (flag_cnt == 3) {
327 if (q_flag == 1 && a_flag == 1 && b_flag == 1) {
328 igt_info("\nqemu_path: %s\nhda_path: %s\nbios_path: %s\n",
329 qemu_path, hda_path, bios_path);
330 } else {
331 arg_mismatch("-h");
332 return -1;
333 }
334 } else {
335 arg_mismatch(argv[1]);
336 return -1;
337 }
338
339 destroy_all_guest();
340 clear_dmesg();
341
342 gen_mac_addr();
343 gen_uuid();
344 create_guest();
345
346 if (check_guest_ip()) {
347 ret = 1;
348 }
349
350 destroy_all_guest();
351 sleep(5);
352 remove_vgpu();
353
354 if (check_dmesg() > 0) {
355 ret = 1;
356 }
357
358 igt_assert_eq(ret, 0);
359 igt_exit();
360
361 }
362
363