ofclient

package
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 6, 2026 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Overview

Package ofclient implements a minimal OpenFlow 1.3 client over a Unix socket. Supports: Hello handshake, PORT_DESC multipart for name→number resolution, FLOW_MOD add and delete with OXM_OF_IN_PORT and OXM_OF_TUNNEL_ID fields, and automatic OFPT_ECHO_REPLY to keep the OVS connection alive.

Index

Constants

View Source
const NxVendorID = 0x00002320

NxVendorID is the experimenter ID assigned to the Nicira-extension family.

Variables

This section is empty.

Functions

func BuildActionsOutput

func BuildActionsOutput(portNo uint32) []byte

BuildActionsOutput encodes a single OFPAT_OUTPUT action (16 bytes).

func BuildActionsRegMove

func BuildActionsRegMove(nBits, srcOfs, dstOfs uint16, srcOXMID, dstOXMID uint32) []byte

BuildActionsRegMove encodes NXAST_REG_MOVE: copy `nBits` bits starting at srcOfs of srcOXM into dstOfs of dstOXM. OXM IDs are 4-byte headers (no value).

Wire format: OFPAT_EXPERIMENTER/24 + vendor(4) + subtype(2) + n_bits(2) + src_ofs(2) + dst_ofs(2) + src_oxm_id(4) + dst_oxm_id(4) = 24 bytes.

func BuildActionsResubmitTable

func BuildActionsResubmitTable(tableID uint8) []byte

BuildActionsResubmitTable encodes NXAST_RESUBMIT_TABLE: re-run lookup in table `tableID` with the original in_port. Spec §7 uses this between pipeline stages (resubmit(,1), resubmit(,3), resubmit(,5), resubmit(,6)).

Wire format: OFPAT_EXPERIMENTER(0xffff)/16 + vendor(4) + subtype(2) + in_port(2) + table_id(1) + pad(3) = 16 bytes.

func BuildActionsSetFieldTunnelID

func BuildActionsSetFieldTunnelID(vni uint64) []byte

BuildActionsSetFieldTunnelID encodes OFPAT_SET_FIELD for tunnel_id=vni (16 bytes).

func BuildActionsSetMetadata

func BuildActionsSetMetadata(value uint64) []byte

BuildActionsSetMetadata encodes OFPAT_SET_FIELD for OXM_OF_METADATA. 4-byte action header + 4 OXM hdr + 8 value = 16 bytes (already 8-aligned).

func BuildActionsSetReg0

func BuildActionsSetReg0(value uint32) []byte

BuildActionsSetReg0 encodes OFPAT_SET_FIELD for NXM_NX_REG0. 4-byte action header + 4 OXM hdr + 4 value = 12 bytes; pad to 16 (8-aligned). Used in t0 to mark packet origin: 0 = local port, 1 = arrived from Geneve.

func BuildActionsSetTunDst

func BuildActionsSetTunDst(ipBE uint32) []byte

BuildActionsSetTunDst encodes OFPAT_SET_FIELD for NXM_NX_TUN_IPV4_DST. 4-byte action header + 4 OXM hdr + 4 IP = 12 bytes; pad to 16 (8-aligned).

func BuildMatch

func BuildMatch(inPortNo uint32, vni uint64, hasTunnel bool) []byte

BuildMatch builds an ofp_match (OXM type) for in_port and optionally tunnel_id. Result is padded to a multiple of 8 bytes.

func BuildMatchAdvanced

func BuildMatchAdvanced(
	inPort uint32,
	metadata uint64,
	hasMetadata bool,
	reg0 uint32,
	hasReg0 bool,
	reg1 uint32,
	hasReg1 bool,
	tunID uint64,
	hasTunID bool,
) []byte

BuildMatchAdvanced extends BuildMatch with metadata + reg0 + reg1 matches. Any zero-value parameter (matchMetadata=false / hasReg0=false / hasReg1=false) is omitted. inPort=0 means no in_port match.

func BuildMatchTunSrc

func BuildMatchTunSrc(inPortNo uint32, ip uint32) []byte

BuildMatchTunSrc is BuildMatch with the tunnel source address (NXM_NX_TUN_IPV4_SRC) added: in_port plus the IPv4 source of the outer header of a tunnelled packet. ip is the address in host byte order (as OxmTunIPv4Src takes it).

func OxmIDInPort

func OxmIDInPort() uint32

OxmIDInPort is the 4-byte header for OXM_OF_IN_PORT.

func OxmIDMetadata

func OxmIDMetadata() uint32

OxmIDMetadata is the 4-byte header for OXM_OF_METADATA (field 2).

func OxmIDReg0

func OxmIDReg0() uint32

OxmIDReg0 is the 4-byte header for NXM_NX_REG0 (no value).

func OxmIDReg1

func OxmIDReg1() uint32

OxmIDReg1 is the 4-byte header for NXM_NX_REG1.

