blob: 9a8cba8725f000ac592d08aeff039d27142c3c57 [file] [log] [blame]
William A. Kennington III2e09d272022-10-14 17:15:00 -07001#include "system_queries.hpp"
2
William A. Kennington III6d217512022-11-17 16:18:01 -08003#include "netlink.hpp"
4
William A. Kennington III2e09d272022-10-14 17:15:00 -07005#include <fmt/format.h>
William A. Kennington III2e09d272022-10-14 17:15:00 -07006#include <linux/ethtool.h>
William A. Kennington III6d217512022-11-17 16:18:01 -08007#include <linux/rtnetlink.h>
William A. Kennington III2e09d272022-10-14 17:15:00 -07008#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>
William A. Kennington III2e09d272022-10-14 17:15:00 -070016#include <stdplus/util/cexec.hpp>
17#include <string_view>
William A. Kennington III2e09d272022-10-14 17:15:00 -070018
19namespace phosphor::network::system
20{
21
22using std::literals::string_view_literals::operator""sv;
23using phosphor::logging::entry;
24using phosphor::logging::level;
25using phosphor::logging::log;
26
27static stdplus::Fd& getIFSock()
28{
29 using namespace stdplus::fd;
30 static auto fd =
31 socket(SocketDomain::INet, SocketType::Datagram, SocketProto::IP);
32 return fd;
33}
34
35static ifreq makeIFReq(std::string_view ifname)
36{
37 ifreq ifr = {};
38 const auto copied = std::min<std::size_t>(ifname.size(), IFNAMSIZ - 1);
39 std::copy_n(ifname.begin(), copied, ifr.ifr_name);
40 return ifr;
41}
42
43static ifreq executeIFReq(std::string_view ifname, unsigned long cmd,
44 void* data = nullptr)
45{
46 ifreq ifr = makeIFReq(ifname);
47 ifr.ifr_data = reinterpret_cast<char*>(data);
48 getIFSock().ioctl(cmd, &ifr);
49 return ifr;
50}
51
52inline auto optionalIFReq(stdplus::zstring_view ifname, unsigned long long cmd,
53 std::string_view cmdname, auto&& complete,
54 void* data = nullptr)
55{
56 ifreq ifr;
57 std::optional<decltype(complete(ifr))> ret;
William A. Kennington III3e471c52022-10-27 19:46:07 -070058 auto ukey = std::make_tuple(std::string(ifname), cmd);
59 static std::unordered_set<std::tuple<std::string, unsigned long long>>
60 unsupported;
William A. Kennington III2e09d272022-10-14 17:15:00 -070061 try
62 {
63 ifr = executeIFReq(ifname, cmd, data);
64 }
65 catch (const std::system_error& e)
66 {
67 if (e.code() == std::errc::operation_not_supported)
68 {
William A. Kennington III3e471c52022-10-27 19:46:07 -070069 if (unsupported.find(ukey) == unsupported.end())
70 {
71 unsupported.emplace(std::move(ukey));
72 auto msg =
73 fmt::format("{} not supported on {}", cmdname, ifname);
74 log<level::INFO>(msg.c_str(),
75 entry("INTERFACE=%s", ifname.c_str()));
76 }
William A. Kennington III2e09d272022-10-14 17:15:00 -070077 return ret;
78 }
79 throw;
80 }
William A. Kennington III3e471c52022-10-27 19:46:07 -070081 unsupported.erase(ukey);
William A. Kennington III2e09d272022-10-14 17:15:00 -070082 ret.emplace(complete(ifr));
83 return ret;
84}
85
86EthInfo getEthInfo(stdplus::zstring_view ifname)
87{
88 ethtool_cmd edata = {};
89 edata.cmd = ETHTOOL_GSET;
90 return optionalIFReq(
91 ifname, SIOCETHTOOL, "ETHTOOL"sv,
92 [&](const ifreq&) {
93 return EthInfo{.autoneg = edata.autoneg != 0,
94 .speed = edata.speed};
95 },
96 &edata)
97 .value_or(EthInfo{});
98}
99
William A. Kennington III2e09d272022-10-14 17:15:00 -0700100void setMTU(std::string_view ifname, unsigned mtu)
101{
102 auto ifr = makeIFReq(ifname);
103 ifr.ifr_mtu = mtu;
104 getIFSock().ioctl(SIOCSIFMTU, &ifr);
105}
106
107void setNICUp(std::string_view ifname, bool up)
108{
109 ifreq ifr = executeIFReq(ifname, SIOCGIFFLAGS);
110 ifr.ifr_flags &= ~IFF_UP;
111 ifr.ifr_flags |= up ? IFF_UP : 0;
112 getIFSock().ioctl(SIOCSIFFLAGS, &ifr);
113}
114
William A. Kennington III6d217512022-11-17 16:18:01 -0800115void deleteIntf(unsigned idx)
116{
117 if (idx == 0)
118 {
119 return;
120 }
121 ifinfomsg msg = {};
122 msg.ifi_family = AF_UNSPEC;
123 msg.ifi_index = idx;
124 netlink::performRequest(
125 NETLINK_ROUTE, RTM_DELLINK, NLM_F_REPLACE, msg,
126 [&](const nlmsghdr& hdr, std::string_view data) {
127 int err = 0;
128 if (hdr.nlmsg_type == NLMSG_ERROR)
129 {
130 err = netlink::extractRtData<nlmsgerr>(data).error;
131 }
132 throw std::runtime_error(
133 fmt::format("Failed to delete `{}`: {}", idx, strerror(err)));
134 });
135}
136
William A. Kennington III2e09d272022-10-14 17:15:00 -0700137} // namespace phosphor::network::system