Skip to main content

P4Runtime Client Library

Project description

Finsy P4Runtime Controller Library

pypi ci codecov docs

Finsy is a P4Runtime controller library written in Python using asyncio. Finsy includes support for gNMI.

Check out the examples directory for demo programs.

Installation

To install the latest version, type pip install finsy. Finsy requires Python 3.10 or later.

P4Runtime Scripts

With Finsy, you can write a Python script that reads/writes P4Runtime entities for a single switch.

Here is a complete example that retrieves the P4Info from a switch:

import finsy as fy

async def main():
    async with fy.Switch("sw1", "127.0.0.1:50001") as sw1:
        # Print out a description of the switch's P4Info, if one is configured.
        print(sw1.p4info)

fy.run(main())

Here is another example that prints out all non-default table entries.

import finsy as fy

async def main():
    async with fy.Switch("sw1", "127.0.0.1:50001") as sw1:
        # Do a wildcard read for table entries.
        async for entry in sw1.read(fy.P4TableEntry()):
            print(entry)

fy.run(main())

P4Runtime Controller

You can also write a P4Runtime controller that manages multiple switches independently. Your controller can react to events from the Switch by changing the contents of P4 tables.

Each switch is managed by an async ready_handler function. Your ready_handler function can read or update various P4Runtime entities in the switch. It can also create tasks to listen for packets or digests.

When you write P4Runtime updates to the switch, you use a unary operator (+, -, ~) to specify the operation: INSERT (+), DELETE (-) or MODIFY (~).

import finsy as fy

async def ready_handler(sw: fy.Switch):
    await sw.delete_all()
    await sw.write(
        [
            # Insert (+) multicast group with ports 1, 2, 3 and CONTROLLER.
            +fy.P4MulticastGroupEntry(1, replicas=[1, 2, 3, 255]),
            # Modify (~) default table entry to flood all unmatched packets.
            ~fy.P4TableEntry(
                "ipv4",
                action=fy.P4TableAction("flood"),
                is_default_action=True,
            ),
        ]
    )

    async for packet in sw.read_packets():
        print(f"{sw.name}: {packet}")

Use the SwitchOptions class to specify each switch's settings, including the p4info/p4blob and ready_handler. Use the Controller class to drive multiple switch connections. Each switch will call back into your ready_handler function after the P4Runtime connection is established.

from pathlib import Path

options = fy.SwitchOptions(
    p4info=Path("hello.p4info.txt"),
    p4blob=Path("hello.json"),
    ready_handler=ready_handler,
)

controller = fy.Controller([
    fy.Switch("sw1", "127.0.0.1:50001", options),
    fy.Switch("sw2", "127.0.0.1:50002", options),
    fy.Switch("sw3", "127.0.0.1:50003", options),
])

fy.run(controller.run())

Your ready_handler can spawn concurrent tasks with the Switch.create_task method. Tasks created this way will have their lifetimes managed by the switch object.

If the switch disconnects or its role changes to backup, the task running your ready_handler (and any tasks it spawned) will be cancelled and the ready_handler will begin again.

For more examples, see the examples directory.

Development and Testing

Finsy requires a newer version of Python than is available on many systems. I recommend using pyenv and poetry to maintain your development environment. You may use pip instead of poetry if you prefer.

To set up your development environment for Finsy using Python 3.11.2:

git clone https://github.com/byllyfish/finsy.git
cd finsy
pyenv install 3.11.2
pyenv shell 3.11.2
python -m venv .venv
.venv/bin/activate
poetry install

If you are not using poetry, replace poetry install with pip install -r ci/requirements-dev.txt.

To run the unit tests:

pytest

To run the example tests, run pytest from inside the examples directory. These tests require podman.

cd examples
pytest

If you used pip, you need to make sure that the finsy module is available in the PYTHONPATH when running from the examples directory.

PYTHONPATH=.. pytest

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

finsy-0.15.0.tar.gz (172.4 kB view details)

Uploaded Source

Built Distribution

finsy-0.15.0-py3-none-any.whl (215.2 kB view details)

Uploaded Python 3

File details

Details for the file finsy-0.15.0.tar.gz.

File metadata

  • Download URL: finsy-0.15.0.tar.gz
  • Upload date:
  • Size: 172.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.11.3

File hashes

Hashes for finsy-0.15.0.tar.gz
Algorithm Hash digest
SHA256 93188800518fb5a70da405ea0c69de32edd479850bfa5a64e1fb32fb1f08d75d
MD5 fa64264059bb020e0e42f1248f4f12aa
BLAKE2b-256 791baebf27a762d0ec19dce3a229df42ece95cee675a1a085efe52d40f21d2d3

See more details on using hashes here.

File details

Details for the file finsy-0.15.0-py3-none-any.whl.

File metadata

  • Download URL: finsy-0.15.0-py3-none-any.whl
  • Upload date:
  • Size: 215.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.11.3

File hashes

Hashes for finsy-0.15.0-py3-none-any.whl
Algorithm Hash digest
SHA256 5f39e9240a809e8934862c7c9c3829d22f761c09eeaeede8e55bc5b3f94c78cc
MD5 a8d5b44dfdc04dd6a5809ec9b087ae51
BLAKE2b-256 59b4dbe7934818ac4c79b0658bbeba2c10dd33a9e9784a10b650a0d59076eeb2

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page