-
Notifications
You must be signed in to change notification settings - Fork 2.8k
/
state.go
234 lines (197 loc) · 6.85 KB
/
state.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
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of Cilium
package gobgp
import (
"context"
"fmt"
gobgp "github.com/osrg/gobgp/v3/api"
"github.com/cilium/cilium/api/v1/models"
"github.com/cilium/cilium/pkg/bgpv1/types"
v2alpha1api "github.com/cilium/cilium/pkg/k8s/apis/cilium.io/v2alpha1"
"github.com/cilium/cilium/pkg/time"
)
// GetBgp returns bgp global configuration from gobgp server
func (g *GoBGPServer) GetBGP(ctx context.Context) (types.GetBGPResponse, error) {
bgpConfig, err := g.server.GetBgp(ctx, &gobgp.GetBgpRequest{})
if err != nil {
return types.GetBGPResponse{}, err
}
if bgpConfig.Global == nil {
return types.GetBGPResponse{}, fmt.Errorf("gobgp returned nil config")
}
res := types.BGPGlobal{
ASN: bgpConfig.Global.Asn,
RouterID: bgpConfig.Global.RouterId,
ListenPort: bgpConfig.Global.ListenPort,
}
if bgpConfig.Global.RouteSelectionOptions != nil {
res.RouteSelectionOptions = &types.RouteSelectionOptions{
AdvertiseInactiveRoutes: bgpConfig.Global.RouteSelectionOptions.AdvertiseInactiveRoutes,
}
}
return types.GetBGPResponse{
Global: res,
}, nil
}
// GetPeerState invokes goBGP ListPeer API to get current peering state.
func (g *GoBGPServer) GetPeerState(ctx context.Context) (types.GetPeerStateResponse, error) {
var data []*models.BgpPeer
fn := func(peer *gobgp.Peer) {
if peer == nil {
return
}
peerState := &models.BgpPeer{}
if peer.Transport != nil {
peerState.PeerPort = int64(peer.Transport.RemotePort)
}
if peer.Conf != nil {
peerState.LocalAsn = int64(peer.Conf.LocalAsn)
peerState.PeerAddress = peer.Conf.NeighborAddress
peerState.PeerAsn = int64(peer.Conf.PeerAsn)
peerState.TCPPasswordEnabled = peer.Conf.AuthPassword != ""
}
if peer.State != nil {
peerState.SessionState = toAgentSessionState(peer.State.SessionState).String()
// Uptime is time since session got established.
// It is calculated by difference in time from uptime timestamp till now.
if peer.State.SessionState == gobgp.PeerState_ESTABLISHED && peer.Timers != nil && peer.Timers.State != nil {
peerState.UptimeNanoseconds = int64(time.Since(peer.Timers.State.Uptime.AsTime()))
}
}
for _, afiSafi := range peer.AfiSafis {
if afiSafi.State == nil {
continue
}
peerState.Families = append(peerState.Families, toAgentAfiSafiState(afiSafi.State))
}
if peer.EbgpMultihop != nil && peer.EbgpMultihop.Enabled {
peerState.EbgpMultihopTTL = int64(peer.EbgpMultihop.MultihopTtl)
} else {
peerState.EbgpMultihopTTL = int64(v2alpha1api.DefaultBGPEBGPMultihopTTL) // defaults to 1 if not enabled
}
if peer.Timers != nil {
tConfig := peer.Timers.Config
tState := peer.Timers.State
if tConfig != nil {
peerState.ConnectRetryTimeSeconds = int64(tConfig.ConnectRetry)
peerState.ConfiguredHoldTimeSeconds = int64(tConfig.HoldTime)
peerState.ConfiguredKeepAliveTimeSeconds = int64(tConfig.KeepaliveInterval)
}
if tState != nil {
if tState.NegotiatedHoldTime != 0 {
peerState.AppliedHoldTimeSeconds = int64(tState.NegotiatedHoldTime)
}
if tState.KeepaliveInterval != 0 {
peerState.AppliedKeepAliveTimeSeconds = int64(tState.KeepaliveInterval)
}
}
}
peerState.GracefulRestart = &models.BgpGracefulRestart{}
if peer.GracefulRestart != nil {
peerState.GracefulRestart.Enabled = peer.GracefulRestart.Enabled
peerState.GracefulRestart.RestartTimeSeconds = int64(peer.GracefulRestart.RestartTime)
}
data = append(data, peerState)
}
// API to get peering list from gobgp, enableAdvertised is set to true to get count of
// advertised routes.
err := g.server.ListPeer(ctx, &gobgp.ListPeerRequest{EnableAdvertised: true}, fn)
if err != nil {
return types.GetPeerStateResponse{}, err
}
return types.GetPeerStateResponse{
Peers: data,
}, nil
}
// toAgentAfiSafiState translates gobgp structures to cilium bgp models.
func toAgentAfiSafiState(state *gobgp.AfiSafiState) *models.BgpPeerFamilies {
res := &models.BgpPeerFamilies{}
if state.Family != nil {
res.Afi = toAgentAfi(state.Family.Afi).String()
res.Safi = toAgentSafi(state.Family.Safi).String()
}
res.Received = int64(state.Received)
res.Accepted = int64(state.Accepted)
res.Advertised = int64(state.Advertised)
return res
}
// GetRoutes retrieves routes from the RIB of underlying router
func (g *GoBGPServer) GetRoutes(ctx context.Context, r *types.GetRoutesRequest) (*types.GetRoutesResponse, error) {
errs := []error{}
var routes []*types.Route
fn := func(destination *gobgp.Destination) {
paths, err := ToAgentPaths(destination.Paths)
if err != nil {
errs = append(errs, err)
return
}
routes = append(routes, &types.Route{
Prefix: destination.Prefix,
Paths: paths,
})
}
tt, err := toGoBGPTableType(r.TableType)
if err != nil {
return nil, fmt.Errorf("invalid table type: %w", err)
}
family := &gobgp.Family{
Afi: gobgp.Family_Afi(r.Family.Afi),
Safi: gobgp.Family_Safi(r.Family.Safi),
}
var neighbor string
if r.Neighbor.IsValid() {
neighbor = r.Neighbor.String()
}
req := &gobgp.ListPathRequest{
TableType: tt,
Family: family,
Name: neighbor,
}
if err := g.server.ListPath(ctx, req, fn); err != nil {
return nil, err
}
return &types.GetRoutesResponse{
Routes: routes,
}, nil
}
// GetRoutePolicies retrieves route policies from the underlying router
func (g *GoBGPServer) GetRoutePolicies(ctx context.Context) (*types.GetRoutePoliciesResponse, error) {
// list defined sets into a map for later use
definedSets := make(map[string]*gobgp.DefinedSet)
err := g.server.ListDefinedSet(ctx, &gobgp.ListDefinedSetRequest{DefinedType: gobgp.DefinedType_NEIGHBOR}, func(ds *gobgp.DefinedSet) {
definedSets[ds.Name] = ds
})
if err != nil {
return nil, fmt.Errorf("failed listing neighbor defined sets: %w", err)
}
err = g.server.ListDefinedSet(ctx, &gobgp.ListDefinedSetRequest{DefinedType: gobgp.DefinedType_PREFIX}, func(ds *gobgp.DefinedSet) {
definedSets[ds.Name] = ds
})
if err != nil {
return nil, fmt.Errorf("failed listing prefix defined sets: %w", err)
}
// list policy assignments into a map for later use
assignments := make(map[string]*gobgp.PolicyAssignment)
err = g.server.ListPolicyAssignment(ctx, &gobgp.ListPolicyAssignmentRequest{}, func(a *gobgp.PolicyAssignment) {
for _, p := range a.Policies {
assignments[p.Name] = a
}
})
if err != nil {
return nil, fmt.Errorf("failed listing policy assignments: %w", err)
}
// list & convert policies
var policies []*types.RoutePolicy
err = g.server.ListPolicy(ctx, &gobgp.ListPolicyRequest{}, func(p *gobgp.Policy) {
// process only assigned policies
if assignment, exists := assignments[p.Name]; exists {
policies = append(policies, toAgentPolicy(p, definedSets, assignment))
}
})
if err != nil {
return nil, fmt.Errorf("failed listing route policies: %w", err)
}
return &types.GetRoutePoliciesResponse{
Policies: policies,
}, nil
}