xref: /openbmc/phosphor-networkd/src/ethernet_interface.hpp (revision ab27a819bee0db24233f9d0e7e142b3e3424c5b8)
1 #pragma once
2 #include "dhcp_configuration.hpp"
3 #include "ipaddress.hpp"
4 #include "neighbor.hpp"
5 #include "static_gateway.hpp"
6 #include "types.hpp"
7 #include "xyz/openbmc_project/Network/IP/Create/server.hpp"
8 #include "xyz/openbmc_project/Network/Neighbor/CreateStatic/server.hpp"
9 #include "xyz/openbmc_project/Network/StaticGateway/Create/server.hpp"
10 
11 #include <sdbusplus/bus.hpp>
12 #include <sdbusplus/server/object.hpp>
13 #include <stdplus/pinned.hpp>
14 #include <stdplus/str/maps.hpp>
15 #include <stdplus/zstring_view.hpp>
16 #include <xyz/openbmc_project/Collection/DeleteAll/server.hpp>
17 #include <xyz/openbmc_project/Network/EthernetInterface/server.hpp>
18 #include <xyz/openbmc_project/Network/MACAddress/server.hpp>
19 #include <xyz/openbmc_project/Network/StaticGateway/server.hpp>
20 #include <xyz/openbmc_project/Network/VLAN/server.hpp>
21 #include <xyz/openbmc_project/Object/Delete/server.hpp>
22 
23 #include <optional>
24 #include <string>
25 #include <vector>
26 
27 namespace phosphor
28 {
29 namespace network
30 {
31 
32 using Ifaces = sdbusplus::server::object_t<
33     sdbusplus::xyz::openbmc_project::Network::server::EthernetInterface,
34     sdbusplus::xyz::openbmc_project::Network::server::MACAddress,
35     sdbusplus::xyz::openbmc_project::Network::IP::server::Create,
36     sdbusplus::xyz::openbmc_project::Network::Neighbor::server::CreateStatic,
37     sdbusplus::xyz::openbmc_project::Network::StaticGateway::server::Create,
38     sdbusplus::xyz::openbmc_project::Collection::server::DeleteAll>;
39 
40 using VlanIfaces = sdbusplus::server::object_t<
41     sdbusplus::xyz::openbmc_project::Object::server::Delete,
42     sdbusplus::xyz::openbmc_project::Network::server::VLAN>;
43 
44 using VlanIntf = sdbusplus::xyz::openbmc_project::Network::server::VLAN;
45 
46 using IP = sdbusplus::xyz::openbmc_project::Network::server::IP;
47 
48 using EthernetInterfaceIntf =
49     sdbusplus::xyz::openbmc_project::Network::server::EthernetInterface;
50 using MacAddressIntf =
51     sdbusplus::xyz::openbmc_project::Network::server::MACAddress;
52 using StaticGatewayIntf =
53     sdbusplus::xyz::openbmc_project::Network::server::StaticGateway;
54 
55 using ServerList = std::vector<std::string>;
56 using ObjectPath = sdbusplus::message::object_path;
57 
58 class Manager;
59 
60 class TestEthernetInterface;
61 class TestNetworkManager;
62 
63 namespace config
64 {
65 class Parser;
66 }
67 
68 /** @class EthernetInterface
69  *  @brief OpenBMC Ethernet Interface implementation.
70  *  @details A concrete implementation for the
71  *  xyz.openbmc_project.Network.EthernetInterface DBus API.
72  */
73 class EthernetInterface : public Ifaces
74 {
75   public:
76     EthernetInterface(EthernetInterface&&) = delete;
77     EthernetInterface& operator=(EthernetInterface&&) = delete;
78 
79     /** @brief Constructor to put object onto bus at a dbus path.
80      *  @param[in] bus - Bus to attach to.
81      *  @param[in] manager - parent object.
82      *  @param[in] info - Interface information.
83      *  @param[in] objRoot - Path to attach at.
84      *  @param[in] config - The parsed configuration file.
85      *  @param[in] vlan - The id of the vlan if configured
86      *  @param[in] enabled - Determine if systemd-networkd is managing this link
87      */
88     EthernetInterface(stdplus::PinnedRef<sdbusplus::bus_t> bus,
89                       stdplus::PinnedRef<Manager> manager,
90                       const AllIntfInfo& info, std::string_view objRoot,
91                       const config::Parser& config, bool enabled);
92 
93     /** @brief Network Manager object. */
94     stdplus::PinnedRef<Manager> manager;
95 
96     /** @brief Persistent map of IPAddress dbus objects and their names */
97     std::unordered_map<stdplus::SubnetAny, std::unique_ptr<IPAddress>> addrs;
98 
99     /** @brief Persistent map of Neighbor dbus objects and their names */
100     std::unordered_map<stdplus::InAnyAddr, std::unique_ptr<Neighbor>>
101         staticNeighbors;
102 
103     /** @brief Persistent map of static route dbus objects and their names */
104     std::unordered_map<std::string, std::unique_ptr<StaticGateway>>
105         staticGateways;
106 
107     void addAddr(const AddressInfo& info);
108     void addStaticNeigh(const NeighborInfo& info);
109     void addStaticGateway(const StaticGatewayInfo& info);
110 
111     /** @brief Updates the interface information based on new InterfaceInfo */
112     void updateInfo(const InterfaceInfo& info, bool skipSignal = false);
113 
114     /** @brief Function used to load the ntpservers
115      */
116     void loadNTPServers(const config::Parser& config);
117 
118     /** @brief Function used to load the nameservers.
119      */
120     void loadNameServers(const config::Parser& config);
121 
122     /** @brief Function to create ipAddress dbus object.
123      *  @param[in] addressType - Type of ip address.
124      *  @param[in] ipAddress- IP address.
125      *  @param[in] prefixLength - Length of prefix.
126      */
127 
128     ObjectPath ip(IP::Protocol addressType, std::string ipAddress,
129                   uint8_t prefixLength, std::string) override;
130 
131     /** @brief Function to create static neighbor dbus object.
132      *  @param[in] ipAddress - IP address.
133      *  @param[in] macAddress - Low level MAC address.
134      */
135     ObjectPath neighbor(std::string ipAddress, std::string macAddress) override;
136 
137     /** @brief Function to create static route dbus object.
138      *  @param[in] destination - Destination IP address.
139      *  @param[in] gateway - Gateway
140      *  @parma[in] prefixLength - Number of network bits.
141      */
142     ObjectPath staticGateway(std::string gateway,
143                              IP::Protocol protocolType) override;
144 
145     /** Set value of DHCPEnabled */
146     DHCPConf dhcpEnabled() const override;
147     DHCPConf dhcpEnabled(DHCPConf value) override;
148     using EthernetInterfaceIntf::dhcp4;
149     bool dhcp4(bool value) override;
150     using EthernetInterfaceIntf::dhcp6;
151     bool dhcp6(bool value) override;
152 
dhcpIsEnabled(stdplus::In4Addr) const153     inline bool dhcpIsEnabled(stdplus::In4Addr) const
154     {
155         return dhcp4();
156     }
dhcpIsEnabled(stdplus::In6Addr) const157     inline bool dhcpIsEnabled(stdplus::In6Addr) const
158     {
159         return dhcp6();
160     }
dhcpIsEnabled(stdplus::InAnyAddr addr) const161     inline bool dhcpIsEnabled(stdplus::InAnyAddr addr) const
162     {
163         return std::visit([&](auto v) { return dhcpIsEnabled(v); }, addr);
164     }
165 
166     /** Set size of MTU */
167     size_t mtu(size_t value) override;
168 
169     /** Set value of NICEnabled */
170     bool nicEnabled(bool value) override;
171 
172     /** @brief sets the MAC address.
173      *  @param[in] value - MAC address which needs to be set on the system.
174      *  @returns macAddress of the interface or throws an error.
175      */
176     std::string macAddress(std::string value) override;
177 
178     /** @brief check conf file for Router Advertisements
179      *
180      */
181     bool ipv6AcceptRA(bool value) override;
182     using EthernetInterfaceIntf::ipv6AcceptRA;
183 
184     /** @brief sets the NTP servers.
185      *  @param[in] value - vector of NTP servers.
186      */
187     ServerList ntpServers(ServerList value) override;
188 
189     /** @brief sets the static NTP servers.
190      *  @param[in] value - vector of NTP servers.
191      */
192     ServerList staticNTPServers(ServerList value) override;
193 
194     /** @brief Get value of nameservers */
195     ServerList nameservers() const override;
196 
197     /** @brief sets the Static DNS/nameservers.
198      *  @param[in] value - vector of DNS servers.
199      */
200 
201     ServerList staticNameServers(ServerList value) override;
202 
203     /** @brief create Vlan interface.
204      *  @param[in] id- VLAN identifier.
205      */
206     ObjectPath createVLAN(uint16_t id);
207 
208     /** @brief write the network conf file with the in-memory objects.
209      */
210     void writeConfigurationFile();
211 
212     /** @brief delete all dbus objects.
213      */
214     void deleteAll() override;
215 
216     /** @brief set the default v4 gateway of the interface.
217      *  @param[in] gateway - default v4 gateway of the interface.
218      */
219     std::string defaultGateway(std::string gateway) override;
220 
221     /** @brief set the default v6 gateway of the interface.
222      *  @param[in] gateway - default v6 gateway of the interface.
223      */
224     std::string defaultGateway6(std::string gateway) override;
225 
226     /** @brief Function to reload network configurations.
227      */
228     void reloadConfigs();
229 
230     using EthernetInterfaceIntf::interfaceName;
231     using EthernetInterfaceIntf::linkUp;
232     using EthernetInterfaceIntf::mtu;
233     using EthernetInterfaceIntf::nicEnabled;
234     using MacAddressIntf::macAddress;
235 
236     using EthernetInterfaceIntf::defaultGateway;
237     using EthernetInterfaceIntf::defaultGateway6;
238 
239   protected:
240     /** @brief get the NTP server list from the timsyncd dbus obj
241      *
242      */
243     virtual ServerList getNTPServerFromTimeSyncd();
244 
245     /** @brief get the name server details from the network conf
246      *
247      */
248     virtual ServerList getNameServerFromResolvd() const;
249 
250     /** @brief Persistent sdbusplus DBus bus connection. */
251     stdplus::PinnedRef<sdbusplus::bus_t> bus;
252 
253     /** @brief Dbus object path */
254     std::string objPath;
255 
256     /** @brief Interface index */
257     unsigned ifIdx;
258 
259     struct VlanProperties : VlanIfaces
260     {
261         VlanProperties(sdbusplus::bus_t& bus, stdplus::const_zstring objPath,
262                        const InterfaceInfo& info,
263                        stdplus::PinnedRef<EthernetInterface> eth);
264         void delete_() override;
265         unsigned parentIdx;
266         stdplus::PinnedRef<EthernetInterface> eth;
267     };
268     std::optional<VlanProperties> vlan;
269 
270     std::optional<dhcp::Configuration> dhcp4Conf, dhcp6Conf;
271 
272     friend class TestEthernetInterface;
273     friend class TestNetworkManager;
274 
275   private:
276     EthernetInterface(stdplus::PinnedRef<sdbusplus::bus_t> bus,
277                       stdplus::PinnedRef<Manager> manager,
278                       const AllIntfInfo& info, std::string&& objPath,
279                       const config::Parser& config, bool enabled);
280 };
281 
282 } // namespace network
283 } // namespace phosphor
284