Blame SOURCES/0001-sync-with-1.1.6.patch

50703a
diff --git a/.github/workflows/pylint.yml b/.github/workflows/pylint.yml
50703a
index 93604e8..4e1b6c5 100644
50703a
--- a/.github/workflows/pylint.yml
50703a
+++ b/.github/workflows/pylint.yml
50703a
@@ -1,9 +1,19 @@
50703a
-name: Pylint
50703a
+name: Linters
50703a
 
50703a
 on: [push]
50703a
 
50703a
 jobs:
50703a
-  build:
50703a
+
50703a
+  docker-lint:
50703a
+    runs-on: ubuntu-latest
50703a
+    steps:
50703a
+      - uses: actions/checkout@v3
50703a
+      - uses: hadolint/hadolint-action@v2.1.0
50703a
+        with:
50703a
+          recursive: true
50703a
+          ignore: DL3041
50703a
+
50703a
+  python-lint:
50703a
     runs-on: ubuntu-latest
50703a
 
50703a
     strategy:
50703a
diff --git a/Dockerfile b/Dockerfile
50703a
index ad6742e..0ab5138 100644
50703a
--- a/Dockerfile
50703a
+++ b/Dockerfile
50703a
@@ -2,12 +2,12 @@ FROM fedora:36
50703a
 
50703a
 WORKDIR /root
50703a
 
50703a
-# for nvme-stas
50703a
-RUN dnf install -y python3-dasbus python3-pyudev python3-systemd python3-gobject meson
50703a
-# for libnvme
50703a
-RUN dnf install -y git gcc g++ cmake openssl-devel libuuid-devel json-c-devel swig python-devel meson
50703a
+# first line for nvme-stas
50703a
+# second line for libnvme
50703a
+RUN dnf install -y python3-dasbus python3-pyudev python3-systemd python3-gobject meson \
50703a
+                   git gcc g++ cmake openssl-devel libuuid-devel json-c-devel swig python-devel meson && dnf clean all
50703a
 
50703a
 COPY . .
50703a
-RUN meson .build && ninja -C .build && cd .build && meson install
50703a
+RUN meson .build && ninja -C .build && meson install -C .build
50703a
 
50703a
 ENTRYPOINT ["python3"]
50703a
diff --git a/NEWS.md b/NEWS.md
50703a
index d1515cd..f56a7c9 100644
50703a
--- a/NEWS.md
50703a
+++ b/NEWS.md
50703a
@@ -5,6 +5,7 @@
50703a
 - Fix issues with I/O controller connection audits
50703a
   - Eliminate pcie devices from list of I/O controller connections to audit
50703a
   - Add soaking timer to workaround race condition between kernel and user-space applications on "add" uevents. When the kernel adds a new nvme device (e.g. `/dev/nvme7`) and sends a "add" uevent to notify user-space applications, the attributes associated with that device (e.g. `/sys/class/nvme/nvme7/cntrltype`) may not be fully initialized which can lead `stacd` to dismiss a device that should get audited. 
50703a
+- Make `sticky-connections=enabled` the default (see `stacd.conf`) 
50703a
 
50703a
 ## Changes with release 1.1.5
50703a
 
50703a
@@ -32,7 +33,7 @@ stacd: Bug fix. Check that self._cfg_soak_tmr is not None before dereferencing i
50703a
 
50703a
 ## Changes with release 1.1.1
50703a
 
50703a
-Make `sticky-connections-disabled` by default
50703a
+Make `sticky-connections=disabled` the default (see `stacd.conf`) 
50703a
 
50703a
 ## Changes with release 1.1
50703a
 
50703a
diff --git a/coverage.sh.in b/coverage.sh.in
50703a
index 96b8c53..5ba2ebe 100755
50703a
--- a/coverage.sh.in
50703a
+++ b/coverage.sh.in
50703a
@@ -38,14 +38,24 @@ PRIMARY_GRP=$( id -ng )
50703a
 PRIMARY_USR=$( id -nu )
50703a
 PYTHON_PATH=.:./subprojects/libnvme
50703a
 
50703a
+log() {
50703a
+    msg="$1"
50703a
+    printf "%b[1;36m%s%b[0m\n" "\0033" "${msg}" "\0033"
50703a
+    sudo logger -i "@@@@@  COVERAGE -" -p 4 "${msg}"
50703a
+}
50703a
+
50703a
 sd_stop() {
50703a
-    unit="$1"-cov.service
50703a
+    app="$1"
50703a
+    unit="${app}"-cov.service
50703a
+    log "Stop ${app}"
50703a
     sudo systemctl stop "${unit}" >/dev/null 2>&1
50703a
     sudo systemctl reset-failed "${unit}" >/dev/null 2>&1
50703a
 }
50703a
 
50703a
 sd_restart() {
50703a
-    unit="$1"-cov.service
50703a
+    app="$1"
50703a
+    unit="${app}"-cov.service
50703a
+    log "Restart ${app}"
50703a
     sudo systemctl restart "${unit}" >/dev/null 2>&1
50703a
 }
50703a
 
50703a
@@ -61,7 +71,7 @@ sd_start() {
50703a
         cmd="${app} --syslog -f ${conf}"
50703a
     fi
50703a
 
50703a
-    printf "\n%b[1;36m%s%b[0m\n" "\0033" "Start ${app}" "\0033"
50703a
+    log "Start ${app}"
50703a
 
50703a
     RUNTIME_DIRECTORY=/tmp/${app}
50703a
     rm -rf ${RUNTIME_DIRECTORY}
50703a
@@ -75,7 +85,7 @@ reload_cfg() {
50703a
     app="$1"
50703a
     unit="${app}"-cov.service
50703a
     pid=$( systemctl show --property MainPID --value "${unit}" )
50703a
-    printf "%b[1;36m%s%b[0m\n" "\0033" "Reload config ${app}" "\0033"
50703a
+    log "Reload config ${app}"
50703a
     sudo kill -HUP "${pid}"
50703a
 }
50703a
 
50703a
@@ -83,15 +93,24 @@ if [ ! -d coverage ]; then
50703a
     mkdir coverage
50703a
 fi
50703a
 
50703a
+
50703a
+log "START-START-START-START-START-START-START-START-START-START-START-START"
50703a
+
50703a
+
50703a
+
50703a
 ################################################################################
50703a
 # Load nvme kernel module
50703a
+log "modprobe nvme-tcp"
50703a
 sudo /usr/sbin/modprobe nvme-tcp
50703a
 
50703a
+log "nvme disconnect-all"
50703a
 sudo nvme disconnect-all
50703a
 
50703a
 ################################################################################
50703a
 # Create a dummy config file for @STAFD_PROCNAME@
50703a
-stafd_conf_fname=$(mktemp /tmp/@STAFD_PROCNAME@.conf.XXXXXX)
50703a
+file=/tmp/@STAFD_PROCNAME@.conf.XXXXXX
50703a
+log "Create dummy config file $file"
50703a
+stafd_conf_fname=$(mktemp $file)
50703a
 cat > "${stafd_conf_fname}" <<'EOF'
50703a
 [Global]
50703a
 tron=true
50703a
@@ -102,7 +121,9 @@ EOF
50703a
 
50703a
 ################################################################################
50703a
 # Create a dummy config file for @STACD_PROCNAME@
50703a
-stacd_conf_fname=$(mktemp /tmp/@STACD_PROCNAME@.conf.XXXXXX)
50703a
+file=/tmp/@STACD_PROCNAME@.conf.XXXXXX
50703a
+log "Create dummy config file $file"
50703a
+stacd_conf_fname=$(mktemp $file)
50703a
 cat > "${stacd_conf_fname}" <<'EOF'
50703a
 [Global]
50703a
 tron=true
50703a
@@ -111,6 +132,7 @@ udev-rule=disabled
50703a
 sticky-connections=enabled
50703a
 EOF
50703a
 
50703a
+log "Stop & Mask Avahi daemon"
50703a
 sudo systemctl stop avahi-daemon.service
50703a
 sudo systemctl stop avahi-daemon.socket
50703a
 sudo systemctl mask avahi-daemon.service
50703a
@@ -118,11 +140,11 @@ sudo systemctl mask avahi-daemon.socket
50703a
 sleep 1
50703a
 
50703a
 
50703a
-printf "%b[1;36m%s%b[0m\n" "\0033" "Invoking @STAFD_CTLNAME@ status while @STAFD_PROCNAME@ is not running" "\0033"
50703a
+log "Invoking @STAFD_CTLNAME@ status while @STAFD_PROCNAME@ is not running"
50703a
 coverage run --rcfile=.coveragerc @STAFD_CTLNAME@ ls >/dev/null 2>&1
50703a
 coverage run --rcfile=.coveragerc @STAFD_CTLNAME@ invalid-command >/dev/null 2>&1
50703a
 
50703a
-printf "%b[1;36m%s%b[0m\n" "\0033" "Invoking @STACD_CTLNAME@ status while @STACD_PROCNAME@ is not running" "\0033"
50703a
+log "Invoking @STACD_CTLNAME@ status while @STACD_PROCNAME@ is not running"
50703a
 coverage run --rcfile=.coveragerc @STACD_CTLNAME@ ls >/dev/null 2>&1
50703a
 coverage run --rcfile=.coveragerc @STACD_CTLNAME@ invalid-command >/dev/null 2>&1
50703a
 
50703a
@@ -132,30 +154,33 @@ sd_start "@STAFD_PROCNAME@" "@STAFD_DBUS_NAME@" "${stafd_conf_fname}"
50703a
 sd_start "@STACD_PROCNAME@" "@STACD_DBUS_NAME@" "${stacd_conf_fname}"
50703a
 sleep 3
50703a
 
50703a
-printf "%b[1;36m%s%b[0m\n" "\0033" "Invoking @STAFD_CTLNAME@ status" "\0033"
50703a
+log "Invoking @STAFD_CTLNAME@ status"
50703a
 coverage run --rcfile=.coveragerc @STAFD_CTLNAME@ status  >/dev/null 2>&1
50703a
 
50703a
 reload_cfg "@STAFD_PROCNAME@"
50703a
 sleep 1
50703a
 
50703a
+log "Restart Avahi daemon"
50703a
 sudo systemctl unmask avahi-daemon.socket
50703a
 sudo systemctl unmask avahi-daemon.service
50703a
 sudo systemctl start avahi-daemon.socket
50703a
 sudo systemctl start avahi-daemon.service
50703a
 sleep 2
50703a
 
50703a
+log "Change stafd config: tron=true, persistent-connections=false, zeroconf=enable"
50703a
 cat > "${stafd_conf_fname}" <<'EOF'
50703a
 [Global]
50703a
 tron=true
50703a
 persistent-connections=false
50703a
 
50703a
 [Service Discovery]
50703a
-zeroconf=disabled
50703a
+zeroconf=enabled
50703a
 EOF
50703a
 reload_cfg "@STAFD_PROCNAME@"
50703a
 
50703a
 sleep 1
50703a
 
50703a
+log "Change stafd config: ip-family=ipv4, kato=10, adding multiple controllers"
50703a
 cat > "${stafd_conf_fname}" <<'EOF'
50703a
 [Global]
50703a
 tron=true
50703a
@@ -172,11 +197,15 @@ controller=transport=tcp;traddr=abracadabra
50703a
 controller=
50703a
 controller=trsvcid
50703a
 controller=transport=rdma;traddr=!@#$
50703a
+controller=transport=fc;traddr=21:00:00:00:00:00:00:00;host-traddr=20:00:00:00:00:00:00:00
50703a
+controller=transport=XM;traddr=2.2.2.2
50703a
 blacklist=transport=tcp;traddr=1.1.1.1
50703a
 blacklist=transport=tcp;traddr=1000.1000.1000.1000
50703a
 EOF
50703a
 reload_cfg "@STAFD_PROCNAME@"
50703a
 
50703a
+
50703a
+log "Change stacd config: tron=true, udev-rule=disabled, sticky-connections=disabled"
50703a
 cat > "${stacd_conf_fname}" <<'EOF'
50703a
 [Global]
50703a
 tron=true
50703a
@@ -186,12 +215,12 @@ EOF
50703a
 reload_cfg "@STACD_PROCNAME@"
50703a
 sleep 3
50703a
 
50703a
-printf "%b[1;36m%s%b[0m\n" "\0033" "Invoking @STAFD_CTLNAME@ status" "\0033"
50703a
+log "Invoking @STAFD_CTLNAME@ status"
50703a
 coverage run --rcfile=.coveragerc @STAFD_CTLNAME@ status  >/dev/null 2>&1
50703a
 
50703a
 ################################################################################
50703a
 # Fake mDNS packets from a CDC
50703a
-printf "\n%b[1;36m%s%b[0m\n" "\0033" "Start Avahi publisher" "\0033"
50703a
+log "Start Avahi publisher"
50703a
 AVAHI_PUBLISHER=mdns_publisher.service
50703a
 sudo systemctl stop ${AVAHI_PUBLISHER} >/dev/null 2>&1
50703a
 sudo systemctl reset-failed ${AVAHI_PUBLISHER} >/dev/null 2>&1
50703a
@@ -200,7 +229,7 @@ sleep 1
50703a
 
50703a
 ################################################################################
50703a
 # Start nvme target simulator
50703a
-printf "\n%b[1;36m%s%b[0m\n" "\0033" "Start nvmet" "\0033"
50703a
+log "Start nvmet"
50703a
 sudo ../utils/nvmet/nvmet.py clean
50703a
 sudo ../utils/nvmet/nvmet.py create -f ../utils/nvmet/nvmet.conf
50703a
 sleep 2
50703a
@@ -210,76 +239,76 @@ reload_cfg "@STACD_PROCNAME@"
50703a
 sleep 3
50703a
 
50703a
 ################################################################################
50703a
-printf "\n%b[1;36m%s%b[0m\n" "\0033" "Invoking @STAFD_PROCNAME@ --version" "\0033"
50703a
+log "Invoking @STAFD_PROCNAME@ --version"
50703a
 coverage run --rcfile=.coveragerc @STAFD_PROCNAME@ --version
50703a
-printf "%b[1;36m%s%b[0m\n" "\0033" "Invoking @STAFD_PROCNAME@ --idl" "\0033"
50703a
+log "Invoking @STAFD_PROCNAME@ --idl"
50703a
 coverage run --rcfile=.coveragerc @STAFD_PROCNAME@ --idl /tmp/@STAFD_PROCNAME@.idl
50703a
 
50703a
-printf "\n%b[1;36m%s%b[0m\n" "\0033" "Invoking @STACD_PROCNAME@ --version" "\0033"
50703a
+log "Invoking @STACD_PROCNAME@ --version"
50703a
 coverage run --rcfile=.coveragerc @STACD_PROCNAME@ --version
50703a
-printf "%b[1;36m%s%b[0m\n" "\0033" "Invoking @STACD_PROCNAME@ --idl" "\0033"
50703a
+log "Invoking @STACD_PROCNAME@ --idl"
50703a
 coverage run --rcfile=.coveragerc @STACD_PROCNAME@ --idl /tmp/@STACD_PROCNAME@.idl
50703a
 
50703a
 ################################################################################
50703a
 # Stimulate D-Bus activity
50703a
-printf "\n%b[1;36m%s%b[0m\n" "\0033" "Invoking @STAFD_CTLNAME@ --version" "\0033"
50703a
+log "Invoking @STAFD_CTLNAME@ --version"
50703a
 sudo coverage run --rcfile=.coveragerc @STAFD_CTLNAME@ --version
50703a
-printf "%b[1;36m%s%b[0m\n" "\0033" "Invoking @STAFD_CTLNAME@ with a bad command" "\0033"
50703a
+log "Invoking @STAFD_CTLNAME@ with a bad command"
50703a
 sudo coverage run --rcfile=.coveragerc @STAFD_CTLNAME@ blah
50703a
-printf "%b[1;36m%s%b[0m\n" "\0033" "Invoking @STAFD_CTLNAME@ troff" "\0033"
50703a
+log "Invoking @STAFD_CTLNAME@ troff"
50703a
 sudo coverage run --rcfile=.coveragerc @STAFD_CTLNAME@ troff
50703a
-printf "%b[1;36m%s%b[0m\n" "\0033" "Invoking @STAFD_CTLNAME@ status" "\0033"
50703a
+log "Invoking @STAFD_CTLNAME@ status"
50703a
 coverage run --rcfile=.coveragerc @STAFD_CTLNAME@ status  >/dev/null 2>&1
50703a
-printf "%b[1;36m%s%b[0m\n" "\0033" "Invoking @STAFD_CTLNAME@ tron" "\0033"
50703a
+log "Invoking @STAFD_CTLNAME@ tron"
50703a
 sudo coverage run --rcfile=.coveragerc @STAFD_CTLNAME@ tron
50703a
-printf "%b[1;36m%s%b[0m\n" "\0033" "Invoking @STAFD_CTLNAME@ ls" "\0033"
50703a
+log "Invoking @STAFD_CTLNAME@ ls"
50703a
 coverage run --rcfile=.coveragerc @STAFD_CTLNAME@ ls -d   >/dev/null 2>&1
50703a
-printf "%b[1;36m%s%b[0m\n" "\0033" "Invoking @STAFD_CTLNAME@ adlp" "\0033"
50703a
+log "Invoking @STAFD_CTLNAME@ adlp"
50703a
 coverage run --rcfile=.coveragerc @STAFD_CTLNAME@ adlp -d >/dev/null 2>&1
50703a
-printf "%b[1;36m%s%b[0m\n" "\0033" "Invoking @STAFD_CTLNAME@ dlp" "\0033"
50703a
+log "Invoking @STAFD_CTLNAME@ dlp"
50703a
 coverage run --rcfile=.coveragerc @STAFD_CTLNAME@ dlp -t tcp -a ::1 -s 8009 >/dev/null 2>&1
50703a
 
50703a
-printf "\n%b[1;36m%s%b[0m\n" "\0033" "Invoking @STACD_CTLNAME@ --version" "\0033"
50703a
+log "Invoking @STACD_CTLNAME@ --version"
50703a
 sudo coverage run --rcfile=.coveragerc @STACD_CTLNAME@ --version
50703a
-printf "%b[1;36m%s%b[0m\n" "\0033" "Invoking @STACD_CTLNAME@ with a bad command" "\0033"
50703a
+log "Invoking @STACD_CTLNAME@ with a bad command"
50703a
 sudo coverage run --rcfile=.coveragerc @STACD_CTLNAME@ blah
50703a
-printf "%b[1;36m%s%b[0m\n" "\0033" "Invoking @STACD_CTLNAME@ troff" "\0033"
50703a
+log "Invoking @STACD_CTLNAME@ troff"
50703a
 sudo coverage run --rcfile=.coveragerc @STACD_CTLNAME@ troff
50703a
-printf "%b[1;36m%s%b[0m\n" "\0033" "Invoking @STACD_CTLNAME@ status" "\0033"
50703a
+log "Invoking @STACD_CTLNAME@ status"
50703a
 coverage run --rcfile=.coveragerc @STACD_CTLNAME@ status >/dev/null 2>&1
50703a
-printf "%b[1;36m%s%b[0m\n" "\0033" "Invoking @STACD_CTLNAME@ tron" "\0033"
50703a
+log "Invoking @STACD_CTLNAME@ tron"
50703a
 sudo coverage run --rcfile=.coveragerc @STACD_CTLNAME@ tron
50703a
-printf "%b[1;36m%s%b[0m\n" "\0033" "Invoking @STACD_CTLNAME@ ls" "\0033"
50703a
+log "Invoking @STACD_CTLNAME@ ls"
50703a
 coverage run --rcfile=.coveragerc @STACD_CTLNAME@ ls -d >/dev/null 2>&1
50703a
 
50703a
 ################################################################################
50703a
 # Stimulate AENs activity by removing/restoring namespaces
50703a
-printf "\n%b[1;36m%s%b[0m\n" "\0033" "Remove namespace: klingons" "\0033"
50703a
+log "Remove namespace: klingons"
50703a
 sudo ../utils/nvmet/nvmet.py unlink -p 1 -s klingons
50703a
 sleep 2
50703a
-printf "\n%b[1;36m%s%b[0m\n" "\0033" "Invoking @STACD_CTLNAME@ ls" "\0033"
50703a
+log "Invoking @STACD_CTLNAME@ ls"
50703a
 coverage run --rcfile=.coveragerc @STACD_CTLNAME@ ls -d >/dev/null 2>&1
50703a
 
50703a
-printf "\n%b[1;36m%s%b[0m\n" "\0033" "Restore namespace: klingons" "\0033"
50703a
+log "Restore namespace: klingons"
50703a
 sudo ../utils/nvmet/nvmet.py link -p 1 -s klingons
50703a
 sleep 2
50703a
-printf "\n%b[1;36m%s%b[0m\n" "\0033" "Invoking @STACD_CTLNAME@ ls" "\0033"
50703a
+log "Invoking @STACD_CTLNAME@ ls"
50703a
 coverage run --rcfile=.coveragerc @STACD_CTLNAME@ ls -d >/dev/null 2>&1
50703a
 
50703a
 ################################################################################
50703a
 # Stop Avahi Publisher
50703a
-printf "\n%b[1;36m%s%b[0m\n" "\0033" "Stop Avahi publisher" "\0033"
50703a
+log "Stop Avahi publisher"
50703a
 sudo systemctl stop ${AVAHI_PUBLISHER}
50703a
 sleep 1
50703a
 
50703a
 ################################################################################
50703a
-printf "\n%b[1;36m%s%b[0m\n" "\0033" "Restart Avahi publisher" "\0033"
50703a
+log "Restart Avahi publisher"
50703a
 sudo systemd-run --unit=${AVAHI_PUBLISHER} --working-directory=. avahi-publish -s SFSS _nvme-disc._tcp 8009 "p=tcp"
50703a
 sleep 2
50703a
 
50703a
 ################################################################################
50703a
 # Make config changes for @STAFD_PROCNAME@
50703a
-printf "\n%b[1;36m%s%b[0m\n" "\0033" "Empty configuration and disable zeroconf for @STAFD_PROCNAME@" "\0033"
50703a
+log "Empty configuration and disable zeroconf for @STAFD_PROCNAME@"
50703a
 cat > "${stafd_conf_fname}" <<'EOF'
50703a
 [Global]
50703a
 tron=true
50703a
@@ -293,7 +322,7 @@ sleep 1
50703a
 
50703a
 ################################################################################
50703a
 # Make more config changes for @STAFD_PROCNAME@
50703a
-printf "\n%b[1;36m%s%b[0m\n" "\0033" "Add single controller (::1) and re-enable zeroconf for @STAFD_PROCNAME@" "\0033"
50703a
+log "Add single controller (::1) and re-enable zeroconf for @STAFD_PROCNAME@"
50703a
 cat > "${stafd_conf_fname}" <<'EOF'
50703a
 [Global]
50703a
 tron=true
50703a
@@ -307,24 +336,23 @@ sleep 2
50703a
 
50703a
 ################################################################################
50703a
 # Stop Avahi Publisher
50703a
-printf "\n%b[1;36m%s%b[0m\n" "\0033" "Stop Avahi publisher" "\0033"
50703a
+log "Stop Avahi publisher"
50703a
 sudo systemctl stop ${AVAHI_PUBLISHER}
50703a
 sleep 2
50703a
 
50703a
 ################################################################################
50703a
 # Remove one of the NVMe device's
50703a
-printf "\n%b[1;36m%s%b[0m\n" "\0033" "Remove (disconnect) nvme1" "\0033"
50703a
+log "Remove (disconnect) nvme1"
50703a
 sudo nvme disconnect -d nvme1
50703a
 sleep 2
50703a
 
50703a
 
50703a
 ################################################################################
50703a
-printf "%b[1;36m%s%b[0m\n" "\0033" "Restart @STAFD_PROCNAME@ and @STACD_PROCNAME@" "\0033"
50703a
 sd_restart "@STAFD_PROCNAME@"
50703a
 sd_restart "@STACD_PROCNAME@"
50703a
 sleep 1
50703a
 
50703a
-printf "%b[1;36m%s%b[0m\n" "\0033" "Create invalid conditions for saving/loading @STAFD_PROCNAME@'s last known config" "\0033"
50703a
+log "Create invalid conditions for saving/loading @STAFD_PROCNAME@'s last known config"
50703a
 rm -rf "/tmp/@STAFD_PROCNAME@"
50703a
 sd_stop "@STAFD_PROCNAME@"
50703a
 sd_restart "@STACD_PROCNAME@"
50703a
@@ -334,7 +362,7 @@ sleep 2
50703a
 
50703a
 ################################################################################
50703a
 # Stop everything and collect coverage stats
50703a
-printf "\n%b[1;36m%s%b[0m\n" "\0033" "Stop @STAFD_PROCNAME@ and @STACD_PROCNAME@" "\0033"
50703a
+log "Stop @STAFD_PROCNAME@ and @STACD_PROCNAME@"
50703a
 sd_stop "@STAFD_PROCNAME@"
50703a
 sd_stop "@STACD_PROCNAME@"
50703a
 sleep 1
50703a
@@ -345,33 +373,49 @@ sudo chown -R "${PRIMARY_USR}":"${PRIMARY_GRP}" coverage  >/dev/null 2>&1
50703a
 sudo chown -R "${PRIMARY_USR}":"${PRIMARY_GRP}" staslib/__pycache__  >/dev/null 2>&1
50703a
 sudo chown -R "${PRIMARY_USR}":"${PRIMARY_GRP}" subprojects/libnvme/libnvme/__pycache__  >/dev/null 2>&1
50703a
 
50703a
+log "nvme disconnect-all"
50703a
 sudo nvme disconnect-all
50703a
 
50703a
+log "Remove ${stafd_conf_fname} and ${stacd_conf_fname}"
50703a
 rm "${stafd_conf_fname}"
50703a
 rm "${stacd_conf_fname}"
50703a
 
50703a
+log "Run unit test: test-udev"
50703a
 PYTHONPATH=${PYTHON_PATH} coverage run --rcfile=.coveragerc ../test/test-udev.py
50703a
+log "Run unit test: test-avahi"
50703a
 PYTHONPATH=${PYTHON_PATH} coverage run --rcfile=.coveragerc ../test/test-avahi.py
50703a
+log "Run unit test: test-gtimer"
50703a
 PYTHONPATH=${PYTHON_PATH} coverage run --rcfile=.coveragerc ../test/test-gtimer.py
50703a
+log "Run unit test: test-version"
50703a
 PYTHONPATH=${PYTHON_PATH} coverage run --rcfile=.coveragerc ../test/test-version.py
50703a
+log "Run unit test: test-transport_id"
50703a
 PYTHONPATH=${PYTHON_PATH} coverage run --rcfile=.coveragerc ../test/test-transport_id.py
50703a
+log "Run unit test: test-config"
50703a
 PYTHONPATH=${PYTHON_PATH} coverage run --rcfile=.coveragerc ../test/test-config.py
50703a
+log "Run unit test: test-controller"
50703a
 PYTHONPATH=${PYTHON_PATH} coverage run --rcfile=.coveragerc ../test/test-controller.py
50703a
+log "Run unit test: test-service"
50703a
 PYTHONPATH=${PYTHON_PATH} coverage run --rcfile=.coveragerc ../test/test-service.py
50703a
+log "Run unit test: test-log"
50703a
 PYTHONPATH=${PYTHON_PATH} coverage run --rcfile=.coveragerc ../test/test-log.py
50703a
+log "Run unit test: test-nvme_options"
50703a
 sudo PYTHONPATH=${PYTHON_PATH} coverage run --rcfile=.coveragerc ../test/test-nvme_options.py
50703a
 
50703a
 ################################################################################
50703a
 # Stop nvme target simulator
50703a
-printf "\n%b[1;36m%s%b[0m\n" "\0033" "Stop nvmet" "\0033"
50703a
+log "Stop nvmet"
50703a
 sudo ../utils/nvmet/nvmet.py clean
50703a
 
50703a
-printf "\n%b[1;36m%s%b[0m\n" "\0033" "Collect all coverage data" "\0033"
50703a
+log "Collect all coverage data"
50703a
 coverage combine --rcfile=.coveragerc
50703a
 
50703a
-printf "\n%b[1;36m%s%b[0m\n" "\0033" "Generating coverage report" "\0033"
50703a
+log "Generating coverage report"
50703a
 coverage report -i --rcfile=.coveragerc
50703a
 
50703a
-printf "\n%b[1;36m%s%b[0m\n" "\0033" "Generating coverage report (HTML)" "\0033"
50703a
+log "Generating coverage report (HTML)"
50703a
 coverage html -i --rcfile=.coveragerc
50703a
 
50703a
+
50703a
+log "All done!!!"
50703a
+
50703a
+log "FINISHED-FINISHED-FINISHED-FINISHED-FINISHED-FINISHED-FINISHED-FINISHED"
50703a
diff --git a/doc/man/stacd.conf.xml b/doc/man/stacd.conf.xml
50703a
index 60622f6..65ee71a 100644
50703a
--- a/doc/man/stacd.conf.xml
50703a
+++ b/doc/man/stacd.conf.xml
50703a
@@ -378,7 +378,7 @@
50703a
                             entries in <filename>stacd.conf</filename> have been removed.
50703a
                         </para>
50703a
 
50703a
-                        <formalpara><title>With sticky-connections=disabled (default)</title>
50703a
+                        <formalpara><title>With sticky-connections=disabled</title>
50703a
                             <para>
50703a
                                 stacd immediately disconnects from
50703a
                                 a previously connected IOC if the response to a
50703a
@@ -411,7 +411,7 @@
50703a
                             </formalpara>
50703a
                         </formalpara>
50703a
 
50703a
-                        <formalpara><title>With sticky-connections=enabled</title>
50703a
+                        <formalpara><title>With sticky-connections=enabled (default)</title>
50703a
                             <para>
50703a
                                 stacd does not disconnect from IOCs
50703a
                                 when a DPLE is removed or a <literal>controller=</literal>
50703a
diff --git a/etc/stas/stacd.conf b/etc/stas/stacd.conf
50703a
index 02e7b3e..0434671 100644
50703a
--- a/etc/stas/stacd.conf
50703a
+++ b/etc/stas/stacd.conf
50703a
@@ -202,8 +202,8 @@
50703a
 #
50703a
 #                     Type:    String
50703a
 #                     Range:   [disabled, enabled]
50703a
-#                     Default: disabled
50703a
-#sticky-connections=disabled
50703a
+#                     Default: enabled
50703a
+#sticky-connections=enabled
50703a
 
50703a
 [Controllers]
50703a
 # controller: I/O Controllers (IOC) are specified with this keyword.
50703a
diff --git a/stacd.py b/stacd.py
50703a
index 708e372..28cefac 100755
50703a
--- a/stacd.py
50703a
+++ b/stacd.py
50703a
@@ -10,14 +10,12 @@
50703a
 ''' STorage Appliance Connector Daemon
50703a
 '''
50703a
 import sys
50703a
-import logging
50703a
 from argparse import ArgumentParser
50703a
 from staslib import defs
50703a
 
50703a
-# pylint: disable=consider-using-f-string
50703a
-DBUS_IDL = '''
50703a
+DBUS_IDL = f'''
50703a
 <node>
50703a
-    <interface name="%s.debug">
50703a
+    <interface name="{defs.STACD_DBUS_NAME}.debug">
50703a
         <property name="tron" type="b" access="readwrite"/>
50703a
         <property name="log_level" type="s" access="read"/>
50703a
         <method name="process_info">
50703a
@@ -34,19 +32,16 @@ DBUS_IDL = '''
50703a
         </method>
50703a
     </interface>
50703a
 
50703a
-    <interface name="%s">
50703a
+    <interface name="{defs.STACD_DBUS_NAME}">
50703a
         <method name="list_controllers">
50703a
             <arg direction="in" type="b" name="detailed"/>
50703a
-            <arg direction="out" type="aa{ss}" name="controller_list"/>
50703a
+            <arg direction="out" type="aa{{ss}}" name="controller_list"/>
50703a
         </method>
50703a
     </interface>
50703a
 </node>
50703a
-''' % (
50703a
-    defs.STACD_DBUS_NAME,
50703a
-    defs.STACD_DBUS_NAME,
50703a
-)
50703a
-
50703a
+'''
50703a
 
50703a
+# ******************************************************************************
50703a
 def parse_args(conf_file: str):  # pylint: disable=missing-function-docstring
50703a
     parser = ArgumentParser(
50703a
         description=f'{defs.STAC_DESCRIPTION} ({defs.STAC_ACRONYM}). Must be root to run this program.'
50703a
@@ -77,6 +72,12 @@ ARGS = parse_args(defs.STACD_CONFIG_FILE)
50703a
 
50703a
 if ARGS.version:
50703a
     print(f'{defs.PROJECT_NAME} {defs.VERSION}')
50703a
+    try:
50703a
+        import libnvme
50703a
+
50703a
+        print(f'libnvme {libnvme.__version__}')
50703a
+    except (AttributeError, ModuleNotFoundError):
50703a
+        pass
50703a
     sys.exit(0)
50703a
 
50703a
 if ARGS.idl:
50703a
@@ -85,78 +86,14 @@ if ARGS.idl:
50703a
     sys.exit(0)
50703a
 
50703a
 
50703a
-# There is a reason for having this import here and not at the top of the file.
50703a
-# We want to allow running stafd with the --version and --idl options and exit
50703a
-# without having to import stas.
50703a
-from staslib import stas  # pylint: disable=wrong-import-position
50703a
-
50703a
-# Before going any further, make sure the script is allowed to run.
50703a
-stas.check_if_allowed_to_continue()
50703a
-
50703a
-
50703a
-################################################################################
50703a
-# Preliminary checks have passed. Let her rip!
50703a
-# pylint: disable=wrong-import-position
50703a
-# pylint: disable=wrong-import-order
50703a
-import json
50703a
-import pathlib
50703a
-import systemd.daemon
50703a
-import dasbus.error
50703a
-import dasbus.client.observer
50703a
-import dasbus.client.proxy
50703a
-from gi.repository import GLib
50703a
-from staslib import conf, log, gutil, trid, udev, ctrl, service  # pylint: disable=ungrouped-imports
50703a
-
50703a
-log.init(ARGS.syslog)
50703a
-
50703a
-UDEV_RULE_SUPPRESS = pathlib.Path('/run/udev/rules.d', '70-nvmf-autoconnect.rules')
50703a
-
50703a
-
50703a
-def udev_rule_ctrl(enable):
50703a
-    '''@brief We add an empty udev rule to /run/udev/rules.d to suppress
50703a
-    nvme-cli's udev rule that is used to tell udevd to automatically
50703a
-    connect to I/O controller. This is to avoid race conditions between
50703a
-    stacd and udevd. This is configurable. See "udev-rule" in stacd.conf
50703a
-    for details.
50703a
-    '''
50703a
-    if enable:
50703a
-        try:
50703a
-            UDEV_RULE_SUPPRESS.unlink()
50703a
-        except FileNotFoundError:
50703a
-            pass
50703a
-    else:
50703a
-        if not UDEV_RULE_SUPPRESS.exists():
50703a
-            pathlib.Path('/run/udev/rules.d').mkdir(parents=True, exist_ok=True)
50703a
-            UDEV_RULE_SUPPRESS.symlink_to('/dev/null')
50703a
-
50703a
-
50703a
 # ******************************************************************************
50703a
-class Ioc(ctrl.Controller):
50703a
-    '''@brief This object establishes a connection to one I/O Controller.'''
50703a
-
50703a
-    def __init__(self, root, host, tid: trid.TID):
50703a
-        super().__init__(root, host, tid)
50703a
-
50703a
-    def _on_udev_remove(self, udev_obj):
50703a
-        '''Called when the associated nvme device (/dev/nvmeX) is removed
50703a
-        from the system.
50703a
-        '''
50703a
-        super()._on_udev_remove(udev_obj)
50703a
-
50703a
-        # Defer removal of this object to the next main loop's idle period.
50703a
-        GLib.idle_add(STAC.remove_controller, self)
50703a
-
50703a
-    def _find_existing_connection(self):
50703a
-        return self._udev.find_nvme_ioc_device(self.tid)
50703a
-
50703a
-
50703a
-# ******************************************************************************
50703a
-class Stac(service.Service):
50703a
-    '''STorage Appliance Connector (STAC)'''
50703a
+if __name__ == '__main__':
50703a
+    import json
50703a
+    import logging
50703a
+    from staslib import log, service, stas, udev  # pylint: disable=ungrouped-imports
50703a
 
50703a
-    CONF_STABILITY_SOAK_TIME_SEC = 1.5
50703a
-    CONF_STABILITY_LONG_SOAK_TIME_SEC = 10  # pylint: disable=invalid-name
50703a
-    ADD_EVENT_SOAK_TIME_SEC = 1
50703a
+    # Before going any further, make sure the script is allowed to run.
50703a
+    stas.check_if_allowed_to_continue()
50703a
 
50703a
     class Dbus:
50703a
         '''This is the DBus interface that external programs can use to
50703a
@@ -205,229 +142,8 @@ class Stac(service.Service):
50703a
                 for controller in STAC.get_controllers()
50703a
             ]
