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