blob: 5283da4413614249aaaad950b7ddc7fdf4787c4a [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;
30
Ratan Gupta5978dd12017-07-25 13:47:13 +053031namespace fs = std::experimental::filesystem;
32
Ratan Gupta4f1c18b2017-05-25 12:59:35 +053033class Manager; // forward declaration of network manager.
Ratan Gupta8c834932017-04-14 16:30:24 +053034
Ratan Gupta47722dc2017-05-26 18:32:23 +053035class TestEthernetInterface;
36
Ratan Gupta5978dd12017-07-25 13:47:13 +053037class VlanInterface;
38
39class IPAddress;
40
Ratan Gupta8c834932017-04-14 16:30:24 +053041using LinkSpeed = uint16_t;
42using DuplexMode = uint8_t;
43using Autoneg = uint8_t;
Ratan Gupta5978dd12017-07-25 13:47:13 +053044using VlanId = uint32_t;
45using InterfaceName = std::string;
Ratan Gupta8c834932017-04-14 16:30:24 +053046using InterfaceInfo = std::tuple<LinkSpeed, DuplexMode, Autoneg>;
Ratan Gupta29b0e432017-05-25 12:51:40 +053047using AddressMap = std::map<std::string, std::shared_ptr<IPAddress>>;
Ratan Gupta5978dd12017-07-25 13:47:13 +053048using VlanInterfaceMap = std::map<InterfaceName, std::unique_ptr<VlanInterface>>;
Ratan Gupta8c834932017-04-14 16:30:24 +053049
50/** @class EthernetInterface
51 * @brief OpenBMC Ethernet Interface implementation.
52 * @details A concrete implementation for the
53 * xyz.openbmc_project.Network.EthernetInterface DBus API.
54 */
Ratan Gupta82549cc2017-04-21 08:45:23 +053055class EthernetInterface : public Ifaces
Ratan Gupta8c834932017-04-14 16:30:24 +053056{
57 public:
58 EthernetInterface() = delete;
59 EthernetInterface(const EthernetInterface&) = delete;
60 EthernetInterface& operator=(const EthernetInterface&) = delete;
61 EthernetInterface(EthernetInterface&&) = delete;
62 EthernetInterface& operator=(EthernetInterface&&) = delete;
63 virtual ~EthernetInterface() = default;
64
65 /** @brief Constructor to put object onto bus at a dbus path.
66 * @param[in] bus - Bus to attach to.
67 * @param[in] objPath - Path to attach at.
Ratan Gupta8c834932017-04-14 16:30:24 +053068 * @param[in] dhcpEnabled - is dhcp enabled(true/false).
Ratan Gupta4f1c18b2017-05-25 12:59:35 +053069 * @param[in] parent - parent object.
Ratan Gupta3d3e4fc2017-07-25 13:38:19 +053070 * @param[in] emitSignal - true if the object added signal needs to be
71 * send.
Ratan Gupta8c834932017-04-14 16:30:24 +053072 */
73 EthernetInterface(sdbusplus::bus::bus& bus,
Ratan Gupta91a99cc2017-04-14 16:32:09 +053074 const std::string& objPath,
Ratan Gupta4f1c18b2017-05-25 12:59:35 +053075 bool dhcpEnabled,
Ratan Gupta3d3e4fc2017-07-25 13:38:19 +053076 Manager& parent,
77 bool emitSignal = true);
Ratan Gupta82549cc2017-04-21 08:45:23 +053078
79 /** @brief Function to create ipaddress dbus object.
80 * @param[in] addressType - Type of ip address.
Ratan Gupta29b0e432017-05-25 12:51:40 +053081 * @param[in] ipaddress- IP address.
Ratan Gupta82549cc2017-04-21 08:45:23 +053082 * @param[in] prefixLength - Length of prefix.
83 * @param[in] gateway - Gateway ip address.
84 */
85
86 void iP(IP::Protocol addressType,
87 std::string ipaddress,
88 uint8_t prefixLength,
89 std::string gateway) override;
Ratan Gupta8c834932017-04-14 16:30:24 +053090
Ratan Gupta29b0e432017-05-25 12:51:40 +053091 /* @brief delete the dbus object of the given ipaddress.
92 * @param[in] ipaddress - IP address.
Ratan Gupta2eff84f2017-04-20 19:19:15 +053093 */
Ratan Gupta2eff84f2017-04-20 19:19:15 +053094 void deleteObject(const std::string& ipaddress);
Ratan Gupta8c834932017-04-14 16:30:24 +053095
Ratan Guptabc886292017-07-25 18:29:57 +053096 /* @brief delete the vlan dbus object of the given interface.
97 * Also deletes the device file and the network file.
98 * @param[in] interface - VLAN Interface.
99 */
100 void deleteVLANObject(const std::string& interface);
101
Ratan Gupta87c13982017-06-15 09:27:27 +0530102 /* @brief creates the dbus object(IPaddres) given in the address list.
Ratan Gupta29b0e432017-05-25 12:51:40 +0530103 * @param[in] addrs - address list for which dbus objects needs
104 * to create.
105 */
Ratan Gupta87c13982017-06-15 09:27:27 +0530106 void createIPAddressObjects();
Ratan Gupta29b0e432017-05-25 12:51:40 +0530107
108 /* @brief Gets all the ip addresses.
109 * @returns the list of ipaddress.
110 */
111 const AddressMap& getAddresses() const { return addrs; }
Ratan Gupta8c834932017-04-14 16:30:24 +0530112
Ratan Gupta87c13982017-06-15 09:27:27 +0530113 /** Set value of DHCPEnabled */
114 bool dHCPEnabled(bool value) override;
115
Ratan Gupta5978dd12017-07-25 13:47:13 +0530116 /** @brief create Vlan interface.
117 * @param[in] id- VLAN identifier.
118 */
119 void createVLAN(VlanId id);
120
Ratan Gupta92bc2fe2017-07-26 22:40:21 +0530121 /** @brief load the vlan info from the system
122 * and creates the ip address dbus objects.
123 * @param[in] vlanID- VLAN identifier.
124 */
125 void loadVLAN(VlanId vlanID);
126
Ratan Gupta2b106532017-07-25 16:05:02 +0530127 /** @brief write the network conf file with the in-memory objects.
128 */
129 void writeConfigurationFile();
130
Ratan Gupta87c13982017-06-15 09:27:27 +0530131 using EthernetInterfaceIntf::dHCPEnabled;
Ratan Gupta5978dd12017-07-25 13:47:13 +0530132 using EthernetInterfaceIntf::interfaceName;
133
Ratan Gupta3d3e4fc2017-07-25 13:38:19 +0530134 protected:
Ratan Gupta8c834932017-04-14 16:30:24 +0530135
136 /** @brief get the info of the ethernet interface.
137 * @return tuple having the link speed,autonegotiation,duplexmode .
138 */
139
140 InterfaceInfo getInterfaceInfo() const;
141
142 /** @brief get the mac address of the interface.
Ratan Guptada7d3af2017-08-13 17:49:56 +0530143 * @param[in] interfaceName - Network interface name.
Ratan Gupta8c834932017-04-14 16:30:24 +0530144 * @return macaddress on success
145 */
146
Ratan Guptada7d3af2017-08-13 17:49:56 +0530147 std::string getMACAddress(const std::string& interfaceName) const;
Ratan Gupta8c834932017-04-14 16:30:24 +0530148
Ratan Gupta82549cc2017-04-21 08:45:23 +0530149 /** @brief construct the ip address dbus object path.
150 * @param[in] addressType - Type of ip address.
Ratan Gupta65e5abe2017-05-23 13:20:44 +0530151 * @param[in] ipaddress - IP address.
152 * @param[in] prefixLength - Length of prefix.
153 * @param[in] gateway - Gateway addess.
154
Ratan Gupta82549cc2017-04-21 08:45:23 +0530155 * @return path of the address object.
156 */
157
Ratan Gupta65e5abe2017-05-23 13:20:44 +0530158 std::string generateObjectPath(IP::Protocol addressType,
159 const std::string& ipaddress,
160 uint8_t prefixLength,
161 const std::string& gateway) const;
Ratan Gupta82549cc2017-04-21 08:45:23 +0530162
Ratan Gupta65e5abe2017-05-23 13:20:44 +0530163 /** @brief generates the id by doing hash of ipaddress,
164 * prefixlength and the gateway.
165 * @param[in] ipaddress - IP address.
166 * @param[in] prefixLength - Length of prefix.
167 * @param[in] gateway - Gateway addess.
168 * @return hash string.
Ratan Gupta82549cc2017-04-21 08:45:23 +0530169 */
170
Ratan Gupta65e5abe2017-05-23 13:20:44 +0530171 static std::string generateId(const std::string& ipaddress,
172 uint8_t prefixLength,
173 const std::string& gateway);
Ratan Gupta82549cc2017-04-21 08:45:23 +0530174
Ratan Gupta2b106532017-07-25 16:05:02 +0530175 /** @brief write the dhcp section **/
176 void writeDHCPSection(std::fstream& stream);;
177
Ratan Gupta82549cc2017-04-21 08:45:23 +0530178 /** @brief Persistent sdbusplus DBus bus connection. */
179 sdbusplus::bus::bus& bus;
180
Ratan Gupta4f1c18b2017-05-25 12:59:35 +0530181 /** @brief Network Manager object. */
182 Manager& manager;
183
Ratan Gupta82549cc2017-04-21 08:45:23 +0530184 /** @brief Persistent map of IPAddress dbus objects and their names */
Ratan Gupta29b0e432017-05-25 12:51:40 +0530185 AddressMap addrs;
Ratan Gupta82549cc2017-04-21 08:45:23 +0530186
Ratan Gupta5978dd12017-07-25 13:47:13 +0530187 /** @brief Persistent map of VLAN interface dbus objects and their names */
188 VlanInterfaceMap vlanInterfaces;
189
Ratan Gupta47722dc2017-05-26 18:32:23 +0530190 /** @brief Dbus object path */
191 std::string objPath;
192
193 friend class TestEthernetInterface;
Ratan Gupta8c834932017-04-14 16:30:24 +0530194};
195
196} // namespace network
197} // namespace phosphor