50703a
 
50703a
-    # ==========================================================================
50703a
-    def __init__(self, args):
50703a
-        super().__init__(args, self._reload_hdlr)
50703a
-
50703a
-        # We don't want to apply configuration changes to nvme-cli right away.
50703a
-        # Often, multiple changes will occur in a short amount of time (sub-second).
50703a
-        # We want to wait until there are no more changes before applying them
50703a
-        # to the system. The following timer acts as a "soak period". Changes
50703a
-        # will be applied by calling self._on_config_ctrls() at the end of
50703a
-        # the soak period.
50703a
-        self._cfg_soak_tmr = gutil.GTimer(Stac.CONF_STABILITY_SOAK_TIME_SEC, self._on_config_ctrls)
50703a
-        self._cfg_soak_tmr.start()
50703a
-
50703a
-        self._add_event_soak_tmr = gutil.GTimer(Stac.ADD_EVENT_SOAK_TIME_SEC, self._on_add_event_soaked)
50703a
-
50703a
-        self._config_connections_audit()
50703a
-
50703a
-        # Create the D-Bus instance.
50703a
-        self._config_dbus(Stac.Dbus(), defs.STACD_DBUS_NAME, defs.STACD_DBUS_PATH)
50703a
-
50703a
-        # Connect to STAF D-Bus interface
50703a
-        self._staf = None
50703a
-        self._staf_watcher = dasbus.client.observer.DBusObserver(self._sysbus, defs.STAFD_DBUS_NAME)
50703a
-        self._staf_watcher.service_available.connect(self._connect_to_staf)
50703a
-        self._staf_watcher.service_unavailable.connect(self._disconnect_from_staf)
50703a
-        self._staf_watcher.connect_once_available()
50703a
-
50703a
-        # Suppress udev rule to auto-connect when AEN is received.
50703a
-        udev_rule_ctrl(conf.SvcConf().udev_rule_enabled)
50703a
-
50703a
-    def _release_resources(self):
50703a
-        logging.debug('Stac._release_resources()')
50703a
-
50703a
-        if self._add_event_soak_tmr:
50703a
-            self._add_event_soak_tmr.kill()
50703a
-
50703a
-        udev_rule_ctrl(True)
50703a
-
50703a
-        if self._udev:
50703a
-            self._udev.unregister_for_action_events('add')
50703a
-
50703a
-        self._destroy_staf_comlink(self._staf_watcher)
50703a
-        if self._staf_watcher is not None:
50703a
-            self._staf_watcher.disconnect()
50703a
-
50703a
-        super()._release_resources()
50703a
-
50703a
-        self._staf = None
50703a
-        self._staf_watcher = None
50703a
-        self._add_event_soak_tmr = None
50703a
-
50703a
-    def _audit_connections(self, tids):
50703a
-        '''A host should only connect to I/O controllers that have been zoned
50703a
-        for that host or a manual "controller" entry exists in stcd.conf.
50703a
-        A host should disconnect from an I/O controller when that I/O controller
50703a
-        is removed from the zone or a manual "controller" entry is removed from
50703a
-        stacd.conf. stacd will audit connections if "sticky-connections=disabled".
50703a
-        stacd will delete any connection that is not supposed to exist.
50703a
-        '''
50703a
-        logging.debug('Stac._audit_connections()          - tids = %s', tids)
50703a
-        num_controllers = len(self._controllers)
50703a
-        for tid in tids:
50703a
-            if tid not in self._controllers:
50703a
-                self._controllers[tid] = Ioc(self._root, self._host, tid)
50703a
-
50703a
-        if num_controllers != len(self._controllers):
50703a
-            self._cfg_soak_tmr.start(Stac.CONF_STABILITY_SOAK_TIME_SEC)
50703a
-
50703a
-    def _on_add_event(self, udev_obj):  # pylint: disable=unused-argument
50703a
-        '''@brief This function is called when a "add" event is received from
50703a
-        the kernel for an NVMe device. This is used to trigger an audit and make
50703a
-        sure that the connection to an I/O controller is allowed.
50703a
-
50703a
-        WARNING: There is a race condition with the "add" event from the kernel.
50703a
-        The kernel sends the "add" event a bit early and the sysfs attributes
50703a
-        associated with the nvme object are not always fully initialized.
50703a
-        To workaround this problem we use a soaking timer to give time for the
50703a
-        sysfs attributes to stabilize.
50703a
-        '''
50703a
-        self._add_event_soak_tmr.start()
50703a
-
50703a
-    def _on_add_event_soaked(self):
50703a
-        '''@brief After the add event has been soaking for ADD_EVENT_SOAK_TIME_SEC
50703a
-        seconds, we can audit the connections.
50703a
-        '''
50703a
-        if not conf.SvcConf().sticky_connections:
50703a
-            self._audit_connections(self._udev.get_nvme_ioc_tids())
50703a
-        return GLib.SOURCE_REMOVE
50703a
-
50703a
-    def _config_connections_audit(self):
50703a
-        '''This function checks the "sticky_connections" parameter to determine
50703a
-        whether audits should be performed. Audits are enabled when
50703a
-        "sticky_connections" is disabled.
50703a
-        '''
50703a
-        if not conf.SvcConf().sticky_connections:
50703a
-            if self._udev.get_registered_action_cback('add') is None:
50703a
-                self._udev.register_for_action_events('add', self._on_add_event)
50703a
-                self._audit_connections(self._udev.get_nvme_ioc_tids())
50703a
-        else:
50703a
-            self._udev.unregister_for_action_events('add')
50703a
-
50703a
-    def _keep_connections_on_exit(self):
50703a
-        '''@brief Determine whether connections should remain when the
50703a
-        process exits.
50703a
-        '''
50703a
-        return True
50703a
-
50703a
-    def _reload_hdlr(self):
50703a
-        '''@brief Reload configuration file. This is triggered by the SIGHUP
50703a
-        signal, which can be sent with "systemctl reload stacd".
50703a
-        '''
50703a
-        systemd.daemon.notify('RELOADING=1')
50703a
-        service_cnf = conf.SvcConf()
50703a
-        service_cnf.reload()
50703a
-        self.tron = service_cnf.tron
50703a
-        self._config_connections_audit()
50703a
-        self._cfg_soak_tmr.start(Stac.CONF_STABILITY_SOAK_TIME_SEC)
50703a
-        udev_rule_ctrl(service_cnf.udev_rule_enabled)
50703a
-        systemd.daemon.notify('READY=1')
50703a
-        return GLib.SOURCE_CONTINUE
50703a
-
50703a
-    def _get_log_pages_from_stafd(self):
50703a
-        if self._staf:
50703a
-            try:
50703a
-                return json.loads(self._staf.get_all_log_pages(True))
50703a
-            except dasbus.error.DBusError:
50703a
-                pass
50703a
-
50703a
-        return list()
50703a
-
50703a
-    def _config_ctrls_finish(self, configured_ctrl_list):
50703a
-        configured_ctrl_list = [
50703a
-            ctrl_dict for ctrl_dict in configured_ctrl_list if 'traddr' in ctrl_dict and 'subsysnqn' in ctrl_dict
50703a
-        ]
50703a
-        logging.debug('Stac._config_ctrls_finish()        - configured_ctrl_list = %s', configured_ctrl_list)
50703a
-
50703a
-        discovered_ctrl_list = list()
50703a
-        for staf_data in self._get_log_pages_from_stafd():
50703a
-            host_traddr = staf_data['discovery-controller']['host-traddr']
50703a
-            host_iface = staf_data['discovery-controller']['host-iface']
50703a
-            for dlpe in staf_data['log-pages']:
50703a
-                if dlpe.get('subtype') == 'nvme':  # eliminate discovery controllers
50703a
-                    discovered_ctrl_list.append(stas.cid_from_dlpe(dlpe, host_traddr, host_iface))
50703a
-
50703a
-        logging.debug('Stac._config_ctrls_finish()        - discovered_ctrl_list = %s', discovered_ctrl_list)
50703a
-
50703a
-        controllers = stas.remove_blacklisted(configured_ctrl_list + discovered_ctrl_list)
50703a
-        controllers = stas.remove_invalid_addresses(controllers)
50703a
-
50703a
-        new_controller_ids = {trid.TID(controller) for controller in controllers}
50703a
-        cur_controller_ids = set(self._controllers.keys())
50703a
-        controllers_to_add = new_controller_ids - cur_controller_ids
50703a
-        controllers_to_del = cur_controller_ids - new_controller_ids
50703a
-
50703a
-        logging.debug('Stac._config_ctrls_finish()        - controllers_to_add   = %s', list(controllers_to_add))
50703a
-        logging.debug('Stac._config_ctrls_finish()        - controllers_to_del   = %s', list(controllers_to_del))
50703a
-
50703a
-        for tid in controllers_to_del:
50703a
-            controller = self._controllers.pop(tid, None)
50703a
-            if controller is not None:
50703a
-                controller.disconnect(self.remove_controller, conf.SvcConf().sticky_connections)
50703a
-
50703a
-        for tid in controllers_to_add:
50703a
-            self._controllers[tid] = Ioc(self._root, self._host, tid)
50703a
-
50703a
-    def _connect_to_staf(self, _):
50703a
-        '''@brief Hook up DBus signal handlers for signals from stafd.'''
50703a
-        try:
50703a
-            self._staf = self._sysbus.get_proxy(defs.STAFD_DBUS_NAME, defs.STAFD_DBUS_PATH)
50703a
-            self._staf.log_pages_changed.connect(self._log_pages_changed)
50703a
-            self._cfg_soak_tmr.start()
50703a
-
50703a
-            # Make sure timer is set back to its normal value.
50703a
-            self._cfg_soak_tmr.set_timeout(Stac.CONF_STABILITY_SOAK_TIME_SEC)
50703a
-            logging.debug('Stac._connect_to_staf()            - Connected to staf')
50703a
-        except dasbus.error.DBusError:
50703a
-            logging.error('Failed to connect to staf')
50703a
-
50703a
-    def _destroy_staf_comlink(self, watcher):  # pylint: disable=unused-argument
50703a
-        if self._staf:
50703a
-            self._staf.log_pages_changed.disconnect(self._log_pages_changed)
50703a
-            dasbus.client.proxy.disconnect_proxy(self._staf)
50703a
-            self._staf = None
50703a
-
50703a
-    def _disconnect_from_staf(self, watcher):
50703a
-        self._destroy_staf_comlink(watcher)
50703a
-
50703a
-        # When we lose connectivity with stafd, the most logical explanation
50703a
-        # is that stafd restarted. In that case, it may take some time for stafd
50703a
-        # to re-populate its log pages cache. So let's give stafd plenty of time
50703a
-        # to update its log pages cache and send log pages change notifications
50703a
-        # before triggering a stacd re-config. We do this by momentarily
50703a
-        # increasing the config soak timer to a longer period.
50703a
-        if self._cfg_soak_tmr:
50703a
-            self._cfg_soak_tmr.set_timeout(Stac.CONF_STABILITY_LONG_SOAK_TIME_SEC)
50703a
-
50703a
-        logging.debug('Stac._disconnect_from_staf()       - Disconnected from staf')
50703a
-
50703a
-    def _log_pages_changed(  # pylint: disable=too-many-arguments
50703a
-        self, transport, traddr, trsvcid, host_traddr, host_iface, subsysnqn, device
50703a
-    ):
50703a
-        logging.debug(
50703a
-            'Stac._log_pages_changed()          - transport=%s, traddr=%s, trsvcid=%s, host_traddr=%s, host_iface=%s, subsysnqn=%s, device=%s',
50703a
-            transport,
50703a
-            traddr,
50703a
-            trsvcid,
50703a
-            host_traddr,
50703a
-            host_iface,
50703a
-            subsysnqn,
50703a
-            device,
50703a
-        )
50703a
-        self._cfg_soak_tmr.start(Stac.CONF_STABILITY_SOAK_TIME_SEC)
50703a
-
50703a
-    def _load_last_known_config(self):
50703a
-        return dict()
50703a
-
50703a
-    def _dump_last_known_config(self, controllers):
50703a
-        pass
50703a
-
50703a
-
50703a
-# ******************************************************************************
50703a
-if __name__ == '__main__':
50703a
-    STAC = Stac(ARGS)
50703a
+    log.init(ARGS.syslog)
50703a
+    STAC = service.Stac(ARGS, Dbus())
50703a
     STAC.run()
