Implement ethtool get/set value operations
Bug: 156784343
Test: manual
Change-Id: I33b23bf9c6639cab6006c756d4ddbe561b9441ba
diff --git a/automotive/can/1.0/default/libnetdevice/Android.bp b/automotive/can/1.0/default/libnetdevice/Android.bp
index c5fd311..928ad13 100644
--- a/automotive/can/1.0/default/libnetdevice/Android.bp
+++ b/automotive/can/1.0/default/libnetdevice/Android.bp
@@ -35,6 +35,8 @@
"NetlinkSocket.cpp",
"can.cpp",
"common.cpp",
+ "ethtool.cpp",
+ "ifreqs.cpp",
"libnetdevice.cpp",
"printer.cpp",
"vlan.cpp",
diff --git a/automotive/can/1.0/default/libnetdevice/common.cpp b/automotive/can/1.0/default/libnetdevice/common.cpp
index f64d7d3..103cf53 100644
--- a/automotive/can/1.0/default/libnetdevice/common.cpp
+++ b/automotive/can/1.0/default/libnetdevice/common.cpp
@@ -22,6 +22,8 @@
namespace android::netdevice {
+socketparams::Params socketparams::current = general;
+
unsigned int nametoindex(const std::string& ifname) {
const auto ifidx = if_nametoindex(ifname.c_str());
if (ifidx != 0) return ifidx;
diff --git a/automotive/can/1.0/default/libnetdevice/common.h b/automotive/can/1.0/default/libnetdevice/common.h
index c521835..1e04d41 100644
--- a/automotive/can/1.0/default/libnetdevice/common.h
+++ b/automotive/can/1.0/default/libnetdevice/common.h
@@ -18,10 +18,28 @@
#include "nlbuf.h"
+#include <linux/can.h>
+#include <net/if.h>
+
#include <string>
namespace android::netdevice {
+namespace socketparams {
+
+struct Params {
+ int domain;
+ int type;
+ int protocol;
+};
+
+constexpr Params general = {AF_INET, SOCK_DGRAM, 0};
+constexpr Params can = {PF_CAN, SOCK_RAW, CAN_RAW};
+
+extern Params current;
+
+} // namespace socketparams
+
/**
* Returns the index of a given network interface.
*
diff --git a/automotive/can/1.0/default/libnetdevice/ethtool.cpp b/automotive/can/1.0/default/libnetdevice/ethtool.cpp
new file mode 100644
index 0000000..762ef5c
--- /dev/null
+++ b/automotive/can/1.0/default/libnetdevice/ethtool.cpp
@@ -0,0 +1,47 @@
+/*
+ * Copyright (C) 2020 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <libnetdevice/ethtool.h>
+
+#include "ifreqs.h"
+
+#include <linux/ethtool.h>
+
+namespace android::netdevice::ethtool {
+
+std::optional<uint32_t> getValue(const std::string& ifname, uint32_t command) {
+ struct ethtool_value valueop = {};
+ valueop.cmd = command;
+
+ auto ifr = ifreqs::fromName(ifname);
+ ifr.ifr_data = &valueop;
+
+ if (!ifreqs::send(SIOCETHTOOL, ifr)) return std::nullopt;
+ return valueop.data;
+}
+
+bool setValue(const std::string& ifname, uint32_t command, uint32_t value) {
+ struct ethtool_value valueop = {};
+ valueop.cmd = command;
+ valueop.data = value;
+
+ auto ifr = ifreqs::fromName(ifname);
+ ifr.ifr_data = &valueop;
+
+ return ifreqs::send(SIOCETHTOOL, ifr);
+}
+
+} // namespace android::netdevice::ethtool
diff --git a/automotive/can/1.0/default/libnetdevice/ifreqs.cpp b/automotive/can/1.0/default/libnetdevice/ifreqs.cpp
new file mode 100644
index 0000000..9335021
--- /dev/null
+++ b/automotive/can/1.0/default/libnetdevice/ifreqs.cpp
@@ -0,0 +1,48 @@
+/*
+ * Copyright (C) 2020 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "ifreqs.h"
+
+#include "common.h"
+
+#include <android-base/logging.h>
+#include <android-base/unique_fd.h>
+
+namespace android::netdevice::ifreqs {
+
+bool send(unsigned long request, struct ifreq& ifr) {
+ base::unique_fd sock(socket(socketparams::current.domain, socketparams::current.type,
+ socketparams::current.protocol));
+ if (!sock.ok()) {
+ LOG(ERROR) << "Can't create socket";
+ return false;
+ }
+
+ if (ioctl(sock.get(), request, &ifr) < 0) {
+ PLOG(ERROR) << "ioctl(" << std::hex << request << std::dec << ") failed";
+ return false;
+ }
+
+ return true;
+}
+
+struct ifreq fromName(const std::string& ifname) {
+ struct ifreq ifr = {};
+ strlcpy(ifr.ifr_name, ifname.c_str(), IF_NAMESIZE);
+ return ifr;
+}
+
+} // namespace android::netdevice::ifreqs
diff --git a/automotive/can/1.0/default/libnetdevice/ifreqs.h b/automotive/can/1.0/default/libnetdevice/ifreqs.h
new file mode 100644
index 0000000..25a40a6
--- /dev/null
+++ b/automotive/can/1.0/default/libnetdevice/ifreqs.h
@@ -0,0 +1,42 @@
+/*
+ * Copyright (C) 2020 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#pragma once
+
+#include <net/if.h>
+
+#include <string>
+
+namespace android::netdevice::ifreqs {
+
+/**
+ * Sends ioctl interface request.
+ *
+ * \param request Request type (such as SIOCGIFFLAGS)
+ * \param ifr Request data (both input and output)
+ * \return true if the call succeeded, false otherwise
+ */
+bool send(unsigned long request, struct ifreq& ifr);
+
+/**
+ * Initializes interface request with interface name.
+ *
+ * \param ifname Interface to initialize request with
+ * \return Interface request with ifr_name field set to ifname
+ */
+struct ifreq fromName(const std::string& ifname);
+
+} // namespace android::netdevice::ifreqs
diff --git a/automotive/can/1.0/default/libnetdevice/include/libnetdevice/ethtool.h b/automotive/can/1.0/default/libnetdevice/include/libnetdevice/ethtool.h
new file mode 100644
index 0000000..26bfdce
--- /dev/null
+++ b/automotive/can/1.0/default/libnetdevice/include/libnetdevice/ethtool.h
@@ -0,0 +1,45 @@
+/*
+ * Copyright (C) 2020 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#pragma once
+
+#include <optional>
+#include <string>
+
+namespace android::netdevice::ethtool {
+
+/**
+ * Fetch a single value with ethtool_value.
+ *
+ * \see linux/ethtool.h
+ * \param ifname Interface to fetch data for
+ * \param command Fetch command (ETHTOOL_G*)
+ * \return value, or nullopt if fetch failed
+ */
+std::optional<uint32_t> getValue(const std::string& ifname, uint32_t command);
+
+/**
+ * Set a single value with ethtool_value.
+ *
+ * \see linux/ethtool.h
+ * \param ifname Interface to set data for
+ * \param command Set command (ETHTOOL_S*)
+ * \param value New value
+ * \return true if succeeded, false otherwise
+ */
+bool setValue(const std::string& ifname, uint32_t command, uint32_t value);
+
+} // namespace android::netdevice::ethtool
diff --git a/automotive/can/1.0/default/libnetdevice/libnetdevice.cpp b/automotive/can/1.0/default/libnetdevice/libnetdevice.cpp
index 827f8f3..73b91d0 100644
--- a/automotive/can/1.0/default/libnetdevice/libnetdevice.cpp
+++ b/automotive/can/1.0/default/libnetdevice/libnetdevice.cpp
@@ -19,6 +19,7 @@
#include "NetlinkRequest.h"
#include "NetlinkSocket.h"
#include "common.h"
+#include "ifreqs.h"
#include <android-base/logging.h>
@@ -27,21 +28,6 @@
namespace android::netdevice {
-namespace socketparams {
-
-struct Params {
- int domain;
- int type;
- int protocol;
-};
-
-static constexpr Params general = {AF_INET, SOCK_DGRAM, 0};
-static constexpr Params can = {PF_CAN, SOCK_RAW, CAN_RAW};
-
-static Params current = general;
-
-} // namespace socketparams
-
void useCanSockets(bool yes) {
socketparams::current = yes ? socketparams::can : socketparams::general;
}
@@ -50,31 +36,9 @@
return nametoindex(ifname) != 0;
}
-static bool sendIfreq(unsigned long request, struct ifreq& ifr) {
- base::unique_fd sock(socket(socketparams::current.domain, socketparams::current.type,
- socketparams::current.protocol));
- if (!sock.ok()) {
- LOG(ERROR) << "Can't create socket";
- return false;
- }
-
- if (ioctl(sock.get(), request, &ifr) < 0) {
- PLOG(ERROR) << "ioctl(" << std::hex << request << std::dec << ") failed";
- return false;
- }
-
- return true;
-}
-
-static struct ifreq ifreqFromName(const std::string& ifname) {
- struct ifreq ifr = {};
- strlcpy(ifr.ifr_name, ifname.c_str(), IF_NAMESIZE);
- return ifr;
-}
-
std::optional<bool> isUp(std::string ifname) {
- struct ifreq ifr = ifreqFromName(ifname);
- if (!sendIfreq(SIOCGIFFLAGS, ifr)) return std::nullopt;
+ auto ifr = ifreqs::fromName(ifname);
+ if (!ifreqs::send(SIOCGIFFLAGS, ifr)) return std::nullopt;
return ifr.ifr_flags & IFF_UP;
}
@@ -83,17 +47,17 @@
}
bool up(std::string ifname) {
- struct ifreq ifr = ifreqFromName(ifname);
- if (!sendIfreq(SIOCGIFFLAGS, ifr)) return false;
+ auto ifr = ifreqs::fromName(ifname);
+ if (!ifreqs::send(SIOCGIFFLAGS, ifr)) return false;
ifr.ifr_flags |= IFF_UP;
- return sendIfreq(SIOCSIFFLAGS, ifr);
+ return ifreqs::send(SIOCSIFFLAGS, ifr);
}
bool down(std::string ifname) {
- struct ifreq ifr = ifreqFromName(ifname);
- if (!sendIfreq(SIOCGIFFLAGS, ifr)) return false;
+ auto ifr = ifreqs::fromName(ifname);
+ if (!ifreqs::send(SIOCGIFFLAGS, ifr)) return false;
ifr.ifr_flags &= ~IFF_UP;
- return sendIfreq(SIOCSIFFLAGS, ifr);
+ return ifreqs::send(SIOCSIFFLAGS, ifr);
}
bool add(std::string dev, std::string type) {