summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorFUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp>2014-12-25 22:12:34 -0800
committerFUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp>2014-12-25 22:12:34 -0800
commit94e35ee50de5750d46ae32b1c9d736671a0e84a6 (patch)
treed6e1d461cff683308cd91db7e7cbf25b8d48b82d
parent0adb1d0dcd57b17444a3364e576b3bcf94266ce4 (diff)
packet: define BGPPathAttr type
Signed-off-by: FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp>
-rw-r--r--packet/bgp.go20
-rw-r--r--packet/bgpattrtype_string.go28
-rw-r--r--table/path.go4
-rw-r--r--table/table_manager.go57
4 files changed, 41 insertions, 68 deletions
diff --git a/packet/bgp.go b/packet/bgp.go
index ddb81814..747957fa 100644
--- a/packet/bgp.go
+++ b/packet/bgp.go
@@ -983,8 +983,10 @@ const (
BGP_ATTR_FLAG_OPTIONAL = 1 << 7
)
+type BGPAttrType uint8
+
const (
- _ = iota
+ _ BGPAttrType = iota
BGP_ATTR_TYPE_ORIGIN
BGP_ATTR_TYPE_AS_PATH
BGP_ATTR_TYPE_NEXT_HOP
@@ -1084,7 +1086,7 @@ type PathAttributeInterface interface {
type PathAttribute struct {
Flags uint8
- Type uint8
+ Type BGPAttrType
Length uint16
Value []byte
}
@@ -1101,7 +1103,7 @@ func (p *PathAttribute) Len() int {
func (p *PathAttribute) DecodeFromBytes(data []byte) error {
p.Flags = data[0]
- p.Type = data[1]
+ p.Type = BGPAttrType(data[1])
if p.Flags&BGP_ATTR_FLAG_EXTENDED_LENGTH != 0 {
p.Length = binary.BigEndian.Uint16(data[2:4])
@@ -1124,7 +1126,7 @@ func (p *PathAttribute) Serialize() ([]byte, error) {
}
buf := make([]byte, p.Len())
buf[0] = p.Flags
- buf[1] = p.Type
+ buf[1] = uint8(p.Type)
if p.Flags&BGP_ATTR_FLAG_EXTENDED_LENGTH != 0 {
binary.BigEndian.PutUint16(buf[2:4], p.Length)
copy(buf[4:], p.Value)
@@ -1157,7 +1159,7 @@ type AsPathParam struct {
func (a *AsPathParam) Serialize() ([]byte, error) {
buf := make([]byte, 2+len(a.AS)*2)
- buf[0] = a.Type
+ buf[0] = uint8(a.Type)
buf[1] = a.Num
for j, as := range a.AS {
binary.BigEndian.PutUint16(buf[2+j*2:], as)
@@ -1734,13 +1736,13 @@ func (e *OpaqueExtended) Serialize() ([]byte, error) {
}
type UnknownExtended struct {
- Type uint8
+ Type BGPAttrType
Value []byte
}
func (e *UnknownExtended) Serialize() ([]byte, error) {
buf := make([]byte, 8)
- buf[0] = e.Type
+ buf[0] = uint8(e.Type)
copy(buf[1:], e.Value)
return buf, nil
}
@@ -1777,7 +1779,7 @@ func parseExtended(data []byte) ExtendedCommunityInterface {
return e
}
e := &UnknownExtended{}
- e.Type = data[1]
+ e.Type = BGPAttrType(data[1])
e.Value = data[2:8]
return e
}
@@ -1884,7 +1886,7 @@ type PathAttributeUnknown struct {
}
func getPathAttribute(data []byte) PathAttributeInterface {
- switch data[1] {
+ switch BGPAttrType(data[1]) {
case BGP_ATTR_TYPE_ORIGIN:
return &PathAttributeOrigin{}
case BGP_ATTR_TYPE_AS_PATH:
diff --git a/packet/bgpattrtype_string.go b/packet/bgpattrtype_string.go
new file mode 100644
index 00000000..1a2cf1d0
--- /dev/null
+++ b/packet/bgpattrtype_string.go
@@ -0,0 +1,28 @@
+// generated by stringer -type BGPAttrType bgp.go; DO NOT EDIT
+
+package bgp
+
+import "fmt"
+
+const (
+ _BGPAttrType_name_0 = "BGP_ATTR_TYPE_ORIGINBGP_ATTR_TYPE_AS_PATHBGP_ATTR_TYPE_NEXT_HOPBGP_ATTR_TYPE_MULTI_EXIT_DISCBGP_ATTR_TYPE_LOCAL_PREFBGP_ATTR_TYPE_ATOMIC_AGGREGATEBGP_ATTR_TYPE_AGGREGATORBGP_ATTR_TYPE_COMMUNITIESBGP_ATTR_TYPE_ORIGINATOR_IDBGP_ATTR_TYPE_CLUSTER_LIST"
+ _BGPAttrType_name_1 = "BGP_ATTR_TYPE_MP_REACH_NLRIBGP_ATTR_TYPE_MP_UNREACH_NLRIBGP_ATTR_TYPE_EXTENDED_COMMUNITIESBGP_ATTR_TYPE_AS4_PATHBGP_ATTR_TYPE_AS4_AGGREGATOR"
+)
+
+var (
+ _BGPAttrType_index_0 = [...]uint8{0, 20, 41, 63, 92, 116, 146, 170, 195, 222, 248}
+ _BGPAttrType_index_1 = [...]uint8{0, 27, 56, 90, 112, 140}
+)
+
+func (i BGPAttrType) String() string {
+ switch {
+ case 1 <= i && i <= 10:
+ i -= 1
+ return _BGPAttrType_name_0[_BGPAttrType_index_0[i]:_BGPAttrType_index_0[i+1]]
+ case 14 <= i && i <= 18:
+ i -= 14
+ return _BGPAttrType_name_1[_BGPAttrType_index_1[i]:_BGPAttrType_index_1[i+1]]
+ default:
+ return fmt.Sprintf("BGPAttrType(%d)", i)
+ }
+}
diff --git a/table/path.go b/table/path.go
index 635cbf5d..4d4e0c44 100644
--- a/table/path.go
+++ b/table/path.go
@@ -27,7 +27,7 @@ import (
type Path interface {
String() string
GetPathAttrs() []bgp.PathAttributeInterface
- GetPathAttr(int) (int, bgp.PathAttributeInterface)
+ GetPathAttr(bgp.BGPAttrType) (int, bgp.PathAttributeInterface)
getRouteFamily() RouteFamily
setSource(source *PeerInfo)
getSource() *PeerInfo
@@ -193,7 +193,7 @@ func (pd *PathDefault) GetPathAttrs() []bgp.PathAttributeInterface {
return pd.pathAttrs
}
-func (pd *PathDefault) GetPathAttr(pattrType int) (int, bgp.PathAttributeInterface) {
+func (pd *PathDefault) GetPathAttr(pattrType bgp.BGPAttrType) (int, bgp.PathAttributeInterface) {
attrMap := [bgp.BGP_ATTR_TYPE_AS4_AGGREGATOR + 1]reflect.Type{}
attrMap[bgp.BGP_ATTR_TYPE_ORIGIN] = reflect.TypeOf(&bgp.PathAttributeOrigin{})
attrMap[bgp.BGP_ATTR_TYPE_AS_PATH] = reflect.TypeOf(&bgp.PathAttributeAsPath{})
diff --git a/table/table_manager.go b/table/table_manager.go
index c6b15543..18dbfb2d 100644
--- a/table/table_manager.go
+++ b/table/table_manager.go
@@ -54,63 +54,6 @@ func (rf RouteFamily) String() string {
}
}
-type AttributeType int
-
-const (
- BGP_ATTR_TYPE_ORIGIN AttributeType = bgp.BGP_ATTR_TYPE_ORIGIN
- BGP_ATTR_TYPE_AS_PATH AttributeType = bgp.BGP_ATTR_TYPE_AS_PATH
- BGP_ATTR_TYPE_NEXT_HOP AttributeType = bgp.BGP_ATTR_TYPE_NEXT_HOP
- BGP_ATTR_TYPE_MULTI_EXIT_DISC AttributeType = bgp.BGP_ATTR_TYPE_MULTI_EXIT_DISC
- BGP_ATTR_TYPE_LOCAL_PREF AttributeType = bgp.BGP_ATTR_TYPE_LOCAL_PREF
- BGP_ATTR_TYPE_ATOMIC_AGGREGATE AttributeType = bgp.BGP_ATTR_TYPE_ATOMIC_AGGREGATE
- BGP_ATTR_TYPE_AGGREGATOR AttributeType = bgp.BGP_ATTR_TYPE_AGGREGATOR
- BGP_ATTR_TYPE_COMMUNITIES AttributeType = bgp.BGP_ATTR_TYPE_COMMUNITIES
- BGP_ATTR_TYPE_ORIGINATOR_ID AttributeType = bgp.BGP_ATTR_TYPE_ORIGINATOR_ID
- BGP_ATTR_TYPE_CLUSTER_LIST AttributeType = bgp.BGP_ATTR_TYPE_CLUSTER_LIST
- BGP_ATTR_TYPE_MP_REACH_NLRI AttributeType = bgp.BGP_ATTR_TYPE_MP_REACH_NLRI
- BGP_ATTR_TYPE_MP_UNREACH_NLRI AttributeType = bgp.BGP_ATTR_TYPE_MP_UNREACH_NLRI
- BGP_ATTR_TYPE_EXTENDED_COMMUNITIES AttributeType = bgp.BGP_ATTR_TYPE_EXTENDED_COMMUNITIES
- BGP_ATTR_TYPE_AS4_PATH AttributeType = bgp.BGP_ATTR_TYPE_AS4_PATH
- BGP_ATTR_TYPE_AS4_AGGREGATOR AttributeType = bgp.BGP_ATTR_TYPE_AS4_AGGREGATOR
-)
-
-func (attr AttributeType) String() string {
- switch attr {
- case BGP_ATTR_TYPE_ORIGIN:
- return "BGP_ATTR_TYPE_ORIGIN"
- case BGP_ATTR_TYPE_AS_PATH:
- return "BGP_ATTR_TYPE_AS_PATH"
- case BGP_ATTR_TYPE_NEXT_HOP:
- return "BGP_ATTR_TYPE_NEXT_HOP"
- case BGP_ATTR_TYPE_MULTI_EXIT_DISC:
- return "BGP_ATTR_TYPE_MULTI_EXIT_DISC"
- case BGP_ATTR_TYPE_LOCAL_PREF:
- return "BGP_ATTR_TYPE_LOCAL_PREF"
- case BGP_ATTR_TYPE_ATOMIC_AGGREGATE:
- return "BGP_ATTR_TYPE_ATOMIC_AGGREGATE"
- case BGP_ATTR_TYPE_AGGREGATOR:
- return "BGP_ATTR_TYPE_AGGREGATOR"
- case BGP_ATTR_TYPE_COMMUNITIES:
- return "BGP_ATTR_TYPE_COMMUNITIES"
- case BGP_ATTR_TYPE_ORIGINATOR_ID:
- return "BGP_ATTR_TYPE_ORIGINATOR_ID"
- case BGP_ATTR_TYPE_CLUSTER_LIST:
- return "BGP_ATTR_TYPE_CLUSTER_LIST"
- case BGP_ATTR_TYPE_MP_REACH_NLRI:
- return "BGP_ATTR_TYPE_MP_REACH_NLRI"
- case BGP_ATTR_TYPE_MP_UNREACH_NLRI:
- return "BGP_ATTR_TYPE_MP_UNREACH_NLRI"
- case BGP_ATTR_TYPE_EXTENDED_COMMUNITIES:
- return "BGP_ATTR_TYPE_EXTENDED_COMMUNITIES"
- case BGP_ATTR_TYPE_AS4_PATH:
- return "BGP_ATTR_TYPE_AS4_PATH"
- case BGP_ATTR_TYPE_AS4_AGGREGATOR:
- return "BGP_ATTR_TYPE_AS4_AGGREGATOR"
- default:
- return "Unknown"
- }
-}
-
type ProcessMessage struct {
innerMessage *bgp.BGPMessage
fromPeer *PeerInfo