blob: e2418a25c18ef76cb8184f544976dfda39baac90 [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,
Johnathan Mantey817012a2020-01-30 15:07:39 -080088 DHCPConf dhcpEnabled, Manager& parent,
Gunnar Mills57d9c502018-09-14 14:42:34 -050089 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 */
Johnathan Mantey817012a2020-01-30 15:07:39 -0800158 DHCPConf dHCPEnabled(DHCPConf value) override;
159
160 /** @brief Selectively disables DHCP
161 * @param[in] protocol - The IPv4 or IPv6 protocol to return to static
162 * addressing mode
163 */
164 void disableDHCP(IP::Protocol protocol);
Ratan Gupta87c13982017-06-15 09:27:27 +0530165
Johnathan Manteyfaa72e52020-01-08 10:38:58 -0800166 /** Retrieve Link State */
167 bool linkUp() const override;
168
Johnathan Manteyd0679f92019-10-29 16:20:28 -0700169 /** Retrieve NIC State */
170 bool nICEnabled() const override;
171
172 /** Set value of NICEnabled */
173 bool nICEnabled(bool value) override;
174
Gunnar Mills57d9c502018-09-14 14:42:34 -0500175 /** @brief sets the MAC address.
176 * @param[in] value - MAC address which needs to be set on the system.
177 * @returns macAddress of the interface or throws an error.
178 */
179 std::string mACAddress(std::string value) override;
Ratan Guptabd303b12017-08-18 17:10:07 +0530180
Johnathan Mantey5b023f52019-06-24 16:06:37 -0700181 /** @brief get the IPv6AcceptRA flag from the network configuration file
182 *
183 */
184 bool getIPv6AcceptRAFromConf();
185
186 /** @brief check conf file for Router Advertisements
187 *
188 */
189 bool iPv6AcceptRA(bool value) override;
190
Gunnar Mills57d9c502018-09-14 14:42:34 -0500191 /** @brief sets the NTP servers.
192 * @param[in] value - vector of NTP servers.
193 */
194 ServerList nTPServers(ServerList value) override;
Ratan Gupta497c0c92017-08-22 19:15:59 +0530195
Gunnar Mills57d9c502018-09-14 14:42:34 -0500196 /** @brief sets the DNS/nameservers.
197 * @param[in] value - vector of DNS servers.
198 */
199 ServerList nameservers(ServerList value) override;
Ratan Gupta6dec3902017-08-20 15:28:12 +0530200
Manojkiran Edaacd6dd52019-10-15 15:00:51 +0530201 /** @brief sets the Static DNS/nameservers.
202 * @param[in] value - vector of DNS servers.
203 */
204
205 ServerList staticNameServers(ServerList value) override;
206
Gunnar Mills57d9c502018-09-14 14:42:34 -0500207 /** @brief create Vlan interface.
208 * @param[in] id- VLAN identifier.
209 */
William A. Kennington IIIf4b4ff82019-04-09 19:06:52 -0700210 ObjectPath createVLAN(VlanId id);
Ratan Gupta5978dd12017-07-25 13:47:13 +0530211
Gunnar Mills57d9c502018-09-14 14:42:34 -0500212 /** @brief load the vlan info from the system
213 * and creates the ip address dbus objects.
214 * @param[in] vlanID- VLAN identifier.
215 */
216 void loadVLAN(VlanId vlanID);
Ratan Gupta92bc2fe2017-07-26 22:40:21 +0530217
Gunnar Mills57d9c502018-09-14 14:42:34 -0500218 /** @brief write the network conf file with the in-memory objects.
219 */
220 void writeConfigurationFile();
Ratan Gupta2b106532017-07-25 16:05:02 +0530221
Gunnar Mills57d9c502018-09-14 14:42:34 -0500222 /** @brief delete all dbus objects.
223 */
224 void deleteAll();
Ratan Guptae9c9b812017-09-22 17:15:37 +0530225
Ravi Tejaa5a09442020-07-17 00:57:33 -0500226 /** @brief set the default v4 gateway of the interface.
227 * @param[in] gateway - default v4 gateway of the interface.
228 */
229 std::string defaultGateway(std::string gateway) override;
230
231 /** @brief set the default v6 gateway of the interface.
232 * @param[in] gateway - default v6 gateway of the interface.
233 */
234 std::string defaultGateway6(std::string gateway) override;
235
Gunnar Mills57d9c502018-09-14 14:42:34 -0500236 using EthernetInterfaceIntf::dHCPEnabled;
237 using EthernetInterfaceIntf::interfaceName;
Johnathan Manteyfaa72e52020-01-08 10:38:58 -0800238 using EthernetInterfaceIntf::linkUp;
Johnathan Manteyd0679f92019-10-29 16:20:28 -0700239 using EthernetInterfaceIntf::nICEnabled;
Gunnar Mills57d9c502018-09-14 14:42:34 -0500240 using MacAddressIntf::mACAddress;
Ratan Gupta5978dd12017-07-25 13:47:13 +0530241
Ravi Tejaa5a09442020-07-17 00:57:33 -0500242 using EthernetInterfaceIntf::defaultGateway;
243 using EthernetInterfaceIntf::defaultGateway6;
Gunnar Mills57d9c502018-09-14 14:42:34 -0500244 /** @brief Absolute path of the resolv conf file */
245 static constexpr auto resolvConfFile = "/etc/resolv.conf";
Ratan Gupta6dec3902017-08-20 15:28:12 +0530246
Gunnar Mills57d9c502018-09-14 14:42:34 -0500247 protected:
248 /** @brief get the info of the ethernet interface.
249 * @return tuple having the link speed,autonegotiation,duplexmode .
250 */
251 InterfaceInfo getInterfaceInfo() const;
Ratan Gupta8c834932017-04-14 16:30:24 +0530252
Gunnar Mills57d9c502018-09-14 14:42:34 -0500253 /* @brief delete the vlan interface from system.
254 * @param[in] interface - vlan Interface.
255 */
256 void deleteVLANFromSystem(const std::string& interface);
Ratan Guptae9c9b812017-09-22 17:15:37 +0530257
Gunnar Mills57d9c502018-09-14 14:42:34 -0500258 /** @brief get the mac address of the interface.
259 * @param[in] interfaceName - Network interface name.
260 * @return macaddress on success
261 */
Ratan Gupta8c834932017-04-14 16:30:24 +0530262
Gunnar Mills57d9c502018-09-14 14:42:34 -0500263 std::string getMACAddress(const std::string& interfaceName) const;
Ratan Gupta8c834932017-04-14 16:30:24 +0530264
Gunnar Mills57d9c502018-09-14 14:42:34 -0500265 /** @brief construct the ip address dbus object path.
266 * @param[in] addressType - Type of ip address.
267 * @param[in] ipaddress - IP address.
268 * @param[in] prefixLength - Length of prefix.
269 * @param[in] gateway - Gateway address.
Ratan Gupta65e5abe2017-05-23 13:20:44 +0530270
Gunnar Mills57d9c502018-09-14 14:42:34 -0500271 * @return path of the address object.
272 */
Ratan Gupta82549cc2017-04-21 08:45:23 +0530273
Gunnar Mills57d9c502018-09-14 14:42:34 -0500274 std::string generateObjectPath(IP::Protocol addressType,
275 const std::string& ipaddress,
276 uint8_t prefixLength,
277 const std::string& gateway) const;
Ratan Gupta82549cc2017-04-21 08:45:23 +0530278
William A. Kennington III08505792019-01-30 16:00:04 -0800279 std::string
280 generateStaticNeighborObjectPath(const std::string& iPAddress,
281 const std::string& mACAddress) const;
282
Gunnar Mills57d9c502018-09-14 14:42:34 -0500283 /** @brief generates the id by doing hash of ipaddress,
284 * prefixlength and the gateway.
285 * @param[in] ipaddress - IP address.
286 * @param[in] prefixLength - Length of prefix.
287 * @param[in] gateway - Gateway address.
288 * @return hash string.
289 */
Ratan Gupta82549cc2017-04-21 08:45:23 +0530290
Gunnar Mills57d9c502018-09-14 14:42:34 -0500291 static std::string generateId(const std::string& ipaddress,
292 uint8_t prefixLength,
293 const std::string& gateway);
Ratan Gupta82549cc2017-04-21 08:45:23 +0530294
William A. Kennington III08505792019-01-30 16:00:04 -0800295 /** @brief generates the id by doing hash of ipaddress
296 * and the mac address.
297 * @param[in] iPAddress - IP address.
298 * @param[in] mACAddress - Gateway address.
299 * @return hash string.
300 */
301 static std::string generateNeighborId(const std::string& iPAddress,
302 const std::string& mACAddress);
303
Gunnar Mills57d9c502018-09-14 14:42:34 -0500304 /** @brief write the dhcp section **/
305 void writeDHCPSection(std::fstream& stream);
Ratan Gupta2b106532017-07-25 16:05:02 +0530306
Gunnar Mills57d9c502018-09-14 14:42:34 -0500307 /** @brief get the NTP server list from the network conf
308 *
309 */
310 ServerList getNTPServersFromConf();
Ratan Gupta497c0c92017-08-22 19:15:59 +0530311
Gunnar Mills57d9c502018-09-14 14:42:34 -0500312 /** @brief get the name server details from the network conf
313 *
314 */
Manojkiran Edaacd6dd52019-10-15 15:00:51 +0530315 virtual ServerList getNameServerFromResolvd();
316 ServerList getstaticNameServerFromConf();
Ratan Gupta6dec3902017-08-20 15:28:12 +0530317
Gunnar Mills57d9c502018-09-14 14:42:34 -0500318 /** @brief Persistent sdbusplus DBus bus connection. */
319 sdbusplus::bus::bus& bus;
Ratan Gupta82549cc2017-04-21 08:45:23 +0530320
Gunnar Mills57d9c502018-09-14 14:42:34 -0500321 /** @brief Network Manager object. */
322 Manager& manager;
Ratan Gupta4f1c18b2017-05-25 12:59:35 +0530323
Gunnar Mills57d9c502018-09-14 14:42:34 -0500324 /** @brief Persistent map of IPAddress dbus objects and their names */
325 AddressMap addrs;
Ratan Gupta82549cc2017-04-21 08:45:23 +0530326
William A. Kennington III08505792019-01-30 16:00:04 -0800327 /** @brief Persistent map of Neighbor dbus objects and their names */
328 NeighborMap staticNeighbors;
329
Gunnar Mills57d9c502018-09-14 14:42:34 -0500330 /** @brief Persistent map of VLAN interface dbus objects and their names */
331 VlanInterfaceMap vlanInterfaces;
Ratan Gupta5978dd12017-07-25 13:47:13 +0530332
Gunnar Mills57d9c502018-09-14 14:42:34 -0500333 /** @brief Dbus object path */
334 std::string objPath;
Ratan Gupta47722dc2017-05-26 18:32:23 +0530335
Gunnar Mills57d9c502018-09-14 14:42:34 -0500336 friend class TestEthernetInterface;
Johnathan Mantey817012a2020-01-30 15:07:39 -0800337
338 private:
339 /** @brief Determines if DHCP is active for the IP::Protocol supplied.
340 * @param[in] protocol - Either IPv4 or IPv6
341 * @param[in] ignoreProtocol - Allows IPv4 and IPv6 to be checked using a
342 * single call.
343 * @returns true/false value if DHCP is active for the input protocol
344 */
345 bool dhcpIsEnabled(IP::Protocol protocol, bool ignoreProtocol = false);
346
347 /** @brief Determines if DHCP will be active following next reconfig
348 * @param[in] protocol - Either IPv4 or IPv6
349 * @param[in] nextDHCPState - The new DHCP mode to take affect
350 * @returns true/false value if DHCP is active for the input protocol
351 */
352 bool dhcpToBeEnabled(IP::Protocol family, const std::string& nextDHCPState);
353
354 /** @brief Determines if the address is manually assigned
355 * @param[in] origin - The origin entry of the IP::Address
356 * @returns true/false value if the address is static
357 */
358 bool originIsManuallyAssigned(IP::AddressOrigin origin);
Ratan Gupta8c834932017-04-14 16:30:24 +0530359};
360
361} // namespace network
362} // namespace phosphor