blob: 06a2e5fcd23b4244eb2651f3254b903f186eb76f [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 Gupta5978dd12017-07-25 13:47:13 +053035namespace fs = std::experimental::filesystem;
36
Ratan Gupta4f1c18b2017-05-25 12:59:35 +053037class Manager; // forward declaration of network manager.
Ratan Gupta8c834932017-04-14 16:30:24 +053038
Ratan Gupta47722dc2017-05-26 18:32:23 +053039class TestEthernetInterface;
40
Ratan Gupta5978dd12017-07-25 13:47:13 +053041class VlanInterface;
42
43class IPAddress;
44
Ratan Gupta8c834932017-04-14 16:30:24 +053045using LinkSpeed = uint16_t;
46using DuplexMode = uint8_t;
47using Autoneg = uint8_t;
Ratan Gupta5978dd12017-07-25 13:47:13 +053048using VlanId = uint32_t;
49using InterfaceName = std::string;
Ratan Gupta8c834932017-04-14 16:30:24 +053050using InterfaceInfo = std::tuple<LinkSpeed, DuplexMode, Autoneg>;
Ratan Gupta29b0e432017-05-25 12:51:40 +053051using AddressMap = std::map<std::string, std::shared_ptr<IPAddress>>;
Ratan Gupta5978dd12017-07-25 13:47:13 +053052using VlanInterfaceMap = std::map<InterfaceName, std::unique_ptr<VlanInterface>>;
Ratan Gupta8c834932017-04-14 16:30:24 +053053
54/** @class EthernetInterface
55 * @brief OpenBMC Ethernet Interface implementation.
56 * @details A concrete implementation for the
57 * xyz.openbmc_project.Network.EthernetInterface DBus API.
58 */
Ratan Gupta82549cc2017-04-21 08:45:23 +053059class EthernetInterface : public Ifaces
Ratan Gupta8c834932017-04-14 16:30:24 +053060{
61 public:
62 EthernetInterface() = delete;
63 EthernetInterface(const EthernetInterface&) = delete;
64 EthernetInterface& operator=(const EthernetInterface&) = delete;
65 EthernetInterface(EthernetInterface&&) = delete;
66 EthernetInterface& operator=(EthernetInterface&&) = delete;
67 virtual ~EthernetInterface() = default;
68
69 /** @brief Constructor to put object onto bus at a dbus path.
70 * @param[in] bus - Bus to attach to.
71 * @param[in] objPath - Path to attach at.
Ratan Gupta8c834932017-04-14 16:30:24 +053072 * @param[in] dhcpEnabled - is dhcp enabled(true/false).
Ratan Gupta4f1c18b2017-05-25 12:59:35 +053073 * @param[in] parent - parent object.
Ratan Gupta3d3e4fc2017-07-25 13:38:19 +053074 * @param[in] emitSignal - true if the object added signal needs to be
75 * send.
Ratan Gupta8c834932017-04-14 16:30:24 +053076 */
77 EthernetInterface(sdbusplus::bus::bus& bus,
Ratan Gupta91a99cc2017-04-14 16:32:09 +053078 const std::string& objPath,
Ratan Gupta4f1c18b2017-05-25 12:59:35 +053079 bool dhcpEnabled,
Ratan Gupta3d3e4fc2017-07-25 13:38:19 +053080 Manager& parent,
81 bool emitSignal = true);
Ratan Gupta82549cc2017-04-21 08:45:23 +053082
83 /** @brief Function to create ipaddress dbus object.
84 * @param[in] addressType - Type of ip address.
Ratan Gupta29b0e432017-05-25 12:51:40 +053085 * @param[in] ipaddress- IP address.
Ratan Gupta82549cc2017-04-21 08:45:23 +053086 * @param[in] prefixLength - Length of prefix.
87 * @param[in] gateway - Gateway ip address.
88 */
89
90 void iP(IP::Protocol addressType,
91 std::string ipaddress,
92 uint8_t prefixLength,
93 std::string gateway) override;
Ratan Gupta8c834932017-04-14 16:30:24 +053094
Ratan Gupta29b0e432017-05-25 12:51:40 +053095 /* @brief delete the dbus object of the given ipaddress.
96 * @param[in] ipaddress - IP address.
Ratan Gupta2eff84f2017-04-20 19:19:15 +053097 */
Ratan Gupta2eff84f2017-04-20 19:19:15 +053098 void deleteObject(const std::string& ipaddress);
Ratan Gupta8c834932017-04-14 16:30:24 +053099
Ratan Guptabc886292017-07-25 18:29:57 +0530100 /* @brief delete the vlan dbus object of the given interface.
101 * Also deletes the device file and the network file.
102 * @param[in] interface - VLAN Interface.
103 */
104 void deleteVLANObject(const std::string& interface);
105
Ratan Gupta87c13982017-06-15 09:27:27 +0530106 /* @brief creates the dbus object(IPaddres) given in the address list.
Ratan Gupta29b0e432017-05-25 12:51:40 +0530107 * @param[in] addrs - address list for which dbus objects needs
108 * to create.
109 */
Ratan Gupta87c13982017-06-15 09:27:27 +0530110 void createIPAddressObjects();
Ratan Gupta29b0e432017-05-25 12:51:40 +0530111
112 /* @brief Gets all the ip addresses.
113 * @returns the list of ipaddress.
114 */
115 const AddressMap& getAddresses() const { return addrs; }
Ratan Gupta8c834932017-04-14 16:30:24 +0530116
Ratan Gupta87c13982017-06-15 09:27:27 +0530117 /** Set value of DHCPEnabled */
118 bool dHCPEnabled(bool value) override;
119
Ratan Guptabd303b12017-08-18 17:10:07 +0530120 /** @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.
123 */
124 std::string mACAddress(std::string value) override;
125
Ratan Gupta5978dd12017-07-25 13:47:13 +0530126 /** @brief create Vlan interface.
127 * @param[in] id- VLAN identifier.
128 */
129 void createVLAN(VlanId id);
130
Ratan Gupta92bc2fe2017-07-26 22:40:21 +0530131 /** @brief load the vlan info from the system
132 * and creates the ip address dbus objects.
133 * @param[in] vlanID- VLAN identifier.
134 */
135 void loadVLAN(VlanId vlanID);
136
Ratan Gupta2b106532017-07-25 16:05:02 +0530137 /** @brief write the network conf file with the in-memory objects.
138 */
139 void writeConfigurationFile();
140
Ratan Guptae9c9b812017-09-22 17:15:37 +0530141 /** @brief delete all dbus objects.
142 */
143 void deleteAll();
144
Ratan Gupta87c13982017-06-15 09:27:27 +0530145 using EthernetInterfaceIntf::dHCPEnabled;
Ratan Gupta5978dd12017-07-25 13:47:13 +0530146 using EthernetInterfaceIntf::interfaceName;
Ratan Guptabd303b12017-08-18 17:10:07 +0530147 using MacAddressIntf::mACAddress;
Ratan Gupta5978dd12017-07-25 13:47:13 +0530148
Ratan Gupta3d3e4fc2017-07-25 13:38:19 +0530149 protected:
Ratan Gupta8c834932017-04-14 16:30:24 +0530150 /** @brief get the info of the ethernet interface.
151 * @return tuple having the link speed,autonegotiation,duplexmode .
152 */
Ratan Gupta8c834932017-04-14 16:30:24 +0530153 InterfaceInfo getInterfaceInfo() const;
154
Ratan Guptae9c9b812017-09-22 17:15:37 +0530155 /* @brief delete the vlan interface from system.
156 * @param[in] interface - vlan Interface.
157 */
158 void deleteVLANFromSystem(const std::string& interface);
159
Ratan Gupta8c834932017-04-14 16:30:24 +0530160 /** @brief get the mac address of the interface.
Ratan Guptada7d3af2017-08-13 17:49:56 +0530161 * @param[in] interfaceName - Network interface name.
Ratan Gupta8c834932017-04-14 16:30:24 +0530162 * @return macaddress on success
163 */
164
Ratan Guptada7d3af2017-08-13 17:49:56 +0530165 std::string getMACAddress(const std::string& interfaceName) const;
Ratan Gupta8c834932017-04-14 16:30:24 +0530166
Ratan Gupta82549cc2017-04-21 08:45:23 +0530167 /** @brief construct the ip address dbus object path.
168 * @param[in] addressType - Type of ip address.
Ratan Gupta65e5abe2017-05-23 13:20:44 +0530169 * @param[in] ipaddress - IP address.
170 * @param[in] prefixLength - Length of prefix.
171 * @param[in] gateway - Gateway addess.
172
Ratan Gupta82549cc2017-04-21 08:45:23 +0530173 * @return path of the address object.
174 */
175
Ratan Gupta65e5abe2017-05-23 13:20:44 +0530176 std::string generateObjectPath(IP::Protocol addressType,
177 const std::string& ipaddress,
178 uint8_t prefixLength,
179 const std::string& gateway) const;
Ratan Gupta82549cc2017-04-21 08:45:23 +0530180
Ratan Gupta65e5abe2017-05-23 13:20:44 +0530181 /** @brief generates the id by doing hash of ipaddress,
182 * prefixlength and the gateway.
183 * @param[in] ipaddress - IP address.
184 * @param[in] prefixLength - Length of prefix.
185 * @param[in] gateway - Gateway addess.
186 * @return hash string.
Ratan Gupta82549cc2017-04-21 08:45:23 +0530187 */
188
Ratan Gupta65e5abe2017-05-23 13:20:44 +0530189 static std::string generateId(const std::string& ipaddress,
190 uint8_t prefixLength,
191 const std::string& gateway);
Ratan Gupta82549cc2017-04-21 08:45:23 +0530192
Ratan Gupta2b106532017-07-25 16:05:02 +0530193 /** @brief write the dhcp section **/
194 void writeDHCPSection(std::fstream& stream);;
195
Ratan Gupta82549cc2017-04-21 08:45:23 +0530196 /** @brief Persistent sdbusplus DBus bus connection. */
197 sdbusplus::bus::bus& bus;
198
Ratan Gupta4f1c18b2017-05-25 12:59:35 +0530199 /** @brief Network Manager object. */
200 Manager& manager;
201
Ratan Gupta82549cc2017-04-21 08:45:23 +0530202 /** @brief Persistent map of IPAddress dbus objects and their names */
Ratan Gupta29b0e432017-05-25 12:51:40 +0530203 AddressMap addrs;
Ratan Gupta82549cc2017-04-21 08:45:23 +0530204
Ratan Gupta5978dd12017-07-25 13:47:13 +0530205 /** @brief Persistent map of VLAN interface dbus objects and their names */
206 VlanInterfaceMap vlanInterfaces;
207
Ratan Gupta47722dc2017-05-26 18:32:23 +0530208 /** @brief Dbus object path */
209 std::string objPath;
210
211 friend class TestEthernetInterface;
Ratan Gupta8c834932017-04-14 16:30:24 +0530212};
213
214} // namespace network
215} // namespace phosphor