utils - Various Utilities
Various utility classes and functions.
These are used in multiple modules across the framework. They’re here because they provide some non-specific functionality, greatly simplify imports or just don’t fit elsewhere.
- REGEX_FOR_PCI_ADDRESS
The regex representing a PCI address, e.g.
0000:00:08.0
.- Type:
str
- expand_range(range_str: str) list[int]
Process range_str into a list of integers.
There are two possible formats of range_str:
n
- a single integer,n-m
- a range of integers.
The returned range includes both
n
andm
. Empty string returns an empty list.- Parameters:
range_str (str) – The range to expand.
- Returns:
All the numbers from the range.
- Return type:
list[int]
- get_packet_summaries(packets: list[scapy.packet.Packet]) str
Format a string summary from packets.
- Parameters:
packets (list[scapy.packet.Packet]) – The packets to format.
- Returns:
The summary of packets.
- Return type:
str
- class StrEnum
Bases:
Enum
Enum with members stored as strings.
- class MesonArgs
Bases:
object
Aggregate the arguments needed to build DPDK.
- __init__(default_library: str | None = None, **dpdk_args: str | bool)
Initialize the meson arguments.
- Parameters:
default_library (str | None) – The default library type, Meson supports
shared
,static
andboth
. Defaults toNone
, in which case the argument won’t be used.dpdk_args (str | bool) – The arguments found in
meson_options.txt
in root DPDK directory. Do not use-D
with them.
Example
meson_args = MesonArgs(enable_kmods=True).
- class TarCompressionFormat
Bases:
StrEnum
Compression formats that tar can use.
Enum names are the shell compression commands and Enum values are the associated file extensions.
The ‘none’ member represents no compression, only archiving with tar. Its value is set to ‘tar’ to indicate that the file is an uncompressed tar archive.
- property extension
Return the extension associated with the compression format.
If the compression format is ‘none’, the extension will be in the format ‘tar’. For other compression formats, the extension will be in the format ‘tar.{compression format}’.
- convert_to_list_of_string(value: Any | list[Any]) list[str]
Convert the input to the list of strings.
- create_tarball(dir_path: Path, compress_format: TarCompressionFormat = TarCompressionFormat.none, exclude: Any | list[Any] | None = None) Path
Create a tarball from the contents of the specified directory.
This method creates a tarball containing all files and directories within dir_path. The tarball will be saved in the directory of dir_path and will be named based on dir_path.
- Parameters:
dir_path (Path) – The directory path.
compress_format (TarCompressionFormat) – The compression format to use. Defaults to no compression.
exclude (Any | list[Any] | None) – Patterns for files or directories to exclude from the tarball. These patterns are used with fnmatch.fnmatch to filter out files.
- Returns:
The path to the created tarball.
- Return type:
Path
- extract_tarball(tar_path: str | pathlib.Path)
Extract the contents of a tarball.
The tarball will be extracted in the same path as tar_path parent path.
- Parameters:
tar_path (str | pathlib.Path) – The path to the tarball file to extract.
- class PacketProtocols
Bases:
Flag
Flag specifying which protocols to use for packet generation.
- IP = 1
- TCP = 3
- UDP = 5
- ALL = 7
- generate_random_packets(number_of: int, payload_size: int = 1500, protocols: PacketProtocols = PacketProtocols.ALL, ports_range: range = range(1024, 49152), mtu: int = 1500) list[scapy.packet.Packet]
Generate a number of random packets.
The payload of the packets will consist of random bytes. If payload_size is too big, then the maximum payload size allowed for the specific packet type is used. The size is calculated based on the specified mtu, therefore it is essential that mtu is set correctly to match the MTU of the port that will send out the generated packets.
If protocols has any L4 protocol enabled then all the packets are generated with any of the specified L4 protocols chosen at random. If only
IP
is set, then only L3 packets are generated.If L4 packets will be generated, then the TCP/UDP ports to be used will be chosen at random from ports_range.
- Parameters:
number_of (int) – The number of packets to generate.
payload_size (int) – The packet payload size to generate, capped based on mtu.
protocols (PacketProtocols) – The protocols to use for the generated packets.
ports_range (range) – The range of L4 port numbers to use. Used only if protocols has L4 protocols.
mtu (int) – The MTU of the NIC port that will send out the generated packets.
- Raises:
InternalError – If the payload_size is invalid.
- Returns:
A list containing the randomly generated packets.
- Return type:
list[scapy.packet.Packet]
- class MultiInheritanceBaseClass
Bases:
object
A base class for classes utilizing multiple inheritance.
This class enables it’s subclasses to support both single and multiple inheritance by acting as a stopping point in the tree of calls to the constructors of superclasses. This class is able to exist at the end of the Method Resolution Order (MRO) so that subclasses can call
super.__init__()
without repercussion.- __init__(*args, **kwargs) None
Call the init method of
object
.
- to_pascal_case(text: str) str
Convert text from snake_case to PascalCase.