blob: 31405286f54a253e16505eed1605d7a576f9ce29 [file] [log] [blame]
Ratan Gupta8c834932017-04-14 16:30:24 +05301#pragma once
Jishnu CM57dfea92023-05-05 06:07:26 -05002#include "dhcp_configuration.hpp"
William A. Kennington III9a20a6e2022-10-05 13:41:12 -07003#include "ipaddress.hpp"
4#include "neighbor.hpp"
Ravi Tejaab27a812023-05-02 10:01:53 -05005#include "static_gateway.hpp"
Ratan Gupta82549cc2017-04-21 08:45:23 +05306#include "types.hpp"
Ratan Gupta82549cc2017-04-21 08:45:23 +05307#include "xyz/openbmc_project/Network/IP/Create/server.hpp"
William A. Kennington III08505792019-01-30 16:00:04 -08008#include "xyz/openbmc_project/Network/Neighbor/CreateStatic/server.hpp"
Ravi Tejaab27a812023-05-02 10:01:53 -05009#include "xyz/openbmc_project/Network/StaticGateway/Create/server.hpp"
Ratan Gupta8c834932017-04-14 16:30:24 +053010
11#include <sdbusplus/bus.hpp>
12#include <sdbusplus/server/object.hpp>
William A. Kennington III9ede1b72022-11-21 01:59:28 -080013#include <stdplus/pinned.hpp>
Jishnu CM57dfea92023-05-05 06:07:26 -050014#include <stdplus/str/maps.hpp>
William A. Kennington IIIbe3bd2f2022-10-11 14:11:27 -070015#include <stdplus/zstring_view.hpp>
Patrick Venture189d44e2018-07-09 12:30:59 -070016#include <xyz/openbmc_project/Collection/DeleteAll/server.hpp>
17#include <xyz/openbmc_project/Network/EthernetInterface/server.hpp>
18#include <xyz/openbmc_project/Network/MACAddress/server.hpp>
Ravi Tejaab27a812023-05-02 10:01:53 -050019#include <xyz/openbmc_project/Network/StaticGateway/server.hpp>
William A. Kennington III09f3a4a2022-10-25 02:59:16 -070020#include <xyz/openbmc_project/Network/VLAN/server.hpp>
21#include <xyz/openbmc_project/Object/Delete/server.hpp>
Ratan Gupta8c834932017-04-14 16:30:24 +053022
Patrick Williams89d734b2023-05-10 07:50:25 -050023#include <optional>
24#include <string>
25#include <vector>
26
Ratan Gupta8c834932017-04-14 16:30:24 +053027namespace phosphor
28{
29namespace network
30{
Ratan Gupta8c834932017-04-14 16:30:24 +053031
Patrick Williamsc38b0712022-07-22 19:26:54 -050032using Ifaces = sdbusplus::server::object_t<
Gunnar Mills57d9c502018-09-14 14:42:34 -050033 sdbusplus::xyz::openbmc_project::Network::server::EthernetInterface,
34 sdbusplus::xyz::openbmc_project::Network::server::MACAddress,
35 sdbusplus::xyz::openbmc_project::Network::IP::server::Create,
William A. Kennington III08505792019-01-30 16:00:04 -080036 sdbusplus::xyz::openbmc_project::Network::Neighbor::server::CreateStatic,
Ravi Tejaab27a812023-05-02 10:01:53 -050037 sdbusplus::xyz::openbmc_project::Network::StaticGateway::server::Create,
Gunnar Mills57d9c502018-09-14 14:42:34 -050038 sdbusplus::xyz::openbmc_project::Collection::server::DeleteAll>;
Ratan Gupta8c834932017-04-14 16:30:24 +053039
William A. Kennington III09f3a4a2022-10-25 02:59:16 -070040using VlanIfaces = sdbusplus::server::object_t<
41 sdbusplus::xyz::openbmc_project::Object::server::Delete,
42 sdbusplus::xyz::openbmc_project::Network::server::VLAN>;
43
44using VlanIntf = sdbusplus::xyz::openbmc_project::Network::server::VLAN;
45
Ratan Gupta82549cc2017-04-21 08:45:23 +053046using IP = sdbusplus::xyz::openbmc_project::Network::server::IP;
Ratan Gupta87c13982017-06-15 09:27:27 +053047
48using EthernetInterfaceIntf =
49 sdbusplus::xyz::openbmc_project::Network::server::EthernetInterface;
Ratan Guptabd303b12017-08-18 17:10:07 +053050using MacAddressIntf =
51 sdbusplus::xyz::openbmc_project::Network::server::MACAddress;
Ravi Tejaab27a812023-05-02 10:01:53 -050052using StaticGatewayIntf =
53 sdbusplus::xyz::openbmc_project::Network::server::StaticGateway;
Ratan Gupta87c13982017-06-15 09:27:27 +053054
Ratan Gupta497c0c92017-08-22 19:15:59 +053055using ServerList = std::vector<std::string>;
raviteja-bce379562019-03-28 05:59:36 -050056using ObjectPath = sdbusplus::message::object_path;
Ratan Gupta497c0c92017-08-22 19:15:59 +053057
William A. Kennington III9a20a6e2022-10-05 13:41:12 -070058class Manager;
Ratan Gupta8c834932017-04-14 16:30:24 +053059
Ratan Gupta47722dc2017-05-26 18:32:23 +053060class TestEthernetInterface;
William A. Kennington III09f3a4a2022-10-25 02:59:16 -070061class TestNetworkManager;
Ratan Gupta5978dd12017-07-25 13:47:13 +053062
William A. Kennington III95530ec2022-08-19 01:44:39 -070063namespace config
64{
65class Parser;
66}
William A. Kennington IIId298f932022-10-17 14:31:38 -070067
Ratan Gupta8c834932017-04-14 16:30:24 +053068/** @class EthernetInterface
69 * @brief OpenBMC Ethernet Interface implementation.
70 * @details A concrete implementation for the
71 * xyz.openbmc_project.Network.EthernetInterface DBus API.
72 */
Ratan Gupta82549cc2017-04-21 08:45:23 +053073class EthernetInterface : public Ifaces
Ratan Gupta8c834932017-04-14 16:30:24 +053074{
Gunnar Mills57d9c502018-09-14 14:42:34 -050075 public:
Gunnar Mills57d9c502018-09-14 14:42:34 -050076 EthernetInterface(EthernetInterface&&) = delete;
77 EthernetInterface& operator=(EthernetInterface&&) = delete;
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.
William A. Kennington IIId298f932022-10-17 14:31:38 -070081 * @param[in] manager - parent object.
82 * @param[in] info - Interface information.
83 * @param[in] objRoot - Path to attach at.
Manojkiran Edad92826d2024-06-17 14:43:14 +053084 * @param[in] config - The parsed configuration file.
William A. Kennington III09f3a4a2022-10-25 02:59:16 -070085 * @param[in] vlan - The id of the vlan if configured
William A. Kennington III80d29012022-11-12 02:31:40 -080086 * @param[in] enabled - Determine if systemd-networkd is managing this link
Gunnar Mills57d9c502018-09-14 14:42:34 -050087 */
William A. Kennington III9ede1b72022-11-21 01:59:28 -080088 EthernetInterface(stdplus::PinnedRef<sdbusplus::bus_t> bus,
89 stdplus::PinnedRef<Manager> manager,
William A. Kennington III13d665c2022-11-15 20:34:40 -080090 const AllIntfInfo& info, std::string_view objRoot,
William A. Kennington IIIb6452542022-11-15 18:09:12 -080091 const config::Parser& config, bool enabled);
Ratan Gupta82549cc2017-04-21 08:45:23 +053092
William A. Kennington III59e5b912022-11-02 02:49:46 -070093 /** @brief Network Manager object. */
William A. Kennington III9ede1b72022-11-21 01:59:28 -080094 stdplus::PinnedRef<Manager> manager;
William A. Kennington III59e5b912022-11-02 02:49:46 -070095
96 /** @brief Persistent map of IPAddress dbus objects and their names */
William A. Kennington III9b2a20d2023-06-17 14:05:48 -070097 std::unordered_map<stdplus::SubnetAny, std::unique_ptr<IPAddress>> addrs;
William A. Kennington III59e5b912022-11-02 02:49:46 -070098
William A. Kennington III434a9432022-11-04 18:38:46 -070099 /** @brief Persistent map of Neighbor dbus objects and their names */
William A. Kennington III9b2a20d2023-06-17 14:05:48 -0700100 std::unordered_map<stdplus::InAnyAddr, std::unique_ptr<Neighbor>>
101 staticNeighbors;
William A. Kennington III434a9432022-11-04 18:38:46 -0700102
Ravi Tejaab27a812023-05-02 10:01:53 -0500103 /** @brief Persistent map of static route dbus objects and their names */
104 std::unordered_map<std::string, std::unique_ptr<StaticGateway>>
105 staticGateways;
106
William A. Kennington IIId6f53402022-11-07 14:48:53 -0800107 void addAddr(const AddressInfo& info);
William A. Kennington IIIbc2502c2022-11-07 16:31:54 -0800108 void addStaticNeigh(const NeighborInfo& info);
Ravi Tejaab27a812023-05-02 10:01:53 -0500109 void addStaticGateway(const StaticGatewayInfo& info);
William A. Kennington IIId6f53402022-11-07 14:48:53 -0800110
William A. Kennington IIId298f932022-10-17 14:31:38 -0700111 /** @brief Updates the interface information based on new InterfaceInfo */
William A. Kennington IIId99e6db2022-11-15 20:39:45 -0800112 void updateInfo(const InterfaceInfo& info, bool skipSignal = false);
William A. Kennington IIId298f932022-10-17 14:31:38 -0700113
Asmitha Karunanithi003b8b72022-01-06 04:17:59 -0600114 /** @brief Function used to load the ntpservers
115 */
116 void loadNTPServers(const config::Parser& config);
117
Manojkiran Edaacd6dd52019-10-15 15:00:51 +0530118 /** @brief Function used to load the nameservers.
119 */
William A. Kennington IIIa520a392022-08-08 12:17:34 -0700120 void loadNameServers(const config::Parser& config);
Manojkiran Edaacd6dd52019-10-15 15:00:51 +0530121
Patrick Williams6aef7692021-05-01 06:39:41 -0500122 /** @brief Function to create ipAddress dbus object.
Gunnar Mills57d9c502018-09-14 14:42:34 -0500123 * @param[in] addressType - Type of ip address.
Patrick Williams6aef7692021-05-01 06:39:41 -0500124 * @param[in] ipAddress- IP address.
Gunnar Mills57d9c502018-09-14 14:42:34 -0500125 * @param[in] prefixLength - Length of prefix.
Gunnar Mills57d9c502018-09-14 14:42:34 -0500126 */
Ratan Gupta82549cc2017-04-21 08:45:23 +0530127
Patrick Williams6aef7692021-05-01 06:39:41 -0500128 ObjectPath ip(IP::Protocol addressType, std::string ipAddress,
William A. Kennington IIIe25f8b42022-10-11 14:43:28 -0700129 uint8_t prefixLength, std::string) override;
Ratan Gupta8c834932017-04-14 16:30:24 +0530130
William A. Kennington III08505792019-01-30 16:00:04 -0800131 /** @brief Function to create static neighbor dbus object.
132 * @param[in] ipAddress - IP address.
133 * @param[in] macAddress - Low level MAC address.
134 */
Patrick Williams6aef7692021-05-01 06:39:41 -0500135 ObjectPath neighbor(std::string ipAddress, std::string macAddress) override;
William A. Kennington III08505792019-01-30 16:00:04 -0800136
Ravi Tejaab27a812023-05-02 10:01:53 -0500137 /** @brief Function to create static route dbus object.
138 * @param[in] destination - Destination IP address.
139 * @param[in] gateway - Gateway
140 * @parma[in] prefixLength - Number of network bits.
141 */
142 ObjectPath staticGateway(std::string gateway,
143 IP::Protocol protocolType) override;
144
Gunnar Mills57d9c502018-09-14 14:42:34 -0500145 /** Set value of DHCPEnabled */
William A. Kennington III8060c0d2022-08-18 19:19:34 -0700146 DHCPConf dhcpEnabled() const override;
Patrick Williams6aef7692021-05-01 06:39:41 -0500147 DHCPConf dhcpEnabled(DHCPConf value) override;
William A. Kennington III8060c0d2022-08-18 19:19:34 -0700148 using EthernetInterfaceIntf::dhcp4;
149 bool dhcp4(bool value) override;
150 using EthernetInterfaceIntf::dhcp6;
151 bool dhcp6(bool value) override;
Ratan Gupta87c13982017-06-15 09:27:27 +0530152
William A. Kennington III9b2a20d2023-06-17 14:05:48 -0700153 inline bool dhcpIsEnabled(stdplus::In4Addr) const
William A. Kennington III59e5b912022-11-02 02:49:46 -0700154 {
155 return dhcp4();
156 }
William A. Kennington III9b2a20d2023-06-17 14:05:48 -0700157 inline bool dhcpIsEnabled(stdplus::In6Addr) const
William A. Kennington III59e5b912022-11-02 02:49:46 -0700158 {
159 return dhcp6();
160 }
William A. Kennington III9b2a20d2023-06-17 14:05:48 -0700161 inline bool dhcpIsEnabled(stdplus::InAnyAddr addr) const
William A. Kennington III59e5b912022-11-02 02:49:46 -0700162 {
163 return std::visit([&](auto v) { return dhcpIsEnabled(v); }, addr);
164 }
165
Tejas Patil2c0fc562021-08-03 19:13:46 +0530166 /** Set size of MTU */
167 size_t mtu(size_t value) override;
168
Johnathan Manteyd0679f92019-10-29 16:20:28 -0700169 /** Set value of NICEnabled */
Patrick Williams6aef7692021-05-01 06:39:41 -0500170 bool nicEnabled(bool value) override;
Johnathan Manteyd0679f92019-10-29 16:20:28 -0700171
Gunnar Mills57d9c502018-09-14 14:42:34 -0500172 /** @brief sets the MAC address.
173 * @param[in] value - MAC address which needs to be set on the system.
174 * @returns macAddress of the interface or throws an error.
175 */
Patrick Williams6aef7692021-05-01 06:39:41 -0500176 std::string macAddress(std::string value) override;
Ratan Guptabd303b12017-08-18 17:10:07 +0530177
Johnathan Mantey5b023f52019-06-24 16:06:37 -0700178 /** @brief check conf file for Router Advertisements
179 *
180 */
Patrick Williams6aef7692021-05-01 06:39:41 -0500181 bool ipv6AcceptRA(bool value) override;
William A. Kennington III8060c0d2022-08-18 19:19:34 -0700182 using EthernetInterfaceIntf::ipv6AcceptRA;
Johnathan Mantey5b023f52019-06-24 16:06:37 -0700183
Gunnar Mills57d9c502018-09-14 14:42:34 -0500184 /** @brief sets the NTP servers.
185 * @param[in] value - vector of NTP servers.
186 */
Patrick Williams6aef7692021-05-01 06:39:41 -0500187 ServerList ntpServers(ServerList value) override;
Ratan Gupta497c0c92017-08-22 19:15:59 +0530188
Asmitha Karunanithi003b8b72022-01-06 04:17:59 -0600189 /** @brief sets the static NTP servers.
190 * @param[in] value - vector of NTP servers.
191 */
192 ServerList staticNTPServers(ServerList value) override;
193
Ravi Teja4f8b9a02023-09-06 13:41:52 -0500194 /** @brief Get value of nameservers */
195 ServerList nameservers() const override;
196
Manojkiran Edaacd6dd52019-10-15 15:00:51 +0530197 /** @brief sets the Static DNS/nameservers.
198 * @param[in] value - vector of DNS servers.
199 */
200
201 ServerList staticNameServers(ServerList value) override;
202
Gunnar Mills57d9c502018-09-14 14:42:34 -0500203 /** @brief create Vlan interface.
204 * @param[in] id- VLAN identifier.
205 */
William A. Kennington IIId298f932022-10-17 14:31:38 -0700206 ObjectPath createVLAN(uint16_t id);
Ratan Gupta5978dd12017-07-25 13:47:13 +0530207
Gunnar Mills57d9c502018-09-14 14:42:34 -0500208 /** @brief write the network conf file with the in-memory objects.
209 */
210 void writeConfigurationFile();
Ratan Gupta2b106532017-07-25 16:05:02 +0530211
Gunnar Mills57d9c502018-09-14 14:42:34 -0500212 /** @brief delete all dbus objects.
213 */
William A. Kennington IIIaf77f822023-06-23 02:17:20 -0700214 void deleteAll() override;
Ratan Guptae9c9b812017-09-22 17:15:37 +0530215
Ravi Tejaa5a09442020-07-17 00:57:33 -0500216 /** @brief set the default v4 gateway of the interface.
217 * @param[in] gateway - default v4 gateway of the interface.
218 */
219 std::string defaultGateway(std::string gateway) override;
220
221 /** @brief set the default v6 gateway of the interface.
222 * @param[in] gateway - default v6 gateway of the interface.
223 */
224 std::string defaultGateway6(std::string gateway) override;
225
Jishnu CM57dfea92023-05-05 06:07:26 -0500226 /** @brief Function to reload network configurations.
227 */
228 void reloadConfigs();
229
Gunnar Mills57d9c502018-09-14 14:42:34 -0500230 using EthernetInterfaceIntf::interfaceName;
Johnathan Manteyfaa72e52020-01-08 10:38:58 -0800231 using EthernetInterfaceIntf::linkUp;
Tejas Patil2c0fc562021-08-03 19:13:46 +0530232 using EthernetInterfaceIntf::mtu;
Patrick Williams6aef7692021-05-01 06:39:41 -0500233 using EthernetInterfaceIntf::nicEnabled;
234 using MacAddressIntf::macAddress;
Ratan Gupta5978dd12017-07-25 13:47:13 +0530235
Ravi Tejaa5a09442020-07-17 00:57:33 -0500236 using EthernetInterfaceIntf::defaultGateway;
237 using EthernetInterfaceIntf::defaultGateway6;
Ratan Gupta6dec3902017-08-20 15:28:12 +0530238
Gunnar Mills57d9c502018-09-14 14:42:34 -0500239 protected:
Asmitha Karunanithi003b8b72022-01-06 04:17:59 -0600240 /** @brief get the NTP server list from the timsyncd dbus obj
Gunnar Mills57d9c502018-09-14 14:42:34 -0500241 *
242 */
Asmitha Karunanithi003b8b72022-01-06 04:17:59 -0600243 virtual ServerList getNTPServerFromTimeSyncd();
Ratan Gupta497c0c92017-08-22 19:15:59 +0530244
Gunnar Mills57d9c502018-09-14 14:42:34 -0500245 /** @brief get the name server details from the network conf
246 *
247 */
Ravi Teja4f8b9a02023-09-06 13:41:52 -0500248 virtual ServerList getNameServerFromResolvd() const;
Ratan Gupta6dec3902017-08-20 15:28:12 +0530249
Gunnar Mills57d9c502018-09-14 14:42:34 -0500250 /** @brief Persistent sdbusplus DBus bus connection. */
William A. Kennington III9ede1b72022-11-21 01:59:28 -0800251 stdplus::PinnedRef<sdbusplus::bus_t> bus;
Ratan Gupta82549cc2017-04-21 08:45:23 +0530252
Gunnar Mills57d9c502018-09-14 14:42:34 -0500253 /** @brief Dbus object path */
254 std::string objPath;
Ratan Gupta47722dc2017-05-26 18:32:23 +0530255
William A. Kennington III2e09d272022-10-14 17:15:00 -0700256 /** @brief Interface index */
257 unsigned ifIdx;
258
William A. Kennington III09f3a4a2022-10-25 02:59:16 -0700259 struct VlanProperties : VlanIfaces
260 {
261 VlanProperties(sdbusplus::bus_t& bus, stdplus::const_zstring objPath,
William A. Kennington III9ede1b72022-11-21 01:59:28 -0800262 const InterfaceInfo& info,
263 stdplus::PinnedRef<EthernetInterface> eth);
William A. Kennington III09f3a4a2022-10-25 02:59:16 -0700264 void delete_() override;
265 unsigned parentIdx;
William A. Kennington III9ede1b72022-11-21 01:59:28 -0800266 stdplus::PinnedRef<EthernetInterface> eth;
William A. Kennington III09f3a4a2022-10-25 02:59:16 -0700267 };
268 std::optional<VlanProperties> vlan;
269
William A. Kennington IIIf179e702024-04-17 00:50:28 -0700270 std::optional<dhcp::Configuration> dhcp4Conf, dhcp6Conf;
271
Gunnar Mills57d9c502018-09-14 14:42:34 -0500272 friend class TestEthernetInterface;
William A. Kennington III09f3a4a2022-10-25 02:59:16 -0700273 friend class TestNetworkManager;
Johnathan Mantey817012a2020-01-30 15:07:39 -0800274
275 private:
William A. Kennington III9ede1b72022-11-21 01:59:28 -0800276 EthernetInterface(stdplus::PinnedRef<sdbusplus::bus_t> bus,
277 stdplus::PinnedRef<Manager> manager,
William A. Kennington III13d665c2022-11-15 20:34:40 -0800278 const AllIntfInfo& info, std::string&& objPath,
William A. Kennington IIIb6452542022-11-15 18:09:12 -0800279 const config::Parser& config, bool enabled);
Ratan Gupta8c834932017-04-14 16:30:24 +0530280};
281
282} // namespace network
283} // namespace phosphor