linux-support 0.0.25

Comprehensive Linux support for namespaces, cgroups, processes, scheduling, parsing /proc, parsing /sys, signals, hyper threads, CPUS, NUMA nodes, unusual file descriptors, PCI devices and much, much more
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
// This file is part of linux-support. It is subject to the license terms in the COPYRIGHT file found in the top-level directory of this distribution and at https://raw.githubusercontent.com/lemonrock/linux-support/master/COPYRIGHT. No part of linux-support, including this file, may be copied, modified, propagated, or distributed except according to the terms contained in the COPYRIGHT file.
// Copyright © 2020 The developers of linux-support. See the COPYRIGHT file in the top-level directory of this distribution and at https://raw.githubusercontent.com/lemonrock/linux-support/master/COPYRIGHT.


/// A transmit queue reference to a folder `/sys/class/net/<network_interface_name>/queues/tx-<N>` where `N` is `queue_identifier`.
///
/// The files `traffic_class` (which is only read only) seems unreadable on my test machine.
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct TransmitSysfsQueue<'a>
{
	network_interface_name: &'a NetworkInterfaceName,
	
	queue_identifier: QueueIdentifier,
}

impl<'a> SysfsQueue<'a> for TransmitSysfsQueue<'a>
{
	const Prefix: &'static str = "tx";
	
	fn new(network_interface_name: &'a NetworkInterfaceName, queue_identifier: QueueIdentifier) -> Self
	{
		Self
		{
			network_interface_name,
			queue_identifier,
		}
	}
	
	#[inline(always)]
	fn network_interface_name(&self) -> &'a NetworkInterfaceName
	{
		self.network_interface_name
	}
	
	#[inline(always)]
	fn queue_identifier(&self) -> QueueIdentifier
	{
		self.queue_identifier
	}
}

impl<'a> TransmitSysfsQueue<'a>
{
	/// Traffic class.
	///
	/// Returns `Ok(Some(_))` if successfully retrieved.
	///
	/// ***Only supported if the network device is multiqueue (ie has more than one transmit queue); if not supported, `Ok(None)` is returned`.
	/// Most virtual network devices are not multiqueue.
	pub fn traffic_class(&self, sys_path: &SysPath) -> io::Result<Option<TransmitQueueTrafficClass>>
	{
		match self.file_path(sys_path, "traffic_class").read_raw_without_line_feed()
		{
			Ok(value) =>
			{
				let value = &value[..];
				let mut bytes = value.split_bytes_n(2, b'-');
				
				Ok
				(
					Some
					(
						TransmitQueueTrafficClass
						{
							traffic_class: ParseNumber::parse_decimal_number(bytes.next().unwrap()).map_err(io_error_invalid_data)?,
						
							subordinate_device: match bytes.next()
							{
								Some(subordinate_device_bytes) => Some(ParseNumber::parse_decimal_number(subordinate_device_bytes).map_err(io_error_invalid_data)?),
								
								None => None,
							},
						}
					)
				)
			},
			
			Err(error) => if error.raw_os_error() == Some(ENOENT)
			{
				Ok(None)
			}
			else
			{
				Err(error)
			}
		}
	}
	
	/// Maximum rate.
	///
	/// Default is `None` (disabled).
	///
	/// Only available in Linux kernel compiled with `CONFIG_XPS`.
	///
	/// ***Not supported by some network devices, in which case `Ok(0)` (rather than `EOPNOTSUPP`)!***
	#[inline(always)]
	pub fn maximum_rate_in_megabits_per_second(&self, sys_path: &SysPath) -> io::Result<Option<NonZeroU32>>
	{
		self.tx_maxrate_file_path(sys_path).read_value()
	}
	
