xref: /openbmc/phosphor-networkd/src/network_manager.hpp (revision c38b07102f67751ebd6aa46814c9d65c36b89812)
1 #pragma once
2 
3 #include "dhcp_configuration.hpp"
4 #include "ethernet_interface.hpp"
5 #include "routing_table.hpp"
6 #include "system_configuration.hpp"
7 #include "vlan_interface.hpp"
8 #include "xyz/openbmc_project/Network/VLAN/Create/server.hpp"
9 
10 #include <filesystem>
11 #include <function2/function2.hpp>
12 #include <memory>
13 #include <sdbusplus/bus.hpp>
14 #include <string>
15 #include <utility>
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() = delete;
47     Manager(const Manager&) = delete;
48     Manager& operator=(const Manager&) = delete;
49     Manager(Manager&&) = delete;
50     Manager& operator=(Manager&&) = delete;
51     virtual ~Manager() = default;
52 
53     /** @brief Constructor to put object onto bus at a dbus path.
54      *  @param[in] bus - Bus to attach to.
55      *  @param[in] objPath - Path to attach at.
56      *  @param[in] dir - Network Configuration directory path.
57      */
58     Manager(sdbusplus::bus_t& bus, const char* objPath, const std::string& dir);
59 
60     ObjectPath vlan(IntfName 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 Fetch the interface and the ipaddress details
67      *         from the system and create the ethernet interraces
68      *         dbus object.
69      */
70     virtual void createInterfaces();
71 
72     /** @brief create child interface object and the system conf object.
73      */
74     void createChildObjects();
75 
76     /** @brief sets the network conf directory.
77      *  @param[in] dirName - Absolute path of the directory.
78      */
79     void setConfDir(const fs::path& dir);
80 
81     /** @brief gets the network conf directory.
82      */
83     fs::path getConfDir()
84     {
85         return confDir;
86     }
87 
88     /** @brief gets the system conf object.
89      *
90      */
91     const SystemConfPtr& getSystemConf()
92     {
93         return systemConf;
94     }
95 
96     /** @brief gets the dhcp conf object.
97      *
98      */
99     const DHCPConfPtr& getDHCPConf()
100     {
101         return dhcpConf;
102     }
103 
104     /** @brief create the default network files for each interface
105      *  @detail if force param is true then forcefully create the network
106      *          files otherwise if network file doesn't exist then
107      *          create it.
108      *  @param[in] force - forcefully create the file
109      *  @return true if network file created else false
110      */
111     bool createDefaultNetworkFiles(bool force);
112 
113     /** @brief This function gets the MAC address from the VPD and
114      *  sets it on the corresponding ethernet interface during first
115      *  Boot, once it sets the MAC from VPD, it creates a file named
116      *  firstBoot under /var/lib to make sure we dont run this function
117      *  again.
118      *
119      *  @param[in] ethPair - Its a pair of ethernet interface name & the
120      * corresponding MAC Address from the VPD
121      *
122      *  return - NULL
123      */
124     void setFistBootMACOnInterface(
125         const std::pair<std::string, std::string>& ethPair);
126 
127     /** @brief Arms a timer to tell systemd-network to reload all of the network
128      * configurations
129      */
130     virtual void reloadConfigs();
131 
132     /** @brief Tell systemd-network to reload all of the network configurations
133      */
134     void doReloadConfigs();
135 
136     /** @brief Returns the number of interfaces under this manager.
137      *
138      * @return the number of interfaces managed by this manager.
139      */
140     int getInterfaceCount()
141     {
142         return interfaces.size();
143     }
144 
145     /** @brief Does the requested interface exist under this manager?
146      *
147      * @param[in] intf - the interface name to check.
148      * @return true if found, false otherwise.
149      */
150     bool hasInterface(const std::string& intf)
151     {
152         return (interfaces.find(intf) != interfaces.end());
153     }
154 
155     /** @brief Get the routing table owned by the manager
156      *
157      * @return Routing table reference.
158      */
159     inline const auto& getRouteTable() const
160     {
161         return routeTable;
162     }
163 
164     /** @brief Adds a hook that runs immediately prior to reloading
165      *
166      *  @param[in] hook - The hook to execute before reloading
167      */
168     inline void addReloadPreHook(fu2::unique_function<void()>&& hook)
169     {
170         reloadPreHooks.push_back(std::move(hook));
171     }
172 
173   protected:
174     /** @brief Persistent sdbusplus DBus bus connection. */
175     sdbusplus::bus_t& bus;
176 
177     /** @brief Persistent map of EthernetInterface dbus objects and their names
178      */
179     std::map<IntfName, std::shared_ptr<EthernetInterface>> interfaces;
180 
181     /** @brief BMC network reset - resets network configuration for BMC. */
182     void reset() override;
183 
184     /** @brief Path of Object. */
185     std::string objectPath;
186 
187     /** @brief pointer to system conf object. */
188     SystemConfPtr systemConf = nullptr;
189 
190     /** @brief pointer to dhcp conf object. */
191     DHCPConfPtr dhcpConf = nullptr;
192 
193     /** @brief Network Configuration directory. */
194     fs::path confDir;
195 
196     /** @brief The routing table */
197     route::Table routeTable;
198 
199     /** @brief List of hooks to execute during the next reload */
200     std::vector<fu2::unique_function<void()>> reloadPreHooks;
201 };
202 
203 } // namespace network
204 } // namespace phosphor
205