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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
// 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.


/// Route netlink attribute header.
///
/// Followed by attribute value.
#[repr(C)]
pub struct rtattr<NAT: NetlinkAttributeType>
{
	rta_len: u16,
	
	rta_type: u16,

	marker: PhantomData<NAT>,
}

impl<NAT: NetlinkAttributeType> rtattr<NAT>
{
	#[inline(always)]
	pub(crate) fn type_(&self) -> (bool, bool, NAT)
	{
		(
			self.rta_type & (nlattr::NLA_F_NESTED) != 0,
			self.rta_type & (nlattr::NLA_F_NET_BYTEORDER) != 0,
			NAT::from(self.rta_type & nlattr::NLA_TYPE_MASK),
		)
	}
	
	#[inline(always)]
	pub(crate) fn get_attribute_value_nested(&self) -> &[u8]
	{
		let (is_nested, is_in_network_byte_order, _is) = self.type_();
		debug_assert!(is_nested, "Is not nested");
		debug_assert!(!is_in_network_byte_order, "Is in network byte order");
		
		self.attribute_value()
	}
	
	#[inline(always)]
	pub(super) fn get_attribute_value_queuing_discipline(&self) -> Result<QueuingDisciplineAlgorithm, String>
	{
		let asciiz_string = self.get_attribute_value_asciiz_string().map_err(|error| error.to_string())?;
		let bytes = asciiz_string.to_bytes();
		QueuingDisciplineAlgorithm::from_bytes(bytes).map_err(|error| format!("{}", error))
	}
	
	#[inline(always)]
	pub(super) fn get_attribute_value_network_interface_name(&self) -> Result<NetworkInterfaceName, String>
	{
		let asciiz_string = self.get_attribute_value_asciiz_string().map_err(|error| error.to_string())?;
		let bytes = asciiz_string.to_bytes();
		NetworkInterfaceName::from_bytes(bytes).map_err(|error| format!("{}", error))
	}
	
	#[inline(always)]
	pub(super) fn get_attribute_value_network_interface_alias(&self) -> Result<NetworkInterfaceAlias, String>
	{
		let asciiz_string = self.get_attribute_value_asciiz_string().map_err(|error| error.to_string())?;
		let bytes = asciiz_string.to_bytes();
		NetworkInterfaceAlias::from_bytes(bytes).map_err(|error| format!("{}", error))
	}
	
	#[inline(always)]
	pub(super) fn get_attribute_value_network_interface_alternative_name(&self) -> Result<NetworkInterfaceAlternativeName, String>
	{
		let asciiz_string = self.get_attribute_value_asciiz_string().map_err(|error| error.to_string())?;
		let bytes = asciiz_string.to_bytes();
		NetworkInterfaceAlternativeName::from_bytes(bytes).map_err(|error| format!("{}", error))
	}
	
	#[inline(always)]
	pub(super) fn get_attribute_value_queue_count(&self) -> Result<QueueCount, String>
	{
		let value = self.get_attribute_value_non_zero_u32()?;
		QueueCount::try_from(value).map_err(|error| error.to_string())
	}
	
	#[inline(always)]
	pub(super) fn get_attribute_value_maximum_transmission_unit(&self) -> Result<MaximumTransmissionUnitPayloadSize, String>
	{
		match self.get_attribute_value_u32()
		{
			Err(error) => Err(format!("{}", error)),
			
			Ok(value) => MaximumTransmissionUnitPayloadSize::try_from(value).map_err(|error| format!("Out of range: {}", error))
		}
	}
	
	#[inline(always)]
	pub(super) fn get_attribute_value_net_dev_group(&self) -> Result<NetworkDeviceGroup, TryFromSliceError>
	{
		self.get_attribute_value_u32().map(|value| NetworkDeviceGroup(value))
	}
	
	#[inline(always)]
	pub(super) fn get_attribute_value_net_namespace_identifier(&self) -> Result<NetNamespaceIdentifer, TryFromSliceError>
	{
		self.get_attribute_value_i32().map(NetNamespaceIdentifer)
	}
	
	#[inline(always)]
	pub(super) fn get_attribute_value_c_string(&self) -> Result<CString, FromBytesWithNulError>
	{
		self.get_attribute_value_asciiz_string().map(|c_str| c_str.to_owned())
	}
	
	#[inline(always)]
	fn get_attribute_value_asciiz_string(&self) -> Result<&CStr, FromBytesWithNulError>
	{
		let value = self.attribute_value();
		CStr::from_bytes_with_nul(value)
	}
	
