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