blob: 3a746d20c61965a7eb58c9cdff79f820378b23df [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 Gupta8c834932017-04-14 16:30:24 +05309
10#include <sdbusplus/bus.hpp>
11#include <sdbusplus/server/object.hpp>
Ratan Gupta8c834932017-04-14 16:30:24 +053012#include <string>
Ratan Gupta5978dd12017-07-25 13:47:13 +053013#include <experimental/filesystem>
Ratan Gupta8c834932017-04-14 16:30:24 +053014
15namespace phosphor
16{
17namespace network
18{
Ratan Gupta8c834932017-04-14 16:30:24 +053019
Ratan Gupta82549cc2017-04-21 08:45:23 +053020using Ifaces =
21 sdbusplus::server::object::object<
22 sdbusplus::xyz::openbmc_project::Network::server::EthernetInterface,
Ratan Gupta1caa2302017-07-10 10:30:13 +053023 sdbusplus::xyz::openbmc_project::Network::server::MACAddress,
Ratan Gupta82549cc2017-04-21 08:45:23 +053024 sdbusplus::xyz::openbmc_project::Network::IP::server::Create>;
Ratan Gupta8c834932017-04-14 16:30:24 +053025
Ratan Gupta82549cc2017-04-21 08:45:23 +053026using IP = sdbusplus::xyz::openbmc_project::Network::server::IP;
Ratan Gupta87c13982017-06-15 09:27:27 +053027
28using EthernetInterfaceIntf =
29 sdbusplus::xyz::openbmc_project::Network::server::EthernetInterface;
Ratan Guptabd303b12017-08-18 17:10:07 +053030using MacAddressIntf =
31 sdbusplus::xyz::openbmc_project::Network::server::MACAddress;
Ratan Gupta87c13982017-06-15 09:27:27 +053032
Ratan Gupta5978dd12017-07-25 13:47:13 +053033namespace fs = std::experimental::filesystem;
34
Ratan Gupta4f1c18b2017-05-25 12:59:35 +053035class Manager; // forward declaration of network manager.
Ratan Gupta8c834932017-04-14 16:30:24 +053036
Ratan Gupta47722dc2017-05-26 18:32:23 +053037class TestEthernetInterface;
38
Ratan Gupta5978dd12017-07-25 13:47:13 +053039class VlanInterface;
40
41class IPAddress;
42
Ratan Gupta8c834932017-04-14 16:30:24 +053043using LinkSpeed = uint16_t;
44using DuplexMode = uint8_t;
45using Autoneg = uint8_t;
Ratan Gupta5978dd12017-07-25 13:47:13 +053046using VlanId = uint32_t;
47using InterfaceName = std::string;
Ratan Gupta8c834932017-04-14 16:30:24 +053048using InterfaceInfo = std::tuple<LinkSpeed, DuplexMode, Autoneg>;
Ratan Gupta29b0e432017-05-25 12:51:40 +053049using AddressMap = std::map<std::string, std::shared_ptr<IPAddress>>;
Ratan Gupta5978dd12017-07-25 13:47:13 +053050using VlanInterfaceMap = std::map<InterfaceName, std::unique_ptr<VlanInterface>>;
Ratan Gupta8c834932017-04-14 16:30:24 +053051
52/** @class EthernetInterface
53 * @brief OpenBMC Ethernet Interface implementation.
54 * @details A concrete implementation for the
55 * xyz.openbmc_project.Network.EthernetInterface DBus API.
56 */
Ratan Gupta82549cc2017-04-21 08:45:23 +053057class EthernetInterface : public Ifaces
Ratan Gupta8c834932017-04-14 16:30:24 +053058{
59 public:
60 EthernetInterface() = delete;
61 EthernetInterface(const EthernetInterface&) = delete;
62 EthernetInterface& operator=(const EthernetInterface&) = delete;
63 EthernetInterface(EthernetInterface&&) = delete;
64 EthernetInterface& operator=(EthernetInterface&&) = delete;
65 virtual ~EthernetInterface() = default;
66
67 /** @brief Constructor to put object onto bus at a dbus path.
68 * @param[in] bus - Bus to attach to.
69 * @param[in] objPath - Path to attach at.
Ratan Gupta8c834932017-04-14 16:30:24 +053070 * @param[in] dhcpEnabled - is dhcp enabled(true/false).
Ratan Gupta4f1c18b2017-05-25 12:59:35 +053071 * @param[in] parent - parent object.
Ratan Gupta3d3e4fc2017-07-25 13:38:19 +053072 * @param[in] emitSignal - true if the object added signal needs to be
73 * send.
Ratan Gupta8c834932017-04-14 16:30:24 +053074 */
75 EthernetInterface(sdbusplus::bus::bus& bus,
Ratan Gupta91a99cc2017-04-14 16:32:09 +053076 const std::string& objPath,
Ratan Gupta4f1c18b2017-05-25 12:59:35 +053077 bool dhcpEnabled,
Ratan Gupta3d3e4fc2017-07-25 13:38:19 +053078 Manager& parent,
79 bool emitSignal = true);
Ratan Gupta82549cc2017-04-21 08:45:23 +053080
81 /** @brief Function to create ipaddress dbus object.
82 * @param[in] addressType - Type of ip address.
Ratan Gupta29b0e432017-05-25 12:51:40 +053083 * @param[in] ipaddress- IP address.
Ratan Gupta82549cc2017-04-21 08:45:23 +053084 * @param[in] prefixLength - Length of prefix.
85 * @param[in] gateway - Gateway ip address.
86 */
87
88 void iP(IP::Protocol addressType,
89 std::string ipaddress,
90 uint8_t prefixLength,
91 std::string gateway) override;
Ratan Gupta8c834932017-04-14 16:30:24 +053092
Ratan Gupta29b0e432017-05-25 12:51:40 +053093 /* @brief delete the dbus object of the given ipaddress.
94 * @param[in] ipaddress - IP address.
Ratan Gupta2eff84f2017-04-20 19:19:15 +053095 */
Ratan Gupta2eff84f2017-04-20 19:19:15 +053096 void deleteObject(const std::string& ipaddress);
Ratan Gupta8c834932017-04-14 16:30:24 +053097
Ratan Guptabc886292017-07-25 18:29:57 +053098 /* @brief delete the vlan dbus object of the given interface.
99 * Also deletes the device file and the network file.
100 * @param[in] interface - VLAN Interface.
101 */
102 void deleteVLANObject(const std::string& interface);
103
Ratan Gupta87c13982017-06-15 09:27:27 +0530104 /* @brief creates the dbus object(IPaddres) given in the address list.
Ratan Gupta29b0e432017-05-25 12:51:40 +0530105 * @param[in] addrs - address list for which dbus objects needs
106 * to create.
107 */
Ratan Gupta87c13982017-06-15 09:27:27 +0530108 void createIPAddressObjects();
Ratan Gupta29b0e432017-05-25 12:51:40 +0530109
110 /* @brief Gets all the ip addresses.
111 * @returns the list of ipaddress.
112 */
113 const AddressMap& getAddresses() const { return addrs; }
Ratan Gupta8c834932017-04-14 16:30:24 +0530114
Ratan Gupta87c13982017-06-15 09:27:27 +0530115 /** Set value of DHCPEnabled */
116 bool dHCPEnabled(bool value) override;
117
Ratan Guptabd303b12017-08-18 17:10:07 +0530118 /** @brief sets the MAC address.
119 * @param[in] value - MAC address which needs to be set on the system.
120 * @returns macAddress of the interface.
121 */
122 std::string mACAddress(std::string value) override;
123
Ratan Gupta5978dd12017-07-25 13:47:13 +0530124 /** @brief create Vlan interface.
125 * @param[in] id- VLAN identifier.
126 */
127 void createVLAN(VlanId id);
128
Ratan Gupta92bc2fe2017-07-26 22:40:21 +0530129 /** @brief load the vlan info from the system
130 * and creates the ip address dbus objects.
131 * @param[in] vlanID- VLAN identifier.
132 */
133 void loadVLAN(VlanId vlanID);
134
Ratan Gupta2b106532017-07-25 16:05:02 +0530135 /** @brief write the network conf file with the in-memory objects.
136 */
137 void writeConfigurationFile();
138
Ratan Gupta87c13982017-06-15 09:27:27 +0530139 using EthernetInterfaceIntf::dHCPEnabled;
Ratan Gupta5978dd12017-07-25 13:47:13 +0530140 using EthernetInterfaceIntf::interfaceName;
Ratan Guptabd303b12017-08-18 17:10:07 +0530141 using MacAddressIntf::mACAddress;
Ratan Gupta5978dd12017-07-25 13:47:13 +0530142
Ratan Gupta3d3e4fc2017-07-25 13:38:19 +0530143 protected:
Ratan Gupta8c834932017-04-14 16:30:24 +0530144
145 /** @brief get the info of the ethernet interface.
146 * @return tuple having the link speed,autonegotiation,duplexmode .
147 */
148
149 InterfaceInfo getInterfaceInfo() const;
150
151 /** @brief get the mac address of the interface.
Ratan Guptada7d3af2017-08-13 17:49:56 +0530152 * @param[in] interfaceName - Network interface name.
Ratan Gupta8c834932017-04-14 16:30:24 +0530153 * @return macaddress on success
154 */
155
Ratan Guptada7d3af2017-08-13 17:49:56 +0530156 std::string getMACAddress(const std::string& interfaceName) const;
Ratan Gupta8c834932017-04-14 16:30:24 +0530157
Ratan Gupta82549cc2017-04-21 08:45:23 +0530158 /** @brief construct the ip address dbus object path.
159 * @param[in] addressType - Type of ip address.
Ratan Gupta65e5abe2017-05-23 13:20:44 +0530160 * @param[in] ipaddress - IP address.
161 * @param[in] prefixLength - Length of prefix.
162 * @param[in] gateway - Gateway addess.
163
Ratan Gupta82549cc2017-04-21 08:45:23 +0530164 * @return path of the address object.
165 */
166
Ratan Gupta65e5abe2017-05-23 13:20:44 +0530167 std::string generateObjectPath(IP::Protocol addressType,
168 const std::string& ipaddress,
169 uint8_t prefixLength,
170 const std::string& gateway) const;
Ratan Gupta82549cc2017-04-21 08:45:23 +0530171
Ratan Gupta65e5abe2017-05-23 13:20:44 +0530172 /** @brief generates the id by doing hash of ipaddress,
173 * prefixlength and the gateway.
174 * @param[in] ipaddress - IP address.
175 * @param[in] prefixLength - Length of prefix.
176 * @param[in] gateway - Gateway addess.
177 * @return hash string.
Ratan Gupta82549cc2017-04-21 08:45:23 +0530178 */
179
Ratan Gupta65e5abe2017-05-23 13:20:44 +0530180 static std::string generateId(const std::string& ipaddress,
181 uint8_t prefixLength,
182 const std::string& gateway);
Ratan Gupta82549cc2017-04-21 08:45:23 +0530183
Ratan Gupta2b106532017-07-25 16:05:02 +0530184 /** @brief write the dhcp section **/
185 void writeDHCPSection(std::fstream& stream);;
186
Ratan Gupta82549cc2017-04-21 08:45:23 +0530187 /** @brief Persistent sdbusplus DBus bus connection. */
188 sdbusplus::bus::bus& bus;
189
Ratan Gupta4f1c18b2017-05-25 12:59:35 +0530190 /** @brief Network Manager object. */
191 Manager& manager;
192
Ratan Gupta82549cc2017-04-21 08:45:23 +0530193 /** @brief Persistent map of IPAddress dbus objects and their names */
Ratan Gupta29b0e432017-05-25 12:51:40 +0530194 AddressMap addrs;
Ratan Gupta82549cc2017-04-21 08:45:23 +0530195
Ratan Gupta5978dd12017-07-25 13:47:13 +0530196 /** @brief Persistent map of VLAN interface dbus objects and their names */
197 VlanInterfaceMap vlanInterfaces;
198
Ratan Gupta47722dc2017-05-26 18:32:23 +0530199 /** @brief Dbus object path */
200 std::string objPath;
201
202 friend class TestEthernetInterface;
Ratan Gupta8c834932017-04-14 16:30:24 +0530203};
204
205} // namespace network
206} // namespace phosphor