xref: /openbmc/phosphor-networkd/src/network_manager.hpp (revision 60903ee7fc85d87a51bd5bc1f08d4c86234b322e)
1 #pragma once
2 #include "dhcp_configuration.hpp"
3 #include "ethernet_interface.hpp"
4 #include "system_configuration.hpp"
5 #include "types.hpp"
6 #include "xyz/openbmc_project/Network/VLAN/Create/server.hpp"
7 
8 #include <function2/function2.hpp>
9 #include <sdbusplus/bus.hpp>
10 #include <sdbusplus/bus/match.hpp>
11 #include <sdbusplus/message/native_types.hpp>
12 #include <stdplus/pinned.hpp>
13 #include <stdplus/str/maps.hpp>
14 #include <stdplus/zstring_view.hpp>
15 #include <xyz/openbmc_project/Common/FactoryReset/server.hpp>
16 
17 #include <filesystem>
18 #include <memory>
19 #include <string>
20 #include <string_view>
21 #include <vector>
22 
23 namespace phosphor
24 {
25 namespace network
26 {
27 
28 using ManagerIface = sdbusplus::server::object_t<
29     sdbusplus::xyz::openbmc_project::Network::VLAN::server::Create,
30     sdbusplus::xyz::openbmc_project::Common::server::FactoryReset>;
31 
32 class DelayedExecutor
33 {
34   public:
35     virtual ~DelayedExecutor() = default;
36 
37     virtual void schedule() = 0;
38     virtual void setCallback(fu2::unique_function<void()>&& cb) = 0;
39 };
40 
41 /** @class Manager
42  *  @brief OpenBMC network manager implementation.
43  */
44 class Manager : public ManagerIface
45 {
46   public:
47     Manager(Manager&&) = delete;
48     Manager& operator=(Manager&&) = delete;
49 
50     /** @brief Constructor to put object onto bus at a dbus path.
51      *  @param[in] bus - Bus to attach to.
52      *  @param[in] reload - The executor for reloading configs
53      *  @param[in] objPath - Path to attach at.
54      *  @param[in] confDir - Network Configuration directory path.
55      */
56     Manager(stdplus::PinnedRef<sdbusplus::bus_t> bus, DelayedExecutor& reload,
57             stdplus::zstring_view objPath,
58             const std::filesystem::path& confDir);
59 
60     ObjectPath vlan(std::string interfaceName, uint32_t id) override;
61 
62     /** @brief write the network conf file with the in-memory objects.
63      */
64     void writeToConfigurationFile();
65 
66     /** @brief Adds a single interface to the interface map */
67     void addInterface(const InterfaceInfo& info);
68     void removeInterface(const InterfaceInfo& info);
69 
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 
82     /** @brief gets the network conf directory.
83      */
84     inline const auto& getConfDir() const
85     {
86         return confDir;
87     }
88 
89     /** @brief gets the system conf object.
90      *
91      */
92     inline auto& getSystemConf()
93     {
94         return *systemConf;
95     }
96 
97     /** @brief gets the dhcp conf object.
98      *
99      */
100     inline auto& getDHCPConf()
101     {
102         return *dhcpConf;
103     }
104 
105     /** @brief Arms a timer to tell systemd-network to reload all of the network
106      * configurations
107      */
108     inline void reloadConfigs()
109     {
110         reload.schedule();
111     }
112 
113     /** @brief Persistent map of EthernetInterface dbus objects and their names
114      */
115     stdplus::string_umap<std::unique_ptr<EthernetInterface>> interfaces;
116     std::unordered_map<unsigned, EthernetInterface*> interfacesByIdx;
117     std::unordered_set<unsigned> ignoredIntf;
118 
119     /** @brief Adds a hook that runs immediately prior to reloading
120      *
121      *  @param[in] hook - The hook to execute before reloading
122      */
123     inline void addReloadPreHook(fu2::unique_function<void()>&& hook)
124     {
125         reloadPreHooks.push_back(std::move(hook));
126     }
127     inline void addReloadPostHook(fu2::unique_function<void()>&& hook)
128     {
129         reloadPostHooks.push_back(std::move(hook));
130     }
131 
132   protected:
133     /** @brief Handle to the object used to trigger reloads of networkd. */
134     DelayedExecutor& reload;
135 
136     /** @brief Persistent sdbusplus DBus bus connection. */
137     stdplus::PinnedRef<sdbusplus::bus_t> bus;
138 
139     /** @brief BMC network reset - resets network configuration for BMC. */
140     void reset() override;
141 
142     /** @brief Path of Object. */
143     sdbusplus::message::object_path objPath;
144 
145     /** @brief pointer to system conf object. */
146     std::unique_ptr<SystemConfiguration> systemConf = nullptr;
147 
148     /** @brief pointer to dhcp conf object. */
149     std::unique_ptr<dhcp::Configuration> dhcpConf = nullptr;
150 
151     /** @brief Network Configuration directory. */
152     std::filesystem::path confDir;
153 
154     /** @brief Map of interface info for undiscovered interfaces */
155     std::unordered_map<unsigned, AllIntfInfo> intfInfo;
156 
157     /** @brief Map of enabled interfaces */
158     std::unordered_map<unsigned, bool> systemdNetworkdEnabled;
159     sdbusplus::bus::match_t systemdNetworkdEnabledMatch;
160 
161     /** @brief List of hooks to execute during the next reload */
162     std::vector<fu2::unique_function<void()>> reloadPreHooks;
163     std::vector<fu2::unique_function<void()>> reloadPostHooks;
164 
165     /** @brief Handles the recipt of an adminstrative state string */
166     void handleAdminState(std::string_view state, unsigned ifidx);
167 
168     /** @brief Creates the interface in the maps */
169     void createInterface(const AllIntfInfo& info, bool enabled);
170 };
171 
172 } // namespace network
173 } // namespace phosphor
174