blob: bfe1d5410c213feae7d355fbe1e97c1b771574bd [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>;
34
Ratan Gupta5978dd12017-07-25 13:47:13 +053035namespace fs = std::experimental::filesystem;
36
Ratan Gupta4f1c18b2017-05-25 12:59:35 +053037class Manager; // forward declaration of network manager.
Ratan Gupta8c834932017-04-14 16:30:24 +053038
Ratan Gupta47722dc2017-05-26 18:32:23 +053039class TestEthernetInterface;
40
Ratan Gupta5978dd12017-07-25 13:47:13 +053041class VlanInterface;
42
43class IPAddress;
44
Ratan Gupta8c834932017-04-14 16:30:24 +053045using LinkSpeed = uint16_t;
46using DuplexMode = uint8_t;
47using Autoneg = uint8_t;
Ratan Gupta5978dd12017-07-25 13:47:13 +053048using VlanId = uint32_t;
49using InterfaceName = std::string;
Ratan Gupta8c834932017-04-14 16:30:24 +053050using InterfaceInfo = std::tuple<LinkSpeed, DuplexMode, Autoneg>;
Ratan Gupta29b0e432017-05-25 12:51:40 +053051using AddressMap = std::map<std::string, std::shared_ptr<IPAddress>>;
Gunnar Mills57d9c502018-09-14 14:42:34 -050052using VlanInterfaceMap =
53 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{
Gunnar Mills57d9c502018-09-14 14:42:34 -050062 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;
Ratan Gupta8c834932017-04-14 16:30:24 +053069
Gunnar Mills57d9c502018-09-14 14:42:34 -050070 /** @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.
73 * @param[in] dhcpEnabled - is dhcp enabled(true/false).
74 * @param[in] parent - parent object.
75 * @param[in] emitSignal - true if the object added signal needs to be
76 * send.
77 */
78 EthernetInterface(sdbusplus::bus::bus& bus, const std::string& objPath,
79 bool dhcpEnabled, Manager& parent,
80 bool emitSignal = true);
Ratan Gupta82549cc2017-04-21 08:45:23 +053081
Gunnar Mills57d9c502018-09-14 14:42:34 -050082 /** @brief Function to create ipaddress dbus object.
83 * @param[in] addressType - Type of ip address.
84 * @param[in] ipaddress- IP address.
85 * @param[in] prefixLength - Length of prefix.
86 * @param[in] gateway - Gateway ip address.
87 */
Ratan Gupta82549cc2017-04-21 08:45:23 +053088
Gunnar Mills57d9c502018-09-14 14:42:34 -050089 void iP(IP::Protocol addressType, std::string ipaddress,
90 uint8_t prefixLength, std::string gateway) override;
Ratan Gupta8c834932017-04-14 16:30:24 +053091
Gunnar Mills57d9c502018-09-14 14:42:34 -050092 /* @brief delete the dbus object of the given ipaddress.
93 * @param[in] ipaddress - IP address.
94 */
95 void deleteObject(const std::string& ipaddress);
Ratan Gupta8c834932017-04-14 16:30:24 +053096
Gunnar Mills57d9c502018-09-14 14:42:34 -050097 /* @brief delete the vlan dbus object of the given interface.
98 * Also deletes the device file and the network file.
99 * @param[in] interface - VLAN Interface.
100 */
101 void deleteVLANObject(const std::string& interface);
Ratan Guptabc886292017-07-25 18:29:57 +0530102
Gunnar Mills57d9c502018-09-14 14:42:34 -0500103 /* @brief creates the dbus object(IPaddres) given in the address list.
104 * @param[in] addrs - address list for which dbus objects needs
105 * to create.
106 */
107 void createIPAddressObjects();
Ratan Gupta29b0e432017-05-25 12:51:40 +0530108
Gunnar Mills57d9c502018-09-14 14:42:34 -0500109 /* @brief Gets all the ip addresses.
110 * @returns the list of ipaddress.
111 */
112 const AddressMap& getAddresses() const
113 {
114 return addrs;
115 }
Ratan Gupta8c834932017-04-14 16:30:24 +0530116
Gunnar Mills57d9c502018-09-14 14:42:34 -0500117 /** Set value of DHCPEnabled */
118 bool dHCPEnabled(bool value) override;
Ratan Gupta87c13982017-06-15 09:27:27 +0530119
Gunnar Mills57d9c502018-09-14 14:42:34 -0500120 /** @brief sets the MAC address.
121 * @param[in] value - MAC address which needs to be set on the system.
122 * @returns macAddress of the interface or throws an error.
123 */
124 std::string mACAddress(std::string value) override;
Ratan Guptabd303b12017-08-18 17:10:07 +0530125
Gunnar Mills57d9c502018-09-14 14:42:34 -0500126 /** @brief sets the NTP servers.
127 * @param[in] value - vector of NTP servers.
128 */
129 ServerList nTPServers(ServerList value) override;
Ratan Gupta497c0c92017-08-22 19:15:59 +0530130
Gunnar Mills57d9c502018-09-14 14:42:34 -0500131 /** @brief sets the DNS/nameservers.
132 * @param[in] value - vector of DNS servers.
133 */
134 ServerList nameservers(ServerList value) override;
Ratan Gupta6dec390f2017-08-20 15:28:12 +0530135
Gunnar Mills57d9c502018-09-14 14:42:34 -0500136 /** @brief create Vlan interface.
137 * @param[in] id- VLAN identifier.
138 */
139 void createVLAN(VlanId id);
Ratan Gupta5978dd12017-07-25 13:47:13 +0530140
Gunnar Mills57d9c502018-09-14 14:42:34 -0500141 /** @brief load the vlan info from the system
142 * and creates the ip address dbus objects.
143 * @param[in] vlanID- VLAN identifier.
144 */
145 void loadVLAN(VlanId vlanID);
Ratan Gupta92bc2fe2017-07-26 22:40:21 +0530146
Gunnar Mills57d9c502018-09-14 14:42:34 -0500147 /** @brief write the network conf file with the in-memory objects.
148 */
149 void writeConfigurationFile();
Ratan Gupta2b106532017-07-25 16:05:02 +0530150
Gunnar Mills57d9c502018-09-14 14:42:34 -0500151 /** @brief delete all dbus objects.
152 */
153 void deleteAll();
Ratan Guptae9c9b812017-09-22 17:15:37 +0530154
Gunnar Mills57d9c502018-09-14 14:42:34 -0500155 using EthernetInterfaceIntf::dHCPEnabled;
156 using EthernetInterfaceIntf::interfaceName;
157 using MacAddressIntf::mACAddress;
Ratan Gupta5978dd12017-07-25 13:47:13 +0530158
Gunnar Mills57d9c502018-09-14 14:42:34 -0500159 /** @brief Absolute path of the resolv conf file */
160 static constexpr auto resolvConfFile = "/etc/resolv.conf";
Ratan Gupta6dec390f2017-08-20 15:28:12 +0530161
Gunnar Mills57d9c502018-09-14 14:42:34 -0500162 protected:
163 /** @brief get the info of the ethernet interface.
164 * @return tuple having the link speed,autonegotiation,duplexmode .
165 */
166 InterfaceInfo getInterfaceInfo() const;
Ratan Gupta8c834932017-04-14 16:30:24 +0530167
Gunnar Mills57d9c502018-09-14 14:42:34 -0500168 /* @brief delete the vlan interface from system.
169 * @param[in] interface - vlan Interface.
170 */
171 void deleteVLANFromSystem(const std::string& interface);
Ratan Guptae9c9b812017-09-22 17:15:37 +0530172
Gunnar Mills57d9c502018-09-14 14:42:34 -0500173 /** @brief get the mac address of the interface.
174 * @param[in] interfaceName - Network interface name.
175 * @return macaddress on success
176 */
Ratan Gupta8c834932017-04-14 16:30:24 +0530177
Gunnar Mills57d9c502018-09-14 14:42:34 -0500178 std::string getMACAddress(const std::string& interfaceName) const;
Ratan Gupta8c834932017-04-14 16:30:24 +0530179
Gunnar Mills57d9c502018-09-14 14:42:34 -0500180 /** @brief construct the ip address dbus object path.
181 * @param[in] addressType - Type of ip address.
182 * @param[in] ipaddress - IP address.
183 * @param[in] prefixLength - Length of prefix.
184 * @param[in] gateway - Gateway address.
Ratan Gupta65e5abe2017-05-23 13:20:44 +0530185
Gunnar Mills57d9c502018-09-14 14:42:34 -0500186 * @return path of the address object.
187 */
Ratan Gupta82549cc2017-04-21 08:45:23 +0530188
Gunnar Mills57d9c502018-09-14 14:42:34 -0500189 std::string generateObjectPath(IP::Protocol addressType,
190 const std::string& ipaddress,
191 uint8_t prefixLength,
192 const std::string& gateway) const;
Ratan Gupta82549cc2017-04-21 08:45:23 +0530193
Gunnar Mills57d9c502018-09-14 14:42:34 -0500194 /** @brief generates the id by doing hash of ipaddress,
195 * prefixlength and the gateway.
196 * @param[in] ipaddress - IP address.
197 * @param[in] prefixLength - Length of prefix.
198 * @param[in] gateway - Gateway address.
199 * @return hash string.
200 */
Ratan Gupta82549cc2017-04-21 08:45:23 +0530201
Gunnar Mills57d9c502018-09-14 14:42:34 -0500202 static std::string generateId(const std::string& ipaddress,
203 uint8_t prefixLength,
204 const std::string& gateway);
Ratan Gupta82549cc2017-04-21 08:45:23 +0530205
Gunnar Mills57d9c502018-09-14 14:42:34 -0500206 /** @brief write the dhcp section **/
207 void writeDHCPSection(std::fstream& stream);
Ratan Gupta2b106532017-07-25 16:05:02 +0530208
Gunnar Mills57d9c502018-09-14 14:42:34 -0500209 /** @brief get the NTP server list from the network conf
210 *
211 */
212 ServerList getNTPServersFromConf();
Ratan Gupta497c0c92017-08-22 19:15:59 +0530213
Gunnar Mills57d9c502018-09-14 14:42:34 -0500214 /** @brief write the DNS entries to resolver file.
215 * @param[in] dnsList - DNS server list which needs to be written.
216 * @param[in] file - File to write the name server entries to.
217 */
218 void writeDNSEntries(const ServerList& dnsList, const std::string& file);
Ratan Gupta6dec390f2017-08-20 15:28:12 +0530219
Gunnar Mills57d9c502018-09-14 14:42:34 -0500220 /** @brief get the name server details from the network conf
221 *
222 */
223 ServerList getNameServerFromConf();
Ratan Gupta6dec390f2017-08-20 15:28:12 +0530224
Gunnar Mills57d9c502018-09-14 14:42:34 -0500225 /** @brief Persistent sdbusplus DBus bus connection. */
226 sdbusplus::bus::bus& bus;
Ratan Gupta82549cc2017-04-21 08:45:23 +0530227
Gunnar Mills57d9c502018-09-14 14:42:34 -0500228 /** @brief Network Manager object. */
229 Manager& manager;
Ratan Gupta4f1c18b2017-05-25 12:59:35 +0530230
Gunnar Mills57d9c502018-09-14 14:42:34 -0500231 /** @brief Persistent map of IPAddress dbus objects and their names */
232 AddressMap addrs;
Ratan Gupta82549cc2017-04-21 08:45:23 +0530233
Gunnar Mills57d9c502018-09-14 14:42:34 -0500234 /** @brief Persistent map of VLAN interface dbus objects and their names */
235 VlanInterfaceMap vlanInterfaces;
Ratan Gupta5978dd12017-07-25 13:47:13 +0530236
Gunnar Mills57d9c502018-09-14 14:42:34 -0500237 /** @brief Dbus object path */
238 std::string objPath;
Ratan Gupta47722dc2017-05-26 18:32:23 +0530239
Gunnar Mills57d9c502018-09-14 14:42:34 -0500240 friend class TestEthernetInterface;
Ratan Gupta8c834932017-04-14 16:30:24 +0530241};
242
243} // namespace network
244} // namespace phosphor