Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 1 | #include "config.h" |
| 2 | |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 3 | #include "network_manager.hpp" |
Patrick Venture | 189d44e | 2018-07-09 12:30:59 -0700 | [diff] [blame] | 4 | |
William A. Kennington III | 09f3a4a | 2022-10-25 02:59:16 -0700 | [diff] [blame] | 5 | #include "config_parser.hpp" |
Ratan Gupta | 5978dd1 | 2017-07-25 13:47:13 +0530 | [diff] [blame] | 6 | #include "ipaddress.hpp" |
William A. Kennington III | 2e09d27 | 2022-10-14 17:15:00 -0700 | [diff] [blame] | 7 | #include "system_queries.hpp" |
William A. Kennington III | 3a70fa2 | 2018-09-20 18:48:20 -0700 | [diff] [blame] | 8 | #include "types.hpp" |
Ratan Gupta | 738a67f | 2017-04-21 10:38:05 +0530 | [diff] [blame] | 9 | |
Manojkiran Eda | cc099a8 | 2020-05-11 14:25:16 +0530 | [diff] [blame] | 10 | #include <filesystem> |
Patrick Venture | 189d44e | 2018-07-09 12:30:59 -0700 | [diff] [blame] | 11 | #include <fstream> |
Patrick Venture | 189d44e | 2018-07-09 12:30:59 -0700 | [diff] [blame] | 12 | #include <phosphor-logging/elog-errors.hpp> |
| 13 | #include <phosphor-logging/log.hpp> |
Patrick Venture | 189d44e | 2018-07-09 12:30:59 -0700 | [diff] [blame] | 14 | #include <xyz/openbmc_project/Common/error.hpp> |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 15 | |
William A. Kennington III | f1aa51c | 2019-02-12 19:58:11 -0800 | [diff] [blame] | 16 | constexpr char SYSTEMD_BUSNAME[] = "org.freedesktop.systemd1"; |
| 17 | constexpr char SYSTEMD_PATH[] = "/org/freedesktop/systemd1"; |
| 18 | constexpr char SYSTEMD_INTERFACE[] = "org.freedesktop.systemd1.Manager"; |
Manojkiran Eda | cc099a8 | 2020-05-11 14:25:16 +0530 | [diff] [blame] | 19 | constexpr auto FirstBootFile = "/var/lib/network/firstBoot_"; |
William A. Kennington III | f1aa51c | 2019-02-12 19:58:11 -0800 | [diff] [blame] | 20 | |
William A. Kennington III | 56ecc78 | 2021-10-07 18:44:50 -0700 | [diff] [blame] | 21 | constexpr char NETWORKD_BUSNAME[] = "org.freedesktop.network1"; |
| 22 | constexpr char NETWORKD_PATH[] = "/org/freedesktop/network1"; |
| 23 | constexpr char NETWORKD_INTERFACE[] = "org.freedesktop.network1.Manager"; |
| 24 | |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 25 | namespace phosphor |
| 26 | { |
| 27 | namespace network |
| 28 | { |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 29 | |
William A. Kennington III | d41db38 | 2021-11-09 20:42:29 -0800 | [diff] [blame] | 30 | extern std::unique_ptr<Timer> refreshObjectTimer; |
William A. Kennington III | c7cf25f | 2021-11-09 16:16:59 -0800 | [diff] [blame] | 31 | extern std::unique_ptr<Timer> reloadTimer; |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 32 | using namespace phosphor::logging; |
Ratan Gupta | ef85eb9 | 2017-06-15 08:57:54 +0530 | [diff] [blame] | 33 | using namespace sdbusplus::xyz::openbmc_project::Common::Error; |
Jiaqing Zhao | b685cb6 | 2022-04-12 22:57:34 +0800 | [diff] [blame] | 34 | using Argument = xyz::openbmc_project::Common::InvalidArgument; |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 35 | |
Patrick Williams | c38b071 | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 36 | Manager::Manager(sdbusplus::bus_t& bus, const char* objPath, |
William A. Kennington III | be3bd2f | 2022-10-11 14:11:27 -0700 | [diff] [blame] | 37 | const fs::path& confDir) : |
Patrick Williams | 166b959 | 2022-03-30 16:09:16 -0500 | [diff] [blame] | 38 | details::VLANCreateIface(bus, objPath, |
| 39 | details::VLANCreateIface::action::defer_emit), |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 40 | bus(bus), objectPath(objPath) |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 41 | { |
Ratan Gupta | 255d514 | 2017-08-10 09:02:08 +0530 | [diff] [blame] | 42 | setConfDir(confDir); |
Ratan Gupta | ef85eb9 | 2017-06-15 08:57:54 +0530 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | void Manager::setConfDir(const fs::path& dir) |
| 46 | { |
| 47 | confDir = dir; |
Ratan Gupta | 255d514 | 2017-08-10 09:02:08 +0530 | [diff] [blame] | 48 | |
| 49 | if (!fs::exists(confDir)) |
| 50 | { |
| 51 | if (!fs::create_directories(confDir)) |
| 52 | { |
| 53 | log<level::ERR>("Unable to create the network conf dir", |
| 54 | entry("DIR=%s", confDir.c_str())); |
| 55 | elog<InternalFailure>(); |
| 56 | } |
| 57 | } |
Ratan Gupta | 29b0e43 | 2017-05-25 12:51:40 +0530 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | void Manager::createInterfaces() |
| 61 | { |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 62 | // clear all the interfaces first |
Ratan Gupta | ef85eb9 | 2017-06-15 08:57:54 +0530 | [diff] [blame] | 63 | interfaces.clear(); |
William A. Kennington III | 67b09da | 2022-10-31 14:09:53 -0700 | [diff] [blame] | 64 | interfacesByIdx.clear(); |
William A. Kennington III | fd862be | 2022-10-09 18:40:55 -0700 | [diff] [blame] | 65 | for (auto& interface : system::getInterfaces()) |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 66 | { |
William A. Kennington III | fd862be | 2022-10-09 18:40:55 -0700 | [diff] [blame] | 67 | config::Parser config( |
| 68 | config::pathForIntfConf(confDir, *interface.name)); |
William A. Kennington III | 09f3a4a | 2022-10-25 02:59:16 -0700 | [diff] [blame] | 69 | auto intf = std::make_unique<EthernetInterface>(bus, *this, interface, |
| 70 | objectPath, config); |
Ratan Gupta | 92bc2fe | 2017-07-26 22:40:21 +0530 | [diff] [blame] | 71 | intf->createIPAddressObjects(); |
William A. Kennington III | 0850579 | 2019-01-30 16:00:04 -0800 | [diff] [blame] | 72 | intf->createStaticNeighborObjects(); |
William A. Kennington III | a520a39 | 2022-08-08 12:17:34 -0700 | [diff] [blame] | 73 | intf->loadNameServers(config); |
Asmitha Karunanithi | 003b8b7 | 2022-01-06 04:17:59 -0600 | [diff] [blame] | 74 | intf->loadNTPServers(config); |
William A. Kennington III | 67b09da | 2022-10-31 14:09:53 -0700 | [diff] [blame] | 75 | auto ptr = intf.get(); |
| 76 | interfaces.emplace(std::move(*interface.name), std::move(intf)); |
| 77 | interfacesByIdx.emplace(interface.idx, ptr); |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 78 | } |
| 79 | } |
| 80 | |
Ratan Gupta | ef85eb9 | 2017-06-15 08:57:54 +0530 | [diff] [blame] | 81 | void Manager::createChildObjects() |
| 82 | { |
William A. Kennington III | e056484 | 2021-10-23 16:02:22 -0700 | [diff] [blame] | 83 | routeTable.refresh(); |
| 84 | |
Ratan Gupta | ef85eb9 | 2017-06-15 08:57:54 +0530 | [diff] [blame] | 85 | // creates the ethernet interface dbus object. |
| 86 | createInterfaces(); |
Ratan Gupta | e05083a | 2017-09-16 07:12:11 +0530 | [diff] [blame] | 87 | |
| 88 | systemConf.reset(nullptr); |
| 89 | dhcpConf.reset(nullptr); |
| 90 | |
Ratan Gupta | ef85eb9 | 2017-06-15 08:57:54 +0530 | [diff] [blame] | 91 | fs::path objPath = objectPath; |
| 92 | objPath /= "config"; |
Ratan Gupta | e05083a | 2017-09-16 07:12:11 +0530 | [diff] [blame] | 93 | |
| 94 | // create the system conf object. |
Ratan Gupta | ef85eb9 | 2017-06-15 08:57:54 +0530 | [diff] [blame] | 95 | systemConf = std::make_unique<phosphor::network::SystemConfiguration>( |
Jiaqing Zhao | 24b5a61 | 2022-04-11 16:46:16 +0800 | [diff] [blame] | 96 | bus, objPath.string()); |
Ratan Gupta | d16f88c | 2017-07-11 17:47:57 +0530 | [diff] [blame] | 97 | // create the dhcp conf object. |
| 98 | objPath /= "dhcp"; |
| 99 | dhcpConf = std::make_unique<phosphor::network::dhcp::Configuration>( |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 100 | bus, objPath.string(), *this); |
Ratan Gupta | ef85eb9 | 2017-06-15 08:57:54 +0530 | [diff] [blame] | 101 | } |
| 102 | |
William A. Kennington III | 085bbdc | 2022-10-05 02:45:37 -0700 | [diff] [blame] | 103 | ObjectPath Manager::vlan(std::string interfaceName, uint32_t id) |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 104 | { |
Jiaqing Zhao | b685cb6 | 2022-04-12 22:57:34 +0800 | [diff] [blame] | 105 | if (id == 0 || id >= 4095) |
| 106 | { |
| 107 | log<level::ERR>("VLAN ID is not valid", entry("VLANID=%u", id)); |
| 108 | elog<InvalidArgument>( |
| 109 | Argument::ARGUMENT_NAME("VLANId"), |
| 110 | Argument::ARGUMENT_VALUE(std::to_string(id).c_str())); |
| 111 | } |
| 112 | |
William A. Kennington III | 9644479 | 2022-10-05 15:16:22 -0700 | [diff] [blame] | 113 | auto it = interfaces.find(interfaceName); |
| 114 | if (it == interfaces.end()) |
| 115 | { |
| 116 | using ResourceErr = |
| 117 | phosphor::logging::xyz::openbmc_project::Common::ResourceNotFound; |
| 118 | elog<ResourceNotFound>(ResourceErr::RESOURCE(interfaceName.c_str())); |
| 119 | } |
| 120 | return it->second->createVLAN(id); |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 121 | } |
| 122 | |
Michael Tritz | 29f2fd6 | 2017-05-22 15:27:26 -0500 | [diff] [blame] | 123 | void Manager::reset() |
| 124 | { |
William A. Kennington III | 9a1d9af | 2021-11-09 17:51:05 -0800 | [diff] [blame] | 125 | if (fs::is_directory(confDir)) |
Michael Tritz | 29f2fd6 | 2017-05-22 15:27:26 -0500 | [diff] [blame] | 126 | { |
William A. Kennington III | 9a1d9af | 2021-11-09 17:51:05 -0800 | [diff] [blame] | 127 | for (const auto& file : fs::directory_iterator(confDir)) |
| 128 | { |
| 129 | fs::remove(file.path()); |
| 130 | } |
Michael Tritz | 29f2fd6 | 2017-05-22 15:27:26 -0500 | [diff] [blame] | 131 | } |
William A. Kennington III | 9a1d9af | 2021-11-09 17:51:05 -0800 | [diff] [blame] | 132 | log<level::INFO>("Network Factory Reset queued."); |
Michael Tritz | 29f2fd6 | 2017-05-22 15:27:26 -0500 | [diff] [blame] | 133 | } |
| 134 | |
Ratan Gupta | 4f1c18b | 2017-05-25 12:59:35 +0530 | [diff] [blame] | 135 | // Need to merge the below function with the code which writes the |
| 136 | // config file during factory reset. |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 137 | // TODO openbmc/openbmc#1751 |
Ratan Gupta | 4f1c18b | 2017-05-25 12:59:35 +0530 | [diff] [blame] | 138 | void Manager::writeToConfigurationFile() |
| 139 | { |
| 140 | // write all the static ip address in the systemd-network conf file |
Ratan Gupta | 4f1c18b | 2017-05-25 12:59:35 +0530 | [diff] [blame] | 141 | for (const auto& intf : interfaces) |
| 142 | { |
Ratan Gupta | 2b10653 | 2017-07-25 16:05:02 +0530 | [diff] [blame] | 143 | intf.second->writeConfigurationFile(); |
Ratan Gupta | 4f1c18b | 2017-05-25 12:59:35 +0530 | [diff] [blame] | 144 | } |
Ratan Gupta | e05083a | 2017-09-16 07:12:11 +0530 | [diff] [blame] | 145 | } |
| 146 | |
William A. Kennington III | 6f39c5e | 2021-05-13 18:39:23 -0700 | [diff] [blame] | 147 | #ifdef SYNC_MAC_FROM_INVENTORY |
Manojkiran Eda | cc099a8 | 2020-05-11 14:25:16 +0530 | [diff] [blame] | 148 | void Manager::setFistBootMACOnInterface( |
| 149 | const std::pair<std::string, std::string>& inventoryEthPair) |
| 150 | { |
| 151 | for (const auto& interface : interfaces) |
| 152 | { |
| 153 | if (interface.first == inventoryEthPair.first) |
| 154 | { |
| 155 | auto returnMAC = |
Patrick Williams | 6aef769 | 2021-05-01 06:39:41 -0500 | [diff] [blame] | 156 | interface.second->macAddress(inventoryEthPair.second); |
Manojkiran Eda | cc099a8 | 2020-05-11 14:25:16 +0530 | [diff] [blame] | 157 | if (returnMAC == inventoryEthPair.second) |
| 158 | { |
| 159 | log<level::INFO>("Set the MAC on "), |
| 160 | entry("interface : ", interface.first.c_str()), |
| 161 | entry("MAC : ", inventoryEthPair.second.c_str()); |
| 162 | std::error_code ec; |
| 163 | if (std::filesystem::is_directory("/var/lib/network", ec)) |
| 164 | { |
| 165 | std::ofstream persistentFile(FirstBootFile + |
| 166 | interface.first); |
| 167 | } |
| 168 | break; |
| 169 | } |
| 170 | else |
| 171 | { |
| 172 | log<level::INFO>("MAC is Not Set on ethernet Interface"); |
| 173 | } |
| 174 | } |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | #endif |
| 179 | |
William A. Kennington III | 85dc57a | 2022-11-07 16:53:24 -0800 | [diff] [blame] | 180 | void Manager::reloadConfigsNoRefresh() |
William A. Kennington III | 56ecc78 | 2021-10-07 18:44:50 -0700 | [diff] [blame] | 181 | { |
William A. Kennington III | c7cf25f | 2021-11-09 16:16:59 -0800 | [diff] [blame] | 182 | reloadTimer->restartOnce(reloadTimeout); |
William A. Kennington III | 85dc57a | 2022-11-07 16:53:24 -0800 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | void Manager::reloadConfigs() |
| 186 | { |
| 187 | reloadConfigsNoRefresh(); |
William A. Kennington III | d41db38 | 2021-11-09 20:42:29 -0800 | [diff] [blame] | 188 | // Ensure that the next refresh happens after reconfiguration |
| 189 | refreshObjectTimer->setRemaining(reloadTimeout + refreshTimeout); |
William A. Kennington III | c7cf25f | 2021-11-09 16:16:59 -0800 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | void Manager::doReloadConfigs() |
| 193 | { |
William A. Kennington III | 6ff633a | 2021-11-09 17:09:12 -0800 | [diff] [blame] | 194 | for (auto& hook : reloadPreHooks) |
| 195 | { |
| 196 | try |
| 197 | { |
| 198 | hook(); |
| 199 | } |
| 200 | catch (const std::exception& ex) |
| 201 | { |
| 202 | log<level::ERR>("Failed executing reload hook, ignoring", |
| 203 | entry("ERR=%s", ex.what())); |
| 204 | } |
| 205 | } |
| 206 | reloadPreHooks.clear(); |
William A. Kennington III | 56ecc78 | 2021-10-07 18:44:50 -0700 | [diff] [blame] | 207 | try |
| 208 | { |
| 209 | auto method = bus.new_method_call(NETWORKD_BUSNAME, NETWORKD_PATH, |
| 210 | NETWORKD_INTERFACE, "Reload"); |
| 211 | bus.call_noreply(method); |
| 212 | } |
Patrick Williams | c38b071 | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 213 | catch (const sdbusplus::exception_t& ex) |
William A. Kennington III | 56ecc78 | 2021-10-07 18:44:50 -0700 | [diff] [blame] | 214 | { |
| 215 | log<level::ERR>("Failed to reload configuration", |
| 216 | entry("ERR=%s", ex.what())); |
| 217 | elog<InternalFailure>(); |
| 218 | } |
William A. Kennington III | d41db38 | 2021-11-09 20:42:29 -0800 | [diff] [blame] | 219 | // Ensure reconfiguration has enough time |
William A. Kennington III | 85dc57a | 2022-11-07 16:53:24 -0800 | [diff] [blame] | 220 | if (refreshObjectTimer->isEnabled()) |
| 221 | { |
| 222 | refreshObjectTimer->setRemaining(refreshTimeout); |
| 223 | } |
William A. Kennington III | 56ecc78 | 2021-10-07 18:44:50 -0700 | [diff] [blame] | 224 | } |
| 225 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 226 | } // namespace network |
| 227 | } // namespace phosphor |