treewide: Codify memory pinning

This enforces movability constraints.

Change-Id: I49dbd43a791c1d70eb7e97f30708231c053901d9
Signed-off-by: William A. Kennington III <wak@google.com>
diff --git a/src/dhcp_configuration.cpp b/src/dhcp_configuration.cpp
index 154bbee..5907f4a 100644
--- a/src/dhcp_configuration.cpp
+++ b/src/dhcp_configuration.cpp
@@ -23,15 +23,16 @@
 using namespace sdbusplus::xyz::openbmc_project::Common::Error;
 
 Configuration::Configuration(sdbusplus::bus_t& bus,
-                             stdplus::const_zstring objPath, Manager& parent) :
+                             stdplus::const_zstring objPath,
+                             stdplus::PinnedRef<Manager> parent) :
     Iface(bus, objPath.c_str(), Iface::action::defer_emit),
-    bus(bus), manager(parent)
+    manager(parent)
 {
     config::Parser conf;
     std::filesystem::directory_entry newest_file;
     time_t newest_time = 0;
     for (const auto& dirent :
-         std::filesystem::directory_iterator(manager.getConfDir()))
+         std::filesystem::directory_iterator(manager.get().getConfDir()))
     {
         struct stat st = {};
         stat(dirent.path().native().c_str(), &st);
@@ -65,8 +66,8 @@
 
     auto name = ConfigIntf::sendHostNameEnabled(value);
 
-    manager.writeToConfigurationFile();
-    manager.reloadConfigs();
+    manager.get().writeToConfigurationFile();
+    manager.get().reloadConfigs();
 
     return name;
 }
@@ -79,8 +80,8 @@
     }
 
     auto name = ConfigIntf::hostNameEnabled(value);
-    manager.writeToConfigurationFile();
-    manager.reloadConfigs();
+    manager.get().writeToConfigurationFile();
+    manager.get().reloadConfigs();
 
     return name;
 }
@@ -93,8 +94,8 @@
     }
 
     auto ntp = ConfigIntf::ntpEnabled(value);
-    manager.writeToConfigurationFile();
-    manager.reloadConfigs();
+    manager.get().writeToConfigurationFile();
+    manager.get().reloadConfigs();
 
     return ntp;
 }
@@ -107,8 +108,8 @@
     }
 
     auto dns = ConfigIntf::dnsEnabled(value);
-    manager.writeToConfigurationFile();
-    manager.reloadConfigs();
+    manager.get().writeToConfigurationFile();
+    manager.get().reloadConfigs();
 
     return dns;
 }
diff --git a/src/dhcp_configuration.hpp b/src/dhcp_configuration.hpp
index 98f8e46..964b7d5 100644
--- a/src/dhcp_configuration.hpp
+++ b/src/dhcp_configuration.hpp
@@ -1,6 +1,7 @@
 #pragma once
 #include <sdbusplus/bus.hpp>
 #include <sdbusplus/server/object.hpp>
+#include <stdplus/pinned.hpp>
 #include <stdplus/zstring.hpp>
 #include <xyz/openbmc_project/Network/DHCPConfiguration/server.hpp>
 
@@ -27,20 +28,13 @@
 class Configuration : public Iface
 {
   public:
-    Configuration() = default;
-    Configuration(const Configuration&) = delete;
-    Configuration& operator=(const Configuration&) = delete;
-    Configuration(Configuration&&) = delete;
-    Configuration& operator=(Configuration&&) = delete;
-    virtual ~Configuration() = default;
-
     /** @brief Constructor to put object onto bus at a dbus path.
      *  @param[in] bus - Bus to attach to.
      *  @param[in] objPath - Path to attach at.
      *  @param[in] parent - Parent object.
      */
     Configuration(sdbusplus::bus_t& bus, stdplus::const_zstring objPath,
-                  Manager& parent);
+                  stdplus::PinnedRef<Manager> parent);
 
     /** @brief If true then DNS servers received from the DHCP server
      *         will be used and take precedence over any statically
@@ -83,11 +77,8 @@
     using ConfigIntf::sendHostNameEnabled;
 
   private:
-    /** @brief sdbusplus DBus bus connection. */
-    sdbusplus::bus_t& bus;
-
     /** @brief Network Manager object. */
-    phosphor::network::Manager& manager;
+    stdplus::PinnedRef<Manager> manager;
 };
 
 } // namespace dhcp
diff --git a/src/ethernet_interface.cpp b/src/ethernet_interface.cpp
index 102173f..5e61cbd 100644
--- a/src/ethernet_interface.cpp
+++ b/src/ethernet_interface.cpp
@@ -69,7 +69,8 @@
     return ret;
 }
 
-EthernetInterface::EthernetInterface(sdbusplus::bus_t& bus, Manager& manager,
+EthernetInterface::EthernetInterface(stdplus::PinnedRef<sdbusplus::bus_t> bus,
+                                     stdplus::PinnedRef<Manager> manager,
                                      const AllIntfInfo& info,
                                      std::string_view objRoot,
                                      const config::Parser& config,
@@ -79,7 +80,8 @@
 {
 }
 
-EthernetInterface::EthernetInterface(sdbusplus::bus_t& bus, Manager& manager,
+EthernetInterface::EthernetInterface(stdplus::PinnedRef<sdbusplus::bus_t> bus,
+                                     stdplus::PinnedRef<Manager> manager,
                                      const AllIntfInfo& info,
                                      std::string&& objPath,
                                      const config::Parser& config,
@@ -270,7 +272,7 @@
     }
 
     writeConfigurationFile();
-    manager.reloadConfigs();
+    manager.get().reloadConfigs();
 
     return it->second->getObjPath();
 }
@@ -326,7 +328,7 @@
     }
 
     writeConfigurationFile();
-    manager.reloadConfigs();
+    manager.get().reloadConfigs();
 
     return it->second->getObjPath();
 }
