blob: 8e28b51445201cba4b29186c26488a15c69e4765 [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
Patrick Williams6aef7692021-05-01 06:39:41 -050016#ifndef SDBUSPP_NEW_CAMELCASE
17#define dhcpEnabled dHCPEnabled
18#define ip iP
19#define ipAddress iPAddress
20#define ipv6AcceptRA iPv6AcceptRA
21#define macAddress mACAddress
22#define nicEnabled nICEnabled
23#define ntpServers nTPServers
24#endif
25
Ratan Gupta8c834932017-04-14 16:30:24 +053026namespace phosphor
27{
28namespace network
29{
Ratan Gupta8c834932017-04-14 16:30:24 +053030
Gunnar Mills57d9c502018-09-14 14:42:34 -050031using Ifaces = sdbusplus::server::object::object<
32 sdbusplus::xyz::openbmc_project::Network::server::EthernetInterface,
33 sdbusplus::xyz::openbmc_project::Network::server::MACAddress,
34 sdbusplus::xyz::openbmc_project::Network::IP::server::Create,
William A. Kennington III08505792019-01-30 16:00:04 -080035 sdbusplus::xyz::openbmc_project::Network::Neighbor::server::CreateStatic,
Gunnar Mills57d9c502018-09-14 14:42:34 -050036 sdbusplus::xyz::openbmc_project::Collection::server::DeleteAll>;
Ratan Gupta8c834932017-04-14 16:30:24 +053037
Ratan Gupta82549cc2017-04-21 08:45:23 +053038using IP = sdbusplus::xyz::openbmc_project::Network::server::IP;
Ratan Gupta87c13982017-06-15 09:27:27 +053039
40using EthernetInterfaceIntf =
41 sdbusplus::xyz::openbmc_project::Network::server::EthernetInterface;
Ratan Guptabd303b12017-08-18 17:10:07 +053042using MacAddressIntf =
43 sdbusplus::xyz::openbmc_project::Network::server::MACAddress;
Ratan Gupta87c13982017-06-15 09:27:27 +053044
Ratan Gupta497c0c92017-08-22 19:15:59 +053045using ServerList = std::vector<std::string>;
raviteja-bce379562019-03-28 05:59:36 -050046using ObjectPath = sdbusplus::message::object_path;
Ratan Gupta497c0c92017-08-22 19:15:59 +053047
Manojkiran Edaa879baa2020-06-13 14:39:08 +053048namespace fs = std::filesystem;
Ratan Gupta5978dd12017-07-25 13:47:13 +053049
Ratan Gupta4f1c18b2017-05-25 12:59:35 +053050class Manager; // forward declaration of network manager.
Ratan Gupta8c834932017-04-14 16:30:24 +053051
Ratan Gupta47722dc2017-05-26 18:32:23 +053052class TestEthernetInterface;
53
Ratan Gupta5978dd12017-07-25 13:47:13 +053054class VlanInterface;
55
56class IPAddress;
57
William A. Kennington III08505792019-01-30 16:00:04 -080058class Neighbor;
59
Ratan Gupta8c834932017-04-14 16:30:24 +053060using LinkSpeed = uint16_t;
61using DuplexMode = uint8_t;
62using Autoneg = uint8_t;
Johnathan Manteyfaa72e52020-01-08 10:38:58 -080063using LinkUp = bool;
Johnathan Manteyd0679f92019-10-29 16:20:28 -070064using NICEnabled = bool;
Ratan Gupta5978dd12017-07-25 13:47:13 +053065using VlanId = uint32_t;
66using InterfaceName = std::string;
Johnathan Manteyd0679f92019-10-29 16:20:28 -070067using InterfaceInfo =
68 std::tuple<LinkSpeed, DuplexMode, Autoneg, LinkUp, NICEnabled>;
Ratan Gupta29b0e432017-05-25 12:51:40 +053069using AddressMap = std::map<std::string, std::shared_ptr<IPAddress>>;
William A. Kennington III08505792019-01-30 16:00:04 -080070using NeighborMap = std::map<std::string, std::shared_ptr<Neighbor>>;
Gunnar Mills57d9c502018-09-14 14:42:34 -050071using VlanInterfaceMap =
72 std::map<InterfaceName, std::unique_ptr<VlanInterface>>;
Ratan Gupta8c834932017-04-14 16:30:24 +053073
74/** @class EthernetInterface
75 * @brief OpenBMC Ethernet Interface implementation.
76 * @details A concrete implementation for the
77 * xyz.openbmc_project.Network.EthernetInterface DBus API.
78 */
Ratan Gupta82549cc2017-04-21 08:45:23 +053079class EthernetInterface : public Ifaces
Ratan Gupta8c834932017-04-14 16:30:24 +053080{
Gunnar Mills57d9c502018-09-14 14:42:34 -050081 public:
82 EthernetInterface() = delete;
83 EthernetInterface(const EthernetInterface&) = delete;
84 EthernetInterface& operator=(const EthernetInterface&) = delete;
85 EthernetInterface(EthernetInterface&&) = delete;
86 EthernetInterface& operator=(EthernetInterface&&) = delete;
87 virtual ~EthernetInterface() = default;
Ratan Gupta8c834932017-04-14 16:30:24 +053088
Gunnar Mills57d9c502018-09-14 14:42:34 -050089 /** @brief Constructor to put object onto bus at a dbus path.
90 * @param[in] bus - Bus to attach to.
91 * @param[in] objPath - Path to attach at.
92 * @param[in] dhcpEnabled - is dhcp enabled(true/false).
93 * @param[in] parent - parent object.
94 * @param[in] emitSignal - true if the object added signal needs to be
95 * send.
96 */
97 EthernetInterface(sdbusplus::bus::bus& bus, const std::string& objPath,
Johnathan Mantey817012a2020-01-30 15:07:39 -080098 DHCPConf dhcpEnabled, Manager& parent,
Gunnar Mills57d9c502018-09-14 14:42:34 -050099 bool emitSignal = true);
Ratan Gupta82549cc2017-04-21 08:45:23 +0530100
Manojkiran Edaacd6dd52019-10-15 15:00:51 +0530101 /** @brief Function used to load the nameservers.
102 */
103 virtual void loadNameServers();
104
Patrick Williams6aef7692021-05-01 06:39:41 -0500105 /** @brief Function to create ipAddress dbus object.
Gunnar Mills57d9c502018-09-14 14:42:34 -0500106 * @param[in] addressType - Type of ip address.
Patrick Williams6aef7692021-05-01 06:39:41 -0500107 * @param[in] ipAddress- IP address.
Gunnar Mills57d9c502018-09-14 14:42:34 -0500108 * @param[in] prefixLength - Length of prefix.
109 * @param[in] gateway - Gateway ip address.
110 */
Ratan Gupta82549cc2017-04-21 08:45:23 +0530111
Patrick Williams6aef7692021-05-01 06:39:41 -0500112 ObjectPath ip(IP::Protocol addressType, std::string ipAddress,
raviteja-bce379562019-03-28 05:59:36 -0500113 uint8_t prefixLength, std::string gateway) override;
Ratan Gupta8c834932017-04-14 16:30:24 +0530114
William A. Kennington III08505792019-01-30 16:00:04 -0800115 /** @brief Function to create static neighbor dbus object.
116 * @param[in] ipAddress - IP address.
117 * @param[in] macAddress - Low level MAC address.
118 */
Patrick Williams6aef7692021-05-01 06:39:41 -0500119 ObjectPath neighbor(std::string ipAddress, std::string macAddress) override;
William A. Kennington III08505792019-01-30 16:00:04 -0800120
Patrick Williams6aef7692021-05-01 06:39:41 -0500121 /* @brief delete the dbus object of the given ipAddress.
122 * @param[in] ipAddress - IP address.
Gunnar Mills57d9c502018-09-14 14:42:34 -0500123 */
Patrick Williams6aef7692021-05-01 06:39:41 -0500124 void deleteObject(const std::string& ipAddress);
Ratan Gupta8c834932017-04-14 16:30:24 +0530125
Patrick Williams6aef7692021-05-01 06:39:41 -0500126 /* @brief delete the dbus object of the given ipAddress.
127 * @param[in] ipAddress - IP address.
William A. Kennington III08505792019-01-30 16:00:04 -0800128 */
129 void deleteStaticNeighborObject(const std::string& ipAddress);
130
Gunnar Mills57d9c502018-09-14 14:42:34 -0500131 /* @brief delete the vlan dbus object of the given interface.
132 * Also deletes the device file and the network file.
133 * @param[in] interface - VLAN Interface.
134 */
135 void deleteVLANObject(const std::string& interface);
Ratan Guptabc886292017-07-25 18:29:57 +0530136
Gunnar Mills57d9c502018-09-14 14:42:34 -0500137 /* @brief creates the dbus object(IPaddres) given in the address list.
138 * @param[in] addrs - address list for which dbus objects needs
139 * to create.
140 */
141 void createIPAddressObjects();
Ratan Gupta29b0e432017-05-25 12:51:40 +0530142
William A. Kennington III08505792019-01-30 16:00:04 -0800143 /* @brief creates the dbus object(Neighbor) given in the neighbor list.
144 */
145 void createStaticNeighborObjects();
146
William A. Kennington IIId7946a72019-04-19 14:24:09 -0700147 /* @brief Gets the index of the interface on the system
148 */
149 unsigned ifIndex() const;
150
Gunnar Mills57d9c502018-09-14 14:42:34 -0500151 /* @brief Gets all the ip addresses.
Patrick Williams6aef7692021-05-01 06:39:41 -0500152 * @returns the list of ipAddress.
Gunnar Mills57d9c502018-09-14 14:42:34 -0500153 */
154 const AddressMap& getAddresses() const
155 {
156 return addrs;
157 }
Ratan Gupta8c834932017-04-14 16:30:24 +0530158
William A. Kennington III08505792019-01-30 16:00:04 -0800159 /* @brief Gets all the static neighbor entries.
160 * @returns Static neighbor map.
161 */
162 const NeighborMap& getStaticNeighbors() const
163 {
164 return staticNeighbors;
165 }
166
Gunnar Mills57d9c502018-09-14 14:42:34 -0500167 /** Set value of DHCPEnabled */
Patrick Williams6aef7692021-05-01 06:39:41 -0500168 DHCPConf dhcpEnabled(DHCPConf value) override;
Johnathan Mantey817012a2020-01-30 15:07:39 -0800169
170 /** @brief Selectively disables DHCP
171 * @param[in] protocol - The IPv4 or IPv6 protocol to return to static
172 * addressing mode
173 */
174 void disableDHCP(IP::Protocol protocol);
Ratan Gupta87c13982017-06-15 09:27:27 +0530175
Johnathan Manteyfaa72e52020-01-08 10:38:58 -0800176 /** Retrieve Link State */
177 bool linkUp() const override;
178
Johnathan Manteyd0679f92019-10-29 16:20:28 -0700179 /** Retrieve NIC State */
Patrick Williams6aef7692021-05-01 06:39:41 -0500180 bool nicEnabled() const override;
Johnathan Manteyd0679f92019-10-29 16:20:28 -0700181
182 /** Set value of NICEnabled */
Patrick Williams6aef7692021-05-01 06:39:41 -0500183 bool nicEnabled(bool value) override;
Johnathan Manteyd0679f92019-10-29 16:20:28 -0700184
Gunnar Mills57d9c502018-09-14 14:42:34 -0500185 /** @brief sets the MAC address.
186 * @param[in] value - MAC address which needs to be set on the system.
187 * @returns macAddress of the interface or throws an error.
188 */
Patrick Williams6aef7692021-05-01 06:39:41 -0500189 std::string macAddress(std::string value) override;
Ratan Guptabd303b12017-08-18 17:10:07 +0530190
Johnathan Mantey5b023f52019-06-24 16:06:37 -0700191 /** @brief get the IPv6AcceptRA flag from the network configuration file
192 *
193 */
194 bool getIPv6AcceptRAFromConf();
195
196 /** @brief check conf file for Router Advertisements
197 *
198 */
Patrick Williams6aef7692021-05-01 06:39:41 -0500199 bool ipv6AcceptRA(bool value) override;
Johnathan Mantey5b023f52019-06-24 16:06:37 -0700200
Gunnar Mills57d9c502018-09-14 14:42:34 -0500201 /** @brief sets the NTP servers.
202 * @param[in] value - vector of NTP servers.
203 */
Patrick Williams6aef7692021-05-01 06:39:41 -0500204 ServerList ntpServers(ServerList value) override;
Ratan Gupta497c0c92017-08-22 19:15:59 +0530205
Gunnar Mills57d9c502018-09-14 14:42:34 -0500206 /** @brief sets the DNS/nameservers.
207 * @param[in] value - vector of DNS servers.
208 */
209 ServerList nameservers(ServerList value) override;
Ratan Gupta6dec3902017-08-20 15:28:12 +0530210
Manojkiran Edaacd6dd52019-10-15 15:00:51 +0530211 /** @brief sets the Static DNS/nameservers.
212 * @param[in] value - vector of DNS servers.
213 */
214
215 ServerList staticNameServers(ServerList value) override;
216
Gunnar Mills57d9c502018-09-14 14:42:34 -0500217 /** @brief create Vlan interface.
218 * @param[in] id- VLAN identifier.
219 */
William A. Kennington IIIf4b4ff82019-04-09 19:06:52 -0700220 ObjectPath createVLAN(VlanId id);
Ratan Gupta5978dd12017-07-25 13:47:13 +0530221
Gunnar Mills57d9c502018-09-14 14:42:34 -0500222 /** @brief load the vlan info from the system
223 * and creates the ip address dbus objects.
224 * @param[in] vlanID- VLAN identifier.
225 */
226 void loadVLAN(VlanId vlanID);
Ratan Gupta92bc2fe2017-07-26 22:40:21 +0530227
Gunnar Mills57d9c502018-09-14 14:42:34 -0500228 /** @brief write the network conf file with the in-memory objects.
229 */
230 void writeConfigurationFile();
Ratan Gupta2b106532017-07-25 16:05:02 +0530231
Gunnar Mills57d9c502018-09-14 14:42:34 -0500232 /** @brief delete all dbus objects.
233 */
234 void deleteAll();
Ratan Guptae9c9b812017-09-22 17:15:37 +0530235
Ravi Tejaa5a09442020-07-17 00:57:33 -0500236 /** @brief set the default v4 gateway of the interface.
237 * @param[in] gateway - default v4 gateway of the interface.
238 */
239 std::string defaultGateway(std::string gateway) override;
240
241 /** @brief set the default v6 gateway of the interface.
242 * @param[in] gateway - default v6 gateway of the interface.
243 */
244 std::string defaultGateway6(std::string gateway) override;
245
Patrick Williams6aef7692021-05-01 06:39:41 -0500246 using EthernetInterfaceIntf::dhcpEnabled;
Gunnar Mills57d9c502018-09-14 14:42:34 -0500247 using EthernetInterfaceIntf::interfaceName;
Johnathan Manteyfaa72e52020-01-08 10:38:58 -0800248 using EthernetInterfaceIntf::linkUp;
Patrick Williams6aef7692021-05-01 06:39:41 -0500249 using EthernetInterfaceIntf::nicEnabled;
250 using MacAddressIntf::macAddress;
Ratan Gupta5978dd12017-07-25 13:47:13 +0530251
Ravi Tejaa5a09442020-07-17 00:57:33 -0500252 using EthernetInterfaceIntf::defaultGateway;
253 using EthernetInterfaceIntf::defaultGateway6;
Gunnar Mills57d9c502018-09-14 14:42:34 -0500254 /** @brief Absolute path of the resolv conf file */
255 static constexpr auto resolvConfFile = "/etc/resolv.conf";
Ratan Gupta6dec3902017-08-20 15:28:12 +0530256
Gunnar Mills57d9c502018-09-14 14:42:34 -0500257 protected:
258 /** @brief get the info of the ethernet interface.
259 * @return tuple having the link speed,autonegotiation,duplexmode .
260 */
261 InterfaceInfo getInterfaceInfo() const;
Ratan Gupta8c834932017-04-14 16:30:24 +0530262
Gunnar Mills57d9c502018-09-14 14:42:34 -0500263 /* @brief delete the vlan interface from system.
264 * @param[in] interface - vlan Interface.
265 */
266 void deleteVLANFromSystem(const std::string& interface);
Ratan Guptae9c9b812017-09-22 17:15:37 +0530267
Gunnar Mills57d9c502018-09-14 14:42:34 -0500268 /** @brief get the mac address of the interface.
269 * @param[in] interfaceName - Network interface name.
270 * @return macaddress on success
271 */
Ratan Gupta8c834932017-04-14 16:30:24 +0530272
Gunnar Mills57d9c502018-09-14 14:42:34 -0500273 std::string getMACAddress(const std::string& interfaceName) const;
Ratan Gupta8c834932017-04-14 16:30:24 +0530274
Gunnar Mills57d9c502018-09-14 14:42:34 -0500275 /** @brief construct the ip address dbus object path.
276 * @param[in] addressType - Type of ip address.
Patrick Williams6aef7692021-05-01 06:39:41 -0500277 * @param[in] ipAddress - IP address.
Gunnar Mills57d9c502018-09-14 14:42:34 -0500278 * @param[in] prefixLength - Length of prefix.
279 * @param[in] gateway - Gateway address.
Ratan Gupta65e5abe2017-05-23 13:20:44 +0530280
Gunnar Mills57d9c502018-09-14 14:42:34 -0500281 * @return path of the address object.
282 */
Ratan Gupta82549cc2017-04-21 08:45:23 +0530283
Gunnar Mills57d9c502018-09-14 14:42:34 -0500284 std::string generateObjectPath(IP::Protocol addressType,
Patrick Williams6aef7692021-05-01 06:39:41 -0500285 const std::string& ipAddress,
Gunnar Mills57d9c502018-09-14 14:42:34 -0500286 uint8_t prefixLength,
287 const std::string& gateway) const;
Ratan Gupta82549cc2017-04-21 08:45:23 +0530288
William A. Kennington III08505792019-01-30 16:00:04 -0800289 std::string
Patrick Williams6aef7692021-05-01 06:39:41 -0500290 generateStaticNeighborObjectPath(const std::string& ipAddress,
291 const std::string& macAddress) const;
William A. Kennington III08505792019-01-30 16:00:04 -0800292
Patrick Williams6aef7692021-05-01 06:39:41 -0500293 /** @brief generates the id by doing hash of ipAddress,
Gunnar Mills57d9c502018-09-14 14:42:34 -0500294 * prefixlength and the gateway.
Patrick Williams6aef7692021-05-01 06:39:41 -0500295 * @param[in] ipAddress - IP address.
Gunnar Mills57d9c502018-09-14 14:42:34 -0500296 * @param[in] prefixLength - Length of prefix.
297 * @param[in] gateway - Gateway address.
298 * @return hash string.
299 */
Ratan Gupta82549cc2017-04-21 08:45:23 +0530300
Patrick Williams6aef7692021-05-01 06:39:41 -0500301 static std::string generateId(const std::string& ipAddress,
Gunnar Mills57d9c502018-09-14 14:42:34 -0500302 uint8_t prefixLength,
303 const std::string& gateway);
Ratan Gupta82549cc2017-04-21 08:45:23 +0530304
Patrick Williams6aef7692021-05-01 06:39:41 -0500305 /** @brief generates the id by doing hash of ipAddress
William A. Kennington III08505792019-01-30 16:00:04 -0800306 * and the mac address.
Patrick Williams6aef7692021-05-01 06:39:41 -0500307 * @param[in] ipAddress - IP address.
308 * @param[in] macAddress - Gateway address.
William A. Kennington III08505792019-01-30 16:00:04 -0800309 * @return hash string.
310 */
Patrick Williams6aef7692021-05-01 06:39:41 -0500311 static std::string generateNeighborId(const std::string& ipAddress,
312 const std::string& macAddress);
William A. Kennington III08505792019-01-30 16:00:04 -0800313
Gunnar Mills57d9c502018-09-14 14:42:34 -0500314 /** @brief write the dhcp section **/
315 void writeDHCPSection(std::fstream& stream);
Ratan Gupta2b106532017-07-25 16:05:02 +0530316
Gunnar Mills57d9c502018-09-14 14:42:34 -0500317 /** @brief get the NTP server list from the network conf
318 *
319 */
320 ServerList getNTPServersFromConf();
Ratan Gupta497c0c92017-08-22 19:15:59 +0530321
Gunnar Mills57d9c502018-09-14 14:42:34 -0500322 /** @brief get the name server details from the network conf
323 *
324 */
Manojkiran Edaacd6dd52019-10-15 15:00:51 +0530325 virtual ServerList getNameServerFromResolvd();
326 ServerList getstaticNameServerFromConf();
Ratan Gupta6dec3902017-08-20 15:28:12 +0530327
Gunnar Mills57d9c502018-09-14 14:42:34 -0500328 /** @brief Persistent sdbusplus DBus bus connection. */
329 sdbusplus::bus::bus& bus;
Ratan Gupta82549cc2017-04-21 08:45:23 +0530330
Gunnar Mills57d9c502018-09-14 14:42:34 -0500331 /** @brief Network Manager object. */
332 Manager& manager;
Ratan Gupta4f1c18b2017-05-25 12:59:35 +0530333
Gunnar Mills57d9c502018-09-14 14:42:34 -0500334 /** @brief Persistent map of IPAddress dbus objects and their names */
335 AddressMap addrs;
Ratan Gupta82549cc2017-04-21 08:45:23 +0530336
William A. Kennington III08505792019-01-30 16:00:04 -0800337 /** @brief Persistent map of Neighbor dbus objects and their names */
338 NeighborMap staticNeighbors;
339
Gunnar Mills57d9c502018-09-14 14:42:34 -0500340 /** @brief Persistent map of VLAN interface dbus objects and their names */
341 VlanInterfaceMap vlanInterfaces;
Ratan Gupta5978dd12017-07-25 13:47:13 +0530342
Gunnar Mills57d9c502018-09-14 14:42:34 -0500343 /** @brief Dbus object path */
344 std::string objPath;
Ratan Gupta47722dc2017-05-26 18:32:23 +0530345
Gunnar Mills57d9c502018-09-14 14:42:34 -0500346 friend class TestEthernetInterface;
Johnathan Mantey817012a2020-01-30 15:07:39 -0800347
348 private:
349 /** @brief Determines if DHCP is active for the IP::Protocol supplied.
350 * @param[in] protocol - Either IPv4 or IPv6
351 * @param[in] ignoreProtocol - Allows IPv4 and IPv6 to be checked using a
352 * single call.
353 * @returns true/false value if DHCP is active for the input protocol
354 */
355 bool dhcpIsEnabled(IP::Protocol protocol, bool ignoreProtocol = false);
356
357 /** @brief Determines if DHCP will be active following next reconfig
358 * @param[in] protocol - Either IPv4 or IPv6
359 * @param[in] nextDHCPState - The new DHCP mode to take affect
360 * @returns true/false value if DHCP is active for the input protocol
361 */
362 bool dhcpToBeEnabled(IP::Protocol family, const std::string& nextDHCPState);
363
364 /** @brief Determines if the address is manually assigned
365 * @param[in] origin - The origin entry of the IP::Address
366 * @returns true/false value if the address is static
367 */
368 bool originIsManuallyAssigned(IP::AddressOrigin origin);
Ratan Gupta8c834932017-04-14 16:30:24 +0530369};
370
371} // namespace network
372} // namespace phosphor