blob: 36669d6d5017008f39699432b566ce0dc48ea149 [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>
12
13#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 Gupta82549cc2017-04-21 08:45:23 +053025 sdbusplus::xyz::openbmc_project::Network::IP::server::Create>;
Ratan Gupta8c834932017-04-14 16:30:24 +053026
Ratan Gupta82549cc2017-04-21 08:45:23 +053027using IP = sdbusplus::xyz::openbmc_project::Network::server::IP;
Ratan Gupta87c13982017-06-15 09:27:27 +053028
29using EthernetInterfaceIntf =
30 sdbusplus::xyz::openbmc_project::Network::server::EthernetInterface;
31
Ratan Gupta5978dd12017-07-25 13:47:13 +053032namespace fs = std::experimental::filesystem;
33
Ratan Gupta4f1c18b2017-05-25 12:59:35 +053034class Manager; // forward declaration of network manager.
Ratan Gupta8c834932017-04-14 16:30:24 +053035
Ratan Gupta47722dc2017-05-26 18:32:23 +053036class TestEthernetInterface;
37
Ratan Gupta5978dd12017-07-25 13:47:13 +053038class VlanInterface;
39
40class IPAddress;
41
Ratan Gupta8c834932017-04-14 16:30:24 +053042using LinkSpeed = uint16_t;
43using DuplexMode = uint8_t;
44using Autoneg = uint8_t;
Ratan Gupta5978dd12017-07-25 13:47:13 +053045using VlanId = uint32_t;
46using InterfaceName = std::string;
Ratan Gupta8c834932017-04-14 16:30:24 +053047using InterfaceInfo = std::tuple<LinkSpeed, DuplexMode, Autoneg>;
Ratan Gupta29b0e432017-05-25 12:51:40 +053048using AddressMap = std::map<std::string, std::shared_ptr<IPAddress>>;
Ratan Gupta5978dd12017-07-25 13:47:13 +053049using VlanInterfaceMap = std::map<InterfaceName, std::unique_ptr<VlanInterface>>;
Ratan Gupta8c834932017-04-14 16:30:24 +053050
51/** @class EthernetInterface
52 * @brief OpenBMC Ethernet Interface implementation.
53 * @details A concrete implementation for the
54 * xyz.openbmc_project.Network.EthernetInterface DBus API.
55 */
Ratan Gupta82549cc2017-04-21 08:45:23 +053056class EthernetInterface : public Ifaces
Ratan Gupta8c834932017-04-14 16:30:24 +053057{
58 public:
59 EthernetInterface() = delete;
60 EthernetInterface(const EthernetInterface&) = delete;
61 EthernetInterface& operator=(const EthernetInterface&) = delete;
62 EthernetInterface(EthernetInterface&&) = delete;
63 EthernetInterface& operator=(EthernetInterface&&) = delete;
64 virtual ~EthernetInterface() = default;
65
66 /** @brief Constructor to put object onto bus at a dbus path.
67 * @param[in] bus - Bus to attach to.
68 * @param[in] objPath - Path to attach at.
Ratan Gupta8c834932017-04-14 16:30:24 +053069 * @param[in] dhcpEnabled - is dhcp enabled(true/false).
Ratan Gupta4f1c18b2017-05-25 12:59:35 +053070 * @param[in] parent - parent object.
Ratan Gupta3d3e4fc2017-07-25 13:38:19 +053071 * @param[in] emitSignal - true if the object added signal needs to be
72 * send.
Ratan Gupta8c834932017-04-14 16:30:24 +053073 */
74 EthernetInterface(sdbusplus::bus::bus& bus,
Ratan Gupta91a99cc2017-04-14 16:32:09 +053075 const std::string& objPath,
Ratan Gupta4f1c18b2017-05-25 12:59:35 +053076 bool dhcpEnabled,
Ratan Gupta3d3e4fc2017-07-25 13:38:19 +053077 Manager& parent,
78 bool emitSignal = true);
Ratan Gupta82549cc2017-04-21 08:45:23 +053079
80 /** @brief Function to create ipaddress dbus object.
81 * @param[in] addressType - Type of ip address.
Ratan Gupta29b0e432017-05-25 12:51:40 +053082 * @param[in] ipaddress- IP address.
Ratan Gupta82549cc2017-04-21 08:45:23 +053083 * @param[in] prefixLength - Length of prefix.
84 * @param[in] gateway - Gateway ip address.
85 */
86
87 void iP(IP::Protocol addressType,
88 std::string ipaddress,
89 uint8_t prefixLength,
90 std::string gateway) override;
Ratan Gupta8c834932017-04-14 16:30:24 +053091
Ratan Gupta29b0e432017-05-25 12:51:40 +053092 /* @brief delete the dbus object of the given ipaddress.
93 * @param[in] ipaddress - IP address.
Ratan Gupta2eff84f2017-04-20 19:19:15 +053094 */
Ratan Gupta2eff84f2017-04-20 19:19:15 +053095 void deleteObject(const std::string& ipaddress);
Ratan Gupta8c834932017-04-14 16:30:24 +053096
Ratan Gupta87c13982017-06-15 09:27:27 +053097 /* @brief creates the dbus object(IPaddres) given in the address list.
Ratan Gupta29b0e432017-05-25 12:51:40 +053098 * @param[in] addrs - address list for which dbus objects needs
99 * to create.
100 */
Ratan Gupta87c13982017-06-15 09:27:27 +0530101 void createIPAddressObjects();
Ratan Gupta29b0e432017-05-25 12:51:40 +0530102
103 /* @brief Gets all the ip addresses.
104 * @returns the list of ipaddress.
105 */
106 const AddressMap& getAddresses() const { return addrs; }
Ratan Gupta8c834932017-04-14 16:30:24 +0530107
Ratan Gupta87c13982017-06-15 09:27:27 +0530108 /** Set value of DHCPEnabled */
109 bool dHCPEnabled(bool value) override;
110
Ratan Gupta5978dd12017-07-25 13:47:13 +0530111 /** @brief create Vlan interface.
112 * @param[in] id- VLAN identifier.
113 */
114 void createVLAN(VlanId id);
115
Ratan Gupta92bc2fe2017-07-26 22:40:21 +0530116 /** @brief load the vlan info from the system
117 * and creates the ip address dbus objects.
118 * @param[in] vlanID- VLAN identifier.
119 */
120 void loadVLAN(VlanId vlanID);
121
Ratan Gupta2b106532017-07-25 16:05:02 +0530122 /** @brief write the network conf file with the in-memory objects.
123 */
124 void writeConfigurationFile();
125
Ratan Gupta87c13982017-06-15 09:27:27 +0530126 using EthernetInterfaceIntf::dHCPEnabled;
Ratan Gupta5978dd12017-07-25 13:47:13 +0530127 using EthernetInterfaceIntf::interfaceName;
128
Ratan Gupta3d3e4fc2017-07-25 13:38:19 +0530129 protected:
Ratan Gupta8c834932017-04-14 16:30:24 +0530130
131 /** @brief get the info of the ethernet interface.
132 * @return tuple having the link speed,autonegotiation,duplexmode .
133 */
134
135 InterfaceInfo getInterfaceInfo() const;
136
137 /** @brief get the mac address of the interface.
138 * @return macaddress on success
139 */
140
141 std::string getMACAddress() const;
142
Ratan Gupta82549cc2017-04-21 08:45:23 +0530143 /** @brief construct the ip address dbus object path.
144 * @param[in] addressType - Type of ip address.
Ratan Gupta65e5abe2017-05-23 13:20:44 +0530145 * @param[in] ipaddress - IP address.
146 * @param[in] prefixLength - Length of prefix.
147 * @param[in] gateway - Gateway addess.
148
Ratan Gupta82549cc2017-04-21 08:45:23 +0530149 * @return path of the address object.
150 */
151
Ratan Gupta65e5abe2017-05-23 13:20:44 +0530152 std::string generateObjectPath(IP::Protocol addressType,
153 const std::string& ipaddress,
154 uint8_t prefixLength,
155 const std::string& gateway) const;
Ratan Gupta82549cc2017-04-21 08:45:23 +0530156
Ratan Gupta65e5abe2017-05-23 13:20:44 +0530157 /** @brief generates the id by doing hash of ipaddress,
158 * prefixlength and the gateway.
159 * @param[in] ipaddress - IP address.
160 * @param[in] prefixLength - Length of prefix.
161 * @param[in] gateway - Gateway addess.
162 * @return hash string.
Ratan Gupta82549cc2017-04-21 08:45:23 +0530163 */
164
Ratan Gupta65e5abe2017-05-23 13:20:44 +0530165 static std::string generateId(const std::string& ipaddress,
166 uint8_t prefixLength,
167 const std::string& gateway);
Ratan Gupta82549cc2017-04-21 08:45:23 +0530168
Ratan Gupta2b106532017-07-25 16:05:02 +0530169 /** @brief write the dhcp section **/
170 void writeDHCPSection(std::fstream& stream);;
171
Ratan Gupta82549cc2017-04-21 08:45:23 +0530172 /** @brief Persistent sdbusplus DBus bus connection. */
173 sdbusplus::bus::bus& bus;
174
Ratan Gupta4f1c18b2017-05-25 12:59:35 +0530175 /** @brief Network Manager object. */
176 Manager& manager;
177
Ratan Gupta82549cc2017-04-21 08:45:23 +0530178 /** @brief Persistent map of IPAddress dbus objects and their names */
Ratan Gupta29b0e432017-05-25 12:51:40 +0530179 AddressMap addrs;
Ratan Gupta82549cc2017-04-21 08:45:23 +0530180
Ratan Gupta5978dd12017-07-25 13:47:13 +0530181 /** @brief Persistent map of VLAN interface dbus objects and their names */
182 VlanInterfaceMap vlanInterfaces;
183
Ratan Gupta47722dc2017-05-26 18:32:23 +0530184 /** @brief Dbus object path */
185 std::string objPath;
186
187 friend class TestEthernetInterface;
Ratan Gupta8c834932017-04-14 16:30:24 +0530188};
189
190} // namespace network
191} // namespace phosphor