blob: 4ff9e17ad11bc2d5db6b6ef33c1b2282ba01faa0 [file] [log] [blame]
Gunnar Mills57d9c502018-09-14 14:42:34 -05001#include "config.h"
2
Ratan Gupta6811f822017-04-14 16:34:56 +05303#include "network_manager.hpp"
Patrick Venture189d44e2018-07-09 12:30:59 -07004
Ratan Gupta5978dd12017-07-25 13:47:13 +05305#include "ipaddress.hpp"
Patrick Venture189d44e2018-07-09 12:30:59 -07006#include "network_config.hpp"
William A. Kennington III3a70fa22018-09-20 18:48:20 -07007#include "types.hpp"
Patrick Venture189d44e2018-07-09 12:30:59 -07008#include "util.hpp"
Ratan Gupta738a67f2017-04-21 10:38:05 +05309
Ratan Gupta6811f822017-04-14 16:34:56 +053010#include <arpa/inet.h>
11#include <dirent.h>
12#include <net/if.h>
13
Patrick Venture189d44e2018-07-09 12:30:59 -070014#include <algorithm>
15#include <bitset>
Manojkiran Edacc099a82020-05-11 14:25:16 +053016#include <filesystem>
Patrick Venture189d44e2018-07-09 12:30:59 -070017#include <fstream>
18#include <map>
19#include <phosphor-logging/elog-errors.hpp>
20#include <phosphor-logging/log.hpp>
Michael Tritz29f2fd62017-05-22 15:27:26 -050021#include <string>
Patrick Venture189d44e2018-07-09 12:30:59 -070022#include <xyz/openbmc_project/Common/error.hpp>
Ratan Gupta6811f822017-04-14 16:34:56 +053023
William A. Kennington IIIf1aa51c2019-02-12 19:58:11 -080024constexpr char SYSTEMD_BUSNAME[] = "org.freedesktop.systemd1";
25constexpr char SYSTEMD_PATH[] = "/org/freedesktop/systemd1";
26constexpr char SYSTEMD_INTERFACE[] = "org.freedesktop.systemd1.Manager";
Manojkiran Edacc099a82020-05-11 14:25:16 +053027constexpr auto FirstBootFile = "/var/lib/network/firstBoot_";
William A. Kennington IIIf1aa51c2019-02-12 19:58:11 -080028
Ratan Gupta6811f822017-04-14 16:34:56 +053029namespace phosphor
30{
31namespace network
32{
Ratan Gupta82549cc2017-04-21 08:45:23 +053033
William A. Kennington III3a70fa22018-09-20 18:48:20 -070034extern std::unique_ptr<Timer> refreshObjectTimer;
35extern std::unique_ptr<Timer> restartTimer;
Ratan Gupta6811f822017-04-14 16:34:56 +053036using namespace phosphor::logging;
Ratan Guptaef85eb92017-06-15 08:57:54 +053037using namespace sdbusplus::xyz::openbmc_project::Common::Error;
Ratan Gupta6811f822017-04-14 16:34:56 +053038
Ratan Gupta255d5142017-08-10 09:02:08 +053039Manager::Manager(sdbusplus::bus::bus& bus, const char* objPath,
Gunnar Mills57d9c502018-09-14 14:42:34 -050040 const std::string& path) :
Ratan Gupta29b0e432017-05-25 12:51:40 +053041 details::VLANCreateIface(bus, objPath, true),
Gunnar Mills57d9c502018-09-14 14:42:34 -050042 bus(bus), objectPath(objPath)
Ratan Gupta6811f822017-04-14 16:34:56 +053043{
Ratan Gupta255d5142017-08-10 09:02:08 +053044 fs::path confDir(path);
45 setConfDir(confDir);
Ratan Guptaef85eb92017-06-15 08:57:54 +053046}
47
Ratan Guptab610caf2017-09-19 09:33:51 +053048bool Manager::createDefaultNetworkFiles(bool force)
49{
50 auto isCreated = false;
51 try
52 {
53 // Directory would have created before with
54 // setConfDir function.
55 if (force)
56 {
57 // Factory Reset case
58 // we need to forcefully write the files
59 // so delete the existing ones.
60 if (fs::is_directory(confDir))
61 {
62 for (const auto& file : fs::directory_iterator(confDir))
63 {
64 fs::remove(file.path());
65 }
66 }
67 }
68
69 auto interfaceStrList = getInterfaces();
70 for (const auto& interface : interfaceStrList)
71 {
Michael Tritz08c34f42017-10-16 14:59:09 -050072 // if the interface has '.' in the name, it means that this is a
73 // VLAN - don't create the network file.
74 if (interface.find(".") != std::string::npos)
75 {
76 continue;
77 }
78
Ratan Guptab610caf2017-09-19 09:33:51 +053079 auto fileName = systemd::config::networkFilePrefix + interface +
Gunnar Mills57d9c502018-09-14 14:42:34 -050080 systemd::config::networkFileSuffix;
Ratan Guptab610caf2017-09-19 09:33:51 +053081
82 fs::path filePath = confDir;
83 filePath /= fileName;
84
85 // create the interface specific network file
86 // if not exist or we forcefully wants to write
87 // the network file.
88
89 if (force || !fs::is_regular_file(filePath.string()))
90 {
91 bmc::writeDHCPDefault(filePath.string(), interface);
92 log<level::INFO>("Created the default network file.",
Gunnar Mills57d9c502018-09-14 14:42:34 -050093 entry("INTERFACE=%s", interface.c_str()));
Ratan Guptab610caf2017-09-19 09:33:51 +053094 isCreated = true;
95 }
96 }
97 }
Patrick Williams5758db32021-10-06 12:29:22 -050098 catch (const std::exception& e)
Ratan Guptab610caf2017-09-19 09:33:51 +053099 {
100 log<level::ERR>("Unable to create the default network file");
101 }
Alexander Filippov1ea35992021-03-26 13:10:05 +0300102
103 if (isCreated)
104 {
105 // if files created restart the network.
106 // don't need to call the create child objects as eventhandler
107 // will create it.
108 restartSystemdUnit(phosphor::network::networkdService);
109 }
110
Ratan Guptab610caf2017-09-19 09:33:51 +0530111 return isCreated;
112}
113
Ratan Guptaef85eb92017-06-15 08:57:54 +0530114void Manager::setConfDir(const fs::path& dir)
115{
116 confDir = dir;
Ratan Gupta255d5142017-08-10 09:02:08 +0530117
118 if (!fs::exists(confDir))
119 {
120 if (!fs::create_directories(confDir))
121 {
122 log<level::ERR>("Unable to create the network conf dir",
123 entry("DIR=%s", confDir.c_str()));
124 elog<InternalFailure>();
125 }
126 }
Ratan Gupta29b0e432017-05-25 12:51:40 +0530127}
128
129void Manager::createInterfaces()
130{
Gunnar Mills57d9c502018-09-14 14:42:34 -0500131 // clear all the interfaces first
Ratan Guptaef85eb92017-06-15 08:57:54 +0530132 interfaces.clear();
Ratan Gupta29b0e432017-05-25 12:51:40 +0530133
Ratan Guptafd4b0f02017-09-16 06:01:24 +0530134 auto interfaceStrList = getInterfaces();
Ratan Gupta6811f822017-04-14 16:34:56 +0530135
Ratan Guptafd4b0f02017-09-16 06:01:24 +0530136 for (auto& interface : interfaceStrList)
Ratan Gupta6811f822017-04-14 16:34:56 +0530137 {
Ratan Gupta29b0e432017-05-25 12:51:40 +0530138 fs::path objPath = objectPath;
Ratan Guptafd4b0f02017-09-16 06:01:24 +0530139 auto index = interface.find(".");
Ratan Gupta92bc2fe2017-07-26 22:40:21 +0530140
141 // interface can be of vlan type or normal ethernet interface.
142 // vlan interface looks like "interface.vlanid",so here by looking
143 // at the interface name we decide that we need
144 // to create the vlaninterface or normal physical interface.
145 if (index != std::string::npos)
146 {
Gunnar Mills57d9c502018-09-14 14:42:34 -0500147 // it is vlan interface
Ratan Guptafd4b0f02017-09-16 06:01:24 +0530148 auto interfaceName = interface.substr(0, index);
149 auto vlanid = interface.substr(index + 1);
Ratan Gupta92bc2fe2017-07-26 22:40:21 +0530150 uint32_t vlanInt = std::stoul(vlanid);
151
Ratan Guptafd4b0f02017-09-16 06:01:24 +0530152 interfaces[interfaceName]->loadVLAN(vlanInt);
Ratan Gupta6e8df632017-08-13 09:41:58 +0530153 continue;
Ratan Gupta92bc2fe2017-07-26 22:40:21 +0530154 }
Ratan Guptafd4b0f02017-09-16 06:01:24 +0530155 // normal ethernet interface
156 objPath /= interface;
Ratan Gupta6811f822017-04-14 16:34:56 +0530157
Ratan Guptafd4b0f02017-09-16 06:01:24 +0530158 auto dhcp = getDHCPValue(confDir, interface);
Ratan Gupta34f96d62017-06-15 09:16:22 +0530159
Gunnar Mills57d9c502018-09-14 14:42:34 -0500160 auto intf = std::make_shared<phosphor::network::EthernetInterface>(
161 bus, objPath.string(), dhcp, *this);
Ratan Gupta92bc2fe2017-07-26 22:40:21 +0530162
163 intf->createIPAddressObjects();
William A. Kennington III08505792019-01-30 16:00:04 -0800164 intf->createStaticNeighborObjects();
Manojkiran Edaacd6dd52019-10-15 15:00:51 +0530165 intf->loadNameServers();
Ratan Gupta92bc2fe2017-07-26 22:40:21 +0530166
Gunnar Mills57d9c502018-09-14 14:42:34 -0500167 this->interfaces.emplace(
168 std::make_pair(std::move(interface), std::move(intf)));
Ratan Gupta6811f822017-04-14 16:34:56 +0530169 }
170}
171
Ratan Guptaef85eb92017-06-15 08:57:54 +0530172void Manager::createChildObjects()
173{
174 // creates the ethernet interface dbus object.
175 createInterfaces();
Ratan Guptae05083a2017-09-16 07:12:11 +0530176
177 systemConf.reset(nullptr);
178 dhcpConf.reset(nullptr);
179
Ratan Guptaef85eb92017-06-15 08:57:54 +0530180 fs::path objPath = objectPath;
181 objPath /= "config";
Ratan Guptae05083a2017-09-16 07:12:11 +0530182
183 // create the system conf object.
Ratan Guptaef85eb92017-06-15 08:57:54 +0530184 systemConf = std::make_unique<phosphor::network::SystemConfiguration>(
Gunnar Mills57d9c502018-09-14 14:42:34 -0500185 bus, objPath.string(), *this);
Ratan Guptad16f88c2017-07-11 17:47:57 +0530186 // create the dhcp conf object.
187 objPath /= "dhcp";
188 dhcpConf = std::make_unique<phosphor::network::dhcp::Configuration>(
Gunnar Mills57d9c502018-09-14 14:42:34 -0500189 bus, objPath.string(), *this);
Ratan Guptaef85eb92017-06-15 08:57:54 +0530190}
191
Patrick Williams8173e022021-05-01 06:39:41 -0500192ObjectPath Manager::vlan(IntfName interfaceName, uint32_t id)
Ratan Gupta6811f822017-04-14 16:34:56 +0530193{
William A. Kennington IIIf4b4ff82019-04-09 19:06:52 -0700194 return interfaces[interfaceName]->createVLAN(id);
Ratan Gupta6811f822017-04-14 16:34:56 +0530195}
196
Michael Tritz29f2fd62017-05-22 15:27:26 -0500197void Manager::reset()
198{
Gunnar Mills57d9c502018-09-14 14:42:34 -0500199 if (!createDefaultNetworkFiles(true))
Michael Tritz29f2fd62017-05-22 15:27:26 -0500200 {
Ratan Guptab610caf2017-09-19 09:33:51 +0530201 log<level::ERR>("Network Factory Reset failed.");
202 return;
203 // TODO: openbmc/openbmc#1721 - Log ResetFailed error here.
Michael Tritz29f2fd62017-05-22 15:27:26 -0500204 }
205
Ratan Guptab610caf2017-09-19 09:33:51 +0530206 log<level::INFO>("Network Factory Reset done.");
Michael Tritz29f2fd62017-05-22 15:27:26 -0500207}
208
Ratan Gupta4f1c18b2017-05-25 12:59:35 +0530209// Need to merge the below function with the code which writes the
210// config file during factory reset.
Gunnar Mills57d9c502018-09-14 14:42:34 -0500211// TODO openbmc/openbmc#1751
Ratan Gupta4f1c18b2017-05-25 12:59:35 +0530212void Manager::writeToConfigurationFile()
213{
214 // write all the static ip address in the systemd-network conf file
Ratan Gupta4f1c18b2017-05-25 12:59:35 +0530215 for (const auto& intf : interfaces)
216 {
Ratan Gupta2b106532017-07-25 16:05:02 +0530217 intf.second->writeConfigurationFile();
Ratan Gupta4f1c18b2017-05-25 12:59:35 +0530218 }
Ratan Gupta16f12882017-09-22 18:26:11 +0530219 restartTimers();
Ratan Guptae05083a2017-09-16 07:12:11 +0530220}
221
William A. Kennington III6f39c5e2021-05-13 18:39:23 -0700222#ifdef SYNC_MAC_FROM_INVENTORY
Manojkiran Edacc099a82020-05-11 14:25:16 +0530223void Manager::setFistBootMACOnInterface(
224 const std::pair<std::string, std::string>& inventoryEthPair)
225{
226 for (const auto& interface : interfaces)
227 {
228 if (interface.first == inventoryEthPair.first)
229 {
230 auto returnMAC =
Patrick Williams6aef7692021-05-01 06:39:41 -0500231 interface.second->macAddress(inventoryEthPair.second);
Manojkiran Edacc099a82020-05-11 14:25:16 +0530232 if (returnMAC == inventoryEthPair.second)
233 {
234 log<level::INFO>("Set the MAC on "),
235 entry("interface : ", interface.first.c_str()),
236 entry("MAC : ", inventoryEthPair.second.c_str());
237 std::error_code ec;
238 if (std::filesystem::is_directory("/var/lib/network", ec))
239 {
240 std::ofstream persistentFile(FirstBootFile +
241 interface.first);
242 }
243 break;
244 }
245 else
246 {
247 log<level::INFO>("MAC is Not Set on ethernet Interface");
248 }
249 }
250 }
251}
252
253#endif
254
Ratan Gupta16f12882017-09-22 18:26:11 +0530255void Manager::restartTimers()
Ratan Guptae05083a2017-09-16 07:12:11 +0530256{
257 using namespace std::chrono;
Ratan Gupta16f12882017-09-22 18:26:11 +0530258 if (refreshObjectTimer && restartTimer)
Ratan Guptae05083a2017-09-16 07:12:11 +0530259 {
William A. Kennington III3a70fa22018-09-20 18:48:20 -0700260 restartTimer->restartOnce(restartTimeout);
261 refreshObjectTimer->restartOnce(refreshTimeout);
Ratan Guptae05083a2017-09-16 07:12:11 +0530262 }
Ratan Gupta70c7e5b2017-07-12 11:41:55 +0530263}
264
Ratan Gupta35297172018-11-28 18:40:16 +0530265void Manager::restartSystemdUnit(const std::string& unit)
266{
267 try
268 {
269 auto method = bus.new_method_call(SYSTEMD_BUSNAME, SYSTEMD_PATH,
William A. Kennington IIIa5b1b0d2018-11-30 17:03:57 -0800270 SYSTEMD_INTERFACE, "ResetFailedUnit");
271 method.append(unit);
272 bus.call_noreply(method);
273 }
Patrick Williamsb108fd72021-09-02 09:45:39 -0500274 catch (const sdbusplus::exception::exception& ex)
William A. Kennington IIIa5b1b0d2018-11-30 17:03:57 -0800275 {
276 log<level::ERR>("Failed to reset failed unit",
277 entry("UNIT=%s", unit.c_str()),
278 entry("ERR=%s", ex.what()));
279 elog<InternalFailure>();
280 }
281
282 try
283 {
284 auto method = bus.new_method_call(SYSTEMD_BUSNAME, SYSTEMD_PATH,
Ratan Gupta35297172018-11-28 18:40:16 +0530285 SYSTEMD_INTERFACE, "RestartUnit");
286 method.append(unit.c_str(), "replace");
287 bus.call_noreply(method);
288 }
Patrick Williamsb108fd72021-09-02 09:45:39 -0500289 catch (const sdbusplus::exception::exception& ex)
Ratan Gupta35297172018-11-28 18:40:16 +0530290 {
Ratan Guptaabbd85c2020-06-12 09:57:02 +0530291 log<level::ERR>("Failed to restart service", entry("ERR=%s", ex.what()),
292 entry("UNIT=%s", unit.c_str()));
Ratan Gupta35297172018-11-28 18:40:16 +0530293 elog<InternalFailure>();
294 }
295}
296
Gunnar Mills57d9c502018-09-14 14:42:34 -0500297} // namespace network
298} // namespace phosphor