func OxmIDTunIPv4Dst

func OxmIDTunIPv4Dst() uint32

OxmIDTunIPv4Dst is the 4-byte header for NXM_NX_TUN_IPV4_DST.

func OxmIDTunIPv4Src

func OxmIDTunIPv4Src() uint32

OxmIDTunIPv4Src is the 4-byte header for NXM_NX_TUN_IPV4_SRC.

func OxmIDTunnelID

func OxmIDTunnelID() uint32

OxmIDTunnelID is the 4-byte header for OXM_OF_TUNNEL_ID.

func OxmInPort

func OxmInPort(portNo uint32) []byte

OxmInPort encodes OXM_OF_IN_PORT (class=0x8000, field=0, length=4). Returns 8 bytes: 4-byte header + 4-byte value.

func OxmMetadata

func OxmMetadata(value uint64) []byte

OxmMetadata encodes OXM_OF_METADATA (class=0x8000, field=2, 8 bytes). Used as the canonical VNI carrier across the pipeline. Field code 2 is OFPXMT_OFB_METADATA; field 4 is ETH_SRC and made OVS reject the FLOW_MOD.

func OxmReg0

func OxmReg0(value uint32) []byte

OxmReg0 encodes NXM_NX_REG0 (class=0x0001, field=0, 4 bytes).

func OxmReg1

func OxmReg1(value uint32) []byte

OxmReg1 encodes NXM_NX_REG1 (class=0x0001, field=1, 4 bytes).

func OxmTunIPv4Dst

func OxmTunIPv4Dst(ip uint32) []byte

OxmTunIPv4Dst encodes NXM_NX_TUN_IPV4_DST (class=0x0001, field=32, 4 bytes).

func OxmTunIPv4Src

func OxmTunIPv4Src(ip uint32) []byte

OxmTunIPv4Src encodes NXM_NX_TUN_IPV4_SRC (class=0x0001, field=31, 4 bytes).

func OxmTunnelID

func OxmTunnelID(vni uint64) []byte

OxmTunnelID encodes OXM_OF_TUNNEL_ID (class=0x8000, field=38, length=8). Returns 12 bytes: 4-byte header + 8-byte value.

func StalePortNumbers

func StalePortNumbers(before, after map[string]uint32) []uint32

StalePortNumbers lists the port numbers that were in use before a refresh and are not the same port after it: the port is gone, or the number now belongs to another name. Flows that name such a number must be removed before the number is used again.

Types

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client is a minimal OpenFlow 1.3 client connected to an OVS bridge management socket. A background goroutine reads all incoming messages, replies to OFPT_ECHO_REQUEST automatically, and dispatches PORT_DESC replies to a waiting queryPortDesc call.

func Connect

func Connect(sockPath string) (*Client, error)

Connect connects to the OVS bridge management socket and performs the OF 1.3 handshake. sockPath is e.g. "/run/openvswitch/br-ovs.mgmt". Retries for up to 30 seconds (socket appears after vswitchd creates the bridge).

func (*Client) Close

func (c *Client) Close() error

Close closes the connection and stops the background reader.

func (*Client) FlowAdd

func (c *Client) FlowAdd(tableID uint8, priority uint16, match, actions []byte) error

FlowAdd sends an OFPT_FLOW_MOD OFPFC_ADD for a flow with the given match and actions.

func (*Client) FlowDelete

func (c *Client) FlowDelete(tableID uint8, match []byte) error

FlowDelete sends an OFPT_FLOW_MOD OFPFC_DELETE (non-strict) for a flow matching match.

func (*Client) FlowDeleteOutPort

func (c *Client) FlowDeleteOutPort(portNo uint32) error

FlowDeleteOutPort deletes the flows of every table that output to the port number.

func (*Client) FlowDeleteStrict

func (c *Client) FlowDeleteStrict(tableID uint8, priority uint16, match []byte) error

FlowDeleteStrict deletes the one flow with exactly this match and priority.

func (*Client) FlowDeleteTable

func (c *Client) FlowDeleteTable(tableID uint8) error

FlowDeleteTable deletes every flow of a table.

func (*Client) PortNo

func (c *Client) PortNo(name string) (uint32, error)

PortNo returns the OpenFlow port number for a named port, or error if unknown.

func (*Client) Ports

func (c *Client) Ports() map[string]uint32

Ports returns a copy of the port map (name to number) as of the last refresh.

func (*Client) RefreshPorts

func (c *Client) RefreshPorts() error

RefreshPorts re-sends PORT_DESC multipart and updates the portMap.

func (*Client) SetErrorHandler

func (c *Client) SetErrorHandler(h func(xid uint32, errType, errCode uint16))

SetErrorHandler registers a callback for asynchronous OFPT_ERROR messages (rejected FLOW_MODs, etc.). Must be set before flows are programmed.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL