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