Blame SOURCES/hvd-Add-vmbus_testing-tool-build-files.patch

f32269
From d8ca5e0a429b8f7395e136e713980db6a7ac8dc2 Mon Sep 17 00:00:00 2001
f32269
From: Mohammed Gamal <mgamal@redhat.com>
f32269
Date: Wed, 15 Apr 2020 12:00:30 +0200
f32269
Subject: [PATCH 2/2] Add vmbus_testing tool build files
f32269
f32269
RH-Author: Mohammed Gamal <mgamal@redhat.com>
f32269
Message-id: <20200414183955.194006-3-mgamal@redhat.com>
f32269
Patchwork-id: 94690
f32269
O-Subject: [RHEL8.3 virt hyperv-daemons PATCH v5 2/2] Add vmbus_testing tool build files
f32269
Bugzilla: 1816750
f32269
RH-Acked-by: Vitaly Kuznetsov <vkuznets@redhat.com>
f32269
RH-Acked-by: Cathy Avery <cavery@redhat.com>
f32269
f32269
Add the vmbus_testing tool to redhat build dirs
f32269
f32269
Signed-off-by: Mohammed Gamal <mgamal@redhat.com>
f32269
---
f32269
 redhat/hyperv-daemons.spec.template |   2 +
f32269
 vmbus_testing                       | 376 ++++++++++++++++++++++++++++
f32269
 2 files changed, 378 insertions(+)
f32269
 create mode 100755 vmbus_testing
f32269
f32269
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
f32269
---
f32269
 redhat/hyperv-daemons.spec.template |   2 +
f32269
 vmbus_testing                       | 376 ++++++++++++++++++++++++++++++++++++
f32269
 2 files changed, 378 insertions(+)
f32269
 create mode 100755 vmbus_testing
