blob: b92c5284dbb2d89767e62d05aa32c62f2350210b [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"
Ratan Guptaef85eb92017-06-15 08:57:54 +05304#include "system_configuration.hpp"
William A. Kennington IIIdd9ef812022-10-05 02:08:02 -07005#include "types.hpp"
Patrick Venture189d44e2018-07-09 12:30:59 -07006#include "xyz/openbmc_project/Network/VLAN/Create/server.hpp"
Ratan Gupta5978dd12017-07-25 13:47:13 +05307
William A. Kennington III6ff633a2021-11-09 17:09:12 -08008#include <function2/function2.hpp>
Patrick Venture189d44e2018-07-09 12:30:59 -07009#include <sdbusplus/bus.hpp>
William A. Kennington III80d29012022-11-12 02:31:40 -080010#include <sdbusplus/bus/match.hpp>
William A. Kennington III5b179382022-11-15 15:23:26 -080011#include <sdbusplus/message/native_types.hpp>
William A. Kennington III9ede1b72022-11-21 01:59:28 -080012#include <stdplus/pinned.hpp>
William A. Kennington III60903ee2023-06-02 15:17:49 -070013#include <stdplus/str/maps.hpp>
William A. Kennington IIIde70ccf2022-11-20 17:18:01 -080014#include <stdplus/zstring_view.hpp>
Patrick Williams89d734b2023-05-10 07:50:25 -050015#include <xyz/openbmc_project/Common/FactoryReset/server.hpp>
16
17#include <filesystem>
18#include <memory>
Ratan Gupta05eb1092017-04-14 16:33:53 +053019#include <string>
William A. Kennington IIIdd9ef812022-10-05 02:08:02 -070020#include <string_view>
Ratan Gupta05eb1092017-04-14 16:33:53 +053021#include <vector>
22
23namespace phosphor
24{
25namespace network
26{
27
William A. Kennington III5b179382022-11-15 15:23:26 -080028using ManagerIface = sdbusplus::server::object_t<
Michael Tritz29f2fd62017-05-22 15:27:26 -050029 sdbusplus::xyz::openbmc_project::Network::VLAN::server::Create,
30 sdbusplus::xyz::openbmc_project::Common::server::FactoryReset>;
Ratan Gupta05eb1092017-04-14 16:33:53 +053031
Ratan Gupta05eb1092017-04-14 16:33:53 +053032/** @class Manager
33 * @brief OpenBMC network manager implementation.
34 */
William A. Kennington III5b179382022-11-15 15:23:26 -080035class Manager : public ManagerIface
Ratan Gupta05eb1092017-04-14 16:33:53 +053036{
Gunnar Mills57d9c502018-09-14 14:42:34 -050037 public:
Gunnar Mills57d9c502018-09-14 14:42:34 -050038 Manager(Manager&&) = delete;
39 Manager& operator=(Manager&&) = delete;
Ratan Gupta05eb1092017-04-14 16:33:53 +053040
Gunnar Mills57d9c502018-09-14 14:42:34 -050041 /** @brief Constructor to put object onto bus at a dbus path.
42 * @param[in] bus - Bus to attach to.
William A. Kennington IIIde70ccf2022-11-20 17:18:01 -080043 * @param[in] reload - The executor for reloading configs
Gunnar Mills57d9c502018-09-14 14:42:34 -050044 * @param[in] objPath - Path to attach at.
William A. Kennington IIIbe3bd2f2022-10-11 14:11:27 -070045 * @param[in] confDir - Network Configuration directory path.
Gunnar Mills57d9c502018-09-14 14:42:34 -050046 */
William A. Kennington IIId40046b2023-06-30 20:36:11 -070047 Manager(stdplus::PinnedRef<sdbusplus::bus_t> bus,
48 stdplus::PinnedRef<DelayedExecutor> reload,
William A. Kennington IIIde70ccf2022-11-20 17:18:01 -080049 stdplus::zstring_view objPath,
William A. Kennington III5b179382022-11-15 15:23:26 -080050 const std::filesystem::path& confDir);
Ratan Gupta05eb1092017-04-14 16:33:53 +053051
William A. Kennington IIIdd9ef812022-10-05 02:08:02 -070052 ObjectPath vlan(std::string interfaceName, uint32_t id) override;
Ratan Gupta05eb1092017-04-14 16:33:53 +053053
Gunnar Mills57d9c502018-09-14 14:42:34 -050054 /** @brief write the network conf file with the in-memory objects.
55 */
56 void writeToConfigurationFile();
Ratan Gupta4f1c18b2017-05-25 12:59:35 +053057
William A. Kennington III80d29012022-11-12 02:31:40 -080058 /** @brief Adds a single interface to the interface map */
William A. Kennington III0813a242022-11-12 18:07:11 -080059 void addInterface(const InterfaceInfo& info);
60 void removeInterface(const InterfaceInfo& info);
William A. Kennington III80d29012022-11-12 02:31:40 -080061
William A. Kennington IIIed5ff472022-11-12 16:24:02 -080062 /** @brief Add / remove an address to the interface or queue */
63 void addAddress(const AddressInfo& info);
64 void removeAddress(const AddressInfo& info);
65
66 /** @brief Add / remove a neighbor to the interface or queue */
67 void addNeighbor(const NeighborInfo& info);
68 void removeNeighbor(const NeighborInfo& info);
69
70 /** @brief Add / remove default gateway for interface */
William A. Kennington III9b2a20d2023-06-17 14:05:48 -070071 void addDefGw(unsigned ifidx, stdplus::InAnyAddr addr);
72 void removeDefGw(unsigned ifidx, stdplus::InAnyAddr addr);
William A. Kennington IIIed5ff472022-11-12 16:24:02 -080073
Gunnar Mills57d9c502018-09-14 14:42:34 -050074 /** @brief gets the network conf directory.
75 */
William A. Kennington III5b179382022-11-15 15:23:26 -080076 inline const auto& getConfDir() const
Gunnar Mills57d9c502018-09-14 14:42:34 -050077 {
78 return confDir;
79 }
Ratan Gupta255d5142017-08-10 09:02:08 +053080
Gunnar Mills57d9c502018-09-14 14:42:34 -050081 /** @brief gets the system conf object.
82 *
83 */
William A. Kennington III5b179382022-11-15 15:23:26 -080084 inline auto& getSystemConf()
Gunnar Mills57d9c502018-09-14 14:42:34 -050085 {
William A. Kennington III5b179382022-11-15 15:23:26 -080086 return *systemConf;
Gunnar Mills57d9c502018-09-14 14:42:34 -050087 }
Ratan Gupta05eb1092017-04-14 16:33:53 +053088
Gunnar Mills57d9c502018-09-14 14:42:34 -050089 /** @brief gets the dhcp conf object.
90 *
91 */
William A. Kennington III5b179382022-11-15 15:23:26 -080092 inline auto& getDHCPConf()
Gunnar Mills57d9c502018-09-14 14:42:34 -050093 {
William A. Kennington III5b179382022-11-15 15:23:26 -080094 return *dhcpConf;
Gunnar Mills57d9c502018-09-14 14:42:34 -050095 }
Ratan Gupta2b106532017-07-25 16:05:02 +053096
William A. Kennington IIIc7cf25f2021-11-09 16:16:59 -080097 /** @brief Arms a timer to tell systemd-network to reload all of the network
98 * configurations
William A. Kennington III56ecc782021-10-07 18:44:50 -070099 */
William A. Kennington IIIde70ccf2022-11-20 17:18:01 -0800100 inline void reloadConfigs()
101 {
William A. Kennington IIId40046b2023-06-30 20:36:11 -0700102 reload.get().schedule();
William A. Kennington IIIde70ccf2022-11-20 17:18:01 -0800103 }
William A. Kennington IIIc7cf25f2021-11-09 16:16:59 -0800104
William A. Kennington III09f3a4a2022-10-25 02:59:16 -0700105 /** @brief Persistent map of EthernetInterface dbus objects and their names
Patrick Ventured94d23e2019-01-07 13:05:23 -0800106 */
William A. Kennington III60903ee2023-06-02 15:17:49 -0700107 stdplus::string_umap<std::unique_ptr<EthernetInterface>> interfaces;
William A. Kennington III67b09da2022-10-31 14:09:53 -0700108 std::unordered_map<unsigned, EthernetInterface*> interfacesByIdx;
William A. Kennington III93f5c6d2022-11-17 16:23:44 -0800109 std::unordered_set<unsigned> ignoredIntf;
Patrick Ventured94d23e2019-01-07 13:05:23 -0800110
William A. Kennington III6ff633a2021-11-09 17:09:12 -0800111 /** @brief Adds a hook that runs immediately prior to reloading
112 *
113 * @param[in] hook - The hook to execute before reloading
114 */
115 inline void addReloadPreHook(fu2::unique_function<void()>&& hook)
116 {
117 reloadPreHooks.push_back(std::move(hook));
118 }
William A. Kennington IIIde4668a2022-11-17 16:02:30 -0800119 inline void addReloadPostHook(fu2::unique_function<void()>&& hook)
120 {
121 reloadPostHooks.push_back(std::move(hook));
122 }
William A. Kennington III6ff633a2021-11-09 17:09:12 -0800123
Ratan Gupta35297172018-11-28 18:40:16 +0530124 protected:
William A. Kennington IIIde70ccf2022-11-20 17:18:01 -0800125 /** @brief Handle to the object used to trigger reloads of networkd. */
William A. Kennington IIId40046b2023-06-30 20:36:11 -0700126 stdplus::PinnedRef<DelayedExecutor> reload;
William A. Kennington IIIde70ccf2022-11-20 17:18:01 -0800127
Gunnar Mills57d9c502018-09-14 14:42:34 -0500128 /** @brief Persistent sdbusplus DBus bus connection. */
William A. Kennington III9ede1b72022-11-21 01:59:28 -0800129 stdplus::PinnedRef<sdbusplus::bus_t> bus;
Ratan Gupta29b0e432017-05-25 12:51:40 +0530130
Gunnar Mills57d9c502018-09-14 14:42:34 -0500131 /** @brief BMC network reset - resets network configuration for BMC. */
132 void reset() override;
Michael Tritz29f2fd62017-05-22 15:27:26 -0500133
Gunnar Mills57d9c502018-09-14 14:42:34 -0500134 /** @brief Path of Object. */
William A. Kennington III5b179382022-11-15 15:23:26 -0800135 sdbusplus::message::object_path objPath;
Ratan Gupta29b0e432017-05-25 12:51:40 +0530136
Gunnar Mills57d9c502018-09-14 14:42:34 -0500137 /** @brief pointer to system conf object. */
William A. Kennington III5b179382022-11-15 15:23:26 -0800138 std::unique_ptr<SystemConfiguration> systemConf = nullptr;
Ratan Guptaef85eb92017-06-15 08:57:54 +0530139
Gunnar Mills57d9c502018-09-14 14:42:34 -0500140 /** @brief pointer to dhcp conf object. */
William A. Kennington III5b179382022-11-15 15:23:26 -0800141 std::unique_ptr<dhcp::Configuration> dhcpConf = nullptr;
Ratan Guptad16f88c2017-07-11 17:47:57 +0530142
Gunnar Mills57d9c502018-09-14 14:42:34 -0500143 /** @brief Network Configuration directory. */
William A. Kennington III5b179382022-11-15 15:23:26 -0800144 std::filesystem::path confDir;
William A. Kennington III6ff633a2021-11-09 17:09:12 -0800145
William A. Kennington III80d29012022-11-12 02:31:40 -0800146 /** @brief Map of interface info for undiscovered interfaces */
William A. Kennington III2fb0c872022-11-15 19:35:39 -0800147 std::unordered_map<unsigned, AllIntfInfo> intfInfo;
William A. Kennington III80d29012022-11-12 02:31:40 -0800148
149 /** @brief Map of enabled interfaces */
150 std::unordered_map<unsigned, bool> systemdNetworkdEnabled;
151 sdbusplus::bus::match_t systemdNetworkdEnabledMatch;
152
William A. Kennington III6ff633a2021-11-09 17:09:12 -0800153 /** @brief List of hooks to execute during the next reload */
154 std::vector<fu2::unique_function<void()>> reloadPreHooks;
William A. Kennington IIIde4668a2022-11-17 16:02:30 -0800155 std::vector<fu2::unique_function<void()>> reloadPostHooks;
William A. Kennington III80d29012022-11-12 02:31:40 -0800156
Manojkiran Edad92826d2024-06-17 14:43:14 +0530157 /** @brief Handles the receipt of an administrative state string */
William A. Kennington III80d29012022-11-12 02:31:40 -0800158 void handleAdminState(std::string_view state, unsigned ifidx);
William A. Kennington III0813a242022-11-12 18:07:11 -0800159
160 /** @brief Creates the interface in the maps */
William A. Kennington III2fb0c872022-11-15 19:35:39 -0800161 void createInterface(const AllIntfInfo& info, bool enabled);
Ratan Gupta05eb1092017-04-14 16:33:53 +0530162};
163
164} // namespace network
165} // namespace phosphor