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

Colibri store backend #3778

Merged
merged 22 commits into from
Jun 19, 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
5 changes: 2 additions & 3 deletions go/cs/reservation/e2e/reservation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func TestValidate(t *testing.T) {

// invalid segment reservation
r = newReservation()
r.SegmentReservations[0].Path = &segment.Path{}
r.SegmentReservations[0].Path = segment.Path{}
err = r.Validate()
require.Error(t, err)

Expand Down Expand Up @@ -93,8 +93,7 @@ func newSegmentReservation(asidPath ...string) *segment.Reservation {
pathComponents[i*3+2] = i*2 + 1
}
pathComponents[len(pathComponents)-1] = 0
p := segmenttest.NewPathFromComponents(pathComponents...)
r.Path = &p
r.Path = segmenttest.NewPathFromComponents(pathComponents...)
return r
}

Expand Down
15 changes: 15 additions & 0 deletions go/cs/reservation/reservationdbtest/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library")

go_library(
name = "go_default_library",
srcs = ["reservationdbtest.go"],
importpath = "github.com/scionproto/scion/go/cs/reservation/reservationdbtest",
visibility = ["//visibility:public"],
deps = [
"//go/cs/reservation/segment:go_default_library",
"//go/cs/reservation/segmenttest:go_default_library",
"//go/cs/reservationstorage/backend:go_default_library",
"//go/lib/xtest:go_default_library",
"@com_github_stretchr_testify//require:go_default_library",
],
)
55 changes: 55 additions & 0 deletions go/cs/reservation/reservationdbtest/reservationdbtest.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Copyright 2020 ETH Zurich, Anapaya Systems
//
// 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.

package reservationdbtest

import (
"context"
"testing"

"github.com/stretchr/testify/require"

"github.com/scionproto/scion/go/cs/reservation/segment"
"github.com/scionproto/scion/go/cs/reservation/segmenttest"
"github.com/scionproto/scion/go/cs/reservationstorage/backend"
"github.com/scionproto/scion/go/lib/xtest"
)

type TestableDB interface {
backend.DB
Prepare(*testing.T, context.Context)
}

func TestDB(t *testing.T, db TestableDB) {
tests := map[string]func(context.Context, *testing.T, backend.DB){
"insert segment reservations create ID": testNewSegmentReservation,
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
ctx := context.Background()
db.Prepare(t, ctx)
test(ctx, t, db)
})
}
}

func testNewSegmentReservation(ctx context.Context, t *testing.T, db backend.DB) {
r := segment.NewReservation()
r.Egress = 1
r.Path = segmenttest.NewPathFromComponents(0, "1-ff00:0:1", 1, 1, "1-ff00:0:2", 0)
r.ID.ASID = xtest.MustParseAS("ff00:0:1")
err := db.NewSegmentRsv(ctx, r)
require.NoError(t, err)
require.Equal(t, xtest.MustParseHexString("00000001"), r.ID.Suffix[:])
}
91 changes: 81 additions & 10 deletions go/cs/reservation/segment/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
package segment

