diff options
author | Hiroshi Yokoi <yokoi.hiroshi@po.ntts.co.jp> | 2015-06-23 21:20:23 +0900 |
---|---|---|
committer | Hiroshi Yokoi <yokoi.hiroshi@po.ntts.co.jp> | 2015-07-01 14:20:46 +0900 |
commit | 8339ada8dfffd086a4fadc10e729bbe07d3e3e83 (patch) | |
tree | 346d82d52c71ce13006dca09dbc6b2ce857bc7eb /test | |
parent | a45849df3993acf57d410df81176cb795510db26 (diff) |
scenario_test: remove policy config part from quagga-rsconfig
Diffstat (limited to 'test')
-rw-r--r-- | test/scenario_test/docker_control.py | 87 | ||||
-rw-r--r-- | test/scenario_test/policy/policy_generator.go | 667 | ||||
-rw-r--r-- | test/scenario_test/quagga-rsconfig.go | 708 | ||||
-rw-r--r-- | test/scenario_test/route_server_policy_test.py | 236 |
4 files changed, 920 insertions, 778 deletions
diff --git a/test/scenario_test/docker_control.py b/test/scenario_test/docker_control.py index b1263a72..f3d24fdd 100644 --- a/test/scenario_test/docker_control.py +++ b/test/scenario_test/docker_control.py @@ -214,18 +214,19 @@ def docker_container_stop_exabgp(): local(cmd, capture=True) -def docker_containers_destroy(): +def docker_containers_destroy(gobgp_remove=True, confdir_remove=True): containers = docker_containers_get() for container in containers: if re.match(r'q[0-9][0-9]*', container) is not None: docker_container_stop_quagga(container) if container == GOBGP_CONTAINER_NAME: - docker_container_stop_gobgp() + docker_container_stop_gobgp(remove=gobgp_remove) if container == EXABGP_CONTAINER_NAME: docker_container_stop_exabgp() bridge_unsetting_for_docker_connection() - cmd = "rm -rf " + CONFIG_DIRR - local(cmd, capture=True) + if confdir_remove: + cmd = "rm -rf " + CONFIG_DIRR + local(cmd, capture=True) def docker_check_running(cname): @@ -314,7 +315,7 @@ def get_notification_from_exabgp_log(): return err_mgs -def make_config(quagga_num, go_path, bridge, peer_opts="", policy_pattern=""): +def make_config(quagga_num, go_path, bridge, peer_opts="", use_compiled=False): if go_path != "": print "specified go path is [ " + go_path + " ]." if os.path.isdir(go_path): @@ -323,29 +324,16 @@ def make_config(quagga_num, go_path, bridge, peer_opts="", policy_pattern=""): print "specified go path do not use." pwd = local("pwd", capture=True) - pp = '' - if policy_pattern: - pp = " -p " + policy_pattern - - cmd = go_path + "go run " + pwd + "/quagga-rsconfig.go -n " + str(quagga_num) +\ - " -c /tmp/gobgp -v " + IP_VERSION + pp +" -i " + bridge["BRIDGE_NAME"][-1] + " " + peer_opts - local(cmd, capture=True) - + tool = go_path + "go run " + pwd + "/quagga-rsconfig.go " + if use_compiled: + tool = pwd + "/quagga-rsconfig " -def make_config_with_policy(quagga_num, go_path, bridge, peer_opts="", policy_pattern=""): - if go_path != "": - print "specified go path is [ " + go_path + " ]." - if os.path.isdir(go_path): - go_path += "/" - else: - print "specified go path is not used." - pwd = local("pwd", capture=True) - cmd = go_path + "go run " + pwd + "/quagga-rsconfig.go "+" -p "+ policy_pattern + " -n " + str(quagga_num) + \ + cmd = tool + " -n " + str(quagga_num) +\ " -c /tmp/gobgp -v " + IP_VERSION + " -i " + bridge["BRIDGE_NAME"][-1] + " " + peer_opts local(cmd, capture=True) -def update_policy_config(go_path, policy_pattern=""): +def update_policy_config(go_path, neighbor, policy_name, target, isReplace=False): if go_path != "": print "specified go path is [ " + go_path + " ]." if os.path.isdir(go_path): @@ -354,13 +342,13 @@ def update_policy_config(go_path, policy_pattern=""): print "specified go path is not used." pwd = local("pwd", capture=True) - cmd = go_path + "go run " + pwd + "/quagga-rsconfig.go --update-policy "+" -p "+ policy_pattern + \ - " -c /tmp/gobgp " + replace = ' -r' if isReplace else '' + cmd = pwd + "/policy_generator -d /tmp/gobgp -n " + neighbor + " -t " + target + " -p " + policy_name + replace local(cmd, capture=True) - reload_config() + # reload_config() -def make_config_append(quagga_num, go_path, bridge, peer_opts="", policy_pattern=""): +def make_config_append(quagga_num, go_path, bridge, peer_opts=""): if go_path != "": print "specified go path is [ " + go_path + " ]." if os.path.isdir(go_path): @@ -369,12 +357,8 @@ def make_config_append(quagga_num, go_path, bridge, peer_opts="", policy_pattern print "specified go path do not use." pwd = local("pwd", capture=True) - pp = '' - if policy_pattern: - pp = " -p " + policy_pattern - cmd = go_path + "go run " + pwd + "/quagga-rsconfig.go -a " + str(quagga_num) +\ - " -c /tmp/gobgp -v " + IP_VERSION + pp +" -i " + bridge["BRIDGE_NAME"][-1] + " " + peer_opts + " -c /tmp/gobgp -v " + IP_VERSION + " -i " + bridge["BRIDGE_NAME"][-1] + " " + peer_opts local(cmd, capture=True) @@ -384,6 +368,21 @@ def reload_config(): print "gobgp config reloaded." +def build_config_tools(go_path): + if go_path != "": + print "specified go path is [ " + go_path + " ]." + if os.path.isdir(go_path): + go_path += "/" + else: + print "the specified go path is not directory.." + + pwd = local("pwd", capture=True) + cmd = go_path + "go build " + pwd + "/quagga-rsconfig.go" + local(cmd, capture=True) + cmd = go_path + "go build " + pwd + "/policy_generator.go" + local(cmd, capture=True) + + def prepare_gobgp(log_debug, use_local): # cleanup gobgp container @@ -465,42 +464,20 @@ def init_test_env_executor(quagga_num, use_local, go_path, log_debug=False, is_r print "complete initialization of test environment." -def init_policy_test_env_executor(quagga_num, use_local, go_path, log_debug=False, policy="", - use_ipv6=False, use_exabgp=False): +def init_policy_test_env_executor(quagga_num, use_ipv6=False, use_exabgp=False): print "start initialization of test environment." - if docker_container_check() or bridge_setting_check(): - print "gobgp test environment already exists. clean up..." - containers = docker_containers_get() - for container in containers: - if re.match(r'q[0-9][0-9]*', container) is not None: - docker_container_stop_quagga(container) - if container == GOBGP_CONTAINER_NAME: - docker_container_stop_gobgp(remove=False) - if container == EXABGP_CONTAINER_NAME: - docker_container_stop_exabgp() - bridge_unsetting_for_docker_connection() - # cmd = "rm -rf " + CONFIG_DIRR - # local(cmd, capture=True) - - # print "make gobgp policy test environment." - # create_config_dir() - global IP_VERSION IP_VERSION = IPv6 if use_ipv6 else IPv4 bridge_setting_for_docker_connection(BRIDGES) - make_config(quagga_num, go_path, BRIDGE_0, policy_pattern=policy) - # run gobgp docker container - # docker_container_run_gobgp(BRIDGE_0) cmd = "docker start %s" % GOBGP_CONTAINER_NAME local(cmd, capture=True) docker_container_set_ipaddress(BRIDGE_0, GOBGP_CONTAINER_NAME, GOBGP_ADDRESS_0[IP_VERSION] + BASE_MASK[IP_VERSION]) if use_exabgp: # run exabgp - make_config_append(100, go_path, BRIDGE_0, peer_opts="--none-peer", policy_pattern=policy) start_exabgp(EXABGP_COMMON_CONF) start_gobgp() diff --git a/test/scenario_test/policy/policy_generator.go b/test/scenario_test/policy/policy_generator.go new file mode 100644 index 00000000..f72eb5c8 --- /dev/null +++ b/test/scenario_test/policy/policy_generator.go @@ -0,0 +1,667 @@ +package main + +import ( + "bytes" + "fmt" + "github.com/BurntSushi/toml" + "github.com/jessevdk/go-flags" + "github.com/osrg/gobgp/config" + "io/ioutil" + "log" + "net" + "os" +) + + +func bindPolicy(outputDir, peer, target, policyName string, isReplace bool) { + + newConf := config.Bgp{} + _, d_err := toml.DecodeFile(fmt.Sprintf("%s/gobgpd.conf", outputDir), &newConf) + if d_err != nil { + log.Fatal(d_err) + } + + for idx, neighbor := range newConf.NeighborList { + ip := net.ParseIP(peer) + + if ip.String() == neighbor.NeighborAddress.String() { + ap := &neighbor.ApplyPolicy + switch target { + case "import": + if isReplace { + ap.ImportPolicies = []string{policyName} + } else { + ap.ImportPolicies = append(ap.ImportPolicies, policyName) + } + case "export": + if isReplace { + ap.ExportPolicies = []string{policyName} + } else { + ap.ExportPolicies = append(ap.ExportPolicies, policyName) + } + case "distribute": + if isReplace { + ap.DistributePolicies = []string{policyName} + } else { + ap.DistributePolicies = append(ap.DistributePolicies, policyName) + } + } + newConf.NeighborList[idx] = neighbor + } + } + + policyConf := createPolicyConfig() + var buffer bytes.Buffer + encoder := toml.NewEncoder(&buffer) + encoder.Encode(newConf) + encoder.Encode(policyConf) + + e_err := ioutil.WriteFile(fmt.Sprintf("%s/gobgpd.conf", outputDir), buffer.Bytes(), 0644) + if e_err != nil { + log.Fatal(e_err) + } +} + +func createPolicyConfig() *config.RoutingPolicy { + + ps0 := config.PrefixSet{ + PrefixSetName: "ps0", + PrefixList: []config.Prefix{ + config.Prefix{ + Address: net.ParseIP("192.168.0.0"), + Masklength: 16, + MasklengthRange: "16..24", + }}, + } + + ps1 := config.PrefixSet{ + PrefixSetName: "ps1", + PrefixList: []config.Prefix{ + config.Prefix{ + Address: net.ParseIP("192.168.20.0"), + Masklength: 24, + }, config.Prefix{ + Address: net.ParseIP("192.168.200.0"), + Masklength: 24, + }}, + } + + ps2 := config.PrefixSet{ + PrefixSetName: "ps2", + PrefixList: []config.Prefix{ + config.Prefix{ + Address: net.ParseIP("192.168.20.0"), + Masklength: 24, + }}, + } + + ps3 := config.PrefixSet{ + PrefixSetName: "ps3", + PrefixList: []config.Prefix{ + config.Prefix{ + Address: net.ParseIP("2001:0:10:2::"), + Masklength: 64, + MasklengthRange: "64..128", + }}, + } + + ps4 := config.PrefixSet{ + PrefixSetName: "ps4", + PrefixList: []config.Prefix{ + config.Prefix{ + Address: net.ParseIP("2001:0:10:20::"), + Masklength: 64, + }, config.Prefix{ + Address: net.ParseIP("2001:0:10:200::"), + Masklength: 64, + }}, + } + + ps5 := config.PrefixSet{ + PrefixSetName: "ps5", + PrefixList: []config.Prefix{ + config.Prefix{ + Address: net.ParseIP("2001:0:10:20::"), + Masklength: 64, + }}, + } + + nsPeer2 := config.NeighborSet{ + NeighborSetName: "nsPeer2", + NeighborInfoList: []config.NeighborInfo{ + config.NeighborInfo{ + Address: net.ParseIP("10.0.0.2"), + }}, + } + + nsPeer2V6 := config.NeighborSet{ + NeighborSetName: "nsPeer2V6", + NeighborInfoList: []config.NeighborInfo{ + config.NeighborInfo{ + Address: net.ParseIP("2001::0:192:168:0:2"), + }}, + } + + nsExabgp := config.NeighborSet{ + NeighborSetName: "nsExabgp", + NeighborInfoList: []config.NeighborInfo{ + config.NeighborInfo{ + Address: net.ParseIP("10.0.0.100"), + }}, + } + + psExabgp := config.PrefixSet{ + PrefixSetName: "psExabgp", + PrefixList: []config.Prefix{ + config.Prefix{ + Address: net.ParseIP("192.168.100.0"), + Masklength: 24, + MasklengthRange: "16..24", + }}, + } + + aspathFrom := config.AsPathSet{ + AsPathSetName: "aspathFrom", + AsPathSetMembers: []string{"^65100"}, + } + + aspathAny := config.AsPathSet{ + AsPathSetName: "aspAny", + AsPathSetMembers: []string{"65098"}, + } + + aspathOrigin := config.AsPathSet{ + AsPathSetName: "aspOrigin", + AsPathSetMembers: []string{"65091$"}, + } + + aspathOnly := config.AsPathSet{ + AsPathSetName: "aspOnly", + AsPathSetMembers: []string{"^65100$"}, + } + + comStr := config.CommunitySet{ + CommunitySetName: "comStr", + CommunityMembers: []string{"65100:10"}, + } + + comRegExp := config.CommunitySet{ + CommunitySetName: "comRegExp", + CommunityMembers: []string{"6[0-9]+:[0-9]+"}, + } + + st0 := config.Statement{ + Name: "st0", + Conditions: config.Conditions{ + MatchPrefixSet: "ps0", + MatchNeighborSet: "nsPeer2", + MatchSetOptions: config.MATCH_SET_OPTIONS_TYPE_ALL, + }, + Actions: config.Actions{ + AcceptRoute: false, + }, + } + + st1 := config.Statement{ + Name: "st1", + Conditions: config.Conditions{ + MatchPrefixSet: "ps1", + MatchNeighborSet: "nsPeer2", + MatchSetOptions: config.MATCH_SET_OPTIONS_TYPE_ALL, + }, + Actions: config.Actions{ + AcceptRoute: false, + }, + } + + st2 := config.Statement{ + Name: "st2", + Conditions: config.Conditions{ + MatchPrefixSet: "ps2", + MatchNeighborSet: "nsPeer2", + MatchSetOptions: config.MATCH_SET_OPTIONS_TYPE_ALL, + }, + Actions: config.Actions{ + AcceptRoute: false, + RejectRoute: true, + }, + } + + st3 := config.Statement{ + Name: "st3", + Conditions: config.Conditions{ + MatchPrefixSet: "ps3", + MatchNeighborSet: "nsPeer2V6", + MatchSetOptions: config.MATCH_SET_OPTIONS_TYPE_ALL, + }, + Actions: config.Actions{ + AcceptRoute: false, + }, + } + + st4 := config.Statement{ + Name: "st4", + Conditions: config.Conditions{ + MatchPrefixSet: "ps4", + MatchNeighborSet: "nsPeer2V6", + MatchSetOptions: config.MATCH_SET_OPTIONS_TYPE_ALL, + }, + Actions: config.Actions{ + AcceptRoute: false, + }, + } + + st5 := config.Statement{ + Name: "st5", + Conditions: config.Conditions{ + MatchPrefixSet: "ps5", + MatchNeighborSet: "nsPeer2V6", + MatchSetOptions: config.MATCH_SET_OPTIONS_TYPE_ALL, + }, + Actions: config.Actions{ + AcceptRoute: false, + RejectRoute: true, + }, + } + + st_aspathlen := config.Statement{ + Name: "st_aspathlen", + Conditions: config.Conditions{ + MatchPrefixSet: "psExabgp", + MatchNeighborSet: "nsExabgp", + BgpConditions: config.BgpConditions{ + AsPathLength: config.AsPathLength{ + Operator: "ge", + Value: 10, + }, + }, + MatchSetOptions: config.MATCH_SET_OPTIONS_TYPE_ALL, + }, + Actions: config.Actions{ + AcceptRoute: false, + }, + } + + st_aspathFrom := config.Statement{ + Name: "st_aspathlen", + Conditions: config.Conditions{ + MatchPrefixSet: "psExabgp", + MatchNeighborSet: "nsExabgp", + BgpConditions: config.BgpConditions{ + MatchAsPathSet: "aspathFrom", + }, + MatchSetOptions: config.MATCH_SET_OPTIONS_TYPE_ALL, + }, + Actions: config.Actions{ + AcceptRoute: false, + }, + } + + st_aspathAny := config.Statement{ + Name: "st_aspathlen", + Conditions: config.Conditions{ + MatchPrefixSet: "psExabgp", + MatchNeighborSet: "nsExabgp", + BgpConditions: config.BgpConditions{ + MatchAsPathSet: "aspAny", + }, + MatchSetOptions: config.MATCH_SET_OPTIONS_TYPE_ALL, + }, + Actions: config.Actions{ + AcceptRoute: false, + }, + } + + st_aspathOrigin := config.Statement{ + Name: "st_aspathlen", + Conditions: config.Conditions{ + MatchPrefixSet: "psExabgp", + MatchNeighborSet: "nsExabgp", + BgpConditions: config.BgpConditions{ + MatchAsPathSet: "aspOrigin", + }, + MatchSetOptions: config.MATCH_SET_OPTIONS_TYPE_ALL, + }, + Actions: config.Actions{ + AcceptRoute: false, + }, + } + + st_aspathOnly := config.Statement{ + Name: "st_aspathlen", + Conditions: config.Conditions{ + MatchPrefixSet: "psExabgp", + MatchNeighborSet: "nsExabgp", + BgpConditions: config.BgpConditions{ + MatchAsPathSet: "aspOnly", + }, + MatchSetOptions: config.MATCH_SET_OPTIONS_TYPE_ALL, + }, + Actions: config.Actions{ + AcceptRoute: false, + }, + } + + st_comStr := config.Statement{ + Name: "st_community", + Conditions: config.Conditions{ + MatchPrefixSet: "psExabgp", + MatchNeighborSet: "nsExabgp", + BgpConditions: config.BgpConditions{ + MatchCommunitySet: "comStr", + }, + MatchSetOptions: config.MATCH_SET_OPTIONS_TYPE_ALL, + }, + Actions: config.Actions{ + AcceptRoute: false, + }, + } + + st_comRegExp := config.Statement{ + Name: "st_community_regexp", + Conditions: config.Conditions{ + MatchPrefixSet: "psExabgp", + MatchNeighborSet: "nsExabgp", + BgpConditions: config.BgpConditions{ + MatchCommunitySet: "comRegExp", + }, + MatchSetOptions: config.MATCH_SET_OPTIONS_TYPE_ALL, + }, + Actions: config.Actions{ + AcceptRoute: false, + }, + } + + st_comAdd := config.Statement{ + Name: "st_community_regexp", + Conditions: config.Conditions{ + MatchPrefixSet: "psExabgp", + MatchNeighborSet: "nsExabgp", + BgpConditions: config.BgpConditions{ + MatchCommunitySet: "comStr", + }, + MatchSetOptions: config.MATCH_SET_OPTIONS_TYPE_ALL, + }, + Actions: config.Actions{ + AcceptRoute: true, + BgpActions: config.BgpActions{ + SetCommunity: config.SetCommunity{ + Communities: []string{"65100:20"}, + Options: "ADD", + }, + }, + }, + } + + st_comReplace := config.Statement{ + Name: "st_community_regexp", + Conditions: config.Conditions{ + MatchPrefixSet: "psExabgp", + MatchNeighborSet: "nsExabgp", + BgpConditions: config.BgpConditions{ + MatchCommunitySet: "comStr", + }, + MatchSetOptions: config.MATCH_SET_OPTIONS_TYPE_ALL, + }, + Actions: config.Actions{ + AcceptRoute: true, + BgpActions: config.BgpActions{ + SetCommunity: config.SetCommunity{ + Communities: []string{"65100:20", "65100:30"}, + Options: "REPLACE", + }, + }, + }, + } + + st_comRemove := config.Statement{ + Name: "st_community_regexp", + Conditions: config.Conditions{ + MatchPrefixSet: "psExabgp", + MatchNeighborSet: "nsExabgp", + BgpConditions: config.BgpConditions{ + MatchCommunitySet: "comStr", + }, + MatchSetOptions: config.MATCH_SET_OPTIONS_TYPE_ALL, + }, + Actions: config.Actions{ + AcceptRoute: true, + BgpActions: config.BgpActions{ + SetCommunity: config.SetCommunity{ + Communities: []string{"65100:20", "65100:30"}, + Options: "REMOVE", + }, + }, + }, + } + + st_comNull := config.Statement{ + Name: "st_community_regexp", + Conditions: config.Conditions{ + MatchPrefixSet: "psExabgp", + MatchNeighborSet: "nsExabgp", + BgpConditions: config.BgpConditions{ + MatchCommunitySet: "comStr", + }, + MatchSetOptions: config.MATCH_SET_OPTIONS_TYPE_ALL, + }, + Actions: config.Actions{ + AcceptRoute: true, + BgpActions: config.BgpActions{ + SetCommunity: config.SetCommunity{ + Communities: []string{}, + Options: "NULL", + }, + }, + }, + } + + + test_01_import_policy_initial := config.PolicyDefinition{ + Name: "test_01_import_policy_initial", + StatementList: []config.Statement{st0}, + } + + test_02_export_policy_initial := config.PolicyDefinition{ + Name: "test_02_export_policy_initial", + StatementList: []config.Statement{st0}, + } + + test_03_import_policy_update := config.PolicyDefinition{ + Name: "test_03_import_policy_update", + StatementList: []config.Statement{st1}, + } + + test_03_import_policy_update_softreset := config.PolicyDefinition{ + Name: "test_03_import_policy_update_softreset", + StatementList: []config.Statement{st2}, + } + + test_04_export_policy_update := config.PolicyDefinition{ + Name: "test_04_export_policy_update", + StatementList: []config.Statement{st1}, + } + + test_04_export_policy_update_softreset := config.PolicyDefinition{ + Name: "test_04_export_policy_update_softreset", + StatementList: []config.Statement{st2}, + } + + test_05_import_policy_initial_ipv6 := config.PolicyDefinition{ + Name: "test_05_import_policy_initial_ipv6", + StatementList: []config.Statement{st3}, + } + + test_06_export_policy_initial_ipv6 := config.PolicyDefinition{ + Name: "test_06_export_policy_initial_ipv6", + StatementList: []config.Statement{st3}, + } + + test_07_import_policy_update := config.PolicyDefinition{ + Name: "test_07_import_policy_update", + StatementList: []config.Statement{st4}, + } + + test_07_import_policy_update_softreset := config.PolicyDefinition{ + Name: "test_07_import_policy_update_softreset", + StatementList: []config.Statement{st5}, + } + + test_08_export_policy_update := config.PolicyDefinition{ + Name: "test_08_export_policy_update", + StatementList: []config.Statement{st4}, + } + + test_08_export_policy_update_softreset := config.PolicyDefinition{ + Name: "test_08_export_policy_update_softreset", + StatementList: []config.Statement{st5}, + } + + test_09_aspath_length_condition_import := config.PolicyDefinition{ + Name: "test_09_aspath_length_condition_import", + StatementList: []config.Statement{st_aspathlen}, + } + + test_10_aspath_from_condition_import := config.PolicyDefinition{ + Name: "test_10_aspath_from_condition_import", + StatementList: []config.Statement{st_aspathFrom}, + } + + test_11_aspath_any_condition_import := config.PolicyDefinition{ + Name: "test_11_aspath_any_condition_import", + StatementList: []config.Statement{st_aspathAny}, + } + + test_12_aspath_origin_condition_import := config.PolicyDefinition{ + Name: "test_12_aspath_origin_condition_import", + StatementList: []config.Statement{st_aspathOrigin}, + } + + test_13_aspath_only_condition_import := config.PolicyDefinition{ + Name: "test_13_aspath_only_condition_import", + StatementList: []config.Statement{st_aspathOnly}, + } + + test_14_aspath_only_condition_import := config.PolicyDefinition{ + Name: "test_14_aspath_only_condition_import", + StatementList: []config.Statement{st_comStr}, + } + + test_15_community_condition_import := config.PolicyDefinition{ + Name: "test_15_community_condition_import", + StatementList: []config.Statement{st_comStr}, + } + + test_16_community_condition_regexp_import := config.PolicyDefinition{ + Name: "test_16_community_condition_regexp_import", + StatementList: []config.Statement{st_comRegExp}, + } + + test_17_community_add_action_import := config.PolicyDefinition{ + Name: "test_17_community_add_action_import", + StatementList: []config.Statement{st_comAdd}, + } + + test_18_community_replace_action_import := config.PolicyDefinition{ + Name: "test_18_community_replace_action_import", + StatementList: []config.Statement{st_comReplace}, + } + + test_19_community_remove_action_import := config.PolicyDefinition{ + Name: "test_19_community_remove_action_import", + StatementList: []config.Statement{st_comRemove}, + } + + test_20_community_null_action_import := config.PolicyDefinition{ + Name: "test_20_community_null_action_import", + StatementList: []config.Statement{st_comNull}, + } + + test_21_community_add_action_export := config.PolicyDefinition{ + Name: "test_21_community_add_action_export", + StatementList: []config.Statement{st_comAdd}, + } + + test_22_community_replace_action_export := config.PolicyDefinition{ + Name: "test_22_community_replace_action_export", + StatementList: []config.Statement{st_comReplace}, + } + + test_23_community_remove_action_export := config.PolicyDefinition{ + Name: "test_23_community_remove_action_export", + StatementList: []config.Statement{st_comRemove}, + } + + test_24_community_null_action_export := config.PolicyDefinition{ + Name: "test_24_community_null_action_export", + StatementList: []config.Statement{st_comNull}, + } + + ds := config.DefinedSets{ + PrefixSetList: []config.PrefixSet{ps0, ps1, ps2, ps3, ps4, ps5, psExabgp}, + NeighborSetList: []config.NeighborSet{nsPeer2, nsPeer2V6, nsExabgp}, + BgpDefinedSets: config.BgpDefinedSets{ + AsPathSetList: []config.AsPathSet{aspathFrom, aspathAny, aspathOrigin, aspathOnly}, + CommunitySetList: []config.CommunitySet{comStr, comRegExp}, + }, + } + + p := &config.RoutingPolicy{ + DefinedSets: ds, + PolicyDefinitionList: []config.PolicyDefinition{ + test_01_import_policy_initial, + test_02_export_policy_initial, + test_03_import_policy_update, + test_03_import_policy_update_softreset, + test_04_export_policy_update, + test_04_export_policy_update_softreset, + test_05_import_policy_initial_ipv6, + test_06_export_policy_initial_ipv6, + test_07_import_policy_update, + test_07_import_policy_update_softreset, + test_08_export_policy_update, + test_08_export_policy_update_softreset, + test_09_aspath_length_condition_import, + test_10_aspath_from_condition_import, + test_11_aspath_any_condition_import, + test_12_aspath_origin_condition_import, + test_13_aspath_only_condition_import, + test_14_aspath_only_condition_import, + test_15_community_condition_import, + test_16_community_condition_regexp_import, + test_17_community_add_action_import, + test_18_community_replace_action_import, + test_19_community_remove_action_import, + test_20_community_null_action_import, + test_21_community_add_action_export, + test_22_community_replace_action_export, + test_23_community_remove_action_export, + test_24_community_null_action_export, + }, + } + return p +} + + +func main() { + var opts struct { + OutputDir string `short:"d" long:"output" description:"specifing the output directory"` + Neighbor string `short:"n" long:"neighbor" description:"neighbor ip adress to which add policy config"` + Target string `short:"t" long:"target" description:"target such as export or import to which add policy"` + PolicyName string `short:"p" long:"policy" description:"policy name bound to peer"` + Replace bool `short:"r" long:"replace" description:"Replace existing policy with new one" default:"false"` + } + + parser := flags.NewParser(&opts, flags.Default) + _, err := parser.Parse() + if err != nil { + fmt.Print(err) + os.Exit(1) + } + + if _, err := os.Stat(opts.OutputDir + "/gobgpd.conf"); os.IsNotExist(err) { + log.Fatal(err) + } + + bindPolicy(opts.OutputDir, opts.Neighbor, opts.Target, opts.PolicyName, opts.Replace) +} diff --git a/test/scenario_test/quagga-rsconfig.go b/test/scenario_test/quagga-rsconfig.go index 0551959f..70d57aa7 100644 --- a/test/scenario_test/quagga-rsconfig.go +++ b/test/scenario_test/quagga-rsconfig.go @@ -88,7 +88,7 @@ func (qt *QuaggaConfig) IPv6Config() *bytes.Buffer { return buf } -func create_config_files(nr int, outputDir string, IPVersion string, nonePeer bool, normalBGP bool, policyPattern string) { +func create_config_files(nr int, outputDir string, IPVersion string, nonePeer bool, normalBGP bool) { quaggaConfigList := make([]*QuaggaConfig, 0) gobgpConf := config.Bgp{ @@ -98,8 +98,6 @@ func create_config_files(nr int, outputDir string, IPVersion string, nonePeer bo }, } - var policyConf *config.RoutingPolicy - for i := 1; i < nr+1; i++ { c := config.Neighbor{ PeerAs: 65000 + uint32(i), @@ -111,8 +109,6 @@ func create_config_files(nr int, outputDir string, IPVersion string, nonePeer bo PeerType: config.PEER_TYPE_EXTERNAL, } - policyConf = bindPolicy(&c, policyPattern) - gobgpConf.NeighborList = append(gobgpConf.NeighborList, c) if !nonePeer { q := NewQuaggaConfig(i, &gobgpConf.Global, &c, net.ParseIP(serverAddress[IPVersion])) @@ -133,9 +129,6 @@ func create_config_files(nr int, outputDir string, IPVersion string, nonePeer bo var buffer bytes.Buffer encoder := toml.NewEncoder(&buffer) encoder.Encode(gobgpConf) - if policyConf != nil { - encoder.Encode(policyConf) - } err := ioutil.WriteFile(fmt.Sprintf("%s/gobgpd.conf", outputDir), buffer.Bytes(), 0644) if err != nil { @@ -143,7 +136,7 @@ func create_config_files(nr int, outputDir string, IPVersion string, nonePeer bo } } -func append_config_files(ar int, outputDir string, IPVersion string, noQuagga bool, normalBGP bool, policyPattern string) { +func append_config_files(ar int, outputDir string, IPVersion string, noQuagga bool, normalBGP bool) { gobgpConf := config.Bgp{ Global: config.Global{ @@ -161,8 +154,6 @@ func append_config_files(ar int, outputDir string, IPVersion string, noQuagga bo PeerType: config.PEER_TYPE_EXTERNAL, } - bindPolicy(&c, policyPattern) - if !noQuagga { q := NewQuaggaConfig(ar, &gobgpConf.Global, &c, net.ParseIP(serverAddress[IPVersion])) os.Mkdir(fmt.Sprintf("%s/q%d", outputDir, ar), 0755) @@ -202,689 +193,6 @@ func append_config_files(ar int, outputDir string, IPVersion string, noQuagga bo } } -func createPolicyConfig() *config.RoutingPolicy { - - ps0 := config.PrefixSet{ - PrefixSetName: "ps0", - PrefixList: []config.Prefix{ - config.Prefix{ - Address: net.ParseIP("192.168.0.0"), - Masklength: 16, - MasklengthRange: "16..24", - }}, - } - - ps1 := config.PrefixSet{ - PrefixSetName: "ps1", - PrefixList: []config.Prefix{ - config.Prefix{ - Address: net.ParseIP("192.168.20.0"), - Masklength: 24, - }, config.Prefix{ - Address: net.ParseIP("192.168.200.0"), - Masklength: 24, - }}, - } - - ps2 := config.PrefixSet{ - PrefixSetName: "ps2", - PrefixList: []config.Prefix{ - config.Prefix{ - Address: net.ParseIP("192.168.20.0"), - Masklength: 24, - }}, - } - - ps3 := config.PrefixSet{ - PrefixSetName: "ps3", - PrefixList: []config.Prefix{ - config.Prefix{ - Address: net.ParseIP("2001:0:10:2::"), - Masklength: 64, - MasklengthRange: "64..128", - }}, - } - - ps4 := config.PrefixSet{ - PrefixSetName: "ps4", - PrefixList: []config.Prefix{ - config.Prefix{ - Address: net.ParseIP("2001:0:10:20::"), - Masklength: 64, - }, config.Prefix{ - Address: net.ParseIP("2001:0:10:200::"), - Masklength: 64, - }}, - } - - ps5 := config.PrefixSet{ - PrefixSetName: "ps5", - PrefixList: []config.Prefix{ - config.Prefix{ - Address: net.ParseIP("2001:0:10:20::"), - Masklength: 64, - }}, - } - - nsPeer2 := config.NeighborSet{ - NeighborSetName: "nsPeer2", - NeighborInfoList: []config.NeighborInfo{ - config.NeighborInfo{ - Address: net.ParseIP("10.0.0.2"), - }}, - } - - nsPeer2V6 := config.NeighborSet{ - NeighborSetName: "nsPeer2V6", - NeighborInfoList: []config.NeighborInfo{ - config.NeighborInfo{ - Address: net.ParseIP("2001::0:192:168:0:2"), - }}, - } - - nsExabgp := config.NeighborSet{ - NeighborSetName: "nsExabgp", - NeighborInfoList: []config.NeighborInfo{ - config.NeighborInfo{ - Address: net.ParseIP("10.0.0.100"), - }}, - } - - psExabgp := config.PrefixSet{ - PrefixSetName: "psExabgp", - PrefixList: []config.Prefix{ - config.Prefix{ - Address: net.ParseIP("192.168.100.0"), - Masklength: 24, - MasklengthRange: "16..24", - }}, - } - - aspathFrom := config.AsPathSet{ - AsPathSetName: "aspathFrom", - AsPathSetMembers: []string{"^65100"}, - } - - aspathAny := config.AsPathSet{ - AsPathSetName: "aspAny", - AsPathSetMembers: []string{"65098"}, - } - - aspathOrigin := config.AsPathSet{ - AsPathSetName: "aspOrigin", - AsPathSetMembers: []string{"65091$"}, - } - - aspathOnly := config.AsPathSet{ - AsPathSetName: "aspOnly", - AsPathSetMembers: []string{"^65100$"}, - } - - comStr := config.CommunitySet{ - CommunitySetName: "comStr", - CommunityMembers: []string{"65100:10"}, - } - - comRegExp := config.CommunitySet{ - CommunitySetName: "comRegExp", - CommunityMembers: []string{"6[0-9]+:[0-9]+"}, - } - - st0 := config.Statement{ - Name: "st0", - Conditions: config.Conditions{ - MatchPrefixSet: "ps0", - MatchNeighborSet: "nsPeer2", - MatchSetOptions: config.MATCH_SET_OPTIONS_TYPE_ALL, - }, - Actions: config.Actions{ - AcceptRoute: false, - }, - } - - st1 := config.Statement{ - Name: "st1", - Conditions: config.Conditions{ - MatchPrefixSet: "ps1", - MatchNeighborSet: "nsPeer2", - MatchSetOptions: config.MATCH_SET_OPTIONS_TYPE_ALL, - }, - Actions: config.Actions{ - AcceptRoute: false, - }, - } - - st3 := config.Statement{ - Name: "st3", - Conditions: config.Conditions{ - MatchPrefixSet: "ps3", - MatchNeighborSet: "nsPeer2V6", - MatchSetOptions: config.MATCH_SET_OPTIONS_TYPE_ALL, - }, - Actions: config.Actions{ - AcceptRoute: false, - }, - } - - st4 := config.Statement{ - Name: "st4", - Conditions: config.Conditions{ - MatchPrefixSet: "ps4", - MatchNeighborSet: "nsPeer2V6", - MatchSetOptions: config.MATCH_SET_OPTIONS_TYPE_ALL, - }, - Actions: config.Actions{ - AcceptRoute: false, - }, - } - - st_aspathlen := config.Statement{ - Name: "st_aspathlen", - Conditions: config.Conditions{ - MatchPrefixSet: "psExabgp", - MatchNeighborSet: "nsExabgp", - BgpConditions: config.BgpConditions{ - AsPathLength: config.AsPathLength{ - Operator: "ge", - Value: 10, - }, - }, - MatchSetOptions: config.MATCH_SET_OPTIONS_TYPE_ALL, - }, - Actions: config.Actions{ - AcceptRoute: false, - }, - } - - st_aspathFrom := config.Statement{ - Name: "st_aspathlen", - Conditions: config.Conditions{ - MatchPrefixSet: "psExabgp", - MatchNeighborSet: "nsExabgp", - BgpConditions: config.BgpConditions{ - MatchAsPathSet: "aspathFrom", - }, - MatchSetOptions: config.MATCH_SET_OPTIONS_TYPE_ALL, - }, - Actions: config.Actions{ - AcceptRoute: false, - }, - } - - st_aspathAny := config.Statement{ - Name: "st_aspathlen", - Conditions: config.Conditions{ - MatchPrefixSet: "psExabgp", - MatchNeighborSet: "nsExabgp", - BgpConditions: config.BgpConditions{ - MatchAsPathSet: "aspAny", - }, - MatchSetOptions: config.MATCH_SET_OPTIONS_TYPE_ALL, - }, - Actions: config.Actions{ - AcceptRoute: false, - }, - } - - st_aspathOrigin := config.Statement{ - Name: "st_aspathlen", - Conditions: config.Conditions{ - MatchPrefixSet: "psExabgp", - MatchNeighborSet: "nsExabgp", - BgpConditions: config.BgpConditions{ - MatchAsPathSet: "aspOrigin", - }, - MatchSetOptions: config.MATCH_SET_OPTIONS_TYPE_ALL, - }, - Actions: config.Actions{ - AcceptRoute: false, - }, - } - - st_aspathOnly := config.Statement{ - Name: "st_aspathlen", - Conditions: config.Conditions{ - MatchPrefixSet: "psExabgp", - MatchNeighborSet: "nsExabgp", - BgpConditions: config.BgpConditions{ - MatchAsPathSet: "aspOnly", - }, - MatchSetOptions: config.MATCH_SET_OPTIONS_TYPE_ALL, - }, - Actions: config.Actions{ - AcceptRoute: false, - }, - } - - st_comStr := config.Statement{ - Name: "st_community", - Conditions: config.Conditions{ - MatchPrefixSet: "psExabgp", - MatchNeighborSet: "nsExabgp", - BgpConditions: config.BgpConditions{ - MatchCommunitySet: "comStr", - }, - MatchSetOptions: config.MATCH_SET_OPTIONS_TYPE_ALL, - }, - Actions: config.Actions{ - AcceptRoute: false, - }, - } - - st_comRegExp := config.Statement{ - Name: "st_community_regexp", - Conditions: config.Conditions{ - MatchPrefixSet: "psExabgp", - MatchNeighborSet: "nsExabgp", - BgpConditions: config.BgpConditions{ - MatchCommunitySet: "comRegExp", - }, - MatchSetOptions: config.MATCH_SET_OPTIONS_TYPE_ALL, - }, - Actions: config.Actions{ - AcceptRoute: false, - }, - } - - st_comAdd := config.Statement{ - Name: "st_community_regexp", - Conditions: config.Conditions{ - MatchPrefixSet: "psExabgp", - MatchNeighborSet: "nsExabgp", - BgpConditions: config.BgpConditions{ - MatchCommunitySet: "comStr", - }, - MatchSetOptions: config.MATCH_SET_OPTIONS_TYPE_ALL, - }, - Actions: config.Actions{ - AcceptRoute: true, - BgpActions: config.BgpActions{ - SetCommunity: config.SetCommunity{ - Communities: []string{"65100:20"}, - Options: "ADD", - }, - }, - }, - } - - st_comReplace := config.Statement{ - Name: "st_community_regexp", - Conditions: config.Conditions{ - MatchPrefixSet: "psExabgp", - MatchNeighborSet: "nsExabgp", - BgpConditions: config.BgpConditions{ - MatchCommunitySet: "comStr", - }, - MatchSetOptions: config.MATCH_SET_OPTIONS_TYPE_ALL, - }, - Actions: config.Actions{ - AcceptRoute: true, - BgpActions: config.BgpActions{ - SetCommunity: config.SetCommunity{ - Communities: []string{"65100:20", "65100:30"}, - Options: "REPLACE", - }, - }, - }, - } - - st_comRemove := config.Statement{ - Name: "st_community_regexp", - Conditions: config.Conditions{ - MatchPrefixSet: "psExabgp", - MatchNeighborSet: "nsExabgp", - BgpConditions: config.BgpConditions{ - MatchCommunitySet: "comStr", - }, - MatchSetOptions: config.MATCH_SET_OPTIONS_TYPE_ALL, - }, - Actions: config.Actions{ - AcceptRoute: true, - BgpActions: config.BgpActions{ - SetCommunity: config.SetCommunity{ - Communities: []string{"65100:20", "65100:30"}, - Options: "REMOVE", - }, - }, - }, - } - - st_comNull := config.Statement{ - Name: "st_community_regexp", - Conditions: config.Conditions{ - MatchPrefixSet: "psExabgp", - MatchNeighborSet: "nsExabgp", - BgpConditions: config.BgpConditions{ - MatchCommunitySet: "comStr", - }, - MatchSetOptions: config.MATCH_SET_OPTIONS_TYPE_ALL, - }, - Actions: config.Actions{ - AcceptRoute: true, - BgpActions: config.BgpActions{ - SetCommunity: config.SetCommunity{ - Communities: []string{}, - Options: "NULL", - }, - }, - }, - } - - pdImportV4 := config.PolicyDefinition{ - Name: "policy_use_ps0", - StatementList: []config.Statement{st0}, - } - - pdExportV4 := config.PolicyDefinition{ - Name: "policy_use_ps1", - StatementList: []config.Statement{st1}, - } - - pdImportV6 := config.PolicyDefinition{ - Name: "policy_use_ps3", - StatementList: []config.Statement{st3}, - } - - pdExportV6 := config.PolicyDefinition{ - Name: "policy_use_ps4", - StatementList: []config.Statement{st4}, - } - - pdAspathlen := config.PolicyDefinition{ - Name: "policy_aspathlen", - StatementList: []config.Statement{st_aspathlen}, - } - - pdaspathFrom := config.PolicyDefinition{ - Name: "policy_aspathFrom", - StatementList: []config.Statement{st_aspathFrom}, - } - - pdaspathAny := config.PolicyDefinition{ - Name: "policy_aspathAny", - StatementList: []config.Statement{st_aspathAny}, - } - - pdaspathOrigin := config.PolicyDefinition{ - Name: "policy_aspathOrigin", - StatementList: []config.Statement{st_aspathOrigin}, - } - - pdaspathOnly := config.PolicyDefinition{ - Name: "policy_aspathOnly", - StatementList: []config.Statement{st_aspathOnly}, - } - - pdCommunity := config.PolicyDefinition{ - Name: "policy_community", - StatementList: []config.Statement{st_comStr}, - } - - pdCommunityRegExp := config.PolicyDefinition{ - Name: "policy_community_regexp", - StatementList: []config.Statement{st_comRegExp}, - } - - pdCommunityAdd := config.PolicyDefinition{ - Name: "policy_community_add", - StatementList: []config.Statement{st_comAdd}, - } - - pdCommunityReplace := config.PolicyDefinition{ - Name: "policy_community_replace", - StatementList: []config.Statement{st_comReplace}, - } - - pdCommunityRemove := config.PolicyDefinition{ - Name: "policy_community_remove", - StatementList: []config.Statement{st_comRemove}, - } - - pdCommunityNull := config.PolicyDefinition{ - Name: "policy_community_null", - StatementList: []config.Statement{st_comNull}, - } - - ds := config.DefinedSets{ - PrefixSetList: []config.PrefixSet{ps0, ps1, ps2, ps3, ps4, ps5, psExabgp}, - NeighborSetList: []config.NeighborSet{nsPeer2, nsPeer2V6, nsExabgp}, - BgpDefinedSets: config.BgpDefinedSets{ - AsPathSetList: []config.AsPathSet{aspathFrom, aspathAny, aspathOrigin, aspathOnly}, - CommunitySetList: []config.CommunitySet{comStr, comRegExp}, - }, - } - - p := &config.RoutingPolicy{ - DefinedSets: ds, - PolicyDefinitionList: []config.PolicyDefinition{pdImportV4, pdExportV4, pdImportV6, pdExportV6, - pdAspathlen, pdaspathFrom, pdaspathAny, pdaspathOrigin, pdaspathOnly, - pdCommunity, pdCommunityRegExp, pdCommunityAdd, pdCommunityReplace, pdCommunityRemove, pdCommunityNull}, - } - - return p -} - -func updatePolicyConfig(outputDir string, pattern string) { - - newConf := config.Bgp{} - policyConf := config.RoutingPolicy{} - - _, d_err := toml.DecodeFile(fmt.Sprintf("%s/gobgpd.conf", outputDir), &newConf) - if d_err != nil { - log.Fatal(d_err) - } - _, d_err = toml.DecodeFile(fmt.Sprintf("%s/gobgpd.conf", outputDir), &policyConf) - if d_err != nil { - log.Fatal(d_err) - } - - fmt.Println(policyConf) - - st2 := config.Statement{ - Name: "st2", - Conditions: config.Conditions{ - MatchPrefixSet: "ps2", - MatchNeighborSet: "nsPeer2", - MatchSetOptions: config.MATCH_SET_OPTIONS_TYPE_ALL, - }, - Actions: config.Actions{ - AcceptRoute: false, - RejectRoute: true, - }, - } - - st5 := config.Statement{ - Name: "st5", - Conditions: config.Conditions{ - MatchPrefixSet: "ps5", - MatchNeighborSet: "nsPeer2V6", - MatchSetOptions: config.MATCH_SET_OPTIONS_TYPE_ALL, - }, - Actions: config.Actions{ - AcceptRoute: false, - RejectRoute: true, - }, - } - - if pattern == "test_03_import_policy_update" || pattern == "test_04_export_policy_update" { - policyConf.PolicyDefinitionList[1].StatementList = []config.Statement{st2} - } - - if pattern == "test_07_import_policy_update" || pattern == "test_08_export_policy_update" { - policyConf.PolicyDefinitionList[3].StatementList = []config.Statement{st5} - } - - var buffer bytes.Buffer - encoder := toml.NewEncoder(&buffer) - encoder.Encode(newConf) - encoder.Encode(policyConf) - e_err := ioutil.WriteFile(fmt.Sprintf("%s/gobgpd.conf", outputDir), buffer.Bytes(), 0644) - if e_err != nil { - log.Fatal(e_err) - } - - return -} - -type PolicyBinding struct { - NeighborAddress string - Policy *config.RoutingPolicy - ImportPolicyNames []string - ExportPolicyNames []string -} - -func getPolicyBinding(pattern string) *PolicyBinding { - - var pb *PolicyBinding = &PolicyBinding{Policy: createPolicyConfig()} - - switch pattern { - case "test_01_import_policy_initial": - pb.NeighborAddress = "10.0.0.3" - pb.ImportPolicyNames = []string{"policy_use_ps0"} - pb.ExportPolicyNames = nil - - case "test_02_export_policy_initial": - pb.NeighborAddress = "10.0.0.3" - pb.ImportPolicyNames = nil - pb.ExportPolicyNames = []string{"policy_use_ps0"} - - case "test_03_import_policy_update": - pb.NeighborAddress = "10.0.0.3" - pb.ImportPolicyNames = []string{"policy_use_ps1"} - pb.ExportPolicyNames = nil - - case "test_04_export_policy_update": - pb.NeighborAddress = "10.0.0.3" - pb.ImportPolicyNames = nil - pb.ExportPolicyNames = []string{"policy_use_ps1"} - - case "test_05_import_policy_initial_ipv6": - pb.NeighborAddress = "2001::0:192:168:0:3" - pb.ImportPolicyNames = []string{"policy_use_ps3"} - pb.ExportPolicyNames = nil - - case "test_06_export_policy_initial_ipv6": - pb.NeighborAddress = "2001::0:192:168:0:3" - pb.ImportPolicyNames = nil - pb.ExportPolicyNames = []string{"policy_use_ps3"} - - case "test_07_import_policy_update": - pb.NeighborAddress = "2001::0:192:168:0:3" - pb.ImportPolicyNames = []string{"policy_use_ps4"} - pb.ExportPolicyNames = nil - - case "test_08_export_policy_update": - pb.NeighborAddress = "2001::0:192:168:0:3" - pb.ImportPolicyNames = nil - pb.ExportPolicyNames = []string{"policy_use_ps4"} - - case "test_09_aspath_length_condition_import": - pb.NeighborAddress = "10.0.0.2" - pb.ImportPolicyNames = []string{"policy_aspathlen"} - pb.ExportPolicyNames = nil - - case "test_10_aspath_from_condition_import": - pb.NeighborAddress = "10.0.0.2" - pb.ImportPolicyNames = []string{"policy_aspathFrom"} - pb.ExportPolicyNames = nil - - case "test_11_aspath_any_condition_import": - pb.NeighborAddress = "10.0.0.2" - pb.ImportPolicyNames = []string{"policy_aspathAny"} - pb.ExportPolicyNames = nil - - case "test_12_aspath_origin_condition_import": - pb.NeighborAddress = "10.0.0.2" - pb.ImportPolicyNames = []string{"policy_aspathOrigin"} - pb.ExportPolicyNames = nil - - case "test_13_aspath_only_condition_import": - pb.NeighborAddress = "10.0.0.2" - pb.ImportPolicyNames = []string{"policy_aspathOnly"} - pb.ExportPolicyNames = nil - - case "test_14_aspath_only_condition_import": - pb.NeighborAddress = "10.0.0.2" - pb.ImportPolicyNames = []string{"policy_aspathOnly"} - pb.ExportPolicyNames = nil - - case "test_15_community_condition_import": - pb.NeighborAddress = "10.0.0.2" - pb.ImportPolicyNames = []string{"policy_community"} - pb.ExportPolicyNames = nil - - case "test_16_community_condition_regexp_import": - pb.NeighborAddress = "10.0.0.2" - pb.ImportPolicyNames = []string{"policy_community_regexp"} - pb.ExportPolicyNames = nil - - case "test_17_community_add_action_import": - pb.NeighborAddress = "10.0.0.2" - pb.ImportPolicyNames = []string{"policy_community_add"} - pb.ExportPolicyNames = nil - - case "test_18_community_replace_action_import": - pb.NeighborAddress = "10.0.0.2" - pb.ImportPolicyNames = []string{"policy_community_replace"} - pb.ExportPolicyNames = nil - - case "test_19_community_remove_action_import": - pb.NeighborAddress = "10.0.0.2" - pb.ImportPolicyNames = []string{"policy_community_remove"} - pb.ExportPolicyNames = nil - - case "test_20_community_null_action_import": - pb.NeighborAddress = "10.0.0.2" - pb.ImportPolicyNames = []string{"policy_community_null"} - pb.ExportPolicyNames = nil - - case "test_21_community_add_action_export": - pb.NeighborAddress = "10.0.0.2" - pb.ImportPolicyNames = nil - pb.ExportPolicyNames = []string{"policy_community_add"} - - case "test_22_community_replace_action_export": - pb.NeighborAddress = "10.0.0.2" - pb.ImportPolicyNames = nil - pb.ExportPolicyNames = []string{"policy_community_replace"} - - case "test_23_community_remove_action_export": - pb.NeighborAddress = "10.0.0.2" - pb.ImportPolicyNames = nil - pb.ExportPolicyNames = []string{"policy_community_remove"} - - case "test_24_community_null_action_export": - pb.NeighborAddress = "10.0.0.2" - pb.ImportPolicyNames = nil - pb.ExportPolicyNames = []string{"policy_community_null"} - default: - pb = nil - } - - return pb - -} - -func bindPolicy(c *config.Neighbor, policyPattern string) *config.RoutingPolicy { - var binding *PolicyBinding - if policyPattern != "" { - binding = getPolicyBinding(policyPattern) - } - - if binding != nil { - ip := net.ParseIP(binding.NeighborAddress) - if ip.String() == c.NeighborAddress.String() { - ap := config.ApplyPolicy{ - ImportPolicies: binding.ImportPolicyNames, - ExportPolicies: binding.ExportPolicyNames, - } - c.ApplyPolicy = ap - return binding.Policy - } - } - return nil -} - func main() { var opts struct { ClientNumber int `short:"n" long:"client-number" description:"specfying the number of clients" default:"8"` @@ -894,8 +202,6 @@ func main() { NetIdentifier int `short:"i" long:"net-identifer" description:"specifing the use network identifier" default:"0"` NonePeer bool `long:"none-peer" description:"disable make quagga config"` NormalBGP bool `long:"normal-bgp" description:"generate normal bgp server configuration"` - PolicyPattern string `short:"p" long:"policy-pattern" description:"specify policy pattern" default:""` - UpdatePolicy bool `long:"update-policy" description:"update exsisting policy config" default:"false"` } parser := flags.NewParser(&opts, flags.Default) _, err := parser.Parse() @@ -924,18 +230,14 @@ func main() { baseNeighborNetMask[IPv4] = ".0/24" } - isCreateMode := opts.AppendClient == 0 && !opts.UpdatePolicy + isCreateMode := opts.AppendClient == 0 if isCreateMode { - create_config_files(opts.ClientNumber, opts.OutputDir, opts.IPVersion, opts.NonePeer, opts.NormalBGP, opts.PolicyPattern) + create_config_files(opts.ClientNumber, opts.OutputDir, opts.IPVersion, opts.NonePeer, opts.NormalBGP) } else { if _, err := os.Stat(fmt.Sprintf("%s/gobgpd.conf", opts.OutputDir)); os.IsNotExist(err) { log.Fatal(err) } - if opts.UpdatePolicy { - updatePolicyConfig(opts.OutputDir, opts.PolicyPattern) - } else { - append_config_files(opts.AppendClient, opts.OutputDir, opts.IPVersion, opts.NonePeer, opts.NormalBGP, opts.PolicyPattern) - } + append_config_files(opts.AppendClient, opts.OutputDir, opts.IPVersion, opts.NonePeer, opts.NormalBGP) } } diff --git a/test/scenario_test/route_server_policy_test.py b/test/scenario_test/route_server_policy_test.py index edcf7d7b..190c30fc 100644 --- a/test/scenario_test/route_server_policy_test.py +++ b/test/scenario_test/route_server_policy_test.py @@ -18,6 +18,7 @@ import sys import nose import quagga_access as qaccess import docker_control as fab +import StringIO from noseplugin import OptionParser from noseplugin import parser_option from gobgp_test import GoBGPTestBase @@ -34,7 +35,6 @@ def print_elapsed_time(f): return wrapped - class GoBGPTest(GoBGPTestBase): quagga_num = 3 @@ -53,27 +53,34 @@ class GoBGPTest(GoBGPTestBase): self.initial_wait_time = 1 self.wait_per_retry = 3 + if fab.docker_container_check() or fab.bridge_setting_check(): + print "gobgp test environment already exists. clean up..." + fab.docker_containers_destroy(False, False) + @classmethod @print_elapsed_time def setUpClass(cls): print 'prepare gobgp' - fab.prepare_gobgp(parser_option.gobgp_log_debug, parser_option.use_local) + cls.go_path = parser_option.go_path + cls.use_local = parser_option.use_local + cls.log_debug = parser_option.gobgp_log_debug + fab.prepare_gobgp(cls.log_debug, cls.use_local) + fab.build_config_tools(cls.go_path) @print_elapsed_time - def initialize(self, policy_pattern=None): - use_local = parser_option.use_local - go_path = parser_option.go_path - log_debug = parser_option.gobgp_log_debug - - fab.init_policy_test_env_executor(self.quagga_num, use_local, go_path, - log_debug, policy=policy_pattern, + def initialize(self): + fab.init_policy_test_env_executor(self.quagga_num, use_ipv6=self.use_ipv6_gobgp, use_exabgp=self.use_exa_bgp) print "please wait " + str(self.initial_wait_time) + " second" time.sleep(self.initial_wait_time) self.assertTrue(self.check_load_config()) + @print_elapsed_time + def setup_config(self, peer, policy_name, target): + fab.make_config(self.quagga_num, self.go_path, BRIDGE_0, use_compiled=True) + fab.update_policy_config(self.go_path, peer, policy_name, target) """ import-policy test @@ -88,9 +95,7 @@ class GoBGPTest(GoBGPTestBase): # initialize test environment # policy_pattern:p1 attaches a policy to reject route 192.168.0.0/16 (16...24) # coming from peer2(10.0.0.2) to peer3(10.0.0.3)'s import-policy. - self.initialize(policy_pattern="test_01_import_policy_initial") - addresses = self.get_neighbor_address(self.gobgp_config) - self.retry_routine_for_state(addresses, "BGP_FSM_ESTABLISHED") + peer1 = "10.0.0.1" peer2 = "10.0.0.2" @@ -98,7 +103,14 @@ class GoBGPTest(GoBGPTestBase): prefix1 = "192.168.2.0/24" w = self.wait_per_retry - path = self.get_paths_in_localrib(peer1, prefix1, retry=self.retry_count_common) + self.setup_config(peer3, "test_01_import_policy_initial", "import") + self.initialize() + + addresses = self.get_neighbor_address(self.gobgp_config) + self.retry_routine_for_state(addresses, "BGP_FSM_ESTABLISHED") + + + path = self.get_paths_in_localrib(peer1, prefix1, retry=self.retry_count_common, interval=w) self.assertIsNotNone(path) # check show ip bgp on peer1(quagga1) @@ -130,17 +142,19 @@ class GoBGPTest(GoBGPTestBase): # initialize test environment # policy_pattern:p1 attaches a policy to reject route 192.168.0.0/16 (16...24) # coming from peer2(10.0.0.2) to peer3(10.0.0.3)'s export-policy. - self.initialize(policy_pattern="test_02_export_policy_initial") - addresses = self.get_neighbor_address(self.gobgp_config) - self.retry_routine_for_state(addresses, "BGP_FSM_ESTABLISHED") - peer1 = "10.0.0.1" peer2 = "10.0.0.2" peer3 = "10.0.0.3" prefix1 = "192.168.2.0/24" w = self.wait_per_retry - paths = self.get_paths_in_localrib(peer1, prefix1, retry=self.retry_count_common) + self.setup_config(peer3, "test_02_export_policy_initial", "export") + self.initialize() + + addresses = self.get_neighbor_address(self.gobgp_config) + self.retry_routine_for_state(addresses, "BGP_FSM_ESTABLISHED") + + paths = self.get_paths_in_localrib(peer1, prefix1, retry=self.retry_count_common, interval=w) self.assertIsNotNone(paths) # check show ip bgp on peer1(quagga1) @@ -192,7 +206,6 @@ class GoBGPTest(GoBGPTestBase): # policy_pattern:p3 attaches a policy to reject route # 192.168.2.0/24, 192.168.20.0/24, 192.168.200.0/24 # coming from peer2(10.0.0.2) to peer3(10.0.0.3)'s import-policy. - self.initialize(policy_pattern="test_03_import_policy_update") peer1 = "10.0.0.1" peer2 = "10.0.0.2" @@ -202,6 +215,9 @@ class GoBGPTest(GoBGPTestBase): r3 = "192.168.200.0/24" w = self.wait_per_retry + self.setup_config(peer3, "test_03_import_policy_update", "import") + self.initialize() + # add other network tn = qaccess.login(peer2) print "add network 192.168.20.0/24" @@ -253,7 +269,8 @@ class GoBGPTest(GoBGPTestBase): # update policy print "update_policy_config" - fab.update_policy_config(parser_option.go_path, policy_pattern="test_03_import_policy_update") + fab.update_policy_config(self.go_path, peer3, "test_03_import_policy_update_softreset", "import", isReplace=True) + fab.reload_config() time.sleep(self.initial_wait_time) # soft reset @@ -749,6 +766,9 @@ class GoBGPTest(GoBGPTestBase): as_path = reduce(lambda a,b: a + " " + b, asns) generate_exabgp_config(prefix1, aspath=as_path) + + #make_config_append(100, go_path, BRIDGE_0, peer_opts="--none-peer") + self.quagga_num = 2 self.use_exa_bgp = True @@ -1571,6 +1591,137 @@ class GoBGPTest(GoBGPTestBase): self.assertFalse(self.check_community(peer2, prefix1.split('/')[0], '65100:10', retry=0)) + """ + distribute-policy test + --------------------- + exabgp ->r1(community=65100:10) -> x | -> peer1-rib -> | -> r2 --> peer1 + r2(192.168.2.0/24) -> o | | + | -> peer2-rib -> | -> r2 --> peer2 + | | + --------------------- + """ + def test_25_distribute_reject(self): + + # initialize test environment + # policy_pattern:CommunityNullEXP attaches a policy to remove its community attr + # to peer2(10.0.0.2)'s export-policy. + + # generate exabgp configuration file + r1 = "192.168.100.0/24" + r2 = "192.168.2.0/24" + asns = ['65100'] + [ str(asn) for asn in range(65099, 65090, -1) ] + community = ' 65100:10 65100:20 65100:30 ' + as_path = reduce(lambda a,b: a + " " + b, asns) + + e = ExabgpConfig(EXABGP_COMMON_CONF) + e.add_route(r1, aspath=as_path, community=community) + e.add_route(r2, aspath=['65100']) + e.write() + + + + + self.quagga_num = 2 + self.use_exa_bgp = True + self.use_ipv6_gobgp = False + + self.initialize(policy_pattern="test_25_distribute_reject") + addresses = self.get_neighbor_address(self.gobgp_config) + self.retry_routine_for_state(addresses, "BGP_FSM_ESTABLISHED") + + peer1 = "10.0.0.1" + peer2 = "10.0.0.2" + + path = self.get_paths_in_localrib(peer1, prefix1, retry=self.retry_count_common) + self.assertIsNotNone(path) + + # check show ip bgp on peer1(quagga1) + qpath = self.get_route(peer1,prefix1, retry=self.retry_count_common) + self.assertIsNotNone(qpath) + + # check adj-rib-out in peer2 + path = self.get_paths_in_localrib(peer2, prefix1, retry=0) + self.assertIsNotNone(path) + attrs = [x for x in path[0]['attrs'] if 'communites' in x ] + self.assertTrue((65100 << 16 | 10) in attrs[0]['communites']) + self.assertTrue((65100 << 16 | 20) in attrs[0]['communites']) + self.assertTrue((65100 << 16 | 30) in attrs[0]['communites']) + # check out-rib + path = self.get_adj_rib_out(peer2, prefix1, retry=1) + attrs = [x for x in path['attrs'] if 'communites' in x ] + self.assertFalse('communites' in attrs) + + # check show ip bgp on peer2(quagga2) + qpath = self.get_route(peer2,prefix1, retry=self.retry_count_common) + self.assertIsNotNone(qpath) + self.assertFalse(self.check_community(peer2, prefix1.split('/')[0], '65100:20', retry=0)) + self.assertFalse(self.check_community(peer2, prefix1.split('/')[0], '65100:30', retry=0)) + self.assertFalse(self.check_community(peer2, prefix1.split('/')[0], '65100:10', retry=0)) + + + + """ + distribute-policy test + --------------------------------------- + exabgp ->(community=65100:10)->| -> peer1-rib -> peer1-adj-rib-out | --> peer1 + (community=65100:20) | | + (community=65100:30) | -> peer2-rib -> peer2-adj-rib-out | --> peer2 + | apply action | + --------------------------------------- + """ + def test_26_distribute_modify_community(self): + + # initialize test environment + # policy_pattern:CommunityNullEXP attaches a policy to remove its community attr + # to peer2(10.0.0.2)'s export-policy. + + # generate exabgp configuration file + prefix1 = "192.168.100.0/24" + asns = ['65100'] + [ str(asn) for asn in range(65099, 65090, -1) ] + community = ' 65100:10 65100:20 65100:30 ' + as_path = reduce(lambda a,b: a + " " + b, asns) + generate_exabgp_config(prefix1, aspath=as_path, community=community) + + + self.quagga_num = 2 + self.use_exa_bgp = True + self.use_ipv6_gobgp = False + + self.initialize(policy_pattern="test_24_community_null_action_export") + addresses = self.get_neighbor_address(self.gobgp_config) + self.retry_routine_for_state(addresses, "BGP_FSM_ESTABLISHED") + + peer1 = "10.0.0.1" + peer2 = "10.0.0.2" + + path = self.get_paths_in_localrib(peer1, prefix1, retry=self.retry_count_common) + self.assertIsNotNone(path) + + # check show ip bgp on peer1(quagga1) + qpath = self.get_route(peer1,prefix1, retry=self.retry_count_common) + self.assertIsNotNone(qpath) + + # check adj-rib-out in peer2 + path = self.get_paths_in_localrib(peer2, prefix1, retry=0) + self.assertIsNotNone(path) + attrs = [x for x in path[0]['attrs'] if 'communites' in x ] + self.assertTrue((65100 << 16 | 10) in attrs[0]['communites']) + self.assertTrue((65100 << 16 | 20) in attrs[0]['communites']) + self.assertTrue((65100 << 16 | 30) in attrs[0]['communites']) + # check out-rib + path = self.get_adj_rib_out(peer2, prefix1, retry=1) + attrs = [x for x in path['attrs'] if 'communites' in x ] + self.assertFalse('communites' in attrs) + + # check show ip bgp on peer2(quagga2) + qpath = self.get_route(peer2,prefix1, retry=self.retry_count_common) + self.assertIsNotNone(qpath) + self.assertFalse(self.check_community(peer2, prefix1.split('/')[0], '65100:20', retry=0)) + self.assertFalse(self.check_community(peer2, prefix1.split('/')[0], '65100:30', retry=0)) + self.assertFalse(self.check_community(peer2, prefix1.split('/')[0], '65100:10', retry=0)) + + + def generate_exabgp_config(pref, aspath='', community=''): value = {'prefix': pref, 'aspath': aspath, @@ -1603,6 +1754,51 @@ neighbor 10.0.255.1 { } ''' +class ExabgpConfig(object): + + basic_conf_begin = ''' +neighbor 10.0.255.1 { + router-id 192.168.0.7; + local-address 10.0.0.100; + local-as 65100; + peer-as 65000; + hold-time 90; + md5 "hoge100"; + graceful-restart; + + family { + inet unicast; + } + static { + # static routes +''' + + basic_conf_end = ''' + } +} +''' + + def __init__(self, config_name): + self.o = StringIO.StringIO() + self.config_name = config_name + print self.basic_conf_begin >> self.o + + def add_route(self, prefix, aspath='', community=''): + value = {'prefix': prefix, + 'aspath': aspath, + 'community': community} + r = "route %(prefix)s next-hop 10.0.0.100 as-path [%(aspath)s] community [%(community)s];" % value + print r >> self.o + + def write(self): + print self.basic_conf_end >> self.o + pwd = local("pwd", capture=True) + conf_dir = pwd + "/exabgp_test_conf" + + with open(conf_dir + "/" + self.config_name, 'w') as f: + f.write(self.o.getvalue()) + + if __name__ == '__main__': if fab.test_user_check() is False: print "you are not root." |