diff options
-rw-r--r-- | gobgp/cmd/mrt.go | 31 | ||||
-rw-r--r-- | packet/mrt/mrt.go (renamed from packet/bgp/mrt.go) | 57 | ||||
-rw-r--r-- | packet/mrt/mrt_test.go (renamed from packet/bgp/mrt_test.go) | 51 | ||||
-rw-r--r-- | server/server.go | 35 | ||||
-rw-r--r-- | server/watcher.go | 9 |
5 files changed, 94 insertions, 89 deletions
diff --git a/gobgp/cmd/mrt.go b/gobgp/cmd/mrt.go index c1063dd6..658e9703 100644 --- a/gobgp/cmd/mrt.go +++ b/gobgp/cmd/mrt.go @@ -20,6 +20,7 @@ import ( "fmt" api "github.com/osrg/gobgp/api" "github.com/osrg/gobgp/packet/bgp" + "github.com/osrg/gobgp/packet/mrt" "github.com/spf13/cobra" "golang.org/x/net/context" "io" @@ -35,8 +36,8 @@ import ( func printMrtMsgs(data []byte) { buffer := bytes.NewBuffer(data) - for buffer.Len() > bgp.MRT_COMMON_HEADER_LEN { - buf := make([]byte, bgp.MRT_COMMON_HEADER_LEN) + for buffer.Len() > mrt.MRT_COMMON_HEADER_LEN { + buf := make([]byte, mrt.MRT_COMMON_HEADER_LEN) _, err := buffer.Read(buf) if err == io.EOF { break @@ -44,7 +45,7 @@ func printMrtMsgs(data []byte) { exitWithError(fmt.Errorf("failed to read: %s", err)) } - h := &bgp.MRTHeader{} + h := &mrt.MRTHeader{} err = h.DecodeFromBytes(buf) if err != nil { exitWithError(fmt.Errorf("failed to parse")) @@ -56,7 +57,7 @@ func printMrtMsgs(data []byte) { exitWithError(fmt.Errorf("failed to read")) } - msg, err := bgp.ParseMRTBody(h, buf) + msg, err := mrt.ParseMRTBody(h, buf) if err != nil { exitWithError(fmt.Errorf("failed to parse: %s", err)) } @@ -203,10 +204,10 @@ func injectMrt(r string, filename string, count int, skip int) error { go func() { - var peers []*bgp.Peer + var peers []*mrt.Peer for { - buf := make([]byte, bgp.MRT_COMMON_HEADER_LEN) + buf := make([]byte, mrt.MRT_COMMON_HEADER_LEN) _, err := file.Read(buf) if err == io.EOF { break @@ -214,7 +215,7 @@ func injectMrt(r string, filename string, count int, skip int) error { exitWithError(fmt.Errorf("failed to read: %s", err)) } - h := &bgp.MRTHeader{} + h := &mrt.MRTHeader{} err = h.DecodeFromBytes(buf) if err != nil { exitWithError(fmt.Errorf("failed to parse")) @@ -226,7 +227,7 @@ func injectMrt(r string, filename string, count int, skip int) error { exitWithError(fmt.Errorf("failed to read")) } - msg, err := bgp.ParseMRTBody(h, buf) + msg, err := mrt.ParseMRTBody(h, buf) if err != nil { exitWithError(fmt.Errorf("failed to parse: %s", err)) } @@ -235,16 +236,16 @@ func injectMrt(r string, filename string, count int, skip int) error { fmt.Println(msg) } - if msg.Header.Type == bgp.TABLE_DUMPv2 { - subType := bgp.MRTSubTypeTableDumpv2(msg.Header.SubType) + if msg.Header.Type == mrt.TABLE_DUMPv2 { + subType := mrt.MRTSubTypeTableDumpv2(msg.Header.SubType) var rf bgp.RouteFamily switch subType { - case bgp.PEER_INDEX_TABLE: - peers = msg.Body.(*bgp.PeerIndexTable).Peers + case mrt.PEER_INDEX_TABLE: + peers = msg.Body.(*mrt.PeerIndexTable).Peers continue - case bgp.RIB_IPV4_UNICAST: + case mrt.RIB_IPV4_UNICAST: rf = bgp.RF_IPv4_UC - case bgp.RIB_IPV6_UNICAST: + case mrt.RIB_IPV6_UNICAST: rf = bgp.RF_IPv6_UC default: exitWithError(fmt.Errorf("unsupported subType: %s", subType)) @@ -254,7 +255,7 @@ func injectMrt(r string, filename string, count int, skip int) error { exitWithError(fmt.Errorf("not found PEER_INDEX_TABLE")) } - rib := msg.Body.(*bgp.Rib) + rib := msg.Body.(*mrt.Rib) nlri := rib.Prefix paths := make([]*api.Path, 0, len(rib.Entries)) diff --git a/packet/bgp/mrt.go b/packet/mrt/mrt.go index 15dbc839..041a2802 100644 --- a/packet/bgp/mrt.go +++ b/packet/mrt/mrt.go @@ -13,12 +13,13 @@ // See the License for the specific language governing permissions and // limitations under the License. -package bgp +package mrt import ( "bytes" "encoding/binary" "fmt" + "github.com/osrg/gobgp/packet/bgp" "math" "net" "time" @@ -343,7 +344,7 @@ func (t *PeerIndexTable) String() string { type RibEntry struct { PeerIndex uint16 OriginatedTime uint32 - PathAttributes []PathAttributeInterface + PathAttributes []bgp.PathAttributeInterface } func (e *RibEntry) DecodeFromBytes(data []byte) ([]byte, error) { @@ -356,7 +357,7 @@ func (e *RibEntry) DecodeFromBytes(data []byte) ([]byte, error) { totalLen := binary.BigEndian.Uint16(data[6:8]) data = data[8:] for attrLen := totalLen; attrLen > 0; { - p, err := GetPathAttribute(data) + p, err := bgp.GetPathAttribute(data) if err != nil { return nil, err } @@ -402,7 +403,7 @@ func (e *RibEntry) Serialize() ([]byte, error) { return buf, nil } -func NewRibEntry(index uint16, time uint32, pathattrs []PathAttributeInterface) *RibEntry { +func NewRibEntry(index uint16, time uint32, pathattrs []bgp.PathAttributeInterface) *RibEntry { return &RibEntry{ PeerIndex: index, OriginatedTime: time, @@ -416,9 +417,9 @@ func (e *RibEntry) String() string { type Rib struct { SequenceNumber uint32 - Prefix AddrPrefixInterface + Prefix bgp.AddrPrefixInterface Entries []*RibEntry - RouteFamily RouteFamily + RouteFamily bgp.RouteFamily } func (u *Rib) DecodeFromBytes(data []byte) error { @@ -427,13 +428,13 @@ func (u *Rib) DecodeFromBytes(data []byte) error { } u.SequenceNumber = binary.BigEndian.Uint32(data[:4]) data = data[4:] - afi, safi := RouteFamilyToAfiSafi(u.RouteFamily) + afi, safi := bgp.RouteFamilyToAfiSafi(u.RouteFamily) if afi == 0 && safi == 0 { afi = binary.BigEndian.Uint16(data[:2]) safi = data[2] data = data[3:] } - prefix, err := NewPrefixFromRouteFamily(afi, safi) + prefix, err := bgp.NewPrefixFromRouteFamily(afi, safi) if err != nil { return err } @@ -460,9 +461,9 @@ func (u *Rib) DecodeFromBytes(data []byte) error { func (u *Rib) Serialize() ([]byte, error) { buf := make([]byte, 4) binary.BigEndian.PutUint32(buf, u.SequenceNumber) - rf := AfiSafiToRouteFamily(u.Prefix.AFI(), u.Prefix.SAFI()) + rf := bgp.AfiSafiToRouteFamily(u.Prefix.AFI(), u.Prefix.SAFI()) switch rf { - case RF_IPv4_UC, RF_IPv4_MC, RF_IPv6_UC, RF_IPv6_MC: + case bgp.RF_IPv4_UC, bgp.RF_IPv4_MC, bgp.RF_IPv6_UC, bgp.RF_IPv6_MC: default: bbuf := make([]byte, 0, 2) binary.BigEndian.PutUint16(bbuf, u.Prefix.AFI()) @@ -489,8 +490,8 @@ func (u *Rib) Serialize() ([]byte, error) { return buf, nil } -func NewRib(seq uint32, prefix AddrPrefixInterface, entries []*RibEntry) *Rib { - rf := AfiSafiToRouteFamily(prefix.AFI(), prefix.SAFI()) +func NewRib(seq uint32, prefix bgp.AddrPrefixInterface, entries []*RibEntry) *Rib { + rf := bgp.AfiSafiToRouteFamily(prefix.AFI(), prefix.SAFI()) return &Rib{ SequenceNumber: seq, Prefix: prefix, @@ -532,11 +533,11 @@ func (m *BGP4MPHeader) decodeFromBytes(data []byte) ([]byte, error) { m.InterfaceIndex = binary.BigEndian.Uint16(data[:2]) m.AddressFamily = binary.BigEndian.Uint16(data[2:4]) switch m.AddressFamily { - case AFI_IP: + case bgp.AFI_IP: m.PeerIpAddress = net.IP(data[4:8]).To4() m.LocalIpAddress = net.IP(data[8:12]).To4() data = data[12:] - case AFI_IP6: + case bgp.AFI_IP6: m.PeerIpAddress = net.IP(data[4:20]) m.LocalIpAddress = net.IP(data[20:36]) data = data[36:] @@ -559,11 +560,11 @@ func (m *BGP4MPHeader) serialize() ([]byte, error) { } var bbuf []byte switch m.AddressFamily { - case AFI_IP: + case bgp.AFI_IP: bbuf = make([]byte, 8) copy(bbuf, m.PeerIpAddress.To4()) copy(bbuf[4:], m.LocalIpAddress.To4()) - case AFI_IP6: + case bgp.AFI_IP6: bbuf = make([]byte, 32) copy(bbuf, m.PeerIpAddress) copy(bbuf[16:], m.LocalIpAddress) @@ -578,12 +579,12 @@ func newBGP4MPHeader(peeras, localas uint32, intfindex uint16, peerip, localip s paddr := net.ParseIP(peerip).To4() laddr := net.ParseIP(localip).To4() if paddr != nil && laddr != nil { - af = AFI_IP + af = bgp.AFI_IP } else { paddr = net.ParseIP(peerip).To16() laddr = net.ParseIP(localip).To16() if paddr != nil && laddr != nil { - af = AFI_IP6 + af = bgp.AFI_IP6 } else { return nil, fmt.Errorf("Peer IP Address and Local IP Address must have the same address family") } @@ -641,7 +642,7 @@ func NewBGP4MPStateChange(peeras, localas uint32, intfindex uint16, peerip, loca type BGP4MPMessage struct { *BGP4MPHeader - BGPMessage *BGPMessage + BGPMessage *bgp.BGPMessage BGPMessagePayload []byte isLocal bool } @@ -652,11 +653,11 @@ func (m *BGP4MPMessage) DecodeFromBytes(data []byte) error { return err } - if len(rest) < BGP_HEADER_LENGTH { + if len(rest) < bgp.BGP_HEADER_LENGTH { return fmt.Errorf("Not all BGP4MPMessageAS4 bytes available") } - msg, err := ParseBGPMessage(rest) + msg, err := bgp.ParseBGPMessage(rest) if err != nil { return err } @@ -679,7 +680,7 @@ func (m *BGP4MPMessage) Serialize() ([]byte, error) { return append(buf, bbuf...), nil } -func NewBGP4MPMessage(peeras, localas uint32, intfindex uint16, peerip, localip string, isAS4 bool, msg *BGPMessage) *BGP4MPMessage { +func NewBGP4MPMessage(peeras, localas uint32, intfindex uint16, peerip, localip string, isAS4 bool, msg *bgp.BGPMessage) *BGP4MPMessage { header, _ := newBGP4MPHeader(peeras, localas, intfindex, peerip, localip, isAS4) return &BGP4MPMessage{ BGP4MPHeader: header, @@ -687,7 +688,7 @@ func NewBGP4MPMessage(peeras, localas uint32, intfindex uint16, peerip, localip } } -func NewBGP4MPMessageLocal(peeras, localas uint32, intfindex uint16, peerip, localip string, isAS4 bool, msg *BGPMessage) *BGP4MPMessage { +func NewBGP4MPMessageLocal(peeras, localas uint32, intfindex uint16, peerip, localip string, isAS4 bool, msg *bgp.BGPMessage) *BGP4MPMessage { header, _ := newBGP4MPHeader(peeras, localas, intfindex, peerip, localip, isAS4) return &BGP4MPMessage{ BGP4MPHeader: header, @@ -736,18 +737,18 @@ func ParseMRTBody(h *MRTHeader, data []byte) (*MRTMessage, error) { switch h.Type { case TABLE_DUMPv2: subType := MRTSubTypeTableDumpv2(h.SubType) - rf := RouteFamily(0) + rf := bgp.RouteFamily(0) switch subType { case PEER_INDEX_TABLE: msg.Body = &PeerIndexTable{} case RIB_IPV4_UNICAST: - rf = RF_IPv4_UC + rf = bgp.RF_IPv4_UC case RIB_IPV4_MULTICAST: - rf = RF_IPv4_MC + rf = bgp.RF_IPv4_MC case RIB_IPV6_UNICAST: - rf = RF_IPv6_UC + rf = bgp.RF_IPv6_UC case RIB_IPV6_MULTICAST: - rf = RF_IPv6_MC + rf = bgp.RF_IPv6_MC case RIB_GENERIC: default: return nil, fmt.Errorf("unsupported table dumpv2 subtype: %v\n", subType) diff --git a/packet/bgp/mrt_test.go b/packet/mrt/mrt_test.go index 11a233ec..634d69ae 100644 --- a/packet/bgp/mrt_test.go +++ b/packet/mrt/mrt_test.go @@ -13,11 +13,12 @@ // See the License for the specific language governing permissions and // limitations under the License. -package bgp +package mrt import ( "bufio" "bytes" + "github.com/osrg/gobgp/packet/bgp" "github.com/stretchr/testify/assert" "reflect" "testing" @@ -99,18 +100,18 @@ func TestMrtPeerIndexTable(t *testing.T) { } func TestMrtRibEntry(t *testing.T) { - aspath1 := []AsPathParamInterface{ - NewAsPathParam(2, []uint16{1000}), - NewAsPathParam(1, []uint16{1001, 1002}), - NewAsPathParam(2, []uint16{1003, 1004}), + aspath1 := []bgp.AsPathParamInterface{ + bgp.NewAsPathParam(2, []uint16{1000}), + bgp.NewAsPathParam(1, []uint16{1001, 1002}), + bgp.NewAsPathParam(2, []uint16{1003, 1004}), } - p := []PathAttributeInterface{ - NewPathAttributeOrigin(3), - NewPathAttributeAsPath(aspath1), - NewPathAttributeNextHop("129.1.1.2"), - NewPathAttributeMultiExitDisc(1 << 20), - NewPathAttributeLocalPref(1 << 22), + p := []bgp.PathAttributeInterface{ + bgp.NewPathAttributeOrigin(3), + bgp.NewPathAttributeAsPath(aspath1), + bgp.NewPathAttributeNextHop("129.1.1.2"), + bgp.NewPathAttributeMultiExitDisc(1 << 20), + bgp.NewPathAttributeLocalPref(1 << 22), } e1 := NewRibEntry(1, uint32(time.Now().Unix()), p) @@ -129,31 +130,31 @@ func TestMrtRibEntry(t *testing.T) { } func TestMrtRib(t *testing.T) { - aspath1 := []AsPathParamInterface{ - NewAsPathParam(2, []uint16{1000}), - NewAsPathParam(1, []uint16{1001, 1002}), - NewAsPathParam(2, []uint16{1003, 1004}), + aspath1 := []bgp.AsPathParamInterface{ + bgp.NewAsPathParam(2, []uint16{1000}), + bgp.NewAsPathParam(1, []uint16{1001, 1002}), + bgp.NewAsPathParam(2, []uint16{1003, 1004}), } - p := []PathAttributeInterface{ - NewPathAttributeOrigin(3), - NewPathAttributeAsPath(aspath1), - NewPathAttributeNextHop("129.1.1.2"), - NewPathAttributeMultiExitDisc(1 << 20), - NewPathAttributeLocalPref(1 << 22), + p := []bgp.PathAttributeInterface{ + bgp.NewPathAttributeOrigin(3), + bgp.NewPathAttributeAsPath(aspath1), + bgp.NewPathAttributeNextHop("129.1.1.2"), + bgp.NewPathAttributeMultiExitDisc(1 << 20), + bgp.NewPathAttributeLocalPref(1 << 22), } e1 := NewRibEntry(1, uint32(time.Now().Unix()), p) e2 := NewRibEntry(2, uint32(time.Now().Unix()), p) e3 := NewRibEntry(3, uint32(time.Now().Unix()), p) - r1 := NewRib(1, NewIPAddrPrefix(24, "192.168.0.0"), []*RibEntry{e1, e2, e3}) + r1 := NewRib(1, bgp.NewIPAddrPrefix(24, "192.168.0.0"), []*RibEntry{e1, e2, e3}) b1, err := r1.Serialize() if err != nil { t.Fatal(err) } r2 := &Rib{ - RouteFamily: RF_IPv4_UC, + RouteFamily: bgp.RF_IPv4_UC, } err = r2.DecodeFromBytes(b1) if err != nil { @@ -181,7 +182,7 @@ func TestMrtBgp4mpStateChange(t *testing.T) { } func TestMrtBgp4mpMessage(t *testing.T) { - msg := NewBGPKeepAliveMessage() + msg := bgp.NewBGPKeepAliveMessage() m1 := NewBGP4MPMessage(65000, 65001, 1, "192.168.0.1", "192.168.0.2", false, msg) b1, err := m1.Serialize() if err != nil { @@ -199,7 +200,7 @@ func TestMrtSplit(t *testing.T) { var b bytes.Buffer numwrite, numread := 10, 0 for i := 0; i < numwrite; i++ { - msg := NewBGPKeepAliveMessage() + msg := bgp.NewBGPKeepAliveMessage() m1 := NewBGP4MPMessage(65000, 65001, 1, "192.168.0.1", "192.168.0.2", false, msg) mm, _ := NewMRTMessage(1234, BGP4MP, MESSAGE, m1) b1, err := mm.Serialize() diff --git a/server/server.go b/server/server.go index 1b59402d..4d0088ad 100644 --- a/server/server.go +++ b/server/server.go @@ -30,6 +30,7 @@ import ( api "github.com/osrg/gobgp/api" "github.com/osrg/gobgp/config" "github.com/osrg/gobgp/packet/bgp" + "github.com/osrg/gobgp/packet/mrt" "github.com/osrg/gobgp/table" "github.com/osrg/gobgp/zebra" "github.com/satori/go.uuid" @@ -2957,20 +2958,20 @@ func (server *BgpServer) handleMrt(grpcReq *GrpcRequest) { return } -func (server *BgpServer) mkMrtPeerIndexTableMsg(t uint32, view string) (*bgp.MRTMessage, error) { - peers := make([]*bgp.Peer, 0, len(server.neighborMap)) +func (server *BgpServer) mkMrtPeerIndexTableMsg(t uint32, view string) (*mrt.MRTMessage, error) { + peers := make([]*mrt.Peer, 0, len(server.neighborMap)) for _, peer := range server.neighborMap { id := peer.fsm.peerInfo.ID.To4().String() ipaddr := peer.conf.Config.NeighborAddress asn := peer.conf.Config.PeerAs - peers = append(peers, bgp.NewPeer(id, ipaddr, asn, true)) + peers = append(peers, mrt.NewPeer(id, ipaddr, asn, true)) } bgpid := server.bgpConfig.Global.Config.RouterId - table := bgp.NewPeerIndexTable(bgpid, view, peers) - return bgp.NewMRTMessage(t, bgp.TABLE_DUMPv2, bgp.PEER_INDEX_TABLE, table) + table := mrt.NewPeerIndexTable(bgpid, view, peers) + return mrt.NewMRTMessage(t, mrt.TABLE_DUMPv2, mrt.PEER_INDEX_TABLE, table) } -func (server *BgpServer) mkMrtRibMsgs(tbl *table.Table, t uint32) ([]*bgp.MRTMessage, error) { +func (server *BgpServer) mkMrtRibMsgs(tbl *table.Table, t uint32) ([]*mrt.MRTMessage, error) { getPeerIndex := func(info *table.PeerInfo) uint16 { var idx uint16 for _, peer := range server.neighborMap { @@ -2982,42 +2983,42 @@ func (server *BgpServer) mkMrtRibMsgs(tbl *table.Table, t uint32) ([]*bgp.MRTMes return idx } - var subtype bgp.MRTSubTypeTableDumpv2 + var subtype mrt.MRTSubTypeTableDumpv2 switch tbl.GetRoutefamily() { case bgp.RF_IPv4_UC: - subtype = bgp.RIB_IPV4_UNICAST + subtype = mrt.RIB_IPV4_UNICAST case bgp.RF_IPv4_MC: - subtype = bgp.RIB_IPV4_MULTICAST + subtype = mrt.RIB_IPV4_MULTICAST case bgp.RF_IPv6_UC: - subtype = bgp.RIB_IPV6_UNICAST + subtype = mrt.RIB_IPV6_UNICAST case bgp.RF_IPv6_MC: - subtype = bgp.RIB_IPV6_MULTICAST + subtype = mrt.RIB_IPV6_MULTICAST default: - subtype = bgp.RIB_GENERIC + subtype = mrt.RIB_GENERIC } var seq uint32 - msgs := make([]*bgp.MRTMessage, 0, len(tbl.GetDestinations())) + msgs := make([]*mrt.MRTMessage, 0, len(tbl.GetDestinations())) for _, dst := range tbl.GetDestinations() { l := dst.GetKnownPathList(table.GLOBAL_RIB_NAME) - entries := make([]*bgp.RibEntry, 0, len(l)) + entries := make([]*mrt.RibEntry, 0, len(l)) for _, p := range l { // mrt doesn't assume to dump locally generated routes if p.IsLocal() { continue } idx := getPeerIndex(p.GetSource()) - e := bgp.NewRibEntry(idx, uint32(p.GetTimestamp().Unix()), p.GetPathAttrs()) + e := mrt.NewRibEntry(idx, uint32(p.GetTimestamp().Unix()), p.GetPathAttrs()) entries = append(entries, e) } // if dst only contains locally generated routes, ignore it if len(entries) == 0 { continue } - rib := bgp.NewRib(seq, dst.GetNlri(), entries) + rib := mrt.NewRib(seq, dst.GetNlri(), entries) seq++ - msg, err := bgp.NewMRTMessage(t, bgp.TABLE_DUMPv2, subtype, rib) + msg, err := mrt.NewMRTMessage(t, mrt.TABLE_DUMPv2, subtype, rib) if err != nil { return nil, err } diff --git a/server/watcher.go b/server/watcher.go index d3befa0e..d08a6ef3 100644 --- a/server/watcher.go +++ b/server/watcher.go @@ -19,6 +19,7 @@ import ( "bytes" log "github.com/Sirupsen/logrus" "github.com/osrg/gobgp/packet/bgp" + "github.com/osrg/gobgp/packet/mrt" "github.com/osrg/gobgp/table" "gopkg.in/tomb.v2" "net" @@ -137,13 +138,13 @@ func (w *mrtWatcher) loop() error { for { serialize := func(ev watcherEvent) ([]byte, error) { m := ev.(*watcherEventUpdateMsg) - subtype := bgp.MESSAGE_AS4 - mp := bgp.NewBGP4MPMessage(m.peerAS, m.localAS, 0, m.peerAddress.String(), m.localAddress.String(), m.fourBytesAs, nil) + subtype := mrt.MESSAGE_AS4 + mp := mrt.NewBGP4MPMessage(m.peerAS, m.localAS, 0, m.peerAddress.String(), m.localAddress.String(), m.fourBytesAs, nil) mp.BGPMessagePayload = m.payload if m.fourBytesAs == false { - subtype = bgp.MESSAGE + subtype = mrt.MESSAGE } - bm, err := bgp.NewMRTMessage(uint32(m.timestamp.Unix()), bgp.BGP4MP, subtype, mp) + bm, err := mrt.NewMRTMessage(uint32(m.timestamp.Unix()), mrt.BGP4MP, subtype, mp) if err != nil { log.WithFields(log.Fields{ "Topic": "mrt", |