]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
[#3017] create checkDetectIfaces to do persistent checks
authorRazvan Becheriu <razvan@isc.org>
Sat, 14 Oct 2023 05:42:17 +0000 (08:42 +0300)
committerAndrei Pavel <andrei@isc.org>
Tue, 17 Oct 2023 09:35:55 +0000 (12:35 +0300)
src/bin/dhcp4/tests/ctrl_dhcp4_srv_unittest.cc
src/bin/dhcp6/tests/ctrl_dhcp6_srv_unittest.cc
src/lib/dhcp/iface_mgr.cc
src/lib/dhcp/iface_mgr.h
src/lib/dhcp/iface_mgr_bsd.cc
src/lib/dhcp/iface_mgr_linux.cc
src/lib/dhcp/iface_mgr_sun.cc

index a47ca2c58197e72d8541fbf5673577546d50c982..963cfd12b24d40f297344cfbe8acc3fed1014e32 100644 (file)
@@ -121,7 +121,8 @@ public:
         }
         reset();
         IfaceMgr::instance().setTestMode(false);
-        IfaceMgr::instance().setDetectCallback(isc::dhcp::IfaceMgr::DetectCallback());
+        IfaceMgr::instance().setDetectCallback(std::bind(&IfaceMgr::checkDetectIfaces,
+                                               IfaceMgr::instancePtr().get(), ph::_1));
     }
 
     /// @brief Destructor
@@ -135,7 +136,8 @@ public:
 
         server_.reset();
         IfaceMgr::instance().setTestMode(false);
-        IfaceMgr::instance().setDetectCallback(isc::dhcp::IfaceMgr::DetectCallback());
+        IfaceMgr::instance().setDetectCallback(std::bind(&IfaceMgr::checkDetectIfaces,
+                                               IfaceMgr::instancePtr().get(), ph::_1));
         IfaceMgr::instance().clearIfaces();
         IfaceMgr::instance().closeSockets();
         IfaceMgr::instance().detectIfaces();
