Skip to content

Commit

Permalink
bgpd: Add set as-path replace <any|ASN> cmd for route-maps
Browse files Browse the repository at this point in the history
```
route-map tstas permit 10
 set as-path replace 1
exit
```

Before:

```
donatas-laptop(config-router-af)# do show ip bgp 10.10.10.10/32
BGP routing table entry for 10.10.10.10/32, version 13
Paths: (1 available, best #1, table default)
  Advertised to non peer-group peers:
  192.168.10.65
  65000 1 2 3 123
    192.168.10.65 from 192.168.10.65 (10.10.10.11)
      Origin IGP, metric 0, valid, external, best (First path received)
      Last update: Mon Apr 25 10:39:50 2022
```

After:

```
donatas-laptop(config-router-af)# do show ip bgp 10.10.10.10/32
BGP routing table entry for 10.10.10.10/32, version 15
Paths: (1 available, best #1, table default)
  Advertised to non peer-group peers:
  192.168.10.65
  65000 65010 2 3 123
    192.168.10.65 from 192.168.10.65 (10.10.10.11)
      Origin IGP, metric 0, valid, external, best (First path received)
      Last update: Mon Apr 25 10:40:16 2022
```

Signed-off-by: Donatas Abraitis <donatas@opensourcerouting.org>
  • Loading branch information
ton31337 committed Apr 25, 2022
1 parent c27892b commit 77e3d82
Show file tree
Hide file tree
Showing 18 changed files with 366 additions and 0 deletions.
22 changes: 22 additions & 0 deletions bgpd/bgp_aspath.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions bgpd/bgp_aspath.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
91 changes: 91 additions & 0 deletions bgpd/bgp_routemap.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)]",
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
7 changes: 7 additions & 0 deletions bgpd/bgp_routemap_nb.c
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,13 @@ const struct frr_yang_module_info frr_bgp_route_map_info = {
.destroy = 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",
.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 = {
Expand Down
4 changes: 4 additions & 0 deletions bgpd/bgp_routemap_nb.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
52 changes: 52 additions & 0 deletions bgpd/bgp_routemap_nb_config.c
Original file line number Diff line number Diff line change
Expand Up @@ -2207,6 +2207,58 @@ lib_route_map_entry_set_action_rmap_set_action_exclude_as_path_destroy(
return NB_OK;
}

/*
* 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
Expand Down
5 changes: 5 additions & 0 deletions doc/user/bgp.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions lib/routemap.h
Original file line number Diff line number Diff line change
Expand Up @@ -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) \
Expand Down
5 changes: 5 additions & 0 deletions lib/routemap_cli.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Empty file.
17 changes: 17 additions & 0 deletions tests/topotests/bgp_set_aspath_replace/r1/bgpd.conf
Original file line number Diff line number Diff line change
@@ -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
!
6 changes: 6 additions & 0 deletions tests/topotests/bgp_set_aspath_replace/r1/zebra.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
!
interface r1-eth0
ip address 192.168.1.1/24
!
ip forwarding
!
8 changes: 8 additions & 0 deletions tests/topotests/bgp_set_aspath_replace/r2/bgpd.conf
Original file line number Diff line number Diff line change
@@ -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
!
9 changes: 9 additions & 0 deletions tests/topotests/bgp_set_aspath_replace/r2/zebra.conf
Original file line number Diff line number Diff line change
@@ -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
!
9 changes: 9 additions & 0 deletions tests/topotests/bgp_set_aspath_replace/r3/bgpd.conf
Original file line number Diff line number Diff line change
@@ -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
!
10 changes: 10 additions & 0 deletions tests/topotests/bgp_set_aspath_replace/r3/zebra.conf
Original file line number Diff line number Diff line change
@@ -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
!
Loading

0 comments on commit 77e3d82

Please sign in to comment.