50703a
 
50703a
     STAC = None
50703a
diff --git a/stafd.py b/stafd.py
50703a
index aff64fd..8a77c51 100755
50703a
--- a/stafd.py
50703a
+++ b/stafd.py
50703a
@@ -10,14 +10,12 @@
50703a
 ''' STorage Appliance Finder Daemon
50703a
 '''
50703a
 import sys
50703a
-import logging
50703a
 from argparse import ArgumentParser
50703a
 from staslib import defs
50703a
 
50703a
-# pylint: disable=consider-using-f-string
50703a
-DBUS_IDL = '''
50703a
+DBUS_IDL = f'''
50703a
 <node>
50703a
-    <interface name="%s.debug">
50703a
+    <interface name="{defs.STAFD_DBUS_NAME}.debug">
50703a
         <property name="tron" type="b" access="readwrite"/>
50703a
         <property name="log_level" type="s" access="read"/>
50703a
         <method name="process_info">
50703a
@@ -34,10 +32,10 @@ DBUS_IDL = '''
50703a
         </method>
50703a
     </interface>
50703a
 
50703a
-    <interface name="%s">
50703a
+    <interface name="{defs.STAFD_DBUS_NAME}">
50703a
         <method name="list_controllers">
50703a
             <arg direction="in" type="b" name="detailed"/>
50703a
-            <arg direction="out" type="aa{ss}" name="controller_list"/>
50703a
+            <arg direction="out" type="aa{{ss}}" name="controller_list"/>
50703a
         </method>
50703a
         <method name="get_log_pages">
50703a
             <arg direction="in" type="s" name="transport"/>
50703a
@@ -46,7 +44,7 @@ DBUS_IDL = '''
50703a
             <arg direction="in" type="s" name="host_traddr"/>
50703a
             <arg direction="in" type="s" name="host_iface"/>
50703a
             <arg direction="in" type="s" name="subsysnqn"/>
50703a
-            <arg direction="out" type="aa{ss}" name="log_pages"/>
50703a
+            <arg direction="out" type="aa{{ss}}" name="log_pages"/>
50703a
         </method>
50703a
         <method name="get_all_log_pages">
50703a
             <arg direction="in" type="b" name="detailed"/>
50703a
@@ -63,12 +61,10 @@ DBUS_IDL = '''
50703a
         </signal>
50703a
     </interface>
50703a
 </node>
50703a
-''' % (
50703a
-    defs.STAFD_DBUS_NAME,
50703a
-    defs.STAFD_DBUS_NAME,
50703a
-)
50703a
+'''
50703a
 
50703a
 
50703a
+# ******************************************************************************
50703a
 def parse_args(conf_file: str):  # pylint: disable=missing-function-docstring
50703a
     parser = ArgumentParser(
50703a
         description=f'{defs.STAF_DESCRIPTION} ({defs.STAF_ACRONYM}). Must be root to run this program.'
50703a
@@ -99,6 +95,12 @@ ARGS = parse_args(defs.STAFD_CONFIG_FILE)
50703a
 
50703a
 if ARGS.version:
50703a
     print(f'{defs.PROJECT_NAME} {defs.VERSION}')
50703a
+    try:
50703a
+        import libnvme
50703a
+
50703a
+        print(f'libnvme {libnvme.__version__}')
50703a
+    except (AttributeError, ModuleNotFoundError):
50703a
+        pass
50703a
     sys.exit(0)
50703a
 
50703a
 if ARGS.idl:
50703a
@@ -107,250 +109,15 @@ if ARGS.idl:
50703a
     sys.exit(0)
50703a
 
50703a
 
50703a
-# There is a reason for having this import here and not at the top of the file.
50703a
-# We want to allow running stafd with the --version and --idl options and exit
50703a
-# without having to import stas and avahi.
50703a
-from staslib import stas, avahi  # pylint: disable=wrong-import-position
50703a
-
50703a
-# Before going any further, make sure the script is allowed to run.
50703a
-stas.check_if_allowed_to_continue()
50703a
-
50703a
-
50703a
-################################################################################
50703a
-# Preliminary checks have passed. Let her rip!
50703a
-# pylint: disable=wrong-import-position
50703a
-# pylint: disable=wrong-import-order
50703a
-import json
50703a
-import pickle
50703a
-import dasbus.server.interface
50703a
-import systemd.daemon
50703a
-from libnvme import nvme
50703a
-from gi.repository import GLib
50703a
-from staslib import conf, log, gutil, trid, udev, ctrl, service  # pylint: disable=ungrouped-imports
50703a
-
50703a
-log.init(ARGS.syslog)
50703a
-
50703a
-DLP_CHANGED = (
50703a
-    (nvme.NVME_LOG_LID_DISCOVER << 16) | (nvme.NVME_AER_NOTICE_DISC_CHANGED << 8) | nvme.NVME_AER_NOTICE
50703a
-)  # 0x70f002
50703a
-
50703a
-
50703a
 # ******************************************************************************
50703a
-class Dc(ctrl.Controller):
50703a
-    '''@brief This object establishes a connection to one Discover Controller (DC).
50703a
-    It retrieves the discovery log pages and caches them.
50703a
-    It also monitors udev events associated with that DC and updates
50703a
-    the cached discovery log pages accordingly.
50703a
-    '''
50703a
-
50703a
-    GET_LOG_PAGE_RETRY_RERIOD_SEC = 20
50703a
-    REGISTRATION_RETRY_RERIOD_SEC = 10
50703a
-
50703a
-    def __init__(self, root, host, tid: trid.TID, log_pages=None):
50703a
-        super().__init__(root, host, tid, discovery_ctrl=True)
50703a
-        self._register_op = None
50703a
-        self._get_log_op = None
50703a
-        self._log_pages = log_pages if log_pages else list()  # Log pages cache
50703a
-
50703a
-    def _release_resources(self):
50703a
-        logging.debug('Dc._release_resources()            - %s | %s', self.id, self.device)
50703a
-        super()._release_resources()
50703a
-        self._log_pages = list()
50703a
-
50703a
-    def _kill_ops(self):
50703a
-        super()._kill_ops()
50703a
-        if self._get_log_op:
50703a
-            self._get_log_op.kill()
50703a
-            self._get_log_op = None
50703a
-        if self._register_op:
50703a
-            self._register_op.kill()
50703a
-            self._register_op = None
50703a
-
50703a
-    def info(self) -> dict:
50703a
-        '''@brief Get the controller info for this object'''
50703a
-        info = super().info()
50703a
-        if self._get_log_op:
50703a
-            info['get log page operation'] = self._get_log_op.as_dict()
50703a
-        if self._register_op:
50703a
-            info['register operation'] = self._register_op.as_dict()
50703a
-        return info
50703a
-
50703a
-    def cancel(self):
50703a
-        '''@brief Used to cancel pending operations.'''
50703a
-        super().cancel()
50703a
-        if self._get_log_op:
50703a
-            self._get_log_op.cancel()
50703a
-        if self._register_op:
50703a
-            self._register_op.cancel()
50703a
-
50703a
-    def log_pages(self) -> list:
50703a
-        '''@brief Get the cached log pages for this object'''
50703a
-        return self._log_pages
50703a
-
50703a
-    def referrals(self) -> list:
50703a
-        '''@brief Return the list of referrals'''
50703a
-        return [page for page in self._log_pages if page['subtype'] == 'referral']
50703a
-
50703a
-    def _on_aen(self, aen: int):
50703a
-        super()._on_aen(aen)
50703a
-        if aen == DLP_CHANGED and self._get_log_op:
50703a
-            self._get_log_op.run_async()
50703a
-
50703a
-    def _on_nvme_event(self, nvme_event: str):
50703a
-        super()._on_nvme_event(nvme_event)
50703a
-        if nvme_event == 'connected' and self._register_op:
50703a
-            self._register_op.run_async()
50703a
-
50703a
-    def _on_udev_remove(self, udev_obj):
50703a
-        super()._on_udev_remove(udev_obj)
50703a
-        if self._try_to_connect_deferred:
50703a
-            self._try_to_connect_deferred.schedule()
50703a
-
50703a
-    def _find_existing_connection(self):
50703a
-        return self._udev.find_nvme_dc_device(self.tid)
50703a
-
50703a
-    # --------------------------------------------------------------------------
50703a
-    def _on_connect_success(self, op_obj, data):
50703a
-        '''@brief Function called when we successfully connect to the
50703a
-        Discovery Controller.
50703a
-        '''
50703a
-        super()._on_connect_success(op_obj, data)
50703a
-
50703a
-        if self._alive():
50703a
-            if self._ctrl.is_registration_supported():
50703a
-                self._register_op = gutil.AsyncOperationWithRetry(
50703a
-                    self._on_registration_success,
50703a
-                    self._on_registration_fail,
50703a
-                    self._ctrl.registration_ctlr,
50703a
-                    nvme.NVMF_DIM_TAS_REGISTER,
50703a
-                )
50703a
-                self._register_op.run_async()
50703a
-            else:
50703a
-                self._get_log_op = gutil.AsyncOperationWithRetry(
50703a
-                    self._on_get_log_success, self._on_get_log_fail, self._ctrl.discover
50703a
-                )
50703a
-                self._get_log_op.run_async()
50703a
-
50703a
-    # --------------------------------------------------------------------------
50703a
-    def _on_registration_success(self, op_obj, data):  # pylint: disable=unused-argument
50703a
-        '''@brief Function called when we successfully register with the
50703a
-        Discovery Controller. See self._register_op object
50703a
-        for details.
50703a
-        '''
50703a
-        if self._alive():
50703a
-            if data is not None:
50703a
-                logging.warning('%s | %s - Registration error. %s.', self.id, self.device, data)
50703a
-            else:
50703a
-                logging.debug('Dc._on_registration_success()      - %s | %s', self.id, self.device)
50703a
-            self._get_log_op = gutil.AsyncOperationWithRetry(
50703a
-                self._on_get_log_success, self._on_get_log_fail, self._ctrl.discover
50703a
-            )
50703a
-            self._get_log_op.run_async()
50703a
-        else:
50703a
-            logging.debug(
50703a
-                'Dc._on_registration_success()      - %s | %s Received event on dead object.', self.id, self.device
50703a
-            )
50703a
-
50703a
-    def _on_registration_fail(self, op_obj, err, fail_cnt):
50703a
-        '''@brief Function called when we fail to register with the
50703a
-        Discovery Controller. See self._register_op object
50703a
-        for details.
50703a
-        '''
50703a
-        if self._alive():
50703a
-            logging.debug(
50703a
-                'Dc._on_registration_fail()         - %s | %s: %s. Retry in %s sec',
50703a
-                self.id,
50703a
-                self.device,
50703a
-                err,
50703a
-                Dc.REGISTRATION_RETRY_RERIOD_SEC,
50703a
-            )
50703a
-            if fail_cnt == 1:  # Throttle the logs. Only print the first time we fail to connect
50703a
-                logging.error('%s | %s - Failed to register with Discovery Controller. %s', self.id, self.device, err)
50703a
-            # op_obj.retry(Dc.REGISTRATION_RETRY_RERIOD_SEC)
50703a
-        else:
50703a
-            logging.debug(
50703a
-                'Dc._on_registration_fail()         - %s | %s Received event on dead object. %s',
50703a
-                self.id,
50703a
-                self.device,
50703a
-                err,
50703a
-            )
50703a
-            op_obj.kill()
50703a
-
50703a
-    # --------------------------------------------------------------------------
50703a
-    def _on_get_log_success(self, op_obj, data):  # pylint: disable=unused-argument
50703a
-        '''@brief Function called when we successfully retrieve the log pages
50703a
-        from the Discovery Controller. See self._get_log_op object
50703a
-        for details.
50703a
-        '''
50703a
-        if self._alive():
50703a
-            # Note that for historical reasons too long to explain, the CDC may
50703a
-            # return invalid addresses ("0.0.0.0", "::", or ""). Those need to be
50703a
-            # filtered out.
50703a
-            referrals_before = self.referrals()
50703a
-            self._log_pages = (
50703a
-                [
50703a
-                    {k: str(v) for k, v in dictionary.items()}
50703a
-                    for dictionary in data
50703a
-                    if dictionary.get('traddr') not in ('0.0.0.0', '::', '')
50703a
-                ]
50703a
-                if data
50703a
-                else list()
50703a
-            )
50703a
-            logging.info(
50703a
-                '%s | %s - Received discovery log pages (num records=%s).', self.id, self.device, len(self._log_pages)
50703a
-            )
50703a
-            referrals_after = self.referrals()
50703a
-            STAF.log_pages_changed(self, self.device)
50703a
-            if referrals_after != referrals_before:
50703a
-                logging.debug(
50703a
-                    'Dc._on_get_log_success()           - %s | %s Referrals before = %s',
50703a
-                    self.id,
50703a
-                    self.device,
50703a
-                    referrals_before,
50703a
-                )
50703a
-                logging.debug(
50703a
-                    'Dc._on_get_log_success()           - %s | %s Referrals after  = %s',
50703a
-                    self.id,
50703a
-                    self.device,
50703a
-                    referrals_after,
50703a
-                )
50703a
-                STAF.referrals_changed()
50703a
-        else:
50703a
-            logging.debug(
50703a
-                'Dc._on_get_log_success()           - %s | %s Received event on dead object.', self.id, self.device
50703a
-            )
50703a
-
50703a
-    def _on_get_log_fail(self, op_obj, err, fail_cnt):
50703a
-        '''@brief Function called when we fail to retrieve the log pages
50703a
-        from the Discovery Controller. See self._get_log_op object
50703a
-        for details.
50703a
-        '''
50703a
-        if self._alive():
50703a
-            logging.debug(
50703a
-                'Dc._on_get_log_fail()              - %s | %s: %s. Retry in %s sec',
50703a
-                self.id,
50703a
-                self.device,
50703a
-                err,
50703a
-                Dc.GET_LOG_PAGE_RETRY_RERIOD_SEC,
50703a
-            )
50703a
-            if fail_cnt == 1:  # Throttle the logs. Only print the first time we fail to connect
50703a
-                logging.error('%s | %s - Failed to retrieve log pages. %s', self.id, self.device, err)
50703a
-            op_obj.retry(Dc.GET_LOG_PAGE_RETRY_RERIOD_SEC)
50703a
-        else:
50703a
-            logging.debug(
50703a
-                'Dc._on_get_log_fail()              - %s | %s Received event on dead object. %s',
50703a
-                self.id,
50703a
-                self.device,
50703a
-                err,
50703a
-            )
50703a
-            op_obj.kill()
50703a
-
50703a
-
50703a
-# ******************************************************************************
50703a
-class Staf(service.Service):
50703a
-    '''STorage Appliance Finder (STAF)'''
50703a
+if __name__ == '__main__':
50703a
+    import json
50703a
+    import logging
50703a
+    import dasbus.server.interface
50703a
+    from staslib import log, service, stas, udev  # pylint: disable=ungrouped-imports
50703a
 
50703a
-    CONF_STABILITY_SOAK_TIME_SEC = 1.5
50703a
+    # Before going any further, make sure the script is allowed to run.
50703a
+    stas.check_if_allowed_to_continue()
50703a
 
50703a
     class Dbus:
50703a
         '''This is the DBus interface that external programs can use to
50703a
@@ -431,148 +198,8 @@ class Staf(service.Service):
50703a
                 for controller in STAF.get_controllers()
50703a
             ]
50703a
 