	/// Set maximum rate.
	///
	/// Default is `None` (disabled).
	///
	/// Only available in Linux kernel compiled with `CONFIG_XPS`.
	///
	/// Returns `Ok(true)` if successfully set.
	///
	/// ***Not supported by some network devices, in which case `Ok(false)` is returned.***.
	/// `Ok(false)` is also returned if the file does not exist.
	#[inline(always)]
	pub fn set_maximum_rate_in_megabits_per_second(&self, sys_path: &SysPath, maximum_rate_in_megabits_per_second: Option<NonZeroU32>) -> io::Result<bool>
	{
		assert_effective_user_id_is_root("write /sys/class/net/<network_interface_name>/queues/tx-<queue_identifier/tx_maxrate");
		
		let file_path = self.tx_maxrate_file_path(sys_path);
		
		if file_path.exists()
		{
			let maximum_rate_in_megabits_per_second: u32 = unsafe { transmute(maximum_rate_in_megabits_per_second) };
			match file_path.write_value(UnpaddedDecimalInteger(maximum_rate_in_megabits_per_second))
			{
				Ok(()) => Ok(true),
				
				Err(error) => if error.raw_os_error() == Some(EOPNOTSUPP)
				{
					Ok(false)
				}
				else
				{
					Err(error)
				}
			}
		}
		else
		{
			Ok(false)
		}
	}
	/// Number of transmit timeout events.
	#[inline(always)]
	pub fn number_of_timeout_events(&self, sys_path: &SysPath) -> io::Result<usize>
	{
		self.file_path(sys_path, "tx_timeout").read_value()
	}
	
	/// Transmit Packet Steering (XPS) mapping of receive queues to this transmit queue.
	///
	/// Used as ?legacy alternative to receive queue affinity.
	///
	/// Default is disabled.
	///
	/// Returns `Ok(Some(_))` if successfully retrieved.
	///
	/// ***Only supported if the network device is multiqueue (ie has more than one transmit queue); if not supported, `Ok(None)` is returned`.
	/// Most virtual network devices are not multiqueue.
	#[inline(always)]
	pub fn transmit_packet_steering_hyper_thread_affinity(&self, sys_path: &SysPath) -> io::Result<Option<HyperThreads>>
	{
		match self.xps_cpus_file_path(sys_path).parse_comma_separated_bit_set()
		{
			Ok(bit_set) => Ok(Some(HyperThreads(bit_set))),
			
			Err(error) => if error.raw_os_error() == Some(ENOENT)
			{
				Ok(None)
			}
			else
			{
				Err(error)
			}
		}
	}
	
	/// Transmit Packet Steering (XPS) mapping of receive queues to this transmit queue.
	///
	/// Used as ?legacy alternative to receive queue affinity.
	///
	/// Default is disabled.
	///
	/// Returns `Ok(true)` if successfully set.
	///
	/// ***Only supported if the network device is multiqueue (ie has more than one transmit queue); if not supported, `Ok(false)`.
	/// Most virtual network devices are not multiqueue.
	/// `Ok(false)` is also returned if the file does not exist.
	#[inline(always)]
	pub fn set_transmit_packet_steering_hyper_thread_affinity(&self, sys_path: &SysPath, hyper_threads: &HyperThreads) -> io::Result<bool>
	{
		assert_effective_user_id_is_root("write /sys/class/net/<network_interface_name>/queues/tx-<queue_identifier/xps_cpus");
		
		let file_path = self.xps_cpus_file_path(sys_path);
		
		if file_path.exists()
		{
			let mask = IntoBitMask(hyper_threads);
			match file_path.write_value(mask)
			{
				Ok(()) => Ok(true),
				
				Err(error) => if error.raw_os_error() == Some(ENOENT)
				{
					Ok(false)
				}
				else
				{
					Err(error)
				}
			}
		}
		else
		{
			Ok(false)
		}
	}
	
	/// Transmit Packet Steering (XPS) mapping of receive queues to this transmit queue.
	///
	/// Used as a (better) alternative to HyperThread affinity.
	///
	/// Default is disabled, but, if enabled, the commonest pattern is to have 1 transmit queue mapped to just 1 receive queue (ideally, using the same `QueueIdentifier`).
	#[inline(always)]
	pub fn transmit_packet_steering_receive_queue_affinity(&self, sys_path: &SysPath) -> io::Result<QueueIdentifiers>
	{
		self.xps_rxqs_file_path(sys_path).parse_comma_separated_bit_set().map(QueueIdentifiers)
	}
	
