blob: a4b127cfcc80f5c023e0aed4817d7fcd5ee36fa5 [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
8#include <sdbusplus/bus.hpp>
9#include <sdbusplus/server/object.hpp>
William A. Kennington IIIbe3bd2f2022-10-11 14:11:27 -070010#include <stdplus/zstring.hpp>
11#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>
Ratan Gupta8c834932017-04-14 16:30:24 +053017
18namespace phosphor
19{
20namespace network
21{
Ratan Gupta8c834932017-04-14 16:30:24 +053022
Patrick Williamsc38b0712022-07-22 19:26:54 -050023using Ifaces = sdbusplus::server::object_t<
Gunnar Mills57d9c502018-09-14 14:42:34 -050024 sdbusplus::xyz::openbmc_project::Network::server::EthernetInterface,
25 sdbusplus::xyz::openbmc_project::Network::server::MACAddress,
26 sdbusplus::xyz::openbmc_project::Network::IP::server::Create,
William A. Kennington III08505792019-01-30 16:00:04 -080027 sdbusplus::xyz::openbmc_project::Network::Neighbor::server::CreateStatic,
Gunnar Mills57d9c502018-09-14 14:42:34 -050028 sdbusplus::xyz::openbmc_project::Collection::server::DeleteAll>;
Ratan Gupta8c834932017-04-14 16:30:24 +053029
Ratan Gupta82549cc2017-04-21 08:45:23 +053030using IP = sdbusplus::xyz::openbmc_project::Network::server::IP;
Ratan Gupta87c13982017-06-15 09:27:27 +053031
32using EthernetInterfaceIntf =
33 sdbusplus::xyz::openbmc_project::Network::server::EthernetInterface;
Ratan Guptabd303b12017-08-18 17:10:07 +053034using MacAddressIntf =
35 sdbusplus::xyz::openbmc_project::Network::server::MACAddress;
Ratan Gupta87c13982017-06-15 09:27:27 +053036
Ratan Gupta497c0c92017-08-22 19:15:59 +053037using ServerList = std::vector<std::string>;
raviteja-bce379562019-03-28 05:59:36 -050038using ObjectPath = sdbusplus::message::object_path;
Ratan Gupta497c0c92017-08-22 19:15:59 +053039
William A. Kennington III4ee7a7e2022-10-11 16:37:22 -070040using VlanId = uint32_t;
41
William A. Kennington III9a20a6e2022-10-05 13:41:12 -070042class Manager;
Ratan Gupta8c834932017-04-14 16:30:24 +053043
Ratan Gupta47722dc2017-05-26 18:32:23 +053044class TestEthernetInterface;
45
Ratan Gupta5978dd12017-07-25 13:47:13 +053046class VlanInterface;
47
William A. Kennington III95530ec2022-08-19 01:44:39 -070048namespace config
49{
50class Parser;
51}
52
Ratan Gupta8c834932017-04-14 16:30:24 +053053/** @class EthernetInterface
54 * @brief OpenBMC Ethernet Interface implementation.
55 * @details A concrete implementation for the
56 * xyz.openbmc_project.Network.EthernetInterface DBus API.
57 */
Ratan Gupta82549cc2017-04-21 08:45:23 +053058class EthernetInterface : public Ifaces
Ratan Gupta8c834932017-04-14 16:30:24 +053059{
Gunnar Mills57d9c502018-09-14 14:42:34 -050060 public:
61 EthernetInterface() = delete;
62 EthernetInterface(const EthernetInterface&) = delete;
63 EthernetInterface& operator=(const EthernetInterface&) = delete;
64 EthernetInterface(EthernetInterface&&) = delete;
65 EthernetInterface& operator=(EthernetInterface&&) = delete;
66 virtual ~EthernetInterface() = default;
Ratan Gupta8c834932017-04-14 16:30:24 +053067
Gunnar Mills57d9c502018-09-14 14:42:34 -050068 /** @brief Constructor to put object onto bus at a dbus path.
69 * @param[in] bus - Bus to attach to.
70 * @param[in] objPath - Path to attach at.
William A. Kennington IIIa520a392022-08-08 12:17:34 -070071 * @param[in] config - The parsed configuation file.
Gunnar Mills57d9c502018-09-14 14:42:34 -050072 * @param[in] parent - parent object.
73 * @param[in] emitSignal - true if the object added signal needs to be
74 * send.
William A. Kennington III26275a32021-07-13 20:32:42 -070075 * @param[in] enabled - Override the lookup of nicEnabled
Gunnar Mills57d9c502018-09-14 14:42:34 -050076 */
William A. Kennington IIIbe3bd2f2022-10-11 14:11:27 -070077 EthernetInterface(sdbusplus::bus_t& bus, stdplus::zstring_view objPath,
William A. Kennington III0caf2212022-08-18 18:15:51 -070078 const config::Parser& config, Manager& parent,
79 bool emitSignal = true,
William A. Kennington III26275a32021-07-13 20:32:42 -070080 std::optional<bool> enabled = std::nullopt);
Ratan Gupta82549cc2017-04-21 08:45:23 +053081
Asmitha Karunanithi003b8b72022-01-06 04:17:59 -060082 /** @brief Function used to load the ntpservers
83 */
84 void loadNTPServers(const config::Parser& config);
85
Manojkiran Edaacd6dd52019-10-15 15:00:51 +053086 /** @brief Function used to load the nameservers.
87 */
William A. Kennington IIIa520a392022-08-08 12:17:34 -070088 void loadNameServers(const config::Parser& config);
Manojkiran Edaacd6dd52019-10-15 15:00:51 +053089
Patrick Williams6aef7692021-05-01 06:39:41 -050090 /** @brief Function to create ipAddress dbus object.
Gunnar Mills57d9c502018-09-14 14:42:34 -050091 * @param[in] addressType - Type of ip address.
Patrick Williams6aef7692021-05-01 06:39:41 -050092 * @param[in] ipAddress- IP address.
Gunnar Mills57d9c502018-09-14 14:42:34 -050093 * @param[in] prefixLength - Length of prefix.
Gunnar Mills57d9c502018-09-14 14:42:34 -050094 */
Ratan Gupta82549cc2017-04-21 08:45:23 +053095
Patrick Williams6aef7692021-05-01 06:39:41 -050096 ObjectPath ip(IP::Protocol addressType, std::string ipAddress,
William A. Kennington IIIe25f8b42022-10-11 14:43:28 -070097 uint8_t prefixLength, std::string) override;
Ratan Gupta8c834932017-04-14 16:30:24 +053098
William A. Kennington III08505792019-01-30 16:00:04 -080099 /** @brief Function to create static neighbor dbus object.
100 * @param[in] ipAddress - IP address.
101 * @param[in] macAddress - Low level MAC address.
102 */
Patrick Williams6aef7692021-05-01 06:39:41 -0500103 ObjectPath neighbor(std::string ipAddress, std::string macAddress) override;
William A. Kennington III08505792019-01-30 16:00:04 -0800104
Patrick Williams6aef7692021-05-01 06:39:41 -0500105 /* @brief delete the dbus object of the given ipAddress.
106 * @param[in] ipAddress - IP address.
Gunnar Mills57d9c502018-09-14 14:42:34 -0500107 */
William A. Kennington IIIbe3bd2f2022-10-11 14:11:27 -0700108 void deleteObject(std::string_view ipAddress);
Ratan Gupta8c834932017-04-14 16:30:24 +0530109
Patrick Williams6aef7692021-05-01 06:39:41 -0500110 /* @brief delete the dbus object of the given ipAddress.
111 * @param[in] ipAddress - IP address.
William A. Kennington III08505792019-01-30 16:00:04 -0800112 */
William A. Kennington IIIbe3bd2f2022-10-11 14:11:27 -0700113 void deleteStaticNeighborObject(std::string_view ipAddress);
William A. Kennington III08505792019-01-30 16:00:04 -0800114
Gunnar Mills57d9c502018-09-14 14:42:34 -0500115 /* @brief delete the vlan dbus object of the given interface.
116 * Also deletes the device file and the network file.
117 * @param[in] interface - VLAN Interface.
118 */
William A. Kennington IIIbe3bd2f2022-10-11 14:11:27 -0700119 void deleteVLANObject(stdplus::zstring_view interface);
Ratan Guptabc886292017-07-25 18:29:57 +0530120
Gunnar Mills57d9c502018-09-14 14:42:34 -0500121 /* @brief creates the dbus object(IPaddres) given in the address list.
122 * @param[in] addrs - address list for which dbus objects needs
123 * to create.
124 */
125 void createIPAddressObjects();
Ratan Gupta29b0e432017-05-25 12:51:40 +0530126
William A. Kennington III08505792019-01-30 16:00:04 -0800127 /* @brief creates the dbus object(Neighbor) given in the neighbor list.
128 */
129 void createStaticNeighborObjects();
130
William A. Kennington IIId7946a72019-04-19 14:24:09 -0700131 /* @brief Gets the index of the interface on the system
132 */
133 unsigned ifIndex() const;
134
Gunnar Mills57d9c502018-09-14 14:42:34 -0500135 /* @brief Gets all the ip addresses.
Patrick Williams6aef7692021-05-01 06:39:41 -0500136 * @returns the list of ipAddress.
Gunnar Mills57d9c502018-09-14 14:42:34 -0500137 */
William A. Kennington III9a20a6e2022-10-05 13:41:12 -0700138 inline const auto& getAddresses() const
Gunnar Mills57d9c502018-09-14 14:42:34 -0500139 {
140 return addrs;
141 }
Ratan Gupta8c834932017-04-14 16:30:24 +0530142
William A. Kennington III08505792019-01-30 16:00:04 -0800143 /* @brief Gets all the static neighbor entries.
144 * @returns Static neighbor map.
145 */
William A. Kennington III9a20a6e2022-10-05 13:41:12 -0700146 inline const auto& getStaticNeighbors() const
William A. Kennington III08505792019-01-30 16:00:04 -0800147 {
148 return staticNeighbors;
149 }
150
Gunnar Mills57d9c502018-09-14 14:42:34 -0500151 /** Set value of DHCPEnabled */
William A. Kennington III8060c0d2022-08-18 19:19:34 -0700152 DHCPConf dhcpEnabled() const override;
Patrick Williams6aef7692021-05-01 06:39:41 -0500153 DHCPConf dhcpEnabled(DHCPConf value) override;
William A. Kennington III8060c0d2022-08-18 19:19:34 -0700154 using EthernetInterfaceIntf::dhcp4;
155 bool dhcp4(bool value) override;
156 using EthernetInterfaceIntf::dhcp6;
157 bool dhcp6(bool value) override;
Ratan Gupta87c13982017-06-15 09:27:27 +0530158
Johnathan Manteyfaa72e52020-01-08 10:38:58 -0800159 /** Retrieve Link State */
160 bool linkUp() const override;
161
Tejas Patil2c0fc562021-08-03 19:13:46 +0530162 /** Retrieve MTU Size */
163 size_t mtu() const override;
164
165 /** Set size of MTU */
166 size_t mtu(size_t value) override;
167
Johnathan Manteyd0679f92019-10-29 16:20:28 -0700168 /** Set value of NICEnabled */
Patrick Williams6aef7692021-05-01 06:39:41 -0500169 bool nicEnabled(bool value) override;
Johnathan Manteyd0679f92019-10-29 16:20:28 -0700170
Gunnar Mills57d9c502018-09-14 14:42:34 -0500171 /** @brief sets the MAC address.
172 * @param[in] value - MAC address which needs to be set on the system.
173 * @returns macAddress of the interface or throws an error.
174 */
Patrick Williams6aef7692021-05-01 06:39:41 -0500175 std::string macAddress(std::string value) override;
Ratan Guptabd303b12017-08-18 17:10:07 +0530176
Johnathan Mantey5b023f52019-06-24 16:06:37 -0700177 /** @brief check conf file for Router Advertisements
178 *
179 */
Patrick Williams6aef7692021-05-01 06:39:41 -0500180 bool ipv6AcceptRA(bool value) override;
William A. Kennington III8060c0d2022-08-18 19:19:34 -0700181 using EthernetInterfaceIntf::ipv6AcceptRA;
Johnathan Mantey5b023f52019-06-24 16:06:37 -0700182
Gunnar Mills57d9c502018-09-14 14:42:34 -0500183 /** @brief sets the NTP servers.
184 * @param[in] value - vector of NTP servers.
185 */
Patrick Williams6aef7692021-05-01 06:39:41 -0500186 ServerList ntpServers(ServerList value) override;
Ratan Gupta497c0c92017-08-22 19:15:59 +0530187
Asmitha Karunanithi003b8b72022-01-06 04:17:59 -0600188 /** @brief sets the static NTP servers.
189 * @param[in] value - vector of NTP servers.
190 */
191 ServerList staticNTPServers(ServerList value) override;
192
Manojkiran Edaacd6dd52019-10-15 15:00:51 +0530193 /** @brief sets the Static DNS/nameservers.
194 * @param[in] value - vector of DNS servers.
195 */
196
197 ServerList staticNameServers(ServerList value) override;
198
Gunnar Mills57d9c502018-09-14 14:42:34 -0500199 /** @brief create Vlan interface.
200 * @param[in] id- VLAN identifier.
201 */
William A. Kennington IIIf4b4ff82019-04-09 19:06:52 -0700202 ObjectPath createVLAN(VlanId id);
Ratan Gupta5978dd12017-07-25 13:47:13 +0530203
Gunnar Mills57d9c502018-09-14 14:42:34 -0500204 /** @brief load the vlan info from the system
205 * and creates the ip address dbus objects.
206 * @param[in] vlanID- VLAN identifier.
207 */
208 void loadVLAN(VlanId vlanID);
Ratan Gupta92bc2fe2017-07-26 22:40:21 +0530209
Gunnar Mills57d9c502018-09-14 14:42:34 -0500210 /** @brief write the network conf file with the in-memory objects.
211 */
212 void writeConfigurationFile();
Ratan Gupta2b106532017-07-25 16:05:02 +0530213
Gunnar Mills57d9c502018-09-14 14:42:34 -0500214 /** @brief delete all dbus objects.
215 */
216 void deleteAll();
Ratan Guptae9c9b812017-09-22 17:15:37 +0530217
Ravi Tejaa5a09442020-07-17 00:57:33 -0500218 /** @brief set the default v4 gateway of the interface.
219 * @param[in] gateway - default v4 gateway of the interface.
220 */
221 std::string defaultGateway(std::string gateway) override;
222
223 /** @brief set the default v6 gateway of the interface.
224 * @param[in] gateway - default v6 gateway of the interface.
225 */
226 std::string defaultGateway6(std::string gateway) override;
227
Gunnar Mills57d9c502018-09-14 14:42:34 -0500228 using EthernetInterfaceIntf::interfaceName;
Johnathan Manteyfaa72e52020-01-08 10:38:58 -0800229 using EthernetInterfaceIntf::linkUp;
Tejas Patil2c0fc562021-08-03 19:13:46 +0530230 using EthernetInterfaceIntf::mtu;
Patrick Williams6aef7692021-05-01 06:39:41 -0500231 using EthernetInterfaceIntf::nicEnabled;
232 using MacAddressIntf::macAddress;
Ratan Gupta5978dd12017-07-25 13:47:13 +0530233
Ravi Tejaa5a09442020-07-17 00:57:33 -0500234 using EthernetInterfaceIntf::defaultGateway;
235 using EthernetInterfaceIntf::defaultGateway6;
Ratan Gupta6dec390f2017-08-20 15:28:12 +0530236
Gunnar Mills57d9c502018-09-14 14:42:34 -0500237 protected:
Gunnar Mills57d9c502018-09-14 14:42:34 -0500238 /* @brief delete the vlan interface from system.
239 * @param[in] interface - vlan Interface.
240 */
William A. Kennington IIIbe3bd2f2022-10-11 14:11:27 -0700241 void deleteVLANFromSystem(stdplus::zstring_view interface);
Ratan Guptae9c9b812017-09-22 17:15:37 +0530242
Gunnar Mills57d9c502018-09-14 14:42:34 -0500243 /** @brief get the mac address of the interface.
244 * @param[in] interfaceName - Network interface name.
245 * @return macaddress on success
246 */
Ratan Gupta8c834932017-04-14 16:30:24 +0530247
William A. Kennington IIIbe3bd2f2022-10-11 14:11:27 -0700248 std::string getMACAddress(stdplus::const_zstring interfaceName) const;
Ratan Gupta8c834932017-04-14 16:30:24 +0530249
Gunnar Mills57d9c502018-09-14 14:42:34 -0500250 /** @brief construct the ip address dbus object path.
251 * @param[in] addressType - Type of ip address.
Patrick Williams6aef7692021-05-01 06:39:41 -0500252 * @param[in] ipAddress - IP address.
Gunnar Mills57d9c502018-09-14 14:42:34 -0500253 * @param[in] prefixLength - Length of prefix.
Lei YU34027572021-08-11 15:23:52 +0800254 * @param[in] origin - The origin entry of the IP::Address
Ratan Gupta65e5abe2017-05-23 13:20:44 +0530255
Gunnar Mills57d9c502018-09-14 14:42:34 -0500256 * @return path of the address object.
257 */
Gunnar Mills57d9c502018-09-14 14:42:34 -0500258 std::string generateObjectPath(IP::Protocol addressType,
William A. Kennington IIIbe3bd2f2022-10-11 14:11:27 -0700259 std::string_view ipAddress,
Gunnar Mills57d9c502018-09-14 14:42:34 -0500260 uint8_t prefixLength,
Lei YU34027572021-08-11 15:23:52 +0800261 IP::AddressOrigin origin) const;
Ratan Gupta82549cc2017-04-21 08:45:23 +0530262
William A. Kennington III08505792019-01-30 16:00:04 -0800263 std::string
William A. Kennington IIIbe3bd2f2022-10-11 14:11:27 -0700264 generateStaticNeighborObjectPath(std::string_view ipAddress,
265 std::string_view macAddress) const;
William A. Kennington III08505792019-01-30 16:00:04 -0800266
Asmitha Karunanithi003b8b72022-01-06 04:17:59 -0600267 /** @brief get the NTP server list from the timsyncd dbus obj
Gunnar Mills57d9c502018-09-14 14:42:34 -0500268 *
269 */
Asmitha Karunanithi003b8b72022-01-06 04:17:59 -0600270 virtual ServerList getNTPServerFromTimeSyncd();
Ratan Gupta497c0c92017-08-22 19:15:59 +0530271
Gunnar Mills57d9c502018-09-14 14:42:34 -0500272 /** @brief get the name server details from the network conf
273 *
274 */
Manojkiran Edaacd6dd52019-10-15 15:00:51 +0530275 virtual ServerList getNameServerFromResolvd();
Ratan Gupta6dec390f2017-08-20 15:28:12 +0530276
Gunnar Mills57d9c502018-09-14 14:42:34 -0500277 /** @brief Persistent sdbusplus DBus bus connection. */
Patrick Williamsc38b0712022-07-22 19:26:54 -0500278 sdbusplus::bus_t& bus;
Ratan Gupta82549cc2017-04-21 08:45:23 +0530279
Gunnar Mills57d9c502018-09-14 14:42:34 -0500280 /** @brief Network Manager object. */
281 Manager& manager;
Ratan Gupta4f1c18b2017-05-25 12:59:35 +0530282
Gunnar Mills57d9c502018-09-14 14:42:34 -0500283 /** @brief Persistent map of IPAddress dbus objects and their names */
William A. Kennington III9a20a6e2022-10-05 13:41:12 -0700284 string_umap<std::unique_ptr<IPAddress>> addrs;
Ratan Gupta82549cc2017-04-21 08:45:23 +0530285
William A. Kennington III08505792019-01-30 16:00:04 -0800286 /** @brief Persistent map of Neighbor dbus objects and their names */
William A. Kennington III9a20a6e2022-10-05 13:41:12 -0700287 string_umap<std::unique_ptr<Neighbor>> staticNeighbors;
William A. Kennington III08505792019-01-30 16:00:04 -0800288
Gunnar Mills57d9c502018-09-14 14:42:34 -0500289 /** @brief Persistent map of VLAN interface dbus objects and their names */
William A. Kennington III9a20a6e2022-10-05 13:41:12 -0700290 string_umap<std::unique_ptr<VlanInterface>> vlanInterfaces;
Ratan Gupta5978dd12017-07-25 13:47:13 +0530291
Gunnar Mills57d9c502018-09-14 14:42:34 -0500292 /** @brief Dbus object path */
293 std::string objPath;
Ratan Gupta47722dc2017-05-26 18:32:23 +0530294
Gunnar Mills57d9c502018-09-14 14:42:34 -0500295 friend class TestEthernetInterface;
Johnathan Mantey817012a2020-01-30 15:07:39 -0800296
297 private:
298 /** @brief Determines if DHCP is active for the IP::Protocol supplied.
299 * @param[in] protocol - Either IPv4 or IPv6
Johnathan Mantey817012a2020-01-30 15:07:39 -0800300 * @returns true/false value if DHCP is active for the input protocol
301 */
William A. Kennington III24957b92021-12-03 13:59:19 -0800302 bool dhcpIsEnabled(IP::Protocol protocol);
Johnathan Mantey817012a2020-01-30 15:07:39 -0800303
Johnathan Mantey817012a2020-01-30 15:07:39 -0800304 /** @brief Determines if the address is manually assigned
305 * @param[in] origin - The origin entry of the IP::Address
306 * @returns true/false value if the address is static
307 */
308 bool originIsManuallyAssigned(IP::AddressOrigin origin);
William A. Kennington III26275a32021-07-13 20:32:42 -0700309
310 /** @brief Determines if the NIC is enabled in systemd
311 * @returns true/false value if the NIC is enabled
312 */
313 bool queryNicEnabled() const;
William A. Kennington IIIa520a392022-08-08 12:17:34 -0700314
315 std::string vlanIntfName(VlanId id) const;
316 std::string vlanObjPath(VlanId id) const;
Ratan Gupta8c834932017-04-14 16:30:24 +0530317};
318
319} // namespace network
320} // namespace phosphor