bridge

package
v2.0.0-...-f0b7fcc Latest Latest
Warning

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

Go to latest
Published: Mar 26, 2024 License: GPL-3.0 Imports: 20 Imported by: 0

Documentation

Overview

This package provides a singleton bridge object that wraps openvswitch. It allows the programmatic creation and deletion of bridges and taps, packet captures, applying qos constraints, and adding tunnels and trunks.

It also tracks information about taps such as recent bandwidth stats and snoops on traffic to the identify IP addresses associated with them.

Index

Constants

View Source
const DefaultSnapLen = 1600

Variables

View Source
var ErrNoNetflow = errors.New("bridge has no netflow object")
View Source
var ExternalDependencies = []string{
	"ip",
	"ovs-vsctl",
	"ovs-ofctl",
	"tc",
}

Functions

func CheckOVS

func CheckOVS() error

CheckOVS runs a simple openvswitch command to test whether openvswitch is running or not.

func DestroyBridge

func DestroyBridge(name string) error

DestroyBridge deletes an `unmanaged` bridge. This can be used when cleaning up from a crash. See `Bride.Destroy` for managed bridges.

func DestroyTap

func DestroyTap(name string) error

DestroyTap destroys an `unmanaged` tap using the `ip` command. This can be used when cleaning up from a crash or when a tap is not connected to a bridges. See `Bridge.DestroyTap` for managed taps.

Types

type Bridge

type Bridge struct {
	Name string
	// contains filtered or unexported fields
}

Bridge stores state about an openvswitch bridge including the taps, tunnels, trunks, and netflow.

func (*Bridge) AddBond

func (b *Bridge) AddBond(name, mode, lacp string, fallback bool, interfaces map[string]int, vlan int) (string, error)

func (*Bridge) AddTap

func (b *Bridge) AddTap(tap, mac string, lan int, host bool) error

AddTap adds an existing tap to the bridge. Can be used in conjunction with `Bridge.RemoveTap` to relocate tap to a different bridge or VLAN.

func (*Bridge) AddTrunk

func (b *Bridge) AddTrunk(iface string) error

AddTrunk add an existing interface as a trunk port to the bridge.

func (*Bridge) AddTunnel

func (b *Bridge) AddTunnel(typ TunnelType, remoteIP, key string) error

AddTunnel adds a new vxlan or GRE tunnel to a bridge.

func (*Bridge) Capture

func (b *Bridge) Capture(fname string, config ...CaptureConfig) (int, error)

Capture traffic from a bridge to fname. Only the first config is used, if there is more than one. Returns an ID which can be passed to RemoveCapture to stop the capture.

func (*Bridge) CaptureTap

func (b *Bridge) CaptureTap(tap, fname string, config ...CaptureConfig) (int, error)

CaptureTap captures traffic for the specified tap to fname. Only the first config is used, if there is more than one. Returns an ID which can be passed to RemoveCapture to stop the capture.

func (*Bridge) Config

func (b *Bridge) Config(s string) error

func (*Bridge) CreateBondName

func (b *Bridge) CreateBondName() string

CreateBondName will return the next created tap name from the name channel

func (*Bridge) CreateContainerTap

func (b *Bridge) CreateContainerTap(tap, ns, mac string, vlan, index int) (string, error)

CreateContainerTap creates a veth tap and adds it to the bridge. tap is the name of the tap, it will be automatically generated if unspecified. ns is the network namespace for the tap. mac is the MAC address to assign to the interface. vlan is the VLAN for the traffic. index is the veth interface number for the container.

func (*Bridge) CreateHostTap

func (b *Bridge) CreateHostTap(tap string, lan int) (string, error)

CreateHostTap creates and adds a host tap to a bridge. If a name is not provided, one will be automatically generated.

func (*Bridge) CreateMirror

func (b *Bridge) CreateMirror(src, dst string) error

CreateMirror mirrors traffic. src is the tap to mirror, an empty src implies mirroring the entire bridge. dst is the tap to mirror to, which must already exist.

func (*Bridge) CreateTap

func (b *Bridge) CreateTap(tap, mac string, vlan int) (string, error)

CreateTap creates a new tap and adds it to the bridge. mac is the MAC address to assign to the interface. vlan is the VLAN for the traffic. If a name is not provided, one will be automatically generated

func (*Bridge) CreateTapName

func (b *Bridge) CreateTapName() string

CreateTapName will return the next created tap name from the name channel

func (*Bridge) DeleteBond

func (b *Bridge) DeleteBond(name string) error

func (*Bridge) DestroyMirror

func (b *Bridge) DestroyMirror(tap string) error

func (*Bridge) DestroyNetflow

func (b *Bridge) DestroyNetflow() error

DestroyNetflow destroys the active netflow.

func (*Bridge) DestroyTap

func (b *Bridge) DestroyTap(tap string) error

DestroyTap removes a tap from the bridge and marks it as defunct. See `Bridge.ReapTaps` to clean up defunct taps. If the tap is a mirror, it cleans up the mirror too.

func (*Bridge) GetNetflow

func (b *Bridge) GetNetflow() (*gonetflow.Netflow, error)

GetNetflow returns the active netflow for the bridge.

func (*Bridge) GetQos

func (b *Bridge) GetQos(tap string) []QosOption

func (*Bridge) NewNetflow

func (b *Bridge) NewNetflow(timeout int) (*gonetflow.Netflow, error)

NewNetflow creates a new netflow for the bridge.

func (*Bridge) ReapTaps

func (b *Bridge) ReapTaps() error

