dpdk-core 0.1.5

A wrapper around DPDK's core framework
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
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
// This file is part of dpdk. 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/dpdk/master/COPYRIGHT. No part of dpdk, including this file, may be copied, modified, propagated, or distributed except according to the terms contained in the COPYRIGHT file.
// Copyright © 2016-2018 The developers of dpdk. See the COPYRIGHT file in the top-level directory of this distribution and at https://raw.githubusercontent.com/lemonrock/dpdk/master/COPYRIGHT.


/// Logical core numbers start at zero.
///
/// Logical core numbers are not necessarily contiguous but usually are.
///
/// To get a contiguous range, use `self.index()`.
///
/// A logical core is equivalent to a hyper thread.
///
/// Not all logical cores may have been assigned to be used by DPDK, and, of those that have, they may have one of these roles:-
///
/// * Master
/// * Slave
///
/// And also:-
///
/// * Normal
/// * Service
///
/// Only one core of all cores in a process can be a Master.
///
/// A logical core belongs to a NUMA node.
///
/// DPDK 18.02 defaults to a maximum of 128 logical cores.
///
/// To iterate over known logical cores, use either `AllLogicalCoreIterator` or `SlaveLogicalCoreIterator`.
///
/// To find the logical core the running code is currently executing on, use `LogicalCoreChoice::current_logical_core()`.
#[derive(Default, Debug, Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash)]
#[derive(Deserialize, Serialize)]
pub struct LogicalCore(u16);

impl Display for LogicalCore
{
	#[inline(always)]
	fn fmt(&self, f: &mut Formatter) -> fmt::Result
	{
		write!(f, "{}", self.0)
	}
}

impl Into<u16> for LogicalCore
{
	#[inline(always)]
	fn into(self) -> u16
	{
		self.0 as u16
	}
}

impl Into<u32> for LogicalCore
{
	#[inline(always)]
	fn into(self) -> u32
	{
		self.0 as u32
	}
}

impl Into<u64> for LogicalCore
{
	#[inline(always)]
	fn into(self) -> u64
	{
		self.0 as u64
	}
}

impl Into<usize> for LogicalCore
{
	#[inline(always)]
	fn into(self) -> usize
	{
		self.0 as usize
	}
}

impl Into<isize> for LogicalCore
{
	#[inline(always)]
	fn into(self) -> isize
	{
		self.0 as isize
	}
}

impl LogicalCore
{
	/// Maximum number of `LogicalCore`s.
	pub const Maximum: usize = RTE_MAX_LCORE;
	
	/// Creates a new instance.
	///
	/// Only valid after `rte_eal_init()` called.
	///
	/// Returns an error if this logical core is not for use by DPDK EAL.
	///
	/// Panics if this logical core equals or exceeds `Maximum`.
	#[inline(always)]
	pub fn from_u16(value: u16) -> Result<Self, ()>
	{
		if unlikely!(Self::is_invalid(value))
		{
			Err(())
		}
		else
		{
			Ok(LogicalCore(value))
		}
	}
	
	#[inline(always)]
	pub(crate) fn is_invalid(value: u16) -> bool
	{
		debug_assert!((value as usize) < Self::Maximum, "value '{}' exceeds Self::Maximum '{}'", value, Self::Maximum);
		
		Self::logical_core_global_configuration()[value as usize].core_index < 0
	}
	
	/// Current logical core cpu set.
	///
	/// From a DPDK thread-local static.
	#[inline(always)]
	pub fn current_logical_core_cpu_set() -> &'static mut rte_cpuset_t
	{
		unsafe { &mut per_lcore__cpuset }
	}
	
	/// Current logical core `errno`.
	///
	/// From a DPDK thread-local static.
	#[inline(always)]
	pub fn current_logical_core_error_number() -> i32
	{
		unsafe { per_lcore__rte_errno }
	}
	
	/// Current logical core thread affinity.
	///
	/// Ordinarily logical cores are bound 1:1 to one thread to a hyper threaded CPU core.
	#[inline(always)]
	pub fn current_logical_core_thread_affinity() -> rte_cpuset_t
	{
		let mut cpu_set = unsafe { uninitialized() };
		unsafe { rte_thread_get_affinity(&mut cpu_set) }
		cpu_set
	}
	
