bgpd: Create set distance XXX command for routemaps

Allow bgp to set a local Administrative distance to use
for installing routes into the rib.

Example:
!
router bgp 9323
 bgp router-id 1.2.3.4
 neighbor enp0s8 interface remote-as external
 !
 address-family ipv4 unicast
  neighbor enp0s8 route-map DISTANCE in
 exit-address-family
!
route-map DISTANCE permit 10
 set distance 153
!
line vty
!
end
eva# show ip route
Codes: K - kernel route, C - connected, S - static, R - RIP,
       O - OSPF, I - IS-IS, B - BGP, E - EIGRP, N - NHRP,
       T - Table, v - VNC, V - VNC-Direct, A - Babel, D - SHARP,
       F - PBR, f - OpenFabric,
       > - selected route, * - FIB route, q - queued route, r - rejected route

B   0.0.0.0/0 [153/0] via fe80::a00:27ff:fe84:c2d6, enp0s8, 00:00:06
K>* 0.0.0.0/0 [0/100] via 10.0.2.2, enp0s3, 00:06:31
B>* 1.1.1.1/32 [153/0] via fe80::a00:27ff:fe84:c2d6, enp0s8, 00:00:06
B>* 1.1.1.2/32 [153/0] via fe80::a00:27ff:fe84:c2d6, enp0s8, 00:00:06
B>* 1.1.1.3/32 [153/0] via fe80::a00:27ff:fe84:c2d6, enp0s8, 00:00:06
C>* 10.0.2.0/24 is directly connected, enp0s3, 00:06:31
K>* 169.254.0.0/16 [0/1000] is directly connected, enp0s3, 00:06:31
eva#

Signed-off-by: Donald Sharp <sharpd@cumulusnetworks.com>
This commit is contained in:
Donald Sharp 2019-09-13 16:43:16 -04:00
parent 921308a535
commit 7b7d48e586
4 changed files with 59 additions and 3 deletions

View file

@ -519,8 +519,7 @@ unsigned int attrhash_key_make(const void *p)
MIX(attr->mp_nexthop_len);
key = jhash(attr->mp_nexthop_global.s6_addr, IPV6_MAX_BYTELEN, key);
key = jhash(attr->mp_nexthop_local.s6_addr, IPV6_MAX_BYTELEN, key);
MIX(attr->nh_ifindex);
MIX(attr->nh_lla_ifindex);
MIX3(attr->nh_ifindex, attr->nh_lla_ifindex, attr->distance);
return key;
}
@ -562,7 +561,8 @@ bool attrhash_cmp(const void *p1, const void *p2)
&attr2->originator_id)
&& overlay_index_same(attr1, attr2)
&& attr1->nh_ifindex == attr2->nh_ifindex
&& attr1->nh_lla_ifindex == attr2->nh_lla_ifindex)
&& attr1->nh_lla_ifindex == attr2->nh_lla_ifindex
&& attr1->distance == attr2->distance)
return true;
}

View file

@ -206,6 +206,9 @@ struct attr {
/* EVPN local router-mac */
struct ethaddr rmac;
/* Distance as applied by Route map */
uint8_t distance;
};
/* rmap_change_flags definition */

View file

@ -11877,6 +11877,9 @@ uint8_t bgp_distance_apply(struct prefix *p, struct bgp_path_info *pinfo,
peer = pinfo->peer;
if (pinfo->attr->distance)
return pinfo->attr->distance;
/* Check source address. */
sockunion2hostprefix(&peer->su, &q);
rn = bgp_node_match(bgp_distance_table[afi][safi], &q);

View file

@ -1683,6 +1683,30 @@ struct route_map_rule_cmd route_set_weight_cmd = {
"weight", route_set_weight, route_value_compile, route_value_free,
};
/* `set distance DISTANCE */
static enum route_map_cmd_result_t
route_set_distance(void *rule, const struct prefix *prefix,
route_map_object_t type, void *object)
{
struct bgp_path_info *path = object;
struct rmap_value *rv = rule;
if (type != RMAP_BGP)
return RMAP_OKAY;
path->attr->distance = rv->value;
return RMAP_OKAY;
}
/* set distance rule structure */
struct route_map_rule_cmd route_set_distance_cmd = {
"distance",
route_set_distance,
route_value_compile,
route_value_free,
};
/* `set metric METRIC' */
/* Set metric to attribute. */
@ -4064,6 +4088,29 @@ DEFUN (set_ip_nexthop_unchanged,
"unchanged");
}
DEFUN (set_distance,
set_distance_cmd,
"set distance (0-255)",
SET_STR
"BGP Administrative Distance to use\n"
"Distance value\n")
{
int idx_number = 2;
return generic_set_add(vty, VTY_GET_CONTEXT(route_map_index),
"distance", argv[idx_number]->arg);
}
DEFUN (no_set_distance,
no_set_distance_cmd,
"no set distance [(0-255)]",
NO_STR SET_STR
"BGP Administrative Distance to use\n"
"Distance value\n")
{
return generic_set_delete(vty, VTY_GET_CONTEXT(route_map_index),
"distance", NULL);
}
DEFUN (set_local_pref,
set_local_pref_cmd,
@ -5119,6 +5166,7 @@ void bgp_route_map_init(void)
route_map_install_set(&route_set_weight_cmd);
route_map_install_set(&route_set_label_index_cmd);
route_map_install_set(&route_set_metric_cmd);
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_origin_cmd);
@ -5172,6 +5220,8 @@ void bgp_route_map_init(void)
install_element(RMAP_NODE, &set_ip_nexthop_peer_cmd);
install_element(RMAP_NODE, &set_ip_nexthop_unchanged_cmd);
install_element(RMAP_NODE, &set_local_pref_cmd);
install_element(RMAP_NODE, &set_distance_cmd);
install_element(RMAP_NODE, &no_set_distance_cmd);
install_element(RMAP_NODE, &no_set_local_pref_cmd);
install_element(RMAP_NODE, &set_weight_cmd);
install_element(RMAP_NODE, &set_label_index_cmd);