ReapTap should be called periodically to remove defunct taps.

func (*Bridge) RecoverBond

func (b *Bridge) RecoverBond(name string, interfaces map[string]int) error

func (*Bridge) RecoverTap

func (b *Bridge) RecoverTap(tap, mac string, lan int, host bool) error

func (*Bridge) RemoveQos

func (b *Bridge) RemoveQos(tap string) error

func (*Bridge) RemoveTap

func (b *Bridge) RemoveTap(tap string) error

RemoveTap removes a tap from the bridge but doesn't remove the underlying device so that it may be added to another bridge. See `Bridge.AddTap`.

func (*Bridge) RemoveTrunk

func (b *Bridge) RemoveTrunk(iface string) error

RemoveTrunk removes a trunk port from the bridge.

func (*Bridge) RemoveTunnel

func (b *Bridge) RemoveTunnel(iface string) error

RemoveTunnel removes a tunnel from the bridge.

func (*Bridge) SetNetflowTimeout

func (b *Bridge) SetNetflowTimeout(timeout int) error

SetNetflowTimeout updates the timeout on the active netflow.

func (*Bridge) SetTapQinQ

func (b *Bridge) SetTapQinQ(tap string, outer int) error

SetTapQinQ updates the port table in OVS to set the VLAN mode for the given tap to dot1q-tunnel and the outer VLAN tag for the tunnel to the given VLAN ID.

func (*Bridge) StopCapture

func (b *Bridge) StopCapture(id int) error

func (*Bridge) UpdateQos

func (b *Bridge) UpdateQos(tap string, op QosOption) error

type BridgeInfo

type BridgeInfo struct {
	Name     string
	PreExist bool
	VLANs    []int
	Trunks   []string
	Tunnels  []string
	Mirrors  []string
	Bonds    map[string][]string
	Config   map[string]string
}

BridgeInfo is a summary of fields from a Bridge.

type Bridges

type Bridges struct {
	Default string // Default bridge name when one isn't specified
	// contains filtered or unexported fields
}

Bridges manages a collection of `Bridge` structs.

func NewBridges

func NewBridges(d, tf, bf string) *Bridges

NewBridges creates a new Bridges using d as the default bridge name, tf as the format string for the tap names (e.g. "mega_tap%v"), and bf as the format string for the bond names (e.g. "mega_bond%v").

func (Bridges) BandwidthStats

func (b Bridges) BandwidthStats() (float64, float64)

BandwidthStats computes the sum of the rates of MB received and transmitted across all taps on the given bridge.

func (Bridges) Destroy

func (b Bridges) Destroy() error

Destroy calls `Bridge.Destroy` on each bridge, returning the first error.

func (Bridges) DestroyBridge

func (b Bridges) DestroyBridge(name string) error

DestroyBridge destroys a bridge by name, removing all of the taps, etc. associated with it.

func (Bridges) FindTap

func (b Bridges) FindTap(t string) (Tap, error)

FindTap finds a non-defunct Tap with the specified name. This is non-deterministic if there are multiple taps with the same name.

func (Bridges) Get

func (b Bridges) Get(name string) (*Bridge, error)

Get a bridge by name. If one doesn't exist, it will be created.

func (Bridges) HostTaps

func (b Bridges) HostTaps() []Tap

HostTaps returns a list of taps that are marked as host taps.

func (Bridges) Info

func (b Bridges) Info() []BridgeInfo

Info collects `BridgeInfo` for all managed bridges.

func (Bridges) Names

func (b Bridges) Names() []string

Names returns a list of all the managed bridge names.

func (Bridges) ReapTaps

func (b Bridges) ReapTaps() error

ReapTaps calls `Bridge.ReapTaps` on each bridge, returning the first error.

type CaptureConfig

type CaptureConfig struct {
	// Filter is a BPF string to apply to all packets. See `man pcap-filter`
	// for the syntax and semantics.
	Filter string

	// SnapLen controls how many bytes to capture for each packet. According to
	// `man pcap`, 65535 should be sufficient to capture full packets on most
	// networks. If you only need headers, you can set it much lower (i.e.
	// 200). When zero, we use DefaultSnapLen.
	SnapLen uint32
}

type QosOption

type QosOption struct {
	Type  QosType
	Value string
}

type QosType

type QosType int

Qos option types

const (
	Rate QosType = iota
	Loss
	Delay
)

type Tap

type Tap struct {
	Name      string // Name of the tap
	Bridge    string // Bridge that the tap is connected to
	VLAN      int    // VLAN ID for the tap
	MAC       string // MAC address
	Host      bool   // Set when created as a host tap (and, thus, promiscuous)
	Container bool   // Set when created via CreateContainerTap
	Bond      string // Bond that the tap belongs to
	Defunct   bool   // Set when Tap should be reaped

	IP4 string // Snooped IPv4 address
	IP6 string // Snooped IPv6 address
	// contains filtered or unexported fields
}

Tap represents an interface that is attached to an openvswitch bridge.

func (Tap) BandwidthStats

func (t Tap) BandwidthStats() (float64, float64)

BandwidthStats computes the average rate of MB received and transmitted on the given tap over the 10 previous 5 second intervals. Returns the received and transmitted rates, in MBps.

type TunnelType

type TunnelType int

TunnelType is used to specify the type of tunnel for `AddTunnel`.

const (
	TunnelVXLAN TunnelType = iota
	TunnelGRE
)

func (TunnelType) String

func (t TunnelType) String() string

Jump to

Keyboard shortcuts

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