William A. Kennington III | 7d6fa42 | 2021-02-08 17:04:02 -0800 | [diff] [blame] | 1 | #pragma once |
| 2 | |
| 3 | #include "platforms/nemora/portable/net_types.h" |
| 4 | |
| 5 | #include <net_iface.h> |
| 6 | |
| 7 | #include <sdbusplus/bus.hpp> |
| 8 | |
| 9 | #include <experimental/optional> |
| 10 | #include <list> |
| 11 | #include <map> |
| 12 | #include <optional> |
| 13 | #include <string> |
| 14 | #include <vector> |
| 15 | |
| 16 | // The API for configuring and querying network. |
| 17 | |
| 18 | namespace net |
| 19 | { |
| 20 | |
| 21 | using DBusObjectPath = std::string; |
| 22 | using DBusService = std::string; |
| 23 | using DBusInterface = std::string; |
| 24 | using ObjectTree = |
| 25 | std::map<DBusObjectPath, std::map<DBusService, std::vector<DBusInterface>>>; |
| 26 | |
| 27 | class ConfigBase |
| 28 | { |
| 29 | public: |
| 30 | virtual ~ConfigBase() = default; |
| 31 | |
| 32 | virtual int get_mac_addr(mac_addr_t* mac) = 0; |
| 33 | |
| 34 | virtual int set_mac_addr(const mac_addr_t& mac) = 0; |
| 35 | |
| 36 | // Called each time is_nic_hostless state is sampled. |
| 37 | virtual int set_nic_hostless(bool is_nic_hostless) = 0; |
| 38 | }; |
| 39 | |
| 40 | // Calls phosphord-networkd |
| 41 | class PhosphorConfig : public ConfigBase |
| 42 | { |
| 43 | public: |
| 44 | explicit PhosphorConfig(const std::string& iface_name); |
| 45 | |
| 46 | // Reads the MAC address from phosphor-networkd interface or internal |
| 47 | // cache, and store in the mac pointer. |
| 48 | // Returns -1 if failed, 0 if succeeded. |
| 49 | int get_mac_addr(mac_addr_t* mac) override; |
| 50 | |
| 51 | // Sets the MAC address over phosphor-networkd, and update internal |
| 52 | // cache. |
| 53 | // Returns -1 if failed, 0 if succeeded. |
| 54 | int set_mac_addr(const mac_addr_t& mac) override; |
| 55 | |
| 56 | virtual int set_nic_hostless(bool is_nic_hostless) override; |
| 57 | |
| 58 | private: |
| 59 | sdbusplus::message::message new_networkd_call(sdbusplus::bus::bus* dbus, |
| 60 | bool get = false) const; |
| 61 | |
| 62 | const std::string iface_name_; |
| 63 | const std::string iface_path_; |
| 64 | |
| 65 | // Stores the currently configured nic state, if previously set |
| 66 | std::optional<bool> was_nic_hostless_; |
| 67 | |
| 68 | // The MAC address obtained from NIC. |
| 69 | // ncsid will commit this MAC address over DBus to phosphor-networkd |
| 70 | // and expect it to be persisted. If actual host MAC address changes or |
| 71 | // BMC MAC address is overwritten, a daemon reboot is needed to reset |
| 72 | // the MAC. |
| 73 | // Initialized to nullopt which evaluates to false. Once a value is |
| 74 | // set, bool() evaluates to true. |
| 75 | std::experimental::optional<mac_addr_t> shared_host_mac_; |
| 76 | |
| 77 | // List of outstanding pids for config jobs |
| 78 | std::list<pid_t> running_pids_; |
| 79 | |
| 80 | // Holds a reference to the bus for issuing commands to update network |
| 81 | // config |
| 82 | sdbusplus::bus::bus bus; |
| 83 | }; |
| 84 | |
| 85 | } // namespace net |