-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathvrf.go
More file actions
284 lines (257 loc) · 8.6 KB
/
Copy pathvrf.go
File metadata and controls
284 lines (257 loc) · 8.6 KB
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/*
Copyright 2019 Yoichi Hariguchi
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 iproute
import (
"fmt"
"github.com/vishvananda/netlink"
"github.com/vishvananda/netlink/nl"
)
type Vrf struct {
Link *netlink.Vrf
}
// VrfGetLinkByIndex returns a pointer to Vrf whose ifindex is `idx'
// in: idx Ifindex of the target VRF
// return: 1. Pointer to netlink.Vrf whose ifindex is `idx' if success
// nil otherwise
// 2. nil if success
// non-nil otherwise
func VrfGetByIndex(idx int) (*Vrf, error) {
if l, err := netlink.LinkByIndex(idx); err == nil {
switch l := l.(type) {
case *netlink.Vrf:
return &Vrf{Link: l}, nil
default:
return nil, fmt.Errorf("VrfGetByIndex(%d): not a VRF", idx)
}
} else {
return nil, err
}
}
// VrfGetLinkByName returns a pointer to Vrf whose name is `name'
// in: name Name of VRF
// return: 1. Pointer to netlink.Vrf associated with `name'
// undetermined otherwise.
// 2. nil if there is a VRF whose name is `name'
// non-nil otherwise
func VrfGetByName(name string) (*Vrf, error) {
if l, err := netlink.LinkByName(name); err == nil {
switch l := l.(type) {
case *netlink.Vrf:
return &Vrf{Link: l}, nil
default:
return nil, fmt.Errorf("VrfGetByName(%s): not a VRF", name)
}
} else {
return nil, err
}
}
// VrfAdd adds VRF whose name is `name' and whose table id is `tid'
// in: name Name of VRF to be added
// tid Table ID for VRF `name'
// return: nil if success
// non-nil otherwise
func VrfAdd(name string, tid uint32, up bool) (*Vrf, error) {
err := netlink.LinkAdd(&netlink.Vrf{
LinkAttrs: netlink.LinkAttrs{Name: name},
Table: uint32(tid),
})
if err != nil {
return nil, err
}
if vrf, err := VrfGetByName(name); err == nil {
if up {
vrf.IfUp()
}
return vrf, nil
} else {
return nil, err
}
}
// VrfDelete deletes VRF whose name is `name'
// in: name Name of VRF
// return: nil if success
// non-nil otherwise
func VrfDelete(name string) error {
return LinkDel(name)
}
func VrfIfExists(name string) (bool, error) {
return ifExists(name, &netlink.Vrf{})
}
// VrfBindIf binds an interface to a VRF
// in: vrfName Name of VRF
// ifName Name of interface to be bound to VRF `vrfName'
// return: nil if success
// non-nil otherwise
func VrfBindIf(vrfName, ifName string) error {
if vrf, err := VrfGetByName(vrfName); err == nil {
return vrf.BindIf(ifName)
} else {
return fmt.Errorf("VrfGetByName(%s): %v", vrfName, err)
}
}
// VrfGetRoutesByTid returns a slice of netlink.Route whose
// table id is `tid', family is `family', and table type is `tableType'
// in: tid Table ID
// family FAMILY_ALL, FAMILY_V4, FAMILY_V6, or FAMILY_MPLS
// tableType RTN_UNSPEC, RTN_UNICAST, RTN_LOCAL, RTN_BROADCAST,
// RTN_ANYCAST,RTN_MULTICAST, RTN_BLACKHOLE, RTN_UNREACHABLE,
// RTN_PROHIBIT, RTN_THROW, RTN_NAT, or RTN_XRESOLVE
// return: 1. slice of netlink.Route if success
// undetermined otherwise
// 2. nil if success
// non-nil otherwise
func VrfGetRoutesByTid(tid int, family int, tableType int) (Routes, error) {
routeFilter := &netlink.Route{
Table: tid,
Type: tableType,
}
filterMask := RT_FILTER_TABLE | RT_FILTER_TYPE
return netlink.RouteListFiltered(family, routeFilter, filterMask)
}
// VrfGetRoutesByName returns a slice of netlink.Route belonging to the VRF
// whose nanme is `name', family is `family', and table type is `tableType'
// in: name Name of VRF
// family FAMILY_ALL, FAMILY_V4, FAMILY_V6, or FAMILY_MPLS
// tableType RTN_UNSPEC, RTN_UNICAST, RTN_LOCAL, RTN_BROADCAST,
// RTN_ANYCAST,RTN_MULTICAST, RTN_BLACKHOLE, RTN_UNREACHABLE,
// RTN_PROHIBIT, RTN_THROW, RTN_NAT, or RTN_XRESOLVE
// return: 1. slice of netlink.Route if success
// undetermined otherwise
// 2. nil if success
// non-nil otherwise
func VrfGetRoutesByName(name string, family int, tblType int) (Routes, error) {
if vrf, err := VrfGetByName(name); err == nil {
return VrfGetRoutesByTid(int(vrf.Tid()), family, tblType)
} else {
errMsg := fmt.Sprintf("VrfGetRoutesByName(%s): ", vrf.Name())
return nil, fmt.Errorf(errMsg+"%v", err)
}
}
// VrfGetIPv4routesByName returns a slice of IPv4 netlink.Route
// belonging to the VRF whose nanme is `name'
// in: name Name of VRF
// return: 1. slice of netlink.Route if success. All of them are IPv4
// undetermined otherwise
// 2. nil if success
// non-nil otherwise
func VrfGetIPv4routesByName(name string) (Routes, error) {
if vrf, err := VrfGetByName(name); err == nil {
return VrfGetRoutesByTid(int(vrf.Tid()), nl.FAMILY_V4, RTN_UNICAST)
} else {
return Routes{}, err
}
}
// VrfGetIPv4localRoutesByName returns a slice of IPv4 netlink.Route of
// local routes belonging to the VRF whose nanme is `name'
// in: name Name of VRF
// return: 1. slice of netlink.Route if success. All of them are IPv4
// undetermined otherwise
// 2. nil if success
// non-nil otherwise
func VrfGetIPv4localRoutes(vrf string) (Routes, error) {
return VrfGetRoutesByName(vrf, nl.FAMILY_V4, RTN_LOCAL)
}
// VrfGetIPv6routesByName returns a slice of IPv6 netlink.Route
// belonging to the VRF whose nanme is `name'
// in: name Name of VRF
// return: 1. slice of netlink.Route if success. All of them are IPv4
// undetermined otherwise
// 2. nil if success
// non-nil otherwise
func VrfGetIPv6routesByName(name string) (Routes, error) {
if vrf, err := VrfGetByName(name); err == nil {
return VrfGetRoutesByTid(int(vrf.Tid()), nl.FAMILY_V6, RTN_UNICAST)
} else {
return Routes{}, err
}
}
// VrfAddRouteByName adds a route to a VRF
// in: name Name of the target VRF
// r Pointer to the route to be added
// return: nil if success
// non-nil otherwise
func VrfAddRouteByName(name string, r *Route) error {
errMsg := fmt.Sprintf("VrfAddRouteByName(%s, %v): ", name, r)
if vrf, err := VrfGetByName(name); err == nil {
r.Table = int(vrf.Tid())
return netlink.RouteAdd(r)
} else {
return fmt.Errorf(errMsg+"VrfGetByName(): %v", err)
}
}
// VrfDeleteRouteByName deletes a route in a VRF.
// in: name Name of the target VRF
// r Pointer to the route to be deleted
// return: nil if success
// non-nil otherwise
func VrfDeleteRouteByName(name string, r *Route) error {
errMsg := fmt.Sprintf("VrfDeleteRouteByName(%s, %v): ", name, r)
if vrf, err := VrfGetByName(name); err == nil {
r.Table = int(vrf.Tid())
return netlink.RouteDel(r)
} else {
return fmt.Errorf(errMsg+"VrfGetByName(): %v", err)
}
}
// VrfReplaceRouteByName replaces the existing route in a VRF.
// The route is added to the VRF unless it exists.
// in: name Name of the target VRF
// r Pointer to the route to be added
// return: nil if success
// non-nil otherwise
func VrfReplaceRouteByName(name string, r *Route) error {
errMsg := fmt.Sprintf("VrfReplaceRouteByName(%s, %v): ", name, r)
if vrf, err := VrfGetByName(name); err == nil {
r.Table = int(vrf.Tid())
return netlink.RouteReplace(r)
} else {
return fmt.Errorf(errMsg+"VrfGetByName(): %v", err)
}
}
// Equal checks if two VRFs are identical or not.
// in: other Pointer to `Vrf'
// return: true if VRF `other' has the same name, ifindex, and table id
// false otherwise.
func (vrf *Vrf) Equal(other *Vrf) bool {
if vrf.Name() == other.Name() &&
vrf.Index() == other.Index() && vrf.Tid() == other.Tid() {
return true
}
return false
}
func (vrf *Vrf) Name() string {
return vrf.Link.Attrs().Name
}
func (vrf *Vrf) Index() int {
return vrf.Link.Attrs().Index
}
func (vrf *Vrf) Tid() uint32 {
return vrf.Link.Table
}
func (vrf *Vrf) IfUp() error {
return netlink.LinkSetUp(vrf.Link)
}
func (vrf *Vrf) IfDown() error {
return netlink.LinkSetDown(vrf.Link)
}
// VrfBindIf binds an interface to a VRF
// in: ifName Name of interface to be bound to VRF `vrfName'
// return: nil if success
// non-nil otherwise
func (vrf *Vrf) BindIf(ifName string) error {
if l, err := netlink.LinkByName(ifName); err == nil {
return netlink.LinkSetMasterByIndex(l, vrf.Index())
} else {
return fmt.Errorf("LinkByName(%s): %v", ifName, err)
}
}