stack

package
v0.0.0-...-a6dae5b Latest Latest
Warning

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

Go to latest
Published: Jun 7, 2017 License: BSD-3-Clause Imports: 10 Imported by: 0

Documentation

Overview

Package stack provides the glue between networking protocols and the consumers of the networking stack.

For consumers, the only function of interest is New(), everything else is provided by the tcpip/public package.

For protocol implementers, RegisterTransportProtocol() and RegisterNetworkProtocol() are used to register protocols with the stack, which will then be instantiated when consumers interact with the stack.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func New

func New(network []string, transport []string) tcpip.Stack

New allocates a new networking stack with only the requested networking and transport protocols.

func RegisterLinkEndpoint

func RegisterLinkEndpoint(linkEP LinkEndpoint) tcpip.LinkEndpointID

RegisterLinkEndpoint register a link-layer protocol endpoint and returns an ID that can be used to refer to it.

func RegisterNetworkProtocol

func RegisterNetworkProtocol(name string, p NetworkProtocol)

RegisterNetworkProtocol registers a new network protocol with the stack so that it becomes available to users of the stack. This function is intended to be called by init() functions of the protocols.

func RegisterTransportProtocol

func RegisterTransportProtocol(name string, p TransportProtocol)

RegisterTransportProtocol registers a new transport protocol with the stack so that it becomes available to users of the stack. This function is intended to be called by init() functions of the protocols.

Types

type LinkAddressCache

type LinkAddressCache interface {
	// CheckLocalAddress determines if the given local address exists, and if it
	// does, returns the id of the NIC it's bound to. Returns 0 if the address
	// does not exist.
	CheckLocalAddress(nicid tcpip.NICID, addr tcpip.Address) tcpip.NICID

	// AddLinkAddress adds a link address to the cache.
	AddLinkAddress(nicid tcpip.NICID, addr tcpip.Address, linkAddr tcpip.LinkAddress)
}

A LinkAddressCache caches link addresses.

type LinkAddressResolver

type LinkAddressResolver interface {
	// LinkAddressRequest sends a request for the LinkAddress of addr.
	// The request is sent on linkEP with localAddr as the source.
	//
	// A valid response will cause the discovery protocol's network
	// endpoint to call AddLinkAddress.
	LinkAddressRequest(addr, localAddr tcpip.Address, linkEP LinkEndpoint) error

	// LinkAddressProtocol returns the network protocol of the
	// addresses this this resolver can resolve.
	LinkAddressProtocol() tcpip.NetworkProtocolNumber
}

A LinkAddressResolver is an extension to a NetworkProtocol that can resolve link addresses.

type LinkEndpoint

type LinkEndpoint interface {
	// MTU is the maximum transmission unit for this endpoint. This is
	// usually dictated by the backing physical network; when such a
	// physical network doesn't exist, the limit is generally 64k, which
	// includes the maximum size of an IP packet.
	MTU() uint32

	// MaxHeaderLength returns the maximum size the data link (and
	// lower level layers combined) headers can have. Higher levels use this
	// information to reserve space in the front of the packets they're
	// building.
	MaxHeaderLength() uint16

	// LinkAddress returns the link address (typically a MAC) of the
	// link endpoint.
	LinkAddress() tcpip.LinkAddress

	// WritePacket writes a packet with the given protocol through the given
	// route.
	WritePacket(r *Route, hdr *buffer.Prependable, payload buffer.View, protocol tcpip.NetworkProtocolNumber) error

	// Attach attaches the data link layer endpoint to the network-layer
	// dispatcher of the stack.
	Attach(dispatcher NetworkDispatcher)
}

LinkEndpoint is the interface implemented by data link layer protocols (e.g., ethernet, loopback, raw) and used by network layer protocols to send packets out through the implementer's data link endpoint.

func FindLinkEndpoint

func FindLinkEndpoint(id tcpip.LinkEndpointID) LinkEndpoint

FindLinkEndpoint finds the link endpoint associated with the given ID.

type NIC

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

NIC represents a "network interface card" to which the networking stack is attached.

func (*NIC) AddAddress

func (n *NIC) AddAddress(protocol tcpip.NetworkProtocolNumber, addr tcpip.Address) error

AddAddress adds a new address to n, so that it starts accepting packets targeted at the given address (and network protocol).

func (*NIC) AddSubnet

func (n *NIC) AddSubnet(protocol tcpip.NetworkProtocolNumber, subnet tcpip.Subnet)

AddSubnet adds a new subnet to n, so that it starts accepting packets targeted at the given address and network protocol.

func (*NIC) DeliverNetworkPacket

func (n *NIC) DeliverNetworkPacket(linkEP LinkEndpoint, remoteLinkAddr tcpip.LinkAddress, protocol tcpip.NetworkProtocolNumber, vv *buffer.VectorisedView)

DeliverNetworkPacket finds the appropriate network protocol endpoint and hands the packet over for further processing. This function is called when the NIC receives a packet from the physical interface. Note that the ownership of the slice backing vv is retained by the caller. This rule applies only to the slice itself, not to the items of the slice; the ownership of the items is not retained by the caller.

func (*NIC) DeliverTransportPacket

func (n *NIC) DeliverTransportPacket(r *Route, protocol tcpip.TransportProtocolNumber, vv *buffer.VectorisedView)

DeliverTransportPacket delivers the packets to the appropriate transport protocol endpoint.

func (*NIC) ID

func (n *NIC) ID() tcpip.NICID

ID returns the identifier of n.

func (*NIC) RemoveAddress

func (n *NIC) RemoveAddress(addr tcpip.Address) error

RemoveAddress removes an address from n.

func (*NIC) Subnets

func (n *NIC) Subnets() []tcpip.Subnet

Subnets returns the Subnets associated with this NIC.

type NetworkDispatcher

type NetworkDispatcher interface {
	// DeliverNetworkPacket finds the appropriate network protocol
	// endpoint and hands the packet over for further processing.
	DeliverNetworkPacket(linkEP LinkEndpoint, remoteLinkAddr tcpip.LinkAddress, protocol tcpip.NetworkProtocolNumber, vv *buffer.VectorisedView)
}

NetworkDispatcher contains the methods used by the network stack to deliver packets to the appropriate network endpoint after it has been handled by the data link layer.

type NetworkEndpoint

type NetworkEndpoint interface {
	// MTU is the maximum transmission unit for this endpoint. This is
	// generally calculated as the MTU of the underlying data link endpoint
	// minus the network endpoint max header length.
	MTU() uint32

	// MaxHeaderLength returns the maximum size the network (and lower
	// level layers combined) headers can have. Higher levels use this
	// information to reserve space in the front of the packets they're
	// building.
	MaxHeaderLength() uint16

	// WritePacket writes a packet to the given destination address and
	// protocol.
	WritePacket(r *Route, hdr *buffer.Prependable, payload buffer.View, protocol tcpip.TransportProtocolNumber) error

	// ID returns the network protocol endpoint ID.
	ID() *NetworkEndpointID

	// NICID returns the id of the NIC this endpoint belongs to.
	NICID() tcpip.NICID

	// HandlePacket is called by the link layer when new packets arrive to
	// this network endpoint.
	HandlePacket(r *Route, vv *buffer.VectorisedView)

	// Close is called when the endpoint is reomved from a stack.
	Close()
}

NetworkEndpoint is the interface that needs to be implemented by endpoints of network layer protocols (e.g., ipv4, ipv6).

type NetworkEndpointID

type NetworkEndpointID struct {
	LocalAddress tcpip.Address
}

NetworkEndpointID is the identifier of a network layer protocol endpoint. Currently the local address is sufficient because all supported protocols (i.e., IPv4 and IPv6) have different sizes for their addresses.

type NetworkProtocol

type NetworkProtocol interface {
	// Number returns the network protocol number.
	Number() tcpip.NetworkProtocolNumber

	// MinimumPacketSize returns the minimum valid packet size of this
	// network protocol. The stack automatically drops any packets smaller
	// than this targeted at this protocol.
	MinimumPacketSize() int

	// ParsePorts returns the source and destination addresses stored in a
	// packet of this protocol.
	ParseAddresses(v buffer.View) (src, dst tcpip.Address)

	// NewEndpoint creates a new endpoint of this protocol.
	NewEndpoint(nicid tcpip.NICID, addr tcpip.Address, linkAddrCache LinkAddressCache, dispatcher TransportDispatcher, sender LinkEndpoint) (NetworkEndpoint, error)
}

NetworkProtocol is the interface that needs to be implemented by network protocols (e.g., ipv4, ipv6) that want to be part of the networking stack.

type Route

type Route struct {
	// RemoteAddress is the final destination of the route.
	RemoteAddress tcpip.Address

	// RemoteLinkAddress is the link-layer (MAC) address of the
	// final destination of the route.
	RemoteLinkAddress tcpip.LinkAddress

	// LocalAddress is the local address where the route starts.
	LocalAddress tcpip.Address

	// LocalLinkAddress is the link-layer (MAC) address of the
	// where the route starts.
	LocalLinkAddress tcpip.LinkAddress

	// NextHop is the next node in the path to the destination.
	NextHop tcpip.Address

	// NetProto is the network-layer protocol.
	NetProto tcpip.NetworkProtocolNumber
	// contains filtered or unexported fields
}

Route represents a route through the networking stack to a given destination.

func (*Route) Clone

func (r *Route) Clone() Route

Clone Clone a route such that the original one can be released and the new one will remain valid.

func (*Route) MTU

func (r *Route) MTU() uint32

MTU returns the MTU of the underlying network endpoint.

func (*Route) MaxHeaderLength

func (r *Route) MaxHeaderLength() uint16

MaxHeaderLength forwards the call to the network endpoint's implementation.

func (*Route) NICID

func (r *Route) NICID() tcpip.NICID

NICID returns the id of the NIC from which this route originates.

func (*Route) PseudoHeaderChecksum

func (r *Route) PseudoHeaderChecksum(protocol tcpip.TransportProtocolNumber) uint16

PseudoHeaderChecksum forwards the call to the network endpoint's implementation.

func (*Route) Release

func (r *Route) Release()

Release frees all resources associated with the route.

func (*Route) WritePacket

func (r *Route) WritePacket(hdr *buffer.Prependable, payload buffer.View, protocol tcpip.TransportProtocolNumber) error

WritePacket writes the packet through the given route.

type Stack

type Stack struct {
	*ports.PortManager
	// contains filtered or unexported fields
}

Stack is a networking stack, with all supported protocols, NICs, and route table.

func (*Stack) AddAddress

func (s *Stack) AddAddress(id tcpip.NICID, protocol tcpip.NetworkProtocolNumber, addr tcpip.Address) error

AddAddress adds a new network-layer address to the specified NIC.

func (*Stack) AddLinkAddress

func (s *Stack) AddLinkAddress(nicid tcpip.NICID, addr tcpip.Address, linkAddr tcpip.LinkAddress)

AddLinkAddress adds a link address to the stack link cache.

func (*Stack) AddSubnet

func (s *Stack) AddSubnet(id tcpip.NICID, protocol tcpip.NetworkProtocolNumber, subnet tcpip.Subnet) error

AddSubnet adds a subnet range to the specified NIC.

func (*Stack) CheckLocalAddress

func (s *Stack) CheckLocalAddress(nicid tcpip.NICID, addr tcpip.Address) tcpip.NICID

CheckLocalAddress determines if the given local address exists, and if it does, returns the id of the NIC it's bound to. Returns 0 if the address does not exist.

func (*Stack) CheckNetworkProtocol

func (s *Stack) CheckNetworkProtocol(protocol tcpip.NetworkProtocolNumber) bool

CheckNetworkProtocol checks if a given network protocol is enabled in the stack.

func (*Stack) CreateDisabledNIC

func (s *Stack) CreateDisabledNIC(id tcpip.NICID, linkEP tcpip.LinkEndpointID) error

CreateDisabledNIC creates a NIC with the provided id and link-layer endpoint, but leave it disable. Stack.EnableNIC must be called before the link-layer endpoint starts delivering packets to it.

func (*Stack) CreateNIC

func (s *Stack) CreateNIC(id tcpip.NICID, linkEP tcpip.LinkEndpointID) error

CreateNIC creates a NIC with the provided id and link-layer endpoint.

func (*Stack) EnableNIC

func (s *Stack) EnableNIC(id tcpip.NICID) error

EnableNIC enables the given NIC so that the link-layer endpoint can start delivering packets to it.

func (*Stack) FindRoute

func (s *Stack) FindRoute(id tcpip.NICID, localAddr, remoteAddr tcpip.Address, netProto tcpip.NetworkProtocolNumber) (Route, error)

FindRoute creates a route to the given destination address, leaving through the given nic and local address (if provided).

func (*Stack) MutableStats

func (s *Stack) MutableStats() *tcpip.Stats

MutableStats returns a mutable copy of the current stats.

This is not generally exported via the public interface, but is available internally.

func (*Stack) NICSubnets

func (s *Stack) NICSubnets() map[tcpip.NICID][]tcpip.Subnet

NICSubnets returns a map of NICIDs to their associated subnets.

func (*Stack) NewEndpoint

func (s *Stack) NewEndpoint(transport tcpip.TransportProtocolNumber, network tcpip.NetworkProtocolNumber, waiterQueue *waiter.Queue) (tcpip.Endpoint, error)

NewEndpoint creates a new transport layer endpoint of the given protocol.

