Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

unify path types in combinator, segutil, sciond #3893

Merged
merged 4 commits into from
Oct 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions go/cs/segutil/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,9 @@ go_library(
"//go/lib/addr:go_default_library",
"//go/lib/common:go_default_library",
"//go/lib/ctrl/seg:go_default_library",
"//go/lib/infra/modules/combinator:go_default_library",
"//go/lib/infra/modules/segfetcher:go_default_library",
"//go/lib/pathpol:go_default_library",
"//go/lib/serrors:go_default_library",
"//go/lib/snet:go_default_library",
"//go/lib/spath:go_default_library",
],
)

Expand Down
148 changes: 2 additions & 146 deletions go/cs/segutil/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,15 @@
package segutil

import (
"bytes"
"context"
"fmt"
"net"
"strings"
"time"

"github.com/scionproto/scion/go/lib/addr"
"github.com/scionproto/scion/go/lib/infra/modules/combinator"
"github.com/scionproto/scion/go/lib/infra/modules/segfetcher"
"github.com/scionproto/scion/go/lib/serrors"
"github.com/scionproto/scion/go/lib/snet"
"github.com/scionproto/scion/go/lib/spath"
)

// Router returns paths backed by the local path database.
// XXX(matzf): this doesn't do meaningful work anymore, drop it.
type Router struct {
Pather segfetcher.Pather
}
Expand All @@ -47,142 +40,5 @@ func (r *Router) Route(ctx context.Context, dst addr.IA) (snet.Path, error) {

// AllRoutes is similar to Route except that it returns multiple paths.
func (r *Router) AllRoutes(ctx context.Context, dst addr.IA) ([]snet.Path, error) {
cPaths, err := r.Pather.GetPaths(ctx, dst, false)
if err != nil {
return nil, err
}
var paths []snet.Path
var errs serrors.List
for _, path := range cPaths {
p, err := r.translate(path, dst)
if err != nil {
errs = append(errs, err)
continue
}
paths = append(paths, p)
}
if len(paths) == 0 {
return nil, serrors.New("no paths after translation", "errs", errs.ToError())
}
return paths, nil
}

func (r *Router) translate(comb *combinator.Path, dst addr.IA) (path, error) {
if len(comb.Interfaces) == 0 {
return path{dst: dst}, nil
}
buf := &bytes.Buffer{}
if _, err := comb.WriteTo(buf); err != nil {
return path{}, err
}
sp := spath.NewV2(buf.Bytes(), false)
nextHop, ok := r.Pather.TopoProvider.Get().UnderlayNextHop(comb.Interfaces[0].ID)
if !ok {
return path{}, serrors.New("Unable to find first-hop BR for path",
"ifid", comb.Interfaces[0].ID)
}
p := path{
interfaces: make([]snet.PathInterface, len(comb.Interfaces)),
underlay: nextHop,
spath: sp,
metadata: pathMetadata{
mtu: comb.Mtu,
expiry: comb.ComputeExpTime(),
},
}
copy(p.interfaces, comb.Interfaces)
return p, nil
}

type path struct {
interfaces []snet.PathInterface
underlay *net.UDPAddr
spath *spath.Path
dst addr.IA
metadata pathMetadata
}

type pathMetadata struct {
mtu uint16
expiry time.Time
}

func (p path) UnderlayNextHop() *net.UDPAddr {
if p.underlay == nil {
return nil
}
return &net.UDPAddr{
IP: append(p.underlay.IP[:0:0], p.underlay.IP...),
Port: p.underlay.Port,
Zone: p.underlay.Zone,
}
}

func (p path) Path() *spath.Path {
if p.spath == nil {
return nil
}
return p.spath.Copy()
}

func (p path) Interfaces() []snet.PathInterface {
if p.interfaces == nil {
return nil
}
intfs := make([]snet.PathInterface, 0, len(p.interfaces))
for _, intf := range p.interfaces {
intfs = append(intfs, intf)
}
return intfs
}

func (p path) Destination() addr.IA {
if len(p.interfaces) == 0 {
return p.dst
}
return p.interfaces[len(p.interfaces)-1].IA
}

func (p path) Metadata() snet.PathMetadata {
return p.metadata
}

func (p path) Copy() snet.Path {
return path{
interfaces: append(p.interfaces[:0:0], p.interfaces...),
underlay: p.UnderlayNextHop(), // creates copy
spath: p.Path(), // creates copy
metadata: p.metadata,
}
}

func (p path) String() string {
hops := p.fmtInterfaces()
return fmt.Sprintf("Hops: [%s] MTU: %d, NextHop: %s",
strings.Join(hops, ">"), p.Metadata().MTU(), p.UnderlayNextHop())
}

func (p path) fmtInterfaces() []string {
var hops []string
if len(p.interfaces) == 0 {
return hops
}
intf := p.interfaces[0]
hops = append(hops, fmt.Sprintf("%s %d", intf.IA, intf.ID))
for i := 1; i < len(p.interfaces)-1; i += 2 {
inIntf := p.interfaces[i]
outIntf := p.interfaces[i+1]
hops = append(hops, fmt.Sprintf("%d %s %d", inIntf.ID, inIntf.IA, outIntf.ID))
}
intf = p.interfaces[len(p.interfaces)-1]
hops = append(hops, fmt.Sprintf("%d %s", intf.ID, intf.IA))
return hops
}

func (m pathMetadata) MTU() uint16 {
return m.mtu
}

func (m pathMetadata) Expiry() time.Time {
return m.expiry
return r.Pather.GetPaths(ctx, dst, false)
}
5 changes: 3 additions & 2 deletions go/lib/infra/modules/combinator/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ go_library(
"//go/lib/addr:go_default_library",
"//go/lib/common:go_default_library",
"//go/lib/ctrl/seg:go_default_library",
"//go/lib/serrors:go_default_library",
"//go/lib/slayers/path:go_default_library",
"//go/lib/slayers/path/scion:go_default_library",
"//go/lib/snet:go_default_library",
"//go/lib/snet/path:go_default_library",
"//go/lib/spath:go_default_library",
"//go/lib/util:go_default_library",
"//go/proto:go_default_library",
Expand All @@ -35,7 +35,8 @@ go_test(
"//go/lib/addr:go_default_library",
"//go/lib/common:go_default_library",
"//go/lib/ctrl/seg:go_default_library",
"//go/lib/util:go_default_library",
"//go/lib/slayers/path:go_default_library",
"//go/lib/slayers/path/scion:go_default_library",
"//go/lib/xtest:go_default_library",
"//go/lib/xtest/graph:go_default_library",
"@com_github_golang_mock//gomock:go_default_library",
Expand Down
Loading