	/// Override current logical core thread affinity.
	///
	/// Ordinarily logical cores are bound 1:1 to one thread to a hyper threaded CPU core. This binding is set up be `rte_eal_init`.
	#[inline(always)]
	pub fn override_current_logical_core_thread_affinity(cpu_set: &mut rte_cpuset_t)
	{
		if unlikely!(rte_thread_set_affinity(cpu_set) != 0)
		{
			panic!("Could not set current logical core thread affinity");
		}
	}
	
	/// Gets the number of logical cores.
	#[inline(always)]
	pub fn number_of_logical_cores() -> usize
	{
		DpdkProcess::global_configuration().lcore_count as usize
	}
	
	/// Gets the number of logical cores.
	///
	/// Should be equal to or (usually) smaller than `Self::number_of_logical_cores()`.
	#[inline(always)]
	pub fn number_of_service_cores() -> usize
	{
		DpdkProcess::global_configuration().service_lcore_count as usize
	}
	
	/// Gets the master logical core.
	#[inline(always)]
	pub fn master() -> Self
	{
		let master = DpdkProcess::global_configuration().master_lcore;
		debug_assert!(master <= (::std::u16::MAX as u32), "master '{}' is larger than ::std::u16::MAX '{}'", master, ::std::u16::MAX);
		LogicalCore::from_u16(master as u16).unwrap()
	}
	
	/// The number of service cores.
	#[inline(always)]
	pub fn number_of_logical_cores_used_as_service_cores() -> usize
	{
		let result = unsafe { rte_service_lcore_count() };
		if likely!(result >= 0)
		{
			result as usize
		}
		else
		{
			panic!("rte_service_lcore_count failed")
		}
	}
	
	/// List of all current logical cores used as service cores.
	///
	/// Result is out-of-date if `add_to_logical_cores_used_as_service_cores()` or `remove_from_logical_cores_used_as_service_cores()` is called.
	///
	/// Size of result is the same as `self.number_of_logical_core_used_as_service_cores()`.
	#[inline(always)]
	pub fn list_service_logical_cores() -> Vec<LogicalCore>
	{
		let mut array: [u32; LogicalCore::Maximum] = unsafe { uninitialized() };
		
		let result = unsafe { rte_service_lcore_list(array.as_mut_ptr(), array.len() as u32) };
		if likely!(result >= 0)
		{
			let count = result as usize;
			let mut list = Vec::with_capacity(count);
			let mut index = 0;
			while index < count
			{
				list.push(LogicalCore::from_u16(array[index] as u16).unwrap());
				index += 1;
			}
			list
		}
		else
		{
			panic!("rte_service_lcore_list failed");
		}
	}
	
	/// Start power management.
	#[inline(always)]
	pub fn start_power_management(self) -> Result<LogicalCorePowerManagement, ()>
	{
		LogicalCorePowerManagement::start(self)
	}
	
	/// Gets if the logical core role is normal.
	#[inline(always)]
	pub fn is_role_normal(self) -> bool
	{
		self.logical_core_role() == rte_lcore_role_t::ROLE_RTE
	}
	
	/// Gets if the logical core role is service.
	///
	/// Some DPDK functionality (rte_timer threads) will only run on logical cores which are in the service role.
	#[inline(always)]
	pub fn is_role_service(self) -> bool
	{
		self.logical_core_role() == rte_lcore_role_t::ROLE_SERVICE
	}
	
	/// Gets the number of services running on this core if it is a service core.
	#[inline(always)]
	pub fn number_of_services_running_on_this_service_core(self) -> Option<usize>
	{
		if self.is_role_service()
		{
			let result = unsafe { rte_service_lcore_count_services(self.into()) };
			if likely!(result >= 0)
			{
				Some(result as usize)
			}
			else
			{
				panic!("rte_service_lcore_count_services() failed")
			}
		}
		else
		{
			None
		}
	}
	
	/// This core must be:-
	///
	/// * In the wait state  (error returned if not); use `self.stop_service_core()`;
	/// * Not already added (error returned if not)
	#[inline(always)]
	pub fn add_to_logical_cores_used_as_service_cores(self) -> Result<(), ()>
	{
		let result = unsafe { rte_service_lcore_add(self.into()) };
		if likely!(result == 0)
		{
			Ok(())
		}
		else
		{
			match result
			{
				NegativeE::EBUSY | NegativeE::EALREADY => Err(()),
				NegativeE::EINVAL => panic!("EINVAL from rte_service_lcore_add()"),
				unexpected @ _ => panic!("Unexpected '{}' from rte_service_lcore_add()", unexpected),
			}
		}
	}
	
