1 /*
2  * Copyright 2021 Google LLC
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #pragma once
18 
19 #include "platforms/nemora/portable/net_types.h"
20 
21 #include <net_iface.h>
22 
23 #include <sdbusplus/bus.hpp>
24 
25 #include <experimental/optional>
26 #include <list>
27 #include <map>
28 #include <optional>
29 #include <string>
30 #include <vector>
31 
32 // The API for configuring and querying network.
33 
34 namespace net
35 {
36 
37 using DBusObjectPath = std::string;
38 using DBusService = std::string;
39 using DBusInterface = std::string;
40 using ObjectTree =
41     std::map<DBusObjectPath, std::map<DBusService, std::vector<DBusInterface>>>;
42 
43 class ConfigBase
44 {
45   public:
46     virtual ~ConfigBase() = default;
47 
48     virtual int get_mac_addr(mac_addr_t* mac) = 0;
49 
50     virtual int set_mac_addr(const mac_addr_t& mac) = 0;
51 
52     // Called each time is_nic_hostless state is sampled.
53     virtual int set_nic_hostless(bool is_nic_hostless) = 0;
54 };
55 
56 // Calls phosphord-networkd
57 class PhosphorConfig : public ConfigBase
58 {
59   public:
60     explicit PhosphorConfig(const std::string& iface_name);
61 
62     // Reads the MAC address from phosphor-networkd interface or internal
63     // cache, and store in the mac pointer.
64     // Returns -1 if failed, 0 if succeeded.
65     int get_mac_addr(mac_addr_t* mac) override;
66 
67     // Sets the MAC address over phosphor-networkd, and update internal
68     // cache.
69     // Returns -1 if failed, 0 if succeeded.
70     int set_mac_addr(const mac_addr_t& mac) override;
71 
72     virtual int set_nic_hostless(bool is_nic_hostless) override;
73 
74   private:
75     sdbusplus::message::message new_networkd_call(sdbusplus::bus::bus* dbus,
76                                                   bool get = false) const;
77 
78     const std::string iface_name_;
79     const std::string iface_path_;
80 
81     // Stores the currently configured nic state, if previously set
82     std::optional<bool> was_nic_hostless_;
83 
84     // The MAC address obtained from NIC.
85     // ncsid will commit this MAC address over DBus to phosphor-networkd
86     // and expect it to be persisted. If actual host MAC address changes or
87     // BMC MAC address is overwritten, a daemon reboot is needed to reset
88     // the MAC.
89     //   Initialized to nullopt which evaluates to false. Once a value is
90     // set, bool() evaluates to true.
91     std::experimental::optional<mac_addr_t> shared_host_mac_;
92 
93     // List of outstanding pids for config jobs
94     std::list<pid_t> running_pids_;
95 
96     // Holds a reference to the bus for issuing commands to update network
97     // config
98     sdbusplus::bus::bus bus;
99 };
100 
101 } // namespace net
102