dhcplb

package
v0.0.0-...-838dcff Latest Latest
Warning

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

Go to latest
Published: Oct 20, 2021 License: MIT Imports: 24 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ErrUnknown  = "E_UNKNOWN"
	ErrPanic    = "E_PANIC"
	ErrRead     = "E_READ"
	ErrConnect  = "E_CONN"
	ErrWrite    = "E_WRITE"
	ErrGi0      = "E_GI_0"
	ErrParse    = "E_PARSE"
	ErrNoServer = "E_NO_SERVER"
	ErrConnRate = "E_CONN_RATE"
)

List of possible errors.

Variables

This section is empty.

Functions

func WatchConfig

func WatchConfig(
	configPath, overridesPath string, version int, provider ConfigProvider,
) (chan *Config, error)

WatchConfig will keep watching for changes to both config and override json files. It uses fsnotify library (it uses inotify in Linux), and call LoadConfig when it an inotify event signals the modification of the json files.

Types

type Config

type Config struct {
	Version              int
	Addr                 *net.UDPAddr
	Algorithm            DHCPBalancingAlgorithm
	ServerUpdateInterval time.Duration
	PacketBufSize        int
	Handler              Handler
	HostSourcer          DHCPServerSourcer
	RCRatio              uint32
	Overrides            map[string]Override
	Extras               interface{}
	CacheSize            int
	CacheRate            int
	Rate                 int
	ReplyAddr            *net.UDPAddr
}

Config represents the server configuration.

func LoadConfig

func LoadConfig(path, overridesPath string, version int, provider ConfigProvider) (*Config, error)

LoadConfig will take the path of the json file, the path of the override json file, an integer version and a ConfigProvider and will return a pointer to a Config object.

func ParseConfig

func ParseConfig(jsonConfig, jsonOverrides []byte, version int, provider ConfigProvider) (*Config, error)

ParseConfig will take JSON config files, a version and a ConfigProvider, and return a pointer to a Config struct

type ConfigProvider

type ConfigProvider interface {
	NewHostSourcer(
		sourcerType, args string, version int) (DHCPServerSourcer, error)
	ParseExtras(extras json.RawMessage) (interface{}, error)
	NewDHCPBalancingAlgorithm(version int) (DHCPBalancingAlgorithm, error)
	NewHandler(extras interface{}, version int) (Handler, error)
}

ConfigProvider is an interface which provides methods to fetch the HostSourcer, parse extra configuration, provide additional load balancing implementations and how to handle dhcp requests in server mode

type DHCPBalancingAlgorithm

type DHCPBalancingAlgorithm interface {
	SelectServerFromList(list []*DHCPServer, message *DHCPMessage) (*DHCPServer, error)
	SelectRatioBasedDhcpServer(message *DHCPMessage) (*DHCPServer, error)
	UpdateStableServerList(list []*DHCPServer) error
	UpdateRCServerList(list []*DHCPServer) error
	SetRCRatio(ratio uint32)
	// An unique name for the algorithm, this string can be used in the
	// configuration file, in the section where the algorithm is selecetd.
	Name() string
}

DHCPBalancingAlgorithm defines an interface for load balancing algorithms. Users can implement their own and add them to config.go (in the configSpec.algorithm method)

type DHCPMessage

type DHCPMessage struct {
	XID      []byte
	Peer     *net.UDPAddr
	ClientID []byte
	Mac      net.HardwareAddr
	Serial   string
}

DHCPMessage represents coordinates of a dhcp message.

type DHCPServer

type DHCPServer struct {
	Hostname string
	Address  net.IP
	Port     int
	IsRC     bool
}

DHCPServer holds information about a single dhcp server

func NewDHCPServer

func NewDHCPServer(hostname string, ip net.IP, port int) *DHCPServer

NewDHCPServer returns an instance of DHCPServer

func (*DHCPServer) String

func (d *DHCPServer) String() string

type DHCPServerSourcer

type DHCPServerSourcer interface {
	GetStableServers() ([]*DHCPServer, error)
	GetRCServers() ([]*DHCPServer, error)
	GetServersFromTier(tier string) ([]*DHCPServer, error)
}

DHCPServerSourcer is an interface used to fetch stable, rc and servers from a "tier" (group of servers).

