ovs

package module
v0.9.0 Latest Latest
Warning

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

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

README

OVS Build Status GoDoc License

A simple OVS flow executor supporting Go1.18+.

Example

package main

import (
	"context"

	"github.com/xgfone/go-exec"
	"github.com/xgfone/go-ovs"
)

func main() {
	initBridge("br-ovs")
	initFlows("br-ovs")
}

func must(err error) {
	if err != nil {
		panic(err)
	}
}

func initBridge(bridge string) {
	must(exec.Execute(context.Background(),
		"ovs-vsctl", "--may-exist", "add-br", bridge,
		"--", "set-fail-mode", bridge, "secure",
	))

	must(exec.Execute(context.Background(),
		"ip", "link", "set", bridge, "up",
	))
}

func initFlows(bridge string) {
	// Table 0
	ovs.MustAddFlow(bridge, "table=0,in_port=1,actions=goto_table:1")
	ovs.MustAddFlow(bridge, "table=0,in_port=2,actions=goto_table:2")
	ovs.MustAddFlow(bridge, "table=0,in_port=3,actions=goto_table:3")
	ovs.MustAddFlow(bridge, "table=0,in_port=4,actions=goto_table:4")
	ovs.MustAddFlow(bridge, "table=0,priority=0,actions=drop")

	// TODO ...
}

Documentation

Overview

Package ovs supplies some convenient functions to execute the ovs command.

Index

Examples

Constants

View Source
const (
	ARP  = 0x0806
	IPv4 = 0x0800
	IPv6 = 0x86dd
)

L2 Data-Link Protocol Number

View Source
const (
	ICMP = 1
	TCP  = 6
	UDP  = 17
	GRE  = 47
)

L3 IP Protocol Number

View Source
const (
	DROP   = "drop"
	LOCAL  = "local"
	FLOOD  = "flood"
	NORMAL = "normal"
)

OVS Actions

View Source
const (
	BroadcastMac = "ff:ff:ff:ff:ff:ff"
)

MAC address

Variables

View Source
var (
	IPCmd    = "ip"
	OfctlCmd = "ovs-ofctl"
	VsctlCmd = "ovs-vsctl"
)

Some linux commands.

Functions

func AddFlows

func AddFlows(bridge string, flows ...string) (err error)

AddFlows adds the flows.

func AddPatchPort added in v0.5.0

func AddPatchPort(bridge, patch, peerPatch string, ofport int) (err error)

AddPatchPort adds a patch port for the bridge, the peer patch of which is peerPatch.

func AddPort added in v0.5.0

func AddPort(bridge, iface string, ofport int) (err error)

AddPort adds the interface to the bridge.

func AddVxLANPort added in v0.5.0

func AddVxLANPort(bridge, port, localIP, remoteIP string, ofport int) (err error)

AddVxLANPort add an VxLAN port into the bridge.

func CreateBridge added in v0.5.0

func CreateBridge(name string, secureFailMode ...bool) (err error)

CreateBridge creates a new bridge named name if not exist.

If secureFailMode is true, set the fail mode of the bridge to "secure".

func DelFlows

func DelFlows(bridge string, matches ...string) (err error)

DelFlows deletes the flows.

func DelFlowsStrict

func DelFlowsStrict(bridge string, priority int, matches ...string) (err error)

DelFlowsStrict deletes the flows with the option --strict.

func DelPort added in v0.5.0

func DelPort(bridge, port string) (err error)

DelPort deletes the port from the bridge.

func DeleteBridge added in v0.5.0

func DeleteBridge(name string) (err error)

DeleteBridge deletes the bridge named name.

func GetAllFlows

func GetAllFlows(bridge string, isName, isStats bool) (flows []string, err error)

GetAllFlows returns the list of all the flows of the bridge.

func IntToHexString added in v0.5.0

func IntToHexString(i int) string

IntToHexString converts the integer to string as the hexdecimal with the prefix "0x".

func IntToString added in v0.5.0

func IntToString(i int) string

IntToString converts the integer to string as the decimal.

func ListAllOFPorts added in v0.5.0

func ListAllOFPorts(bridge string) (map[string]int, error)

ListAllOFPorts returns all the port names with its number on the bridge.

func MustAddFlow

func MustAddFlow(bridge, flow string)

MustAddFlow is the same as AddFlows, but the program exits if there is an error.

func MustAddPatchPort added in v0.5.0

func MustAddPatchPort(bridge, patch, peerPatch string, ofport int)

MustAddPatchPort is the same as AddPatchPort, but exit the program if failing.

func MustAddPort added in v0.5.0

func MustAddPort(bridge, iface string, ofport int)

MustAddPort is the same as AddPort, but exit the program if failing.

func MustAddVxLANPort added in v0.5.0

func MustAddVxLANPort(bridge, port, localIP, remoteIP string, ofport int)

MustAddVxLANPort is the same as AddVxLANPort, but exit the program if failing.

func MustCreateBridge added in v0.5.0

func MustCreateBridge(name string, secureFailMode ...bool)

MustCreateBridge is the same as CreateBridge, but exit the program if failing.

func MustDelFlow

func MustDelFlow(bridge, match string)

MustDelFlow is the same as DelFlows, but the program exits if there is an error.

func MustDelFlowStrict

func MustDelFlowStrict(bridge string, priority int, match string)

MustDelFlowStrict is the same as DelFlowsStrict, but the program exits if there is an error.

func MustDelPort added in v0.5.0

func MustDelPort(bridge, iface string)

MustDelPort is the same as DelPort, but exit the program if failing.

func MustDeleteBridge added in v0.5.0

func MustDeleteBridge(name string)

MustDeleteBridge is the same as DeleteBridge, but exit the program if failing.

func MustSetInterfaceUp added in v0.5.0

func MustSetInterfaceUp(iface string)

MustSetInterfaceUp is the same as SetInterfaceUp, but exit the program if failing.

func PortRuleMasking added in v0.2.0

func PortRuleMasking(minPort, maxPort int) []string

PortRuleMasking translates a range [port_min, port_max] into a set of bitwise matches.

Each match has the form 'port/mask'. The port and mask are 16-bit numbers written in hexadecimal prefixed by 0x. Each 1-bit in mask requires that the corresponding bit in port must match. Each 0-bit in mask causes the corresponding bit to be ignored.

Example
ports := PortRuleMasking(1000, 1999)
fmt.Println(len(ports))
fmt.Println(ports[0])
fmt.Println(ports[1])
fmt.Println(ports[2])
fmt.Println(ports[3])
fmt.Println(ports[4])
fmt.Println(ports[5])
fmt.Println(ports[6])
Output:

7
0x03e8/0xfff8
0x03f0/0xfff0
0x0400/0xfe00
0x0600/0xff00
0x0700/0xff80
0x0780/0xffc0
0x07c0/0xfff0

func SendARPRequest

func SendARPRequest(bridge, output, inPort, srcMac, srcIP, dstIP string,
	vlanID ...uint16) (err error)

SendARPRequest sends the ARP request by the ovs bridge.

vlanID may be 0, which won't add the VLAN header into the ARP request packet.

func SetInterfaceUp added in v0.5.0

func SetInterfaceUp(iface string) (err error)

SetInterfaceUp sets up the interface.

func StringToInt added in v0.5.0

func StringToInt(s string) int

StringToInt parses the decimal or hexadecimal string to the integer, which will panic if failing.

Types

This section is empty.

Jump to

Keyboard shortcuts

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