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