50703a
-    # ==========================================================================
50703a
-    def __init__(self, args):
50703a
-        super().__init__(args, self._reload_hdlr)
50703a
-
50703a
-        self._avahi = avahi.Avahi(self._sysbus, self._avahi_change)
50703a
-        self._avahi.config_stypes(conf.SvcConf().get_stypes())
50703a
-
50703a
-        # We don't want to apply configuration changes to nvme-cli right away.
50703a
-        # Often, multiple changes will occur in a short amount of time (sub-second).
50703a
-        # We want to wait until there are no more changes before applying them
50703a
-        # to the system. The following timer acts as a "soak period". Changes
50703a
-        # will be applied by calling self._on_config_ctrls() at the end of
50703a
-        # the soak period.
50703a
-        self._cfg_soak_tmr = gutil.GTimer(Staf.CONF_STABILITY_SOAK_TIME_SEC, self._on_config_ctrls)
50703a
-        self._cfg_soak_tmr.start()
50703a
-
50703a
-        # Create the D-Bus instance.
50703a
-        self._config_dbus(Staf.Dbus(), defs.STAFD_DBUS_NAME, defs.STAFD_DBUS_PATH)
50703a
-
50703a
-    def info(self) -> dict:
50703a
-        '''@brief Get the status info for this object (used for debug)'''
50703a
-        info = super().info()
50703a
-        info['avahi'] = self._avahi.info()
50703a
-        return info
50703a
-
50703a
-    def _release_resources(self):
50703a
-        logging.debug('Staf._release_resources()')
50703a
-        super()._release_resources()
50703a
-        if self._avahi:
50703a
-            self._avahi.kill()
50703a
-            self._avahi = None
50703a
-
50703a
-    def _load_last_known_config(self):
50703a
-        try:
50703a
-            with open(self._lkc_file, 'rb') as file:
50703a
-                config = pickle.load(file)
50703a
-        except (FileNotFoundError, AttributeError):
50703a
-            return dict()
50703a
-
50703a
-        logging.debug('Staf._load_last_known_config()     - DC count = %s', len(config))
50703a
-        return {tid: Dc(self._root, self._host, tid, log_pages) for tid, log_pages in config.items()}
50703a
-
50703a
-    def _dump_last_known_config(self, controllers):
50703a
-        try:
50703a
-            with open(self._lkc_file, 'wb') as file:
50703a
-                config = {tid: dc.log_pages() for tid, dc in controllers.items()}
50703a
-                logging.debug('Staf._dump_last_known_config()     - DC count = %s', len(config))
50703a
-                pickle.dump(config, file)
50703a
-        except FileNotFoundError as ex:
50703a
-            logging.error('Unable to save last known config: %s', ex)
50703a
-
50703a
-    def _keep_connections_on_exit(self):
50703a
-        '''@brief Determine whether connections should remain when the
50703a
-        process exits.
50703a
-        '''
50703a
-        return conf.SvcConf().persistent_connections
50703a
-
50703a
-    def _reload_hdlr(self):
50703a
-        '''@brief Reload configuration file. This is triggered by the SIGHUP
50703a
-        signal, which can be sent with "systemctl reload stafd".
50703a
-        '''
50703a
-        systemd.daemon.notify('RELOADING=1')
50703a
-        service_cnf = conf.SvcConf()
50703a
-        service_cnf.reload()
50703a
-        self.tron = service_cnf.tron
50703a
-        self._avahi.kick_start()  # Make sure Avahi is running
50703a
-        self._avahi.config_stypes(service_cnf.get_stypes())
50703a
-        self._cfg_soak_tmr.start()
50703a
-        systemd.daemon.notify('READY=1')
50703a
-        return GLib.SOURCE_CONTINUE
50703a
-
50703a
-    def log_pages_changed(self, controller, device):
50703a
-        '''@brief Function invoked when a controller's cached log pages
50703a
-        have changed. This will emit a D-Bus signal to inform
50703a
-        other applications that the cached log pages have changed.
50703a
-        '''
50703a
-        self._dbus_iface.log_pages_changed.emit(
50703a
-            controller.tid.transport,
50703a
-            controller.tid.traddr,
50703a
-            controller.tid.trsvcid,
50703a
-            controller.tid.host_traddr,
50703a
-            controller.tid.host_iface,
50703a
-            controller.tid.subsysnqn,
50703a
-            device,
50703a
-        )
50703a
-
50703a
-    def referrals_changed(self):
50703a
-        '''@brief Function invoked when a controller's cached referrals
50703a
-        have changed.
50703a
-        '''
50703a
-        logging.debug('Staf.referrals_changed()')
50703a
-        self._cfg_soak_tmr.start()
50703a
-
50703a
-    def _referrals(self) -> list:
50703a
-        return [
50703a
-            stas.cid_from_dlpe(dlpe, controller.tid.host_traddr, controller.tid.host_iface)
50703a
-            for controller in self.get_controllers()
50703a
-            for dlpe in controller.referrals()
50703a
-        ]
50703a
-
50703a
-    def _config_ctrls_finish(self, configured_ctrl_list):
50703a
-        '''@brief Finish discovery controllers configuration after
50703a
-        hostnames (if any) have been resolved.
50703a
-        '''
50703a
-        configured_ctrl_list = [
50703a
-            ctrl_dict
50703a
-            for ctrl_dict in configured_ctrl_list
50703a
-            if 'traddr' in ctrl_dict and ctrl_dict.setdefault('subsysnqn', defs.WELL_KNOWN_DISC_NQN)
50703a
-        ]
50703a
-
50703a
-        discovered_ctrl_list = self._avahi.get_controllers()
50703a
-        referral_ctrl_list = self._referrals()
50703a
-        logging.debug('Staf._config_ctrls_finish()        - configured_ctrl_list = %s', configured_ctrl_list)
50703a
-        logging.debug('Staf._config_ctrls_finish()        - discovered_ctrl_list = %s', discovered_ctrl_list)
50703a
-        logging.debug('Staf._config_ctrls_finish()        - referral_ctrl_list   = %s', referral_ctrl_list)
50703a
-
50703a
-        controllers = stas.remove_blacklisted(configured_ctrl_list + discovered_ctrl_list + referral_ctrl_list)
50703a
-        controllers = stas.remove_invalid_addresses(controllers)
50703a
-
50703a
-        new_controller_ids = {trid.TID(controller) for controller in controllers}
50703a
-        cur_controller_ids = set(self._controllers.keys())
50703a
-        controllers_to_add = new_controller_ids - cur_controller_ids
50703a
-        controllers_to_del = cur_controller_ids - new_controller_ids
50703a
-
50703a
-        logging.debug('Staf._config_ctrls_finish()        - controllers_to_add   = %s', list(controllers_to_add))
50703a
-        logging.debug('Staf._config_ctrls_finish()        - controllers_to_del   = %s', list(controllers_to_del))
50703a
-
50703a
-        for tid in controllers_to_del:
50703a
-            controller = self._controllers.pop(tid, None)
50703a
-            if controller is not None:
50703a
-                controller.disconnect(self.remove_controller, conf.SvcConf().persistent_connections)
50703a
-
50703a
-        for tid in controllers_to_add:
50703a
-            self._controllers[tid] = Dc(self._root, self._host, tid)
50703a
-
50703a
-    def _avahi_change(self):
50703a
-        self._cfg_soak_tmr.start()
50703a
-
50703a
-
50703a
-# ******************************************************************************
50703a
-if __name__ == '__main__':
50703a
-    STAF = Staf(ARGS)
50703a
+    log.init(ARGS.syslog)
50703a
+    STAF = service.Staf(ARGS, Dbus())
50703a
     STAF.run()
50703a
 
50703a
     STAF = None
50703a
diff --git a/staslib/avahi.py b/staslib/avahi.py
50703a
index 768bbf4..90a67c8 100644
50703a
--- a/staslib/avahi.py
50703a
+++ b/staslib/avahi.py
50703a
@@ -172,9 +172,7 @@ class Avahi:  # pylint: disable=too-many-instance-attributes
50703a
         services = dict()
50703a
         for service, obj in self._services.items():
50703a
             interface, protocol, name, stype, domain = service
50703a
-            key = '({}, {}, {}.{}, {})'.format(  # pylint: disable=consider-using-f-string
50703a
-                socket.if_indextoname(interface), Avahi.protos.get(protocol, 'unknown'), name, domain, stype
50703a
-            )
50703a
+            key = f'({socket.if_indextoname(interface)}, {Avahi.protos.get(protocol, "unknown")}, {name}.{domain}, {stype})'
50703a
             services[key] = obj.get('data', {})
50703a
 
50703a
         info = {
50703a
@@ -316,7 +314,7 @@ class Avahi:  # pylint: disable=too-many-instance-attributes
50703a
         _interface_name: str,
50703a
         _signal_name: str,
50703a
         args: typing.Tuple[int, int, str, str, str, int],
50703a
-        *_user_data
50703a
+        *_user_data,
50703a
     ):
50703a
         (interface, protocol, name, stype, domain, flags) = args
50703a
         logging.debug(
50703a
@@ -352,7 +350,7 @@ class Avahi:  # pylint: disable=too-many-instance-attributes
50703a
         _interface_name: str,
50703a
         _signal_name: str,
50703a
         args: typing.Tuple[int, int, str, str, str, int],
50703a
-        *_user_data
50703a
+        *_user_data,
50703a
     ):
50703a
         (interface, protocol, name, stype, domain, flags) = args
50703a
         logging.debug(
50703a
@@ -386,7 +384,7 @@ class Avahi:  # pylint: disable=too-many-instance-attributes
50703a
         _interface_name: str,
50703a
         _signal_name: str,
50703a
         args: typing.Tuple[int, int, str, str, str, str, int, str, int, list, int],
50703a
-        *_user_data
50703a
+        *_user_data,
50703a
     ):
50703a
         (interface, protocol, name, stype, domain, host, aprotocol, address, port, txt, flags) = args
50703a
         txt = _txt2dict(txt)
50703a
@@ -428,7 +426,7 @@ class Avahi:  # pylint: disable=too-many-instance-attributes
50703a
         interface_name: str,
50703a
         _signal_name: str,
50703a
         args: typing.Tuple[str],
50703a
-        *_user_data
50703a
+        *_user_data,
50703a
     ):
50703a
         (error,) = args
50703a
         if 'ServiceResolver' not in interface_name or 'TimeoutError' not in error:
50703a
diff --git a/staslib/conf.py b/staslib/conf.py
50703a
index 3f52e4f..c314a9e 100644
50703a
--- a/staslib/conf.py
50703a
+++ b/staslib/conf.py
50703a
@@ -74,7 +74,7 @@ class SvcConf(metaclass=singleton.Singleton):
50703a
             ('Global', 'ignore-iface'): 'false',
50703a
             ('Global', 'ip-family'): 'ipv4+ipv6',
50703a
             ('Global', 'udev-rule'): 'enabled',
50703a
-            ('Global', 'sticky-connections'): 'disabled',
50703a
+            ('Global', 'sticky-connections'): 'enabled',
50703a
             ('Service Discovery', 'zeroconf'): 'enabled',
50703a
             ('Controllers', 'controller'): list(),
50703a
             ('Controllers', 'blacklist'): list(),
50703a
diff --git a/staslib/ctrl.py b/staslib/ctrl.py
50703a
index 5504baa..dbc1973 100644
50703a
--- a/staslib/ctrl.py
50703a
+++ b/staslib/ctrl.py
50703a
@@ -10,69 +10,76 @@
50703a
 Dc (Discovery Controller) and Ioc (I/O Controller) objects are derived.'''
50703a
 
50703a
 import logging
50703a
-from gi.repository import Gio, GLib
50703a
+from gi.repository import GLib
50703a
 from libnvme import nvme
50703a
-from staslib import conf, gutil, trid, udev
50703a
+from staslib import conf, gutil, trid, udev, stas
50703a
 
50703a
 
50703a
 DC_KATO_DEFAULT = 30  # seconds
50703a
 
50703a
 
50703a
 # ******************************************************************************
50703a
-class Controller:  # pylint: disable=too-many-instance-attributes
50703a
+class Controller(stas.ControllerABC):
50703a
     '''@brief Base class used to manage the connection to a controller.'''
50703a
 
50703a
-    CONNECT_RETRY_PERIOD_SEC = 60
50703a
-    FAST_CONNECT_RETRY_PERIOD_SEC = 3
50703a
-
50703a
     def __init__(self, root, host, tid: trid.TID, discovery_ctrl=False):
50703a
-        self._root              = root
50703a
-        self._host              = host
50703a
-        self._udev              = udev.UDEV
50703a
-        self._tid               = tid
50703a
-        self._cancellable       = Gio.Cancellable()
50703a
-        self._connect_op        = None
50703a
-        self._connect_attempts  = 0
50703a
-        self._retry_connect_tmr = gutil.GTimer(Controller.CONNECT_RETRY_PERIOD_SEC, self._on_try_to_connect)
50703a
-        self._device            = None
50703a
-        self._ctrl              = None
50703a
-        self._discovery_ctrl    = discovery_ctrl
50703a
-        self._try_to_connect_deferred = gutil.Deferred(self._try_to_connect)
50703a
-        self._try_to_connect_deferred.schedule()
50703a
+        self._udev       = udev.UDEV
50703a
+        self._device     = None  # Refers to the nvme device (e.g. /dev/nvme[n])
50703a
+        self._ctrl       = None  # libnvme's nvme.ctrl object
50703a
+        self._connect_op = None
50703a
+
50703a
+        super().__init__(root, host, tid, discovery_ctrl)
50703a
 
50703a
     def _release_resources(self):
50703a
         logging.debug('Controller._release_resources()    - %s', self.id)
50703a
 
50703a
-        # Remove pending deferred from main loop
50703a
-        if self._try_to_connect_deferred:
50703a
-            self._try_to_connect_deferred.cancel()
50703a
-        self._try_to_connect_deferred = None
50703a
-
50703a
         if self._udev:
50703a
             self._udev.unregister_for_device_events(self._on_udev_notification)
50703a
 
50703a
-        if self._retry_connect_tmr is not None:
50703a
-            self._retry_connect_tmr.kill()
50703a
-
50703a
-        if self._cancellable and not self._cancellable.is_cancelled():
50703a
-            self._cancellable.cancel()
50703a
-
50703a
         self._kill_ops()
50703a
 
50703a
-        self._tid = None
50703a
+        super()._release_resources()
50703a
+
50703a
         self._ctrl = None
50703a
-        self._device = None
50703a
-        self._retry_connect_tmr = None
50703a
-        self._cancellable = None
50703a
         self._udev = None
50703a
 
50703a
-    def _alive(self):
50703a
-        '''There may be race condition where a queued event gets processed
50703a
-        after the object is no longer configured (i.e. alive). This method
50703a
-        can be used by callback functions to make sure the object is still
50703a
-        alive before processing further.
50703a
-        '''
50703a
-        return self._cancellable and not self._cancellable.is_cancelled()
50703a
+    @property
50703a
+    def device(self) -> str:
50703a
+        '''@brief return the Linux nvme device id (e.g. nvme3) or empty
50703a
+        string if no device is associated with this controller'''
50703a
+        if not self._device and self._ctrl and self._ctrl.name:
50703a
+            self._device = self._ctrl.name
50703a
+
50703a
+        return self._device or 'nvme?'
50703a
+
50703a
+    def controller_id_dict(self) -> dict:
50703a
+        '''@brief return the controller ID as a dict.'''
50703a
+        cid = super().controller_id_dict()
50703a
+        cid['device'] = self.device
50703a
+        return cid
50703a
+
50703a
+    def details(self) -> dict:
50703a
+        '''@brief return detailed debug info about this controller'''
50703a
+        details = super().details()
50703a
+        details.update(
50703a
+            self._udev.get_attributes(self.device,
50703a
+                                      ('hostid', 'hostnqn', 'model',
50703a
+                                       'serial', 'dctype', 'cntrltype'))
50703a
+        )
50703a
+        return details
50703a
+
50703a
+    def info(self) -> dict:
50703a
+        '''@brief Get the controller info for this object'''
50703a
+        info = super().info()
50703a
+        if self._connect_op:
50703a
+            info['connect operation'] = self._connect_op.as_dict()
50703a
+        return info
50703a
+
50703a
+    def cancel(self):
50703a
+        '''@brief Used to cancel pending operations.'''
50703a
+        super().cancel()
50703a
+        if self._connect_op:
50703a
+            self._connect_op.cancel()
50703a
 
50703a
     def _kill_ops(self):
50703a
         if self._connect_op:
50703a
@@ -91,7 +98,7 @@ class Controller:  # pylint: disable=too-many-instance-attributes
50703a
                     self._on_nvme_event(nvme_event)
50703a
             elif udev_obj.action == 'remove':
50703a
                 logging.info('%s | %s - Received "remove" event', self.id, udev_obj.sys_name)
50703a
-                self._on_udev_remove(udev_obj)
50703a
+                self._on_ctrl_removed(udev_obj)
50703a
             else:
50703a
                 logging.debug(
50703a
                     'Controller._on_udev_notification() - %s | %s - Received "%s" notification.',
50703a
@@ -108,33 +115,12 @@ class Controller:  # pylint: disable=too-many-instance-attributes
50703a
                 udev_obj.sys_name,
50703a
             )
50703a
 
50703a
-    def _on_aen(self, aen: int):
50703a
-        pass
50703a
-
50703a
-    def _on_nvme_event(self, nvme_event):
50703a
-        pass
50703a
-
50703a
-    def _on_udev_remove(self, udev_obj):  # pylint: disable=unused-argument
50703a
+    def _on_ctrl_removed(self, obj):  # pylint: disable=unused-argument
50703a
         self._udev.unregister_for_device_events(self._on_udev_notification)
50703a
         self._kill_ops()  # Kill all pending operations
50703a
         self._ctrl = None
50703a
 
50703a
-    def _find_existing_connection(self):
50703a
-        raise NotImplementedError()
50703a
-
50703a
-    def _on_try_to_connect(self):
50703a
-        self._try_to_connect_deferred.schedule()
50703a
-        return GLib.SOURCE_REMOVE
50703a
-
50703a
-    def _try_to_connect(self):
50703a
-        # This is a deferred function call. Make sure
50703a
-        # the source of the deferred is still good.
50703a
-        source = GLib.main_current_source()
50703a
-        if source and source.is_destroyed():
50703a
-            return
50703a
-
50703a
-        self._connect_attempts += 1
50703a
-
50703a
+    def _do_connect(self):
50703a
         host_iface = (
50703a
             self.tid.host_iface
50703a
             if (self.tid.host_iface and not conf.SvcConf().ignore_iface and conf.NvmeOptions().host_iface_supp)
50703a
@@ -164,7 +150,6 @@ class Controller:  # pylint: disable=too-many-instance-attributes
50703a
                 self._on_connect_success, self._on_connect_fail, self._ctrl.init, self._host, int(udev_obj.sys_number)
50703a
             )
50703a
         else:
50703a
-            self._device = None
50703a
             service_conf = conf.SvcConf()
50703a
             cfg = { 'hdr_digest':  service_conf.hdr_digest,
50703a
                     'data_digest': service_conf.data_digest }
50703a
@@ -198,11 +183,10 @@ class Controller:  # pylint: disable=too-many-instance-attributes
50703a
         self._connect_op = None
50703a
 
50703a
         if self._alive():
50703a
-            if not self._device:
50703a
-                self._device = self._ctrl.name
50703a
+            self._device = self._ctrl.name
50703a
             logging.info('%s | %s - Connection established!', self.id, self.device)
50703a
             self._connect_attempts = 0
50703a
-            self._udev.register_for_device_events(self.device, self._on_udev_notification)
50703a
+            self._udev.register_for_device_events(self._device, self._on_udev_notification)
50703a
         else:
50703a
             logging.debug(
50703a
                 'Controller._on_connect_success()   - %s | %s Received event on dead object. data=%s',
50703a
@@ -227,11 +211,11 @@ class Controller:  # pylint: disable=too-many-instance-attributes
50703a
                 # the same time. This is perfectly fine, except that we may get a bogus
50703a
                 # failed to connect error. By doing a fast re-try, stacd can quickly
50703a
                 # verify that the connection was actually successful.
50703a
-                self._retry_connect_tmr.set_timeout(Controller.FAST_CONNECT_RETRY_PERIOD_SEC)
50703a
+                self._retry_connect_tmr.set_timeout(self.FAST_CONNECT_RETRY_PERIOD_SEC)
50703a
             elif self._connect_attempts == 2:
50703a
                 # If the fast connect re-try fails, then we can print a message to
50703a
                 # indicate the failure, and start a slow re-try period.
50703a
-                self._retry_connect_tmr.set_timeout(Controller.CONNECT_RETRY_PERIOD_SEC)
50703a
+                self._retry_connect_tmr.set_timeout(self.CONNECT_RETRY_PERIOD_SEC)
50703a
                 logging.error('%s Failed to connect to controller. %s', self.id, getattr(err, 'message', err))
50703a
 
50703a
             logging.debug(
50703a
@@ -248,53 +232,6 @@ class Controller:  # pylint: disable=too-many-instance-attributes
50703a
                 getattr(err, 'message', err),
50703a
             )
50703a
 
50703a
-    @property
50703a
-    def id(self) -> str:  # pylint: disable=missing-function-docstring
50703a
-        return str(self.tid)
50703a
-
50703a
-    @property
50703a
-    def tid(self):  # pylint: disable=missing-function-docstring
50703a
-        return self._tid
50703a
-
50703a
-    @property
50703a
-    def device(self) -> str:  # pylint: disable=missing-function-docstring
50703a
-        return self._device if self._device else ''
50703a
-
50703a
-    def controller_id_dict(self) -> dict:
50703a
-        '''@brief return the controller ID as a dict.'''
50703a
-        cid = self.tid.as_dict()
50703a
-        cid['device'] = self.device
50703a
-        return cid
50703a
-
50703a
-    def details(self) -> dict:
50703a
-        '''@brief return detailed debug info about this controller'''
50703a
-        details = self.controller_id_dict()
50703a
-        details.update(self._udev.get_attributes(self.device, ('hostid', 'hostnqn', 'model', 'serial')))
50703a
-        details['connect attempts'] = str(self._connect_attempts)
50703a
-        details['retry connect timer'] = str(self._retry_connect_tmr)
50703a
-        return details
50703a
-
50703a
-    def info(self) -> dict:
50703a
-        '''@brief Get the controller info for this object'''
50703a
-        info = self.details()
50703a
-        if self._connect_op:
50703a
-            info['connect operation'] = self._connect_op.as_dict()
50703a
-        return info
50703a
-
50703a
-    def cancel(self):
50703a
-        '''@brief Used to cancel pending operations.'''
50703a
-        if self._cancellable and not self._cancellable.is_cancelled():
50703a
-            logging.debug('Controller.cancel()                - %s', self.id)
50703a
-            self._cancellable.cancel()
50703a
-
50703a
-        if self._connect_op:
50703a
-            self._connect_op.cancel()
50703a
-
50703a
-    def kill(self):
50703a
-        '''@brief Used to release all resources associated with this object.'''
50703a
-        logging.debug('Controller.kill()                  - %s', self.id)
50703a
-        self._release_resources()
50703a
-
50703a
     def disconnect(self, disconnected_cb, keep_connection):
50703a
         '''@brief Issue an asynchronous disconnect command to a Controller.
