xref: /openbmc/phosphor-networkd/src/network_manager.hpp (revision f7116de80882b7edad729e7d8747639eff600b08)
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 write the lldp conf file
59      */
60     void writeLLDPDConfigurationFile();
61 
62     /** @brief Adds a single interface to the interface map */
63     void addInterface(const InterfaceInfo& info);
64     void removeInterface(const InterfaceInfo& info);
65 
66     /** @brief Add / remove an address to the interface or queue */
67     void addAddress(const AddressInfo& info);
68     void removeAddress(const AddressInfo& info);
69 
70     /** @brief Add / remove a neighbor to the interface or queue */
71     void addNeighbor(const NeighborInfo& info);
72     void removeNeighbor(const NeighborInfo& info);
73 
74     /** @brief Add / remove default gateway for interface */
75     void addDefGw(unsigned ifidx, stdplus::InAnyAddr addr);
76     void removeDefGw(unsigned ifidx, stdplus::InAnyAddr addr);
77 
78     /** @brief gets the network conf directory.
79      */
getConfDir() const80     inline const auto& getConfDir() const
81     {
82         return confDir;
83     }
84 
85     /** @brief gets the system conf object.
86      *
87      */
getSystemConf()88     inline auto& getSystemConf()
89     {
90         return *systemConf;
91     }
92 
93     /** @brief gets the dhcp conf object.
94      *
95      */
getDHCPConf()96     inline auto& getDHCPConf()
97     {
98         return *dhcpConf;
99     }
100 
101     /** @brief Arms a timer to tell systemd-network to reload all of the network
102      * configurations
103      */
reloadConfigs()104     inline void reloadConfigs()
105     {
106         reload.get().schedule();
107     }
108 
109     /** Reload LLDP configuration
110      */
111     void reloadLLDPService();
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      */
addReloadPreHook(fu2::unique_function<void ()> && hook)123     inline void addReloadPreHook(fu2::unique_function<void()>&& hook)
124     {
125         reloadPreHooks.push_back(std::move(hook));
126     }
addReloadPostHook(fu2::unique_function<void ()> && hook)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     stdplus::PinnedRef<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 receipt of an administrative 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