Skip to content

Commit

Permalink
[#3017] create checkDetectIfaces to do persistent checks
Browse files Browse the repository at this point in the history
  • Loading branch information
Razvan Becheriu authored and andrei-pavel committed Oct 17, 2023
1 parent 34e98d1 commit bd70361
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 29 deletions.
12 changes: 8 additions & 4 deletions src/bin/dhcp4/tests/ctrl_dhcp4_srv_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,8 @@ class CtrlChannelDhcpv4SrvTest : public ::testing::Test {
}
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
Expand All @@ -135,7 +136,8 @@ class CtrlChannelDhcpv4SrvTest : public ::testing::Test {

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();
Expand Down Expand Up @@ -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"));
Expand Down Expand Up @@ -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"));
Expand Down
12 changes: 8 additions & 4 deletions src/bin/dhcp6/tests/ctrl_dhcp6_srv_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -156,15 +156,17 @@ class CtrlChannelDhcpv6SrvTest : public CtrlDhcpv6SrvTest {
}
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
~CtrlChannelDhcpv6SrvTest() {
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();
Expand Down Expand Up @@ -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"));
Expand Down Expand Up @@ -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"));
Expand Down
16 changes: 12 additions & 4 deletions src/lib/dhcp/iface_mgr.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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.
Expand All @@ -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
Expand Down Expand Up @@ -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()));
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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
22 changes: 17 additions & 5 deletions src/lib/dhcp/iface_mgr.h
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ class Iface : public boost::noncopyable {
/// @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.
Expand Down Expand Up @@ -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.
///
Expand Down Expand Up @@ -804,6 +806,16 @@ class IfaceMgr : public boost::noncopyable {
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
Expand Down Expand Up @@ -1590,8 +1602,8 @@ class IfaceMgr : public boost::noncopyable {
/// @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.
Expand Down
4 changes: 0 additions & 4 deletions src/lib/dhcp/iface_mgr_bsd.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 0 additions & 4 deletions src/lib/dhcp/iface_mgr_linux.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
4 changes: 0 additions & 4 deletions src/lib/dhcp/iface_mgr_sun.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit bd70361

Please sign in to comment.