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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
use std::alloc;
use std::ffi::CStr;
use std::mem;
use std::os::fd::AsRawFd;
use std::os::fd::FromRawFd;
use std::os::fd::OwnedFd;
use std::ptr;
use std::str;

use nix::errno::Errno;
use nix::libc;
use nix::sys::socket::socket;
use nix::sys::socket::AddressFamily;
use nix::sys::socket::SockFlag;
use nix::sys::socket::SockType;

use crate::errors::EthtoolError;
use crate::ethtool_sys;
const ETH_GSTATS_LEN: usize = 8;

fn if_name_bytes(if_name: &str) -> [libc::c_char; libc::IF_NAMESIZE] {
    let mut it = if_name.as_bytes().iter().copied();
    [0; libc::IF_NAMESIZE].map(|_| it.next().unwrap_or(0) as libc::c_char)
}

fn ioctl(
    fd: &OwnedFd,
    if_name: [libc::c_char; libc::IF_NAMESIZE],
    data: *mut libc::c_char,
) -> Result<(), Errno> {
    let ifr = libc::ifreq {
        ifr_name: if_name,
        ifr_ifru: libc::__c_anonymous_ifr_ifru { ifru_data: data },
    };

    let exit_code = unsafe { libc::ioctl(fd.as_raw_fd(), nix::libc::SIOCETHTOOL, &ifr) };
    if exit_code != 0 {
        return Err(Errno::from_i32(exit_code));
    }
    Ok(())
}

/// Parses the byte array returned by ioctl for ETHTOOL_GSTRINGS command.
/// In case of error during parsing any stat name,
/// the function returns a `ParseError`.
fn parse_names(data: &[u8]) -> Result<Vec<String>, EthtoolError> {
    let names = data
        .chunks(ethtool_sys::ETH_GSTRING_LEN as usize)
        .map(|chunk| {
            // Find the position of the null terminator for specific stat name
            let c_str = CStr::from_bytes_until_nul(chunk);
            match c_str {
                Ok(c_str) => Ok(c_str.to_string_lossy().into_owned()),
                Err(err) => Err(EthtoolError::ParseError(err.to_string())),
            }
        })
        .collect::<Result<Vec<String>, EthtoolError>>()?;

    Ok(names)
}

/// Parses the byte array returned by ioctl for ETHTOOL_GSTATS command.
/// In case of error during parsing any feature,
/// the function returns a `ParseError`.
fn parse_values(data: &[u64], length: usize) -> Result<Vec<u64>, EthtoolError> {
    let values = data.iter().take(length).copied().collect::<Vec<u64>>();

    Ok(values)
}

struct StringSetInfo {
    layout: alloc::Layout,
    ptr: *mut ethtool_sys::ethtool_sset_info,
}

impl StringSetInfo {
    /// Allocates memory for ethtool_sset_info struct and initializes it.
    fn new() -> Result<Self, EthtoolError> {
        // Calculate the layout with proper alignment
        let layout = alloc::Layout::from_size_align(
            mem::size_of::<ethtool_sys::ethtool_sset_info>(),
            mem::align_of::<ethtool_sys::ethtool_sset_info>(),
        )
        .map_err(EthtoolError::CStructInitError)?;

        // Allocate memory for the struct
        let sset_info_ptr = unsafe { alloc::alloc(layout) } as *mut ethtool_sys::ethtool_sset_info;

        // Initialize the fields of the struct
        unsafe {
            let cmd_ptr = ptr::addr_of_mut!((*sset_info_ptr).cmd);
            let reserved_ptr = ptr::addr_of_mut!((*sset_info_ptr).reserved);
            let sset_mask_ptr = ptr::addr_of_mut!((*sset_info_ptr).sset_mask);
            let data_ptr = ptr::addr_of_mut!((*sset_info_ptr).data);

            cmd_ptr.write(ethtool_sys::ETHTOOL_GSSET_INFO);
            reserved_ptr.write(1u32);
            sset_mask_ptr.write((1 << ethtool_sys::ethtool_stringset_ETH_SS_STATS) as u64);

            data_ptr.write_bytes(0u8, mem::size_of::<u32>());
        }

        Ok(StringSetInfo {
            layout,
            ptr: sset_info_ptr,
        })
    }

