blob: d7342a80a9047f8822804bd06579c319970ed2d0 [file] [log] [blame]
Ratan Gupta8c834932017-04-14 16:30:24 +05301#pragma once
William A. Kennington III9a20a6e2022-10-05 13:41:12 -07002#include "ipaddress.hpp"
3#include "neighbor.hpp"
Ratan Gupta82549cc2017-04-21 08:45:23 +05304#include "types.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
William A. Kennington IIId298f932022-10-17 14:31:38 -07008#include <optional>
Ratan Gupta8c834932017-04-14 16:30:24 +05309#include <sdbusplus/bus.hpp>
10#include <sdbusplus/server/object.hpp>
William A. Kennington IIIbe3bd2f2022-10-11 14:11:27 -070011#include <stdplus/zstring_view.hpp>
Ratan Gupta8c834932017-04-14 16:30:24 +053012#include <string>
William A. Kennington III95530ec2022-08-19 01:44:39 -070013#include <vector>
Patrick Venture189d44e2018-07-09 12:30:59 -070014#include <xyz/openbmc_project/Collection/DeleteAll/server.hpp>
15#include <xyz/openbmc_project/Network/EthernetInterface/server.hpp>
16#include <xyz/openbmc_project/Network/MACAddress/server.hpp>
William A. Kennington III09f3a4a2022-10-25 02:59:16 -070017#include <xyz/openbmc_project/Network/VLAN/server.hpp>
18#include <xyz/openbmc_project/Object/Delete/server.hpp>
Ratan Gupta8c834932017-04-14 16:30:24 +053019
20namespace phosphor
21{
22namespace network
23{
Ratan Gupta8c834932017-04-14 16:30:24 +053024
Patrick Williamsc38b0712022-07-22 19:26:54 -050025using Ifaces = sdbusplus::server::object_t<
Gunnar Mills57d9c502018-09-14 14:42:34 -050026 sdbusplus::xyz::openbmc_project::Network::server::EthernetInterface,
27 sdbusplus::xyz::openbmc_project::Network::server::MACAddress,
28 sdbusplus::xyz::openbmc_project::Network::IP::server::Create,
William A. Kennington III08505792019-01-30 16:00:04 -080029 sdbusplus::xyz::openbmc_project::Network::Neighbor::server::CreateStatic,
Gunnar Mills57d9c502018-09-14 14:42:34 -050030 sdbusplus::xyz::openbmc_project::Collection::server::DeleteAll>;
Ratan Gupta8c834932017-04-14 16:30:24 +053031
William A. Kennington III09f3a4a2022-10-25 02:59:16 -070032using VlanIfaces = sdbusplus::server::object_t<
33 sdbusplus::xyz::openbmc_project::Object::server::Delete,
34 sdbusplus::xyz::openbmc_project::Network::server::VLAN>;
35
36using VlanIntf = sdbusplus::xyz::openbmc_project::Network::server::VLAN;
37
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
William A. Kennington III9a20a6e2022-10-05 13:41:12 -070048class Manager;
Ratan Gupta8c834932017-04-14 16:30:24 +053049
Ratan Gupta47722dc2017-05-26 18:32:23 +053050class TestEthernetInterface;
William A. Kennington III09f3a4a2022-10-25 02:59:16 -070051class TestNetworkManager;
Ratan Gupta5978dd12017-07-25 13:47:13 +053052
William A. Kennington III95530ec2022-08-19 01:44:39 -070053namespace config
54{
55class Parser;
56}
William A. Kennington IIIfd862be2022-10-09 18:40:55 -070057namespace system
William A. Kennington IIId298f932022-10-17 14:31:38 -070058{
William A. Kennington IIIfd862be2022-10-09 18:40:55 -070059struct InterfaceInfo;
William A. Kennington IIId298f932022-10-17 14:31:38 -070060};
61
Ratan Gupta8c834932017-04-14 16:30:24 +053062/** @class EthernetInterface
63 * @brief OpenBMC Ethernet Interface implementation.
64 * @details A concrete implementation for the
65 * xyz.openbmc_project.Network.EthernetInterface DBus API.
66 */
Ratan Gupta82549cc2017-04-21 08:45:23 +053067class EthernetInterface : public Ifaces
Ratan Gupta8c834932017-04-14 16:30:24 +053068{
Gunnar Mills57d9c502018-09-14 14:42:34 -050069 public:
70 EthernetInterface() = delete;
71 EthernetInterface(const EthernetInterface&) = delete;
72 EthernetInterface& operator=(const EthernetInterface&) = delete;
73 EthernetInterface(EthernetInterface&&) = delete;
74 EthernetInterface& operator=(EthernetInterface&&) = delete;
75 virtual ~EthernetInterface() = default;
Ratan Gupta8c834932017-04-14 16:30:24 +053076
Gunnar Mills57d9c502018-09-14 14:42:34 -050077 /** @brief Constructor to put object onto bus at a dbus path.
78 * @param[in] bus - Bus to attach to.
William A. Kennington IIId298f932022-10-17 14:31:38 -070079 * @param[in] manager - parent object.
80 * @param[in] info - Interface information.
81 * @param[in] objRoot - Path to attach at.
William A. Kennington IIIa520a392022-08-08 12:17:34 -070082 * @param[in] config - The parsed configuation file.
William A. Kennington III09f3a4a2022-10-25 02:59:16 -070083 * @param[in] vlan - The id of the vlan if configured
Gunnar Mills57d9c502018-09-14 14:42:34 -050084 * @param[in] emitSignal - true if the object added signal needs to be
85 * send.
William A. Kennington III26275a32021-07-13 20:32:42 -070086 * @param[in] enabled - Override the lookup of nicEnabled
Gunnar Mills57d9c502018-09-14 14:42:34 -050087 */
William A. Kennington IIId298f932022-10-17 14:31:38 -070088 EthernetInterface(sdbusplus::bus_t& bus, Manager& manager,
William A. Kennington IIIfd862be2022-10-09 18:40:55 -070089 const system::InterfaceInfo& info,
90 std::string_view objRoot, const config::Parser& config,
91 bool emitSignal = true,
William A. Kennington III26275a32021-07-13 20:32:42 -070092 std::optional<bool> enabled = std::nullopt);
Ratan Gupta82549cc2017-04-21 08:45:23 +053093
William A. Kennington III59e5b912022-11-02 02:49:46 -070094 /** @brief Network Manager object. */
95 Manager& manager;
96
97 /** @brief Persistent map of IPAddress dbus objects and their names */
98 std::unordered_map<IfAddr, std::unique_ptr<IPAddress>> addrs;
99
William A. Kennington IIId298f932022-10-17 14:31:38 -0700100 /** @brief Updates the interface information based on new InterfaceInfo */
William A. Kennington IIIfd862be2022-10-09 18:40:55 -0700101 void updateInfo(const system::InterfaceInfo& info);
William A. Kennington IIId298f932022-10-17 14:31:38 -0700102
Asmitha Karunanithi003b8b72022-01-06 04:17:59 -0600103 /** @brief Function used to load the ntpservers
104 */
105 void loadNTPServers(const config::Parser& config);
106
Manojkiran Edaacd6dd52019-10-15 15:00:51 +0530107 /** @brief Function used to load the nameservers.
108 */
William A. Kennington IIIa520a392022-08-08 12:17:34 -0700109 void loadNameServers(const config::Parser& config);
Manojkiran Edaacd6dd52019-10-15 15:00:51 +0530110
Patrick Williams6aef7692021-05-01 06:39:41 -0500111 /** @brief Function to create ipAddress dbus object.
Gunnar Mills57d9c502018-09-14 14:42:34 -0500112 * @param[in] addressType - Type of ip address.
Patrick Williams6aef7692021-05-01 06:39:41 -0500113 * @param[in] ipAddress- IP address.
Gunnar Mills57d9c502018-09-14 14:42:34 -0500114 * @param[in] prefixLength - Length of prefix.
Gunnar Mills57d9c502018-09-14 14:42:34 -0500115 */
Ratan Gupta82549cc2017-04-21 08:45:23 +0530116
Patrick Williams6aef7692021-05-01 06:39:41 -0500117 ObjectPath ip(IP::Protocol addressType, std::string ipAddress,
William A. Kennington IIIe25f8b42022-10-11 14:43:28 -0700118 uint8_t prefixLength, std::string) override;
Ratan Gupta8c834932017-04-14 16:30:24 +0530119
William A. Kennington III08505792019-01-30 16:00:04 -0800120 /** @brief Function to create static neighbor dbus object.
121 * @param[in] ipAddress - IP address.
122 * @param[in] macAddress - Low level MAC address.
123 */
Patrick Williams6aef7692021-05-01 06:39:41 -0500124 ObjectPath neighbor(std::string ipAddress, std::string macAddress) override;
William A. Kennington III08505792019-01-30 16:00:04 -0800125
Patrick Williams6aef7692021-05-01 06:39:41 -0500126 /* @brief delete the dbus object of the given ipAddress.
127 * @param[in] ipAddress - IP address.
Gunnar Mills57d9c502018-09-14 14:42:34 -0500128 */
William A. Kennington IIIbe3bd2f2022-10-11 14:11:27 -0700129 void deleteStaticNeighborObject(std::string_view ipAddress);
William A. Kennington III08505792019-01-30 16:00:04 -0800130
Gunnar Mills57d9c502018-09-14 14:42:34 -0500131 /* @brief creates the dbus object(IPaddres) given in the address list.
132 * @param[in] addrs - address list for which dbus objects needs
133 * to create.
134 */
135 void createIPAddressObjects();
Ratan Gupta29b0e432017-05-25 12:51:40 +0530136
William A. Kennington III08505792019-01-30 16:00:04 -0800137 /* @brief creates the dbus object(Neighbor) given in the neighbor list.
138 */
139 void createStaticNeighborObjects();
140
William A. Kennington III08505792019-01-30 16:00:04 -0800141 /* @brief Gets all the static neighbor entries.
142 * @returns Static neighbor map.
143 */
William A. Kennington III9a20a6e2022-10-05 13:41:12 -0700144 inline const auto& getStaticNeighbors() const
William A. Kennington III08505792019-01-30 16:00:04 -0800145 {
146 return staticNeighbors;
147 }
148
Gunnar Mills57d9c502018-09-14 14:42:34 -0500149 /** Set value of DHCPEnabled */
William A. Kennington III8060c0d2022-08-18 19:19:34 -0700150 DHCPConf dhcpEnabled() const override;
Patrick Williams6aef7692021-05-01 06:39:41 -0500151 DHCPConf dhcpEnabled(DHCPConf value) override;
William A. Kennington III8060c0d2022-08-18 19:19:34 -0700152 using EthernetInterfaceIntf::dhcp4;
153 bool dhcp4(bool value) override;
154 using EthernetInterfaceIntf::dhcp6;
155 bool dhcp6(bool value) override;
Ratan Gupta87c13982017-06-15 09:27:27 +0530156
William A. Kennington III59e5b912022-11-02 02:49:46 -0700157 inline bool dhcpIsEnabled(in_addr) const
158 {
159 return dhcp4();
160 }
161 inline bool dhcpIsEnabled(in6_addr) const
162 {
163 return dhcp6();
164 }
165 inline bool dhcpIsEnabled(InAddrAny addr) const
166 {
167 return std::visit([&](auto v) { return dhcpIsEnabled(v); }, addr);
168 }
169
Johnathan Manteyfaa72e52020-01-08 10:38:58 -0800170 /** Retrieve Link State */
171 bool linkUp() const override;
172
Tejas Patil2c0fc562021-08-03 19:13:46 +0530173 /** Retrieve MTU Size */
174 size_t mtu() const override;
175
176 /** Set size of MTU */
177 size_t mtu(size_t value) override;
178
Johnathan Manteyd0679f92019-10-29 16:20:28 -0700179 /** Set value of NICEnabled */
Patrick Williams6aef7692021-05-01 06:39:41 -0500180 bool nicEnabled(bool value) override;
Johnathan Manteyd0679f92019-10-29 16:20:28 -0700181
Gunnar Mills57d9c502018-09-14 14:42:34 -0500182 /** @brief sets the MAC address.
183 * @param[in] value - MAC address which needs to be set on the system.
184 * @returns macAddress of the interface or throws an error.
185 */
Patrick Williams6aef7692021-05-01 06:39:41 -0500186 std::string macAddress(std::string value) override;
Ratan Guptabd303b12017-08-18 17:10:07 +0530187
Johnathan Mantey5b023f52019-06-24 16:06:37 -0700188 /** @brief check conf file for Router Advertisements
189 *
190 */
Patrick Williams6aef7692021-05-01 06:39:41 -0500191 bool ipv6AcceptRA(bool value) override;
William A. Kennington III8060c0d2022-08-18 19:19:34 -0700192 using EthernetInterfaceIntf::ipv6AcceptRA;
Johnathan Mantey5b023f52019-06-24 16:06:37 -0700193
Gunnar Mills57d9c502018-09-14 14:42:34 -0500194 /** @brief sets the NTP servers.
195 * @param[in] value - vector of NTP servers.
196 */
Patrick Williams6aef7692021-05-01 06:39:41 -0500197 ServerList ntpServers(ServerList value) override;
Ratan Gupta497c0c92017-08-22 19:15:59 +0530198
Asmitha Karunanithi003b8b72022-01-06 04:17:59 -0600199 /** @brief sets the static NTP servers.
200 * @param[in] value - vector of NTP servers.
201 */
202 ServerList staticNTPServers(ServerList value) override;
203
Manojkiran Edaacd6dd52019-10-15 15:00:51 +0530204 /** @brief sets the Static DNS/nameservers.
205 * @param[in] value - vector of DNS servers.
206 */
207
208 ServerList staticNameServers(ServerList value) override;
209
Gunnar Mills57d9c502018-09-14 14:42:34 -0500210 /** @brief create Vlan interface.
211 * @param[in] id- VLAN identifier.
212 */
William A. Kennington IIId298f932022-10-17 14:31:38 -0700213 ObjectPath createVLAN(uint16_t id);
Ratan Gupta5978dd12017-07-25 13:47:13 +0530214
Gunnar Mills57d9c502018-09-14 14:42:34 -0500215 /** @brief write the network conf file with the in-memory objects.
216 */
217 void writeConfigurationFile();
Ratan Gupta2b106532017-07-25 16:05:02 +0530218
Gunnar Mills57d9c502018-09-14 14:42:34 -0500219 /** @brief delete all dbus objects.
220 */
221 void deleteAll();
Ratan Guptae9c9b812017-09-22 17:15:37 +0530222
Ravi Tejaa5a09442020-07-17 00:57:33 -0500223 /** @brief set the default v4 gateway of the interface.
224 * @param[in] gateway - default v4 gateway of the interface.
225 */
226 std::string defaultGateway(std::string gateway) override;
227
228 /** @brief set the default v6 gateway of the interface.
229 * @param[in] gateway - default v6 gateway of the interface.
230 */
231 std::string defaultGateway6(std::string gateway) override;
232
Gunnar Mills57d9c502018-09-14 14:42:34 -0500233 using EthernetInterfaceIntf::interfaceName;
Johnathan Manteyfaa72e52020-01-08 10:38:58 -0800234 using EthernetInterfaceIntf::linkUp;
Tejas Patil2c0fc562021-08-03 19:13:46 +0530235 using EthernetInterfaceIntf::mtu;
Patrick Williams6aef7692021-05-01 06:39:41 -0500236 using EthernetInterfaceIntf::nicEnabled;
237 using MacAddressIntf::macAddress;
Ratan Gupta5978dd12017-07-25 13:47:13 +0530238
Ravi Tejaa5a09442020-07-17 00:57:33 -0500239 using EthernetInterfaceIntf::defaultGateway;
240 using EthernetInterfaceIntf::defaultGateway6;
Ratan Gupta6dec3902017-08-20 15:28:12 +0530241
Gunnar Mills57d9c502018-09-14 14:42:34 -0500242 protected:
Gunnar Mills57d9c502018-09-14 14:42:34 -0500243 /** @brief construct the ip address dbus object path.
244 * @param[in] addressType - Type of ip address.
Patrick Williams6aef7692021-05-01 06:39:41 -0500245 * @param[in] ipAddress - IP address.
Gunnar Mills57d9c502018-09-14 14:42:34 -0500246 * @param[in] prefixLength - Length of prefix.
Lei YU34027572021-08-11 15:23:52 +0800247 * @param[in] origin - The origin entry of the IP::Address
Ratan Gupta65e5abe2017-05-23 13:20:44 +0530248
Gunnar Mills57d9c502018-09-14 14:42:34 -0500249 * @return path of the address object.
250 */
Gunnar Mills57d9c502018-09-14 14:42:34 -0500251 std::string generateObjectPath(IP::Protocol addressType,
William A. Kennington IIIbe3bd2f2022-10-11 14:11:27 -0700252 std::string_view ipAddress,
Gunnar Mills57d9c502018-09-14 14:42:34 -0500253 uint8_t prefixLength,
Lei YU34027572021-08-11 15:23:52 +0800254 IP::AddressOrigin origin) const;
Ratan Gupta82549cc2017-04-21 08:45:23 +0530255
William A. Kennington III08505792019-01-30 16:00:04 -0800256 std::string
William A. Kennington IIIbe3bd2f2022-10-11 14:11:27 -0700257 generateStaticNeighborObjectPath(std::string_view ipAddress,
258 std::string_view macAddress) const;
William A. Kennington III08505792019-01-30 16:00:04 -0800259
Asmitha Karunanithi003b8b72022-01-06 04:17:59 -0600260 /** @brief get the NTP server list from the timsyncd dbus obj
Gunnar Mills57d9c502018-09-14 14:42:34 -0500261 *
262 */
Asmitha Karunanithi003b8b72022-01-06 04:17:59 -0600263 virtual ServerList getNTPServerFromTimeSyncd();
Ratan Gupta497c0c92017-08-22 19:15:59 +0530264
Gunnar Mills57d9c502018-09-14 14:42:34 -0500265 /** @brief get the name server details from the network conf
266 *
267 */
Manojkiran Edaacd6dd52019-10-15 15:00:51 +0530268 virtual ServerList getNameServerFromResolvd();
Ratan Gupta6dec3902017-08-20 15:28:12 +0530269
Gunnar Mills57d9c502018-09-14 14:42:34 -0500270 /** @brief Persistent sdbusplus DBus bus connection. */
Patrick Williamsc38b0712022-07-22 19:26:54 -0500271 sdbusplus::bus_t& bus;
Ratan Gupta82549cc2017-04-21 08:45:23 +0530272
William A. Kennington III08505792019-01-30 16:00:04 -0800273 /** @brief Persistent map of Neighbor dbus objects and their names */
William A. Kennington III9a20a6e2022-10-05 13:41:12 -0700274 string_umap<std::unique_ptr<Neighbor>> staticNeighbors;
William A. Kennington III08505792019-01-30 16:00:04 -0800275
Gunnar Mills57d9c502018-09-14 14:42:34 -0500276 /** @brief Dbus object path */
277 std::string objPath;
Ratan Gupta47722dc2017-05-26 18:32:23 +0530278
William A. Kennington III2e09d272022-10-14 17:15:00 -0700279 /** @brief Interface index */
280 unsigned ifIdx;
281
William A. Kennington III09f3a4a2022-10-25 02:59:16 -0700282 struct VlanProperties : VlanIfaces
283 {
284 VlanProperties(sdbusplus::bus_t& bus, stdplus::const_zstring objPath,
285 const system::InterfaceInfo& info,
286 EthernetInterface& eth, bool emitSignal = true);
287 void delete_() override;
288 unsigned parentIdx;
289 EthernetInterface& eth;
290 };
291 std::optional<VlanProperties> vlan;
292
Gunnar Mills57d9c502018-09-14 14:42:34 -0500293 friend class TestEthernetInterface;
William A. Kennington III09f3a4a2022-10-25 02:59:16 -0700294 friend class TestNetworkManager;
Johnathan Mantey817012a2020-01-30 15:07:39 -0800295
296 private:
William A. Kennington IIId298f932022-10-17 14:31:38 -0700297 EthernetInterface(sdbusplus::bus_t& bus, Manager& manager,
William A. Kennington IIIfd862be2022-10-09 18:40:55 -0700298 const system::InterfaceInfo& info, std::string&& objPath,
William A. Kennington IIId298f932022-10-17 14:31:38 -0700299 const config::Parser& config, bool emitSignal,
300 std::optional<bool> enabled);
301
Johnathan Mantey817012a2020-01-30 15:07:39 -0800302 /** @brief Determines if the address is manually assigned
303 * @param[in] origin - The origin entry of the IP::Address
304 * @returns true/false value if the address is static
305 */
306 bool originIsManuallyAssigned(IP::AddressOrigin origin);
William A. Kennington III26275a32021-07-13 20:32:42 -0700307
308 /** @brief Determines if the NIC is enabled in systemd
309 * @returns true/false value if the NIC is enabled
310 */
311 bool queryNicEnabled() const;
Ratan Gupta8c834932017-04-14 16:30:24 +0530312};
313
314} // namespace network
315} // namespace phosphor