type FileSourcer

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

FileSourcer holds various information about json the config files, list of stable and rc servers, the fsnotify Watcher and stuff needed for synchronization.

func NewFileSourcer

func NewFileSourcer(stablePath, rcPath string, version int) (*FileSourcer, error)

NewFileSourcer returns a new FileSourcer, stablePath and rcPath are the paths of the text files containing list of servers. If rcPath is empty it will be ignored, stablePath must be not null, version is the protocol version and should be either 4 or 6.

func (*FileSourcer) GetRCServers

func (fs *FileSourcer) GetRCServers() ([]*DHCPServer, error)

GetRCServers returns a list of rc dhcp servers

func (*FileSourcer) GetServersFromTier

func (fs *FileSourcer) GetServersFromTier(path string) ([]*DHCPServer, error)

GetServersFromTier returns a list of DHCPServer from a file

func (*FileSourcer) GetStableServers

func (fs *FileSourcer) GetStableServers() ([]*DHCPServer, error)

GetStableServers returns a list of stable dhcp servers

type Handler

type Handler interface {
	ServeDHCPv4(packet *dhcpv4.DHCPv4) (*dhcpv4.DHCPv4, error)
	ServeDHCPv6(packet dhcpv6.DHCPv6) (dhcpv6.DHCPv6, error)
}

Handler is an interface used while serving DHCP requests.

type LogMessage

type LogMessage struct {
	Version      int
	Packet       []byte
	Peer         *net.UDPAddr
	Server       string
	ServerIsRC   bool
	Latency      time.Duration
	Success      bool
	ErrorName    string
	ErrorDetails error
}

LogMessage holds the info of a log line.

type Override

type Override struct {
	// note that Host override takes precedence over Tier
	Host       string `json:"host"`
	Tier       string `json:"tier"`
	Expiration string `json:"expiration"`
}

Override represents the dhcp server or the group of dhcp servers (tier) we want to send packets to.

type Overrides

type Overrides struct {
	V4 map[string]Override `json:"v4"`
	V6 map[string]Override `json:"v6"`
}

Overrides is a struct that holds v4 and v6 list of overrides. The keys of the map are mac addresses.

type PersonalizedLogger

type PersonalizedLogger interface {
	Log(msg LogMessage) error
}

PersonalizedLogger is an interface used to log a LogMessage using your own logic. It will be used in loggerHelperImpl.

type Server

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

UDP acceptor

func NewServer

func NewServer(config *Config, serverMode bool, personalizedLogger PersonalizedLogger) (*Server, error)

NewServer initialized a Server before returning it.

func (*Server) GetConfig

func (s *Server) GetConfig() *Config

returns a pointer to the current config struct, so that if it does get changed while being used, it shouldn't affect the caller and this copy struct should be GC'ed when it falls out of scope

func (*Server) HasServers

func (s *Server) HasServers() bool

HasServers checks if the list of backend servers is not empty

func (*Server) ListenAndServe

func (s *Server) ListenAndServe() error

ListenAndServe starts the server

func (*Server) SetConfig

func (s *Server) SetConfig(config *Config)

SetConfig updates the server config

type Throttle

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

An LRU cache implementation of Throttle.

We keep track of request rates per client in an LRU cache to keep memory usage under control against malcious requests. Each value in the cache is a rate.Limiter struct which is an implementation of Taken Bucket algorithm.

Adding new items to the cache is also limited to control cache invalidation rate.

func NewThrottle

func NewThrottle(Capacity int, CacheRate int, MaxRatePerItem int) (*Throttle, error)

NewThrottle returns a Throttle struct

Capacity:
    Maximum capacity of the LRU cache

CacheRate (per second):
    Maximum allowed rate for adding new items to the cache. By that way it
    prevents the cache invalidation to happen too soon for the existing rate
    items in the cache. Cache rate will be infinite for 0 or negative values.

MaxRatePerItem (per second):
    Maximum allowed requests rate for each key in the cache. Throttling will
    be disabled for 0 or negative values. No cache will be created in that case.

func (*Throttle) OK

func (c *Throttle) OK(key interface{}) (bool, error)

Returns true if the rate is below maximum for the given key

Jump to

Keyboard shortcuts

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