import (
"encoding/binary"
"io"

"github.com/scionproto/scion/go/lib/addr"
"github.com/scionproto/scion/go/lib/common"
"github.com/scionproto/scion/go/lib/serrors"
Expand All @@ -23,6 +26,25 @@ import (
// Path represents a reservation path, in the reservation order.
type Path []PathStepWithIA

var _ io.Reader = (*Path)(nil)

// NewPathFromRaw constructs a new Path from the byte representation.
func NewPathFromRaw(buff []byte) (Path, error) {
if len(buff)%PathStepWithIALen != 0 {
return nil, serrors.New("buffer input is not a multiple of a path step", "len", len(buff))
}
steps := len(buff) / PathStepWithIALen
p := make(Path, steps)
for i := 0; i < steps; i++ {
offset := i * PathStepWithIALen
p[i].Ingress = common.IFIDType(binary.BigEndian.Uint64(buff[offset:]))
p[i].Egress = common.IFIDType(binary.BigEndian.Uint64(buff[offset+8:]))
p[i].IA = addr.IAFromRaw(buff[offset+16:])
}
return p, nil
}

// Validate returns an error if there is invalid data.
func (p Path) Validate() error {
if len(p) < 2 {
return serrors.New("invalid path length", "len", len(p))
Expand All @@ -37,35 +59,84 @@ func (p Path) Validate() error {
return nil
}

// Equal returns true if both Path contain the same values.
func (p Path) Equal(o Path) bool {
if len(p) != len(o) {
return false
}
for i := 0; i < len(p); i++ {
if !p[i].Equal(&o[i]) {
if p[i] != o[i] {
return false
}
}
return true
}

// GetSrcIA returns the source IA in the path or a zero IA if the path is nil (it's not the
// source AS of the reservation and has no access to the Path of the reservation).
// If the Path is not nil, it assumes is valid, i.e. it has at least length 2.
func (p Path) GetSrcIA() addr.IA {
if len(p) == 0 {
return addr.IA{}
}
return p[0].IA
}

// GetDstIA returns the source IA in the path or a zero IA if the path is nil (it's not the
// source AS of the reservation and has no access to the Path of the reservation).
// If the Path is not nil, it assumes is valid, i.e. it has at least length 2.
func (p Path) GetDstIA() addr.IA {
if len(p) == 0 {
return addr.IA{}
}
return p[len(p)-1].IA
}

// Len returns the length of this Path in bytes, when serialized.
func (p Path) Len() int {
if len(p) == 0 {
return 0
}
return len(p) * PathStepWithIALen
}

func (p Path) Read(buff []byte) (int, error) {
if len(p) == 0 {
return 0, nil
}
if len(buff) < p.Len() {
return 0, serrors.New("buffer too small", "min_size", p.Len(), "actual_size", len(buff))
}
for i, s := range p {
offset := i * PathStepWithIALen
binary.BigEndian.PutUint64(buff[offset:], uint64(s.Ingress))
binary.BigEndian.PutUint64(buff[offset+8:], uint64(s.Egress))
binary.BigEndian.PutUint64(buff[offset+16:], uint64(s.IA.IAInt()))
}
return p.Len(), nil
}

// ToRaw returns a buffer representing this Path.
func (p Path) ToRaw() []byte {
if len(p) == 0 {
return []byte{}
}
buff := make([]byte, p.Len())
p.Read(buff)
return buff
}

// PathStep is one hop of the Path. For a source AS Ingress will be invalid. Conversely for dst.
type PathStep struct {
// IA addr.IA
Ingress common.IFIDType
Egress common.IFIDType
}

func (s *PathStep) Equal(o *PathStep) bool {
// return s.IA == o.IA && s.Ingress == o.Ingress && s.Egress == o.Egress
return s.Ingress == o.Ingress && s.Egress == o.Egress
}

// PathStepWithIA is a step in a reservation path as seen from the source AS.
type PathStepWithIA struct {
PathStep
IA addr.IA
}

func (s *PathStepWithIA) Equal(o *PathStepWithIA) bool {
return s.PathStep.Equal(&o.PathStep) && s.IA == o.IA
}
// PathStepWithIALen amounts for Ingress+Egress+IA.
const PathStepWithIALen = 8 + 8 + 8
70 changes: 70 additions & 0 deletions go/cs/reservation/segment/path_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (

"github.com/scionproto/scion/go/cs/reservation/segment"
"github.com/scionproto/scion/go/cs/reservation/segmenttest"
"github.com/scionproto/scion/go/lib/xtest"
)

func TestValidatePath(t *testing.T) {
Expand Down Expand Up @@ -73,6 +74,16 @@ func TestEqualPath(t *testing.T) {
1, "1-ff00:0:2", 0),
IsEqual: true,
},
"eq3": {
Path1: nil,
Path2: nil,
IsEqual: true,
},
"eq4": {
Path1: nil,
Path2: make(segment.Path, 0),
IsEqual: true,
},
"neq1": {
Path1: segmenttest.NewPathFromComponents(0, "1-ff00:0:1", 1, 1, "1-ff00:0:2", 0),
Path2: segmenttest.NewPathFromComponents(1, "1-ff00:0:1", 1, 1, "1-ff00:0:2", 0),
Expand Down Expand Up @@ -104,3 +115,62 @@ func TestEqualPath(t *testing.T) {
})
}
}

func TestGetIAs(t *testing.T) {
p := segmenttest.NewPathFromComponents(0, "1-ff00:0:1", 1, 1, "1-ff00:0:2", 0)
require.Equal(t, xtest.MustParseIA("1-ff00:0:1"), p.GetSrcIA())
require.Equal(t, xtest.MustParseIA("1-ff00:0:2"), p.GetDstIA())
p = nil
require.Equal(t, xtest.MustParseIA("0-0"), p.GetSrcIA())
require.Equal(t, xtest.MustParseIA("0-0"), p.GetDstIA())
p = make(segment.Path, 0)
require.Equal(t, xtest.MustParseIA("0-0"), p.GetSrcIA())
require.Equal(t, xtest.MustParseIA("0-0"), p.GetDstIA())
}

func TestPathLen(t *testing.T) {
p := segmenttest.NewPathFromComponents(0, "1-ff00:0:1", 1, 1, "1-ff00:0:2", 0)
require.Equal(t, 8*6, p.Len())
p = segment.Path{}
require.Equal(t, 0, p.Len())
p = nil
require.Equal(t, 0, p.Len())
p = make(segment.Path, 0)
require.Equal(t, 0, p.Len())
}

func TestToFromBinary(t *testing.T) {
p := segmenttest.NewPathFromComponents(0, "1-ff00:0:1", 1, 1, "1-ff00:0:2", 0)
var buff []byte
_, err := p.Read(buff)
require.Error(t, err)
_, err = p.Read(buff)
require.Error(t, err)
buff = make([]byte, 2*3*8)
c, err := p.Read(buff)
require.NoError(t, err)
require.Equal(t, 2*3*8, c)

anotherP, err := segment.NewPathFromRaw(buff)
require.NoError(t, err)
require.Equal(t, p, anotherP)

anotherBuff := p.ToRaw()
require.Equal(t, buff, anotherBuff)
// wrong buffer
buff = buff[:len(buff)-1]
_, err = segment.NewPathFromRaw(buff)
require.Error(t, err)
// empty and nil buffer
p, err = segment.NewPathFromRaw(nil)
require.NoError(t, err)
require.Empty(t, p)
p, err = segment.NewPathFromRaw([]byte{})
require.NoError(t, err)
require.Empty(t, p)
// empty and nil path
p = nil
require.Empty(t, p.ToRaw())
p = make(segment.Path, 0)
require.Empty(t, p.ToRaw())
}
14 changes: 7 additions & 7 deletions go/cs/reservation/segment/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ type Request struct {
Metadata base.RequestMetadata // information about the request (forwarding path)
ID reservation.SegmentID // the ID this request refers to
Timestamp time.Time // the mandatory timestamp
IngressIFID common.IFIDType // the interface the reservation traffic uses to enter the AS
EgressIFID common.IFIDType // the interface the reservation traffic uses to leave the AS
Ingress common.IFIDType // the interface the reservation traffic uses to enter the AS
Egress common.IFIDType // the interface the reservation traffic uses to leave the AS
Reservation *Reservation // nil if no reservation yet
}

Expand Down Expand Up @@ -64,11 +64,11 @@ func NewRequest(ts time.Time, ID *colibri_mgmt.SegmentReservationID,
return nil, serrors.WrapStr("new segment request", err)
}
return &Request{
Timestamp: ts,
Metadata: *metadata,
ID: *segID,
IngressIFID: ingressIFID,
EgressIFID: egressIFID,
Timestamp: ts,
Metadata: *metadata,
ID: *segID,
Ingress: ingressIFID,
Egress: egressIFID,
}, nil
}

Expand Down
4 changes: 2 additions & 2 deletions go/cs/reservation/segment/request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ func TestNewSetupReqFromCtrlMsg(t *testing.T) {
require.NoError(t, err)
require.Equal(t, *p, r.Metadata.Path)
checkRequest(t, ctrlMsg, r, ts)
require.Equal(t, common.IFIDType(1), r.IngressIFID)
require.Equal(t, common.IFIDType(2), r.EgressIFID)
require.Equal(t, common.IFIDType(1), r.Ingress)
require.Equal(t, common.IFIDType(2), r.Egress)
}

func TestRequestToCtrlMsg(t *testing.T) {
Expand Down
Loading