xref: /dpdk/dts/framework/testbed_model/tg_node.py (revision 3da59f30a23f2e795d2315f3d949e1b3e0ce0c3d)
1# SPDX-License-Identifier: BSD-3-Clause
2# Copyright(c) 2010-2014 Intel Corporation
3# Copyright(c) 2022 University of New Hampshire
4# Copyright(c) 2023 PANTHEON.tech s.r.o.
5
6"""Traffic generator node.
7
8A traffic generator (TG) generates traffic that's sent towards the SUT node.
9A TG node is where the TG runs.
10"""
11
12from scapy.packet import Packet  # type: ignore[import]
13
14from framework.config import TGNodeConfiguration
15
16from .node import Node
17from .port import Port
18from .traffic_generator import CapturingTrafficGenerator, create_traffic_generator
19
20
21class TGNode(Node):
22    """The traffic generator node.
23
24    The TG node extends :class:`Node` with TG specific features:
25
26        * Traffic generator initialization,
27        * The sending of traffic and receiving packets,
28        * The sending of traffic without receiving packets.
29
30    Not all traffic generators are capable of capturing traffic, which is why there
31    must be a way to send traffic without that.
32
33    Attributes:
34        traffic_generator: The traffic generator running on the node.
35    """
36
37    traffic_generator: CapturingTrafficGenerator
38
39    def __init__(self, node_config: TGNodeConfiguration):
40        """Extend the constructor with TG node specifics.
41
42        Initialize the traffic generator on the TG node.
43
44        Args:
45            node_config: The TG node's test run configuration.
46        """
47        super(TGNode, self).__init__(node_config)
48        self.traffic_generator = create_traffic_generator(self, node_config.traffic_generator)
49        self._logger.info(f"Created node: {self.name}")
50
51    def send_packet_and_capture(
52        self,
53        packet: Packet,
54        send_port: Port,
55        receive_port: Port,
56        duration: float = 1,
57    ) -> list[Packet]:
58        """Send `packet`, return received traffic.
59
60        Send `packet` on `send_port` and then return all traffic captured
61        on `receive_port` for the given duration. Also record the captured traffic
62        in a pcap file.
63
64        Args:
65            packet: The packet to send.
66            send_port: The egress port on the TG node.
67            receive_port: The ingress port in the TG node.
68            duration: Capture traffic for this amount of time after sending `packet`.
69
70        Returns:
71             A list of received packets. May be empty if no packets are captured.
72        """
73        return self.traffic_generator.send_packet_and_capture(
74            packet, send_port, receive_port, duration
75        )
76
77    def close(self) -> None:
78        """Free all resources used by the node.
79
80        This extends the superclass method with TG cleanup.
81        """
82        self.traffic_generator.close()
83        super(TGNode, self).close()
84