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