1 #pragma once
2 
3 #include "dhcp_configuration.hpp"
4 #include "ethernet_interface.hpp"
5 #include "system_configuration.hpp"
6 #include "vlan_interface.hpp"
7 #include "xyz/openbmc_project/Network/VLAN/Create/server.hpp"
8 
9 #include <filesystem>
10 #include <list>
11 #include <memory>
12 #include <sdbusplus/bus.hpp>
13 #include <string>
14 #include <vector>
15 #include <xyz/openbmc_project/Common/FactoryReset/server.hpp>
16 
17 #ifndef SDBUSPP_NEW_CAMELCASE
18 #define vlan vLAN
19 #endif
20 
21 namespace phosphor
22 {
23 namespace network
24 {
25 
26 using SystemConfPtr = std::unique_ptr<SystemConfiguration>;
27 using DHCPConfPtr = std::unique_ptr<dhcp::Configuration>;
28 
29 namespace fs = std::filesystem;
30 namespace details
31 {
32 
33 template <typename T, typename U>
34 using ServerObject = typename sdbusplus::server::object::object<T, U>;
35 
36 using VLANCreateIface = details::ServerObject<
37     sdbusplus::xyz::openbmc_project::Network::VLAN::server::Create,
38     sdbusplus::xyz::openbmc_project::Common::server::FactoryReset>;
39 
40 } // namespace details
41 
42 /** @class Manager
43  *  @brief OpenBMC network manager implementation.
44  */
45 class Manager : public details::VLANCreateIface
46 {
47   public:
48     Manager() = delete;
49     Manager(const Manager&) = delete;
50     Manager& operator=(const Manager&) = delete;
51     Manager(Manager&&) = delete;
52     Manager& operator=(Manager&&) = delete;
53     virtual ~Manager() = default;
54 
55     /** @brief Constructor to put object onto bus at a dbus path.
56      *  @param[in] bus - Bus to attach to.
57      *  @param[in] objPath - Path to attach at.
58      *  @param[in] dir - Network Configuration directory path.
59      */
60     Manager(sdbusplus::bus::bus& bus, const char* objPath,
61             const std::string& dir);
62 
63     ObjectPath vlan(IntfName interfaceName, uint32_t id) override;
64 
65     /** @brief write the network conf file with the in-memory objects.
66      */
67     void writeToConfigurationFile();
68 
69     /** @brief Fetch the interface and the ipaddress details
70      *         from the system and create the ethernet interraces
71      *         dbus object.
72      */
73     virtual void createInterfaces();
74 
75     /** @brief create child interface object and the system conf object.
76      */
77     void createChildObjects();
78 
79     /** @brief sets the network conf directory.
80      *  @param[in] dirName - Absolute path of the directory.
81      */
82     void setConfDir(const fs::path& dir);
83 
84     /** @brief gets the network conf directory.
85      */
86     fs::path getConfDir()
87     {
88         return confDir;
89     }
90 
91     /** @brief gets the system conf object.
92      *
93      */
94     const SystemConfPtr& getSystemConf()
95     {
96         return systemConf;
97     }
98 
99     /** @brief gets the dhcp conf object.
100      *
101      */
102     const DHCPConfPtr& getDHCPConf()
103     {
104         return dhcpConf;
105     }
106 
107     /** @brief create the default network files for each interface
108      *  @detail if force param is true then forcefully create the network
109      *          files otherwise if network file doesn't exist then
110      *          create it.
111      *  @param[in] force - forcefully create the file
112      *  @return true if network file created else false
113      */
114     bool createDefaultNetworkFiles(bool force);
115 
116     /** @brief restart the network timers. */
117     void restartTimers();
118 
119     /** @brief This function gets the MAC address from the VPD and
120      *  sets it on the corresponding ethernet interface during first
121      *  Boot, once it sets the MAC from VPD, it creates a file named
122      *  firstBoot under /var/lib to make sure we dont run this function
123      *  again.
124      *
125      *  @param[in] ethPair - Its a pair of ethernet interface name & the
126      * corresponding MAC Address from the VPD
127      *
128      *  return - NULL
129      */
130     void setFistBootMACOnInterface(
131         const std::pair<std::string, std::string>& ethPair);
132 
133     /** @brief Restart the systemd unit
134      *  @param[in] unit - systemd unit name which needs to be
135      *                    restarted.
136      */
137     virtual void restartSystemdUnit(const std::string& unit);
138 
139     /** @brief Returns the number of interfaces under this manager.
140      *
141      * @return the number of interfaces managed by this manager.
142      */
143     int getInterfaceCount()
144     {
145         return interfaces.size();
146     }
147 
148     /** @brief Does the requested interface exist under this manager?
149      *
150      * @param[in] intf - the interface name to check.
151      * @return true if found, false otherwise.
152      */
153     bool hasInterface(const std::string& intf)
154     {
155         return (interfaces.find(intf) != interfaces.end());
156     }
157 
158   protected:
159     /** @brief Persistent sdbusplus DBus bus connection. */
160     sdbusplus::bus::bus& bus;
161 
162     /** @brief Persistent map of EthernetInterface dbus objects and their names
163      */
164     std::map<IntfName, std::shared_ptr<EthernetInterface>> interfaces;
165 
166     /** @brief BMC network reset - resets network configuration for BMC. */
167     void reset() override;
168 
169     /** @brief Path of Object. */
170     std::string objectPath;
171 
172     /** @brief pointer to system conf object. */
173     SystemConfPtr systemConf = nullptr;
174 
175     /** @brief pointer to dhcp conf object. */
176     DHCPConfPtr dhcpConf = nullptr;
177 
178     /** @brief Network Configuration directory. */
179     fs::path confDir;
180 };
181 
182 } // namespace network
183 } // namespace phosphor
184