From patchwork Fri Dec 30 17:24:53 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: "Xu, Terrence" X-Patchwork-Id: 9492337 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id D7A1260453 for ; Fri, 30 Dec 2016 17:28:58 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id BDACA1FF8F for ; Fri, 30 Dec 2016 17:28:58 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id B0BDC22701; Fri, 30 Dec 2016 17:28:58 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-4.2 required=2.0 tests=BAYES_00, RCVD_IN_DNSWL_MED autolearn=ham version=3.3.1 Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher DHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 8E7E21FF8F for ; Fri, 30 Dec 2016 17:28:57 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 4698B6E1DA; Fri, 30 Dec 2016 17:28:53 +0000 (UTC) X-Original-To: intel-gfx@lists.freedesktop.org Delivered-To: intel-gfx@lists.freedesktop.org Received: from mga04.intel.com (mga04.intel.com [192.55.52.120]) by gabe.freedesktop.org (Postfix) with ESMTPS id CE1856E1DA for ; Fri, 30 Dec 2016 17:28:50 +0000 (UTC) Received: from orsmga001.jf.intel.com ([10.7.209.18]) by fmsmga104.fm.intel.com with ESMTP; 30 Dec 2016 09:28:49 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos; i="5.33,432,1477983600"; d="scan'208"; a="1077690491" Received: from buildserver-ub.sh.intel.com ([10.239.47.232]) by orsmga001.jf.intel.com with ESMTP; 30 Dec 2016 09:28:47 -0800 From: Terrence Xu To: intel-gfx@lists.freedesktop.org Date: Sat, 31 Dec 2016 01:24:53 +0800 Message-Id: <1483118693-5542-1-git-send-email-terrence.xu@intel.com> X-Mailer: git-send-email 1.9.1 MIME-Version: 1.0 Cc: zhenyu.z.wang@intel.com, tomi.p.sarvela@intel.com, Terrence Xu , zhiyuan.lv@intel.com, gordon.jin@intel.com, Benyu Xu , hongbo.wang@intel.com Subject: [Intel-gfx] [PATCH igt] Add the new case for create GVT-g Linux guest based on KVMGT X-BeenThere: intel-gfx@lists.freedesktop.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: Intel graphics driver community testing & development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: intel-gfx-bounces@lists.freedesktop.org Sender: "Intel-gfx" X-Virus-Scanned: ClamAV using ClamSMTP GVT-g (IntelĀ® Graphics Virtualization Technology) is a full GPU virtualization solution with mediated pass-through support. This case is for create basic Linux guest based on KVMGT with VFIO framework, it including create vgpu, create guest, check IP address, destroy guest, remove vgpu,check dmesg log, etc functions. Signed-off-by: Terrence Xu Signed-off-by: Benyu Xu --- tests/Makefile.sources | 1 + tests/gvtg_guest.c | 323 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 324 insertions(+) create mode 100644 tests/gvtg_guest.c diff --git a/tests/Makefile.sources b/tests/Makefile.sources index 04dd2d5..09b936e 100644 --- a/tests/Makefile.sources +++ b/tests/Makefile.sources @@ -219,6 +219,7 @@ TESTS_progs = \ prime_udl \ drv_module_reload \ kms_sysfs_edid_timing \ + gvtg_guest \ $(NULL) # IMPORTANT: The ZZ_ tests need to be run last! diff --git a/tests/gvtg_guest.c b/tests/gvtg_guest.c new file mode 100644 index 0000000..b9e6304 --- /dev/null +++ b/tests/gvtg_guest.c @@ -0,0 +1,323 @@ +/* + * Copyright 2016 Intel Corporation + * Terrence Xu + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + */ + +/* + * This program is intended for testing of validate GVT-g virtual machine + * creation of allow for testing of KVMGT / XenGT. + * + * TODO: + * Enable more GVT-g related test cases. + */ +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include "igt.h" +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define RANDOM(x) (rand() % x) + +static unsigned char mac_addr[17] = {'0', '0', ':', '0', '0', ':', + '0', '0', ':', '0', '0', ':', '0', '0', ':', '0', '0'}; +static char uuid[40]; +static char guest_ip[32]; + +static char qemu_path[PATH_MAX] = {0}; +static char hda_path[PATH_MAX] = {0}; +static char bios_path[PATH_MAX] = {0}; + +static int super_system(const char *cmd, char *retmsg, int msg_len) +{ + FILE *fp; + int res = -1; + if (cmd == NULL || retmsg == NULL || msg_len < 0) { + igt_info("Error: %s system paramer invalid!\n", __func__); + return 1; + } + fp = popen(cmd, "r"); + if (fp == NULL) { + perror("popen"); + igt_info("Error: %s popen error: %s\n", __func__, strerror(errno)); + return 2; + } else { + memset(retmsg, 0, msg_len); + while (fgets(retmsg, msg_len, fp)) { + ; + }; + res = pclose(fp); + if (res == -1) { + igt_info("Error:%s close popen file pointer fp error!\n", __func__); + return 3; + } + retmsg[strlen(retmsg) - 1] = '\0'; + return 0; + } +} + +static void create_guest(void) +{ + char create_qcow_cmd[PATH_MAX] = {0}; + char create_vgpu_cmd[PATH_MAX] = {0}; + char create_instance_cmd[PATH_MAX] = {0}; + + sprintf(create_qcow_cmd, "qemu-img create -b %s -f qcow2 %s.qcow2", + hda_path, hda_path); + sprintf(create_vgpu_cmd, "echo \"%s\" > /sys/bus/pci/devices/0000:00:02.0/" + "mdev_supported_types/i915-GVTg_V4_1/create", uuid); + sprintf(create_instance_cmd, "%s -m 2048 -smp 2 -M pc -name gvtg_guest" + " -hda %s.qcow2 -bios %s -enable-kvm --net nic,macaddr=%s -net" + " tap,script=/etc/qemu-ifup -vga none -device isa-vga -k en-us" + " -serial stdio -vnc :1 -machine kernel_irqchip=on -global" + " PIIX4_PM.disable_s3=1 -global PIIX4_PM.disable_s4=1 -cpu host" + " -usb -usbdevice tablet -device vfio-pci,sysfsdev=" + "/sys/bus/pci/devices/0000:00:02.0/%s &", + qemu_path, hda_path, bios_path, mac_addr, uuid); + system(create_qcow_cmd); + system(create_vgpu_cmd); + system(create_instance_cmd); +} + +static void destroy_all_guest(void) +{ + system("pkill qemu"); +} + +static void remove_vgpu(void) +{ + char cmd[128] = {0}; + sprintf(cmd, "echo 1 > /sys/bus/pci/devices/0000:00:02.0/%s/remove", uuid); + system(cmd); +} + +static void gen_mac_addr(void) +{ + unsigned char mac_enum[16] = {'0', '1', '2', '3', '4', '5', '6', + '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'}; + unsigned short i, n = 2; + srand(getpid()); + for (i = 2, n = 2; i < 15; i++, n++) { + if (mac_addr[n] != ':') { + mac_addr[n] = mac_enum[RANDOM(16)]; + } + } +} + +static void gen_uuid(void) +{ + super_system("uuidgen", uuid, sizeof(uuid)); +} + +static char *fetch_ip_by_mac(unsigned char *mac) +{ + char cmd[1024] = {0}; + sprintf(cmd, "arp-scan -l -I $(ip addr show|grep inet|grep global|" + "awk '{print $NF;}')|grep -i %s|awk '{print $1}'", mac); + super_system(cmd, guest_ip, sizeof(guest_ip)); + return guest_ip; +} + +static int check_guest_ip(void) +{ + int timeout = 0; + while (timeout <= 12) { + igt_info("Try to connnect guest,the %d time.\n", timeout); + if (timeout == 12) { + igt_info("Cannot connect to guest.\n"); + return 1; + break; + } + + fetch_ip_by_mac(mac_addr); + + if (guest_ip[0] != '\0') { + igt_info("Fetched guest ip address: %s.\n", guest_ip); + break; + } + timeout++; + sleep(5); + } + return 0; +} + +static void clear_dmesg(void) +{ + system("dmesg -c"); +} + +static int check_dmesg(void) +{ + char errorlog[PATH_MAX] = {0}; + + super_system("dmesg|grep -E \"GPU HANG|gfx reset|BUG\"", errorlog, + sizeof(errorlog)); + + if (errorlog[0] == '\0') { + return 0; + } else { + return 1; + } +} + +static const char optstr[] = "h"; + +static void print_help(void) +{ + igt_info("\n [options]\n" + "-h, --help display usage\n" + "-q, --qemu the qemu path\n" + "-a, --hda the hda raw image / qcow path\n" + "-b, --bios the seabios path\n\n" + ); +} + +static void arg_mismatch(void) +{ + igt_info(" argument mismatch!\n"); +} + +int main(int argc, char **argv) +{ + int opt = -1; + const char *optstring = "hq:a:b:"; + static struct option long_options[] = { + {"help", 0, NULL, 'h'}, + {"qemu", 1, NULL, 'q'}, + {"hda", 1, NULL, 'a'}, + {"bios", 1, NULL, 'b'}, + {0, 0, 0, 0} + }; + + int ret = 0; + int flag_cnt = 0; + int h_flag = 0; + int q_flag = 0; + int a_flag = 0; + int b_flag = 0; + + if (argc == 1) { + print_help(); + return 0; + } + + while ((opt = getopt_long(argc, argv, optstring, long_options, NULL)) + != -1) { + switch (opt) { + case 'h': + h_flag = 1; + break; + case 'q': + strcpy(qemu_path, optarg); + q_flag = 1; + break; + case 'a': + strcpy(hda_path, optarg); + a_flag = 1; + break; + case 'b': + strcpy(bios_path, optarg); + b_flag = 1; + break; + case ':': + ret = -1; + break; + case '?': + ret = -1; + break; + } + + if (ret) { + break; + } + }; + + if (ret != 0) { + arg_mismatch(); + return ret; + } + + flag_cnt = h_flag + q_flag + a_flag + b_flag; + + if (flag_cnt == 1) { + if (h_flag == 1) { + print_help(); + return 0; + } else { + arg_mismatch(); + return -1; + } + } else if (flag_cnt == 3) { + if (q_flag == 1 && a_flag == 1 && b_flag == 1) { + igt_info("\nqemu_path: %s\n hda_path: %s\n bios_path: %s\n", + qemu_path, hda_path, bios_path); + } else { + arg_mismatch(); + return -1; + } + } else { + arg_mismatch(); + return -1; + } + + destroy_all_guest(); + clear_dmesg(); + + gen_mac_addr(); + gen_uuid(); + create_guest(); + + if (check_guest_ip()) { + ret = 1; + } + + destroy_all_guest(); + sleep(5); + remove_vgpu(); + + if (check_dmesg() > 0) { + ret = 1; + } + + igt_assert_eq(ret, 0); + igt_exit(); +}