xref: /openbmc/phosphor-networkd/src/network_manager.hpp (revision 67b09da41d5372bce0977381f6778ea088f5e8c0)
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     std::unordered_map<unsigned, EthernetInterface*> interfacesByIdx;
131 
132     /** @brief Get the routing table owned by the manager
133      *
134      * @return Routing table reference.
135      */
136     inline const auto& getRouteTable() const
137     {
138         return routeTable;
139     }
140 
141     /** @brief Adds a hook that runs immediately prior to reloading
142      *
143      *  @param[in] hook - The hook to execute before reloading
144      */
145     inline void addReloadPreHook(fu2::unique_function<void()>&& hook)
146     {
147         reloadPreHooks.push_back(std::move(hook));
148     }
149 
150   protected:
151     /** @brief Persistent sdbusplus DBus bus connection. */
152     sdbusplus::bus_t& bus;
153 
154     /** @brief BMC network reset - resets network configuration for BMC. */
155     void reset() override;
156 
157     /** @brief Path of Object. */
158     std::string objectPath;
159 
160     /** @brief pointer to system conf object. */
161     SystemConfPtr systemConf = nullptr;
162 
163     /** @brief pointer to dhcp conf object. */
164     DHCPConfPtr dhcpConf = nullptr;
165 
166     /** @brief Network Configuration directory. */
167     fs::path confDir;
168 
169     /** @brief The routing table */
170     route::Table routeTable;
171 
172     /** @brief List of hooks to execute during the next reload */
173     std::vector<fu2::unique_function<void()>> reloadPreHooks;
174 };
175 
176 } // namespace network
177 } // namespace phosphor
178