blob: 4a0f0303c92a6e1e008a4bec8ec6ec5815d40eb1 [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
Patrick Venture189d44e2018-07-09 12:30:59 -07008#include <experimental/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
16namespace phosphor
17{
18namespace network
19{
Ratan Gupta8c834932017-04-14 16:30:24 +053020
Gunnar Mills57d9c502018-09-14 14:42:34 -050021using Ifaces = sdbusplus::server::object::object<
22 sdbusplus::xyz::openbmc_project::Network::server::EthernetInterface,
23 sdbusplus::xyz::openbmc_project::Network::server::MACAddress,
24 sdbusplus::xyz::openbmc_project::Network::IP::server::Create,
William A. Kennington III08505792019-01-30 16:00:04 -080025 sdbusplus::xyz::openbmc_project::Network::Neighbor::server::CreateStatic,
Gunnar Mills57d9c502018-09-14 14:42:34 -050026 sdbusplus::xyz::openbmc_project::Collection::server::DeleteAll>;
Ratan Gupta8c834932017-04-14 16:30:24 +053027
Ratan Gupta82549cc2017-04-21 08:45:23 +053028using IP = sdbusplus::xyz::openbmc_project::Network::server::IP;
Ratan Gupta87c13982017-06-15 09:27:27 +053029
30using EthernetInterfaceIntf =
31 sdbusplus::xyz::openbmc_project::Network::server::EthernetInterface;
Ratan Guptabd303b12017-08-18 17:10:07 +053032using MacAddressIntf =
33 sdbusplus::xyz::openbmc_project::Network::server::MACAddress;
Ratan Gupta87c13982017-06-15 09:27:27 +053034
Ratan Gupta497c0c92017-08-22 19:15:59 +053035using ServerList = std::vector<std::string>;
raviteja-bce379562019-03-28 05:59:36 -050036using ObjectPath = sdbusplus::message::object_path;
Ratan Gupta497c0c92017-08-22 19:15:59 +053037
Ratan Gupta5978dd12017-07-25 13:47:13 +053038namespace fs = std::experimental::filesystem;
39
Ratan Gupta4f1c18b2017-05-25 12:59:35 +053040class Manager; // forward declaration of network manager.
Ratan Gupta8c834932017-04-14 16:30:24 +053041
Ratan Gupta47722dc2017-05-26 18:32:23 +053042class TestEthernetInterface;
43
Ratan Gupta5978dd12017-07-25 13:47:13 +053044class VlanInterface;
45
46class IPAddress;
47
William A. Kennington III08505792019-01-30 16:00:04 -080048class Neighbor;
49
Ratan Gupta8c834932017-04-14 16:30:24 +053050using LinkSpeed = uint16_t;
51using DuplexMode = uint8_t;
52using Autoneg = uint8_t;
Johnathan Manteyfaa72e52020-01-08 10:38:58 -080053using LinkUp = bool;
Ratan Gupta5978dd12017-07-25 13:47:13 +053054using VlanId = uint32_t;
55using InterfaceName = std::string;
Johnathan Manteyfaa72e52020-01-08 10:38:58 -080056using InterfaceInfo = std::tuple<LinkSpeed, DuplexMode, Autoneg, LinkUp>;
Ratan Gupta29b0e432017-05-25 12:51:40 +053057using AddressMap = std::map<std::string, std::shared_ptr<IPAddress>>;
William A. Kennington III08505792019-01-30 16:00:04 -080058using NeighborMap = std::map<std::string, std::shared_ptr<Neighbor>>;
Gunnar Mills57d9c502018-09-14 14:42:34 -050059using VlanInterfaceMap =
60 std::map<InterfaceName, std::unique_ptr<VlanInterface>>;
Ratan Gupta8c834932017-04-14 16:30:24 +053061
62/** @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.
79 * @param[in] objPath - Path to attach at.
80 * @param[in] dhcpEnabled - is dhcp enabled(true/false).
81 * @param[in] parent - parent object.
82 * @param[in] emitSignal - true if the object added signal needs to be
83 * send.
84 */
85 EthernetInterface(sdbusplus::bus::bus& bus, const std::string& objPath,
86 bool dhcpEnabled, Manager& parent,
87 bool emitSignal = true);
Ratan Gupta82549cc2017-04-21 08:45:23 +053088
Manojkiran Edaacd6dd52019-10-15 15:00:51 +053089 /** @brief Function used to load the nameservers.
90 */
91 virtual void loadNameServers();
92
Gunnar Mills57d9c502018-09-14 14:42:34 -050093 /** @brief Function to create ipaddress dbus object.
94 * @param[in] addressType - Type of ip address.
95 * @param[in] ipaddress- IP address.
96 * @param[in] prefixLength - Length of prefix.
97 * @param[in] gateway - Gateway ip address.
98 */
Ratan Gupta82549cc2017-04-21 08:45:23 +053099
raviteja-bce379562019-03-28 05:59:36 -0500100 ObjectPath iP(IP::Protocol addressType, std::string ipaddress,
101 uint8_t prefixLength, std::string gateway) override;
Ratan Gupta8c834932017-04-14 16:30:24 +0530102
William A. Kennington III08505792019-01-30 16:00:04 -0800103 /** @brief Function to create static neighbor dbus object.
104 * @param[in] ipAddress - IP address.
105 * @param[in] macAddress - Low level MAC address.
106 */
107 ObjectPath neighbor(std::string iPAddress, std::string mACAddress) override;
108
Gunnar Mills57d9c502018-09-14 14:42:34 -0500109 /* @brief delete the dbus object of the given ipaddress.
110 * @param[in] ipaddress - IP address.
111 */
112 void deleteObject(const std::string& ipaddress);
Ratan Gupta8c834932017-04-14 16:30:24 +0530113
William A. Kennington III08505792019-01-30 16:00:04 -0800114 /* @brief delete the dbus object of the given ipaddress.
115 * @param[in] ipaddress - IP address.
116 */
117 void deleteStaticNeighborObject(const std::string& ipAddress);
118
Gunnar Mills57d9c502018-09-14 14:42:34 -0500119 /* @brief delete the vlan dbus object of the given interface.
120 * Also deletes the device file and the network file.
121 * @param[in] interface - VLAN Interface.
122 */
123 void deleteVLANObject(const std::string& interface);
Ratan Guptabc886292017-07-25 18:29:57 +0530124
Gunnar Mills57d9c502018-09-14 14:42:34 -0500125 /* @brief creates the dbus object(IPaddres) given in the address list.
126 * @param[in] addrs - address list for which dbus objects needs
127 * to create.
128 */
129 void createIPAddressObjects();
Ratan Gupta29b0e432017-05-25 12:51:40 +0530130
William A. Kennington III08505792019-01-30 16:00:04 -0800131 /* @brief creates the dbus object(Neighbor) given in the neighbor list.
132 */
133 void createStaticNeighborObjects();
134
William A. Kennington IIId7946a72019-04-19 14:24:09 -0700135 /* @brief Gets the index of the interface on the system
136 */
137 unsigned ifIndex() const;
138
Gunnar Mills57d9c502018-09-14 14:42:34 -0500139 /* @brief Gets all the ip addresses.
140 * @returns the list of ipaddress.
141 */
142 const AddressMap& getAddresses() const
143 {
144 return addrs;
145 }
Ratan Gupta8c834932017-04-14 16:30:24 +0530146
William A. Kennington III08505792019-01-30 16:00:04 -0800147 /* @brief Gets all the static neighbor entries.
148 * @returns Static neighbor map.
149 */
150 const NeighborMap& getStaticNeighbors() const
151 {
152 return staticNeighbors;
153 }
154
Gunnar Mills57d9c502018-09-14 14:42:34 -0500155 /** Set value of DHCPEnabled */
156 bool dHCPEnabled(bool value) override;
Ratan Gupta87c13982017-06-15 09:27:27 +0530157
Johnathan Manteyfaa72e52020-01-08 10:38:58 -0800158 /** Retrieve Link State */
159 bool linkUp() const override;
160
Gunnar Mills57d9c502018-09-14 14:42:34 -0500161 /** @brief sets the MAC address.
162 * @param[in] value - MAC address which needs to be set on the system.
163 * @returns macAddress of the interface or throws an error.
164 */
165 std::string mACAddress(std::string value) override;
Ratan Guptabd303b12017-08-18 17:10:07 +0530166
Johnathan Mantey5b023f52019-06-24 16:06:37 -0700167 /** @brief get the IPv6AcceptRA flag from the network configuration file
168 *
169 */
170 bool getIPv6AcceptRAFromConf();
171
172 /** @brief check conf file for Router Advertisements
173 *
174 */
175 bool iPv6AcceptRA(bool value) override;
176
Gunnar Mills57d9c502018-09-14 14:42:34 -0500177 /** @brief sets the NTP servers.
178 * @param[in] value - vector of NTP servers.
179 */
180 ServerList nTPServers(ServerList value) override;
Ratan Gupta497c0c92017-08-22 19:15:59 +0530181
Gunnar Mills57d9c502018-09-14 14:42:34 -0500182 /** @brief sets the DNS/nameservers.
183 * @param[in] value - vector of DNS servers.
184 */
185 ServerList nameservers(ServerList value) override;
Ratan Gupta6dec3902017-08-20 15:28:12 +0530186
Manojkiran Edaacd6dd52019-10-15 15:00:51 +0530187 /** @brief sets the Static DNS/nameservers.
188 * @param[in] value - vector of DNS servers.
189 */
190
191 ServerList staticNameServers(ServerList value) override;
192
Gunnar Mills57d9c502018-09-14 14:42:34 -0500193 /** @brief create Vlan interface.
194 * @param[in] id- VLAN identifier.
195 */
William A. Kennington IIIf4b4ff82019-04-09 19:06:52 -0700196 ObjectPath createVLAN(VlanId id);
Ratan Gupta5978dd12017-07-25 13:47:13 +0530197
Gunnar Mills57d9c502018-09-14 14:42:34 -0500198 /** @brief load the vlan info from the system
199 * and creates the ip address dbus objects.
200 * @param[in] vlanID- VLAN identifier.
201 */
202 void loadVLAN(VlanId vlanID);
Ratan Gupta92bc2fe2017-07-26 22:40:21 +0530203
Gunnar Mills57d9c502018-09-14 14:42:34 -0500204 /** @brief write the network conf file with the in-memory objects.
205 */
206 void writeConfigurationFile();
Ratan Gupta2b106532017-07-25 16:05:02 +0530207
Gunnar Mills57d9c502018-09-14 14:42:34 -0500208 /** @brief delete all dbus objects.
209 */
210 void deleteAll();
Ratan Guptae9c9b812017-09-22 17:15:37 +0530211
Gunnar Mills57d9c502018-09-14 14:42:34 -0500212 using EthernetInterfaceIntf::dHCPEnabled;
213 using EthernetInterfaceIntf::interfaceName;
Johnathan Manteyfaa72e52020-01-08 10:38:58 -0800214 using EthernetInterfaceIntf::linkUp;
Gunnar Mills57d9c502018-09-14 14:42:34 -0500215 using MacAddressIntf::mACAddress;
Ratan Gupta5978dd12017-07-25 13:47:13 +0530216
Gunnar Mills57d9c502018-09-14 14:42:34 -0500217 /** @brief Absolute path of the resolv conf file */
218 static constexpr auto resolvConfFile = "/etc/resolv.conf";
Ratan Gupta6dec3902017-08-20 15:28:12 +0530219
Gunnar Mills57d9c502018-09-14 14:42:34 -0500220 protected:
221 /** @brief get the info of the ethernet interface.
222 * @return tuple having the link speed,autonegotiation,duplexmode .
223 */
224 InterfaceInfo getInterfaceInfo() const;
Ratan Gupta8c834932017-04-14 16:30:24 +0530225
Gunnar Mills57d9c502018-09-14 14:42:34 -0500226 /* @brief delete the vlan interface from system.
227 * @param[in] interface - vlan Interface.
228 */
229 void deleteVLANFromSystem(const std::string& interface);
Ratan Guptae9c9b812017-09-22 17:15:37 +0530230
Gunnar Mills57d9c502018-09-14 14:42:34 -0500231 /** @brief get the mac address of the interface.
232 * @param[in] interfaceName - Network interface name.
233 * @return macaddress on success
234 */
Ratan Gupta8c834932017-04-14 16:30:24 +0530235
Gunnar Mills57d9c502018-09-14 14:42:34 -0500236 std::string getMACAddress(const std::string& interfaceName) const;
Ratan Gupta8c834932017-04-14 16:30:24 +0530237
Gunnar Mills57d9c502018-09-14 14:42:34 -0500238 /** @brief construct the ip address dbus object path.
239 * @param[in] addressType - Type of ip address.
240 * @param[in] ipaddress - IP address.
241 * @param[in] prefixLength - Length of prefix.
242 * @param[in] gateway - Gateway address.
Ratan Gupta65e5abe2017-05-23 13:20:44 +0530243
Gunnar Mills57d9c502018-09-14 14:42:34 -0500244 * @return path of the address object.
245 */
Ratan Gupta82549cc2017-04-21 08:45:23 +0530246
Gunnar Mills57d9c502018-09-14 14:42:34 -0500247 std::string generateObjectPath(IP::Protocol addressType,
248 const std::string& ipaddress,
249 uint8_t prefixLength,
250 const std::string& gateway) const;
Ratan Gupta82549cc2017-04-21 08:45:23 +0530251
William A. Kennington III08505792019-01-30 16:00:04 -0800252 std::string
253 generateStaticNeighborObjectPath(const std::string& iPAddress,
254 const std::string& mACAddress) const;
255
Gunnar Mills57d9c502018-09-14 14:42:34 -0500256 /** @brief generates the id by doing hash of ipaddress,
257 * prefixlength and the gateway.
258 * @param[in] ipaddress - IP address.
259 * @param[in] prefixLength - Length of prefix.
260 * @param[in] gateway - Gateway address.
261 * @return hash string.
262 */
Ratan Gupta82549cc2017-04-21 08:45:23 +0530263
Gunnar Mills57d9c502018-09-14 14:42:34 -0500264 static std::string generateId(const std::string& ipaddress,
265 uint8_t prefixLength,
266 const std::string& gateway);
Ratan Gupta82549cc2017-04-21 08:45:23 +0530267
William A. Kennington III08505792019-01-30 16:00:04 -0800268 /** @brief generates the id by doing hash of ipaddress
269 * and the mac address.
270 * @param[in] iPAddress - IP address.
271 * @param[in] mACAddress - Gateway address.
272 * @return hash string.
273 */
274 static std::string generateNeighborId(const std::string& iPAddress,
275 const std::string& mACAddress);
276
Gunnar Mills57d9c502018-09-14 14:42:34 -0500277 /** @brief write the dhcp section **/
278 void writeDHCPSection(std::fstream& stream);
Ratan Gupta2b106532017-07-25 16:05:02 +0530279
Gunnar Mills57d9c502018-09-14 14:42:34 -0500280 /** @brief get the NTP server list from the network conf
281 *
282 */
283 ServerList getNTPServersFromConf();
Ratan Gupta497c0c92017-08-22 19:15:59 +0530284
Gunnar Mills57d9c502018-09-14 14:42:34 -0500285 /** @brief get the name server details from the network conf
286 *
287 */
Manojkiran Edaacd6dd52019-10-15 15:00:51 +0530288 virtual ServerList getNameServerFromResolvd();
289 ServerList getstaticNameServerFromConf();
Ratan Gupta6dec3902017-08-20 15:28:12 +0530290
Gunnar Mills57d9c502018-09-14 14:42:34 -0500291 /** @brief Persistent sdbusplus DBus bus connection. */
292 sdbusplus::bus::bus& bus;
Ratan Gupta82549cc2017-04-21 08:45:23 +0530293
Gunnar Mills57d9c502018-09-14 14:42:34 -0500294 /** @brief Network Manager object. */
295 Manager& manager;
Ratan Gupta4f1c18b2017-05-25 12:59:35 +0530296
Gunnar Mills57d9c502018-09-14 14:42:34 -0500297 /** @brief Persistent map of IPAddress dbus objects and their names */
298 AddressMap addrs;
Ratan Gupta82549cc2017-04-21 08:45:23 +0530299
William A. Kennington III08505792019-01-30 16:00:04 -0800300 /** @brief Persistent map of Neighbor dbus objects and their names */
301 NeighborMap staticNeighbors;
302
Gunnar Mills57d9c502018-09-14 14:42:34 -0500303 /** @brief Persistent map of VLAN interface dbus objects and their names */
304 VlanInterfaceMap vlanInterfaces;
Ratan Gupta5978dd12017-07-25 13:47:13 +0530305
Gunnar Mills57d9c502018-09-14 14:42:34 -0500306 /** @brief Dbus object path */
307 std::string objPath;
Ratan Gupta47722dc2017-05-26 18:32:23 +0530308
Gunnar Mills57d9c502018-09-14 14:42:34 -0500309 friend class TestEthernetInterface;
Ratan Gupta8c834932017-04-14 16:30:24 +0530310};
311
312} // namespace network
313} // namespace phosphor