	#[inline(always)]
	pub(super) fn get_attribute_value_network_interface_index(&self) -> Result<NetworkInterfaceIndex, String>
	{
		self.get_attribute_value_non_zero_u32().map(|non_zero_u32| NetworkInterfaceIndex::from(non_zero_u32))
	}
	
	#[inline(always)]
	pub(super) fn get_attribute_value_internet_protocol_address_lifetime(&self) -> Result<InternetProtocolAddressLifetime, TryFromSliceError>
	{
		self.get_attribute_value_u32().map(InternetProtocolAddressLifetime)
	}
	
	#[inline(always)]
	pub(super) fn get_attribute_value_milliseconds(&self) -> Result<Milliseconds, TryFromSliceError>
	{
		self.get_attribute_value_u32().map(Milliseconds)
	}
	
	#[inline(always)]
	pub(super) fn get_attribute_value_non_zero_u32(&self) -> Result<NonZeroU32, String>
	{
		let value = self.get_attribute_value_u32().map_err(|error| error.to_string())?;
		NonZeroU32::new(value).ok_or(format!("Was zero"))
	}
	
	#[inline(always)]
	pub(super) fn get_attribute_value_bool(&self) -> Result<bool, TryFromSliceError>
	{
		self.get_attribute_value_u8().map(|value| value != 0)
	}
	
	#[inline(always)]
	pub(super) fn get_attribute_value_u8(&self) -> Result<u8, TryFromSliceError>
	{
		let value: [u8; 1] = self.attribute_value().try_into()?;
		Ok(u8::from_ne_bytes(value))
	}
	
	#[inline(always)]
	pub(super) fn get_attribute_value_u32(&self) -> Result<u32, TryFromSliceError>
	{
		let value: [u8; 4] = self.attribute_value().try_into()?;
		Ok(u32::from_ne_bytes(value))
	}
	
	#[inline(always)]
	pub(super) fn get_attribute_value_i32(&self) -> Result<i32, TryFromSliceError>
	{
		let value: [u8; 4] = self.attribute_value().try_into()?;
		Ok(i32::from_ne_bytes(value))
	}
	
	#[inline(always)]
	pub(super) fn get_attribute_value_struct_cloned<T: Sized + Clone>(&self) -> Result<T, String>
	{
		self.get_attribute_value_struct::<T>().map(|reference| reference.clone())
	}
	
	#[inline(always)]
	pub(crate) fn socket_memory_information(&self) -> Result<HashMap<SK_MEMINFO_, u32>, String>
	{
		let slice = self.get_attribute_values_u32()?;
		let length = min(SK_MEMINFO_::COUNT, slice.len());
		let mut socket_memory_information = HashMap::with_capacity(length);
		for index in 0 ..length
		{
			let key = unsafe { transmute(index) };
			socket_memory_information.insert(key, slice.get_unchecked_value_safe(index));
		}
		Ok(socket_memory_information)
	}
	
	#[inline(always)]
	pub(crate) fn get_attribute_values_u32(&self) -> Result<&[u32], String>
	{
		let attribute_value = self.attribute_value();
		let length = attribute_value.len();
		const SizeOfU32: usize = size_of::<u32>();
		if length % SizeOfU32 == 0
		{
			Ok(unsafe { from_raw_parts(attribute_value.as_ptr() as *const u32, length / SizeOfU32) })
		}
		else
		{
			Err(format!("Not a multiple of size of u32"))
		}
	}
	
	#[inline(always)]
	pub(crate) fn get_attribute_value_struct<T: Sized>(&self) -> Result<&T, String>
	{
		let attribute_value = self.attribute_value();
		let length = attribute_value.len();
		let known_size = size_of::<T>();
		// Can be greater than if Linux has subsequently added more fields.
		if length >= known_size
		{
			Ok(unsafe { & * (attribute_value.as_ptr() as *const T) })
		}
		else
		{
			Err(format!("Invalid length {} for T does not match size {} ", length, known_size))
		}
	}
	
	#[inline(always)]
	fn attribute_value(&self) -> &[u8]
	{
		unsafe { from_raw_parts(self.RTA_DATA(), self.RTA_PAYLOAD()) }
	}
	
	const RTA_ALIGNTO: usize = 4;
	
	const RTA_HDRLEN: usize = size_of::<Self>();
	
