1 #pragma once 2 3 #include "types.hpp" 4 #include "util.hpp" 5 #include "xyz/openbmc_project/Network/IP/Create/server.hpp" 6 #include "xyz/openbmc_project/Network/Neighbor/CreateStatic/server.hpp" 7 8 #include <filesystem> 9 #include <sdbusplus/bus.hpp> 10 #include <sdbusplus/server/object.hpp> 11 #include <string> 12 #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> 15 16 namespace phosphor 17 { 18 namespace network 19 { 20 21 using 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, 25 sdbusplus::xyz::openbmc_project::Network::Neighbor::server::CreateStatic, 26 sdbusplus::xyz::openbmc_project::Collection::server::DeleteAll>; 27 28 using IP = sdbusplus::xyz::openbmc_project::Network::server::IP; 29 30 using EthernetInterfaceIntf = 31 sdbusplus::xyz::openbmc_project::Network::server::EthernetInterface; 32 using MacAddressIntf = 33 sdbusplus::xyz::openbmc_project::Network::server::MACAddress; 34 35 using ServerList = std::vector<std::string>; 36 using ObjectPath = sdbusplus::message::object_path; 37 38 namespace fs = std::filesystem; 39 40 class Manager; // forward declaration of network manager. 41 42 class TestEthernetInterface; 43 44 class VlanInterface; 45 46 class IPAddress; 47 48 class Neighbor; 49 50 using LinkSpeed = uint16_t; 51 using DuplexMode = uint8_t; 52 using Autoneg = uint8_t; 53 using LinkUp = bool; 54 using NICEnabled = bool; 55 using MTU = size_t; 56 using VlanId = uint32_t; 57 using InterfaceName = std::string; 58 using InterfaceInfo = 59 std::tuple<LinkSpeed, DuplexMode, Autoneg, LinkUp, NICEnabled, MTU>; 60 using AddressMap = std::map<std::string, std::shared_ptr<IPAddress>>; 61 using NeighborMap = std::map<std::string, std::shared_ptr<Neighbor>>; 62 using VlanInterfaceMap = 63 std::map<InterfaceName, std::unique_ptr<VlanInterface>>; 64 65 /** @class EthernetInterface 66 * @brief OpenBMC Ethernet Interface implementation. 67 * @details A concrete implementation for the 68 * xyz.openbmc_project.Network.EthernetInterface DBus API. 69 */ 70 class EthernetInterface : public Ifaces 71 { 72 public: 73 EthernetInterface() = delete; 74 EthernetInterface(const EthernetInterface&) = delete; 75 EthernetInterface& operator=(const EthernetInterface&) = delete; 76 EthernetInterface(EthernetInterface&&) = delete; 77 EthernetInterface& operator=(EthernetInterface&&) = delete; 78 virtual ~EthernetInterface() = default; 79 80 /** @brief Constructor to put object onto bus at a dbus path. 81 * @param[in] bus - Bus to attach to. 82 * @param[in] objPath - Path to attach at. 83 * @param[in] dhcpEnabled - is dhcp enabled(true/false). 84 * @param[in] parent - parent object. 85 * @param[in] emitSignal - true if the object added signal needs to be 86 * send. 87 * @param[in] enabled - Override the lookup of nicEnabled 88 */ 89 EthernetInterface(sdbusplus::bus::bus& bus, const std::string& objPath, 90 DHCPConf dhcpEnabled, Manager& parent, 91 bool emitSignal = true, 92 std::optional<bool> enabled = std::nullopt); 93 94 /** @brief Function used to load the nameservers. 95 */ 96 virtual void loadNameServers(); 97 98 /** @brief Function to create ipAddress dbus object. 99 * @param[in] addressType - Type of ip address. 100 * @param[in] ipAddress- IP address. 101 * @param[in] prefixLength - Length of prefix. 102 * @param[in] gateway - Gateway ip address. 103 */ 104 105 ObjectPath ip(IP::Protocol addressType, std::string ipAddress, 106 uint8_t prefixLength, std::string gateway) override; 107 108 /** @brief Function to create static neighbor dbus object. 109 * @param[in] ipAddress - IP address. 110 * @param[in] macAddress - Low level MAC address. 111 */ 112 ObjectPath neighbor(std::string ipAddress, std::string macAddress) override; 113 114 /* @brief delete the dbus object of the given ipAddress. 115 * @param[in] ipAddress - IP address. 116 */ 117 void deleteObject(const std::string& ipAddress); 118 119 /* @brief delete the dbus object of the given ipAddress. 120 * @param[in] ipAddress - IP address. 121 */ 122 void deleteStaticNeighborObject(const std::string& ipAddress); 123 124 /* @brief delete the vlan dbus object of the given interface. 125 * Also deletes the device file and the network file. 126 * @param[in] interface - VLAN Interface. 127 */ 128 void deleteVLANObject(const std::string& interface); 129 130 /* @brief creates the dbus object(IPaddres) given in the address list. 131 * @param[in] addrs - address list for which dbus objects needs 132 * to create. 133 */ 134 void createIPAddressObjects(); 135 136 /* @brief creates the dbus object(Neighbor) given in the neighbor list. 137 */ 138 void createStaticNeighborObjects(); 139 140 /* @brief Gets the index of the interface on the system 141 */ 142 unsigned ifIndex() const; 143 144 /* @brief Gets all the ip addresses. 145 * @returns the list of ipAddress. 146 */ 147 const AddressMap& getAddresses() const 148 { 149 return addrs; 150 } 151 152 /* @brief Gets all the static neighbor entries. 153 * @returns Static neighbor map. 154 */ 155 const NeighborMap& getStaticNeighbors() const 156 { 157 return staticNeighbors; 158 } 159 160 /** Set value of DHCPEnabled */ 161 DHCPConf dhcpEnabled(DHCPConf value) override; 162 163 /** @brief Selectively disables DHCP 164 * @param[in] protocol - The IPv4 or IPv6 protocol to return to static 165 * addressing mode 166 */ 167 void disableDHCP(IP::Protocol protocol); 168 169 /** Retrieve Link State */ 170 bool linkUp() const override; 171 172 /** Retrieve MTU Size */ 173 size_t mtu() const override; 174 175 /** Set size of MTU */ 176 size_t mtu(size_t value) override; 177 178 /** Set value of NICEnabled */ 179 bool nicEnabled(bool value) override; 180 181 /** @brief sets the MAC address. 182 * @param[in] value - MAC address which needs to be set on the system. 183 * @returns macAddress of the interface or throws an error. 184 */ 185 std::string macAddress(std::string value) override; 186 187 /** @brief get the IPv6AcceptRA flag from the network configuration file 188 * 189 */ 190 bool getIPv6AcceptRAFromConf(); 191 192 /** @brief check conf file for Router Advertisements 193 * 194 */ 195 bool ipv6AcceptRA(bool value) override; 196 197 /** @brief sets the NTP servers. 198 * @param[in] value - vector of NTP servers. 199 */ 200 ServerList ntpServers(ServerList value) override; 201 202 /** @brief sets the DNS/nameservers. 203 * @param[in] value - vector of DNS servers. 204 */ 205 ServerList nameservers(ServerList value) override; 206 207 /** @brief sets the Static DNS/nameservers. 208 * @param[in] value - vector of DNS servers. 209 */ 210 211 ServerList staticNameServers(ServerList value) override; 212 213 /** @brief create Vlan interface. 214 * @param[in] id- VLAN identifier. 215 */ 216 ObjectPath createVLAN(VlanId id); 217 218 /** @brief load the vlan info from the system 219 * and creates the ip address dbus objects. 220 * @param[in] vlanID- VLAN identifier. 221 */ 222 void loadVLAN(VlanId vlanID); 223 224 /** @brief write the network conf file with the in-memory objects. 225 */ 226 void writeConfigurationFile(); 227 228 /** @brief delete all dbus objects. 229 */ 230 void deleteAll(); 231 232 /** @brief set the default v4 gateway of the interface. 233 * @param[in] gateway - default v4 gateway of the interface. 234 */ 235 std::string defaultGateway(std::string gateway) override; 236 237 /** @brief set the default v6 gateway of the interface. 238 * @param[in] gateway - default v6 gateway of the interface. 239 */ 240 std::string defaultGateway6(std::string gateway) override; 241 242 using EthernetInterfaceIntf::dhcpEnabled; 243 using EthernetInterfaceIntf::interfaceName; 244 using EthernetInterfaceIntf::linkUp; 245 using EthernetInterfaceIntf::mtu; 246 using EthernetInterfaceIntf::nicEnabled; 247 using MacAddressIntf::macAddress; 248 249 using EthernetInterfaceIntf::defaultGateway; 250 using EthernetInterfaceIntf::defaultGateway6; 251 /** @brief Absolute path of the resolv conf file */ 252 static constexpr auto resolvConfFile = "/etc/resolv.conf"; 253 254 protected: 255 /** @brief get the info of the ethernet interface. 256 * @return tuple having the link speed,autonegotiation,duplexmode . 257 */ 258 InterfaceInfo getInterfaceInfo() const; 259 260 /* @brief delete the vlan interface from system. 261 * @param[in] interface - vlan Interface. 262 */ 263 void deleteVLANFromSystem(const std::string& interface); 264 265 /** @brief get the mac address of the interface. 266 * @param[in] interfaceName - Network interface name. 267 * @return macaddress on success 268 */ 269 270 std::string getMACAddress(const std::string& interfaceName) const; 271 272 /** @brief construct the ip address dbus object path. 273 * @param[in] addressType - Type of ip address. 274 * @param[in] ipAddress - IP address. 275 * @param[in] prefixLength - Length of prefix. 276 * @param[in] gateway - Gateway address. 277 * @param[in] origin - The origin entry of the IP::Address 278 279 * @return path of the address object. 280 */ 281 282 std::string generateObjectPath(IP::Protocol addressType, 283 const std::string& ipAddress, 284 uint8_t prefixLength, 285 const std::string& gateway, 286 IP::AddressOrigin origin) const; 287 288 std::string 289 generateStaticNeighborObjectPath(const std::string& ipAddress, 290 const std::string& macAddress) const; 291 292 /** @brief generates the id by doing hash of ipAddress, 293 * prefixlength and the gateway. 294 * @param[in] ipAddress - IP address. 295 * @param[in] prefixLength - Length of prefix. 296 * @param[in] gateway - Gateway address. 297 * @param[in] origin - The string of the address origin 298 * @return hash string. 299 */ 300 301 static std::string generateId(const std::string& ipAddress, 302 uint8_t prefixLength, 303 const std::string& gateway, 304 const std::string& origin); 305 306 /** @brief generates the id by doing hash of ipAddress 307 * and the mac address. 308 * @param[in] ipAddress - IP address. 309 * @param[in] macAddress - Gateway address. 310 * @return hash string. 311 */ 312 static std::string generateNeighborId(const std::string& ipAddress, 313 const std::string& macAddress); 314 315 /** @brief write the dhcp section **/ 316 void writeDHCPSection(std::fstream& stream); 317 318 /** @brief get the NTP server list from the network conf 319 * 320 */ 321 ServerList getNTPServersFromConf(); 322 323 /** @brief get the name server details from the network conf 324 * 325 */ 326 virtual ServerList getNameServerFromResolvd(); 327 ServerList getstaticNameServerFromConf(); 328 329 /** @brief Persistent sdbusplus DBus bus connection. */ 330 sdbusplus::bus::bus& bus; 331 332 /** @brief Network Manager object. */ 333 Manager& manager; 334 335 /** @brief Persistent map of IPAddress dbus objects and their names */ 336 AddressMap addrs; 337 338 /** @brief Persistent map of Neighbor dbus objects and their names */ 339 NeighborMap staticNeighbors; 340 341 /** @brief Persistent map of VLAN interface dbus objects and their names */ 342 VlanInterfaceMap vlanInterfaces; 343 344 /** @brief Dbus object path */ 345 std::string objPath; 346 347 friend class TestEthernetInterface; 348 349 private: 350 /** @brief Determines if DHCP is active for the IP::Protocol supplied. 351 * @param[in] protocol - Either IPv4 or IPv6 352 * @returns true/false value if DHCP is active for the input protocol 353 */ 354 bool dhcpIsEnabled(IP::Protocol protocol); 355 356 /** @brief Determines if the address is manually assigned 357 * @param[in] origin - The origin entry of the IP::Address 358 * @returns true/false value if the address is static 359 */ 360 bool originIsManuallyAssigned(IP::AddressOrigin origin); 361 362 /** @brief Determines if the NIC is enabled in systemd 363 * @returns true/false value if the NIC is enabled 364 */ 365 bool queryNicEnabled() const; 366 }; 367 368 } // namespace network 369 } // namespace phosphor 370