From: Luca Vizzarro <Luca.Vizzarro@arm.com>
To: dev@dpdk.org
Cc: "Jeremy Spewock" <jspewock@iol.unh.edu>,
"Juraj Linkeš" <juraj.linkes@pantheon.tech>,
"Honnappa Nagarahalli" <honnappa.nagarahalli@arm.com>,
"Luca Vizzarro" <luca.vizzarro@arm.com>,
"Paul Szczpanek" <paul.szczepanek@arm.com>,
"Alex Chapman" <alex.chapman@arm.com>
Subject: [PATCH 3/5] dts: add random packet generator
Date: Tue, 6 Aug 2024 13:14:14 +0100 [thread overview]
Message-ID: <20240806121417.2567708-4-Luca.Vizzarro@arm.com> (raw)
In-Reply-To: <20240806121417.2567708-1-Luca.Vizzarro@arm.com>
From: Luca Vizzarro <luca.vizzarro@arm.com>
Add a basic utility that can create random L3 and L4 packets with random
payloads and port numbers (if L4).
Signed-off-by: Luca Vizzarro <luca.vizzarro@arm.com>
Reviewed-by: Paul Szczpanek <paul.szczepanek@arm.com>
Reviewed-by: Alex Chapman <alex.chapman@arm.com>
---
dts/framework/utils.py | 79 ++++++++++++++++++++++++++++++++++++++++--
1 file changed, 77 insertions(+), 2 deletions(-)
diff --git a/dts/framework/utils.py b/dts/framework/utils.py
index 6b5d5a805f..c768dd0c99 100644
--- a/dts/framework/utils.py
+++ b/dts/framework/utils.py
@@ -17,14 +17,16 @@
import atexit
import json
import os
+import random
import subprocess
-from enum import Enum
+from enum import Enum, Flag
from pathlib import Path
from subprocess import SubprocessError
+from scapy.layers.inet import IP, TCP, UDP, Ether # type: ignore[import-untyped]
from scapy.packet import Packet # type: ignore[import-untyped]
-from .exception import ConfigurationError
+from .exception import ConfigurationError, InternalError
REGEX_FOR_PCI_ADDRESS: str = "/[0-9a-fA-F]{4}:[0-9a-fA-F]{2}:[0-9a-fA-F]{2}.[0-9]{1}/"
@@ -244,3 +246,76 @@ def _delete_tarball(self) -> None:
def __fspath__(self) -> str:
"""The os.PathLike protocol implementation."""
return str(self._tarball_path)
+
+
+class PacketProtocols(Flag):
+ """Flag specifying which protocols to use for packet generation."""
+
+ #:
+ IP = 1
+ #:
+ TCP = 2 | IP
+ #:
+ UDP = 4 | IP
+ #:
+ ALL = TCP | UDP
+
+
+def generate_random_packets(
+ number_of: int,
+ payload_size: int = 1500,
+ protocols: PacketProtocols = PacketProtocols.ALL,
+ ports_range: range = range(1024, 49152),
+ mtu: int = 1500,
+) -> list[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 :attr:`~PacketProtocols.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`.
+
+ Args:
+ number_of: The number of packets to generate.
+ payload_size: The packet payload size to generate, capped based on `mtu`.
+ protocols: The protocols to use for the generated packets.
+ ports_range: The range of L4 port numbers to use. Used only if `protocols` has L4 protocols.
+ mtu: 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.
+ """
+ if payload_size < 0:
+ raise InternalError(f"An invalid payload_size of {payload_size} was given.")
+
+ l4_factories = []
+ if protocols & PacketProtocols.TCP:
+ l4_factories.append(TCP)
+ if protocols & PacketProtocols.UDP:
+ l4_factories.append(UDP)
+
+ def _make_packet() -> Packet:
+ packet = Ether()
+
+ if protocols & PacketProtocols.IP:
+ packet /= IP()
+
+ if len(l4_factories) > 0:
+ src_port, dst_port = random.choices(ports_range, k=2)
+ packet /= random.choice(l4_factories)(sport=src_port, dport=dst_port)
+
+ max_payload_size = mtu - len(packet)
+ usable_payload_size = payload_size if payload_size < max_payload_size else max_payload_size
+ return packet / random.randbytes(usable_payload_size)
+
+ return [_make_packet() for _ in range(number_of)]
--
2.34.1
next prev parent reply other threads:[~2024-08-06 12:16 UTC|newest]
Thread overview: 47+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-08-06 12:14 [PATCH 0/4] dts: add pktgen and testpmd changes Luca Vizzarro
2024-08-06 12:14 ` [PATCH 1/5] dts: add ability to send/receive multiple packets Luca Vizzarro
2024-08-06 12:14 ` [PATCH 2/5] dts: add random generation seed setting Luca Vizzarro
2024-08-06 12:14 ` Luca Vizzarro [this message]
2024-08-06 12:14 ` [PATCH 4/5] dts: add ability to start/stop ports Luca Vizzarro
2024-08-06 12:14 ` [PATCH 4/5] dts: add ability to start/stop testpmd ports Luca Vizzarro
2024-08-06 12:14 ` [PATCH 5/5] dts: add testpmd set ports queues Luca Vizzarro
2024-08-06 12:46 ` [PATCH v2 0/5] dts: add pktgen and testpmd changes Luca Vizzarro
2024-08-06 12:46 ` [PATCH v2 1/5] dts: add ability to send/receive multiple packets Luca Vizzarro
2024-08-09 15:10 ` Jeremy Spewock
2024-09-06 16:23 ` Luca Vizzarro
2024-09-09 14:12 ` Jeremy Spewock
2024-08-23 10:17 ` Juraj Linkeš
2024-09-06 16:30 ` Luca Vizzarro
2024-08-06 12:46 ` [PATCH v2 2/5] dts: add random generation seed setting Luca Vizzarro
2024-08-09 15:10 ` Jeremy Spewock
2024-08-23 10:30 ` Juraj Linkeš
2024-08-06 12:46 ` [PATCH v2 3/5] dts: add random packet generator Luca Vizzarro
2024-08-09 15:11 ` Jeremy Spewock
2024-08-23 11:58 ` Juraj Linkeš
2024-09-06 16:31 ` Luca Vizzarro
2024-08-06 12:46 ` [PATCH v2 4/5] dts: add ability to start/stop testpmd ports Luca Vizzarro
2024-08-09 15:13 ` Jeremy Spewock
2024-09-06 16:41 ` Luca Vizzarro
2024-08-23 12:16 ` Juraj Linkeš
2024-09-06 16:46 ` Luca Vizzarro
2024-09-09 7:32 ` Juraj Linkeš
2024-09-09 14:20 ` Jeremy Spewock
2024-09-09 15:16 ` Juraj Linkeš
2024-08-06 12:46 ` [PATCH v2 5/5] dts: add testpmd set ports queues Luca Vizzarro
2024-08-09 15:14 ` Jeremy Spewock
2024-08-20 16:04 ` Jeremy Spewock
2024-09-06 16:51 ` Luca Vizzarro
2024-08-23 12:22 ` Juraj Linkeš
2024-09-06 16:53 ` Luca Vizzarro
2024-09-09 11:01 ` [PATCH v3 0/5] dts: add pktgen and testpmd changes Luca Vizzarro
2024-09-09 11:01 ` [PATCH v3 1/5] dts: add ability to send/receive multiple packets Luca Vizzarro
2024-09-09 17:12 ` Patrick Robb
2024-09-09 11:01 ` [PATCH v3 2/5] dts: add random generation seed setting Luca Vizzarro
2024-09-09 11:01 ` [PATCH v3 3/5] dts: add random packet generator Luca Vizzarro
2024-09-09 11:01 ` [PATCH v3 4/5] dts: add ability to start/stop testpmd ports Luca Vizzarro
2024-09-09 11:54 ` Juraj Linkeš
2024-09-09 14:20 ` Jeremy Spewock
2024-09-09 11:01 ` [PATCH v3 5/5] dts: add testpmd set ports queues Luca Vizzarro
2024-09-09 12:01 ` Juraj Linkeš
2024-09-09 14:20 ` Jeremy Spewock
2024-09-09 15:50 ` [PATCH 0/4] dts: add pktgen and testpmd changes Juraj Linkeš
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20240806121417.2567708-4-Luca.Vizzarro@arm.com \
--to=luca.vizzarro@arm.com \
--cc=alex.chapman@arm.com \
--cc=dev@dpdk.org \
--cc=honnappa.nagarahalli@arm.com \
--cc=jspewock@iol.unh.edu \
--cc=juraj.linkes@pantheon.tech \
--cc=paul.szczepanek@arm.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).