blob: cac4d93754e71b7b9790840972a2efdbad3a48fa [file] [log] [blame]
Ratan Gupta6811f822017-04-14 16:34:56 +05301#include "config.h"
Ratan Gupta34f96d62017-06-15 09:16:22 +05302#include "config_parser.hpp"
Ratan Gupta3681a502017-06-17 19:20:04 +05303#include "util.hpp"
Ratan Gupta6811f822017-04-14 16:34:56 +05304#include "network_manager.hpp"
Michael Tritz29f2fd62017-05-22 15:27:26 -05005#include "network_config.hpp"
Ratan Gupta5978dd12017-07-25 13:47:13 +05306#include "ipaddress.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 Gupta6811f822017-04-14 16:34:56 +053028using namespace phosphor::logging;
Ratan Guptaef85eb92017-06-15 08:57:54 +053029using namespace sdbusplus::xyz::openbmc_project::Common::Error;
Ratan Gupta6811f822017-04-14 16:34:56 +053030
Ratan Gupta255d5142017-08-10 09:02:08 +053031Manager::Manager(sdbusplus::bus::bus& bus, const char* objPath,
32 const std::string& path):
Ratan Gupta29b0e432017-05-25 12:51:40 +053033 details::VLANCreateIface(bus, objPath, true),
34 bus(bus),
35 objectPath(objPath)
Ratan Gupta6811f822017-04-14 16:34:56 +053036{
Ratan Gupta255d5142017-08-10 09:02:08 +053037 fs::path confDir(path);
38 setConfDir(confDir);
Ratan Guptaef85eb92017-06-15 08:57:54 +053039}
40
41void Manager::setConfDir(const fs::path& dir)
42{
43 confDir = dir;
Ratan Gupta255d5142017-08-10 09:02:08 +053044
45 if (!fs::exists(confDir))
46 {
47 if (!fs::create_directories(confDir))
48 {
49 log<level::ERR>("Unable to create the network conf dir",
50 entry("DIR=%s", confDir.c_str()));
51 elog<InternalFailure>();
52 }
53 }
54
Ratan Gupta29b0e432017-05-25 12:51:40 +053055}
56
57void Manager::createInterfaces()
58{
Ratan Guptaef85eb92017-06-15 08:57:54 +053059 //clear all the interfaces first
60 interfaces.clear();
Ratan Gupta29b0e432017-05-25 12:51:40 +053061
Ratan Gupta82549cc2017-04-21 08:45:23 +053062 auto interfaceInfoList = getInterfaceAddrs();
Ratan Gupta6811f822017-04-14 16:34:56 +053063
Ratan Gupta738a67f2017-04-21 10:38:05 +053064 for (const auto& intfInfo : interfaceInfoList)
Ratan Gupta6811f822017-04-14 16:34:56 +053065 {
Ratan Gupta29b0e432017-05-25 12:51:40 +053066 fs::path objPath = objectPath;
67 objPath /= intfInfo.first;
Ratan Gupta6811f822017-04-14 16:34:56 +053068
Ratan Gupta34f96d62017-06-15 09:16:22 +053069 auto dhcp = getDHCPValue(intfInfo.first);
70
Ratan Gupta6811f822017-04-14 16:34:56 +053071 this->interfaces.emplace(std::make_pair(
Ratan Gupta738a67f2017-04-21 10:38:05 +053072 intfInfo.first,
73 std::make_unique<
74 phosphor::network::EthernetInterface>
75 (bus,
Ratan Gupta29b0e432017-05-25 12:51:40 +053076 objPath.string(),
Ratan Gupta34f96d62017-06-15 09:16:22 +053077 dhcp,
Ratan Gupta4f1c18b2017-05-25 12:59:35 +053078 *this)));
Ratan Gupta29b0e432017-05-25 12:51:40 +053079
Ratan Gupta6811f822017-04-14 16:34:56 +053080 }
Ratan Gupta87c13982017-06-15 09:27:27 +053081
Ratan Gupta6811f822017-04-14 16:34:56 +053082}
83
Ratan Guptaef85eb92017-06-15 08:57:54 +053084void Manager::createChildObjects()
85{
86 // creates the ethernet interface dbus object.
87 createInterfaces();
88 // create the system conf object.
89 fs::path objPath = objectPath;
90 objPath /= "config";
91 systemConf = std::make_unique<phosphor::network::SystemConfiguration>(
92 bus, objPath.string(), *this);
Ratan Guptad16f88c2017-07-11 17:47:57 +053093 // create the dhcp conf object.
94 objPath /= "dhcp";
95 dhcpConf = std::make_unique<phosphor::network::dhcp::Configuration>(
96 bus, objPath.string(), *this);
Ratan Guptaef85eb92017-06-15 08:57:54 +053097
98}
99
Ratan Gupta584df832017-07-31 16:21:54 +0530100void Manager::vLAN(IntfName interfaceName, uint32_t id)
Ratan Gupta6811f822017-04-14 16:34:56 +0530101{
Ratan Gupta2b106532017-07-25 16:05:02 +0530102 interfaces[interfaceName]->createVLAN(id);
Ratan Gupta6811f822017-04-14 16:34:56 +0530103}
104
Michael Tritz29f2fd62017-05-22 15:27:26 -0500105void Manager::reset()
106{
107 const std::string networkConfig = "/etc/systemd/network/";
108 bool filesExist, interfacesMapped = false;
109
110 if(fs::is_directory(networkConfig))
111 {
112 for(auto& file : fs::directory_iterator(networkConfig))
113 {
114 std::string filename = file.path().filename().c_str();
115
116 if(filename.substr(filename.find_last_of(".") + 1) == "network")
117 {
118 fs::remove(file.path());
119 filesExist = true;
120 }
121 }
122
123 if(!filesExist)
124 {
125 log<level::INFO>("No existing network configuration was found.");
126 }
127
128 for (auto& intf : interfaces)
129 {
130 std::string filename = networkConfig + "00-bmc-" + intf.first +
131 ".network";
132
133 bmc::writeDHCPDefault(filename, intf.first);
134 interfacesMapped = true;
135 }
136
137 if(interfacesMapped)
138 {
139 log<level::INFO>("Network configuration reset to DHCP.");
140 }
141 else
142 {
143 log<level::ERR>("No network interfaces are mapped.");
144 // TODO: openbmc/openbmc#1721 - Log ResetFailed error here.
145 }
146 }
147 else
148 {
149 log<level::ERR>("Network configuration directory not found!");
150 // TODO: openbmc/openbmc#1721 - Log ResetFailed error here.
151 }
152
153 return;
154}
155
Ratan Gupta4f1c18b2017-05-25 12:59:35 +0530156// Need to merge the below function with the code which writes the
157// config file during factory reset.
158//TODO openbmc/openbmc#1751
159void Manager::writeToConfigurationFile()
160{
161 // write all the static ip address in the systemd-network conf file
162
Ratan Gupta4f1c18b2017-05-25 12:59:35 +0530163 for (const auto& intf : interfaces)
164 {
Ratan Gupta2b106532017-07-25 16:05:02 +0530165 intf.second->writeConfigurationFile();
Ratan Gupta4f1c18b2017-05-25 12:59:35 +0530166
Ratan Gupta4f1c18b2017-05-25 12:59:35 +0530167 }
Ratan Gupta70c7e5b2017-07-12 11:41:55 +0530168}
169
Ratan Gupta34f96d62017-06-15 09:16:22 +0530170bool Manager::getDHCPValue(const std::string& intf)
171{
172 bool dhcp = false;
173 // Get the interface mode value from systemd conf
174 using namespace std::string_literals;
175 fs::path confPath = confDir;
176 std::string fileName = "00-bmc-"s + intf + ".network"s;
177 confPath /= fileName;
178
179 try
180 {
181 config::Parser parser(confPath.string());
182 auto values = parser.getValues("Network","DHCP");
183 // There will be only single value for DHCP key.
184 if (values[0] == "true")
185 {
186 dhcp = true;
187 }
188 }
189 catch (InternalFailure& e)
190 {
Ratan Guptadea3ead2017-08-02 18:09:25 +0530191 log<level::INFO>("Exception occured during getting of DHCP value");
Ratan Gupta34f96d62017-06-15 09:16:22 +0530192 }
193 return dhcp;
194}
195
Ratan Gupta6811f822017-04-14 16:34:56 +0530196}//namespace network
197}//namespace phosphor