@@ -336,7 +338,7 @@
     if (ipv6AcceptRA() != EthernetInterfaceIntf::ipv6AcceptRA(value))
     {
         writeConfigurationFile();
-        manager.reloadConfigs();
+        manager.get().reloadConfigs();
     }
     return value;
 }
@@ -346,7 +348,7 @@
     if (dhcp4() != EthernetInterfaceIntf::dhcp4(value))
     {
         writeConfigurationFile();
-        manager.reloadConfigs();
+        manager.get().reloadConfigs();
     }
     return value;
 }
@@ -356,7 +358,7 @@
     if (dhcp6() != EthernetInterfaceIntf::dhcp6(value))
     {
         writeConfigurationFile();
-        manager.reloadConfigs();
+        manager.get().reloadConfigs();
     }
     return value;
 }
@@ -378,7 +380,7 @@
     if (old4 != new4 || old6 != new6 || oldra != newra)
     {
         writeConfigurationFile();
-        manager.reloadConfigs();
+        manager.get().reloadConfigs();
     }
     return value;
 }
@@ -423,10 +425,10 @@
     {
         // We only need to bring down the interface, networkd will always bring
         // up managed interfaces
-        manager.addReloadPreHook(
+        manager.get().addReloadPreHook(
             [ifname = interfaceName()]() { system::setNICUp(ifname, false); });
     }
-    manager.reloadConfigs();
+    manager.get().reloadConfigs();
 
     return value;
 }
@@ -453,7 +455,7 @@
         EthernetInterfaceIntf::staticNameServers(value);
 
         writeConfigurationFile();
