xref: /openbmc/phosphor-networkd/src/ethernet_interface.hpp (revision 434a943cd836eb7149d4e38244a3aaa1a973c04e)
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 Network Manager object. */
95     Manager& manager;
96 
97     /** @brief Persistent map of IPAddress dbus objects and their names */
98     std::unordered_map<IfAddr, std::unique_ptr<IPAddress>> addrs;
99 
100     /** @brief Persistent map of Neighbor dbus objects and their names */
101     std::unordered_map<InAddrAny, std::unique_ptr<Neighbor>> staticNeighbors;
102 
103     /** @brief Updates the interface information based on new InterfaceInfo */
104     void updateInfo(const system::InterfaceInfo& info);
105 
106     /** @brief Function used to load the ntpservers
107      */
108     void loadNTPServers(const config::Parser& config);
109 
110     /** @brief Function used to load the nameservers.
111      */
112     void loadNameServers(const config::Parser& config);
113 
114     /** @brief Function to create ipAddress dbus object.
115      *  @param[in] addressType - Type of ip address.
116      *  @param[in] ipAddress- IP address.
117      *  @param[in] prefixLength - Length of prefix.
118      */
119 
120     ObjectPath ip(IP::Protocol addressType, std::string ipAddress,
121                   uint8_t prefixLength, std::string) override;
122 
123     /** @brief Function to create static neighbor dbus object.
124      *  @param[in] ipAddress - IP address.
125      *  @param[in] macAddress - Low level MAC address.
126      */
127     ObjectPath neighbor(std::string ipAddress, std::string macAddress) override;
128 
129     /* @brief creates the dbus object(IPaddres) given in the address list.
130      * @param[in] addrs - address list for which dbus objects needs
131      *                    to create.
132      */
133     void createIPAddressObjects();
134 
135     /* @brief creates the dbus object(Neighbor) given in the neighbor list.
136      */
137     void createStaticNeighborObjects();
138 
139     /** Set value of DHCPEnabled */
140     DHCPConf dhcpEnabled() const override;
141     DHCPConf dhcpEnabled(DHCPConf value) override;
142     using EthernetInterfaceIntf::dhcp4;
143     bool dhcp4(bool value) override;
144     using EthernetInterfaceIntf::dhcp6;
145     bool dhcp6(bool value) override;
146 
147     inline bool dhcpIsEnabled(in_addr) const
148     {
149         return dhcp4();
150     }
151     inline bool dhcpIsEnabled(in6_addr) const
152     {
153         return dhcp6();
154     }
155     inline bool dhcpIsEnabled(InAddrAny addr) const
156     {
157         return std::visit([&](auto v) { return dhcpIsEnabled(v); }, addr);
158     }
159 
160     /** Retrieve Link State */
161     bool linkUp() const override;
162 
163     /** Retrieve MTU Size */
164     size_t mtu() const override;
165 
166     /** Set size of MTU */
167     size_t mtu(size_t value) override;
168 
169     /** Set value of NICEnabled */
170     bool nicEnabled(bool value) override;
171 
172     /** @brief sets the MAC address.
173      *  @param[in] value - MAC address which needs to be set on the system.
174      *  @returns macAddress of the interface or throws an error.
175      */
176     std::string macAddress(std::string value) override;
177 
178     /** @brief check conf file for Router Advertisements
179      *
180      */
181     bool ipv6AcceptRA(bool value) override;
182     using EthernetInterfaceIntf::ipv6AcceptRA;
183 
184     /** @brief sets the NTP servers.
185      *  @param[in] value - vector of NTP servers.
186      */
187     ServerList ntpServers(ServerList value) override;
188 
189     /** @brief sets the static NTP servers.
190      *  @param[in] value - vector of NTP servers.
191      */
192     ServerList staticNTPServers(ServerList value) override;
193 
194     /** @brief sets the Static DNS/nameservers.
195      *  @param[in] value - vector of DNS servers.
196      */
197 
198     ServerList staticNameServers(ServerList value) override;
199 
200     /** @brief create Vlan interface.
201      *  @param[in] id- VLAN identifier.
202      */
203     ObjectPath createVLAN(uint16_t id);
204 
205     /** @brief write the network conf file with the in-memory objects.
206      */
207     void writeConfigurationFile();
208 
209     /** @brief delete all dbus objects.
210      */
211     void deleteAll();
212 
213     /** @brief set the default v4 gateway of the interface.
214      *  @param[in] gateway - default v4 gateway of the interface.
215      */
216     std::string defaultGateway(std::string gateway) override;
217 
218     /** @brief set the default v6 gateway of the interface.
219      *  @param[in] gateway - default v6 gateway of the interface.
220      */
221     std::string defaultGateway6(std::string gateway) override;
222 
223     using EthernetInterfaceIntf::interfaceName;
224     using EthernetInterfaceIntf::linkUp;
225     using EthernetInterfaceIntf::mtu;
226     using EthernetInterfaceIntf::nicEnabled;
227     using MacAddressIntf::macAddress;
228 
229     using EthernetInterfaceIntf::defaultGateway;
230     using EthernetInterfaceIntf::defaultGateway6;
231 
232   protected:
233     /** @brief get the NTP server list from the timsyncd dbus obj
234      *
235      */
236     virtual ServerList getNTPServerFromTimeSyncd();
237 
238     /** @brief get the name server details from the network conf
239      *
240      */
241     virtual ServerList getNameServerFromResolvd();
242 
243     /** @brief Persistent sdbusplus DBus bus connection. */
244     sdbusplus::bus_t& bus;
245 
246     /** @brief Dbus object path */
247     std::string objPath;
248 
249     /** @brief Interface index */
250     unsigned ifIdx;
251 
252     struct VlanProperties : VlanIfaces
253     {
254         VlanProperties(sdbusplus::bus_t& bus, stdplus::const_zstring objPath,
255                        const system::InterfaceInfo& info,
256                        EthernetInterface& eth, bool emitSignal = true);
257         void delete_() override;
258         unsigned parentIdx;
259         EthernetInterface& eth;
260     };
261     std::optional<VlanProperties> vlan;
262 
263     friend class TestEthernetInterface;
264     friend class TestNetworkManager;
265 
266   private:
267     EthernetInterface(sdbusplus::bus_t& bus, Manager& manager,
268                       const system::InterfaceInfo& info, std::string&& objPath,
269                       const config::Parser& config, bool emitSignal,
270                       std::optional<bool> enabled);
271 
272     /** @brief Determines if the address is manually assigned
273      *  @param[in] origin - The origin entry of the IP::Address
274      *  @returns true/false value if the address is static
275      */
276     bool originIsManuallyAssigned(IP::AddressOrigin origin);
277 
278     /** @brief Determines if the NIC is enabled in systemd
279      *  @returns true/false value if the NIC is enabled
280      */
281     bool queryNicEnabled() const;
282 };
283 
284 } // namespace network
285 } // namespace phosphor
286