Skip to content
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

Add bf pktpy source #221

Merged
merged 5 commits into from
Mar 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 10 additions & 38 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,12 @@ More information about Black, you find at
The following software is required to run PTF:

* Python 3.x
* Scapy 2.5.0 (unless you provide another packet manipulation module)
* pypcap (optional - VLAN tests will fail without this)
* tcpdump (optional - Scapy will complain if it's missing)

The following packages are optional for running PTF:

* Scapy 2.5.0 (you may also use the included `bf_pktpy` module instead)
* pypcap (VLAN tests will fail without this)
* tcpdump (Scapy will complain if it's missing)

Root/sudo privilege is required on the host, in order to run `ptf`.

Expand Down Expand Up @@ -74,11 +77,7 @@ apt-get install tcpdump

### Using `bf_pktpy` as an alternate packet manipulation module

The Python module `bf_pktpy` is included as part of the
`open-p4studio` repository:

+ https://github.com/p4lang/open-p4studio/tree/main/pkgsrc/ptf-modules/bf-pktpy

The Python module `bf_pktpy` is included as part of the ptf package.
It was developed as an alternative to `scapy`. The tradeoffs of using
`bf_pktpy` vs. `scapy` are:

Expand All @@ -89,34 +88,7 @@ It was developed as an alternative to `scapy`. The tradeoffs of using
under a different license.
+ `bf_pktpy` implements only a small subset of the functionality of
`scapy`, but it does include support for very commonly-used packet
headers. It is released under an Apache 2.0 license (see
https://github.com/p4lang/open-p4studio/blob/main/pkgsrc/ptf-modules/bf-pktpy/LICENSE).

The package `bf_pktpy` is not currently available from PyPI. To
install `bf_pktpy` from source code, choose one of these methods:

```bash
pip install "bf-pktpy@git+https://github.com/p4lang/open-p4studio#subdirectory=pkgsrc/ptf-utils/bf-pktpy"
```

```bash
git clone https://github.com/p4lang/open-p4studio
cd open-p4studio/pkgsrc/ptf-utils/bf-pktpy
pip install .
```

To make effective use of `bf_pktpy` you must also install these
additional Python packages. All are released under an MIT or
BSD-3-Clause license, which are compatible for releasing with
`bf_pktpy`, or for importing in a project with most licenses,
including `Apache-2.0`, `BSD-3-Clause`, `GPL-2.0-only`, or many
others.

```bash
pip install six getmac scapy_helper psutil
sudo apt-get install python3-dev
pip install netifaces
```
headers. It is released under an Apache 2.0 license.

If you want to use `bf_pktpy` when running the command `ptf` from the
command line, provide the `-pmm` option as shown below.
Expand Down Expand Up @@ -274,9 +246,9 @@ timeout takes precedence over the global timeout passed on the command line.
## Pluggable packet manipulation module

By default, `ptf` uses `Scapy` as the packet manipulation module, but it can
also operate on a different one.
also operate on a different one, e.g. the included `bf_pktpy` module.

Such module **must define/implement the same symbols**, as defined in `Scapy`
Such a module **must define/implement the same symbols**, as defined in `Scapy`
implementation of packet. Most of them are just names of most common frame
headers (Ether, IP, TCP, UDP, ...).

Expand Down
6 changes: 6 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ platforms = any
python_requires = >=3.4
setup_requires =
setuptools_scm
install_requires =
six
getmac
scapy_helper
Copy link
Contributor

Choose a reason for hiding this comment

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

What is this particular dependency needed for? License looks fine and it doesn't pull in scapy. Just curious.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I haven't looked in detail. All I know is that if you try to do import bf_pktpy without this package, you get an error that it cannot find it. It appears to define a few little functions for converting between different IP and MAC address formats, and probably a couple of other things I'm forgetting.

netifaces
psutil

[options.packages.find]
where = src
19 changes: 19 additions & 0 deletions src/bf_pktpy/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Copyright (c) 2021 Intel Corporation.
#
# 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.

from bf_pktpy.library.specs import Ether, IP, IPv6, ARP, TCP, UDP, ICMP, MPLS
from bf_pktpy.library.specs import GRE, Dot1Q, BOOTP, DHCP, BFD

# from bf_pktpy.library.specs import Arp
from bf_pktpy.library.utils import Interface, Stream, Listener, Decoder
from bf_pktpy.commands import send, sendp, sr, sr1, srp, srp1
from bf_pktpy.commands import srloop, srploop, sniff, bridge_and_sniff
17 changes: 17 additions & 0 deletions src/bf_pktpy/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Copyright (c) 2021 Intel Corporation.
#
# 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.


from main import run_interactive

if __name__ == "__main__":
run_interactive()
19 changes: 19 additions & 0 deletions src/bf_pktpy/all/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Copyright (c) 2021 Intel Corporation.
#
# 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.


# Import all from packets & utils
from bf_pktpy.packets import *
from bf_pktpy.library.helpers.bytes2hex import bytes2hex
from bf_pktpy.library.helpers.get_if_list import get_if_list
from bf_pktpy.library.utils.hexdump import hexdump
from bf_pktpy.library.utils.ls import ls
23 changes: 23 additions & 0 deletions src/bf_pktpy/build_information.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Copyright (c) 2021 Intel Corporation.
#
# 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.

from bf_pktpy.library.helpers import constants


class BuildInformation:
def __init__(self):
self._version = "0.2"

def show_details(self, logo=False):
if logo:
print(constants.logo)
print("\tBarefoot PKTPY (Packet Generator)\n\tVersion: %s" % self._version)
Loading