Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 1 | #include "config.h" |
| 2 | #include "ipaddress.hpp" |
Ratan Gupta | 91a99cc | 2017-04-14 16:32:09 +0530 | [diff] [blame] | 3 | #include "ethernet_interface.hpp" |
Ratan Gupta | 5978dd1 | 2017-07-25 13:47:13 +0530 | [diff] [blame^] | 4 | #include "vlan_interface.hpp" |
Ratan Gupta | 4f1c18b | 2017-05-25 12:59:35 +0530 | [diff] [blame] | 5 | #include "network_manager.hpp" |
Ratan Gupta | fc2c724 | 2017-05-29 08:46:06 +0530 | [diff] [blame] | 6 | #include "routing_table.hpp" |
Ratan Gupta | 91a99cc | 2017-04-14 16:32:09 +0530 | [diff] [blame] | 7 | |
| 8 | #include <phosphor-logging/log.hpp> |
| 9 | |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 10 | #include <arpa/inet.h> |
Ratan Gupta | 91a99cc | 2017-04-14 16:32:09 +0530 | [diff] [blame] | 11 | #include <linux/ethtool.h> |
| 12 | #include <net/if.h> |
| 13 | #include <linux/sockios.h> |
| 14 | #include <netinet/in.h> |
| 15 | #include <sys/ioctl.h> |
| 16 | #include <sys/socket.h> |
| 17 | #include <unistd.h> |
| 18 | |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 19 | #include <string> |
| 20 | #include <algorithm> |
Ratan Gupta | 65e5abe | 2017-05-23 13:20:44 +0530 | [diff] [blame] | 21 | #include <sstream> |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 22 | #include <experimental/filesystem> |
| 23 | |
Ratan Gupta | 91a99cc | 2017-04-14 16:32:09 +0530 | [diff] [blame] | 24 | namespace phosphor |
| 25 | { |
| 26 | namespace network |
| 27 | { |
| 28 | |
| 29 | using namespace phosphor::logging; |
| 30 | constexpr auto MAC_ADDRESS_FORMAT = "%02X:%02X:%02X:%02X:%02X:%02X"; |
| 31 | constexpr size_t SIZE_MAC = 18; |
| 32 | constexpr size_t SIZE_BUFF = 512; |
| 33 | |
| 34 | EthernetInterface::EthernetInterface(sdbusplus::bus::bus& bus, |
| 35 | const std::string& objPath, |
Ratan Gupta | 4f1c18b | 2017-05-25 12:59:35 +0530 | [diff] [blame] | 36 | bool dhcpEnabled, |
Ratan Gupta | 3d3e4fc | 2017-07-25 13:38:19 +0530 | [diff] [blame] | 37 | Manager& parent, |
| 38 | bool emitSignal) : |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 39 | Ifaces(bus, objPath.c_str(), true), |
Ratan Gupta | 5978dd1 | 2017-07-25 13:47:13 +0530 | [diff] [blame^] | 40 | confDir(NETWORK_CONF_DIR), |
Ratan Gupta | 4f1c18b | 2017-05-25 12:59:35 +0530 | [diff] [blame] | 41 | bus(bus), |
Ratan Gupta | 47722dc | 2017-05-26 18:32:23 +0530 | [diff] [blame] | 42 | manager(parent), |
| 43 | objPath(objPath) |
Ratan Gupta | 91a99cc | 2017-04-14 16:32:09 +0530 | [diff] [blame] | 44 | { |
| 45 | auto intfName = objPath.substr(objPath.rfind("/") + 1); |
Ratan Gupta | 5978dd1 | 2017-07-25 13:47:13 +0530 | [diff] [blame^] | 46 | std::replace(intfName.begin(), intfName.end(), '_', '.'); |
Ratan Gupta | 91a99cc | 2017-04-14 16:32:09 +0530 | [diff] [blame] | 47 | interfaceName(intfName); |
| 48 | dHCPEnabled(dhcpEnabled); |
| 49 | mACAddress(getMACAddress()); |
Ratan Gupta | 5978dd1 | 2017-07-25 13:47:13 +0530 | [diff] [blame^] | 50 | |
Ratan Gupta | 29b0e43 | 2017-05-25 12:51:40 +0530 | [diff] [blame] | 51 | // Emit deferred signal. |
Ratan Gupta | 3d3e4fc | 2017-07-25 13:38:19 +0530 | [diff] [blame] | 52 | if (emitSignal) |
| 53 | { |
| 54 | this->emit_object_added(); |
| 55 | } |
Ratan Gupta | 29b0e43 | 2017-05-25 12:51:40 +0530 | [diff] [blame] | 56 | } |
| 57 | |
Ratan Gupta | 87c1398 | 2017-06-15 09:27:27 +0530 | [diff] [blame] | 58 | void EthernetInterface::createIPAddressObjects() |
Ratan Gupta | 29b0e43 | 2017-05-25 12:51:40 +0530 | [diff] [blame] | 59 | { |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 60 | std::string gateway; |
Ratan Gupta | 87c1398 | 2017-06-15 09:27:27 +0530 | [diff] [blame] | 61 | addrs.clear(); |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 62 | |
Ratan Gupta | 87c1398 | 2017-06-15 09:27:27 +0530 | [diff] [blame] | 63 | auto addrs = getInterfaceAddrs()[interfaceName()]; |
Ratan Gupta | 5978dd1 | 2017-07-25 13:47:13 +0530 | [diff] [blame^] | 64 | |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 65 | IP::Protocol addressType = IP::Protocol::IPv4; |
Ratan Gupta | 29b0e43 | 2017-05-25 12:51:40 +0530 | [diff] [blame] | 66 | IP::AddressOrigin origin = IP::AddressOrigin::Static; |
Ratan Gupta | fc2c724 | 2017-05-29 08:46:06 +0530 | [diff] [blame] | 67 | route::Table routingTable; |
Ratan Gupta | 5978dd1 | 2017-07-25 13:47:13 +0530 | [diff] [blame^] | 68 | |
Ratan Gupta | 6a387c1 | 2017-08-03 13:26:19 +0530 | [diff] [blame] | 69 | for (auto& addr : addrs) |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 70 | { |
| 71 | if (addr.addrType == AF_INET6) |
| 72 | { |
| 73 | addressType = IP::Protocol::IPv6; |
| 74 | } |
Ratan Gupta | fc2c724 | 2017-05-29 08:46:06 +0530 | [diff] [blame] | 75 | if (dHCPEnabled()) |
| 76 | { |
| 77 | origin = IP::AddressOrigin::DHCP; |
| 78 | } |
| 79 | else if (isLinkLocal(addr.ipaddress)) |
| 80 | { |
| 81 | origin = IP::AddressOrigin::LinkLocal; |
| 82 | } |
| 83 | gateway = routingTable.getGateway(addr.addrType, addr.ipaddress, addr.prefix); |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 84 | |
Ratan Gupta | 65e5abe | 2017-05-23 13:20:44 +0530 | [diff] [blame] | 85 | std::string ipAddressObjectPath = generateObjectPath(addressType, |
| 86 | addr.ipaddress, |
| 87 | addr.prefix, |
| 88 | gateway); |
Ratan Gupta | fc2c724 | 2017-05-29 08:46:06 +0530 | [diff] [blame] | 89 | |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 90 | this->addrs.emplace( |
Ratan Gupta | e578d56 | 2017-08-02 07:04:16 +0530 | [diff] [blame] | 91 | std::move(addr.ipaddress), |
| 92 | std::make_shared<phosphor::network::IPAddress>( |
Ratan Gupta | 719f83a | 2017-06-02 11:54:53 +0530 | [diff] [blame] | 93 | bus, |
| 94 | ipAddressObjectPath.c_str(), |
| 95 | *this, |
| 96 | addressType, |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 97 | addr.ipaddress, |
Ratan Gupta | 29b0e43 | 2017-05-25 12:51:40 +0530 | [diff] [blame] | 98 | origin, |
Ratan Gupta | 719f83a | 2017-06-02 11:54:53 +0530 | [diff] [blame] | 99 | addr.prefix, |
Ratan Gupta | e578d56 | 2017-08-02 07:04:16 +0530 | [diff] [blame] | 100 | gateway)); |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 101 | } |
Ratan Gupta | fc2c724 | 2017-05-29 08:46:06 +0530 | [diff] [blame] | 102 | |
Ratan Gupta | 91a99cc | 2017-04-14 16:32:09 +0530 | [diff] [blame] | 103 | } |
| 104 | |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 105 | void EthernetInterface::iP(IP::Protocol protType, |
| 106 | std::string ipaddress, |
| 107 | uint8_t prefixLength, |
| 108 | std::string gateway) |
| 109 | { |
Ratan Gupta | fc2c724 | 2017-05-29 08:46:06 +0530 | [diff] [blame] | 110 | |
| 111 | if (dHCPEnabled()) |
| 112 | { |
Ratan Gupta | 82e1ef9 | 2017-06-15 08:39:15 +0530 | [diff] [blame] | 113 | log<level::INFO>("DHCP enabled on the interface"), |
| 114 | entry("INTERFACE=%s",interfaceName()); |
Ratan Gupta | fc2c724 | 2017-05-29 08:46:06 +0530 | [diff] [blame] | 115 | return; |
| 116 | } |
| 117 | |
Ratan Gupta | 29b0e43 | 2017-05-25 12:51:40 +0530 | [diff] [blame] | 118 | IP::AddressOrigin origin = IP::AddressOrigin::Static; |
| 119 | |
Ratan Gupta | 65e5abe | 2017-05-23 13:20:44 +0530 | [diff] [blame] | 120 | std::string objectPath = generateObjectPath(protType, |
| 121 | ipaddress, |
| 122 | prefixLength, |
| 123 | gateway); |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 124 | |
| 125 | this->addrs.emplace( |
Ratan Gupta | e578d56 | 2017-08-02 07:04:16 +0530 | [diff] [blame] | 126 | std::move(ipaddress), |
| 127 | std::make_shared<phosphor::network::IPAddress>( |
| 128 | bus, |
| 129 | objectPath.c_str(), |
| 130 | *this, |
| 131 | protType, |
| 132 | ipaddress, |
| 133 | origin, |
| 134 | prefixLength, |
| 135 | gateway)); |
Ratan Gupta | 4f1c18b | 2017-05-25 12:59:35 +0530 | [diff] [blame] | 136 | |
| 137 | manager.writeToConfigurationFile(); |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | |
Ratan Gupta | 91a99cc | 2017-04-14 16:32:09 +0530 | [diff] [blame] | 141 | /* |
| 142 | Note: We don't have support for ethtool now |
| 143 | will enable this code once we bring the ethtool |
| 144 | in the image. |
| 145 | TODO: https://github.com/openbmc/openbmc/issues/1484 |
| 146 | */ |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 147 | |
Ratan Gupta | 91a99cc | 2017-04-14 16:32:09 +0530 | [diff] [blame] | 148 | InterfaceInfo EthernetInterface::getInterfaceInfo() const |
| 149 | { |
| 150 | int sock{-1}; |
| 151 | struct ifreq ifr{0}; |
| 152 | struct ethtool_cmd edata{0}; |
| 153 | LinkSpeed speed {0}; |
| 154 | Autoneg autoneg {0}; |
| 155 | DuplexMode duplex {0}; |
| 156 | do |
| 157 | { |
| 158 | sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP); |
| 159 | if (sock < 0) |
| 160 | { |
| 161 | log<level::ERR>("socket creation failed:", |
| 162 | entry("ERROR=%s", strerror(errno))); |
| 163 | break; |
| 164 | } |
| 165 | |
| 166 | strncpy(ifr.ifr_name, interfaceName().c_str(), sizeof(ifr.ifr_name)); |
| 167 | ifr.ifr_data = reinterpret_cast<char*>(&edata); |
| 168 | |
| 169 | edata.cmd = ETHTOOL_GSET; |
| 170 | |
| 171 | if (ioctl(sock, SIOCETHTOOL, &ifr) < 0) |
| 172 | { |
| 173 | log<level::ERR>("ioctl failed for SIOCETHTOOL:", |
| 174 | entry("ERROR=%s", strerror(errno))); |
| 175 | break; |
| 176 | |
| 177 | } |
| 178 | speed = edata.speed; |
| 179 | duplex = edata.duplex; |
| 180 | autoneg = edata.autoneg; |
| 181 | } |
| 182 | while (0); |
| 183 | |
| 184 | if (sock) |
| 185 | { |
| 186 | close(sock); |
| 187 | } |
| 188 | return std::make_tuple(speed, duplex, autoneg); |
| 189 | } |
| 190 | |
| 191 | /** @brief get the mac address of the interface. |
| 192 | * @return macaddress on success |
| 193 | */ |
| 194 | |
| 195 | std::string EthernetInterface::getMACAddress() const |
| 196 | { |
| 197 | struct ifreq ifr; |
| 198 | struct ifconf ifc; |
| 199 | char buf[SIZE_BUFF]; |
| 200 | char macAddress[SIZE_MAC] = ""; |
| 201 | |
| 202 | int sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP); |
| 203 | if (sock < 0) |
| 204 | { |
| 205 | log<level::ERR>("socket creation failed:", |
| 206 | entry("ERROR=%s", strerror(errno))); |
| 207 | return macAddress; |
| 208 | } |
| 209 | |
| 210 | ifc.ifc_len = sizeof(buf); |
| 211 | ifc.ifc_buf = buf; |
| 212 | if (ioctl(sock, SIOCGIFCONF, &ifc) < 0) |
| 213 | { |
| 214 | log<level::ERR>("ioctl failed for SIOCGIFCONF:", |
| 215 | entry("ERROR=%s", strerror(errno))); |
| 216 | return macAddress; |
| 217 | } |
| 218 | |
| 219 | struct ifreq* it = ifc.ifc_req; |
| 220 | const struct ifreq* const end = it + (ifc.ifc_len / sizeof(struct ifreq)); |
| 221 | |
| 222 | for (; it != end; ++it) |
| 223 | { |
| 224 | if (interfaceName() == it->ifr_name) |
| 225 | { |
| 226 | break; |
| 227 | } |
| 228 | } |
| 229 | if (interfaceName() == it->ifr_name) |
| 230 | { |
| 231 | strcpy(ifr.ifr_name, it->ifr_name); |
| 232 | if (ioctl(sock, SIOCGIFHWADDR, &ifr) != 0) |
| 233 | { |
| 234 | log<level::ERR>("ioctl failed for SIOCGIFHWADDR:", |
| 235 | entry("ERROR=%s", strerror(errno))); |
| 236 | return macAddress; |
| 237 | } |
| 238 | |
| 239 | snprintf(macAddress, SIZE_MAC, MAC_ADDRESS_FORMAT, |
| 240 | ifr.ifr_hwaddr.sa_data[0], ifr.ifr_hwaddr.sa_data[1], |
| 241 | ifr.ifr_hwaddr.sa_data[2], ifr.ifr_hwaddr.sa_data[3], |
| 242 | ifr.ifr_hwaddr.sa_data[4], ifr.ifr_hwaddr.sa_data[5]); |
| 243 | } |
| 244 | return macAddress; |
| 245 | } |
Ratan Gupta | 2eff84f | 2017-04-20 19:19:15 +0530 | [diff] [blame] | 246 | |
Ratan Gupta | 65e5abe | 2017-05-23 13:20:44 +0530 | [diff] [blame] | 247 | std::string EthernetInterface::generateId(const std::string& ipaddress, |
| 248 | uint8_t prefixLength, |
| 249 | const std::string& gateway) |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 250 | { |
Ratan Gupta | 65e5abe | 2017-05-23 13:20:44 +0530 | [diff] [blame] | 251 | std::stringstream hexId; |
| 252 | std::string hashString = ipaddress; |
| 253 | hashString += std::to_string(prefixLength); |
| 254 | hashString += gateway; |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 255 | |
Ratan Gupta | 65e5abe | 2017-05-23 13:20:44 +0530 | [diff] [blame] | 256 | // Only want 8 hex digits. |
| 257 | hexId << std::hex << ((std::hash<std::string> {}( |
Ratan Gupta | fc2c724 | 2017-05-29 08:46:06 +0530 | [diff] [blame] | 258 | hashString)) & 0xFFFFFFFF); |
Ratan Gupta | 65e5abe | 2017-05-23 13:20:44 +0530 | [diff] [blame] | 259 | return hexId.str(); |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 260 | } |
| 261 | |
Ratan Gupta | 2eff84f | 2017-04-20 19:19:15 +0530 | [diff] [blame] | 262 | void EthernetInterface::deleteObject(const std::string& ipaddress) |
| 263 | { |
Ratan Gupta | 29b0e43 | 2017-05-25 12:51:40 +0530 | [diff] [blame] | 264 | auto it = addrs.find(ipaddress); |
Ratan Gupta | fc2c724 | 2017-05-29 08:46:06 +0530 | [diff] [blame] | 265 | if (it == addrs.end()) |
Ratan Gupta | 29b0e43 | 2017-05-25 12:51:40 +0530 | [diff] [blame] | 266 | { |
Ratan Gupta | fc2c724 | 2017-05-29 08:46:06 +0530 | [diff] [blame] | 267 | log<level::ERR>("DeleteObject:Unable to find the object."); |
| 268 | return; |
Ratan Gupta | 29b0e43 | 2017-05-25 12:51:40 +0530 | [diff] [blame] | 269 | } |
| 270 | this->addrs.erase(it); |
Ratan Gupta | 4f1c18b | 2017-05-25 12:59:35 +0530 | [diff] [blame] | 271 | manager.writeToConfigurationFile(); |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 272 | } |
| 273 | |
Ratan Gupta | 65e5abe | 2017-05-23 13:20:44 +0530 | [diff] [blame] | 274 | std::string EthernetInterface::generateObjectPath(IP::Protocol addressType, |
| 275 | const std::string& ipaddress, |
| 276 | uint8_t prefixLength, |
| 277 | const std::string& gateway) const |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 278 | { |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 279 | std::string type = convertForMessage(addressType); |
Ratan Gupta | 29b0e43 | 2017-05-25 12:51:40 +0530 | [diff] [blame] | 280 | type = type.substr(type.rfind('.') + 1); |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 281 | std::transform(type.begin(), type.end(), type.begin(), ::tolower); |
| 282 | |
| 283 | std::experimental::filesystem::path objectPath; |
Ratan Gupta | 47722dc | 2017-05-26 18:32:23 +0530 | [diff] [blame] | 284 | objectPath /= objPath; |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 285 | objectPath /= type; |
Ratan Gupta | 29b0e43 | 2017-05-25 12:51:40 +0530 | [diff] [blame] | 286 | objectPath /= generateId(ipaddress, prefixLength, gateway); |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 287 | return objectPath.string(); |
Ratan Gupta | 2eff84f | 2017-04-20 19:19:15 +0530 | [diff] [blame] | 288 | } |
| 289 | |
Ratan Gupta | 87c1398 | 2017-06-15 09:27:27 +0530 | [diff] [blame] | 290 | bool EthernetInterface::dHCPEnabled(bool value) |
| 291 | { |
Ratan Gupta | 5978dd1 | 2017-07-25 13:47:13 +0530 | [diff] [blame^] | 292 | if (value == EthernetInterfaceIntf::dHCPEnabled()) |
| 293 | { |
| 294 | return value; |
| 295 | } |
| 296 | |
Ratan Gupta | 87c1398 | 2017-06-15 09:27:27 +0530 | [diff] [blame] | 297 | EthernetInterfaceIntf::dHCPEnabled(value); |
Ratan Gupta | 5978dd1 | 2017-07-25 13:47:13 +0530 | [diff] [blame^] | 298 | if (value) |
Ratan Gupta | 87c1398 | 2017-06-15 09:27:27 +0530 | [diff] [blame] | 299 | { |
| 300 | manager.writeToConfigurationFile(); |
| 301 | createIPAddressObjects(); |
| 302 | } |
| 303 | return value; |
| 304 | } |
| 305 | |
Ratan Gupta | 5978dd1 | 2017-07-25 13:47:13 +0530 | [diff] [blame^] | 306 | void EthernetInterface::createVLAN(VlanId id) |
| 307 | { |
| 308 | std::string vlanInterfaceName = interfaceName() + "." + |
| 309 | std::to_string(id); |
| 310 | std::string path = objPath; |
| 311 | path += "_" + std::to_string(id); |
| 312 | |
| 313 | |
| 314 | auto vlanIntf = std::make_unique<phosphor::network::VlanInterface>( |
| 315 | bus, |
| 316 | path.c_str(), |
| 317 | EthernetInterfaceIntf::dHCPEnabled(), |
| 318 | id, |
| 319 | *this, |
| 320 | manager); |
| 321 | |
| 322 | // write the device file for the vlan interface. |
| 323 | vlanIntf->writeDeviceFile(); |
| 324 | |
| 325 | this->vlanInterfaces.emplace(std::move(vlanInterfaceName), |
| 326 | std::move(vlanIntf)); |
| 327 | // write the new vlan device entry to the configuration(network) file. |
| 328 | manager.writeToConfigurationFile(); |
| 329 | |
| 330 | } |
Ratan Gupta | 91a99cc | 2017-04-14 16:32:09 +0530 | [diff] [blame] | 331 | }//namespace network |
| 332 | }//namespace phosphor |