Source-based routing in Golang
Find a file
Vitaliy Potyarkin 61946c217b
All checks were successful
Vulncheck / Vulncheck (pull_request) Successful in 1m18s
Tests and linters / Tests with -race (pull_request) Successful in 1m27s
Tests and linters / gopls check (pull_request) Successful in 1m34s
DCO action / DCO (pull_request) Successful in 2m12s
Pre-commit hooks / Pre-commit (pull_request) Successful in 2m43s
Tests and linters / Run gofumpt (pull_request) Successful in 2m42s
Tests and linters / Lint (pull_request) Successful in 2m53s
Tests and linters / Tests (pull_request) Successful in 2m53s
Tests and linters / Staticcheck (pull_request) Successful in 2m56s
[#12] Add Apache License
(close #12)

Signed-off-by: Vitaliy Potyarkin <v.potyarkin@yadro.com>
2024-11-08 12:28:23 +03:00
.forgejo/workflows [#11] .forgejo: Add CI/CD checks 2024-10-09 16:30:12 +03:00
.gitattributes [#11] git: Add gitattributes and gitignore files 2024-10-09 16:22:45 +03:00
.gitignore [#11] git: Add gitattributes and gitignore files 2024-10-09 16:22:45 +03:00
.golangci.yml [#11] makefile: Add static code analyzers 2024-10-09 16:22:46 +03:00
.pre-commit-config.yaml [#11] .forgejo: Add CI/CD checks 2024-10-09 16:30:12 +03:00
balancer.go [#11] dialer: Fix static check warnings 2024-10-09 16:22:47 +03:00
dialer.go [#11] dialer: Add event handler 2024-10-15 10:56:04 +03:00
dialer_hostname_test.go [#11] dialer: Use source IPs intead of interfaces 2024-10-09 16:22:45 +03:00
dialer_test.go [#11] dialer: Use source IPs intead of interfaces 2024-10-09 16:22:45 +03:00
go.mod [#11] mod: Bump deps versions 2024-10-09 16:22:46 +03:00
go.sum [#11] mod: Bump deps versions 2024-10-09 16:22:46 +03:00
LICENSE [#12] Add Apache License 2024-11-08 12:28:23 +03:00
Makefile [#11] makefile: Add static code analyzers 2024-10-09 16:22:46 +03:00
README.md [#12] Add Apache License 2024-11-08 12:28:23 +03:00

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.