blob: e922504c5ed3f3507579e92905def576e67c5f8e [file] [log] [blame]
William A. Kennington III2e09d272022-10-14 17:15:00 -07001#include "system_queries.hpp"
2
William A. Kennington III6d217512022-11-17 16:18:01 -08003#include "netlink.hpp"
4
William A. Kennington III2e09d272022-10-14 17:15:00 -07005#include <fmt/format.h>
William A. Kennington III2e09d272022-10-14 17:15:00 -07006#include <linux/ethtool.h>
William A. Kennington III6d217512022-11-17 16:18:01 -08007#include <linux/rtnetlink.h>
William A. Kennington III2e09d272022-10-14 17:15:00 -07008#include <linux/sockios.h>
9#include <net/if.h>
10
Jagpal Singh Gilla2947b42023-04-17 21:10:14 -070011#include <phosphor-logging/lg2.hpp>
William A. Kennington III2e09d272022-10-14 17:15:00 -070012#include <stdplus/fd/create.hpp>
William A. Kennington III11b59a32023-06-02 15:56:33 -070013#include <stdplus/hash/tuple.hpp>
William A. Kennington III2e09d272022-10-14 17:15:00 -070014#include <stdplus/util/cexec.hpp>
Patrick Williams89d734b2023-05-10 07:50:25 -050015
16#include <algorithm>
17#include <optional>
18#include <stdexcept>
William A. Kennington III2e09d272022-10-14 17:15:00 -070019#include <string_view>
William A. Kennington III2e09d272022-10-14 17:15:00 -070020
21namespace phosphor::network::system
22{
23
24using std::literals::string_view_literals::operator""sv;
William A. Kennington III2e09d272022-10-14 17:15:00 -070025
26static stdplus::Fd& getIFSock()
27{
28 using namespace stdplus::fd;
Patrick Williams89d734b2023-05-10 07:50:25 -050029 static auto fd = socket(SocketDomain::INet, SocketType::Datagram,
30 SocketProto::IP);
William A. Kennington III2e09d272022-10-14 17:15:00 -070031 return fd;
32}
33
34static 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
42static 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
51inline 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 III3e471c52022-10-27 19:46:07 -070057 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 III2e09d272022-10-14 17:15:00 -070060 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 III3e471c52022-10-27 19:46:07 -070068 if (unsupported.find(ukey) == unsupported.end())
69 {
70 unsupported.emplace(std::move(ukey));
William A. Kennington III1d25ca42023-05-30 16:55:28 -070071 lg2::info("{NET_IFREQ} not supported on {NET_INTF}",
72 "NET_IFREQ", cmdname, "NET_INTF", ifname);
William A. Kennington III3e471c52022-10-27 19:46:07 -070073 }
William A. Kennington III2e09d272022-10-14 17:15:00 -070074 return ret;
75 }
76 throw;
77 }
William A. Kennington III3e471c52022-10-27 19:46:07 -070078 unsupported.erase(ukey);
William A. Kennington III2e09d272022-10-14 17:15:00 -070079 ret.emplace(complete(ifr));
80 return ret;
81}
82
83EthInfo 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 Williams89d734b2023-05-10 07:50:25 -050090 return EthInfo{.autoneg = edata.autoneg != 0, .speed = edata.speed};
William A. Kennington III2e09d272022-10-14 17:15:00 -070091 },
92 &edata)
93 .value_or(EthInfo{});
94}
95
William A. Kennington III2e09d272022-10-14 17:15:00 -070096void 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
103void 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 III6d217512022-11-17 16:18:01 -0800111void 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 Williams89d734b2023-05-10 07:50:25 -0500120 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 III6d217512022-11-17 16:18:01 -0800130}
131
William A. Kennington III2e09d272022-10-14 17:15:00 -0700132} // namespace phosphor::network::system