blob: 146da3319007dc58b13fb171a3ae8d799ba2f873 [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"
Ratan Guptaef85eb92017-06-15 08:57:54 +05304#include "system_configuration.hpp"
William A. Kennington IIIdd9ef812022-10-05 02:08:02 -07005#include "types.hpp"
Patrick Venture189d44e2018-07-09 12:30:59 -07006#include "xyz/openbmc_project/Network/VLAN/Create/server.hpp"
Ratan Gupta5978dd12017-07-25 13:47:13 +05307
Manojkiran Edaa879baa2020-06-13 14:39:08 +05308#include <filesystem>
William A. Kennington III6ff633a2021-11-09 17:09:12 -08009#include <function2/function2.hpp>
Ratan Gupta6811f822017-04-14 16:34:56 +053010#include <memory>
Patrick Venture189d44e2018-07-09 12:30:59 -070011#include <sdbusplus/bus.hpp>
William A. Kennington III80d29012022-11-12 02:31:40 -080012#include <sdbusplus/bus/match.hpp>
William A. Kennington III5b179382022-11-15 15:23:26 -080013#include <sdbusplus/message/native_types.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
William A. Kennington III5b179382022-11-15 15:23:26 -080024using ManagerIface = sdbusplus::server::object_t<
Michael Tritz29f2fd62017-05-22 15:27:26 -050025 sdbusplus::xyz::openbmc_project::Network::VLAN::server::Create,
26 sdbusplus::xyz::openbmc_project::Common::server::FactoryReset>;
Ratan Gupta05eb1092017-04-14 16:33:53 +053027
Ratan Gupta05eb1092017-04-14 16:33:53 +053028/** @class Manager
29 * @brief OpenBMC network manager implementation.
30 */
William A. Kennington III5b179382022-11-15 15:23:26 -080031class Manager : public ManagerIface
Ratan Gupta05eb1092017-04-14 16:33:53 +053032{
Gunnar Mills57d9c502018-09-14 14:42:34 -050033 public:
Gunnar Mills57d9c502018-09-14 14:42:34 -050034 Manager(const Manager&) = delete;
35 Manager& operator=(const Manager&) = delete;
36 Manager(Manager&&) = delete;
37 Manager& operator=(Manager&&) = delete;
38 virtual ~Manager() = default;
Ratan Gupta05eb1092017-04-14 16:33:53 +053039
Gunnar Mills57d9c502018-09-14 14:42:34 -050040 /** @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.
William A. Kennington IIIbe3bd2f2022-10-11 14:11:27 -070043 * @param[in] confDir - Network Configuration directory path.
Gunnar Mills57d9c502018-09-14 14:42:34 -050044 */
William A. Kennington IIIbe3bd2f2022-10-11 14:11:27 -070045 Manager(sdbusplus::bus_t& bus, const char* objPath,
William A. Kennington III5b179382022-11-15 15:23:26 -080046 const std::filesystem::path& confDir);
Ratan Gupta05eb1092017-04-14 16:33:53 +053047
William A. Kennington IIIdd9ef812022-10-05 02:08:02 -070048 ObjectPath vlan(std::string interfaceName, uint32_t id) override;
Ratan Gupta05eb1092017-04-14 16:33:53 +053049
Gunnar Mills57d9c502018-09-14 14:42:34 -050050 /** @brief write the network conf file with the in-memory objects.
51 */
52 void writeToConfigurationFile();
Ratan Gupta4f1c18b2017-05-25 12:59:35 +053053
William A. Kennington III80d29012022-11-12 02:31:40 -080054 /** @brief Adds a single interface to the interface map */
William A. Kennington III0813a242022-11-12 18:07:11 -080055 void addInterface(const InterfaceInfo& info);
56 void removeInterface(const InterfaceInfo& info);
William A. Kennington III80d29012022-11-12 02:31:40 -080057
William A. Kennington IIIed5ff472022-11-12 16:24:02 -080058 /** @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
Gunnar Mills57d9c502018-09-14 14:42:34 -050070 /** @brief gets the network conf directory.
71 */
William A. Kennington III5b179382022-11-15 15:23:26 -080072 inline const auto& getConfDir() const
Gunnar Mills57d9c502018-09-14 14:42:34 -050073 {
74 return confDir;
75 }
Ratan Gupta255d5142017-08-10 09:02:08 +053076
Gunnar Mills57d9c502018-09-14 14:42:34 -050077 /** @brief gets the system conf object.
78 *
79 */
William A. Kennington III5b179382022-11-15 15:23:26 -080080 inline auto& getSystemConf()
Gunnar Mills57d9c502018-09-14 14:42:34 -050081 {
William A. Kennington III5b179382022-11-15 15:23:26 -080082 return *systemConf;
Gunnar Mills57d9c502018-09-14 14:42:34 -050083 }
Ratan Gupta05eb1092017-04-14 16:33:53 +053084
Gunnar Mills57d9c502018-09-14 14:42:34 -050085 /** @brief gets the dhcp conf object.
86 *
87 */
William A. Kennington III5b179382022-11-15 15:23:26 -080088 inline auto& getDHCPConf()
Gunnar Mills57d9c502018-09-14 14:42:34 -050089 {
William A. Kennington III5b179382022-11-15 15:23:26 -080090 return *dhcpConf;
Gunnar Mills57d9c502018-09-14 14:42:34 -050091 }
Ratan Gupta2b106532017-07-25 16:05:02 +053092
Manojkiran Edacc099a82020-05-11 14:25:16 +053093 /** @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
William A. Kennington IIIc7cf25f2021-11-09 16:16:59 -0800107 /** @brief Arms a timer to tell systemd-network to reload all of the network
108 * configurations
William A. Kennington III56ecc782021-10-07 18:44:50 -0700109 */
110 virtual void reloadConfigs();
111
William A. Kennington IIIc7cf25f2021-11-09 16:16:59 -0800112 /** @brief Tell systemd-network to reload all of the network configurations
113 */
114 void doReloadConfigs();
115
William A. Kennington III09f3a4a2022-10-25 02:59:16 -0700116 /** @brief Persistent map of EthernetInterface dbus objects and their names
Patrick Ventured94d23e2019-01-07 13:05:23 -0800117 */
William A. Kennington III09f3a4a2022-10-25 02:59:16 -0700118 string_umap<std::unique_ptr<EthernetInterface>> interfaces;
William A. Kennington III67b09da2022-10-31 14:09:53 -0700119 std::unordered_map<unsigned, EthernetInterface*> interfacesByIdx;
Patrick Ventured94d23e2019-01-07 13:05:23 -0800120
William A. Kennington III6ff633a2021-11-09 17:09:12 -0800121 /** @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 }
William A. Kennington IIIde4668a2022-11-17 16:02:30 -0800129 inline void addReloadPostHook(fu2::unique_function<void()>&& hook)
130 {
131 reloadPostHooks.push_back(std::move(hook));
132 }
William A. Kennington III6ff633a2021-11-09 17:09:12 -0800133
Ratan Gupta35297172018-11-28 18:40:16 +0530134 protected:
Gunnar Mills57d9c502018-09-14 14:42:34 -0500135 /** @brief Persistent sdbusplus DBus bus connection. */
Patrick Williamsc38b0712022-07-22 19:26:54 -0500136 sdbusplus::bus_t& bus;
Ratan Gupta29b0e432017-05-25 12:51:40 +0530137
Gunnar Mills57d9c502018-09-14 14:42:34 -0500138 /** @brief BMC network reset - resets network configuration for BMC. */
139 void reset() override;
Michael Tritz29f2fd62017-05-22 15:27:26 -0500140
Gunnar Mills57d9c502018-09-14 14:42:34 -0500141 /** @brief Path of Object. */
William A. Kennington III5b179382022-11-15 15:23:26 -0800142 sdbusplus::message::object_path objPath;
Ratan Gupta29b0e432017-05-25 12:51:40 +0530143
Gunnar Mills57d9c502018-09-14 14:42:34 -0500144 /** @brief pointer to system conf object. */
William A. Kennington III5b179382022-11-15 15:23:26 -0800145 std::unique_ptr<SystemConfiguration> systemConf = nullptr;
Ratan Guptaef85eb92017-06-15 08:57:54 +0530146
Gunnar Mills57d9c502018-09-14 14:42:34 -0500147 /** @brief pointer to dhcp conf object. */
William A. Kennington III5b179382022-11-15 15:23:26 -0800148 std::unique_ptr<dhcp::Configuration> dhcpConf = nullptr;
Ratan Guptad16f88c2017-07-11 17:47:57 +0530149
Gunnar Mills57d9c502018-09-14 14:42:34 -0500150 /** @brief Network Configuration directory. */
William A. Kennington III5b179382022-11-15 15:23:26 -0800151 std::filesystem::path confDir;
William A. Kennington III6ff633a2021-11-09 17:09:12 -0800152
William A. Kennington III80d29012022-11-12 02:31:40 -0800153 /** @brief Map of interface info for undiscovered interfaces */
William A. Kennington III2fb0c872022-11-15 19:35:39 -0800154 std::unordered_map<unsigned, AllIntfInfo> intfInfo;
William A. Kennington III3ee5b7e2022-11-15 15:04:37 -0800155 std::unordered_set<unsigned> ignoredIntf;
William A. Kennington III80d29012022-11-12 02:31:40 -0800156
157 /** @brief Map of enabled interfaces */
158 std::unordered_map<unsigned, bool> systemdNetworkdEnabled;
159 sdbusplus::bus::match_t systemdNetworkdEnabledMatch;
160
William A. Kennington III6ff633a2021-11-09 17:09:12 -0800161 /** @brief List of hooks to execute during the next reload */
162 std::vector<fu2::unique_function<void()>> reloadPreHooks;
William A. Kennington IIIde4668a2022-11-17 16:02:30 -0800163 std::vector<fu2::unique_function<void()>> reloadPostHooks;
William A. Kennington III80d29012022-11-12 02:31:40 -0800164
165 /** @brief Handles the recipt of an adminstrative state string */
166 void handleAdminState(std::string_view state, unsigned ifidx);
William A. Kennington III0813a242022-11-12 18:07:11 -0800167
168 /** @brief Creates the interface in the maps */
William A. Kennington III2fb0c872022-11-15 19:35:39 -0800169 void createInterface(const AllIntfInfo& info, bool enabled);
Ratan Gupta05eb1092017-04-14 16:33:53 +0530170};
171
172} // namespace network
173} // namespace phosphor