-        manager.reloadConfigs();
+        manager.get().reloadConfigs();
     }
     catch (const InternalFailure& e)
     {
@@ -479,14 +481,15 @@
 ServerList EthernetInterface::getNTPServerFromTimeSyncd()
 {
     ServerList servers; // Variable to capture the NTP Server IPs
-    auto method = bus.new_method_call(TIMESYNCD_SERVICE, TIMESYNCD_SERVICE_PATH,
-                                      PROPERTY_INTERFACE, METHOD_GET);
+    auto method =
+        bus.get().new_method_call(TIMESYNCD_SERVICE, TIMESYNCD_SERVICE_PATH,
+                                  PROPERTY_INTERFACE, METHOD_GET);
 
     method.append(TIMESYNCD_INTERFACE, "LinkNTPServers");
 
     try
     {
-        auto reply = bus.call(method);
+        auto reply = bus.get().call(method);
         std::variant<ServerList> response;
         reply.read(response);
         servers = std::get<ServerList>(response);
@@ -523,14 +526,14 @@
 
     using type = std::vector<std::tuple<int32_t, std::vector<uint8_t>>>;
     std::variant<type> name; // Variable to capture the DNS property
-    auto method = bus.new_method_call(RESOLVED_SERVICE, OBJ_PATH.c_str(),
-                                      PROPERTY_INTERFACE, METHOD_GET);
+    auto method = bus.get().new_method_call(RESOLVED_SERVICE, OBJ_PATH.c_str(),
+                                            PROPERTY_INTERFACE, METHOD_GET);
 
     method.append(RESOLVED_INTERFACE, "DNS");
 
     try
     {
-        auto reply = bus.call(method);
+        auto reply = bus.get().call(method);
         reply.read(name);
     }
     catch (const sdbusplus::exception_t& e)
@@ -552,7 +555,8 @@
 {
     auto intfName = fmt::format(FMT_COMPILE("{}.{}"), interfaceName(), id);
     auto idStr = std::to_string(id);
-    if (manager.interfaces.find(intfName) != manager.interfaces.end())
+    if (manager.get().interfaces.find(intfName) !=
+        manager.get().interfaces.end())
     {
         log<level::ERR>("VLAN already exists", entry("VLANID=%u", id));
         elog<InvalidArgument>(Argument::ARGUMENT_NAME("VLANId"),
@@ -582,7 +586,7 @@
         bus, manager, info, objRoot, config::Parser(), nicEnabled());
     ObjectPath ret = vlanIntf->objPath;
 
-    manager.interfaces.emplace(intfName, std::move(vlanIntf));
+    manager.get().interfaces.emplace(intfName, std::move(vlanIntf));
 
     // write the device file for the vlan interface.
     config::Parser config;
@@ -590,10 +594,11 @@
     netdev["Name"].emplace_back(intfName);
     netdev["Kind"].emplace_back("vlan");
     config.map["VLAN"].emplace_back()["Id"].emplace_back(std::move(idStr));
-    config.writeFile(config::pathForIntfDev(manager.getConfDir(), intfName));
+    config.writeFile(
+        config::pathForIntfDev(manager.get().getConfDir(), intfName));
 
     writeConfigurationFile();
-    manager.reloadConfigs();
+    manager.get().reloadConfigs();
 
     return ret;
 }
@@ -605,7 +610,7 @@
         EthernetInterfaceIntf::staticNTPServers(value);
 
         writeConfigurationFile();
-        manager.reloadConfigs();
+        manager.get().reloadConfigs();
     }
     catch (InternalFailure& e)
     {
@@ -653,7 +658,7 @@
                                              : (dhcp6() ? "ipv6" : "false"));
         {
             auto& vlans = network["VLAN"];
-            for (const auto& [_, intf] : manager.interfaces)
+            for (const auto& [_, intf] : manager.get().interfaces)
             {
                 if (intf->vlan && intf->vlan->parentIdx == ifIdx)
                 {
@@ -722,7 +727,7 @@
     {
         auto& dhcp = config.map["DHCP"].emplace_back();
         dhcp["ClientIdentifier"].emplace_back("mac");
-        const auto& conf = manager.getDHCPConf();
+        const auto& conf = manager.get().getDHCPConf();
         auto dns_enabled = conf.dnsEnabled() ? "true" : "false";
         dhcp["UseDNS"].emplace_back(dns_enabled);
         dhcp["UseDomains"].emplace_back(dns_enabled);
@@ -732,7 +737,8 @@
         dhcp["SendHostname"].emplace_back(conf.sendHostNameEnabled() ? "true"
                                                                      : "false");
     }
-    auto path = config::pathForIntfConf(manager.getConfDir(), interfaceName());
+    auto path =
+        config::pathForIntfConf(manager.get().getConfDir(), interfaceName());
     config.writeFile(path);
     auto msg = fmt::format("Wrote networkd file: {}", path.native());
     log<level::INFO>(msg.c_str(), entry("FILE=%s", path.c_str()));
@@ -774,7 +780,7 @@
     if (newMAC != oldMAC)
     {
         // Update everything that depends on the MAC value
-        for (const auto& [_, intf] : manager.interfaces)
+        for (const auto& [_, intf] : manager.get().interfaces)
         {
             if (intf->vlan && intf->vlan->parentIdx == ifIdx)
             {
@@ -784,11 +790,11 @@
         MacAddressIntf::macAddress(validMAC);
 
         writeConfigurationFile();
-        manager.addReloadPreHook([interface]() {
+        manager.get().addReloadPreHook([interface]() {
             // The MAC and LLADDRs will only update if the NIC is already down
             system::setNICUp(interface, false);
         });
-        manager.reloadConfigs();
+        manager.get().reloadConfigs();
     }
 
 #ifdef HAVE_UBOOT_ENV
@@ -816,7 +822,7 @@
     addrs.clear();
 
     writeConfigurationFile();
-    manager.reloadConfigs();
+    manager.get().reloadConfigs();
 }
 
 std::string EthernetInterface::defaultGateway(std::string gateway)
@@ -843,7 +849,7 @@
     EthernetInterfaceIntf::defaultGateway(gateway);
 
     writeConfigurationFile();
-    manager.reloadConfigs();
+    manager.get().reloadConfigs();
 
     return gateway;
 }
@@ -872,14 +878,14 @@
     EthernetInterfaceIntf::defaultGateway6(gateway);
 
     writeConfigurationFile();
-    manager.reloadConfigs();
+    manager.get().reloadConfigs();
 
     return gateway;
 }
 
 EthernetInterface::VlanProperties::VlanProperties(
     sdbusplus::bus_t& bus, stdplus::const_zstring objPath,
-    const InterfaceInfo& info, EthernetInterface& eth) :
+    const InterfaceInfo& info, stdplus::PinnedRef<EthernetInterface> eth) :
     VlanIfaces(bus, objPath.c_str(), VlanIfaces::action::defer_emit),
     parentIdx(*info.parent_idx), eth(eth)
 {
@@ -889,24 +895,24 @@
 
 void EthernetInterface::VlanProperties::delete_()
 {
-    auto intf = eth.interfaceName();
+    auto intf = eth.get().interfaceName();
 
     // Remove all configs for the current interface
-    const auto& confDir = eth.manager.getConfDir();
+    const auto& confDir = eth.get().manager.get().getConfDir();
     std::error_code ec;
     std::filesystem::remove(config::pathForIntfConf(confDir, intf), ec);
     std::filesystem::remove(config::pathForIntfDev(confDir, intf), ec);
 
-    if (eth.ifIdx > 0)
+    if (eth.get().ifIdx > 0)
     {
-        eth.manager.interfacesByIdx.erase(eth.ifIdx);
+        eth.get().manager.get().interfacesByIdx.erase(eth.get().ifIdx);
     }
-    auto it = eth.manager.interfaces.find(intf);
+    auto it = eth.get().manager.get().interfaces.find(intf);
     auto obj = std::move(it->second);
-    eth.manager.interfaces.erase(it);
+    eth.get().manager.get().interfaces.erase(it);
 
     // Write an updated parent interface since it has a VLAN entry
-    for (const auto& [_, intf] : eth.manager.interfaces)
+    for (const auto& [_, intf] : eth.get().manager.get().interfaces)
     {
         if (intf->ifIdx == parentIdx)
         {
@@ -914,17 +920,17 @@
         }
     }
 
-    if (eth.ifIdx > 0)
+    if (eth.get().ifIdx > 0)
     {
         // We need to forcibly delete the interface as systemd does not
-        eth.manager.addReloadPostHook(
-            [idx = eth.ifIdx]() { system::deleteIntf(idx); });
+        eth.get().manager.get().addReloadPostHook(
+            [idx = eth.get().ifIdx]() { system::deleteIntf(idx); });
 
         // Ignore the interface so the reload doesn't re-query it
-        eth.manager.ignoredIntf.emplace(eth.ifIdx);
+        eth.get().manager.get().ignoredIntf.emplace(eth.get().ifIdx);
     }
 
-    eth.manager.reloadConfigs();
+    eth.get().manager.get().reloadConfigs();
 }
 
 } // namespace network
diff --git a/src/ethernet_interface.hpp b/src/ethernet_interface.hpp
index a1e9975..b66030a 100644
--- a/src/ethernet_interface.hpp
+++ b/src/ethernet_interface.hpp
@@ -8,6 +8,7 @@
 #include <optional>
 #include <sdbusplus/bus.hpp>
 #include <sdbusplus/server/object.hpp>
+#include <stdplus/pinned.hpp>
 #include <stdplus/zstring_view.hpp>
 #include <string>
 #include <vector>
@@ -63,12 +64,8 @@
 class EthernetInterface : public Ifaces
 {
   public:
-    EthernetInterface() = delete;
-    EthernetInterface(const EthernetInterface&) = delete;
-    EthernetInterface& operator=(const EthernetInterface&) = delete;
     EthernetInterface(EthernetInterface&&) = delete;
     EthernetInterface& operator=(EthernetInterface&&) = delete;
-    virtual ~EthernetInterface() = default;
 
     /** @brief Constructor to put object onto bus at a dbus path.
      *  @param[in] bus - Bus to attach to.
@@ -79,12 +76,13 @@
      *  @param[in] vlan - The id of the vlan if configured
      *  @param[in] enabled - Determine if systemd-networkd is managing this link
      */
-    EthernetInterface(sdbusplus::bus_t& bus, Manager& manager,
+    EthernetInterface(stdplus::PinnedRef<sdbusplus::bus_t> bus,
+                      stdplus::PinnedRef<Manager> manager,
                       const AllIntfInfo& info, std::string_view objRoot,
                       const config::Parser& config, bool enabled);
 
     /** @brief Network Manager object. */
-    Manager& manager;
+    stdplus::PinnedRef<Manager> manager;
 
     /** @brief Persistent map of IPAddress dbus objects and their names */
     std::unordered_map<IfAddr, std::unique_ptr<IPAddress>> addrs;
@@ -220,7 +218,7 @@
     virtual ServerList getNameServerFromResolvd();
 
     /** @brief Persistent sdbusplus DBus bus connection. */
-    sdbusplus::bus_t& bus;
+    stdplus::PinnedRef<sdbusplus::bus_t> bus;
 
     /** @brief Dbus object path */
     std::string objPath;
@@ -231,10 +229,11 @@
     struct VlanProperties : VlanIfaces
     {
         VlanProperties(sdbusplus::bus_t& bus, stdplus::const_zstring objPath,
-                       const InterfaceInfo& info, EthernetInterface& eth);
+                       const InterfaceInfo& info,
+                       stdplus::PinnedRef<EthernetInterface> eth);
         void delete_() override;
         unsigned parentIdx;
-        EthernetInterface& eth;
+        stdplus::PinnedRef<EthernetInterface> eth;
     };
     std::optional<VlanProperties> vlan;
 
@@ -242,7 +241,8 @@
     friend class TestNetworkManager;
 
   private:
-    EthernetInterface(sdbusplus::bus_t& bus, Manager& manager,
+    EthernetInterface(stdplus::PinnedRef<sdbusplus::bus_t> bus,
+                      stdplus::PinnedRef<Manager> manager,
                       const AllIntfInfo& info, std::string&& objPath,
                       const config::Parser& config, bool enabled);
 
diff --git a/src/inventory_mac.cpp b/src/inventory_mac.cpp
index fb38d72..7eaa3c4 100644
--- a/src/inventory_mac.cpp
+++ b/src/inventory_mac.cpp
@@ -337,9 +337,10 @@
     }
 }
 
-std::unique_ptr<Runtime> watch(sdbusplus::bus_t& bus, Manager& m)
+std::unique_ptr<Runtime> watch(stdplus::PinnedRef<sdbusplus::bus_t> bus,
+                               stdplus::PinnedRef<Manager> m)
 {
-    manager = &m;
+    manager = &m.get();
     std::ifstream in(configFile);
     nlohmann::json configJson;
     in >> configJson;
diff --git a/src/inventory_mac.hpp b/src/inventory_mac.hpp
index 72b312d..1b6aa8b 100644
--- a/src/inventory_mac.hpp
+++ b/src/inventory_mac.hpp
@@ -1,6 +1,7 @@
 #pragma once
 #include <memory>
 #include <sdbusplus/bus.hpp>
+#include <stdplus/pinned.hpp>
 
 namespace phosphor::network
 {
@@ -14,7 +15,8 @@
 {
     virtual ~Runtime() = default;
 };
-std::unique_ptr<Runtime> watch(sdbusplus::bus_t& bus, Manager& m);
+std::unique_ptr<Runtime> watch(stdplus::PinnedRef<sdbusplus::bus_t> bus,
+                               stdplus::PinnedRef<Manager> m);
 
 } // namespace inventory
 } // namespace phosphor::network
diff --git a/src/ipaddress.cpp b/src/ipaddress.cpp
index 07e37e4..62ac220 100644
--- a/src/ipaddress.cpp
+++ b/src/ipaddress.cpp
@@ -46,7 +46,7 @@
 };
 
 IPAddress::IPAddress(sdbusplus::bus_t& bus, std::string_view objRoot,
-                     EthernetInterface& parent, IfAddr addr,
+                     stdplus::PinnedRef<EthernetInterface> parent, IfAddr addr,
                      AddressOrigin origin) :
     IPAddress(bus, makeObjPath(objRoot, addr), parent, addr, origin)
 {
@@ -54,7 +54,7 @@
 
 IPAddress::IPAddress(sdbusplus::bus_t& bus,
                      sdbusplus::message::object_path objPath,
-                     EthernetInterface& parent, IfAddr addr,
+                     stdplus::PinnedRef<EthernetInterface> parent, IfAddr addr,
                      AddressOrigin origin) :
     IPIfaces(bus, objPath.str.c_str(), IPIfaces::action::defer_emit),
     parent(parent), objPath(std::move(objPath))
@@ -94,23 +94,24 @@
         log<level::ERR>("Tried to delete a non-static address"),
             entry("ADDRESS=%s", address().c_str()),
             entry("PREFIX=%" PRIu8, prefixLength()),
-            entry("INTERFACE=%s", parent.interfaceName().c_str());
+            entry("INTERFACE=%s", parent.get().interfaceName().c_str());
         elog<InternalFailure>();
     }
 
     std::unique_ptr<IPAddress> ptr;
-    for (auto it = parent.addrs.begin(); it != parent.addrs.end(); ++it)
+    auto& addrs = parent.get().addrs;
+    for (auto it = addrs.begin(); it != addrs.end(); ++it)
     {
         if (it->second.get() == this)
         {
             ptr = std::move(it->second);
-            parent.addrs.erase(it);
+            addrs.erase(it);
             break;
         }
     }
 
-    parent.writeConfigurationFile();
-    parent.manager.reloadConfigs();
+    parent.get().writeConfigurationFile();
+    parent.get().manager.get().reloadConfigs();
 }
 
 } // namespace network
diff --git a/src/ipaddress.hpp b/src/ipaddress.hpp
index 05aafbd..743025b 100644
--- a/src/ipaddress.hpp
+++ b/src/ipaddress.hpp
@@ -4,6 +4,7 @@
 #include <sdbusplus/bus.hpp>
 #include <sdbusplus/message/native_types.hpp>
 #include <sdbusplus/server/object.hpp>
+#include <stdplus/pinned.hpp>
 #include <string_view>
 #include <xyz/openbmc_project/Network/IP/server.hpp>
 #include <xyz/openbmc_project/Object/Delete/server.hpp>
@@ -30,13 +31,6 @@
 class IPAddress : public IPIfaces
 {
   public:
-    IPAddress() = delete;
-    IPAddress(const IPAddress&) = delete;
-    IPAddress& operator=(const IPAddress&) = delete;
-    IPAddress(IPAddress&&) = delete;
-    IPAddress& operator=(IPAddress&&) = delete;
-    virtual ~IPAddress() = default;
-
     /** @brief Constructor to put object onto bus at a dbus path.
      *  @param[in] bus - Bus to attach to.
      *  @param[in] objRoot - Path to attach at.
@@ -45,7 +39,8 @@
      *  @param[in] origin - origin of ipaddress(dhcp/static/SLAAC/LinkLocal).
      */
     IPAddress(sdbusplus::bus_t& bus, std::string_view objRoot,
-              EthernetInterface& parent, IfAddr addr, IP::AddressOrigin origin);
+              stdplus::PinnedRef<EthernetInterface> parent, IfAddr addr,
+              IP::AddressOrigin origin);
 
     std::string address(std::string ipAddress) override;
     uint8_t prefixLength(uint8_t) override;
@@ -70,13 +65,14 @@
 
   private:
     /** @brief Parent Object. */
-    EthernetInterface& parent;
+    stdplus::PinnedRef<EthernetInterface> parent;
 
     /** @brief Dbus object path */
     sdbusplus::message::object_path objPath;
 
     IPAddress(sdbusplus::bus_t& bus, sdbusplus::message::object_path objPath,
-              EthernetInterface& parent, IfAddr addr, IP::AddressOrigin origin);
+              stdplus::PinnedRef<EthernetInterface> parent, IfAddr addr,
+              IP::AddressOrigin origin);
 };
 
 } // namespace network
