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