Flows are defined by an OpenFlow controller by sending OFPT_FLOW_MOD messages to the datapath, by calling method send_flow_mod_add() or send_flow_mod_modify() on the controller stub. A flow definition includes the actions to perform on the packet in the flow. Every action is represented by an object that is an instance of a class implementing the IAction interface:
Serialize this action object into a binary string that is a serialized form of this action object into an OpenFlow action. The ofp_action_header structure is not included in the generated strings.
The returned string can be passed to deserialize() to recreate a copy of this action object.
Returns an action object deserialized from a sequence of bytes.
Parameter: | buf – A ReceiveBuffer object that contains the bytes that are the serialized form of the action, after the ofp_action_header structure. |
---|---|
Raises: | ValueError if the buffer has an invalid number of available bytes, or if some elements cannot be deserialized. |
The following action classes implement that interface and can be used to encode and decode standard OpenFlow actions. All these classes are named tuple classes (cf. Python’s collections), i.e. action objects are also tuples whose elements each correspond to an attribute. To create an action object, its attributes can be given in positional order, or using keywords, i.e.:
from openfaucet import ofaction
action = ofaction.ActionOutput(12, 128)
is equivalent to:
from openfaucet import ofaction
action = ofaction.ActionOutput(port=12, max_len=128)
Likewise, attributes can be accessed by position or by name, i.e.:
port = action[0]
is equivalent to:
port = action.port
Attribute access by name is recommended for readability. Every action object, just like any tuple, is immutable and hashable. A copy of an existing action object with some of its attributes modified can be created by calling _replace(), for instance:
new_action = action._replace(port=42)
Output and QoS
Packets can be output to physical ports, which have port numbers 1 to OFPP_MAX:
In addition to physical ports, several logical ports are defined:
The following actions output packets to ports directly or to queues associated with specific ports.
An OFPAT_OUTPUT action, which sends packets out a given port.
For instance, to flood packets:
from openfaucet import ofaction, ofproto
action = ofaction.ActionOutput(ofproto.OFPP_FLOOD, 0)
An OFPAT_ENQUEUE action, which sends packets out a given queue.
An OFPAT_SET_NW_TOS action, which sets the IPv4 ToS DSCP of packets. This action is applied only to IPv4 packets.
802.1q VLAN
The following actions encapsulate / decapsulate packets into 802.1q VLAN headers.
An OFPAT_SET_VLAN_VID action, which sets the 802.1q VLAN ID of packets.
An OFPAT_SET_VLAN_PCP action, which sets the 802.1q VLAN priority of packets.
Address rewriting
The following actions rewrite the packets’ source and destination addresses and ports, at the Ethernet, IPv4, and TCP/UDP levels.
An OFPAT_SET_DL_SRC action, which sets the Ethernet source address of packets.
An OFPAT_SET_DL_DST action, which sets the Ethernet destination address of packets.
For instance, to create an action that sets the Ethernet destination address to ab:cd:ef:01:23:45:
from openfaucet import ofaction
action = ofaction.ActionSetDlDst('\xab\xcd\xef\x01\x23\x45')
An OFPAT_SET_NW_SRC action, which sets the IPv4 source address of packets. This action is applied only to IPv4 packets.
An OFPAT_SET_NW_DST action, which sets the IPv4 destination address of packets. This action is applied only to IPv4 packets.
For instance, to redirect all packets to address 192.168.0.1:
import socket
from openfaucet import ofaction
nw_addr = socket.inet_pton(socket.AF_INET, '192.168.0.1')
action = ofaction.ActionSetNwDst(nw_addr)
An OFPAT_SET_TP_SRC action, which sets the TCP or UDP source port of packets. This action is applied only to IPv4 TCP or UDP packets.
An OFPAT_SET_TP_DST action, which sets the TCP or UDP destination port of packets. This action is applied only to IPv4 TCP or UDP packets.
For instance, to redirect all packets to port 8080:
from openfaucet import ofaction
action = ofaction.ActionSetTpDst(8080)