func (*Stack) RegisterTransportEndpoint

func (s *Stack) RegisterTransportEndpoint(nicID tcpip.NICID, netProtos []tcpip.NetworkProtocolNumber, protocol tcpip.TransportProtocolNumber, id TransportEndpointID, ep TransportEndpoint) error

RegisterTransportEndpoint registers the given endpoint with the stack transport dispatcher. Received packets that match the provided id will be delivered to the given endpoint; specifying a nic is optional, but nic-specific IDs have precedence over global ones.

func (*Stack) RemoveAddress

func (s *Stack) RemoveAddress(id tcpip.NICID, addr tcpip.Address) error

RemoveAddress removes an existing network-layer address from the specified NIC.

func (*Stack) SetPromiscuousMode

func (s *Stack) SetPromiscuousMode(nicID tcpip.NICID, enable bool) error

SetPromiscuousMode enables or disables promiscuous mode in the given NIC.

func (*Stack) SetRouteTable

func (s *Stack) SetRouteTable(table []tcpip.Route)

SetRouteTable assigns the route table to be used by this stack. It specifies which NIC to use for a given destination address mask.

func (*Stack) SetTransportProtocolHandler

func (s *Stack) SetTransportProtocolHandler(p tcpip.TransportProtocolNumber, h func(*Route, TransportEndpointID, *buffer.VectorisedView) bool)

SetTransportProtocolHandler sets the per-stack default handler for the given protocol.

It must be called only during initialization of the stack. Changing it as the stack is operating is not supported.

func (*Stack) Stats

func (s *Stack) Stats() tcpip.Stats

Stats returns a snapshot of the current stats.

func (*Stack) UnregisterTransportEndpoint

func (s *Stack) UnregisterTransportEndpoint(nicID tcpip.NICID, netProtos []tcpip.NetworkProtocolNumber, protocol tcpip.TransportProtocolNumber, id TransportEndpointID)

UnregisterTransportEndpoint removes the endpoint with the given id from the stack transport dispatcher.

type TransportDispatcher

type TransportDispatcher interface {
	// DeliverTransportPacket delivers the packets to the appropriate
	// transport protocol endpoint.
	DeliverTransportPacket(r *Route, protocol tcpip.TransportProtocolNumber, vv *buffer.VectorisedView)
}

TransportDispatcher contains the methods used by the network stack to deliver packets to the appropriate transport endpoint after it has been handled by the network layer.

type TransportEndpoint

type TransportEndpoint interface {
	// HandlePacket is called by the stack when new packets arrive to
	// this transport endpoint.
	HandlePacket(r *Route, id TransportEndpointID, vv *buffer.VectorisedView)
}

TransportEndpoint is the interface that needs to be implemented by transport protocol (e.g., tcp, udp) endpoints that can handle packets.

type TransportEndpointID

type TransportEndpointID struct {
	// LocalPort is the local port associated with the endpoint.
	LocalPort uint16

	// LocalAddress is the local [network layer] address associated with
	// the endpoint.
	LocalAddress tcpip.Address

	// RemotePort is the remote port associated with the endpoint.
	RemotePort uint16

	// RemoteAddress it the remote [network layer] address associated with
	// the endpoint.
	RemoteAddress tcpip.Address
}

TransportEndpointID is the identifier of a transport layer protocol endpoint.

type TransportProtocol

type TransportProtocol interface {
	// Number returns the transport protocol number.
	Number() tcpip.TransportProtocolNumber

	// NewEndpoint creates a new endpoint of the transport protocol.
	NewEndpoint(stack *Stack, netProto tcpip.NetworkProtocolNumber, waitQueue *waiter.Queue) (tcpip.Endpoint, error)

	// MinimumPacketSize returns the minimum valid packet size of this
	// transport protocol. The stack automatically drops any packets smaller
	// than this targeted at this protocol.
	MinimumPacketSize() int

	// ParsePorts returns the source and destination ports stored in a
	// packet of this protocol.
	ParsePorts(v buffer.View) (src, dst uint16, err error)

	// HandleUnknownDestinationPacket handles packets targeted at this
	// protocol but that don't match any existing endpoint. For example,
	// it is targeted at a port that have no listeners.
	//
	// The return value indicates whether the packet was well-formed (for
	// stats purposes only).
	HandleUnknownDestinationPacket(r *Route, id TransportEndpointID, vv *buffer.VectorisedView) bool
}

TransportProtocol is the interface that needs to be implemented by transport protocols (e.g., tcp, udp) that want to be part of the networking stack.

Jump to

Keyboard shortcuts

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