blob: 3f0eaab590bb828ff630469928f17085031f3efe [file] [log] [blame]
Ratan Gupta05eb1092017-04-14 16:33:53 +05301#pragma once
Patrick Venture189d44e2018-07-09 12:30:59 -07002#include "dhcp_configuration.hpp"
Ratan Gupta05eb1092017-04-14 16:33:53 +05303#include "ethernet_interface.hpp"
William A. Kennington IIIe0564842021-10-23 16:02:22 -07004#include "routing_table.hpp"
Ratan Guptaef85eb92017-06-15 08:57:54 +05305#include "system_configuration.hpp"
William A. Kennington IIIdd9ef812022-10-05 02:08:02 -07006#include "types.hpp"
Patrick Venture189d44e2018-07-09 12:30:59 -07007#include "xyz/openbmc_project/Network/VLAN/Create/server.hpp"
Ratan Gupta5978dd12017-07-25 13:47:13 +05308
Manojkiran Edaa879baa2020-06-13 14:39:08 +05309#include <filesystem>
William A. Kennington III6ff633a2021-11-09 17:09:12 -080010#include <function2/function2.hpp>
Ratan Gupta6811f822017-04-14 16:34:56 +053011#include <memory>
Patrick Venture189d44e2018-07-09 12:30:59 -070012#include <sdbusplus/bus.hpp>
William A. Kennington III80d29012022-11-12 02:31:40 -080013#include <sdbusplus/bus/match.hpp>
Ratan Gupta05eb1092017-04-14 16:33:53 +053014#include <string>
William A. Kennington IIIdd9ef812022-10-05 02:08:02 -070015#include <string_view>
Ratan Gupta05eb1092017-04-14 16:33:53 +053016#include <vector>
Patrick Venture189d44e2018-07-09 12:30:59 -070017#include <xyz/openbmc_project/Common/FactoryReset/server.hpp>
Ratan Gupta05eb1092017-04-14 16:33:53 +053018
19namespace phosphor
20{
21namespace network
22{
23
Ratan Gupta2b106532017-07-25 16:05:02 +053024using SystemConfPtr = std::unique_ptr<SystemConfiguration>;
25using DHCPConfPtr = std::unique_ptr<dhcp::Configuration>;
26
Manojkiran Edaa879baa2020-06-13 14:39:08 +053027namespace fs = std::filesystem;
Ratan Gupta05eb1092017-04-14 16:33:53 +053028namespace details
29{
30
Michael Tritz29f2fd62017-05-22 15:27:26 -050031template <typename T, typename U>
Patrick Williamsc38b0712022-07-22 19:26:54 -050032using ServerObject = typename sdbusplus::server::object_t<T, U>;
Ratan Gupta05eb1092017-04-14 16:33:53 +053033
Michael Tritz29f2fd62017-05-22 15:27:26 -050034using VLANCreateIface = details::ServerObject<
35 sdbusplus::xyz::openbmc_project::Network::VLAN::server::Create,
36 sdbusplus::xyz::openbmc_project::Common::server::FactoryReset>;
Ratan Gupta05eb1092017-04-14 16:33:53 +053037
Ratan Gupta05eb1092017-04-14 16:33:53 +053038} // namespace details
39
40/** @class Manager
41 * @brief OpenBMC network manager implementation.
42 */
43class Manager : public details::VLANCreateIface
44{
Gunnar Mills57d9c502018-09-14 14:42:34 -050045 public:
Gunnar Mills57d9c502018-09-14 14:42:34 -050046 Manager(const Manager&) = delete;
47 Manager& operator=(const Manager&) = delete;
48 Manager(Manager&&) = delete;
49 Manager& operator=(Manager&&) = delete;
50 virtual ~Manager() = default;
Ratan Gupta05eb1092017-04-14 16:33:53 +053051
Gunnar Mills57d9c502018-09-14 14:42:34 -050052 /** @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.
William A. Kennington IIIbe3bd2f2022-10-11 14:11:27 -070055 * @param[in] confDir - Network Configuration directory path.
Gunnar Mills57d9c502018-09-14 14:42:34 -050056 */
William A. Kennington IIIbe3bd2f2022-10-11 14:11:27 -070057 Manager(sdbusplus::bus_t& bus, const char* objPath,
58 const fs::path& confDir);
Ratan Gupta05eb1092017-04-14 16:33:53 +053059
William A. Kennington IIIdd9ef812022-10-05 02:08:02 -070060 ObjectPath vlan(std::string interfaceName, uint32_t id) override;
Ratan Gupta05eb1092017-04-14 16:33:53 +053061
Gunnar Mills57d9c502018-09-14 14:42:34 -050062 /** @brief write the network conf file with the in-memory objects.
63 */
64 void writeToConfigurationFile();
Ratan Gupta4f1c18b2017-05-25 12:59:35 +053065
William A. Kennington III80d29012022-11-12 02:31:40 -080066 /** @brief Adds a single interface to the interface map */
William A. Kennington III0813a242022-11-12 18:07:11 -080067 void addInterface(const InterfaceInfo& info);
68 void removeInterface(const InterfaceInfo& info);
William A. Kennington III80d29012022-11-12 02:31:40 -080069
William A. Kennington IIIed5ff472022-11-12 16:24:02 -080070 /** @brief Add / remove an address to the interface or queue */
71 void addAddress(const AddressInfo& info);
72 void removeAddress(const AddressInfo& info);
73
74 /** @brief Add / remove a neighbor to the interface or queue */
75 void addNeighbor(const NeighborInfo& info);
76 void removeNeighbor(const NeighborInfo& info);
77
78 /** @brief Add / remove default gateway for interface */
79 void addDefGw(unsigned ifidx, InAddrAny addr);
80 void removeDefGw(unsigned ifidx, InAddrAny addr);
81
Gunnar Mills57d9c502018-09-14 14:42:34 -050082 /** @brief Fetch the interface and the ipaddress details
83 * from the system and create the ethernet interraces
84 * dbus object.
85 */
Manojkiran Edaacd6dd52019-10-15 15:00:51 +053086 virtual void createInterfaces();
Ratan Gupta29b0e432017-05-25 12:51:40 +053087
Gunnar Mills57d9c502018-09-14 14:42:34 -050088 /** @brief create child interface object and the system conf object.
89 */
90 void createChildObjects();
Ratan Guptaef85eb92017-06-15 08:57:54 +053091
Gunnar Mills57d9c502018-09-14 14:42:34 -050092 /** @brief sets the network conf directory.
93 * @param[in] dirName - Absolute path of the directory.
94 */
95 void setConfDir(const fs::path& dir);
Ratan Guptaef85eb92017-06-15 08:57:54 +053096
Gunnar Mills57d9c502018-09-14 14:42:34 -050097 /** @brief gets the network conf directory.
98 */
William A. Kennington III72131802022-10-05 02:08:36 -070099 inline const fs::path& getConfDir() const
Gunnar Mills57d9c502018-09-14 14:42:34 -0500100 {
101 return confDir;
102 }
Ratan Gupta255d5142017-08-10 09:02:08 +0530103
Gunnar Mills57d9c502018-09-14 14:42:34 -0500104 /** @brief gets the system conf object.
105 *
106 */
William A. Kennington III72131802022-10-05 02:08:36 -0700107 inline const SystemConfPtr& getSystemConf()
Gunnar Mills57d9c502018-09-14 14:42:34 -0500108 {
109 return systemConf;
110 }
Ratan Gupta05eb1092017-04-14 16:33:53 +0530111
Gunnar Mills57d9c502018-09-14 14:42:34 -0500112 /** @brief gets the dhcp conf object.
113 *
114 */
William A. Kennington III72131802022-10-05 02:08:36 -0700115 inline const DHCPConfPtr& getDHCPConf()
Gunnar Mills57d9c502018-09-14 14:42:34 -0500116 {
117 return dhcpConf;
118 }
Ratan Gupta2b106532017-07-25 16:05:02 +0530119
Manojkiran Edacc099a82020-05-11 14:25:16 +0530120 /** @brief This function gets the MAC address from the VPD and
121 * sets it on the corresponding ethernet interface during first
122 * Boot, once it sets the MAC from VPD, it creates a file named
123 * firstBoot under /var/lib to make sure we dont run this function
124 * again.
125 *
126 * @param[in] ethPair - Its a pair of ethernet interface name & the
127 * corresponding MAC Address from the VPD
128 *
129 * return - NULL
130 */
131 void setFistBootMACOnInterface(
132 const std::pair<std::string, std::string>& ethPair);
133
William A. Kennington IIIc7cf25f2021-11-09 16:16:59 -0800134 /** @brief Arms a timer to tell systemd-network to reload all of the network
135 * configurations
William A. Kennington III56ecc782021-10-07 18:44:50 -0700136 */
William A. Kennington III85dc57a2022-11-07 16:53:24 -0800137 virtual void reloadConfigsNoRefresh();
William A. Kennington III56ecc782021-10-07 18:44:50 -0700138 virtual void reloadConfigs();
139
William A. Kennington IIIc7cf25f2021-11-09 16:16:59 -0800140 /** @brief Tell systemd-network to reload all of the network configurations
141 */
142 void doReloadConfigs();
143
William A. Kennington III09f3a4a2022-10-25 02:59:16 -0700144 /** @brief Persistent map of EthernetInterface dbus objects and their names
Patrick Ventured94d23e2019-01-07 13:05:23 -0800145 */
William A. Kennington III09f3a4a2022-10-25 02:59:16 -0700146 string_umap<std::unique_ptr<EthernetInterface>> interfaces;
William A. Kennington III67b09da2022-10-31 14:09:53 -0700147 std::unordered_map<unsigned, EthernetInterface*> interfacesByIdx;
Patrick Ventured94d23e2019-01-07 13:05:23 -0800148
William A. Kennington IIIe0564842021-10-23 16:02:22 -0700149 /** @brief Get the routing table owned by the manager
150 *
151 * @return Routing table reference.
152 */
153 inline const auto& getRouteTable() const
154 {
155 return routeTable;
156 }
157
William A. Kennington III6ff633a2021-11-09 17:09:12 -0800158 /** @brief Adds a hook that runs immediately prior to reloading
159 *
160 * @param[in] hook - The hook to execute before reloading
161 */
162 inline void addReloadPreHook(fu2::unique_function<void()>&& hook)
163 {
164 reloadPreHooks.push_back(std::move(hook));
165 }
166
Ratan Gupta35297172018-11-28 18:40:16 +0530167 protected:
Gunnar Mills57d9c502018-09-14 14:42:34 -0500168 /** @brief Persistent sdbusplus DBus bus connection. */
Patrick Williamsc38b0712022-07-22 19:26:54 -0500169 sdbusplus::bus_t& bus;
Ratan Gupta29b0e432017-05-25 12:51:40 +0530170
Gunnar Mills57d9c502018-09-14 14:42:34 -0500171 /** @brief BMC network reset - resets network configuration for BMC. */
172 void reset() override;
Michael Tritz29f2fd62017-05-22 15:27:26 -0500173
Gunnar Mills57d9c502018-09-14 14:42:34 -0500174 /** @brief Path of Object. */
175 std::string objectPath;
Ratan Gupta29b0e432017-05-25 12:51:40 +0530176
Gunnar Mills57d9c502018-09-14 14:42:34 -0500177 /** @brief pointer to system conf object. */
178 SystemConfPtr systemConf = nullptr;
Ratan Guptaef85eb92017-06-15 08:57:54 +0530179
Gunnar Mills57d9c502018-09-14 14:42:34 -0500180 /** @brief pointer to dhcp conf object. */
181 DHCPConfPtr dhcpConf = nullptr;
Ratan Guptad16f88c2017-07-11 17:47:57 +0530182
Gunnar Mills57d9c502018-09-14 14:42:34 -0500183 /** @brief Network Configuration directory. */
184 fs::path confDir;
William A. Kennington IIIe0564842021-10-23 16:02:22 -0700185
186 /** @brief The routing table */
187 route::Table routeTable;
William A. Kennington III6ff633a2021-11-09 17:09:12 -0800188
William A. Kennington III80d29012022-11-12 02:31:40 -0800189 /** @brief Map of interface info for undiscovered interfaces */
William A. Kennington IIIf30d5602022-11-13 17:09:55 -0800190 struct UndiscoveredInfo
191 {
192 InterfaceInfo intf;
193 std::optional<in_addr> defgw4 = std::nullopt;
194 std::optional<in6_addr> defgw6 = std::nullopt;
195 std::unordered_map<IfAddr, AddressInfo> addrs = {};
196 std::unordered_map<InAddrAny, NeighborInfo> staticNeighs = {};
197 };
198 std::unordered_map<unsigned, UndiscoveredInfo> undiscoveredIntfInfo;
William A. Kennington III80d29012022-11-12 02:31:40 -0800199
200 /** @brief Map of enabled interfaces */
201 std::unordered_map<unsigned, bool> systemdNetworkdEnabled;
202 sdbusplus::bus::match_t systemdNetworkdEnabledMatch;
203
William A. Kennington III6ff633a2021-11-09 17:09:12 -0800204 /** @brief List of hooks to execute during the next reload */
205 std::vector<fu2::unique_function<void()>> reloadPreHooks;
William A. Kennington III80d29012022-11-12 02:31:40 -0800206
207 /** @brief Handles the recipt of an adminstrative state string */
208 void handleAdminState(std::string_view state, unsigned ifidx);
William A. Kennington III0813a242022-11-12 18:07:11 -0800209
210 /** @brief Creates the interface in the maps */
William A. Kennington IIIf30d5602022-11-13 17:09:55 -0800211 void createInterface(const UndiscoveredInfo& info, bool enabled);
Ratan Gupta05eb1092017-04-14 16:33:53 +0530212};
213
214} // namespace network
215} // namespace phosphor