blob: 6c59547bae0b2020ea81a0dd5c68f45e7dd79cc9 [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
11#include <algorithm>
12#include <optional>
Jagpal Singh Gilla2947b42023-04-17 21:10:14 -070013#include <phosphor-logging/lg2.hpp>
William A. Kennington III2e09d272022-10-14 17:15:00 -070014#include <stdexcept>
15#include <stdplus/fd/create.hpp>
William A. Kennington III2e09d272022-10-14 17:15:00 -070016#include <stdplus/util/cexec.hpp>
17#include <string_view>
William A. Kennington III2e09d272022-10-14 17:15:00 -070018
19namespace phosphor::network::system
20{
21
22using std::literals::string_view_literals::operator""sv;
William A. Kennington III2e09d272022-10-14 17:15:00 -070023
24static stdplus::Fd& getIFSock()
25{
26 using namespace stdplus::fd;
27 static auto fd =
28 socket(SocketDomain::INet, SocketType::Datagram, SocketProto::IP);
29 return fd;
30}
31
32static ifreq makeIFReq(std::string_view ifname)
33{
34 ifreq ifr = {};
35 const auto copied = std::min<std::size_t>(ifname.size(), IFNAMSIZ - 1);
36 std::copy_n(ifname.begin(), copied, ifr.ifr_name);
37 return ifr;
38}
39
40static ifreq executeIFReq(std::string_view ifname, unsigned long cmd,
41 void* data = nullptr)
42{
43 ifreq ifr = makeIFReq(ifname);
44 ifr.ifr_data = reinterpret_cast<char*>(data);
45 getIFSock().ioctl(cmd, &ifr);
46 return ifr;
47}
48
49inline auto optionalIFReq(stdplus::zstring_view ifname, unsigned long long cmd,
50 std::string_view cmdname, auto&& complete,
51 void* data = nullptr)
52{
53 ifreq ifr;
54 std::optional<decltype(complete(ifr))> ret;
William A. Kennington III3e471c52022-10-27 19:46:07 -070055 auto ukey = std::make_tuple(std::string(ifname), cmd);
56 static std::unordered_set<std::tuple<std::string, unsigned long long>>
57 unsupported;
William A. Kennington III2e09d272022-10-14 17:15:00 -070058 try
59 {
60 ifr = executeIFReq(ifname, cmd, data);
61 }
62 catch (const std::system_error& e)
63 {
64 if (e.code() == std::errc::operation_not_supported)
65 {
William A. Kennington III3e471c52022-10-27 19:46:07 -070066 if (unsupported.find(ukey) == unsupported.end())
67 {
68 unsupported.emplace(std::move(ukey));
Jagpal Singh Gilla2947b42023-04-17 21:10:14 -070069 lg2::info("{CMD_NAME} not supported on {INTERFACE_NAME}",
70 "CMD_NAME", cmdname, "INTERFACE_NAME", ifname);
William A. Kennington III3e471c52022-10-27 19:46:07 -070071 }
William A. Kennington III2e09d272022-10-14 17:15:00 -070072 return ret;
73 }
74 throw;
75 }
William A. Kennington III3e471c52022-10-27 19:46:07 -070076 unsupported.erase(ukey);
William A. Kennington III2e09d272022-10-14 17:15:00 -070077 ret.emplace(complete(ifr));
78 return ret;
79}
80
81EthInfo getEthInfo(stdplus::zstring_view ifname)
82{
83 ethtool_cmd edata = {};
84 edata.cmd = ETHTOOL_GSET;
85 return optionalIFReq(
86 ifname, SIOCETHTOOL, "ETHTOOL"sv,
87 [&](const ifreq&) {
88 return EthInfo{.autoneg = edata.autoneg != 0,
89 .speed = edata.speed};
90 },
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;
119 netlink::performRequest(
120 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 });
130}
131
William A. Kennington III2e09d272022-10-14 17:15:00 -0700132} // namespace phosphor::network::system