blob: e9d8ad9883595436e96861095810d69938ed7a36 [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 Gupta16f12882017-09-22 18:26:11 +053028extern std::unique_ptr<phosphor::network::Timer> refreshObjectTimer;
29extern std::unique_ptr<phosphor::network::Timer> restartTimer;
Ratan Gupta6811f822017-04-14 16:34:56 +053030using namespace phosphor::logging;
Ratan Guptaef85eb92017-06-15 08:57:54 +053031using namespace sdbusplus::xyz::openbmc_project::Common::Error;
Ratan Gupta6811f822017-04-14 16:34:56 +053032
Ratan Gupta255d5142017-08-10 09:02:08 +053033Manager::Manager(sdbusplus::bus::bus& bus, const char* objPath,
34 const std::string& path):
Ratan Gupta29b0e432017-05-25 12:51:40 +053035 details::VLANCreateIface(bus, objPath, true),
36 bus(bus),
37 objectPath(objPath)
Ratan Gupta6811f822017-04-14 16:34:56 +053038{
Ratan Gupta255d5142017-08-10 09:02:08 +053039 fs::path confDir(path);
40 setConfDir(confDir);
Ratan Guptaef85eb92017-06-15 08:57:54 +053041}
42
Ratan Guptab610caf2017-09-19 09:33:51 +053043bool Manager::createDefaultNetworkFiles(bool force)
44{
45 auto isCreated = false;
46 try
47 {
48 // Directory would have created before with
49 // setConfDir function.
50 if (force)
51 {
52 // Factory Reset case
53 // we need to forcefully write the files
54 // so delete the existing ones.
55 if (fs::is_directory(confDir))
56 {
57 for (const auto& file : fs::directory_iterator(confDir))
58 {
59 fs::remove(file.path());
60 }
61 }
62 }
63
64 auto interfaceStrList = getInterfaces();
65 for (const auto& interface : interfaceStrList)
66 {
67 auto fileName = systemd::config::networkFilePrefix + interface +
68 systemd::config::networkFileSuffix;
69
70 fs::path filePath = confDir;
71 filePath /= fileName;
72
73 // create the interface specific network file
74 // if not exist or we forcefully wants to write
75 // the network file.
76
77 if (force || !fs::is_regular_file(filePath.string()))
78 {
79 bmc::writeDHCPDefault(filePath.string(), interface);
80 log<level::INFO>("Created the default network file.",
81 entry("INTERFACE=%s", interface.c_str()));
82 isCreated = true;
83 }
84 }
85 }
86 catch (std::exception& e)
87 {
88 log<level::ERR>("Unable to create the default network file");
89 }
90 return isCreated;
91}
92
Ratan Guptaef85eb92017-06-15 08:57:54 +053093void Manager::setConfDir(const fs::path& dir)
94{
95 confDir = dir;
Ratan Gupta255d5142017-08-10 09:02:08 +053096
97 if (!fs::exists(confDir))
98 {
99 if (!fs::create_directories(confDir))
100 {
101 log<level::ERR>("Unable to create the network conf dir",
102 entry("DIR=%s", confDir.c_str()));
103 elog<InternalFailure>();
104 }
105 }
106
Ratan Gupta29b0e432017-05-25 12:51:40 +0530107}
108
109void Manager::createInterfaces()
110{
Ratan Guptaef85eb92017-06-15 08:57:54 +0530111 //clear all the interfaces first
112 interfaces.clear();
Ratan Gupta29b0e432017-05-25 12:51:40 +0530113
Ratan Guptafd4b0f02017-09-16 06:01:24 +0530114 auto interfaceStrList = getInterfaces();
Ratan Gupta6811f822017-04-14 16:34:56 +0530115
Ratan Guptafd4b0f02017-09-16 06:01:24 +0530116 for (auto& interface : interfaceStrList)
Ratan Gupta6811f822017-04-14 16:34:56 +0530117 {
Ratan Gupta29b0e432017-05-25 12:51:40 +0530118 fs::path objPath = objectPath;
Ratan Guptafd4b0f02017-09-16 06:01:24 +0530119 auto index = interface.find(".");
Ratan Gupta92bc2fe2017-07-26 22:40:21 +0530120
121 // interface can be of vlan type or normal ethernet interface.
122 // vlan interface looks like "interface.vlanid",so here by looking
123 // at the interface name we decide that we need
124 // to create the vlaninterface or normal physical interface.
125 if (index != std::string::npos)
126 {
127 //it is vlan interface
Ratan Guptafd4b0f02017-09-16 06:01:24 +0530128 auto interfaceName = interface.substr(0, index);
129 auto vlanid = interface.substr(index + 1);
Ratan Gupta92bc2fe2017-07-26 22:40:21 +0530130 uint32_t vlanInt = std::stoul(vlanid);
131
Ratan Guptafd4b0f02017-09-16 06:01:24 +0530132 interfaces[interfaceName]->loadVLAN(vlanInt);
Ratan Gupta6e8df632017-08-13 09:41:58 +0530133 continue;
Ratan Gupta92bc2fe2017-07-26 22:40:21 +0530134 }
Ratan Guptafd4b0f02017-09-16 06:01:24 +0530135 // normal ethernet interface
136 objPath /= interface;
Ratan Gupta6811f822017-04-14 16:34:56 +0530137
Ratan Guptafd4b0f02017-09-16 06:01:24 +0530138 auto dhcp = getDHCPValue(confDir, interface);
Ratan Gupta34f96d62017-06-15 09:16:22 +0530139
Ratan Gupta92bc2fe2017-07-26 22:40:21 +0530140 auto intf = std::make_shared<phosphor::network::EthernetInterface>(
141 bus,
142 objPath.string(),
143 dhcp,
144 *this);
145
146
147 intf->createIPAddressObjects();
148
Ratan Gupta6811f822017-04-14 16:34:56 +0530149 this->interfaces.emplace(std::make_pair(
Ratan Guptafd4b0f02017-09-16 06:01:24 +0530150 std::move(interface), std::move(intf)));
Ratan Gupta29b0e432017-05-25 12:51:40 +0530151
Ratan Gupta6811f822017-04-14 16:34:56 +0530152 }
Ratan Gupta87c13982017-06-15 09:27:27 +0530153
Ratan Gupta6811f822017-04-14 16:34:56 +0530154}
155
Ratan Guptaef85eb92017-06-15 08:57:54 +0530156void Manager::createChildObjects()
157{
158 // creates the ethernet interface dbus object.
159 createInterfaces();
Ratan Guptae05083a2017-09-16 07:12:11 +0530160
161 systemConf.reset(nullptr);
162 dhcpConf.reset(nullptr);
163
Ratan Guptaef85eb92017-06-15 08:57:54 +0530164 fs::path objPath = objectPath;
165 objPath /= "config";
Ratan Guptae05083a2017-09-16 07:12:11 +0530166
167 // create the system conf object.
Ratan Guptaef85eb92017-06-15 08:57:54 +0530168 systemConf = std::make_unique<phosphor::network::SystemConfiguration>(
169 bus, objPath.string(), *this);
Ratan Guptad16f88c2017-07-11 17:47:57 +0530170 // create the dhcp conf object.
171 objPath /= "dhcp";
172 dhcpConf = std::make_unique<phosphor::network::dhcp::Configuration>(
173 bus, objPath.string(), *this);
Ratan Guptaef85eb92017-06-15 08:57:54 +0530174
175}
176
Ratan Gupta584df832017-07-31 16:21:54 +0530177void Manager::vLAN(IntfName interfaceName, uint32_t id)
Ratan Gupta6811f822017-04-14 16:34:56 +0530178{
Ratan Gupta2b106532017-07-25 16:05:02 +0530179 interfaces[interfaceName]->createVLAN(id);
Ratan Gupta6811f822017-04-14 16:34:56 +0530180}
181
Michael Tritz29f2fd62017-05-22 15:27:26 -0500182void Manager::reset()
183{
Ratan Guptab610caf2017-09-19 09:33:51 +0530184 if(!createDefaultNetworkFiles(true))
Michael Tritz29f2fd62017-05-22 15:27:26 -0500185 {
Ratan Guptab610caf2017-09-19 09:33:51 +0530186 log<level::ERR>("Network Factory Reset failed.");
187 return;
188 // TODO: openbmc/openbmc#1721 - Log ResetFailed error here.
Michael Tritz29f2fd62017-05-22 15:27:26 -0500189 }
190
Ratan Guptab610caf2017-09-19 09:33:51 +0530191 log<level::INFO>("Network Factory Reset done.");
Michael Tritz29f2fd62017-05-22 15:27:26 -0500192}
193
Ratan Gupta4f1c18b2017-05-25 12:59:35 +0530194// Need to merge the below function with the code which writes the
195// config file during factory reset.
196//TODO openbmc/openbmc#1751
197void Manager::writeToConfigurationFile()
198{
199 // write all the static ip address in the systemd-network conf file
Ratan Gupta4f1c18b2017-05-25 12:59:35 +0530200 for (const auto& intf : interfaces)
201 {
Ratan Gupta2b106532017-07-25 16:05:02 +0530202 intf.second->writeConfigurationFile();
Ratan Gupta4f1c18b2017-05-25 12:59:35 +0530203
Ratan Gupta4f1c18b2017-05-25 12:59:35 +0530204 }
Ratan Gupta16f12882017-09-22 18:26:11 +0530205 restartTimers();
Ratan Guptae05083a2017-09-16 07:12:11 +0530206}
207
Ratan Gupta16f12882017-09-22 18:26:11 +0530208void Manager::restartTimers()
Ratan Guptae05083a2017-09-16 07:12:11 +0530209{
210 using namespace std::chrono;
Ratan Gupta16f12882017-09-22 18:26:11 +0530211 if (refreshObjectTimer && restartTimer)
Ratan Guptae05083a2017-09-16 07:12:11 +0530212 {
Ratan Gupta16f12882017-09-22 18:26:11 +0530213 // start the restart timer.
214 auto restartTime = duration_cast<microseconds>(
215 phosphor::network::restartTimeout);
216 restartTimer->startTimer(restartTime);
217
218 // start the refresh timer.
219 auto refreshTime = duration_cast<microseconds>(
220 phosphor::network::refreshTimeout);
221 refreshObjectTimer->startTimer(refreshTime);
Ratan Guptae05083a2017-09-16 07:12:11 +0530222 }
Ratan Gupta70c7e5b2017-07-12 11:41:55 +0530223}
224
Ratan Gupta6811f822017-04-14 16:34:56 +0530225}//namespace network
226}//namespace phosphor