blob: 3480a57d25ea3860640c661c27d7cb3739901402 [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 <netinet/ether.h>
9
10#include <optional>
Ratan Gupta8c834932017-04-14 16:30:24 +053011#include <sdbusplus/bus.hpp>
12#include <sdbusplus/server/object.hpp>
William A. Kennington IIIbe3bd2f2022-10-11 14:11:27 -070013#include <stdplus/zstring_view.hpp>
Ratan Gupta8c834932017-04-14 16:30:24 +053014#include <string>
William A. Kennington III95530ec2022-08-19 01:44:39 -070015#include <vector>
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>
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
Ratan Gupta82549cc2017-04-21 08:45:23 +053032using IP = sdbusplus::xyz::openbmc_project::Network::server::IP;
Ratan Gupta87c13982017-06-15 09:27:27 +053033
34using EthernetInterfaceIntf =
35 sdbusplus::xyz::openbmc_project::Network::server::EthernetInterface;
Ratan Guptabd303b12017-08-18 17:10:07 +053036using MacAddressIntf =
37 sdbusplus::xyz::openbmc_project::Network::server::MACAddress;
Ratan Gupta87c13982017-06-15 09:27:27 +053038
Ratan Gupta497c0c92017-08-22 19:15:59 +053039using ServerList = std::vector<std::string>;
raviteja-bce379562019-03-28 05:59:36 -050040using ObjectPath = sdbusplus::message::object_path;
Ratan Gupta497c0c92017-08-22 19:15:59 +053041
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
William A. Kennington IIId298f932022-10-17 14:31:38 -070053/** @class InterfaceInfo
54 * @brief Information about interfaces from the kernel
55 */
56struct InterfaceInfo
57{
58 bool running;
59 unsigned index;
60 std::string name;
61 std::optional<ether_addr> mac;
62 std::optional<unsigned> mtu;
63};
64
65/** @brief Returns an InterfaceInfo for the given string name
66 */
67InterfaceInfo getInterfaceInfo(stdplus::zstring_view ifname);
68
Ratan Gupta8c834932017-04-14 16:30:24 +053069/** @class EthernetInterface
70 * @brief OpenBMC Ethernet Interface implementation.
71 * @details A concrete implementation for the
72 * xyz.openbmc_project.Network.EthernetInterface DBus API.
73 */
Ratan Gupta82549cc2017-04-21 08:45:23 +053074class EthernetInterface : public Ifaces
Ratan Gupta8c834932017-04-14 16:30:24 +053075{
Gunnar Mills57d9c502018-09-14 14:42:34 -050076 public:
77 EthernetInterface() = delete;
78 EthernetInterface(const EthernetInterface&) = delete;
79 EthernetInterface& operator=(const EthernetInterface&) = delete;
80 EthernetInterface(EthernetInterface&&) = delete;
81 EthernetInterface& operator=(EthernetInterface&&) = delete;
82 virtual ~EthernetInterface() = default;
Ratan Gupta8c834932017-04-14 16:30:24 +053083
Gunnar Mills57d9c502018-09-14 14:42:34 -050084 /** @brief Constructor to put object onto bus at a dbus path.
85 * @param[in] bus - Bus to attach to.
William A. Kennington IIId298f932022-10-17 14:31:38 -070086 * @param[in] manager - parent object.
87 * @param[in] info - Interface information.
88 * @param[in] objRoot - Path to attach at.
William A. Kennington IIIa520a392022-08-08 12:17:34 -070089 * @param[in] config - The parsed configuation file.
Gunnar Mills57d9c502018-09-14 14:42:34 -050090 * @param[in] emitSignal - true if the object added signal needs to be
91 * send.
William A. Kennington III26275a32021-07-13 20:32:42 -070092 * @param[in] enabled - Override the lookup of nicEnabled
Gunnar Mills57d9c502018-09-14 14:42:34 -050093 */
William A. Kennington IIId298f932022-10-17 14:31:38 -070094 EthernetInterface(sdbusplus::bus_t& bus, Manager& manager,
95 const InterfaceInfo& info, std::string_view objRoot,
96 const config::Parser& config, bool emitSignal = true,
William A. Kennington III26275a32021-07-13 20:32:42 -070097 std::optional<bool> enabled = std::nullopt);
Ratan Gupta82549cc2017-04-21 08:45:23 +053098
William A. Kennington IIId298f932022-10-17 14:31:38 -070099 /** @brief Updates the interface information based on new InterfaceInfo */
100 void updateInfo(const InterfaceInfo& info);
101
Asmitha Karunanithi003b8b72022-01-06 04:17:59 -0600102 /** @brief Function used to load the ntpservers
103 */
104 void loadNTPServers(const config::Parser& config);
105
Manojkiran Edaacd6dd52019-10-15 15:00:51 +0530106 /** @brief Function used to load the nameservers.
107 */
William A. Kennington IIIa520a392022-08-08 12:17:34 -0700108 void loadNameServers(const config::Parser& config);
Manojkiran Edaacd6dd52019-10-15 15:00:51 +0530109
Patrick Williams6aef7692021-05-01 06:39:41 -0500110 /** @brief Function to create ipAddress dbus object.
Gunnar Mills57d9c502018-09-14 14:42:34 -0500111 * @param[in] addressType - Type of ip address.
Patrick Williams6aef7692021-05-01 06:39:41 -0500112 * @param[in] ipAddress- IP address.
Gunnar Mills57d9c502018-09-14 14:42:34 -0500113 * @param[in] prefixLength - Length of prefix.
Gunnar Mills57d9c502018-09-14 14:42:34 -0500114 */
Ratan Gupta82549cc2017-04-21 08:45:23 +0530115
Patrick Williams6aef7692021-05-01 06:39:41 -0500116 ObjectPath ip(IP::Protocol addressType, std::string ipAddress,
William A. Kennington IIIe25f8b42022-10-11 14:43:28 -0700117 uint8_t prefixLength, std::string) override;
Ratan Gupta8c834932017-04-14 16:30:24 +0530118
William A. Kennington III08505792019-01-30 16:00:04 -0800119 /** @brief Function to create static neighbor dbus object.
120 * @param[in] ipAddress - IP address.
121 * @param[in] macAddress - Low level MAC address.
122 */
Patrick Williams6aef7692021-05-01 06:39:41 -0500123 ObjectPath neighbor(std::string ipAddress, std::string macAddress) override;
William A. Kennington III08505792019-01-30 16:00:04 -0800124
Patrick Williams6aef7692021-05-01 06:39:41 -0500125 /* @brief delete the dbus object of the given ipAddress.
126 * @param[in] ipAddress - IP address.
Gunnar Mills57d9c502018-09-14 14:42:34 -0500127 */
William A. Kennington IIIbe3bd2f2022-10-11 14:11:27 -0700128 void deleteObject(std::string_view ipAddress);
Ratan Gupta8c834932017-04-14 16:30:24 +0530129
Patrick Williams6aef7692021-05-01 06:39:41 -0500130 /* @brief delete the dbus object of the given ipAddress.
131 * @param[in] ipAddress - IP address.
William A. Kennington III08505792019-01-30 16:00:04 -0800132 */
William A. Kennington IIIbe3bd2f2022-10-11 14:11:27 -0700133 void deleteStaticNeighborObject(std::string_view ipAddress);
William A. Kennington III08505792019-01-30 16:00:04 -0800134
Gunnar Mills57d9c502018-09-14 14:42:34 -0500135 /* @brief delete the vlan dbus object of the given interface.
136 * Also deletes the device file and the network file.
137 * @param[in] interface - VLAN Interface.
138 */
William A. Kennington IIIbe3bd2f2022-10-11 14:11:27 -0700139 void deleteVLANObject(stdplus::zstring_view interface);
Ratan Guptabc886292017-07-25 18:29:57 +0530140
Gunnar Mills57d9c502018-09-14 14:42:34 -0500141 /* @brief creates the dbus object(IPaddres) given in the address list.
142 * @param[in] addrs - address list for which dbus objects needs
143 * to create.
144 */
145 void createIPAddressObjects();
Ratan Gupta29b0e432017-05-25 12:51:40 +0530146
William A. Kennington III08505792019-01-30 16:00:04 -0800147 /* @brief creates the dbus object(Neighbor) given in the neighbor list.
148 */
149 void createStaticNeighborObjects();
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 */
William A. Kennington III9a20a6e2022-10-05 13:41:12 -0700154 inline const auto& getAddresses() const
Gunnar Mills57d9c502018-09-14 14:42:34 -0500155 {
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 */
William A. Kennington III9a20a6e2022-10-05 13:41:12 -0700162 inline const auto& getStaticNeighbors() const
William A. Kennington III08505792019-01-30 16:00:04 -0800163 {
164 return staticNeighbors;
165 }
166
Gunnar Mills57d9c502018-09-14 14:42:34 -0500167 /** Set value of DHCPEnabled */
William A. Kennington III8060c0d2022-08-18 19:19:34 -0700168 DHCPConf dhcpEnabled() const override;
Patrick Williams6aef7692021-05-01 06:39:41 -0500169 DHCPConf dhcpEnabled(DHCPConf value) override;
William A. Kennington III8060c0d2022-08-18 19:19:34 -0700170 using EthernetInterfaceIntf::dhcp4;
171 bool dhcp4(bool value) override;
172 using EthernetInterfaceIntf::dhcp6;
173 bool dhcp6(bool value) override;
Ratan Gupta87c13982017-06-15 09:27:27 +0530174
Johnathan Manteyfaa72e52020-01-08 10:38:58 -0800175 /** Retrieve Link State */
176 bool linkUp() const override;
177
Tejas Patil2c0fc562021-08-03 19:13:46 +0530178 /** Retrieve MTU Size */
179 size_t mtu() const override;
180
181 /** Set size of MTU */
182 size_t mtu(size_t value) override;
183
Johnathan Manteyd0679f92019-10-29 16:20:28 -0700184 /** Set value of NICEnabled */
Patrick Williams6aef7692021-05-01 06:39:41 -0500185 bool nicEnabled(bool value) override;
Johnathan Manteyd0679f92019-10-29 16:20:28 -0700186
Gunnar Mills57d9c502018-09-14 14:42:34 -0500187 /** @brief sets the MAC address.
188 * @param[in] value - MAC address which needs to be set on the system.
189 * @returns macAddress of the interface or throws an error.
190 */
Patrick Williams6aef7692021-05-01 06:39:41 -0500191 std::string macAddress(std::string value) override;
Ratan Guptabd303b12017-08-18 17:10:07 +0530192
Johnathan Mantey5b023f52019-06-24 16:06:37 -0700193 /** @brief check conf file for Router Advertisements
194 *
195 */
Patrick Williams6aef7692021-05-01 06:39:41 -0500196 bool ipv6AcceptRA(bool value) override;
William A. Kennington III8060c0d2022-08-18 19:19:34 -0700197 using EthernetInterfaceIntf::ipv6AcceptRA;
Johnathan Mantey5b023f52019-06-24 16:06:37 -0700198
Gunnar Mills57d9c502018-09-14 14:42:34 -0500199 /** @brief sets the NTP servers.
200 * @param[in] value - vector of NTP servers.
201 */
Patrick Williams6aef7692021-05-01 06:39:41 -0500202 ServerList ntpServers(ServerList value) override;
Ratan Gupta497c0c92017-08-22 19:15:59 +0530203
Asmitha Karunanithi003b8b72022-01-06 04:17:59 -0600204 /** @brief sets the static NTP servers.
205 * @param[in] value - vector of NTP servers.
206 */
207 ServerList staticNTPServers(ServerList value) override;
208
Manojkiran Edaacd6dd52019-10-15 15:00:51 +0530209 /** @brief sets the Static DNS/nameservers.
210 * @param[in] value - vector of DNS servers.
211 */
212
213 ServerList staticNameServers(ServerList value) override;
214
Gunnar Mills57d9c502018-09-14 14:42:34 -0500215 /** @brief create Vlan interface.
216 * @param[in] id- VLAN identifier.
217 */
William A. Kennington IIId298f932022-10-17 14:31:38 -0700218 ObjectPath createVLAN(uint16_t id);
Ratan Gupta5978dd12017-07-25 13:47:13 +0530219
Gunnar Mills57d9c502018-09-14 14:42:34 -0500220 /** @brief load the vlan info from the system
221 * and creates the ip address dbus objects.
222 * @param[in] vlanID- VLAN identifier.
223 */
William A. Kennington IIId298f932022-10-17 14:31:38 -0700224 void loadVLAN(std::string_view objRoot, uint16_t vlanID);
Ratan Gupta92bc2fe2017-07-26 22:40:21 +0530225
Gunnar Mills57d9c502018-09-14 14:42:34 -0500226 /** @brief write the network conf file with the in-memory objects.
227 */
228 void writeConfigurationFile();
Ratan Gupta2b106532017-07-25 16:05:02 +0530229
Gunnar Mills57d9c502018-09-14 14:42:34 -0500230 /** @brief delete all dbus objects.
231 */
232 void deleteAll();
Ratan Guptae9c9b812017-09-22 17:15:37 +0530233
Ravi Tejaa5a09442020-07-17 00:57:33 -0500234 /** @brief set the default v4 gateway of the interface.
235 * @param[in] gateway - default v4 gateway of the interface.
236 */
237 std::string defaultGateway(std::string gateway) override;
238
239 /** @brief set the default v6 gateway of the interface.
240 * @param[in] gateway - default v6 gateway of the interface.
241 */
242 std::string defaultGateway6(std::string gateway) override;
243
Gunnar Mills57d9c502018-09-14 14:42:34 -0500244 using EthernetInterfaceIntf::interfaceName;
Johnathan Manteyfaa72e52020-01-08 10:38:58 -0800245 using EthernetInterfaceIntf::linkUp;
Tejas Patil2c0fc562021-08-03 19:13:46 +0530246 using EthernetInterfaceIntf::mtu;
Patrick Williams6aef7692021-05-01 06:39:41 -0500247 using EthernetInterfaceIntf::nicEnabled;
248 using MacAddressIntf::macAddress;
Ratan Gupta5978dd12017-07-25 13:47:13 +0530249
Ravi Tejaa5a09442020-07-17 00:57:33 -0500250 using EthernetInterfaceIntf::defaultGateway;
251 using EthernetInterfaceIntf::defaultGateway6;
Ratan Gupta6dec390f2017-08-20 15:28:12 +0530252
Gunnar Mills57d9c502018-09-14 14:42:34 -0500253 protected:
Gunnar Mills57d9c502018-09-14 14:42:34 -0500254 /* @brief delete the vlan interface from system.
255 * @param[in] interface - vlan Interface.
256 */
William A. Kennington IIIbe3bd2f2022-10-11 14:11:27 -0700257 void deleteVLANFromSystem(stdplus::zstring_view interface);
Ratan Guptae9c9b812017-09-22 17:15:37 +0530258
Gunnar Mills57d9c502018-09-14 14:42:34 -0500259 /** @brief construct the ip address dbus object path.
260 * @param[in] addressType - Type of ip address.
Patrick Williams6aef7692021-05-01 06:39:41 -0500261 * @param[in] ipAddress - IP address.
Gunnar Mills57d9c502018-09-14 14:42:34 -0500262 * @param[in] prefixLength - Length of prefix.
Lei YU34027572021-08-11 15:23:52 +0800263 * @param[in] origin - The origin entry of the IP::Address
Ratan Gupta65e5abe2017-05-23 13:20:44 +0530264
Gunnar Mills57d9c502018-09-14 14:42:34 -0500265 * @return path of the address object.
266 */
Gunnar Mills57d9c502018-09-14 14:42:34 -0500267 std::string generateObjectPath(IP::Protocol addressType,
William A. Kennington IIIbe3bd2f2022-10-11 14:11:27 -0700268 std::string_view ipAddress,
Gunnar Mills57d9c502018-09-14 14:42:34 -0500269 uint8_t prefixLength,
Lei YU34027572021-08-11 15:23:52 +0800270 IP::AddressOrigin origin) const;
Ratan Gupta82549cc2017-04-21 08:45:23 +0530271
William A. Kennington III08505792019-01-30 16:00:04 -0800272 std::string
William A. Kennington IIIbe3bd2f2022-10-11 14:11:27 -0700273 generateStaticNeighborObjectPath(std::string_view ipAddress,
274 std::string_view macAddress) const;
William A. Kennington III08505792019-01-30 16:00:04 -0800275
Asmitha Karunanithi003b8b72022-01-06 04:17:59 -0600276 /** @brief get the NTP server list from the timsyncd dbus obj
Gunnar Mills57d9c502018-09-14 14:42:34 -0500277 *
278 */
Asmitha Karunanithi003b8b72022-01-06 04:17:59 -0600279 virtual ServerList getNTPServerFromTimeSyncd();
Ratan Gupta497c0c92017-08-22 19:15:59 +0530280
Gunnar Mills57d9c502018-09-14 14:42:34 -0500281 /** @brief get the name server details from the network conf
282 *
283 */
Manojkiran Edaacd6dd52019-10-15 15:00:51 +0530284 virtual ServerList getNameServerFromResolvd();
Ratan Gupta6dec390f2017-08-20 15:28:12 +0530285
Gunnar Mills57d9c502018-09-14 14:42:34 -0500286 /** @brief Persistent sdbusplus DBus bus connection. */
Patrick Williamsc38b0712022-07-22 19:26:54 -0500287 sdbusplus::bus_t& bus;
Ratan Gupta82549cc2017-04-21 08:45:23 +0530288
Gunnar Mills57d9c502018-09-14 14:42:34 -0500289 /** @brief Network Manager object. */
290 Manager& manager;
Ratan Gupta4f1c18b2017-05-25 12:59:35 +0530291
Gunnar Mills57d9c502018-09-14 14:42:34 -0500292 /** @brief Persistent map of IPAddress dbus objects and their names */
William A. Kennington III9a20a6e2022-10-05 13:41:12 -0700293 string_umap<std::unique_ptr<IPAddress>> addrs;
Ratan Gupta82549cc2017-04-21 08:45:23 +0530294
William A. Kennington III08505792019-01-30 16:00:04 -0800295 /** @brief Persistent map of Neighbor dbus objects and their names */
William A. Kennington III9a20a6e2022-10-05 13:41:12 -0700296 string_umap<std::unique_ptr<Neighbor>> staticNeighbors;
William A. Kennington III08505792019-01-30 16:00:04 -0800297
Gunnar Mills57d9c502018-09-14 14:42:34 -0500298 /** @brief Persistent map of VLAN interface dbus objects and their names */
William A. Kennington III9a20a6e2022-10-05 13:41:12 -0700299 string_umap<std::unique_ptr<VlanInterface>> vlanInterfaces;
Ratan Gupta5978dd12017-07-25 13:47:13 +0530300
Gunnar Mills57d9c502018-09-14 14:42:34 -0500301 /** @brief Dbus object path */
302 std::string objPath;
Ratan Gupta47722dc2017-05-26 18:32:23 +0530303
William A. Kennington III2e09d272022-10-14 17:15:00 -0700304 /** @brief Interface index */
305 unsigned ifIdx;
306
Gunnar Mills57d9c502018-09-14 14:42:34 -0500307 friend class TestEthernetInterface;
Johnathan Mantey817012a2020-01-30 15:07:39 -0800308
309 private:
William A. Kennington IIId298f932022-10-17 14:31:38 -0700310 EthernetInterface(sdbusplus::bus_t& bus, Manager& manager,
311 const InterfaceInfo& info, std::string&& objPath,
312 const config::Parser& config, bool emitSignal,
313 std::optional<bool> enabled);
314
Johnathan Mantey817012a2020-01-30 15:07:39 -0800315 /** @brief Determines if DHCP is active for the IP::Protocol supplied.
316 * @param[in] protocol - Either IPv4 or IPv6
Johnathan Mantey817012a2020-01-30 15:07:39 -0800317 * @returns true/false value if DHCP is active for the input protocol
318 */
William A. Kennington III24957b92021-12-03 13:59:19 -0800319 bool dhcpIsEnabled(IP::Protocol protocol);
Johnathan Mantey817012a2020-01-30 15:07:39 -0800320
Johnathan Mantey817012a2020-01-30 15:07:39 -0800321 /** @brief Determines if the address is manually assigned
322 * @param[in] origin - The origin entry of the IP::Address
323 * @returns true/false value if the address is static
324 */
325 bool originIsManuallyAssigned(IP::AddressOrigin origin);
William A. Kennington III26275a32021-07-13 20:32:42 -0700326
327 /** @brief Determines if the NIC is enabled in systemd
328 * @returns true/false value if the NIC is enabled
329 */
330 bool queryNicEnabled() const;
William A. Kennington IIIa520a392022-08-08 12:17:34 -0700331
William A. Kennington IIId298f932022-10-17 14:31:38 -0700332 std::string vlanIntfName(uint16_t id) const;
Ratan Gupta8c834932017-04-14 16:30:24 +0530333};
334
335} // namespace network
336} // namespace phosphor