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