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 <string>
11 #include <vector>
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 
16 namespace phosphor
17 {
18 namespace network
19 {
20 
21 using Ifaces = sdbusplus::server::object_t<
22     sdbusplus::xyz::openbmc_project::Network::server::EthernetInterface,
23     sdbusplus::xyz::openbmc_project::Network::server::MACAddress,
24     sdbusplus::xyz::openbmc_project::Network::IP::server::Create,
25     sdbusplus::xyz::openbmc_project::Network::Neighbor::server::CreateStatic,
26     sdbusplus::xyz::openbmc_project::Collection::server::DeleteAll>;
27 
28 using IP = sdbusplus::xyz::openbmc_project::Network::server::IP;
29 
30 using EthernetInterfaceIntf =
31     sdbusplus::xyz::openbmc_project::Network::server::EthernetInterface;
32 using MacAddressIntf =
33     sdbusplus::xyz::openbmc_project::Network::server::MACAddress;
34 
35 using ServerList = std::vector<std::string>;
36 using ObjectPath = sdbusplus::message::object_path;
37 
38 class Manager;
39 
40 class TestEthernetInterface;
41 
42 class VlanInterface;
43 
44 namespace config
45 {
46 class Parser;
47 }
48 
49 using LinkSpeed = uint16_t;
50 using DuplexMode = uint8_t;
51 using Autoneg = uint8_t;
52 using LinkUp = bool;
53 using NICEnabled = bool;
54 using MTU = size_t;
55 using VlanId = uint32_t;
56 using InterfaceName = std::string;
57 using InterfaceInfo =
58     std::tuple<LinkSpeed, DuplexMode, Autoneg, LinkUp, NICEnabled, MTU>;
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() = delete;
69     EthernetInterface(const EthernetInterface&) = delete;
70     EthernetInterface& operator=(const EthernetInterface&) = delete;
71     EthernetInterface(EthernetInterface&&) = delete;
72     EthernetInterface& operator=(EthernetInterface&&) = delete;
73     virtual ~EthernetInterface() = default;
74 
75     /** @brief Constructor to put object onto bus at a dbus path.
76      *  @param[in] bus - Bus to attach to.
77      *  @param[in] objPath - Path to attach at.
78      *  @param[in] config - The parsed configuation file.
79      *  @param[in] parent - parent object.
80      *  @param[in] emitSignal - true if the object added signal needs to be
81      *                          send.
82      *  @param[in] enabled - Override the lookup of nicEnabled
83      */
84     EthernetInterface(sdbusplus::bus_t& bus, const std::string& objPath,
85                       const config::Parser& config, Manager& parent,
86                       bool emitSignal = true,
87                       std::optional<bool> enabled = std::nullopt);
88 
89     /** @brief Function used to load the nameservers.
90      */
91     void loadNameServers(const config::Parser& config);
92 
93     /** @brief Function to create ipAddress dbus object.
94      *  @param[in] addressType - Type of ip address.
95      *  @param[in] ipAddress- IP address.
96      *  @param[in] prefixLength - Length of prefix.
97      *  @param[in] gateway - Gateway ip address.
98      */
99 
100     ObjectPath ip(IP::Protocol addressType, std::string ipAddress,
101                   uint8_t prefixLength, std::string gateway) override;
102 
103     /** @brief Function to create static neighbor dbus object.
104      *  @param[in] ipAddress - IP address.
105      *  @param[in] macAddress - Low level MAC address.
106      */
107     ObjectPath neighbor(std::string ipAddress, std::string macAddress) override;
108 
109     /* @brief delete the dbus object of the given ipAddress.
110      * @param[in] ipAddress - IP address.
111      */
112     void deleteObject(const std::string& ipAddress);
113 
114     /* @brief delete the dbus object of the given ipAddress.
115      * @param[in] ipAddress - IP address.
116      */
117     void deleteStaticNeighborObject(const std::string& ipAddress);
118 
119     /* @brief delete the vlan dbus object of the given interface.
120      *        Also deletes the device file and the network file.
121      * @param[in] interface - VLAN Interface.
122      */
123     void deleteVLANObject(const std::string& interface);
124 
125     /* @brief creates the dbus object(IPaddres) given in the address list.
126      * @param[in] addrs - address list for which dbus objects needs
127      *                    to create.
128      */
129     void createIPAddressObjects();
130 
131     /* @brief creates the dbus object(Neighbor) given in the neighbor list.
132      */
133     void createStaticNeighborObjects();
134 
135     /* @brief Gets the index of the interface on the system
136      */
137     unsigned ifIndex() const;
138 
139     /* @brief Gets all the ip addresses.
140      * @returns the list of ipAddress.
141      */
142     inline const auto& getAddresses() const
143     {
144         return addrs;
145     }
146 
147     /* @brief Gets all the static neighbor entries.
148      * @returns Static neighbor map.
149      */
150     inline const auto& getStaticNeighbors() const
151     {
152         return staticNeighbors;
153     }
154 
155     /** Set value of DHCPEnabled */
156     DHCPConf dhcpEnabled() const override;
157     DHCPConf dhcpEnabled(DHCPConf value) override;
158     using EthernetInterfaceIntf::dhcp4;
159     bool dhcp4(bool value) override;
160     using EthernetInterfaceIntf::dhcp6;
161     bool dhcp6(bool value) override;
162 
163     /** Retrieve Link State */
164     bool linkUp() const override;
165 
166     /** Retrieve MTU Size */
167     size_t mtu() const override;
168 
169     /** Set size of MTU */
170     size_t mtu(size_t value) override;
171 
172     /** Set value of NICEnabled */
173     bool nicEnabled(bool value) override;
174 
175     /** @brief sets the MAC address.
176      *  @param[in] value - MAC address which needs to be set on the system.
177      *  @returns macAddress of the interface or throws an error.
178      */
179     std::string macAddress(std::string value) override;
180 
181     /** @brief check conf file for Router Advertisements
182      *
183      */
184     bool ipv6AcceptRA(bool value) override;
185     using EthernetInterfaceIntf::ipv6AcceptRA;
186 
187     /** @brief sets the NTP servers.
188      *  @param[in] value - vector of NTP servers.
189      */
190     ServerList ntpServers(ServerList value) override;
191 
192     /** @brief sets the Static DNS/nameservers.
193      *  @param[in] value - vector of DNS servers.
194      */
195 
196     ServerList staticNameServers(ServerList value) override;
197 
198     /** @brief create Vlan interface.
199      *  @param[in] id- VLAN identifier.
200      */
201     ObjectPath createVLAN(VlanId id);
202 
203     /** @brief load the vlan info from the system
204      *         and creates the ip address dbus objects.
205      *  @param[in] vlanID- VLAN identifier.
206      */
207     void loadVLAN(VlanId vlanID);
208 
209     /** @brief write the network conf file with the in-memory objects.
210      */
211     void writeConfigurationFile();
212 
213     /** @brief delete all dbus objects.
214      */
215     void deleteAll();
216 
217     /** @brief set the default v4 gateway of the interface.
218      *  @param[in] gateway - default v4 gateway of the interface.
219      */
220     std::string defaultGateway(std::string gateway) override;
221 
222     /** @brief set the default v6 gateway of the interface.
223      *  @param[in] gateway - default v6 gateway of the interface.
224      */
225     std::string defaultGateway6(std::string gateway) override;
226 
227     using EthernetInterfaceIntf::interfaceName;
228     using EthernetInterfaceIntf::linkUp;
229     using EthernetInterfaceIntf::mtu;
230     using EthernetInterfaceIntf::nicEnabled;
231     using MacAddressIntf::macAddress;
232 
233     using EthernetInterfaceIntf::defaultGateway;
234     using EthernetInterfaceIntf::defaultGateway6;
235     /** @brief Absolute path of the resolv conf file */
236     static constexpr auto resolvConfFile = "/etc/resolv.conf";
237 
238   protected:
239     /** @brief get the info of the ethernet interface.
240      *  @return tuple having the link speed,autonegotiation,duplexmode .
241      */
242     InterfaceInfo getInterfaceInfo() const;
243 
244     /* @brief delete the vlan interface from system.
245      * @param[in] interface - vlan Interface.
246      */
247     void deleteVLANFromSystem(const std::string& interface);
248 
249     /** @brief get the mac address of the interface.
250      *  @param[in] interfaceName - Network interface name.
251      *  @return macaddress on success
252      */
253 
254     std::string getMACAddress(const std::string& interfaceName) const;
255 
256     /** @brief construct the ip address dbus object path.
257      *  @param[in] addressType - Type of ip address.
258      *  @param[in] ipAddress - IP address.
259      *  @param[in] prefixLength - Length of prefix.
260      *  @param[in] gateway - Gateway address.
261      *  @param[in] origin - The origin entry of the IP::Address
262 
263      *  @return path of the address object.
264      */
265 
266     std::string generateObjectPath(IP::Protocol addressType,
267                                    const std::string& ipAddress,
268                                    uint8_t prefixLength,
269                                    const std::string& gateway,
270                                    IP::AddressOrigin origin) const;
271 
272     std::string
273         generateStaticNeighborObjectPath(const std::string& ipAddress,
274                                          const std::string& macAddress) const;
275 
276     /** @brief generates the id by doing hash of ipAddress,
277      *         prefixlength and the gateway.
278      *  @param[in] ipAddress - IP address.
279      *  @param[in] prefixLength - Length of prefix.
280      *  @param[in] gateway - Gateway address.
281      *  @param[in] origin - The string of the address origin
282      *  @return hash string.
283      */
284 
285     static std::string generateId(const std::string& ipAddress,
286                                   uint8_t prefixLength,
287                                   const std::string& gateway,
288                                   const std::string& origin);
289 
290     /** @brief generates the id by doing hash of ipAddress
291      *         and the mac address.
292      *  @param[in] ipAddress  - IP address.
293      *  @param[in] macAddress - Gateway address.
294      *  @return hash string.
295      */
296     static std::string generateNeighborId(const std::string& ipAddress,
297                                           const std::string& macAddress);
298 
299     /** @brief get the NTP server list from the network conf
300      *
301      */
302     ServerList getNTPServersFromConf();
303 
304     /** @brief get the name server details from the network conf
305      *
306      */
307     virtual ServerList getNameServerFromResolvd();
308 
309     /** @brief Persistent sdbusplus DBus bus connection. */
310     sdbusplus::bus_t& bus;
311 
312     /** @brief Network Manager object. */
313     Manager& manager;
314 
315     /** @brief Persistent map of IPAddress dbus objects and their names */
316     string_umap<std::unique_ptr<IPAddress>> addrs;
317 
318     /** @brief Persistent map of Neighbor dbus objects and their names */
319     string_umap<std::unique_ptr<Neighbor>> staticNeighbors;
320 
321     /** @brief Persistent map of VLAN interface dbus objects and their names */
322     string_umap<std::unique_ptr<VlanInterface>> vlanInterfaces;
323 
324     /** @brief Dbus object path */
325     std::string objPath;
326 
327     friend class TestEthernetInterface;
328 
329   private:
330     /** @brief Determines if DHCP is active for the IP::Protocol supplied.
331      *  @param[in] protocol - Either IPv4 or IPv6
332      *  @returns true/false value if DHCP is active for the input protocol
333      */
334     bool dhcpIsEnabled(IP::Protocol protocol);
335 
336     /** @brief Determines if the address is manually assigned
337      *  @param[in] origin - The origin entry of the IP::Address
338      *  @returns true/false value if the address is static
339      */
340     bool originIsManuallyAssigned(IP::AddressOrigin origin);
341 
342     /** @brief Determines if the NIC is enabled in systemd
343      *  @returns true/false value if the NIC is enabled
344      */
345     bool queryNicEnabled() const;
346 
347     std::string vlanIntfName(VlanId id) const;
348     std::string vlanObjPath(VlanId id) const;
349 };
350 
351 } // namespace network
352 } // namespace phosphor
353