William A. Kennington III | 2e09d27 | 2022-10-14 17:15:00 -0700 | [diff] [blame] | 1 | #include "system_queries.hpp" |
| 2 | |
William A. Kennington III | 6d21751 | 2022-11-17 16:18:01 -0800 | [diff] [blame] | 3 | #include "netlink.hpp" |
| 4 | |
William A. Kennington III | 2e09d27 | 2022-10-14 17:15:00 -0700 | [diff] [blame] | 5 | #include <fmt/format.h> |
William A. Kennington III | 2e09d27 | 2022-10-14 17:15:00 -0700 | [diff] [blame] | 6 | #include <linux/ethtool.h> |
William A. Kennington III | 6d21751 | 2022-11-17 16:18:01 -0800 | [diff] [blame] | 7 | #include <linux/rtnetlink.h> |
William A. Kennington III | 2e09d27 | 2022-10-14 17:15:00 -0700 | [diff] [blame] | 8 | #include <linux/sockios.h> |
| 9 | #include <net/if.h> |
| 10 | |
| 11 | #include <algorithm> |
| 12 | #include <optional> |
Jagpal Singh Gill | a2947b4 | 2023-04-17 21:10:14 -0700 | [diff] [blame] | 13 | #include <phosphor-logging/lg2.hpp> |
William A. Kennington III | 2e09d27 | 2022-10-14 17:15:00 -0700 | [diff] [blame] | 14 | #include <stdexcept> |
| 15 | #include <stdplus/fd/create.hpp> |
William A. Kennington III | 2e09d27 | 2022-10-14 17:15:00 -0700 | [diff] [blame] | 16 | #include <stdplus/util/cexec.hpp> |
| 17 | #include <string_view> |
William A. Kennington III | 2e09d27 | 2022-10-14 17:15:00 -0700 | [diff] [blame] | 18 | |
| 19 | namespace phosphor::network::system |
| 20 | { |
| 21 | |
| 22 | using std::literals::string_view_literals::operator""sv; |
William A. Kennington III | 2e09d27 | 2022-10-14 17:15:00 -0700 | [diff] [blame] | 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)); |
Jagpal Singh Gill | a2947b4 | 2023-04-17 21:10:14 -0700 | [diff] [blame] | 69 | lg2::info("{CMD_NAME} not supported on {INTERFACE_NAME}", |
| 70 | "CMD_NAME", cmdname, "INTERFACE_NAME", ifname); |
William A. Kennington III | 3e471c5 | 2022-10-27 19:46:07 -0700 | [diff] [blame] | 71 | } |
William A. Kennington III | 2e09d27 | 2022-10-14 17:15:00 -0700 | [diff] [blame] | 72 | return ret; |
| 73 | } |
| 74 | throw; |
| 75 | } |
William A. Kennington III | 3e471c5 | 2022-10-27 19:46:07 -0700 | [diff] [blame] | 76 | unsupported.erase(ukey); |
William A. Kennington III | 2e09d27 | 2022-10-14 17:15:00 -0700 | [diff] [blame] | 77 | ret.emplace(complete(ifr)); |
| 78 | return ret; |
| 79 | } |
| 80 | |
| 81 | EthInfo getEthInfo(stdplus::zstring_view ifname) |
| 82 | { |
| 83 | ethtool_cmd edata = {}; |
| 84 | edata.cmd = ETHTOOL_GSET; |
| 85 | return optionalIFReq( |
| 86 | ifname, SIOCETHTOOL, "ETHTOOL"sv, |
| 87 | [&](const ifreq&) { |
| 88 | return EthInfo{.autoneg = edata.autoneg != 0, |
| 89 | .speed = edata.speed}; |
| 90 | }, |
| 91 | &edata) |
| 92 | .value_or(EthInfo{}); |
| 93 | } |
| 94 | |
William A. Kennington III | 2e09d27 | 2022-10-14 17:15:00 -0700 | [diff] [blame] | 95 | void setMTU(std::string_view ifname, unsigned mtu) |
| 96 | { |
| 97 | auto ifr = makeIFReq(ifname); |
| 98 | ifr.ifr_mtu = mtu; |
| 99 | getIFSock().ioctl(SIOCSIFMTU, &ifr); |
| 100 | } |
| 101 | |
| 102 | void setNICUp(std::string_view ifname, bool up) |
| 103 | { |
| 104 | ifreq ifr = executeIFReq(ifname, SIOCGIFFLAGS); |
| 105 | ifr.ifr_flags &= ~IFF_UP; |
| 106 | ifr.ifr_flags |= up ? IFF_UP : 0; |
| 107 | getIFSock().ioctl(SIOCSIFFLAGS, &ifr); |
| 108 | } |
| 109 | |
William A. Kennington III | 6d21751 | 2022-11-17 16:18:01 -0800 | [diff] [blame] | 110 | void deleteIntf(unsigned idx) |
| 111 | { |
| 112 | if (idx == 0) |
| 113 | { |
| 114 | return; |
| 115 | } |
| 116 | ifinfomsg msg = {}; |
| 117 | msg.ifi_family = AF_UNSPEC; |
| 118 | msg.ifi_index = idx; |
| 119 | netlink::performRequest( |
| 120 | NETLINK_ROUTE, RTM_DELLINK, NLM_F_REPLACE, msg, |
| 121 | [&](const nlmsghdr& hdr, std::string_view data) { |
| 122 | int err = 0; |
| 123 | if (hdr.nlmsg_type == NLMSG_ERROR) |
| 124 | { |
| 125 | err = netlink::extractRtData<nlmsgerr>(data).error; |
| 126 | } |
| 127 | throw std::runtime_error( |
| 128 | fmt::format("Failed to delete `{}`: {}", idx, strerror(err))); |
| 129 | }); |
| 130 | } |
| 131 | |
William A. Kennington III | 2e09d27 | 2022-10-14 17:15:00 -0700 | [diff] [blame] | 132 | } // namespace phosphor::network::system |