blob: 9a360bd5433c63fc8964caeb1ddb0beaa101dd4e [file] [log] [blame]
William A. Kennington III2e09d272022-10-14 17:15:00 -07001#include "system_queries.hpp"
2
William A. Kennington III2e09d272022-10-14 17:15:00 -07003#include <fmt/format.h>
William A. Kennington III2e09d272022-10-14 17:15:00 -07004#include <linux/ethtool.h>
5#include <linux/sockios.h>
6#include <net/if.h>
7
8#include <algorithm>
9#include <optional>
10#include <phosphor-logging/log.hpp>
11#include <stdexcept>
12#include <stdplus/fd/create.hpp>
William A. Kennington III2e09d272022-10-14 17:15:00 -070013#include <stdplus/util/cexec.hpp>
14#include <string_view>
William A. Kennington III2e09d272022-10-14 17:15:00 -070015
16namespace phosphor::network::system
17{
18
19using std::literals::string_view_literals::operator""sv;
20using phosphor::logging::entry;
21using phosphor::logging::level;
22using phosphor::logging::log;
23
24static stdplus::Fd& getIFSock()
25{
26 using namespace stdplus::fd;
27 static auto fd =
28 socket(SocketDomain::INet, SocketType::Datagram, SocketProto::IP);
29 return fd;
30}
31
32static ifreq makeIFReq(std::string_view ifname)
33{
34 ifreq ifr = {};
35 const auto copied = std::min<std::size_t>(ifname.size(), IFNAMSIZ - 1);
36 std::copy_n(ifname.begin(), copied, ifr.ifr_name);
37 return ifr;
38}
39
40static ifreq executeIFReq(std::string_view ifname, unsigned long cmd,
41 void* data = nullptr)
42{
43 ifreq ifr = makeIFReq(ifname);
44 ifr.ifr_data = reinterpret_cast<char*>(data);
45 getIFSock().ioctl(cmd, &ifr);
46 return ifr;
47}
48
49inline auto optionalIFReq(stdplus::zstring_view ifname, unsigned long long cmd,
50 std::string_view cmdname, auto&& complete,
51 void* data = nullptr)
52{
53 ifreq ifr;
54 std::optional<decltype(complete(ifr))> ret;
William A. Kennington III3e471c52022-10-27 19:46:07 -070055 auto ukey = std::make_tuple(std::string(ifname), cmd);
56 static std::unordered_set<std::tuple<std::string, unsigned long long>>
57 unsupported;
William A. Kennington III2e09d272022-10-14 17:15:00 -070058 try
59 {
60 ifr = executeIFReq(ifname, cmd, data);
61 }
62 catch (const std::system_error& e)
63 {
64 if (e.code() == std::errc::operation_not_supported)
65 {
William A. Kennington III3e471c52022-10-27 19:46:07 -070066 if (unsupported.find(ukey) == unsupported.end())
67 {
68 unsupported.emplace(std::move(ukey));
69 auto msg =
70 fmt::format("{} not supported on {}", cmdname, ifname);
71 log<level::INFO>(msg.c_str(),
72 entry("INTERFACE=%s", ifname.c_str()));
73 }
William A. Kennington III2e09d272022-10-14 17:15:00 -070074 return ret;
75 }
76 throw;
77 }
William A. Kennington III3e471c52022-10-27 19:46:07 -070078 unsupported.erase(ukey);
William A. Kennington III2e09d272022-10-14 17:15:00 -070079 ret.emplace(complete(ifr));
80 return ret;
81}
82
83EthInfo getEthInfo(stdplus::zstring_view ifname)
84{
85 ethtool_cmd edata = {};
86 edata.cmd = ETHTOOL_GSET;
87 return optionalIFReq(
88 ifname, SIOCETHTOOL, "ETHTOOL"sv,
89 [&](const ifreq&) {
90 return EthInfo{.autoneg = edata.autoneg != 0,
91 .speed = edata.speed};
92 },
93 &edata)
94 .value_or(EthInfo{});
95}
96
William A. Kennington III2e09d272022-10-14 17:15:00 -070097void setMTU(std::string_view ifname, unsigned mtu)
98{
99 auto ifr = makeIFReq(ifname);
100 ifr.ifr_mtu = mtu;
101 getIFSock().ioctl(SIOCSIFMTU, &ifr);
102}
103
104void setNICUp(std::string_view ifname, bool up)
105{
106 ifreq ifr = executeIFReq(ifname, SIOCGIFFLAGS);
107 ifr.ifr_flags &= ~IFF_UP;
108 ifr.ifr_flags |= up ? IFF_UP : 0;
109 getIFSock().ioctl(SIOCSIFFLAGS, &ifr);
110}
111
William A. Kennington III2e09d272022-10-14 17:15:00 -0700112} // namespace phosphor::network::system