diff options
author | Hiroshi Yokoi <yokoi.hiroshi@po.ntts.co.jp> | 2015-08-26 18:11:38 +0900 |
---|---|---|
committer | Hiroshi Yokoi <yokoi.hiroshi@po.ntts.co.jp> | 2015-09-01 19:42:02 +0900 |
commit | cc51c48a4934bde8f5a5555632633c3c8486b797 (patch) | |
tree | 14998dac3531e6a609bb414d2816f7f7d2ba1d5f /server/zclient.go | |
parent | f8d437ec0ea65656ca4c6d81ec48fe0f7099c60e (diff) |
zebra: distribute routes from zebra
Signed-off-by: Hiroshi Yokoi <yokoi.hiroshi@po.ntts.co.jp>
Diffstat (limited to 'server/zclient.go')
-rw-r--r-- | server/zclient.go | 63 |
1 files changed, 62 insertions, 1 deletions
diff --git a/server/zclient.go b/server/zclient.go index 7c1b9a42..ab163b37 100644 --- a/server/zclient.go +++ b/server/zclient.go @@ -16,12 +16,14 @@ package server import ( + log "github.com/Sirupsen/logrus" "github.com/osrg/gobgp/packet" "github.com/osrg/gobgp/table" "github.com/osrg/gobgp/zebra" "net" "strconv" "strings" + "time" ) type broadcastZapiMsg struct { @@ -84,6 +86,49 @@ func newIPRouteMessage(path *table.Path) *zebra.Message { } } +func createPathFromIPRouteMessage(m *zebra.Message, peerInfo *table.PeerInfo) *table.Path { + + header := m.Header + body := m.Body.(*zebra.IPRouteBody) + isV4 := header.Command == zebra.IPV4_ROUTE_ADD || header.Command == zebra.IPV4_ROUTE_DELETE + + var nlri bgp.AddrPrefixInterface + pattr := make([]bgp.PathAttributeInterface, 0) + var mpnlri *bgp.PathAttributeMpReachNLRI + var isWithdraw bool = header.Command == zebra.IPV4_ROUTE_DELETE || header.Command == zebra.IPV6_ROUTE_DELETE + + origin := bgp.NewPathAttributeOrigin(bgp.BGP_ORIGIN_ATTR_TYPE_IGP) + pattr = append(pattr, origin) + + log.WithFields(log.Fields{ + "Topic": "Zebra", + "RouteType": body.Type.String(), + "Flag": body.Flags.String(), + "Message": body.Message, + "Prefix": body.Prefix, + "PrefixLength": body.PrefixLength, + "Nexthop": body.Nexthops, + "api": header.Command.String(), + }).Debugf("create path from ip route message.") + + if isV4 { + nlri = bgp.NewNLRInfo(body.PrefixLength, body.Prefix.String()) + nexthop := bgp.NewPathAttributeNextHop("0.0.0.0") + pattr = append(pattr, nexthop) + } else { + nlri = bgp.NewIPv6AddrPrefix(body.PrefixLength, body.Prefix.String()) + mpnlri = bgp.NewPathAttributeMpReachNLRI("::", []bgp.AddrPrefixInterface{nlri}) + pattr = append(pattr, mpnlri) + } + + med := bgp.NewPathAttributeMultiExitDisc(body.Metric) + pattr = append(pattr, med) + + p := table.NewPath(peerInfo, nlri, isWithdraw, pattr, false, time.Now(), true) + p.IsFromZebra = true + return p +} + func newBroadcastZapiBestMsg(cli *zebra.Client, path *table.Path) *broadcastZapiMsg { if cli == nil { return nil @@ -98,5 +143,21 @@ func newBroadcastZapiBestMsg(cli *zebra.Client, path *table.Path) *broadcastZapi } } -func handleZapiMsg(msg *zebra.Message) { +func handleZapiMsg(msg *zebra.Message, server *BgpServer) []*SenderMsg { + + switch b := msg.Body.(type) { + case *zebra.IPRouteBody: + pi := &table.PeerInfo{ + AS: server.bgpConfig.Global.GlobalConfig.As, + LocalID: server.bgpConfig.Global.GlobalConfig.RouterId, + } + + if b.Prefix != nil && len(b.Nexthops) > 0 && b.Type != zebra.ROUTE_KERNEL { + p := createPathFromIPRouteMessage(msg, pi) + msgs := server.propagateUpdate("", false, []*table.Path{p}) + return msgs + } + } + + return nil } |