blob: bb6d204a2b8f976bb4716c54f3b254c1e294a4a9 [file] [log] [blame]
Ratan Gupta6811f822017-04-14 16:34:56 +05301#include "config.h"
Ratan Gupta3681a502017-06-17 19:20:04 +05302#include "util.hpp"
Ratan Gupta6811f822017-04-14 16:34:56 +05303#include "network_manager.hpp"
Michael Tritz29f2fd62017-05-22 15:27:26 -05004#include "network_config.hpp"
Ratan Gupta5978dd12017-07-25 13:47:13 +05305#include "ipaddress.hpp"
Ratan Guptae05083a2017-09-16 07:12:11 +05306#include "timer.hpp"
Ratan Gupta44ae86e2017-05-15 21:52:14 +05307#include "xyz/openbmc_project/Common/error.hpp"
Ratan Gupta6811f822017-04-14 16:34:56 +05308
9#include <phosphor-logging/log.hpp>
Ratan Gupta44ae86e2017-05-15 21:52:14 +053010#include <phosphor-logging/elog-errors.hpp>
Ratan Gupta6811f822017-04-14 16:34:56 +053011
12#include <algorithm>
Ratan Gupta738a67f2017-04-21 10:38:05 +053013#include <bitset>
Ratan Gupta738a67f2017-04-21 10:38:05 +053014#include <map>
Ratan Gupta4f1c18b2017-05-25 12:59:35 +053015#include <fstream>
Ratan Gupta738a67f2017-04-21 10:38:05 +053016
Ratan Gupta6811f822017-04-14 16:34:56 +053017#include <arpa/inet.h>
18#include <dirent.h>
19#include <net/if.h>
20
Michael Tritz29f2fd62017-05-22 15:27:26 -050021#include <string>
Ratan Gupta6811f822017-04-14 16:34:56 +053022
23namespace phosphor
24{
25namespace network
26{
Ratan Gupta82549cc2017-04-21 08:45:23 +053027
Ratan Guptae05083a2017-09-16 07:12:11 +053028extern std::unique_ptr<phosphor::network::Timer> refreshTimer;
Ratan Gupta6811f822017-04-14 16:34:56 +053029using namespace phosphor::logging;
Ratan Guptaef85eb92017-06-15 08:57:54 +053030using namespace sdbusplus::xyz::openbmc_project::Common::Error;
Ratan Gupta6811f822017-04-14 16:34:56 +053031
Ratan Gupta255d5142017-08-10 09:02:08 +053032Manager::Manager(sdbusplus::bus::bus& bus, const char* objPath,
33 const std::string& path):
Ratan Gupta29b0e432017-05-25 12:51:40 +053034 details::VLANCreateIface(bus, objPath, true),
35 bus(bus),
36 objectPath(objPath)
Ratan Gupta6811f822017-04-14 16:34:56 +053037{
Ratan Gupta255d5142017-08-10 09:02:08 +053038 fs::path confDir(path);
39 setConfDir(confDir);
Ratan Guptaef85eb92017-06-15 08:57:54 +053040}
41
Ratan Guptab610caf2017-09-19 09:33:51 +053042bool Manager::createDefaultNetworkFiles(bool force)
43{
44 auto isCreated = false;
45 try
46 {
47 // Directory would have created before with
48 // setConfDir function.
49 if (force)
50 {
51 // Factory Reset case
52 // we need to forcefully write the files
53 // so delete the existing ones.
54 if (fs::is_directory(confDir))
55 {
56 for (const auto& file : fs::directory_iterator(confDir))
57 {
58 fs::remove(file.path());
59 }
60 }
61 }
62
63 auto interfaceStrList = getInterfaces();
64 for (const auto& interface : interfaceStrList)
65 {
66 auto fileName = systemd::config::networkFilePrefix + interface +
67 systemd::config::networkFileSuffix;
68
69 fs::path filePath = confDir;
70 filePath /= fileName;
71
72 // create the interface specific network file
73 // if not exist or we forcefully wants to write
74 // the network file.
75
76 if (force || !fs::is_regular_file(filePath.string()))
77 {
78 bmc::writeDHCPDefault(filePath.string(), interface);
79 log<level::INFO>("Created the default network file.",
80 entry("INTERFACE=%s", interface.c_str()));
81 isCreated = true;
82 }
83 }
84 }
85 catch (std::exception& e)
86 {
87 log<level::ERR>("Unable to create the default network file");
88 }
89 return isCreated;
90}
91
Ratan Guptaef85eb92017-06-15 08:57:54 +053092void Manager::setConfDir(const fs::path& dir)
93{
94 confDir = dir;
Ratan Gupta255d5142017-08-10 09:02:08 +053095
96 if (!fs::exists(confDir))
97 {
98 if (!fs::create_directories(confDir))
99 {
100 log<level::ERR>("Unable to create the network conf dir",
101 entry("DIR=%s", confDir.c_str()));
102 elog<InternalFailure>();
103 }
104 }
105
Ratan Gupta29b0e432017-05-25 12:51:40 +0530106}
107
108void Manager::createInterfaces()
109{
Ratan Guptaef85eb92017-06-15 08:57:54 +0530110 //clear all the interfaces first
111 interfaces.clear();
Ratan Gupta29b0e432017-05-25 12:51:40 +0530112
Ratan Guptafd4b0f02017-09-16 06:01:24 +0530113 auto interfaceStrList = getInterfaces();
Ratan Gupta6811f822017-04-14 16:34:56 +0530114
Ratan Guptafd4b0f02017-09-16 06:01:24 +0530115 for (auto& interface : interfaceStrList)
Ratan Gupta6811f822017-04-14 16:34:56 +0530116 {
Ratan Gupta29b0e432017-05-25 12:51:40 +0530117 fs::path objPath = objectPath;
Ratan Guptafd4b0f02017-09-16 06:01:24 +0530118 auto index = interface.find(".");
Ratan Gupta92bc2fe2017-07-26 22:40:21 +0530119
120 // interface can be of vlan type or normal ethernet interface.
121 // vlan interface looks like "interface.vlanid",so here by looking
122 // at the interface name we decide that we need
123 // to create the vlaninterface or normal physical interface.
124 if (index != std::string::npos)
125 {
126 //it is vlan interface
Ratan Guptafd4b0f02017-09-16 06:01:24 +0530127 auto interfaceName = interface.substr(0, index);
128 auto vlanid = interface.substr(index + 1);
Ratan Gupta92bc2fe2017-07-26 22:40:21 +0530129 uint32_t vlanInt = std::stoul(vlanid);
130
Ratan Guptafd4b0f02017-09-16 06:01:24 +0530131 interfaces[interfaceName]->loadVLAN(vlanInt);
Ratan Gupta6e8df632017-08-13 09:41:58 +0530132 continue;
Ratan Gupta92bc2fe2017-07-26 22:40:21 +0530133 }
Ratan Guptafd4b0f02017-09-16 06:01:24 +0530134 // normal ethernet interface
135 objPath /= interface;
Ratan Gupta6811f822017-04-14 16:34:56 +0530136
Ratan Guptafd4b0f02017-09-16 06:01:24 +0530137 auto dhcp = getDHCPValue(confDir, interface);
Ratan Gupta34f96d62017-06-15 09:16:22 +0530138
Ratan Gupta92bc2fe2017-07-26 22:40:21 +0530139 auto intf = std::make_shared<phosphor::network::EthernetInterface>(
140 bus,
141 objPath.string(),
142 dhcp,
143 *this);
144
145
146 intf->createIPAddressObjects();
147
Ratan Gupta6811f822017-04-14 16:34:56 +0530148 this->interfaces.emplace(std::make_pair(
Ratan Guptafd4b0f02017-09-16 06:01:24 +0530149 std::move(interface), std::move(intf)));
Ratan Gupta29b0e432017-05-25 12:51:40 +0530150
Ratan Gupta6811f822017-04-14 16:34:56 +0530151 }
Ratan Gupta87c13982017-06-15 09:27:27 +0530152
Ratan Gupta6811f822017-04-14 16:34:56 +0530153}
154
Ratan Guptaef85eb92017-06-15 08:57:54 +0530155void Manager::createChildObjects()
156{
157 // creates the ethernet interface dbus object.
158 createInterfaces();
Ratan Guptae05083a2017-09-16 07:12:11 +0530159
160 systemConf.reset(nullptr);
161 dhcpConf.reset(nullptr);
162
Ratan Guptaef85eb92017-06-15 08:57:54 +0530163 fs::path objPath = objectPath;
164 objPath /= "config";
Ratan Guptae05083a2017-09-16 07:12:11 +0530165
166 // create the system conf object.
Ratan Guptaef85eb92017-06-15 08:57:54 +0530167 systemConf = std::make_unique<phosphor::network::SystemConfiguration>(
168 bus, objPath.string(), *this);
Ratan Guptad16f88c2017-07-11 17:47:57 +0530169 // create the dhcp conf object.
170 objPath /= "dhcp";
171 dhcpConf = std::make_unique<phosphor::network::dhcp::Configuration>(
172 bus, objPath.string(), *this);
Ratan Guptaef85eb92017-06-15 08:57:54 +0530173
174}
175
Ratan Gupta584df832017-07-31 16:21:54 +0530176void Manager::vLAN(IntfName interfaceName, uint32_t id)
Ratan Gupta6811f822017-04-14 16:34:56 +0530177{
Ratan Gupta2b106532017-07-25 16:05:02 +0530178 interfaces[interfaceName]->createVLAN(id);
Ratan Gupta6811f822017-04-14 16:34:56 +0530179}
180
Michael Tritz29f2fd62017-05-22 15:27:26 -0500181void Manager::reset()
182{
Ratan Guptab610caf2017-09-19 09:33:51 +0530183 if(!createDefaultNetworkFiles(true))
Michael Tritz29f2fd62017-05-22 15:27:26 -0500184 {
Ratan Guptab610caf2017-09-19 09:33:51 +0530185 log<level::ERR>("Network Factory Reset failed.");
186 return;
187 // TODO: openbmc/openbmc#1721 - Log ResetFailed error here.
Michael Tritz29f2fd62017-05-22 15:27:26 -0500188 }
189
Ratan Guptab610caf2017-09-19 09:33:51 +0530190 log<level::INFO>("Network Factory Reset done.");
Michael Tritz29f2fd62017-05-22 15:27:26 -0500191}
192
Ratan Gupta4f1c18b2017-05-25 12:59:35 +0530193// Need to merge the below function with the code which writes the
194// config file during factory reset.
195//TODO openbmc/openbmc#1751
196void Manager::writeToConfigurationFile()
197{
198 // write all the static ip address in the systemd-network conf file
Ratan Gupta4f1c18b2017-05-25 12:59:35 +0530199 for (const auto& intf : interfaces)
200 {
Ratan Gupta2b106532017-07-25 16:05:02 +0530201 intf.second->writeConfigurationFile();
Ratan Gupta4f1c18b2017-05-25 12:59:35 +0530202
Ratan Gupta4f1c18b2017-05-25 12:59:35 +0530203 }
Ratan Guptae05083a2017-09-16 07:12:11 +0530204 restartNetwork();
205}
206
207void Manager::restartNetwork()
208{
209 using namespace std::chrono;
210
211 if (refreshTimer && !refreshTimer->isExpired())
212 {
213 auto time = duration_cast<microseconds>(
214 phosphor::network::networkChangeTimeout);
215 refreshTimer->startTimer(time);
216 }
217 restartSystemdUnit("systemd-networkd.service");
Ratan Gupta70c7e5b2017-07-12 11:41:55 +0530218}
219
Ratan Gupta6811f822017-04-14 16:34:56 +0530220}//namespace network
221}//namespace phosphor