Skip to content

Commit

Permalink
bgpd: Move sticky, default_gw, router_flag into a single flags variable
Browse files Browse the repository at this point in the history
Instead of using 3 uint8_t variables under struct attr, let's use a single
uint8_t as the flags. Saving 2-bytes. Not a big deal, but it's even easier to
track EVPN-related flags/variables.

Signed-off-by: Donatas Abraitis <donatas@opensourcerouting.org>
  • Loading branch information
ton31337 committed Jul 2, 2024
1 parent 9ab4186 commit 73f1206
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 59 deletions.
12 changes: 5 additions & 7 deletions bgpd/bgp_attr.c
Original file line number Diff line number Diff line change
Expand Up @@ -2575,7 +2575,6 @@ bgp_attr_ext_communities(struct bgp_attr_parser_args *args)
struct peer *const peer = args->peer;
struct attr *const attr = args->attr;
const bgp_size_t length = args->length;
uint8_t sticky = 0;
bool proxy = false;
struct ecommunity *ecomm;

Expand Down Expand Up @@ -2605,21 +2604,20 @@ bgp_attr_ext_communities(struct bgp_attr_parser_args *args)
attr->df_pref = bgp_attr_df_pref_from_ec(attr, &attr->df_alg);

/* Extract MAC mobility sequence number, if any. */
attr->mm_seqnum = bgp_attr_mac_mobility_seqnum(attr, &sticky);
attr->sticky = sticky;
attr->mm_seqnum = bgp_attr_mac_mobility_seqnum(attr);

/* Check if this is a Gateway MAC-IP advertisement */
attr->default_gw = bgp_attr_default_gw(attr);
bgp_attr_default_gw(attr);

/* Handle scenario where router flag ecommunity is not
* set but default gw ext community is present.
* Use default gateway, set and propogate R-bit.
*/
if (attr->default_gw)
attr->router_flag = 1;
if (CHECK_FLAG(attr->evpn_flags, ATTR_EVPN_FLAG_DEFAULT_GW))
SET_FLAG(attr->evpn_flags, ATTR_EVPN_FLAG_ROUTER);

/* Check EVPN Neighbor advertisement flags, R-bit */
bgp_attr_evpn_na_flag(attr, &attr->router_flag, &proxy);
bgp_attr_evpn_na_flag(attr, &proxy);
if (proxy)
attr->es_flags |= ATTR_ES_PROXY_ADVERT;

