xref: /openbmc/phosphor-networkd/src/network_manager.hpp (revision b800612229a53bae2ef9ae1cf51c0e86b6236bd6)
1 #pragma once
2 #include "dhcp_configuration.hpp"
3 #include "ethernet_interface.hpp"
4 #include "routing_table.hpp"
5 #include "system_configuration.hpp"
6 #include "types.hpp"
7 #include "xyz/openbmc_project/Network/VLAN/Create/server.hpp"
8 
9 #include <filesystem>
10 #include <function2/function2.hpp>
11 #include <memory>
12 #include <sdbusplus/bus.hpp>
13 #include <sdbusplus/bus/match.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 SystemConfPtr = std::unique_ptr<SystemConfiguration>;
25 using DHCPConfPtr = std::unique_ptr<dhcp::Configuration>;
26 
27 namespace fs = std::filesystem;
28 namespace details
29 {
30 
31 template <typename T, typename U>
32 using ServerObject = typename sdbusplus::server::object_t<T, U>;
33 
34 using VLANCreateIface = details::ServerObject<
35     sdbusplus::xyz::openbmc_project::Network::VLAN::server::Create,
36     sdbusplus::xyz::openbmc_project::Common::server::FactoryReset>;
37 
38 } // namespace details
39 
40 /** @class Manager
41  *  @brief OpenBMC network manager implementation.
42  */
43 class Manager : public details::VLANCreateIface
44 {
45   public:
46     Manager(const Manager&) = delete;
47     Manager& operator=(const Manager&) = delete;
48     Manager(Manager&&) = delete;
49     Manager& operator=(Manager&&) = delete;
50     virtual ~Manager() = default;
51 
52     /** @brief Constructor to put object onto bus at a dbus path.
53      *  @param[in] bus - Bus to attach to.
54      *  @param[in] objPath - Path to attach at.
55      *  @param[in] confDir - Network Configuration directory path.
56      */
57     Manager(sdbusplus::bus_t& bus, const char* objPath,
58             const fs::path& confDir);
59 
60     ObjectPath vlan(std::string interfaceName, uint32_t id) override;
61 
62     /** @brief write the network conf file with the in-memory objects.
63      */
64     void writeToConfigurationFile();
65 
66     /** @brief Adds a single interface to the interface map */
67     void addInterface(const InterfaceInfo& info);
68     void removeInterface(const InterfaceInfo& info);
69 
70     /** @brief Add / remove an address to the interface or queue */
71     void addAddress(const AddressInfo& info);
72     void removeAddress(const AddressInfo& info);
73 
74     /** @brief Add / remove a neighbor to the interface or queue */
75     void addNeighbor(const NeighborInfo& info);
76     void removeNeighbor(const NeighborInfo& info);
77 
78     /** @brief Add / remove default gateway for interface */
79     void addDefGw(unsigned ifidx, InAddrAny addr);
80     void removeDefGw(unsigned ifidx, InAddrAny addr);
81 
82     /** @brief Fetch the interface and the ipaddress details
83      *         from the system and create the ethernet interraces
84      *         dbus object.
85      */
86     virtual void createInterfaces();
87 
88     /** @brief create child interface object and the system conf object.
89      */
90     void createChildObjects();
91 
92     /** @brief sets the network conf directory.
93      *  @param[in] dirName - Absolute path of the directory.
94      */
95     void setConfDir(const fs::path& dir);
96 
97     /** @brief gets the network conf directory.
98      */
99     inline const fs::path& getConfDir() const
100     {
101         return confDir;
102     }
103 
104     /** @brief gets the system conf object.
105      *
106      */
107     inline const SystemConfPtr& getSystemConf()
108     {
109         return systemConf;
110     }
111 
112     /** @brief gets the dhcp conf object.
113      *
114      */
115     inline const DHCPConfPtr& getDHCPConf()
116     {
117         return dhcpConf;
118     }
119 
120     /** @brief This function gets the MAC address from the VPD and
121      *  sets it on the corresponding ethernet interface during first
122      *  Boot, once it sets the MAC from VPD, it creates a file named
123      *  firstBoot under /var/lib to make sure we dont run this function
124      *  again.
125      *
126      *  @param[in] ethPair - Its a pair of ethernet interface name & the
127      * corresponding MAC Address from the VPD
128      *
129      *  return - NULL
130      */
131     void setFistBootMACOnInterface(
132         const std::pair<std::string, std::string>& ethPair);
133 
134     /** @brief Arms a timer to tell systemd-network to reload all of the network
135      * configurations
136      */
137     virtual void reloadConfigsNoRefresh();
138     virtual void reloadConfigs();
139 
140     /** @brief Tell systemd-network to reload all of the network configurations
141      */
142     void doReloadConfigs();
143 
144     /** @brief Persistent map of EthernetInterface dbus objects and their names
145      */
146     string_umap<std::unique_ptr<EthernetInterface>> interfaces;
147     std::unordered_map<unsigned, EthernetInterface*> interfacesByIdx;
148 
149     /** @brief Get the routing table owned by the manager
150      *
151      * @return Routing table reference.
152      */
153     inline const auto& getRouteTable() const
154     {
155         return routeTable;
156     }
157 
158     /** @brief Adds a hook that runs immediately prior to reloading
159      *
160      *  @param[in] hook - The hook to execute before reloading
161      */
162     inline void addReloadPreHook(fu2::unique_function<void()>&& hook)
163     {
164         reloadPreHooks.push_back(std::move(hook));
165     }
166 
167   protected:
168     /** @brief Persistent sdbusplus DBus bus connection. */
169     sdbusplus::bus_t& bus;
170 
171     /** @brief BMC network reset - resets network configuration for BMC. */
172     void reset() override;
173 
174     /** @brief Path of Object. */
175     std::string objectPath;
176 
177     /** @brief pointer to system conf object. */
178     SystemConfPtr systemConf = nullptr;
179 
180     /** @brief pointer to dhcp conf object. */
181     DHCPConfPtr dhcpConf = nullptr;
182 
183     /** @brief Network Configuration directory. */
184     fs::path confDir;
185 
186     /** @brief The routing table */
187     route::Table routeTable;
188 
189     /** @brief Map of interface info for undiscovered interfaces */
190     struct UndiscoveredInfo
191     {
192         InterfaceInfo intf;
193         std::optional<in_addr> defgw4 = std::nullopt;
194         std::optional<in6_addr> defgw6 = std::nullopt;
195         std::unordered_map<IfAddr, AddressInfo> addrs = {};
196         std::unordered_map<InAddrAny, NeighborInfo> staticNeighs = {};
197     };
198     std::unordered_map<unsigned, UndiscoveredInfo> undiscoveredIntfInfo;
199 
200     /** @brief Map of enabled interfaces */
201     std::unordered_map<unsigned, bool> systemdNetworkdEnabled;
202     sdbusplus::bus::match_t systemdNetworkdEnabledMatch;
203 
204     /** @brief List of hooks to execute during the next reload */
205     std::vector<fu2::unique_function<void()>> reloadPreHooks;
206 
207     /** @brief Handles the recipt of an adminstrative state string */
208     void handleAdminState(std::string_view state, unsigned ifidx);
209 
210     /** @brief Creates the interface in the maps */
211     void createInterface(const UndiscoveredInfo& info, bool enabled);
212 };
213 
214 } // namespace network
215 } // namespace phosphor
216