	#[inline(always)]
	const fn RTA_ALIGN(length: usize) -> usize
	{
		(length + Self::RTA_ALIGNTO - 1) & !(Self::RTA_ALIGNTO - 1)
	}
	
	#[inline(always)]
	const fn RTA_LENGTH(length: usize) -> usize
	{
		Self::RTA_ALIGN(Self::RTA_HDRLEN) + length
	}
	
	/// Pointer to start of payload.
	#[inline(always)]
	fn RTA_DATA(&self) -> *const u8
	{
		unsafe { (self as *const Self as *const u8).add(Self::RTA_LENGTH(0)) }
	}
	
	/// Size of payload.
	#[inline(always)]
	fn RTA_PAYLOAD(&self) -> usize
	{
		self.length() - Self::RTA_LENGTH(0)
	}
	
	/// `RTA_NEXT` in `musl`.
	#[inline(always)]
	pub(crate) fn next(&self) -> *const Self
	{
		let length = Self::RTA_ALIGN(self.length());
		unsafe { (self as *const Self as *const u8).add(length) as *const Self }
	}
	
	/// `RTA_OK` in `musl`.
	#[inline(always)]
	pub(crate) const fn ok(this: *const Self, end: *const Self) -> bool
	{
		(unsafe { (end as usize) - (this as usize) }) >= Self::RTA_HDRLEN
	}
	
	#[inline(always)]
	fn length(&self) -> usize
	{
		self.rta_len as usize
	}
	
	#[inline(always)]
	fn debug_assert_is(&self, is: NAT)
	{
		debug_assert_eq!(self.type_(), (false, false, is))
	}
	
	#[inline(always)]
	fn debug_assert_is_not_nested_and_is_in_native_byte_order(&self)
	{
		let (is_nested, is_in_network_byte_order, _is) = self.type_();
		debug_assert!(!is_nested, "Is nested");
		debug_assert!(!is_in_network_byte_order, "Is network byte order")
	}
}

/// Link (`IFLA_*`).
///
/// See Linux header `if_link.h`.
///
/// See man 7 rtnetlink.
impl rtattr<IFLA>
{
	#[inline(always)]
	pub(super) fn get_attribute_value_operational_status(&self) -> Result<IF_OPER, TryFromSliceError>
	{
		self.debug_assert_is(IFLA::IFLA_OPERSTATE);
		
		Ok(unsafe { transmute(self.get_attribute_value_u8()?) })
	}
	
	#[inline(always)]
	pub(super) fn get_attribute_value_link_mode(&self) -> Result<IF_LINK_MODE, TryFromSliceError>
	{
		self.debug_assert_is(IFLA::IFLA_LINKMODE);
		
		Ok(unsafe { transmute(self.get_attribute_value_u8()?) })
	}
	
	#[inline(always)]
	pub(super) fn get_attribute_value_physical_identifier(&self) -> Result<PhysicalIdentifier, PhysicalIdentifierFromBytesError>
	{
		self.debug_assert_is_not_nested_and_is_in_native_byte_order();
		
		PhysicalIdentifier::try_from(self.attribute_value())
	}
	
	#[inline(always)]
	pub(super) fn get_attribute_value_hardware_address(&self) -> &[u8]
	{
		self.debug_assert_is_not_nested_and_is_in_native_byte_order();
		
		use self::IFLA::*;
		debug_assert!(matches!(self.type_().2, IFLA_ADDRESS | IFLA_BROADCAST | IFLA_PERM_ADDRESS), "self.type_().2 {:?} is not one of IFLA_ADDRESS, IFLA_BROADCAST or IFLA_PERM_ADDRESS", self.type_().2);
		
		self.attribute_value()
	}
}

/// Address (`IFA_*`).
///
/// See Linux header `if_addr.h`.
///
/// See man 7 rtnetlink.
impl rtattr<IFA>
{
	#[inline(always)]
	pub(super) fn get_attribute_value_extended_interface_flags(&self) -> Result<ExtendedInterfaceFlags, TryFromSliceError>
	{
		self.debug_assert_is(IFA::IFA_FLAGS);
		
		Ok(ExtendedInterfaceFlags::from_bits_truncate(self.get_attribute_value_u32()?))
	}
	
	#[inline(always)]
	pub(super) fn get_attribute_value_raw_protocol_address(&self) -> &[u8]
	{
		self.debug_assert_is_not_nested_and_is_in_native_byte_order();
		
		use self::IFA::*;
		debug_assert!(matches!(self.type_().2, IFA_ADDRESS | IFA_LOCAL | IFA_BROADCAST | IFA_ANYCAST | IFA_MULTICAST), "self.type_().2 {:?} is not one of IFA_ADDRESS, IFA_LOCAL, IFA_BROADCAST, IFA_ANYCAST or IFA_MULTICAST", self.type_().2);
		
		self.attribute_value()
	}
}

