blob: 0f371345219e7b8a7addad8e273077367e47c70c [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"
Patrick Venture189d44e2018-07-09 12:30:59 -07007#include "xyz/openbmc_project/Network/VLAN/Create/server.hpp"
Ratan Gupta5978dd12017-07-25 13:47:13 +05308
Manojkiran Edaa879baa2020-06-13 14:39:08 +05309#include <filesystem>
William A. Kennington III6ff633a2021-11-09 17:09:12 -080010#include <function2/function2.hpp>
Ratan Gupta6811f822017-04-14 16:34:56 +053011#include <memory>
Patrick Venture189d44e2018-07-09 12:30:59 -070012#include <sdbusplus/bus.hpp>
Ratan Gupta05eb1092017-04-14 16:33:53 +053013#include <string>
William A. Kennington IIIdd9ef812022-10-05 02:08:02 -070014#include <string_view>
Ratan Gupta05eb1092017-04-14 16:33:53 +053015#include <vector>
Patrick Venture189d44e2018-07-09 12:30:59 -070016#include <xyz/openbmc_project/Common/FactoryReset/server.hpp>
Ratan Gupta05eb1092017-04-14 16:33:53 +053017
18namespace phosphor
19{
20namespace network
21{
22
Ratan Gupta2b106532017-07-25 16:05:02 +053023using SystemConfPtr = std::unique_ptr<SystemConfiguration>;
24using DHCPConfPtr = std::unique_ptr<dhcp::Configuration>;
25
Manojkiran Edaa879baa2020-06-13 14:39:08 +053026namespace fs = std::filesystem;
Ratan Gupta05eb1092017-04-14 16:33:53 +053027namespace details
28{
29
Michael Tritz29f2fd62017-05-22 15:27:26 -050030template <typename T, typename U>
Patrick Williamsc38b0712022-07-22 19:26:54 -050031using ServerObject = typename sdbusplus::server::object_t<T, U>;
Ratan Gupta05eb1092017-04-14 16:33:53 +053032
Michael Tritz29f2fd62017-05-22 15:27:26 -050033using VLANCreateIface = details::ServerObject<
34 sdbusplus::xyz::openbmc_project::Network::VLAN::server::Create,
35 sdbusplus::xyz::openbmc_project::Common::server::FactoryReset>;
Ratan Gupta05eb1092017-04-14 16:33:53 +053036
Ratan Gupta05eb1092017-04-14 16:33:53 +053037} // namespace details
38
39/** @class Manager
40 * @brief OpenBMC network manager implementation.
41 */
42class Manager : public details::VLANCreateIface
43{
Gunnar Mills57d9c502018-09-14 14:42:34 -050044 public:
45 Manager() = delete;
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 Gupta05eb1092017-04-14 16:33:53 +053051
Gunnar Mills57d9c502018-09-14 14:42:34 -050052 /** @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 IIIbe3bd2f2022-10-11 14:11:27 -070055 * @param[in] confDir - Network Configuration directory path.
Gunnar Mills57d9c502018-09-14 14:42:34 -050056 */
William A. Kennington IIIbe3bd2f2022-10-11 14:11:27 -070057 Manager(sdbusplus::bus_t& bus, const char* objPath,
58 const fs::path& confDir);
Ratan Gupta05eb1092017-04-14 16:33:53 +053059
William A. Kennington IIIdd9ef812022-10-05 02:08:02 -070060 ObjectPath vlan(std::string interfaceName, uint32_t id) override;
Ratan Gupta05eb1092017-04-14 16:33:53 +053061
Gunnar Mills57d9c502018-09-14 14:42:34 -050062 /** @brief write the network conf file with the in-memory objects.
63 */
64 void writeToConfigurationFile();
Ratan Gupta4f1c18b2017-05-25 12:59:35 +053065
Gunnar Mills57d9c502018-09-14 14:42:34 -050066 /** @brief Fetch the interface and the ipaddress details
67 * from the system and create the ethernet interraces
68 * dbus object.
69 */
Manojkiran Edaacd6dd52019-10-15 15:00:51 +053070 virtual void createInterfaces();
Ratan Gupta29b0e432017-05-25 12:51:40 +053071
Gunnar Mills57d9c502018-09-14 14:42:34 -050072 /** @brief create child interface object and the system conf object.
73 */
74 void createChildObjects();
Ratan Guptaef85eb92017-06-15 08:57:54 +053075
Gunnar Mills57d9c502018-09-14 14:42:34 -050076 /** @brief sets the network conf directory.
77 * @param[in] dirName - Absolute path of the directory.
78 */
79 void setConfDir(const fs::path& dir);
Ratan Guptaef85eb92017-06-15 08:57:54 +053080
Gunnar Mills57d9c502018-09-14 14:42:34 -050081 /** @brief gets the network conf directory.
82 */
William A. Kennington III72131802022-10-05 02:08:36 -070083 inline const fs::path& getConfDir() const
Gunnar Mills57d9c502018-09-14 14:42:34 -050084 {
85 return confDir;
86 }
Ratan Gupta255d5142017-08-10 09:02:08 +053087
Gunnar Mills57d9c502018-09-14 14:42:34 -050088 /** @brief gets the system conf object.
89 *
90 */
William A. Kennington III72131802022-10-05 02:08:36 -070091 inline const SystemConfPtr& getSystemConf()
Gunnar Mills57d9c502018-09-14 14:42:34 -050092 {
93 return systemConf;
94 }
Ratan Gupta05eb1092017-04-14 16:33:53 +053095
Gunnar Mills57d9c502018-09-14 14:42:34 -050096 /** @brief gets the dhcp conf object.
97 *
98 */
William A. Kennington III72131802022-10-05 02:08:36 -070099 inline const DHCPConfPtr& getDHCPConf()
Gunnar Mills57d9c502018-09-14 14:42:34 -0500100 {
101 return dhcpConf;
102 }
Ratan Gupta2b106532017-07-25 16:05:02 +0530103
Manojkiran Edacc099a82020-05-11 14:25:16 +0530104 /** @brief This function gets the MAC address from the VPD and
105 * sets it on the corresponding ethernet interface during first
106 * Boot, once it sets the MAC from VPD, it creates a file named
107 * firstBoot under /var/lib to make sure we dont run this function
108 * again.
109 *
110 * @param[in] ethPair - Its a pair of ethernet interface name & the
111 * corresponding MAC Address from the VPD
112 *
113 * return - NULL
114 */
115 void setFistBootMACOnInterface(
116 const std::pair<std::string, std::string>& ethPair);
117
William A. Kennington IIIc7cf25f2021-11-09 16:16:59 -0800118 /** @brief Arms a timer to tell systemd-network to reload all of the network
119 * configurations
William A. Kennington III56ecc782021-10-07 18:44:50 -0700120 */
121 virtual void reloadConfigs();
122
William A. Kennington IIIc7cf25f2021-11-09 16:16:59 -0800123 /** @brief Tell systemd-network to reload all of the network configurations
124 */
125 void doReloadConfigs();
126
William A. Kennington III09f3a4a2022-10-25 02:59:16 -0700127 /** @brief Persistent map of EthernetInterface dbus objects and their names
Patrick Ventured94d23e2019-01-07 13:05:23 -0800128 */
William A. Kennington III09f3a4a2022-10-25 02:59:16 -0700129 string_umap<std::unique_ptr<EthernetInterface>> interfaces;
William A. Kennington III67b09da2022-10-31 14:09:53 -0700130 std::unordered_map<unsigned, EthernetInterface*> interfacesByIdx;
Patrick Ventured94d23e2019-01-07 13:05:23 -0800131
William A. Kennington IIIe0564842021-10-23 16:02:22 -0700132 /** @brief Get the routing table owned by the manager
133 *
134 * @return Routing table reference.
135 */
136 inline const auto& getRouteTable() const
137 {
138 return routeTable;
139 }
140
William A. Kennington III6ff633a2021-11-09 17:09:12 -0800141 /** @brief Adds a hook that runs immediately prior to reloading
142 *
143 * @param[in] hook - The hook to execute before reloading
144 */
145 inline void addReloadPreHook(fu2::unique_function<void()>&& hook)
146 {
147 reloadPreHooks.push_back(std::move(hook));
148 }
149
Ratan Gupta35297172018-11-28 18:40:16 +0530150 protected:
Gunnar Mills57d9c502018-09-14 14:42:34 -0500151 /** @brief Persistent sdbusplus DBus bus connection. */
Patrick Williamsc38b0712022-07-22 19:26:54 -0500152 sdbusplus::bus_t& bus;
Ratan Gupta29b0e432017-05-25 12:51:40 +0530153
Gunnar Mills57d9c502018-09-14 14:42:34 -0500154 /** @brief BMC network reset - resets network configuration for BMC. */
155 void reset() override;
Michael Tritz29f2fd62017-05-22 15:27:26 -0500156
Gunnar Mills57d9c502018-09-14 14:42:34 -0500157 /** @brief Path of Object. */
158 std::string objectPath;
Ratan Gupta29b0e432017-05-25 12:51:40 +0530159
Gunnar Mills57d9c502018-09-14 14:42:34 -0500160 /** @brief pointer to system conf object. */
161 SystemConfPtr systemConf = nullptr;
Ratan Guptaef85eb92017-06-15 08:57:54 +0530162
Gunnar Mills57d9c502018-09-14 14:42:34 -0500163 /** @brief pointer to dhcp conf object. */
164 DHCPConfPtr dhcpConf = nullptr;
Ratan Guptad16f88c2017-07-11 17:47:57 +0530165
Gunnar Mills57d9c502018-09-14 14:42:34 -0500166 /** @brief Network Configuration directory. */
167 fs::path confDir;
William A. Kennington IIIe0564842021-10-23 16:02:22 -0700168
169 /** @brief The routing table */
170 route::Table routeTable;
William A. Kennington III6ff633a2021-11-09 17:09:12 -0800171
172 /** @brief List of hooks to execute during the next reload */
173 std::vector<fu2::unique_function<void()>> reloadPreHooks;
Ratan Gupta05eb1092017-04-14 16:33:53 +0530174};
175
176} // namespace network
177} // namespace phosphor