blob: 4617f56bdc224f811569cda9c94b4909cace7fcd [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 Gupta2b106532017-07-25 16:05:02 +0530116 /** @brief write the network conf file with the in-memory objects.
117 */
118 void writeConfigurationFile();
119
120
Ratan Gupta87c13982017-06-15 09:27:27 +0530121 using EthernetInterfaceIntf::dHCPEnabled;
Ratan Gupta5978dd12017-07-25 13:47:13 +0530122 using EthernetInterfaceIntf::interfaceName;
123
Ratan Gupta3d3e4fc2017-07-25 13:38:19 +0530124 protected:
Ratan Gupta8c834932017-04-14 16:30:24 +0530125
126 /** @brief get the info of the ethernet interface.
127 * @return tuple having the link speed,autonegotiation,duplexmode .
128 */
129
130 InterfaceInfo getInterfaceInfo() const;
131
132 /** @brief get the mac address of the interface.
133 * @return macaddress on success
134 */
135
136 std::string getMACAddress() const;
137
Ratan Gupta82549cc2017-04-21 08:45:23 +0530138 /** @brief construct the ip address dbus object path.
139 * @param[in] addressType - Type of ip address.
Ratan Gupta65e5abe2017-05-23 13:20:44 +0530140 * @param[in] ipaddress - IP address.
141 * @param[in] prefixLength - Length of prefix.
142 * @param[in] gateway - Gateway addess.
143
Ratan Gupta82549cc2017-04-21 08:45:23 +0530144 * @return path of the address object.
145 */
146
Ratan Gupta65e5abe2017-05-23 13:20:44 +0530147 std::string generateObjectPath(IP::Protocol addressType,
148 const std::string& ipaddress,
149 uint8_t prefixLength,
150 const std::string& gateway) const;
Ratan Gupta82549cc2017-04-21 08:45:23 +0530151
Ratan Gupta65e5abe2017-05-23 13:20:44 +0530152 /** @brief generates the id by doing hash of ipaddress,
153 * prefixlength and the gateway.
154 * @param[in] ipaddress - IP address.
155 * @param[in] prefixLength - Length of prefix.
156 * @param[in] gateway - Gateway addess.
157 * @return hash string.
Ratan Gupta82549cc2017-04-21 08:45:23 +0530158 */
159
Ratan Gupta65e5abe2017-05-23 13:20:44 +0530160 static std::string generateId(const std::string& ipaddress,
161 uint8_t prefixLength,
162 const std::string& gateway);
Ratan Gupta82549cc2017-04-21 08:45:23 +0530163
Ratan Gupta2b106532017-07-25 16:05:02 +0530164 /** @brief write the dhcp section **/
165 void writeDHCPSection(std::fstream& stream);;
166
Ratan Gupta82549cc2017-04-21 08:45:23 +0530167 /** @brief Persistent sdbusplus DBus bus connection. */
168 sdbusplus::bus::bus& bus;
169
Ratan Gupta4f1c18b2017-05-25 12:59:35 +0530170 /** @brief Network Manager object. */
171 Manager& manager;
172
Ratan Gupta82549cc2017-04-21 08:45:23 +0530173 /** @brief Persistent map of IPAddress dbus objects and their names */
Ratan Gupta29b0e432017-05-25 12:51:40 +0530174 AddressMap addrs;
Ratan Gupta82549cc2017-04-21 08:45:23 +0530175
Ratan Gupta5978dd12017-07-25 13:47:13 +0530176 /** @brief Persistent map of VLAN interface dbus objects and their names */
177 VlanInterfaceMap vlanInterfaces;
178
Ratan Gupta47722dc2017-05-26 18:32:23 +0530179 /** @brief Dbus object path */
180 std::string objPath;
181
182 friend class TestEthernetInterface;
Ratan Gupta8c834932017-04-14 16:30:24 +0530183};
184
185} // namespace network
186} // namespace phosphor