diff options
-rw-r--r-- | cmd/gobgpd/main.go | 60 | ||||
-rw-r--r-- | docs/sources/lib.md | 5 | ||||
-rw-r--r-- | pkg/server/grpc_server.go | 143 | ||||
-rw-r--r-- | pkg/server/server.go | 43 |
4 files changed, 125 insertions, 126 deletions
diff --git a/cmd/gobgpd/main.go b/cmd/gobgpd/main.go index 23e7ec96..262883ee 100644 --- a/cmd/gobgpd/main.go +++ b/cmd/gobgpd/main.go @@ -146,25 +146,19 @@ func main() { os.Exit(0) } - log.Info("gobgpd started") - bgpServer := server.NewBgpServer() - go bgpServer.Serve() - - var grpcOpts []grpc.ServerOption + maxSize := 256 << 20 + grpcOpts := []grpc.ServerOption{grpc.MaxRecvMsgSize(maxSize), grpc.MaxSendMsgSize(maxSize)} 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)} + grpcOpts = append(grpcOpts, grpc.Creds(creds)) } - // start grpc Server - apiServer := server.NewServer(bgpServer, grpc.NewServer(grpcOpts...), opts.GrpcHosts) - go func() { - if err := apiServer.Serve(); err != nil { - log.Fatalf("failed to listen grpc port: %s", err) - } - }() + + log.Info("gobgpd started") + bgpServer := server.NewBgpServer(server.GrpcListenAddress(opts.GrpcHosts), server.GrpcOption(grpcOpts)) + go bgpServer.Serve() if opts.ConfigFile != "" { go config.ReadConfigfileServe(opts.ConfigFile, opts.ConfigType, configCh) @@ -175,7 +169,7 @@ func main() { for { select { case <-sigCh: - apiServer.StopBgp(context.Background(), &api.StopBgpRequest{}) + bgpServer.StopBgp(context.Background(), &api.StopBgpRequest{}) return case newConfig := <-configCh: var added, deleted, updated []config.Neighbor @@ -184,7 +178,7 @@ func main() { if c == nil { c = newConfig - if _, err := apiServer.StartBgp(context.Background(), &api.StartBgpRequest{ + if err := bgpServer.StartBgp(context.Background(), &api.StartBgpRequest{ Global: config.NewGlobalFromConfigStruct(&c.Global), }); err != nil { log.Fatalf("failed to set global config: %s", err) @@ -196,7 +190,7 @@ func main() { for _, t := range tps { l = append(l, string(t)) } - if _, err := apiServer.EnableZebra(context.Background(), &api.EnableZebraRequest{ + if err := bgpServer.EnableZebra(context.Background(), &api.EnableZebraRequest{ Url: c.Zebra.Config.Url, RouteTypes: l, Version: uint32(c.Zebra.Config.Version), @@ -212,7 +206,7 @@ func main() { } for _, c := range newConfig.RpkiServers { - if _, err := apiServer.AddRpki(context.Background(), &api.AddRpkiRequest{ + if err := bgpServer.AddRpki(context.Background(), &api.AddRpkiRequest{ Address: c.Config.Address, Port: c.Config.Port, Lifetime: c.Config.RecordLifetime, @@ -221,7 +215,7 @@ func main() { } } for _, c := range newConfig.BmpServers { - if _, err := apiServer.AddBmp(context.Background(), &api.AddBmpRequest{ + if err := bgpServer.AddBmp(context.Background(), &api.AddBmpRequest{ Address: c.Config.Address, Port: c.Config.Port, Type: api.AddBmpRequest_MonitoringPolicy(c.Config.RouteMonitoringPolicy.ToInt()), @@ -244,7 +238,7 @@ func main() { log.Fatalf("failed to load vrf export rt config: %s", err) } - if _, err := apiServer.AddVrf(context.Background(), &api.AddVrfRequest{ + if err := bgpServer.AddVrf(context.Background(), &api.AddVrfRequest{ Vrf: &api.Vrf{ Name: vrf.Config.Name, Rd: apiutil.MarshalRD(rd), @@ -260,7 +254,7 @@ func main() { if len(c.Config.FileName) == 0 { continue } - if _, err := apiServer.EnableMrt(context.Background(), &api.EnableMrtRequest{ + if err := bgpServer.EnableMrt(context.Background(), &api.EnableMrtRequest{ DumpType: int32(c.Config.DumpType.ToInt()), Filename: c.Config.FileName, Interval: c.Config.DumpInterval, @@ -273,7 +267,7 @@ func main() { if err != nil { log.Warn(err) } else { - apiServer.SetPolicies(context.Background(), &api.SetPoliciesRequest{ + bgpServer.SetPolicies(context.Background(), &api.SetPoliciesRequest{ DefinedSets: rp.DefinedSets, Policies: rp.Policies, }) @@ -301,7 +295,7 @@ func main() { if err != nil { log.Warn(err) } else { - apiServer.SetPolicies(context.Background(), &api.SetPoliciesRequest{ + bgpServer.SetPolicies(context.Background(), &api.SetPoliciesRequest{ DefinedSets: rp.DefinedSets, Policies: rp.Policies, }) @@ -332,7 +326,7 @@ func main() { def := toDefaultTable(a.DefaultImportPolicy) ps := toPolicies(a.ImportPolicyList) - apiServer.SetPolicyAssignment(context.Background(), &api.SetPolicyAssignmentRequest{ + bgpServer.SetPolicyAssignment(context.Background(), &api.SetPolicyAssignmentRequest{ Assignment: table.NewAPIPolicyAssignmentFromTableStruct(&table.PolicyAssignment{ Name: table.GLOBAL_RIB_NAME, Type: table.POLICY_DIRECTION_IMPORT, @@ -343,7 +337,7 @@ func main() { def = toDefaultTable(a.DefaultExportPolicy) ps = toPolicies(a.ExportPolicyList) - apiServer.SetPolicyAssignment(context.Background(), &api.SetPolicyAssignmentRequest{ + bgpServer.SetPolicyAssignment(context.Background(), &api.SetPolicyAssignmentRequest{ Assignment: table.NewAPIPolicyAssignmentFromTableStruct(&table.PolicyAssignment{ Name: table.GLOBAL_RIB_NAME, Type: table.POLICY_DIRECTION_EXPORT, @@ -359,7 +353,7 @@ func main() { } for _, pg := range addedPg { log.Infof("PeerGroup %s is added", pg.Config.PeerGroupName) - if _, err := apiServer.AddPeerGroup(context.Background(), &api.AddPeerGroupRequest{ + if err := bgpServer.AddPeerGroup(context.Background(), &api.AddPeerGroupRequest{ PeerGroup: config.NewPeerGroupFromConfigStruct(&pg), }); err != nil { log.Warn(err) @@ -367,7 +361,7 @@ func main() { } for _, pg := range deletedPg { log.Infof("PeerGroup %s is deleted", pg.Config.PeerGroupName) - if _, err := apiServer.DeletePeerGroup(context.Background(), &api.DeletePeerGroupRequest{ + if err := bgpServer.DeletePeerGroup(context.Background(), &api.DeletePeerGroupRequest{ Name: pg.Config.PeerGroupName, }); err != nil { log.Warn(err) @@ -375,7 +369,7 @@ func main() { } for _, pg := range updatedPg { log.Infof("PeerGroup %v is updated", pg.State.PeerGroupName) - if u, err := apiServer.UpdatePeerGroup(context.Background(), &api.UpdatePeerGroupRequest{ + if u, err := bgpServer.UpdatePeerGroup(context.Background(), &api.UpdatePeerGroupRequest{ PeerGroup: config.NewPeerGroupFromConfigStruct(&pg), }); err != nil { log.Warn(err) @@ -385,7 +379,7 @@ func main() { } for _, pg := range updatedPg { log.Infof("PeerGroup %s is updated", pg.Config.PeerGroupName) - if _, err := apiServer.UpdatePeerGroup(context.Background(), &api.UpdatePeerGroupRequest{ + if _, err := bgpServer.UpdatePeerGroup(context.Background(), &api.UpdatePeerGroupRequest{ PeerGroup: config.NewPeerGroupFromConfigStruct(&pg), }); err != nil { log.Warn(err) @@ -393,7 +387,7 @@ func main() { } for _, dn := range newConfig.DynamicNeighbors { log.Infof("Dynamic Neighbor %s is added to PeerGroup %s", dn.Config.Prefix, dn.Config.PeerGroup) - if _, err := apiServer.AddDynamicNeighbor(context.Background(), &api.AddDynamicNeighborRequest{ + if err := bgpServer.AddDynamicNeighbor(context.Background(), &api.AddDynamicNeighborRequest{ DynamicNeighbor: &api.DynamicNeighbor{ Prefix: dn.Config.Prefix, PeerGroup: dn.Config.PeerGroup, @@ -404,7 +398,7 @@ func main() { } for _, p := range added { log.Infof("Peer %v is added", p.State.NeighborAddress) - if _, err := apiServer.AddPeer(context.Background(), &api.AddPeerRequest{ + if err := bgpServer.AddPeer(context.Background(), &api.AddPeerRequest{ Peer: config.NewPeerFromConfigStruct(&p), }); err != nil { log.Warn(err) @@ -412,7 +406,7 @@ func main() { } for _, p := range deleted { log.Infof("Peer %v is deleted", p.State.NeighborAddress) - if _, err := apiServer.DeletePeer(context.Background(), &api.DeletePeerRequest{ + if err := bgpServer.DeletePeer(context.Background(), &api.DeletePeerRequest{ Address: p.State.NeighborAddress, }); err != nil { log.Warn(err) @@ -420,7 +414,7 @@ func main() { } for _, p := range updated { log.Infof("Peer %v is updated", p.State.NeighborAddress) - if u, err := apiServer.UpdatePeer(context.Background(), &api.UpdatePeerRequest{ + if u, err := bgpServer.UpdatePeer(context.Background(), &api.UpdatePeerRequest{ Peer: config.NewPeerFromConfigStruct(&p), }); err != nil { log.Warn(err) @@ -430,7 +424,7 @@ func main() { } if updatePolicy { - if _, err := apiServer.ResetPeer(context.Background(), &api.ResetPeerRequest{ + if err := bgpServer.ResetPeer(context.Background(), &api.ResetPeerRequest{ Address: "", Direction: api.ResetPeerRequest_IN, Soft: true, diff --git a/docs/sources/lib.md b/docs/sources/lib.md index fb8ba5ee..c76133f9 100644 --- a/docs/sources/lib.md +++ b/docs/sources/lib.md @@ -27,11 +27,6 @@ func main() { s := gobgp.NewBgpServer() go s.Serve() - // start grpc api server. this is not mandatory - // but you will be able to use `gobgp` cmd with this. - g := gobgp.NewGrpcServer(s, ":50051") - go g.Serve() - // global configuration if err := s.StartBgp(context.Background(), &api.StartBgpRequest{ Global: &api.Global{ diff --git a/pkg/server/grpc_server.go b/pkg/server/grpc_server.go index 2b4fe02b..86c756a9 100644 --- a/pkg/server/grpc_server.go +++ b/pkg/server/grpc_server.go @@ -41,20 +41,15 @@ import ( "github.com/osrg/gobgp/pkg/packet/bgp" ) -type Server struct { +type server struct { bgpServer *BgpServer grpcServer *grpc.Server hosts string } -func NewGrpcServer(b *BgpServer, hosts string) *Server { - size := 256 << 20 - return NewServer(b, grpc.NewServer(grpc.MaxRecvMsgSize(size), grpc.MaxSendMsgSize(size)), hosts) -} - -func NewServer(b *BgpServer, g *grpc.Server, hosts string) *Server { +func newAPIserver(b *BgpServer, g *grpc.Server, hosts string) *server { grpc.EnableTracing = false - s := &Server{ + s := &server{ bgpServer: b, grpcServer: g, hosts: hosts, @@ -63,7 +58,7 @@ func NewServer(b *BgpServer, g *grpc.Server, hosts string) *Server { return s } -func (s *Server) Serve() error { +func (s *server) serve() error { var wg sync.WaitGroup l := strings.Split(s.hosts, ",") wg.Add(len(l)) @@ -94,7 +89,7 @@ func (s *Server) Serve() error { return nil } -func (s *Server) ListPeer(r *api.ListPeerRequest, stream api.GobgpApi_ListPeerServer) error { +func (s *server) ListPeer(r *api.ListPeerRequest, stream api.GobgpApi_ListPeerServer) error { l, err := s.bgpServer.ListPeer(context.Background(), r) for _, e := range l { if err := stream.Send(&api.ListPeerResponse{Peer: e}); err != nil { @@ -160,7 +155,7 @@ func getValidation(v []*table.Validation, i int) *table.Validation { } } -func (s *Server) ListPath(r *api.ListPathRequest, stream api.GobgpApi_ListPathServer) error { +func (s *server) ListPath(r *api.ListPathRequest, stream api.GobgpApi_ListPathServer) error { dsts, err := s.bgpServer.ListPath(context.Background(), r) for _, d := range dsts { if err := stream.Send(&api.ListPathResponse{Destination: d}); err != nil { @@ -170,7 +165,7 @@ func (s *Server) ListPath(r *api.ListPathRequest, stream api.GobgpApi_ListPathSe return err } -func (s *Server) MonitorTable(arg *api.MonitorTableRequest, stream api.GobgpApi_MonitorTableServer) error { +func (s *server) MonitorTable(arg *api.MonitorTableRequest, stream api.GobgpApi_MonitorTableServer) error { tm, err := s.bgpServer.NewTableMonitor(arg) if err != nil { return err @@ -189,7 +184,7 @@ func (s *Server) MonitorTable(arg *api.MonitorTableRequest, stream api.GobgpApi_ }() } -func (s *Server) MonitorPeer(arg *api.MonitorPeerRequest, stream api.GobgpApi_MonitorPeerServer) error { +func (s *server) MonitorPeer(arg *api.MonitorPeerRequest, stream api.GobgpApi_MonitorPeerServer) error { pm, err := s.bgpServer.NewPeerMonitor(arg) if err != nil { return err @@ -208,23 +203,23 @@ func (s *Server) MonitorPeer(arg *api.MonitorPeerRequest, stream api.GobgpApi_Mo }() } -func (s *Server) ResetPeer(ctx context.Context, r *api.ResetPeerRequest) (*empty.Empty, error) { +func (s *server) ResetPeer(ctx context.Context, r *api.ResetPeerRequest) (*empty.Empty, error) { return &empty.Empty{}, s.bgpServer.ResetPeer(ctx, r) } -func (s *Server) ShutdownPeer(ctx context.Context, r *api.ShutdownPeerRequest) (*empty.Empty, error) { +func (s *server) ShutdownPeer(ctx context.Context, r *api.ShutdownPeerRequest) (*empty.Empty, error) { return &empty.Empty{}, s.bgpServer.ShutdownPeer(ctx, r) } -func (s *Server) EnablePeer(ctx context.Context, r *api.EnablePeerRequest) (*empty.Empty, error) { - return &empty.Empty{}, s.bgpServer.EnableNeighbor(ctx, r) +func (s *server) EnablePeer(ctx context.Context, r *api.EnablePeerRequest) (*empty.Empty, error) { + return &empty.Empty{}, s.bgpServer.EnablePeer(ctx, r) } -func (s *Server) DisablePeer(ctx context.Context, r *api.DisablePeerRequest) (*empty.Empty, error) { - return &empty.Empty{}, s.bgpServer.DisableNeighbor(ctx, r) +func (s *server) DisablePeer(ctx context.Context, r *api.DisablePeerRequest) (*empty.Empty, error) { + return &empty.Empty{}, s.bgpServer.DisablePeer(ctx, r) } -func (s *Server) SetPolicies(ctx context.Context, r *api.SetPoliciesRequest) (*empty.Empty, error) { +func (s *server) SetPolicies(ctx context.Context, r *api.SetPoliciesRequest) (*empty.Empty, error) { return &empty.Empty{}, s.bgpServer.SetPolicies(ctx, r) } @@ -326,23 +321,23 @@ func api2PathList(resource api.Resource, ApiPathList []*api.Path) ([]*table.Path return pathList, nil } -func (s *Server) AddPath(ctx context.Context, r *api.AddPathRequest) (*api.AddPathResponse, error) { +func (s *server) AddPath(ctx context.Context, r *api.AddPathRequest) (*api.AddPathResponse, error) { return s.bgpServer.AddPath(ctx, r) } -func (s *Server) DeletePath(ctx context.Context, r *api.DeletePathRequest) (*empty.Empty, error) { +func (s *server) DeletePath(ctx context.Context, r *api.DeletePathRequest) (*empty.Empty, error) { return &empty.Empty{}, s.bgpServer.DeletePath(ctx, r) } -func (s *Server) EnableMrt(ctx context.Context, r *api.EnableMrtRequest) (*empty.Empty, error) { +func (s *server) EnableMrt(ctx context.Context, r *api.EnableMrtRequest) (*empty.Empty, error) { return &empty.Empty{}, s.bgpServer.EnableMrt(ctx, r) } -func (s *Server) DisableMrt(ctx context.Context, r *api.DisableMrtRequest) (*empty.Empty, error) { +func (s *server) DisableMrt(ctx context.Context, r *api.DisableMrtRequest) (*empty.Empty, error) { return &empty.Empty{}, s.bgpServer.DisableMrt(ctx, r) } -func (s *Server) AddPathStream(stream api.GobgpApi_AddPathStreamServer) error { +func (s *server) AddPathStream(stream api.GobgpApi_AddPathStreamServer) error { for { arg, err := stream.Recv() if err == io.EOF { @@ -366,35 +361,35 @@ func (s *Server) AddPathStream(stream api.GobgpApi_AddPathStreamServer) error { return stream.SendAndClose(&empty.Empty{}) } -func (s *Server) AddBmp(ctx context.Context, r *api.AddBmpRequest) (*empty.Empty, error) { +func (s *server) AddBmp(ctx context.Context, r *api.AddBmpRequest) (*empty.Empty, error) { return &empty.Empty{}, s.bgpServer.AddBmp(ctx, r) } -func (s *Server) DeleteBmp(ctx context.Context, r *api.DeleteBmpRequest) (*empty.Empty, error) { +func (s *server) DeleteBmp(ctx context.Context, r *api.DeleteBmpRequest) (*empty.Empty, error) { return &empty.Empty{}, s.bgpServer.DeleteBmp(ctx, r) } -func (s *Server) AddRpki(ctx context.Context, r *api.AddRpkiRequest) (*empty.Empty, error) { +func (s *server) AddRpki(ctx context.Context, r *api.AddRpkiRequest) (*empty.Empty, error) { return &empty.Empty{}, s.bgpServer.AddRpki(ctx, r) } -func (s *Server) DeleteRpki(ctx context.Context, r *api.DeleteRpkiRequest) (*empty.Empty, error) { +func (s *server) DeleteRpki(ctx context.Context, r *api.DeleteRpkiRequest) (*empty.Empty, error) { return &empty.Empty{}, s.bgpServer.DeleteRpki(ctx, r) } -func (s *Server) EnableRpki(ctx context.Context, r *api.EnableRpkiRequest) (*empty.Empty, error) { +func (s *server) EnableRpki(ctx context.Context, r *api.EnableRpkiRequest) (*empty.Empty, error) { return &empty.Empty{}, s.bgpServer.EnableRpki(ctx, r) } -func (s *Server) DisableRpki(ctx context.Context, r *api.DisableRpkiRequest) (*empty.Empty, error) { +func (s *server) DisableRpki(ctx context.Context, r *api.DisableRpkiRequest) (*empty.Empty, error) { return &empty.Empty{}, s.bgpServer.DisableRpki(ctx, r) } -func (s *Server) ResetRpki(ctx context.Context, r *api.ResetRpkiRequest) (*empty.Empty, error) { +func (s *server) ResetRpki(ctx context.Context, r *api.ResetRpkiRequest) (*empty.Empty, error) { return &empty.Empty{}, s.bgpServer.ResetRpki(ctx, r) } -func (s *Server) ListRpki(r *api.ListRpkiRequest, stream api.GobgpApi_ListRpkiServer) error { +func (s *server) ListRpki(r *api.ListRpkiRequest, stream api.GobgpApi_ListRpkiServer) error { servers, err := s.bgpServer.ListRpki(context.Background(), r) if err != nil { return err @@ -407,7 +402,7 @@ func (s *Server) ListRpki(r *api.ListRpkiRequest, stream api.GobgpApi_ListRpkiSe return nil } -func (s *Server) ListRpkiTable(r *api.ListRpkiTableRequest, stream api.GobgpApi_ListRpkiTableServer) error { +func (s *server) ListRpkiTable(r *api.ListRpkiTableRequest, stream api.GobgpApi_ListRpkiTableServer) error { roas, err := s.bgpServer.ListRpkiTable(context.Background(), r) if err != nil { return err @@ -420,11 +415,11 @@ func (s *Server) ListRpkiTable(r *api.ListRpkiTableRequest, stream api.GobgpApi_ return nil } -func (s *Server) EnableZebra(ctx context.Context, r *api.EnableZebraRequest) (*empty.Empty, error) { +func (s *server) EnableZebra(ctx context.Context, r *api.EnableZebraRequest) (*empty.Empty, error) { return &empty.Empty{}, s.bgpServer.EnableZebra(ctx, r) } -func (s *Server) ListVrf(r *api.ListVrfRequest, stream api.GobgpApi_ListVrfServer) error { +func (s *server) ListVrf(r *api.ListVrfRequest, stream api.GobgpApi_ListVrfServer) error { for _, v := range s.bgpServer.ListVrf(context.Background(), r) { if err := stream.Send(&api.ListVrfResponse{Vrf: v}); err != nil { return err @@ -433,11 +428,11 @@ func (s *Server) ListVrf(r *api.ListVrfRequest, stream api.GobgpApi_ListVrfServe return nil } -func (s *Server) AddVrf(ctx context.Context, r *api.AddVrfRequest) (*empty.Empty, error) { +func (s *server) AddVrf(ctx context.Context, r *api.AddVrfRequest) (*empty.Empty, error) { return &empty.Empty{}, s.bgpServer.AddVrf(ctx, r) } -func (s *Server) DeleteVrf(ctx context.Context, r *api.DeleteVrfRequest) (*empty.Empty, error) { +func (s *server) DeleteVrf(ctx context.Context, r *api.DeleteVrfRequest) (*empty.Empty, error) { return &empty.Empty{}, s.bgpServer.DeleteVrf(ctx, r) } @@ -776,51 +771,31 @@ func newPeerGroupFromAPIStruct(a *api.PeerGroup) (*config.PeerGroup, error) { return pconf, nil } -func (s *Server) AddPeer(ctx context.Context, r *api.AddPeerRequest) (*empty.Empty, error) { +func (s *server) AddPeer(ctx context.Context, r *api.AddPeerRequest) (*empty.Empty, error) { return &empty.Empty{}, s.bgpServer.AddPeer(ctx, r) } -func (s *Server) DeletePeer(ctx context.Context, r *api.DeletePeerRequest) (*empty.Empty, error) { +func (s *server) DeletePeer(ctx context.Context, r *api.DeletePeerRequest) (*empty.Empty, error) { return &empty.Empty{}, s.bgpServer.DeletePeer(ctx, r) } -func (s *Server) UpdatePeer(ctx context.Context, r *api.UpdatePeerRequest) (*api.UpdatePeerResponse, error) { - rsp, err := s.bgpServer.UpdateNeighbor(ctx, r) - if err != nil { - return nil, err - } - if r.DoSoftResetIn && rsp.NeedsSoftResetIn { - return &api.UpdatePeerResponse{NeedsSoftResetIn: false}, s.bgpServer.ResetPeer(ctx, &api.ResetPeerRequest{ - Soft: true, - Direction: api.ResetPeerRequest_IN, - }) - } - return rsp, nil +func (s *server) UpdatePeer(ctx context.Context, r *api.UpdatePeerRequest) (*api.UpdatePeerResponse, error) { + return s.bgpServer.UpdatePeer(ctx, r) } -func (s *Server) AddPeerGroup(ctx context.Context, r *api.AddPeerGroupRequest) (*empty.Empty, error) { +func (s *server) AddPeerGroup(ctx context.Context, r *api.AddPeerGroupRequest) (*empty.Empty, error) { return &empty.Empty{}, s.bgpServer.AddPeerGroup(ctx, r) } -func (s *Server) DeletePeerGroup(ctx context.Context, r *api.DeletePeerGroupRequest) (*empty.Empty, error) { +func (s *server) DeletePeerGroup(ctx context.Context, r *api.DeletePeerGroupRequest) (*empty.Empty, error) { return &empty.Empty{}, s.bgpServer.DeletePeerGroup(ctx, r) } -func (s *Server) UpdatePeerGroup(ctx context.Context, r *api.UpdatePeerGroupRequest) (*api.UpdatePeerGroupResponse, error) { - rsp, err := s.bgpServer.UpdatePeerGroup(ctx, r) - if err != nil { - return nil, err - } - if r.DoSoftResetIn && rsp.NeedsSoftResetIn { - return &api.UpdatePeerGroupResponse{NeedsSoftResetIn: false}, s.bgpServer.ResetPeer(ctx, &api.ResetPeerRequest{ - Soft: true, - Direction: api.ResetPeerRequest_IN, - }) - } - return rsp, err +func (s *server) UpdatePeerGroup(ctx context.Context, r *api.UpdatePeerGroupRequest) (*api.UpdatePeerGroupResponse, error) { + return s.bgpServer.UpdatePeerGroup(ctx, r) } -func (s *Server) AddDynamicNeighbor(ctx context.Context, r *api.AddDynamicNeighborRequest) (*empty.Empty, error) { +func (s *server) AddDynamicNeighbor(ctx context.Context, r *api.AddDynamicNeighborRequest) (*empty.Empty, error) { return &empty.Empty{}, s.bgpServer.AddDynamicNeighbor(ctx, r) } @@ -972,7 +947,7 @@ func newDefinedSetFromApiStruct(a *api.DefinedSet) (table.DefinedSet, error) { var _regexpPrefixMaskLengthRange = regexp.MustCompile(`(\d+)\.\.(\d+)`) -func (s *Server) ListDefinedSet(r *api.ListDefinedSetRequest, stream api.GobgpApi_ListDefinedSetServer) error { +func (s *server) ListDefinedSet(r *api.ListDefinedSetRequest, stream api.GobgpApi_ListDefinedSetServer) error { sets, err := s.bgpServer.ListDefinedSet(context.Background(), r) if err != nil { return err @@ -985,11 +960,11 @@ func (s *Server) ListDefinedSet(r *api.ListDefinedSetRequest, stream api.GobgpAp return nil } -func (s *Server) AddDefinedSet(ctx context.Context, r *api.AddDefinedSetRequest) (*empty.Empty, error) { +func (s *server) AddDefinedSet(ctx context.Context, r *api.AddDefinedSetRequest) (*empty.Empty, error) { return &empty.Empty{}, s.bgpServer.AddDefinedSet(ctx, r) } -func (s *Server) DeleteDefinedSet(ctx context.Context, r *api.DeleteDefinedSetRequest) (*empty.Empty, error) { +func (s *server) DeleteDefinedSet(ctx context.Context, r *api.DeleteDefinedSetRequest) (*empty.Empty, error) { return &empty.Empty{}, s.bgpServer.DeleteDefinedSet(ctx, r) } @@ -1535,7 +1510,7 @@ func newStatementFromApiStruct(a *api.Statement) (*table.Statement, error) { }, nil } -func (s *Server) ListStatement(r *api.ListStatementRequest, stream api.GobgpApi_ListStatementServer) error { +func (s *server) ListStatement(r *api.ListStatementRequest, stream api.GobgpApi_ListStatementServer) error { l, err := s.bgpServer.ListStatement(context.Background(), r) if err != nil { for _, st := range l { @@ -1548,11 +1523,11 @@ func (s *Server) ListStatement(r *api.ListStatementRequest, stream api.GobgpApi_ return err } -func (s *Server) AddStatement(ctx context.Context, r *api.AddStatementRequest) (*empty.Empty, error) { +func (s *server) AddStatement(ctx context.Context, r *api.AddStatementRequest) (*empty.Empty, error) { return &empty.Empty{}, s.bgpServer.AddStatement(ctx, r) } -func (s *Server) DeleteStatement(ctx context.Context, r *api.DeleteStatementRequest) (*empty.Empty, error) { +func (s *server) DeleteStatement(ctx context.Context, r *api.DeleteStatementRequest) (*empty.Empty, error) { return &empty.Empty{}, s.bgpServer.DeleteStatement(ctx, r) } @@ -1618,7 +1593,7 @@ func newRoaListFromTableStructList(origin []*table.ROA) []*api.Roa { return l } -func (s *Server) ListPolicy(r *api.ListPolicyRequest, stream api.GobgpApi_ListPolicyServer) error { +func (s *server) ListPolicy(r *api.ListPolicyRequest, stream api.GobgpApi_ListPolicyServer) error { l, err := s.bgpServer.ListPolicy(context.Background(), r) for _, p := range l { if err := stream.Send(&api.ListPolicyResponse{Policy: p}); err != nil { @@ -1628,15 +1603,15 @@ func (s *Server) ListPolicy(r *api.ListPolicyRequest, stream api.GobgpApi_ListPo return err } -func (s *Server) AddPolicy(ctx context.Context, r *api.AddPolicyRequest) (*empty.Empty, error) { +func (s *server) AddPolicy(ctx context.Context, r *api.AddPolicyRequest) (*empty.Empty, error) { return &empty.Empty{}, s.bgpServer.AddPolicy(ctx, r) } -func (s *Server) DeletePolicy(ctx context.Context, r *api.DeletePolicyRequest) (*empty.Empty, error) { +func (s *server) DeletePolicy(ctx context.Context, r *api.DeletePolicyRequest) (*empty.Empty, error) { return &empty.Empty{}, s.bgpServer.DeletePolicy(ctx, r) } -func (s *Server) ListPolicyAssignment(r *api.ListPolicyAssignmentRequest, stream api.GobgpApi_ListPolicyAssignmentServer) error { +func (s *server) ListPolicyAssignment(r *api.ListPolicyAssignmentRequest, stream api.GobgpApi_ListPolicyAssignmentServer) error { l, err := s.bgpServer.ListPolicyAssignment(context.Background(), r) if err == nil { for _, a := range l { @@ -1667,19 +1642,19 @@ func toPolicyDefinition(policies []*api.Policy) []*config.PolicyDefinition { return l } -func (s *Server) AddPolicyAssignment(ctx context.Context, r *api.AddPolicyAssignmentRequest) (*empty.Empty, error) { +func (s *server) AddPolicyAssignment(ctx context.Context, r *api.AddPolicyAssignmentRequest) (*empty.Empty, error) { return &empty.Empty{}, s.bgpServer.AddPolicyAssignment(ctx, r) } -func (s *Server) DeletePolicyAssignment(ctx context.Context, r *api.DeletePolicyAssignmentRequest) (*empty.Empty, error) { +func (s *server) DeletePolicyAssignment(ctx context.Context, r *api.DeletePolicyAssignmentRequest) (*empty.Empty, error) { return &empty.Empty{}, s.bgpServer.DeletePolicyAssignment(ctx, r) } -func (s *Server) SetPolicyAssignment(ctx context.Context, r *api.SetPolicyAssignmentRequest) (*empty.Empty, error) { +func (s *server) SetPolicyAssignment(ctx context.Context, r *api.SetPolicyAssignmentRequest) (*empty.Empty, error) { return &empty.Empty{}, s.bgpServer.SetPolicyAssignment(ctx, r) } -func (s *Server) GetBgp(ctx context.Context, r *api.GetBgpRequest) (*api.GetBgpResponse, error) { +func (s *server) GetBgp(ctx context.Context, r *api.GetBgpRequest) (*api.GetBgpResponse, error) { return s.bgpServer.GetBgp(ctx, r) } @@ -1765,14 +1740,14 @@ func newGlobalFromAPIStruct(a *api.Global) *config.Global { return global } -func (s *Server) StartBgp(ctx context.Context, r *api.StartBgpRequest) (*empty.Empty, error) { +func (s *server) StartBgp(ctx context.Context, r *api.StartBgpRequest) (*empty.Empty, error) { return &empty.Empty{}, s.bgpServer.StartBgp(ctx, r) } -func (s *Server) StopBgp(ctx context.Context, r *api.StopBgpRequest) (*empty.Empty, error) { +func (s *server) StopBgp(ctx context.Context, r *api.StopBgpRequest) (*empty.Empty, error) { return &empty.Empty{}, s.bgpServer.StopBgp(ctx, r) } -func (s *Server) GetTable(ctx context.Context, r *api.GetTableRequest) (*api.GetTableResponse, error) { +func (s *server) GetTable(ctx context.Context, r *api.GetTableRequest) (*api.GetTableResponse, error) { return s.bgpServer.GetTable(ctx, r) } diff --git a/pkg/server/server.go b/pkg/server/server.go index 49065400..ed78d292 100644 --- a/pkg/server/server.go +++ b/pkg/server/server.go @@ -27,6 +27,7 @@ import ( "github.com/eapache/channels" uuid "github.com/satori/go.uuid" log "github.com/sirupsen/logrus" + "google.golang.org/grpc" api "github.com/osrg/gobgp/api" "github.com/osrg/gobgp/internal/pkg/apiutil" @@ -96,6 +97,25 @@ func newTCPListener(address string, port uint32, ch chan *net.TCPConn) (*tcpList }, nil } +type options struct { + grpcAddress string + grpcOption []grpc.ServerOption +} + +type ServerOption func(*options) + +func GrpcListenAddress(addr string) ServerOption { + return func(o *options) { + o.grpcAddress = addr + } +} + +func GrpcOption(opt []grpc.ServerOption) ServerOption { + return func(o *options) { + o.grpcOption = opt + } +} + type BgpServer struct { bgpConfig config.Bgp fsmincomingCh *channels.InfiniteChannel @@ -118,7 +138,12 @@ type BgpServer struct { uuidMap map[uuid.UUID]string } -func NewBgpServer() *BgpServer { +func NewBgpServer(opt ...ServerOption) *BgpServer { + opts := options{} + for _, o := range opt { + o(&opts) + } + roaManager, _ := newROAManager(0) s := &BgpServer{ neighborMap: make(map[string]*peer), @@ -131,6 +156,16 @@ func NewBgpServer() *BgpServer { } s.bmpManager = newBmpClientManager(s) s.mrtManager = newMrtManager(s) + if len(opts.grpcAddress) != 0 { + grpc.EnableTracing = false + api := newAPIserver(s, grpc.NewServer(opts.grpcOption...), opts.grpcAddress) + go func() { + if err := api.serve(); err != nil { + log.Fatalf("failed to listen grpc port: %s", err) + } + }() + + } return s } @@ -2815,7 +2850,7 @@ func (s *BgpServer) updateNeighbor(c *config.Neighbor) (needsSoftResetIn bool, e return needsSoftResetIn, err } -func (s *BgpServer) UpdateNeighbor(ctx context.Context, r *api.UpdatePeerRequest) (rsp *api.UpdatePeerResponse, err error) { +func (s *BgpServer) UpdatePeer(ctx context.Context, r *api.UpdatePeerRequest) (rsp *api.UpdatePeerResponse, err error) { doSoftReset := false err = s.mgmtOperation(func() error { c, err := newNeighborFromAPIStruct(r.Peer) @@ -2931,13 +2966,13 @@ func (s *BgpServer) setAdminState(addr, communication string, enable bool) error return nil } -func (s *BgpServer) EnableNeighbor(ctx context.Context, r *api.EnablePeerRequest) error { +func (s *BgpServer) EnablePeer(ctx context.Context, r *api.EnablePeerRequest) error { return s.mgmtOperation(func() error { return s.setAdminState(r.Address, "", true) }, true) } -func (s *BgpServer) DisableNeighbor(ctx context.Context, r *api.DisablePeerRequest) error { +func (s *BgpServer) DisablePeer(ctx context.Context, r *api.DisablePeerRequest) error { return s.mgmtOperation(func() error { return s.setAdminState(r.Address, r.Communication, false) }, true) |