Blame SOURCES/nfs-utils-2.5.4-rpcctl.patch

449c36
diff --git a/configure.ac b/configure.ac
449c36
index 93520a80..d01ce6e4 100644
449c36
--- a/configure.ac
449c36
+++ b/configure.ac
449c36
@@ -712,6 +712,7 @@ AC_CONFIG_FILES([
449c36
 	tools/rpcgen/Makefile
449c36
 	tools/mountstats/Makefile
449c36
 	tools/nfs-iostat/Makefile
449c36
+	tools/rpcctl/Makefile
449c36
 	tools/nfsdclnts/Makefile
449c36
 	tools/nfsconf/Makefile
449c36
 	tools/nfsdclddb/Makefile
449c36
diff --git a/tools/Makefile.am b/tools/Makefile.am
449c36
index 9b4b0803..c3feabbe 100644
449c36
--- a/tools/Makefile.am
449c36
+++ b/tools/Makefile.am
449c36
@@ -12,6 +12,6 @@ if CONFIG_NFSDCLD
449c36
 OPTDIRS += nfsdclddb
449c36
 endif
449c36
 
449c36
-SUBDIRS = locktest rpcdebug nlmtest mountstats nfs-iostat nfsdclnts $(OPTDIRS)
449c36
+SUBDIRS = locktest rpcdebug nlmtest mountstats nfs-iostat rpcctl nfsdclnts $(OPTDIRS)
449c36
 
449c36
 MAINTAINERCLEANFILES = Makefile.in
449c36
diff --git a/tools/rpcctl/Makefile.am b/tools/rpcctl/Makefile.am
449c36
new file mode 100644
449c36
index 00000000..33fb431f
449c36
--- /dev/null
449c36
+++ b/tools/rpcctl/Makefile.am
449c36
@@ -0,0 +1,13 @@
449c36
+## Process this file with automake to produce Makefile.in
449c36
+PYTHON_FILES =  rpcctl.py
449c36
+
449c36
+man8_MANS = rpcctl.man
449c36
+
449c36
+EXTRA_DIST = $(man8_MANS) $(PYTHON_FILES)
449c36
+
449c36
+all-local: $(PYTHON_FILES)
449c36
+
449c36
+install-data-hook:
449c36
+	$(INSTALL) -m 755 rpcctl.py $(DESTDIR)$(sbindir)/rpcctl
449c36
+
449c36
+MAINTAINERCLEANFILES=Makefile.in
449c36
diff --git a/tools/rpcctl/rpcctl.man b/tools/rpcctl/rpcctl.man
449c36
new file mode 100644
449c36
index 00000000..b87ba0df
449c36
--- /dev/null
449c36
+++ b/tools/rpcctl/rpcctl.man
449c36
@@ -0,0 +1,67 @@
449c36
+.\"
449c36
+.\" rpcctl(8)
449c36
+.\"
449c36
+.TH rpcctl 8 "15 Feb 2022"
449c36
+.SH NAME
449c36
+rpcctl \- Displays SunRPC connection information
449c36
+.SH SYNOPSIS
449c36
+.nf
449c36
+.BR rpcctl " [ \fB\-h \fR| \fB\-\-help \fR] { \fBclient \fR| \fBswitch \fR| \fBxprt \fR}"
449c36
+.P
449c36
+.BR "rpcctl client" " \fR[ \fB\-h \fR| \fB\-\-help \fR] { \fBshow \fR}"
449c36
+.BR "rpcctl client show " "\fR[ \fB\-h \f| \fB\-\-help \fR] [ \fIXPRT \fR]"
449c36
+.P
449c36
+.BR "rpcctl switch" " \fR[ \fB\-h \fR| \fB\-\-help \fR] { \fBset \fR| \fBshow \fR}"
449c36
+.BR "rpcctl switch set" " \fR[ \fB\-h \fR| \fB\-\-help \fR] \fISWITCH \fBdstaddr \fINEWADDR"
449c36
+.BR "rpcctl switch show" " \fR[ \fB\-h \fR| \fB\-\-help \fR] [ \fISWITCH \fR]"
449c36
+.P
449c36
+.BR "rpcctl xprt" " \fR[ \fB\-h \fR| \fB\-\-help \fR] { \fBremove \fR| \fBset \fR| \fBshow \fR}"
449c36
+.BR "rpcctl xprt remove" " \fR[ \fB\-h \fR| \fB\-\-help \fR] \fIXPRT"
449c36
+.BR "rpcctl xprt set" " \fR[ \fB\-h \fR| \fB\-\-help \fR] \fIXPRT \fR{ \fBdstaddr \fINEWADDR \fR| \fBoffline \fR| \fBonline \fR}"
449c36
+.BR "rpcctl xprt show" " \fR[ \fB\-h \fR| \fB\-\-help \fR] [ \fIXPRT \fR]"
449c36
+.fi
449c36
+.SH DESCRIPTION
449c36
+.RB "The " rpcctl " command displays information collected in the SunRPC sysfs files about the system's SunRPC objects.
449c36
+.P
449c36
+.SS rpcctl client \fR- \fBCommands operating on RPC clients
449c36
+.IP "\fBshow \fR[ \fICLIENT \fR] \fB(default)"
449c36
+Show detailed information about the RPC clients on this system.
449c36
+If \fICLIENT \fRwas provided, then only show information about a single RPC client.
449c36
+.P
449c36
+.SS rpcctl switch \fR- \fBCommands operating on groups of transports
449c36
+.IP "\fBset \fISWITCH \fBdstaddr \fINEWADDR"
449c36
+Change the destination address of all transports in the \fISWITCH \fRto \fINEWADDR\fR.
449c36
+\fINEWADDR \fRcan be an IP address, DNS name, or anything else resolvable by \fBgethostbyname\fR(3).
449c36
+.IP "\fBshow \fR[ \fISWITCH \fR] \fB(default)"
449c36
+Show detailed information about groups of transports on this system.
449c36
+If \fISWITCH \fRwas provided, then only show information about a single transport group.
449c36
+.P
449c36
+.SS rpcctl xprt \fR- \fBCommands operating on individual transports
449c36
+.IP "\fBremove \fIXPRT"
449c36
+Removes the specified \fIXPRT \fRfrom the system.
449c36
+Note that "main" transports cannot be removed.
449c36
+.P
449c36
+.IP "\fBset \fIXPRT \fBdstaddr \fINEWADDR"
449c36
+Change the destination address of the specified \fIXPRT \fR to \fINEWADDR\fR.
449c36
+\fINEWADDR \fRcan be an IP address, DNS name, or anything else resolvable by \fBgethostbyname\fR(3).
449c36
+.P
449c36
+.IP "\fBset \fIXPRT \fBoffline"
449c36
+Sets the specified \fIXPRT\fR's state to offline.
449c36
+.P
449c36
+.IP "\fBset \fIXPRT \fBonline"
449c36
+Sets the specified \fIXPRT\fR's state to online.
449c36
+.IP "\fBshow \fR[ \fIXPRT \fR] \fB(default)"
449c36
+Show detailed information about this system's transports.
449c36
+If \fIXPRT \fRwas provided, then only show information about a single transport.
449c36
+.SH EXAMPLES
449c36
+.IP "\fBrpcctl switch show switch-2"
449c36
+Show details about the RPC switch named "switch-2".
449c36
+.IP "\fBrpcctl xprt remove xprt-4"
449c36
+Remove the xprt named "xprt-4" from the system.
449c36
+.IP "\fBrpcctl xprt set xprt-3 dstaddr https://linux-nfs.org
449c36
+Change the dstaddr of the xprt named "xprt-3" to point to linux-nfs.org
449c36
+.SH DIRECTORY
449c36
+.TP
449c36
+.B /sys/kernel/sunrpc/
449c36
+.SH AUTHOR
449c36
+Anna Schumaker <Anna.Schumaker@Netapp.com>
449c36
diff --git a/tools/rpcctl/rpcctl.py b/tools/rpcctl/rpcctl.py
449c36
new file mode 100755
449c36
index 00000000..b8df556b
449c36
--- /dev/null
449c36
+++ b/tools/rpcctl/rpcctl.py
449c36
@@ -0,0 +1,255 @@
449c36
+#!/usr/bin/python3
449c36
+import argparse
449c36
+import collections
449c36
+import errno
449c36
+import os
449c36
+import pathlib
449c36
+import socket
449c36
+import sys
449c36
+
449c36
+with open("/proc/mounts", 'r') as f:
449c36
+    mount = [ line.split()[1] for line in f if "sysfs" in line ]
449c36
+    if len(mount) == 0:
449c36
+        print("ERROR: sysfs is not mounted")
449c36
+        sys.exit(1)
449c36
+
449c36
+sunrpc = pathlib.Path(mount[0]) / "kernel" / "sunrpc"
449c36
+if not sunrpc.is_dir():
449c36
+    print("ERROR: sysfs does not have sunrpc directory")
449c36
+    sys.exit(1)
449c36
+
449c36
+def read_addr_file(path):
449c36
+    try:
449c36
+        with open(path, 'r') as f:
449c36
+            return f.readline().strip()
449c36
+    except:
449c36
+        return "(enoent)"
449c36
+
449c36
+def write_addr_file(path, newaddr):
449c36
+    with open(path, 'w') as f:
449c36
+        f.write(newaddr)
449c36
+    return read_addr_file(path)
449c36
+
449c36
+def read_info_file(path):
449c36
+    res = collections.defaultdict(int)
449c36
+    try:
449c36
+        with open(path) as info:
449c36
+            lines = [ l.split("=", 1) for l in info if "=" in l ]
449c36
+            res.update({ key:int(val.strip()) for (key, val) in lines })
449c36
+    finally:
449c36
+        return res
449c36
+
449c36
+
449c36
+class Xprt:
449c36
+    def __init__(self, path):
449c36
+        self.path = path
449c36
+        self.name = path.stem.rsplit("-", 1)[0]
449c36
+        self.type = path.stem.split("-")[2]
449c36
+        self.info = read_info_file(path / "xprt_info")
449c36
+        self.dstaddr = read_addr_file(path / "dstaddr")
449c36
+        self.srcaddr = read_addr_file(path / "srcaddr")
449c36
+        self.read_state()
449c36
+
449c36
+    def __lt__(self, rhs):
449c36
+        return self.name < rhs.name
449c36
+
449c36
+    def _xprt(self):
449c36
+        main = ", main" if self.info.get("main_xprt") else ""
449c36
+        return f"{self.name}: {self.type}, {self.dstaddr}, " \
449c36
+               f"port {self.info['dst_port']}, state <{self.state}>{main}"
449c36
+
449c36
+    def _src_reqs(self):
449c36
+        return f"	Source: {self.srcaddr}, port {self.info['src_port']}, " \
449c36
+               f"Requests: {self.info['num_reqs']}"
449c36
+
449c36
+    def _cong_slots(self):
449c36
+        return f"	Congestion: cur {self.info['cur_cong']}, win {self.info['cong_win']}, " \
449c36
+               f"Slots: min {self.info['min_num_slots']}, max {self.info['max_num_slots']}"
449c36
+
449c36
+    def _queues(self):
449c36
+        return f"	Queues: binding {self.info['binding_q_len']}, " \
449c36
+               f"sending {self.info['sending_q_len']}, pending {self.info['pending_q_len']}, " \
449c36
+               f"backlog {self.info['backlog_q_len']}, tasks {self.info['tasks_queuelen']}"
449c36
+
449c36
+    def __str__(self):
449c36
+        if not self.path.exists():
449c36
+            return f"{self.name}: has been removed"
449c36
+        return "\n".join([self._xprt(), self._src_reqs(),
449c36
+                          self._cong_slots(), self._queues() ])
449c36
+
449c36
+    def read_state(self):
449c36
+        if self.path.exists():
449c36
+            with open(self.path / "xprt_state") as f:
449c36
+                self.state = ','.join(f.readline().split()[1:])
449c36
+
449c36
+    def small_str(self):
449c36
+        main = " [main]" if self.info.get("main_xprt") else ""
449c36
+        return f"{self.name}: {self.type}, {self.dstaddr}{main}"
449c36
+
449c36
+    def set_dstaddr(self, newaddr):
449c36
+        self.dstaddr = write_addr_file(self.path / "dstaddr", newaddr)
449c36
+
449c36
+    def set_state(self, state):
449c36
+        with open(self.path / "xprt_state", 'w') as f:
449c36
+            f.write(state)
449c36
+        self.read_state()
449c36
+
449c36
+    def add_command(subparser):
449c36
+        parser = subparser.add_parser("xprt", help="Commands for individual xprts")
449c36
+        parser.set_defaults(func=Xprt.show, xprt=None)
449c36
+        subparser = parser.add_subparsers()
449c36
+
449c36
+        remove = subparser.add_parser("remove", help="Remove an xprt")
449c36
+        remove.add_argument("xprt", metavar="XPRT", nargs=1,
449c36
+                            help="Name of the xprt to remove")
449c36
+        remove.set_defaults(func=Xprt.set_property, property="remove")
449c36
+
449c36
+        show = subparser.add_parser("show", help="Show xprts")
449c36
+        show.add_argument("xprt", metavar="XPRT", nargs='?',
449c36
+                          help="Name of a specific xprt to show")
449c36
+        show.set_defaults(func=Xprt.show)
449c36
+
449c36
+        set = subparser.add_parser("set", help="Change an xprt property")
449c36
+        set.add_argument("xprt", metavar="XPRT", nargs=1,
449c36
+                         help="Name of a specific xprt to modify")
449c36
+        subparser = set.add_subparsers(required=True)
449c36
+        online = subparser.add_parser("online", help="Set an xprt online")
449c36
+        online.set_defaults(func=Xprt.set_property, property="online")
449c36
+        offline = subparser.add_parser("offline", help="Set an xprt offline")
449c36
+        offline.set_defaults(func=Xprt.set_property, property="offline")
449c36
+        dstaddr = subparser.add_parser("dstaddr", help="Change an xprt's dstaddr")
449c36
+        dstaddr.add_argument("newaddr", metavar="NEWADDR", nargs=1,
449c36
+                             help="The new address for the xprt")
449c36
+        dstaddr.set_defaults(func=Xprt.set_property, property="dstaddr")
449c36
+
449c36
+    def get_by_name(name):
449c36
+        glob = f"**/{name}-*" if name else "**/xprt-*"
449c36
+        res = [ Xprt(x) for x in (sunrpc / "xprt-switches").glob(glob) ]
449c36
+        if name and len(res) == 0:
449c36
+            raise FileNotFoundError(errno.ENOENT, os.strerror(errno.ENOENT),
449c36
+                                    f"{sunrpc / 'xprt-switches' / glob}")
449c36
+        return sorted(res)
449c36
+
449c36
+    def show(args):
449c36
+        for xprt in Xprt.get_by_name(args.xprt):
449c36
+            print(xprt)
449c36
+
449c36
+    def set_property(args):
449c36
+        for xprt in Xprt.get_by_name(args.xprt[0]):
449c36
+            if args.property == "dstaddr":
449c36
+                xprt.set_dstaddr(socket.gethostbyname(args.newaddr[0]))
449c36
+            elif args.property == "remove":
449c36
+                xprt.set_state("offline")
449c36
+                xprt.set_state("remove")
449c36
+            else:
449c36
+                args.set_state(args.property)
449c36
+        print(xprt)
449c36
+
449c36
+
449c36
+class XprtSwitch:
449c36
+    def __init__(self, path, sep=":"):
449c36
+        self.path = path
449c36
+        self.name = path.stem
449c36
+        self.info = read_info_file(path / "xprt_switch_info")
449c36
+        self.xprts = sorted([ Xprt(p) for p in self.path.iterdir() if p.is_dir() ])
449c36
+        self.sep = sep
449c36
+
449c36
+    def __lt__(self, rhs):
449c36
+        return self.name < rhs.name
449c36
+
449c36
+    def __str__(self):
449c36
+        switch =  f"{self.name}{self.sep} " \
449c36
+                  f"xprts {self.info['num_xprts']}, " \
449c36
+                  f"active {self.info['num_active']}, " \
449c36
+                  f"queue {self.info['queue_len']}"
449c36
+        xprts = [ f"	{x.small_str()}" for x in self.xprts ]
449c36
+        return "\n".join([ switch ] + xprts)
449c36
+
449c36
+    def add_command(subparser):
449c36
+        parser = subparser.add_parser("switch", help="Commands for xprt switches")
449c36
+        parser.set_defaults(func=XprtSwitch.show, switch=None)
449c36
+        subparser = parser.add_subparsers()
449c36
+
449c36
+        show = subparser.add_parser("show", help="Show xprt switches")
449c36
+        show.add_argument("switch", metavar="SWITCH", nargs='?',
449c36
+                          help="Name of a specific switch to show")
449c36
+        show.set_defaults(func=XprtSwitch.show)
449c36
+
449c36
+        set = subparser.add_parser("set", help="Change an xprt switch property")
449c36
+        set.add_argument("switch", metavar="SWITCH", nargs=1,
449c36
+                         help="Name of a specific xprt switch to modify")
449c36
+        subparser = set.add_subparsers(required=True)
449c36
+        dstaddr = subparser.add_parser("dstaddr", help="Change an xprt switch's dstaddr")
449c36
+        dstaddr.add_argument("newaddr", metavar="NEWADDR", nargs=1,
449c36
+                             help="The new address for the xprt switch")
449c36
+        dstaddr.set_defaults(func=XprtSwitch.set_property, property="dstaddr")
449c36
+
449c36
+    def get_by_name(name):
449c36
+        xprt_switches = sunrpc / "xprt-switches"
449c36
+        if name:
449c36
+            return [ XprtSwitch(xprt_switches / name) ]
449c36
+        return [ XprtSwitch(f) for f in sorted(xprt_switches.iterdir()) ]
449c36
+
449c36
+    def show(args):
449c36
+        for switch in XprtSwitch.get_by_name(args.switch):
449c36
+            print(switch)
449c36
+
449c36
+    def set_property(args):
449c36
+        for switch in XprtSwitch.get_by_name(args.switch[0]):
449c36
+            resolved = socket.gethostbyname(args.newaddr[0])
449c36
+            for xprt in switch.xprts:
449c36
+                xprt.set_dstaddr(resolved)
449c36
+        print(switch)
449c36
+
449c36
+
449c36
+class RpcClient:
449c36
+    def __init__(self, path):
449c36
+        self.path = path
449c36
+        self.name = path.stem
449c36
+        self.switch = XprtSwitch(path / (path / "switch").readlink(), sep=",")
449c36
+
449c36
+    def __lt__(self, rhs):
449c36
+        return self.name < rhs.name
449c36
+
449c36
+    def __str__(self):
449c36
+        return f"{self.name}: {self.switch}"
449c36
+
449c36
+    def add_command(subparser):
449c36
+        parser = subparser.add_parser("client", help="Commands for rpc clients")
449c36
+        parser.set_defaults(func=RpcClient.show, client=None)
449c36
+        subparser = parser.add_subparsers()
449c36
+
449c36
+        show = subparser.add_parser("show", help="Show rpc clients")
449c36
+        show.add_argument("client", metavar="CLIENT", nargs='?',
449c36
+                          help="Name of a specific rpc client to show")
449c36
+        parser.set_defaults(func=RpcClient.show)
449c36
+
449c36
+    def get_by_name(name):
449c36
+        rpc_clients = sunrpc / "rpc-clients"
449c36
+        if name:
449c36
+            return [ RpcClient(rpc_clients / name) ]
449c36
+        return [ RpcClient(f) for f in sorted(rpc_clients.iterdir()) ]
449c36
+
449c36
+    def show(args):
449c36
+        for client in RpcClient.get_by_name(args.client):
449c36
+            print(client)
449c36
+
449c36
+
449c36
+parser = argparse.ArgumentParser()
449c36
+
449c36
+def show_small_help(args):
449c36
+    parser.print_usage()
449c36
+    print("sunrpc dir:", sunrpc)
449c36
+parser.set_defaults(func=show_small_help)
449c36
+
449c36
+subparser = parser.add_subparsers(title="commands")
449c36
+RpcClient.add_command(subparser)
449c36
+XprtSwitch.add_command(subparser)
449c36
+Xprt.add_command(subparser)
449c36
+
449c36
+args = parser.parse_args()
449c36
+try:
449c36
+    args.func(args)
449c36
+except Exception as e:
449c36
+    print(str(e))
449c36
+    sys.exit(1)