Skip to content

Commit

Permalink
Watch NTP server change in timesyncd
Browse files Browse the repository at this point in the history
In phosphor-networkd, network-provided NTP servers are updated only at
the time of interface creation. Any modifications to the NTP server
are not reflected in networkd until the next restart. As of systemd
version 255, timesyncd emits a property changed signal when the
LinkNTPServers property is updated. This commit implements a watch for
the signal from timesyncd, ensuring that phosphor-networkd loads the
updated NTP servers dynamically.

Also, any change in the dynamic NTP servers when system is in manual
mode as the timesyncd is disabled. However the change is not updated
even when the system is switched to NTP mode. This commit also consists
of the changes to the reload the NTP servers whenever it switches from
manual to NTP mode.

Tested by:

1. Connected the BMC to a DHCP server
2. Configured the DHCP server with the NTP server IP.
3. Verified that timesyncd is updated with the new NTP server IP.
4. Confirmed that phosphor-networkd receives the signal and updates
the D-Bus with the latest NTP servers from timesyncd.
5. Switched to manual mode, updated the NTP servers in DHCP. Verified
the updated NTP server is reflected in networkd.

Change-Id: I6a58e44795e60490d8bee106548de043be40bfe8
Signed-off-by: Jishnu CM <[email protected]>
  • Loading branch information
cm-jishnu authored and cmjishnu committed Oct 24, 2024
1 parent 16f784b commit cae0c8c
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 0 deletions.
37 changes: 37 additions & 0 deletions src/ethernet_interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1249,5 +1249,42 @@ void EthernetInterface::watchNTPServers()
});
}

void EthernetInterface::watchTimeSyncActiveState()
{
activeStateMatch = std::make_unique<sdbusplus::bus::match::match>(
bus,
"type='signal',member='PropertiesChanged',interface='org.freedesktop."
"DBus.Properties',path='/org/freedesktop/systemd1/unit/systemd_2dtimesyncd_2eservice',"
"arg0='org.freedesktop.systemd1.Unit'",
[this](sdbusplus::message::message& msg) {
if (msg.is_method_error())
{
return;
}

std::string interface;
std::map<std::string, std::variant<std::string>> changedProperties;
std::vector<std::string> invalidatedProperties;
msg.read(interface, changedProperties, invalidatedProperties);

if (interface == "org.freedesktop.systemd1.Unit")
{
auto it = changedProperties.find("ActiveState");
if (it != changedProperties.end())
{
std::string activeState = std::get<std::string>(it->second);
if (activeState == "active" || activeState == "inactive")
{
lg2::info("systemd-timesyncd switched to : {SYD_STATE}",
"SYD_STATE", activeState);
config::Parser config(config::pathForIntfConf(
manager.get().getConfDir(), interfaceName()));
loadNTPServers(config);
}
}
}
});
}

} // namespace network
} // namespace phosphor
13 changes: 13 additions & 0 deletions src/ethernet_interface.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,10 @@ class EthernetInterface : public Ifaces
*/
void watchNTPServers();

/** @brief Function to watch status of systemd timesyncd.
*/
void watchTimeSyncActiveState();

/** @brief Function to create ipAddress dbus object.
* @param[in] addressType - Type of ip address.
* @param[in] ipAddress- IP address.
Expand Down Expand Up @@ -307,6 +311,15 @@ class EthernetInterface : public Ifaces
bool originIsManuallyAssigned(IP::AddressOrigin origin);

std::unique_ptr<sdbusplus::bus::match::match> ntpServerMatch;
std::unique_ptr<sdbusplus::bus::match::match> activeStateMatch;

int handleNCSITimeout();

std::filesystem::path ncsiTimeoutPath;
std::filesystem::path ncsiWatchDriver;
std::string ncsiWatchDeviceName;

friend struct NCSITimeoutWatch;
};

} // namespace network
Expand Down
1 change: 1 addition & 0 deletions src/network_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ void Manager::createInterface(const AllIntfInfo& info, bool enabled)
intf->loadNTPServers(config);
intf->loadStaticGateways(config);
intf->watchNTPServers();
intf->watchTimeSyncActiveState();
auto ptr = intf.get();
interfaces.insert_or_assign(*info.intf.name, std::move(intf));
interfacesByIdx.insert_or_assign(info.intf.idx, ptr);
Expand Down

0 comments on commit cae0c8c

Please sign in to comment.