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