diff --git a/src/neighbor.cpp b/src/neighbor.cpp
index 6b8d853..959f524 100644
--- a/src/neighbor.cpp
+++ b/src/neighbor.cpp
@@ -21,16 +21,16 @@
 }
 
 Neighbor::Neighbor(sdbusplus::bus_t& bus, std::string_view objRoot,
-                   EthernetInterface& parent, InAddrAny addr, ether_addr lladdr,
-                   State state) :
+                   stdplus::PinnedRef<EthernetInterface> parent, InAddrAny addr,
+                   ether_addr lladdr, State state) :
     Neighbor(bus, makeObjPath(objRoot, addr), parent, addr, lladdr, state)
 {
 }
 
 Neighbor::Neighbor(sdbusplus::bus_t& bus,
                    sdbusplus::message::object_path objPath,
-                   EthernetInterface& parent, InAddrAny addr, ether_addr lladdr,
-                   State state) :
+                   stdplus::PinnedRef<EthernetInterface> parent, InAddrAny addr,
+                   ether_addr lladdr, State state) :
     NeighborObj(bus, objPath.str.c_str(), NeighborObj::action::defer_emit),
     parent(parent), objPath(std::move(objPath))
 {
@@ -42,7 +42,7 @@
 
 void Neighbor::delete_()
 {
-    auto& neighbors = parent.staticNeighbors;
+    auto& neighbors = parent.get().staticNeighbors;
     std::unique_ptr<Neighbor> ptr;
     for (auto it = neighbors.begin(); it != neighbors.end(); ++it)
     {
@@ -54,8 +54,8 @@
         }
     }
 
-    parent.writeConfigurationFile();
-    parent.manager.reloadConfigs();
+    parent.get().writeConfigurationFile();
+    parent.get().manager.get().reloadConfigs();
 }
 
 using sdbusplus::xyz::openbmc_project::Common::Error::NotAllowed;
