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
// linux source:
// block/genhd.c >= v2.6.18
// https://elixir.bootlin.com/linux/v2.6.18/source/net/core/dev.c#L2108
use crate::netdevs::NetDev;
use crate::netdevs::NetDevs;
use crate::scanner::ProcScanner;
use crate::ProcResult;
use cfg_iif::cfg_iif;
#[derive(Debug, Default, Clone)]
pub(crate) struct NetDevsParser();
impl NetDevsParser {
pub fn parse(&mut self, sl: &[u8]) -> ProcResult<NetDevs> {
let mut netdevs = NetDevs::default();
if sl.is_empty() {
return Ok(netdevs);
}
//
let mut sc = ProcScanner::new(sl);
let mut idx: usize = 0;
//
// skip header 1
sc.scan_until(b'\n')?;
// skip header 2
sc.scan_until(b'\n')?;
//
while !sc.is_empty() {
//
if idx >= netdevs.nets.len() {
netdevs.nets.resize(idx + 1, NetDev::default());
}
let net_ref: &mut NetDev = match netdevs.nets.get_mut(idx) {
Some(net) => net,
None => return Err(crate::ProcError::InternalError),
};
{
// content of /proc/net/dev
// " br0: 15826243374 23367319 0 0 0 0 0 0 3034115058 22744337 0 0 0 0 0 0\n"
// "virbr1-nic: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n"
// on linux:
// "%6s:%8lu %7lu %4lu %4lu %4lu %5lu %10lu %9lu "
// "%8lu %7lu %4lu %4lu %4lu %5lu %7lu %10lu\n"
//
// from https://elixir.bootlin.com/linux/v2.6.18/source/net/core/dev.c
//
sc.skip_spaces();
net_ref.name = sc.next(b':')?;
//
if sc.check(b'N') {
sc.scan_until(b'\n')?;
} else {
sc.skip_spaces();
{
net_ref.rx_bytes = sc.next(b' ')?;
}
sc.skip_spaces();
{
net_ref.rx_packets = sc.next(b' ')?;
}
sc.skip_spaces();
cfg_iif!(feature = "has_netdevs_rx_errors" {
net_ref.rx_errors = sc.next(b' ')?;
} else {
sc.scan_until(b' ')?;
});
sc.skip_spaces();
cfg_iif!(feature = "has_netdevs_rx_dropped_errors" {
net_ref.rx_dropped_errors = sc.next(b' ')?;
} else {
sc.scan_until(b' ')?;
});
sc.skip_spaces();
cfg_iif!(feature = "has_netdevs_rx_fifo_errors" {
net_ref.rx_fifo_errors = sc.next(b' ')?;
} else {
sc.scan_until(b' ')?;
});
sc.skip_spaces();
cfg_iif!(feature = "has_netdevs_rx_frame_errors" {
net_ref.rx_frame_errors = sc.next(b' ')?;
} else {
sc.scan_until(b' ')?;
});
sc.skip_spaces();
cfg_iif!(feature = "has_netdevs_rx_compressed" {
net_ref.rx_compressed = sc.next(b' ')?;
} else {
sc.scan_until(b' ')?;
});
sc.skip_spaces();
cfg_iif!(feature = "has_netdevs_rx_multicast" {
net_ref.rx_multicast = sc.next(b' ')?;
} else {
sc.scan_until(b' ')?;
});
//
sc.skip_spaces();
{
net_ref.tx_bytes = sc.next(b' ')?;
}
sc.skip_spaces();
{
net_ref.tx_packets = sc.next(b' ')?;
}
sc.skip_spaces();
cfg_iif!(feature = "has_netdevs_tx_errors" {
net_ref.tx_errors = sc.next(b' ')?;
} else {
sc.scan_until(b' ')?;
});
sc.skip_spaces();
cfg_iif!(feature = "has_netdevs_tx_dropped_errors" {
net_ref.tx_dropped_errors = sc.next(b' ')?;
} else {
sc.scan_until(b' ')?;
});
sc.skip_spaces();
cfg_iif!(feature = "has_netdevs_tx_fifo_errors" {
net_ref.tx_fifo_errors = sc.next(b' ')?;
} else {
sc.scan_until(b' ')?;
});
sc.skip_spaces();
cfg_iif!(feature = "has_netdevs_tx_collisions" {
net_ref.tx_collisions = sc.next(b' ')?;
} else {
sc.scan_until(b' ')?;
});
sc.skip_spaces();
cfg_iif!(feature = "has_netdevs_tx_carrier_errors" {
net_ref.tx_carrier_errors = sc.next(b' ')?;
} else {
sc.scan_until(b' ')?;
});
sc.skip_spaces();
cfg_iif!(feature = "has_netdevs_tx_compressed" {
net_ref.tx_compressed = sc.next(b'\n')?;
} else {
sc.scan_until(b'\n')?;
});
}
}
//
idx += 1;
}
netdevs.nets.resize(idx, NetDev::default());
//
Ok(netdevs)
}
}