-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
gobgp.go
369 lines (333 loc) · 9.36 KB
/
gobgp.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
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of Cilium
package test
import (
"context"
"fmt"
"github.com/cilium/cilium/pkg/bgpv1/gobgp"
gobgpapi "github.com/osrg/gobgp/v3/api"
"github.com/osrg/gobgp/v3/pkg/apiutil"
"github.com/osrg/gobgp/v3/pkg/packet/bgp"
gobgpb "github.com/osrg/gobgp/v3/pkg/packet/bgp"
"github.com/osrg/gobgp/v3/pkg/server"
)
// goBGP configuration used in tests
var (
gobgpASN = uint32(65011)
gobgpASN2 = uint32(65012)
gobgpListenPort = int32(1791)
gobgpListenPort2 = int32(1792)
gobgpGlobal = &gobgpapi.Global{
Asn: gobgpASN,
RouterId: dummies[instance1Link].ipv4.Addr().String(),
ListenPort: gobgpListenPort,
}
gobgpGlobal2 = &gobgpapi.Global{
Asn: gobgpASN2,
RouterId: dummies[instance1Link].ipv4.Addr().String(),
ListenPort: gobgpListenPort2,
}
gobgpGlobalIBGP = &gobgpapi.Global{
Asn: ciliumASN, // iBGP
RouterId: dummies[instance1Link].ipv4.Addr().String(),
ListenPort: gobgpListenPort,
}
gbgpNeighConf = &gobgpapi.Peer{
Conf: &gobgpapi.PeerConf{
NeighborAddress: dummies[ciliumLink].ipv4.Addr().String(),
PeerAsn: ciliumASN,
},
Transport: &gobgpapi.Transport{
RemoteAddress: dummies[ciliumLink].ipv4.Addr().String(),
RemotePort: ciliumListenPort,
LocalAddress: dummies[instance1Link].ipv4.Addr().String(),
PassiveMode: false,
},
AfiSafis: []*gobgpapi.AfiSafi{
{
Config: &gobgpapi.AfiSafiConfig{
Family: gobgp.GoBGPIPv4Family,
},
},
{
Config: &gobgpapi.AfiSafiConfig{
Family: gobgp.GoBGPIPv6Family,
},
},
},
}
gbgpNeighConf2 = &gobgpapi.Peer{
Conf: &gobgpapi.PeerConf{
NeighborAddress: dummies[ciliumLink].ipv4.Addr().String(),
PeerAsn: ciliumASN,
},
Transport: &gobgpapi.Transport{
RemoteAddress: dummies[ciliumLink].ipv4.Addr().String(),
RemotePort: ciliumListenPort,
LocalAddress: dummies[instance2Link].ipv4.Addr().String(),
PassiveMode: false,
},
AfiSafis: []*gobgpapi.AfiSafi{
{
Config: &gobgpapi.AfiSafiConfig{
Family: gobgp.GoBGPIPv4Family,
},
},
{
Config: &gobgpapi.AfiSafiConfig{
Family: gobgp.GoBGPIPv6Family,
},
},
},
}
gbgpNeighConfPassword = &gobgpapi.Peer{
Conf: &gobgpapi.PeerConf{
NeighborAddress: dummies[ciliumLink].ipv4.Addr().String(),
PeerAsn: ciliumASN,
AuthPassword: "testing-123",
},
Transport: &gobgpapi.Transport{
RemoteAddress: dummies[ciliumLink].ipv4.Addr().String(),
RemotePort: ciliumListenPort,
LocalAddress: dummies[instance1Link].ipv4.Addr().String(),
PassiveMode: false,
},
AfiSafis: []*gobgpapi.AfiSafi{
{
Config: &gobgpapi.AfiSafiConfig{
Family: gobgp.GoBGPIPv4Family,
},
},
{
Config: &gobgpapi.AfiSafiConfig{
Family: gobgp.GoBGPIPv6Family,
},
},
},
}
gobgpConf = gobgpConfig{
global: gobgpGlobal,
neighbors: []*gobgpapi.Peer{
gbgpNeighConf,
},
}
gobgpConf2 = gobgpConfig{
global: gobgpGlobal2,
neighbors: []*gobgpapi.Peer{
gbgpNeighConf2,
},
}
gobgpConfPassword = gobgpConfig{
global: gobgpGlobal,
neighbors: []*gobgpapi.Peer{
gbgpNeighConfPassword,
},
}
gobgpConfIBGP = gobgpConfig{
global: gobgpGlobalIBGP,
neighbors: []*gobgpapi.Peer{
gbgpNeighConf,
},
}
)
// gobgpConfig used for starting gobgp instance
type gobgpConfig struct {
global *gobgpapi.Global
neighbors []*gobgpapi.Peer
}
// routeEvent contains information about new event in routing table of gobgp
type routeEvent struct {
sourceASN uint32
prefix string
prefixLen uint8
isWithdrawn bool
extraPathAttributes []bgp.PathAttributeInterface // non-standard path attributes (other than Origin / ASPath / NextHop / MpReachNLRI)
}
// peerEvent contains information about peer state change of gobgp
type peerEvent struct {
peerASN uint32
state string
}
// goBGP wrapper on gobgp server and provides route and peer event handling
type goBGP struct {
context context.Context
server *server.BgpServer
peerEvents chan *gobgpapi.WatchEventResponse_PeerEvent
tableEvents chan *gobgpapi.WatchEventResponse_TableEvent
peerNotif chan peerEvent
routeNotif chan routeEvent
}
// startGoBGP initialized new gobgp server, configures neighbors and starts listening on route and peer events
func startGoBGP(ctx context.Context, conf gobgpConfig) (g *goBGP, err error) {
g = &goBGP{
context: ctx,
server: server.NewBgpServer(server.LoggerOption(gobgp.NewServerLogger(log, gobgp.LogParams{
AS: conf.global.Asn,
Component: "tests.BGP",
SubSys: "basic",
}))),
peerEvents: make(chan *gobgpapi.WatchEventResponse_PeerEvent),
tableEvents: make(chan *gobgpapi.WatchEventResponse_TableEvent),
peerNotif: make(chan peerEvent),
routeNotif: make(chan routeEvent),
}
go g.server.Serve()
go g.readEvents()
// in case of err, clean up
defer func() {
if err != nil {
g.server.Stop()
}
}()
log.Info("GoBGP test instance: starting")
err = g.server.StartBgp(ctx, &gobgpapi.StartBgpRequest{Global: conf.global})
if err != nil {
return
}
// register watchers for peer and route events
watchRequest := &gobgpapi.WatchEventRequest{
Peer: &gobgpapi.WatchEventRequest_Peer{},
Table: &gobgpapi.WatchEventRequest_Table{
Filters: []*gobgpapi.WatchEventRequest_Table_Filter{
{
Type: gobgpapi.WatchEventRequest_Table_Filter_BEST,
Init: true,
},
},
},
}
err = g.server.WatchEvent(ctx, watchRequest, func(r *gobgpapi.WatchEventResponse) {
switch r.Event.(type) {
case *gobgpapi.WatchEventResponse_Peer:
g.peerEvents <- r.GetPeer()
case *gobgpapi.WatchEventResponse_Table:
g.tableEvents <- r.GetTable()
}
})
if err != nil {
return
}
// configure neighbors
for _, peer := range conf.neighbors {
err = g.server.AddPeer(ctx, &gobgpapi.AddPeerRequest{Peer: peer})
if err != nil {
return
}
}
return
}
// stopGoBGP stops server
func (g *goBGP) stopGoBGP() {
log.Infof("GoBGP test instance: stopping")
g.server.Stop()
}
// readEvents receives peer and route events from gobgp callbacks, unmarshal response to well-defined structs and
// pass this to consumers of peer and route events.
// Note this will block if there is no consumer reading, in which case test context would timeout resulting in termination
// of this goroutine.
func (g *goBGP) readEvents() {
for {
select {
case e := <-g.tableEvents:
for _, p := range e.Paths {
var prefix string
var length uint8
nlri, err := apiutil.UnmarshalNLRI(gobgpb.AfiSafiToRouteFamily(uint16(p.Family.Afi), uint8(p.Family.Safi)), p.Nlri)
if err != nil {
log.Errorf("failed to unmarshal path nlri %v: %v", p, err)
continue
}
switch a := nlri.(type) {
case *gobgpb.IPAddrPrefix:
prefix = a.Prefix.String()
length = a.Length
case *gobgpb.IPv6AddrPrefix:
prefix = a.Prefix.String()
length = a.Length
default:
log.Errorf("failed to identify nlri %v", nlri)
continue
}
pattrs, err := apiutil.UnmarshalPathAttributes(p.Pattrs)
if err != nil {
log.Errorf("failed to unmarshal path attributes %v: %v", p, err)
continue
}
select {
case g.routeNotif <- routeEvent{
sourceASN: p.SourceAsn,
prefix: prefix,
prefixLen: length,
isWithdrawn: p.IsWithdraw,
extraPathAttributes: g.filterStandardPathAttributes(pattrs),
}:
case <-g.context.Done():
return
}
}
case e := <-g.peerEvents:
if e.Peer != nil {
select {
case g.peerNotif <- peerEvent{
peerASN: e.Peer.Conf.PeerAsn,
state: e.Peer.State.SessionState.String(),
}:
case <-g.context.Done():
return
}
}
case <-g.context.Done():
return
}
}
}
// waitForSessionState consumes state changes from gobgp and compares it with expected states
func (g *goBGP) waitForSessionState(ctx context.Context, expectedStates []string) error {
for {
select {
case e := <-g.peerNotif:
log.Infof("GoBGP test instance: Peer Event: %v", e)
for _, state := range expectedStates {
if e.state == state {
return nil
}
}
case <-ctx.Done():
return fmt.Errorf("did not receive expected peering state %q: %w", expectedStates, ctx.Err())
}
}
}
// getRouteEvents drains number of events from routeNotif chan and return those events to caller.
func (g *goBGP) getRouteEvents(ctx context.Context, numExpectedEvents int) ([]routeEvent, error) {
var receivedEvents []routeEvent
for i := 0; i < numExpectedEvents; i++ {
select {
case r := <-g.routeNotif:
log.Infof("GoBGP test instance: Route Event: %v", r)
receivedEvents = append(receivedEvents, r)
case <-ctx.Done():
return receivedEvents, fmt.Errorf("time elapsed waiting for all route events - received %d, expected %d : %w",
len(receivedEvents), numExpectedEvents, ctx.Err())
}
}
return receivedEvents, nil
}
// filterStandardPathAttributes filters standard path attributes (usually present on all routes) from the
// provided list of the path attributes.
func (g *goBGP) filterStandardPathAttributes(attrs []bgp.PathAttributeInterface) []bgp.PathAttributeInterface {
var res []bgp.PathAttributeInterface
for _, a := range attrs {
switch a.(type) {
case *bgp.PathAttributeOrigin:
continue
case *bgp.PathAttributeAsPath:
continue
case *bgp.PathAttributeNextHop:
continue
case *bgp.PathAttributeMpReachNLRI:
continue
}
res = append(res, a)
}
return res
}