summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--api/gobgp.pb.go283
-rw-r--r--api/gobgp.proto34
-rw-r--r--server/grpc_server.go64
-rw-r--r--server/peer.go57
-rw-r--r--server/server.go243
-rw-r--r--table/policy.go45
-rw-r--r--table/table_manager.go65
7 files changed, 393 insertions, 398 deletions
diff --git a/api/gobgp.pb.go b/api/gobgp.pb.go
index 53aa0f7b..95ed3410 100644
--- a/api/gobgp.pb.go
+++ b/api/gobgp.pb.go
@@ -12,12 +12,12 @@ It has these top-level messages:
Error
Arguments
ModPathArguments
- PolicyArguments
MrtArguments
ModVrfArguments
ModDefinedSetArguments
ModStatementArguments
ModPolicyArguments
+ ModPolicyAssignmentArguments
Path
Destination
PeerConf
@@ -34,7 +34,7 @@ It has these top-level messages:
Actions
Statement
Policy
- ApplyPolicy
+ PolicyAssignment
MrtMessage
RPKIConf
RPKIState
@@ -61,44 +61,26 @@ var _ = math.Inf
type Resource int32
const (
- Resource_GLOBAL Resource = 0
- Resource_LOCAL Resource = 1
- Resource_ADJ_IN Resource = 2
- Resource_ADJ_OUT Resource = 3
- Resource_POLICY_PREFIX Resource = 4
- Resource_POLICY_NEIGHBOR Resource = 5
- Resource_POLICY_ASPATH Resource = 6
- Resource_POLICY_COMMUNITY Resource = 7
- Resource_POLICY_ROUTEPOLICY Resource = 8
- Resource_POLICY_EXTCOMMUNITY Resource = 9
- Resource_VRF Resource = 10
+ Resource_GLOBAL Resource = 0
+ Resource_LOCAL Resource = 1
+ Resource_ADJ_IN Resource = 2
+ Resource_ADJ_OUT Resource = 3
+ Resource_VRF Resource = 4
)
var Resource_name = map[int32]string{
- 0: "GLOBAL",
- 1: "LOCAL",
- 2: "ADJ_IN",
- 3: "ADJ_OUT",
- 4: "POLICY_PREFIX",
- 5: "POLICY_NEIGHBOR",
- 6: "POLICY_ASPATH",
- 7: "POLICY_COMMUNITY",
- 8: "POLICY_ROUTEPOLICY",
- 9: "POLICY_EXTCOMMUNITY",
- 10: "VRF",
+ 0: "GLOBAL",
+ 1: "LOCAL",
+ 2: "ADJ_IN",
+ 3: "ADJ_OUT",
+ 4: "VRF",
}
var Resource_value = map[string]int32{
- "GLOBAL": 0,
- "LOCAL": 1,
- "ADJ_IN": 2,
- "ADJ_OUT": 3,
- "POLICY_PREFIX": 4,
- "POLICY_NEIGHBOR": 5,
- "POLICY_ASPATH": 6,
- "POLICY_COMMUNITY": 7,
- "POLICY_ROUTEPOLICY": 8,
- "POLICY_EXTCOMMUNITY": 9,
- "VRF": 10,
+ "GLOBAL": 0,
+ "LOCAL": 1,
+ "ADJ_IN": 2,
+ "ADJ_OUT": 3,
+ "VRF": 4,
}
func (x Resource) String() string {
@@ -233,33 +215,6 @@ func (m *ModPathArguments) GetPaths() []*Path {
return nil
}
-type PolicyArguments struct {
- Resource Resource `protobuf:"varint,1,opt,name=resource,enum=gobgpapi.Resource" json:"resource,omitempty"`
- Operation Operation `protobuf:"varint,2,opt,name=operation,enum=gobgpapi.Operation" json:"operation,omitempty"`
- NeighborAddress string `protobuf:"bytes,3,opt,name=neighbor_address" json:"neighbor_address,omitempty"`
- Name string `protobuf:"bytes,4,opt,name=name" json:"name,omitempty"`
- PolicyDefinition *Policy `protobuf:"bytes,6,opt,name=policy_definition" json:"policy_definition,omitempty"`
- ApplyPolicy *ApplyPolicy `protobuf:"bytes,7,opt,name=apply_policy" json:"apply_policy,omitempty"`
-}
-
-func (m *PolicyArguments) Reset() { *m = PolicyArguments{} }
-func (m *PolicyArguments) String() string { return proto.CompactTextString(m) }
-func (*PolicyArguments) ProtoMessage() {}
-
-func (m *PolicyArguments) GetPolicyDefinition() *Policy {
- if m != nil {
- return m.PolicyDefinition
- }
- return nil
-}
-
-func (m *PolicyArguments) GetApplyPolicy() *ApplyPolicy {
- if m != nil {
- return m.ApplyPolicy
- }
- return nil
-}
-
type MrtArguments struct {
Resource Resource `protobuf:"varint,1,opt,name=resource,enum=gobgpapi.Resource" json:"resource,omitempty"`
Rf uint32 `protobuf:"varint,2,opt,name=rf" json:"rf,omitempty"`
@@ -324,8 +279,7 @@ type ModPolicyArguments struct {
Policy *Policy `protobuf:"bytes,2,opt,name=policy" json:"policy,omitempty"`
// if this flag is set, gobgpd won't define new statements
// but refer existing statements using statement's names.
- // if you use this flag don't put any conditions/actions to
- // each statements and only set name.
+ // this flag only works with Operation_ADD
ReferExistingStatements bool `protobuf:"varint,3,opt,name=refer_existing_statements" json:"refer_existing_statements,omitempty"`
// if this flag is set, gobgpd won't delete any statements
// even if the policy containing some statements are deleted.
@@ -344,6 +298,22 @@ func (m *ModPolicyArguments) GetPolicy() *Policy {
return nil
}
+type ModPolicyAssignmentArguments struct {
+ Operation Operation `protobuf:"varint,1,opt,name=operation,enum=gobgpapi.Operation" json:"operation,omitempty"`
+ Assignment *PolicyAssignment `protobuf:"bytes,2,opt,name=assignment" json:"assignment,omitempty"`
+}
+
+func (m *ModPolicyAssignmentArguments) Reset() { *m = ModPolicyAssignmentArguments{} }
+func (m *ModPolicyAssignmentArguments) String() string { return proto.CompactTextString(m) }
+func (*ModPolicyAssignmentArguments) ProtoMessage() {}
+
+func (m *ModPolicyAssignmentArguments) GetAssignment() *PolicyAssignment {
+ if m != nil {
+ return m.Assignment
+ }
+ return nil
+}
+
type Path struct {
Nlri []byte `protobuf:"bytes,1,opt,name=nlri,proto3" json:"nlri,omitempty"`
Pattrs [][]byte `protobuf:"bytes,2,rep,name=pattrs,proto3" json:"pattrs,omitempty"`
@@ -659,15 +629,24 @@ func (m *Policy) GetStatements() []*Statement {
return nil
}
-type ApplyPolicy struct {
+type PolicyAssignment struct {
Type PolicyType `protobuf:"varint,1,opt,name=type,enum=gobgpapi.PolicyType" json:"type,omitempty"`
- Policies []string `protobuf:"bytes,2,rep,name=policies" json:"policies,omitempty"`
- Default RouteAction `protobuf:"varint,3,opt,name=default,enum=gobgpapi.RouteAction" json:"default,omitempty"`
+ Resource Resource `protobuf:"varint,2,opt,name=resource,enum=gobgpapi.Resource" json:"resource,omitempty"`
+ Name string `protobuf:"bytes,3,opt,name=name" json:"name,omitempty"`
+ Policies []*Policy `protobuf:"bytes,4,rep,name=policies" json:"policies,omitempty"`
+ Default RouteAction `protobuf:"varint,5,opt,name=default,enum=gobgpapi.RouteAction" json:"default,omitempty"`
}
-func (m *ApplyPolicy) Reset() { *m = ApplyPolicy{} }
-func (m *ApplyPolicy) String() string { return proto.CompactTextString(m) }
-func (*ApplyPolicy) ProtoMessage() {}
+func (m *PolicyAssignment) Reset() { *m = PolicyAssignment{} }
+func (m *PolicyAssignment) String() string { return proto.CompactTextString(m) }
+func (*PolicyAssignment) ProtoMessage() {}
+
+func (m *PolicyAssignment) GetPolicies() []*Policy {
+ if m != nil {
+ return m.Policies
+ }
+ return nil
+}
type MrtMessage struct {
Data []byte `protobuf:"bytes,1,opt,name=data,proto3" json:"data,omitempty"`
@@ -767,8 +746,6 @@ type GobgpApiClient interface {
Enable(ctx context.Context, in *Arguments, opts ...grpc.CallOption) (*Error, error)
Disable(ctx context.Context, in *Arguments, opts ...grpc.CallOption) (*Error, error)
ModPath(ctx context.Context, opts ...grpc.CallOption) (GobgpApi_ModPathClient, error)
- GetNeighborPolicy(ctx context.Context, in *PolicyArguments, opts ...grpc.CallOption) (*ApplyPolicy, error)
- ModNeighborPolicy(ctx context.Context, opts ...grpc.CallOption) (GobgpApi_ModNeighborPolicyClient, error)
MonitorBestChanged(ctx context.Context, in *Arguments, opts ...grpc.CallOption) (GobgpApi_MonitorBestChangedClient, error)
MonitorPeerState(ctx context.Context, in *Arguments, opts ...grpc.CallOption) (GobgpApi_MonitorPeerStateClient, error)
GetMrt(ctx context.Context, in *MrtArguments, opts ...grpc.CallOption) (GobgpApi_GetMrtClient, error)
@@ -785,6 +762,8 @@ type GobgpApiClient interface {
GetPolicy(ctx context.Context, in *Policy, opts ...grpc.CallOption) (*Policy, error)
GetPolicies(ctx context.Context, in *Policy, opts ...grpc.CallOption) (GobgpApi_GetPoliciesClient, error)
ModPolicy(ctx context.Context, in *ModPolicyArguments, opts ...grpc.CallOption) (*Error, error)
+ GetPolicyAssignment(ctx context.Context, in *PolicyAssignment, opts ...grpc.CallOption) (*PolicyAssignment, error)
+ ModPolicyAssignment(ctx context.Context, in *ModPolicyAssignmentArguments, opts ...grpc.CallOption) (*Error, error)
}
type gobgpApiClient struct {
@@ -965,48 +944,8 @@ func (x *gobgpApiModPathClient) CloseAndRecv() (*Error, error) {
return m, nil
}
-func (c *gobgpApiClient) GetNeighborPolicy(ctx context.Context, in *PolicyArguments, opts ...grpc.CallOption) (*ApplyPolicy, error) {
- out := new(ApplyPolicy)
- err := grpc.Invoke(ctx, "/gobgpapi.GobgpApi/GetNeighborPolicy", in, out, c.cc, opts...)
- if err != nil {
- return nil, err
- }
- return out, nil
-}
-
-func (c *gobgpApiClient) ModNeighborPolicy(ctx context.Context, opts ...grpc.CallOption) (GobgpApi_ModNeighborPolicyClient, error) {
- stream, err := grpc.NewClientStream(ctx, &_GobgpApi_serviceDesc.Streams[3], c.cc, "/gobgpapi.GobgpApi/ModNeighborPolicy", opts...)
- if err != nil {
- return nil, err
- }
- x := &gobgpApiModNeighborPolicyClient{stream}
- return x, nil
-}
-
-type GobgpApi_ModNeighborPolicyClient interface {
- Send(*PolicyArguments) error
- Recv() (*Error, error)
- grpc.ClientStream
-}
-
-type gobgpApiModNeighborPolicyClient struct {
- grpc.ClientStream
-}
-
-func (x *gobgpApiModNeighborPolicyClient) Send(m *PolicyArguments) error {
- return x.ClientStream.SendMsg(m)
-}
-
-func (x *gobgpApiModNeighborPolicyClient) Recv() (*Error, error) {
- m := new(Error)
- if err := x.ClientStream.RecvMsg(m); err != nil {
- return nil, err
- }
- return m, nil
-}
-
func (c *gobgpApiClient) MonitorBestChanged(ctx context.Context, in *Arguments, opts ...grpc.CallOption) (GobgpApi_MonitorBestChangedClient, error) {
- stream, err := grpc.NewClientStream(ctx, &_GobgpApi_serviceDesc.Streams[4], c.cc, "/gobgpapi.GobgpApi/MonitorBestChanged", opts...)
+ stream, err := grpc.NewClientStream(ctx, &_GobgpApi_serviceDesc.Streams[3], c.cc, "/gobgpapi.GobgpApi/MonitorBestChanged", opts...)
if err != nil {
return nil, err
}
@@ -1038,7 +977,7 @@ func (x *gobgpApiMonitorBestChangedClient) Recv() (*Destination, error) {
}
func (c *gobgpApiClient) MonitorPeerState(ctx context.Context, in *Arguments, opts ...grpc.CallOption) (GobgpApi_MonitorPeerStateClient, error) {
- stream, err := grpc.NewClientStream(ctx, &_GobgpApi_serviceDesc.Streams[5], c.cc, "/gobgpapi.GobgpApi/MonitorPeerState", opts...)
+ stream, err := grpc.NewClientStream(ctx, &_GobgpApi_serviceDesc.Streams[4], c.cc, "/gobgpapi.GobgpApi/MonitorPeerState", opts...)
if err != nil {
return nil, err
}
@@ -1070,7 +1009,7 @@ func (x *gobgpApiMonitorPeerStateClient) Recv() (*Peer, error) {
}
func (c *gobgpApiClient) GetMrt(ctx context.Context, in *MrtArguments, opts ...grpc.CallOption) (GobgpApi_GetMrtClient, error) {
- stream, err := grpc.NewClientStream(ctx, &_GobgpApi_serviceDesc.Streams[6], c.cc, "/gobgpapi.GobgpApi/GetMrt", opts...)
+ stream, err := grpc.NewClientStream(ctx, &_GobgpApi_serviceDesc.Streams[5], c.cc, "/gobgpapi.GobgpApi/GetMrt", opts...)
if err != nil {
return nil, err
}
@@ -1102,7 +1041,7 @@ func (x *gobgpApiGetMrtClient) Recv() (*MrtMessage, error) {
}
func (c *gobgpApiClient) GetRPKI(ctx context.Context, in *Arguments, opts ...grpc.CallOption) (GobgpApi_GetRPKIClient, error) {
- stream, err := grpc.NewClientStream(ctx, &_GobgpApi_serviceDesc.Streams[7], c.cc, "/gobgpapi.GobgpApi/GetRPKI", opts...)
+ stream, err := grpc.NewClientStream(ctx, &_GobgpApi_serviceDesc.Streams[6], c.cc, "/gobgpapi.GobgpApi/GetRPKI", opts...)
if err != nil {
return nil, err
}
@@ -1134,7 +1073,7 @@ func (x *gobgpApiGetRPKIClient) Recv() (*RPKI, error) {
}
func (c *gobgpApiClient) GetROA(ctx context.Context, in *Arguments, opts ...grpc.CallOption) (GobgpApi_GetROAClient, error) {
- stream, err := grpc.NewClientStream(ctx, &_GobgpApi_serviceDesc.Streams[8], c.cc, "/gobgpapi.GobgpApi/GetROA", opts...)
+ stream, err := grpc.NewClientStream(ctx, &_GobgpApi_serviceDesc.Streams[7], c.cc, "/gobgpapi.GobgpApi/GetROA", opts...)
if err != nil {
return nil, err
}
@@ -1166,7 +1105,7 @@ func (x *gobgpApiGetROAClient) Recv() (*ROA, error) {
}
func (c *gobgpApiClient) GetVrfs(ctx context.Context, in *Arguments, opts ...grpc.CallOption) (GobgpApi_GetVrfsClient, error) {
- stream, err := grpc.NewClientStream(ctx, &_GobgpApi_serviceDesc.Streams[9], c.cc, "/gobgpapi.GobgpApi/GetVrfs", opts...)
+ stream, err := grpc.NewClientStream(ctx, &_GobgpApi_serviceDesc.Streams[8], c.cc, "/gobgpapi.GobgpApi/GetVrfs", opts...)
if err != nil {
return nil, err
}
@@ -1216,7 +1155,7 @@ func (c *gobgpApiClient) GetDefinedSet(ctx context.Context, in *DefinedSet, opts
}
func (c *gobgpApiClient) GetDefinedSets(ctx context.Context, in *DefinedSet, opts ...grpc.CallOption) (GobgpApi_GetDefinedSetsClient, error) {
- stream, err := grpc.NewClientStream(ctx, &_GobgpApi_serviceDesc.Streams[10], c.cc, "/gobgpapi.GobgpApi/GetDefinedSets", opts...)
+ stream, err := grpc.NewClientStream(ctx, &_GobgpApi_serviceDesc.Streams[9], c.cc, "/gobgpapi.GobgpApi/GetDefinedSets", opts...)
if err != nil {
return nil, err
}
@@ -1266,7 +1205,7 @@ func (c *gobgpApiClient) GetStatement(ctx context.Context, in *Statement, opts .
}
func (c *gobgpApiClient) GetStatements(ctx context.Context, in *Statement, opts ...grpc.CallOption) (GobgpApi_GetStatementsClient, error) {
- stream, err := grpc.NewClientStream(ctx, &_GobgpApi_serviceDesc.Streams[11], c.cc, "/gobgpapi.GobgpApi/GetStatements", opts...)
+ stream, err := grpc.NewClientStream(ctx, &_GobgpApi_serviceDesc.Streams[10], c.cc, "/gobgpapi.GobgpApi/GetStatements", opts...)
if err != nil {
return nil, err
}
@@ -1316,7 +1255,7 @@ func (c *gobgpApiClient) GetPolicy(ctx context.Context, in *Policy, opts ...grpc
}
func (c *gobgpApiClient) GetPolicies(ctx context.Context, in *Policy, opts ...grpc.CallOption) (GobgpApi_GetPoliciesClient, error) {
- stream, err := grpc.NewClientStream(ctx, &_GobgpApi_serviceDesc.Streams[12], c.cc, "/gobgpapi.GobgpApi/GetPolicies", opts...)
+ stream, err := grpc.NewClientStream(ctx, &_GobgpApi_serviceDesc.Streams[11], c.cc, "/gobgpapi.GobgpApi/GetPolicies", opts...)
if err != nil {
return nil, err
}
@@ -1356,6 +1295,24 @@ func (c *gobgpApiClient) ModPolicy(ctx context.Context, in *ModPolicyArguments,
return out, nil
}
+func (c *gobgpApiClient) GetPolicyAssignment(ctx context.Context, in *PolicyAssignment, opts ...grpc.CallOption) (*PolicyAssignment, error) {
+ out := new(PolicyAssignment)
+ err := grpc.Invoke(ctx, "/gobgpapi.GobgpApi/GetPolicyAssignment", in, out, c.cc, opts...)
+ if err != nil {
+ return nil, err
+ }
+ return out, nil
+}
+
+func (c *gobgpApiClient) ModPolicyAssignment(ctx context.Context, in *ModPolicyAssignmentArguments, opts ...grpc.CallOption) (*Error, error) {
+ out := new(Error)
+ err := grpc.Invoke(ctx, "/gobgpapi.GobgpApi/ModPolicyAssignment", in, out, c.cc, opts...)
+ if err != nil {
+ return nil, err
+ }
+ return out, nil
+}
+
// Server API for GobgpApi service
type GobgpApiServer interface {
@@ -1370,8 +1327,6 @@ type GobgpApiServer interface {
Enable(context.Context, *Arguments) (*Error, error)
Disable(context.Context, *Arguments) (*Error, error)
ModPath(GobgpApi_ModPathServer) error
- GetNeighborPolicy(context.Context, *PolicyArguments) (*ApplyPolicy, error)
- ModNeighborPolicy(GobgpApi_ModNeighborPolicyServer) error
MonitorBestChanged(*Arguments, GobgpApi_MonitorBestChangedServer) error
MonitorPeerState(*Arguments, GobgpApi_MonitorPeerStateServer) error
GetMrt(*MrtArguments, GobgpApi_GetMrtServer) error
@@ -1388,6 +1343,8 @@ type GobgpApiServer interface {
GetPolicy(context.Context, *Policy) (*Policy, error)
GetPolicies(*Policy, GobgpApi_GetPoliciesServer) error
ModPolicy(context.Context, *ModPolicyArguments) (*Error, error)
+ GetPolicyAssignment(context.Context, *PolicyAssignment) (*PolicyAssignment, error)
+ ModPolicyAssignment(context.Context, *ModPolicyAssignmentArguments) (*Error, error)
}
func RegisterGobgpApiServer(s *grpc.Server, srv GobgpApiServer) {
@@ -1558,44 +1515,6 @@ func (x *gobgpApiModPathServer) Recv() (*ModPathArguments, error) {
return m, nil
}
-func _GobgpApi_GetNeighborPolicy_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error) (interface{}, error) {
- in := new(PolicyArguments)
- if err := dec(in); err != nil {
- return nil, err
- }
- out, err := srv.(GobgpApiServer).GetNeighborPolicy(ctx, in)
- if err != nil {
- return nil, err
- }
- return out, nil
-}
-
-func _GobgpApi_ModNeighborPolicy_Handler(srv interface{}, stream grpc.ServerStream) error {
- return srv.(GobgpApiServer).ModNeighborPolicy(&gobgpApiModNeighborPolicyServer{stream})
-}
-
-type GobgpApi_ModNeighborPolicyServer interface {
- Send(*Error) error
- Recv() (*PolicyArguments, error)
- grpc.ServerStream
-}
-
-type gobgpApiModNeighborPolicyServer struct {
- grpc.ServerStream
-}
-
-func (x *gobgpApiModNeighborPolicyServer) Send(m *Error) error {
- return x.ServerStream.SendMsg(m)
-}
-
-func (x *gobgpApiModNeighborPolicyServer) Recv() (*PolicyArguments, error) {
- m := new(PolicyArguments)
- if err := x.ServerStream.RecvMsg(m); err != nil {
- return nil, err
- }
- return m, nil
-}
-
func _GobgpApi_MonitorBestChanged_Handler(srv interface{}, stream grpc.ServerStream) error {
m := new(Arguments)
if err := stream.RecvMsg(m); err != nil {
@@ -1869,6 +1788,30 @@ func _GobgpApi_ModPolicy_Handler(srv interface{}, ctx context.Context, dec func(
return out, nil
}
+func _GobgpApi_GetPolicyAssignment_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error) (interface{}, error) {
+ in := new(PolicyAssignment)
+ if err := dec(in); err != nil {
+ return nil, err
+ }
+ out, err := srv.(GobgpApiServer).GetPolicyAssignment(ctx, in)
+ if err != nil {
+ return nil, err
+ }
+ return out, nil
+}
+
+func _GobgpApi_ModPolicyAssignment_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error) (interface{}, error) {
+ in := new(ModPolicyAssignmentArguments)
+ if err := dec(in); err != nil {
+ return nil, err
+ }
+ out, err := srv.(GobgpApiServer).ModPolicyAssignment(ctx, in)
+ if err != nil {
+ return nil, err
+ }
+ return out, nil
+}
+
var _GobgpApi_serviceDesc = grpc.ServiceDesc{
ServiceName: "gobgpapi.GobgpApi",
HandlerType: (*GobgpApiServer)(nil),
@@ -1906,10 +1849,6 @@ var _GobgpApi_serviceDesc = grpc.ServiceDesc{
Handler: _GobgpApi_Disable_Handler,
},
{
- MethodName: "GetNeighborPolicy",
- Handler: _GobgpApi_GetNeighborPolicy_Handler,
- },
- {
MethodName: "ModVrf",
Handler: _GobgpApi_ModVrf_Handler,
},
@@ -1937,6 +1876,14 @@ var _GobgpApi_serviceDesc = grpc.ServiceDesc{
MethodName: "ModPolicy",
Handler: _GobgpApi_ModPolicy_Handler,
},
+ {
+ MethodName: "GetPolicyAssignment",
+ Handler: _GobgpApi_GetPolicyAssignment_Handler,
+ },
+ {
+ MethodName: "ModPolicyAssignment",
+ Handler: _GobgpApi_ModPolicyAssignment_Handler,
+ },
},
Streams: []grpc.StreamDesc{
{
@@ -1955,12 +1902,6 @@ var _GobgpApi_serviceDesc = grpc.ServiceDesc{
ClientStreams: true,
},
{
- StreamName: "ModNeighborPolicy",
- Handler: _GobgpApi_ModNeighborPolicy_Handler,
- ServerStreams: true,
- ClientStreams: true,
- },
- {
StreamName: "MonitorBestChanged",
Handler: _GobgpApi_MonitorBestChanged_Handler,
ServerStreams: true,
diff --git a/api/gobgp.proto b/api/gobgp.proto
index a1cce0a2..1875d708 100644
--- a/api/gobgp.proto
+++ b/api/gobgp.proto
@@ -31,8 +31,6 @@ service GobgpApi {
rpc Enable(Arguments) returns (Error) {}
rpc Disable(Arguments) returns (Error) {}
rpc ModPath(stream ModPathArguments) returns (Error) {}
- rpc GetNeighborPolicy(PolicyArguments) returns (ApplyPolicy) {}
- rpc ModNeighborPolicy(stream PolicyArguments) returns (stream Error) {}
rpc MonitorBestChanged(Arguments) returns (stream Destination) {}
rpc MonitorPeerState(Arguments) returns (stream Peer) {}
rpc GetMrt(MrtArguments) returns (stream MrtMessage) {}
@@ -49,6 +47,8 @@ service GobgpApi {
rpc GetPolicy(Policy) returns (Policy) {}
rpc GetPolicies(Policy) returns (stream Policy) {}
rpc ModPolicy(ModPolicyArguments) returns (Error) {}
+ rpc GetPolicyAssignment(PolicyAssignment) returns (PolicyAssignment) {}
+ rpc ModPolicyAssignment(ModPolicyAssignmentArguments) returns (Error) {}
}
message Error {
@@ -72,15 +72,6 @@ message ModPathArguments {
repeated Path paths = 3;
}
-message PolicyArguments {
- Resource resource = 1;
- Operation operation = 2;
- string neighbor_address = 3;
- string name = 4;
- Policy policy_definition = 6;
- ApplyPolicy apply_policy = 7;
-}
-
message MrtArguments {
Resource resource = 1;
uint32 rf = 2;
@@ -116,18 +107,17 @@ message ModPolicyArguments {
bool preserve_statements = 4;
}
+message ModPolicyAssignmentArguments {
+ Operation operation = 1;
+ PolicyAssignment assignment = 2;
+}
+
enum Resource {
GLOBAL = 0;
LOCAL = 1;
ADJ_IN = 2;
ADJ_OUT = 3;
- POLICY_PREFIX = 4;
- POLICY_NEIGHBOR = 5;
- POLICY_ASPATH = 6;
- POLICY_COMMUNITY = 7;
- POLICY_ROUTEPOLICY = 8;
- POLICY_EXTCOMMUNITY = 9;
- VRF = 10;
+ VRF = 4;
}
enum Operation {
@@ -281,10 +271,12 @@ enum PolicyType {
EXPORT = 2;
}
-message ApplyPolicy {
+message PolicyAssignment {
PolicyType type = 1;
- repeated string policies = 2;
- RouteAction default = 3;
+ Resource resource = 2;
+ string name = 3;
+ repeated Policy policies = 4;
+ RouteAction default = 5;
}
message MrtMessage {
diff --git a/server/grpc_server.go b/server/grpc_server.go
index 0ff61b47..f6f233a0 100644
--- a/server/grpc_server.go
+++ b/server/grpc_server.go
@@ -40,8 +40,6 @@ const (
REQ_NEIGHBOR_SOFT_RESET_OUT
REQ_NEIGHBOR_ENABLE
REQ_NEIGHBOR_DISABLE
- REQ_NEIGHBOR_POLICY
- REQ_MOD_NEIGHBOR_POLICY
REQ_GLOBAL_RIB
REQ_MONITOR_GLOBAL_BEST_CHANGED
REQ_MONITOR_NEIGHBOR_PEER_STATE
@@ -53,13 +51,14 @@ const (
REQ_VRFS
REQ_VRF_MOD
REQ_MOD_PATH
- REQ_GLOBAL_POLICY
REQ_DEFINED_SET
REQ_MOD_DEFINED_SET
REQ_STATEMENT
REQ_MOD_STATEMENT
REQ_POLICY
REQ_MOD_POLICY
+ REQ_POLICY_ASSIGNMENT
+ REQ_MOD_POLICY_ASSIGNMENT
)
const GRPC_PORT = 8080
@@ -240,53 +239,6 @@ func (s *Server) ModPath(stream api.GobgpApi_ModPathServer) error {
return err
}
-func (s *Server) GetNeighborPolicy(ctx context.Context, arg *api.PolicyArguments) (*api.ApplyPolicy, error) {
- r := 0
- switch arg.Resource {
- case api.Resource_GLOBAL:
- r = REQ_GLOBAL_POLICY
- case api.Resource_LOCAL:
- r = REQ_NEIGHBOR_POLICY
- default:
- return nil, fmt.Errorf("unsupported resource: %s", arg.Resource)
- }
-
- req := NewGrpcRequest(r, arg.NeighborAddress, bgp.RouteFamily(0), arg)
- s.bgpServerCh <- req
-
- res := <-req.ResponseCh
- if err := res.Err(); err != nil {
- log.Debug(err.Error())
- return nil, err
- }
- return res.Data.(*api.ApplyPolicy), nil
-}
-
-func (s *Server) ModNeighborPolicy(stream api.GobgpApi_ModNeighborPolicyServer) error {
- for {
- arg, err := stream.Recv()
- if err == io.EOF {
- return nil
- } else if err != nil {
- return err
- }
-
- req := NewGrpcRequest(REQ_MOD_NEIGHBOR_POLICY, arg.NeighborAddress, bgp.RouteFamily(0), arg)
- s.bgpServerCh <- req
- res := <-req.ResponseCh
- if err := res.Err(); err != nil {
- log.Debug(err.Error())
- return err
- }
- err = stream.Send(&api.Error{
- Code: api.Error_SUCCESS,
- })
- if err != nil {
- return err
- }
- }
-}
-
func (s *Server) GetMrt(arg *api.MrtArguments, stream api.GobgpApi_GetMrtServer) error {
var reqType int
switch arg.Resource {
@@ -416,6 +368,18 @@ func (s *Server) ModPolicy(ctx context.Context, arg *api.ModPolicyArguments) (*a
return s.mod(REQ_MOD_POLICY, arg)
}
+func (s *Server) GetPolicyAssignment(ctx context.Context, arg *api.PolicyAssignment) (*api.PolicyAssignment, error) {
+ d, err := s.get(REQ_POLICY_ASSIGNMENT, arg)
+ if err != nil {
+ return nil, err
+ }
+ return d.(*api.PolicyAssignment), nil
+}
+
+func (s *Server) ModPolicyAssignment(ctx context.Context, arg *api.ModPolicyAssignmentArguments) (*api.Error, error) {
+ return s.mod(REQ_MOD_POLICY_ASSIGNMENT, arg)
+}
+
type GrpcRequest struct {
RequestType int
Name string
diff --git a/server/peer.go b/server/peer.go
index 71d07c25..cbc3005a 100644
--- a/server/peer.go
+++ b/server/peer.go
@@ -41,7 +41,7 @@ type Peer struct {
peerInfo *table.PeerInfo
outgoing chan *bgp.BGPMessage
inPolicies []*table.Policy
- defaultInPolicy config.DefaultPolicyType
+ defaultInPolicy table.RouteType
accepted uint32
staleAccepted bool
isConfederationMember bool
@@ -381,49 +381,52 @@ func (peer *Peer) ToApiStruct() *api.Peer {
}
}
-func (peer *Peer) setPolicy(policy map[string]*table.Policy) {
- policyConf := peer.conf.ApplyPolicy
- inPolicies := make([]*table.Policy, 0)
- for _, policyName := range policyConf.ApplyPolicyConfig.InPolicy {
- log.WithFields(log.Fields{
- "Topic": "Peer",
- "Key": peer.conf.NeighborConfig.NeighborAddress,
- "PolicyName": policyName,
- }).Info("in-policy installed")
- if pol, ok := policy[policyName]; ok {
- log.Debug("in policy : ", pol)
- inPolicies = append(inPolicies, pol)
+func (peer *Peer) GetPolicy(d table.PolicyDirection) []*table.Policy {
+ switch d {
+ case table.POLICY_DIRECTION_IN:
+ return peer.inPolicies
+ default:
+ if peer.localRib != nil {
+ return peer.localRib.GetPolicy(d)
}
}
- peer.inPolicies = inPolicies
- peer.defaultInPolicy = policyConf.ApplyPolicyConfig.DefaultInPolicy
- if peer.localRib != nil {
- peer.localRib.SetPolicy(policyConf, policy)
- }
+ return nil
}
-func (peer *Peer) GetPolicy(d table.PolicyDirection) []*table.Policy {
+func (peer *Peer) SetPolicy(d table.PolicyDirection, policies []*table.Policy) error {
switch d {
case table.POLICY_DIRECTION_IN:
- return peer.inPolicies
+ peer.inPolicies = policies
default:
- return peer.localRib.GetPolicy(d)
+ if peer.localRib != nil {
+ return peer.localRib.SetPolicy(d, policies)
+ }
}
+ return nil
}
func (peer *Peer) GetDefaultPolicy(d table.PolicyDirection) table.RouteType {
- var def config.DefaultPolicyType
switch d {
case table.POLICY_DIRECTION_IN:
- def = peer.defaultInPolicy
+ return peer.defaultInPolicy
default:
- return peer.localRib.GetDefaultPolicy(d)
+ if peer.localRib != nil {
+ return peer.localRib.GetDefaultPolicy(d)
+ }
}
+ return table.ROUTE_TYPE_NONE
+}
- if def == config.DEFAULT_POLICY_TYPE_ACCEPT_ROUTE {
- return table.ROUTE_TYPE_ACCEPT
+func (peer *Peer) SetDefaultPolicy(d table.PolicyDirection, typ table.RouteType) error {
+ switch d {
+ case table.POLICY_DIRECTION_IN:
+ peer.defaultInPolicy = typ
+ default:
+ if peer.localRib != nil {
+ return peer.localRib.SetDefaultPolicy(d, typ)
+ }
}
- return table.ROUTE_TYPE_REJECT
+ return nil
}
func (peer *Peer) ApplyPolicy(d table.PolicyDirection, paths []*table.Path) ([]*table.Path, []*table.Path) {
diff --git a/server/server.go b/server/server.go
index 171bee55..b92aec16 100644
--- a/server/server.go
+++ b/server/server.go
@@ -211,7 +211,9 @@ func (server *BgpServer) Serve() {
}(g.AfiSafis.AfiSafiList)
server.globalRib = table.NewTableManager(GLOBAL_RIB_NAME, rfList, g.MplsLabelRange.MinLabel, g.MplsLabelRange.MaxLabel)
-
+ if server.policy != nil {
+ server.setPolicyByConfig(server.globalRib, g.ApplyPolicy)
+ }
listenerMap := make(map[string]*net.TCPListener)
acceptCh := make(chan *net.TCPConn)
l4, err1 := listenAndAccept("tcp4", server.listenPort, acceptCh)
@@ -352,6 +354,7 @@ func (server *BgpServer) Serve() {
SetTcpMD5SigSockopts(listener(config.NeighborConfig.NeighborAddress), addr, config.NeighborConfig.AuthPassword)
peer := NewPeer(g, config)
+ server.setPolicyByConfig(peer, config.ApplyPolicy)
if peer.isRouteServerClient() {
pathList := make([]*table.Path, 0)
rfList := peer.configuredRFlist()
@@ -396,10 +399,8 @@ func (server *BgpServer) Serve() {
case config := <-server.updatedPeerCh:
addr := config.NeighborConfig.NeighborAddress.String()
peer := server.neighborMap[addr]
- if peer.isRouteServerClient() {
- peer.conf.ApplyPolicy = config.ApplyPolicy
- peer.setPolicy(server.policy.PolicyMap)
- }
+ peer.conf = config
+ server.setPolicyByConfig(peer, config.ApplyPolicy)
case e := <-incoming:
peer, found := server.neighborMap[e.MsgSrc]
if !found {
@@ -863,6 +864,21 @@ func (server *BgpServer) UpdatePolicy(policy config.RoutingPolicy) {
server.policyUpdateCh <- policy
}
+func (server *BgpServer) setPolicyByConfig(p policyPoint, c config.ApplyPolicy) {
+ for _, dir := range []table.PolicyDirection{table.POLICY_DIRECTION_IN, table.POLICY_DIRECTION_IMPORT, table.POLICY_DIRECTION_EXPORT} {
+ ps, def, err := server.policy.GetAssignmentFromConfig(dir, c)
+ if err != nil {
+ log.WithFields(log.Fields{
+ "Topic": "Policy",
+ "Dir": dir,
+ }).Errorf("failed to get policy info: %s", err)
+ continue
+ }
+ p.SetDefaultPolicy(dir, def)
+ p.SetPolicy(dir, ps)
+ }
+}
+
func (server *BgpServer) SetPolicy(pl config.RoutingPolicy) error {
p, err := table.NewRoutingPolicy(pl)
if err != nil {
@@ -873,7 +889,7 @@ func (server *BgpServer) SetPolicy(pl config.RoutingPolicy) error {
}
server.policy = p
if server.globalRib != nil {
- server.globalRib.SetPolicy(server.bgpConfig.Global.ApplyPolicy, server.policy.PolicyMap)
+ server.setPolicyByConfig(server.globalRib, server.bgpConfig.Global.ApplyPolicy)
}
return nil
}
@@ -885,7 +901,7 @@ func (server *BgpServer) handlePolicy(pl config.RoutingPolicy) {
"Topic": "Peer",
"Key": peer.conf.NeighborConfig.NeighborAddress,
}).Info("call set policy")
- peer.setPolicy(server.policy.PolicyMap)
+ server.setPolicyByConfig(peer, peer.conf.ApplyPolicy)
}
}
@@ -1547,89 +1563,6 @@ func (server *BgpServer) handleGrpc(grpcReq *GrpcRequest) []*SenderMsg {
result.Data = err
grpcReq.ResponseCh <- result
close(grpcReq.ResponseCh)
-
- case REQ_NEIGHBOR_POLICY, REQ_GLOBAL_POLICY:
- arg := grpcReq.Data.(*api.PolicyArguments)
- var names []string
- def := api.RouteAction_REJECT
- var applyPolicy config.ApplyPolicy
- switch grpcReq.RequestType {
- case REQ_NEIGHBOR_POLICY:
- peer, err := server.checkNeighborRequest(grpcReq)
- if err != nil {
- return msgs
- }
- applyPolicy = peer.conf.ApplyPolicy
- case REQ_GLOBAL_RIB:
- applyPolicy = server.bgpConfig.Global.ApplyPolicy
- }
- switch arg.ApplyPolicy.Type {
- case api.PolicyType_IMPORT:
- names = applyPolicy.ApplyPolicyConfig.ImportPolicy
- if applyPolicy.ApplyPolicyConfig.DefaultImportPolicy == config.DEFAULT_POLICY_TYPE_ACCEPT_ROUTE {
- def = api.RouteAction_ACCEPT
- }
- case api.PolicyType_EXPORT:
- names = applyPolicy.ApplyPolicyConfig.ExportPolicy
- if applyPolicy.ApplyPolicyConfig.DefaultExportPolicy == config.DEFAULT_POLICY_TYPE_ACCEPT_ROUTE {
- def = api.RouteAction_ACCEPT
- }
- case api.PolicyType_IN:
- names = applyPolicy.ApplyPolicyConfig.InPolicy
- if applyPolicy.ApplyPolicyConfig.DefaultInPolicy == config.DEFAULT_POLICY_TYPE_ACCEPT_ROUTE {
- def = api.RouteAction_ACCEPT
- }
- }
- result := &GrpcResponse{
- Data: &api.ApplyPolicy{
- Policies: names,
- Default: def,
- },
- }
- grpcReq.ResponseCh <- result
- close(grpcReq.ResponseCh)
- case REQ_MOD_NEIGHBOR_POLICY:
- peer, err := server.checkNeighborRequest(grpcReq)
- if err != nil {
- break
- }
- result := &GrpcResponse{}
- arg := grpcReq.Data.(*api.PolicyArguments)
- applyPolicy := peer.conf.ApplyPolicy.ApplyPolicyConfig
- def := config.DEFAULT_POLICY_TYPE_ACCEPT_ROUTE
- switch arg.Operation {
- case api.Operation_ADD:
- if arg.ApplyPolicy.Default != api.RouteAction_REJECT {
- def = config.DEFAULT_POLICY_TYPE_REJECT_ROUTE
- }
- switch arg.ApplyPolicy.Type {
- case api.PolicyType_IMPORT:
- applyPolicy.DefaultImportPolicy = def
- applyPolicy.ImportPolicy = arg.ApplyPolicy.Policies
- case api.PolicyType_EXPORT:
- applyPolicy.DefaultExportPolicy = def
- applyPolicy.ExportPolicy = arg.ApplyPolicy.Policies
- case api.PolicyType_IN:
- applyPolicy.DefaultInPolicy = def
- applyPolicy.InPolicy = arg.ApplyPolicy.Policies
- }
- case api.Operation_DEL:
- switch arg.ApplyPolicy.Type {
- case api.PolicyType_IMPORT:
- applyPolicy.DefaultImportPolicy = def
- applyPolicy.ImportPolicy = nil
- case api.PolicyType_EXPORT:
- applyPolicy.DefaultExportPolicy = def
- applyPolicy.ExportPolicy = nil
- case api.PolicyType_IN:
- applyPolicy.DefaultInPolicy = def
- applyPolicy.InPolicy = nil
- }
- }
- peer.setPolicy(server.policy.PolicyMap)
-
- grpcReq.ResponseCh <- result
- close(grpcReq.ResponseCh)
case REQ_DEFINED_SET:
if err := server.handleGrpcGetDefinedSet(grpcReq); err != nil {
grpcReq.ResponseCh <- &GrpcResponse{
@@ -1669,6 +1602,19 @@ func (server *BgpServer) handleGrpc(grpcReq *GrpcRequest) []*SenderMsg {
ResponseErr: err,
}
close(grpcReq.ResponseCh)
+ case REQ_POLICY_ASSIGNMENT:
+ if err := server.handleGrpcGetPolicyAssignment(grpcReq); err != nil {
+ grpcReq.ResponseCh <- &GrpcResponse{
+ ResponseErr: err,
+ }
+ }
+ close(grpcReq.ResponseCh)
+ case REQ_MOD_POLICY_ASSIGNMENT:
+ err := server.handleGrpcModPolicyAssignment(grpcReq)
+ grpcReq.ResponseCh <- &GrpcResponse{
+ ResponseErr: err,
+ }
+ close(grpcReq.ResponseCh)
case REQ_MONITOR_GLOBAL_BEST_CHANGED, REQ_MONITOR_NEIGHBOR_PEER_STATE:
server.broadcastReqs = append(server.broadcastReqs, grpcReq)
case REQ_MRT_GLOBAL_RIB, REQ_MRT_LOCAL_RIB:
@@ -1914,6 +1860,123 @@ func (server *BgpServer) handleGrpcModPolicy(grpcReq *GrpcRequest) error {
return err
}
+type policyPoint interface {
+ GetDefaultPolicy(table.PolicyDirection) table.RouteType
+ GetPolicy(table.PolicyDirection) []*table.Policy
+ SetDefaultPolicy(table.PolicyDirection, table.RouteType) error
+ SetPolicy(table.PolicyDirection, []*table.Policy) error
+}
+
+func (server *BgpServer) getPolicyInfo(a *api.PolicyAssignment) (policyPoint, table.PolicyDirection, error) {
+ switch a.Resource {
+ case api.Resource_GLOBAL:
+ switch a.Type {
+ case api.PolicyType_IMPORT:
+ return server.globalRib, table.POLICY_DIRECTION_IMPORT, nil
+ case api.PolicyType_EXPORT:
+ return server.globalRib, table.POLICY_DIRECTION_EXPORT, nil
+ default:
+ return nil, table.POLICY_DIRECTION_NONE, fmt.Errorf("invalid policy type")
+ }
+ case api.Resource_LOCAL:
+ peer, ok := server.neighborMap[a.Name]
+ if !ok {
+ return nil, table.POLICY_DIRECTION_NONE, fmt.Errorf("not found peer %s", a.Name)
+ }
+ switch a.Type {
+ case api.PolicyType_IN:
+ return peer, table.POLICY_DIRECTION_IN, nil
+ case api.PolicyType_IMPORT:
+ return peer, table.POLICY_DIRECTION_IMPORT, nil
+ case api.PolicyType_EXPORT:
+ return peer, table.POLICY_DIRECTION_EXPORT, nil
+ default:
+ return nil, table.POLICY_DIRECTION_NONE, fmt.Errorf("invalid policy type")
+ }
+ default:
+ return nil, table.POLICY_DIRECTION_NONE, fmt.Errorf("invalid resource type")
+ }
+
+}
+
+func (server *BgpServer) handleGrpcGetPolicyAssignment(grpcReq *GrpcRequest) error {
+ arg := grpcReq.Data.(*api.PolicyAssignment)
+ i, dir, err := server.getPolicyInfo(arg)
+ if err != nil {
+ return err
+ }
+ arg.Default = i.GetDefaultPolicy(dir).ToApiStruct()
+ ps := i.GetPolicy(dir)
+ arg.Policies = make([]*api.Policy, 0, len(ps))
+ for _, x := range ps {
+ arg.Policies = append(arg.Policies, x.ToApiStruct())
+ }
+ grpcReq.ResponseCh <- &GrpcResponse{
+ Data: arg,
+ }
+ return nil
+}
+
+func (server *BgpServer) handleGrpcModPolicyAssignment(grpcReq *GrpcRequest) error {
+ var err error
+ var dir table.PolicyDirection
+ var i policyPoint
+ arg := grpcReq.Data.(*api.ModPolicyAssignmentArguments)
+ assignment := arg.Assignment
+ i, dir, err = server.getPolicyInfo(assignment)
+ if err != nil {
+ return err
+ }
+ ps := make([]*table.Policy, 0, len(assignment.Policies))
+ for _, x := range assignment.Policies {
+ p, ok := server.policy.PolicyMap[x.Name]
+ if !ok {
+ return fmt.Errorf("not found policy %s", x.Name)
+ }
+ ps = append(ps, p)
+ }
+ cur := i.GetPolicy(dir)
+ switch arg.Operation {
+ case api.Operation_ADD, api.Operation_REPLACE:
+ if arg.Operation == api.Operation_REPLACE || cur == nil {
+ err = i.SetPolicy(dir, ps)
+ } else {
+ err = i.SetPolicy(dir, append(cur, ps...))
+ }
+ if err != nil {
+ return err
+ }
+ switch assignment.Default {
+ case api.RouteAction_ACCEPT:
+ err = i.SetDefaultPolicy(dir, table.ROUTE_TYPE_ACCEPT)
+ case api.RouteAction_REJECT:
+ err = i.SetDefaultPolicy(dir, table.ROUTE_TYPE_REJECT)
+ }
+ case api.Operation_DEL:
+ n := make([]*table.Policy, 0, len(cur)-len(ps))
+ for _, x := range ps {
+ found := false
+ for _, y := range cur {
+ if x.Name() == y.Name() {
+ found = true
+ break
+ }
+ }
+ if !found {
+ n = append(n, x)
+ }
+ }
+ err = i.SetPolicy(dir, n)
+ case api.Operation_DEL_ALL:
+ err = i.SetPolicy(dir, nil)
+ if err != nil {
+ return err
+ }
+ err = i.SetDefaultPolicy(dir, table.ROUTE_TYPE_NONE)
+ }
+ return err
+}
+
func (server *BgpServer) handleMrt(grpcReq *GrpcRequest) {
now := uint32(time.Now().Unix())
view := ""
diff --git a/table/policy.go b/table/policy.go
index 5829fc98..92273467 100644
--- a/table/policy.go
+++ b/table/policy.go
@@ -48,10 +48,21 @@ const (
ROUTE_TYPE_REJECT
)
+func (t RouteType) ToApiStruct() api.RouteAction {
+ switch t {
+ case ROUTE_TYPE_ACCEPT:
+ return api.RouteAction_ACCEPT
+ case ROUTE_TYPE_REJECT:
+ return api.RouteAction_REJECT
+ }
+ return api.RouteAction_NONE
+}
+
type PolicyDirection int
const (
- POLICY_DIRECTION_IMPORT PolicyDirection = iota
+ POLICY_DIRECTION_NONE PolicyDirection = iota
+ POLICY_DIRECTION_IMPORT
POLICY_DIRECTION_EXPORT
POLICY_DIRECTION_IN
)
@@ -2352,6 +2363,38 @@ type RoutingPolicy struct {
StatementMap map[string]*Statement
}
+func (r *RoutingPolicy) GetAssignmentFromConfig(dir PolicyDirection, a config.ApplyPolicy) ([]*Policy, RouteType, error) {
+ var names []string
+ var cdef config.DefaultPolicyType
+ def := ROUTE_TYPE_ACCEPT
+ c := a.ApplyPolicyConfig
+ switch dir {
+ case POLICY_DIRECTION_IN:
+ names = c.InPolicy
+ cdef = c.DefaultInPolicy
+ case POLICY_DIRECTION_IMPORT:
+ names = c.ImportPolicy
+ cdef = c.DefaultImportPolicy
+ case POLICY_DIRECTION_EXPORT:
+ names = c.ExportPolicy
+ cdef = c.DefaultExportPolicy
+ default:
+ return nil, def, fmt.Errorf("invalid policy direction")
+ }
+ if cdef == config.DEFAULT_POLICY_TYPE_REJECT_ROUTE {
+ def = ROUTE_TYPE_REJECT
+ }
+ ps := make([]*Policy, 0, len(names))
+ for _, name := range names {
+ p, ok := r.PolicyMap[name]
+ if !ok {
+ return nil, def, fmt.Errorf("not found policy %s", name)
+ }
+ ps = append(ps, p)
+ }
+ return ps, def, nil
+}
+
func (r *RoutingPolicy) InUse(d DefinedSet) bool {
name := d.Name()
for _, p := range r.PolicyMap {
diff --git a/table/table_manager.go b/table/table_manager.go
index a8d9e8ff..8ca127d6 100644
--- a/table/table_manager.go
+++ b/table/table_manager.go
@@ -19,7 +19,6 @@ import (
"bytes"
"fmt"
log "github.com/Sirupsen/logrus"
- "github.com/osrg/gobgp/config"
"github.com/osrg/gobgp/packet"
"net"
"reflect"
@@ -115,9 +114,9 @@ type TableManager struct {
maxLabel uint32
nextLabel uint32
importPolicies []*Policy
- defaultImportPolicy config.DefaultPolicyType
+ defaultImportPolicy RouteType
exportPolicies []*Policy
- defaultExportPolicy config.DefaultPolicyType
+ defaultExportPolicy RouteType
}
func NewTableManager(owner string, rfList []bgp.RouteFamily, minLabel, maxLabel uint32) *TableManager {
@@ -135,35 +134,6 @@ func NewTableManager(owner string, rfList []bgp.RouteFamily, minLabel, maxLabel
return t
}
-func (manager *TableManager) SetPolicy(c config.ApplyPolicy, p map[string]*Policy) {
- manager.defaultImportPolicy = c.ApplyPolicyConfig.DefaultImportPolicy
- manager.defaultExportPolicy = c.ApplyPolicyConfig.DefaultExportPolicy
- f := func(dir string, arg []string) []*Policy {
- ret := make([]*Policy, 0, len(arg))
- for _, name := range arg {
- pol, ok := p[name]
- if !ok {
- log.WithFields(log.Fields{
- "Topic": "table",
- "Key": manager.owner,
- "PolicyName": name,
- }).Warnf("not found %s. failed to set %s policy", name, dir)
- continue
- }
- ret = append(ret, pol)
- log.WithFields(log.Fields{
- "Topic": "table",
- "Key": manager.owner,
- "PolicyName": name,
- }).Infof("%s policy installed", dir)
- }
- return ret
- }
- manager.importPolicies = f("import", c.ApplyPolicyConfig.ImportPolicy)
- manager.exportPolicies = f("export", c.ApplyPolicyConfig.ExportPolicy)
-
-}
-
func (manager *TableManager) GetPolicy(d PolicyDirection) []*Policy {
switch d {
case POLICY_DIRECTION_IMPORT:
@@ -174,19 +144,38 @@ func (manager *TableManager) GetPolicy(d PolicyDirection) []*Policy {
return nil
}
+func (manager *TableManager) SetPolicy(d PolicyDirection, policies []*Policy) error {
+ switch d {
+ case POLICY_DIRECTION_IMPORT:
+ manager.importPolicies = policies
+ case POLICY_DIRECTION_EXPORT:
+ manager.exportPolicies = policies
+ default:
+ return fmt.Errorf("unsupported policy type: %d", d)
+ }
+ return nil
+}
+
func (manager *TableManager) GetDefaultPolicy(d PolicyDirection) RouteType {
- var def config.DefaultPolicyType
switch d {
case POLICY_DIRECTION_IMPORT:
- def = manager.defaultImportPolicy
+ return manager.defaultImportPolicy
case POLICY_DIRECTION_EXPORT:
- def = manager.defaultExportPolicy
+ return manager.defaultExportPolicy
}
+ return ROUTE_TYPE_NONE
+}
- if def == config.DEFAULT_POLICY_TYPE_ACCEPT_ROUTE {
- return ROUTE_TYPE_ACCEPT
+func (manager *TableManager) SetDefaultPolicy(d PolicyDirection, typ RouteType) error {
+ switch d {
+ case POLICY_DIRECTION_IMPORT:
+ manager.defaultImportPolicy = typ
+ case POLICY_DIRECTION_EXPORT:
+ manager.defaultExportPolicy = typ
+ default:
+ return fmt.Errorf("unsupported policy type: %d", d)
}
- return ROUTE_TYPE_REJECT
+ return nil
}
func (manager *TableManager) ApplyPolicy(d PolicyDirection, paths []*Path) []*Path {