Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 1 | #include "config.h" |
Ratan Gupta | 34f96d6 | 2017-06-15 09:16:22 +0530 | [diff] [blame] | 2 | #include "config_parser.hpp" |
Ratan Gupta | 3681a50 | 2017-06-17 19:20:04 +0530 | [diff] [blame] | 3 | #include "util.hpp" |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 4 | #include "network_manager.hpp" |
Michael Tritz | 29f2fd6 | 2017-05-22 15:27:26 -0500 | [diff] [blame] | 5 | #include "network_config.hpp" |
Ratan Gupta | 5978dd1 | 2017-07-25 13:47:13 +0530 | [diff] [blame] | 6 | #include "ipaddress.hpp" |
Ratan Gupta | 44ae86e | 2017-05-15 21:52:14 +0530 | [diff] [blame] | 7 | #include "xyz/openbmc_project/Common/error.hpp" |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 8 | |
| 9 | #include <phosphor-logging/log.hpp> |
Ratan Gupta | 44ae86e | 2017-05-15 21:52:14 +0530 | [diff] [blame] | 10 | #include <phosphor-logging/elog-errors.hpp> |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 11 | |
| 12 | #include <algorithm> |
Ratan Gupta | 738a67f | 2017-04-21 10:38:05 +0530 | [diff] [blame] | 13 | #include <bitset> |
Ratan Gupta | 738a67f | 2017-04-21 10:38:05 +0530 | [diff] [blame] | 14 | #include <map> |
Ratan Gupta | 4f1c18b | 2017-05-25 12:59:35 +0530 | [diff] [blame] | 15 | #include <fstream> |
Ratan Gupta | 738a67f | 2017-04-21 10:38:05 +0530 | [diff] [blame] | 16 | |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 17 | #include <arpa/inet.h> |
| 18 | #include <dirent.h> |
| 19 | #include <net/if.h> |
| 20 | |
Michael Tritz | 29f2fd6 | 2017-05-22 15:27:26 -0500 | [diff] [blame] | 21 | #include <string> |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 22 | |
| 23 | namespace phosphor |
| 24 | { |
| 25 | namespace network |
| 26 | { |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 27 | |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 28 | using namespace phosphor::logging; |
Ratan Gupta | ef85eb9 | 2017-06-15 08:57:54 +0530 | [diff] [blame] | 29 | using namespace sdbusplus::xyz::openbmc_project::Common::Error; |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 30 | |
Ratan Gupta | 255d514 | 2017-08-10 09:02:08 +0530 | [diff] [blame] | 31 | Manager::Manager(sdbusplus::bus::bus& bus, const char* objPath, |
| 32 | const std::string& path): |
Ratan Gupta | 29b0e43 | 2017-05-25 12:51:40 +0530 | [diff] [blame] | 33 | details::VLANCreateIface(bus, objPath, true), |
| 34 | bus(bus), |
| 35 | objectPath(objPath) |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 36 | { |
Ratan Gupta | 255d514 | 2017-08-10 09:02:08 +0530 | [diff] [blame] | 37 | fs::path confDir(path); |
| 38 | setConfDir(confDir); |
Ratan Gupta | ef85eb9 | 2017-06-15 08:57:54 +0530 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | void Manager::setConfDir(const fs::path& dir) |
| 42 | { |
| 43 | confDir = dir; |
Ratan Gupta | 255d514 | 2017-08-10 09:02:08 +0530 | [diff] [blame] | 44 | |
| 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 Gupta | 29b0e43 | 2017-05-25 12:51:40 +0530 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | void Manager::createInterfaces() |
| 58 | { |
Ratan Gupta | ef85eb9 | 2017-06-15 08:57:54 +0530 | [diff] [blame] | 59 | //clear all the interfaces first |
| 60 | interfaces.clear(); |
Ratan Gupta | 29b0e43 | 2017-05-25 12:51:40 +0530 | [diff] [blame] | 61 | |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 62 | auto interfaceInfoList = getInterfaceAddrs(); |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 63 | |
Ratan Gupta | 738a67f | 2017-04-21 10:38:05 +0530 | [diff] [blame] | 64 | for (const auto& intfInfo : interfaceInfoList) |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 65 | { |
Ratan Gupta | 29b0e43 | 2017-05-25 12:51:40 +0530 | [diff] [blame] | 66 | fs::path objPath = objectPath; |
| 67 | objPath /= intfInfo.first; |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 68 | |
Ratan Gupta | 34f96d6 | 2017-06-15 09:16:22 +0530 | [diff] [blame] | 69 | auto dhcp = getDHCPValue(intfInfo.first); |
| 70 | |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 71 | this->interfaces.emplace(std::make_pair( |
Ratan Gupta | 738a67f | 2017-04-21 10:38:05 +0530 | [diff] [blame] | 72 | intfInfo.first, |
| 73 | std::make_unique< |
| 74 | phosphor::network::EthernetInterface> |
| 75 | (bus, |
Ratan Gupta | 29b0e43 | 2017-05-25 12:51:40 +0530 | [diff] [blame] | 76 | objPath.string(), |
Ratan Gupta | 34f96d6 | 2017-06-15 09:16:22 +0530 | [diff] [blame] | 77 | dhcp, |
Ratan Gupta | 4f1c18b | 2017-05-25 12:59:35 +0530 | [diff] [blame] | 78 | *this))); |
Ratan Gupta | 29b0e43 | 2017-05-25 12:51:40 +0530 | [diff] [blame] | 79 | |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 80 | } |
Ratan Gupta | 87c1398 | 2017-06-15 09:27:27 +0530 | [diff] [blame] | 81 | |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 82 | } |
| 83 | |
Ratan Gupta | ef85eb9 | 2017-06-15 08:57:54 +0530 | [diff] [blame] | 84 | void 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 Gupta | d16f88c | 2017-07-11 17:47:57 +0530 | [diff] [blame] | 93 | // create the dhcp conf object. |
| 94 | objPath /= "dhcp"; |
| 95 | dhcpConf = std::make_unique<phosphor::network::dhcp::Configuration>( |
| 96 | bus, objPath.string(), *this); |
Ratan Gupta | ef85eb9 | 2017-06-15 08:57:54 +0530 | [diff] [blame] | 97 | |
| 98 | } |
| 99 | |
Ratan Gupta | 584df83 | 2017-07-31 16:21:54 +0530 | [diff] [blame] | 100 | void Manager::vLAN(IntfName interfaceName, uint32_t id) |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 101 | { |
Ratan Gupta | 2b10653 | 2017-07-25 16:05:02 +0530 | [diff] [blame^] | 102 | interfaces[interfaceName]->createVLAN(id); |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 103 | } |
| 104 | |
Michael Tritz | 29f2fd6 | 2017-05-22 15:27:26 -0500 | [diff] [blame] | 105 | void 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 Gupta | 4f1c18b | 2017-05-25 12:59:35 +0530 | [diff] [blame] | 156 | // Need to merge the below function with the code which writes the |
| 157 | // config file during factory reset. |
| 158 | //TODO openbmc/openbmc#1751 |
| 159 | void Manager::writeToConfigurationFile() |
| 160 | { |
| 161 | // write all the static ip address in the systemd-network conf file |
| 162 | |
Ratan Gupta | 4f1c18b | 2017-05-25 12:59:35 +0530 | [diff] [blame] | 163 | for (const auto& intf : interfaces) |
| 164 | { |
Ratan Gupta | 2b10653 | 2017-07-25 16:05:02 +0530 | [diff] [blame^] | 165 | intf.second->writeConfigurationFile(); |
Ratan Gupta | 4f1c18b | 2017-05-25 12:59:35 +0530 | [diff] [blame] | 166 | |
Ratan Gupta | 4f1c18b | 2017-05-25 12:59:35 +0530 | [diff] [blame] | 167 | } |
Ratan Gupta | 70c7e5b | 2017-07-12 11:41:55 +0530 | [diff] [blame] | 168 | } |
| 169 | |
Ratan Gupta | 34f96d6 | 2017-06-15 09:16:22 +0530 | [diff] [blame] | 170 | bool 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 Gupta | dea3ead | 2017-08-02 18:09:25 +0530 | [diff] [blame] | 191 | log<level::INFO>("Exception occured during getting of DHCP value"); |
Ratan Gupta | 34f96d6 | 2017-06-15 09:16:22 +0530 | [diff] [blame] | 192 | } |
| 193 | return dhcp; |
| 194 | } |
| 195 | |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 196 | }//namespace network |
| 197 | }//namespace phosphor |