@@ -30,21 +30,28 @@ type Route struct {
30
30
Community uint32
31
31
}
32
32
33
+ type Peer struct {
34
+ Neighbor string
35
+ RemoteAS uint32
36
+ RemotePort uint32
37
+ KeepaliveIntervalSec uint64
38
+ }
39
+
33
40
type Connector interface {
34
- Start (ctx context.Context ) error
35
- AddPeer (neighbor string , remoteAS uint32 , remotePort uint32 , keepaliveIntervalSec uint64 ) error
41
+ Start () error
36
42
AddPath (prefix string , prefixLen uint32 , nexthop string , community uint32 ) error
37
43
ListPath () ([]Route , error )
44
+ Stop ()
38
45
}
39
46
40
47
type bgpServerConnector struct {
41
48
logger * slog.Logger
42
49
server * gobgpserver.BgpServer
43
- ctx context.Context
44
50
asn uint32
45
51
routerId string
46
52
listenPort int32
47
53
grpcPort int
54
+ peers []Peer
48
55
}
49
56
50
57
func NewDefaultConnector (logger * slog.Logger , configs ... func (* bgpServerConnector )) Connector {
@@ -85,48 +92,62 @@ func WithGrpcPort(grpcPort int) func(*bgpServerConnector) {
85
92
}
86
93
}
87
94
88
- func (bs * bgpServerConnector ) Start (ctx context.Context ) error {
89
- bs .ctx = ctx
95
+ func WithPeers (peers []Peer ) func (* bgpServerConnector ) {
96
+ return func (c * bgpServerConnector ) {
97
+ c .peers = peers
98
+ }
99
+ }
100
+
101
+ func (bs * bgpServerConnector ) Start () error {
90
102
go bs .server .Serve ()
91
- return bs .server .StartBgp (ctx , & gobgpapi.StartBgpRequest {
103
+
104
+ err := bs .server .StartBgp (context .Background (), & gobgpapi.StartBgpRequest {
92
105
Global : & gobgpapi.Global {
93
106
Asn : bs .asn ,
94
107
RouterId : bs .routerId ,
95
108
ListenAddresses : []string {"0.0.0.0" },
96
109
ListenPort : bs .listenPort ,
97
110
},
98
111
})
99
- }
112
+ if err != nil {
113
+ return err
114
+ }
100
115
101
- func (bs * bgpServerConnector ) AddPeer (neighbor string , remoteAS uint32 , remotePort uint32 , keepaliveIntervalSec uint64 ) error {
102
- p := & gobgpapi.Peer {
103
- Conf : & gobgpapi.PeerConf {
104
- NeighborAddress : neighbor ,
105
- PeerAsn : remoteAS ,
106
- },
107
- Transport : & gobgpapi.Transport {
108
- RemoteAddress : neighbor ,
109
- RemotePort : remotePort ,
110
- },
111
- Timers : & gobgpapi.Timers {
112
- Config : & gobgpapi.TimersConfig {
113
- KeepaliveInterval : keepaliveIntervalSec ,
114
- HoldTime : keepaliveIntervalSec * 3 ,
116
+ for _ , peer := range bs .peers {
117
+ p := & gobgpapi.Peer {
118
+ Conf : & gobgpapi.PeerConf {
119
+ NeighborAddress : peer .Neighbor ,
120
+ PeerAsn : peer .RemoteAS ,
121
+ },
122
+ Transport : & gobgpapi.Transport {
123
+ RemoteAddress : peer .Neighbor ,
124
+ RemotePort : peer .RemoteAS ,
125
+ },
126
+ Timers : & gobgpapi.Timers {
127
+ Config : & gobgpapi.TimersConfig {
128
+ KeepaliveInterval : peer .KeepaliveIntervalSec ,
129
+ HoldTime : peer .KeepaliveIntervalSec * 3 ,
130
+ },
115
131
},
116
- },
117
132
118
- // route reflector client is always on
119
- RouteReflector : & gobgpapi.RouteReflector {
120
- RouteReflectorClient : true ,
121
- },
133
+ // route reflector client is always on
134
+ RouteReflector : & gobgpapi.RouteReflector {
135
+ RouteReflectorClient : true ,
136
+ },
137
+ }
138
+
139
+ err := bs .server .AddPeer (
140
+ context .Background (),
141
+ & gobgpapi.AddPeerRequest {
142
+ Peer : p ,
143
+ },
144
+ )
145
+ if err != nil {
146
+ return err
147
+ }
122
148
}
123
149
124
- return bs .server .AddPeer (
125
- bs .ctx ,
126
- & gobgpapi.AddPeerRequest {
127
- Peer : p ,
128
- },
129
- )
150
+ return nil
130
151
}
131
152
132
153
func (bs * bgpServerConnector ) AddPath (prefix string , prefixLen uint32 , nexthop string , community uint32 ) error {
@@ -152,7 +173,7 @@ func (bs *bgpServerConnector) AddPath(prefix string, prefixLen uint32, nexthop s
152
173
attrs = []* apb.Any {attrOrigin , attrNextHop , attrCommunities }
153
174
}
154
175
155
- _ , err = bs .server .AddPath (bs . ctx , & gobgpapi.AddPathRequest {
176
+ _ , err = bs .server .AddPath (context . Background () , & gobgpapi.AddPathRequest {
156
177
TableType : gobgpapi .TableType_GLOBAL ,
157
178
Path : & gobgpapi.Path {
158
179
Family : & gobgpapi.Family {
@@ -170,15 +191,14 @@ func (bs *bgpServerConnector) AddPath(prefix string, prefixLen uint32, nexthop s
170
191
func (bs * bgpServerConnector ) ListPath () ([]Route , error ) {
171
192
var routes []Route
172
193
173
- err := bs .server .ListPath (bs . ctx , & gobgpapi.ListPathRequest {
194
+ err := bs .server .ListPath (context . Background () , & gobgpapi.ListPathRequest {
174
195
TableType : gobgpapi .TableType_GLOBAL ,
175
196
Family : & gobgpapi.Family {
176
197
Afi : gobgpapi .Family_AFI_IP ,
177
198
Safi : gobgpapi .Family_SAFI_UNICAST ,
178
199
},
179
200
EnableFiltered : true ,
180
201
}, func (d * gobgpapi.Destination ) {
181
- bs .logger .Debug ("ListPath: found prefix" , "prefix" , d .Prefix )
182
202
for _ , path := range d .Paths {
183
203
for _ , attr := range path .GetPattrs () {
184
204
m , err := attr .UnmarshalNew ()
@@ -192,7 +212,6 @@ func (bs *bgpServerConnector) ListPath() ([]Route, error) {
192
212
}
193
213
194
214
for _ , comm := range ca .Communities {
195
- bs .logger .Debug ("ListPath: found community" , "community" , EncodeCommunity (comm ))
196
215
routes = append (routes , Route {
197
216
Prefix : d .Prefix ,
198
217
Community : comm ,
@@ -205,6 +224,10 @@ func (bs *bgpServerConnector) ListPath() ([]Route, error) {
205
224
return routes , err
206
225
}
207
226
227
+ func (bs * bgpServerConnector ) Stop () {
228
+ bs .server .Stop ()
229
+ }
230
+
208
231
// EncodeCommunity converts plain community value to human readable notation(for example 65001:10)
209
232
func EncodeCommunity (comm uint32 ) string {
210
233
upper := comm >> 16
0 commit comments