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 <list>
12 #include <memory>
13 #include <sdbusplus/bus.hpp>
14 #include <string>
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::object<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] dir - Network Configuration directory path.
56      */
57     Manager(sdbusplus::bus::bus& bus, const char* objPath,
58             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 Tell systemd-network to reload all of the network configurations
128      */
129     virtual void reloadConfigs();
130 
131     /** @brief Returns the number of interfaces under this manager.
132      *
133      * @return the number of interfaces managed by this manager.
134      */
135     int getInterfaceCount()
136     {
137         return interfaces.size();
138     }
139 
140     /** @brief Does the requested interface exist under this manager?
141      *
142      * @param[in] intf - the interface name to check.
143      * @return true if found, false otherwise.
144      */
145     bool hasInterface(const std::string& intf)
146     {
147         return (interfaces.find(intf) != interfaces.end());
148     }
149 
150     /** @brief Get the routing table owned by the manager
151      *
152      * @return Routing table reference.
153      */
154     inline const auto& getRouteTable() const
155     {
156         return routeTable;
157     }
158 
159   protected:
160     /** @brief Persistent sdbusplus DBus bus connection. */
161     sdbusplus::bus::bus& bus;
162 
163     /** @brief Persistent map of EthernetInterface dbus objects and their names
164      */
165     std::map<IntfName, std::shared_ptr<EthernetInterface>> interfaces;
166 
167     /** @brief BMC network reset - resets network configuration for BMC. */
168     void reset() override;
169 
170     /** @brief Path of Object. */
171     std::string objectPath;
172 
173     /** @brief pointer to system conf object. */
174     SystemConfPtr systemConf = nullptr;
175 
176     /** @brief pointer to dhcp conf object. */
177     DHCPConfPtr dhcpConf = nullptr;
178 
179     /** @brief Network Configuration directory. */
180     fs::path confDir;
181 
182     /** @brief The routing table */
183     route::Table routeTable;
184 };
185 
186 } // namespace network
187 } // namespace phosphor
188