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