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