	/// Transmit Packet Steering (XPS) mapping of receive queues to this transmit queue.
	///
	/// Used as a (better) alternative to HyperThread affinity.
	///
	/// Default is disabled, but, if enabled, the commonest pattern is to have 1 transmit queue mapped to just 1 receive queue (ideally, using the same `QueueIdentifier`).
	///
	/// `receive_queue_to_map_to` must not have a size greater than the number of receive queues supported by the network device.
	///
	/// This number can be obtained from:-
	///
	/// * `GetLinkMessageData.number_of_receive_queues` (using Route Netlink)
	/// * or possibly `NetworkDeviceInputOutputControl.number_of_channels().combined / receive_only`; internally in Linux these values, which should be the same, are obtained via different driver functions.
	#[inline(always)]
	pub fn set_transmit_packet_steering_receive_queue_affinity(&self, sys_path: &SysPath, receive_queues_to_map_to: &QueueIdentifiers) -> io::Result<()>
	{
		assert_effective_user_id_is_root("write /sys/class/net/<network_interface_name>/queues/tx-<queue_identifier/xps_rxqs");
		
		let file_path = self.xps_rxqs_file_path(sys_path);
		
		if file_path.exists()
		{
			let mask = IntoBitMask(receive_queues_to_map_to);
			file_path.write_value(mask)
		}
		else
		{
			Ok(())
		}
	}
	
	/// Time interval to measure across.
	///
	/// Default is 1,000 milliseconds.
	#[inline(always)]
	pub fn byte_limits_hold_time(&self, sys_path: &SysPath) -> io::Result<Milliseconds>
	{
		self.byte_queue_limits_hold_time_file_path(sys_path).read_value()
	}
	
	/// Time interval to measure across.
	///
	/// Default is 1,000 milliseconds.
	#[inline(always)]
	pub fn set_byte_limits_hold_time(&self, sys_path: &SysPath, hold_time: Milliseconds) -> io::Result<()>
	{
		assert_effective_user_id_is_root("write /sys/class/net/<network_interface_name>/queues/tx-<queue_identifier/byte_queue_limits/hold_time");
		
		let file_path = self.byte_queue_limits_hold_time_file_path(sys_path);
		
		if file_path.exists()
		{
			file_path.write_value(hold_time)
		}
		else
		{
			Ok(())
		}
	}
	
	/// Number of bytes currently inflight.
	#[inline(always)]
	pub fn number_of_bytes_inflight(&self, sys_path: &SysPath) -> io::Result<u64>
	{
		self.byte_queue_limits_file_path(sys_path, "inflight").read_value()
	}
	
	/// Minimum value for current limit on bytes to transmit.
	///
	/// Default is 0 bytes.
	#[inline(always)]
	pub fn minimum_current_byte_limit(&self, sys_path: &SysPath) -> io::Result<Option<NonZeroU64>>
	{
		self.byte_queue_limits_limit_min_file_path(sys_path).read_value()
	}
	
	/// Minimum value for current limit on bytes to transmit.
	///
	/// Default is 0 bytes (off).
	#[inline(always)]
	pub fn set_minimum_current_byte_limit(&self, sys_path: &SysPath, minimum_current_byte_limit: Option<NonZeroU64>) -> io::Result<()>
	{
		assert_effective_user_id_is_root("write /sys/class/net/<network_interface_name>/queues/tx-<queue_identifier/byte_queue_limits/limit_min");
		
		let file_path = self.byte_queue_limits_limit_min_file_path(sys_path);
		
		if file_path.exists()
		{
			let value: u64 = unsafe { transmute(minimum_current_byte_limit) };
			file_path.write_value(UnpaddedDecimalInteger(value))
		}
		else
		{
			Ok(())
		}
	}
	
