Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Table #59

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion gobgp/cmd/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,14 @@ import (
"sort"
"strconv"
"strings"
"time"

cli "github.com/osrg/gobgp/client"
"github.com/osrg/gobgp/config"
"github.com/osrg/gobgp/packet/bgp"
"github.com/osrg/gobgp/table"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
)

const (
Expand Down Expand Up @@ -223,8 +226,26 @@ func (v vrfs) Less(i, j int) bool {
}

func newClient() *cli.Client {
var grpcOpts []grpc.DialOption
if globalOpts.TLS {
var creds credentials.TransportCredentials
if globalOpts.CaFile == "" {
creds = credentials.NewClientTLSFromCert(nil, "")
} else {
var err error
creds, err = credentials.NewClientTLSFromFile(globalOpts.CaFile, "")
if err != nil {
exitWithError(err)
}
}
grpcOpts = []grpc.DialOption{
grpc.WithTimeout(time.Second),
grpc.WithBlock(),
grpc.WithTransportCredentials(creds),
}
}
target := net.JoinHostPort(globalOpts.Host, strconv.Itoa(globalOpts.Port))
client, err := cli.New(target)
client, err := cli.New(target, grpcOpts...)
if err != nil {
exitWithError(err)
}
Expand Down
4 changes: 4 additions & 0 deletions gobgp/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ var globalOpts struct {
GenCmpl bool
BashCmplFile string
PprofPort int
TLS bool
CaFile string
}

var cmds []string
Expand Down Expand Up @@ -77,6 +79,8 @@ func NewRootCmd() *cobra.Command {
rootCmd.PersistentFlags().BoolVarP(&globalOpts.GenCmpl, "gen-cmpl", "c", false, "generate completion file")
rootCmd.PersistentFlags().StringVarP(&globalOpts.BashCmplFile, "bash-cmpl-file", "", "gobgp-completion.bash", "bash cmpl filename")
rootCmd.PersistentFlags().IntVarP(&globalOpts.PprofPort, "pprof-port", "r", 0, "pprof port")
rootCmd.PersistentFlags().BoolVarP(&globalOpts.TLS, "tls", "", false, "connection uses TLS if true, else plain TCP")
rootCmd.PersistentFlags().StringVarP(&globalOpts.CaFile, "tls-ca-file", "", "", "The file containing the CA root cert file")

globalCmd := NewGlobalCmd()
neighborCmd := NewNeighborCmd()
Expand Down
32 changes: 23 additions & 9 deletions gobgpd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@
package main

import (
"io/ioutil"
"net/http"
_ "net/http/pprof"
"os"
"os/signal"
"runtime"
"syscall"

log "github.com/Sirupsen/logrus"
"github.com/jessevdk/go-flags"
p "github.com/kr/pretty"
Expand All @@ -24,13 +32,8 @@ import (
"github.com/osrg/gobgp/packet/bgp"
"github.com/osrg/gobgp/server"
"github.com/osrg/gobgp/table"
"io/ioutil"
"net/http"
_ "net/http/pprof"
"os"
"os/signal"
"runtime"
"syscall"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
)

func main() {
Expand All @@ -51,6 +54,9 @@ func main() {
Dry bool `short:"d" long:"dry-run" description:"check configuration"`
PProfHost string `long:"pprof-host" description:"specify the host that gobgpd listens on for pprof" default:"localhost:6060"`
PProfDisable bool `long:"pprof-disable" description:"disable pprof profiling"`
TLS bool `long:"tls" description:"enable TLS authentication for gRPC API"`
TLSCertFile string `long:"tls-cert-file" description:"The TLS cert file"`
TLSKeyFile string `long:"tls-key-file" description:"The TLS key file"`
}
_, err := flags.Parse(&opts)
if err != nil {
Expand Down Expand Up @@ -118,10 +124,18 @@ func main() {
bgpServer := server.NewBgpServer()
go bgpServer.Serve()

var grpcOpts []grpc.ServerOption
if opts.TLS {
creds, err := credentials.NewServerTLSFromFile(opts.TLSCertFile, opts.TLSKeyFile)
if err != nil {
log.Fatalf("Failed to generate credentials: %v", err)
}
grpcOpts = []grpc.ServerOption{grpc.Creds(creds)}
}
// start grpc Server
grpcServer := api.NewGrpcServer(bgpServer, opts.GrpcHosts)
apiServer := api.NewServer(bgpServer, grpc.NewServer(grpcOpts...), opts.GrpcHosts)
go func() {
if err := grpcServer.Serve(); err != nil {
if err := apiServer.Serve(); err != nil {
log.Fatalf("failed to listen grpc port: %s", err)
}
}()
Expand Down
11 changes: 10 additions & 1 deletion table/destination.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ func (dest *Destination) Calculate(ids []string) (map[string]*Path, map[string]*
bestList := make(map[string]*Path, len(ids))
oldList := make(map[string]*Path, len(ids))
oldKnownPathList := dest.knownPathList

// First remove the withdrawn paths.
dest.explicitWithdraw()
// Do implicit withdrawal
Expand All @@ -246,6 +247,8 @@ func (dest *Destination) Calculate(ids []string) (map[string]*Path, map[string]*
dest.newPathList = make([]*Path, 0)
// Compute new best path
dest.computeKnownBestPath()
// Cache to reduce massive call of old.Clone(true)
withdrawCache := make(map[*Path]*Path)

f := func(id string) (*Path, *Path) {
old := getBestPath(id, &oldKnownPathList)
Expand All @@ -270,7 +273,13 @@ func (dest *Destination) Calculate(ids []string) (map[string]*Path, map[string]*
if old == nil {
return nil, nil
}
return old.Clone(true), old
if cache := withdrawCache[old]; cache != nil {
return cache, old
} else {
cache = old.Clone(true)
withdrawCache[old] = cache
return cache, old
}
}
return best, old
}
Expand Down