Ratan Gupta | 05eb109 | 2017-04-14 16:33:53 +0530 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Patrick Venture | 189d44e | 2018-07-09 12:30:59 -0700 | [diff] [blame] | 3 | #include "dhcp_configuration.hpp" |
Ratan Gupta | 05eb109 | 2017-04-14 16:33:53 +0530 | [diff] [blame] | 4 | #include "ethernet_interface.hpp" |
William A. Kennington III | e056484 | 2021-10-23 16:02:22 -0700 | [diff] [blame] | 5 | #include "routing_table.hpp" |
Ratan Gupta | ef85eb9 | 2017-06-15 08:57:54 +0530 | [diff] [blame] | 6 | #include "system_configuration.hpp" |
Ratan Gupta | 5978dd1 | 2017-07-25 13:47:13 +0530 | [diff] [blame] | 7 | #include "vlan_interface.hpp" |
Patrick Venture | 189d44e | 2018-07-09 12:30:59 -0700 | [diff] [blame] | 8 | #include "xyz/openbmc_project/Network/VLAN/Create/server.hpp" |
Ratan Gupta | 5978dd1 | 2017-07-25 13:47:13 +0530 | [diff] [blame] | 9 | |
Manojkiran Eda | a879baa | 2020-06-13 14:39:08 +0530 | [diff] [blame] | 10 | #include <filesystem> |
William A. Kennington III | 6ff633a | 2021-11-09 17:09:12 -0800 | [diff] [blame] | 11 | #include <function2/function2.hpp> |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 12 | #include <memory> |
Patrick Venture | 189d44e | 2018-07-09 12:30:59 -0700 | [diff] [blame] | 13 | #include <sdbusplus/bus.hpp> |
Ratan Gupta | 05eb109 | 2017-04-14 16:33:53 +0530 | [diff] [blame] | 14 | #include <string> |
William A. Kennington III | 6ff633a | 2021-11-09 17:09:12 -0800 | [diff] [blame] | 15 | #include <utility> |
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: |
| 46 | Manager() = delete; |
| 47 | Manager(const Manager&) = delete; |
| 48 | Manager& operator=(const Manager&) = delete; |
| 49 | Manager(Manager&&) = delete; |
| 50 | Manager& operator=(Manager&&) = delete; |
| 51 | virtual ~Manager() = default; |
Ratan Gupta | 05eb109 | 2017-04-14 16:33:53 +0530 | [diff] [blame] | 52 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 53 | /** @brief Constructor to put object onto bus at a dbus path. |
| 54 | * @param[in] bus - Bus to attach to. |
| 55 | * @param[in] objPath - Path to attach at. |
| 56 | * @param[in] dir - Network Configuration directory path. |
| 57 | */ |
Patrick Williams | c38b071 | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 58 | Manager(sdbusplus::bus_t& bus, const char* objPath, const std::string& dir); |
Ratan Gupta | 05eb109 | 2017-04-14 16:33:53 +0530 | [diff] [blame] | 59 | |
Patrick Williams | 8173e02 | 2021-05-01 06:39:41 -0500 | [diff] [blame] | 60 | ObjectPath vlan(IntfName 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 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 66 | /** @brief Fetch the interface and the ipaddress details |
| 67 | * from the system and create the ethernet interraces |
| 68 | * dbus object. |
| 69 | */ |
Manojkiran Eda | acd6dd5 | 2019-10-15 15:00:51 +0530 | [diff] [blame] | 70 | virtual void createInterfaces(); |
Ratan Gupta | 29b0e43 | 2017-05-25 12:51:40 +0530 | [diff] [blame] | 71 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 72 | /** @brief create child interface object and the system conf object. |
| 73 | */ |
| 74 | void createChildObjects(); |
Ratan Gupta | ef85eb9 | 2017-06-15 08:57:54 +0530 | [diff] [blame] | 75 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 76 | /** @brief sets the network conf directory. |
| 77 | * @param[in] dirName - Absolute path of the directory. |
| 78 | */ |
| 79 | void setConfDir(const fs::path& dir); |
Ratan Gupta | ef85eb9 | 2017-06-15 08:57:54 +0530 | [diff] [blame] | 80 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 81 | /** @brief gets the network conf directory. |
| 82 | */ |
| 83 | fs::path getConfDir() |
| 84 | { |
| 85 | return confDir; |
| 86 | } |
Ratan Gupta | 255d514 | 2017-08-10 09:02:08 +0530 | [diff] [blame] | 87 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 88 | /** @brief gets the system conf object. |
| 89 | * |
| 90 | */ |
| 91 | const SystemConfPtr& getSystemConf() |
| 92 | { |
| 93 | return systemConf; |
| 94 | } |
Ratan Gupta | 05eb109 | 2017-04-14 16:33:53 +0530 | [diff] [blame] | 95 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 96 | /** @brief gets the dhcp conf object. |
| 97 | * |
| 98 | */ |
| 99 | const DHCPConfPtr& getDHCPConf() |
| 100 | { |
| 101 | return dhcpConf; |
| 102 | } |
Ratan Gupta | 2b10653 | 2017-07-25 16:05:02 +0530 | [diff] [blame] | 103 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 104 | /** @brief create the default network files for each interface |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 105 | * @return true if network file created else false |
| 106 | */ |
William A. Kennington III | 9a1d9af | 2021-11-09 17:51:05 -0800 | [diff] [blame] | 107 | bool createDefaultNetworkFiles(); |
Ratan Gupta | 70c7e5b | 2017-07-12 11:41:55 +0530 | [diff] [blame] | 108 | |
Manojkiran Eda | cc099a8 | 2020-05-11 14:25:16 +0530 | [diff] [blame] | 109 | /** @brief This function gets the MAC address from the VPD and |
| 110 | * sets it on the corresponding ethernet interface during first |
| 111 | * Boot, once it sets the MAC from VPD, it creates a file named |
| 112 | * firstBoot under /var/lib to make sure we dont run this function |
| 113 | * again. |
| 114 | * |
| 115 | * @param[in] ethPair - Its a pair of ethernet interface name & the |
| 116 | * corresponding MAC Address from the VPD |
| 117 | * |
| 118 | * return - NULL |
| 119 | */ |
| 120 | void setFistBootMACOnInterface( |
| 121 | const std::pair<std::string, std::string>& ethPair); |
| 122 | |
William A. Kennington III | c7cf25f | 2021-11-09 16:16:59 -0800 | [diff] [blame] | 123 | /** @brief Arms a timer to tell systemd-network to reload all of the network |
| 124 | * configurations |
William A. Kennington III | 56ecc78 | 2021-10-07 18:44:50 -0700 | [diff] [blame] | 125 | */ |
| 126 | virtual void reloadConfigs(); |
| 127 | |
William A. Kennington III | c7cf25f | 2021-11-09 16:16:59 -0800 | [diff] [blame] | 128 | /** @brief Tell systemd-network to reload all of the network configurations |
| 129 | */ |
| 130 | void doReloadConfigs(); |
| 131 | |
Patrick Venture | d94d23e | 2019-01-07 13:05:23 -0800 | [diff] [blame] | 132 | /** @brief Returns the number of interfaces under this manager. |
| 133 | * |
| 134 | * @return the number of interfaces managed by this manager. |
| 135 | */ |
| 136 | int getInterfaceCount() |
| 137 | { |
| 138 | return interfaces.size(); |
| 139 | } |
| 140 | |
| 141 | /** @brief Does the requested interface exist under this manager? |
| 142 | * |
| 143 | * @param[in] intf - the interface name to check. |
| 144 | * @return true if found, false otherwise. |
| 145 | */ |
| 146 | bool hasInterface(const std::string& intf) |
| 147 | { |
| 148 | return (interfaces.find(intf) != interfaces.end()); |
| 149 | } |
| 150 | |
William A. Kennington III | e056484 | 2021-10-23 16:02:22 -0700 | [diff] [blame] | 151 | /** @brief Get the routing table owned by the manager |
| 152 | * |
| 153 | * @return Routing table reference. |
| 154 | */ |
| 155 | inline const auto& getRouteTable() const |
| 156 | { |
| 157 | return routeTable; |
| 158 | } |
| 159 | |
William A. Kennington III | 6ff633a | 2021-11-09 17:09:12 -0800 | [diff] [blame] | 160 | /** @brief Adds a hook that runs immediately prior to reloading |
| 161 | * |
| 162 | * @param[in] hook - The hook to execute before reloading |
| 163 | */ |
| 164 | inline void addReloadPreHook(fu2::unique_function<void()>&& hook) |
| 165 | { |
| 166 | reloadPreHooks.push_back(std::move(hook)); |
| 167 | } |
| 168 | |
Ratan Gupta | 3529717 | 2018-11-28 18:40:16 +0530 | [diff] [blame] | 169 | protected: |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 170 | /** @brief Persistent sdbusplus DBus bus connection. */ |
Patrick Williams | c38b071 | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 171 | sdbusplus::bus_t& bus; |
Ratan Gupta | 29b0e43 | 2017-05-25 12:51:40 +0530 | [diff] [blame] | 172 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 173 | /** @brief Persistent map of EthernetInterface dbus objects and their names |
| 174 | */ |
| 175 | std::map<IntfName, std::shared_ptr<EthernetInterface>> interfaces; |
Ratan Gupta | 05eb109 | 2017-04-14 16:33:53 +0530 | [diff] [blame] | 176 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 177 | /** @brief BMC network reset - resets network configuration for BMC. */ |
| 178 | void reset() override; |
Michael Tritz | 29f2fd6 | 2017-05-22 15:27:26 -0500 | [diff] [blame] | 179 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 180 | /** @brief Path of Object. */ |
| 181 | std::string objectPath; |
Ratan Gupta | 29b0e43 | 2017-05-25 12:51:40 +0530 | [diff] [blame] | 182 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 183 | /** @brief pointer to system conf object. */ |
| 184 | SystemConfPtr systemConf = nullptr; |
Ratan Gupta | ef85eb9 | 2017-06-15 08:57:54 +0530 | [diff] [blame] | 185 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 186 | /** @brief pointer to dhcp conf object. */ |
| 187 | DHCPConfPtr dhcpConf = nullptr; |
Ratan Gupta | d16f88c | 2017-07-11 17:47:57 +0530 | [diff] [blame] | 188 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 189 | /** @brief Network Configuration directory. */ |
| 190 | fs::path confDir; |
William A. Kennington III | e056484 | 2021-10-23 16:02:22 -0700 | [diff] [blame] | 191 | |
| 192 | /** @brief The routing table */ |
| 193 | route::Table routeTable; |
William A. Kennington III | 6ff633a | 2021-11-09 17:09:12 -0800 | [diff] [blame] | 194 | |
| 195 | /** @brief List of hooks to execute during the next reload */ |
| 196 | std::vector<fu2::unique_function<void()>> reloadPreHooks; |
Ratan Gupta | 05eb109 | 2017-04-14 16:33:53 +0530 | [diff] [blame] | 197 | }; |
| 198 | |
| 199 | } // namespace network |
| 200 | } // namespace phosphor |