blob: 5c6358b247ca44ca4236f43d3410781e98552e76 [file] [log] [blame]
Ratan Gupta05eb1092017-04-14 16:33:53 +05301#pragma once
2
3#include "ethernet_interface.hpp"
Ratan Guptaef85eb92017-06-15 08:57:54 +05304#include "system_configuration.hpp"
Ratan Gupta82549cc2017-04-21 08:45:23 +05305#include "types.hpp"
Ratan Gupta05eb1092017-04-14 16:33:53 +05306#include "xyz/openbmc_project/Network/VLAN/Create/server.hpp"
Michael Tritz29f2fd62017-05-22 15:27:26 -05007#include <xyz/openbmc_project/Common/FactoryReset/server.hpp>
Ratan Gupta05eb1092017-04-14 16:33:53 +05308
9#include <sdbusplus/bus.hpp>
Ratan Gupta6811f822017-04-14 16:34:56 +053010#include <ifaddrs.h>
Ratan Gupta05eb1092017-04-14 16:33:53 +053011
12#include <list>
Ratan Gupta6811f822017-04-14 16:34:56 +053013#include <memory>
Ratan Gupta05eb1092017-04-14 16:33:53 +053014#include <string>
15#include <vector>
Ratan Guptaef85eb92017-06-15 08:57:54 +053016#include <experimental/filesystem>
Ratan Gupta05eb1092017-04-14 16:33:53 +053017
18namespace phosphor
19{
20namespace network
21{
22
Ratan Guptaef85eb92017-06-15 08:57:54 +053023namespace fs = std::experimental::filesystem;
Ratan Gupta05eb1092017-04-14 16:33:53 +053024namespace details
25{
26
Michael Tritz29f2fd62017-05-22 15:27:26 -050027template <typename T, typename U>
28using ServerObject = typename sdbusplus::server::object::object<T, U>;
Ratan Gupta05eb1092017-04-14 16:33:53 +053029
Michael Tritz29f2fd62017-05-22 15:27:26 -050030using VLANCreateIface = details::ServerObject<
31 sdbusplus::xyz::openbmc_project::Network::VLAN::server::Create,
32 sdbusplus::xyz::openbmc_project::Common::server::FactoryReset>;
Ratan Gupta05eb1092017-04-14 16:33:53 +053033
34using IntfName = std::string;
35
Ratan Gupta738a67f2017-04-21 10:38:05 +053036struct AddrInfo
37{
Ratan Gupta05eb1092017-04-14 16:33:53 +053038 short addrType;
39 std::string ipaddress;
40};
41
Ratan Gupta6811f822017-04-14 16:34:56 +053042using Addr_t = ifaddrs*;
43
44struct AddrDeleter
45{
46 void operator()(Addr_t ptr) const
47 {
48 freeifaddrs(ptr);
49 }
50};
51
52using AddrPtr = std::unique_ptr<ifaddrs, AddrDeleter>;
53
Ratan Gupta05eb1092017-04-14 16:33:53 +053054using AddrList = std::list<AddrInfo>;
55using IntfAddrMap = std::map<IntfName, AddrList>;
56
57} // namespace details
58
Ratan Gupta8ab17922017-05-25 13:07:05 +053059class TestNetworkManager; //forward declaration
60
Ratan Gupta05eb1092017-04-14 16:33:53 +053061/** @class Manager
62 * @brief OpenBMC network manager implementation.
63 */
64class Manager : public details::VLANCreateIface
65{
66 public:
67 Manager() = delete;
68 Manager(const Manager&) = delete;
69 Manager& operator=(const Manager&) = delete;
70 Manager(Manager&&) = delete;
71 Manager& operator=(Manager&&) = delete;
72 virtual ~Manager() = default;
73
74 /** @brief Constructor to put object onto bus at a dbus path.
75 * @param[in] bus - Bus to attach to.
76 * @param[in] objPath - Path to attach at.
77 */
78 Manager(sdbusplus::bus::bus& bus, const char* objPath);
79
Ratan Gupta82549cc2017-04-21 08:45:23 +053080 void vLAN(IntfName interfaceName, uint16_t id) override;
Ratan Gupta05eb1092017-04-14 16:33:53 +053081
Ratan Gupta4f1c18b2017-05-25 12:59:35 +053082 /** @brief write the network conf file with the in-memory objects.
83 */
84 void writeToConfigurationFile();
85
Ratan Gupta29b0e432017-05-25 12:51:40 +053086 /** @brief Fetch the interface and the ipaddress details
87 * from the system and create the ethernet interraces
88 * dbus object.
89 */
90 void createInterfaces();
91
Ratan Guptaef85eb92017-06-15 08:57:54 +053092 /** @brief create child interface object and the system conf object.
Ratan Guptafc2c7242017-05-29 08:46:06 +053093 */
Ratan Guptaef85eb92017-06-15 08:57:54 +053094 void createChildObjects();
95
96 /** @brief sets the network conf directory.
97 * @param[in] dirName - Absolute path of the directory.
98 */
99 void setConfDir(const fs::path& dir);
100
Ratan Gupta05eb1092017-04-14 16:33:53 +0530101 private:
102 /** @brief Get all the interfaces from the system.
103 * @returns list of interface names.
104 */
Ratan Gupta82549cc2017-04-21 08:45:23 +0530105 IntfAddrMap getInterfaceAddrs() const;
Ratan Gupta05eb1092017-04-14 16:33:53 +0530106
Ratan Gupta4f1c18b2017-05-25 12:59:35 +0530107 /** @brief Restart the systemd networkd
108 */
109 void restartSystemdNetworkd();
110
Ratan Gupta29b0e432017-05-25 12:51:40 +0530111 /** @brief Persistent sdbusplus DBus bus connection. */
112 sdbusplus::bus::bus& bus;
113
Ratan Gupta05eb1092017-04-14 16:33:53 +0530114 /** @brief Persistent map of EthernetInterface dbus objects and their names */
Ratan Gupta82549cc2017-04-21 08:45:23 +0530115 std::map<IntfName, std::unique_ptr<EthernetInterface>> interfaces;
Ratan Gupta05eb1092017-04-14 16:33:53 +0530116
Michael Tritz29f2fd62017-05-22 15:27:26 -0500117 /** @brief BMC network reset - resets network configuration for BMC. */
118 void reset() override;
119
Ratan Gupta34f96d62017-06-15 09:16:22 +0530120 /** @brief read the DHCP value from the configuration file
121 * @param[in] intf - Interface name.
122 */
123 bool getDHCPValue(const std::string& intf);
124
Ratan Gupta29b0e432017-05-25 12:51:40 +0530125 /** @brief Path of Object. */
126 std::string objectPath;
127
Ratan Guptaef85eb92017-06-15 08:57:54 +0530128 /** @brief pointer to system conf object. */
129 std::unique_ptr<SystemConfiguration> systemConf = nullptr;
130
131 /** @brief Network Configuration directory. */
132 fs::path confDir;
133
Ratan Gupta8ab17922017-05-25 13:07:05 +0530134 friend class TestNetworkManager;
135
Ratan Gupta05eb1092017-04-14 16:33:53 +0530136};
137
138} // namespace network
139} // namespace phosphor