diff --git a/src/neighbor.hpp b/src/neighbor.hpp
index 37f8d17..861a1ec 100644
--- a/src/neighbor.hpp
+++ b/src/neighbor.hpp
@@ -4,6 +4,7 @@
 #include <sdbusplus/bus.hpp>
 #include <sdbusplus/message/native_types.hpp>
 #include <sdbusplus/server/object.hpp>
+#include <stdplus/pinned.hpp>
 #include <string_view>
 #include <xyz/openbmc_project/Network/Neighbor/server.hpp>
 #include <xyz/openbmc_project/Object/Delete/server.hpp>
@@ -28,15 +29,6 @@
 class Neighbor : public NeighborObj
 {
   public:
-    using State = NeighborIntf::State;
-
-    Neighbor() = delete;
-    Neighbor(const Neighbor&) = delete;
-    Neighbor& operator=(const Neighbor&) = delete;
-    Neighbor(Neighbor&&) = delete;
-    Neighbor& operator=(Neighbor&&) = delete;
-    virtual ~Neighbor() = default;
-
     /** @brief Constructor to put object onto bus at a dbus path.
      *  @param[in] bus - Bus to attach to.
      *  @param[in] objRoot - Path to attach at.
@@ -46,8 +38,8 @@
      *  @param[in] state - The state of the neighbor entry.
      */
     Neighbor(sdbusplus::bus_t& bus, std::string_view objRoot,
-             EthernetInterface& parent, InAddrAny addr, ether_addr lladdr,
-             State state);
+             stdplus::PinnedRef<EthernetInterface> parent, InAddrAny addr,
+             ether_addr lladdr, State state);
 
     /** @brief Delete this d-bus object.
      */
