1 #include "system_queries.hpp" 2 3 #include "netlink.hpp" 4 5 #include <linux/ethtool.h> 6 #include <linux/rtnetlink.h> 7 #include <linux/sockios.h> 8 #include <net/if.h> 9 10 #include <phosphor-logging/lg2.hpp> 11 #include <stdplus/fd/create.hpp> 12 #include <stdplus/hash/tuple.hpp> 13 #include <stdplus/util/cexec.hpp> 14 15 #include <algorithm> 16 #include <format> 17 #include <optional> 18 #include <stdexcept> 19 #include <string_view> 20 #include <unordered_set> 21 22 namespace phosphor::network::system 23 { 24 25 using std::literals::string_view_literals::operator""sv; 26 27 static stdplus::Fd& getIFSock() 28 { 29 using namespace stdplus::fd; 30 static auto fd = socket(SocketDomain::INet, SocketType::Datagram, 31 SocketProto::IP); 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; 58 auto ukey = std::make_tuple(std::string(ifname), cmd); 59 static std::unordered_set<std::tuple<std::string, unsigned long long>> 60 unsupported; 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 { 69 if (unsupported.find(ukey) == unsupported.end()) 70 { 71 unsupported.emplace(std::move(ukey)); 72 lg2::info("{NET_IFREQ} not supported on {NET_INTF}", 73 "NET_IFREQ", cmdname, "NET_INTF", ifname); 74 } 75 return ret; 76 } 77 throw; 78 } 79 unsupported.erase(ukey); 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&) { 91 return EthInfo{.autoneg = edata.autoneg != 0, .speed = edata.speed}; 92 }, 93 &edata) 94 .value_or(EthInfo{}); 95 } 96 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 lg2::info("Setting NIC {UPDOWN} on {NET_INTF}", "UPDOWN", 110 up ? "up"sv : "down"sv, "NET_INTF", ifname); 111 getIFSock().ioctl(SIOCSIFFLAGS, &ifr); 112 } 113 114 void deleteIntf(unsigned idx) 115 { 116 if (idx == 0) 117 { 118 return; 119 } 120 ifinfomsg msg = {}; 121 msg.ifi_family = AF_UNSPEC; 122 msg.ifi_index = idx; 123 netlink::performRequest(NETLINK_ROUTE, RTM_DELLINK, NLM_F_REPLACE, msg, 124 [&](const nlmsghdr& hdr, std::string_view data) { 125 int err = 0; 126 if (hdr.nlmsg_type == NLMSG_ERROR) 127 { 128 err = netlink::extractRtData<nlmsgerr>(data).error; 129 } 130 throw std::runtime_error( 131 std::format("Failed to delete `{}`: {}", idx, strerror(err))); 132 }); 133 } 134 135 } // namespace phosphor::network::system 136