summaryrefslogtreecommitdiffhomepage
path: root/zebra
diff options
context:
space:
mode:
authorHiroshi Yokoi <yokoi.hiroshi@po.ntts.co.jp>2015-08-26 18:11:38 +0900
committerHiroshi Yokoi <yokoi.hiroshi@po.ntts.co.jp>2015-09-01 19:42:02 +0900
commitcc51c48a4934bde8f5a5555632633c3c8486b797 (patch)
tree14998dac3531e6a609bb414d2816f7f7d2ba1d5f /zebra
parentf8d437ec0ea65656ca4c6d81ec48fe0f7099c60e (diff)
zebra: distribute routes from zebra
Signed-off-by: Hiroshi Yokoi <yokoi.hiroshi@po.ntts.co.jp>
Diffstat (limited to 'zebra')
-rw-r--r--zebra/zapi.go149
-rw-r--r--zebra/zapi_test.go320
2 files changed, 454 insertions, 15 deletions
diff --git a/zebra/zapi.go b/zebra/zapi.go
index c014531c..08fcf80d 100644
--- a/zebra/zapi.go
+++ b/zebra/zapi.go
@@ -226,7 +226,7 @@ func NewClient(network, address string, typ ROUTE_TYPE) (*Client, error) {
log.Error("failed to read header: ", err)
return err
}
-
+ log.Debugf("read header from zebra: %v", headerBuf)
hd := &Header{}
err = hd.DecodeFromBytes(headerBuf)
if err != nil {
@@ -239,7 +239,7 @@ func NewClient(network, address string, typ ROUTE_TYPE) (*Client, error) {
log.Error("failed to read body: ", err)
return err
}
-
+ log.Debugf("read body from zebra: %v", bodyBuf)
m, err := ParseMessage(hd, bodyBuf)
if err != nil {
log.Warn("failed to parse message: ", err)
@@ -272,6 +272,12 @@ func (c *Client) Send(m *Message) {
}
func (c *Client) SendCommand(command API_TYPE, body Body) error {
+
+ log.WithFields(log.Fields{
+ "Topic": "Zebra",
+ "Command": command.String(),
+ "Body": body,
+ }).Debug("send command to zebra")
m := &Message{
Header: Header{
Len: HEADER_SIZE,
@@ -288,7 +294,7 @@ func (c *Client) SendCommand(command API_TYPE, body Body) error {
func (c *Client) SendHello() error {
if c.redistDefault > 0 {
body := &HelloBody{
- Redist: c.redistDefault,
+ RedistDefault: c.redistDefault,
}
return c.SendCommand(HELLO, body)
}
@@ -303,6 +309,24 @@ func (c *Client) SendInterfaceAdd() error {
return c.SendCommand(INTERFACE_ADD, nil)
}
+func (c *Client) SendRedistribute() error {
+ for i := ROUTE_SYSTEM; i < ROUTE_MAX; i++ {
+ if c.redistDefault != i {
+ body := &RedistributeBody{
+ Redist: i,
+ }
+ if e := c.SendCommand(REDISTRIBUTE_ADD, body); e != nil {
+ return e
+ }
+ }
+ }
+
+ if e := c.SendCommand(REDISTRIBUTE_DEFAULT_ADD, nil); e != nil {
+ return e
+ }
+ return nil
+}
+
func (c *Client) Close() error {
close(c.outgoing)
return c.conn.Close()
@@ -341,15 +365,28 @@ type Body interface {
}
type HelloBody struct {
- Redist ROUTE_TYPE
+ RedistDefault ROUTE_TYPE
}
func (b *HelloBody) DecodeFromBytes(data []byte) error {
- b.Redist = ROUTE_TYPE(data[0])
+ b.RedistDefault = ROUTE_TYPE(data[0])
return nil
}
func (b *HelloBody) Serialize() ([]byte, error) {
+ return []byte{uint8(b.RedistDefault)}, nil
+}
+
+type RedistributeBody struct {
+ Redist ROUTE_TYPE
+}
+
+func (b *RedistributeBody) DecodeFromBytes(data []byte) error {
+ b.Redist = ROUTE_TYPE(data[0])
+ return nil
+}
+
+func (b *RedistributeBody) Serialize() ([]byte, error) {
return []byte{uint8(b.Redist)}, nil
}
@@ -471,16 +508,7 @@ type IPRouteBody struct {
Ifindexs []uint32
Distance uint8
Metric uint32
-}
-
-func (b *IPRouteBody) DecodeFromBytes(data []byte) error {
- b.Type = ROUTE_TYPE(data[0])
- b.Flags = FLAG(data[1])
- b.Message = data[2]
- b.SAFI = SAFI(data[3])
- b.Prefix = data[3:7]
- b.PrefixLength = data[7]
- return nil
+ Api API_TYPE
}
func (b *IPRouteBody) Serialize() ([]byte, error) {
@@ -537,6 +565,94 @@ func (b *IPRouteBody) Serialize() ([]byte, error) {
return buf, nil
}
+func (b *IPRouteBody) DecodeFromBytes(data []byte) error {
+
+ isV4 := b.Api == IPV4_ROUTE_ADD || b.Api == IPV4_ROUTE_DELETE
+ var addrLen uint8 = net.IPv4len
+ if !isV4 {
+ addrLen = net.IPv6len
+ }
+
+ b.Type = ROUTE_TYPE(data[0])
+ b.Flags = FLAG(data[1])
+ b.Message = data[2]
+ b.PrefixLength = data[3]
+ b.SAFI = SAFI(SAFI_UNICAST)
+
+ if b.PrefixLength > addrLen*8 {
+ return fmt.Errorf("prefix length is greater than %d", addrLen*8)
+ }
+
+ byteLen := int((b.PrefixLength + 7) / 8)
+
+ curPos := 4
+ buf := make([]byte, addrLen)
+ copy(buf, data[curPos:curPos+byteLen])
+
+ if isV4 {
+ b.Prefix = net.IP(buf).To4()
+ } else {
+ b.Prefix = net.IP(buf).To16()
+ }
+
+ curPos += byteLen
+
+ rest := 0
+ var numNexthop int
+ if b.Message&MESSAGE_NEXTHOP > 0 {
+ numNexthop = int(data[curPos])
+ // rest = numNexthop(1) + (nexthop(4 or 16) + placeholder(1) + ifindex(4)) * numNexthop
+ rest += 1 + numNexthop*(int(addrLen)+5)
+ }
+
+ if b.Message&MESSAGE_DISTANCE > 0 {
+ // distance(1)
+ rest += 1
+ }
+
+ if b.Message&MESSAGE_METRIC > 0 {
+ // metric(4)
+ rest += 4
+ }
+
+ if len(data[curPos:]) != rest {
+ return fmt.Errorf("message length invalid")
+ }
+
+ b.Nexthops = []net.IP{}
+ b.Ifindexs = []uint32{}
+
+ if b.Message&MESSAGE_NEXTHOP > 0 {
+ curPos += 1
+ for i := 0; i < numNexthop; i++ {
+ addr := data[curPos : curPos+int(addrLen)]
+ var nexthop net.IP
+ if isV4 {
+ nexthop = net.IP(addr).To4()
+ } else {
+ nexthop = net.IP(addr).To16()
+ }
+ b.Nexthops = append(b.Nexthops, nexthop)
+
+ // skip nexthop and 1byte place holder
+ curPos += int(addrLen + 1)
+ ifidx := binary.BigEndian.Uint32(data[curPos : curPos+4])
+ b.Ifindexs = append(b.Ifindexs, ifidx)
+ curPos += 4
+ }
+ }
+
+ if b.Message&MESSAGE_DISTANCE > 0 {
+ b.Distance = data[curPos]
+ }
+ if b.Message&MESSAGE_METRIC > 0 {
+ curPos += 1
+ b.Metric = binary.BigEndian.Uint32(data[curPos : curPos+4])
+ }
+
+ return nil
+}
+
type Message struct {
Header Header
Body Body
@@ -569,6 +685,9 @@ func ParseMessage(hdr *Header, data []byte) (*Message, error) {
m.Body = &InterfaceAddressUpdateBody{}
case ROUTER_ID_UPDATE:
m.Body = &RouterIDUpdateBody{}
+ case IPV4_ROUTE_ADD, IPV6_ROUTE_ADD, IPV4_ROUTE_DELETE, IPV6_ROUTE_DELETE:
+ m.Body = &IPRouteBody{Api: m.Header.Command}
+ log.Debugf("ipv4/v6 route add/delete message received: %v", data)
default:
return nil, fmt.Errorf("Unknown zapi command: %d", m.Header.Command)
}
diff --git a/zebra/zapi_test.go b/zebra/zapi_test.go
new file mode 100644
index 00000000..fcb912a1
--- /dev/null
+++ b/zebra/zapi_test.go
@@ -0,0 +1,320 @@
+// Copyright (C) 2014, 2015 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.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+// implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package zebra
+
+import (
+ "encoding/binary"
+ "github.com/stretchr/testify/assert"
+ "net"
+ "testing"
+)
+
+func Test_Header(t *testing.T) {
+ assert := assert.New(t)
+
+ //DecodeFromBytes
+ buf := make([]byte, 6)
+ binary.BigEndian.PutUint16(buf[0:], 10)
+ buf[2] = HEADER_MARKER
+ buf[3] = VERSION
+ binary.BigEndian.PutUint16(buf[4:], uint16(IPV4_ROUTE_ADD))
+ h := &Header{}
+ err := h.DecodeFromBytes(buf)
+ assert.Equal(nil, err)
+
+ //Serialize
+ buf, err = h.Serialize()
+ assert.Equal(nil, err)
+ h2 := &Header{}
+ err = h2.DecodeFromBytes(buf)
+ assert.Equal(nil, err)
+ assert.Equal(h, h2)
+
+ // header_size mismatch
+ buf = make([]byte, HEADER_SIZE-1)
+ binary.BigEndian.PutUint16(buf[0:], 10)
+ buf[2] = 0xff
+ buf[3] = 0x02
+ h3 := &Header{}
+ err = h3.DecodeFromBytes(buf)
+ assert.NotEqual(nil, err)
+}
+
+func Test_InterfaceUpdateBody(t *testing.T) {
+ assert := assert.New(t)
+
+ //DecodeFromBytes
+ buf := make([]byte, INTERFACE_NAMSIZ+49)
+ pos := INTERFACE_NAMSIZ
+ binary.BigEndian.PutUint32(buf[pos:], 1)
+ pos += 4
+ buf[pos] = INTERFACE_ACTIVE
+ pos += 1
+ binary.BigEndian.PutUint64(buf[pos:], 1)
+ pos += 8 // flags
+ binary.BigEndian.PutUint32(buf[pos:], 1)
+ pos += 4 // metric
+ binary.BigEndian.PutUint32(buf[pos:], 1500)
+ pos += 4 // MTU
+ binary.BigEndian.PutUint32(buf[pos:], 1500)
+ pos += 4 // MTU6
+ binary.BigEndian.PutUint32(buf[pos:], 200)
+ pos += 4 // bandwidth
+ binary.BigEndian.PutUint32(buf[pos:], 6)
+ pos += 4 // hwaddr_len
+ mac, _ := net.ParseMAC("01:23:45:67:89:ab")
+ copy(buf[pos:pos+6], []byte(mac))
+ pos += 4
+ b := &InterfaceUpdateBody{}
+ err := b.DecodeFromBytes(buf)
+ assert.Equal(nil, err)
+ assert.Equal("01:23:45:67:89:ab", b.HardwareAddr.String())
+
+ buf = make([]byte, INTERFACE_NAMSIZ+28)
+ b = &InterfaceUpdateBody{}
+ err = b.DecodeFromBytes(buf)
+ assert.NotEqual(nil, err)
+}
+
+func Test_InterfaceAddressUpdateBody(t *testing.T) {
+ assert := assert.New(t)
+
+ //DecodeFromBytes
+ buf := make([]byte, 11)
+ pos := 0
+ binary.BigEndian.PutUint32(buf[pos:], 0)
+ pos += 4
+ buf[pos] = 0x01
+ pos += 1
+ buf[pos] = 0x2
+ pos += 1
+ ip := net.ParseIP("192.168.100.1").To4()
+ copy(buf[pos:pos+4], []byte(ip))
+ pos += 4
+ buf[pos] = byte(24)
+
+ b := &InterfaceAddressUpdateBody{}
+ err := b.DecodeFromBytes(buf)
+ assert.Equal(uint32(0), b.Index)
+ assert.Equal(uint8(1), b.Flags)
+ assert.Equal("192.168.100.1", b.Prefix.String())
+ assert.Equal(uint8(24), b.Length)
+
+ // af invalid
+ buf[5] = 0x4
+ pos += 1
+ b = &InterfaceAddressUpdateBody{}
+ err = b.DecodeFromBytes(buf)
+ assert.NotEqual(nil, err)
+}
+
+func Test_RouterIDUpdateBody(t *testing.T) {
+ assert := assert.New(t)
+
+ //DecodeFromBytes
+ buf := make([]byte, 6)
+ pos := 0
+ buf[pos] = 0x2
+ pos += 1
+ ip := net.ParseIP("192.168.100.1").To4()
+ copy(buf[pos:pos+4], []byte(ip))
+ pos += 4
+ buf[pos] = byte(32)
+
+ b := &RouterIDUpdateBody{}
+ err := b.DecodeFromBytes(buf)
+ assert.Equal(nil, err)
+ assert.Equal("192.168.100.1", b.Prefix.String())
+ assert.Equal(uint8(32), b.Length)
+
+ // af invalid
+ buf[0] = 0x4
+ pos += 1
+ b = &RouterIDUpdateBody{}
+ err = b.DecodeFromBytes(buf)
+ assert.NotEqual(nil, err)
+}
+
+func Test_IPRouteBody_IPv4(t *testing.T) {
+ assert := assert.New(t)
+
+ //DecodeFromBytes IPV4_ROUTE
+ buf := make([]byte, 22)
+ buf[0] = byte(ROUTE_CONNECT)
+ buf[1] = byte(FLAG_SELECTED)
+ buf[2] = MESSAGE_NEXTHOP | MESSAGE_DISTANCE | MESSAGE_METRIC
+ buf[3] = 24
+ ip := net.ParseIP("192.168.100.0").To4()
+ copy(buf[4:7], []byte(ip))
+
+ buf[7] = 1
+ nexthop := net.ParseIP("0.0.0.0").To4()
+ copy(buf[8:12], []byte(nexthop))
+
+ buf[12] = 1
+ binary.BigEndian.PutUint32(buf[13:], 1)
+ buf[17] = 0 // distance
+ binary.BigEndian.PutUint32(buf[18:], 1)
+ r := &IPRouteBody{Api: IPV4_ROUTE_ADD}
+ err := r.DecodeFromBytes(buf)
+
+ assert.Equal(nil, err)
+ assert.Equal("192.168.100.0", r.Prefix.String())
+ assert.Equal(uint8(0x18), r.PrefixLength)
+ assert.Equal(uint8(MESSAGE_NEXTHOP|MESSAGE_DISTANCE|MESSAGE_METRIC), r.Message)
+ assert.Equal("0.0.0.0", r.Nexthops[0].String())
+ assert.Equal(uint32(1), r.Ifindexs[0])
+ assert.Equal(uint8(0), r.Distance)
+ assert.Equal(uint32(1), r.Metric)
+
+ //Serialize
+ buf, err = r.Serialize()
+ assert.Equal(nil, err)
+ assert.Equal([]byte{0x2, 0x10, 0xd}, buf[0:3])
+ assert.Equal([]byte{0x0, 0x1}, buf[3:5])
+ assert.Equal(byte(24), buf[5])
+ ip = net.ParseIP("192.168.100.0").To4()
+ assert.Equal([]byte(ip)[0:3], buf[6:9])
+ assert.Equal(byte(NEXTHOP_IPV4), buf[10])
+ assert.Equal(byte(NEXTHOP_IFINDEX), buf[15])
+ assert.Equal(byte(0x0), buf[20])
+
+ bi := make([]byte, 4)
+ binary.BigEndian.PutUint32(bi, 1)
+ assert.Equal(bi, buf[21:])
+
+ // length invalid
+ buf = make([]byte, 18)
+ buf[0] = byte(ROUTE_CONNECT)
+ buf[1] = byte(FLAG_SELECTED)
+ buf[2] = MESSAGE_NEXTHOP | MESSAGE_DISTANCE | MESSAGE_METRIC
+ buf[3] = 24
+ ip = net.ParseIP("192.168.100.0").To4()
+ copy(buf[4:7], []byte(ip))
+ buf[7] = 1
+ nexthop = net.ParseIP("0.0.0.0").To4()
+ copy(buf[8:12], []byte(nexthop))
+ buf[12] = 1
+ binary.BigEndian.PutUint32(buf[13:], 1)
+
+ r = &IPRouteBody{Api: IPV4_ROUTE_ADD}
+ err = r.DecodeFromBytes(buf)
+ assert.Equal("message length invalid", err.Error())
+
+ // no nexthop
+ buf = make([]byte, 12)
+ buf[0] = byte(ROUTE_CONNECT)
+ buf[1] = byte(FLAG_SELECTED)
+ buf[2] = MESSAGE_DISTANCE | MESSAGE_METRIC
+ buf[3] = 24
+ ip = net.ParseIP("192.168.100.0").To4()
+ copy(buf[4:7], []byte(ip))
+ buf[7] = 1
+ binary.BigEndian.PutUint32(buf[8:], 0)
+ r = &IPRouteBody{Api: IPV6_ROUTE_ADD}
+ err = r.DecodeFromBytes(buf)
+ assert.Equal(nil, err)
+
+}
+
+func Test_IPRouteBody_IPv6(t *testing.T) {
+ assert := assert.New(t)
+
+ //DecodeFromBytes IPV6_ROUTE
+ buf := make([]byte, 39)
+ buf[0] = byte(ROUTE_CONNECT)
+ buf[1] = byte(FLAG_SELECTED)
+ buf[2] = MESSAGE_NEXTHOP | MESSAGE_DISTANCE | MESSAGE_METRIC
+ buf[3] = 64
+ ip := net.ParseIP("2001:db8:0:f101::").To16()
+ copy(buf[4:12], []byte(ip))
+
+ buf[12] = 1
+ nexthop := net.ParseIP("::").To16()
+ copy(buf[13:29], []byte(nexthop))
+ // ifindex
+ buf[29] = 1
+ binary.BigEndian.PutUint32(buf[30:], 1)
+
+ buf[34] = 0 // distance
+ binary.BigEndian.PutUint32(buf[35:], 1)
+ r := &IPRouteBody{Api: IPV6_ROUTE_ADD}
+ err := r.DecodeFromBytes(buf)
+
+ assert.Equal(nil, err)
+ assert.Equal("2001:db8:0:f101::", r.Prefix.String())
+ assert.Equal(uint8(64), r.PrefixLength)
+ assert.Equal(uint8(MESSAGE_NEXTHOP|MESSAGE_DISTANCE|MESSAGE_METRIC), r.Message)
+ assert.Equal("::", r.Nexthops[0].String())
+ assert.Equal(uint32(1), r.Ifindexs[0])
+ assert.Equal(uint8(0), r.Distance)
+ assert.Equal(uint32(1), r.Metric)
+
+ //Serialize
+ buf, err = r.Serialize()
+ assert.Equal(nil, err)
+ assert.Equal([]byte{0x2, 0x10, 0xd}, buf[0:3])
+ assert.Equal([]byte{0x0, 0x1}, buf[3:5])
+ assert.Equal(byte(64), buf[5])
+ ip = net.ParseIP("2001:db8:0:f101::").To16()
+ assert.Equal([]byte(ip)[0:8], buf[6:14])
+ assert.Equal(byte(2), buf[14])
+ assert.Equal(byte(NEXTHOP_IPV6), buf[15])
+ ip = net.ParseIP("::").To16()
+ assert.Equal([]byte(ip), buf[16:32])
+ assert.Equal(byte(NEXTHOP_IFINDEX), buf[32])
+ bi := make([]byte, 4)
+ binary.BigEndian.PutUint32(bi, 1)
+ assert.Equal(bi, buf[33:37])
+
+ //distance
+ assert.Equal(byte(0), buf[37])
+ bi = make([]byte, 4)
+ binary.BigEndian.PutUint32(bi, 1)
+ assert.Equal(bi, buf[38:])
+
+ // length invalid
+ buf = make([]byte, 50)
+ buf[0] = byte(ROUTE_CONNECT)
+ buf[1] = byte(FLAG_SELECTED)
+ buf[2] = MESSAGE_NEXTHOP | MESSAGE_DISTANCE | MESSAGE_METRIC
+ buf[3] = 24
+ ip = net.ParseIP("2001:db8:0:f101::").To4()
+ copy(buf[4:12], []byte(ip))
+ buf[13] = 1
+ nexthop = net.ParseIP("::").To16()
+ copy(buf[14:30], []byte(nexthop))
+ buf[31] = 1
+ binary.BigEndian.PutUint32(buf[32:], 1)
+
+ r = &IPRouteBody{Api: IPV6_ROUTE_ADD}
+ err = r.DecodeFromBytes(buf)
+ assert.Equal("message length invalid", err.Error())
+
+ // no nexthop
+ buf = make([]byte, 11)
+ buf[0] = byte(ROUTE_CONNECT)
+ buf[1] = byte(FLAG_SELECTED)
+ buf[2] = MESSAGE_DISTANCE | MESSAGE_METRIC
+ buf[3] = 16
+ ip = net.ParseIP("2501::").To16()
+ copy(buf[4:6], []byte(ip))
+ buf[6] = 1
+ binary.BigEndian.PutUint32(buf[7:], 0)
+ r = &IPRouteBody{Api: IPV6_ROUTE_ADD}
+ err = r.DecodeFromBytes(buf)
+ assert.Equal(nil, err)
+}