1 /*
2 * Copyright (C) 2020 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <libnetdevice/ethtool.h>
18
19 #include "ifreqs.h"
20
21 #include <linux/ethtool.h>
22
23 namespace android::netdevice::ethtool {
24
getValue(const std::string & ifname,uint32_t command)25 std::optional<uint32_t> getValue(const std::string& ifname, uint32_t command) {
26 struct ethtool_value valueop = {};
27 valueop.cmd = command;
28
29 auto ifr = ifreqs::fromName(ifname);
30 ifr.ifr_data = &valueop;
31
32 if (!ifreqs::send(SIOCETHTOOL, ifr)) return std::nullopt;
33 return valueop.data;
34 }
35
setValue(const std::string & ifname,uint32_t command,uint32_t value)36 bool setValue(const std::string& ifname, uint32_t command, uint32_t value) {
37 struct ethtool_value valueop = {};
38 valueop.cmd = command;
39 valueop.data = value;
40
41 auto ifr = ifreqs::fromName(ifname);
42 ifr.ifr_data = &valueop;
43
44 return ifreqs::send(SIOCETHTOOL, ifr);
45 }
46
47 } // namespace android::netdevice::ethtool
48