diff options
author | FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp> | 2016-07-20 02:52:59 +0900 |
---|---|---|
committer | FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp> | 2016-07-20 02:52:59 +0900 |
commit | b6e606a99480cffd47e73e7b74aa3a10df5ad47d (patch) | |
tree | ad34613d89ea3316271ff6c2f82caff804ff9c81 | |
parent | 49e45d4e0c0a81dc8f9ec4b3a39433b22af04f16 (diff) |
remove gRPC dependency from peer.go
move gRPC dependency from peer.go to grpc_server.go
Preparation for the removal of gRPC dependency from packages except
for api package.
Signed-off-by: FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp>
-rw-r--r-- | config/bgp_configs.go | 87 | ||||
-rw-r--r-- | server/grpc_server.go | 92 | ||||
-rw-r--r-- | server/peer.go | 138 | ||||
-rw-r--r-- | server/server.go | 8 | ||||
-rw-r--r-- | tools/pyang_plugins/bgpyang2golang.py | 20 | ||||
-rw-r--r-- | tools/pyang_plugins/gobgp.yang | 42 |
6 files changed, 256 insertions, 131 deletions
diff --git a/config/bgp_configs.go b/config/bgp_configs.go index db9d2054..36a51da5 100644 --- a/config/bgp_configs.go +++ b/config/bgp_configs.go @@ -1,7 +1,7 @@ // DO NOT EDIT // generated by pyang using OpenConfig https://github.com/openconfig/public // -// Copyright (C) 2014,2015 Nippon Telegraph and Telephone Corporation. +// Copyright (C) 2014-2016 Nippon Telegraph and Telephone Corporation. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -18,7 +18,10 @@ package config -import "fmt" +import ( + "bytes" + "fmt" +) func mapkey(index int, name string) string { if name != "" { @@ -2190,6 +2193,65 @@ func (lhs *Timers) Equal(rhs *Timers) bool { return true } +//struct for container gobgp:Capabilities +type Capabilities struct { + // original -> gobgp:remote + // original type is list of binary + RemoteList [][]byte `mapstructure:"remote-list"` + // original -> gobgp:local + // original type is list of binary + LocalList [][]byte `mapstructure:"local-list"` +} + +func (lhs *Capabilities) Equal(rhs *Capabilities) bool { + if lhs == nil || rhs == nil { + return false + } + if len(lhs.RemoteList) != len(rhs.RemoteList) { + return false + } + for idx, l := range lhs.RemoteList { + if bytes.Compare(l, rhs.RemoteList[idx]) != 0 { + return false + } + } + if len(lhs.LocalList) != len(rhs.LocalList) { + return false + } + for idx, l := range lhs.LocalList { + if bytes.Compare(l, rhs.LocalList[idx]) != 0 { + return false + } + } + return true +} + +//struct for container gobgp:adj-table +type AdjTable struct { + // original -> gobgp:ADVERTISED + Advertised uint32 `mapstructure:"advertised"` + // original -> gobgp:RECEIVED + Received uint32 `mapstructure:"received"` + // original -> gobgp:ACCEPTED + Accepted uint32 `mapstructure:"accepted"` +} + +func (lhs *AdjTable) Equal(rhs *AdjTable) bool { + if lhs == nil || rhs == nil { + return false + } + if lhs.Advertised != rhs.Advertised { + return false + } + if lhs.Received != rhs.Received { + return false + } + if lhs.Accepted != rhs.Accepted { + return false + } + return true +} + //struct for container bgp:queues type Queues struct { // original -> bgp-op:input @@ -2368,9 +2430,18 @@ type NeighborState struct { Messages Messages `mapstructure:"messages"` // original -> bgp:queues Queues Queues `mapstructure:"queues"` + // original -> gobgp:adj-table + AdjTable AdjTable `mapstructure:"adj-table"` + // original -> gobgp:Capabilities + Capabilities Capabilities `mapstructure:"capabilities"` + // original -> gobgp:received-open-message + //gobgp:received-open-message's original type is binary + ReceivedOpenMessage []byte `mapstructure:"received-open-message"` // original -> gobgp:admin-down //gobgp:admin-down's original type is boolean AdminDown bool `mapstructure:"admin-down"` + // original -> gobgp:admin-state + AdminState string `mapstructure:"admin-state"` // original -> gobgp:established-count EstablishedCount uint32 `mapstructure:"established-count"` // original -> gobgp:flops @@ -2428,9 +2499,21 @@ func (lhs *NeighborState) Equal(rhs *NeighborState) bool { if !lhs.Queues.Equal(&(rhs.Queues)) { return false } + if !lhs.AdjTable.Equal(&(rhs.AdjTable)) { + return false + } + if !lhs.Capabilities.Equal(&(rhs.Capabilities)) { + return false + } + if bytes.Compare(lhs.ReceivedOpenMessage, rhs.ReceivedOpenMessage) != 0 { + return false + } if lhs.AdminDown != rhs.AdminDown { return false } + if lhs.AdminState != rhs.AdminState { + return false + } if lhs.EstablishedCount != rhs.EstablishedCount { return false } diff --git a/server/grpc_server.go b/server/grpc_server.go index 5963b780..1ad4735a 100644 --- a/server/grpc_server.go +++ b/server/grpc_server.go @@ -1,4 +1,4 @@ -// Copyright (C) 2014,2015 Nippon Telegraph and Telephone Corporation. +// Copyright (C) 2014-2016 Nippon Telegraph and Telephone Corporation. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -19,6 +19,7 @@ import ( "fmt" log "github.com/Sirupsen/logrus" api "github.com/osrg/gobgp/api" + "github.com/osrg/gobgp/config" "github.com/osrg/gobgp/packet/bgp" "golang.org/x/net/context" "google.golang.org/grpc" @@ -147,7 +148,94 @@ func (s *Server) GetNeighbor(ctx context.Context, arg *api.GetNeighborRequest) ( if res.Err() != nil { return nil, res.Err() } - return res.Data.(*api.GetNeighborResponse), nil + + toApi := func(pconf *config.Neighbor) *api.Peer { + prefixLimits := make([]*api.PrefixLimit, 0, len(pconf.AfiSafis)) + for _, family := range pconf.AfiSafis { + if c := family.PrefixLimit.Config; c.MaxPrefixes > 0 { + k, _ := bgp.GetRouteFamily(string(family.Config.AfiSafiName)) + prefixLimits = append(prefixLimits, &api.PrefixLimit{ + Family: uint32(k), + MaxPrefixes: c.MaxPrefixes, + ShutdownThresholdPct: uint32(c.ShutdownThresholdPct), + }) + } + } + + timer := pconf.Timers + s := pconf.State + return &api.Peer{ + Conf: &api.PeerConf{ + NeighborAddress: pconf.Config.NeighborAddress, + Id: s.Description, + PeerAs: pconf.Config.PeerAs, + LocalAs: pconf.Config.LocalAs, + PeerType: uint32(pconf.Config.PeerType.ToInt()), + AuthPassword: pconf.Config.AuthPassword, + RemovePrivateAs: uint32(pconf.Config.RemovePrivateAs.ToInt()), + RouteFlapDamping: pconf.Config.RouteFlapDamping, + SendCommunity: uint32(pconf.Config.SendCommunity.ToInt()), + Description: pconf.Config.Description, + PeerGroup: pconf.Config.PeerGroup, + RemoteCap: s.Capabilities.RemoteList, + LocalCap: s.Capabilities.LocalList, + PrefixLimits: prefixLimits, + }, + Info: &api.PeerState{ + BgpState: bgp.FSMState(s.SessionState.ToInt()).String(), + AdminState: s.AdminState, + Messages: &api.Messages{ + Received: &api.Message{ + NOTIFICATION: s.Messages.Received.Notification, + UPDATE: s.Messages.Received.Update, + OPEN: s.Messages.Received.Open, + KEEPALIVE: s.Messages.Received.Keepalive, + REFRESH: s.Messages.Received.Refresh, + DISCARDED: s.Messages.Received.Discarded, + TOTAL: s.Messages.Received.Total, + }, + Sent: &api.Message{ + NOTIFICATION: s.Messages.Sent.Notification, + UPDATE: s.Messages.Sent.Update, + OPEN: s.Messages.Sent.Open, + KEEPALIVE: s.Messages.Sent.Keepalive, + REFRESH: s.Messages.Sent.Refresh, + DISCARDED: s.Messages.Sent.Discarded, + TOTAL: s.Messages.Sent.Total, + }, + }, + Received: s.AdjTable.Received, + Accepted: s.AdjTable.Accepted, + Advertised: s.AdjTable.Advertised, + }, + Timers: &api.Timers{ + Config: &api.TimersConfig{ + ConnectRetry: uint64(timer.Config.ConnectRetry), + HoldTime: uint64(timer.Config.HoldTime), + KeepaliveInterval: uint64(timer.Config.KeepaliveInterval), + }, + State: &api.TimersState{ + KeepaliveInterval: uint64(timer.State.KeepaliveInterval), + NegotiatedHoldTime: uint64(timer.State.NegotiatedHoldTime), + Uptime: uint64(timer.State.Uptime), + Downtime: uint64(timer.State.Downtime), + }, + }, + RouteReflector: &api.RouteReflector{ + RouteReflectorClient: pconf.RouteReflector.Config.RouteReflectorClient, + RouteReflectorClusterId: string(pconf.RouteReflector.Config.RouteReflectorClusterId), + }, + RouteServer: &api.RouteServer{ + RouteServerClient: pconf.RouteServer.Config.RouteServerClient, + }, + } + } + + p := []*api.Peer{} + for _, e := range res.Data.([]*config.Neighbor) { + p = append(p, toApi(e)) + } + return &api.GetNeighborResponse{Peers: p}, nil } func handleMultipleResponses(req *GrpcRequest, f func(*GrpcResponse) error) error { diff --git a/server/peer.go b/server/peer.go index 1058f2e9..030044d0 100644 --- a/server/peer.go +++ b/server/peer.go @@ -1,4 +1,4 @@ -// Copyright (C) 2014 Nippon Telegraph and Telephone Corporation. +// Copyright (C) 2014-2016 Nippon Telegraph and Telephone Corporation. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -16,11 +16,9 @@ package server import ( - "encoding/json" "fmt" log "github.com/Sirupsen/logrus" "github.com/eapache/channels" - api "github.com/osrg/gobgp/api" "github.com/osrg/gobgp/config" "github.com/osrg/gobgp/packet/bgp" "github.com/osrg/gobgp/table" @@ -384,14 +382,14 @@ func (peer *Peer) PassConn(conn *net.TCPConn) { } } -func (peer *Peer) MarshalJSON() ([]byte, error) { - return json.Marshal(peer.ToApiStruct()) -} - -func (peer *Peer) ToApiStruct() *api.Peer { +func (peer *Peer) ToConfig() *config.Neighbor { + // create copy which can be access to without mutex + conf := *peer.fsm.pConf - f := peer.fsm - c := f.pConf + conf.AfiSafis = make([]config.AfiSafi, len(peer.fsm.pConf.AfiSafis)) + for i := 0; i < len(peer.fsm.pConf.AfiSafis); i++ { + conf.AfiSafis[i] = peer.fsm.pConf.AfiSafis[i] + } remoteCap := make([][]byte, 0, len(peer.fsm.capMap)) for _, c := range peer.fsm.capMap { @@ -400,6 +398,7 @@ func (peer *Peer) ToApiStruct() *api.Peer { remoteCap = append(remoteCap, buf) } } + conf.State.Capabilities.RemoteList = remoteCap caps := capabilitiesFromConfig(peer.fsm.pConf) localCap := make([][]byte, 0, len(caps)) @@ -407,120 +406,25 @@ func (peer *Peer) ToApiStruct() *api.Peer { buf, _ := c.Serialize() localCap = append(localCap, buf) } + conf.State.Capabilities.LocalList = localCap - prefixLimits := make([]*api.PrefixLimit, 0, len(peer.fsm.pConf.AfiSafis)) - for _, family := range peer.fsm.pConf.AfiSafis { - if c := family.PrefixLimit.Config; c.MaxPrefixes > 0 { - k, _ := bgp.GetRouteFamily(string(family.Config.AfiSafiName)) - prefixLimits = append(prefixLimits, &api.PrefixLimit{ - Family: uint32(k), - MaxPrefixes: c.MaxPrefixes, - ShutdownThresholdPct: uint32(c.ShutdownThresholdPct), - }) - } - } - - conf := &api.PeerConf{ - NeighborAddress: c.Config.NeighborAddress, - Id: peer.fsm.peerInfo.ID.To4().String(), - PeerAs: c.Config.PeerAs, - LocalAs: c.Config.LocalAs, - PeerType: uint32(c.Config.PeerType.ToInt()), - AuthPassword: c.Config.AuthPassword, - RemovePrivateAs: uint32(c.Config.RemovePrivateAs.ToInt()), - RouteFlapDamping: c.Config.RouteFlapDamping, - SendCommunity: uint32(c.Config.SendCommunity.ToInt()), - Description: c.Config.Description, - PeerGroup: c.Config.PeerGroup, - RemoteCap: remoteCap, - LocalCap: localCap, - PrefixLimits: prefixLimits, - } - - timer := c.Timers - s := c.State + conf.State.Description = peer.fsm.peerInfo.ID.To4().String() + conf.State.SessionState = config.IntToSessionStateMap[int(peer.fsm.state)] + conf.State.AdminState = peer.fsm.adminState.String() - advertised := uint32(0) - received := uint32(0) - accepted := uint32(0) - if f.state == bgp.BGP_FSM_ESTABLISHED { + if peer.fsm.state == bgp.BGP_FSM_ESTABLISHED { rfList := peer.configuredRFlist() - advertised = uint32(peer.adjRibOut.Count(rfList)) - received = uint32(peer.adjRibIn.Count(rfList)) - accepted = uint32(peer.adjRibIn.Accepted(rfList)) - } + conf.State.AdjTable.Advertised = uint32(peer.adjRibOut.Count(rfList)) + conf.State.AdjTable.Received = uint32(peer.adjRibIn.Count(rfList)) + conf.State.AdjTable.Accepted = uint32(peer.adjRibIn.Accepted(rfList)) - uptime := int64(0) - if timer.State.Uptime != 0 { - uptime = timer.State.Uptime - } - downtime := int64(0) - if timer.State.Downtime != 0 { - downtime = timer.State.Downtime - } + conf.Transport.State.LocalAddress, conf.Transport.State.LocalPort = peer.fsm.LocalHostPort() + _, conf.Transport.State.RemotePort = peer.fsm.RemoteHostPort() - timerconf := &api.TimersConfig{ - ConnectRetry: uint64(timer.Config.ConnectRetry), - HoldTime: uint64(timer.Config.HoldTime), - KeepaliveInterval: uint64(timer.Config.KeepaliveInterval), - } - - timerstate := &api.TimersState{ - KeepaliveInterval: uint64(timer.State.KeepaliveInterval), - NegotiatedHoldTime: uint64(timer.State.NegotiatedHoldTime), - Uptime: uint64(uptime), - Downtime: uint64(downtime), - } - - apitimer := &api.Timers{ - Config: timerconf, - State: timerstate, - } - msgrcv := &api.Message{ - NOTIFICATION: s.Messages.Received.Notification, - UPDATE: s.Messages.Received.Update, - OPEN: s.Messages.Received.Open, - KEEPALIVE: s.Messages.Received.Keepalive, - REFRESH: s.Messages.Received.Refresh, - DISCARDED: s.Messages.Received.Discarded, - TOTAL: s.Messages.Received.Total, - } - msgsnt := &api.Message{ - NOTIFICATION: s.Messages.Sent.Notification, - UPDATE: s.Messages.Sent.Update, - OPEN: s.Messages.Sent.Open, - KEEPALIVE: s.Messages.Sent.Keepalive, - REFRESH: s.Messages.Sent.Refresh, - DISCARDED: s.Messages.Sent.Discarded, - TOTAL: s.Messages.Sent.Total, - } - msg := &api.Messages{ - Received: msgrcv, - Sent: msgsnt, - } - info := &api.PeerState{ - BgpState: f.state.String(), - AdminState: f.adminState.String(), - Messages: msg, - Received: received, - Accepted: accepted, - Advertised: advertised, - } - rr := &api.RouteReflector{ - RouteReflectorClient: peer.fsm.pConf.RouteReflector.Config.RouteReflectorClient, - RouteReflectorClusterId: string(peer.fsm.pConf.RouteReflector.Config.RouteReflectorClusterId), - } - rs := &api.RouteServer{ - RouteServerClient: peer.fsm.pConf.RouteServer.Config.RouteServerClient, - } + conf.State.ReceivedOpenMessage, _ = peer.fsm.recvOpen.Serialize() - return &api.Peer{ - Conf: conf, - Info: info, - Timers: apitimer, - RouteReflector: rr, - RouteServer: rs, } + return &conf } func (peer *Peer) DropAll(rfList []bgp.RouteFamily) { diff --git a/server/server.go b/server/server.go index 05a1f295..0642619b 100644 --- a/server/server.go +++ b/server/server.go @@ -1682,14 +1682,12 @@ func (server *BgpServer) handleGrpc(grpcReq *GrpcRequest) { } close(grpcReq.ResponseCh) case REQ_NEIGHBOR: - l := []*api.Peer{} + l := make([]*config.Neighbor, 0) for _, peer := range server.neighborMap { - l = append(l, peer.ToApiStruct()) + l = append(l, peer.ToConfig()) } grpcReq.ResponseCh <- &GrpcResponse{ - Data: &api.GetNeighborResponse{ - Peers: l, - }, + Data: l, } close(grpcReq.ResponseCh) case REQ_ADJ_RIB_IN, REQ_ADJ_RIB_OUT: diff --git a/tools/pyang_plugins/bgpyang2golang.py b/tools/pyang_plugins/bgpyang2golang.py index af440407..ff5900c0 100644 --- a/tools/pyang_plugins/bgpyang2golang.py +++ b/tools/pyang_plugins/bgpyang2golang.py @@ -23,7 +23,7 @@ _COPYRIGHT_NOTICE = """ // DO NOT EDIT // generated by pyang using OpenConfig https://github.com/openconfig/public // -// Copyright (C) 2014,2015 Nippon Telegraph and Telephone Corporation. +// Copyright (C) 2014-2016 Nippon Telegraph and Telephone Corporation. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -289,7 +289,10 @@ def emit_class_def(ctx, yang_statement, struct_name, prefix): if val_name == 'State': continue if typ == EQUAL_TYPE_LEAF: - print >> o, 'if lhs.{0} != rhs.{0} {{'.format(val_name) + if type_name == '[]byte': + print >> o, 'if bytes.Compare(lhs.{0}, rhs.{0}) != 0 {{'.format(val_name) + else: + print >> o, 'if lhs.{0} != rhs.{0} {{'.format(val_name) print >> o, 'return false' print >> o, '}' elif typ == EQUAL_TYPE_CONTAINER: @@ -301,11 +304,14 @@ def emit_class_def(ctx, yang_statement, struct_name, prefix): print >> o, 'return false' print >> o, '}' print >> o, 'for idx, l := range lhs.{0} {{'.format(val_name) - print >> o, 'if l != rhs.{0}[idx] {{'.format(val_name) + if type_name == '[][]byte': + print >> o, 'if bytes.Compare(l, rhs.{0}[idx]) != 0 {{'.format(val_name) + else: + print >> o, 'if l != rhs.{0}[idx] {{'.format(val_name) print >> o, 'return false' print >> o, '}' print >> o, '}' - elif typ ==EQUAL_TYPE_MAP: + elif typ == EQUAL_TYPE_MAP: print >> o, 'if len(lhs.{0}) != len(rhs.{0}) {{'.format(val_name) print >> o, 'return false' print >> o, '}' @@ -657,6 +663,7 @@ _type_translation_map = { 'inet:port-number': 'uint16', 'yang:timeticks': 'int64', 'ptypes:install-protocol-type': 'string', + 'binary': '[]byte', } @@ -688,7 +695,10 @@ def generate_header(ctx): print _COPYRIGHT_NOTICE print 'package config' print '' - print 'import "fmt"' + print 'import (' + print '"fmt"' + print '"bytes"' + print ')' print '' diff --git a/tools/pyang_plugins/gobgp.yang b/tools/pyang_plugins/gobgp.yang index f9aba5f7..9ccf1d4f 100644 --- a/tools/pyang_plugins/gobgp.yang +++ b/tools/pyang_plugins/gobgp.yang @@ -188,6 +188,23 @@ module gobgp { } + grouping gobgp-adjacent-table { + container adj-table { + leaf ADVERTISED { + type uint32; + } + + leaf RECEIVED { + type uint32; + } + + leaf ACCEPTED { + type uint32; + } + } + } + + grouping gobgp-timer { description "additional timer"; @@ -561,6 +578,27 @@ module gobgp { uses gobgp-message-counter; } + augment "/bgp:bgp/bgp:neighbors/bgp:neighbor/bgp:state" { + description "additional counters"; + uses gobgp-adjacent-table; + } + + + augment "/bgp:bgp/bgp:neighbors/bgp:neighbor/bgp:state" { + container Capabilities { + leaf-list remote { + type binary; + } + leaf-list local { + type binary; + } + } + + leaf received-open-message { + type binary; + } + } + augment "/bgp:bgp/bgp:neighbors/bgp:neighbor/bgp:config" { description "additional state elements"; @@ -580,6 +618,10 @@ module gobgp { "The state of administrative operation. If the state is true, it indicates the neighbor is disabled by the administrator"; } + leaf admin-state { + type string; + } + leaf established-count { type uint32; description |