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 "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 <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() = 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] confDir - Network Configuration directory path.
57      */
58     Manager(sdbusplus::bus_t& bus, const char* objPath,
59             const fs::path& confDir);
60 
61     ObjectPath vlan(std::string interfaceName, uint32_t id) override;
62 
63     /** @brief write the network conf file with the in-memory objects.
64      */
65     void writeToConfigurationFile();
66 
67     /** @brief Fetch the interface and the ipaddress details
68      *         from the system and create the ethernet interraces
69      *         dbus object.
70      */
71     virtual void createInterfaces();
72 
73     /** @brief create child interface object and the system conf object.
74      */
75     void createChildObjects();
76 
77     /** @brief sets the network conf directory.
78      *  @param[in] dirName - Absolute path of the directory.
79      */
80     void setConfDir(const fs::path& dir);
81 
82     /** @brief gets the network conf directory.
83      */
84     inline const fs::path& getConfDir() const
85     {
86         return confDir;
87     }
88 
89     /** @brief gets the system conf object.
90      *
91      */
92     inline const SystemConfPtr& getSystemConf()
93     {
94         return systemConf;
95     }
96 
97     /** @brief gets the dhcp conf object.
98      *
99      */
100     inline const DHCPConfPtr& getDHCPConf()
101     {
102         return dhcpConf;
103     }
104 
105     /** @brief create the default network files for each interface
106      *  @return true if network file created else false
107      */
108     bool createDefaultNetworkFiles();
109 
110     /** @brief This function gets the MAC address from the VPD and
111      *  sets it on the corresponding ethernet interface during first
112      *  Boot, once it sets the MAC from VPD, it creates a file named
113      *  firstBoot under /var/lib to make sure we dont run this function
114      *  again.
115      *
116      *  @param[in] ethPair - Its a pair of ethernet interface name & the
117      * corresponding MAC Address from the VPD
118      *
119      *  return - NULL
120      */
121     void setFistBootMACOnInterface(
122         const std::pair<std::string, std::string>& ethPair);
123 
124     /** @brief Arms a timer to tell systemd-network to reload all of the network
125      * configurations
126      */
127     virtual void reloadConfigs();
128 
129     /** @brief Tell systemd-network to reload all of the network configurations
130      */
131     void doReloadConfigs();
132 
133     /** @brief Get the interfaces owned by the manager
134      *
135      * @return Interfaces reference.
136      */
137     inline const auto& getInterfaces() const
138     {
139         return interfaces;
140     }
141 
142     /** @brief Get the routing table owned by the manager
143      *
144      * @return Routing table reference.
145      */
146     inline const auto& getRouteTable() const
147     {
148         return routeTable;
149     }
150 
151     /** @brief Adds a hook that runs immediately prior to reloading
152      *
153      *  @param[in] hook - The hook to execute before reloading
154      */
155     inline void addReloadPreHook(fu2::unique_function<void()>&& hook)
156     {
157         reloadPreHooks.push_back(std::move(hook));
158     }
159 
160   protected:
161     /** @brief Persistent sdbusplus DBus bus connection. */
162     sdbusplus::bus_t& bus;
163 
164     /** @brief Persistent map of EthernetInterface dbus objects and their names
165      */
166     string_umap<std::unique_ptr<EthernetInterface>> interfaces;
167 
168     /** @brief BMC network reset - resets network configuration for BMC. */
169     void reset() override;
170 
171     /** @brief Path of Object. */
172     std::string objectPath;
173 
174     /** @brief pointer to system conf object. */
175     SystemConfPtr systemConf = nullptr;
176 
177     /** @brief pointer to dhcp conf object. */
178     DHCPConfPtr dhcpConf = nullptr;
179 
180     /** @brief Network Configuration directory. */
181     fs::path confDir;
182 
183     /** @brief The routing table */
184     route::Table routeTable;
185 
186     /** @brief List of hooks to execute during the next reload */
187     std::vector<fu2::unique_function<void()>> reloadPreHooks;
188 };
189 
190 } // namespace network
191 } // namespace phosphor
192