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