@@ -67,14 +59,14 @@
 
   private:
     /** @brief Parent Object. */
-    EthernetInterface& parent;
+    stdplus::PinnedRef<EthernetInterface> parent;
 
     /** @brief Dbus object path */
     sdbusplus::message::object_path objPath;
 
     Neighbor(sdbusplus::bus_t& bus, sdbusplus::message::object_path objPath,
-             EthernetInterface& parent, InAddrAny addr, ether_addr lladdr,
-             State state);
+             stdplus::PinnedRef<EthernetInterface> parent, InAddrAny addr,
+             ether_addr lladdr, State state);
 };
 
 } // namespace network
diff --git a/src/network_manager.cpp b/src/network_manager.cpp
index 557e826..060242c 100644
--- a/src/network_manager.cpp
+++ b/src/network_manager.cpp
@@ -14,6 +14,7 @@
 #include <phosphor-logging/elog-errors.hpp>
 #include <phosphor-logging/log.hpp>
 #include <sdbusplus/message.hpp>
+#include <stdplus/pinned.hpp>
 #include <xyz/openbmc_project/Common/error.hpp>
 
 constexpr char SYSTEMD_BUSNAME[] = "org.freedesktop.systemd1";
@@ -39,13 +40,14 @@
     "link',interface='org.freedesktop.DBus.Properties',member='"
     "PropertiesChanged',arg0='org.freedesktop.network1.Link',";
 
