William A. Kennington III | 2e09d27 | 2022-10-14 17:15:00 -0700 | [diff] [blame] | 1 | #include "system_queries.hpp" |
| 2 | |
William A. Kennington III | fd862be | 2022-10-09 18:40:55 -0700 | [diff] [blame] | 3 | #include "netlink.hpp" |
William A. Kennington III | 6a92363 | 2022-11-06 18:17:33 -0800 | [diff] [blame] | 4 | #include "rtnetlink.hpp" |
William A. Kennington III | 2e09d27 | 2022-10-14 17:15:00 -0700 | [diff] [blame] | 5 | |
| 6 | #include <fmt/format.h> |
William A. Kennington III | 2e09d27 | 2022-10-14 17:15:00 -0700 | [diff] [blame] | 7 | #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 | |
| 21 | namespace phosphor::network::system |
| 22 | { |
| 23 | |
| 24 | using std::literals::string_view_literals::operator""sv; |
| 25 | using phosphor::logging::entry; |
| 26 | using phosphor::logging::level; |
| 27 | using phosphor::logging::log; |
| 28 | |
| 29 | static 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 | |
| 37 | static 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 | |
| 45 | static 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 | |
| 54 | inline 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 III | 3e471c5 | 2022-10-27 19:46:07 -0700 | [diff] [blame] | 60 | 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 III | 2e09d27 | 2022-10-14 17:15:00 -0700 | [diff] [blame] | 63 | 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 III | 3e471c5 | 2022-10-27 19:46:07 -0700 | [diff] [blame] | 71 | 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 III | 2e09d27 | 2022-10-14 17:15:00 -0700 | [diff] [blame] | 79 | return ret; |
| 80 | } |
| 81 | throw; |
| 82 | } |
William A. Kennington III | 3e471c5 | 2022-10-27 19:46:07 -0700 | [diff] [blame] | 83 | unsupported.erase(ukey); |
William A. Kennington III | 2e09d27 | 2022-10-14 17:15:00 -0700 | [diff] [blame] | 84 | ret.emplace(complete(ifr)); |
| 85 | return ret; |
| 86 | } |
| 87 | |
| 88 | EthInfo 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 | |
| 102 | bool intfIsRunning(std::string_view ifname) |
| 103 | { |
| 104 | return executeIFReq(ifname, SIOCGIFFLAGS).ifr_flags & IFF_RUNNING; |
| 105 | } |
| 106 | |
William A. Kennington III | 2e09d27 | 2022-10-14 17:15:00 -0700 | [diff] [blame] | 107 | std::optional<unsigned> getMTU(stdplus::zstring_view ifname) |
| 108 | { |
| 109 | return optionalIFReq(ifname, SIOCGIFMTU, "GMTU", |
| 110 | [](const ifreq& ifr) { return ifr.ifr_mtu; }); |
| 111 | } |
| 112 | |
| 113 | void 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 | |
| 120 | void 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 III | 6a92363 | 2022-11-06 18:17:33 -0800 | [diff] [blame] | 128 | bool detail::validateNewAddr(const AddressInfo& info, |
| 129 | const AddressFilter& filter) noexcept |
| 130 | { |
| 131 | if (filter.ifidx != 0 && filter.ifidx != info.ifidx) |
| 132 | { |
| 133 | return false; |
| 134 | } |
| 135 | return true; |
| 136 | } |
| 137 | |
William A. Kennington III | a842690 | 2022-11-07 15:37:41 -0800 | [diff] [blame] | 138 | bool detail::validateNewNeigh(const NeighborInfo& info, |
| 139 | const NeighborFilter& filter) noexcept |
| 140 | { |
| 141 | if (filter.ifidx != 0 && filter.ifidx != info.ifidx) |
| 142 | { |
| 143 | return false; |
| 144 | } |
| 145 | return true; |
| 146 | } |
| 147 | |
William A. Kennington III | fd862be | 2022-10-09 18:40:55 -0700 | [diff] [blame] | 148 | std::vector<InterfaceInfo> getInterfaces() |
| 149 | { |
| 150 | std::vector<InterfaceInfo> ret; |
William A. Kennington III | 1aeacc9 | 2022-11-13 18:26:45 -0800 | [diff] [blame] | 151 | auto cb = [&](const nlmsghdr&, std::string_view msg) { |
William A. Kennington III | c0fdaaf | 2022-11-07 16:44:56 -0800 | [diff] [blame] | 152 | try |
William A. Kennington III | fd862be | 2022-10-09 18:40:55 -0700 | [diff] [blame] | 153 | { |
William A. Kennington III | 1aeacc9 | 2022-11-13 18:26:45 -0800 | [diff] [blame] | 154 | ret.emplace_back(netlink::intfFromRtm(msg)); |
William A. Kennington III | c0fdaaf | 2022-11-07 16:44:56 -0800 | [diff] [blame] | 155 | } |
| 156 | catch (const std::exception& e) |
| 157 | { |
| 158 | auto msg = fmt::format("Failed parsing interface: {}", e.what()); |
| 159 | log<level::ERR>(msg.c_str()); |
William A. Kennington III | fd862be | 2022-10-09 18:40:55 -0700 | [diff] [blame] | 160 | } |
| 161 | }; |
| 162 | ifinfomsg msg{}; |
| 163 | netlink::performRequest(NETLINK_ROUTE, RTM_GETLINK, NLM_F_DUMP, msg, cb); |
William A. Kennington III | 2e09d27 | 2022-10-14 17:15:00 -0700 | [diff] [blame] | 164 | return ret; |
| 165 | } |
| 166 | |
William A. Kennington III | 6a92363 | 2022-11-06 18:17:33 -0800 | [diff] [blame] | 167 | std::vector<AddressInfo> getAddresses(const AddressFilter& filter) |
| 168 | { |
| 169 | std::vector<AddressInfo> ret; |
| 170 | auto cb = [&](const nlmsghdr&, std::string_view msg) { |
William A. Kennington III | c0fdaaf | 2022-11-07 16:44:56 -0800 | [diff] [blame] | 171 | try |
William A. Kennington III | 6a92363 | 2022-11-06 18:17:33 -0800 | [diff] [blame] | 172 | { |
William A. Kennington III | c0fdaaf | 2022-11-07 16:44:56 -0800 | [diff] [blame] | 173 | auto info = netlink::addrFromRtm(msg); |
| 174 | if (detail::validateNewAddr(info, filter)) |
| 175 | { |
| 176 | ret.emplace_back(std::move(info)); |
| 177 | } |
| 178 | } |
| 179 | catch (const std::exception& e) |
| 180 | { |
| 181 | auto msg = fmt::format("Failed parsing address for ifidx {}: {}", |
| 182 | filter.ifidx, e.what()); |
| 183 | log<level::ERR>(msg.c_str()); |
William A. Kennington III | 6a92363 | 2022-11-06 18:17:33 -0800 | [diff] [blame] | 184 | } |
| 185 | }; |
| 186 | ifaddrmsg msg{}; |
| 187 | msg.ifa_index = filter.ifidx; |
| 188 | netlink::performRequest(NETLINK_ROUTE, RTM_GETADDR, NLM_F_DUMP, msg, cb); |
| 189 | return ret; |
| 190 | } |
| 191 | |
William A. Kennington III | a842690 | 2022-11-07 15:37:41 -0800 | [diff] [blame] | 192 | std::vector<NeighborInfo> getNeighbors(const NeighborFilter& filter) |
| 193 | { |
| 194 | std::vector<NeighborInfo> ret; |
| 195 | auto cb = [&](const nlmsghdr&, std::string_view msg) { |
William A. Kennington III | c0fdaaf | 2022-11-07 16:44:56 -0800 | [diff] [blame] | 196 | try |
William A. Kennington III | a842690 | 2022-11-07 15:37:41 -0800 | [diff] [blame] | 197 | { |
William A. Kennington III | c0fdaaf | 2022-11-07 16:44:56 -0800 | [diff] [blame] | 198 | auto info = netlink::neighFromRtm(msg); |
| 199 | if (detail::validateNewNeigh(info, filter)) |
| 200 | { |
| 201 | ret.push_back(std::move(info)); |
| 202 | } |
| 203 | } |
| 204 | catch (const std::exception& e) |
| 205 | { |
| 206 | auto msg = fmt::format("Failed parsing neighbor for ifidx {}: {}", |
| 207 | filter.ifidx, e.what()); |
| 208 | log<level::ERR>(msg.c_str()); |
William A. Kennington III | a842690 | 2022-11-07 15:37:41 -0800 | [diff] [blame] | 209 | } |
| 210 | }; |
| 211 | ndmsg msg{}; |
| 212 | msg.ndm_ifindex = filter.ifidx; |
| 213 | netlink::performRequest(NETLINK_ROUTE, RTM_GETNEIGH, NLM_F_DUMP, msg, cb); |
| 214 | return ret; |
| 215 | } |
| 216 | |
William A. Kennington III | 2e09d27 | 2022-10-14 17:15:00 -0700 | [diff] [blame] | 217 | } // namespace phosphor::network::system |