blob: 0ce674f21a173df948fd1627e4eefd12fb93a75c [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 III2e09d272022-10-14 17:15:00 -070013#include <stdplus/util/cexec.hpp>
Patrick Williams89d734b2023-05-10 07:50:25 -050014
15#include <algorithm>
16#include <optional>
17#include <stdexcept>
William A. Kennington III2e09d272022-10-14 17:15:00 -070018#include <string_view>
William A. Kennington III2e09d272022-10-14 17:15:00 -070019
20namespace phosphor::network::system
21{
22
23using std::literals::string_view_literals::operator""sv;
William A. Kennington III2e09d272022-10-14 17:15:00 -070024
25static stdplus::Fd& getIFSock()
26{
27 using namespace stdplus::fd;
Patrick Williams89d734b2023-05-10 07:50:25 -050028 static auto fd = socket(SocketDomain::INet, SocketType::Datagram,
29 SocketProto::IP);
William A. Kennington III2e09d272022-10-14 17:15:00 -070030 return fd;
31}
32
33static ifreq makeIFReq(std::string_view ifname)
34{
35 ifreq ifr = {};
36 const auto copied = std::min<std::size_t>(ifname.size(), IFNAMSIZ - 1);
37 std::copy_n(ifname.begin(), copied, ifr.ifr_name);
38 return ifr;
39}
40
41static ifreq executeIFReq(std::string_view ifname, unsigned long cmd,
42 void* data = nullptr)
43{
44 ifreq ifr = makeIFReq(ifname);
45 ifr.ifr_data = reinterpret_cast<char*>(data);
46 getIFSock().ioctl(cmd, &ifr);
47 return ifr;
48}
49
50inline auto optionalIFReq(stdplus::zstring_view ifname, unsigned long long cmd,
51 std::string_view cmdname, auto&& complete,
52 void* data = nullptr)
53{
54 ifreq ifr;
55 std::optional<decltype(complete(ifr))> ret;
William A. Kennington III3e471c52022-10-27 19:46:07 -070056 auto ukey = std::make_tuple(std::string(ifname), cmd);
57 static std::unordered_set<std::tuple<std::string, unsigned long long>>
58 unsupported;
William A. Kennington III2e09d272022-10-14 17:15:00 -070059 try
60 {
61 ifr = executeIFReq(ifname, cmd, data);
62 }
63 catch (const std::system_error& e)
64 {
65 if (e.code() == std::errc::operation_not_supported)
66 {
William A. Kennington III3e471c52022-10-27 19:46:07 -070067 if (unsupported.find(ukey) == unsupported.end())
68 {
69 unsupported.emplace(std::move(ukey));
William A. Kennington III1d25ca42023-05-30 16:55:28 -070070 lg2::info("{NET_IFREQ} not supported on {NET_INTF}",
71 "NET_IFREQ", cmdname, "NET_INTF", ifname);
William A. Kennington III3e471c52022-10-27 19:46:07 -070072 }
William A. Kennington III2e09d272022-10-14 17:15:00 -070073 return ret;
74 }
75 throw;
76 }
William A. Kennington III3e471c52022-10-27 19:46:07 -070077 unsupported.erase(ukey);
William A. Kennington III2e09d272022-10-14 17:15:00 -070078 ret.emplace(complete(ifr));
79 return ret;
80}
81
82EthInfo getEthInfo(stdplus::zstring_view ifname)
83{
84 ethtool_cmd edata = {};
85 edata.cmd = ETHTOOL_GSET;
86 return optionalIFReq(
87 ifname, SIOCETHTOOL, "ETHTOOL"sv,
88 [&](const ifreq&) {
Patrick Williams89d734b2023-05-10 07:50:25 -050089 return EthInfo{.autoneg = edata.autoneg != 0, .speed = edata.speed};
William A. Kennington III2e09d272022-10-14 17:15:00 -070090 },
91 &edata)
92 .value_or(EthInfo{});
93}
94
William A. Kennington III2e09d272022-10-14 17:15:00 -070095void setMTU(std::string_view ifname, unsigned mtu)
96{
97 auto ifr = makeIFReq(ifname);
98 ifr.ifr_mtu = mtu;
99 getIFSock().ioctl(SIOCSIFMTU, &ifr);
100}
101
102void setNICUp(std::string_view ifname, bool up)
103{
104 ifreq ifr = executeIFReq(ifname, SIOCGIFFLAGS);
105 ifr.ifr_flags &= ~IFF_UP;
106 ifr.ifr_flags |= up ? IFF_UP : 0;
107 getIFSock().ioctl(SIOCSIFFLAGS, &ifr);
108}
109
William A. Kennington III6d217512022-11-17 16:18:01 -0800110void deleteIntf(unsigned idx)
111{
112 if (idx == 0)
113 {
114 return;
115 }
116 ifinfomsg msg = {};
117 msg.ifi_family = AF_UNSPEC;
118 msg.ifi_index = idx;
Patrick Williams89d734b2023-05-10 07:50:25 -0500119 netlink::performRequest(NETLINK_ROUTE, RTM_DELLINK, NLM_F_REPLACE, msg,
120 [&](const nlmsghdr& hdr, std::string_view data) {
121 int err = 0;
122 if (hdr.nlmsg_type == NLMSG_ERROR)
123 {
124 err = netlink::extractRtData<nlmsgerr>(data).error;
125 }
126 throw std::runtime_error(
127 fmt::format("Failed to delete `{}`: {}", idx, strerror(err)));
128 });
William A. Kennington III6d217512022-11-17 16:18:01 -0800129}
130
William A. Kennington III2e09d272022-10-14 17:15:00 -0700131} // namespace phosphor::network::system