blob: 0152f4a7338a725599471bafb546afebc9d910f9 [file] [log] [blame]
William A. Kennington III2e09d272022-10-14 17:15:00 -07001#include "system_queries.hpp"
2
William A. Kennington IIIfd862be2022-10-09 18:40:55 -07003#include "netlink.hpp"
William A. Kennington III2e09d272022-10-14 17:15:00 -07004#include "util.hpp"
5
6#include <fmt/format.h>
William A. Kennington III2e09d272022-10-14 17:15:00 -07007#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
William A. Kennington III2e09d272022-10-14 17:15:00 -0700107std::optional<unsigned> getMTU(stdplus::zstring_view ifname)
108{
109 return optionalIFReq(ifname, SIOCGIFMTU, "GMTU",
110 [](const ifreq& ifr) { return ifr.ifr_mtu; });
111}
112
113void setMTU(std::string_view ifname, unsigned mtu)
114{
115 auto ifr = makeIFReq(ifname);
116 ifr.ifr_mtu = mtu;
117 getIFSock().ioctl(SIOCSIFMTU, &ifr);
118}
119
120void setNICUp(std::string_view ifname, bool up)
121{
122 ifreq ifr = executeIFReq(ifname, SIOCGIFFLAGS);
123 ifr.ifr_flags &= ~IFF_UP;
124 ifr.ifr_flags |= up ? IFF_UP : 0;
125 getIFSock().ioctl(SIOCSIFFLAGS, &ifr);
126}
127
William A. Kennington IIIfd862be2022-10-09 18:40:55 -0700128InterfaceInfo detail::parseInterface(const nlmsghdr& hdr, std::string_view msg)
William A. Kennington III2e09d272022-10-14 17:15:00 -0700129{
William A. Kennington IIIfd862be2022-10-09 18:40:55 -0700130 if (hdr.nlmsg_type != RTM_NEWLINK)
William A. Kennington III2e09d272022-10-14 17:15:00 -0700131 {
William A. Kennington IIIfd862be2022-10-09 18:40:55 -0700132 throw std::runtime_error("Not an interface msg");
133 }
William A. Kennington IIIf1289a02022-10-24 18:30:36 -0700134 const auto& ifinfo = netlink::extractRtData<ifinfomsg>(msg);
William A. Kennington IIIfd862be2022-10-09 18:40:55 -0700135 InterfaceInfo ret;
136 ret.flags = ifinfo.ifi_flags;
137 ret.idx = ifinfo.ifi_index;
138 while (!msg.empty())
139 {
140 auto [hdr, data] = netlink::extractRtAttr(msg);
141 if (hdr.rta_type == IFLA_IFNAME)
William A. Kennington III2e09d272022-10-14 17:15:00 -0700142 {
William A. Kennington IIIfd862be2022-10-09 18:40:55 -0700143 ret.name.emplace(data.begin(), data.end() - 1);
144 }
145 else if (hdr.rta_type == IFLA_ADDRESS)
146 {
147 if (data.size() != sizeof(ether_addr))
148 {
149 // Some interfaces have IP addresses for their LLADDR
150 continue;
151 }
152 ret.mac.emplace(stdplus::raw::copyFrom<ether_addr>(data));
153 }
154 else if (hdr.rta_type == IFLA_MTU)
155 {
156 ret.mtu.emplace(stdplus::raw::copyFrom<unsigned>(data));
William A. Kennington III2e09d272022-10-14 17:15:00 -0700157 }
158 }
William A. Kennington IIIfd862be2022-10-09 18:40:55 -0700159 return ret;
160}
161
162bool detail::validateNewInterface(const InterfaceInfo& info)
163{
164 if (info.flags & IFF_LOOPBACK)
165 {
166 return false;
167 }
168 if (!info.name)
169 {
170 throw std::invalid_argument("Interface Dump missing name");
171 }
172 const auto& ignored = internal::getIgnoredInterfaces();
173 if (ignored.find(*info.name) != ignored.end())
174 {
175 return false;
176 }
177 return true;
178}
179
180std::vector<InterfaceInfo> getInterfaces()
181{
182 std::vector<InterfaceInfo> ret;
183 auto cb = [&](const nlmsghdr& hdr, std::string_view msg) {
184 auto info = detail::parseInterface(hdr, msg);
185 if (detail::validateNewInterface(info))
186 {
187 ret.emplace_back(std::move(info));
188 }
189 };
190 ifinfomsg msg{};
191 netlink::performRequest(NETLINK_ROUTE, RTM_GETLINK, NLM_F_DUMP, msg, cb);
William A. Kennington III2e09d272022-10-14 17:15:00 -0700192 return ret;
193}
194
195} // namespace phosphor::network::system