diff options
| -rw-r--r-- | bgpd/bgp_clist.c | 38 | ||||
| -rw-r--r-- | bgpd/bgp_clist.h | 3 | ||||
| -rw-r--r-- | bgpd/bgp_routemap.c | 125 | ||||
| -rw-r--r-- | bgpd/bgp_routemap_nb_config.c | 2 | ||||
| -rw-r--r-- | doc/user/routemap.rst | 4 | ||||
| -rw-r--r-- | lib/command.h | 4 | ||||
| -rw-r--r-- | lib/routemap.h | 2 | ||||
| -rw-r--r-- | lib/routemap_cli.c | 10 | ||||
| -rw-r--r-- | tests/topotests/bgp_extcomm_list_delete/__init__.py | 0 | ||||
| -rw-r--r-- | tests/topotests/bgp_extcomm_list_delete/r1/bgpd.conf | 20 | ||||
| -rw-r--r-- | tests/topotests/bgp_extcomm_list_delete/r1/zebra.conf | 6 | ||||
| -rw-r--r-- | tests/topotests/bgp_extcomm_list_delete/r2/bgpd.conf | 10 | ||||
| -rw-r--r-- | tests/topotests/bgp_extcomm_list_delete/r2/zebra.conf | 6 | ||||
| -rw-r--r-- | tests/topotests/bgp_extcomm_list_delete/test_bgp_extcomm-list_delete.py | 162 | ||||
| -rw-r--r-- | yang/frr-bgp-route-map.yang | 10 |
15 files changed, 401 insertions, 1 deletions
diff --git a/bgpd/bgp_clist.c b/bgpd/bgp_clist.c index f3c308afb9..3b987c2f57 100644 --- a/bgpd/bgp_clist.c +++ b/bgpd/bgp_clist.c @@ -986,6 +986,44 @@ struct lcommunity *lcommunity_list_match_delete(struct lcommunity *lcom, return lcom; } +/* Delete all permitted extended communities in the list from ecom.*/ +struct ecommunity *ecommunity_list_match_delete(struct ecommunity *ecom, + struct community_list *list) +{ + struct community_entry *entry; + uint32_t com_index_to_delete[ecom->size]; + uint8_t *ptr; + uint32_t delete_index = 0; + uint32_t i; + struct ecommunity local_ecom = {.size = 1}; + struct ecommunity_val local_eval = {0}; + + for (i = 0; i < ecom->size; i++) { + local_ecom.val = ecom->val + (i * ECOMMUNITY_SIZE); + for (entry = list->head; entry; entry = entry->next) { + if (((entry->style == EXTCOMMUNITY_LIST_STANDARD) && + ecommunity_include(entry->u.ecom, &local_ecom)) || + ((entry->style == EXTCOMMUNITY_LIST_EXPANDED) && + ecommunity_regexp_match(ecom, entry->reg))) { + if (entry->direct == COMMUNITY_PERMIT) { + com_index_to_delete[delete_index] = i; + delete_index++; + } + break; + } + } + } + + /* Delete all of the extended communities we flagged for deletion */ + for (i = delete_index; i > 0; i--) { + ptr = ecom->val + (com_index_to_delete[i-1] * ECOMMUNITY_SIZE); + memcpy(&local_eval.val, ptr, sizeof(local_eval.val)); + ecommunity_del_val(ecom, &local_eval); + } + + return ecom; +} + /* Helper to check if every octet do not exceed UINT_MAX */ bool lcommunity_list_valid(const char *community, int style) { diff --git a/bgpd/bgp_clist.h b/bgpd/bgp_clist.h index 8e5d637bab..251169876d 100644 --- a/bgpd/bgp_clist.h +++ b/bgpd/bgp_clist.h @@ -163,6 +163,9 @@ community_list_match_delete(struct community *com, struct community_list *list); extern struct lcommunity * lcommunity_list_match_delete(struct lcommunity *lcom, struct community_list *list); +extern struct ecommunity * +ecommunity_list_match_delete(struct ecommunity *ecom, + struct community_list *list); static inline uint32_t bgp_clist_hash_key(char *name) { diff --git a/bgpd/bgp_routemap.c b/bgpd/bgp_routemap.c index 0990567776..0f86cfdd2d 100644 --- a/bgpd/bgp_routemap.c +++ b/bgpd/bgp_routemap.c @@ -2852,6 +2852,83 @@ static const struct route_map_rule_cmd route_set_community_delete_cmd = { route_set_community_delete_free, }; +/* `set extcomm-list (<1-99>|<100-500>|WORD) delete' */ +static enum route_map_cmd_result_t +route_set_ecommunity_delete(void *rule, const struct prefix *prefix, + void *object) +{ + struct community_list *list; + struct ecommunity *merge; + struct ecommunity *new; + struct ecommunity *old; + struct bgp_path_info *path; + struct rmap_community *rcom = rule; + + if (!rcom) + return RMAP_OKAY; + + path = object; + list = community_list_lookup(bgp_clist, rcom->name, rcom->name_hash, + EXTCOMMUNITY_LIST_MASTER); + old = bgp_attr_get_ecommunity(path->attr); + if (list && old) { + merge = ecommunity_list_match_delete(ecommunity_dup(old), list); + new = ecommunity_uniq_sort(merge); + ecommunity_free(&merge); + + /* HACK: if the old community is not intern'd, + * we should free it here, or all reference to it may be + * lost. + * Really need to cleanup attribute caching sometime. + */ + if (old->refcnt == 0) + ecommunity_free(&old); + + if (new->size == 0) { + bgp_attr_set_ecommunity(path->attr, NULL); + ecommunity_free(&new); + } else { + bgp_attr_set_ecommunity(path->attr, new); + } + } + + return RMAP_OKAY; +} + +static void *route_set_ecommunity_delete_compile(const char *arg) +{ + struct rmap_community *rcom; + char **splits; + int num; + + frrstr_split(arg, " ", &splits, &num); + + rcom = XCALLOC(MTYPE_ROUTE_MAP_COMPILED, sizeof(struct rmap_community)); + rcom->name = XSTRDUP(MTYPE_ROUTE_MAP_COMPILED, splits[0]); + rcom->name_hash = bgp_clist_hash_key(rcom->name); + + for (int i = 0; i < num; i++) + XFREE(MTYPE_TMP, splits[i]); + XFREE(MTYPE_TMP, splits); + + return rcom; +} + +static void route_set_ecommunity_delete_free(void *rule) +{ + struct rmap_community *rcom = rule; + + XFREE(MTYPE_ROUTE_MAP_COMPILED, rcom->name); + XFREE(MTYPE_ROUTE_MAP_COMPILED, rcom); +} + +static const struct route_map_rule_cmd route_set_ecommunity_delete_cmd = { + "extended-comm-list", + route_set_ecommunity_delete, + route_set_ecommunity_delete_compile, + route_set_ecommunity_delete_free, +}; + /* `set extcommunity rt COMMUNITY' */ struct rmap_ecom_set { @@ -5522,6 +5599,51 @@ DEFUN_YANG (no_match_ecommunity, } +DEFPY_YANG (set_ecommunity_delete, + set_ecommunity_delete_cmd, + "set extended-comm-list " EXTCOMM_LIST_CMD_STR " delete", + SET_STR + "set BGP extended community list (for deletion)\n" + EXTCOMM_STD_LIST_NUM_STR + EXTCOMM_EXP_LIST_NUM_STR + EXTCOMM_LIST_NAME_STR + "Delete matching extended communities\n") +{ + const char *xpath = + "./set-action[action='frr-bgp-route-map:extended-comm-list-delete']"; + char xpath_value[XPATH_MAXLEN]; + int idx_comm_list = 2; + + nb_cli_enqueue_change(vty, xpath, NB_OP_CREATE, NULL); + + snprintf(xpath_value, sizeof(xpath_value), + "%s/rmap-set-action/frr-bgp-route-map:comm-list-name", + xpath); + nb_cli_enqueue_change(vty, xpath_value, NB_OP_MODIFY, + argv[idx_comm_list]->arg); + return nb_cli_apply_changes(vty, NULL); +} + + +DEFPY_YANG (no_set_ecommunity_delete, + no_set_ecommunity_delete_cmd, + "no set extended-comm-list [" EXTCOMM_LIST_CMD_STR "] delete", + NO_STR + SET_STR + "set BGP extended community list (for deletion)\n" + EXTCOMM_STD_LIST_NUM_STR + EXTCOMM_EXP_LIST_NUM_STR + EXTCOMM_LIST_NAME_STR + "Delete matching extended communities\n") +{ + const char *xpath = + "./set-action[action='frr-bgp-route-map:extended-comm-list-delete']"; + + nb_cli_enqueue_change(vty, xpath, NB_OP_DESTROY, NULL); + return nb_cli_apply_changes(vty, NULL); +} + + DEFUN_YANG (match_aspath, match_aspath_cmd, "match as-path AS_PATH_FILTER_NAME", @@ -7605,6 +7727,7 @@ void bgp_route_map_init(void) route_map_install_set(&route_set_aggregator_as_cmd); route_map_install_set(&route_set_community_cmd); route_map_install_set(&route_set_community_delete_cmd); + route_map_install_set(&route_set_ecommunity_delete_cmd); route_map_install_set(&route_set_lcommunity_cmd); route_map_install_set(&route_set_lcommunity_delete_cmd); route_map_install_set(&route_set_vpnv4_nexthop_cmd); @@ -7724,6 +7847,8 @@ void bgp_route_map_init(void) install_element(RMAP_NODE, &set_ecommunity_color_cmd); install_element(RMAP_NODE, &no_set_ecommunity_color_cmd); install_element(RMAP_NODE, &no_set_ecommunity_color_all_cmd); + install_element(RMAP_NODE, &set_ecommunity_delete_cmd); + install_element(RMAP_NODE, &no_set_ecommunity_delete_cmd); #ifdef KEEP_OLD_VPN_COMMANDS install_element(RMAP_NODE, &set_vpn_nexthop_cmd); install_element(RMAP_NODE, &no_set_vpn_nexthop_cmd); diff --git a/bgpd/bgp_routemap_nb_config.c b/bgpd/bgp_routemap_nb_config.c index 03b588a33b..9ef9031e54 100644 --- a/bgpd/bgp_routemap_nb_config.c +++ b/bgpd/bgp_routemap_nb_config.c @@ -2835,6 +2835,8 @@ int lib_route_map_entry_set_action_rmap_set_action_comm_list_name_modify( "../../frr-route-map:action"); if (IS_SET_COMM_LIST_DEL(action)) rhc->rhc_rule = "comm-list"; + else if (IS_SET_EXTCOMM_LIST_DEL(action)) + rhc->rhc_rule = "extended-comm-list"; else rhc->rhc_rule = "large-comm-list"; diff --git a/doc/user/routemap.rst b/doc/user/routemap.rst index bd19ae88e2..3d43e74744 100644 --- a/doc/user/routemap.rst +++ b/doc/user/routemap.rst @@ -335,6 +335,10 @@ Route Map Set Command Set the BGP community attribute. +.. clicmd:: set extended-comm-list <EXTCOMMUNITY_LIST_NAME> delete + + Set BGP extended community list for deletion. + .. clicmd:: set ipv6 next-hop local IPV6_ADDRESS Set the BGP-4+ link local IPv6 nexthop address. diff --git a/lib/command.h b/lib/command.h index 39fbfa661a..3167c652c5 100644 --- a/lib/command.h +++ b/lib/command.h @@ -416,6 +416,10 @@ struct cmd_node { #define COMMUNITY_AANN_STR "Community number where AA and NN are (0-65535)\n" #define COMMUNITY_VAL_STR \ "Community number in AA:NN format (where AA and NN are (0-65535)) or local-AS|no-advertise|no-export|internet|graceful-shutdown|accept-own-nexthop|accept-own|route-filter-translated-v4|route-filter-v4|route-filter-translated-v6|route-filter-v6|llgr-stale|no-llgr|blackhole|no-peer or additive\n" +#define EXTCOMM_LIST_CMD_STR "<(1-99)|(100-500)|EXTCOMMUNITY_LIST_NAME>" +#define EXTCOMM_STD_LIST_NUM_STR "Extended community-list number (standard)\n" +#define EXTCOMM_EXP_LIST_NUM_STR "Extended community-list number (expanded)\n" +#define EXTCOMM_LIST_NAME_STR "Extended community-list name\n" #define MPLS_TE_STR "MPLS-TE specific commands\n" #define LINK_PARAMS_STR "Configure interface link parameters\n" #define OSPF_RI_STR "OSPF Router Information specific commands\n" diff --git a/lib/routemap.h b/lib/routemap.h index a83ef9c967..08e341221d 100644 --- a/lib/routemap.h +++ b/lib/routemap.h @@ -348,6 +348,8 @@ DECLARE_QOBJ_TYPE(route_map); (strmatch(A, "frr-bgp-route-map:comm-list-delete")) #define IS_SET_LCOMM_LIST_DEL(A) \ (strmatch(A, "frr-bgp-route-map:large-comm-list-delete")) +#define IS_SET_EXTCOMM_LIST_DEL(A) \ + (strmatch(A, "frr-bgp-route-map:extended-comm-list-delete")) #define IS_SET_LCOMMUNITY(A) \ (strmatch(A, "frr-bgp-route-map:set-large-community")) #define IS_SET_COMMUNITY(A) \ diff --git a/lib/routemap_cli.c b/lib/routemap_cli.c index c1bdd28eab..a5e66880a7 100644 --- a/lib/routemap_cli.c +++ b/lib/routemap_cli.c @@ -1186,6 +1186,16 @@ void route_map_action_show(struct vty *vty, const struct lyd_node *dnode, assert(acl); vty_out(vty, " set large-comm-list %s delete\n", acl); + } else if (IS_SET_EXTCOMM_LIST_DEL(action)) { + acl = NULL; + ln = yang_dnode_get(dnode, "./rmap-set-action/frr-bgp-route-map:comm-list-name"); + + if (ln) + acl = yang_dnode_get_string(ln, NULL); + + assert(acl); + + vty_out(vty, " set extended-comm-list %s delete\n", acl); } else if (IS_SET_LCOMMUNITY(action)) { if (yang_dnode_exists( dnode, diff --git a/tests/topotests/bgp_extcomm_list_delete/__init__.py b/tests/topotests/bgp_extcomm_list_delete/__init__.py new file mode 100644 index 0000000000..e69de29bb2 --- /dev/null +++ b/tests/topotests/bgp_extcomm_list_delete/__init__.py diff --git a/tests/topotests/bgp_extcomm_list_delete/r1/bgpd.conf b/tests/topotests/bgp_extcomm_list_delete/r1/bgpd.conf new file mode 100644 index 0000000000..3394c1c568 --- /dev/null +++ b/tests/topotests/bgp_extcomm_list_delete/r1/bgpd.conf @@ -0,0 +1,20 @@ +router bgp 65000 + no bgp ebgp-requires-policy + no bgp network import-check + neighbor 192.168.255.2 remote-as 65001 + address-family ipv4 unicast + network 10.10.10.1/32 route-map r2-out-rt + network 10.10.10.2/32 route-map r2-out-soo + network 10.10.10.3/32 route-map r2-out-nt + redistribute connected + exit-address-family +! +route-map r2-out-rt permit 10 + set extcommunity rt 1.1.1.1:1 2.2.2.2:2 3.3.3.3:3 4.4.4.4:4 +! +route-map r2-out-soo permit 20 + set extcommunity soo 1.1.1.1:1 2.2.2.2:2 3.3.3.3:3 4.4.4.4:4 +! +route-map r2-out-nt permit 30 + set extcommunity nt 192.168.255.2:0 2.2.2.2:0 3.3.3.3:0 4.4.4.4:0 +! diff --git a/tests/topotests/bgp_extcomm_list_delete/r1/zebra.conf b/tests/topotests/bgp_extcomm_list_delete/r1/zebra.conf new file mode 100644 index 0000000000..e2c399e536 --- /dev/null +++ b/tests/topotests/bgp_extcomm_list_delete/r1/zebra.conf @@ -0,0 +1,6 @@ +! +interface r1-eth0 + ip address 192.168.255.1/24 +! +ip forwarding +! diff --git a/tests/topotests/bgp_extcomm_list_delete/r2/bgpd.conf b/tests/topotests/bgp_extcomm_list_delete/r2/bgpd.conf new file mode 100644 index 0000000000..ca497e6e05 --- /dev/null +++ b/tests/topotests/bgp_extcomm_list_delete/r2/bgpd.conf @@ -0,0 +1,10 @@ +router bgp 65001 + no bgp ebgp-requires-policy + neighbor 192.168.255.1 remote-as 65000 + neighbor 192.168.255.1 timers 3 10 + address-family ipv4 + neighbor 192.168.255.1 route-map r1-in in + exit-address-family +! +route-map r1-in permit 10 +! diff --git a/tests/topotests/bgp_extcomm_list_delete/r2/zebra.conf b/tests/topotests/bgp_extcomm_list_delete/r2/zebra.conf new file mode 100644 index 0000000000..606c17bec9 --- /dev/null +++ b/tests/topotests/bgp_extcomm_list_delete/r2/zebra.conf @@ -0,0 +1,6 @@ +! +interface r2-eth0 + ip address 192.168.255.2/24 +! +ip forwarding +! diff --git a/tests/topotests/bgp_extcomm_list_delete/test_bgp_extcomm-list_delete.py b/tests/topotests/bgp_extcomm_list_delete/test_bgp_extcomm-list_delete.py new file mode 100644 index 0000000000..eb05986fe1 --- /dev/null +++ b/tests/topotests/bgp_extcomm_list_delete/test_bgp_extcomm-list_delete.py @@ -0,0 +1,162 @@ +#!/usr/bin/env python +# SPDX-License-Identifier: ISC +# +# Copyright 2023 6WIND S.A. +# Authored by Farid Mihoub <farid.mihoub@6wind.com> +# + +""" +bgp_extcomm_list-delete.py: + +Test the following commands: +route-map test permit 10 + set extended-comm-list <arg> delete +""" + +import functools +import json +import os +import pytest +import re +import sys + +CWD = os.path.dirname(os.path.realpath(__file__)) +sys.path.append(os.path.join(CWD, "../")) + +# pylint: disable=C0413 +from lib.topogen import Topogen, TopoRouter, get_topogen +from lib.topolog import logger +from lib import topotest + + +pytestmark = [pytest.mark.bgpd] + + +def build_topo(tgen): + for routern in range(1, 3): + tgen.add_router("r{}".format(routern)) + + switch = tgen.add_switch("s1") + switch.add_link(tgen.gears["r1"]) + switch.add_link(tgen.gears["r2"]) + + +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_convergence(): + tgen = get_topogen() + r2 = tgen.gears["r2"] + + if tgen.routers_have_failure(): + pytest.skip(tgen.errors) + + def _bgp_converge(): + output = json.loads(r2.vtysh_cmd("show ip bgp neighbor 192.168.255.1 json")) + expected = { + "192.168.255.1": { + "bgpState": "Established", + "addressFamilyInfo": { + "ipv4Unicast": { + "acceptedPrefixCounter": 4, + } + }, + } + } + return topotest.json_cmp(output, expected) + + test_func = functools.partial(_bgp_converge) + _, result = topotest.run_and_expect(test_func, None, count=60, wait=0.5) + assert result is None, "Can't converge initially" + + +def _set_extcomm_list(gear, ecom_t, ecom): + "Set the extended community for deletion." + cmd = [ + "con t\n", + f"bgp extcommunity-list standard r1-{ecom_t} permit {ecom_t} {ecom}\n", + f"route-map r1-in permit 10\n", + f"set extended-comm-list r1-{ecom_t} delete\n", + ] + gear.vtysh_cmd("".join(cmd)) + + +def _bgp_extcomm_list_del_check(gear, prefix, ecom): + """ + Check the non-presense of the extended community for the given prefix. + """ + # get the extended community list attribute for the given prefix + output = json.loads(gear.vtysh_cmd(f"show ip bgp {prefix} json")) + ecoms = output.get("paths", [])[0].get("extendedCommunity", {}) + ecoms = ecoms.get("string") + + # ecoms might be None at the first time + if not ecoms: + return False + return re.search(ecom, ecoms) is None + + +def test_rt_extcomm_list_delete(): + tgen = get_topogen() + r2 = tgen.gears["r2"] + + # set the extended community for deletion + _set_extcomm_list(r2, "rt", "1.1.1.1:1") + + # check for the deletion of the extended community + test_func = functools.partial( + _bgp_extcomm_list_del_check, r2, "10.10.10.1/32", r"1.1.1.1:1") + _, result = topotest.run_and_expect(test_func, True, count=60, wait=0.5) + assert result, "RT extended community 1.1.1.1:1 was not stripped." + + +def test_soo_extcomm_list_delete(): + tgen = get_topogen() + r2 = tgen.gears["r2"] + + # set the extended community for deletion + _set_extcomm_list(r2, "soo", "2.2.2.2:2") + + # check for the deletion of the extended community + test_func = functools.partial( + _bgp_extcomm_list_del_check, r2, "10.10.10.2/32", r"2.2.2.2:2") + _, result = topotest.run_and_expect(test_func, True, count=60, wait=0.5) + assert result, "SoO extended community 2.2.2.2:2 was not stripped." + + +def test_nt_extcomm_list_delete(): + tgen = get_topogen() + r2 = tgen.gears["r2"] + + # set the extended community for deletion + _set_extcomm_list(r2, "nt", "3.3.3.3:0") + + # check for the deletion of the extended community + test_func = functools.partial( + _bgp_extcomm_list_del_check, r2, "10.10.10.3/32", r"3.3.3.3") + _, result = topotest.run_and_expect(test_func, True, count=60, wait=0.5) + assert result, "NT extended community 3.3.3.3:0 was not stripped." + + +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 4b6619739d..05fe57c7d7 100644 --- a/yang/frr-bgp-route-map.yang +++ b/yang/frr-bgp-route-map.yang @@ -352,6 +352,12 @@ identity set-extcommunity-color { "Set BGP large community list (for deletion)"; } + identity extended-comm-list-delete { + base frr-route-map:rmap-set-type; + description + "Set BGP extended community list (for deletion)"; + } + identity set-evpn-gateway-ip-ipv4 { base frr-route-map:rmap-set-type; description @@ -1109,7 +1115,9 @@ identity set-extcommunity-color { case comm-list-name { 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:comm-list-delete') or " - + "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:large-comm-list-delete')"; + + "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:large-comm-list-delete') or " + + "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:extended-comm-list-delete')"; leaf comm-list-name { type bgp-filter:bgp-list-name; } |
