xref: /dpdk/dts/framework/testbed_model/tg_node.py (revision bd4a5aa413583aa698f10849c4784a3d524566bc)
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 (
19    CapturingTrafficGenerator,
20    PacketFilteringConfig,
21    create_traffic_generator,
22)
23
24
25class TGNode(Node):
26    """The traffic generator node.
27
28    The TG node extends :class:`Node` with TG specific features:
29
30        * Traffic generator initialization,
31        * The sending of traffic and receiving packets,
32        * The sending of traffic without receiving packets.
33
34    Not all traffic generators are capable of capturing traffic, which is why there
35    must be a way to send traffic without that.
36
37    Attributes:
38        traffic_generator: The traffic generator running on the node.
39    """
40
41    traffic_generator: CapturingTrafficGenerator
42
43    def __init__(self, node_config: TGNodeConfiguration):
44        """Extend the constructor with TG node specifics.
45
46        Initialize the traffic generator on the TG node.
47
48        Args:
49            node_config: The TG node's test run configuration.
50        """
51        super(TGNode, self).__init__(node_config)
52        self.traffic_generator = create_traffic_generator(self, node_config.traffic_generator)
53        self._logger.info(f"Created node: {self.name}")
54
55    def send_packet_and_capture(
56        self,
57        packet: Packet,
58        send_port: Port,
59        receive_port: Port,
60        filter_config: PacketFilteringConfig = PacketFilteringConfig(),
61        duration: float = 1,
62    ) -> list[Packet]:
63        """Send `packet`, return received traffic.
64
65        Send `packet` on `send_port` and then return all traffic captured
66        on `receive_port` for the given duration. Also record the captured traffic
67        in a pcap file.
68
69        Args:
70            packet: The packet to send.
71            send_port: The egress port on the TG node.
72            receive_port: The ingress port in the TG node.
73            filter_config: The filter to use when capturing packets.
74            duration: Capture traffic for this amount of time after sending `packet`.
75
76        Returns:
77             A list of received packets. May be empty if no packets are captured.
78        """
79        return self.traffic_generator.send_packet_and_capture(
80            packet,
81            send_port,
82            receive_port,
83            filter_config,
84            duration,
85        )
86
87    def close(self) -> None:
88        """Free all resources used by the node.
89
90        This extends the superclass method with TG cleanup.
91        """
92        self.traffic_generator.close()
93        super(TGNode, self).close()
94