xref: /openbmc/phosphor-networkd/src/network_manager.hpp (revision aac4b22fc42e3037832f2df0b25e393e134ea707)
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(InterfaceInfo& info, bool enabled);
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 Fetch the interface and the ipaddress details
82      *         from the system and create the ethernet interraces
83      *         dbus object.
84      */
85     virtual void createInterfaces();
86 
87     /** @brief create child interface object and the system conf object.
88      */
89     void createChildObjects();
90 
91     /** @brief sets the network conf directory.
92      *  @param[in] dirName - Absolute path of the directory.
93      */
94     void setConfDir(const fs::path& dir);
95 
96     /** @brief gets the network conf directory.
97      */
98     inline const fs::path& getConfDir() const
99     {
100         return confDir;
101     }
102 
103     /** @brief gets the system conf object.
104      *
105      */
106     inline const SystemConfPtr& getSystemConf()
107     {
108         return systemConf;
109     }
110 
111     /** @brief gets the dhcp conf object.
112      *
113      */
114     inline const DHCPConfPtr& getDHCPConf()
115     {
116         return dhcpConf;
117     }
118 
119     /** @brief This function gets the MAC address from the VPD and
120      *  sets it on the corresponding ethernet interface during first
121      *  Boot, once it sets the MAC from VPD, it creates a file named
122      *  firstBoot under /var/lib to make sure we dont run this function
123      *  again.
124      *
125      *  @param[in] ethPair - Its a pair of ethernet interface name & the
126      * corresponding MAC Address from the VPD
127      *
128      *  return - NULL
129      */
130     void setFistBootMACOnInterface(
131         const std::pair<std::string, std::string>& ethPair);
132 
133     /** @brief Arms a timer to tell systemd-network to reload all of the network
134      * configurations
135      */
136     virtual void reloadConfigsNoRefresh();
137     virtual void reloadConfigs();
138 
139     /** @brief Tell systemd-network to reload all of the network configurations
140      */
141     void doReloadConfigs();
142 
143     /** @brief Persistent map of EthernetInterface dbus objects and their names
144      */
145     string_umap<std::unique_ptr<EthernetInterface>> interfaces;
146     std::unordered_map<unsigned, EthernetInterface*> interfacesByIdx;
147 
148     /** @brief Get the routing table owned by the manager
149      *
150      * @return Routing table reference.
151      */
152     inline const auto& getRouteTable() const
153     {
154         return routeTable;
155     }
156 
157     /** @brief Adds a hook that runs immediately prior to reloading
158      *
159      *  @param[in] hook - The hook to execute before reloading
160      */
161     inline void addReloadPreHook(fu2::unique_function<void()>&& hook)
162     {
163         reloadPreHooks.push_back(std::move(hook));
164     }
165 
166   protected:
167     /** @brief Persistent sdbusplus DBus bus connection. */
168     sdbusplus::bus_t& bus;
169 
170     /** @brief BMC network reset - resets network configuration for BMC. */
171     void reset() override;
172 
173     /** @brief Path of Object. */
174     std::string objectPath;
175 
176     /** @brief pointer to system conf object. */
177     SystemConfPtr systemConf = nullptr;
178 
179     /** @brief pointer to dhcp conf object. */
180     DHCPConfPtr dhcpConf = nullptr;
181 
182     /** @brief Network Configuration directory. */
183     fs::path confDir;
184 
185     /** @brief The routing table */
186     route::Table routeTable;
187 
188     /** @brief Map of interface info for undiscovered interfaces */
189     std::unordered_map<unsigned, InterfaceInfo> undiscoveredIntfInfo;
190 
191     /** @brief Map of enabled interfaces */
192     std::unordered_map<unsigned, bool> systemdNetworkdEnabled;
193     sdbusplus::bus::match_t systemdNetworkdEnabledMatch;
194 
195     /** @brief List of hooks to execute during the next reload */
196     std::vector<fu2::unique_function<void()>> reloadPreHooks;
197 
198     /** @brief Handles the recipt of an adminstrative state string */
199     void handleAdminState(std::string_view state, unsigned ifidx);
200 };
201 
202 } // namespace network
203 } // namespace phosphor
204