diff options
| -rw-r--r-- | bgpd/bgp_aspath.c | 22 | ||||
| -rw-r--r-- | bgpd/bgp_aspath.h | 2 | ||||
| -rw-r--r-- | bgpd/bgp_routemap.c | 91 | ||||
| -rw-r--r-- | bgpd/bgp_routemap_nb.c | 7 | ||||
| -rw-r--r-- | bgpd/bgp_routemap_nb.h | 4 | ||||
| -rw-r--r-- | bgpd/bgp_routemap_nb_config.c | 52 | ||||
| -rw-r--r-- | doc/user/bgp.rst | 5 | ||||
| -rw-r--r-- | lib/routemap.h | 1 | ||||
| -rw-r--r-- | lib/routemap_cli.c | 5 | ||||
| -rw-r--r-- | tests/topotests/bgp_set_aspath_replace/__init__.py | 0 | ||||
| -rw-r--r-- | tests/topotests/bgp_set_aspath_replace/r1/bgpd.conf | 17 | ||||
| -rw-r--r-- | tests/topotests/bgp_set_aspath_replace/r1/zebra.conf | 6 | ||||
| -rw-r--r-- | tests/topotests/bgp_set_aspath_replace/r2/bgpd.conf | 8 | ||||
| -rw-r--r-- | tests/topotests/bgp_set_aspath_replace/r2/zebra.conf | 9 | ||||
| -rw-r--r-- | tests/topotests/bgp_set_aspath_replace/r3/bgpd.conf | 9 | ||||
| -rw-r--r-- | tests/topotests/bgp_set_aspath_replace/r3/zebra.conf | 10 | ||||
| -rw-r--r-- | tests/topotests/bgp_set_aspath_replace/test_bgp_set_aspath_replace.py | 103 | ||||
| -rw-r--r-- | yang/frr-bgp-route-map.yang | 15 | 
18 files changed, 366 insertions, 0 deletions
diff --git a/bgpd/bgp_aspath.c b/bgpd/bgp_aspath.c index fd644ebf0a..880e15fadb 100644 --- a/bgpd/bgp_aspath.c +++ b/bgpd/bgp_aspath.c @@ -1258,6 +1258,28 @@ struct aspath *aspath_replace_specific_asn(struct aspath *aspath,  	return new;  } +/* Replace all ASNs with our own ASN */ +struct aspath *aspath_replace_all_asn(struct aspath *aspath, as_t our_asn) +{ +	struct aspath *new; +	struct assegment *seg; + +	new = aspath_dup(aspath); +	seg = new->segments; + +	while (seg) { +		int i; + +		for (i = 0; i < seg->length; i++) +			seg->as[i] = our_asn; + +		seg = seg->next; +	} + +	aspath_str_update(new, false); +	return new; +} +  /* Replace all private ASNs with our own ASN */  struct aspath *aspath_replace_private_asns(struct aspath *aspath, as_t asn,  					   as_t peer_asn) diff --git a/bgpd/bgp_aspath.h b/bgpd/bgp_aspath.h index 4b16818167..912db7b254 100644 --- a/bgpd/bgp_aspath.h +++ b/bgpd/bgp_aspath.h @@ -112,6 +112,8 @@ extern bool aspath_single_asn_check(struct aspath *, as_t asn);  extern struct aspath *aspath_replace_specific_asn(struct aspath *aspath,  						  as_t target_asn,  						  as_t our_asn); +extern struct aspath *aspath_replace_all_asn(struct aspath *aspath, +					     as_t our_asn);  extern struct aspath *aspath_replace_private_asns(struct aspath *aspath,  						  as_t asn, as_t peer_asn);  extern struct aspath *aspath_remove_private_asns(struct aspath *aspath, diff --git a/bgpd/bgp_routemap.c b/bgpd/bgp_routemap.c index 6c303a9e5f..20ee2e4d49 100644 --- a/bgpd/bgp_routemap.c +++ b/bgpd/bgp_routemap.c @@ -2174,6 +2174,57 @@ static const struct route_map_rule_cmd route_set_aspath_exclude_cmd = {  	route_aspath_free,  }; +/* `set as-path replace AS-PATH` */ +static void *route_aspath_replace_compile(const char *arg) +{ +	return XSTRDUP(MTYPE_ROUTE_MAP_COMPILED, arg); +} + +static void route_aspath_replace_free(void *rule) +{ +	XFREE(MTYPE_ROUTE_MAP_COMPILED, rule); +} + +static enum route_map_cmd_result_t +route_set_aspath_replace(void *rule, const struct prefix *dummy, void *object) +{ +	struct aspath *aspath_new; +	const char *replace = rule; +	struct bgp_path_info *path = object; +	as_t own_asn = path->peer->change_local_as ? path->peer->change_local_as +						   : path->peer->local_as; + +	if (path->peer->sort != BGP_PEER_EBGP) { +		zlog_warn( +			"`set as-path replace` is supported only for EBGP peers"); +		return RMAP_NOOP; +	} + +	if (path->attr->aspath->refcnt) +		aspath_new = aspath_dup(path->attr->aspath); +	else +		aspath_new = path->attr->aspath; + +	if (strmatch(replace, "any")) { +		path->attr->aspath = +			aspath_replace_all_asn(aspath_new, own_asn); +	} else { +		as_t replace_asn = strtoul(replace, NULL, 10); + +		path->attr->aspath = aspath_replace_specific_asn( +			aspath_new, replace_asn, own_asn); +	} + +	return RMAP_OKAY; +} + +static const struct route_map_rule_cmd route_set_aspath_replace_cmd = { +	"as-path replace", +	route_set_aspath_replace, +	route_aspath_replace_compile, +	route_aspath_replace_free, +}; +  /* `set community COMMUNITY' */  struct rmap_com_set {  	struct community *com; @@ -5389,6 +5440,43 @@ DEFUN_YANG (set_aspath_prepend_lastas,  	return nb_cli_apply_changes(vty, NULL);  } +DEFPY_YANG (set_aspath_replace_asn, +	    set_aspath_replace_asn_cmd, +	    "set as-path replace <any|(1-4294967295)>$replace", +	    SET_STR +	    "Transform BGP AS_PATH attribute\n" +	    "Replace AS number to local AS number\n" +	    "Replace any AS number to local AS number\n" +	    "Replace a specific AS number to local AS number\n") +{ +	const char *xpath = +		"./set-action[action='frr-bgp-route-map:as-path-replace']"; +	char xpath_value[XPATH_MAXLEN]; + +	nb_cli_enqueue_change(vty, xpath, NB_OP_CREATE, NULL); +	snprintf(xpath_value, sizeof(xpath_value), +		 "%s/rmap-set-action/frr-bgp-route-map:replace-as-path", xpath); +	nb_cli_enqueue_change(vty, xpath_value, NB_OP_MODIFY, replace); +	return nb_cli_apply_changes(vty, NULL); +} + +DEFPY_YANG (no_set_aspath_replace_asn, +	    no_set_aspath_replace_asn_cmd, +	    "no set as-path replace [<any|(1-4294967295)>]", +	    NO_STR +	    SET_STR +	    "Transform BGP AS_PATH attribute\n" +	    "Replace AS number to local AS number\n" +	    "Replace any AS number to local AS number\n" +	    "Replace a specific AS number to local AS number\n") +{ +	const char *xpath = +		"./set-action[action='frr-bgp-route-map:as-path-replace']"; + +	nb_cli_enqueue_change(vty, xpath, NB_OP_DESTROY, NULL); +	return nb_cli_apply_changes(vty, NULL); +} +  DEFUN_YANG (no_set_aspath_prepend,  	    no_set_aspath_prepend_cmd,  	    "no set as-path prepend [(1-4294967295)]", @@ -6727,6 +6815,7 @@ void bgp_route_map_init(void)  	route_map_install_set(&route_set_distance_cmd);  	route_map_install_set(&route_set_aspath_prepend_cmd);  	route_map_install_set(&route_set_aspath_exclude_cmd); +	route_map_install_set(&route_set_aspath_replace_cmd);  	route_map_install_set(&route_set_origin_cmd);  	route_map_install_set(&route_set_atomic_aggregate_cmd);  	route_map_install_set(&route_set_aggregator_as_cmd); @@ -6800,10 +6889,12 @@ void bgp_route_map_init(void)  	install_element(RMAP_NODE, &set_aspath_prepend_asn_cmd);  	install_element(RMAP_NODE, &set_aspath_prepend_lastas_cmd);  	install_element(RMAP_NODE, &set_aspath_exclude_cmd); +	install_element(RMAP_NODE, &set_aspath_replace_asn_cmd);  	install_element(RMAP_NODE, &no_set_aspath_prepend_cmd);  	install_element(RMAP_NODE, &no_set_aspath_prepend_lastas_cmd);  	install_element(RMAP_NODE, &no_set_aspath_exclude_cmd);  	install_element(RMAP_NODE, &no_set_aspath_exclude_all_cmd); +	install_element(RMAP_NODE, &no_set_aspath_replace_asn_cmd);  	install_element(RMAP_NODE, &set_origin_cmd);  	install_element(RMAP_NODE, &no_set_origin_cmd);  	install_element(RMAP_NODE, &set_atomic_aggregate_cmd); diff --git a/bgpd/bgp_routemap_nb.c b/bgpd/bgp_routemap_nb.c index caf1553ec1..585596e1aa 100644 --- a/bgpd/bgp_routemap_nb.c +++ b/bgpd/bgp_routemap_nb.c @@ -297,6 +297,13 @@ const struct frr_yang_module_info frr_bgp_route_map_info = {  			}  		},  		{ +			.xpath = "/frr-route-map:lib/route-map/entry/set-action/rmap-set-action/frr-bgp-route-map:replace-as-path", +			.cbs = { +				.modify = lib_route_map_entry_set_action_rmap_set_action_replace_as_path_modify, +				.destroy = lib_route_map_entry_set_action_rmap_set_action_replace_as_path_destroy, +			} +		}, +		{  			.xpath = "/frr-route-map:lib/route-map/entry/set-action/rmap-set-action/frr-bgp-route-map:community-none",  			.cbs = {  				.modify = lib_route_map_entry_set_action_rmap_set_action_community_none_modify, diff --git a/bgpd/bgp_routemap_nb.h b/bgpd/bgp_routemap_nb.h index e0b3a6926f..a01adf7d5d 100644 --- a/bgpd/bgp_routemap_nb.h +++ b/bgpd/bgp_routemap_nb.h @@ -108,6 +108,10 @@ int lib_route_map_entry_set_action_rmap_set_action_last_as_modify(struct nb_cb_m  int lib_route_map_entry_set_action_rmap_set_action_last_as_destroy(struct nb_cb_destroy_args *args);  int lib_route_map_entry_set_action_rmap_set_action_exclude_as_path_modify(struct nb_cb_modify_args *args);  int lib_route_map_entry_set_action_rmap_set_action_exclude_as_path_destroy(struct nb_cb_destroy_args *args); +int lib_route_map_entry_set_action_rmap_set_action_replace_as_path_modify( +	struct nb_cb_modify_args *args); +int lib_route_map_entry_set_action_rmap_set_action_replace_as_path_destroy( +	struct nb_cb_destroy_args *args);  int lib_route_map_entry_set_action_rmap_set_action_community_none_modify(struct nb_cb_modify_args *args);  int lib_route_map_entry_set_action_rmap_set_action_community_none_destroy(struct nb_cb_destroy_args *args);  int lib_route_map_entry_set_action_rmap_set_action_community_string_modify(struct nb_cb_modify_args *args); diff --git a/bgpd/bgp_routemap_nb_config.c b/bgpd/bgp_routemap_nb_config.c index 773538ee41..b87877b1e0 100644 --- a/bgpd/bgp_routemap_nb_config.c +++ b/bgpd/bgp_routemap_nb_config.c @@ -2209,6 +2209,58 @@ lib_route_map_entry_set_action_rmap_set_action_exclude_as_path_destroy(  /*   * XPath: + * /frr-route-map:lib/route-map/entry/set-action/rmap-set-action/frr-bgp-route-map:replace-as-path + */ +int lib_route_map_entry_set_action_rmap_set_action_replace_as_path_modify( +	struct nb_cb_modify_args *args) +{ +	struct routemap_hook_context *rhc; +	const char *type; +	int rv; + +	switch (args->event) { +	case NB_EV_VALIDATE: +	case NB_EV_PREPARE: +	case NB_EV_ABORT: +		break; +	case NB_EV_APPLY: +		/* Add configuration. */ +		rhc = nb_running_get_entry(args->dnode, NULL, true); +		type = yang_dnode_get_string(args->dnode, NULL); + +		/* Set destroy information. */ +		rhc->rhc_shook = generic_set_delete; +		rhc->rhc_rule = "as-path replace"; +		rhc->rhc_event = RMAP_EVENT_SET_DELETED; + +		rv = generic_set_add(rhc->rhc_rmi, "as-path replace", type, +				     args->errmsg, args->errmsg_len); +		if (rv != CMD_SUCCESS) { +			rhc->rhc_shook = NULL; +			return NB_ERR_INCONSISTENCY; +		} +	} + +	return NB_OK; +} + +int lib_route_map_entry_set_action_rmap_set_action_replace_as_path_destroy( +	struct nb_cb_destroy_args *args) +{ +	switch (args->event) { +	case NB_EV_VALIDATE: +	case NB_EV_PREPARE: +	case NB_EV_ABORT: +		break; +	case NB_EV_APPLY: +		return lib_route_map_entry_set_destroy(args); +	} + +	return NB_OK; +} + +/* + * XPath:   * /frr-route-map:lib/route-map/entry/set-action/rmap-set-action/frr-bgp-route-map:community-none   */  int lib_route_map_entry_set_action_rmap_set_action_community_none_modify( diff --git a/doc/user/bgp.rst b/doc/user/bgp.rst index e7adad91b7..6f99b41140 100644 --- a/doc/user/bgp.rst +++ b/doc/user/bgp.rst @@ -1983,6 +1983,11 @@ Using AS Path in Route Map     Prepend the existing last AS number (the leftmost ASN) to the AS_PATH.     The no form of this command removes this set operation from the route-map. +.. clicmd:: set as-path replace <any|ASN> + +   Replace a specific AS number to local AS number. ``any`` replaces each +   AS number in the AS-PATH with the local AS number. +  .. _bgp-communities-attribute:  Communities Attribute diff --git a/lib/routemap.h b/lib/routemap.h index 3ef60222bf..13dafe6849 100644 --- a/lib/routemap.h +++ b/lib/routemap.h @@ -370,6 +370,7 @@ DECLARE_QOBJ_TYPE(route_map);  	(strmatch(A, "frr-bgp-route-map:as-path-prepend"))  #define IS_SET_AS_EXCLUDE(A)                                                   \  	(strmatch(A, "frr-bgp-route-map:as-path-exclude")) +#define IS_SET_AS_REPLACE(A) (strmatch(A, "frr-bgp-route-map:as-path-replace"))  #define IS_SET_IPV6_NH_GLOBAL(A)                                               \  	(strmatch(A, "frr-bgp-route-map:ipv6-nexthop-global"))  #define IS_SET_IPV6_VPN_NH(A)                                                  \ diff --git a/lib/routemap_cli.c b/lib/routemap_cli.c index 315007be1c..ff98a14c41 100644 --- a/lib/routemap_cli.c +++ b/lib/routemap_cli.c @@ -1197,6 +1197,11 @@ void route_map_action_show(struct vty *vty, const struct lyd_node *dnode,  			yang_dnode_get_string(  				dnode,  				"./rmap-set-action/frr-bgp-route-map:exclude-as-path")); +	} else if (IS_SET_AS_REPLACE(action)) { +		vty_out(vty, " set as-path replace %s\n", +			yang_dnode_get_string( +				dnode, +				"./rmap-set-action/frr-bgp-route-map:replace-as-path"));  	} else if (IS_SET_AS_PREPEND(action)) {  		if (yang_dnode_exists(  			    dnode, diff --git a/tests/topotests/bgp_set_aspath_replace/__init__.py b/tests/topotests/bgp_set_aspath_replace/__init__.py new file mode 100644 index 0000000000..e69de29bb2 --- /dev/null +++ b/tests/topotests/bgp_set_aspath_replace/__init__.py diff --git a/tests/topotests/bgp_set_aspath_replace/r1/bgpd.conf b/tests/topotests/bgp_set_aspath_replace/r1/bgpd.conf new file mode 100644 index 0000000000..1e98f4e491 --- /dev/null +++ b/tests/topotests/bgp_set_aspath_replace/r1/bgpd.conf @@ -0,0 +1,17 @@ +! +router bgp 65001 + no bgp ebgp-requires-policy + neighbor 192.168.1.2 remote-as external + neighbor 192.168.1.2 timers 3 10 + address-family ipv4 unicast +  neighbor 192.168.1.2 route-map r2 in + exit-address-family +! +ip prefix-list p1 seq 5 permit 172.16.255.31/32 +! +route-map r2 permit 10 + match ip address prefix-list p1 + set as-path replace 65003 +route-map r2 permit 20 + set as-path replace any +! diff --git a/tests/topotests/bgp_set_aspath_replace/r1/zebra.conf b/tests/topotests/bgp_set_aspath_replace/r1/zebra.conf new file mode 100644 index 0000000000..acf120b200 --- /dev/null +++ b/tests/topotests/bgp_set_aspath_replace/r1/zebra.conf @@ -0,0 +1,6 @@ +! +interface r1-eth0 + ip address 192.168.1.1/24 +! +ip forwarding +! diff --git a/tests/topotests/bgp_set_aspath_replace/r2/bgpd.conf b/tests/topotests/bgp_set_aspath_replace/r2/bgpd.conf new file mode 100644 index 0000000000..23367f94ff --- /dev/null +++ b/tests/topotests/bgp_set_aspath_replace/r2/bgpd.conf @@ -0,0 +1,8 @@ +! +router bgp 65002 + no bgp ebgp-requires-policy + neighbor 192.168.1.1 remote-as external + neighbor 192.168.1.1 timers 3 10 + neighbor 192.168.2.1 remote-as external + neighbor 192.168.2.1 timers 3 10 +! diff --git a/tests/topotests/bgp_set_aspath_replace/r2/zebra.conf b/tests/topotests/bgp_set_aspath_replace/r2/zebra.conf new file mode 100644 index 0000000000..f229954341 --- /dev/null +++ b/tests/topotests/bgp_set_aspath_replace/r2/zebra.conf @@ -0,0 +1,9 @@ +! +interface r2-eth0 + ip address 192.168.1.2/24 +! +interface r2-eth1 + ip address 192.168.2.2/24 +! +ip forwarding +! diff --git a/tests/topotests/bgp_set_aspath_replace/r3/bgpd.conf b/tests/topotests/bgp_set_aspath_replace/r3/bgpd.conf new file mode 100644 index 0000000000..b7a7ceda13 --- /dev/null +++ b/tests/topotests/bgp_set_aspath_replace/r3/bgpd.conf @@ -0,0 +1,9 @@ +! +router bgp 65003 + no bgp ebgp-requires-policy + neighbor 192.168.2.2 remote-as external + neighbor 192.168.2.2 timers 3 10 + address-family ipv4 unicast +  redistribute connected + exit-address-family +! diff --git a/tests/topotests/bgp_set_aspath_replace/r3/zebra.conf b/tests/topotests/bgp_set_aspath_replace/r3/zebra.conf new file mode 100644 index 0000000000..3fa6c64484 --- /dev/null +++ b/tests/topotests/bgp_set_aspath_replace/r3/zebra.conf @@ -0,0 +1,10 @@ +! +int lo + ip address 172.16.255.31/32 + ip address 172.16.255.32/32 +! +interface r3-eth0 + ip address 192.168.2.1/24 +! +ip forwarding +! diff --git a/tests/topotests/bgp_set_aspath_replace/test_bgp_set_aspath_replace.py b/tests/topotests/bgp_set_aspath_replace/test_bgp_set_aspath_replace.py new file mode 100644 index 0000000000..d5549ae899 --- /dev/null +++ b/tests/topotests/bgp_set_aspath_replace/test_bgp_set_aspath_replace.py @@ -0,0 +1,103 @@ +#!/usr/bin/env python + +# +# test_bgp_set_aspath_replace.py +# +# Copyright (c) 2022 by +# Donatas Abraitis <donatas.abraitis@gmail.com> +# +# Permission to use, copy, modify, and/or distribute this software +# for any purpose with or without fee is hereby granted, provided +# that the above copyright notice and this permission notice appear +# in all copies. +# +# THE SOFTWARE IS PROVIDED "AS IS" AND NETDEF DISCLAIMS ALL WARRANTIES +# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL NETDEF BE LIABLE FOR +# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY +# DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, +# WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS +# ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE +# OF THIS SOFTWARE. +# + +""" +Test if `set as-path replace` is working correctly for route-maps. +""" + +import os +import sys +import json +import pytest +import functools + +CWD = os.path.dirname(os.path.realpath(__file__)) +sys.path.append(os.path.join(CWD, "../")) + +# pylint: disable=C0413 +from lib import topotest +from lib.topogen import Topogen, TopoRouter, get_topogen + +pytestmark = [pytest.mark.bgpd] + + +def build_topo(tgen): +    for routern in range(1, 5): +        tgen.add_router("r{}".format(routern)) + +    switch = tgen.add_switch("s1") +    switch.add_link(tgen.gears["r1"]) +    switch.add_link(tgen.gears["r2"]) + +    switch = tgen.add_switch("s2") +    switch.add_link(tgen.gears["r2"]) +    switch.add_link(tgen.gears["r3"]) + + +def setup_module(mod): +    tgen = Topogen(build_topo, mod.__name__) +    tgen.start_topology() + +    router_list = tgen.routers() + +    for i, (rname, router) in enumerate(router_list.items(), 1): +        router.load_config( +            TopoRouter.RD_ZEBRA, os.path.join(CWD, "{}/zebra.conf".format(rname)) +        ) +        router.load_config( +            TopoRouter.RD_BGP, os.path.join(CWD, "{}/bgpd.conf".format(rname)) +        ) + +    tgen.start_router() + + +def teardown_module(mod): +    tgen = get_topogen() +    tgen.stop_topology() + + +def test_bgp_maximum_prefix_out(): +    tgen = get_topogen() + +    if tgen.routers_have_failure(): +        pytest.skip(tgen.errors) + +    def _bgp_converge(router): +        output = json.loads(router.vtysh_cmd("show bgp ipv4 unicast json")) +        expected = { +            "routes": { +                "172.16.255.31/32": [{"path": "65002 65001"}], +                "172.16.255.32/32": [{"path": "65001 65001"}], +            } +        } +        return topotest.json_cmp(output, expected) + +    test_func = functools.partial(_bgp_converge, tgen.gears["r1"]) +    _, result = topotest.run_and_expect(test_func, None, count=30, wait=0.5) + +    assert result is None, "Failed overriding incoming AS-PATH with route-map" + + +if __name__ == "__main__": +    args = ["-s"] + sys.argv[1:] +    sys.exit(pytest.main(args)) diff --git a/yang/frr-bgp-route-map.yang b/yang/frr-bgp-route-map.yang index 74008bc078..eaa7891f0c 100644 --- a/yang/frr-bgp-route-map.yang +++ b/yang/frr-bgp-route-map.yang @@ -282,6 +282,12 @@ module frr-bgp-route-map {        "Set the BGP AS-path attribute";    } +  identity as-path-replace { +    base frr-route-map:rmap-set-type; +    description +      "Replace ASNs to local AS number"; +  } +    identity set-community {      base frr-route-map:rmap-set-type;      description @@ -793,6 +799,15 @@ module frr-bgp-route-map {        }      } +    case as-path-replace { +      when "derived-from-or-self(/frr-route-map:lib/frr-route-map:route-map/frr-route-map:entry/frr-route-map:set-action/frr-route-map:action, 'frr-bgp-route-map:as-path-replace')"; +      leaf replace-as-path { +        type string; +        description +          "Replace ASNs to local AS number"; +      } +    } +      case community {        when "derived-from-or-self(/frr-route-map:lib/frr-route-map:route-map/frr-route-map:entry/frr-route-map:set-action/frr-route-map:action, 'frr-bgp-route-map:set-community')";        choice community {  | 
