blob: dd9a31989678748dc8f360904629f090f957d5c6 [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 Gupta44ae86e2017-05-15 21:52:14 +05306#include "xyz/openbmc_project/Common/error.hpp"
Ratan Gupta6811f822017-04-14 16:34:56 +05307
8#include <phosphor-logging/log.hpp>
Ratan Gupta44ae86e2017-05-15 21:52:14 +05309#include <phosphor-logging/elog-errors.hpp>
Ratan Gupta6811f822017-04-14 16:34:56 +053010
11#include <algorithm>
Ratan Gupta738a67f2017-04-21 10:38:05 +053012#include <bitset>
Ratan Gupta738a67f2017-04-21 10:38:05 +053013#include <map>
Ratan Gupta4f1c18b2017-05-25 12:59:35 +053014#include <fstream>
Ratan Gupta738a67f2017-04-21 10:38:05 +053015
Ratan Gupta6811f822017-04-14 16:34:56 +053016#include <arpa/inet.h>
17#include <dirent.h>
18#include <net/if.h>
19
Michael Tritz29f2fd62017-05-22 15:27:26 -050020#include <string>
Ratan Gupta6811f822017-04-14 16:34:56 +053021
22namespace phosphor
23{
24namespace network
25{
Ratan Gupta82549cc2017-04-21 08:45:23 +053026
Ratan Gupta6811f822017-04-14 16:34:56 +053027using namespace phosphor::logging;
Ratan Guptaef85eb92017-06-15 08:57:54 +053028using namespace sdbusplus::xyz::openbmc_project::Common::Error;
Ratan Gupta6811f822017-04-14 16:34:56 +053029
Ratan Gupta255d5142017-08-10 09:02:08 +053030Manager::Manager(sdbusplus::bus::bus& bus, const char* objPath,
31 const std::string& path):
Ratan Gupta29b0e432017-05-25 12:51:40 +053032 details::VLANCreateIface(bus, objPath, true),
33 bus(bus),
34 objectPath(objPath)
Ratan Gupta6811f822017-04-14 16:34:56 +053035{
Ratan Gupta255d5142017-08-10 09:02:08 +053036 fs::path confDir(path);
37 setConfDir(confDir);
Ratan Guptaef85eb92017-06-15 08:57:54 +053038}
39
40void Manager::setConfDir(const fs::path& dir)
41{
42 confDir = dir;
Ratan Gupta255d5142017-08-10 09:02:08 +053043
44 if (!fs::exists(confDir))
45 {
46 if (!fs::create_directories(confDir))
47 {
48 log<level::ERR>("Unable to create the network conf dir",
49 entry("DIR=%s", confDir.c_str()));
50 elog<InternalFailure>();
51 }
52 }
53
Ratan Gupta29b0e432017-05-25 12:51:40 +053054}
55
56void Manager::createInterfaces()
57{
Ratan Guptaef85eb92017-06-15 08:57:54 +053058 //clear all the interfaces first
59 interfaces.clear();
Ratan Gupta29b0e432017-05-25 12:51:40 +053060
Ratan Gupta82549cc2017-04-21 08:45:23 +053061 auto interfaceInfoList = getInterfaceAddrs();
Ratan Gupta6811f822017-04-14 16:34:56 +053062
Ratan Gupta738a67f2017-04-21 10:38:05 +053063 for (const auto& intfInfo : interfaceInfoList)
Ratan Gupta6811f822017-04-14 16:34:56 +053064 {
Ratan Gupta29b0e432017-05-25 12:51:40 +053065 fs::path objPath = objectPath;
Ratan Gupta92bc2fe2017-07-26 22:40:21 +053066 auto index = intfInfo.first.find(".");
67
68 // interface can be of vlan type or normal ethernet interface.
69 // vlan interface looks like "interface.vlanid",so here by looking
70 // at the interface name we decide that we need
71 // to create the vlaninterface or normal physical interface.
72 if (index != std::string::npos)
73 {
74 //it is vlan interface
75 auto interface = intfInfo.first.substr(0, index);
76 auto vlanid = intfInfo.first.substr(index + 1);
77 uint32_t vlanInt = std::stoul(vlanid);
78
79 interfaces[interface]->loadVLAN(vlanInt);
Ratan Gupta6e8df632017-08-13 09:41:58 +053080 continue;
Ratan Gupta92bc2fe2017-07-26 22:40:21 +053081 }
82 // normal ethernet inetrface
Ratan Gupta29b0e432017-05-25 12:51:40 +053083 objPath /= intfInfo.first;
Ratan Gupta6811f822017-04-14 16:34:56 +053084
Ratan Gupta56187e72017-08-13 09:40:14 +053085 auto dhcp = getDHCPValue(confDir, intfInfo.first);
Ratan Gupta34f96d62017-06-15 09:16:22 +053086
Ratan Gupta92bc2fe2017-07-26 22:40:21 +053087 auto intf = std::make_shared<phosphor::network::EthernetInterface>(
88 bus,
89 objPath.string(),
90 dhcp,
91 *this);
92
93
94 intf->createIPAddressObjects();
95
Ratan Gupta6811f822017-04-14 16:34:56 +053096 this->interfaces.emplace(std::make_pair(
Ratan Gupta92bc2fe2017-07-26 22:40:21 +053097 intfInfo.first, std::move(intf)));
Ratan Gupta29b0e432017-05-25 12:51:40 +053098
Ratan Gupta6811f822017-04-14 16:34:56 +053099 }
Ratan Gupta87c13982017-06-15 09:27:27 +0530100
Ratan Gupta6811f822017-04-14 16:34:56 +0530101}
102
Ratan Guptaef85eb92017-06-15 08:57:54 +0530103void Manager::createChildObjects()
104{
105 // creates the ethernet interface dbus object.
106 createInterfaces();
107 // create the system conf object.
108 fs::path objPath = objectPath;
109 objPath /= "config";
110 systemConf = std::make_unique<phosphor::network::SystemConfiguration>(
111 bus, objPath.string(), *this);
Ratan Guptad16f88c2017-07-11 17:47:57 +0530112 // create the dhcp conf object.
113 objPath /= "dhcp";
114 dhcpConf = std::make_unique<phosphor::network::dhcp::Configuration>(
115 bus, objPath.string(), *this);
Ratan Guptaef85eb92017-06-15 08:57:54 +0530116
117}
118
Ratan Gupta584df832017-07-31 16:21:54 +0530119void Manager::vLAN(IntfName interfaceName, uint32_t id)
Ratan Gupta6811f822017-04-14 16:34:56 +0530120{
Ratan Gupta2b106532017-07-25 16:05:02 +0530121 interfaces[interfaceName]->createVLAN(id);
Ratan Gupta6811f822017-04-14 16:34:56 +0530122}
123
Michael Tritz29f2fd62017-05-22 15:27:26 -0500124void Manager::reset()
125{
Ratan Gupta1bc8ed32017-07-25 22:34:19 +0530126 const std::string networkConfig = confDir.string();
127 bool interfacesMapped = false;
Michael Tritz29f2fd62017-05-22 15:27:26 -0500128
129 if(fs::is_directory(networkConfig))
130 {
131 for(auto& file : fs::directory_iterator(networkConfig))
132 {
Ratan Gupta1bc8ed32017-07-25 22:34:19 +0530133 fs::remove(file.path());
Michael Tritz29f2fd62017-05-22 15:27:26 -0500134 }
135
136 for (auto& intf : interfaces)
137 {
Ratan Gupta1bc8ed32017-07-25 22:34:19 +0530138 std::string filename = networkConfig +
139 systemd::config::networkFilePrefix +
140 intf.first +
141 systemd::config::networkFileSuffix;
Michael Tritz29f2fd62017-05-22 15:27:26 -0500142
143 bmc::writeDHCPDefault(filename, intf.first);
144 interfacesMapped = true;
145 }
146
147 if(interfacesMapped)
148 {
149 log<level::INFO>("Network configuration reset to DHCP.");
150 }
151 else
152 {
153 log<level::ERR>("No network interfaces are mapped.");
154 // TODO: openbmc/openbmc#1721 - Log ResetFailed error here.
155 }
156 }
157 else
158 {
159 log<level::ERR>("Network configuration directory not found!");
160 // TODO: openbmc/openbmc#1721 - Log ResetFailed error here.
161 }
162
163 return;
164}
165
Ratan Gupta4f1c18b2017-05-25 12:59:35 +0530166// Need to merge the below function with the code which writes the
167// config file during factory reset.
168//TODO openbmc/openbmc#1751
169void Manager::writeToConfigurationFile()
170{
171 // write all the static ip address in the systemd-network conf file
172
Ratan Gupta4f1c18b2017-05-25 12:59:35 +0530173 for (const auto& intf : interfaces)
174 {
Ratan Gupta2b106532017-07-25 16:05:02 +0530175 intf.second->writeConfigurationFile();
Ratan Gupta4f1c18b2017-05-25 12:59:35 +0530176
Ratan Gupta4f1c18b2017-05-25 12:59:35 +0530177 }
Ratan Gupta70c7e5b2017-07-12 11:41:55 +0530178}
179
Ratan Gupta6811f822017-04-14 16:34:56 +0530180}//namespace network
181}//namespace phosphor