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