bfrt_helper.fields

Base Fields

JSONSerialisable

class JSONSerialisable[source]

Bases: object

Base class that enables converting a derived’s contents to JSON

json()[source]

Generates a dictionary representation of a classes contents.

static serialise(value)[source]

Recursively descends, serialising class members

Field

class Field(self, value=0)[source]

Bases: JSONSerialisable

Base class for all BfRt Helper field objects.

A field is datatype and value defined within a P4 program. Such fields can be manipulated using gRPC, however requires translation from the client side as types can have arbitrary bitwidths.

Internally, the value is stored as an integer, since bitwise operations are easier to apply.

Raises:

InvalidValue – Raised if the value assigned to the object is greater than the maximum permissable value

to_bytes()[source]

Converts internal value to a byte array representing the contents

When marshalling to gRPC, the byte representation is used.

classmethod from_bytes(data)[source]

Accepts a string of bytes and converts it to an instance of the derived class.

classmethod max_value()[source]

Using the derived classes bitwidth, retrieve the maximum value. This is \(2^x-1\).

Predefined Fields

DevPort

class DevPort(self, value=0)[source]

Bases: Field

bitwidth = 32

DigestType

class DigestType(self, value=0)[source]

Bases: Field

Representation of a digest as defined in the BfRt spec.

A digest is used to send messages from the Tofino hardware to the runtime components. The 3 bit field can be used to inform the controller of any special semantics for a given message, for instance what type it is, or what data it includes. For instance, you could send a message containing an IP address, but change the digest type depending on whether it was IPv4 or IPv6.

Another example would be notifying the controller of any new MAC addresses seen on the wire.

In any case, this would not typically be used to send messages, but instead is useful for storing when received.

bitwidth = 3

EgressSpec

class EgressSpec(self, value=0)[source]

Bases: Field

Legacy port representation

bitwidth = 9

IPv4Address

class IPv4Address(self, address: str)[source]

Bases: Field

Utility class for better representing IP addresses in a more pleasing way.

Can accept string representations and internally convert them to the required integer type, and vice versa when printing as a string. When the value is sent to the gRPC interface, it will be converted to a byte array as required.

Uses the ipaddress module.

Parameters:

address (str) – Dotted decimal IPv4 address string

bitwidth = 32

Layer2Port

class Layer2Port(self, value=0)[source]

Bases: Field

bitwidth = 16

MACAddress

class MACAddress(self, address)[source]

Bases: Field

Utility class for better representing IP addresses in a more pleasing way.

Can accept string representations and internally convert them to the required integer type, and vice versa when printing as a string. When the value is sent to the gRPC interface, it will be converted to a byte array as required.

Internally uses string manipulation and integer casting for serialisation from and to integer value.

Parameters:

address (str) – Colon seperated 6 byte hexadecimal address string.

bitwidth = 48

MulticastGroupId

class MulticastGroupId(self, value=0)[source]

Bases: Field

Mutlicast group P4 data type, as defined by the Tofino core architecture

Parameters:

value (int) – Group id.

bitwidth = 16

MulticastNodeId

class MulticastNodeId(self, value=0)[source]

Bases: Field

bitwidth = 32

PortId

class PortId(self, value=0)[source]

Bases: Field

Typical port id data type.

The port id typically represents an egress or ingress port, and is 9 bits wide.

Parameters:

value (int) – Port id.

bitwidth = 9

ReplicationId

class ReplicationId(self, value=0)[source]

Bases: Field

Representation of a replication ID as defined in the BfRt spec

A replication id is a tag that is added to metadata that can be used to perform additional operations when moving packets across multicast groups.

bitwidth = 16

StringField

class StringField(self, value=0)[source]

Bases: Field

Represents a gRPC string field.

Note

This is here for completeness purposes only, and is not used (perhaps even required), by any available gRPC function (so far).

VlanID

class VlanID(self, value=0)[source]

Bases: Field

User class representing a parsed Vlan ID

Parameters:

value (int) – VLAN id.

bitwidth = 12

Exceptions

MismatchedKeys

class MismatchedTypes(a, b)[source]

Bases: Exception

Raised when a field’s type (e.g., bitwidth or object) do not match when comparing two match objects.