Ratan Gupta | 8c83493 | 2017-04-14 16:30:24 +0530 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 3 | #include "types.hpp" |
Ratan Gupta | 8804feb | 2017-05-25 10:49:57 +0530 | [diff] [blame] | 4 | #include "util.hpp" |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 5 | #include "xyz/openbmc_project/Network/IP/Create/server.hpp" |
Ratan Gupta | 8c83493 | 2017-04-14 16:30:24 +0530 | [diff] [blame] | 6 | |
Patrick Venture | 189d44e | 2018-07-09 12:30:59 -0700 | [diff] [blame] | 7 | #include <experimental/filesystem> |
Ratan Gupta | 8c83493 | 2017-04-14 16:30:24 +0530 | [diff] [blame] | 8 | #include <sdbusplus/bus.hpp> |
| 9 | #include <sdbusplus/server/object.hpp> |
Ratan Gupta | 8c83493 | 2017-04-14 16:30:24 +0530 | [diff] [blame] | 10 | #include <string> |
Patrick Venture | 189d44e | 2018-07-09 12:30:59 -0700 | [diff] [blame] | 11 | #include <xyz/openbmc_project/Collection/DeleteAll/server.hpp> |
| 12 | #include <xyz/openbmc_project/Network/EthernetInterface/server.hpp> |
| 13 | #include <xyz/openbmc_project/Network/MACAddress/server.hpp> |
Ratan Gupta | 8c83493 | 2017-04-14 16:30:24 +0530 | [diff] [blame] | 14 | |
| 15 | namespace phosphor |
| 16 | { |
| 17 | namespace network |
| 18 | { |
Ratan Gupta | 8c83493 | 2017-04-14 16:30:24 +0530 | [diff] [blame] | 19 | |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 20 | using Ifaces = |
| 21 | sdbusplus::server::object::object< |
| 22 | sdbusplus::xyz::openbmc_project::Network::server::EthernetInterface, |
Ratan Gupta | 1caa230 | 2017-07-10 10:30:13 +0530 | [diff] [blame] | 23 | sdbusplus::xyz::openbmc_project::Network::server::MACAddress, |
Ratan Gupta | e9c9b81 | 2017-09-22 17:15:37 +0530 | [diff] [blame] | 24 | sdbusplus::xyz::openbmc_project::Network::IP::server::Create, |
| 25 | sdbusplus::xyz::openbmc_project::Collection::server::DeleteAll>; |
Ratan Gupta | 8c83493 | 2017-04-14 16:30:24 +0530 | [diff] [blame] | 26 | |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 27 | using IP = sdbusplus::xyz::openbmc_project::Network::server::IP; |
Ratan Gupta | 87c1398 | 2017-06-15 09:27:27 +0530 | [diff] [blame] | 28 | |
| 29 | using EthernetInterfaceIntf = |
| 30 | sdbusplus::xyz::openbmc_project::Network::server::EthernetInterface; |
Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 31 | using MacAddressIntf = |
| 32 | sdbusplus::xyz::openbmc_project::Network::server::MACAddress; |
Ratan Gupta | 87c1398 | 2017-06-15 09:27:27 +0530 | [diff] [blame] | 33 | |
Ratan Gupta | 497c0c9 | 2017-08-22 19:15:59 +0530 | [diff] [blame] | 34 | using ServerList = std::vector<std::string>; |
| 35 | |
Ratan Gupta | 5978dd1 | 2017-07-25 13:47:13 +0530 | [diff] [blame] | 36 | namespace fs = std::experimental::filesystem; |
| 37 | |
Ratan Gupta | 4f1c18b | 2017-05-25 12:59:35 +0530 | [diff] [blame] | 38 | class Manager; // forward declaration of network manager. |
Ratan Gupta | 8c83493 | 2017-04-14 16:30:24 +0530 | [diff] [blame] | 39 | |
Ratan Gupta | 47722dc | 2017-05-26 18:32:23 +0530 | [diff] [blame] | 40 | class TestEthernetInterface; |
| 41 | |
Ratan Gupta | 5978dd1 | 2017-07-25 13:47:13 +0530 | [diff] [blame] | 42 | class VlanInterface; |
| 43 | |
| 44 | class IPAddress; |
| 45 | |
Ratan Gupta | 8c83493 | 2017-04-14 16:30:24 +0530 | [diff] [blame] | 46 | using LinkSpeed = uint16_t; |
| 47 | using DuplexMode = uint8_t; |
| 48 | using Autoneg = uint8_t; |
Ratan Gupta | 5978dd1 | 2017-07-25 13:47:13 +0530 | [diff] [blame] | 49 | using VlanId = uint32_t; |
| 50 | using InterfaceName = std::string; |
Ratan Gupta | 8c83493 | 2017-04-14 16:30:24 +0530 | [diff] [blame] | 51 | using InterfaceInfo = std::tuple<LinkSpeed, DuplexMode, Autoneg>; |
Ratan Gupta | 29b0e43 | 2017-05-25 12:51:40 +0530 | [diff] [blame] | 52 | using AddressMap = std::map<std::string, std::shared_ptr<IPAddress>>; |
Ratan Gupta | 5978dd1 | 2017-07-25 13:47:13 +0530 | [diff] [blame] | 53 | using VlanInterfaceMap = std::map<InterfaceName, std::unique_ptr<VlanInterface>>; |
Ratan Gupta | 8c83493 | 2017-04-14 16:30:24 +0530 | [diff] [blame] | 54 | |
| 55 | /** @class EthernetInterface |
| 56 | * @brief OpenBMC Ethernet Interface implementation. |
| 57 | * @details A concrete implementation for the |
| 58 | * xyz.openbmc_project.Network.EthernetInterface DBus API. |
| 59 | */ |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 60 | class EthernetInterface : public Ifaces |
Ratan Gupta | 8c83493 | 2017-04-14 16:30:24 +0530 | [diff] [blame] | 61 | { |
| 62 | public: |
| 63 | EthernetInterface() = delete; |
| 64 | EthernetInterface(const EthernetInterface&) = delete; |
| 65 | EthernetInterface& operator=(const EthernetInterface&) = delete; |
| 66 | EthernetInterface(EthernetInterface&&) = delete; |
| 67 | EthernetInterface& operator=(EthernetInterface&&) = delete; |
| 68 | virtual ~EthernetInterface() = default; |
| 69 | |
| 70 | /** @brief Constructor to put object onto bus at a dbus path. |
| 71 | * @param[in] bus - Bus to attach to. |
| 72 | * @param[in] objPath - Path to attach at. |
Ratan Gupta | 8c83493 | 2017-04-14 16:30:24 +0530 | [diff] [blame] | 73 | * @param[in] dhcpEnabled - is dhcp enabled(true/false). |
Ratan Gupta | 4f1c18b | 2017-05-25 12:59:35 +0530 | [diff] [blame] | 74 | * @param[in] parent - parent object. |
Ratan Gupta | 3d3e4fc | 2017-07-25 13:38:19 +0530 | [diff] [blame] | 75 | * @param[in] emitSignal - true if the object added signal needs to be |
| 76 | * send. |
Ratan Gupta | 8c83493 | 2017-04-14 16:30:24 +0530 | [diff] [blame] | 77 | */ |
| 78 | EthernetInterface(sdbusplus::bus::bus& bus, |
Ratan Gupta | 91a99cc | 2017-04-14 16:32:09 +0530 | [diff] [blame] | 79 | const std::string& objPath, |
Ratan Gupta | 4f1c18b | 2017-05-25 12:59:35 +0530 | [diff] [blame] | 80 | bool dhcpEnabled, |
Ratan Gupta | 3d3e4fc | 2017-07-25 13:38:19 +0530 | [diff] [blame] | 81 | Manager& parent, |
| 82 | bool emitSignal = true); |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 83 | |
| 84 | /** @brief Function to create ipaddress dbus object. |
| 85 | * @param[in] addressType - Type of ip address. |
Ratan Gupta | 29b0e43 | 2017-05-25 12:51:40 +0530 | [diff] [blame] | 86 | * @param[in] ipaddress- IP address. |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 87 | * @param[in] prefixLength - Length of prefix. |
| 88 | * @param[in] gateway - Gateway ip address. |
| 89 | */ |
| 90 | |
| 91 | void iP(IP::Protocol addressType, |
| 92 | std::string ipaddress, |
| 93 | uint8_t prefixLength, |
| 94 | std::string gateway) override; |
Ratan Gupta | 8c83493 | 2017-04-14 16:30:24 +0530 | [diff] [blame] | 95 | |
Ratan Gupta | 29b0e43 | 2017-05-25 12:51:40 +0530 | [diff] [blame] | 96 | /* @brief delete the dbus object of the given ipaddress. |
| 97 | * @param[in] ipaddress - IP address. |
Ratan Gupta | 2eff84f | 2017-04-20 19:19:15 +0530 | [diff] [blame] | 98 | */ |
Ratan Gupta | 2eff84f | 2017-04-20 19:19:15 +0530 | [diff] [blame] | 99 | void deleteObject(const std::string& ipaddress); |
Ratan Gupta | 8c83493 | 2017-04-14 16:30:24 +0530 | [diff] [blame] | 100 | |
Ratan Gupta | bc88629 | 2017-07-25 18:29:57 +0530 | [diff] [blame] | 101 | /* @brief delete the vlan dbus object of the given interface. |
| 102 | * Also deletes the device file and the network file. |
| 103 | * @param[in] interface - VLAN Interface. |
| 104 | */ |
| 105 | void deleteVLANObject(const std::string& interface); |
| 106 | |
Ratan Gupta | 87c1398 | 2017-06-15 09:27:27 +0530 | [diff] [blame] | 107 | /* @brief creates the dbus object(IPaddres) given in the address list. |
Ratan Gupta | 29b0e43 | 2017-05-25 12:51:40 +0530 | [diff] [blame] | 108 | * @param[in] addrs - address list for which dbus objects needs |
| 109 | * to create. |
| 110 | */ |
Ratan Gupta | 87c1398 | 2017-06-15 09:27:27 +0530 | [diff] [blame] | 111 | void createIPAddressObjects(); |
Ratan Gupta | 29b0e43 | 2017-05-25 12:51:40 +0530 | [diff] [blame] | 112 | |
| 113 | /* @brief Gets all the ip addresses. |
| 114 | * @returns the list of ipaddress. |
| 115 | */ |
| 116 | const AddressMap& getAddresses() const { return addrs; } |
Ratan Gupta | 8c83493 | 2017-04-14 16:30:24 +0530 | [diff] [blame] | 117 | |
Ratan Gupta | 87c1398 | 2017-06-15 09:27:27 +0530 | [diff] [blame] | 118 | /** Set value of DHCPEnabled */ |
| 119 | bool dHCPEnabled(bool value) override; |
| 120 | |
Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 121 | /** @brief sets the MAC address. |
| 122 | * @param[in] value - MAC address which needs to be set on the system. |
Gunnar Mills | 90480c4 | 2018-06-19 16:02:17 -0500 | [diff] [blame] | 123 | * @returns macAddress of the interface or throws an error. |
Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 124 | */ |
| 125 | std::string mACAddress(std::string value) override; |
| 126 | |
Ratan Gupta | 497c0c9 | 2017-08-22 19:15:59 +0530 | [diff] [blame] | 127 | /** @brief sets the NTP servers. |
| 128 | * @param[in] value - vector of NTP servers. |
| 129 | */ |
| 130 | ServerList nTPServers(ServerList value) override; |
| 131 | |
Ratan Gupta | 6dec390f | 2017-08-20 15:28:12 +0530 | [diff] [blame] | 132 | /** @brief sets the DNS/nameservers. |
| 133 | * @param[in] value - vector of DNS servers. |
| 134 | */ |
| 135 | ServerList nameservers(ServerList value) override; |
| 136 | |
Ratan Gupta | 5978dd1 | 2017-07-25 13:47:13 +0530 | [diff] [blame] | 137 | /** @brief create Vlan interface. |
| 138 | * @param[in] id- VLAN identifier. |
| 139 | */ |
| 140 | void createVLAN(VlanId id); |
| 141 | |
Ratan Gupta | 92bc2fe | 2017-07-26 22:40:21 +0530 | [diff] [blame] | 142 | /** @brief load the vlan info from the system |
| 143 | * and creates the ip address dbus objects. |
| 144 | * @param[in] vlanID- VLAN identifier. |
| 145 | */ |
| 146 | void loadVLAN(VlanId vlanID); |
| 147 | |
Ratan Gupta | 2b10653 | 2017-07-25 16:05:02 +0530 | [diff] [blame] | 148 | /** @brief write the network conf file with the in-memory objects. |
| 149 | */ |
| 150 | void writeConfigurationFile(); |
| 151 | |
Ratan Gupta | e9c9b81 | 2017-09-22 17:15:37 +0530 | [diff] [blame] | 152 | /** @brief delete all dbus objects. |
| 153 | */ |
| 154 | void deleteAll(); |
| 155 | |
Ratan Gupta | 87c1398 | 2017-06-15 09:27:27 +0530 | [diff] [blame] | 156 | using EthernetInterfaceIntf::dHCPEnabled; |
Ratan Gupta | 5978dd1 | 2017-07-25 13:47:13 +0530 | [diff] [blame] | 157 | using EthernetInterfaceIntf::interfaceName; |
Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 158 | using MacAddressIntf::mACAddress; |
Ratan Gupta | 5978dd1 | 2017-07-25 13:47:13 +0530 | [diff] [blame] | 159 | |
Ratan Gupta | 6dec390f | 2017-08-20 15:28:12 +0530 | [diff] [blame] | 160 | /** @brief Absolute path of the resolv conf file */ |
| 161 | static constexpr auto resolvConfFile = "/etc/resolv.conf"; |
| 162 | |
Ratan Gupta | 3d3e4fc | 2017-07-25 13:38:19 +0530 | [diff] [blame] | 163 | protected: |
Ratan Gupta | 8c83493 | 2017-04-14 16:30:24 +0530 | [diff] [blame] | 164 | /** @brief get the info of the ethernet interface. |
| 165 | * @return tuple having the link speed,autonegotiation,duplexmode . |
| 166 | */ |
Ratan Gupta | 8c83493 | 2017-04-14 16:30:24 +0530 | [diff] [blame] | 167 | InterfaceInfo getInterfaceInfo() const; |
| 168 | |
Ratan Gupta | e9c9b81 | 2017-09-22 17:15:37 +0530 | [diff] [blame] | 169 | /* @brief delete the vlan interface from system. |
| 170 | * @param[in] interface - vlan Interface. |
| 171 | */ |
| 172 | void deleteVLANFromSystem(const std::string& interface); |
| 173 | |
Ratan Gupta | 8c83493 | 2017-04-14 16:30:24 +0530 | [diff] [blame] | 174 | /** @brief get the mac address of the interface. |
Ratan Gupta | da7d3af | 2017-08-13 17:49:56 +0530 | [diff] [blame] | 175 | * @param[in] interfaceName - Network interface name. |
Ratan Gupta | 8c83493 | 2017-04-14 16:30:24 +0530 | [diff] [blame] | 176 | * @return macaddress on success |
| 177 | */ |
| 178 | |
Ratan Gupta | da7d3af | 2017-08-13 17:49:56 +0530 | [diff] [blame] | 179 | std::string getMACAddress(const std::string& interfaceName) const; |
Ratan Gupta | 8c83493 | 2017-04-14 16:30:24 +0530 | [diff] [blame] | 180 | |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 181 | /** @brief construct the ip address dbus object path. |
| 182 | * @param[in] addressType - Type of ip address. |
Ratan Gupta | 65e5abe | 2017-05-23 13:20:44 +0530 | [diff] [blame] | 183 | * @param[in] ipaddress - IP address. |
| 184 | * @param[in] prefixLength - Length of prefix. |
Gunnar Mills | 6af6144 | 2018-04-08 14:50:06 -0500 | [diff] [blame] | 185 | * @param[in] gateway - Gateway address. |
Ratan Gupta | 65e5abe | 2017-05-23 13:20:44 +0530 | [diff] [blame] | 186 | |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 187 | * @return path of the address object. |
| 188 | */ |
| 189 | |
Ratan Gupta | 65e5abe | 2017-05-23 13:20:44 +0530 | [diff] [blame] | 190 | std::string generateObjectPath(IP::Protocol addressType, |
| 191 | const std::string& ipaddress, |
| 192 | uint8_t prefixLength, |
| 193 | const std::string& gateway) const; |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 194 | |
Ratan Gupta | 65e5abe | 2017-05-23 13:20:44 +0530 | [diff] [blame] | 195 | /** @brief generates the id by doing hash of ipaddress, |
| 196 | * prefixlength and the gateway. |
| 197 | * @param[in] ipaddress - IP address. |
| 198 | * @param[in] prefixLength - Length of prefix. |
Gunnar Mills | 6af6144 | 2018-04-08 14:50:06 -0500 | [diff] [blame] | 199 | * @param[in] gateway - Gateway address. |
Ratan Gupta | 65e5abe | 2017-05-23 13:20:44 +0530 | [diff] [blame] | 200 | * @return hash string. |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 201 | */ |
| 202 | |
Ratan Gupta | 65e5abe | 2017-05-23 13:20:44 +0530 | [diff] [blame] | 203 | static std::string generateId(const std::string& ipaddress, |
| 204 | uint8_t prefixLength, |
| 205 | const std::string& gateway); |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 206 | |
Ratan Gupta | 2b10653 | 2017-07-25 16:05:02 +0530 | [diff] [blame] | 207 | /** @brief write the dhcp section **/ |
| 208 | void writeDHCPSection(std::fstream& stream);; |
| 209 | |
Ratan Gupta | 497c0c9 | 2017-08-22 19:15:59 +0530 | [diff] [blame] | 210 | /** @brief get the NTP server list from the network conf |
| 211 | * |
| 212 | */ |
| 213 | ServerList getNTPServersFromConf(); |
| 214 | |
Ratan Gupta | 6dec390f | 2017-08-20 15:28:12 +0530 | [diff] [blame] | 215 | /** @brief write the DNS entries to resolver file. |
| 216 | * @param[in] dnsList - DNS server list which needs to be written. |
| 217 | * @param[in] file - File to write the name server entries to. |
| 218 | */ |
| 219 | void writeDNSEntries(const ServerList& dnsList, |
| 220 | const std::string& file); |
| 221 | |
| 222 | /** @brief get the name server details from the network conf |
| 223 | * |
| 224 | */ |
| 225 | ServerList getNameServerFromConf(); |
| 226 | |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 227 | /** @brief Persistent sdbusplus DBus bus connection. */ |
| 228 | sdbusplus::bus::bus& bus; |
| 229 | |
Ratan Gupta | 4f1c18b | 2017-05-25 12:59:35 +0530 | [diff] [blame] | 230 | /** @brief Network Manager object. */ |
| 231 | Manager& manager; |
| 232 | |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 233 | /** @brief Persistent map of IPAddress dbus objects and their names */ |
Ratan Gupta | 29b0e43 | 2017-05-25 12:51:40 +0530 | [diff] [blame] | 234 | AddressMap addrs; |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 235 | |
Ratan Gupta | 5978dd1 | 2017-07-25 13:47:13 +0530 | [diff] [blame] | 236 | /** @brief Persistent map of VLAN interface dbus objects and their names */ |
| 237 | VlanInterfaceMap vlanInterfaces; |
| 238 | |
Ratan Gupta | 47722dc | 2017-05-26 18:32:23 +0530 | [diff] [blame] | 239 | /** @brief Dbus object path */ |
| 240 | std::string objPath; |
| 241 | |
| 242 | friend class TestEthernetInterface; |
Ratan Gupta | 8c83493 | 2017-04-14 16:30:24 +0530 | [diff] [blame] | 243 | }; |
| 244 | |
| 245 | } // namespace network |
| 246 | } // namespace phosphor |