@@ -1623,7 +1625,8 @@ TEST_F(CtrlChannelDhcpv4SrvTest, configReloadValid) {
 // file is loaded correctly.
 TEST_F(CtrlChannelDhcpv4SrvTest, configReloadDetectInterfaces) {
     interfaces_ = "\"eth0\"";
-    IfacePtr eth0 = IfaceMgrTestConfig::createIface("eth0", 0);
+    IfacePtr eth0 = IfaceMgrTestConfig::createIface("eth0", ETH0_INDEX,
+                                                    "11:22:33:44:55:66");
     auto detectIfaces = [&](bool update_only) {
         if (!update_only) {
             eth0->addAddress(IOAddress("10.0.0.1"));
@@ -1662,7 +1665,8 @@ TEST_F(CtrlChannelDhcpv4SrvTest, configReloadDetectInterfaces) {
     f << cfg_txt;
     f.close();
 
-    IfacePtr eth1 = IfaceMgrTestConfig::createIface("eth1", 1);
+    IfacePtr eth1 = IfaceMgrTestConfig::createIface("eth1", ETH1_INDEX,
+                                                    "AA:BB:CC:DD:EE:FF");
     auto detectUpdateIfaces = [&](bool update_only) {
         if (!update_only) {
             eth1->addAddress(IOAddress("192.0.2.3"));
index 46372e8ccb84193558922c7e0db3e3a866703ab5..5baa867c7be9606659ed727c5fae53e574c63669 100644 (file)
@@ -156,7 +156,8 @@ public:
         }
         reset();
         IfaceMgr::instance().setTestMode(false);
-        IfaceMgr::instance().setDetectCallback(isc::dhcp::IfaceMgr::DetectCallback());
+        IfaceMgr::instance().setDetectCallback(std::bind(&IfaceMgr::checkDetectIfaces,
+                                               IfaceMgr::instancePtr().get(), ph::_1));
     }
 
     /// @brief Destructor
@@ -164,7 +165,8 @@ public:
         server_.reset();
         reset();
         IfaceMgr::instance().setTestMode(false);
-        IfaceMgr::instance().setDetectCallback(isc::dhcp::IfaceMgr::DetectCallback());
+        IfaceMgr::instance().setDetectCallback(std::bind(&IfaceMgr::checkDetectIfaces,
+                                               IfaceMgr::instancePtr().get(), ph::_1));
         IfaceMgr::instance().clearIfaces();
         IfaceMgr::instance().closeSockets();
         IfaceMgr::instance().detectIfaces();
@@ -1655,7 +1657,8 @@ TEST_F(CtrlChannelDhcpv6SrvTest, configReloadValid) {
 // file is loaded correctly.
 TEST_F(CtrlChannelDhcpv6SrvTest, configReloadDetectInterfaces) {
     interfaces_ = "\"eth0\"";
-    IfacePtr eth0 = IfaceMgrTestConfig::createIface("eth0", 0);
+    IfacePtr eth0 = IfaceMgrTestConfig::createIface("eth0", ETH0_INDEX,
+                                                    "11:22:33:44:55:66");
     auto detectIfaces = [&](bool update_only) {
         if (!update_only) {
             eth0->addAddress(IOAddress("10.0.0.1"));
@@ -1693,7 +1696,8 @@ TEST_F(CtrlChannelDhcpv6SrvTest, configReloadDetectInterfaces) {
     f << cfg_txt;
     f.close();
 
-    IfacePtr eth1 = IfaceMgrTestConfig::createIface("eth1", ETH1_INDEX);
+    IfacePtr eth1 = IfaceMgrTestConfig::createIface("eth1", ETH1_INDEX,
+                                                    "AA:BB:CC:DD:EE:FF");
     auto detectUpdateIfaces = [&](bool update_only) {
         if (!update_only) {
             eth1->addAddress(IOAddress("192.0.2.3"));
index 11f6e2f143074873ee5ef1f03ea09260e3dcac93..c23a9e9836cf29bf50c2fa1dbd8035da895f799b 100644 (file)
@@ -45,6 +45,7 @@ using namespace isc::asiolink;
 using namespace isc::util;
 using namespace isc::util::io;
 using namespace isc::util::io::internal;
+namespace ph = std::placeholders;
 
 namespace isc {
 namespace dhcp {
@@ -185,8 +186,7 @@ bool Iface::delSocket(const uint16_t sockfd) {
 IfaceMgr::IfaceMgr()
     : packet_filter_(new PktFilterInet()),
       packet_filter6_(new PktFilterInet6()),
-      test_mode_(false),
-      allow_loopback_(false) {
+      test_mode_(false), allow_loopback_(false) {
 
     // Ensure that PQMs have been created to guarantee we have
     // default packet queues in place.
@@ -197,6 +197,8 @@ IfaceMgr::IfaceMgr()
         isc_throw(Unexpected, "Failed to create PacketQueueManagers: " << ex.what());
     }
 
+    detect_callback_ = std::bind(&IfaceMgr::checkDetectIfaces, this, ph::_1);
+
     try {
 
         // required for sending/receiving packets
@@ -798,7 +800,6 @@ IfaceCollection::getIface(const unsigned int ifindex) {
     return (getIfaceInternal(ifindex, MultiThreadingMgr::instance().getMode()));
 }
 
-
 IfacePtr
 IfaceCollection::getIface(const std::string& ifname) {
     return (getIfaceInternal(ifname, MultiThreadingMgr::instance().getMode()));
@@ -1843,7 +1844,6 @@ IfaceMgr::getSocket(const isc::dhcp::Pkt6Ptr& pkt) {
         isc_throw(IfaceNotFound, "Tried to find socket for non-existent interface");
     }
 
-
     const Iface::SocketCollection& socket_collection = iface->getSockets();
 
     Iface::SocketCollection::const_iterator candidate = socket_collection.end();
@@ -1977,5 +1977,13 @@ Iface::getErrors() const {
     return errors_;
 }
 
+bool
+IfaceMgr::checkDetectIfaces(bool update_only) {
+    if (test_mode_ && update_only) {
+        return (false);
+    }
+    return (true);
+}
+
 } // end of namespace isc::dhcp
 } // end of namespace isc
index a3b0596ec7af0da450371e3b88fa8baefddd3a84..ca0a536096b71f369ca79e16cf282a55f81e956d 100644 (file)
@@ -312,7 +312,7 @@ public:
     /// @param addr address to be removed.
     ///
     /// @return true if removal was successful (address was in collection),
-    ///         false otherwise
+    /// false otherwise
     bool delAddress(const isc::asiolink::IOAddress& addr);
 
     /// @brief Adds socket descriptor to an interface.
@@ -655,11 +655,13 @@ std::function<void(const std::string& errmsg)> IfaceMgrErrorMsgCallback;
 ///
 class IfaceMgr : public boost::noncopyable {
 public:
-    /// Defines callback used when data is received over external sockets.
+    /// @brief Defines callback used when data is received over external sockets.
+    ///
     /// @param fd socket descriptor of the ready socket
     typedef std::function<void (int fd)> SocketCallback;
 
-    /// Defines callback used when detecting interfaces.
+    /// @brief Defines callback used when detecting interfaces.
+    ///
     /// @param update_only Only add interfaces that do not exist and update
     /// existing interfaces.
     ///
@@ -804,6 +806,16 @@ public:
         detect_callback_ = cb;
     }
 
+    /// @brief Check if the specific system calls used to detect interfaces
+    /// should be executed.
+    ///
+    /// @param update_only Only add interfaces that do not exist and update
+    /// existing interfaces.
+    ///
+    /// @return true if the specific system calls should be executed, false
+    /// otherwise causing the @ref detectIfaces to return immediately.
+    bool checkDetectIfaces(bool update_only);
+
     /// @brief Detects network interfaces.
     ///
     /// If the @ref detect_callback_ returns true, the specific system calls are
@@ -1590,8 +1602,8 @@ private:
     /// @brief Indicates if the IfaceMgr is in the test mode.
     bool test_mode_;
 
-    /// @brief Detect callback used to perform action before system dependent
-    /// function calls. Currently this function is used in unittests only.
+    /// @brief Detect callback used to perform actions before system dependent
+    /// function calls.
     ///
     /// If the @ref detect_callback_ returns true, the specific system calls are
     /// executed, otherwise the @ref detectIfaces will return immediately.
index 3b328cb7814f8878390c6ce90eed2b0ca83822a0..553842aa412676a3b5db7a1264afe920c1dd032a 100644 (file)
@@ -37,10 +37,6 @@ IfaceMgr::detectIfaces(bool update_only) {
         }
     }
 
-    if (isTestMode() && update_only) {
-        return;
-    }
-
     struct ifaddrs* iflist = 0;// The whole interface list
     struct ifaddrs* ifptr = 0; // The interface we're processing now
 
index 222e3d648fa04194c9c2c113e8bcd90170d93111..e4053b0664eae680ecff432faeb239f47b0c5490 100644 (file)
@@ -420,10 +420,6 @@ void IfaceMgr::detectIfaces(bool update_only) {
         }
     }
 
-    if (isTestMode() && update_only) {
-        return;
-    }
-
     // Copies of netlink messages about links will be stored here.
     Netlink::NetlinkMessages link_info;
 
index c7d7e461aeced7549293789455af150b31fb9f2b..2fd765f96f4d239a88c98f8ee957dadc2afb2faa 100644 (file)
@@ -37,10 +37,6 @@ IfaceMgr::detectIfaces(bool update_only) {
         }
     }
 
-    if (isTestMode() && update_only) {
-        return;
-    }
-
     struct ifaddrs* iflist = 0;// The whole interface list
     struct ifaddrs* ifptr = 0; // The interface we're processing now