50703a
         Once the async command has completed, the callback 'disconnected_cb'
50703a
@@ -313,7 +250,7 @@ class Controller:  # pylint: disable=too-many-instance-attributes
50703a
             # cannot be called directly as the current Controller object is in the
50703a
             # process of being disconnected and the callback will in fact delete
50703a
             # the object. This would invariably lead to unpredictable outcome.
50703a
-            GLib.idle_add(disconnected_cb, self)
50703a
+            GLib.idle_add(disconnected_cb, self, True)
50703a
 
50703a
     def _on_disconn_success(self, op_obj, data, disconnected_cb):  # pylint: disable=unused-argument
50703a
         logging.debug('Controller._on_disconn_success()   - %s | %s', self.id, self.device)
50703a
@@ -322,7 +259,7 @@ class Controller:  # pylint: disable=too-many-instance-attributes
50703a
         # cannot be called directly as the current Controller object is in the
50703a
         # process of being disconnected and the callback will in fact delete
50703a
         # the object. This would invariably lead to unpredictable outcome.
50703a
-        GLib.idle_add(disconnected_cb, self)
50703a
+        GLib.idle_add(disconnected_cb, self, True)
50703a
 
50703a
     def _on_disconn_fail(self, op_obj, err, fail_cnt, disconnected_cb):  # pylint: disable=unused-argument
50703a
         logging.debug('Controller._on_disconn_fail()      - %s | %s: %s', self.id, self.device, err)
50703a
@@ -331,4 +268,249 @@ class Controller:  # pylint: disable=too-many-instance-attributes
50703a
         # cannot be called directly as the current Controller object is in the
50703a
         # process of being disconnected and the callback will in fact delete
50703a
         # the object. This would invariably lead to unpredictable outcome.
50703a
-        GLib.idle_add(disconnected_cb, self)
50703a
+        GLib.idle_add(disconnected_cb, self, False)
50703a
+
50703a
+
50703a
+# ******************************************************************************
50703a
+class Dc(Controller):
50703a
+    '''@brief This object establishes a connection to one Discover Controller (DC).
50703a
+    It retrieves the discovery log pages and caches them.
50703a
+    It also monitors udev events associated with that DC and updates
50703a
+    the cached discovery log pages accordingly.
50703a
+    '''
50703a
+
50703a
+    DLP_CHANGED = (
50703a
+        (nvme.NVME_LOG_LID_DISCOVER << 16) | (nvme.NVME_AER_NOTICE_DISC_CHANGED << 8) | nvme.NVME_AER_NOTICE
50703a
+    )  # 0x70f002
50703a
+    GET_LOG_PAGE_RETRY_RERIOD_SEC = 20
50703a
+    REGISTRATION_RETRY_RERIOD_SEC = 10
50703a
+
50703a
+    def __init__(self, staf, root, host, tid: trid.TID, log_pages=None):  # pylint: disable=too-many-arguments
50703a
+        super().__init__(root, host, tid, discovery_ctrl=True)
50703a
+        self._staf = staf
50703a
+        self._register_op = None
50703a
+        self._get_log_op = None
50703a
+        self._log_pages = log_pages if log_pages else list()  # Log pages cache
50703a
+
50703a
+    def _release_resources(self):
50703a
+        logging.debug('Dc._release_resources()            - %s | %s', self.id, self.device)
50703a
+        super()._release_resources()
50703a
+        self._log_pages = list()
50703a
+        self._staf = None
50703a
+
50703a
+    def _kill_ops(self):
50703a
+        super()._kill_ops()
50703a
+        if self._get_log_op:
50703a
+            self._get_log_op.kill()
50703a
+            self._get_log_op = None
50703a
+        if self._register_op:
50703a
+            self._register_op.kill()
50703a
+            self._register_op = None
50703a
+
50703a
+    def info(self) -> dict:
50703a
+        '''@brief Get the controller info for this object'''
50703a
+        info = super().info()
50703a
+        if self._get_log_op:
50703a
+            info['get log page operation'] = self._get_log_op.as_dict()
50703a
+        if self._register_op:
50703a
+            info['register operation'] = self._register_op.as_dict()
50703a
+        return info
50703a
+
50703a
+    def cancel(self):
50703a
+        '''@brief Used to cancel pending operations.'''
50703a
+        super().cancel()
50703a
+        if self._get_log_op:
50703a
+            self._get_log_op.cancel()
50703a
+        if self._register_op:
50703a
+            self._register_op.cancel()
50703a
+
50703a
+    def log_pages(self) -> list:
50703a
+        '''@brief Get the cached log pages for this object'''
50703a
+        return self._log_pages
50703a
+
50703a
+    def referrals(self) -> list:
50703a
+        '''@brief Return the list of referrals'''
50703a
+        return [page for page in self._log_pages if page['subtype'] == 'referral']
50703a
+
50703a
+    def _on_aen(self, aen: int):
50703a
+        if aen == self.DLP_CHANGED and self._get_log_op:
50703a
+            self._get_log_op.run_async()
50703a
+
50703a
+    def _on_nvme_event(self, nvme_event: str):
50703a
+        if nvme_event == 'connected' and self._register_op:
50703a
+            self._register_op.run_async()
50703a
+
50703a
+    def _on_ctrl_removed(self, obj):
50703a
+        super()._on_ctrl_removed(obj)
50703a
+        if self._try_to_connect_deferred:
50703a
+            self._try_to_connect_deferred.schedule()
50703a
+
50703a
+    def _find_existing_connection(self):
50703a
+        return self._udev.find_nvme_dc_device(self.tid)
50703a
+
50703a
+    # --------------------------------------------------------------------------
50703a
+    def _on_connect_success(self, op_obj, data):
50703a
+        '''@brief Function called when we successfully connect to the
50703a
+        Discovery Controller.
50703a
+        '''
50703a
+        super()._on_connect_success(op_obj, data)
50703a
+
50703a
+        if self._alive():
50703a
+            if self._ctrl.is_registration_supported():
50703a
+                self._register_op = gutil.AsyncOperationWithRetry(
50703a
+                    self._on_registration_success,
50703a
+                    self._on_registration_fail,
50703a
+                    self._ctrl.registration_ctlr,
50703a
+                    nvme.NVMF_DIM_TAS_REGISTER,
50703a
+                )
50703a
+                self._register_op.run_async()
50703a
+            else:
50703a
+                self._get_log_op = gutil.AsyncOperationWithRetry(
50703a
+                    self._on_get_log_success, self._on_get_log_fail, self._ctrl.discover
50703a
+                )
50703a
+                self._get_log_op.run_async()
50703a
+
50703a
+    # --------------------------------------------------------------------------
50703a
+    def _on_registration_success(self, op_obj, data):  # pylint: disable=unused-argument
50703a
+        '''@brief Function called when we successfully register with the
50703a
+        Discovery Controller. See self._register_op object
50703a
+        for details.
50703a
+        '''
50703a
+        if self._alive():
50703a
+            if data is not None:
50703a
+                logging.warning('%s | %s - Registration error. %s.', self.id, self.device, data)
50703a
+            else:
50703a
+                logging.debug('Dc._on_registration_success()      - %s | %s', self.id, self.device)
50703a
+            self._get_log_op = gutil.AsyncOperationWithRetry(
50703a
+                self._on_get_log_success, self._on_get_log_fail, self._ctrl.discover
50703a
+            )
50703a
+            self._get_log_op.run_async()
50703a
+        else:
50703a
+            logging.debug(
50703a
+                'Dc._on_registration_success()      - %s | %s Received event on dead object.', self.id, self.device
50703a
+            )
50703a
+
50703a
+    def _on_registration_fail(self, op_obj, err, fail_cnt):
50703a
+        '''@brief Function called when we fail to register with the
50703a
+        Discovery Controller. See self._register_op object
50703a
+        for details.
50703a
+        '''
50703a
+        if self._alive():
50703a
+            logging.debug(
50703a
+                'Dc._on_registration_fail()         - %s | %s: %s. Retry in %s sec',
50703a
+                self.id,
50703a
+                self.device,
50703a
+                err,
50703a
+                Dc.REGISTRATION_RETRY_RERIOD_SEC,
50703a
+            )
50703a
+            if fail_cnt == 1:  # Throttle the logs. Only print the first time we fail to connect
50703a
+                logging.error('%s | %s - Failed to register with Discovery Controller. %s', self.id, self.device, err)
50703a
+            # op_obj.retry(Dc.REGISTRATION_RETRY_RERIOD_SEC)
50703a
+        else:
50703a
+            logging.debug(
50703a
+                'Dc._on_registration_fail()         - %s | %s Received event on dead object. %s',
50703a
+                self.id,
50703a
+                self.device,
50703a
+                err,
50703a
+            )
50703a
+            op_obj.kill()
50703a
+
50703a
+    # --------------------------------------------------------------------------
50703a
+    def _on_get_log_success(self, op_obj, data):  # pylint: disable=unused-argument
50703a
+        '''@brief Function called when we successfully retrieve the log pages
50703a
+        from the Discovery Controller. See self._get_log_op object
50703a
+        for details.
50703a
+        '''
50703a
+        if self._alive():
50703a
+            # Note that for historical reasons too long to explain, the CDC may
50703a
+            # return invalid addresses ("0.0.0.0", "::", or ""). Those need to be
50703a
+            # filtered out.
50703a
+            referrals_before = self.referrals()
50703a
+            self._log_pages = (
50703a
+                [
50703a
+                    {k: str(v) for k, v in dictionary.items()}
50703a
+                    for dictionary in data
50703a
+                    if dictionary.get('traddr') not in ('0.0.0.0', '::', '')
50703a
+                ]
50703a
+                if data
50703a
+                else list()
50703a
+            )
50703a
+            logging.info(
50703a
+                '%s | %s - Received discovery log pages (num records=%s).', self.id, self.device, len(self._log_pages)
50703a
+            )
50703a
+            referrals_after = self.referrals()
50703a
+            self._staf.log_pages_changed(self, self.device)
50703a
+            if referrals_after != referrals_before:
50703a
+                logging.debug(
50703a
+                    'Dc._on_get_log_success()           - %s | %s Referrals before = %s',
50703a
+                    self.id,
50703a
+                    self.device,
50703a
+                    referrals_before,
50703a
+                )
50703a
+                logging.debug(
50703a
+                    'Dc._on_get_log_success()           - %s | %s Referrals after  = %s',
50703a
+                    self.id,
50703a
+                    self.device,
50703a
+                    referrals_after,
50703a
+                )
50703a
+                self._staf.referrals_changed()
50703a
+        else:
50703a
+            logging.debug(
50703a
+                'Dc._on_get_log_success()           - %s | %s Received event on dead object.', self.id, self.device
50703a
+            )
50703a
+
50703a
+    def _on_get_log_fail(self, op_obj, err, fail_cnt):
50703a
+        '''@brief Function called when we fail to retrieve the log pages
50703a
+        from the Discovery Controller. See self._get_log_op object
50703a
+        for details.
50703a
+        '''
50703a
+        if self._alive():
50703a
+            logging.debug(
50703a
+                'Dc._on_get_log_fail()              - %s | %s: %s. Retry in %s sec',
50703a
+                self.id,
50703a
+                self.device,
50703a
+                err,
50703a
+                Dc.GET_LOG_PAGE_RETRY_RERIOD_SEC,
50703a
+            )
50703a
+            if fail_cnt == 1:  # Throttle the logs. Only print the first time we fail to connect
50703a
+                logging.error('%s | %s - Failed to retrieve log pages. %s', self.id, self.device, err)
50703a
+            op_obj.retry(Dc.GET_LOG_PAGE_RETRY_RERIOD_SEC)
50703a
+        else:
50703a
+            logging.debug(
50703a
+                'Dc._on_get_log_fail()              - %s | %s Received event on dead object. %s',
50703a
+                self.id,
50703a
+                self.device,
50703a
+                err,
50703a
+            )
50703a
+            op_obj.kill()
50703a
+
50703a
+
50703a
+# ******************************************************************************
50703a
+class Ioc(Controller):
50703a
+    '''@brief This object establishes a connection to one I/O Controller.'''
50703a
+
50703a
+    def __init__(self, stac, root, host, tid: trid.TID):
50703a
+        self._stac = stac
50703a
+        super().__init__(root, host, tid)
50703a
+
50703a
+    def _release_resources(self):
50703a
+        super()._release_resources()
50703a
+        self._stac = None
50703a
+
50703a
+    def _on_ctrl_removed(self, obj):
50703a
+        '''Called when the associated nvme device (/dev/nvmeX) is removed
50703a
+        from the system.
50703a
+        '''
50703a
+        super()._on_ctrl_removed(obj)
50703a
+
50703a
+        # Defer removal of this object to the next main loop's idle period.
50703a
+        GLib.idle_add(self._stac.remove_controller, self, True)
50703a
+
50703a
+    def _find_existing_connection(self):
50703a
+        return self._udev.find_nvme_ioc_device(self.tid)
50703a
+
50703a
+    def _on_aen(self, aen: int):
50703a
+        pass
50703a
+
50703a
+    def _on_nvme_event(self, nvme_event):
50703a
+        pass
50703a
diff --git a/staslib/gutil.py b/staslib/gutil.py
50703a
index b302f3a..36ce2c7 100644
50703a
--- a/staslib/gutil.py
50703a
+++ b/staslib/gutil.py
50703a
@@ -104,8 +104,7 @@ class GTimer:
50703a
 
50703a
 
50703a
 # ******************************************************************************
50703a
-class NameResolver:
50703a
-    # pylint: disable=too-few-public-methods
50703a
+class NameResolver:  # pylint: disable=too-few-public-methods
50703a
     '''@brief DNS resolver to convert host names to IP addresses.'''
50703a
 
50703a
     def __init__(self):
50703a
@@ -133,8 +132,10 @@ class NameResolver:
50703a
                 else:
50703a
                     logging.error('Cannot resolve traddr: %s', hostname)
50703a
 
50703a
-            except GLib.GError:
50703a
-                logging.error('Cannot resolve traddr: %s', hostname)
50703a
+            except GLib.GError as err:
50703a
+                # We don't need to report "cancellation" errors.
50703a
+                if not err.matches(Gio.io_error_quark(), Gio.IOErrorEnum.CANCELLED):
50703a
+                    logging.error('Cannot resolve traddr: %s. %s', hostname, err.message)  # pylint: disable=no-member
50703a
 
50703a
             logging.debug('NameResolver.resolve_ctrl_async()  - resolved \'%s\' -> %s', hostname, traddr)
50703a
             controllers[indx]['traddr'] = traddr
50703a
diff --git a/staslib/log.py b/staslib/log.py
50703a
index c624978..9622e98 100644
50703a
--- a/staslib/log.py
50703a
+++ b/staslib/log.py
50703a
@@ -24,7 +24,7 @@ def init(syslog: bool):
50703a
     if syslog:
50703a
         try:
50703a
             # Try journal logger first
50703a
-            import systemd.journal  # pylint: disable=redefined-outer-name,import-outside-toplevel
50703a
+            import systemd.journal  # pylint: disable=import-outside-toplevel
50703a
 
50703a
             handler = systemd.journal.JournalHandler(SYSLOG_IDENTIFIER=defs.PROG_NAME)
50703a
         except ModuleNotFoundError:
50703a
@@ -32,9 +32,7 @@ def init(syslog: bool):
50703a
             from logging.handlers import SysLogHandler  # pylint: disable=import-outside-toplevel
50703a
 
50703a
             handler = SysLogHandler(address="/dev/log")
50703a
-            handler.setFormatter(
50703a
-                logging.Formatter('{}: %(message)s'.format(defs.PROG_NAME))  # pylint: disable=consider-using-f-string
50703a
-            )
50703a
+            handler.setFormatter(logging.Formatter(f'{defs.PROG_NAME}: %(message)s'))
50703a
     else:
50703a
         # Log to stdout
50703a
         handler = logging.StreamHandler(stream=sys.stdout)
50703a
diff --git a/staslib/service.py b/staslib/service.py
50703a
index 556a9f9..a48e66d 100644
50703a
--- a/staslib/service.py
50703a
+++ b/staslib/service.py
50703a
@@ -9,248 +9,416 @@
50703a
 '''This module defines the base Service object from
50703a
 which the Staf and the Stac objects are derived.'''
50703a
 
50703a
-import os
50703a
-import signal
50703a
+import json
50703a
+import pickle
50703a
 import logging
50703a
+import pathlib
50703a
 import systemd.daemon
50703a
-import dasbus.connection
50703a
+import dasbus.error
50703a
+import dasbus.client.observer
50703a
+import dasbus.client.proxy
50703a
 
50703a
-from gi.repository import Gio, GLib
50703a
+from gi.repository import GLib
50703a
 from libnvme import nvme
50703a
-from staslib import conf, ctrl, defs, gutil, log, stas, trid, udev
50703a
+from staslib import avahi, conf, ctrl, defs, gutil, stas, trid, udev
50703a
 
50703a
 
50703a
 # ******************************************************************************
50703a
-class Service:  # pylint: disable=too-many-instance-attributes
50703a
+class Service(stas.ServiceABC):
50703a
     '''@brief Base class used to manage a STorage Appliance Service'''
50703a
 
50703a
     def __init__(self, args, reload_hdlr):
50703a
-
50703a
         sysconf = conf.SysConf()
50703a
         self._root = nvme.root()
50703a
         self._host = nvme.host(self._root, sysconf.hostnqn, sysconf.hostid, sysconf.hostsymname)
50703a
 
50703a
-        service_conf = conf.SvcConf()
50703a
-        service_conf.set_conf_file(args.conf_file) # reload configuration
50703a
-        self._tron = args.tron or service_conf.tron
50703a
-        log.set_level_from_tron(self._tron)
50703a
-        self._root.log_level("debug" if self._tron else "err")
50703a
+        super().__init__(args, reload_hdlr)
50703a
 
50703a
-        self._lkc_file     = os.path.join(os.environ.get('RUNTIME_DIRECTORY', os.path.join('/run', defs.PROG_NAME)), 'last-known-config.pickle')
50703a
-        self._loop         = GLib.MainLoop()
50703a
-        self._udev         = udev.UDEV
50703a
-        self._cancellable  = Gio.Cancellable()
50703a
-        self._resolver     = gutil.NameResolver()
50703a
-        self._controllers  = self._load_last_known_config()
50703a
-        self._dbus_iface   = None
50703a
-        self._cfg_soak_tmr = None
50703a
-        self._sysbus       = dasbus.connection.SystemMessageBus()
50703a
-
50703a
-        GLib.unix_signal_add(GLib.PRIORITY_HIGH, signal.SIGINT, self._stop_hdlr)  # CTRL-C
50703a
-        GLib.unix_signal_add(GLib.PRIORITY_HIGH, signal.SIGTERM, self._stop_hdlr)  # systemctl stop stafd
50703a
-        GLib.unix_signal_add(GLib.PRIORITY_HIGH, signal.SIGHUP, reload_hdlr)  # systemctl reload stafd
50703a
-
50703a
-        nvme_options = conf.NvmeOptions()
50703a
-        if not nvme_options.host_iface_supp or not nvme_options.discovery_supp:
50703a
-            logging.warning(
50703a
-                'Kernel does not appear to support all the options needed to run this program. Consider updating to a later kernel version.'
50703a
-            )
50703a
+        self._root.log_level("debug" if self._tron else "err")
50703a
 
50703a
     def _release_resources(self):
50703a
         logging.debug('Service._release_resources()')
50703a
+        super()._release_resources()
50703a
 
50703a
-        if self._cancellable and not self._cancellable.is_cancelled():
50703a
-            self._cancellable.cancel()
50703a
+        self._host = None
50703a
+        self._root = None
50703a
 
50703a
-        if self._cfg_soak_tmr is not None:
50703a
-            self._cfg_soak_tmr.kill()
50703a
+    @stas.ServiceABC.tron.setter
50703a
+    def tron(self, value):
50703a
+        '''@brief Set Trace ON property'''
50703a
+        super(__class__, self.__class__).tron.__set__(self, value)
50703a
+        self._root.log_level("debug" if self._tron else "err")
50703a
 
50703a
-        self._controllers.clear()
50703a
 
