blob: 4483631647c49b6fe43fc3238a64d9836f8cdb51 [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>
16#include <fstream>
17#include <map>
18#include <phosphor-logging/elog-errors.hpp>
19#include <phosphor-logging/log.hpp>
Michael Tritz29f2fd62017-05-22 15:27:26 -050020#include <string>
Patrick Venture189d44e2018-07-09 12:30:59 -070021#include <xyz/openbmc_project/Common/error.hpp>
Ratan Gupta6811f822017-04-14 16:34:56 +053022
William A. Kennington IIIf1aa51c2019-02-12 19:58:11 -080023constexpr char SYSTEMD_BUSNAME[] = "org.freedesktop.systemd1";
24constexpr char SYSTEMD_PATH[] = "/org/freedesktop/systemd1";
25constexpr char SYSTEMD_INTERFACE[] = "org.freedesktop.systemd1.Manager";
26
Ratan Gupta6811f822017-04-14 16:34:56 +053027namespace phosphor
28{
29namespace network
30{
Ratan Gupta82549cc2017-04-21 08:45:23 +053031
William A. Kennington III3a70fa22018-09-20 18:48:20 -070032extern std::unique_ptr<Timer> refreshObjectTimer;
33extern std::unique_ptr<Timer> restartTimer;
Ratan Gupta6811f822017-04-14 16:34:56 +053034using namespace phosphor::logging;
Ratan Guptaef85eb92017-06-15 08:57:54 +053035using namespace sdbusplus::xyz::openbmc_project::Common::Error;
Ratan Gupta6811f822017-04-14 16:34:56 +053036
Ratan Gupta255d5142017-08-10 09:02:08 +053037Manager::Manager(sdbusplus::bus::bus& bus, const char* objPath,
Gunnar Mills57d9c502018-09-14 14:42:34 -050038 const std::string& path) :
Ratan Gupta29b0e432017-05-25 12:51:40 +053039 details::VLANCreateIface(bus, objPath, true),
Gunnar Mills57d9c502018-09-14 14:42:34 -050040 bus(bus), objectPath(objPath)
Ratan Gupta6811f822017-04-14 16:34:56 +053041{
Ratan Gupta255d5142017-08-10 09:02:08 +053042 fs::path confDir(path);
43 setConfDir(confDir);
Ratan Guptaef85eb92017-06-15 08:57:54 +053044}
45
Ratan Guptab610caf2017-09-19 09:33:51 +053046bool Manager::createDefaultNetworkFiles(bool force)
47{
48 auto isCreated = false;
49 try
50 {
51 // Directory would have created before with
52 // setConfDir function.
53 if (force)
54 {
55 // Factory Reset case
56 // we need to forcefully write the files
57 // so delete the existing ones.
58 if (fs::is_directory(confDir))
59 {
60 for (const auto& file : fs::directory_iterator(confDir))
61 {
62 fs::remove(file.path());
63 }
64 }
65 }
66
67 auto interfaceStrList = getInterfaces();
68 for (const auto& interface : interfaceStrList)
69 {
Michael Tritz08c34f42017-10-16 14:59:09 -050070 // if the interface has '.' in the name, it means that this is a
71 // VLAN - don't create the network file.
72 if (interface.find(".") != std::string::npos)
73 {
74 continue;
75 }
76
Ratan Guptab610caf2017-09-19 09:33:51 +053077 auto fileName = systemd::config::networkFilePrefix + interface +
Gunnar Mills57d9c502018-09-14 14:42:34 -050078 systemd::config::networkFileSuffix;
Ratan Guptab610caf2017-09-19 09:33:51 +053079
80 fs::path filePath = confDir;
81 filePath /= fileName;
82
83 // create the interface specific network file
84 // if not exist or we forcefully wants to write
85 // the network file.
86
87 if (force || !fs::is_regular_file(filePath.string()))
88 {
89 bmc::writeDHCPDefault(filePath.string(), interface);
90 log<level::INFO>("Created the default network file.",
Gunnar Mills57d9c502018-09-14 14:42:34 -050091 entry("INTERFACE=%s", interface.c_str()));
Ratan Guptab610caf2017-09-19 09:33:51 +053092 isCreated = true;
93 }
94 }
95 }
96 catch (std::exception& e)
97 {
98 log<level::ERR>("Unable to create the default network file");
99 }
100 return isCreated;
101}
102
Ratan Guptaef85eb92017-06-15 08:57:54 +0530103void Manager::setConfDir(const fs::path& dir)
104{
105 confDir = dir;
Ratan Gupta255d5142017-08-10 09:02:08 +0530106
107 if (!fs::exists(confDir))
108 {
109 if (!fs::create_directories(confDir))
110 {
111 log<level::ERR>("Unable to create the network conf dir",
112 entry("DIR=%s", confDir.c_str()));
113 elog<InternalFailure>();
114 }
115 }
Ratan Gupta29b0e432017-05-25 12:51:40 +0530116}
117
118void Manager::createInterfaces()
119{
Gunnar Mills57d9c502018-09-14 14:42:34 -0500120 // clear all the interfaces first
Ratan Guptaef85eb92017-06-15 08:57:54 +0530121 interfaces.clear();
Ratan Gupta29b0e432017-05-25 12:51:40 +0530122
Ratan Guptafd4b0f02017-09-16 06:01:24 +0530123 auto interfaceStrList = getInterfaces();
Ratan Gupta6811f822017-04-14 16:34:56 +0530124
Ratan Guptafd4b0f02017-09-16 06:01:24 +0530125 for (auto& interface : interfaceStrList)
Ratan Gupta6811f822017-04-14 16:34:56 +0530126 {
Ratan Gupta29b0e432017-05-25 12:51:40 +0530127 fs::path objPath = objectPath;
Ratan Guptafd4b0f02017-09-16 06:01:24 +0530128 auto index = interface.find(".");
Ratan Gupta92bc2fe2017-07-26 22:40:21 +0530129
130 // interface can be of vlan type or normal ethernet interface.
131 // vlan interface looks like "interface.vlanid",so here by looking
132 // at the interface name we decide that we need
133 // to create the vlaninterface or normal physical interface.
134 if (index != std::string::npos)
135 {
Gunnar Mills57d9c502018-09-14 14:42:34 -0500136 // it is vlan interface
Ratan Guptafd4b0f02017-09-16 06:01:24 +0530137 auto interfaceName = interface.substr(0, index);
138 auto vlanid = interface.substr(index + 1);
Ratan Gupta92bc2fe2017-07-26 22:40:21 +0530139 uint32_t vlanInt = std::stoul(vlanid);
140
Ratan Guptafd4b0f02017-09-16 06:01:24 +0530141 interfaces[interfaceName]->loadVLAN(vlanInt);
Ratan Gupta6e8df632017-08-13 09:41:58 +0530142 continue;
Ratan Gupta92bc2fe2017-07-26 22:40:21 +0530143 }
Ratan Guptafd4b0f02017-09-16 06:01:24 +0530144 // normal ethernet interface
145 objPath /= interface;
Ratan Gupta6811f822017-04-14 16:34:56 +0530146
Ratan Guptafd4b0f02017-09-16 06:01:24 +0530147 auto dhcp = getDHCPValue(confDir, interface);
Ratan Gupta34f96d62017-06-15 09:16:22 +0530148
Gunnar Mills57d9c502018-09-14 14:42:34 -0500149 auto intf = std::make_shared<phosphor::network::EthernetInterface>(
150 bus, objPath.string(), dhcp, *this);
Ratan Gupta92bc2fe2017-07-26 22:40:21 +0530151
152 intf->createIPAddressObjects();
153
Gunnar Mills57d9c502018-09-14 14:42:34 -0500154 this->interfaces.emplace(
155 std::make_pair(std::move(interface), std::move(intf)));
Ratan Gupta6811f822017-04-14 16:34:56 +0530156 }
157}
158
Ratan Guptaef85eb92017-06-15 08:57:54 +0530159void Manager::createChildObjects()
160{
161 // creates the ethernet interface dbus object.
162 createInterfaces();
Ratan Guptae05083a2017-09-16 07:12:11 +0530163
164 systemConf.reset(nullptr);
165 dhcpConf.reset(nullptr);
166
Ratan Guptaef85eb92017-06-15 08:57:54 +0530167 fs::path objPath = objectPath;
168 objPath /= "config";
Ratan Guptae05083a2017-09-16 07:12:11 +0530169
170 // create the system conf object.
Ratan Guptaef85eb92017-06-15 08:57:54 +0530171 systemConf = std::make_unique<phosphor::network::SystemConfiguration>(
Gunnar Mills57d9c502018-09-14 14:42:34 -0500172 bus, objPath.string(), *this);
Ratan Guptad16f88c2017-07-11 17:47:57 +0530173 // create the dhcp conf object.
174 objPath /= "dhcp";
175 dhcpConf = std::make_unique<phosphor::network::dhcp::Configuration>(
Gunnar Mills57d9c502018-09-14 14:42:34 -0500176 bus, objPath.string(), *this);
Ratan Guptaef85eb92017-06-15 08:57:54 +0530177}
178
Ratan Gupta584df832017-07-31 16:21:54 +0530179void Manager::vLAN(IntfName interfaceName, uint32_t id)
Ratan Gupta6811f822017-04-14 16:34:56 +0530180{
Ratan Gupta2b106532017-07-25 16:05:02 +0530181 interfaces[interfaceName]->createVLAN(id);
Ratan Gupta6811f822017-04-14 16:34:56 +0530182}
183
Michael Tritz29f2fd62017-05-22 15:27:26 -0500184void Manager::reset()
185{
Gunnar Mills57d9c502018-09-14 14:42:34 -0500186 if (!createDefaultNetworkFiles(true))
Michael Tritz29f2fd62017-05-22 15:27:26 -0500187 {
Ratan Guptab610caf2017-09-19 09:33:51 +0530188 log<level::ERR>("Network Factory Reset failed.");
189 return;
190 // TODO: openbmc/openbmc#1721 - Log ResetFailed error here.
Michael Tritz29f2fd62017-05-22 15:27:26 -0500191 }
192
Ratan Guptab610caf2017-09-19 09:33:51 +0530193 log<level::INFO>("Network Factory Reset done.");
Michael Tritz29f2fd62017-05-22 15:27:26 -0500194}
195
Ratan Gupta4f1c18b2017-05-25 12:59:35 +0530196// Need to merge the below function with the code which writes the
197// config file during factory reset.
Gunnar Mills57d9c502018-09-14 14:42:34 -0500198// TODO openbmc/openbmc#1751
Ratan Gupta4f1c18b2017-05-25 12:59:35 +0530199void Manager::writeToConfigurationFile()
200{
201 // write all the static ip address in the systemd-network conf file
Ratan Gupta4f1c18b2017-05-25 12:59:35 +0530202 for (const auto& intf : interfaces)
203 {
Ratan Gupta2b106532017-07-25 16:05:02 +0530204 intf.second->writeConfigurationFile();
Ratan Gupta4f1c18b2017-05-25 12:59:35 +0530205 }
Ratan Gupta16f12882017-09-22 18:26:11 +0530206 restartTimers();
Ratan Guptae05083a2017-09-16 07:12:11 +0530207}
208
Ratan Gupta16f12882017-09-22 18:26:11 +0530209void Manager::restartTimers()
Ratan Guptae05083a2017-09-16 07:12:11 +0530210{
211 using namespace std::chrono;
Ratan Gupta16f12882017-09-22 18:26:11 +0530212 if (refreshObjectTimer && restartTimer)
Ratan Guptae05083a2017-09-16 07:12:11 +0530213 {
William A. Kennington III3a70fa22018-09-20 18:48:20 -0700214 restartTimer->restartOnce(restartTimeout);
215 refreshObjectTimer->restartOnce(refreshTimeout);
Ratan Guptae05083a2017-09-16 07:12:11 +0530216 }
Ratan Gupta70c7e5b2017-07-12 11:41:55 +0530217}
218
Ratan Gupta35297172018-11-28 18:40:16 +0530219void Manager::restartSystemdUnit(const std::string& unit)
220{
221 try
222 {
223 auto method = bus.new_method_call(SYSTEMD_BUSNAME, SYSTEMD_PATH,
William A. Kennington IIIa5b1b0d2018-11-30 17:03:57 -0800224 SYSTEMD_INTERFACE, "ResetFailedUnit");
225 method.append(unit);
226 bus.call_noreply(method);
227 }
228 catch (const sdbusplus::exception::SdBusError& ex)
229 {
230 log<level::ERR>("Failed to reset failed unit",
231 entry("UNIT=%s", unit.c_str()),
232 entry("ERR=%s", ex.what()));
233 elog<InternalFailure>();
234 }
235
236 try
237 {
238 auto method = bus.new_method_call(SYSTEMD_BUSNAME, SYSTEMD_PATH,
Ratan Gupta35297172018-11-28 18:40:16 +0530239 SYSTEMD_INTERFACE, "RestartUnit");
240 method.append(unit.c_str(), "replace");
241 bus.call_noreply(method);
242 }
243 catch (const sdbusplus::exception::SdBusError& ex)
244 {
245 log<level::ERR>("Failed to restart nslcd service",
246 entry("ERR=%s", ex.what()));
247 elog<InternalFailure>();
248 }
249}
250
Gunnar Mills57d9c502018-09-14 14:42:34 -0500251} // namespace network
252} // namespace phosphor