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 <linux/ethtool.h> |
William A. Kennington III | 6d21751 | 2022-11-17 16:18:01 -0800 | [diff] [blame] | 6 | #include <linux/rtnetlink.h> |
William A. Kennington III | 2e09d27 | 2022-10-14 17:15:00 -0700 | [diff] [blame] | 7 | #include <linux/sockios.h> |
| 8 | #include <net/if.h> |
| 9 | |
Jagpal Singh Gill | a2947b4 | 2023-04-17 21:10:14 -0700 | [diff] [blame] | 10 | #include <phosphor-logging/lg2.hpp> |
William A. Kennington III | 2e09d27 | 2022-10-14 17:15:00 -0700 | [diff] [blame] | 11 | #include <stdplus/fd/create.hpp> |
William A. Kennington III | 11b59a3 | 2023-06-02 15:56:33 -0700 | [diff] [blame] | 12 | #include <stdplus/hash/tuple.hpp> |
William A. Kennington III | 2e09d27 | 2022-10-14 17:15:00 -0700 | [diff] [blame] | 13 | #include <stdplus/util/cexec.hpp> |
Patrick Williams | 89d734b | 2023-05-10 07:50:25 -0500 | [diff] [blame] | 14 | |
| 15 | #include <algorithm> |
William A. Kennington III | cafc151 | 2023-07-25 02:22:32 -0700 | [diff] [blame] | 16 | #include <format> |
Patrick Williams | 89d734b | 2023-05-10 07:50:25 -0500 | [diff] [blame] | 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 | 9b2a20d | 2023-06-17 14:05:48 -0700 | [diff] [blame] | 20 | #include <unordered_set> |
William A. Kennington III | 2e09d27 | 2022-10-14 17:15:00 -0700 | [diff] [blame] | 21 | |
| 22 | namespace phosphor::network::system |
| 23 | { |
| 24 | |
| 25 | using std::literals::string_view_literals::operator""sv; |
William A. Kennington III | 2e09d27 | 2022-10-14 17:15:00 -0700 | [diff] [blame] | 26 | |
| 27 | static stdplus::Fd& getIFSock() |
| 28 | { |
| 29 | using namespace stdplus::fd; |
Patrick Williams | ad20502 | 2024-08-16 15:20:07 -0400 | [diff] [blame^] | 30 | static auto fd = |
| 31 | socket(SocketDomain::INet, SocketType::Datagram, SocketProto::IP); |
William A. Kennington III | 2e09d27 | 2022-10-14 17:15:00 -0700 | [diff] [blame] | 32 | return fd; |
| 33 | } |
| 34 | |
| 35 | static 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 | |
| 43 | static 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 | |
| 52 | inline 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 III | 3e471c5 | 2022-10-27 19:46:07 -0700 | [diff] [blame] | 58 | 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 III | 2e09d27 | 2022-10-14 17:15:00 -0700 | [diff] [blame] | 61 | 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 III | 3e471c5 | 2022-10-27 19:46:07 -0700 | [diff] [blame] | 69 | if (unsupported.find(ukey) == unsupported.end()) |
| 70 | { |
| 71 | unsupported.emplace(std::move(ukey)); |
William A. Kennington III | 1d25ca4 | 2023-05-30 16:55:28 -0700 | [diff] [blame] | 72 | lg2::info("{NET_IFREQ} not supported on {NET_INTF}", |
| 73 | "NET_IFREQ", cmdname, "NET_INTF", ifname); |
William A. Kennington III | 3e471c5 | 2022-10-27 19:46:07 -0700 | [diff] [blame] | 74 | } |
William A. Kennington III | 2e09d27 | 2022-10-14 17:15:00 -0700 | [diff] [blame] | 75 | return ret; |
| 76 | } |
| 77 | throw; |
| 78 | } |
William A. Kennington III | 3e471c5 | 2022-10-27 19:46:07 -0700 | [diff] [blame] | 79 | unsupported.erase(ukey); |
William A. Kennington III | 2e09d27 | 2022-10-14 17:15:00 -0700 | [diff] [blame] | 80 | ret.emplace(complete(ifr)); |
| 81 | return ret; |
| 82 | } |
| 83 | |
| 84 | EthInfo getEthInfo(stdplus::zstring_view ifname) |
| 85 | { |
| 86 | ethtool_cmd edata = {}; |
| 87 | edata.cmd = ETHTOOL_GSET; |
| 88 | return optionalIFReq( |
| 89 | ifname, SIOCETHTOOL, "ETHTOOL"sv, |
| 90 | [&](const ifreq&) { |
Patrick Williams | ad20502 | 2024-08-16 15:20:07 -0400 | [diff] [blame^] | 91 | return EthInfo{.autoneg = edata.autoneg != 0, |
| 92 | .speed = edata.speed}; |
| 93 | }, |
William A. Kennington III | 2e09d27 | 2022-10-14 17:15:00 -0700 | [diff] [blame] | 94 | &edata) |
| 95 | .value_or(EthInfo{}); |
| 96 | } |
| 97 | |
William A. Kennington III | 2e09d27 | 2022-10-14 17:15:00 -0700 | [diff] [blame] | 98 | void setMTU(std::string_view ifname, unsigned mtu) |
| 99 | { |
| 100 | auto ifr = makeIFReq(ifname); |
| 101 | ifr.ifr_mtu = mtu; |
| 102 | getIFSock().ioctl(SIOCSIFMTU, &ifr); |
| 103 | } |
| 104 | |
| 105 | void setNICUp(std::string_view ifname, bool up) |
| 106 | { |
| 107 | ifreq ifr = executeIFReq(ifname, SIOCGIFFLAGS); |
| 108 | ifr.ifr_flags &= ~IFF_UP; |
| 109 | ifr.ifr_flags |= up ? IFF_UP : 0; |
William A. Kennington III | c53f2ed | 2023-09-28 12:06:33 -0700 | [diff] [blame] | 110 | lg2::info("Setting NIC {UPDOWN} on {NET_INTF}", "UPDOWN", |
| 111 | up ? "up"sv : "down"sv, "NET_INTF", ifname); |
William A. Kennington III | 2e09d27 | 2022-10-14 17:15:00 -0700 | [diff] [blame] | 112 | getIFSock().ioctl(SIOCSIFFLAGS, &ifr); |
| 113 | } |
| 114 | |
William A. Kennington III | 6d21751 | 2022-11-17 16:18:01 -0800 | [diff] [blame] | 115 | void 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; |
Patrick Williams | ad20502 | 2024-08-16 15:20:07 -0400 | [diff] [blame^] | 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 | std::format("Failed to delete `{}`: {}", idx, strerror(err))); |
| 134 | }); |
William A. Kennington III | 6d21751 | 2022-11-17 16:18:01 -0800 | [diff] [blame] | 135 | } |
| 136 | |
William A. Kennington III | 2e09d27 | 2022-10-14 17:15:00 -0700 | [diff] [blame] | 137 | } // namespace phosphor::network::system |