blob: 7192e265523726abf658d91959985080608900ef [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
Manojkiran Edaa879baa2020-06-13 14:39:08 +05308#include <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
Manojkiran Edaa879baa2020-06-13 14:39:08 +053038namespace fs = std::filesystem;
Ratan Gupta5978dd12017-07-25 13:47:13 +053039
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;
Johnathan Manteyfaa72e52020-01-08 10:38:58 -080053using LinkUp = bool;
Johnathan Manteyd0679f92019-10-29 16:20:28 -070054using NICEnabled = bool;
Ratan Gupta5978dd12017-07-25 13:47:13 +053055using VlanId = uint32_t;
56using InterfaceName = std::string;
Johnathan Manteyd0679f92019-10-29 16:20:28 -070057using InterfaceInfo =
58 std::tuple<LinkSpeed, DuplexMode, Autoneg, LinkUp, NICEnabled>;
Ratan Gupta29b0e432017-05-25 12:51:40 +053059using AddressMap = std::map<std::string, std::shared_ptr<IPAddress>>;
William A. Kennington III08505792019-01-30 16:00:04 -080060using NeighborMap = std::map<std::string, std::shared_ptr<Neighbor>>;
Gunnar Mills57d9c502018-09-14 14:42:34 -050061using VlanInterfaceMap =
62 std::map<InterfaceName, std::unique_ptr<VlanInterface>>;
Ratan Gupta8c834932017-04-14 16:30:24 +053063
64/** @class EthernetInterface
65 * @brief OpenBMC Ethernet Interface implementation.
66 * @details A concrete implementation for the
67 * xyz.openbmc_project.Network.EthernetInterface DBus API.
68 */
Ratan Gupta82549cc2017-04-21 08:45:23 +053069class EthernetInterface : public Ifaces
Ratan Gupta8c834932017-04-14 16:30:24 +053070{
Gunnar Mills57d9c502018-09-14 14:42:34 -050071 public:
72 EthernetInterface() = delete;
73 EthernetInterface(const EthernetInterface&) = delete;
74 EthernetInterface& operator=(const EthernetInterface&) = delete;
75 EthernetInterface(EthernetInterface&&) = delete;
76 EthernetInterface& operator=(EthernetInterface&&) = delete;
77 virtual ~EthernetInterface() = default;
Ratan Gupta8c834932017-04-14 16:30:24 +053078
Gunnar Mills57d9c502018-09-14 14:42:34 -050079 /** @brief Constructor to put object onto bus at a dbus path.
80 * @param[in] bus - Bus to attach to.
81 * @param[in] objPath - Path to attach at.
82 * @param[in] dhcpEnabled - is dhcp enabled(true/false).
83 * @param[in] parent - parent object.
84 * @param[in] emitSignal - true if the object added signal needs to be
85 * send.
86 */
87 EthernetInterface(sdbusplus::bus::bus& bus, const std::string& objPath,
88 bool dhcpEnabled, Manager& parent,
89 bool emitSignal = true);
Ratan Gupta82549cc2017-04-21 08:45:23 +053090
Manojkiran Edaacd6dd52019-10-15 15:00:51 +053091 /** @brief Function used to load the nameservers.
92 */
93 virtual void loadNameServers();
94
Gunnar Mills57d9c502018-09-14 14:42:34 -050095 /** @brief Function to create ipaddress dbus object.
96 * @param[in] addressType - Type of ip address.
97 * @param[in] ipaddress- IP address.
98 * @param[in] prefixLength - Length of prefix.
99 * @param[in] gateway - Gateway ip address.
100 */
Ratan Gupta82549cc2017-04-21 08:45:23 +0530101
raviteja-bce379562019-03-28 05:59:36 -0500102 ObjectPath iP(IP::Protocol addressType, std::string ipaddress,
103 uint8_t prefixLength, std::string gateway) override;
Ratan Gupta8c834932017-04-14 16:30:24 +0530104
William A. Kennington III08505792019-01-30 16:00:04 -0800105 /** @brief Function to create static neighbor dbus object.
106 * @param[in] ipAddress - IP address.
107 * @param[in] macAddress - Low level MAC address.
108 */
109 ObjectPath neighbor(std::string iPAddress, std::string mACAddress) override;
110
Gunnar Mills57d9c502018-09-14 14:42:34 -0500111 /* @brief delete the dbus object of the given ipaddress.
112 * @param[in] ipaddress - IP address.
113 */
114 void deleteObject(const std::string& ipaddress);
Ratan Gupta8c834932017-04-14 16:30:24 +0530115
William A. Kennington III08505792019-01-30 16:00:04 -0800116 /* @brief delete the dbus object of the given ipaddress.
117 * @param[in] ipaddress - IP address.
118 */
119 void deleteStaticNeighborObject(const std::string& ipAddress);
120
Gunnar Mills57d9c502018-09-14 14:42:34 -0500121 /* @brief delete the vlan dbus object of the given interface.
122 * Also deletes the device file and the network file.
123 * @param[in] interface - VLAN Interface.
124 */
125 void deleteVLANObject(const std::string& interface);
Ratan Guptabc886292017-07-25 18:29:57 +0530126
Gunnar Mills57d9c502018-09-14 14:42:34 -0500127 /* @brief creates the dbus object(IPaddres) given in the address list.
128 * @param[in] addrs - address list for which dbus objects needs
129 * to create.
130 */
131 void createIPAddressObjects();
Ratan Gupta29b0e432017-05-25 12:51:40 +0530132
William A. Kennington III08505792019-01-30 16:00:04 -0800133 /* @brief creates the dbus object(Neighbor) given in the neighbor list.
134 */
135 void createStaticNeighborObjects();
136
William A. Kennington IIId7946a72019-04-19 14:24:09 -0700137 /* @brief Gets the index of the interface on the system
138 */
139 unsigned ifIndex() const;
140
Gunnar Mills57d9c502018-09-14 14:42:34 -0500141 /* @brief Gets all the ip addresses.
142 * @returns the list of ipaddress.
143 */
144 const AddressMap& getAddresses() const
145 {
146 return addrs;
147 }
Ratan Gupta8c834932017-04-14 16:30:24 +0530148
William A. Kennington III08505792019-01-30 16:00:04 -0800149 /* @brief Gets all the static neighbor entries.
150 * @returns Static neighbor map.
151 */
152 const NeighborMap& getStaticNeighbors() const
153 {
154 return staticNeighbors;
155 }
156
Gunnar Mills57d9c502018-09-14 14:42:34 -0500157 /** Set value of DHCPEnabled */
158 bool dHCPEnabled(bool value) override;
Ratan Gupta87c13982017-06-15 09:27:27 +0530159
Johnathan Manteyfaa72e52020-01-08 10:38:58 -0800160 /** Retrieve Link State */
161 bool linkUp() const override;
162
Johnathan Manteyd0679f92019-10-29 16:20:28 -0700163 /** Retrieve NIC State */
164 bool nICEnabled() const override;
165
166 /** Set value of NICEnabled */
167 bool nICEnabled(bool value) override;
168
Gunnar Mills57d9c502018-09-14 14:42:34 -0500169 /** @brief sets the MAC address.
170 * @param[in] value - MAC address which needs to be set on the system.
171 * @returns macAddress of the interface or throws an error.
172 */
173 std::string mACAddress(std::string value) override;
Ratan Guptabd303b12017-08-18 17:10:07 +0530174
Johnathan Mantey5b023f52019-06-24 16:06:37 -0700175 /** @brief get the IPv6AcceptRA flag from the network configuration file
176 *
177 */
178 bool getIPv6AcceptRAFromConf();
179
180 /** @brief check conf file for Router Advertisements
181 *
182 */
183 bool iPv6AcceptRA(bool value) override;
184
Gunnar Mills57d9c502018-09-14 14:42:34 -0500185 /** @brief sets the NTP servers.
186 * @param[in] value - vector of NTP servers.
187 */
188 ServerList nTPServers(ServerList value) override;
Ratan Gupta497c0c92017-08-22 19:15:59 +0530189
Gunnar Mills57d9c502018-09-14 14:42:34 -0500190 /** @brief sets the DNS/nameservers.
191 * @param[in] value - vector of DNS servers.
192 */
193 ServerList nameservers(ServerList value) override;
Ratan Gupta6dec390f2017-08-20 15:28:12 +0530194
Manojkiran Edaacd6dd52019-10-15 15:00:51 +0530195 /** @brief sets the Static DNS/nameservers.
196 * @param[in] value - vector of DNS servers.
197 */
198
199 ServerList staticNameServers(ServerList value) override;
200
Gunnar Mills57d9c502018-09-14 14:42:34 -0500201 /** @brief create Vlan interface.
202 * @param[in] id- VLAN identifier.
203 */
William A. Kennington IIIf4b4ff82019-04-09 19:06:52 -0700204 ObjectPath createVLAN(VlanId id);
Ratan Gupta5978dd12017-07-25 13:47:13 +0530205
Gunnar Mills57d9c502018-09-14 14:42:34 -0500206 /** @brief load the vlan info from the system
207 * and creates the ip address dbus objects.
208 * @param[in] vlanID- VLAN identifier.
209 */
210 void loadVLAN(VlanId vlanID);
Ratan Gupta92bc2fe2017-07-26 22:40:21 +0530211
Gunnar Mills57d9c502018-09-14 14:42:34 -0500212 /** @brief write the network conf file with the in-memory objects.
213 */
214 void writeConfigurationFile();
Ratan Gupta2b106532017-07-25 16:05:02 +0530215
Gunnar Mills57d9c502018-09-14 14:42:34 -0500216 /** @brief delete all dbus objects.
217 */
218 void deleteAll();
Ratan Guptae9c9b812017-09-22 17:15:37 +0530219
Gunnar Mills57d9c502018-09-14 14:42:34 -0500220 using EthernetInterfaceIntf::dHCPEnabled;
221 using EthernetInterfaceIntf::interfaceName;
Johnathan Manteyfaa72e52020-01-08 10:38:58 -0800222 using EthernetInterfaceIntf::linkUp;
Johnathan Manteyd0679f92019-10-29 16:20:28 -0700223 using EthernetInterfaceIntf::nICEnabled;
Gunnar Mills57d9c502018-09-14 14:42:34 -0500224 using MacAddressIntf::mACAddress;
Ratan Gupta5978dd12017-07-25 13:47:13 +0530225
Gunnar Mills57d9c502018-09-14 14:42:34 -0500226 /** @brief Absolute path of the resolv conf file */
227 static constexpr auto resolvConfFile = "/etc/resolv.conf";
Ratan Gupta6dec390f2017-08-20 15:28:12 +0530228
Gunnar Mills57d9c502018-09-14 14:42:34 -0500229 protected:
230 /** @brief get the info of the ethernet interface.
231 * @return tuple having the link speed,autonegotiation,duplexmode .
232 */
233 InterfaceInfo getInterfaceInfo() const;
Ratan Gupta8c834932017-04-14 16:30:24 +0530234
Gunnar Mills57d9c502018-09-14 14:42:34 -0500235 /* @brief delete the vlan interface from system.
236 * @param[in] interface - vlan Interface.
237 */
238 void deleteVLANFromSystem(const std::string& interface);
Ratan Guptae9c9b812017-09-22 17:15:37 +0530239
Gunnar Mills57d9c502018-09-14 14:42:34 -0500240 /** @brief get the mac address of the interface.
241 * @param[in] interfaceName - Network interface name.
242 * @return macaddress on success
243 */
Ratan Gupta8c834932017-04-14 16:30:24 +0530244
Gunnar Mills57d9c502018-09-14 14:42:34 -0500245 std::string getMACAddress(const std::string& interfaceName) const;
Ratan Gupta8c834932017-04-14 16:30:24 +0530246
Gunnar Mills57d9c502018-09-14 14:42:34 -0500247 /** @brief construct the ip address dbus object path.
248 * @param[in] addressType - Type of ip address.
249 * @param[in] ipaddress - IP address.
250 * @param[in] prefixLength - Length of prefix.
251 * @param[in] gateway - Gateway address.
Ratan Gupta65e5abe2017-05-23 13:20:44 +0530252
Gunnar Mills57d9c502018-09-14 14:42:34 -0500253 * @return path of the address object.
254 */
Ratan Gupta82549cc2017-04-21 08:45:23 +0530255
Gunnar Mills57d9c502018-09-14 14:42:34 -0500256 std::string generateObjectPath(IP::Protocol addressType,
257 const std::string& ipaddress,
258 uint8_t prefixLength,
259 const std::string& gateway) const;
Ratan Gupta82549cc2017-04-21 08:45:23 +0530260
William A. Kennington III08505792019-01-30 16:00:04 -0800261 std::string
262 generateStaticNeighborObjectPath(const std::string& iPAddress,
263 const std::string& mACAddress) const;
264
Gunnar Mills57d9c502018-09-14 14:42:34 -0500265 /** @brief generates the id by doing hash of ipaddress,
266 * prefixlength and the gateway.
267 * @param[in] ipaddress - IP address.
268 * @param[in] prefixLength - Length of prefix.
269 * @param[in] gateway - Gateway address.
270 * @return hash string.
271 */
Ratan Gupta82549cc2017-04-21 08:45:23 +0530272
Gunnar Mills57d9c502018-09-14 14:42:34 -0500273 static std::string generateId(const std::string& ipaddress,
274 uint8_t prefixLength,
275 const std::string& gateway);
Ratan Gupta82549cc2017-04-21 08:45:23 +0530276
William A. Kennington III08505792019-01-30 16:00:04 -0800277 /** @brief generates the id by doing hash of ipaddress
278 * and the mac address.
279 * @param[in] iPAddress - IP address.
280 * @param[in] mACAddress - Gateway address.
281 * @return hash string.
282 */
283 static std::string generateNeighborId(const std::string& iPAddress,
284 const std::string& mACAddress);
285
Gunnar Mills57d9c502018-09-14 14:42:34 -0500286 /** @brief write the dhcp section **/
287 void writeDHCPSection(std::fstream& stream);
Ratan Gupta2b106532017-07-25 16:05:02 +0530288
Gunnar Mills57d9c502018-09-14 14:42:34 -0500289 /** @brief get the NTP server list from the network conf
290 *
291 */
292 ServerList getNTPServersFromConf();
Ratan Gupta497c0c92017-08-22 19:15:59 +0530293
Gunnar Mills57d9c502018-09-14 14:42:34 -0500294 /** @brief get the name server details from the network conf
295 *
296 */
Manojkiran Edaacd6dd52019-10-15 15:00:51 +0530297 virtual ServerList getNameServerFromResolvd();
298 ServerList getstaticNameServerFromConf();
Ratan Gupta6dec390f2017-08-20 15:28:12 +0530299
Gunnar Mills57d9c502018-09-14 14:42:34 -0500300 /** @brief Persistent sdbusplus DBus bus connection. */
301 sdbusplus::bus::bus& bus;
Ratan Gupta82549cc2017-04-21 08:45:23 +0530302
Gunnar Mills57d9c502018-09-14 14:42:34 -0500303 /** @brief Network Manager object. */
304 Manager& manager;
Ratan Gupta4f1c18b2017-05-25 12:59:35 +0530305
Gunnar Mills57d9c502018-09-14 14:42:34 -0500306 /** @brief Persistent map of IPAddress dbus objects and their names */
307 AddressMap addrs;
Ratan Gupta82549cc2017-04-21 08:45:23 +0530308
William A. Kennington III08505792019-01-30 16:00:04 -0800309 /** @brief Persistent map of Neighbor dbus objects and their names */
310 NeighborMap staticNeighbors;
311
Gunnar Mills57d9c502018-09-14 14:42:34 -0500312 /** @brief Persistent map of VLAN interface dbus objects and their names */
313 VlanInterfaceMap vlanInterfaces;
Ratan Gupta5978dd12017-07-25 13:47:13 +0530314
Gunnar Mills57d9c502018-09-14 14:42:34 -0500315 /** @brief Dbus object path */
316 std::string objPath;
Ratan Gupta47722dc2017-05-26 18:32:23 +0530317
Gunnar Mills57d9c502018-09-14 14:42:34 -0500318 friend class TestEthernetInterface;
Ratan Gupta8c834932017-04-14 16:30:24 +0530319};
320
321} // namespace network
322} // namespace phosphor