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 Manager
33  *  @brief OpenBMC network manager implementation.
34  */
35 class Manager : public ManagerIface
36 {
37   public:
38     Manager(Manager&&) = delete;
39     Manager& operator=(Manager&&) = delete;
40 
41     /** @brief Constructor to put object onto bus at a dbus path.
42      *  @param[in] bus - Bus to attach to.
43      *  @param[in] reload - The executor for reloading configs
44      *  @param[in] objPath - Path to attach at.
45      *  @param[in] confDir - Network Configuration directory path.
46      */
47     Manager(stdplus::PinnedRef<sdbusplus::bus_t> bus,
48             stdplus::PinnedRef<DelayedExecutor> reload,
49             stdplus::zstring_view objPath,
50             const std::filesystem::path& confDir);
51 
52     ObjectPath vlan(std::string interfaceName, uint32_t id) override;
53 
54     /** @brief write the network conf file with the in-memory objects.
55      */
56     void writeToConfigurationFile();
57 
58     /** @brief Adds a single interface to the interface map */
59     void addInterface(const InterfaceInfo& info);
60     void removeInterface(const InterfaceInfo& info);
61 
62     /** @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 */
71     void addDefGw(unsigned ifidx, stdplus::InAnyAddr addr);
72     void removeDefGw(unsigned ifidx, stdplus::InAnyAddr addr);
73 
74     /** @brief gets the network conf directory.
75      */
getConfDir() const76     inline const auto& getConfDir() const
77     {
78         return confDir;
79     }
80 
81     /** @brief gets the system conf object.
82      *
83      */
getSystemConf()84     inline auto& getSystemConf()
85     {
86         return *systemConf;
87     }
88 
89     /** @brief gets the dhcp conf object.
90      *
91      */
getDHCPConf()92     inline auto& getDHCPConf()
93     {
94         return *dhcpConf;
95     }
96 
97     /** @brief Arms a timer to tell systemd-network to reload all of the network
98      * configurations
99      */
reloadConfigs()100     inline void reloadConfigs()
101     {
102         reload.get().schedule();
103     }
104 
105     /** @brief Persistent map of EthernetInterface dbus objects and their names
106      */
107     stdplus::string_umap<std::unique_ptr<EthernetInterface>> interfaces;
108     std::unordered_map<unsigned, EthernetInterface*> interfacesByIdx;
109     std::unordered_set<unsigned> ignoredIntf;
110 
111     /** @brief Adds a hook that runs immediately prior to reloading
112      *
113      *  @param[in] hook - The hook to execute before reloading
114      */
addReloadPreHook(fu2::unique_function<void ()> && hook)115     inline void addReloadPreHook(fu2::unique_function<void()>&& hook)
116     {
117         reloadPreHooks.push_back(std::move(hook));
118     }
addReloadPostHook(fu2::unique_function<void ()> && hook)119     inline void addReloadPostHook(fu2::unique_function<void()>&& hook)
120     {
121         reloadPostHooks.push_back(std::move(hook));
122     }
123 
124   protected:
125     /** @brief Handle to the object used to trigger reloads of networkd. */
126     stdplus::PinnedRef<DelayedExecutor> reload;
127 
128     /** @brief Persistent sdbusplus DBus bus connection. */
129     stdplus::PinnedRef<sdbusplus::bus_t> bus;
130 
131     /** @brief BMC network reset - resets network configuration for BMC. */
132     void reset() override;
133 
134     /** @brief Path of Object. */
135     sdbusplus::message::object_path objPath;
136 
137     /** @brief pointer to system conf object. */
138     std::unique_ptr<SystemConfiguration> systemConf = nullptr;
139 
140     /** @brief pointer to dhcp conf object. */
141     std::unique_ptr<dhcp::Configuration> dhcpConf = nullptr;
142 
143     /** @brief Network Configuration directory. */
144     std::filesystem::path confDir;
145 
146     /** @brief Map of interface info for undiscovered interfaces */
147     std::unordered_map<unsigned, AllIntfInfo> intfInfo;
148 
149     /** @brief Map of enabled interfaces */
150     std::unordered_map<unsigned, bool> systemdNetworkdEnabled;
151     sdbusplus::bus::match_t systemdNetworkdEnabledMatch;
152 
153     /** @brief List of hooks to execute during the next reload */
154     std::vector<fu2::unique_function<void()>> reloadPreHooks;
155     std::vector<fu2::unique_function<void()>> reloadPostHooks;
156 
157     /** @brief Handles the receipt of an administrative state string */
158     void handleAdminState(std::string_view state, unsigned ifidx);
159 
160     /** @brief Creates the interface in the maps */
161     void createInterface(const AllIntfInfo& info, bool enabled);
162 };
163 
164 } // namespace network
165 } // namespace phosphor
166