xref: /dpdk/dts/framework/testbed_model/tg_node.py (revision 2e69387a656396a7382fe27d8d9f61f1b5890573)
1cecfe0aaSJuraj Linkeš# SPDX-License-Identifier: BSD-3-Clause
2cecfe0aaSJuraj Linkeš# Copyright(c) 2010-2014 Intel Corporation
3cecfe0aaSJuraj Linkeš# Copyright(c) 2022 University of New Hampshire
4cecfe0aaSJuraj Linkeš# Copyright(c) 2023 PANTHEON.tech s.r.o.
5cecfe0aaSJuraj Linkeš
6cecfe0aaSJuraj Linkeš"""Traffic generator node.
7cecfe0aaSJuraj Linkeš
86ef07151SJuraj LinkešA traffic generator (TG) generates traffic that's sent towards the SUT node.
96ef07151SJuraj LinkešA TG node is where the TG runs.
10cecfe0aaSJuraj Linkeš"""
11cecfe0aaSJuraj Linkeš
12282688eaSLuca Vizzarrofrom scapy.packet import Packet  # type: ignore[import-untyped]
13cecfe0aaSJuraj Linkeš
14840b1e01SJuraj Linkešfrom framework.config import TGNodeConfiguration
152b2f5a8aSLuca Vizzarrofrom framework.testbed_model.traffic_generator.capturing_traffic_generator import (
162b2f5a8aSLuca Vizzarro    PacketFilteringConfig,
172b2f5a8aSLuca Vizzarro)
18cecfe0aaSJuraj Linkeš
19cecfe0aaSJuraj Linkešfrom .node import Node
20840b1e01SJuraj Linkešfrom .port import Port
212b2f5a8aSLuca Vizzarrofrom .traffic_generator import CapturingTrafficGenerator, create_traffic_generator
22cecfe0aaSJuraj Linkeš
23cecfe0aaSJuraj Linkeš
24cecfe0aaSJuraj Linkešclass TGNode(Node):
256ef07151SJuraj Linkeš    """The traffic generator node.
26cecfe0aaSJuraj Linkeš
276ef07151SJuraj Linkeš    The TG node extends :class:`Node` with TG specific features:
28cecfe0aaSJuraj Linkeš
296ef07151SJuraj Linkeš        * Traffic generator initialization,
306ef07151SJuraj Linkeš        * The sending of traffic and receiving packets,
316ef07151SJuraj Linkeš        * The sending of traffic without receiving packets.
326ef07151SJuraj Linkeš
336ef07151SJuraj Linkeš    Not all traffic generators are capable of capturing traffic, which is why there
346ef07151SJuraj Linkeš    must be a way to send traffic without that.
35cecfe0aaSJuraj Linkeš
36cecfe0aaSJuraj Linkeš    Attributes:
37cecfe0aaSJuraj Linkeš        traffic_generator: The traffic generator running on the node.
38cecfe0aaSJuraj Linkeš    """
39cecfe0aaSJuraj Linkeš
40cecfe0aaSJuraj Linkeš    traffic_generator: CapturingTrafficGenerator
41cecfe0aaSJuraj Linkeš
42cecfe0aaSJuraj Linkeš    def __init__(self, node_config: TGNodeConfiguration):
436ef07151SJuraj Linkeš        """Extend the constructor with TG node specifics.
446ef07151SJuraj Linkeš
456ef07151SJuraj Linkeš        Initialize the traffic generator on the TG node.
466ef07151SJuraj Linkeš
476ef07151SJuraj Linkeš        Args:
486ef07151SJuraj Linkeš            node_config: The TG node's test run configuration.
496ef07151SJuraj Linkeš        """
50f614e737SJuraj Linkeš        super().__init__(node_config)
51517b4b26SJuraj Linkeš        self.traffic_generator = create_traffic_generator(self, node_config.traffic_generator)
52cecfe0aaSJuraj Linkeš        self._logger.info(f"Created node: {self.name}")
53cecfe0aaSJuraj Linkeš
540bf6796aSLuca Vizzarro    def send_packets_and_capture(
55cecfe0aaSJuraj Linkeš        self,
560bf6796aSLuca Vizzarro        packets: list[Packet],
57cecfe0aaSJuraj Linkeš        send_port: Port,
58cecfe0aaSJuraj Linkeš        receive_port: Port,
59bad934bfSJeremy Spewock        filter_config: PacketFilteringConfig = PacketFilteringConfig(),
60cecfe0aaSJuraj Linkeš        duration: float = 1,
61cecfe0aaSJuraj Linkeš    ) -> list[Packet]:
620bf6796aSLuca Vizzarro        """Send `packets`, return received traffic.
63cecfe0aaSJuraj Linkeš
640bf6796aSLuca Vizzarro        Send `packets` on `send_port` and then return all traffic captured
656ef07151SJuraj Linkeš        on `receive_port` for the given duration. Also record the captured traffic
66cecfe0aaSJuraj Linkeš        in a pcap file.
67cecfe0aaSJuraj Linkeš
68cecfe0aaSJuraj Linkeš        Args:
690bf6796aSLuca Vizzarro            packets: The packets to send.
70cecfe0aaSJuraj Linkeš            send_port: The egress port on the TG node.
71cecfe0aaSJuraj Linkeš            receive_port: The ingress port in the TG node.
72bad934bfSJeremy Spewock            filter_config: The filter to use when capturing packets.
736ef07151SJuraj Linkeš            duration: Capture traffic for this amount of time after sending `packet`.
74cecfe0aaSJuraj Linkeš
75cecfe0aaSJuraj Linkeš        Returns:
76cecfe0aaSJuraj Linkeš             A list of received packets. May be empty if no packets are captured.
77cecfe0aaSJuraj Linkeš        """
780bf6796aSLuca Vizzarro        return self.traffic_generator.send_packets_and_capture(
790bf6796aSLuca Vizzarro            packets,
80bad934bfSJeremy Spewock            send_port,
81bad934bfSJeremy Spewock            receive_port,
82bad934bfSJeremy Spewock            filter_config,
83bad934bfSJeremy Spewock            duration,
84cecfe0aaSJuraj Linkeš        )
85cecfe0aaSJuraj Linkeš
86*2e69387aSJeremy Spewock    def send_packets(self, packets: list[Packet], port: Port):
87*2e69387aSJeremy Spewock        """Send packets without capturing resulting received packets.
88*2e69387aSJeremy Spewock
89*2e69387aSJeremy Spewock        Args:
90*2e69387aSJeremy Spewock            packets: Packets to send.
91*2e69387aSJeremy Spewock            port: Port to send the packets on.
92*2e69387aSJeremy Spewock        """
93*2e69387aSJeremy Spewock        self.traffic_generator.send_packets(packets, port)
94*2e69387aSJeremy Spewock
95cecfe0aaSJuraj Linkeš    def close(self) -> None:
966ef07151SJuraj Linkeš        """Free all resources used by the node.
976ef07151SJuraj Linkeš
986ef07151SJuraj Linkeš        This extends the superclass method with TG cleanup.
996ef07151SJuraj Linkeš        """
100cecfe0aaSJuraj Linkeš        self.traffic_generator.close()
101f614e737SJuraj Linkeš        super().close()
102