William A. Kennington III | 2e09d27 | 2022-10-14 17:15:00 -0700 | [diff] [blame] | 1 | #include "system_queries.hpp" |
| 2 | |
William A. Kennington III | 2e09d27 | 2022-10-14 17:15:00 -0700 | [diff] [blame] | 3 | #include <fmt/format.h> |
William A. Kennington III | 2e09d27 | 2022-10-14 17:15:00 -0700 | [diff] [blame] | 4 | #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 III | 2e09d27 | 2022-10-14 17:15:00 -0700 | [diff] [blame] | 13 | #include <stdplus/util/cexec.hpp> |
| 14 | #include <string_view> |
William A. Kennington III | 2e09d27 | 2022-10-14 17:15:00 -0700 | [diff] [blame] | 15 | |
| 16 | namespace phosphor::network::system |
| 17 | { |
| 18 | |
| 19 | using std::literals::string_view_literals::operator""sv; |
| 20 | using phosphor::logging::entry; |
| 21 | using phosphor::logging::level; |
| 22 | using phosphor::logging::log; |
| 23 | |
| 24 | static 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 | |
| 32 | static 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 | |
| 40 | static 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 | |
| 49 | inline 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 III | 3e471c5 | 2022-10-27 19:46:07 -0700 | [diff] [blame] | 55 | 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 III | 2e09d27 | 2022-10-14 17:15:00 -0700 | [diff] [blame] | 58 | 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 III | 3e471c5 | 2022-10-27 19:46:07 -0700 | [diff] [blame] | 66 | 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 III | 2e09d27 | 2022-10-14 17:15:00 -0700 | [diff] [blame] | 74 | return ret; |
| 75 | } |
| 76 | throw; |
| 77 | } |
William A. Kennington III | 3e471c5 | 2022-10-27 19:46:07 -0700 | [diff] [blame] | 78 | unsupported.erase(ukey); |
William A. Kennington III | 2e09d27 | 2022-10-14 17:15:00 -0700 | [diff] [blame] | 79 | ret.emplace(complete(ifr)); |
| 80 | return ret; |
| 81 | } |
| 82 | |
| 83 | EthInfo 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 III | 2e09d27 | 2022-10-14 17:15:00 -0700 | [diff] [blame] | 97 | void 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 | |
| 104 | void 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 III | 2e09d27 | 2022-10-14 17:15:00 -0700 | [diff] [blame] | 112 | } // namespace phosphor::network::system |