Skip to content

Commit

Permalink
Merge pull request #16 from utilitywarehouse/gobgp-v3.0.0
Browse files Browse the repository at this point in the history
Update gobgp to v3.0.0
  • Loading branch information
ffilippopoulos authored Mar 30, 2022
2 parents e6f0442 + d8274d1 commit f5290a8
Show file tree
Hide file tree
Showing 3 changed files with 154 additions and 59 deletions.
38 changes: 21 additions & 17 deletions bgp.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
// https://github.com/osrg/gobgp/blob/master/docs/sources/lib.md
package main

import (
"context"
"fmt"

"github.com/golang/protobuf/ptypes"
"github.com/golang/protobuf/ptypes/any"
api "github.com/osrg/gobgp/api"
gobgp "github.com/osrg/gobgp/pkg/server"
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 (
Expand All @@ -19,14 +19,14 @@ type BgpServer struct {
server *gobgp.BgpServer
}

func initBgpServer(routerId string, as uint32, listenPort int32) (*BgpServer, error) {
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{
As: as,
Asn: asn,
RouterId: routerId,
ListenPort: listenPort,
},
Expand All @@ -35,36 +35,40 @@ func initBgpServer(routerId string, as uint32, listenPort int32) (*BgpServer, er
}

// monitor the change of the peer state
if err := s.MonitorPeer(context.Background(), &api.MonitorPeerRequest{}, func(p *api.Peer) { log.Info(p) }); err != nil {
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, as uint32) error {
func (bs *BgpServer) AddPeer(address string, asn uint32) error {
n := &api.Peer{
Conf: &api.PeerConf{
NeighborAddress: address,
PeerAs: as,
PeerAsn: asn,
},
}
return bs.server.AddPeer(context.Background(), &api.AddPeerRequest{Peer: n})
}

func (bs *BgpServer) AddV4Path(prefix string, prefixLen uint32, nextHop string) error {
nlri, _ := ptypes.MarshalAny(&api.IPAddressPrefix{
nlri, _ := apb.New(&api.IPAddressPrefix{
Prefix: prefix,
PrefixLen: prefixLen,
})

a1, _ := ptypes.MarshalAny(&api.OriginAttribute{
a1, _ := apb.New(&api.OriginAttribute{
Origin: 0, // the prefix originates from an interior routing protocol (IGP)
})
a2, _ := ptypes.MarshalAny(&api.NextHopAttribute{
a2, _ := apb.New(&api.NextHopAttribute{
NextHop: nextHop,
})
attrs := []*any.Any{a1, a2}
attrs := []*apb.Any{a1, a2}

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

func (bs *BgpServer) DeleteV4Path(prefix string, prefixLen uint32, nextHop string) error {
nlri, _ := ptypes.MarshalAny(&api.IPAddressPrefix{
nlri, _ := apb.New(&api.IPAddressPrefix{
Prefix: prefix,
PrefixLen: prefixLen,
})

a1, _ := ptypes.MarshalAny(&api.OriginAttribute{
a1, _ := apb.New(&api.OriginAttribute{
Origin: 0, // the prefix originates from an interior routing protocol (IGP)
})
a2, _ := ptypes.MarshalAny(&api.NextHopAttribute{
a2, _ := apb.New(&api.NextHopAttribute{
NextHop: nextHop,
})
attrs := []*any.Any{a1, a2}
attrs := []*apb.Any{a1, a2}

err := bs.server.DeletePath(context.Background(), &api.DeletePathRequest{
Path: &api.Path{
Expand Down
10 changes: 5 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ module github.com/utilitywarehouse/bgp-lb
go 1.18

require (
github.com/golang/protobuf v1.5.2
github.com/moby/ipvs v1.0.1
github.com/osrg/gobgp v0.0.0-20201101142815-7ce0dddd4f49
github.com/osrg/gobgp/v3 v3.0.0
github.com/prometheus/client_golang v1.12.1
github.com/sirupsen/logrus v1.8.1
github.com/stretchr/testify v1.7.1
github.com/vishvananda/netlink v1.1.0
github.com/vishvananda/netlink v1.1.1-0.20210330154013-f5de75959ad5
google.golang.org/protobuf v1.27.1
)

require (
Expand All @@ -20,6 +20,7 @@ require (
github.com/eapache/channels v1.1.0 // indirect
github.com/eapache/queue v1.1.0 // indirect
github.com/fsnotify/fsnotify v1.5.1 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/k-sone/critbitgo v1.4.0 // indirect
Expand All @@ -41,9 +42,8 @@ require (
golang.org/x/net v0.0.0-20220325170049-de3da57026de // indirect
golang.org/x/sys v0.0.0-20220327210214-530d0810a4d0 // indirect
golang.org/x/text v0.3.7 // indirect
google.golang.org/genproto v0.0.0-20220324131243-acbaeb5b85eb // indirect
google.golang.org/genproto v0.0.0-20211208223120-3a66f561d7aa // indirect
google.golang.org/grpc v1.45.0 // indirect
google.golang.org/protobuf v1.28.0 // indirect
gopkg.in/ini.v1 v1.66.4 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
Expand Down
Loading

0 comments on commit f5290a8

Please sign in to comment.