From 40e40449ff08a0f226ca8b5cee04307a66a5e59f Mon Sep 17 00:00:00 2001 From: FUJITA Tomonori Date: Tue, 30 Dec 2014 22:08:08 +0900 Subject: serve: move functions about path and bgp message to table/message.go Signed-off-by: FUJITA Tomonori --- server/peer.go | 32 +++++--------------------------- table/message.go | 24 ++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 27 deletions(-) diff --git a/server/peer.go b/server/peer.go index aa3405f6..702da772 100644 --- a/server/peer.go +++ b/server/peer.go @@ -94,40 +94,18 @@ func (peer *Peer) sendMessages(msgs []*bgp.BGPMessage) { } } -func path2v4update(path table.Path) *bgp.BGPMessage { - if path.IsWithdraw() { - draw := path.GetNlri().(*bgp.WithdrawnRoute) - return bgp.NewBGPUpdateMessage([]bgp.WithdrawnRoute{*draw}, []bgp.PathAttributeInterface{}, []bgp.NLRInfo{}) - } else { - nlri := path.GetNlri().(*bgp.NLRInfo) - pathAttrs := path.GetPathAttrs() - return bgp.NewBGPUpdateMessage([]bgp.WithdrawnRoute{}, pathAttrs, []bgp.NLRInfo{*nlri}) - } -} - -func path2v6update(path table.Path) *bgp.BGPMessage { - if path.IsWithdraw() { - pathAttrs := path.GetPathAttrs() - return bgp.NewBGPUpdateMessage([]bgp.WithdrawnRoute{}, pathAttrs, []bgp.NLRInfo{}) - } else { - pathAttrs := path.GetPathAttrs() - return bgp.NewBGPUpdateMessage([]bgp.WithdrawnRoute{}, pathAttrs, []bgp.NLRInfo{}) - } -} - func (peer *Peer) path2update(pathList []table.Path) []*bgp.BGPMessage { - // TODO: merge multiple messages // TODO: 4bytes and 2bytes conversion. msgs := make([]*bgp.BGPMessage, 0) + var pMsg *bgp.BGPMessage for _, p := range pathList { if peer.rf != p.GetRouteFamily() { continue } - - if peer.rf == bgp.RF_IPv4_UC { - msgs = append(msgs, path2v4update(p)) - } else { - msgs = append(msgs, path2v6update(p)) + msg, _ := table.CreateUpdateMsgFromPath(p, pMsg) + if msg != nil { + msgs = append(msgs, msg) + pMsg = msg } } return msgs diff --git a/table/message.go b/table/message.go index 35eb501a..7ea1c361 100644 --- a/table/message.go +++ b/table/message.go @@ -107,3 +107,27 @@ func UpdatePathAttrs4ByteAs(msg *bgp.BGPUpdate) error { } return nil } + +func CreateUpdateMsgFromPath(path Path, msg *bgp.BGPMessage) (*bgp.BGPMessage, error) { + rf := path.GetRouteFamily() + + if rf == bgp.RF_IPv4_UC { + if path.IsWithdraw() { + draw := path.GetNlri().(*bgp.WithdrawnRoute) + return bgp.NewBGPUpdateMessage([]bgp.WithdrawnRoute{*draw}, []bgp.PathAttributeInterface{}, []bgp.NLRInfo{}), nil + } else { + nlri := path.GetNlri().(*bgp.NLRInfo) + pathAttrs := path.GetPathAttrs() + return bgp.NewBGPUpdateMessage([]bgp.WithdrawnRoute{}, pathAttrs, []bgp.NLRInfo{*nlri}), nil + } + } else if rf == bgp.RF_IPv6_UC { + if path.IsWithdraw() { + pathAttrs := path.GetPathAttrs() + return bgp.NewBGPUpdateMessage([]bgp.WithdrawnRoute{}, pathAttrs, []bgp.NLRInfo{}), nil + } else { + pathAttrs := path.GetPathAttrs() + return bgp.NewBGPUpdateMessage([]bgp.WithdrawnRoute{}, pathAttrs, []bgp.NLRInfo{}), nil + } + } + return nil, nil +} -- cgit v1.2.3