f32269
f32269
diff --git a/vmbus_testing b/vmbus_testing
f32269
new file mode 100755
f32269
index 0000000..e721290
f32269
--- /dev/null
f32269
+++ b/vmbus_testing
f32269
@@ -0,0 +1,376 @@
f32269
+#!/usr/bin/env python3
f32269
+# SPDX-License-Identifier: GPL-2.0
f32269
+#
f32269
+# Program to allow users to fuzz test Hyper-V drivers
f32269
+# by interfacing with Hyper-V debugfs attributes.
f32269
+# Current test methods available:
f32269
+#       1. delay testing
f32269
+#
f32269
+# Current file/directory structure of hyper-V debugfs:
f32269
+#       /sys/kernel/debug/hyperv/UUID
f32269
+#       /sys/kernel/debug/hyperv/UUID/<test-state filename>
f32269
+#       /sys/kernel/debug/hyperv/UUID/<test-method sub-directory>
f32269
+#
f32269
+# author: Branden Bonaby <brandonbonaby94@gmail.com>
f32269
+
f32269
+import os
f32269
+import cmd
f32269
+import argparse
f32269
+import glob
f32269
+from argparse import RawDescriptionHelpFormatter
f32269
+from argparse import RawTextHelpFormatter
f32269
+from enum import Enum
f32269
+
f32269
+# Do not change unless, you change the debugfs attributes
f32269
+# in /drivers/hv/debugfs.c. All fuzz testing
f32269
+# attributes will start with "fuzz_test".
f32269
+
f32269
+# debugfs path for hyperv must exist before proceeding
f32269
+debugfs_hyperv_path = "/sys/kernel/debug/hyperv"
f32269
+if not os.path.isdir(debugfs_hyperv_path):
f32269
+        print("{} doesn't exist/check permissions".format(debugfs_hyperv_path))
f32269
+        exit(-1)
f32269
+
f32269
+class dev_state(Enum):
f32269
+        off = 0
f32269
+        on = 1
f32269
+
f32269
+# File names, that correspond to the files created in
f32269
+# /drivers/hv/debugfs.c
f32269
+class f_names(Enum):
f32269
+        state_f = "fuzz_test_state"
f32269
+        buff_f =  "fuzz_test_buffer_interrupt_delay"
f32269
+        mess_f =  "fuzz_test_message_delay"
f32269
+
f32269
+# Both single_actions and all_actions are used
f32269
+# for error checking and to allow for some subparser
f32269
+# names to be abbreviated. Do not abbreviate the
f32269
+# test method names, as it will become less intuitive
f32269
+# as to what the user can do. If you do decide to
f32269
+# abbreviate the test method name, make sure the main
f32269
+# function reflects this change.
f32269
+
f32269
+all_actions = [
f32269
+        "disable_all",
f32269
+        "D",
f32269
+        "enable_all",
f32269
+        "view_all",
f32269
+        "V"
f32269
+]
f32269
+
f32269
+single_actions = [
f32269
+        "disable_single",
f32269
+        "d",
f32269
+        "enable_single",
f32269
+        "view_single",
f32269
+        "v"
f32269
+]
f32269
+
f32269
+def main():
f32269
+
f32269
+        file_map = recursive_file_lookup(debugfs_hyperv_path, dict())
f32269
+        args = parse_args()
f32269
+        if (not args.action):
f32269
+                print ("Error, no options selected...exiting")
f32269
+                exit(-1)
f32269
+        arg_set = { k for (k,v) in vars(args).items() if v and k != "action" }
f32269
+        arg_set.add(args.action)
f32269
+        path = args.path if "path" in arg_set else None
f32269
+        if (path and path[-1] == "/"):
f32269
+                path = path[:-1]
f32269
+        validate_args_path(path, arg_set, file_map)
f32269
+        if (path and "enable_single" in arg_set):
f32269
+            state_path = locate_state(path, file_map)
f32269
+            set_test_state(state_path, dev_state.on.value, args.quiet)
f32269
+
f32269
+        # Use subparsers as the key for different actions
f32269
+        if ("delay" in arg_set):
f32269
+                validate_delay_values(args.delay_time)
f32269
+                if (args.enable_all):
f32269
+                        set_delay_all_devices(file_map, args.delay_time,
f32269
+                                              args.quiet)
f32269
+                else:
f32269
+                        set_delay_values(path, file_map, args.delay_time,
f32269
+                                         args.quiet)
f32269
+        elif ("disable_all" in arg_set or "D" in arg_set):
f32269
+                disable_all_testing(file_map)
f32269
+        elif ("disable_single" in arg_set or "d" in arg_set):
f32269
+                disable_testing_single_device(path, file_map)
f32269
+        elif ("view_all" in arg_set or "V" in arg_set):
f32269
+                get_all_devices_test_status(file_map)
f32269
+        elif ("view_single" in arg_set or  "v" in arg_set):
f32269
+                get_device_test_values(path, file_map)
f32269
+
f32269
+# Get the state location
f32269
+def locate_state(device, file_map):
f32269
+        return file_map[device][f_names.state_f.value]
f32269
+
f32269
+# Validate delay values to make sure they are acceptable to
f32269
+# enable delays on a device
f32269
+def validate_delay_values(delay):
f32269
+
f32269
+        if (delay[0]  == -1 and delay[1] == -1):
f32269
+                print("\nError, At least 1 value must be greater than 0")
f32269
+                exit(-1)
f32269
+        for i in delay:
f32269
+                if (i < -1 or i == 0 or i > 1000):
f32269
+                        print("\nError, Values must be  equal to -1 "
f32269
+                              "or be > 0 and <= 1000")
f32269
+                        exit(-1)
f32269
+
f32269
+# Validate argument path
f32269
+def validate_args_path(path, arg_set, file_map):
f32269
+
f32269
+        if (not path and any(element in arg_set for element in single_actions)):
f32269
+                print("Error, path (-p) REQUIRED for the specified option. "
f32269
+                      "Use (-h) to check usage.")
f32269
+                exit(-1)
f32269
+        elif (path and any(item in arg_set for item in all_actions)):
f32269
+                print("Error, path (-p) NOT REQUIRED for the specified option. "
f32269
+                      "Use (-h) to check usage." )
f32269
+                exit(-1)
f32269
+        elif (path not in file_map and any(item in arg_set
f32269
+                                           for item in single_actions)):
f32269
+                print("Error, path '{}' not a valid vmbus device".format(path))
f32269
+                exit(-1)
f32269
+
f32269
+# display Testing status of single device
f32269
+def get_device_test_values(path, file_map):
f32269
+
f32269
+        for name in file_map[path]:
f32269
+                file_location = file_map[path][name]
f32269
+                print( name + " = " + str(read_test_files(file_location)))
f32269
+
f32269
+# Create a map of the vmbus devices and their associated files
f32269
+# [key=device, value = [key = filename, value = file path]]
f32269
+def recursive_file_lookup(path, file_map):
f32269
+
f32269
+        for f_path in glob.iglob(path + '**/*'):
f32269
+                if (os.path.isfile(f_path)):
f32269
+                        if (f_path.rsplit("/",2)[0] == debugfs_hyperv_path):
f32269
+                                directory = f_path.rsplit("/",1)[0]
f32269
+                        else:
f32269
+                                directory = f_path.rsplit("/",2)[0]
f32269
+                        f_name = f_path.split("/")[-1]
f32269
+                        if (file_map.get(directory)):
f32269
+                                file_map[directory].update({f_name:f_path})
f32269
+                        else:
f32269
+                                file_map[directory] = {f_name:f_path}
f32269
+                elif (os.path.isdir(f_path)):
f32269
+                        recursive_file_lookup(f_path,file_map)
f32269
+        return file_map
f32269
+
f32269
+# display Testing state of devices
f32269
+def get_all_devices_test_status(file_map):
f32269
+
f32269
+        for device in file_map:
f32269
+                if (get_test_state(locate_state(device, file_map)) is 1):
f32269
+                        print("Testing = ON for: {}"
f32269
+                              .format(device.split("/")[5]))
f32269
+                else:
f32269
+                        print("Testing = OFF for: {}"
f32269
+                              .format(device.split("/")[5]))
f32269
+
f32269
+# read the vmbus device files, path must be absolute path before calling
f32269
+def read_test_files(path):
f32269
+        try:
f32269
+                with open(path,"r") as f:
f32269
+                        file_value = f.readline().strip()
f32269
+                return int(file_value)
f32269
+
f32269
+        except IOError as e:
f32269
+                errno, strerror = e.args
f32269
+                print("I/O error({0}): {1} on file {2}"
f32269
+                      .format(errno, strerror, path))
f32269
+                exit(-1)
f32269
+        except ValueError:
f32269
+                print ("Element to int conversion error in: \n{}".format(path))
f32269
+                exit(-1)
f32269
+
f32269
+# writing to vmbus device files, path must be absolute path before calling
f32269
+def write_test_files(path, value):
f32269
+
f32269
+        try:
f32269
+                with open(path,"w") as f:
f32269
+                        f.write("{}".format(value))
f32269
+        except IOError as e:
f32269
+                errno, strerror = e.args
f32269
+                print("I/O error({0}): {1} on file {2}"
f32269
+                      .format(errno, strerror, path))
f32269
+                exit(-1)
f32269
+
f32269
+# set testing state of device
f32269
+def set_test_state(state_path, state_value, quiet):
f32269
+
f32269
+        write_test_files(state_path, state_value)
f32269
+        if (get_test_state(state_path) is 1):
f32269
+                if (not quiet):
f32269
+                        print("Testing = ON for device: {}"
f32269
+                              .format(state_path.split("/")[5]))
f32269
+        else:
f32269
+                if (not quiet):
f32269
+                        print("Testing = OFF for device: {}"
f32269
+                              .format(state_path.split("/")[5]))
f32269
+
f32269
+# get testing state of device
f32269
+def get_test_state(state_path):
f32269
+        #state == 1 - test = ON
f32269
+        #state == 0 - test = OFF
f32269
+        return  read_test_files(state_path)
f32269
+
f32269
+# write 1 - 1000 microseconds, into a single device using the
f32269
+# fuzz_test_buffer_interrupt_delay and fuzz_test_message_delay
f32269
+# debugfs attributes
f32269
+def set_delay_values(device, file_map, delay_length, quiet):
f32269
+
f32269
+        try:
f32269
+                interrupt = file_map[device][f_names.buff_f.value]
f32269
+                message = file_map[device][f_names.mess_f.value]
f32269
+
f32269
+                # delay[0]- buffer interrupt delay, delay[1]- message delay
f32269
+                if (delay_length[0] >= 0 and delay_length[0] <= 1000):
f32269
+                        write_test_files(interrupt, delay_length[0])
f32269
+                if (delay_length[1] >= 0 and delay_length[1] <= 1000):
f32269
+                        write_test_files(message, delay_length[1])
f32269
+                if (not quiet):
f32269
+                        print("Buffer delay testing = {} for: {}"
f32269
+                              .format(read_test_files(interrupt),
f32269
+                                      interrupt.split("/")[5]))
f32269
+                        print("Message delay testing = {} for: {}"
f32269
+                              .format(read_test_files(message),
f32269
+                                      message.split("/")[5]))
f32269
+        except IOError as e:
f32269
+                errno, strerror = e.args
f32269
+                print("I/O error({0}): {1} on files {2}{3}"
f32269
+                      .format(errno, strerror, interrupt, message))
f32269
+                exit(-1)
f32269
+
f32269
+# enabling delay testing on all devices
f32269
+def set_delay_all_devices(file_map, delay, quiet):
f32269
+
f32269
+        for device in (file_map):
f32269
+                set_test_state(locate_state(device, file_map),
f32269
+                               dev_state.on.value,
f32269
+                               quiet)
f32269
+                set_delay_values(device, file_map, delay, quiet)
f32269
+
f32269
+# disable all testing on a SINGLE device.
f32269
+def disable_testing_single_device(device, file_map):
f32269
+
f32269
+        for name in file_map[device]:
f32269
+                file_location = file_map[device][name]
f32269
+                write_test_files(file_location, dev_state.off.value)
f32269
+        print("ALL testing now OFF for {}".format(device.split("/")[-1]))
f32269
+
f32269
+# disable all testing on ALL devices
f32269
+def disable_all_testing(file_map):
f32269
+
f32269
+        for device in file_map:
f32269
+                disable_testing_single_device(device, file_map)
f32269
+
f32269
+def parse_args():
f32269
+        parser = argparse.ArgumentParser(prog = "vmbus_testing",usage ="\n"
f32269
+                "%(prog)s [delay]   [-h] [-e|-E] -t [-p]\n"
f32269
+                "%(prog)s [view_all       | V]      [-h]\n"
f32269
+                "%(prog)s [disable_all    | D]      [-h]\n"
f32269
+                "%(prog)s [disable_single | d]      [-h|-p]\n"
f32269
+                "%(prog)s [view_single    | v]      [-h|-p]\n"
f32269
+                "%(prog)s --version\n",
f32269
+                description = "\nUse lsvmbus to get vmbus device type "
f32269
+                "information.\n" "\nThe debugfs root path is "
f32269
+                "/sys/kernel/debug/hyperv",
f32269
+                formatter_class = RawDescriptionHelpFormatter)
f32269
+        subparsers = parser.add_subparsers(dest = "action")
f32269
+        parser.add_argument("--version", action = "version",
f32269
+                version = '%(prog)s 0.1.0')
f32269
+        parser.add_argument("-q","--quiet", action = "store_true",
f32269
+                help = "silence none important test messages."
f32269
+                       " This will only work when enabling testing"
f32269
+                       " on a device.")
f32269
+        # Use the path parser to hold the --path attribute so it can
f32269
+        # be shared between subparsers. Also do the same for the state
f32269
+        # parser, as all testing methods will use --enable_all and
f32269
+        # enable_single.
f32269
+        path_parser = argparse.ArgumentParser(add_help=False)
f32269
+        path_parser.add_argument("-p","--path", metavar = "",
f32269
+                help = "Debugfs path to a vmbus device. The path "
f32269
+                "must be the absolute path to the device.")
f32269
+        state_parser = argparse.ArgumentParser(add_help=False)
f32269
+        state_group = state_parser.add_mutually_exclusive_group(required = True)
f32269
+        state_group.add_argument("-E", "--enable_all", action = "store_const",
f32269
+                                 const = "enable_all",
f32269
+                                 help = "Enable the specified test type "
f32269
+                                 "on ALL vmbus devices.")
f32269
+        state_group.add_argument("-e", "--enable_single",
f32269
+                                 action = "store_const",
f32269
+                                 const = "enable_single",
f32269
+                                 help = "Enable the specified test type on a "
f32269
+                                 "SINGLE vmbus device.")
f32269
+        parser_delay = subparsers.add_parser("delay",
f32269
+                        parents = [state_parser, path_parser],
f32269
+                        help = "Delay the ring buffer interrupt or the "
f32269
+                        "ring buffer message reads in microseconds.",
f32269
+                        prog = "vmbus_testing",
f32269
+                        usage = "%(prog)s [-h]\n"
f32269
+                        "%(prog)s -E -t [value] [value]\n"
f32269
+                        "%(prog)s -e -t [value] [value] -p",
f32269
+                        description = "Delay the ring buffer interrupt for "
f32269
+                        "vmbus devices, or delay the ring buffer message "
f32269
+                        "reads for vmbus devices (both in microseconds). This "
f32269
+                        "is only on the host to guest channel.")
f32269
+        parser_delay.add_argument("-t", "--delay_time", metavar = "", nargs = 2,
f32269
+                        type = check_range, default =[0,0], required = (True),
f32269
+                        help = "Set [buffer] & [message] delay time. "
f32269
+                        "Value constraints: -1 == value "
f32269
+                        "or 0 < value <= 1000.\n"
f32269
+                        "Use -1 to keep the previous value for that delay "
f32269
+                        "type, or a value > 0 <= 1000 to change the delay "
f32269
+                        "time.")
f32269
+        parser_dis_all = subparsers.add_parser("disable_all",
f32269
+                        aliases = ['D'], prog = "vmbus_testing",
f32269
+                        usage = "%(prog)s [disable_all | D] -h\n"
f32269
+                        "%(prog)s [disable_all | D]\n",
f32269
+                        help = "Disable ALL testing on ALL vmbus devices.",
f32269
+                        description = "Disable ALL testing on ALL vmbus "
f32269
+                        "devices.")
f32269
+        parser_dis_single = subparsers.add_parser("disable_single",
f32269
+                        aliases = ['d'],
f32269
+                        parents = [path_parser], prog = "vmbus_testing",
f32269
+                        usage = "%(prog)s [disable_single | d] -h\n"
f32269
+                        "%(prog)s [disable_single | d] -p\n",
f32269
+                        help = "Disable ALL testing on a SINGLE vmbus device.",
f32269
+                        description = "Disable ALL testing on a SINGLE vmbus "
f32269
+                        "device.")
f32269
+        parser_view_all = subparsers.add_parser("view_all", aliases = ['V'],
f32269
+                        help = "View the test state for ALL vmbus devices.",
f32269
+                        prog = "vmbus_testing",
f32269
+                        usage = "%(prog)s [view_all | V] -h\n"
f32269
+                        "%(prog)s [view_all | V]\n",
f32269
+                        description = "This shows the test state for ALL the "
f32269
+                        "vmbus devices.")
f32269
+        parser_view_single = subparsers.add_parser("view_single",
f32269
+                        aliases = ['v'],parents = [path_parser],
f32269
+                        help = "View the test values for a SINGLE vmbus "
f32269
+                        "device.",
f32269
+                        description = "This shows the test values for a SINGLE "
f32269
+                        "vmbus device.", prog = "vmbus_testing",
f32269
+                        usage = "%(prog)s [view_single | v] -h\n"
f32269
+                        "%(prog)s [view_single | v] -p")
f32269
+
f32269
+        return  parser.parse_args()
f32269
+
f32269
+# value checking for range checking input in parser
f32269
+def check_range(arg1):
f32269
+
f32269
+        try:
f32269
+                val = int(arg1)
f32269
+        except ValueError as err:
f32269
+                raise argparse.ArgumentTypeError(str(err))
f32269
+        if val < -1 or val > 1000:
f32269
+                message = ("\n\nvalue must be -1 or  0 < value <= 1000. "
f32269
+                           "Value program received: {}\n").format(val)
f32269
+                raise argparse.ArgumentTypeError(message)
f32269
+        return val
f32269
+
f32269
+if __name__ == "__main__":
f32269
+        main()
f32269
-- 
f32269
1.8.3.1
f32269