	/// This core must be:-
	///
	/// * In the wait state  (error returned if not); use `self.stop_service_core()`;
	/// * Already added (error returned if not)
	/// * A service core (panics if not)
	#[inline(always)]
	pub fn remove_from_logical_cores_used_as_service_cores(self) -> Result<(), ()>
	{
		debug_assert!(self.is_role_service(), "Is not a service core");
		
		let result = unsafe { rte_service_lcore_del(self.into()) };
		if likely!(result == 0)
		{
			Ok(())
		}
		else
		{
			match result
			{
				NegativeE::EBUSY => Err(()),
				NegativeE::EINVAL => panic!("EINVAL from rte_service_lcore_del()"),
				unexpected @ _ => panic!("Unexpected '{}' from rte_service_lcore_del()", unexpected),
			}
		}
	}
	
	/// Panics if not a service core.
	pub fn start_service_core(self) -> Result<(), ()>
	{
		debug_assert!(self.is_role_service(), "Is not a service core");
		
		let result = unsafe { rte_service_lcore_start(self.into()) };
		if likely!(result == 0)
		{
			Ok(())
		}
		else
		{
			match result
			{
				NegativeE::EINVAL => panic!("EINVAL from rte_service_lcore_start()"),
				unexpected @ _ => panic!("Unexpected '{}' from rte_service_lcore_start()", unexpected),
			}
		}
	}
	
	/// Panics if not a service core.
	///
	/// Returns an error if already stopped or is not in wait state (stop all services on this logical core first).
	pub fn stop_service_core(self) -> Result<(), ()>
	{
		debug_assert!(self.is_role_service(), "Is not a service core");
		
		let result = unsafe { rte_service_lcore_stop(self.into()) };
		if likely!(result == 0)
		{
			Ok(())
		}
		else
		{
			match result
			{
				NegativeE::EBUSY | NegativeE::EALREADY => Err(()),
				NegativeE::EINVAL => panic!("EINVAL from rte_service_lcore_stop()"),
				unexpected @ _ => panic!("Unexpected '{}' from rte_service_lcore_stop()", unexpected),
			}
		}
	}
	
	/// Contiguous index.
	#[inline(always)]
	pub fn index(self) -> usize
	{
		let core_index = self.logical_core_configuration().core_index;
		debug_assert!(core_index >= 0, "logical core is not one configured for use with DPDK");
		core_index as usize
	}
	
	/// NUMA node of this core.
	#[inline(always)]
	pub fn numa_node(self) -> NumaNode
	{
		let socket_id = self.logical_core_configuration().socket_id;
		NumaNode::from_u32(socket_id)
	}
	
	/// POSIX thread associated with this logical core.
	///
	/// Ordinarily logical cores are bound 1:1 to one thread to a hyper threaded CPU core.
	#[inline(always)]
	pub fn thread(self) -> pthread_t
	{
		self.logical_core_configuration().thread_id
	}
	
	/// Set POSIX thread name for this logical core.
	///
	/// Ordinarily logical cores are bound 1:1 to one thread to a hyper threaded CPU core.
	///
	/// Many internal DPDK functions will set this, so it may not always be appropriate to override it.
	///
	/// Panics if can not be set.
	#[inline(always)]
	pub fn set_thread_name(self, name: &CStr)
	{
		if unlikely!(rte_thread_setname(self.thread(), name.as_ptr()) != 0)
		{
			panic!("Could not set thread name for logical core");
		}
	}
	
	/// An iterator over all logical cores.
	#[inline(always)]
	pub fn all_logical_cores() -> AllLogicalCoreIterator
	{
		Default::default()
	}
	
	/// An iterator over slave logical cores.
	#[inline(always)]
	pub fn slave_logical_cores() -> SlaveLogicalCoreIterator
	{
		Default::default()
	}
	
	/// An iterator over slave logical cores with service cores omitted.
	#[inline(always)]
	pub fn slave_logical_cores_without_service_cores() -> impl Iterator<Item=Self>
	{
		Self::slave_logical_cores().filter(|slave_logical_core| !slave_logical_core.is_role_service())
	}
	