	/// Current limit on bytes to transmit.
	///
	/// Default is 0 bytes (off).
	#[inline(always)]
	pub fn current_byte_limit(&self, sys_path: &SysPath) -> io::Result<Option<NonZeroU64>>
	{
		self.byte_queue_limits_limit_file_path(sys_path).read_value()
	}
	
	/// Minimum value for current limit on bytes to transmit.
	///
	/// Default is 0 bytes (off).
	#[inline(always)]
	pub fn set_current_byte_limit(&self, sys_path: &SysPath, current_byte_limit: Option<NonZeroU64>) -> io::Result<()>
	{
		assert_effective_user_id_is_root("write /sys/class/net/<network_interface_name>/queues/tx-<queue_identifier/byte_queue_limits/limit");
		
		let file_path = self.byte_queue_limits_limit_file_path(sys_path);
		
		if file_path.exists()
		{
			let value: u64 = unsafe { transmute(current_byte_limit) };
			file_path.write_value(UnpaddedDecimalInteger(value))
		}
		else
		{
			Ok(())
		}
	}
	
	/// Maximum value for current limit on bytes to transmit.
	///
	/// Default is 1,879,048,192 bytes.
	#[inline(always)]
	pub fn maximum_current_byte_limit(&self, sys_path: &SysPath) -> io::Result<NonZeroU64>
	{
		self.byte_queue_limits_limit_max_file_path(sys_path).read_value()
	}
	
	/// Maximum value for current limit on bytes to transmit.
	///
	/// Default is 0 bytes.
	#[inline(always)]
	pub fn set_maximum_current_byte_limit(&self, sys_path: &SysPath, maximum_current_byte_limit: NonZeroU64) -> io::Result<()>
	{
		assert_effective_user_id_is_root("write /sys/class/net/<network_interface_name>/queues/tx-<queue_identifier/byte_queue_limits/limit_max");
		
		let file_path = self.byte_queue_limits_limit_max_file_path(sys_path);
		
		if file_path.exists()
		{
			file_path.write_value(UnpaddedDecimalInteger(maximum_current_byte_limit))
		}
		else
		{
			Ok(())
		}
	}
	
	#[inline(always)]
	fn tx_maxrate_file_path(&self, sys_path: &SysPath) -> PathBuf
	{
		self.file_path(sys_path, "tx_maxrate")
	}
	
	#[inline(always)]
	fn xps_cpus_file_path(&self, sys_path: &SysPath) -> PathBuf
	{
		self.file_path(sys_path, "xps_cpus")
	}
	
	#[inline(always)]
	fn xps_rxqs_file_path(&self, sys_path: &SysPath) -> PathBuf
	{
		self.file_path(sys_path, "xps_rxqs")
	}
	
	#[inline(always)]
	fn byte_queue_limits_limit_min_file_path(&self, sys_path: &SysPath) -> PathBuf
	{
		self.byte_queue_limits_file_path(sys_path, "limit_min")
	}
	
	#[inline(always)]
	fn byte_queue_limits_limit_max_file_path(&self, sys_path: &SysPath) -> PathBuf
	{
		self.byte_queue_limits_file_path(sys_path, "limit_max")
	}
	
	#[inline(always)]
	fn byte_queue_limits_limit_file_path(&self, sys_path: &SysPath) -> PathBuf
	{
		self.byte_queue_limits_file_path(sys_path, "limit")
	}
	
	#[inline(always)]
	fn byte_queue_limits_hold_time_file_path(&self, sys_path: &SysPath) -> PathBuf
	{
		self.byte_queue_limits_file_path(sys_path, "hold_time")
	}
	
	#[inline(always)]
	fn byte_queue_limits_file_path(&self, sys_path: &SysPath, file_name: &str) -> PathBuf
	{
		self.file_path(sys_path, "byte_queue_limits").append(file_name)
	}
}