blob: 18bb7d5cc27883e4d853acdabd54f2574b887369 [file] [log] [blame]
Ratan Gupta05eb1092017-04-14 16:33:53 +05301#pragma once
Patrick Venture189d44e2018-07-09 12:30:59 -07002#include "dhcp_configuration.hpp"
Ratan Gupta05eb1092017-04-14 16:33:53 +05303#include "ethernet_interface.hpp"
William A. Kennington IIIe0564842021-10-23 16:02:22 -07004#include "routing_table.hpp"
Ratan Guptaef85eb92017-06-15 08:57:54 +05305#include "system_configuration.hpp"
William A. Kennington IIIdd9ef812022-10-05 02:08:02 -07006#include "types.hpp"
Ratan Gupta5978dd12017-07-25 13:47:13 +05307#include "vlan_interface.hpp"
Patrick Venture189d44e2018-07-09 12:30:59 -07008#include "xyz/openbmc_project/Network/VLAN/Create/server.hpp"
Ratan Gupta5978dd12017-07-25 13:47:13 +05309
Manojkiran Edaa879baa2020-06-13 14:39:08 +053010#include <filesystem>
William A. Kennington III6ff633a2021-11-09 17:09:12 -080011#include <function2/function2.hpp>
Ratan Gupta6811f822017-04-14 16:34:56 +053012#include <memory>
Patrick Venture189d44e2018-07-09 12:30:59 -070013#include <sdbusplus/bus.hpp>
Ratan Gupta05eb1092017-04-14 16:33:53 +053014#include <string>
William A. Kennington IIIdd9ef812022-10-05 02:08:02 -070015#include <string_view>
Ratan Gupta05eb1092017-04-14 16:33:53 +053016#include <vector>
Patrick Venture189d44e2018-07-09 12:30:59 -070017#include <xyz/openbmc_project/Common/FactoryReset/server.hpp>
Ratan Gupta05eb1092017-04-14 16:33:53 +053018
19namespace phosphor
20{
21namespace network
22{
23
Ratan Gupta2b106532017-07-25 16:05:02 +053024using SystemConfPtr = std::unique_ptr<SystemConfiguration>;
25using DHCPConfPtr = std::unique_ptr<dhcp::Configuration>;
26
Manojkiran Edaa879baa2020-06-13 14:39:08 +053027namespace fs = std::filesystem;
Ratan Gupta05eb1092017-04-14 16:33:53 +053028namespace details
29{
30
Michael Tritz29f2fd62017-05-22 15:27:26 -050031template <typename T, typename U>
Patrick Williamsc38b0712022-07-22 19:26:54 -050032using ServerObject = typename sdbusplus::server::object_t<T, U>;
Ratan Gupta05eb1092017-04-14 16:33:53 +053033
Michael Tritz29f2fd62017-05-22 15:27:26 -050034using VLANCreateIface = details::ServerObject<
35 sdbusplus::xyz::openbmc_project::Network::VLAN::server::Create,
36 sdbusplus::xyz::openbmc_project::Common::server::FactoryReset>;
Ratan Gupta05eb1092017-04-14 16:33:53 +053037
Ratan Gupta05eb1092017-04-14 16:33:53 +053038} // namespace details
39
40/** @class Manager
41 * @brief OpenBMC network manager implementation.
42 */
43class Manager : public details::VLANCreateIface
44{
Gunnar Mills57d9c502018-09-14 14:42:34 -050045 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 Gupta05eb1092017-04-14 16:33:53 +053052
Gunnar Mills57d9c502018-09-14 14:42:34 -050053 /** @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.
William A. Kennington IIIbe3bd2f2022-10-11 14:11:27 -070056 * @param[in] confDir - Network Configuration directory path.
Gunnar Mills57d9c502018-09-14 14:42:34 -050057 */
William A. Kennington IIIbe3bd2f2022-10-11 14:11:27 -070058 Manager(sdbusplus::bus_t& bus, const char* objPath,
59 const fs::path& confDir);
Ratan Gupta05eb1092017-04-14 16:33:53 +053060
William A. Kennington IIIdd9ef812022-10-05 02:08:02 -070061 ObjectPath vlan(std::string interfaceName, uint32_t id) override;
Ratan Gupta05eb1092017-04-14 16:33:53 +053062
Gunnar Mills57d9c502018-09-14 14:42:34 -050063 /** @brief write the network conf file with the in-memory objects.
64 */
65 void writeToConfigurationFile();
Ratan Gupta4f1c18b2017-05-25 12:59:35 +053066
Gunnar Mills57d9c502018-09-14 14:42:34 -050067 /** @brief Fetch the interface and the ipaddress details
68 * from the system and create the ethernet interraces
69 * dbus object.
70 */
Manojkiran Edaacd6dd52019-10-15 15:00:51 +053071 virtual void createInterfaces();
Ratan Gupta29b0e432017-05-25 12:51:40 +053072
Gunnar Mills57d9c502018-09-14 14:42:34 -050073 /** @brief create child interface object and the system conf object.
74 */
75 void createChildObjects();
Ratan Guptaef85eb92017-06-15 08:57:54 +053076
Gunnar Mills57d9c502018-09-14 14:42:34 -050077 /** @brief sets the network conf directory.
78 * @param[in] dirName - Absolute path of the directory.
79 */
80 void setConfDir(const fs::path& dir);
Ratan Guptaef85eb92017-06-15 08:57:54 +053081
Gunnar Mills57d9c502018-09-14 14:42:34 -050082 /** @brief gets the network conf directory.
83 */
William A. Kennington III72131802022-10-05 02:08:36 -070084 inline const fs::path& getConfDir() const
Gunnar Mills57d9c502018-09-14 14:42:34 -050085 {
86 return confDir;
87 }
Ratan Gupta255d5142017-08-10 09:02:08 +053088
Gunnar Mills57d9c502018-09-14 14:42:34 -050089 /** @brief gets the system conf object.
90 *
91 */
William A. Kennington III72131802022-10-05 02:08:36 -070092 inline const SystemConfPtr& getSystemConf()
Gunnar Mills57d9c502018-09-14 14:42:34 -050093 {
94 return systemConf;
95 }
Ratan Gupta05eb1092017-04-14 16:33:53 +053096
Gunnar Mills57d9c502018-09-14 14:42:34 -050097 /** @brief gets the dhcp conf object.
98 *
99 */
William A. Kennington III72131802022-10-05 02:08:36 -0700100 inline const DHCPConfPtr& getDHCPConf()
Gunnar Mills57d9c502018-09-14 14:42:34 -0500101 {
102 return dhcpConf;
103 }
Ratan Gupta2b106532017-07-25 16:05:02 +0530104
Gunnar Mills57d9c502018-09-14 14:42:34 -0500105 /** @brief create the default network files for each interface
Gunnar Mills57d9c502018-09-14 14:42:34 -0500106 * @return true if network file created else false
107 */
William A. Kennington III9a1d9af2021-11-09 17:51:05 -0800108 bool createDefaultNetworkFiles();
Ratan Gupta70c7e5b2017-07-12 11:41:55 +0530109
Manojkiran Edacc099a82020-05-11 14:25:16 +0530110 /** @brief This function gets the MAC address from the VPD and
111 * sets it on the corresponding ethernet interface during first
112 * Boot, once it sets the MAC from VPD, it creates a file named
113 * firstBoot under /var/lib to make sure we dont run this function
114 * again.
115 *
116 * @param[in] ethPair - Its a pair of ethernet interface name & the
117 * corresponding MAC Address from the VPD
118 *
119 * return - NULL
120 */
121 void setFistBootMACOnInterface(
122 const std::pair<std::string, std::string>& ethPair);
123
William A. Kennington IIIc7cf25f2021-11-09 16:16:59 -0800124 /** @brief Arms a timer to tell systemd-network to reload all of the network
125 * configurations
William A. Kennington III56ecc782021-10-07 18:44:50 -0700126 */
127 virtual void reloadConfigs();
128
William A. Kennington IIIc7cf25f2021-11-09 16:16:59 -0800129 /** @brief Tell systemd-network to reload all of the network configurations
130 */
131 void doReloadConfigs();
132
William A. Kennington III96444792022-10-05 15:16:22 -0700133 /** @brief Get the interfaces owned by the manager
Patrick Ventured94d23e2019-01-07 13:05:23 -0800134 *
William A. Kennington III96444792022-10-05 15:16:22 -0700135 * @return Interfaces reference.
Patrick Ventured94d23e2019-01-07 13:05:23 -0800136 */
William A. Kennington III96444792022-10-05 15:16:22 -0700137 inline const auto& getInterfaces() const
Patrick Ventured94d23e2019-01-07 13:05:23 -0800138 {
William A. Kennington III96444792022-10-05 15:16:22 -0700139 return interfaces;
Patrick Ventured94d23e2019-01-07 13:05:23 -0800140 }
141
William A. Kennington IIIe0564842021-10-23 16:02:22 -0700142 /** @brief Get the routing table owned by the manager
143 *
144 * @return Routing table reference.
145 */
146 inline const auto& getRouteTable() const
147 {
148 return routeTable;
149 }
150
William A. Kennington III6ff633a2021-11-09 17:09:12 -0800151 /** @brief Adds a hook that runs immediately prior to reloading
152 *
153 * @param[in] hook - The hook to execute before reloading
154 */
155 inline void addReloadPreHook(fu2::unique_function<void()>&& hook)
156 {
157 reloadPreHooks.push_back(std::move(hook));
158 }
159
Ratan Gupta35297172018-11-28 18:40:16 +0530160 protected:
Gunnar Mills57d9c502018-09-14 14:42:34 -0500161 /** @brief Persistent sdbusplus DBus bus connection. */
Patrick Williamsc38b0712022-07-22 19:26:54 -0500162 sdbusplus::bus_t& bus;
Ratan Gupta29b0e432017-05-25 12:51:40 +0530163
Gunnar Mills57d9c502018-09-14 14:42:34 -0500164 /** @brief Persistent map of EthernetInterface dbus objects and their names
165 */
William A. Kennington IIIdd9ef812022-10-05 02:08:02 -0700166 string_umap<std::unique_ptr<EthernetInterface>> interfaces;
Ratan Gupta05eb1092017-04-14 16:33:53 +0530167
Gunnar Mills57d9c502018-09-14 14:42:34 -0500168 /** @brief BMC network reset - resets network configuration for BMC. */
169 void reset() override;
Michael Tritz29f2fd62017-05-22 15:27:26 -0500170
Gunnar Mills57d9c502018-09-14 14:42:34 -0500171 /** @brief Path of Object. */
172 std::string objectPath;
Ratan Gupta29b0e432017-05-25 12:51:40 +0530173
Gunnar Mills57d9c502018-09-14 14:42:34 -0500174 /** @brief pointer to system conf object. */
175 SystemConfPtr systemConf = nullptr;
Ratan Guptaef85eb92017-06-15 08:57:54 +0530176
Gunnar Mills57d9c502018-09-14 14:42:34 -0500177 /** @brief pointer to dhcp conf object. */
178 DHCPConfPtr dhcpConf = nullptr;
Ratan Guptad16f88c2017-07-11 17:47:57 +0530179
Gunnar Mills57d9c502018-09-14 14:42:34 -0500180 /** @brief Network Configuration directory. */
181 fs::path confDir;
William A. Kennington IIIe0564842021-10-23 16:02:22 -0700182
183 /** @brief The routing table */
184 route::Table routeTable;
William A. Kennington III6ff633a2021-11-09 17:09:12 -0800185
186 /** @brief List of hooks to execute during the next reload */
187 std::vector<fu2::unique_function<void()>> reloadPreHooks;
Ratan Gupta05eb1092017-04-14 16:33:53 +0530188};
189
190} // namespace network
191} // namespace phosphor