-
Notifications
You must be signed in to change notification settings - Fork 0
/
bgp.go
143 lines (126 loc) · 3.43 KB
/
bgp.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
// https://github.com/osrg/gobgp/blob/master/docs/sources/lib.md
package main
import (
"context"
"fmt"
api "github.com/osrg/gobgp/v3/api"
gobgp "github.com/osrg/gobgp/v3/pkg/server"
log "github.com/sirupsen/logrus"
apb "google.golang.org/protobuf/types/known/anypb"
)
var (
v4Family = &api.Family{Afi: api.Family_AFI_IP, Safi: api.Family_SAFI_UNICAST} // &gobgpapi.Family literal is not a constant
)
type BgpServer struct {
server *gobgp.BgpServer
}
func initBgpServer(routerId string, asn uint32, listenPort int32) (*BgpServer, error) {
s := gobgp.NewBgpServer()
go s.Serve()
// global configuration
if err := s.StartBgp(context.Background(), &api.StartBgpRequest{
Global: &api.Global{
Asn: asn,
RouterId: routerId,
ListenPort: listenPort,
},
}); err != nil {
return nil, err
}
// monitor the change of the peer state
if err := s.WatchEvent(context.Background(), &api.WatchEventRequest{Peer: &api.WatchEventRequest_Peer{}}, func(r *api.WatchEventResponse) {
if p := r.GetPeer(); p != nil && p.Type == api.WatchEventResponse_PeerEvent_STATE {
log.Info(p)
}
}); err != nil {
log.Fatal(err)
}
return &BgpServer{server: s}, nil
}
func (bs *BgpServer) AddPeer(address string, asn uint32) error {
n := &api.Peer{
Conf: &api.PeerConf{
NeighborAddress: address,
PeerAsn: asn,
},
}
return bs.server.AddPeer(context.Background(), &api.AddPeerRequest{Peer: n})
}
func (bs *BgpServer) AddV4Path(prefix string, prefixLen uint32, nextHop string) error {
nlri, _ := apb.New(&api.IPAddressPrefix{
Prefix: prefix,
PrefixLen: prefixLen,
})
a1, _ := apb.New(&api.OriginAttribute{
Origin: 0, // the prefix originates from an interior routing protocol (IGP)
})
a2, _ := apb.New(&api.NextHopAttribute{
NextHop: nextHop,
})
attrs := []*apb.Any{a1, a2}
_, err := bs.server.AddPath(context.Background(), &api.AddPathRequest{
Path: &api.Path{
Family: v4Family,
Nlri: nlri,
Pattrs: attrs,
},
})
if err != nil {
return err
}
setBGPPathAdvertisementMetric(prefix, fmt.Sprint(prefixLen), nextHop)
return nil
}
func (bs *BgpServer) DeleteV4Path(prefix string, prefixLen uint32, nextHop string) error {
nlri, _ := apb.New(&api.IPAddressPrefix{
Prefix: prefix,
PrefixLen: prefixLen,
})
a1, _ := apb.New(&api.OriginAttribute{
Origin: 0, // the prefix originates from an interior routing protocol (IGP)
})
a2, _ := apb.New(&api.NextHopAttribute{
NextHop: nextHop,
})
attrs := []*apb.Any{a1, a2}
err := bs.server.DeletePath(context.Background(), &api.DeletePathRequest{
Path: &api.Path{
Family: v4Family,
Nlri: nlri,
Pattrs: attrs,
},
})
if err != nil {
return err
}
unsetBGPPathAdvertisementMetric(prefix, fmt.Sprint(prefixLen), nextHop)
return nil
}
func (bs *BgpServer) ListV4Paths() {
bs.server.ListPath(context.Background(), &api.ListPathRequest{Family: v4Family}, func(p *api.Destination) {
log.Info(p)
})
}
// bgpSetup starts the bgp server and adds the peers
func bgpSetup(bgpConfig bgpConfig) *BgpServer {
// Start bgp server
bgp, err := initBgpServer(
bgpConfig.Local.RouterId,
bgpConfig.Local.AS,
bgpConfig.Local.ListenPort,
)
if err != nil {
log.WithFields(log.Fields{
"error": err,
}).Fatal("Cannot start bgp server")
}
// Add Peers
for _, peer := range bgpConfig.Peers {
if err := bgp.AddPeer(peer.Address, peer.AS); err != nil {
log.WithFields(log.Fields{
"error": err,
}).Fatal("Cannot add bgpp peer")
}
}
return bgp
}