Skip to content

Commit f5290a8

Browse files
Merge pull request #16 from utilitywarehouse/gobgp-v3.0.0
Update gobgp to v3.0.0
2 parents e6f0442 + d8274d1 commit f5290a8

File tree

3 files changed

+154
-59
lines changed

3 files changed

+154
-59
lines changed

bgp.go

+21-17
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1+
// https://github.com/osrg/gobgp/blob/master/docs/sources/lib.md
12
package main
23

34
import (
45
"context"
56
"fmt"
67

7-
"github.com/golang/protobuf/ptypes"
8-
"github.com/golang/protobuf/ptypes/any"
9-
api "github.com/osrg/gobgp/api"
10-
gobgp "github.com/osrg/gobgp/pkg/server"
8+
api "github.com/osrg/gobgp/v3/api"
9+
gobgp "github.com/osrg/gobgp/v3/pkg/server"
1110
log "github.com/sirupsen/logrus"
11+
apb "google.golang.org/protobuf/types/known/anypb"
1212
)
1313

1414
var (
@@ -19,14 +19,14 @@ type BgpServer struct {
1919
server *gobgp.BgpServer
2020
}
2121

22-
func initBgpServer(routerId string, as uint32, listenPort int32) (*BgpServer, error) {
22+
func initBgpServer(routerId string, asn uint32, listenPort int32) (*BgpServer, error) {
2323
s := gobgp.NewBgpServer()
2424
go s.Serve()
2525

2626
// global configuration
2727
if err := s.StartBgp(context.Background(), &api.StartBgpRequest{
2828
Global: &api.Global{
29-
As: as,
29+
Asn: asn,
3030
RouterId: routerId,
3131
ListenPort: listenPort,
3232
},
@@ -35,36 +35,40 @@ func initBgpServer(routerId string, as uint32, listenPort int32) (*BgpServer, er
3535
}
3636

3737
// monitor the change of the peer state
38-
if err := s.MonitorPeer(context.Background(), &api.MonitorPeerRequest{}, func(p *api.Peer) { log.Info(p) }); err != nil {
38+
if err := s.WatchEvent(context.Background(), &api.WatchEventRequest{Peer: &api.WatchEventRequest_Peer{}}, func(r *api.WatchEventResponse) {
39+
if p := r.GetPeer(); p != nil && p.Type == api.WatchEventResponse_PeerEvent_STATE {
40+
log.Info(p)
41+
}
42+
}); err != nil {
3943
log.Fatal(err)
4044
}
4145

4246
return &BgpServer{server: s}, nil
4347
}
4448

45-
func (bs *BgpServer) AddPeer(address string, as uint32) error {
49+
func (bs *BgpServer) AddPeer(address string, asn uint32) error {
4650
n := &api.Peer{
4751
Conf: &api.PeerConf{
4852
NeighborAddress: address,
49-
PeerAs: as,
53+
PeerAsn: asn,
5054
},
5155
}
5256
return bs.server.AddPeer(context.Background(), &api.AddPeerRequest{Peer: n})
5357
}
5458

5559
func (bs *BgpServer) AddV4Path(prefix string, prefixLen uint32, nextHop string) error {
56-
nlri, _ := ptypes.MarshalAny(&api.IPAddressPrefix{
60+
nlri, _ := apb.New(&api.IPAddressPrefix{
5761
Prefix: prefix,
5862
PrefixLen: prefixLen,
5963
})
6064

61-
a1, _ := ptypes.MarshalAny(&api.OriginAttribute{
65+
a1, _ := apb.New(&api.OriginAttribute{
6266
Origin: 0, // the prefix originates from an interior routing protocol (IGP)
6367
})
64-
a2, _ := ptypes.MarshalAny(&api.NextHopAttribute{
68+
a2, _ := apb.New(&api.NextHopAttribute{
6569
NextHop: nextHop,
6670
})
67-
attrs := []*any.Any{a1, a2}
71+
attrs := []*apb.Any{a1, a2}
6872

6973
_, err := bs.server.AddPath(context.Background(), &api.AddPathRequest{
7074
Path: &api.Path{
@@ -81,18 +85,18 @@ func (bs *BgpServer) AddV4Path(prefix string, prefixLen uint32, nextHop string)
8185
}
8286

8387
func (bs *BgpServer) DeleteV4Path(prefix string, prefixLen uint32, nextHop string) error {
84-
nlri, _ := ptypes.MarshalAny(&api.IPAddressPrefix{
88+
nlri, _ := apb.New(&api.IPAddressPrefix{
8589
Prefix: prefix,
8690
PrefixLen: prefixLen,
8791
})
8892

89-
a1, _ := ptypes.MarshalAny(&api.OriginAttribute{
93+
a1, _ := apb.New(&api.OriginAttribute{
9094
Origin: 0, // the prefix originates from an interior routing protocol (IGP)
9195
})
92-
a2, _ := ptypes.MarshalAny(&api.NextHopAttribute{
96+
a2, _ := apb.New(&api.NextHopAttribute{
9397
NextHop: nextHop,
9498
})
95-
attrs := []*any.Any{a1, a2}
99+
attrs := []*apb.Any{a1, a2}
96100

97101
err := bs.server.DeletePath(context.Background(), &api.DeletePathRequest{
98102
Path: &api.Path{

go.mod

+5-5
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@ module github.com/utilitywarehouse/bgp-lb
33
go 1.18
44

55
require (
6-
github.com/golang/protobuf v1.5.2
76
github.com/moby/ipvs v1.0.1
8-
github.com/osrg/gobgp v0.0.0-20201101142815-7ce0dddd4f49
7+
github.com/osrg/gobgp/v3 v3.0.0
98
github.com/prometheus/client_golang v1.12.1
109
github.com/sirupsen/logrus v1.8.1
1110
github.com/stretchr/testify v1.7.1
12-
github.com/vishvananda/netlink v1.1.0
11+
github.com/vishvananda/netlink v1.1.1-0.20210330154013-f5de75959ad5
12+
google.golang.org/protobuf v1.27.1
1313
)
1414

1515
require (
@@ -20,6 +20,7 @@ require (
2020
github.com/eapache/channels v1.1.0 // indirect
2121
github.com/eapache/queue v1.1.0 // indirect
2222
github.com/fsnotify/fsnotify v1.5.1 // indirect
23+
github.com/golang/protobuf v1.5.2 // indirect
2324
github.com/google/uuid v1.3.0 // indirect
2425
github.com/hashicorp/hcl v1.0.0 // indirect
2526
github.com/k-sone/critbitgo v1.4.0 // indirect
@@ -41,9 +42,8 @@ require (
4142
golang.org/x/net v0.0.0-20220325170049-de3da57026de // indirect
4243
golang.org/x/sys v0.0.0-20220327210214-530d0810a4d0 // indirect
4344
golang.org/x/text v0.3.7 // indirect
44-
google.golang.org/genproto v0.0.0-20220324131243-acbaeb5b85eb // indirect
45+
google.golang.org/genproto v0.0.0-20211208223120-3a66f561d7aa // indirect
4546
google.golang.org/grpc v1.45.0 // indirect
46-
google.golang.org/protobuf v1.28.0 // indirect
4747
gopkg.in/ini.v1 v1.66.4 // indirect
4848
gopkg.in/yaml.v2 v2.4.0 // indirect
4949
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect

0 commit comments

Comments
 (0)