50703a
-        if self._sysbus:
50703a
-            self._sysbus.disconnect()
50703a
+# ******************************************************************************
50703a
+def udev_rule_ctrl(enable):
50703a
+    '''@brief We add an empty udev rule to /run/udev/rules.d to suppress
50703a
+    nvme-cli's udev rule that is used to tell udevd to automatically
50703a
+    connect to I/O controller. This is to avoid race conditions between
50703a
+    stacd and udevd. This is configurable. See "udev-rule" in stacd.conf
50703a
+    for details.
50703a
+    '''
50703a
+    udev_rule_suppress = pathlib.Path('/run/udev/rules.d', '70-nvmf-autoconnect.rules')
50703a
+    if enable:
50703a
+        try:
50703a
+            udev_rule_suppress.unlink()
50703a
+        except FileNotFoundError:
50703a
+            pass
50703a
+    else:
50703a
+        if not udev_rule_suppress.exists():
50703a
+            pathlib.Path('/run/udev/rules.d').mkdir(parents=True, exist_ok=True)
50703a
+            udev_rule_suppress.symlink_to('/dev/null')
50703a
 
50703a
-        self._cfg_soak_tmr = None
50703a
-        self._cancellable = None
50703a
-        self._resolver = None
50703a
-        self._lkc_file = None
50703a
-        self._sysbus = None
50703a
-        self._udev = None
50703a
 
50703a
-    def _config_dbus(self, iface_obj, bus_name: str, obj_name: str):
50703a
-        self._dbus_iface = iface_obj
50703a
-        self._sysbus.publish_object(obj_name, iface_obj)
50703a
-        self._sysbus.register_service(bus_name)
50703a
+# ******************************************************************************
50703a
+class Stac(Service):
50703a
+    '''STorage Appliance Connector (STAC)'''
50703a
 
50703a
-    @property
50703a
-    def tron(self):
50703a
-        '''@brief Get Trace ON property'''
50703a
-        return self._tron
50703a
+    CONF_STABILITY_LONG_SOAK_TIME_SEC = 10  # pylint: disable=invalid-name
50703a
+    ADD_EVENT_SOAK_TIME_SEC = 1
50703a
 
50703a
-    @tron.setter
50703a
-    def tron(self, value):  # pylint: disable=no-self-use
50703a
-        '''@brief Set Trace ON property'''
50703a
-        self._tron = value
50703a
-        log.set_level_from_tron(self._tron)
50703a
-        self._root.log_level("debug" if self._tron else "err")
50703a
+    def __init__(self, args, dbus):
50703a
+        super().__init__(args, self._reload_hdlr)
50703a
 
50703a
-    def run(self):
50703a
-        '''@brief Start the main loop execution'''
50703a
-        try:
50703a
-            self._loop.run()
50703a
-        except Exception as ex:  # pylint: disable=broad-except
50703a
-            logging.critical('exception: %s', ex)
50703a
+        self._udev = udev.UDEV
50703a
 
50703a
-        self._loop = None
50703a
+        self._add_event_soak_tmr = gutil.GTimer(self.ADD_EVENT_SOAK_TIME_SEC, self._on_add_event_soaked)
50703a
 
50703a
-    def info(self) -> dict:
50703a
-        '''@brief Get the status info for this object (used for debug)'''
50703a
-        nvme_options = conf.NvmeOptions()
50703a
-        return {
50703a
-            'last known config file': self._lkc_file,
50703a
-            'config soak timer': str(self._cfg_soak_tmr),
50703a
-            'kernel support': {
50703a
-                'TP8013': nvme_options.discovery_supp,
50703a
-                'host_iface': nvme_options.host_iface_supp,
50703a
-            },
50703a
-            'system config': conf.SysConf().as_dict(),
50703a
-        }
50703a
-
50703a
-    def get_controllers(self):
50703a
-        '''@brief return the list of controller objects'''
50703a
-        return self._controllers.values()
50703a
-
50703a
-    def get_controller(
50703a
-        self, transport: str, traddr: str, trsvcid: str, host_traddr: str, host_iface: str, subsysnqn: str
50703a
-    ):  # pylint: disable=too-many-arguments
50703a
-        '''@brief get the specified controller object from the list of controllers'''
50703a
-        cid = {
50703a
-            'transport': transport,
50703a
-            'traddr': traddr,
50703a
-            'trsvcid': trsvcid,
50703a
-            'host-traddr': host_traddr,
50703a
-            'host-iface': host_iface,
50703a
-            'subsysnqn': subsysnqn,
50703a
-        }
50703a
-        return self._controllers.get(trid.TID(cid))
50703a
-
50703a
-    def _remove_ctrl_from_dict(self, controller):
50703a
-        tid_to_pop = controller.tid
50703a
-        if not tid_to_pop:
50703a
-            # Being paranoid. This should not happen, but let's say the
50703a
-            # controller object has been purged, but it is somehow still
50703a
-            # listed in self._controllers.
50703a
-            for tid, _controller in self._controllers.items():
50703a
-                if _controller is controller:
50703a
-                    tid_to_pop = tid
50703a
-                    break
50703a
-
50703a
-        if tid_to_pop:
50703a
-            logging.debug('Service._remove_ctrl_from_dict()   - %s | %s', tid_to_pop, controller.device)
50703a
-            self._controllers.pop(tid_to_pop, None)
50703a
-        else:
50703a
-            logging.debug('Service._remove_ctrl_from_dict()   - already removed')
50703a
+        self._config_connections_audit()
50703a
 
50703a
-    def remove_controller(self, controller):
50703a
-        '''@brief remove the specified controller object from the list of controllers'''
50703a
-        logging.debug('Service.remove_controller()')
50703a
-        if isinstance(controller, ctrl.Controller):
50703a
-            self._remove_ctrl_from_dict(controller)
50703a
+        # Create the D-Bus instance.
50703a
+        self._config_dbus(dbus, defs.STACD_DBUS_NAME, defs.STACD_DBUS_PATH)
50703a
 
50703a
-            controller.kill()
50703a
+        # Connect to STAF D-Bus interface
50703a
+        self._staf = None
50703a
+        self._staf_watcher = dasbus.client.observer.DBusObserver(self._sysbus, defs.STAFD_DBUS_NAME)
50703a
+        self._staf_watcher.service_available.connect(self._connect_to_staf)
50703a
+        self._staf_watcher.service_unavailable.connect(self._disconnect_from_staf)
50703a
+        self._staf_watcher.connect_once_available()
50703a
 
50703a
-        if self._cfg_soak_tmr:
50703a
-            self._cfg_soak_tmr.start()
50703a
+        # Suppress udev rule to auto-connect when AEN is received.
50703a
+        udev_rule_ctrl(conf.SvcConf().udev_rule_enabled)
50703a
 
50703a
-    def _cancel(self):
50703a
-        logging.debug('Service._cancel()')
50703a
-        if not self._cancellable.is_cancelled():
50703a
-            self._cancellable.cancel()
50703a
+    def _release_resources(self):
50703a
+        logging.debug('Stac._release_resources()')
50703a
+
50703a
+        if self._add_event_soak_tmr:
50703a
+            self._add_event_soak_tmr.kill()
50703a
+
50703a
+        udev_rule_ctrl(True)
50703a
+
50703a
+        if self._udev:
50703a
+            self._udev.unregister_for_action_events('add')
50703a
+
50703a
+        self._destroy_staf_comlink(self._staf_watcher)
50703a
+        if self._staf_watcher is not None:
50703a
+            self._staf_watcher.disconnect()
50703a
 
50703a
-        for controller in self._controllers.values():
50703a
-            controller.cancel()
50703a
+        super()._release_resources()
50703a
+
50703a
+        self._udev = None
50703a
+        self._staf = None
50703a
+        self._staf_watcher = None
50703a
+        self._add_event_soak_tmr = None
50703a
+
50703a
+    def _audit_connections(self, tids):
50703a
+        '''A host should only connect to I/O controllers that have been zoned
50703a
+        for that host or a manual "controller" entry exists in stcd.conf.
50703a
+        A host should disconnect from an I/O controller when that I/O controller
50703a
+        is removed from the zone or a manual "controller" entry is removed from
50703a
+        stacd.conf. stacd will audit connections if "sticky-connections=disabled".
50703a
+        stacd will delete any connection that is not supposed to exist.
50703a
+        '''
50703a
+        logging.debug('Stac._audit_connections()          - tids = %s', tids)
50703a
+        num_controllers = len(self._controllers)
50703a
+        for tid in tids:
50703a
+            if tid not in self._controllers:
50703a
+                self._controllers[tid] = ctrl.Ioc(self, self._root, self._host, tid)
50703a
+
50703a
+        if num_controllers != len(self._controllers):
50703a
+            self._cfg_soak_tmr.start(self.CONF_STABILITY_SOAK_TIME_SEC)
50703a
+
50703a
+    def _on_add_event(self, udev_obj):  # pylint: disable=unused-argument
50703a
+        '''@brief This function is called when a "add" event is received from
50703a
+        the kernel for an NVMe device. This is used to trigger an audit and make
50703a
+        sure that the connection to an I/O controller is allowed.
50703a
+
50703a
+        WARNING: There is a race condition with the "add" event from the kernel.
50703a
+        The kernel sends the "add" event a bit early and the sysfs attributes
50703a
+        associated with the nvme object are not always fully initialized.
50703a
+        To workaround this problem we use a soaking timer to give time for the
50703a
+        sysfs attributes to stabilize.
50703a
+        '''
50703a
+        self._add_event_soak_tmr.start()
50703a
+
50703a
+    def _on_add_event_soaked(self):
50703a
+        '''@brief After the add event has been soaking for ADD_EVENT_SOAK_TIME_SEC
50703a
+        seconds, we can audit the connections.
50703a
+        '''
50703a
+        if not conf.SvcConf().sticky_connections:
50703a
+            self._audit_connections(self._udev.get_nvme_ioc_tids())
50703a
+        return GLib.SOURCE_REMOVE
50703a
+
50703a
+    def _config_connections_audit(self):
50703a
+        '''This function checks the "sticky_connections" parameter to determine
50703a
+        whether audits should be performed. Audits are enabled when
50703a
+        "sticky_connections" is disabled.
50703a
+        '''
50703a
+        if not conf.SvcConf().sticky_connections:
50703a
+            if self._udev.get_registered_action_cback('add') is None:
50703a
+                self._udev.register_for_action_events('add', self._on_add_event)
50703a
+                self._audit_connections(self._udev.get_nvme_ioc_tids())
50703a
+        else:
50703a
+            self._udev.unregister_for_action_events('add')
50703a
 
50703a
     def _keep_connections_on_exit(self):
50703a
         '''@brief Determine whether connections should remain when the
50703a
         process exits.
50703a
-
50703a
-        NOTE) This is the base class method used to define the interface.
50703a
-        It must be overloaded by a child class.
50703a
         '''
50703a
-        raise NotImplementedError()
50703a
+        return True
50703a
 
50703a
-    def _stop_hdlr(self):
50703a
-        systemd.daemon.notify('STOPPING=1')
50703a
+    def _reload_hdlr(self):
50703a
+        '''@brief Reload configuration file. This is triggered by the SIGHUP
50703a
+        signal, which can be sent with "systemctl reload stacd".
50703a
+        '''
50703a
+        systemd.daemon.notify('RELOADING=1')
50703a
+        service_cnf = conf.SvcConf()
50703a
+        service_cnf.reload()
50703a
+        self.tron = service_cnf.tron
50703a
+        self._config_connections_audit()
50703a
+        self._cfg_soak_tmr.start(self.CONF_STABILITY_SOAK_TIME_SEC)
50703a
+        udev_rule_ctrl(service_cnf.udev_rule_enabled)
50703a
+        systemd.daemon.notify('READY=1')
50703a
+        return GLib.SOURCE_CONTINUE
50703a
+
50703a
+    def _get_log_pages_from_stafd(self):
50703a
+        if self._staf:
50703a
+            try:
50703a
+                return json.loads(self._staf.get_all_log_pages(True))
50703a
+            except dasbus.error.DBusError:
50703a
+                pass
50703a
+
50703a
+        return list()
50703a
 
50703a
-        self._cancel()  # Cancel pending operations
50703a
+    def _config_ctrls_finish(self, configured_ctrl_list):
50703a
+        configured_ctrl_list = [
50703a
+            ctrl_dict for ctrl_dict in configured_ctrl_list if 'traddr' in ctrl_dict and 'subsysnqn' in ctrl_dict
50703a
+        ]
50703a
+        logging.debug('Stac._config_ctrls_finish()        - configured_ctrl_list = %s', configured_ctrl_list)
50703a
+
50703a
+        discovered_ctrl_list = list()
50703a
+        for staf_data in self._get_log_pages_from_stafd():
50703a
+            host_traddr = staf_data['discovery-controller']['host-traddr']
50703a
+            host_iface = staf_data['discovery-controller']['host-iface']
50703a
+            for dlpe in staf_data['log-pages']:
50703a
+                if dlpe.get('subtype') == 'nvme':  # eliminate discovery controllers
50703a
+                    discovered_ctrl_list.append(stas.cid_from_dlpe(dlpe, host_traddr, host_iface))
50703a
+
50703a
+        logging.debug('Stac._config_ctrls_finish()        - discovered_ctrl_list = %s', discovered_ctrl_list)
50703a
+
50703a
+        controllers = stas.remove_blacklisted(configured_ctrl_list + discovered_ctrl_list)
50703a
+        controllers = stas.remove_invalid_addresses(controllers)
50703a
+
50703a
+        new_controller_ids = {trid.TID(controller) for controller in controllers}
50703a
+        cur_controller_ids = set(self._controllers.keys())
50703a
+        controllers_to_add = new_controller_ids - cur_controller_ids
50703a
+        controllers_to_del = cur_controller_ids - new_controller_ids
50703a
+
50703a
+        logging.debug('Stac._config_ctrls_finish()        - controllers_to_add   = %s', list(controllers_to_add))
50703a
+        logging.debug('Stac._config_ctrls_finish()        - controllers_to_del   = %s', list(controllers_to_del))
50703a
+
50703a
+        for tid in controllers_to_del:
50703a
+            controller = self._controllers.pop(tid, None)
50703a
+            if controller is not None:
50703a
+                controller.disconnect(self.remove_controller, conf.SvcConf().sticky_connections)
50703a
+
50703a
+        for tid in controllers_to_add:
50703a
+            self._controllers[tid] = ctrl.Ioc(self, self._root, self._host, tid)
50703a
+
50703a
+    def _connect_to_staf(self, _):
50703a
+        '''@brief Hook up DBus signal handlers for signals from stafd.'''
50703a
+        try:
50703a
+            self._staf = self._sysbus.get_proxy(defs.STAFD_DBUS_NAME, defs.STAFD_DBUS_PATH)
50703a
+            self._staf.log_pages_changed.connect(self._log_pages_changed)
50703a
+            self._cfg_soak_tmr.start()
50703a
 
50703a
-        self._dump_last_known_config(self._controllers)
50703a
+            # Make sure timer is set back to its normal value.
50703a
+            self._cfg_soak_tmr.set_timeout(self.CONF_STABILITY_SOAK_TIME_SEC)
50703a
+            logging.debug('Stac._connect_to_staf()            - Connected to staf')
50703a
+        except dasbus.error.DBusError:
50703a
+            logging.error('Failed to connect to staf')
50703a
+
50703a
+    def _destroy_staf_comlink(self, watcher):  # pylint: disable=unused-argument
50703a
+        if self._staf:
50703a
+            self._staf.log_pages_changed.disconnect(self._log_pages_changed)
50703a
+            dasbus.client.proxy.disconnect_proxy(self._staf)
50703a
+            self._staf = None
50703a
+
50703a
+    def _disconnect_from_staf(self, watcher):
50703a
+        self._destroy_staf_comlink(watcher)
50703a
+
50703a
+        # When we lose connectivity with stafd, the most logical explanation
50703a
+        # is that stafd restarted. In that case, it may take some time for stafd
50703a
+        # to re-populate its log pages cache. So let's give stafd plenty of time
50703a
+        # to update its log pages cache and send log pages change notifications
50703a
+        # before triggering a stacd re-config. We do this by momentarily
50703a
+        # increasing the config soak timer to a longer period.
50703a
+        if self._cfg_soak_tmr:
50703a
+            self._cfg_soak_tmr.set_timeout(self.CONF_STABILITY_LONG_SOAK_TIME_SEC)
50703a
+
50703a
+        logging.debug('Stac._disconnect_from_staf()       - Disconnected from staf')
50703a
+
50703a
+    def _log_pages_changed(  # pylint: disable=too-many-arguments
50703a
+        self, transport, traddr, trsvcid, host_traddr, host_iface, subsysnqn, device
50703a
+    ):
50703a
+        logging.debug(
50703a
+            'Stac._log_pages_changed()          - transport=%s, traddr=%s, trsvcid=%s, host_traddr=%s, host_iface=%s, subsysnqn=%s, device=%s',
50703a
+            transport,
50703a
+            traddr,
50703a
+            trsvcid,
50703a
+            host_traddr,
50703a
+            host_iface,
50703a
+            subsysnqn,
50703a
+            device,
50703a
+        )
50703a
+        if self._cfg_soak_tmr:
50703a
+            self._cfg_soak_tmr.start(self.CONF_STABILITY_SOAK_TIME_SEC)
50703a
 
50703a
-        if len(self._controllers) == 0:
50703a
-            GLib.idle_add(self._exit)
50703a
-        else:
50703a
-            # Tell all controller objects to disconnect
50703a
-            keep_connections = self._keep_connections_on_exit()
50703a
-            controllers = self._controllers.values()
50703a
-            for controller in controllers:
50703a
-                controller.disconnect(self._on_final_disconnect, keep_connections)
50703a
+    def _load_last_known_config(self):
50703a
+        return dict()
50703a
 
50703a
-        return GLib.SOURCE_REMOVE
50703a
+    def _dump_last_known_config(self, controllers):
50703a
+        pass
50703a
 
50703a
-    def _on_final_disconnect(self, controller):
50703a
-        '''Callback invoked after a controller is disconnected.
50703a
-        THIS IS USED DURING PROCESS SHUTDOWN TO WAIT FOR ALL CONTROLLERS TO BE
50703a
-        DISCONNECTED BEFORE EXITING THE PROGRAM. ONLY CALL ON SHUTDOWN!
50703a
-        '''
50703a
-        logging.debug('Service._on_final_disconnect()')
50703a
-        self._remove_ctrl_from_dict(controller)
50703a
 
50703a
-        controller.kill()
50703a
+# ******************************************************************************
50703a
+class Staf(Service):
50703a
+    '''STorage Appliance Finder (STAF)'''
50703a
 
50703a
-        # When all controllers have disconnected, we can finish the clean up
50703a
-        if len(self._controllers) == 0:
50703a
-            # Defer exit to the next main loop's idle period.
50703a
-            GLib.idle_add(self._exit)
50703a
+    def __init__(self, args, dbus):
50703a
+        super().__init__(args, self._reload_hdlr)
50703a
 
50703a
-    def _exit(self):
50703a
-        logging.debug('Service._exit()')
50703a
-        self._release_resources()
50703a
-        self._loop.quit()
50703a
+        self._avahi = avahi.Avahi(self._sysbus, self._avahi_change)
50703a
+        self._avahi.config_stypes(conf.SvcConf().get_stypes())
50703a
 
50703a
-    def _on_config_ctrls(self, *_user_data):
50703a
-        self._config_ctrls()
50703a
-        return GLib.SOURCE_REMOVE
50703a
+        # Create the D-Bus instance.
50703a
+        self._config_dbus(dbus, defs.STAFD_DBUS_NAME, defs.STAFD_DBUS_PATH)
50703a
 
50703a
-    def _config_ctrls(self):
50703a
-        '''@brief Start controllers configuration.'''
50703a
-        # The configuration file may contain controllers and/or blacklist
50703a
-        # elements with traddr specified as hostname instead of IP address.
50703a
-        # Because of this, we need to remove those blacklisted elements before
50703a
-        # running name resolution. And we will need to remove blacklisted
50703a
-        # elements after name resolution is complete (i.e. in the calback
50703a
-        # function _config_ctrls_finish)
50703a
-        logging.debug('Service._config_ctrls()')
50703a
-        configured_controllers = stas.remove_blacklisted(conf.SvcConf().get_controllers())
50703a
-        self._resolver.resolve_ctrl_async(self._cancellable, configured_controllers, self._config_ctrls_finish)
50703a
+    def info(self) -> dict:
50703a
+        '''@brief Get the status info for this object (used for debug)'''
50703a
+        info = super().info()
50703a
+        info['avahi'] = self._avahi.info()
50703a
+        return info
50703a
 
50703a
-    def _config_ctrls_finish(self, configured_ctrl_list):
50703a
-        '''@brief Finish controllers configuration after hostnames (if any)
50703a
-        have been resolved.
50703a
-
50703a
-        Configuring controllers must be done asynchronously in 2 steps.
50703a
-        In the first step, host names get resolved to find their IP addresses.
50703a
-        Name resolution can take a while, especially when an external name
50703a
-        resolution server is used. Once that step completed, the callback
50703a
-        method _config_ctrls_finish() (i.e. this method), gets invoked to
50703a
-        complete the controller configuration.
50703a
-
50703a
-        NOTE) This is the base class method used to define the interface.
50703a
-        It must be overloaded by a child class.
50703a
-        '''
50703a
-        raise NotImplementedError()
50703a
+    def _release_resources(self):
50703a
+        logging.debug('Staf._release_resources()')
50703a
+        super()._release_resources()
50703a
+        if self._avahi:
50703a
+            self._avahi.kill()
50703a
+            self._avahi = None
50703a
 
50703a
     def _load_last_known_config(self):
50703a
-        raise NotImplementedError()
50703a
+        try:
50703a
+            with open(self._lkc_file, 'rb') as file:
50703a
+                config = pickle.load(file)
50703a
+        except (FileNotFoundError, AttributeError):
50703a
+            return dict()
50703a
+
50703a
+        logging.debug('Staf._load_last_known_config()     - DC count = %s', len(config))
50703a
+        return {tid: ctrl.Dc(self, self._root, self._host, tid, log_pages) for tid, log_pages in config.items()}
50703a
 
50703a
     def _dump_last_known_config(self, controllers):
