blob: f02d56813aa545ede839b1e13066d097dbc2c496 [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;
60 try
61 {
62 ifr = executeIFReq(ifname, cmd, data);
63 }
64 catch (const std::system_error& e)
65 {
66 if (e.code() == std::errc::operation_not_supported)
67 {
68 auto msg = fmt::format("{} not supported on {}", cmdname, ifname);
69 log<level::INFO>(msg.c_str(),
70 entry("INTERFACE=%s", ifname.c_str()));
71 return ret;
72 }
73 throw;
74 }
75 ret.emplace(complete(ifr));
76 return ret;
77}
78
79EthInfo getEthInfo(stdplus::zstring_view ifname)
80{
81 ethtool_cmd edata = {};
82 edata.cmd = ETHTOOL_GSET;
83 return optionalIFReq(
84 ifname, SIOCETHTOOL, "ETHTOOL"sv,
85 [&](const ifreq&) {
86 return EthInfo{.autoneg = edata.autoneg != 0,
87 .speed = edata.speed};
88 },
89 &edata)
90 .value_or(EthInfo{});
91}
92
93bool intfIsRunning(std::string_view ifname)
94{
95 return executeIFReq(ifname, SIOCGIFFLAGS).ifr_flags & IFF_RUNNING;
96}
97
98unsigned intfIndex(stdplus::const_zstring ifname)
99{
100 unsigned idx = if_nametoindex(ifname.c_str());
101 if (idx == 0)
102 {
103 auto msg = fmt::format("if_nametoindex({})", ifname);
104 throw std::system_error(errno, std::generic_category(), msg);
105 }
106 return idx;
107}
108
109std::optional<ether_addr> getMAC(stdplus::zstring_view ifname)
110{
111 return optionalIFReq(
112 ifname, SIOCGIFHWADDR, "IFHWADDR", [](const ifreq& ifr) {
113 return stdplus::raw::refFrom<ether_addr>(ifr.ifr_hwaddr.sa_data);
114 });
115}
116
117std::optional<unsigned> getMTU(stdplus::zstring_view ifname)
118{
119 return optionalIFReq(ifname, SIOCGIFMTU, "GMTU",
120 [](const ifreq& ifr) { return ifr.ifr_mtu; });
121}
122
123void setMTU(std::string_view ifname, unsigned mtu)
124{
125 auto ifr = makeIFReq(ifname);
126 ifr.ifr_mtu = mtu;
127 getIFSock().ioctl(SIOCSIFMTU, &ifr);
128}
129
130void setNICUp(std::string_view ifname, bool up)
131{
132 ifreq ifr = executeIFReq(ifname, SIOCGIFFLAGS);
133 ifr.ifr_flags &= ~IFF_UP;
134 ifr.ifr_flags |= up ? IFF_UP : 0;
135 getIFSock().ioctl(SIOCSIFFLAGS, &ifr);
136}
137
138string_uset getInterfaces()
139{
140 string_uset ret;
141 struct ifaddrs* root;
142 CHECK_ERRNO(getifaddrs(&root), "getifaddrs");
143 const auto& ignored = internal::getIgnoredInterfaces();
144 for (auto it = root; it != nullptr; it = it->ifa_next)
145 {
146 if (!(it->ifa_flags & IFF_LOOPBACK) &&
147 ignored.find(it->ifa_name) == ignored.end())
148 {
149 ret.emplace(it->ifa_name);
150 }
151 }
152 freeifaddrs(root);
153 return ret;
154}
155
156} // namespace phosphor::network::system