-Manager::Manager(sdbusplus::bus_t& bus, DelayedExecutor& reload,
-                 stdplus::zstring_view objPath,
+Manager::Manager(stdplus::PinnedRef<sdbusplus::bus_t> bus,
+                 DelayedExecutor& reload, stdplus::zstring_view objPath,
                  const std::filesystem::path& confDir) :
     ManagerIface(bus, objPath.c_str(), ManagerIface::action::defer_emit),
     reload(reload), bus(bus), objPath(std::string(objPath)), confDir(confDir),
     systemdNetworkdEnabledMatch(
-        bus, enabledMatch, [&](sdbusplus::message_t& m) {
+        bus, enabledMatch,
+        [man = stdplus::PinnedRef(*this)](sdbusplus::message_t& m) {
             std::string intf;
             std::unordered_map<std::string, std::variant<std::string>> values;
             try
@@ -64,7 +66,7 @@
                 }
                 auto ifidx = DecodeInt<unsigned, 10>{}(obj.substr(sep + 3));
                 const auto& state = std::get<std::string>(it->second);
-                handleAdminState(state, ifidx);
+                man.get().handleAdminState(state, ifidx);
             }
             catch (const std::exception& e)
             {
@@ -92,9 +94,10 @@
         reloadPreHooks.clear();
         try
         {
-            auto method = bus.new_method_call(NETWORKD_BUSNAME, NETWORKD_PATH,
-                                              NETWORKD_INTERFACE, "Reload");
-            bus.call_noreply(method);
+            bus.get()
+                .new_method_call(NETWORKD_BUSNAME, NETWORKD_PATH,
+                                 NETWORKD_INTERFACE, "Reload")
+                .call();
             log<level::INFO>("Reloaded systemd-networkd");
         }
         catch (const sdbusplus::exception_t& ex)
@@ -122,11 +125,12 @@
         links;
     try
     {
-        auto rsp =
-            bus.new_method_call("org.freedesktop.network1",
-                                "/org/freedesktop/network1",
-                                "org.freedesktop.network1.Manager", "ListLinks")
-                .call();
+        auto rsp = bus.get()
+                       .new_method_call("org.freedesktop.network1",
+                                        "/org/freedesktop/network1",
+                                        "org.freedesktop.network1.Manager",
+                                        "ListLinks")
+                       .call();
         rsp.read(links);
     }
     catch (const sdbusplus::exception::SdBusError& e)
@@ -138,8 +142,8 @@
         unsigned ifidx = std::get<0>(link);
         auto obj = fmt::format("/org/freedesktop/network1/link/_3{}", ifidx);
         auto req =
-            bus.new_method_call("org.freedesktop.network1", obj.c_str(),
-                                "org.freedesktop.DBus.Properties", "Get");
+            bus.get().new_method_call("org.freedesktop.network1", obj.c_str(),
+                                      "org.freedesktop.DBus.Properties", "Get");
         req.append("org.freedesktop.network1.Link", "AdministrativeState");
         auto rsp = req.call();
         std::variant<std::string> val;
diff --git a/src/network_manager.hpp b/src/network_manager.hpp
index 890a837..490c4bc 100644
--- a/src/network_manager.hpp
+++ b/src/network_manager.hpp
@@ -11,6 +11,7 @@
 #include <sdbusplus/bus.hpp>
 #include <sdbusplus/bus/match.hpp>
 #include <sdbusplus/message/native_types.hpp>
+#include <stdplus/pinned.hpp>
 #include <stdplus/zstring_view.hpp>
 #include <string>
 #include <string_view>
@@ -41,8 +42,6 @@
 class Manager : public ManagerIface
 {
   public:
-    Manager(const Manager&) = delete;
-    Manager& operator=(const Manager&) = delete;
     Manager(Manager&&) = delete;
     Manager& operator=(Manager&&) = delete;
 
@@ -52,7 +51,7 @@
      *  @param[in] objPath - Path to attach at.
      *  @param[in] confDir - Network Configuration directory path.
      */
-    Manager(sdbusplus::bus_t& bus, DelayedExecutor& reload,
+    Manager(stdplus::PinnedRef<sdbusplus::bus_t> bus, DelayedExecutor& reload,
             stdplus::zstring_view objPath,
             const std::filesystem::path& confDir);
 
@@ -133,7 +132,7 @@
     DelayedExecutor& reload;
 
     /** @brief Persistent sdbusplus DBus bus connection. */
-    sdbusplus::bus_t& bus;
+    stdplus::PinnedRef<sdbusplus::bus_t> bus;
 
     /** @brief BMC network reset - resets network configuration for BMC. */
     void reset() override;
diff --git a/src/network_manager_main.cpp b/src/network_manager_main.cpp
index 170241d..dc562cb 100644
--- a/src/network_manager_main.cpp
+++ b/src/network_manager_main.cpp
@@ -17,6 +17,7 @@
 #include <sdeventplus/event.hpp>
 #include <sdeventplus/source/signal.hpp>
 #include <sdeventplus/utility/timer.hpp>
+#include <stdplus/pinned.hpp>
 #include <stdplus/signal.hpp>
 
 using phosphor::logging::level;
@@ -65,11 +66,11 @@
     stdplus::signal::block(SIGTERM);
     sdeventplus::source::Signal(event, SIGTERM, termCb).set_floating(true);
 
-    auto bus = sdbusplus::bus::new_default();
+    stdplus::Pinned bus = sdbusplus::bus::new_default();
     bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL);
     sdbusplus::server::manager_t objManager(bus, DEFAULT_OBJPATH);
 
-    TimerExecutor reload(event, std::chrono::seconds(3));
+    stdplus::Pinned<TimerExecutor> reload(event, std::chrono::seconds(3));
     Manager manager(bus, reload, DEFAULT_OBJPATH, "/etc/systemd/network");
     netlink::Server svr(event, manager);
 
diff --git a/src/system_configuration.cpp b/src/system_configuration.cpp
index bccdb02..58b6dfc 100644
--- a/src/system_configuration.cpp
+++ b/src/system_configuration.cpp
@@ -2,6 +2,7 @@
 
 #include <phosphor-logging/elog-errors.hpp>
 #include <phosphor-logging/log.hpp>
+#include <stdplus/pinned.hpp>
 #include <xyz/openbmc_project/Common/error.hpp>
 
 namespace phosphor
@@ -22,40 +23,43 @@
     "interface='org.freedesktop.DBus.Properties',member='PropertiesChanged',"
     "arg0='org.freedesktop.hostname1'";
 
-SystemConfiguration::SystemConfiguration(sdbusplus::bus_t& bus,
-                                         stdplus::const_zstring objPath) :
+SystemConfiguration::SystemConfiguration(
+    stdplus::PinnedRef<sdbusplus::bus_t> bus, stdplus::const_zstring objPath) :
     Iface(bus, objPath.c_str(), Iface::action::defer_emit),
-    bus(bus), hostnamePropMatch(bus, propMatch, [&](sdbusplus::message_t& m) {
-        std::string intf;
-        std::unordered_map<std::string, std::variant<std::string>> values;
-        try
-        {
-            m.read(intf, values);
-            auto it = values.find("Hostname");
-            if (it == values.end())
+    bus(bus),
+    hostnamePropMatch(
+        bus, propMatch,
+        [sc = stdplus::PinnedRef(*this)](sdbusplus::message_t& m) {
+            std::string intf;
+            std::unordered_map<std::string, std::variant<std::string>> values;
+            try
             {
-                return;
+                m.read(intf, values);
+                auto it = values.find("Hostname");
+                if (it == values.end())
+                {
+                    return;
+                }
+                sc.get().Iface::hostName(std::get<std::string>(it->second));
             }
-            Iface::hostName(std::get<std::string>(it->second));
-        }
-        catch (const std::exception& e)
-        {
-            log<level::ERR>(
-                fmt::format("Hostname match parsing failed: {}", e.what())
-                    .c_str(),
-                entry("ERROR=%s", e.what()));
-        }
-    })
+            catch (const std::exception& e)
+            {
+                log<level::ERR>(
+                    fmt::format("Hostname match parsing failed: {}", e.what())
+                        .c_str(),
+                    entry("ERROR=%s", e.what()));
+            }
+        })
 {
     try
     {
         std::variant<std::string> name;
         auto req =
-            bus.new_method_call(HOSTNAMED_SVC, HOSTNAMED_OBJ,
-                                "org.freedesktop.DBus.Properties", "Get");
+            bus.get().new_method_call(HOSTNAMED_SVC, HOSTNAMED_OBJ,
+                                      "org.freedesktop.DBus.Properties", "Get");
 
         req.append(HOSTNAMED_INTF, "Hostname");
-        auto reply = bus.call(req);
+        auto reply = req.call();
         reply.read(name);
         SystemConfigIntf::hostName(std::get<std::string>(name), true);
     }
@@ -76,10 +80,10 @@
     }
     try
     {
-        auto method = bus.new_method_call(HOSTNAMED_SVC, HOSTNAMED_OBJ,
-                                          HOSTNAMED_INTF, "SetStaticHostname");
+        auto method = bus.get().new_method_call(
+            HOSTNAMED_SVC, HOSTNAMED_OBJ, HOSTNAMED_INTF, "SetStaticHostname");
         method.append(name, /*interactive=*/false);
-        bus.call_noreply(method);
+        bus.get().call_noreply(method);
         return SystemConfigIntf::hostName(std::move(name));
     }
     catch (const std::exception& e)
diff --git a/src/system_configuration.hpp b/src/system_configuration.hpp
index b3d91d3..4eceec0 100644
--- a/src/system_configuration.hpp
+++ b/src/system_configuration.hpp
@@ -3,6 +3,7 @@
 #include <sdbusplus/bus.hpp>
 #include <sdbusplus/bus/match.hpp>
 #include <sdbusplus/server/object.hpp>
+#include <stdplus/pinned.hpp>
 #include <stdplus/zstring.hpp>
 #include <string>
 #include <xyz/openbmc_project/Network/SystemConfiguration/server.hpp>
@@ -27,19 +28,16 @@
 class SystemConfiguration : public Iface
 {
   public:
-    SystemConfiguration() = default;
-    SystemConfiguration(const SystemConfiguration&) = delete;
-    SystemConfiguration& operator=(const SystemConfiguration&) = delete;
     SystemConfiguration(SystemConfiguration&&) = delete;
     SystemConfiguration& operator=(SystemConfiguration&&) = delete;
-    virtual ~SystemConfiguration() = default;
 
     /** @brief Constructor to put object onto bus at a dbus path.
      *  @param[in] bus - Bus to attach to.
      *  @param[in] objPath - Path to attach at.
      *  @param[in] parent - Parent object.
      */
-    SystemConfiguration(sdbusplus::bus_t& bus, stdplus::const_zstring objPath);
+    SystemConfiguration(stdplus::PinnedRef<sdbusplus::bus_t> bus,
+                        stdplus::const_zstring objPath);
 
     /** @brief set the hostname of the system.
      *  @param[in] name - host name of the system.
@@ -48,7 +46,7 @@
 
   private:
     /** @brief Persistent sdbusplus DBus bus connection. */
-    sdbusplus::bus_t& bus;
+    stdplus::PinnedRef<sdbusplus::bus_t> bus;
 
     /** @brief Monitor for hostname changes */
     sdbusplus::bus::match_t hostnamePropMatch;
diff --git a/test/test_ethernet_interface.cpp b/test/test_ethernet_interface.cpp
index e9f3973..884224b 100644
--- a/test/test_ethernet_interface.cpp
+++ b/test/test_ethernet_interface.cpp
@@ -24,7 +24,7 @@
 class TestEthernetInterface : public stdplus::gtest::TestWithTmp
 {
   public:
-    sdbusplus::bus_t bus;
+    stdplus::Pinned<sdbusplus::bus_t> bus;
     std::filesystem::path confDir;
     TestManager manager;
     MockEthernetInterface interface;
@@ -36,8 +36,9 @@
     {
     }
 
-    static MockEthernetInterface makeInterface(sdbusplus::bus_t& bus,
-                                               TestManager& manager)
+    static MockEthernetInterface
+        makeInterface(stdplus::PinnedRef<sdbusplus::bus_t> bus,
+                      TestManager& manager)
     {
         AllIntfInfo info{InterfaceInfo{.idx = 1, .flags = 0, .name = "test0"}};
         return {bus, manager, info, "/xyz/openbmc_test/network"sv,
diff --git a/test/test_network_manager.cpp b/test/test_network_manager.cpp
index 2e51ac1..7e39665 100644
--- a/test/test_network_manager.cpp
+++ b/test/test_network_manager.cpp
@@ -19,7 +19,7 @@
 class TestNetworkManager : public stdplus::gtest::TestWithTmp
 {
   protected:
-    sdbusplus::bus_t bus;
+    stdplus::Pinned<sdbusplus::bus_t> bus;
     TestManager manager;
     TestNetworkManager() :
         bus(sdbusplus::bus::new_default()),
diff --git a/test/test_network_manager.hpp b/test/test_network_manager.hpp
index eaf853b..68d5088 100644
--- a/test/test_network_manager.hpp
+++ b/test/test_network_manager.hpp
@@ -32,7 +32,8 @@
 
 struct TestManager : TestManagerData, Manager
 {
-    inline TestManager(sdbusplus::bus_t& bus, stdplus::zstring_view path,
+    inline TestManager(stdplus::PinnedRef<sdbusplus::bus_t> bus,
+                       stdplus::zstring_view path,
                        const std::filesystem::path& dir) :
         Manager(bus, reloadForManager(), path, dir)
     {