blob: 68668d6a61c5db5514e6dc5881f325e54d9cf5b4 [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
Gunnar Mills57d9c502018-09-14 14:42:34 -050089 /** @brief Function to create ipaddress dbus object.
90 * @param[in] addressType - Type of ip address.
91 * @param[in] ipaddress- IP address.
92 * @param[in] prefixLength - Length of prefix.
93 * @param[in] gateway - Gateway ip address.
94 */
Ratan Gupta82549cc2017-04-21 08:45:23 +053095
raviteja-bce379562019-03-28 05:59:36 -050096 ObjectPath iP(IP::Protocol addressType, std::string ipaddress,
97 uint8_t prefixLength, std::string gateway) 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 */
103 ObjectPath neighbor(std::string iPAddress, std::string mACAddress) override;
104
Gunnar Mills57d9c502018-09-14 14:42:34 -0500105 /* @brief delete the dbus object of the given ipaddress.
106 * @param[in] ipaddress - IP address.
107 */
108 void deleteObject(const std::string& ipaddress);
Ratan Gupta8c834932017-04-14 16:30:24 +0530109
William A. Kennington III08505792019-01-30 16:00:04 -0800110 /* @brief delete the dbus object of the given ipaddress.
111 * @param[in] ipaddress - IP address.
112 */
113 void deleteStaticNeighborObject(const std::string& ipAddress);
114
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 */
119 void deleteVLANObject(const std::string& 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.
136 * @returns the list of ipaddress.
137 */
138 const AddressMap& getAddresses() const
139 {
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 */
146 const NeighborMap& getStaticNeighbors() const
147 {
148 return staticNeighbors;
149 }
150
Gunnar Mills57d9c502018-09-14 14:42:34 -0500151 /** Set value of DHCPEnabled */
152 bool dHCPEnabled(bool value) override;
Ratan Gupta87c13982017-06-15 09:27:27 +0530153
Johnathan Manteyfaa72e52020-01-08 10:38:58 -0800154 /** Retrieve Link State */
155 bool linkUp() const override;
156
Gunnar Mills57d9c502018-09-14 14:42:34 -0500157 /** @brief sets the MAC address.
158 * @param[in] value - MAC address which needs to be set on the system.
159 * @returns macAddress of the interface or throws an error.
160 */
161 std::string mACAddress(std::string value) override;
Ratan Guptabd303b12017-08-18 17:10:07 +0530162
Johnathan Mantey5b023f52019-06-24 16:06:37 -0700163 /** @brief get the IPv6AcceptRA flag from the network configuration file
164 *
165 */
166 bool getIPv6AcceptRAFromConf();
167
168 /** @brief check conf file for Router Advertisements
169 *
170 */
171 bool iPv6AcceptRA(bool value) override;
172
Gunnar Mills57d9c502018-09-14 14:42:34 -0500173 /** @brief sets the NTP servers.
174 * @param[in] value - vector of NTP servers.
175 */
176 ServerList nTPServers(ServerList value) override;
Ratan Gupta497c0c92017-08-22 19:15:59 +0530177
Gunnar Mills57d9c502018-09-14 14:42:34 -0500178 /** @brief sets the DNS/nameservers.
179 * @param[in] value - vector of DNS servers.
180 */
181 ServerList nameservers(ServerList value) override;
Ratan Gupta6dec3902017-08-20 15:28:12 +0530182
Gunnar Mills57d9c502018-09-14 14:42:34 -0500183 /** @brief create Vlan interface.
184 * @param[in] id- VLAN identifier.
185 */
William A. Kennington IIIf4b4ff82019-04-09 19:06:52 -0700186 ObjectPath createVLAN(VlanId id);
Ratan Gupta5978dd12017-07-25 13:47:13 +0530187
Gunnar Mills57d9c502018-09-14 14:42:34 -0500188 /** @brief load the vlan info from the system
189 * and creates the ip address dbus objects.
190 * @param[in] vlanID- VLAN identifier.
191 */
192 void loadVLAN(VlanId vlanID);
Ratan Gupta92bc2fe2017-07-26 22:40:21 +0530193
Gunnar Mills57d9c502018-09-14 14:42:34 -0500194 /** @brief write the network conf file with the in-memory objects.
195 */
196 void writeConfigurationFile();
Ratan Gupta2b106532017-07-25 16:05:02 +0530197
Gunnar Mills57d9c502018-09-14 14:42:34 -0500198 /** @brief delete all dbus objects.
199 */
200 void deleteAll();
Ratan Guptae9c9b812017-09-22 17:15:37 +0530201
Gunnar Mills57d9c502018-09-14 14:42:34 -0500202 using EthernetInterfaceIntf::dHCPEnabled;
203 using EthernetInterfaceIntf::interfaceName;
Johnathan Manteyfaa72e52020-01-08 10:38:58 -0800204 using EthernetInterfaceIntf::linkUp;
Gunnar Mills57d9c502018-09-14 14:42:34 -0500205 using MacAddressIntf::mACAddress;
Ratan Gupta5978dd12017-07-25 13:47:13 +0530206
Gunnar Mills57d9c502018-09-14 14:42:34 -0500207 /** @brief Absolute path of the resolv conf file */
208 static constexpr auto resolvConfFile = "/etc/resolv.conf";
Ratan Gupta6dec3902017-08-20 15:28:12 +0530209
Gunnar Mills57d9c502018-09-14 14:42:34 -0500210 protected:
211 /** @brief get the info of the ethernet interface.
212 * @return tuple having the link speed,autonegotiation,duplexmode .
213 */
214 InterfaceInfo getInterfaceInfo() const;
Ratan Gupta8c834932017-04-14 16:30:24 +0530215
Gunnar Mills57d9c502018-09-14 14:42:34 -0500216 /* @brief delete the vlan interface from system.
217 * @param[in] interface - vlan Interface.
218 */
219 void deleteVLANFromSystem(const std::string& interface);
Ratan Guptae9c9b812017-09-22 17:15:37 +0530220
Gunnar Mills57d9c502018-09-14 14:42:34 -0500221 /** @brief get the mac address of the interface.
222 * @param[in] interfaceName - Network interface name.
223 * @return macaddress on success
224 */
Ratan Gupta8c834932017-04-14 16:30:24 +0530225
Gunnar Mills57d9c502018-09-14 14:42:34 -0500226 std::string getMACAddress(const std::string& interfaceName) const;
Ratan Gupta8c834932017-04-14 16:30:24 +0530227
Gunnar Mills57d9c502018-09-14 14:42:34 -0500228 /** @brief construct the ip address dbus object path.
229 * @param[in] addressType - Type of ip address.
230 * @param[in] ipaddress - IP address.
231 * @param[in] prefixLength - Length of prefix.
232 * @param[in] gateway - Gateway address.
Ratan Gupta65e5abe2017-05-23 13:20:44 +0530233
Gunnar Mills57d9c502018-09-14 14:42:34 -0500234 * @return path of the address object.
235 */
Ratan Gupta82549cc2017-04-21 08:45:23 +0530236
Gunnar Mills57d9c502018-09-14 14:42:34 -0500237 std::string generateObjectPath(IP::Protocol addressType,
238 const std::string& ipaddress,
239 uint8_t prefixLength,
240 const std::string& gateway) const;
Ratan Gupta82549cc2017-04-21 08:45:23 +0530241
William A. Kennington III08505792019-01-30 16:00:04 -0800242 std::string
243 generateStaticNeighborObjectPath(const std::string& iPAddress,
244 const std::string& mACAddress) const;
245
Gunnar Mills57d9c502018-09-14 14:42:34 -0500246 /** @brief generates the id by doing hash of ipaddress,
247 * prefixlength and the gateway.
248 * @param[in] ipaddress - IP address.
249 * @param[in] prefixLength - Length of prefix.
250 * @param[in] gateway - Gateway address.
251 * @return hash string.
252 */
Ratan Gupta82549cc2017-04-21 08:45:23 +0530253
Gunnar Mills57d9c502018-09-14 14:42:34 -0500254 static std::string generateId(const std::string& ipaddress,
255 uint8_t prefixLength,
256 const std::string& gateway);
Ratan Gupta82549cc2017-04-21 08:45:23 +0530257
William A. Kennington III08505792019-01-30 16:00:04 -0800258 /** @brief generates the id by doing hash of ipaddress
259 * and the mac address.
260 * @param[in] iPAddress - IP address.
261 * @param[in] mACAddress - Gateway address.
262 * @return hash string.
263 */
264 static std::string generateNeighborId(const std::string& iPAddress,
265 const std::string& mACAddress);
266
Gunnar Mills57d9c502018-09-14 14:42:34 -0500267 /** @brief write the dhcp section **/
268 void writeDHCPSection(std::fstream& stream);
Ratan Gupta2b106532017-07-25 16:05:02 +0530269
Gunnar Mills57d9c502018-09-14 14:42:34 -0500270 /** @brief get the NTP server list from the network conf
271 *
272 */
273 ServerList getNTPServersFromConf();
Ratan Gupta497c0c92017-08-22 19:15:59 +0530274
Gunnar Mills57d9c502018-09-14 14:42:34 -0500275 /** @brief get the name server details from the network conf
276 *
277 */
278 ServerList getNameServerFromConf();
Ratan Gupta6dec3902017-08-20 15:28:12 +0530279
Gunnar Mills57d9c502018-09-14 14:42:34 -0500280 /** @brief Persistent sdbusplus DBus bus connection. */
281 sdbusplus::bus::bus& bus;
Ratan Gupta82549cc2017-04-21 08:45:23 +0530282
Gunnar Mills57d9c502018-09-14 14:42:34 -0500283 /** @brief Network Manager object. */
284 Manager& manager;
Ratan Gupta4f1c18b2017-05-25 12:59:35 +0530285
Gunnar Mills57d9c502018-09-14 14:42:34 -0500286 /** @brief Persistent map of IPAddress dbus objects and their names */
287 AddressMap addrs;
Ratan Gupta82549cc2017-04-21 08:45:23 +0530288
William A. Kennington III08505792019-01-30 16:00:04 -0800289 /** @brief Persistent map of Neighbor dbus objects and their names */
290 NeighborMap staticNeighbors;
291
Gunnar Mills57d9c502018-09-14 14:42:34 -0500292 /** @brief Persistent map of VLAN interface dbus objects and their names */
293 VlanInterfaceMap vlanInterfaces;
Ratan Gupta5978dd12017-07-25 13:47:13 +0530294
Gunnar Mills57d9c502018-09-14 14:42:34 -0500295 /** @brief Dbus object path */
296 std::string objPath;
Ratan Gupta47722dc2017-05-26 18:32:23 +0530297
Gunnar Mills57d9c502018-09-14 14:42:34 -0500298 friend class TestEthernetInterface;
Ratan Gupta8c834932017-04-14 16:30:24 +0530299};
300
301} // namespace network
302} // namespace phosphor