summaryrefslogtreecommitdiffhomepage
path: root/table/policy.go
diff options
context:
space:
mode:
authorFUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp>2017-02-24 13:43:53 +0900
committerFUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp>2017-02-24 15:15:08 +0900
commite5070a34534aca10807f7b309498709443f4328a (patch)
tree80cc95ca415f68d177678385f1270ec5a64ba266 /table/policy.go
parentba75b7386948a4c2f6f281c7c93897ee5e3b4e84 (diff)
policy: fix prefixset match about different families
currently wrongly try to match prefixset for v4 to v6 routes, and vice versa. Signed-off-by: FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp>
Diffstat (limited to 'table/policy.go')
-rw-r--r--table/policy.go50
1 files changed, 41 insertions, 9 deletions
diff --git a/table/policy.go b/table/policy.go
index d1acac4c..fe1dcaf8 100644
--- a/table/policy.go
+++ b/table/policy.go
@@ -326,8 +326,9 @@ func NewPrefix(c config.Prefix) (*Prefix, error) {
}
type PrefixSet struct {
- name string
- tree *radix.Tree
+ name string
+ tree *radix.Tree
+ family bgp.RouteFamily
}
func (s *PrefixSet) Name() string {
@@ -343,6 +344,16 @@ func (lhs *PrefixSet) Append(arg DefinedSet) error {
if !ok {
return fmt.Errorf("type cast failed")
}
+ // if either is empty, family can be ignored.
+ if lhs.tree.Len() != 0 && rhs.tree.Len() != 0 {
+ _, w, _ := lhs.tree.Minimum()
+ l := w.([]*Prefix)
+ _, v, _ := rhs.tree.Minimum()
+ r := v.([]*Prefix)
+ if l[0].AddressFamily != r[0].AddressFamily {
+ return fmt.Errorf("can't append different family")
+ }
+ }
rhs.tree.Walk(func(key string, v interface{}) bool {
w, ok := lhs.tree.Get(key)
if ok {
@@ -354,6 +365,8 @@ func (lhs *PrefixSet) Append(arg DefinedSet) error {
}
return false
})
+ _, w, _ := lhs.tree.Minimum()
+ lhs.family = w.([]*Prefix)[0].AddressFamily
return nil
}
@@ -398,6 +411,7 @@ func (lhs *PrefixSet) Replace(arg DefinedSet) error {
return fmt.Errorf("type cast failed")
}
lhs.tree = rhs.tree
+ lhs.family = rhs.family
return nil
}
@@ -441,7 +455,13 @@ func NewPrefixSetFromApiStruct(name string, prefixes []*Prefix) (*PrefixSet, err
return nil, fmt.Errorf("empty prefix set name")
}
tree := radix.New()
- for _, x := range prefixes {
+ var family bgp.RouteFamily
+ for i, x := range prefixes {
+ if i == 0 {
+ family = x.AddressFamily
+ } else if family != x.AddressFamily {
+ return nil, fmt.Errorf("multiple families")
+ }
key := CidrToRadixkey(x.Prefix.String())
d, ok := tree.Get(key)
if ok {
@@ -452,8 +472,9 @@ func NewPrefixSetFromApiStruct(name string, prefixes []*Prefix) (*PrefixSet, err
}
}
return &PrefixSet{
- name: name,
- tree: tree,
+ name: name,
+ tree: tree,
+ family: family,
}, nil
}
@@ -466,11 +487,17 @@ func NewPrefixSet(c config.PrefixSet) (*PrefixSet, error) {
return nil, fmt.Errorf("empty prefix set name")
}
tree := radix.New()
- for _, x := range c.PrefixList {
+ var family bgp.RouteFamily
+ for i, x := range c.PrefixList {
y, err := NewPrefix(x)
if err != nil {
return nil, err
}
+ if i == 0 {
+ family = y.AddressFamily
+ } else if family != y.AddressFamily {
+ return nil, fmt.Errorf("multiple families")
+ }
key := CidrToRadixkey(y.Prefix.String())
d, ok := tree.Get(key)
if ok {
@@ -481,8 +508,9 @@ func NewPrefixSet(c config.PrefixSet) (*PrefixSet, error) {
}
}
return &PrefixSet{
- name: name,
- tree: tree,
+ name: name,
+ tree: tree,
+ family: family,
}, nil
}
@@ -1210,7 +1238,8 @@ func (c *PrefixCondition) Evaluate(path *Path, _ *PolicyOptions) bool {
}
return buffer.String()[:ones]
}
- switch path.GetRouteFamily() {
+ family := path.GetRouteFamily()
+ switch family {
case bgp.RF_IPv4_UC:
masklen = path.GetNlri().(*bgp.IPAddrPrefix).Length
key = keyf(path.GetNlri().(*bgp.IPAddrPrefix).Prefix, int(masklen))
@@ -1220,6 +1249,9 @@ func (c *PrefixCondition) Evaluate(path *Path, _ *PolicyOptions) bool {
default:
return false
}
+ if family != c.set.family {
+ return false
+ }
result := false
_, ps, ok := c.set.tree.LongestPrefix(key)