diff options
author | FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp> | 2016-07-26 12:04:04 +0900 |
---|---|---|
committer | FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp> | 2016-07-26 12:04:04 +0900 |
commit | e0e421bbca8f620fc40c98de7d8ae386a8f8a6ad (patch) | |
tree | 6aca5e2e5cea4c7c60bc5ed451f90544f83e2ec8 | |
parent | f623f8d538cd0ce9b31a97248ac862bb1c8b8b7c (diff) |
move gRPC-related code for REQ_START_SERVER to grpc_server.go
Signed-off-by: FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp>
-rw-r--r-- | gobgpd/main.go | 2 | ||||
-rw-r--r-- | server/grpc_server.go | 37 | ||||
-rw-r--r-- | server/server.go | 101 | ||||
-rw-r--r-- | server/server_test.go | 2 |
4 files changed, 66 insertions, 76 deletions
diff --git a/gobgpd/main.go b/gobgpd/main.go index c8f32926..aaf038a2 100644 --- a/gobgpd/main.go +++ b/gobgpd/main.go @@ -206,7 +206,7 @@ func main() { if c == nil { c = newConfig - if err := bgpServer.SetGlobalType(newConfig.Global); err != nil { + if err := bgpServer.Start(&newConfig.Global); err != nil { log.Fatalf("failed to set global config: %s", err) } if newConfig.Zebra.Config.Enabled { diff --git a/server/grpc_server.go b/server/grpc_server.go index 8ad5c3c8..c6486eac 100644 --- a/server/grpc_server.go +++ b/server/grpc_server.go @@ -1732,11 +1732,42 @@ func (s *Server) GetServer(ctx context.Context, arg *api.GetServerRequest) (*api } func (s *Server) StartServer(ctx context.Context, arg *api.StartServerRequest) (*api.StartServerResponse, error) { - d, err := s.get(REQ_START_SERVER, arg) - if err != nil { + g := arg.Global + if net.ParseIP(g.RouterId) == nil { + return nil, fmt.Errorf("invalid router-id format: %s", g.RouterId) + } + families := make([]config.AfiSafi, 0, len(g.Families)) + for _, f := range g.Families { + name := config.AfiSafiType(bgp.RouteFamily(f).String()) + families = append(families, config.AfiSafi{ + Config: config.AfiSafiConfig{ + AfiSafiName: name, + Enabled: true, + }, + State: config.AfiSafiState{ + AfiSafiName: name, + }, + }) + } + b := &config.BgpConfigSet{ + Global: config.Global{ + Config: config.GlobalConfig{ + As: g.As, + RouterId: g.RouterId, + Port: g.ListenPort, + LocalAddressList: g.ListenAddresses, + }, + MplsLabelRange: config.MplsLabelRange{ + MinLabel: g.MplsLabelMin, + MaxLabel: g.MplsLabelMax, + }, + AfiSafis: families, + }, + } + if err := config.SetDefaultConfigValues(nil, b); err != nil { return nil, err } - return d.(*api.StartServerResponse), err + return &api.StartServerResponse{}, s.bgpServer.Start(&b.Global) } func (s *Server) StopServer(ctx context.Context, arg *api.StopServerRequest) (*api.StopServerResponse, error) { diff --git a/server/server.go b/server/server.go index 6b15527a..75031f30 100644 --- a/server/server.go +++ b/server/server.go @@ -1275,77 +1275,43 @@ END: return msgs } -func (server *BgpServer) handleModConfig(grpcReq *GrpcRequest) error { - var c *config.Global - switch arg := grpcReq.Data.(type) { - case *api.StartServerRequest: - g := arg.Global - if net.ParseIP(g.RouterId) == nil { - return fmt.Errorf("invalid router-id format: %s", g.RouterId) - } - families := make([]config.AfiSafi, 0, len(g.Families)) - for _, f := range g.Families { - name := config.AfiSafiType(bgp.RouteFamily(f).String()) - families = append(families, config.AfiSafi{ - Config: config.AfiSafiConfig{ - AfiSafiName: name, - Enabled: true, - }, - State: config.AfiSafiState{ - AfiSafiName: name, - }, - }) - } - b := &config.BgpConfigSet{ - Global: config.Global{ - Config: config.GlobalConfig{ - As: g.As, - RouterId: g.RouterId, - Port: g.ListenPort, - LocalAddressList: g.ListenAddresses, - }, - MplsLabelRange: config.MplsLabelRange{ - MinLabel: g.MplsLabelMin, - MaxLabel: g.MplsLabelMax, - }, - AfiSafis: families, - }, - } - if err := config.SetDefaultConfigValues(nil, b); err != nil { - return err - } - c = &b.Global - case *config.Global: - c = arg - } +func (s *BgpServer) Start(c *config.Global) (err error) { + ch := make(chan struct{}) + defer func() { <-ch }() - if server.bgpConfig.Global.Config.As != 0 { - return fmt.Errorf("gobgp is already started") - } + s.mgmtCh <- func() { + defer close(ch) - if c.Config.Port > 0 { - acceptCh := make(chan *net.TCPConn, 4096) - for _, addr := range c.Config.LocalAddressList { - l, err := NewTCPListener(addr, uint32(c.Config.Port), acceptCh) - if err != nil { - return err + if s.bgpConfig.Global.Config.As != 0 { + err = fmt.Errorf("gobgp is already started") + return + } + + if c.Config.Port > 0 { + acceptCh := make(chan *net.TCPConn, 4096) + for _, addr := range c.Config.LocalAddressList { + var l *TCPListener + l, err = NewTCPListener(addr, uint32(c.Config.Port), acceptCh) + if err != nil { + return + } + s.listeners = append(s.listeners, l) } - server.listeners = append(server.listeners, l) + s.acceptCh = acceptCh } - server.acceptCh = acceptCh - } - rfs, _ := config.AfiSafis(c.AfiSafis).ToRfList() - server.globalRib = table.NewTableManager(rfs, c.MplsLabelRange.MinLabel, c.MplsLabelRange.MaxLabel) + rfs, _ := config.AfiSafis(c.AfiSafis).ToRfList() + s.globalRib = table.NewTableManager(rfs, c.MplsLabelRange.MinLabel, c.MplsLabelRange.MaxLabel) - p := config.RoutingPolicy{} - if err := server.handlePolicy(p); err != nil { - return err + p := config.RoutingPolicy{} + if err = s.handlePolicy(p); err != nil { + return + } + s.bgpConfig.Global = *c + // update route selection options + table.SelectionOptions = c.RouteSelectionOptions.Config + table.UseMultiplePaths = c.UseMultiplePaths.Config } - server.bgpConfig.Global = *c - // update route selection options - table.SelectionOptions = c.RouteSelectionOptions.Config - table.UseMultiplePaths = c.UseMultiplePaths.Config return nil } @@ -1407,13 +1373,6 @@ func (server *BgpServer) handleGrpc(grpcReq *GrpcRequest) { Data: &g, } close(grpcReq.ResponseCh) - case REQ_START_SERVER: - err := server.handleModConfig(grpcReq) - grpcReq.ResponseCh <- &GrpcResponse{ - ResponseErr: err, - Data: &api.StartServerResponse{}, - } - close(grpcReq.ResponseCh) case REQ_GLOBAL_RIB, REQ_LOCAL_RIB: arg := grpcReq.Data.(*api.GetRibRequest) rib := server.globalRib diff --git a/server/server_test.go b/server/server_test.go index 9a55f775..57fcfaf1 100644 --- a/server/server_test.go +++ b/server/server_test.go @@ -26,7 +26,7 @@ func TestModPolicyAssign(t *testing.T) { assert := assert.New(t) s := NewBgpServer() go s.Serve() - s.SetGlobalType(config.Global{ + s.Start(&config.Global{ Config: config.GlobalConfig{ As: 1, RouterId: "1.1.1.1", |