blob: 3935390bbd139508d93339c12e7788541e8f1c3e [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"
William A. Kennington III08505792019-01-30 16:00:04 -08006#include "xyz/openbmc_project/Network/Neighbor/CreateStatic/server.hpp"
Ratan Gupta8c834932017-04-14 16:30:24 +05307
Patrick Venture189d44e2018-07-09 12:30:59 -07008#include <experimental/filesystem>
Ratan Gupta8c834932017-04-14 16:30:24 +05309#include <sdbusplus/bus.hpp>
10#include <sdbusplus/server/object.hpp>
Ratan Gupta8c834932017-04-14 16:30:24 +053011#include <string>
Patrick Venture189d44e2018-07-09 12:30:59 -070012#include <xyz/openbmc_project/Collection/DeleteAll/server.hpp>
13#include <xyz/openbmc_project/Network/EthernetInterface/server.hpp>
14#include <xyz/openbmc_project/Network/MACAddress/server.hpp>
Ratan Gupta8c834932017-04-14 16:30:24 +053015
16namespace phosphor
17{
18namespace network
19{
Ratan Gupta8c834932017-04-14 16:30:24 +053020
Gunnar Mills57d9c502018-09-14 14:42:34 -050021using Ifaces = sdbusplus::server::object::object<
22 sdbusplus::xyz::openbmc_project::Network::server::EthernetInterface,
23 sdbusplus::xyz::openbmc_project::Network::server::MACAddress,
24 sdbusplus::xyz::openbmc_project::Network::IP::server::Create,
William A. Kennington III08505792019-01-30 16:00:04 -080025 sdbusplus::xyz::openbmc_project::Network::Neighbor::server::CreateStatic,
Gunnar Mills57d9c502018-09-14 14:42:34 -050026 sdbusplus::xyz::openbmc_project::Collection::server::DeleteAll>;
Ratan Gupta8c834932017-04-14 16:30:24 +053027
Ratan Gupta82549cc2017-04-21 08:45:23 +053028using IP = sdbusplus::xyz::openbmc_project::Network::server::IP;
Ratan Gupta87c13982017-06-15 09:27:27 +053029
30using EthernetInterfaceIntf =
31 sdbusplus::xyz::openbmc_project::Network::server::EthernetInterface;
Ratan Guptabd303b12017-08-18 17:10:07 +053032using MacAddressIntf =
33 sdbusplus::xyz::openbmc_project::Network::server::MACAddress;
Ratan Gupta87c13982017-06-15 09:27:27 +053034
Ratan Gupta497c0c92017-08-22 19:15:59 +053035using ServerList = std::vector<std::string>;
raviteja-bce379562019-03-28 05:59:36 -050036using ObjectPath = sdbusplus::message::object_path;
Ratan Gupta497c0c92017-08-22 19:15:59 +053037
Ratan Gupta5978dd12017-07-25 13:47:13 +053038namespace fs = std::experimental::filesystem;
39
Ratan Gupta4f1c18b2017-05-25 12:59:35 +053040class Manager; // forward declaration of network manager.
Ratan Gupta8c834932017-04-14 16:30:24 +053041
Ratan Gupta47722dc2017-05-26 18:32:23 +053042class TestEthernetInterface;
43
Ratan Gupta5978dd12017-07-25 13:47:13 +053044class VlanInterface;
45
46class IPAddress;
47
William A. Kennington III08505792019-01-30 16:00:04 -080048class Neighbor;
49
Ratan Gupta8c834932017-04-14 16:30:24 +053050using LinkSpeed = uint16_t;
51using DuplexMode = uint8_t;
52using Autoneg = uint8_t;
Ratan Gupta5978dd12017-07-25 13:47:13 +053053using VlanId = uint32_t;
54using InterfaceName = std::string;
Ratan Gupta8c834932017-04-14 16:30:24 +053055using InterfaceInfo = std::tuple<LinkSpeed, DuplexMode, Autoneg>;
Ratan Gupta29b0e432017-05-25 12:51:40 +053056using AddressMap = std::map<std::string, std::shared_ptr<IPAddress>>;
William A. Kennington III08505792019-01-30 16:00:04 -080057using NeighborMap = std::map<std::string, std::shared_ptr<Neighbor>>;
Gunnar Mills57d9c502018-09-14 14:42:34 -050058using VlanInterfaceMap =
59 std::map<InterfaceName, std::unique_ptr<VlanInterface>>;
Ratan Gupta8c834932017-04-14 16:30:24 +053060
61/** @class EthernetInterface
62 * @brief OpenBMC Ethernet Interface implementation.
63 * @details A concrete implementation for the
64 * xyz.openbmc_project.Network.EthernetInterface DBus API.
65 */
Ratan Gupta82549cc2017-04-21 08:45:23 +053066class EthernetInterface : public Ifaces
Ratan Gupta8c834932017-04-14 16:30:24 +053067{
Gunnar Mills57d9c502018-09-14 14:42:34 -050068 public:
69 EthernetInterface() = delete;
70 EthernetInterface(const EthernetInterface&) = delete;
71 EthernetInterface& operator=(const EthernetInterface&) = delete;
72 EthernetInterface(EthernetInterface&&) = delete;
73 EthernetInterface& operator=(EthernetInterface&&) = delete;
74 virtual ~EthernetInterface() = default;
Ratan Gupta8c834932017-04-14 16:30:24 +053075
Gunnar Mills57d9c502018-09-14 14:42:34 -050076 /** @brief Constructor to put object onto bus at a dbus path.
77 * @param[in] bus - Bus to attach to.
78 * @param[in] objPath - Path to attach at.
79 * @param[in] dhcpEnabled - is dhcp enabled(true/false).
80 * @param[in] parent - parent object.
81 * @param[in] emitSignal - true if the object added signal needs to be
82 * send.
83 */
84 EthernetInterface(sdbusplus::bus::bus& bus, const std::string& objPath,
85 bool dhcpEnabled, Manager& parent,
86 bool emitSignal = true);
Ratan Gupta82549cc2017-04-21 08:45:23 +053087
Gunnar Mills57d9c502018-09-14 14:42:34 -050088 /** @brief Function to create ipaddress dbus object.
89 * @param[in] addressType - Type of ip address.
90 * @param[in] ipaddress- IP address.
91 * @param[in] prefixLength - Length of prefix.
92 * @param[in] gateway - Gateway ip address.
93 */
Ratan Gupta82549cc2017-04-21 08:45:23 +053094
raviteja-bce379562019-03-28 05:59:36 -050095 ObjectPath iP(IP::Protocol addressType, std::string ipaddress,
96 uint8_t prefixLength, std::string gateway) override;
Ratan Gupta8c834932017-04-14 16:30:24 +053097
William A. Kennington III08505792019-01-30 16:00:04 -080098 /** @brief Function to create static neighbor dbus object.
99 * @param[in] ipAddress - IP address.
100 * @param[in] macAddress - Low level MAC address.
101 */
102 ObjectPath neighbor(std::string iPAddress, std::string mACAddress) override;
103
Gunnar Mills57d9c502018-09-14 14:42:34 -0500104 /* @brief delete the dbus object of the given ipaddress.
105 * @param[in] ipaddress - IP address.
106 */
107 void deleteObject(const std::string& ipaddress);
Ratan Gupta8c834932017-04-14 16:30:24 +0530108
William A. Kennington III08505792019-01-30 16:00:04 -0800109 /* @brief delete the dbus object of the given ipaddress.
110 * @param[in] ipaddress - IP address.
111 */
112 void deleteStaticNeighborObject(const std::string& ipAddress);
113
Gunnar Mills57d9c502018-09-14 14:42:34 -0500114 /* @brief delete the vlan dbus object of the given interface.
115 * Also deletes the device file and the network file.
116 * @param[in] interface - VLAN Interface.
117 */
118 void deleteVLANObject(const std::string& interface);
Ratan Guptabc886292017-07-25 18:29:57 +0530119
Gunnar Mills57d9c502018-09-14 14:42:34 -0500120 /* @brief creates the dbus object(IPaddres) given in the address list.
121 * @param[in] addrs - address list for which dbus objects needs
122 * to create.
123 */
124 void createIPAddressObjects();
Ratan Gupta29b0e432017-05-25 12:51:40 +0530125
William A. Kennington III08505792019-01-30 16:00:04 -0800126 /* @brief creates the dbus object(Neighbor) given in the neighbor list.
127 */
128 void createStaticNeighborObjects();
129
William A. Kennington IIId7946a72019-04-19 14:24:09 -0700130 /* @brief Gets the index of the interface on the system
131 */
132 unsigned ifIndex() const;
133
Gunnar Mills57d9c502018-09-14 14:42:34 -0500134 /* @brief Gets all the ip addresses.
135 * @returns the list of ipaddress.
136 */
137 const AddressMap& getAddresses() const
138 {
139 return addrs;
140 }
Ratan Gupta8c834932017-04-14 16:30:24 +0530141
William A. Kennington III08505792019-01-30 16:00:04 -0800142 /* @brief Gets all the static neighbor entries.
143 * @returns Static neighbor map.
144 */
145 const NeighborMap& getStaticNeighbors() const
146 {
147 return staticNeighbors;
148 }
149
Gunnar Mills57d9c502018-09-14 14:42:34 -0500150 /** Set value of DHCPEnabled */
151 bool dHCPEnabled(bool value) override;
Ratan Gupta87c13982017-06-15 09:27:27 +0530152
Gunnar Mills57d9c502018-09-14 14:42:34 -0500153 /** @brief sets the MAC address.
154 * @param[in] value - MAC address which needs to be set on the system.
155 * @returns macAddress of the interface or throws an error.
156 */
157 std::string mACAddress(std::string value) override;
Ratan Guptabd303b12017-08-18 17:10:07 +0530158
Gunnar Mills57d9c502018-09-14 14:42:34 -0500159 /** @brief sets the NTP servers.
160 * @param[in] value - vector of NTP servers.
161 */
162 ServerList nTPServers(ServerList value) override;
Ratan Gupta497c0c92017-08-22 19:15:59 +0530163
Gunnar Mills57d9c502018-09-14 14:42:34 -0500164 /** @brief sets the DNS/nameservers.
165 * @param[in] value - vector of DNS servers.
166 */
167 ServerList nameservers(ServerList value) override;
Ratan Gupta6dec3902017-08-20 15:28:12 +0530168
Gunnar Mills57d9c502018-09-14 14:42:34 -0500169 /** @brief create Vlan interface.
170 * @param[in] id- VLAN identifier.
171 */
William A. Kennington IIIf4b4ff82019-04-09 19:06:52 -0700172 ObjectPath createVLAN(VlanId id);
Ratan Gupta5978dd12017-07-25 13:47:13 +0530173
Gunnar Mills57d9c502018-09-14 14:42:34 -0500174 /** @brief load the vlan info from the system
175 * and creates the ip address dbus objects.
176 * @param[in] vlanID- VLAN identifier.
177 */
178 void loadVLAN(VlanId vlanID);
Ratan Gupta92bc2fe2017-07-26 22:40:21 +0530179
Gunnar Mills57d9c502018-09-14 14:42:34 -0500180 /** @brief write the network conf file with the in-memory objects.
181 */
182 void writeConfigurationFile();
Ratan Gupta2b106532017-07-25 16:05:02 +0530183
Gunnar Mills57d9c502018-09-14 14:42:34 -0500184 /** @brief delete all dbus objects.
185 */
186 void deleteAll();
Ratan Guptae9c9b812017-09-22 17:15:37 +0530187
Gunnar Mills57d9c502018-09-14 14:42:34 -0500188 using EthernetInterfaceIntf::dHCPEnabled;
189 using EthernetInterfaceIntf::interfaceName;
190 using MacAddressIntf::mACAddress;
Ratan Gupta5978dd12017-07-25 13:47:13 +0530191
Gunnar Mills57d9c502018-09-14 14:42:34 -0500192 /** @brief Absolute path of the resolv conf file */
193 static constexpr auto resolvConfFile = "/etc/resolv.conf";
Ratan Gupta6dec3902017-08-20 15:28:12 +0530194
Gunnar Mills57d9c502018-09-14 14:42:34 -0500195 protected:
196 /** @brief get the info of the ethernet interface.
197 * @return tuple having the link speed,autonegotiation,duplexmode .
198 */
199 InterfaceInfo getInterfaceInfo() const;
Ratan Gupta8c834932017-04-14 16:30:24 +0530200
Gunnar Mills57d9c502018-09-14 14:42:34 -0500201 /* @brief delete the vlan interface from system.
202 * @param[in] interface - vlan Interface.
203 */
204 void deleteVLANFromSystem(const std::string& interface);
Ratan Guptae9c9b812017-09-22 17:15:37 +0530205
Gunnar Mills57d9c502018-09-14 14:42:34 -0500206 /** @brief get the mac address of the interface.
207 * @param[in] interfaceName - Network interface name.
208 * @return macaddress on success
209 */
Ratan Gupta8c834932017-04-14 16:30:24 +0530210
Gunnar Mills57d9c502018-09-14 14:42:34 -0500211 std::string getMACAddress(const std::string& interfaceName) const;
Ratan Gupta8c834932017-04-14 16:30:24 +0530212
Gunnar Mills57d9c502018-09-14 14:42:34 -0500213 /** @brief construct the ip address dbus object path.
214 * @param[in] addressType - Type of ip address.
215 * @param[in] ipaddress - IP address.
216 * @param[in] prefixLength - Length of prefix.
217 * @param[in] gateway - Gateway address.
Ratan Gupta65e5abe2017-05-23 13:20:44 +0530218
Gunnar Mills57d9c502018-09-14 14:42:34 -0500219 * @return path of the address object.
220 */
Ratan Gupta82549cc2017-04-21 08:45:23 +0530221
Gunnar Mills57d9c502018-09-14 14:42:34 -0500222 std::string generateObjectPath(IP::Protocol addressType,
223 const std::string& ipaddress,
224 uint8_t prefixLength,
225 const std::string& gateway) const;
Ratan Gupta82549cc2017-04-21 08:45:23 +0530226
William A. Kennington III08505792019-01-30 16:00:04 -0800227 std::string
228 generateStaticNeighborObjectPath(const std::string& iPAddress,
229 const std::string& mACAddress) const;
230
Gunnar Mills57d9c502018-09-14 14:42:34 -0500231 /** @brief generates the id by doing hash of ipaddress,
232 * prefixlength and the gateway.
233 * @param[in] ipaddress - IP address.
234 * @param[in] prefixLength - Length of prefix.
235 * @param[in] gateway - Gateway address.
236 * @return hash string.
237 */
Ratan Gupta82549cc2017-04-21 08:45:23 +0530238
Gunnar Mills57d9c502018-09-14 14:42:34 -0500239 static std::string generateId(const std::string& ipaddress,
240 uint8_t prefixLength,
241 const std::string& gateway);
Ratan Gupta82549cc2017-04-21 08:45:23 +0530242
William A. Kennington III08505792019-01-30 16:00:04 -0800243 /** @brief generates the id by doing hash of ipaddress
244 * and the mac address.
245 * @param[in] iPAddress - IP address.
246 * @param[in] mACAddress - Gateway address.
247 * @return hash string.
248 */
249 static std::string generateNeighborId(const std::string& iPAddress,
250 const std::string& mACAddress);
251
Gunnar Mills57d9c502018-09-14 14:42:34 -0500252 /** @brief write the dhcp section **/
253 void writeDHCPSection(std::fstream& stream);
Ratan Gupta2b106532017-07-25 16:05:02 +0530254
Gunnar Mills57d9c502018-09-14 14:42:34 -0500255 /** @brief get the NTP server list from the network conf
256 *
257 */
258 ServerList getNTPServersFromConf();
Ratan Gupta497c0c92017-08-22 19:15:59 +0530259
Gunnar Mills57d9c502018-09-14 14:42:34 -0500260 /** @brief write the DNS entries to resolver file.
261 * @param[in] dnsList - DNS server list which needs to be written.
262 * @param[in] file - File to write the name server entries to.
263 */
264 void writeDNSEntries(const ServerList& dnsList, const std::string& file);
Ratan Gupta6dec3902017-08-20 15:28:12 +0530265
Gunnar Mills57d9c502018-09-14 14:42:34 -0500266 /** @brief get the name server details from the network conf
267 *
268 */
269 ServerList getNameServerFromConf();
Ratan Gupta6dec3902017-08-20 15:28:12 +0530270
Gunnar Mills57d9c502018-09-14 14:42:34 -0500271 /** @brief Persistent sdbusplus DBus bus connection. */
272 sdbusplus::bus::bus& bus;
Ratan Gupta82549cc2017-04-21 08:45:23 +0530273
Gunnar Mills57d9c502018-09-14 14:42:34 -0500274 /** @brief Network Manager object. */
275 Manager& manager;
Ratan Gupta4f1c18b2017-05-25 12:59:35 +0530276
Gunnar Mills57d9c502018-09-14 14:42:34 -0500277 /** @brief Persistent map of IPAddress dbus objects and their names */
278 AddressMap addrs;
Ratan Gupta82549cc2017-04-21 08:45:23 +0530279
William A. Kennington III08505792019-01-30 16:00:04 -0800280 /** @brief Persistent map of Neighbor dbus objects and their names */
281 NeighborMap staticNeighbors;
282
Gunnar Mills57d9c502018-09-14 14:42:34 -0500283 /** @brief Persistent map of VLAN interface dbus objects and their names */
284 VlanInterfaceMap vlanInterfaces;
Ratan Gupta5978dd12017-07-25 13:47:13 +0530285
Gunnar Mills57d9c502018-09-14 14:42:34 -0500286 /** @brief Dbus object path */
287 std::string objPath;
Ratan Gupta47722dc2017-05-26 18:32:23 +0530288
Gunnar Mills57d9c502018-09-14 14:42:34 -0500289 friend class TestEthernetInterface;
Ratan Gupta8c834932017-04-14 16:30:24 +0530290};
291
292} // namespace network
293} // namespace phosphor