    fn data(&self) -> Result<usize, EthtoolError> {
        unsafe {
            let value = self.ptr.as_ref().ok_or(EthtoolError::CStructReadError())?;
            Ok(value.data.as_ptr().read() as usize)
        }
    }
}

impl Drop for StringSetInfo {
    fn drop(&mut self) {
        unsafe {
            alloc::dealloc(self.ptr as *mut u8, self.layout);
        }
    }
}

struct GStrings {
    length: usize,
    layout: alloc::Layout,
    ptr: *mut ethtool_sys::ethtool_gstrings,
}

impl GStrings {
    fn new(length: usize) -> Result<Self, EthtoolError> {
        let data_length = length * ethtool_sys::ETH_GSTRING_LEN as usize;

        // Calculate the layout with proper alignment based on the struct itself
        let layout = alloc::Layout::from_size_align(
            mem::size_of::<ethtool_sys::ethtool_gstrings>() + data_length * mem::size_of::<u8>(),
            mem::align_of::<ethtool_sys::ethtool_gstrings>(),
        )
        .map_err(EthtoolError::CStructInitError)?;

        // Allocate memory for the struct
        let gstrings_ptr = unsafe { alloc::alloc(layout) } as *mut ethtool_sys::ethtool_gstrings;
        if gstrings_ptr.is_null() {
            return Err(EthtoolError::AllocationFailure());
        }

        // Initialize the fields of the struct using raw pointers
        unsafe {
            let cmd_ptr = ptr::addr_of_mut!((*gstrings_ptr).cmd);
            let ss_ptr = ptr::addr_of_mut!((*gstrings_ptr).string_set);
            let data_ptr = ptr::addr_of_mut!((*gstrings_ptr).data);

            cmd_ptr.write(ethtool_sys::ETHTOOL_GSTRINGS);
            ss_ptr.write(ethtool_sys::ethtool_stringset_ETH_SS_STATS);

            // Initialize the data field with zeros
            data_ptr.write_bytes(0u8, data_length);
        }

        Ok(GStrings {
            length,
            layout,
            ptr: gstrings_ptr,
        })
    }

    fn data(&self) -> Result<&[u8], EthtoolError> {
        unsafe {
            Ok(std::slice::from_raw_parts(
                (*self.ptr).data.as_ptr(),
                self.length * ethtool_sys::ETH_GSTRING_LEN as usize,
            ))
        }
    }
}

impl Drop for GStrings {
    fn drop(&mut self) {
        unsafe {
            alloc::dealloc(self.ptr as *mut u8, self.layout);
        }
    }
}

struct GStats {
    length: usize,
    layout: alloc::Layout,
    ptr: *mut ethtool_sys::ethtool_stats,
}

impl GStats {
    /// Allocates memory for ethtool_stats struct and initializes it.
    fn new(length: usize) -> Result<Self, EthtoolError> {
        let data_length = length * ETH_GSTATS_LEN;

        // Calculate the layout with proper alignment
        let layout = alloc::Layout::from_size_align(
            mem::size_of::<ethtool_sys::ethtool_stats>() + data_length * mem::size_of::<u64>(),
            mem::align_of::<ethtool_sys::ethtool_stats>(),
        )
        .map_err(EthtoolError::CStructInitError)?;

        // Allocate memory for the struct
        let stats_ptr = unsafe { alloc::alloc(layout) } as *mut ethtool_sys::ethtool_stats;
        if stats_ptr.is_null() {
            return Err(EthtoolError::AllocationFailure());
        }

        // Initialize the fields of the struct
        unsafe {
            let cmd_ptr = ptr::addr_of_mut!((*stats_ptr).cmd);
            let data_ptr = ptr::addr_of_mut!((*stats_ptr).data);

            cmd_ptr.write(ethtool_sys::ETHTOOL_GSTATS);

            // Initialize the data field with zeros
            let data_bytes = data_length * mem::size_of::<u64>();
            data_ptr.write_bytes(0u8, data_bytes);
        }

        Ok(GStats {
            length,
            layout,
            ptr: stats_ptr,
        })
    }