Expand Down
14 changes: 6 additions & 8 deletions bgpd/bgp_attr.h
Original file line number Diff line number Diff line change
Expand Up @@ -197,9 +197,6 @@ struct attr {
#define ATTR_ES_L3_NHG_ACTIVE (1 << 6)
#define ATTR_ES_L3_NHG (ATTR_ES_L3_NHG_USE | ATTR_ES_L3_NHG_ACTIVE)

/* NA router flag (R-bit) support in EVPN */
uint8_t router_flag;

/* Distance as applied by Route map */
uint8_t distance;

Expand Down Expand Up @@ -256,11 +253,12 @@ struct attr {
/* MP Nexthop length */
uint8_t mp_nexthop_len;

/* Static MAC for EVPN */
uint8_t sticky;

/* Flag for default gateway extended community in EVPN */
uint8_t default_gw;
/* EVPN flags */
uint8_t evpn_flags;
#define ATTR_EVPN_FLAG_STICKY (1 << 0)
#define ATTR_EVPN_FLAG_DEFAULT_GW (1 << 1)
/* NA router flag (R-bit) support in EVPN */
#define ATTR_EVPN_FLAG_ROUTER (1 << 2)

/* route tag */
route_tag_t tag;
Expand Down
21 changes: 10 additions & 11 deletions bgpd/bgp_attr_evpn.c
Original file line number Diff line number Diff line change
Expand Up @@ -115,14 +115,14 @@ bool bgp_attr_rmac(struct attr *attr, struct ethaddr *rmac)
/*
* return true if attr contains default gw extended community
*/
uint8_t bgp_attr_default_gw(struct attr *attr)
void bgp_attr_default_gw(struct attr *attr)
{
struct ecommunity *ecom;
uint32_t i;

ecom = bgp_attr_get_ecommunity(attr);
if (!ecom || !ecom->size)
return 0;
return;

/* If there is a default gw extendd community return true otherwise
* return 0 */
Expand All @@ -136,10 +136,9 @@ uint8_t bgp_attr_default_gw(struct attr *attr)

if ((type == ECOMMUNITY_ENCODE_OPAQUE
&& sub_type == ECOMMUNITY_EVPN_SUBTYPE_DEF_GW))
return 1;
SET_FLAG(attr->evpn_flags, ATTR_EVPN_FLAG_DEFAULT_GW);
}

return 0;
UNSET_FLAG(attr->evpn_flags, ATTR_EVPN_FLAG_DEFAULT_GW);
}

/*
Expand Down Expand Up @@ -183,7 +182,7 @@ uint16_t bgp_attr_df_pref_from_ec(struct attr *attr, uint8_t *alg)
* Fetch and return the sequence number from MAC Mobility extended
* community, if present, else 0.
*/
uint32_t bgp_attr_mac_mobility_seqnum(struct attr *attr, uint8_t *sticky)
uint32_t bgp_attr_mac_mobility_seqnum(struct attr *attr)
{
struct ecommunity *ecom;
uint32_t i;
Expand Down Expand Up @@ -213,9 +212,9 @@ uint32_t bgp_attr_mac_mobility_seqnum(struct attr *attr, uint8_t *sticky)
flags = *pnt++;

if (flags & ECOMMUNITY_EVPN_SUBTYPE_MACMOBILITY_FLAG_STICKY)
*sticky = 1;
SET_FLAG(attr->evpn_flags, ATTR_EVPN_FLAG_STICKY);
else
*sticky = 0;
UNSET_FLAG(attr->evpn_flags, ATTR_EVPN_FLAG_STICKY);

pnt++;
pnt = ptr_get_be32(pnt, &seq_num);
Expand All @@ -229,8 +228,7 @@ uint32_t bgp_attr_mac_mobility_seqnum(struct attr *attr, uint8_t *sticky)
/*
* return true if attr contains router flag extended community
*/
void bgp_attr_evpn_na_flag(struct attr *attr,
uint8_t *router_flag, bool *proxy)
void bgp_attr_evpn_na_flag(struct attr *attr, bool *proxy)
{
struct ecommunity *ecom;
uint32_t i;
Expand All @@ -254,7 +252,8 @@ void bgp_attr_evpn_na_flag(struct attr *attr,
val = *pnt++;

if (val & ECOMMUNITY_EVPN_SUBTYPE_ND_ROUTER_FLAG)
*router_flag = 1;
SET_FLAG(attr->evpn_flags,
ATTR_EVPN_FLAG_ROUTER);

if (val & ECOMMUNITY_EVPN_SUBTYPE_PROXY_FLAG)
*proxy = true;
Expand Down
8 changes: 3 additions & 5 deletions bgpd/bgp_attr_evpn.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,10 @@ extern void bgp_add_routermac_ecom(struct attr *attr,
extern int bgp_build_evpn_prefix(int type, uint32_t eth_tag,
struct prefix *dst);
extern bool bgp_attr_rmac(struct attr *attr, struct ethaddr *rmac);
extern uint32_t bgp_attr_mac_mobility_seqnum(struct attr *attr,
uint8_t *sticky);
extern uint8_t bgp_attr_default_gw(struct attr *attr);
extern uint32_t bgp_attr_mac_mobility_seqnum(struct attr *attr);
extern void bgp_attr_default_gw(struct attr *attr);

extern void bgp_attr_evpn_na_flag(struct attr *attr, uint8_t *router_flag,
bool *proxy);
extern void bgp_attr_evpn_na_flag(struct attr *attr, bool *proxy);
extern uint16_t bgp_attr_df_pref_from_ec(struct attr *attr, uint8_t *alg);


Expand Down
52 changes: 28 additions & 24 deletions bgpd/bgp_evpn.c
Original file line number Diff line number Diff line change
Expand Up @@ -1159,7 +1159,7 @@ static void build_evpn_route_extcomm(struct bgpevpn *vpn, struct attr *attr,
}

/* Add MAC mobility (sticky) if needed. */
if (attr->sticky) {
if (CHECK_FLAG(attr->evpn_flags, ATTR_EVPN_FLAG_STICKY)) {
seqnum = 0;
memset(&ecom_sticky, 0, sizeof(ecom_sticky));
encode_mac_mobility_extcomm(1, seqnum, &eval_sticky);
Expand All @@ -1179,7 +1179,7 @@ static void build_evpn_route_extcomm(struct bgpevpn *vpn, struct attr *attr,
}

/* Add default gateway, if needed. */
if (attr->default_gw) {
if (CHECK_FLAG(attr->evpn_flags, ATTR_EVPN_FLAG_DEFAULT_GW)) {
memset(&ecom_default_gw, 0, sizeof(ecom_default_gw));
encode_default_gw_extcomm(&eval_default_gw);
ecom_default_gw.size = 1;
Expand All @@ -1191,9 +1191,12 @@ static void build_evpn_route_extcomm(struct bgpevpn *vpn, struct attr *attr,
}

proxy = !!(attr->es_flags & ATTR_ES_PROXY_ADVERT);
if (attr->router_flag || proxy) {
if (CHECK_FLAG(attr->evpn_flags, ATTR_EVPN_FLAG_ROUTER) || proxy) {
memset(&ecom_na, 0, sizeof(ecom_na));
encode_na_flag_extcomm(&eval_na, attr->router_flag, proxy);
encode_na_flag_extcomm(&eval_na,
CHECK_FLAG(attr->evpn_flags,
ATTR_EVPN_FLAG_ROUTER),
proxy);
ecom_na.size = 1;
ecom_na.unit_size = ECOMMUNITY_SIZE;
ecom_na.val = (uint8_t *)eval_na.val;
Expand Down Expand Up @@ -1278,12 +1281,15 @@ enum zclient_send_status evpn_zebra_install(struct bgp *bgp, struct bgpevpn *vpn
flags = 0;

if (pi->sub_type == BGP_ROUTE_IMPORTED) {
if (pi->attr->sticky)
if (CHECK_FLAG(pi->attr->evpn_flags,
ATTR_EVPN_FLAG_STICKY))
SET_FLAG(flags, ZEBRA_MACIP_TYPE_STICKY);
if (pi->attr->default_gw)
if (CHECK_FLAG(pi->attr->evpn_flags,
ATTR_EVPN_FLAG_DEFAULT_GW))
SET_FLAG(flags, ZEBRA_MACIP_TYPE_GW);
if (is_evpn_prefix_ipaddr_v6(p) &&
pi->attr->router_flag)
CHECK_FLAG(pi->attr->evpn_flags,
ATTR_EVPN_FLAG_ROUTER))
SET_FLAG(flags, ZEBRA_MACIP_TYPE_ROUTER_FLAG);

seq = mac_mobility_seqnum(pi->attr);
Expand Down Expand Up @@ -1837,7 +1843,8 @@ static void bgp_evpn_get_sync_info(struct bgp *bgp, esi_t *esi,
*active_on_peer = true;
}

if (second_best_path->attr->router_flag)
if (CHECK_FLAG(second_best_path->attr->evpn_flags,
ATTR_EVPN_FLAG_ROUTER))
*peer_router = true;

/* we use both proxy and non-proxy imports to
Expand Down Expand Up @@ -1937,7 +1944,6 @@ static int update_evpn_route_entry(struct bgp *bgp, struct bgpevpn *vpn,
struct attr local_attr;
struct bgp_labels bgp_labels = {};
int route_change = 1;
uint8_t sticky = 0;
const struct prefix_evpn *evp;

*pi = NULL;
Expand Down Expand Up @@ -1969,9 +1975,7 @@ static int update_evpn_route_entry(struct bgp *bgp, struct bgpevpn *vpn,
local_attr = *attr;

/* Extract MAC mobility sequence number, if any. */
local_attr.mm_seqnum =
bgp_attr_mac_mobility_seqnum(&local_attr, &sticky);
local_attr.sticky = sticky;
local_attr.mm_seqnum = bgp_attr_mac_mobility_seqnum(&local_attr);

/* Add (or update) attribute to hash. */
attr_new = bgp_attr_intern(&local_attr);
Expand Down Expand Up @@ -2066,9 +2070,8 @@ static int update_evpn_route_entry(struct bgp *bgp, struct bgpevpn *vpn,
BGP_PATH_ATTR_CHANGED);

/* Extract MAC mobility sequence number, if any. */
local_attr.mm_seqnum = bgp_attr_mac_mobility_seqnum(
&local_attr, &sticky);
local_attr.sticky = sticky;
local_attr.mm_seqnum =
bgp_attr_mac_mobility_seqnum(&local_attr);

attr_new = bgp_attr_intern(&local_attr);

Expand Down Expand Up @@ -2201,10 +2204,12 @@ static int update_evpn_route(struct bgp *bgp, struct bgpevpn *vpn,
attr.nexthop = vpn->originator_ip;
attr.mp_nexthop_global_in = vpn->originator_ip;
attr.mp_nexthop_len = BGP_ATTR_NHLEN_IPV4;
attr.sticky = CHECK_FLAG(flags, ZEBRA_MACIP_TYPE_STICKY) ? 1 : 0;
attr.default_gw = CHECK_FLAG(flags, ZEBRA_MACIP_TYPE_GW) ? 1 : 0;
attr.router_flag = CHECK_FLAG(flags,
ZEBRA_MACIP_TYPE_ROUTER_FLAG) ? 1 : 0;
if (CHECK_FLAG(flags, ZEBRA_MACIP_TYPE_STICKY))
SET_FLAG(attr.evpn_flags, ATTR_EVPN_FLAG_STICKY);
if (CHECK_FLAG(flags, ZEBRA_MACIP_TYPE_GW))
SET_FLAG(attr.evpn_flags, ATTR_EVPN_FLAG_DEFAULT_GW);
if (CHECK_FLAG(flags, ZEBRA_MACIP_TYPE_ROUTER_FLAG))
SET_FLAG(attr.evpn_flags, ATTR_EVPN_FLAG_ROUTER);
if (CHECK_FLAG(flags, ZEBRA_MACIP_TYPE_PROXY_ADVERT))
attr.es_flags |= ATTR_ES_PROXY_ADVERT;

Expand Down Expand Up @@ -2506,13 +2511,12 @@ void bgp_evpn_update_type2_route_entry(struct bgp *bgp, struct bgpevpn *vpn,
attr.nexthop = vpn->originator_ip;
attr.mp_nexthop_global_in = vpn->originator_ip;
attr.mp_nexthop_len = BGP_ATTR_NHLEN_IPV4;
attr.sticky = (local_pi->attr->sticky) ? 1 : 0;
attr.router_flag = (local_pi->attr->router_flag) ? 1 : 0;
attr.evpn_flags = local_pi->attr->evpn_flags;
attr.es_flags = local_pi->attr->es_flags;
if (local_pi->attr->default_gw) {
attr.default_gw = 1;
if (CHECK_FLAG(local_pi->attr->evpn_flags, ATTR_EVPN_FLAG_DEFAULT_GW)) {
SET_FLAG(attr.evpn_flags, ATTR_EVPN_FLAG_DEFAULT_GW);
if (is_evpn_prefix_ipaddr_v6(&evp))
attr.router_flag = 1;
SET_FLAG(attr.evpn_flags, ATTR_EVPN_FLAG_ROUTER);
}
memcpy(&attr.esi, &local_pi->attr->esi, sizeof(esi_t));
bgp_evpn_get_rmac_nexthop(vpn, &evp, &attr,
Expand Down
2 changes: 1 addition & 1 deletion bgpd/bgp_evpn_private.h
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ static inline void encode_mac_mobility_extcomm(int static_mac, uint32_t seq,
}

static inline void encode_na_flag_extcomm(struct ecommunity_val *eval,
uint8_t na_flag, bool proxy)
bool na_flag, bool proxy)
{
memset(eval, 0, sizeof(*eval));
eval->val[0] = ECOMMUNITY_ENCODE_EVPN;
Expand Down
11 changes: 8 additions & 3 deletions bgpd/bgp_route.c
Original file line number Diff line number Diff line change
Expand Up @@ -851,8 +851,13 @@ int bgp_path_info_cmp(struct bgp *bgp, struct bgp_path_info *new,
* with the
* sticky flag.
*/
if (newattr->sticky != existattr->sticky) {
if (newattr->sticky && !existattr->sticky) {
bool new_sticky = CHECK_FLAG(newattr->evpn_flags,
ATTR_EVPN_FLAG_STICKY);
bool exist_sticky = CHECK_FLAG(existattr->evpn_flags,
ATTR_EVPN_FLAG_STICKY);

if (new_sticky != exist_sticky) {
if (new_sticky && !exist_sticky) {
*reason = bgp_path_selection_evpn_sticky_mac;
if (debug)
zlog_debug(
Expand All @@ -861,7 +866,7 @@ int bgp_path_info_cmp(struct bgp *bgp, struct bgp_path_info *new,
return 1;
}

if (!newattr->sticky && existattr->sticky) {
if (!new_sticky && exist_sticky) {
*reason = bgp_path_selection_evpn_sticky_mac;
if (debug)
zlog_debug(
Expand Down

0 comments on commit 73f1206

Please sign in to comment.