	/// Gets if the logical core execution state.
	///
	/// **WARNING:** Can only be called by code currently running on the master logical core.
	#[inline(always)]
	pub fn execution_state(self) -> rte_lcore_state_t
	{
		Self::debug_assert_code_is_currently_running_on_the_master_logical_core();
		
		self.logical_core_configuration().state
	}
	
	/// Gets if the logical core execution state.
	///
	/// **WARNING:** Can only be called by code currently running on the master logical core.
	///
	/// In debug mode, will also panic if `self` is the master core.
	///
	/// Execution will fail and an error is returned if ths slave logical core is not in the wait state.
	#[inline(always)]
	pub fn execute_code_on_slave<F: SlaveLogicalCoreFunction>(self, function_to_execute_on_slave: Box<F>) -> Result<(), ()>
	{
		Self::debug_assert_code_is_currently_running_on_the_master_logical_core();
		debug_assert_ne!(&self, &Self::master(), "Can not wait for a slave when self is master logical core");
		
		unsafe extern "C" fn execute<F: SlaveLogicalCoreFunction>(arg1: *mut c_void) -> i32
		{
			debug_assert!(arg1.is_not_null(), "arg1 is null");
			
			let mut this = Box::from_raw(arg1 as *mut F);
			
			this.execute();
			
			0
		}
		
		let arg1 = Box::into_raw(function_to_execute_on_slave) as *mut c_void;
		
		match unsafe { rte_eal_remote_launch(execute::<F>, arg1, self.into()) }
		{
			0 => Ok(()),
			
			NegativeE::EBUSY =>
			{
				drop(unsafe { Box::from_raw(arg1) });
				
				Err(())
			}
			
			invalid @ _ => panic!("Invalid result from rte_eal_remote_launch '{}'", invalid),
		}
	}
	
	/// Blocks until all slave logical cores enter the wait state.
	///
	/// **WARNING:** Can only be called by code currently running on the master logical core.
	///
	/// This will also update slaves that are in the finished state to the wait state.
	///
	/// Uses a busy-loop with a CPU pause.
	///
	/// Note that we ignore the return values from the result of executing on the slave logical cores.
	#[inline(always)]
	pub fn block_until_all_slaves_are_in_the_wait_state()
	{
		Self::debug_assert_code_is_currently_running_on_the_master_logical_core();
		
		for slave_logical_core in Self::slave_logical_cores()
		{
			slave_logical_core.block_until_this_slave_is_in_the_wait_state()
		}
	}
	
	/// Blocks until this slave logical core enters the wait state.
	///
	/// **WARNING:** Can only be called by code currently running on the master logical core.
	///
	/// In debug mode, will also panic if `self` is the master core.
	///
	/// This will also update slaves that are in the finished state to the wait state.
	///
	/// Use a busy-loop with a CPU pause.
	///
	/// Note that we ignore the return value from the result of executing on a slave logical core.
	pub fn block_until_this_slave_is_in_the_wait_state(self)
	{
		Self::debug_assert_code_is_currently_running_on_the_master_logical_core();
		debug_assert_ne!(&self, &Self::master(), "Can not wait for a slave when self is master logical core");
		
		unsafe { rte_eal_wait_lcore(self.into()) };
	}
	
	#[inline(always)]
	fn debug_assert_code_is_currently_running_on_the_master_logical_core()
	{
		debug_assert_eq!(Self::master(), LogicalCoreChoice::current_logical_core().expect("current core is not a logical core"), "Code must be running on the master logical core to use this functionality");
	}
	
	#[inline(always)]
	fn logical_core_role(self) -> rte_lcore_role_t
	{
		let index: usize = self.into();
		DpdkProcess::global_configuration().lcore_role[index]
	}
	
	/// Global logical core configuration.
	///
	/// Only valid after `rte_eal_init()` called.
	///
	/// From a DPDK global static.
	#[inline(always)]
	fn logical_core_configuration(self) -> &'static mut lcore_config
	{
		let index: usize = self.into();
		&mut Self::logical_core_global_configuration()[index]
	}
	
	/// Global logical core configuration.
	///
	/// Only valid after `rte_eal_init()` called.
	///
	/// From a DPDK global static.
	#[inline(always)]
	fn logical_core_global_configuration() -> &'static mut [lcore_config; Self::Maximum]
	{
		unsafe { &mut lcore_config }
	}
}