forked from scionproto/scion
/
db.go
145 lines (130 loc) · 4.74 KB
/
db.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// Copyright 2019 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 beacon
import (
"context"
"database/sql"
"fmt"
"io"
"strings"
"time"
"github.com/scionproto/scion/go/lib/addr"
"github.com/scionproto/scion/go/lib/common"
"github.com/scionproto/scion/go/lib/ctrl/path_mgmt"
"github.com/scionproto/scion/go/lib/infra/modules/db"
)
const (
// ErrReadingRows is the error message in case we fail to read more from
// the database.
ErrReadingRows common.ErrMsg = "Failed to read rows"
// ErrParse is the error message in case the parsing a db entry fails.
ErrParse common.ErrMsg = "Failed to parse entry"
)
// DBRead defines all read operations of the beacon DB.
type DBRead interface {
// CandidateBeacons returns up to setSize beacons that are allowed for the
// given usage. The result channel either carries beacons or errors. The
// beacons in the channel are ordered by segment length from shortest to
// longest. The channel must be drained, since the db might spawn go routines
// to fill the channel.
CandidateBeacons(ctx context.Context, setSize int, usage Usage, src addr.IA) (
<-chan BeaconOrErr, error)
// BeaconSources returns all source ISD-AS of the beacons in the database.
BeaconSources(ctx context.Context) ([]addr.IA, error)
// AllRevocations returns all revocations in the database as a channel. The
// result channel either carries revocations or errors. The error can
// either be ErrReadingRows or ErrParse. After a ErrReadingRows occurs the
// channel is closed and the result might be incomplete. The channel must
// be drained, since the implementation might spawn go routines to fill the
// channel.
AllRevocations(ctx context.Context) (<-chan RevocationOrErr, error)
}
// InsertStats provides statistics about an insertion.
type InsertStats struct {
Inserted, Updated, Filtered int
}
// DBWrite defines all write operations of the beacon DB.
type DBWrite interface {
InsertBeacon(ctx context.Context, beacon Beacon, usage Usage) (InsertStats, error)
DeleteExpiredBeacons(ctx context.Context, now time.Time) (int, error)
DeleteRevokedBeacons(ctx context.Context, now time.Time) (int, error)
InsertRevocation(ctx context.Context, revocation *path_mgmt.SignedRevInfo) error
DeleteRevocation(ctx context.Context, ia addr.IA, ifid common.IFIDType) error
DeleteExpiredRevocations(ctx context.Context, now time.Time) (int, error)
}
// DBReadWrite defines all read an write operations of the beacon DB.
type DBReadWrite interface {
DBRead
DBWrite
}
// Transaction defines all operations of a transaction on the beacon DB.
type Transaction interface {
DBReadWrite
Commit() error
Rollback() error
}
// DB defines the interface that all beacon DB backends have to implement.
type DB interface {
DBReadWrite
BeginTransaction(ctx context.Context, opts *sql.TxOptions) (Transaction, error)
db.LimitSetter
io.Closer
}
const (
// UsageUpReg indicates the beacon is allowed to be registered as an up segment.
UsageUpReg Usage = 0x01
// UsageDownReg indicates the beacon is allowed to be registered as a down segment.
UsageDownReg Usage = 0x02
// UsageCoreReg indicates the beacon is allowed to be registered as a core segment.
UsageCoreReg Usage = 0x04
// UsageProp indicates the beacon is allowed to be propagated.
UsageProp Usage = 0x08
)
// Usage indicates what the beacon is allowed to be used for according to the policies.
type Usage int
// UsageFromPolicyType maps the policy type to the usage flag.
func UsageFromPolicyType(policyType PolicyType) Usage {
switch policyType {
case UpRegPolicy:
return UsageUpReg
case DownRegPolicy:
return UsageDownReg
case CoreRegPolicy:
return UsageCoreReg
case PropPolicy:
return UsageProp
default:
panic(fmt.Sprintf("Invalid policyType: %v", policyType))
}
}
// None indicates whether the beacons is not allowed to be used anywhere.
func (u Usage) None() bool {
return u&0x0F == 0
}
func (u Usage) String() string {
names := []string{}
if u&UsageUpReg != 0 {
names = append(names, "UpRegistration")
}
if u&UsageDownReg != 0 {
names = append(names, "DownRegistration")
}
if u&UsageCoreReg != 0 {
names = append(names, "CoreRegistration")
}
if u&UsageProp != 0 {
names = append(names, "Propagation")
}
return fmt.Sprintf("Usage: [%s]", strings.Join(names, ","))
}