-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathethtool++.cc
139 lines (112 loc) · 3.03 KB
/
ethtool++.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/*
* Copyright (C) Internet Systems Consortium, Inc. ("ISC")
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* See the COPYRIGHT file distributed with this work for additional
* information regarding copyright ownership.
*/
#include <algorithm>
#include <cstring>
#include <iostream>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <linux/sockios.h>
#include "ethtool++.h"
#include "util.h"
void Ethtool::ioctl(void* data)
{
ifr.ifr_data = reinterpret_cast<char*>(data);
if (::ioctl(fd, SIOCETHTOOL, &ifr, sizeof(ifr)) < 0) {
throw_errno("ioctl(SIOCETHTOOL)");
}
}
size_t Ethtool::stringset_size(ethtool_stringset ss)
{
// memoize the table
auto iter = sizes.find(ss);
if (iter != sizes.end()) {
return iter->second;
}
// allocate memory on stack
auto size = sizeof(ethtool_sset_info) + sizeof(__u32);
auto p = reinterpret_cast<char*>(alloca(size));
if (!p) {
throw_errno("alloca");
}
std::fill(p, p + size, 0);
// shadow the allocation
auto& sset_info = *reinterpret_cast<ethtool_sset_info*>(p);
// get the data
sset_info.cmd = ETHTOOL_GSSET_INFO;
sset_info.reserved = 0;
sset_info.sset_mask = (1 << ss);
ioctl(&sset_info);
auto result = sset_info.data[0];
sizes[ss] = result;
return result;
}
Ethtool::stringset_t Ethtool::stringset(ethtool_stringset ss)
{
// determine size
size_t count = stringset_size(ss);
// allocate sufficient memory on stack
auto size = sizeof(ethtool_gstrings) + count * ETH_GSTRING_LEN;
auto p = reinterpret_cast<char*>(alloca(size));
if (!p) {
throw_errno("alloca");
}
std::fill(p, p + size, 0);
// shadow the allocation
auto& gstrings = *reinterpret_cast<ethtool_gstrings*>(p);
// get the data
gstrings.cmd = ETHTOOL_GSTRINGS;
gstrings.string_set = ss;
ioctl(&gstrings);
// build the result set
stringset_t result;
result.reserve(count);
for (unsigned int i = 0; i < gstrings.len; ++i) {
auto p = reinterpret_cast<char *>(gstrings.data) + i * ETH_GSTRING_LEN;
result.emplace_back(p); // assume NUL terminated
}
return result;
}
Ethtool::stats_t Ethtool::stats()
{
size_t count = stringset_size(ETH_SS_STATS);
// allocate memory on stack
auto size = sizeof(ethtool_stats) + count * sizeof(__u64);
auto p = reinterpret_cast<char *>(alloca(size));
if (!p) {
throw_errno("alloca");
}
std::fill(p, p + size, 0);
// shadow the allocation
auto& stats = *reinterpret_cast<ethtool_stats*>(p);
// get the data
stats.cmd = ETHTOOL_GSTATS;
ioctl(&stats);
// build the result set
stats_t result(count);
std::copy(stats.data, stats.data + stats.n_stats, result.begin());
return result;
}
Ethtool::Ethtool(const std::string& ifname)
{
fd = ::socket(AF_INET, SOCK_DGRAM, 0);
if (fd < 0) {
throw_errno("socket");
}
stpncpy(ifr.ifr_name, ifname.c_str(), IFNAMSIZ);
// retrieve the driver information
drvinfo.cmd = ETHTOOL_GDRVINFO;
ioctl(&drvinfo);
}
Ethtool::~Ethtool()
{
::close(fd);
}