blob: 7865ab3ebdca43c54830d1f3e683b7ffa6adbbfa [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#include "xyz/openbmc_project/Network/IP/Create/server.hpp"
Ratan Gupta8c834932017-04-14 16:30:24 +05306
Patrick Venture189d44e2018-07-09 12:30:59 -07007#include <experimental/filesystem>
Ratan Gupta8c834932017-04-14 16:30:24 +05308#include <sdbusplus/bus.hpp>
9#include <sdbusplus/server/object.hpp>
Ratan Gupta8c834932017-04-14 16:30:24 +053010#include <string>
Patrick Venture189d44e2018-07-09 12:30:59 -070011#include <xyz/openbmc_project/Collection/DeleteAll/server.hpp>
12#include <xyz/openbmc_project/Network/EthernetInterface/server.hpp>
13#include <xyz/openbmc_project/Network/MACAddress/server.hpp>
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 Guptae9c9b812017-09-22 17:15:37 +053024 sdbusplus::xyz::openbmc_project::Network::IP::server::Create,
25 sdbusplus::xyz::openbmc_project::Collection::server::DeleteAll>;
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;
Ratan Guptabd303b12017-08-18 17:10:07 +053031using MacAddressIntf =
32 sdbusplus::xyz::openbmc_project::Network::server::MACAddress;
Ratan Gupta87c13982017-06-15 09:27:27 +053033
Ratan Gupta497c0c92017-08-22 19:15:59 +053034using ServerList = std::vector<std::string>;
35
Ratan Gupta5978dd12017-07-25 13:47:13 +053036namespace fs = std::experimental::filesystem;
37
Ratan Gupta4f1c18b2017-05-25 12:59:35 +053038class Manager; // forward declaration of network manager.
Ratan Gupta8c834932017-04-14 16:30:24 +053039
Ratan Gupta47722dc2017-05-26 18:32:23 +053040class TestEthernetInterface;
41
Ratan Gupta5978dd12017-07-25 13:47:13 +053042class VlanInterface;
43
44class IPAddress;
45
Ratan Gupta8c834932017-04-14 16:30:24 +053046using LinkSpeed = uint16_t;
47using DuplexMode = uint8_t;
48using Autoneg = uint8_t;
Ratan Gupta5978dd12017-07-25 13:47:13 +053049using VlanId = uint32_t;
50using InterfaceName = std::string;
Ratan Gupta8c834932017-04-14 16:30:24 +053051using InterfaceInfo = std::tuple<LinkSpeed, DuplexMode, Autoneg>;
Ratan Gupta29b0e432017-05-25 12:51:40 +053052using AddressMap = std::map<std::string, std::shared_ptr<IPAddress>>;
Ratan Gupta5978dd12017-07-25 13:47:13 +053053using VlanInterfaceMap = std::map<InterfaceName, std::unique_ptr<VlanInterface>>;
Ratan Gupta8c834932017-04-14 16:30:24 +053054
55/** @class EthernetInterface
56 * @brief OpenBMC Ethernet Interface implementation.
57 * @details A concrete implementation for the
58 * xyz.openbmc_project.Network.EthernetInterface DBus API.
59 */
Ratan Gupta82549cc2017-04-21 08:45:23 +053060class EthernetInterface : public Ifaces
Ratan Gupta8c834932017-04-14 16:30:24 +053061{
62 public:
63 EthernetInterface() = delete;
64 EthernetInterface(const EthernetInterface&) = delete;
65 EthernetInterface& operator=(const EthernetInterface&) = delete;
66 EthernetInterface(EthernetInterface&&) = delete;
67 EthernetInterface& operator=(EthernetInterface&&) = delete;
68 virtual ~EthernetInterface() = default;
69
70 /** @brief Constructor to put object onto bus at a dbus path.
71 * @param[in] bus - Bus to attach to.
72 * @param[in] objPath - Path to attach at.
Ratan Gupta8c834932017-04-14 16:30:24 +053073 * @param[in] dhcpEnabled - is dhcp enabled(true/false).
Ratan Gupta4f1c18b2017-05-25 12:59:35 +053074 * @param[in] parent - parent object.
Ratan Gupta3d3e4fc2017-07-25 13:38:19 +053075 * @param[in] emitSignal - true if the object added signal needs to be
76 * send.
Ratan Gupta8c834932017-04-14 16:30:24 +053077 */
78 EthernetInterface(sdbusplus::bus::bus& bus,
Ratan Gupta91a99cc2017-04-14 16:32:09 +053079 const std::string& objPath,
Ratan Gupta4f1c18b2017-05-25 12:59:35 +053080 bool dhcpEnabled,
Ratan Gupta3d3e4fc2017-07-25 13:38:19 +053081 Manager& parent,
82 bool emitSignal = true);
Ratan Gupta82549cc2017-04-21 08:45:23 +053083
84 /** @brief Function to create ipaddress dbus object.
85 * @param[in] addressType - Type of ip address.
Ratan Gupta29b0e432017-05-25 12:51:40 +053086 * @param[in] ipaddress- IP address.
Ratan Gupta82549cc2017-04-21 08:45:23 +053087 * @param[in] prefixLength - Length of prefix.
88 * @param[in] gateway - Gateway ip address.
89 */
90
91 void iP(IP::Protocol addressType,
92 std::string ipaddress,
93 uint8_t prefixLength,
94 std::string gateway) override;
Ratan Gupta8c834932017-04-14 16:30:24 +053095
Ratan Gupta29b0e432017-05-25 12:51:40 +053096 /* @brief delete the dbus object of the given ipaddress.
97 * @param[in] ipaddress - IP address.
Ratan Gupta2eff84f2017-04-20 19:19:15 +053098 */
Ratan Gupta2eff84f2017-04-20 19:19:15 +053099 void deleteObject(const std::string& ipaddress);
Ratan Gupta8c834932017-04-14 16:30:24 +0530100
Ratan Guptabc886292017-07-25 18:29:57 +0530101 /* @brief delete the vlan dbus object of the given interface.
102 * Also deletes the device file and the network file.
103 * @param[in] interface - VLAN Interface.
104 */
105 void deleteVLANObject(const std::string& interface);
106
Ratan Gupta87c13982017-06-15 09:27:27 +0530107 /* @brief creates the dbus object(IPaddres) given in the address list.
Ratan Gupta29b0e432017-05-25 12:51:40 +0530108 * @param[in] addrs - address list for which dbus objects needs
109 * to create.
110 */
Ratan Gupta87c13982017-06-15 09:27:27 +0530111 void createIPAddressObjects();
Ratan Gupta29b0e432017-05-25 12:51:40 +0530112
113 /* @brief Gets all the ip addresses.
114 * @returns the list of ipaddress.
115 */
116 const AddressMap& getAddresses() const { return addrs; }
Ratan Gupta8c834932017-04-14 16:30:24 +0530117
Ratan Gupta87c13982017-06-15 09:27:27 +0530118 /** Set value of DHCPEnabled */
119 bool dHCPEnabled(bool value) override;
120
Ratan Guptabd303b12017-08-18 17:10:07 +0530121 /** @brief sets the MAC address.
122 * @param[in] value - MAC address which needs to be set on the system.
Gunnar Mills90480c42018-06-19 16:02:17 -0500123 * @returns macAddress of the interface or throws an error.
Ratan Guptabd303b12017-08-18 17:10:07 +0530124 */
125 std::string mACAddress(std::string value) override;
126
Ratan Gupta497c0c92017-08-22 19:15:59 +0530127 /** @brief sets the NTP servers.
128 * @param[in] value - vector of NTP servers.
129 */
130 ServerList nTPServers(ServerList value) override;
131
Ratan Gupta6dec390f2017-08-20 15:28:12 +0530132 /** @brief sets the DNS/nameservers.
133 * @param[in] value - vector of DNS servers.
134 */
135 ServerList nameservers(ServerList value) override;
136
Ratan Gupta5978dd12017-07-25 13:47:13 +0530137 /** @brief create Vlan interface.
138 * @param[in] id- VLAN identifier.
139 */
140 void createVLAN(VlanId id);
141
Ratan Gupta92bc2fe2017-07-26 22:40:21 +0530142 /** @brief load the vlan info from the system
143 * and creates the ip address dbus objects.
144 * @param[in] vlanID- VLAN identifier.
145 */
146 void loadVLAN(VlanId vlanID);
147
Ratan Gupta2b106532017-07-25 16:05:02 +0530148 /** @brief write the network conf file with the in-memory objects.
149 */
150 void writeConfigurationFile();
151
Ratan Guptae9c9b812017-09-22 17:15:37 +0530152 /** @brief delete all dbus objects.
153 */
154 void deleteAll();
155
Ratan Gupta87c13982017-06-15 09:27:27 +0530156 using EthernetInterfaceIntf::dHCPEnabled;
Ratan Gupta5978dd12017-07-25 13:47:13 +0530157 using EthernetInterfaceIntf::interfaceName;
Ratan Guptabd303b12017-08-18 17:10:07 +0530158 using MacAddressIntf::mACAddress;
Ratan Gupta5978dd12017-07-25 13:47:13 +0530159
Ratan Gupta6dec390f2017-08-20 15:28:12 +0530160 /** @brief Absolute path of the resolv conf file */
161 static constexpr auto resolvConfFile = "/etc/resolv.conf";
162
Ratan Gupta3d3e4fc2017-07-25 13:38:19 +0530163 protected:
Ratan Gupta8c834932017-04-14 16:30:24 +0530164 /** @brief get the info of the ethernet interface.
165 * @return tuple having the link speed,autonegotiation,duplexmode .
166 */
Ratan Gupta8c834932017-04-14 16:30:24 +0530167 InterfaceInfo getInterfaceInfo() const;
168
Ratan Guptae9c9b812017-09-22 17:15:37 +0530169 /* @brief delete the vlan interface from system.
170 * @param[in] interface - vlan Interface.
171 */
172 void deleteVLANFromSystem(const std::string& interface);
173
Ratan Gupta8c834932017-04-14 16:30:24 +0530174 /** @brief get the mac address of the interface.
Ratan Guptada7d3af2017-08-13 17:49:56 +0530175 * @param[in] interfaceName - Network interface name.
Ratan Gupta8c834932017-04-14 16:30:24 +0530176 * @return macaddress on success
177 */
178
Ratan Guptada7d3af2017-08-13 17:49:56 +0530179 std::string getMACAddress(const std::string& interfaceName) const;
Ratan Gupta8c834932017-04-14 16:30:24 +0530180
Ratan Gupta82549cc2017-04-21 08:45:23 +0530181 /** @brief construct the ip address dbus object path.
182 * @param[in] addressType - Type of ip address.
Ratan Gupta65e5abe2017-05-23 13:20:44 +0530183 * @param[in] ipaddress - IP address.
184 * @param[in] prefixLength - Length of prefix.
Gunnar Mills6af61442018-04-08 14:50:06 -0500185 * @param[in] gateway - Gateway address.
Ratan Gupta65e5abe2017-05-23 13:20:44 +0530186
Ratan Gupta82549cc2017-04-21 08:45:23 +0530187 * @return path of the address object.
188 */
189
Ratan Gupta65e5abe2017-05-23 13:20:44 +0530190 std::string generateObjectPath(IP::Protocol addressType,
191 const std::string& ipaddress,
192 uint8_t prefixLength,
193 const std::string& gateway) const;
Ratan Gupta82549cc2017-04-21 08:45:23 +0530194
Ratan Gupta65e5abe2017-05-23 13:20:44 +0530195 /** @brief generates the id by doing hash of ipaddress,
196 * prefixlength and the gateway.
197 * @param[in] ipaddress - IP address.
198 * @param[in] prefixLength - Length of prefix.
Gunnar Mills6af61442018-04-08 14:50:06 -0500199 * @param[in] gateway - Gateway address.
Ratan Gupta65e5abe2017-05-23 13:20:44 +0530200 * @return hash string.
Ratan Gupta82549cc2017-04-21 08:45:23 +0530201 */
202
Ratan Gupta65e5abe2017-05-23 13:20:44 +0530203 static std::string generateId(const std::string& ipaddress,
204 uint8_t prefixLength,
205 const std::string& gateway);
Ratan Gupta82549cc2017-04-21 08:45:23 +0530206
Ratan Gupta2b106532017-07-25 16:05:02 +0530207 /** @brief write the dhcp section **/
208 void writeDHCPSection(std::fstream& stream);;
209
Ratan Gupta497c0c92017-08-22 19:15:59 +0530210 /** @brief get the NTP server list from the network conf
211 *
212 */
213 ServerList getNTPServersFromConf();
214
Ratan Gupta6dec390f2017-08-20 15:28:12 +0530215 /** @brief write the DNS entries to resolver file.
216 * @param[in] dnsList - DNS server list which needs to be written.
217 * @param[in] file - File to write the name server entries to.
218 */
219 void writeDNSEntries(const ServerList& dnsList,
220 const std::string& file);
221
222 /** @brief get the name server details from the network conf
223 *
224 */
225 ServerList getNameServerFromConf();
226
Ratan Gupta82549cc2017-04-21 08:45:23 +0530227 /** @brief Persistent sdbusplus DBus bus connection. */
228 sdbusplus::bus::bus& bus;
229
Ratan Gupta4f1c18b2017-05-25 12:59:35 +0530230 /** @brief Network Manager object. */
231 Manager& manager;
232
Ratan Gupta82549cc2017-04-21 08:45:23 +0530233 /** @brief Persistent map of IPAddress dbus objects and their names */
Ratan Gupta29b0e432017-05-25 12:51:40 +0530234 AddressMap addrs;
Ratan Gupta82549cc2017-04-21 08:45:23 +0530235
Ratan Gupta5978dd12017-07-25 13:47:13 +0530236 /** @brief Persistent map of VLAN interface dbus objects and their names */
237 VlanInterfaceMap vlanInterfaces;
238
Ratan Gupta47722dc2017-05-26 18:32:23 +0530239 /** @brief Dbus object path */
240 std::string objPath;
241
242 friend class TestEthernetInterface;
Ratan Gupta8c834932017-04-14 16:30:24 +0530243};
244
245} // namespace network
246} // namespace phosphor