-
Notifications
You must be signed in to change notification settings - Fork 37
/
route.go
191 lines (168 loc) · 4.22 KB
/
route.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
// Copyright (C) 2019 Cisco Systems Inc.
//
// 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 types
import (
"fmt"
"net"
"strings"
"github.com/projectcalico/vpp-dataplane/v3/vpplink/generated/bindings/fib_types"
)
type RoutePath struct {
Gw net.IP
SwIfIndex uint32
Table uint32
IsAttached bool
Preference uint8
RpfID uint32
}
type Route struct {
Dst *net.IPNet
Paths []RoutePath
Table uint32
RpfID uint32
}
func (p *RoutePath) tableString() string {
if p.Table == 0 {
return ""
} else {
return fmt.Sprintf("[table:%d]", p.Table)
}
}
func FromFibPathList(apiPathList []fib_types.FibPath) (routePaths []RoutePath) {
routePaths = make([]RoutePath, 0, len(apiPathList))
for _, vppPath := range apiPathList {
routePaths = append(routePaths, FromFibPath(vppPath))
}
return routePaths
}
func FromFibPath(vppPath fib_types.FibPath) RoutePath {
return RoutePath{
Gw: FromVppIpAddressUnion(vppPath.Nh.Address, vppPath.Proto == fib_types.FIB_API_PATH_NH_PROTO_IP6),
Table: vppPath.TableID,
SwIfIndex: vppPath.SwIfIndex,
}
}
func ToFibPathList(routePaths []RoutePath, isIP6 bool) (apiPathList []fib_types.FibPath) {
apiPathList = make([]fib_types.FibPath, 0, len(routePaths))
for _, routePath := range routePaths {
apiPathList = append(apiPathList, routePath.ToFibPath(isIP6))
}
return apiPathList
}
func (p *RoutePath) ToFibPath(isIP6 bool) fib_types.FibPath {
// There is one case where we need an IPv4 route with an IPv6 path
// (for broadcast)
pathProto := IsV6toFibProto(isIP6)
if p.Gw != nil {
pathProto = IsV6toFibProto(p.Gw.To4() == nil)
}
fibPath := fib_types.FibPath{
SwIfIndex: p.SwIfIndex,
TableID: p.Table,
RpfID: p.RpfID,
Weight: 1,
Preference: p.Preference,
Type: fib_types.FIB_API_PATH_TYPE_NORMAL,
Flags: fib_types.FIB_API_PATH_FLAG_NONE,
Proto: pathProto,
}
if p.IsAttached {
fibPath.Flags |= fib_types.FIB_API_PATH_FLAG_RESOLVE_VIA_ATTACHED
}
if p.Gw != nil {
fibPath.Nh.Address = ToVppAddress(p.Gw).Un
}
return fibPath
}
func (p *RoutePath) swIfIndexString() string {
if p.SwIfIndex == 0 || p.SwIfIndex == InvalidID {
return ""
} else {
return fmt.Sprintf("[if:%d]", p.SwIfIndex)
}
}
func (p *RoutePath) gwString() string {
if p.Gw != nil {
return p.Gw.String()
}
return ""
}
func (p *RoutePath) String() string {
return fmt.Sprintf("%s%s%s", p.tableString(), p.gwString(), p.swIfIndexString())
}
func IsV6toFibProto(isv6 bool) fib_types.FibPathNhProto {
if isv6 {
return fib_types.FIB_API_PATH_NH_PROTO_IP6
}
return fib_types.FIB_API_PATH_NH_PROTO_IP4
}
func AddrIsZeros(p net.IP) bool {
for i := 0; i < len(p); i++ {
if p[i] != 0 {
return false
}
}
return true
}
func (r *Route) IsIP6() bool {
if r.Dst != nil {
return IsIP6(r.Dst.IP)
}
for _, path := range r.Paths {
return IsIP6(path.Gw)
}
return false
}
func (r *Route) pathsString() string {
pathsStr := make([]string, 0, len(r.Paths))
for _, path := range r.Paths {
pathsStr = append(pathsStr, path.String())
}
return strings.Join(pathsStr, ", ")
}
func (r *Route) familyString() string {
if r.IsIP6() {
return "v6"
}
return "v4"
}
func (r *Route) tableString() string {
if r.Table == 0 {
return ""
} else {
return fmt.Sprintf("[table:%d] ", r.Table)
}
}
func (r *Route) dstString() string {
if r.Dst == nil {
return "*"
}
if r.Dst.IP.Equal(net.IPv4zero) || r.Dst.IP.Equal(net.IPv6zero) {
return "*"
}
return r.Dst.String()
}
func (r *Route) String() string {
return fmt.Sprintf("%s %s%s -> %s", r.familyString(), r.tableString(), r.dstString(), r.pathsString())
}
func (r *Route) IsLinkLocal() bool {
if r.Dst == nil {
return false
}
if !IsIP6(r.Dst.IP) {
return false
}
return r.Dst.IP.IsLinkLocalUnicast()
}