blob: c65726a5c36823aa2d6d6d291f1493d47611a2fd [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
Gunnar Mills57d9c502018-09-14 14:42:34 -050020using Ifaces = sdbusplus::server::object::object<
21 sdbusplus::xyz::openbmc_project::Network::server::EthernetInterface,
22 sdbusplus::xyz::openbmc_project::Network::server::MACAddress,
23 sdbusplus::xyz::openbmc_project::Network::IP::server::Create,
24 sdbusplus::xyz::openbmc_project::Collection::server::DeleteAll>;
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 Gupta497c0c92017-08-22 19:15:59 +053033using ServerList = std::vector<std::string>;
raviteja-bce379562019-03-28 05:59:36 -050034using ObjectPath = sdbusplus::message::object_path;
Ratan Gupta497c0c92017-08-22 19:15:59 +053035
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>>;
Gunnar Mills57d9c502018-09-14 14:42:34 -050053using VlanInterfaceMap =
54 std::map<InterfaceName, std::unique_ptr<VlanInterface>>;
Ratan Gupta8c834932017-04-14 16:30:24 +053055
56/** @class EthernetInterface
57 * @brief OpenBMC Ethernet Interface implementation.
58 * @details A concrete implementation for the
59 * xyz.openbmc_project.Network.EthernetInterface DBus API.
60 */
Ratan Gupta82549cc2017-04-21 08:45:23 +053061class EthernetInterface : public Ifaces
Ratan Gupta8c834932017-04-14 16:30:24 +053062{
Gunnar Mills57d9c502018-09-14 14:42:34 -050063 public:
64 EthernetInterface() = delete;
65 EthernetInterface(const EthernetInterface&) = delete;
66 EthernetInterface& operator=(const EthernetInterface&) = delete;
67 EthernetInterface(EthernetInterface&&) = delete;
68 EthernetInterface& operator=(EthernetInterface&&) = delete;
69 virtual ~EthernetInterface() = default;
Ratan Gupta8c834932017-04-14 16:30:24 +053070
Gunnar Mills57d9c502018-09-14 14:42:34 -050071 /** @brief Constructor to put object onto bus at a dbus path.
72 * @param[in] bus - Bus to attach to.
73 * @param[in] objPath - Path to attach at.
74 * @param[in] dhcpEnabled - is dhcp enabled(true/false).
75 * @param[in] parent - parent object.
76 * @param[in] emitSignal - true if the object added signal needs to be
77 * send.
78 */
79 EthernetInterface(sdbusplus::bus::bus& bus, const std::string& objPath,
80 bool dhcpEnabled, Manager& parent,
81 bool emitSignal = true);
Ratan Gupta82549cc2017-04-21 08:45:23 +053082
Gunnar Mills57d9c502018-09-14 14:42:34 -050083 /** @brief Function to create ipaddress dbus object.
84 * @param[in] addressType - Type of ip address.
85 * @param[in] ipaddress- IP address.
86 * @param[in] prefixLength - Length of prefix.
87 * @param[in] gateway - Gateway ip address.
88 */
Ratan Gupta82549cc2017-04-21 08:45:23 +053089
raviteja-bce379562019-03-28 05:59:36 -050090 ObjectPath iP(IP::Protocol addressType, std::string ipaddress,
91 uint8_t prefixLength, std::string gateway) override;
Ratan Gupta8c834932017-04-14 16:30:24 +053092
Gunnar Mills57d9c502018-09-14 14:42:34 -050093 /* @brief delete the dbus object of the given ipaddress.
94 * @param[in] ipaddress - IP address.
95 */
96 void deleteObject(const std::string& ipaddress);
Ratan Gupta8c834932017-04-14 16:30:24 +053097
Gunnar Mills57d9c502018-09-14 14:42:34 -050098 /* @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);
Ratan Guptabc886292017-07-25 18:29:57 +0530103
Gunnar Mills57d9c502018-09-14 14:42:34 -0500104 /* @brief creates the dbus object(IPaddres) given in the address list.
105 * @param[in] addrs - address list for which dbus objects needs
106 * to create.
107 */
108 void createIPAddressObjects();
Ratan Gupta29b0e432017-05-25 12:51:40 +0530109
Gunnar Mills57d9c502018-09-14 14:42:34 -0500110 /* @brief Gets all the ip addresses.
111 * @returns the list of ipaddress.
112 */
113 const AddressMap& getAddresses() const
114 {
115 return addrs;
116 }
Ratan Gupta8c834932017-04-14 16:30:24 +0530117
Gunnar Mills57d9c502018-09-14 14:42:34 -0500118 /** Set value of DHCPEnabled */
119 bool dHCPEnabled(bool value) override;
Ratan Gupta87c13982017-06-15 09:27:27 +0530120
Gunnar Mills57d9c502018-09-14 14:42:34 -0500121 /** @brief sets the MAC address.
122 * @param[in] value - MAC address which needs to be set on the system.
123 * @returns macAddress of the interface or throws an error.
124 */
125 std::string mACAddress(std::string value) override;
Ratan Guptabd303b12017-08-18 17:10:07 +0530126
Gunnar Mills57d9c502018-09-14 14:42:34 -0500127 /** @brief sets the NTP servers.
128 * @param[in] value - vector of NTP servers.
129 */
130 ServerList nTPServers(ServerList value) override;
Ratan Gupta497c0c92017-08-22 19:15:59 +0530131
Gunnar Mills57d9c502018-09-14 14:42:34 -0500132 /** @brief sets the DNS/nameservers.
133 * @param[in] value - vector of DNS servers.
134 */
135 ServerList nameservers(ServerList value) override;
Ratan Gupta6dec3902017-08-20 15:28:12 +0530136
Gunnar Mills57d9c502018-09-14 14:42:34 -0500137 /** @brief create Vlan interface.
138 * @param[in] id- VLAN identifier.
139 */
William A. Kennington IIIf4b4ff82019-04-09 19:06:52 -0700140 ObjectPath createVLAN(VlanId id);
Ratan Gupta5978dd12017-07-25 13:47:13 +0530141
Gunnar Mills57d9c502018-09-14 14:42:34 -0500142 /** @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);
Ratan Gupta92bc2fe2017-07-26 22:40:21 +0530147
Gunnar Mills57d9c502018-09-14 14:42:34 -0500148 /** @brief write the network conf file with the in-memory objects.
149 */
150 void writeConfigurationFile();
Ratan Gupta2b106532017-07-25 16:05:02 +0530151
Gunnar Mills57d9c502018-09-14 14:42:34 -0500152 /** @brief delete all dbus objects.
153 */
154 void deleteAll();
Ratan Guptae9c9b812017-09-22 17:15:37 +0530155
Gunnar Mills57d9c502018-09-14 14:42:34 -0500156 using EthernetInterfaceIntf::dHCPEnabled;
157 using EthernetInterfaceIntf::interfaceName;
158 using MacAddressIntf::mACAddress;
Ratan Gupta5978dd12017-07-25 13:47:13 +0530159
Gunnar Mills57d9c502018-09-14 14:42:34 -0500160 /** @brief Absolute path of the resolv conf file */
161 static constexpr auto resolvConfFile = "/etc/resolv.conf";
Ratan Gupta6dec3902017-08-20 15:28:12 +0530162
Gunnar Mills57d9c502018-09-14 14:42:34 -0500163 protected:
164 /** @brief get the info of the ethernet interface.
165 * @return tuple having the link speed,autonegotiation,duplexmode .
166 */
167 InterfaceInfo getInterfaceInfo() const;
Ratan Gupta8c834932017-04-14 16:30:24 +0530168
Gunnar Mills57d9c502018-09-14 14:42:34 -0500169 /* @brief delete the vlan interface from system.
170 * @param[in] interface - vlan Interface.
171 */
172 void deleteVLANFromSystem(const std::string& interface);
Ratan Guptae9c9b812017-09-22 17:15:37 +0530173
Gunnar Mills57d9c502018-09-14 14:42:34 -0500174 /** @brief get the mac address of the interface.
175 * @param[in] interfaceName - Network interface name.
176 * @return macaddress on success
177 */
Ratan Gupta8c834932017-04-14 16:30:24 +0530178
Gunnar Mills57d9c502018-09-14 14:42:34 -0500179 std::string getMACAddress(const std::string& interfaceName) const;
Ratan Gupta8c834932017-04-14 16:30:24 +0530180
Gunnar Mills57d9c502018-09-14 14:42:34 -0500181 /** @brief construct the ip address dbus object path.
182 * @param[in] addressType - Type of ip address.
183 * @param[in] ipaddress - IP address.
184 * @param[in] prefixLength - Length of prefix.
185 * @param[in] gateway - Gateway address.
Ratan Gupta65e5abe2017-05-23 13:20:44 +0530186
Gunnar Mills57d9c502018-09-14 14:42:34 -0500187 * @return path of the address object.
188 */
Ratan Gupta82549cc2017-04-21 08:45:23 +0530189
Gunnar Mills57d9c502018-09-14 14:42:34 -0500190 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
Gunnar Mills57d9c502018-09-14 14:42:34 -0500195 /** @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.
199 * @param[in] gateway - Gateway address.
200 * @return hash string.
201 */
Ratan Gupta82549cc2017-04-21 08:45:23 +0530202
Gunnar Mills57d9c502018-09-14 14:42:34 -0500203 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
Gunnar Mills57d9c502018-09-14 14:42:34 -0500207 /** @brief write the dhcp section **/
208 void writeDHCPSection(std::fstream& stream);
Ratan Gupta2b106532017-07-25 16:05:02 +0530209
Gunnar Mills57d9c502018-09-14 14:42:34 -0500210 /** @brief get the NTP server list from the network conf
211 *
212 */
213 ServerList getNTPServersFromConf();
Ratan Gupta497c0c92017-08-22 19:15:59 +0530214
Gunnar Mills57d9c502018-09-14 14:42:34 -0500215 /** @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, const std::string& file);
Ratan Gupta6dec3902017-08-20 15:28:12 +0530220
Gunnar Mills57d9c502018-09-14 14:42:34 -0500221 /** @brief get the name server details from the network conf
222 *
223 */
224 ServerList getNameServerFromConf();
Ratan Gupta6dec3902017-08-20 15:28:12 +0530225
Gunnar Mills57d9c502018-09-14 14:42:34 -0500226 /** @brief Persistent sdbusplus DBus bus connection. */
227 sdbusplus::bus::bus& bus;
Ratan Gupta82549cc2017-04-21 08:45:23 +0530228
Gunnar Mills57d9c502018-09-14 14:42:34 -0500229 /** @brief Network Manager object. */
230 Manager& manager;
Ratan Gupta4f1c18b2017-05-25 12:59:35 +0530231
Gunnar Mills57d9c502018-09-14 14:42:34 -0500232 /** @brief Persistent map of IPAddress dbus objects and their names */
233 AddressMap addrs;
Ratan Gupta82549cc2017-04-21 08:45:23 +0530234
Gunnar Mills57d9c502018-09-14 14:42:34 -0500235 /** @brief Persistent map of VLAN interface dbus objects and their names */
236 VlanInterfaceMap vlanInterfaces;
Ratan Gupta5978dd12017-07-25 13:47:13 +0530237
Gunnar Mills57d9c502018-09-14 14:42:34 -0500238 /** @brief Dbus object path */
239 std::string objPath;
Ratan Gupta47722dc2017-05-26 18:32:23 +0530240
Gunnar Mills57d9c502018-09-14 14:42:34 -0500241 friend class TestEthernetInterface;
Ratan Gupta8c834932017-04-14 16:30:24 +0530242};
243
244} // namespace network
245} // namespace phosphor