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