blob: 3f7a0506cf69dc0393b427932985c756e9f106e9 [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>
William A. Kennington III5b179382022-11-15 15:23:26 -080014#include <sdbusplus/message/native_types.hpp>
Ratan Gupta05eb1092017-04-14 16:33:53 +053015#include <string>
William A. Kennington IIIdd9ef812022-10-05 02:08:02 -070016#include <string_view>
Ratan Gupta05eb1092017-04-14 16:33:53 +053017#include <vector>
Patrick Venture189d44e2018-07-09 12:30:59 -070018#include <xyz/openbmc_project/Common/FactoryReset/server.hpp>
Ratan Gupta05eb1092017-04-14 16:33:53 +053019
20namespace phosphor
21{
22namespace network
23{
24
William A. Kennington III5b179382022-11-15 15:23:26 -080025using ManagerIface = sdbusplus::server::object_t<
Michael Tritz29f2fd62017-05-22 15:27:26 -050026 sdbusplus::xyz::openbmc_project::Network::VLAN::server::Create,
27 sdbusplus::xyz::openbmc_project::Common::server::FactoryReset>;
Ratan Gupta05eb1092017-04-14 16:33:53 +053028
Ratan Gupta05eb1092017-04-14 16:33:53 +053029/** @class Manager
30 * @brief OpenBMC network manager implementation.
31 */
William A. Kennington III5b179382022-11-15 15:23:26 -080032class Manager : public ManagerIface
Ratan Gupta05eb1092017-04-14 16:33:53 +053033{
Gunnar Mills57d9c502018-09-14 14:42:34 -050034 public:
Gunnar Mills57d9c502018-09-14 14:42:34 -050035 Manager(const Manager&) = delete;
36 Manager& operator=(const Manager&) = delete;
37 Manager(Manager&&) = delete;
38 Manager& operator=(Manager&&) = delete;
39 virtual ~Manager() = default;
Ratan Gupta05eb1092017-04-14 16:33:53 +053040
Gunnar Mills57d9c502018-09-14 14:42:34 -050041 /** @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.
William A. Kennington IIIbe3bd2f2022-10-11 14:11:27 -070044 * @param[in] confDir - Network Configuration directory path.
Gunnar Mills57d9c502018-09-14 14:42:34 -050045 */
William A. Kennington IIIbe3bd2f2022-10-11 14:11:27 -070046 Manager(sdbusplus::bus_t& bus, const char* objPath,
William A. Kennington III5b179382022-11-15 15:23:26 -080047 const std::filesystem::path& confDir);
Ratan Gupta05eb1092017-04-14 16:33:53 +053048
William A. Kennington IIIdd9ef812022-10-05 02:08:02 -070049 ObjectPath vlan(std::string interfaceName, uint32_t id) override;
Ratan Gupta05eb1092017-04-14 16:33:53 +053050
Gunnar Mills57d9c502018-09-14 14:42:34 -050051 /** @brief write the network conf file with the in-memory objects.
52 */
53 void writeToConfigurationFile();
Ratan Gupta4f1c18b2017-05-25 12:59:35 +053054
William A. Kennington III80d29012022-11-12 02:31:40 -080055 /** @brief Adds a single interface to the interface map */
William A. Kennington III0813a242022-11-12 18:07:11 -080056 void addInterface(const InterfaceInfo& info);
57 void removeInterface(const InterfaceInfo& info);
William A. Kennington III80d29012022-11-12 02:31:40 -080058
William A. Kennington IIIed5ff472022-11-12 16:24:02 -080059 /** @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
Gunnar Mills57d9c502018-09-14 14:42:34 -050071 /** @brief gets the network conf directory.
72 */
William A. Kennington III5b179382022-11-15 15:23:26 -080073 inline const auto& getConfDir() const
Gunnar Mills57d9c502018-09-14 14:42:34 -050074 {
75 return confDir;
76 }
Ratan Gupta255d5142017-08-10 09:02:08 +053077
Gunnar Mills57d9c502018-09-14 14:42:34 -050078 /** @brief gets the system conf object.
79 *
80 */
William A. Kennington III5b179382022-11-15 15:23:26 -080081 inline auto& getSystemConf()
Gunnar Mills57d9c502018-09-14 14:42:34 -050082 {
William A. Kennington III5b179382022-11-15 15:23:26 -080083 return *systemConf;
Gunnar Mills57d9c502018-09-14 14:42:34 -050084 }
Ratan Gupta05eb1092017-04-14 16:33:53 +053085
Gunnar Mills57d9c502018-09-14 14:42:34 -050086 /** @brief gets the dhcp conf object.
87 *
88 */
William A. Kennington III5b179382022-11-15 15:23:26 -080089 inline auto& getDHCPConf()
Gunnar Mills57d9c502018-09-14 14:42:34 -050090 {
William A. Kennington III5b179382022-11-15 15:23:26 -080091 return *dhcpConf;
Gunnar Mills57d9c502018-09-14 14:42:34 -050092 }
Ratan Gupta2b106532017-07-25 16:05:02 +053093
Manojkiran Edacc099a82020-05-11 14:25:16 +053094 /** @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
William A. Kennington IIIc7cf25f2021-11-09 16:16:59 -0800108 /** @brief Arms a timer to tell systemd-network to reload all of the network
109 * configurations
William A. Kennington III56ecc782021-10-07 18:44:50 -0700110 */
111 virtual void reloadConfigs();
112
William A. Kennington IIIc7cf25f2021-11-09 16:16:59 -0800113 /** @brief Tell systemd-network to reload all of the network configurations
114 */
115 void doReloadConfigs();
116
William A. Kennington III09f3a4a2022-10-25 02:59:16 -0700117 /** @brief Persistent map of EthernetInterface dbus objects and their names
Patrick Ventured94d23e2019-01-07 13:05:23 -0800118 */
William A. Kennington III09f3a4a2022-10-25 02:59:16 -0700119 string_umap<std::unique_ptr<EthernetInterface>> interfaces;
William A. Kennington III67b09da2022-10-31 14:09:53 -0700120 std::unordered_map<unsigned, EthernetInterface*> interfacesByIdx;
Patrick Ventured94d23e2019-01-07 13:05:23 -0800121
William A. Kennington III6ff633a2021-11-09 17:09:12 -0800122 /** @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
Ratan Gupta35297172018-11-28 18:40:16 +0530131 protected:
Gunnar Mills57d9c502018-09-14 14:42:34 -0500132 /** @brief Persistent sdbusplus DBus bus connection. */
Patrick Williamsc38b0712022-07-22 19:26:54 -0500133 sdbusplus::bus_t& bus;
Ratan Gupta29b0e432017-05-25 12:51:40 +0530134
Gunnar Mills57d9c502018-09-14 14:42:34 -0500135 /** @brief BMC network reset - resets network configuration for BMC. */
136 void reset() override;
Michael Tritz29f2fd62017-05-22 15:27:26 -0500137
Gunnar Mills57d9c502018-09-14 14:42:34 -0500138 /** @brief Path of Object. */
William A. Kennington III5b179382022-11-15 15:23:26 -0800139 sdbusplus::message::object_path objPath;
Ratan Gupta29b0e432017-05-25 12:51:40 +0530140
Gunnar Mills57d9c502018-09-14 14:42:34 -0500141 /** @brief pointer to system conf object. */
William A. Kennington III5b179382022-11-15 15:23:26 -0800142 std::unique_ptr<SystemConfiguration> systemConf = nullptr;
Ratan Guptaef85eb92017-06-15 08:57:54 +0530143
Gunnar Mills57d9c502018-09-14 14:42:34 -0500144 /** @brief pointer to dhcp conf object. */
William A. Kennington III5b179382022-11-15 15:23:26 -0800145 std::unique_ptr<dhcp::Configuration> dhcpConf = nullptr;
Ratan Guptad16f88c2017-07-11 17:47:57 +0530146
Gunnar Mills57d9c502018-09-14 14:42:34 -0500147 /** @brief Network Configuration directory. */
William A. Kennington III5b179382022-11-15 15:23:26 -0800148 std::filesystem::path confDir;
William A. Kennington III6ff633a2021-11-09 17:09:12 -0800149
William A. Kennington III80d29012022-11-12 02:31:40 -0800150 /** @brief Map of interface info for undiscovered interfaces */
William A. Kennington IIIf30d5602022-11-13 17:09:55 -0800151 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;
William A. Kennington III3ee5b7e2022-11-15 15:04:37 -0800160 std::unordered_set<unsigned> ignoredIntf;
William A. Kennington III80d29012022-11-12 02:31:40 -0800161
162 /** @brief Map of enabled interfaces */
163 std::unordered_map<unsigned, bool> systemdNetworkdEnabled;
164 sdbusplus::bus::match_t systemdNetworkdEnabledMatch;
165
William A. Kennington III6ff633a2021-11-09 17:09:12 -0800166 /** @brief List of hooks to execute during the next reload */
167 std::vector<fu2::unique_function<void()>> reloadPreHooks;
William A. Kennington III80d29012022-11-12 02:31:40 -0800168
169 /** @brief Handles the recipt of an adminstrative state string */
170 void handleAdminState(std::string_view state, unsigned ifidx);
William A. Kennington III0813a242022-11-12 18:07:11 -0800171
172 /** @brief Creates the interface in the maps */
William A. Kennington IIIf30d5602022-11-13 17:09:55 -0800173 void createInterface(const UndiscoveredInfo& info, bool enabled);
Ratan Gupta05eb1092017-04-14 16:33:53 +0530174};
175
176} // namespace network
177} // namespace phosphor