blob: 0aa245ead9cd97828537fbc061412ae34d484c26 [file] [log] [blame]
William A. Kennington III2e09d272022-10-14 17:15:00 -07001#include "system_queries.hpp"
2
3#include "util.hpp"
4
5#include <fmt/format.h>
6#include <ifaddrs.h>
7#include <linux/ethtool.h>
8#include <linux/sockios.h>
9#include <net/if.h>
10
11#include <algorithm>
12#include <optional>
13#include <phosphor-logging/log.hpp>
14#include <stdexcept>
15#include <stdplus/fd/create.hpp>
16#include <stdplus/raw.hpp>
17#include <stdplus/util/cexec.hpp>
18#include <string_view>
19#include <system_error>
20
21namespace phosphor::network::system
22{
23
24using std::literals::string_view_literals::operator""sv;
25using phosphor::logging::entry;
26using phosphor::logging::level;
27using phosphor::logging::log;
28
29static stdplus::Fd& getIFSock()
30{
31 using namespace stdplus::fd;
32 static auto fd =
33 socket(SocketDomain::INet, SocketType::Datagram, SocketProto::IP);
34 return fd;
35}
36
37static ifreq makeIFReq(std::string_view ifname)
38{
39 ifreq ifr = {};
40 const auto copied = std::min<std::size_t>(ifname.size(), IFNAMSIZ - 1);
41 std::copy_n(ifname.begin(), copied, ifr.ifr_name);
42 return ifr;
43}
44
45static ifreq executeIFReq(std::string_view ifname, unsigned long cmd,
46 void* data = nullptr)
47{
48 ifreq ifr = makeIFReq(ifname);
49 ifr.ifr_data = reinterpret_cast<char*>(data);
50 getIFSock().ioctl(cmd, &ifr);
51 return ifr;
52}
53
54inline auto optionalIFReq(stdplus::zstring_view ifname, unsigned long long cmd,
55 std::string_view cmdname, auto&& complete,
56 void* data = nullptr)
57{
58 ifreq ifr;
59 std::optional<decltype(complete(ifr))> ret;
William A. Kennington III3e471c52022-10-27 19:46:07 -070060 auto ukey = std::make_tuple(std::string(ifname), cmd);
61 static std::unordered_set<std::tuple<std::string, unsigned long long>>
62 unsupported;
William A. Kennington III2e09d272022-10-14 17:15:00 -070063 try
64 {
65 ifr = executeIFReq(ifname, cmd, data);
66 }
67 catch (const std::system_error& e)
68 {
69 if (e.code() == std::errc::operation_not_supported)
70 {
William A. Kennington III3e471c52022-10-27 19:46:07 -070071 if (unsupported.find(ukey) == unsupported.end())
72 {
73 unsupported.emplace(std::move(ukey));
74 auto msg =
75 fmt::format("{} not supported on {}", cmdname, ifname);
76 log<level::INFO>(msg.c_str(),
77 entry("INTERFACE=%s", ifname.c_str()));
78 }
William A. Kennington III2e09d272022-10-14 17:15:00 -070079 return ret;
80 }
81 throw;
82 }
William A. Kennington III3e471c52022-10-27 19:46:07 -070083 unsupported.erase(ukey);
William A. Kennington III2e09d272022-10-14 17:15:00 -070084 ret.emplace(complete(ifr));
85 return ret;
86}
87
88EthInfo getEthInfo(stdplus::zstring_view ifname)
89{
90 ethtool_cmd edata = {};
91 edata.cmd = ETHTOOL_GSET;
92 return optionalIFReq(
93 ifname, SIOCETHTOOL, "ETHTOOL"sv,
94 [&](const ifreq&) {
95 return EthInfo{.autoneg = edata.autoneg != 0,
96 .speed = edata.speed};
97 },
98 &edata)
99 .value_or(EthInfo{});
100}
101
102bool intfIsRunning(std::string_view ifname)
103{
104 return executeIFReq(ifname, SIOCGIFFLAGS).ifr_flags & IFF_RUNNING;
105}
106
107unsigned intfIndex(stdplus::const_zstring ifname)
108{
109 unsigned idx = if_nametoindex(ifname.c_str());
110 if (idx == 0)
111 {
112 auto msg = fmt::format("if_nametoindex({})", ifname);
113 throw std::system_error(errno, std::generic_category(), msg);
114 }
115 return idx;
116}
117
118std::optional<ether_addr> getMAC(stdplus::zstring_view ifname)
119{
120 return optionalIFReq(
121 ifname, SIOCGIFHWADDR, "IFHWADDR", [](const ifreq& ifr) {
122 return stdplus::raw::refFrom<ether_addr>(ifr.ifr_hwaddr.sa_data);
123 });
124}
125
126std::optional<unsigned> getMTU(stdplus::zstring_view ifname)
127{
128 return optionalIFReq(ifname, SIOCGIFMTU, "GMTU",
129 [](const ifreq& ifr) { return ifr.ifr_mtu; });
130}
131
132void setMTU(std::string_view ifname, unsigned mtu)
133{
134 auto ifr = makeIFReq(ifname);
135 ifr.ifr_mtu = mtu;
136 getIFSock().ioctl(SIOCSIFMTU, &ifr);
137}
138
139void setNICUp(std::string_view ifname, bool up)
140{
141 ifreq ifr = executeIFReq(ifname, SIOCGIFFLAGS);
142 ifr.ifr_flags &= ~IFF_UP;
143 ifr.ifr_flags |= up ? IFF_UP : 0;
144 getIFSock().ioctl(SIOCSIFFLAGS, &ifr);
145}
146
147string_uset getInterfaces()
148{
149 string_uset ret;
150 struct ifaddrs* root;
151 CHECK_ERRNO(getifaddrs(&root), "getifaddrs");
152 const auto& ignored = internal::getIgnoredInterfaces();
153 for (auto it = root; it != nullptr; it = it->ifa_next)
154 {
155 if (!(it->ifa_flags & IFF_LOOPBACK) &&
156 ignored.find(it->ifa_name) == ignored.end())
157 {
158 ret.emplace(it->ifa_name);
159 }
160 }
161 freeifaddrs(root);
162 return ret;
163}
164
165} // namespace phosphor::network::system