xref: /openbmc/phosphor-networkd/src/network_manager.hpp (revision 09f3a4ade17b076ce611320725e0871c3fa40031)
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 <string>
14 #include <string_view>
15 #include <vector>
16 #include <xyz/openbmc_project/Common/FactoryReset/server.hpp>
17 
18 namespace phosphor
19 {
20 namespace network
21 {
22 
23 using SystemConfPtr = std::unique_ptr<SystemConfiguration>;
24 using DHCPConfPtr = std::unique_ptr<dhcp::Configuration>;
25 
26 namespace fs = std::filesystem;
27 namespace details
28 {
29 
30 template <typename T, typename U>
31 using ServerObject = typename sdbusplus::server::object_t<T, U>;
32 
33 using VLANCreateIface = details::ServerObject<
34     sdbusplus::xyz::openbmc_project::Network::VLAN::server::Create,
35     sdbusplus::xyz::openbmc_project::Common::server::FactoryReset>;
36 
37 } // namespace details
38 
39 /** @class Manager
40  *  @brief OpenBMC network manager implementation.
41  */
42 class Manager : public details::VLANCreateIface
43 {
44   public:
45     Manager() = delete;
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 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     inline const fs::path& getConfDir() const
84     {
85         return confDir;
86     }
87 
88     /** @brief gets the system conf object.
89      *
90      */
91     inline const SystemConfPtr& getSystemConf()
92     {
93         return systemConf;
94     }
95 
96     /** @brief gets the dhcp conf object.
97      *
98      */
99     inline const DHCPConfPtr& getDHCPConf()
100     {
101         return dhcpConf;
102     }
103 
104     /** @brief This function gets the MAC address from the VPD and
105      *  sets it on the corresponding ethernet interface during first
106      *  Boot, once it sets the MAC from VPD, it creates a file named
107      *  firstBoot under /var/lib to make sure we dont run this function
108      *  again.
109      *
110      *  @param[in] ethPair - Its a pair of ethernet interface name & the
111      * corresponding MAC Address from the VPD
112      *
113      *  return - NULL
114      */
115     void setFistBootMACOnInterface(
116         const std::pair<std::string, std::string>& ethPair);
117 
118     /** @brief Arms a timer to tell systemd-network to reload all of the network
119      * configurations
120      */
121     virtual void reloadConfigs();
122 
123     /** @brief Tell systemd-network to reload all of the network configurations
124      */
125     void doReloadConfigs();
126 
127     /** @brief Persistent map of EthernetInterface dbus objects and their names
128      */
129     string_umap<std::unique_ptr<EthernetInterface>> interfaces;
130 
131     /** @brief Get the routing table owned by the manager
132      *
133      * @return Routing table reference.
134      */
135     inline const auto& getRouteTable() const
136     {
137         return routeTable;
138     }
139 
140     /** @brief Adds a hook that runs immediately prior to reloading
141      *
142      *  @param[in] hook - The hook to execute before reloading
143      */
144     inline void addReloadPreHook(fu2::unique_function<void()>&& hook)
145     {
146         reloadPreHooks.push_back(std::move(hook));
147     }
148 
149   protected:
150     /** @brief Persistent sdbusplus DBus bus connection. */
151     sdbusplus::bus_t& bus;
152 
153     /** @brief BMC network reset - resets network configuration for BMC. */
154     void reset() override;
155 
156     /** @brief Path of Object. */
157     std::string objectPath;
158 
159     /** @brief pointer to system conf object. */
160     SystemConfPtr systemConf = nullptr;
161 
162     /** @brief pointer to dhcp conf object. */
163     DHCPConfPtr dhcpConf = nullptr;
164 
165     /** @brief Network Configuration directory. */
166     fs::path confDir;
167 
168     /** @brief The routing table */
169     route::Table routeTable;
170 
171     /** @brief List of hooks to execute during the next reload */
172     std::vector<fu2::unique_function<void()>> reloadPreHooks;
173 };
174 
175 } // namespace network
176 } // namespace phosphor
177