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 | |
Jagpal Singh Gill | a2947b4 | 2023-04-17 21:10:14 -0700 | [diff] [blame] | 11 | #include <phosphor-logging/lg2.hpp> |
William A. Kennington III | 2e09d27 | 2022-10-14 17:15:00 -0700 | [diff] [blame] | 12 | #include <stdplus/fd/create.hpp> |
William A. Kennington III | 11b59a3 | 2023-06-02 15:56:33 -0700 | [diff] [blame] | 13 | #include <stdplus/hash/tuple.hpp> |
William A. Kennington III | 2e09d27 | 2022-10-14 17:15:00 -0700 | [diff] [blame] | 14 | #include <stdplus/util/cexec.hpp> |
Patrick Williams | 89d734b | 2023-05-10 07:50:25 -0500 | [diff] [blame] | 15 | |
| 16 | #include <algorithm> |
| 17 | #include <optional> |
| 18 | #include <stdexcept> |
William A. Kennington III | 2e09d27 | 2022-10-14 17:15:00 -0700 | [diff] [blame] | 19 | #include <string_view> |
William A. Kennington III | 2e09d27 | 2022-10-14 17:15:00 -0700 | [diff] [blame] | 20 | |
| 21 | namespace phosphor::network::system |
| 22 | { |
| 23 | |
| 24 | using std::literals::string_view_literals::operator""sv; |
William A. Kennington III | 2e09d27 | 2022-10-14 17:15:00 -0700 | [diff] [blame] | 25 | |
| 26 | static stdplus::Fd& getIFSock() |
| 27 | { |
| 28 | using namespace stdplus::fd; |
Patrick Williams | 89d734b | 2023-05-10 07:50:25 -0500 | [diff] [blame] | 29 | static auto fd = socket(SocketDomain::INet, SocketType::Datagram, |
| 30 | SocketProto::IP); |
William A. Kennington III | 2e09d27 | 2022-10-14 17:15:00 -0700 | [diff] [blame] | 31 | return fd; |
| 32 | } |
| 33 | |
| 34 | static ifreq makeIFReq(std::string_view ifname) |
| 35 | { |
| 36 | ifreq ifr = {}; |
| 37 | const auto copied = std::min<std::size_t>(ifname.size(), IFNAMSIZ - 1); |
| 38 | std::copy_n(ifname.begin(), copied, ifr.ifr_name); |
| 39 | return ifr; |
| 40 | } |
| 41 | |
| 42 | static ifreq executeIFReq(std::string_view ifname, unsigned long cmd, |
| 43 | void* data = nullptr) |
| 44 | { |
| 45 | ifreq ifr = makeIFReq(ifname); |
| 46 | ifr.ifr_data = reinterpret_cast<char*>(data); |
| 47 | getIFSock().ioctl(cmd, &ifr); |
| 48 | return ifr; |
| 49 | } |
| 50 | |
| 51 | inline auto optionalIFReq(stdplus::zstring_view ifname, unsigned long long cmd, |
| 52 | std::string_view cmdname, auto&& complete, |
| 53 | void* data = nullptr) |
| 54 | { |
| 55 | ifreq ifr; |
| 56 | std::optional<decltype(complete(ifr))> ret; |
William A. Kennington III | 3e471c5 | 2022-10-27 19:46:07 -0700 | [diff] [blame] | 57 | auto ukey = std::make_tuple(std::string(ifname), cmd); |
| 58 | static std::unordered_set<std::tuple<std::string, unsigned long long>> |
| 59 | unsupported; |
William A. Kennington III | 2e09d27 | 2022-10-14 17:15:00 -0700 | [diff] [blame] | 60 | try |
| 61 | { |
| 62 | ifr = executeIFReq(ifname, cmd, data); |
| 63 | } |
| 64 | catch (const std::system_error& e) |
| 65 | { |
| 66 | if (e.code() == std::errc::operation_not_supported) |
| 67 | { |
William A. Kennington III | 3e471c5 | 2022-10-27 19:46:07 -0700 | [diff] [blame] | 68 | if (unsupported.find(ukey) == unsupported.end()) |
| 69 | { |
| 70 | unsupported.emplace(std::move(ukey)); |
William A. Kennington III | 1d25ca4 | 2023-05-30 16:55:28 -0700 | [diff] [blame] | 71 | lg2::info("{NET_IFREQ} not supported on {NET_INTF}", |
| 72 | "NET_IFREQ", cmdname, "NET_INTF", ifname); |
William A. Kennington III | 3e471c5 | 2022-10-27 19:46:07 -0700 | [diff] [blame] | 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&) { |
Patrick Williams | 89d734b | 2023-05-10 07:50:25 -0500 | [diff] [blame] | 90 | return EthInfo{.autoneg = edata.autoneg != 0, .speed = edata.speed}; |
William A. Kennington III | 2e09d27 | 2022-10-14 17:15:00 -0700 | [diff] [blame] | 91 | }, |
| 92 | &edata) |
| 93 | .value_or(EthInfo{}); |
| 94 | } |
| 95 | |
William A. Kennington III | 2e09d27 | 2022-10-14 17:15:00 -0700 | [diff] [blame] | 96 | void setMTU(std::string_view ifname, unsigned mtu) |
| 97 | { |
| 98 | auto ifr = makeIFReq(ifname); |
| 99 | ifr.ifr_mtu = mtu; |
| 100 | getIFSock().ioctl(SIOCSIFMTU, &ifr); |
| 101 | } |
| 102 | |
| 103 | void setNICUp(std::string_view ifname, bool up) |
| 104 | { |
| 105 | ifreq ifr = executeIFReq(ifname, SIOCGIFFLAGS); |
| 106 | ifr.ifr_flags &= ~IFF_UP; |
| 107 | ifr.ifr_flags |= up ? IFF_UP : 0; |
| 108 | getIFSock().ioctl(SIOCSIFFLAGS, &ifr); |
| 109 | } |
| 110 | |
William A. Kennington III | 6d21751 | 2022-11-17 16:18:01 -0800 | [diff] [blame] | 111 | void deleteIntf(unsigned idx) |
| 112 | { |
| 113 | if (idx == 0) |
| 114 | { |
| 115 | return; |
| 116 | } |
| 117 | ifinfomsg msg = {}; |
| 118 | msg.ifi_family = AF_UNSPEC; |
| 119 | msg.ifi_index = idx; |
Patrick Williams | 89d734b | 2023-05-10 07:50:25 -0500 | [diff] [blame] | 120 | netlink::performRequest(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 | }); |
William A. Kennington III | 6d21751 | 2022-11-17 16:18:01 -0800 | [diff] [blame] | 130 | } |
| 131 | |
William A. Kennington III | 2e09d27 | 2022-10-14 17:15:00 -0700 | [diff] [blame] | 132 | } // namespace phosphor::network::system |