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