blob: 02c9aff24d5e2b495684f17dee249813650efbba [file] [log] [blame]
Brandon Kimdab96f12021-02-18 11:21:37 -08001/*
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
William A. Kennington III7d6fa422021-02-08 17:04:02 -080017#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
34namespace net
35{
36
37using DBusObjectPath = std::string;
38using DBusService = std::string;
39using DBusInterface = std::string;
40using ObjectTree =
41 std::map<DBusObjectPath, std::map<DBusService, std::vector<DBusInterface>>>;
42
43class 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
Mo Elbadryd94dc252024-11-07 21:44:23 +000056// Calls Socket Ioctl to obtain information about NIC (previously
57// phosphor-networkd)
William A. Kennington III7d6fa422021-02-08 17:04:02 -080058class PhosphorConfig : public ConfigBase
59{
60 public:
61 explicit PhosphorConfig(const std::string& iface_name);
62
Mo Elbadryd94dc252024-11-07 21:44:23 +000063 // Reads the MAC address from socket interface or internal
William A. Kennington III7d6fa422021-02-08 17:04:02 -080064 // cache, and store in the mac pointer.
65 // Returns -1 if failed, 0 if succeeded.
66 int get_mac_addr(mac_addr_t* mac) override;
67
Mo Elbadryd94dc252024-11-07 21:44:23 +000068 // Sets the MAC address over socket, and update internal
William A. Kennington III7d6fa422021-02-08 17:04:02 -080069 // cache.
70 // Returns -1 if failed, 0 if succeeded.
71 int set_mac_addr(const mac_addr_t& mac) override;
William A. Kennington III7d6fa422021-02-08 17:04:02 -080072 virtual int set_nic_hostless(bool is_nic_hostless) override;
73
74 private:
Patrick Williams59ac2c22022-07-22 19:26:57 -050075 sdbusplus::message_t new_networkd_call(sdbusplus::bus_t* dbus,
76 bool get = false) const;
William A. Kennington III7d6fa422021-02-08 17:04:02 -080077
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
Mo Elbadryd94dc252024-11-07 21:44:23 +000084 // Function helper allows get_mac_addr and set_mac_addr to do
85 // ioctl calls to get and set different states of NIC.
86 void call_nic(auto fd, struct ifreq& ifr, int op);
87
William A. Kennington III7d6fa422021-02-08 17:04:02 -080088 // The MAC address obtained from NIC.
89 // ncsid will commit this MAC address over DBus to phosphor-networkd
90 // and expect it to be persisted. If actual host MAC address changes or
91 // BMC MAC address is overwritten, a daemon reboot is needed to reset
92 // the MAC.
93 // Initialized to nullopt which evaluates to false. Once a value is
94 // set, bool() evaluates to true.
95 std::experimental::optional<mac_addr_t> shared_host_mac_;
96
97 // List of outstanding pids for config jobs
98 std::list<pid_t> running_pids_;
99
100 // Holds a reference to the bus for issuing commands to update network
101 // config
Patrick Williams59ac2c22022-07-22 19:26:57 -0500102 sdbusplus::bus_t bus;
William A. Kennington III7d6fa422021-02-08 17:04:02 -0800103};
104
105} // namespace net