blob: 8ef1795a0b706b26091d020e0a7ba73fa9affd92 [file] [log] [blame]
Ratan Gupta8c834932017-04-14 16:30:24 +05301#pragma once
2
Ratan Gupta82549cc2017-04-21 08:45:23 +05303#include "types.hpp"
Ratan Gupta8804feb2017-05-25 10:49:57 +05304#include "util.hpp"
Ratan Gupta82549cc2017-04-21 08:45:23 +05305
Ratan Gupta8c834932017-04-14 16:30:24 +05306#include "xyz/openbmc_project/Network/EthernetInterface/server.hpp"
Ratan Gupta1caa2302017-07-10 10:30:13 +05307#include "xyz/openbmc_project/Network/MACAddress/server.hpp"
Ratan Gupta82549cc2017-04-21 08:45:23 +05308#include "xyz/openbmc_project/Network/IP/Create/server.hpp"
Ratan Guptae9c9b812017-09-22 17:15:37 +05309#include "xyz/openbmc_project/Collection/DeleteAll/server.hpp"
Ratan Gupta8c834932017-04-14 16:30:24 +053010
11#include <sdbusplus/bus.hpp>
12#include <sdbusplus/server/object.hpp>
Ratan Gupta8c834932017-04-14 16:30:24 +053013#include <string>
Ratan Gupta5978dd12017-07-25 13:47:13 +053014#include <experimental/filesystem>
Ratan Gupta8c834932017-04-14 16:30:24 +053015
16namespace phosphor
17{
18namespace network
19{
Ratan Gupta8c834932017-04-14 16:30:24 +053020
Ratan Gupta82549cc2017-04-21 08:45:23 +053021using Ifaces =
22 sdbusplus::server::object::object<
23 sdbusplus::xyz::openbmc_project::Network::server::EthernetInterface,
Ratan Gupta1caa2302017-07-10 10:30:13 +053024 sdbusplus::xyz::openbmc_project::Network::server::MACAddress,
Ratan Guptae9c9b812017-09-22 17:15:37 +053025 sdbusplus::xyz::openbmc_project::Network::IP::server::Create,
26 sdbusplus::xyz::openbmc_project::Collection::server::DeleteAll>;
Ratan Gupta8c834932017-04-14 16:30:24 +053027
Ratan Gupta82549cc2017-04-21 08:45:23 +053028using IP = sdbusplus::xyz::openbmc_project::Network::server::IP;
Ratan Gupta87c13982017-06-15 09:27:27 +053029
30using EthernetInterfaceIntf =
31 sdbusplus::xyz::openbmc_project::Network::server::EthernetInterface;
Ratan Guptabd303b12017-08-18 17:10:07 +053032using MacAddressIntf =
33 sdbusplus::xyz::openbmc_project::Network::server::MACAddress;
Ratan Gupta87c13982017-06-15 09:27:27 +053034
Ratan Gupta497c0c92017-08-22 19:15:59 +053035using ServerList = std::vector<std::string>;
36
Ratan Gupta5978dd12017-07-25 13:47:13 +053037namespace fs = std::experimental::filesystem;
38
Ratan Gupta4f1c18b2017-05-25 12:59:35 +053039class Manager; // forward declaration of network manager.
Ratan Gupta8c834932017-04-14 16:30:24 +053040
Ratan Gupta47722dc2017-05-26 18:32:23 +053041class TestEthernetInterface;
42
Ratan Gupta5978dd12017-07-25 13:47:13 +053043class VlanInterface;
44
45class IPAddress;
46
Ratan Gupta8c834932017-04-14 16:30:24 +053047using LinkSpeed = uint16_t;
48using DuplexMode = uint8_t;
49using Autoneg = uint8_t;
Ratan Gupta5978dd12017-07-25 13:47:13 +053050using VlanId = uint32_t;
51using InterfaceName = std::string;
Ratan Gupta8c834932017-04-14 16:30:24 +053052using InterfaceInfo = std::tuple<LinkSpeed, DuplexMode, Autoneg>;
Ratan Gupta29b0e432017-05-25 12:51:40 +053053using AddressMap = std::map<std::string, std::shared_ptr<IPAddress>>;
Ratan Gupta5978dd12017-07-25 13:47:13 +053054using VlanInterfaceMap = std::map<InterfaceName, std::unique_ptr<VlanInterface>>;
Ratan Gupta8c834932017-04-14 16:30:24 +053055
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 Gupta82549cc2017-04-21 08:45:23 +053061class EthernetInterface : public Ifaces
Ratan Gupta8c834932017-04-14 16:30:24 +053062{
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 Gupta8c834932017-04-14 16:30:24 +053074 * @param[in] dhcpEnabled - is dhcp enabled(true/false).
Ratan Gupta4f1c18b2017-05-25 12:59:35 +053075 * @param[in] parent - parent object.
Ratan Gupta3d3e4fc2017-07-25 13:38:19 +053076 * @param[in] emitSignal - true if the object added signal needs to be
77 * send.
Ratan Gupta8c834932017-04-14 16:30:24 +053078 */
79 EthernetInterface(sdbusplus::bus::bus& bus,
Ratan Gupta91a99cc2017-04-14 16:32:09 +053080 const std::string& objPath,
Ratan Gupta4f1c18b2017-05-25 12:59:35 +053081 bool dhcpEnabled,
Ratan Gupta3d3e4fc2017-07-25 13:38:19 +053082 Manager& parent,
83 bool emitSignal = true);
Ratan Gupta82549cc2017-04-21 08:45:23 +053084
85 /** @brief Function to create ipaddress dbus object.
86 * @param[in] addressType - Type of ip address.
Ratan Gupta29b0e432017-05-25 12:51:40 +053087 * @param[in] ipaddress- IP address.
Ratan Gupta82549cc2017-04-21 08:45:23 +053088 * @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 Gupta8c834932017-04-14 16:30:24 +053096
Ratan Gupta29b0e432017-05-25 12:51:40 +053097 /* @brief delete the dbus object of the given ipaddress.
98 * @param[in] ipaddress - IP address.
Ratan Gupta2eff84f2017-04-20 19:19:15 +053099 */
Ratan Gupta2eff84f2017-04-20 19:19:15 +0530100 void deleteObject(const std::string& ipaddress);
Ratan Gupta8c834932017-04-14 16:30:24 +0530101
Ratan Guptabc886292017-07-25 18:29:57 +0530102 /* @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 Gupta87c13982017-06-15 09:27:27 +0530108 /* @brief creates the dbus object(IPaddres) given in the address list.
Ratan Gupta29b0e432017-05-25 12:51:40 +0530109 * @param[in] addrs - address list for which dbus objects needs
110 * to create.
111 */
Ratan Gupta87c13982017-06-15 09:27:27 +0530112 void createIPAddressObjects();
Ratan Gupta29b0e432017-05-25 12:51:40 +0530113
114 /* @brief Gets all the ip addresses.
115 * @returns the list of ipaddress.
116 */
117 const AddressMap& getAddresses() const { return addrs; }
Ratan Gupta8c834932017-04-14 16:30:24 +0530118
Ratan Gupta87c13982017-06-15 09:27:27 +0530119 /** Set value of DHCPEnabled */
120 bool dHCPEnabled(bool value) override;
121
Ratan Guptabd303b12017-08-18 17:10:07 +0530122 /** @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 Gupta497c0c92017-08-22 19:15:59 +0530128 /** @brief sets the NTP servers.
129 * @param[in] value - vector of NTP servers.
130 */
131 ServerList nTPServers(ServerList value) override;
132
Ratan Gupta5978dd12017-07-25 13:47:13 +0530133 /** @brief create Vlan interface.
134 * @param[in] id- VLAN identifier.
135 */
136 void createVLAN(VlanId id);
137
Ratan Gupta92bc2fe2017-07-26 22:40:21 +0530138 /** @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 Gupta2b106532017-07-25 16:05:02 +0530144 /** @brief write the network conf file with the in-memory objects.
145 */
146 void writeConfigurationFile();
147
Ratan Guptae9c9b812017-09-22 17:15:37 +0530148 /** @brief delete all dbus objects.
149 */
150 void deleteAll();
151
Ratan Gupta87c13982017-06-15 09:27:27 +0530152 using EthernetInterfaceIntf::dHCPEnabled;
Ratan Gupta5978dd12017-07-25 13:47:13 +0530153 using EthernetInterfaceIntf::interfaceName;
Ratan Guptabd303b12017-08-18 17:10:07 +0530154 using MacAddressIntf::mACAddress;
Ratan Gupta5978dd12017-07-25 13:47:13 +0530155
Ratan Gupta3d3e4fc2017-07-25 13:38:19 +0530156 protected:
Ratan Gupta8c834932017-04-14 16:30:24 +0530157 /** @brief get the info of the ethernet interface.
158 * @return tuple having the link speed,autonegotiation,duplexmode .
159 */
Ratan Gupta8c834932017-04-14 16:30:24 +0530160 InterfaceInfo getInterfaceInfo() const;
161
Ratan Guptae9c9b812017-09-22 17:15:37 +0530162 /* @brief delete the vlan interface from system.
163 * @param[in] interface - vlan Interface.
164 */
165 void deleteVLANFromSystem(const std::string& interface);
166
Ratan Gupta8c834932017-04-14 16:30:24 +0530167 /** @brief get the mac address of the interface.
Ratan Guptada7d3af2017-08-13 17:49:56 +0530168 * @param[in] interfaceName - Network interface name.
Ratan Gupta8c834932017-04-14 16:30:24 +0530169 * @return macaddress on success
170 */
171
Ratan Guptada7d3af2017-08-13 17:49:56 +0530172 std::string getMACAddress(const std::string& interfaceName) const;
Ratan Gupta8c834932017-04-14 16:30:24 +0530173
Ratan Gupta82549cc2017-04-21 08:45:23 +0530174 /** @brief construct the ip address dbus object path.
175 * @param[in] addressType - Type of ip address.
Ratan Gupta65e5abe2017-05-23 13:20:44 +0530176 * @param[in] ipaddress - IP address.
177 * @param[in] prefixLength - Length of prefix.
178 * @param[in] gateway - Gateway addess.
179
Ratan Gupta82549cc2017-04-21 08:45:23 +0530180 * @return path of the address object.
181 */
182
Ratan Gupta65e5abe2017-05-23 13:20:44 +0530183 std::string generateObjectPath(IP::Protocol addressType,
184 const std::string& ipaddress,
185 uint8_t prefixLength,
186 const std::string& gateway) const;
Ratan Gupta82549cc2017-04-21 08:45:23 +0530187
Ratan Gupta65e5abe2017-05-23 13:20:44 +0530188 /** @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 Gupta82549cc2017-04-21 08:45:23 +0530194 */
195
Ratan Gupta65e5abe2017-05-23 13:20:44 +0530196 static std::string generateId(const std::string& ipaddress,
197 uint8_t prefixLength,
198 const std::string& gateway);
Ratan Gupta82549cc2017-04-21 08:45:23 +0530199
Ratan Gupta2b106532017-07-25 16:05:02 +0530200 /** @brief write the dhcp section **/
201 void writeDHCPSection(std::fstream& stream);;
202
Ratan Gupta497c0c92017-08-22 19:15:59 +0530203 /** @brief get the NTP server list from the network conf
204 *
205 */
206 ServerList getNTPServersFromConf();
207
Ratan Gupta82549cc2017-04-21 08:45:23 +0530208 /** @brief Persistent sdbusplus DBus bus connection. */
209 sdbusplus::bus::bus& bus;
210
Ratan Gupta4f1c18b2017-05-25 12:59:35 +0530211 /** @brief Network Manager object. */
212 Manager& manager;
213
Ratan Gupta82549cc2017-04-21 08:45:23 +0530214 /** @brief Persistent map of IPAddress dbus objects and their names */
Ratan Gupta29b0e432017-05-25 12:51:40 +0530215 AddressMap addrs;
Ratan Gupta82549cc2017-04-21 08:45:23 +0530216
Ratan Gupta5978dd12017-07-25 13:47:13 +0530217 /** @brief Persistent map of VLAN interface dbus objects and their names */
218 VlanInterfaceMap vlanInterfaces;
219
Ratan Gupta47722dc2017-05-26 18:32:23 +0530220 /** @brief Dbus object path */
221 std::string objPath;
222
223 friend class TestEthernetInterface;
Ratan Gupta8c834932017-04-14 16:30:24 +0530224};
225
226} // namespace network
227} // namespace phosphor