1 #pragma once
2 #include "dhcp_configuration.hpp"
3 #include "ethernet_interface.hpp"
4 #include "system_configuration.hpp"
5 #include "types.hpp"
6 #include "xyz/openbmc_project/Network/VLAN/Create/server.hpp"
7 
8 #include <filesystem>
9 #include <function2/function2.hpp>
10 #include <memory>
11 #include <sdbusplus/bus.hpp>
12 #include <sdbusplus/bus/match.hpp>
13 #include <sdbusplus/message/native_types.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 ManagerIface = sdbusplus::server::object_t<
25     sdbusplus::xyz::openbmc_project::Network::VLAN::server::Create,
26     sdbusplus::xyz::openbmc_project::Common::server::FactoryReset>;
27 
28 /** @class Manager
29  *  @brief OpenBMC network manager implementation.
30  */
31 class Manager : public ManagerIface
32 {
33   public:
34     Manager(const Manager&) = delete;
35     Manager& operator=(const Manager&) = delete;
36     Manager(Manager&&) = delete;
37     Manager& operator=(Manager&&) = delete;
38     virtual ~Manager() = default;
39 
40     /** @brief Constructor to put object onto bus at a dbus path.
41      *  @param[in] bus - Bus to attach to.
42      *  @param[in] objPath - Path to attach at.
43      *  @param[in] confDir - Network Configuration directory path.
44      */
45     Manager(sdbusplus::bus_t& bus, const char* objPath,
46             const std::filesystem::path& confDir);
47 
48     ObjectPath vlan(std::string interfaceName, uint32_t id) override;
49 
50     /** @brief write the network conf file with the in-memory objects.
51      */
52     void writeToConfigurationFile();
53 
54     /** @brief Adds a single interface to the interface map */
55     void addInterface(const InterfaceInfo& info);
56     void removeInterface(const InterfaceInfo& info);
57 
58     /** @brief Add / remove an address to the interface or queue */
59     void addAddress(const AddressInfo& info);
60     void removeAddress(const AddressInfo& info);
61 
62     /** @brief Add / remove a neighbor to the interface or queue */
63     void addNeighbor(const NeighborInfo& info);
64     void removeNeighbor(const NeighborInfo& info);
65 
66     /** @brief Add / remove default gateway for interface */
67     void addDefGw(unsigned ifidx, InAddrAny addr);
68     void removeDefGw(unsigned ifidx, InAddrAny addr);
69 
70     /** @brief gets the network conf directory.
71      */
72     inline const auto& getConfDir() const
73     {
74         return confDir;
75     }
76 
77     /** @brief gets the system conf object.
78      *
79      */
80     inline auto& getSystemConf()
81     {
82         return *systemConf;
83     }
84 
85     /** @brief gets the dhcp conf object.
86      *
87      */
88     inline auto& getDHCPConf()
89     {
90         return *dhcpConf;
91     }
92 
93     /** @brief This function gets the MAC address from the VPD and
94      *  sets it on the corresponding ethernet interface during first
95      *  Boot, once it sets the MAC from VPD, it creates a file named
96      *  firstBoot under /var/lib to make sure we dont run this function
97      *  again.
98      *
99      *  @param[in] ethPair - Its a pair of ethernet interface name & the
100      * corresponding MAC Address from the VPD
101      *
102      *  return - NULL
103      */
104     void setFistBootMACOnInterface(
105         const std::pair<std::string, std::string>& ethPair);
106 
107     /** @brief Arms a timer to tell systemd-network to reload all of the network
108      * configurations
109      */
110     virtual void reloadConfigs();
111 
112     /** @brief Tell systemd-network to reload all of the network configurations
113      */
114     void doReloadConfigs();
115 
116     /** @brief Persistent map of EthernetInterface dbus objects and their names
117      */
118     string_umap<std::unique_ptr<EthernetInterface>> interfaces;
119     std::unordered_map<unsigned, EthernetInterface*> interfacesByIdx;
120 
121     /** @brief Adds a hook that runs immediately prior to reloading
122      *
123      *  @param[in] hook - The hook to execute before reloading
124      */
125     inline void addReloadPreHook(fu2::unique_function<void()>&& hook)
126     {
127         reloadPreHooks.push_back(std::move(hook));
128     }
129 
130   protected:
131     /** @brief Persistent sdbusplus DBus bus connection. */
132     sdbusplus::bus_t& bus;
133 
134     /** @brief BMC network reset - resets network configuration for BMC. */
135     void reset() override;
136 
137     /** @brief Path of Object. */
138     sdbusplus::message::object_path objPath;
139 
140     /** @brief pointer to system conf object. */
141     std::unique_ptr<SystemConfiguration> systemConf = nullptr;
142 
143     /** @brief pointer to dhcp conf object. */
144     std::unique_ptr<dhcp::Configuration> dhcpConf = nullptr;
145 
146     /** @brief Network Configuration directory. */
147     std::filesystem::path confDir;
148 
149     /** @brief Map of interface info for undiscovered interfaces */
150     std::unordered_map<unsigned, AllIntfInfo> intfInfo;
151     std::unordered_set<unsigned> ignoredIntf;
152 
153     /** @brief Map of enabled interfaces */
154     std::unordered_map<unsigned, bool> systemdNetworkdEnabled;
155     sdbusplus::bus::match_t systemdNetworkdEnabledMatch;
156 
157     /** @brief List of hooks to execute during the next reload */
158     std::vector<fu2::unique_function<void()>> reloadPreHooks;
159 
160     /** @brief Handles the recipt of an adminstrative state string */
161     void handleAdminState(std::string_view state, unsigned ifidx);
162 
163     /** @brief Creates the interface in the maps */
164     void createInterface(const AllIntfInfo& info, bool enabled);
165 };
166 
167 } // namespace network
168 } // namespace phosphor
169