50703a
-        raise NotImplementedError()
50703a
+        try:
50703a
+            with open(self._lkc_file, 'wb') as file:
50703a
+                config = {tid: dc.log_pages() for tid, dc in controllers.items()}
50703a
+                logging.debug('Staf._dump_last_known_config()     - DC count = %s', len(config))
50703a
+                pickle.dump(config, file)
50703a
+        except FileNotFoundError as ex:
50703a
+            logging.error('Unable to save last known config: %s', ex)
50703a
+
50703a
+    def _keep_connections_on_exit(self):
50703a
+        '''@brief Determine whether connections should remain when the
50703a
+        process exits.
50703a
+        '''
50703a
+        return conf.SvcConf().persistent_connections
50703a
+
50703a
+    def _reload_hdlr(self):
50703a
+        '''@brief Reload configuration file. This is triggered by the SIGHUP
50703a
+        signal, which can be sent with "systemctl reload stafd".
50703a
+        '''
50703a
+        systemd.daemon.notify('RELOADING=1')
50703a
+        service_cnf = conf.SvcConf()
50703a
+        service_cnf.reload()
50703a
+        self.tron = service_cnf.tron
50703a
+        self._avahi.kick_start()  # Make sure Avahi is running
50703a
+        self._avahi.config_stypes(service_cnf.get_stypes())
50703a
+        self._cfg_soak_tmr.start()
50703a
+        systemd.daemon.notify('READY=1')
50703a
+        return GLib.SOURCE_CONTINUE
50703a
+
50703a
+    def log_pages_changed(self, controller, device):
50703a
+        '''@brief Function invoked when a controller's cached log pages
50703a
+        have changed. This will emit a D-Bus signal to inform
50703a
+        other applications that the cached log pages have changed.
50703a
+        '''
50703a
+        self._dbus_iface.log_pages_changed.emit(
50703a
+            controller.tid.transport,
50703a
+            controller.tid.traddr,
50703a
+            controller.tid.trsvcid,
50703a
+            controller.tid.host_traddr,
50703a
+            controller.tid.host_iface,
50703a
+            controller.tid.subsysnqn,
50703a
+            device,
50703a
+        )
50703a
+
50703a
+    def referrals_changed(self):
50703a
+        '''@brief Function invoked when a controller's cached referrals
50703a
+        have changed.
50703a
+        '''
50703a
+        logging.debug('Staf.referrals_changed()')
50703a
+        self._cfg_soak_tmr.start()
50703a
+
50703a
+    def _referrals(self) -> list:
50703a
+        return [
50703a
+            stas.cid_from_dlpe(dlpe, controller.tid.host_traddr, controller.tid.host_iface)
50703a
+            for controller in self.get_controllers()
50703a
+            for dlpe in controller.referrals()
50703a
+        ]
50703a
+
50703a
+    def _config_ctrls_finish(self, configured_ctrl_list):
50703a
+        '''@brief Finish discovery controllers configuration after
50703a
+        hostnames (if any) have been resolved.
50703a
+        '''
50703a
+        configured_ctrl_list = [
50703a
+            ctrl_dict
50703a
+            for ctrl_dict in configured_ctrl_list
50703a
+            if 'traddr' in ctrl_dict and ctrl_dict.setdefault('subsysnqn', defs.WELL_KNOWN_DISC_NQN)
50703a
+        ]
50703a
+
50703a
+        discovered_ctrl_list = self._avahi.get_controllers()
50703a
+        referral_ctrl_list = self._referrals()
50703a
+        logging.debug('Staf._config_ctrls_finish()        - configured_ctrl_list = %s', configured_ctrl_list)
50703a
+        logging.debug('Staf._config_ctrls_finish()        - discovered_ctrl_list = %s', discovered_ctrl_list)
50703a
+        logging.debug('Staf._config_ctrls_finish()        - referral_ctrl_list   = %s', referral_ctrl_list)
50703a
+
50703a
+        controllers = stas.remove_blacklisted(configured_ctrl_list + discovered_ctrl_list + referral_ctrl_list)
50703a
+        controllers = stas.remove_invalid_addresses(controllers)
50703a
+
50703a
+        new_controller_ids = {trid.TID(controller) for controller in controllers}
50703a
+        cur_controller_ids = set(self._controllers.keys())
50703a
+        controllers_to_add = new_controller_ids - cur_controller_ids
50703a
+        controllers_to_del = cur_controller_ids - new_controller_ids
50703a
+
50703a
+        logging.debug('Staf._config_ctrls_finish()        - controllers_to_add   = %s', list(controllers_to_add))
50703a
+        logging.debug('Staf._config_ctrls_finish()        - controllers_to_del   = %s', list(controllers_to_del))
50703a
+
50703a
+        for tid in controllers_to_del:
50703a
+            controller = self._controllers.pop(tid, None)
50703a
+            if controller is not None:
50703a
+                controller.disconnect(self.remove_controller, conf.SvcConf().persistent_connections)
50703a
+
50703a
+        for tid in controllers_to_add:
50703a
+            self._controllers[tid] = ctrl.Dc(self, self._root, self._host, tid)
50703a
+
50703a
+    def _avahi_change(self):
50703a
+        self._cfg_soak_tmr.start()
50703a
diff --git a/staslib/stas.py b/staslib/stas.py
50703a
index 7bf91e0..496f063 100644
50703a
--- a/staslib/stas.py
50703a
+++ b/staslib/stas.py
50703a
@@ -6,14 +6,19 @@
50703a
 #
50703a
 # Authors: Martin Belanger <Martin.Belanger@dell.com>
50703a
 #
50703a
-'''Library for staf/stac'''
50703a
+'''Library for staf/stac. You will find here common code for stafd and stacd
50703a
+including the Abstract Base Classes (ABC) for Controllers and Services'''
50703a
 
50703a
 import os
50703a
 import sys
50703a
-import ipaddress
50703a
+import abc
50703a
+import signal
50703a
 import logging
50703a
-
50703a
-from staslib import conf, defs, trid
50703a
+import ipaddress
50703a
+import systemd.daemon
50703a
+import dasbus.connection
50703a
+from gi.repository import Gio, GLib
50703a
+from staslib import conf, defs, gutil, log, trid
50703a
 
50703a
 
50703a
 # ******************************************************************************
50703a
@@ -108,3 +113,379 @@ def remove_invalid_addresses(controllers: list):
50703a
             logging.warning('Invalid transport %s', transport)
50703a
 
50703a
     return valid_controllers
