Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 1 | #include "config.h" |
Ratan Gupta | 3681a50 | 2017-06-17 19:20:04 +0530 | [diff] [blame] | 2 | #include "util.hpp" |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 3 | #include "network_manager.hpp" |
Michael Tritz | 29f2fd6 | 2017-05-22 15:27:26 -0500 | [diff] [blame] | 4 | #include "network_config.hpp" |
Ratan Gupta | 5978dd1 | 2017-07-25 13:47:13 +0530 | [diff] [blame] | 5 | #include "ipaddress.hpp" |
Ratan Gupta | 44ae86e | 2017-05-15 21:52:14 +0530 | [diff] [blame] | 6 | #include "xyz/openbmc_project/Common/error.hpp" |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 7 | |
| 8 | #include <phosphor-logging/log.hpp> |
Ratan Gupta | 44ae86e | 2017-05-15 21:52:14 +0530 | [diff] [blame] | 9 | #include <phosphor-logging/elog-errors.hpp> |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 10 | |
| 11 | #include <algorithm> |
Ratan Gupta | 738a67f | 2017-04-21 10:38:05 +0530 | [diff] [blame] | 12 | #include <bitset> |
Ratan Gupta | 738a67f | 2017-04-21 10:38:05 +0530 | [diff] [blame] | 13 | #include <map> |
Ratan Gupta | 4f1c18b | 2017-05-25 12:59:35 +0530 | [diff] [blame] | 14 | #include <fstream> |
Ratan Gupta | 738a67f | 2017-04-21 10:38:05 +0530 | [diff] [blame] | 15 | |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 16 | #include <arpa/inet.h> |
| 17 | #include <dirent.h> |
| 18 | #include <net/if.h> |
| 19 | |
Michael Tritz | 29f2fd6 | 2017-05-22 15:27:26 -0500 | [diff] [blame] | 20 | #include <string> |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 21 | |
| 22 | namespace phosphor |
| 23 | { |
| 24 | namespace network |
| 25 | { |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 26 | |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 27 | using namespace phosphor::logging; |
Ratan Gupta | ef85eb9 | 2017-06-15 08:57:54 +0530 | [diff] [blame] | 28 | using namespace sdbusplus::xyz::openbmc_project::Common::Error; |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 29 | |
Ratan Gupta | 255d514 | 2017-08-10 09:02:08 +0530 | [diff] [blame] | 30 | Manager::Manager(sdbusplus::bus::bus& bus, const char* objPath, |
| 31 | const std::string& path): |
Ratan Gupta | 29b0e43 | 2017-05-25 12:51:40 +0530 | [diff] [blame] | 32 | details::VLANCreateIface(bus, objPath, true), |
| 33 | bus(bus), |
| 34 | objectPath(objPath) |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 35 | { |
Ratan Gupta | 255d514 | 2017-08-10 09:02:08 +0530 | [diff] [blame] | 36 | fs::path confDir(path); |
| 37 | setConfDir(confDir); |
Ratan Gupta | ef85eb9 | 2017-06-15 08:57:54 +0530 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | void Manager::setConfDir(const fs::path& dir) |
| 41 | { |
| 42 | confDir = dir; |
Ratan Gupta | 255d514 | 2017-08-10 09:02:08 +0530 | [diff] [blame] | 43 | |
| 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 Gupta | 29b0e43 | 2017-05-25 12:51:40 +0530 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | void Manager::createInterfaces() |
| 57 | { |
Ratan Gupta | ef85eb9 | 2017-06-15 08:57:54 +0530 | [diff] [blame] | 58 | //clear all the interfaces first |
| 59 | interfaces.clear(); |
Ratan Gupta | 29b0e43 | 2017-05-25 12:51:40 +0530 | [diff] [blame] | 60 | |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 61 | auto interfaceInfoList = getInterfaceAddrs(); |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 62 | |
Ratan Gupta | 738a67f | 2017-04-21 10:38:05 +0530 | [diff] [blame] | 63 | for (const auto& intfInfo : interfaceInfoList) |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 64 | { |
Ratan Gupta | 29b0e43 | 2017-05-25 12:51:40 +0530 | [diff] [blame] | 65 | fs::path objPath = objectPath; |
Ratan Gupta | 92bc2fe | 2017-07-26 22:40:21 +0530 | [diff] [blame] | 66 | 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); |
| 80 | return; |
| 81 | } |
| 82 | // normal ethernet inetrface |
Ratan Gupta | 29b0e43 | 2017-05-25 12:51:40 +0530 | [diff] [blame] | 83 | objPath /= intfInfo.first; |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 84 | |
Ratan Gupta | 56187e7 | 2017-08-13 09:40:14 +0530 | [diff] [blame^] | 85 | auto dhcp = getDHCPValue(confDir, intfInfo.first); |
Ratan Gupta | 34f96d6 | 2017-06-15 09:16:22 +0530 | [diff] [blame] | 86 | |
Ratan Gupta | 92bc2fe | 2017-07-26 22:40:21 +0530 | [diff] [blame] | 87 | auto intf = std::make_shared<phosphor::network::EthernetInterface>( |
| 88 | bus, |
| 89 | objPath.string(), |
| 90 | dhcp, |
| 91 | *this); |
| 92 | |
| 93 | |
| 94 | intf->createIPAddressObjects(); |
| 95 | |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 96 | this->interfaces.emplace(std::make_pair( |
Ratan Gupta | 92bc2fe | 2017-07-26 22:40:21 +0530 | [diff] [blame] | 97 | intfInfo.first, std::move(intf))); |
Ratan Gupta | 29b0e43 | 2017-05-25 12:51:40 +0530 | [diff] [blame] | 98 | |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 99 | } |
Ratan Gupta | 87c1398 | 2017-06-15 09:27:27 +0530 | [diff] [blame] | 100 | |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 101 | } |
| 102 | |
Ratan Gupta | ef85eb9 | 2017-06-15 08:57:54 +0530 | [diff] [blame] | 103 | void 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 Gupta | d16f88c | 2017-07-11 17:47:57 +0530 | [diff] [blame] | 112 | // create the dhcp conf object. |
| 113 | objPath /= "dhcp"; |
| 114 | dhcpConf = std::make_unique<phosphor::network::dhcp::Configuration>( |
| 115 | bus, objPath.string(), *this); |
Ratan Gupta | ef85eb9 | 2017-06-15 08:57:54 +0530 | [diff] [blame] | 116 | |
| 117 | } |
| 118 | |
Ratan Gupta | 584df83 | 2017-07-31 16:21:54 +0530 | [diff] [blame] | 119 | void Manager::vLAN(IntfName interfaceName, uint32_t id) |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 120 | { |
Ratan Gupta | 2b10653 | 2017-07-25 16:05:02 +0530 | [diff] [blame] | 121 | interfaces[interfaceName]->createVLAN(id); |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 122 | } |
| 123 | |
Michael Tritz | 29f2fd6 | 2017-05-22 15:27:26 -0500 | [diff] [blame] | 124 | void Manager::reset() |
| 125 | { |
Ratan Gupta | 1bc8ed3 | 2017-07-25 22:34:19 +0530 | [diff] [blame] | 126 | const std::string networkConfig = confDir.string(); |
| 127 | bool interfacesMapped = false; |
Michael Tritz | 29f2fd6 | 2017-05-22 15:27:26 -0500 | [diff] [blame] | 128 | |
| 129 | if(fs::is_directory(networkConfig)) |
| 130 | { |
| 131 | for(auto& file : fs::directory_iterator(networkConfig)) |
| 132 | { |
Ratan Gupta | 1bc8ed3 | 2017-07-25 22:34:19 +0530 | [diff] [blame] | 133 | fs::remove(file.path()); |
Michael Tritz | 29f2fd6 | 2017-05-22 15:27:26 -0500 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | for (auto& intf : interfaces) |
| 137 | { |
Ratan Gupta | 1bc8ed3 | 2017-07-25 22:34:19 +0530 | [diff] [blame] | 138 | std::string filename = networkConfig + |
| 139 | systemd::config::networkFilePrefix + |
| 140 | intf.first + |
| 141 | systemd::config::networkFileSuffix; |
Michael Tritz | 29f2fd6 | 2017-05-22 15:27:26 -0500 | [diff] [blame] | 142 | |
| 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 Gupta | 4f1c18b | 2017-05-25 12:59:35 +0530 | [diff] [blame] | 166 | // Need to merge the below function with the code which writes the |
| 167 | // config file during factory reset. |
| 168 | //TODO openbmc/openbmc#1751 |
| 169 | void Manager::writeToConfigurationFile() |
| 170 | { |
| 171 | // write all the static ip address in the systemd-network conf file |
| 172 | |
Ratan Gupta | 4f1c18b | 2017-05-25 12:59:35 +0530 | [diff] [blame] | 173 | for (const auto& intf : interfaces) |
| 174 | { |
Ratan Gupta | 2b10653 | 2017-07-25 16:05:02 +0530 | [diff] [blame] | 175 | intf.second->writeConfigurationFile(); |
Ratan Gupta | 4f1c18b | 2017-05-25 12:59:35 +0530 | [diff] [blame] | 176 | |
Ratan Gupta | 4f1c18b | 2017-05-25 12:59:35 +0530 | [diff] [blame] | 177 | } |
Ratan Gupta | 70c7e5b | 2017-07-12 11:41:55 +0530 | [diff] [blame] | 178 | } |
| 179 | |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 180 | }//namespace network |
| 181 | }//namespace phosphor |