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