| Ratan Gupta | 05eb109 | 2017-04-14 16:33:53 +0530 | [diff] [blame] | 1 | #pragma once | 
| Patrick Venture | 189d44e | 2018-07-09 12:30:59 -0700 | [diff] [blame] | 2 | #include "dhcp_configuration.hpp" | 
| Ratan Gupta | 05eb109 | 2017-04-14 16:33:53 +0530 | [diff] [blame] | 3 | #include "ethernet_interface.hpp" | 
| William A. Kennington III | e056484 | 2021-10-23 16:02:22 -0700 | [diff] [blame] | 4 | #include "routing_table.hpp" | 
| Ratan Gupta | ef85eb9 | 2017-06-15 08:57:54 +0530 | [diff] [blame] | 5 | #include "system_configuration.hpp" | 
| William A. Kennington III | dd9ef81 | 2022-10-05 02:08:02 -0700 | [diff] [blame] | 6 | #include "types.hpp" | 
| Patrick Venture | 189d44e | 2018-07-09 12:30:59 -0700 | [diff] [blame] | 7 | #include "xyz/openbmc_project/Network/VLAN/Create/server.hpp" | 
| Ratan Gupta | 5978dd1 | 2017-07-25 13:47:13 +0530 | [diff] [blame] | 8 |  | 
| Manojkiran Eda | a879baa | 2020-06-13 14:39:08 +0530 | [diff] [blame] | 9 | #include <filesystem> | 
| William A. Kennington III | 6ff633a | 2021-11-09 17:09:12 -0800 | [diff] [blame] | 10 | #include <function2/function2.hpp> | 
| Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 11 | #include <memory> | 
| Patrick Venture | 189d44e | 2018-07-09 12:30:59 -0700 | [diff] [blame] | 12 | #include <sdbusplus/bus.hpp> | 
| William A. Kennington III | 80d2901 | 2022-11-12 02:31:40 -0800 | [diff] [blame] | 13 | #include <sdbusplus/bus/match.hpp> | 
| Ratan Gupta | 05eb109 | 2017-04-14 16:33:53 +0530 | [diff] [blame] | 14 | #include <string> | 
| William A. Kennington III | dd9ef81 | 2022-10-05 02:08:02 -0700 | [diff] [blame] | 15 | #include <string_view> | 
| Ratan Gupta | 05eb109 | 2017-04-14 16:33:53 +0530 | [diff] [blame] | 16 | #include <vector> | 
| Patrick Venture | 189d44e | 2018-07-09 12:30:59 -0700 | [diff] [blame] | 17 | #include <xyz/openbmc_project/Common/FactoryReset/server.hpp> | 
| Ratan Gupta | 05eb109 | 2017-04-14 16:33:53 +0530 | [diff] [blame] | 18 |  | 
|  | 19 | namespace phosphor | 
|  | 20 | { | 
|  | 21 | namespace network | 
|  | 22 | { | 
|  | 23 |  | 
| Ratan Gupta | 2b10653 | 2017-07-25 16:05:02 +0530 | [diff] [blame] | 24 | using SystemConfPtr = std::unique_ptr<SystemConfiguration>; | 
|  | 25 | using DHCPConfPtr = std::unique_ptr<dhcp::Configuration>; | 
|  | 26 |  | 
| Manojkiran Eda | a879baa | 2020-06-13 14:39:08 +0530 | [diff] [blame] | 27 | namespace fs = std::filesystem; | 
| Ratan Gupta | 05eb109 | 2017-04-14 16:33:53 +0530 | [diff] [blame] | 28 | namespace details | 
|  | 29 | { | 
|  | 30 |  | 
| Michael Tritz | 29f2fd6 | 2017-05-22 15:27:26 -0500 | [diff] [blame] | 31 | template <typename T, typename U> | 
| Patrick Williams | c38b071 | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 32 | using ServerObject = typename sdbusplus::server::object_t<T, U>; | 
| Ratan Gupta | 05eb109 | 2017-04-14 16:33:53 +0530 | [diff] [blame] | 33 |  | 
| Michael Tritz | 29f2fd6 | 2017-05-22 15:27:26 -0500 | [diff] [blame] | 34 | using VLANCreateIface = details::ServerObject< | 
|  | 35 | sdbusplus::xyz::openbmc_project::Network::VLAN::server::Create, | 
|  | 36 | sdbusplus::xyz::openbmc_project::Common::server::FactoryReset>; | 
| Ratan Gupta | 05eb109 | 2017-04-14 16:33:53 +0530 | [diff] [blame] | 37 |  | 
| Ratan Gupta | 05eb109 | 2017-04-14 16:33:53 +0530 | [diff] [blame] | 38 | } // namespace details | 
|  | 39 |  | 
|  | 40 | /** @class Manager | 
|  | 41 | *  @brief OpenBMC network manager implementation. | 
|  | 42 | */ | 
|  | 43 | class Manager : public details::VLANCreateIface | 
|  | 44 | { | 
| Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 45 | public: | 
| Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 46 | Manager(const Manager&) = delete; | 
|  | 47 | Manager& operator=(const Manager&) = delete; | 
|  | 48 | Manager(Manager&&) = delete; | 
|  | 49 | Manager& operator=(Manager&&) = delete; | 
|  | 50 | virtual ~Manager() = default; | 
| Ratan Gupta | 05eb109 | 2017-04-14 16:33:53 +0530 | [diff] [blame] | 51 |  | 
| Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 52 | /** @brief Constructor to put object onto bus at a dbus path. | 
|  | 53 | *  @param[in] bus - Bus to attach to. | 
|  | 54 | *  @param[in] objPath - Path to attach at. | 
| William A. Kennington III | be3bd2f | 2022-10-11 14:11:27 -0700 | [diff] [blame] | 55 | *  @param[in] confDir - Network Configuration directory path. | 
| Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 56 | */ | 
| William A. Kennington III | be3bd2f | 2022-10-11 14:11:27 -0700 | [diff] [blame] | 57 | Manager(sdbusplus::bus_t& bus, const char* objPath, | 
|  | 58 | const fs::path& confDir); | 
| Ratan Gupta | 05eb109 | 2017-04-14 16:33:53 +0530 | [diff] [blame] | 59 |  | 
| William A. Kennington III | dd9ef81 | 2022-10-05 02:08:02 -0700 | [diff] [blame] | 60 | ObjectPath vlan(std::string interfaceName, uint32_t id) override; | 
| Ratan Gupta | 05eb109 | 2017-04-14 16:33:53 +0530 | [diff] [blame] | 61 |  | 
| Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 62 | /** @brief write the network conf file with the in-memory objects. | 
|  | 63 | */ | 
|  | 64 | void writeToConfigurationFile(); | 
| Ratan Gupta | 4f1c18b | 2017-05-25 12:59:35 +0530 | [diff] [blame] | 65 |  | 
| William A. Kennington III | 80d2901 | 2022-11-12 02:31:40 -0800 | [diff] [blame] | 66 | /** @brief Adds a single interface to the interface map */ | 
| William A. Kennington III | 0813a24 | 2022-11-12 18:07:11 -0800 | [diff] [blame] | 67 | void addInterface(const InterfaceInfo& info); | 
|  | 68 | void removeInterface(const InterfaceInfo& info); | 
| William A. Kennington III | 80d2901 | 2022-11-12 02:31:40 -0800 | [diff] [blame] | 69 |  | 
| William A. Kennington III | ed5ff47 | 2022-11-12 16:24:02 -0800 | [diff] [blame] | 70 | /** @brief Add / remove an address to the interface or queue */ | 
|  | 71 | void addAddress(const AddressInfo& info); | 
|  | 72 | void removeAddress(const AddressInfo& info); | 
|  | 73 |  | 
|  | 74 | /** @brief Add / remove a neighbor to the interface or queue */ | 
|  | 75 | void addNeighbor(const NeighborInfo& info); | 
|  | 76 | void removeNeighbor(const NeighborInfo& info); | 
|  | 77 |  | 
|  | 78 | /** @brief Add / remove default gateway for interface */ | 
|  | 79 | void addDefGw(unsigned ifidx, InAddrAny addr); | 
|  | 80 | void removeDefGw(unsigned ifidx, InAddrAny addr); | 
|  | 81 |  | 
| Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 82 | /** @brief Fetch the interface and the ipaddress details | 
|  | 83 | *         from the system and create the ethernet interraces | 
|  | 84 | *         dbus object. | 
|  | 85 | */ | 
| Manojkiran Eda | acd6dd5 | 2019-10-15 15:00:51 +0530 | [diff] [blame] | 86 | virtual void createInterfaces(); | 
| Ratan Gupta | 29b0e43 | 2017-05-25 12:51:40 +0530 | [diff] [blame] | 87 |  | 
| Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 88 | /** @brief create child interface object and the system conf object. | 
|  | 89 | */ | 
|  | 90 | void createChildObjects(); | 
| Ratan Gupta | ef85eb9 | 2017-06-15 08:57:54 +0530 | [diff] [blame] | 91 |  | 
| Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 92 | /** @brief sets the network conf directory. | 
|  | 93 | *  @param[in] dirName - Absolute path of the directory. | 
|  | 94 | */ | 
|  | 95 | void setConfDir(const fs::path& dir); | 
| Ratan Gupta | ef85eb9 | 2017-06-15 08:57:54 +0530 | [diff] [blame] | 96 |  | 
| Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 97 | /** @brief gets the network conf directory. | 
|  | 98 | */ | 
| William A. Kennington III | 7213180 | 2022-10-05 02:08:36 -0700 | [diff] [blame] | 99 | inline const fs::path& getConfDir() const | 
| Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 100 | { | 
|  | 101 | return confDir; | 
|  | 102 | } | 
| Ratan Gupta | 255d514 | 2017-08-10 09:02:08 +0530 | [diff] [blame] | 103 |  | 
| Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 104 | /** @brief gets the system conf object. | 
|  | 105 | * | 
|  | 106 | */ | 
| William A. Kennington III | 7213180 | 2022-10-05 02:08:36 -0700 | [diff] [blame] | 107 | inline const SystemConfPtr& getSystemConf() | 
| Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 108 | { | 
|  | 109 | return systemConf; | 
|  | 110 | } | 
| Ratan Gupta | 05eb109 | 2017-04-14 16:33:53 +0530 | [diff] [blame] | 111 |  | 
| Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 112 | /** @brief gets the dhcp conf object. | 
|  | 113 | * | 
|  | 114 | */ | 
| William A. Kennington III | 7213180 | 2022-10-05 02:08:36 -0700 | [diff] [blame] | 115 | inline const DHCPConfPtr& getDHCPConf() | 
| Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 116 | { | 
|  | 117 | return dhcpConf; | 
|  | 118 | } | 
| Ratan Gupta | 2b10653 | 2017-07-25 16:05:02 +0530 | [diff] [blame] | 119 |  | 
| Manojkiran Eda | cc099a8 | 2020-05-11 14:25:16 +0530 | [diff] [blame] | 120 | /** @brief This function gets the MAC address from the VPD and | 
|  | 121 | *  sets it on the corresponding ethernet interface during first | 
|  | 122 | *  Boot, once it sets the MAC from VPD, it creates a file named | 
|  | 123 | *  firstBoot under /var/lib to make sure we dont run this function | 
|  | 124 | *  again. | 
|  | 125 | * | 
|  | 126 | *  @param[in] ethPair - Its a pair of ethernet interface name & the | 
|  | 127 | * corresponding MAC Address from the VPD | 
|  | 128 | * | 
|  | 129 | *  return - NULL | 
|  | 130 | */ | 
|  | 131 | void setFistBootMACOnInterface( | 
|  | 132 | const std::pair<std::string, std::string>& ethPair); | 
|  | 133 |  | 
| William A. Kennington III | c7cf25f | 2021-11-09 16:16:59 -0800 | [diff] [blame] | 134 | /** @brief Arms a timer to tell systemd-network to reload all of the network | 
|  | 135 | * configurations | 
| William A. Kennington III | 56ecc78 | 2021-10-07 18:44:50 -0700 | [diff] [blame] | 136 | */ | 
| William A. Kennington III | 85dc57a | 2022-11-07 16:53:24 -0800 | [diff] [blame] | 137 | virtual void reloadConfigsNoRefresh(); | 
| William A. Kennington III | 56ecc78 | 2021-10-07 18:44:50 -0700 | [diff] [blame] | 138 | virtual void reloadConfigs(); | 
|  | 139 |  | 
| William A. Kennington III | c7cf25f | 2021-11-09 16:16:59 -0800 | [diff] [blame] | 140 | /** @brief Tell systemd-network to reload all of the network configurations | 
|  | 141 | */ | 
|  | 142 | void doReloadConfigs(); | 
|  | 143 |  | 
| William A. Kennington III | 09f3a4a | 2022-10-25 02:59:16 -0700 | [diff] [blame] | 144 | /** @brief Persistent map of EthernetInterface dbus objects and their names | 
| Patrick Venture | d94d23e | 2019-01-07 13:05:23 -0800 | [diff] [blame] | 145 | */ | 
| William A. Kennington III | 09f3a4a | 2022-10-25 02:59:16 -0700 | [diff] [blame] | 146 | string_umap<std::unique_ptr<EthernetInterface>> interfaces; | 
| William A. Kennington III | 67b09da | 2022-10-31 14:09:53 -0700 | [diff] [blame] | 147 | std::unordered_map<unsigned, EthernetInterface*> interfacesByIdx; | 
| Patrick Venture | d94d23e | 2019-01-07 13:05:23 -0800 | [diff] [blame] | 148 |  | 
| William A. Kennington III | e056484 | 2021-10-23 16:02:22 -0700 | [diff] [blame] | 149 | /** @brief Get the routing table owned by the manager | 
|  | 150 | * | 
|  | 151 | * @return Routing table reference. | 
|  | 152 | */ | 
|  | 153 | inline const auto& getRouteTable() const | 
|  | 154 | { | 
|  | 155 | return routeTable; | 
|  | 156 | } | 
|  | 157 |  | 
| William A. Kennington III | 6ff633a | 2021-11-09 17:09:12 -0800 | [diff] [blame] | 158 | /** @brief Adds a hook that runs immediately prior to reloading | 
|  | 159 | * | 
|  | 160 | *  @param[in] hook - The hook to execute before reloading | 
|  | 161 | */ | 
|  | 162 | inline void addReloadPreHook(fu2::unique_function<void()>&& hook) | 
|  | 163 | { | 
|  | 164 | reloadPreHooks.push_back(std::move(hook)); | 
|  | 165 | } | 
|  | 166 |  | 
| Ratan Gupta | 3529717 | 2018-11-28 18:40:16 +0530 | [diff] [blame] | 167 | protected: | 
| Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 168 | /** @brief Persistent sdbusplus DBus bus connection. */ | 
| Patrick Williams | c38b071 | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 169 | sdbusplus::bus_t& bus; | 
| Ratan Gupta | 29b0e43 | 2017-05-25 12:51:40 +0530 | [diff] [blame] | 170 |  | 
| Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 171 | /** @brief BMC network reset - resets network configuration for BMC. */ | 
|  | 172 | void reset() override; | 
| Michael Tritz | 29f2fd6 | 2017-05-22 15:27:26 -0500 | [diff] [blame] | 173 |  | 
| Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 174 | /** @brief Path of Object. */ | 
|  | 175 | std::string objectPath; | 
| Ratan Gupta | 29b0e43 | 2017-05-25 12:51:40 +0530 | [diff] [blame] | 176 |  | 
| Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 177 | /** @brief pointer to system conf object. */ | 
|  | 178 | SystemConfPtr systemConf = nullptr; | 
| Ratan Gupta | ef85eb9 | 2017-06-15 08:57:54 +0530 | [diff] [blame] | 179 |  | 
| Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 180 | /** @brief pointer to dhcp conf object. */ | 
|  | 181 | DHCPConfPtr dhcpConf = nullptr; | 
| Ratan Gupta | d16f88c | 2017-07-11 17:47:57 +0530 | [diff] [blame] | 182 |  | 
| Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 183 | /** @brief Network Configuration directory. */ | 
|  | 184 | fs::path confDir; | 
| William A. Kennington III | e056484 | 2021-10-23 16:02:22 -0700 | [diff] [blame] | 185 |  | 
|  | 186 | /** @brief The routing table */ | 
|  | 187 | route::Table routeTable; | 
| William A. Kennington III | 6ff633a | 2021-11-09 17:09:12 -0800 | [diff] [blame] | 188 |  | 
| William A. Kennington III | 80d2901 | 2022-11-12 02:31:40 -0800 | [diff] [blame] | 189 | /** @brief Map of interface info for undiscovered interfaces */ | 
| William A. Kennington III | f30d560 | 2022-11-13 17:09:55 -0800 | [diff] [blame] | 190 | struct UndiscoveredInfo | 
|  | 191 | { | 
|  | 192 | InterfaceInfo intf; | 
|  | 193 | std::optional<in_addr> defgw4 = std::nullopt; | 
|  | 194 | std::optional<in6_addr> defgw6 = std::nullopt; | 
|  | 195 | std::unordered_map<IfAddr, AddressInfo> addrs = {}; | 
|  | 196 | std::unordered_map<InAddrAny, NeighborInfo> staticNeighs = {}; | 
|  | 197 | }; | 
|  | 198 | std::unordered_map<unsigned, UndiscoveredInfo> undiscoveredIntfInfo; | 
| William A. Kennington III | 80d2901 | 2022-11-12 02:31:40 -0800 | [diff] [blame] | 199 |  | 
|  | 200 | /** @brief Map of enabled interfaces */ | 
|  | 201 | std::unordered_map<unsigned, bool> systemdNetworkdEnabled; | 
|  | 202 | sdbusplus::bus::match_t systemdNetworkdEnabledMatch; | 
|  | 203 |  | 
| William A. Kennington III | 6ff633a | 2021-11-09 17:09:12 -0800 | [diff] [blame] | 204 | /** @brief List of hooks to execute during the next reload */ | 
|  | 205 | std::vector<fu2::unique_function<void()>> reloadPreHooks; | 
| William A. Kennington III | 80d2901 | 2022-11-12 02:31:40 -0800 | [diff] [blame] | 206 |  | 
|  | 207 | /** @brief Handles the recipt of an adminstrative state string */ | 
|  | 208 | void handleAdminState(std::string_view state, unsigned ifidx); | 
| William A. Kennington III | 0813a24 | 2022-11-12 18:07:11 -0800 | [diff] [blame] | 209 |  | 
|  | 210 | /** @brief Creates the interface in the maps */ | 
| William A. Kennington III | f30d560 | 2022-11-13 17:09:55 -0800 | [diff] [blame] | 211 | void createInterface(const UndiscoveredInfo& info, bool enabled); | 
| Ratan Gupta | 05eb109 | 2017-04-14 16:33:53 +0530 | [diff] [blame] | 212 | }; | 
|  | 213 |  | 
|  | 214 | } // namespace network | 
|  | 215 | } // namespace phosphor |