50703a
+
50703a
+
50703a
+# ******************************************************************************
50703a
+class ControllerABC(abc.ABC):  # pylint: disable=too-many-instance-attributes
50703a
+    '''@brief Base class used to manage the connection to a controller.'''
50703a
+
50703a
+    CONNECT_RETRY_PERIOD_SEC = 60
50703a
+    FAST_CONNECT_RETRY_PERIOD_SEC = 3
50703a
+
50703a
+    def __init__(self, root, host, tid: trid.TID, discovery_ctrl=False):
50703a
+        self._root              = root
50703a
+        self._host              = host
50703a
+        self._tid               = tid
50703a
+        self._cancellable       = Gio.Cancellable()
50703a
+        self._connect_attempts  = 0
50703a
+        self._retry_connect_tmr = gutil.GTimer(self.CONNECT_RETRY_PERIOD_SEC, self._on_try_to_connect)
50703a
+        self._discovery_ctrl    = discovery_ctrl
50703a
+        self._try_to_connect_deferred = gutil.Deferred(self._try_to_connect)
50703a
+        self._try_to_connect_deferred.schedule()
50703a
+
50703a
+    def _release_resources(self):
50703a
+        # Remove pending deferred from main loop
50703a
+        if self._try_to_connect_deferred:
50703a
+            self._try_to_connect_deferred.cancel()
50703a
+
50703a
+        if self._retry_connect_tmr is not None:
50703a
+            self._retry_connect_tmr.kill()
50703a
+
50703a
+        if self._cancellable and not self._cancellable.is_cancelled():
50703a
+            self._cancellable.cancel()
50703a
+
50703a
+        self._tid = None
50703a
+        self._cancellable = None
50703a
+        self._retry_connect_tmr = None
50703a
+        self._try_to_connect_deferred = None
50703a
+
50703a
+    @property
50703a
+    def id(self) -> str:
50703a
+        '''@brief Return the Transport ID as a printable string'''
50703a
+        return str(self.tid)
50703a
+
50703a
+    @property
50703a
+    def tid(self):
50703a
+        '''@brief Return the Transport ID object'''
50703a
+        return self._tid
50703a
+
50703a
+    def controller_id_dict(self) -> dict:
50703a
+        '''@brief return the controller ID as a dict.'''
50703a
+        return self.tid.as_dict()
50703a
+
50703a
+    def details(self) -> dict:
50703a
+        '''@brief return detailed debug info about this controller'''
50703a
+        details = self.controller_id_dict()
50703a
+        details['connect attempts'] = str(self._connect_attempts)
50703a
+        details['retry connect timer'] = str(self._retry_connect_tmr)
50703a
+        return details
50703a
+
50703a
+    def info(self) -> dict:
50703a
+        '''@brief Get the controller info for this object'''
50703a
+        return self.details()
50703a
+
50703a
+    def cancel(self):
50703a
+        '''@brief Used to cancel pending operations.'''
50703a
+        if self._cancellable and not self._cancellable.is_cancelled():
50703a
+            logging.debug('ControllerABC.cancel()             - %s', self.id)
50703a
+            self._cancellable.cancel()
50703a
+
50703a
+    def kill(self):
50703a
+        '''@brief Used to release all resources associated with this object.'''
50703a
+        logging.debug('ControllerABC.kill()               - %s', self.id)
50703a
+        self._release_resources()
50703a
+
50703a
+    def _alive(self):
50703a
+        '''There may be race condition where a queued event gets processed
50703a
+        after the object is no longer configured (i.e. alive). This method
50703a
+        can be used by callback functions to make sure the object is still
50703a
+        alive before processing further.
50703a
+        '''
50703a
+        return self._cancellable and not self._cancellable.is_cancelled()
50703a
+
50703a
+    def _on_try_to_connect(self):
50703a
+        self._try_to_connect_deferred.schedule()
50703a
+        return GLib.SOURCE_REMOVE
50703a
+
50703a
+    def _try_to_connect(self):
50703a
+        # This is a deferred function call. Make sure
50703a
+        # the source of the deferred is still good.
50703a
+        source = GLib.main_current_source()
50703a
+        if source and source.is_destroyed():
50703a
+            return
50703a
+
50703a
+        self._connect_attempts += 1
50703a
+
50703a
+        self._do_connect()
50703a
+
50703a
+    @abc.abstractmethod
50703a
+    def _do_connect(self):
50703a
+        raise NotImplementedError()
50703a
+
50703a
+    @abc.abstractmethod
50703a
+    def _on_aen(self, aen: int):
50703a
+        raise NotImplementedError()
50703a
+
50703a
+    @abc.abstractmethod
50703a
+    def _on_nvme_event(self, nvme_event):
50703a
+        raise NotImplementedError()
50703a
+
50703a
+    @abc.abstractmethod
50703a
+    def _on_ctrl_removed(self, obj):
50703a
+        raise NotImplementedError()
50703a
+
50703a
+    @abc.abstractmethod
50703a
+    def _find_existing_connection(self):
50703a
+        raise NotImplementedError()
50703a
+
50703a
+    @abc.abstractmethod
50703a
+    def disconnect(self, disconnected_cb, keep_connection):
50703a
+        '''@brief Issue an asynchronous disconnect command to a Controller.
50703a
+        Once the async command has completed, the callback 'disconnected_cb'
50703a
+        will be invoked. If a controller is already disconnected, then the
50703a
+        callback will be added to the main loop's next idle slot to be executed
50703a
+        ASAP.
50703a
+        '''
50703a
+        raise NotImplementedError()
50703a
+
50703a
+
50703a
+# ******************************************************************************
50703a
+class ServiceABC(abc.ABC):  # pylint: disable=too-many-instance-attributes
50703a
+    '''@brief Base class used to manage a STorage Appliance Service'''
50703a
+
50703a
+    CONF_STABILITY_SOAK_TIME_SEC = 1.5
50703a
+
50703a
+    def __init__(self, args, reload_hdlr):
50703a
+
50703a
+        service_conf = conf.SvcConf()
50703a
+        service_conf.set_conf_file(args.conf_file)  # reload configuration
50703a
+        self._tron = args.tron or service_conf.tron
50703a
+        log.set_level_from_tron(self._tron)
50703a
+
50703a
+        self._lkc_file     = os.path.join(os.environ.get('RUNTIME_DIRECTORY', os.path.join('/run', defs.PROG_NAME)), 'last-known-config.pickle')
50703a
+        self._loop         = GLib.MainLoop()
50703a
+        self._cancellable  = Gio.Cancellable()
50703a
+        self._resolver     = gutil.NameResolver()
50703a
+        self._controllers  = self._load_last_known_config()
50703a
+        self._dbus_iface   = None
50703a
+        self._cfg_soak_tmr = gutil.GTimer(self.CONF_STABILITY_SOAK_TIME_SEC, self._on_config_ctrls)
50703a
+        self._sysbus       = dasbus.connection.SystemMessageBus()
50703a
+
50703a
+        GLib.unix_signal_add(GLib.PRIORITY_HIGH, signal.SIGINT, self._stop_hdlr)  # CTRL-C
50703a
+        GLib.unix_signal_add(GLib.PRIORITY_HIGH, signal.SIGTERM, self._stop_hdlr)  # systemctl stop stafd
50703a
+        GLib.unix_signal_add(GLib.PRIORITY_HIGH, signal.SIGHUP, reload_hdlr)  # systemctl reload stafd
50703a
+
50703a
+        nvme_options = conf.NvmeOptions()
50703a
+        if not nvme_options.host_iface_supp or not nvme_options.discovery_supp:
50703a
+            logging.warning(
50703a
+                'Kernel does not appear to support all the options needed to run this program. Consider updating to a later kernel version.'
50703a
+            )
50703a
+
50703a
+        # We don't want to apply configuration changes to nvme-cli right away.
50703a
+        # Often, multiple changes will occur in a short amount of time (sub-second).
50703a
+        # We want to wait until there are no more changes before applying them
50703a
+        # to the system. The following timer acts as a "soak period". Changes
50703a
+        # will be applied by calling self._on_config_ctrls() at the end of
50703a
+        # the soak period.
50703a
+        self._cfg_soak_tmr.start()
50703a
+
50703a
+    def _release_resources(self):
50703a
+        logging.debug('ServiceABC._release_resources()')
50703a
+
50703a
+        if self._cancellable and not self._cancellable.is_cancelled():
50703a
+            self._cancellable.cancel()
50703a
+
50703a
+        if self._cfg_soak_tmr is not None:
50703a
+            self._cfg_soak_tmr.kill()
50703a
+
50703a
+        self._controllers.clear()
50703a
+
50703a
+        if self._sysbus:
50703a
+            self._sysbus.disconnect()
50703a
+
50703a
+        self._cfg_soak_tmr = None
50703a
+        self._cancellable = None
50703a
+        self._resolver = None
50703a
+        self._lkc_file = None
50703a
+        self._sysbus = None
50703a
+
50703a
+    def _config_dbus(self, iface_obj, bus_name: str, obj_name: str):
50703a
+        self._dbus_iface = iface_obj
50703a
+        self._sysbus.publish_object(obj_name, iface_obj)
50703a
+        self._sysbus.register_service(bus_name)
50703a
+
50703a
+    @property
50703a
+    def tron(self):
50703a
+        '''@brief Get Trace ON property'''
50703a
+        return self._tron
50703a
+
50703a
+    @tron.setter
50703a
+    def tron(self, value):
50703a
+        '''@brief Set Trace ON property'''
50703a
+        self._tron = value
50703a
+        log.set_level_from_tron(self._tron)
50703a
+
50703a
+    def run(self):
50703a
+        '''@brief Start the main loop execution'''
50703a
+        try:
50703a
+            self._loop.run()
50703a
+        except Exception as ex:  # pylint: disable=broad-except
50703a
+            logging.critical('exception: %s', ex)
50703a
+
50703a
+        self._loop = None
50703a
+
50703a
+    def info(self) -> dict:
50703a
+        '''@brief Get the status info for this object (used for debug)'''
50703a
+        nvme_options = conf.NvmeOptions()
50703a
+        return {
50703a
+            'last known config file': self._lkc_file,
50703a
+            'config soak timer': str(self._cfg_soak_tmr),
50703a
+            'kernel support': {
50703a
+                'TP8013': nvme_options.discovery_supp,
50703a
+                'host_iface': nvme_options.host_iface_supp,
50703a
+            },
50703a
+            'system config': conf.SysConf().as_dict(),
50703a
+        }
50703a
+
50703a
+    def get_controllers(self) -> dict:
50703a
+        '''@brief return the list of controller objects'''
50703a
+        return self._controllers.values()
50703a
+
50703a
+    def get_controller(
50703a
+        self, transport: str, traddr: str, trsvcid: str, host_traddr: str, host_iface: str, subsysnqn: str
50703a
+    ):  # pylint: disable=too-many-arguments
50703a
+        '''@brief get the specified controller object from the list of controllers'''
50703a
+        cid = {
50703a
+            'transport': transport,
50703a
+            'traddr': traddr,
50703a
+            'trsvcid': trsvcid,
50703a
+            'host-traddr': host_traddr,
50703a
+            'host-iface': host_iface,
50703a
+            'subsysnqn': subsysnqn,
50703a
+        }
50703a
+        return self._controllers.get(trid.TID(cid))
50703a
+
50703a
+    def _remove_ctrl_from_dict(self, controller):
50703a
+        tid_to_pop = controller.tid
50703a
+        if not tid_to_pop:
50703a
+            # Being paranoid. This should not happen, but let's say the
50703a
+            # controller object has been purged, but it is somehow still
50703a
+            # listed in self._controllers.
50703a
+            for tid, _controller in self._controllers.items():
50703a
+                if _controller is controller:
50703a
+                    tid_to_pop = tid
50703a
+                    break
50703a
+
50703a
+        if tid_to_pop:
50703a
+            logging.debug('ServiceABC._remove_ctrl_from_dict()- %s | %s', tid_to_pop, controller.device)
50703a
+            self._controllers.pop(tid_to_pop, None)
50703a
+        else:
50703a
+            logging.debug('ServiceABC._remove_ctrl_from_dict()- already removed')
50703a
+
50703a
+    def remove_controller(self, controller, success):  # pylint: disable=unused-argument
50703a
+        '''@brief remove the specified controller object from the list of controllers
50703a
+        @param controller: the controller object
50703a
+        @param success: whether the disconnect was successful'''
50703a
+        logging.debug('ServiceABC.remove_controller()')
50703a
+        if isinstance(controller, ControllerABC):
50703a
+            self._remove_ctrl_from_dict(controller)
50703a
+
50703a
+            controller.kill()
50703a
+
50703a
+        if self._cfg_soak_tmr:
50703a
+            self._cfg_soak_tmr.start()
50703a
+
50703a
+    def _cancel(self):
50703a
+        logging.debug('ServiceABC._cancel()')
50703a
+        if not self._cancellable.is_cancelled():
50703a
+            self._cancellable.cancel()
50703a
+
50703a
+        for controller in self._controllers.values():
50703a
+            controller.cancel()
50703a
+
50703a
+    def _stop_hdlr(self):
50703a
+        logging.debug('ServiceABC._stop_hdlr()')
50703a
+        systemd.daemon.notify('STOPPING=1')
50703a
+
50703a
+        self._cancel()  # Cancel pending operations
50703a
+
50703a
+        self._dump_last_known_config(self._controllers)
50703a
+
50703a
+        if len(self._controllers) == 0:
50703a
+            GLib.idle_add(self._exit)
50703a
+        else:
50703a
+            # Tell all controller objects to disconnect
50703a
+            keep_connections = self._keep_connections_on_exit()
50703a
+            controllers = self._controllers.values()
50703a
+            logging.debug(
50703a
+                'ServiceABC._stop_hdlr()            - Controller count = %s, keep_connections = %s',
50703a
+                len(controllers), keep_connections
50703a
+            )
50703a
+            for controller in controllers:
50703a
+                controller.disconnect(self._on_final_disconnect, keep_connections)
50703a
+
50703a
+        return GLib.SOURCE_REMOVE
50703a
+
50703a
+    def _on_final_disconnect(self, controller, success):
50703a
+        '''Callback invoked after a controller is disconnected.
50703a
+        THIS IS USED DURING PROCESS SHUTDOWN TO WAIT FOR ALL CONTROLLERS TO BE
50703a
+        DISCONNECTED BEFORE EXITING THE PROGRAM. ONLY CALL ON SHUTDOWN!
50703a
+        @param controller: the controller object
50703a
+        @param success: whether the disconnect operation was successful
50703a
+        '''
50703a
+        logging.debug('ServiceABC._on_final_disconnect()  - %s | %s disconnect %s',
50703a
+                      controller.id, controller.device, 'succeeded' if success else 'failed')
50703a
+        self._remove_ctrl_from_dict(controller)
50703a
+
50703a
+        controller.kill()
50703a
+
50703a
+        # When all controllers have disconnected, we can finish the clean up
50703a
+        if len(self._controllers) == 0:
50703a
+            # Defer exit to the next main loop's idle period.
50703a
+            GLib.idle_add(self._exit)
50703a
+
50703a
+    def _exit(self):
50703a
+        logging.debug('ServiceABC._exit()')
50703a
+        self._release_resources()
50703a
+        self._loop.quit()
50703a
+
50703a
+    def _on_config_ctrls(self, *_user_data):
50703a
+        self._config_ctrls()
50703a
+        return GLib.SOURCE_REMOVE
50703a
+
50703a
+    def _config_ctrls(self):
50703a
+        '''@brief Start controllers configuration.'''
50703a
+        # The configuration file may contain controllers and/or blacklist
50703a
+        # elements with traddr specified as hostname instead of IP address.
50703a
+        # Because of this, we need to remove those blacklisted elements before
50703a
+        # running name resolution. And we will need to remove blacklisted
50703a
+        # elements after name resolution is complete (i.e. in the calback
50703a
+        # function _config_ctrls_finish)
50703a
+        logging.debug('ServiceABC._config_ctrls()')
50703a
+        configured_controllers = remove_blacklisted(conf.SvcConf().get_controllers())
50703a
+        self._resolver.resolve_ctrl_async(self._cancellable, configured_controllers, self._config_ctrls_finish)
50703a
+
50703a
+    @abc.abstractmethod
50703a
+    def _keep_connections_on_exit(self):
50703a
+        '''@brief Determine whether connections should remain when the
50703a
+        process exits.
50703a
+
50703a
+        NOTE) This is the base class method used to define the interface.
50703a
+        It must be overloaded by a child class.
50703a
+        '''
50703a
+        raise NotImplementedError()
50703a
+
50703a
+    @abc.abstractmethod
50703a
+    def _config_ctrls_finish(self, configured_ctrl_list):
50703a
+        '''@brief Finish controllers configuration after hostnames (if any)
50703a
+        have been resolved.
50703a
+
50703a
+        Configuring controllers must be done asynchronously in 2 steps.
50703a
+        In the first step, host names get resolved to find their IP addresses.
50703a
+        Name resolution can take a while, especially when an external name
50703a
+        resolution server is used. Once that step completed, the callback
50703a
+        method _config_ctrls_finish() (i.e. this method), gets invoked to
50703a
+        complete the controller configuration.
50703a
+
50703a
+        NOTE) This is the base class method used to define the interface.
50703a
+        It must be overloaded by a child class.
50703a
+        '''
50703a
+        raise NotImplementedError()
50703a
+
50703a
+    @abc.abstractmethod
50703a
+    def _load_last_known_config(self):
50703a
+        raise NotImplementedError()
50703a
+
50703a
+    @abc.abstractmethod
50703a
+    def _dump_last_known_config(self, controllers):
50703a
+        raise NotImplementedError()
50703a
diff --git a/staslib/trid.py b/staslib/trid.py
50703a
index def6ab2..38619e7 100644
50703a
--- a/staslib/trid.py
50703a
+++ b/staslib/trid.py
50703a
@@ -12,8 +12,7 @@ throughout nvme-stas to uniquely identify a Controller'''
50703a
 import hashlib
50703a
 from staslib import conf
50703a
 
50703a
-class TID:
50703a
-    # pylint: disable=too-many-instance-attributes
50703a
+class TID:  # pylint: disable=too-many-instance-attributes
50703a
     '''Transport Identifier'''
50703a
     RDMA_IP_PORT = '4420'
50703a
     DISC_IP_PORT = '8009'
50703a
diff --git a/staslib/udev.py b/staslib/udev.py
50703a
index 29370b8..37b63cc 100644
50703a
--- a/staslib/udev.py
50703a
+++ b/staslib/udev.py
50703a
@@ -16,7 +16,7 @@ from staslib import defs, trid
50703a
 try:
50703a
     from pyudev.glib import MonitorObserver
50703a
 except (ModuleNotFoundError, AttributeError):
50703a
-    from staslib.glibudev import MonitorObserver  # pylint: disable=relative-beyond-top-level,ungrouped-imports
50703a
+    from staslib.glibudev import MonitorObserver  # pylint: disable=ungrouped-imports
50703a
 
50703a
 # ******************************************************************************
50703a
 class Udev:
50703a
@@ -99,7 +99,7 @@ class Udev:
50703a
     def get_attributes(self, sys_name: str, attr_ids) -> dict:
50703a
         '''@brief Get all the attributes associated with device @sys_name'''
50703a
         attrs = {attr_id: '' for attr_id in attr_ids}
50703a
-        if sys_name:
50703a
+        if sys_name and sys_name != 'nvme?':
50703a
             udev = self.get_nvme_device(sys_name)
50703a
             if udev is not None:
50703a
                 for attr_id in attr_ids:
50703a
diff --git a/test/test-config.py b/test/test-config.py
50703a
index dad0ebd..db58883 100755
50703a
--- a/test/test-config.py
50703a
+++ b/test/test-config.py
50703a
@@ -40,7 +40,7 @@ class StasProcessConfUnitTest(unittest.TestCase):
50703a
         self.assertFalse(service_conf.data_digest)
50703a
         self.assertTrue(service_conf.persistent_connections)
50703a
         self.assertTrue(service_conf.udev_rule_enabled)
50703a
-        self.assertFalse(service_conf.sticky_connections)
50703a
+        self.assertTrue(service_conf.sticky_connections)
50703a
         self.assertFalse(service_conf.ignore_iface)
50703a
         self.assertIn(6, service_conf.ip_family)
50703a
         self.assertNotIn(4, service_conf.ip_family)
50703a
diff --git a/test/test-controller.py b/test/test-controller.py
50703a
index f23125e..f55781a 100755
50703a
--- a/test/test-controller.py
50703a
+++ b/test/test-controller.py
50703a
@@ -8,24 +8,43 @@ from pyfakefs.fake_filesystem_unittest import TestCase
50703a
 
50703a
 LOOP = GLib.MainLoop()
50703a
 
50703a
+
50703a
+class TestController(ctrl.Controller):
50703a
+    def _find_existing_connection(self):
50703a
+        pass
50703a
+
50703a
+    def _on_aen(self, aen: int):
50703a
+        pass
50703a
+
50703a
+    def _on_nvme_event(self, nvme_event):
50703a
+        pass
50703a
+
50703a
+
50703a
 class Test(TestCase):
50703a
     '''Unit tests for class Controller'''
50703a
 
50703a
     def setUp(self):
50703a
         self.setUpPyfakefs()
50703a
 
50703a
-        self.fs.create_file('/etc/nvme/hostnqn', contents='nqn.2014-08.org.nvmexpress:uuid:01234567-0123-0123-0123-0123456789ab\n')
50703a
-        self.fs.create_file('/etc/nvme/hostid',  contents='01234567-89ab-cdef-0123-456789abcdef\n')
50703a
-        self.fs.create_file('/dev/nvme-fabrics', contents='instance=-1,cntlid=-1,transport=%s,traddr=%s,trsvcid=%s,nqn=%s,queue_size=%d,nr_io_queues=%d,reconnect_delay=%d,ctrl_loss_tmo=%d,keep_alive_tmo=%d,hostnqn=%s,host_traddr=%s,host_iface=%s,hostid=%s,duplicate_connect,disable_sqflow,hdr_digest,data_digest,nr_write_queues=%d,nr_poll_queues=%d,tos=%d,fast_io_fail_tmo=%d,discovery,dhchap_secret=%s,dhchap_ctrl_secret=%s\n')
50703a
+        self.fs.create_file(
50703a
+            '/etc/nvme/hostnqn', contents='nqn.2014-08.org.nvmexpress:uuid:01234567-0123-0123-0123-0123456789ab\n'
50703a
+        )
50703a
+        self.fs.create_file('/etc/nvme/hostid', contents='01234567-89ab-cdef-0123-456789abcdef\n')
50703a
+        self.fs.create_file(
50703a
+            '/dev/nvme-fabrics',
50703a
+            contents='instance=-1,cntlid=-1,transport=%s,traddr=%s,trsvcid=%s,nqn=%s,queue_size=%d,nr_io_queues=%d,reconnect_delay=%d,ctrl_loss_tmo=%d,keep_alive_tmo=%d,hostnqn=%s,host_traddr=%s,host_iface=%s,hostid=%s,duplicate_connect,disable_sqflow,hdr_digest,data_digest,nr_write_queues=%d,nr_poll_queues=%d,tos=%d,fast_io_fail_tmo=%d,discovery,dhchap_secret=%s,dhchap_ctrl_secret=%s\n',
50703a
+        )
50703a
 
50703a
-        self.NVME_TID = trid.TID({
50703a
-            'transport':   'tcp',
50703a
-            'traddr':      '10.10.10.10',
50703a
-            'subsysnqn':   'nqn.1988-11.com.dell:SFSS:2:20220208134025e8',
50703a
-            'trsvcid':     '8009',
50703a
-            'host-traddr': '1.2.3.4',
50703a
-            'host-iface':  'wlp0s20f3',
50703a
-        })
50703a
+        self.NVME_TID = trid.TID(
50703a
+            {
50703a
+                'transport': 'tcp',
50703a
+                'traddr': '10.10.10.10',
50703a
+                'subsysnqn': 'nqn.1988-11.com.dell:SFSS:2:20220208134025e8',
50703a
+                'trsvcid': '8009',
50703a
+                'host-traddr': '1.2.3.4',
50703a
+                'host-iface': 'wlp0s20f3',
50703a
+            }
50703a
+        )
50703a
 
50703a
         sysconf = conf.SysConf()
50703a
         self.root = nvme.root()
50703a
@@ -34,32 +53,92 @@ class Test(TestCase):
50703a
     def tearDown(self):
50703a
         LOOP.quit()
50703a
 
50703a
+    def test_cannot_instantiate_concrete_classes_if_abstract_method_are_not_implemented(self):
50703a
+        # Make sure we can't instantiate the ABC directly (Abstract Base Class).
50703a
+        class Controller(ctrl.Controller):
50703a
+            pass
50703a
+
50703a
+        self.assertRaises(TypeError, lambda: ctrl.Controller(root=self.root, host=self.host, tid=self.NVME_TID))
50703a
+
50703a
     def test_get_device(self):
50703a
-        controller = ctrl.Controller(root=self.root, host=self.host, tid=self.NVME_TID)
50703a
+        controller = TestController(root=self.root, host=self.host, tid=self.NVME_TID)
50703a
         self.assertEqual(controller._connect_attempts, 0)
50703a
-        self.assertRaises(NotImplementedError, controller._try_to_connect)
50703a
+        controller._try_to_connect()
50703a
         self.assertEqual(controller._connect_attempts, 1)
50703a
-        self.assertRaises(NotImplementedError, controller._find_existing_connection)
50703a
-        self.assertEqual(controller.id, "(tcp, 10.10.10.10, 8009, nqn.1988-11.com.dell:SFSS:2:20220208134025e8, wlp0s20f3, 1.2.3.4)")
50703a
+        self.assertEqual(
50703a
+            controller.id, "(tcp, 10.10.10.10, 8009, nqn.1988-11.com.dell:SFSS:2:20220208134025e8, wlp0s20f3, 1.2.3.4)"
50703a
+        )
50703a
         # raise Exception(controller._connect_op)
50703a
-        self.assertEqual(str(controller.tid), "(tcp, 10.10.10.10, 8009, nqn.1988-11.com.dell:SFSS:2:20220208134025e8, wlp0s20f3, 1.2.3.4)")
50703a
-        self.assertEqual(controller.device, '')
50703a
-        self.assertEqual(str(controller.controller_id_dict()), "{'transport': 'tcp', 'traddr': '10.10.10.10', 'trsvcid': '8009', 'host-traddr': '1.2.3.4', 'host-iface': 'wlp0s20f3', 'subsysnqn': 'nqn.1988-11.com.dell:SFSS:2:20220208134025e8', 'device': ''}")
50703a
-        # self.assertEqual(controller.details(), "{'transport': 'tcp', 'traddr': '10.10.10.[265 chars]ff]'}")
50703a
-        self.assertEqual(controller.info(), {'transport': 'tcp', 'traddr': '10.10.10.10', 'trsvcid': '8009', 'host-traddr': '1.2.3.4', 'host-iface': 'wlp0s20f3', 'subsysnqn': 'nqn.1988-11.com.dell:SFSS:2:20220208134025e8', 'device': '', 'hostid': '', 'hostnqn': '', 'model': '', 'serial': '', 'connect attempts': '1', 'retry connect timer': '60.0s [off]'})
50703a
+        self.assertEqual(
50703a
+            str(controller.tid),
50703a
+            "(tcp, 10.10.10.10, 8009, nqn.1988-11.com.dell:SFSS:2:20220208134025e8, wlp0s20f3, 1.2.3.4)",
50703a
+        )
50703a
+        self.assertEqual(controller.device, 'nvme?')
50703a
+        self.assertEqual(
50703a
+            str(controller.controller_id_dict()),
50703a
+            "{'transport': 'tcp', 'traddr': '10.10.10.10', 'trsvcid': '8009', 'host-traddr': '1.2.3.4', 'host-iface': 'wlp0s20f3', 'subsysnqn': 'nqn.1988-11.com.dell:SFSS:2:20220208134025e8', 'device': 'nvme?'}",
50703a
+        )
50703a
+        self.assertEqual(
50703a
+            controller.details(),
50703a
+            {
50703a
+                'dctype': '',
50703a
+                'cntrltype': '',
50703a
+                'transport': 'tcp',
50703a
+                'traddr': '10.10.10.10',
50703a
+                'trsvcid': '8009',
50703a
+                'host-traddr': '1.2.3.4',
50703a
+                'host-iface': 'wlp0s20f3',
50703a
+                'subsysnqn': 'nqn.1988-11.com.dell:SFSS:2:20220208134025e8',
50703a
+                'device': 'nvme?',
50703a
+                'connect attempts': '1',
50703a
+                'retry connect timer': '60.0s [off]',
50703a
+                'hostid': '',
50703a
+                'hostnqn': '',
50703a
+                'model': '',
50703a
+                'serial': '',
50703a
+            },
50703a
+        )
50703a
+        self.assertEqual(
50703a
+            controller.info(),
50703a
+            {
50703a
+                'dctype': '',
50703a
+                'cntrltype': '',
50703a
+                'transport': 'tcp',
50703a
+                'traddr': '10.10.10.10',
50703a
+                'trsvcid': '8009',
50703a
+                'host-traddr': '1.2.3.4',
50703a
+                'host-iface': 'wlp0s20f3',
50703a
+                'subsysnqn': 'nqn.1988-11.com.dell:SFSS:2:20220208134025e8',
50703a
+                'device': 'nvme?',
50703a
+                'connect attempts': '1',
50703a
+                'retry connect timer': '60.0s [off]',
50703a
+                'hostid': '',
50703a
+                'hostnqn': '',
50703a
+                'model': '',
50703a
+                'serial': '',
50703a
+                'connect operation': {'fail count': 0},
50703a
+            },
50703a
+        )
50703a
+
50703a
         # print(controller._connect_op)
50703a
         self.assertEqual(controller.cancel(), None)
50703a
         self.assertEqual(controller.kill(), None)
50703a
         # self.assertEqual(controller.disconnect(), 0)
50703a
 
50703a
     def test_connect(self):
50703a
-        controller = ctrl.Controller(root=self.root, host=self.host, tid=self.NVME_TID)
50703a
+        controller = TestController(root=self.root, host=self.host, tid=self.NVME_TID)
50703a
         self.assertEqual(controller._connect_attempts, 0)
50703a
-        controller._find_existing_connection = lambda : None
50703a
+        controller._find_existing_connection = lambda: None
50703a
         with self.assertLogs(logger=logging.getLogger(), level='DEBUG') as captured:
50703a
             controller._try_to_connect()
50703a
         self.assertEqual(len(captured.records), 1)
50703a
-        self.assertTrue(captured.records[0].getMessage().startswith("Controller._try_to_connect()       - (tcp, 10.10.10.10, 8009, nqn.1988-11.com.dell:SFSS:2:20220208134025e8, wlp0s20f3, 1.2.3.4) Connecting to nvme control with cfg={'hdr_digest': False, 'data_digest': False"))
50703a
+        self.assertTrue(
50703a
+            captured.records[0]
50703a
+            .getMessage()
50703a
+            .startswith(
50703a
+                "Controller._try_to_connect()       - (tcp, 10.10.10.10, 8009, nqn.1988-11.com.dell:SFSS:2:20220208134025e8, wlp0s20f3, 1.2.3.4) Connecting to nvme control with cfg={'hdr_digest': False, 'data_digest': False"
50703a
+            )
50703a
+        )
50703a
         self.assertEqual(controller._connect_attempts, 1)
50703a
 
50703a
 
50703a
diff --git a/test/test-service.py b/test/test-service.py
50703a
index 19f9b0c..4ce37be 100755
50703a
--- a/test/test-service.py
50703a
+++ b/test/test-service.py
50703a
@@ -4,6 +4,7 @@ import unittest
50703a
 from staslib import service
50703a
 from pyfakefs.fake_filesystem_unittest import TestCase
50703a
 
50703a
+
50703a
 class Args:
50703a
     def __init__(self):
50703a
         self.tron = True
50703a
@@ -11,6 +12,20 @@ class Args:
50703a
         self.conf_file = '/dev/null'
50703a
 
50703a
 
50703a
+class TestService(service.Service):
50703a
+    def _config_ctrls_finish(self, configured_ctrl_list):
50703a
+        pass
50703a
+
50703a
+    def _dump_last_known_config(self, controllers):
50703a
+        pass
50703a
+
50703a
+    def _keep_connections_on_exit(self):
50703a
+        pass
50703a
+
50703a
+    def _load_last_known_config(self):
50703a
+        return dict()
50703a
+
50703a
+
50703a
 class Test(TestCase):
50703a
     '''Unit tests for class Service'''
50703a
 
50703a
@@ -18,22 +33,39 @@ class Test(TestCase):
50703a
         self.setUpPyfakefs()
50703a
 
50703a
         os.environ['RUNTIME_DIRECTORY'] = "/run"
50703a
-        self.fs.create_file('/etc/nvme/hostnqn', contents='nqn.2014-08.org.nvmexpress:uuid:01234567-0123-0123-0123-0123456789ab\n')
50703a
-        self.fs.create_file('/etc/nvme/hostid',  contents='01234567-89ab-cdef-0123-456789abcdef\n')
50703a
-        self.fs.create_file('/dev/nvme-fabrics', contents='instance=-1,cntlid=-1,transport=%s,traddr=%s,trsvcid=%s,nqn=%s,queue_size=%d,nr_io_queues=%d,reconnect_delay=%d,ctrl_loss_tmo=%d,keep_alive_tmo=%d,hostnqn=%s,host_traddr=%s,host_iface=%s,hostid=%s,duplicate_connect,disable_sqflow,hdr_digest,data_digest,nr_write_queues=%d,nr_poll_queues=%d,tos=%d,fast_io_fail_tmo=%d,discovery,dhchap_secret=%s,dhchap_ctrl_secret=%s\n')
50703a
+        self.fs.create_file(
50703a
+            '/etc/nvme/hostnqn', contents='nqn.2014-08.org.nvmexpress:uuid:01234567-0123-0123-0123-0123456789ab\n'
50703a
+        )
50703a
+        self.fs.create_file('/etc/nvme/hostid', contents='01234567-89ab-cdef-0123-456789abcdef\n')
50703a
+        self.fs.create_file(
50703a
+            '/dev/nvme-fabrics',
50703a
+            contents='instance=-1,cntlid=-1,transport=%s,traddr=%s,trsvcid=%s,nqn=%s,queue_size=%d,nr_io_queues=%d,reconnect_delay=%d,ctrl_loss_tmo=%d,keep_alive_tmo=%d,hostnqn=%s,host_traddr=%s,host_iface=%s,hostid=%s,duplicate_connect,disable_sqflow,hdr_digest,data_digest,nr_write_queues=%d,nr_poll_queues=%d,tos=%d,fast_io_fail_tmo=%d,discovery,dhchap_secret=%s,dhchap_ctrl_secret=%s\n',
50703a
+        )
50703a
+
50703a
+    def test_cannot_instantiate_concrete_classes_if_abstract_method_are_not_implemented(self):
50703a
+        # Make sure we can't instantiate the ABC directly (Abstract Base Class).
50703a
+        class Service(service.Service):
50703a
+            pass
50703a
+
50703a
+        self.assertRaises(TypeError, lambda: Service(Args(), reload_hdlr=lambda x: x))
50703a
 
50703a
     def test_get_controller(self):
50703a
-        # FIXME: this is hack, fix it later
50703a
-        service.Service._load_last_known_config = lambda x : dict()
50703a
-        # start the test
50703a
-
50703a
-        srv = service.Service(Args(), reload_hdlr=lambda x : x)
50703a
-        self.assertRaises(NotImplementedError, srv._keep_connections_on_exit)
50703a
-        self.assertRaises(NotImplementedError, srv._dump_last_known_config, [])
50703a
-        self.assertRaises(NotImplementedError, srv._on_config_ctrls)
50703a
-        #self.assertEqual(srv.get_controllers(), dict())
50703a
-        self.assertEqual(srv.get_controller(transport='tcp', traddr='10.10.10.10', trsvcid='8009', host_traddr='1.2.3.4', host_iface='wlp0s20f3', subsysnqn='nqn.1988-11.com.dell:SFSS:2:20220208134025e8'), None)
50703a
-        self.assertEqual(srv.remove_controller(controller=None), None)
50703a
+        srv = TestService(Args(), reload_hdlr=lambda x: x)
50703a
+
50703a
+        self.assertEqual(list(srv.get_controllers()), list())
50703a
+        self.assertEqual(
50703a
+            srv.get_controller(
50703a
+                transport='tcp',
50703a
+                traddr='10.10.10.10',
50703a
+                trsvcid='8009',
50703a
+                host_traddr='1.2.3.4',
50703a
+                host_iface='wlp0s20f3',
50703a
+                subsysnqn='nqn.1988-11.com.dell:SFSS:2:20220208134025e8',
50703a
+            ),
50703a
+            None,
50703a
+        )
50703a
+        self.assertEqual(srv.remove_controller(controller=None, success=True), None)
50703a
+
50703a
 
50703a
 if __name__ == '__main__':
50703a
     unittest.main()