impl rtattr<IFLA_XDP>
{
	#[inline(always)]
	pub(super) fn get_attribute_value_attached(&self) -> Result<XDP_ATTACHED, TryFromSliceError>
	{
		self.debug_assert_is(IFLA_XDP::IFLA_XDP_ATTACHED);
		
		Ok(unsafe { transmute(self.get_attribute_value_u8()?) })
	}
	
	#[inline(always)]
	pub(super) fn get_attribute_value_program_identifier(&self) -> Result<ExtendedBpfProgramIdentifier, TryFromSliceError>
	{
		self.debug_assert_is_not_nested_and_is_in_native_byte_order();
		
		use self::IFLA_XDP::*;
		debug_assert!(matches!(self.type_().2, IFLA_XDP_PROG_ID | IFLA_XDP_SKB_PROG_ID | IFLA_XDP_DRV_PROG_ID | IFLA_XDP_HW_PROG_ID), "self.type_().2 {:?} is not one of IFLA_XDP_PROG_ID, IFLA_XDP_SKB_PROG_ID, IFLA_XDP_DRV_PROG_ID or IFLA_XDP_HW_PROG_ID", self.type_().2);
		
		self.get_attribute_value_u32().map(|value| ExtendedBpfProgramIdentifier::from(value))
	}
}

impl rtattr<XDP_DIAG>
{
	#[inline(always)]
	pub(crate) fn get_attribute_value_uid(&self) -> Result<UserIdentifier, TryFromSliceError>
	{
		self.debug_assert_is(XDP_DIAG::XDP_DIAG_UID);
		
		self.get_attribute_value_u32().map(UserIdentifier::from)
	}
	
	#[inline(always)]
	pub(crate) fn get_attribute_value_ring_number_of_descriptors(&self) -> Result<u32, String>
	{
		self.get_attribute_value_struct::<xdp_diag_ring>().map(|ring| ring.entries)
	}
}

impl rtattr<IFLA_INET6>
{
	#[inline(always)]
	pub(crate) fn get_inet6_statistics(&self) -> Result<Box<[u64]>, String>
	{
		use self::IFLA_INET6::*;
		debug_assert!(matches!(self.type_().2, IFLA_INET6_STATS | IFLA_INET6_ICMP6STATS), "self.type_().2 {:?} is not one of IFLA_INET6_STATS or IFLA_INET6_ICMP6STATS", self.type_().2);
		
		// Has a header (number of statistics including header) and a zero-padded trailer.
		let binary_data = self.attribute_value();
		let length = binary_data.len();
		
		type Statistic = u64;
		const LengthMultiple: usize = size_of::<Statistic>();
		if unlikely!(length < LengthMultiple)
		{
			return Err(format!("binary_data length `{}` is less than `{}`", length, LengthMultiple))
		}
		
		if unlikely!(length % LengthMultiple != 0)
		{
			return Err(format!("binary_data length `{}` is not a multiple of `{}`", length, LengthMultiple))
		}
		
		let header = binary_data.as_ptr() as *const Statistic;
		let number_of_statistics_including_this_one = unsafe { *header };
		let number_of_statistics = number_of_statistics_including_this_one - 1;
		
		let statistics = unsafe { header.add(1) };
		Ok(unsafe { from_raw_parts(statistics, number_of_statistics as usize) }.to_vec().into_boxed_slice())
	}
	
	#[inline(always)]
	pub(super) fn get_attribute_value_address_generation_mode(&self) -> Result<in6_addr_gen_mode, TryFromSliceError>
	{
		self.debug_assert_is(IFLA_INET6::IFLA_INET6_ADDR_GEN_MODE);
		
		Ok(unsafe { transmute(self.get_attribute_value_u8()?) })
	}
}

impl rtattr<DEVCONF>
{
	#[inline(always)]
	pub(super) fn get_attribute_value_hmac_policy(&self) -> Result<HmacPolicyForSrEnabledPackets, TryFromSliceError>
	{
		self.debug_assert_is(DEVCONF::DEVCONF_SEG6_REQUIRE_HMAC);
		
		Ok(unsafe { transmute(self.get_attribute_value_i32()?) })
	}
}