multinet

package module
v0.0.0-...-1f47f5d Latest Latest
Warning

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

Go to latest
Published: Feb 17, 2025 License: Apache-2.0 Imports: 8 Imported by: 0

README

Source-based routing in Golang

Consider this routing table:

10.11.70.0/23 dev data0 proto kernel scope link src 10.11.70.42
10.11.70.0/23 dev data1 proto kernel scope link src 10.11.71.42
192.168.123.0/24 dev internal0 proto kernel scope link src 192.168.123.42
192.168.123.0/24 dev internal1 proto kernel scope link src 192.168.123.142

Simple net.Dial to either 10.11.70.42 or 10.11.71.42 will match the first subnet and be routed via data0. This problems is usually solved by bonds. But sometimes you need to invent a bicycle.

Usage

import (
    "context"
    "net"
    "net/netip"

    "git.frostfs.info/TrueCloudLab/multinet"
)

d, err := multinet.NewDialer(Config{
    Subnets:  []Subnet{
        {
            Prefix: netip.MustParsePrefix("10.11.70.0/23"),
            SourceIPs: []netip.Addr{
                netip.MustParseAddr("10.11.70.42"),
                netip.MustParseAddr("10.11.71.42"),
            },
        },
        {
            Prefix: netip.MustParsePrefix("192.168.123.0/24"),
            SourceIPs: []netip.Addr{
                netip.MustParseAddr("192.168.123.42"),
                netip.MustParseAddr("192.168.123.142"),
            },
        },
    },
    Balancer: multinet.BalancerTypeRoundRobin,
})
if err != nil {
    // handle error
}

conn, err := d.DialContext(ctx, "tcp", "10.11.70.42")
if err != nil {
    // handle error
}
// do stuff

Copyright 2023-2024 FrostFS contributors

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BalancerType

type BalancerType string

BalancerType reperents the algorithm which is used to pick source address.

const (
	// BalancerTypeNoop picks first address for which link is up.
	BalancerTypeNoop BalancerType = ""
	// BalancerTypeNoop implements simple round-robin between up links.
	// It is not fair in case some links are down.
	BalancerTypeRoundRobin BalancerType = "roundrobin"
)

type Config

type Config struct {
	// Routable subnets.
	Subnets []Subnet
	// If true, the only configurd subnets available through this dialer.
	// Otherwise, a failback to the net.DefaultDialer.
	Restrict bool
	// Dialer contains default options for the net.Dialer to use.
	// LocalAddr is overridden.
	Dialer net.Dialer
	// Balancer specifies algorithm used to pick source address.
	Balancer BalancerType
	// FallbackDelay specifies the length of time to wait before
	// spawning a RFC 6555 Fast Fallback connection. That is, this
	// is the amount of time to wait for IPv6 to succeed before
	// assuming that IPv6 is misconfigured and falling back to
	// IPv4.
	//
	// If zero, a default delay of 300ms is used.
	// A negative value disables Fast Fallback support.
	FallbackDelay time.Duration
	// DialContext is custom DialContext function.
	// If not specified, default implemenattion is used (`d.DialContext(ctx, network, address)`).
	DialContext func(d *net.Dialer, ctx context.Context, network, address string) (net.Conn, error)
	// EventHandler defines event handler.
	EventHandler EventHandler
}

Config contains Multidialer configuration.

type Dialer

type Dialer interface {
	DialContext(ctx context.Context, network, address string) (net.Conn, error)
}

Dialer contains the single most important method from the net.Dialer.

func NewDialer

func NewDialer(c Config) (Dialer, error)

NewDialer ...

type EventHandler

type EventHandler interface {
	DialPerformed(sourceIP net.Addr, network, address string, err error)
}

type Subnet

type Subnet struct {
	Prefix    netip.Prefix
	SourceIPs []netip.Addr
}

Subnet represents a single subnet, possibly routable from multiple source IPs.

Jump to

Keyboard shortcuts

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