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

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