Skip to content

Replace unnecessary use of Scapy with ptf.pcap_writer in p4tc tests #5165

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

14 changes: 3 additions & 11 deletions backends/tc/runtime/send_packet
Original file line number Diff line number Diff line change
@@ -1,15 +1,7 @@
#!/usr/bin/env python3
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this still needed?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vbnogueira Can we replace this code with the other utilities we are using?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@fruffy It is currently used by the p4tc tests.

I have a way to update ptf and bf-pktpy with a few extra lines of code, such that we can relicense this send_packet file as Apache-2.0. I have those changes in this PR, but there are a couple of other PRs that must be approved and merged first before it can be merged: #5145

# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# Copyright 2025 Intel Corporation
# SPDX-License-Identifier: GPL-2.0-only
# Reason-GPL: imports-scapy

import argparse
from scapy.all import sendp, rdpcap
Expand Down
25 changes: 11 additions & 14 deletions backends/tc/test_infra.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
""" Represents the call which contains all methods needed to execute a TC
test. Currently five phases are defined:
1. Invokes the specified compiler on a provided p4 file.
2. Parses an stf file and generates a pcap output.
3. Loads the generated template nd the eBPF binaries
4. Feeds the generated pcap test packets into scapy
5. Evaluates the output with the expected result from the .stf file
"""Represents the call which contains all methods needed to execute a TC
test. Currently five phases are defined:
1. Invokes the specified compiler on a provided p4 file.
2. Parses an stf file and generates a pcap output.
3. Loads the generated template nd the eBPF binaries
4. Feeds the generated pcap test packets into ptf.pcap_writer.PcapWriter
5. Evaluates the output with the expected result from the .stf file
"""

import difflib
Expand All @@ -40,9 +40,8 @@
sys.path.append(str(FILE_DIR.joinpath("../../tools")))
sys.path.append(str(FILE_DIR.joinpath("../ebpf/targets")))

import scapy.utils as scapy_util
import testutils
from scapy.all import sendp
from ptf.pcap_writer import LINKTYPE_ETHERNET, PcapWriter, rdpcap
from stf.stf_parser import STFParser

PCAP_PREFIX = "pcap" # match pattern
Expand Down Expand Up @@ -271,12 +270,10 @@ def _send_pcap_files(self, iface_pkts_map):
for iface, pkts in iface_pkts_map.items():
direction = "in"
infile = self.filename(self.runtimedir, iface, direction)
# Linktype 1 the Ethernet Link Type, see also 'man pcap-linktype'
fp = scapy_util.RawPcapWriter(infile, linktype=1)
fp._write_header(None)
fp = PcapWriter(infile, linktype=LINKTYPE_ETHERNET)
for pkt_data in pkts:
try:
fp._write_packet(pkt_data)
fp.write(pkt_data)
except ValueError:
testutils.log.error(f"Invalid packet data {pkt_data}")
return testutils.ProcessResult("", testutils.FAILURE)
Expand Down Expand Up @@ -502,7 +499,7 @@ def check_outputs(self):
packets = []
else:
try:
packets = scapy_util.rdpcap(file)
packets = rdpcap(file)
except Exception as e:
testutils.log.error("Corrupt pcap file %s\n%s", file, e)
return testutils.FAILURE
Expand Down
Loading