    fn data(&self) -> Result<&[u64], EthtoolError> {
        unsafe {
            Ok(std::slice::from_raw_parts(
                (*self.ptr).data.as_ptr(),
                self.length * ETH_GSTATS_LEN,
            ))
        }
    }
}

impl Drop for GStats {
    fn drop(&mut self) {
        unsafe {
            alloc::dealloc(self.ptr as *mut u8, self.layout);
        }
    }
}

/// A trait for reading stats using ethtool.
///
/// This trait allows mocking the ethtool calls for unit testing.
pub trait EthtoolReadable {
    fn new(if_name: &str) -> Result<Self, EthtoolError>
    where
        Self: Sized;
    fn stats(&self) -> Result<Vec<(String, u64)>, EthtoolError>;
}

pub struct Ethtool {
    sock_fd: OwnedFd,
    if_name: [libc::c_char; libc::IF_NAMESIZE],
}

impl Ethtool {
    /// Get the number of stats using ETHTOOL_GSSET_INFO command
    fn gsset_info(&self) -> Result<usize, EthtoolError> {
        let sset_info = StringSetInfo::new()?;
        let data = sset_info.ptr as *mut libc::c_char;

        match ioctl(&self.sock_fd, self.if_name, data) {
            Ok(_) => Ok(sset_info.data()?),
            Err(errno) => Err(EthtoolError::SocketError(errno)),
        }
    }

    /// Get the feature names using ETHTOOL_GSTRINGS command
    fn gstrings(&self, length: usize) -> Result<Vec<String>, EthtoolError> {
        let gstrings = GStrings::new(length)?;
        let data = gstrings.ptr as *mut libc::c_char;

        match ioctl(&self.sock_fd, self.if_name, data) {
            Ok(_) => parse_names(gstrings.data()?),
            Err(errno) => Err(EthtoolError::GStringsReadError(errno)),
        }
    }

    /// Get the statistics for the features using ETHTOOL_GSTATS command
    fn gstats(&self, features: &[String]) -> Result<Vec<u64>, EthtoolError> {
        let length = features.len();
        let gstats = GStats::new(length)?;
        let data = gstats.ptr as *mut libc::c_char;

        match ioctl(&self.sock_fd, self.if_name, data) {
            Ok(_) => parse_values(gstats.data()?, length),
            Err(errno) => Err(EthtoolError::GStatsReadError(errno)),
        }
    }
}

impl EthtoolReadable for Ethtool {
    fn new(if_name: &str) -> Result<Self, EthtoolError> {
        match socket(
            AddressFamily::Inet,
            SockType::Datagram,
            SockFlag::empty(),
            None,
        ) {
            Ok(fd) => Ok(Ethtool {
                sock_fd: unsafe { OwnedFd::from_raw_fd(fd) },
                if_name: if_name_bytes(if_name),
            }),
            Err(errno) => Err(EthtoolError::SocketError(errno)),
        }
    }

    /// Get statistics using ethtool
    /// Equivalent to `ethtool -S <ifname>` command
    fn stats(&self) -> Result<Vec<(String, u64)>, EthtoolError> {
        let length = self.gsset_info()?;

        let features = self.gstrings(length)?;
        let values = self.gstats(&features)?;

        let final_stats = features.into_iter().zip(values).collect();
        Ok(final_stats)
    }
}