1 /* EVPN Multihoming procedures
3 * Copyright (C) 2019 Cumulus Networks, Inc.
6 * This file is part of FRR.
8 * FRRouting is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2, or (at your option) any
13 * FRRouting is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
32 #include "lib/printfrr.h"
34 #include "bgpd/bgp_attr_evpn.h"
35 #include "bgpd/bgpd.h"
36 #include "bgpd/bgp_table.h"
37 #include "bgpd/bgp_route.h"
38 #include "bgpd/bgp_attr.h"
39 #include "bgpd/bgp_mplsvpn.h"
40 #include "bgpd/bgp_evpn.h"
41 #include "bgpd/bgp_evpn_private.h"
42 #include "bgpd/bgp_evpn_mh.h"
43 #include "bgpd/bgp_ecommunity.h"
44 #include "bgpd/bgp_encap_types.h"
45 #include "bgpd/bgp_debug.h"
46 #include "bgpd/bgp_errors.h"
47 #include "bgpd/bgp_aspath.h"
48 #include "bgpd/bgp_zebra.h"
49 #include "bgpd/bgp_addpath.h"
50 #include "bgpd/bgp_label.h"
51 #include "bgpd/bgp_nht.h"
52 #include "bgpd/bgp_mpath.h"
53 #include "bgpd/bgp_trace.h"
55 static void bgp_evpn_local_es_down(struct bgp
*bgp
,
56 struct bgp_evpn_es
*es
);
57 static void bgp_evpn_local_type1_evi_route_del(struct bgp
*bgp
,
58 struct bgp_evpn_es
*es
);
59 static struct bgp_evpn_es_vtep
*bgp_evpn_es_vtep_add(struct bgp
*bgp
,
60 struct bgp_evpn_es
*es
,
61 struct in_addr vtep_ip
,
62 bool esr
, uint8_t df_alg
,
64 static void bgp_evpn_es_vtep_del(struct bgp
*bgp
,
65 struct bgp_evpn_es
*es
, struct in_addr vtep_ip
, bool esr
);
66 static void bgp_evpn_es_cons_checks_pend_add(struct bgp_evpn_es
*es
);
67 static void bgp_evpn_es_cons_checks_pend_del(struct bgp_evpn_es
*es
);
68 static struct bgp_evpn_es_evi
*
69 bgp_evpn_local_es_evi_do_del(struct bgp_evpn_es_evi
*es_evi
);
70 static uint32_t bgp_evpn_es_get_active_vtep_cnt(struct bgp_evpn_es
*es
);
71 static void bgp_evpn_l3nhg_update_on_vtep_chg(struct bgp_evpn_es
*es
);
72 static struct bgp_evpn_es
*bgp_evpn_es_new(struct bgp
*bgp
, const esi_t
*esi
);
73 static void bgp_evpn_es_free(struct bgp_evpn_es
*es
, const char *caller
);
74 static void bgp_evpn_path_es_unlink(struct bgp_path_es_info
*es_info
);
75 static void bgp_evpn_mac_update_on_es_local_chg(struct bgp_evpn_es
*es
,
78 esi_t zero_esi_buf
, *zero_esi
= &zero_esi_buf
;
79 static int bgp_evpn_run_consistency_checks(struct thread
*t
);
80 static void bgp_evpn_path_nh_info_free(struct bgp_path_evpn_nh_info
*nh_info
);
81 static void bgp_evpn_path_nh_unlink(struct bgp_path_evpn_nh_info
*nh_info
);
83 /******************************************************************************
84 * per-ES (Ethernet Segment) routing table
86 * Following routes are added to the ES's routing table -
87 * 1. Local and remote ESR (Type-4)
88 * 2. Local EAD-per-ES (Type-1).
90 * Key for these routes is {ESI, VTEP-IP} so the path selection is practically
91 * a no-op i.e. all paths lead to same VTEP-IP (i.e. result in the same VTEP
92 * being added to same ES).
94 * Note the following routes go into the VNI routing table (instead of the
96 * 1. Remote EAD-per-ES
97 * 2. Local and remote EAD-per-EVI
100 /* Calculate the best path for a multi-homing (Type-1 or Type-4) route
101 * installed in the ES's routing table.
103 static int bgp_evpn_es_route_select_install(struct bgp
*bgp
,
104 struct bgp_evpn_es
*es
,
105 struct bgp_dest
*dest
)
108 afi_t afi
= AFI_L2VPN
;
109 safi_t safi
= SAFI_EVPN
;
110 struct bgp_path_info
*old_select
; /* old best */
111 struct bgp_path_info
*new_select
; /* new best */
112 struct bgp_path_info_pair old_and_new
;
114 /* Compute the best path. */
115 bgp_best_selection(bgp
, dest
, &bgp
->maxpaths
[afi
][safi
], &old_and_new
,
117 old_select
= old_and_new
.old
;
118 new_select
= old_and_new
.new;
121 * If the best path hasn't changed - see if something needs to be
124 if (old_select
&& old_select
== new_select
125 && old_select
->type
== ZEBRA_ROUTE_BGP
126 && old_select
->sub_type
== BGP_ROUTE_IMPORTED
127 && !CHECK_FLAG(dest
->flags
, BGP_NODE_USER_CLEAR
)
128 && !CHECK_FLAG(old_select
->flags
, BGP_PATH_ATTR_CHANGED
)
129 && !bgp_addpath_is_addpath_used(&bgp
->tx_addpath
, afi
, safi
)) {
130 if (bgp_zebra_has_route_changed(old_select
)) {
131 bgp_evpn_es_vtep_add(bgp
, es
, old_select
->attr
->nexthop
,
133 old_select
->attr
->df_alg
,
134 old_select
->attr
->df_pref
);
136 UNSET_FLAG(old_select
->flags
, BGP_PATH_MULTIPATH_CHG
);
137 bgp_zebra_clear_route_change_flags(dest
);
141 /* If the user did a "clear" this flag will be set */
142 UNSET_FLAG(dest
->flags
, BGP_NODE_USER_CLEAR
);
144 /* bestpath has changed; update relevant fields and install or uninstall
145 * into the zebra RIB.
147 if (old_select
|| new_select
)
148 bgp_bump_version(dest
);
151 bgp_path_info_unset_flag(dest
, old_select
, BGP_PATH_SELECTED
);
153 bgp_path_info_set_flag(dest
, new_select
, BGP_PATH_SELECTED
);
154 bgp_path_info_unset_flag(dest
, new_select
,
155 BGP_PATH_ATTR_CHANGED
);
156 UNSET_FLAG(new_select
->flags
, BGP_PATH_MULTIPATH_CHG
);
159 if (new_select
&& new_select
->type
== ZEBRA_ROUTE_BGP
160 && new_select
->sub_type
== BGP_ROUTE_IMPORTED
) {
161 bgp_evpn_es_vtep_add(bgp
, es
, new_select
->attr
->nexthop
,
162 true /*esr */, new_select
->attr
->df_alg
,
163 new_select
->attr
->df_pref
);
165 if (old_select
&& old_select
->type
== ZEBRA_ROUTE_BGP
166 && old_select
->sub_type
== BGP_ROUTE_IMPORTED
)
167 bgp_evpn_es_vtep_del(
168 bgp
, es
, old_select
->attr
->nexthop
,
172 /* Clear any route change flags. */
173 bgp_zebra_clear_route_change_flags(dest
);
175 /* Reap old select bgp_path_info, if it has been removed */
176 if (old_select
&& CHECK_FLAG(old_select
->flags
, BGP_PATH_REMOVED
))
177 bgp_path_info_reap(dest
, old_select
);
182 /* Install Type-1/Type-4 route entry in the per-ES routing table */
183 static int bgp_evpn_es_route_install(struct bgp
*bgp
,
184 struct bgp_evpn_es
*es
, struct prefix_evpn
*p
,
185 struct bgp_path_info
*parent_pi
)
188 struct bgp_dest
*dest
= NULL
;
189 struct bgp_path_info
*pi
= NULL
;
190 struct attr
*attr_new
= NULL
;
192 /* Create (or fetch) route within the VNI.
193 * NOTE: There is no RD here.
195 dest
= bgp_node_get(es
->route_table
, (struct prefix
*)p
);
197 /* Check if route entry is already present. */
198 for (pi
= bgp_dest_get_bgp_path_info(dest
); pi
; pi
= pi
->next
)
200 && (struct bgp_path_info
*)pi
->extra
->parent
==
205 /* Add (or update) attribute to hash. */
206 attr_new
= bgp_attr_intern(parent_pi
->attr
);
208 /* Create new route with its attribute. */
209 pi
= info_make(parent_pi
->type
, BGP_ROUTE_IMPORTED
, 0,
210 parent_pi
->peer
, attr_new
, dest
);
211 SET_FLAG(pi
->flags
, BGP_PATH_VALID
);
212 bgp_path_info_extra_get(pi
);
213 pi
->extra
->parent
= bgp_path_info_lock(parent_pi
);
214 bgp_dest_lock_node((struct bgp_dest
*)parent_pi
->net
);
215 bgp_path_info_add(dest
, pi
);
217 if (attrhash_cmp(pi
->attr
, parent_pi
->attr
)
218 && !CHECK_FLAG(pi
->flags
, BGP_PATH_REMOVED
)) {
219 bgp_dest_unlock_node(dest
);
222 /* The attribute has changed. */
223 /* Add (or update) attribute to hash. */
224 attr_new
= bgp_attr_intern(parent_pi
->attr
);
226 /* Restore route, if needed. */
227 if (CHECK_FLAG(pi
->flags
, BGP_PATH_REMOVED
))
228 bgp_path_info_restore(dest
, pi
);
230 /* Mark if nexthop has changed. */
231 if (!IPV4_ADDR_SAME(&pi
->attr
->nexthop
, &attr_new
->nexthop
))
232 SET_FLAG(pi
->flags
, BGP_PATH_IGP_CHANGED
);
234 /* Unintern existing, set to new. */
235 bgp_attr_unintern(&pi
->attr
);
237 pi
->uptime
= bgp_clock();
240 /* Perform route selection and update zebra, if required. */
241 ret
= bgp_evpn_es_route_select_install(bgp
, es
, dest
);
243 bgp_dest_unlock_node(dest
);
248 /* Uninstall Type-1/Type-4 route entry from the ES routing table */
249 static int bgp_evpn_es_route_uninstall(struct bgp
*bgp
, struct bgp_evpn_es
*es
,
250 struct prefix_evpn
*p
, struct bgp_path_info
*parent_pi
)
253 struct bgp_dest
*dest
;
254 struct bgp_path_info
*pi
;
256 if (!es
->route_table
)
259 /* Locate route within the ESI.
260 * NOTE: There is no RD here.
262 dest
= bgp_node_lookup(es
->route_table
, (struct prefix
*)p
);
266 /* Find matching route entry. */
267 for (pi
= bgp_dest_get_bgp_path_info(dest
); pi
; pi
= pi
->next
)
269 && (struct bgp_path_info
*)pi
->extra
->parent
==
274 bgp_dest_unlock_node(dest
);
278 /* Mark entry for deletion */
279 bgp_path_info_delete(dest
, pi
);
281 /* Perform route selection and update zebra, if required. */
282 ret
= bgp_evpn_es_route_select_install(bgp
, es
, dest
);
284 /* Unlock route node. */
285 bgp_dest_unlock_node(dest
);
290 /* Install or unistall a Tyoe-4 route in the per-ES routing table */
291 int bgp_evpn_es_route_install_uninstall(struct bgp
*bgp
, struct bgp_evpn_es
*es
,
292 afi_t afi
, safi_t safi
, struct prefix_evpn
*evp
,
293 struct bgp_path_info
*pi
, int install
)
298 ret
= bgp_evpn_es_route_install(bgp
, es
, evp
, pi
);
300 ret
= bgp_evpn_es_route_uninstall(bgp
, es
, evp
, pi
);
305 "%u: Failed to %s EVPN %s route in ESI %s",
307 install
? "install" : "uninstall",
314 /* Delete (and withdraw) local routes for specified ES from global and ES table.
315 * Also remove all remote routes from the per ES table. Invoked when ES
318 static void bgp_evpn_es_route_del_all(struct bgp
*bgp
, struct bgp_evpn_es
*es
)
320 struct bgp_dest
*dest
;
321 struct bgp_path_info
*pi
, *nextpi
;
323 /* de-activate the ES */
324 bgp_evpn_local_es_down(bgp
, es
);
325 bgp_evpn_local_type1_evi_route_del(bgp
, es
);
327 /* Walk this ES's routing table and delete all routes. */
328 for (dest
= bgp_table_top(es
->route_table
); dest
;
329 dest
= bgp_route_next(dest
)) {
330 for (pi
= bgp_dest_get_bgp_path_info(dest
);
331 (pi
!= NULL
) && (nextpi
= pi
->next
, 1); pi
= nextpi
) {
332 bgp_path_info_delete(dest
, pi
);
333 bgp_path_info_reap(dest
, pi
);
338 /*****************************************************************************
339 * Base APIs for creating MH routes (Type-1 or Type-4) on local ethernet
343 /* create or update local EVPN type1/type4 route entry.
346 * the ES table if ESR/EAD-ES (or)
347 * the VNI table if EAD-EVI (or)
348 * the global table if ESR/EAD-ES/EAD-EVI
350 * Note: vpn is applicable only to EAD-EVI routes (NULL for EAD-ES and
353 int bgp_evpn_mh_route_update(struct bgp
*bgp
, struct bgp_evpn_es
*es
,
354 struct bgpevpn
*vpn
, afi_t afi
, safi_t safi
,
355 struct bgp_dest
*dest
, struct attr
*attr
, int add
,
356 struct bgp_path_info
**ri
, int *route_changed
)
358 struct bgp_path_info
*tmp_pi
= NULL
;
359 struct bgp_path_info
*local_pi
= NULL
; /* local route entry if any */
360 struct bgp_path_info
*remote_pi
= NULL
; /* remote route entry if any */
361 struct attr
*attr_new
= NULL
;
362 struct prefix_evpn
*evp
;
365 evp
= (struct prefix_evpn
*)bgp_dest_get_prefix(dest
);
368 /* locate the local and remote entries if any */
369 for (tmp_pi
= bgp_dest_get_bgp_path_info(dest
); tmp_pi
;
370 tmp_pi
= tmp_pi
->next
) {
371 if (tmp_pi
->peer
== bgp
->peer_self
372 && tmp_pi
->type
== ZEBRA_ROUTE_BGP
373 && tmp_pi
->sub_type
== BGP_ROUTE_STATIC
)
375 if (tmp_pi
->type
== ZEBRA_ROUTE_BGP
376 && tmp_pi
->sub_type
== BGP_ROUTE_IMPORTED
377 && CHECK_FLAG(tmp_pi
->flags
, BGP_PATH_VALID
))
381 /* we don't expect to see a remote_ri at this point as
382 * an ES route has {esi, vtep_ip} as the key in the ES-rt-table
383 * in the VNI-rt-table.
388 "%u ERROR: local es route for ESI: %s Vtep %pI4 also learnt from remote",
389 bgp
->vrf_id
, es
? es
->esi_str
: "Null",
394 if (!local_pi
&& !add
)
397 /* create or update the entry */
400 /* Add or update attribute to hash */
401 attr_new
= bgp_attr_intern(attr
);
403 /* Create new route with its attribute. */
404 tmp_pi
= info_make(ZEBRA_ROUTE_BGP
, BGP_ROUTE_STATIC
, 0,
405 bgp
->peer_self
, attr_new
, dest
);
406 SET_FLAG(tmp_pi
->flags
, BGP_PATH_VALID
);
408 if (evp
->prefix
.route_type
== BGP_EVPN_AD_ROUTE
) {
409 bgp_path_info_extra_get(tmp_pi
);
410 tmp_pi
->extra
->num_labels
= 1;
412 vni2label(vpn
->vni
, &tmp_pi
->extra
->label
[0]);
414 tmp_pi
->extra
->label
[0] = 0;
417 /* add the newly created path to the route-node */
418 bgp_path_info_add(dest
, tmp_pi
);
421 if (attrhash_cmp(tmp_pi
->attr
, attr
)
422 && !CHECK_FLAG(tmp_pi
->flags
, BGP_PATH_REMOVED
))
425 /* The attribute has changed.
426 * Add (or update) attribute to hash.
428 attr_new
= bgp_attr_intern(attr
);
429 bgp_path_info_set_flag(dest
, tmp_pi
,
430 BGP_PATH_ATTR_CHANGED
);
432 /* Restore route, if needed. */
433 if (CHECK_FLAG(tmp_pi
->flags
, BGP_PATH_REMOVED
))
434 bgp_path_info_restore(dest
, tmp_pi
);
436 /* Unintern existing, set to new. */
437 bgp_attr_unintern(&tmp_pi
->attr
);
438 tmp_pi
->attr
= attr_new
;
439 tmp_pi
->uptime
= bgp_clock();
443 if (*route_changed
) {
444 if (BGP_DEBUG(evpn_mh
, EVPN_MH_RT
))
446 "local ES %s vni %u route-type %s nexthop %pI4 updated",
447 es
? es
->esi_str
: "Null", vpn
? vpn
->vni
: 0,
448 evp
->prefix
.route_type
== BGP_EVPN_ES_ROUTE
450 : (vpn
? "ead-evi" : "ead-es"),
451 &attr
->mp_nexthop_global_in
);
454 /* Return back the route entry. */
459 /* Delete local EVPN ESR (type-4) and EAD (type-1) route
461 * Note: vpn is applicable only to EAD-EVI routes (NULL for EAD-ES and
464 static int bgp_evpn_mh_route_delete(struct bgp
*bgp
, struct bgp_evpn_es
*es
,
465 struct bgpevpn
*vpn
, struct prefix_evpn
*p
)
467 afi_t afi
= AFI_L2VPN
;
468 safi_t safi
= SAFI_EVPN
;
469 struct bgp_path_info
*pi
;
470 struct bgp_dest
*dest
= NULL
; /* dest in esi table */
471 struct bgp_dest
*global_dest
= NULL
; /* dest in global table */
472 struct bgp_table
*rt_table
;
473 struct prefix_rd
*prd
;
476 rt_table
= vpn
->route_table
;
479 rt_table
= es
->route_table
;
483 /* First, locate the route node within the ESI or VNI.
484 * If it doesn't exist, ther is nothing to do.
485 * Note: there is no RD here.
487 dest
= bgp_node_lookup(rt_table
, (struct prefix
*)p
);
491 if (BGP_DEBUG(evpn_mh
, EVPN_MH_RT
))
493 "local ES %s vni %u route-type %s nexthop %pI4 delete",
494 es
->esi_str
, vpn
? vpn
->vni
: 0,
495 p
->prefix
.route_type
== BGP_EVPN_ES_ROUTE
497 : (vpn
? "ead-evi" : "ead-es"),
500 /* Next, locate route node in the global EVPN routing table.
501 * Note that this table is a 2-level tree (RD-level + Prefix-level)
504 bgp_global_evpn_node_lookup(bgp
->rib
[afi
][safi
], afi
, safi
,
505 (const struct prefix_evpn
*)p
, prd
);
508 /* Delete route entry in the global EVPN table. */
509 delete_evpn_route_entry(bgp
, afi
, safi
, global_dest
, &pi
);
511 /* Schedule for processing - withdraws to peers happen from
515 bgp_process(bgp
, global_dest
, afi
, safi
);
516 bgp_dest_unlock_node(global_dest
);
520 * Delete route entry in the ESI or VNI routing table.
521 * This can just be removed.
523 delete_evpn_route_entry(bgp
, afi
, safi
, dest
, &pi
);
525 bgp_path_info_reap(dest
, pi
);
526 bgp_dest_unlock_node(dest
);
531 * This function is called when the VNI RD changes.
532 * Delete all EAD/EVI local routes for this VNI from the global routing table.
533 * These routes are scheduled for withdraw from peers.
535 int delete_global_ead_evi_routes(struct bgp
*bgp
, struct bgpevpn
*vpn
)
539 struct bgp_dest
*rdrn
, *rn
;
540 struct bgp_table
*table
;
541 struct bgp_path_info
*pi
;
546 /* Find the RD node for the VNI in the global table */
547 rdrn
= bgp_node_lookup(bgp
->rib
[afi
][safi
], (struct prefix
*)&vpn
->prd
);
548 if (rdrn
&& bgp_dest_has_bgp_path_info_data(rdrn
)) {
549 table
= bgp_dest_get_bgp_table_info(rdrn
);
552 * Iterate over all the routes in this table and delete EAD/EVI
555 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
556 struct prefix_evpn
*evp
= (struct prefix_evpn
*)&rn
->p
;
558 if (evp
->prefix
.route_type
!= BGP_EVPN_AD_ROUTE
)
561 delete_evpn_route_entry(bgp
, afi
, safi
, rn
, &pi
);
563 bgp_process(bgp
, rn
, afi
, safi
);
567 /* Unlock RD node. */
569 bgp_dest_unlock_node(rdrn
);
574 /*****************************************************************************
575 * Ethernet Segment (Type-4) Routes
576 * ESRs are used for DF election. Currently service-carving described in
577 * RFC 7432 is NOT supported. Instead preference based DF election is
579 * Reference: draft-ietf-bess-evpn-pref-df
581 /* Build extended community for EVPN ES (type-4) route */
582 static void bgp_evpn_type4_route_extcomm_build(struct bgp_evpn_es
*es
,
585 struct ecommunity ecom_encap
;
586 struct ecommunity ecom_es_rt
;
587 struct ecommunity ecom_df
;
588 struct ecommunity_val eval
;
589 struct ecommunity_val eval_es_rt
;
590 struct ecommunity_val eval_df
;
591 bgp_encap_types tnl_type
;
595 tnl_type
= BGP_ENCAP_TYPE_VXLAN
;
596 memset(&ecom_encap
, 0, sizeof(ecom_encap
));
597 encode_encap_extcomm(tnl_type
, &eval
);
599 ecom_encap
.unit_size
= ECOMMUNITY_SIZE
;
600 ecom_encap
.val
= (uint8_t *)eval
.val
;
601 attr
->ecommunity
= ecommunity_dup(&ecom_encap
);
604 memset(&mac
, 0, sizeof(struct ethaddr
));
605 memset(&ecom_es_rt
, 0, sizeof(ecom_es_rt
));
606 es_get_system_mac(&es
->esi
, &mac
);
607 encode_es_rt_extcomm(&eval_es_rt
, &mac
);
609 ecom_es_rt
.unit_size
= ECOMMUNITY_SIZE
;
610 ecom_es_rt
.val
= (uint8_t *)eval_es_rt
.val
;
612 ecommunity_merge(attr
->ecommunity
, &ecom_es_rt
);
614 /* DF election extended community */
615 memset(&ecom_df
, 0, sizeof(ecom_df
));
616 encode_df_elect_extcomm(&eval_df
, es
->df_pref
);
618 ecom_df
.val
= (uint8_t *)eval_df
.val
;
619 attr
->ecommunity
= ecommunity_merge(attr
->ecommunity
, &ecom_df
);
621 attr
->flag
|= ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
);
624 /* Create or update local type-4 route */
625 static int bgp_evpn_type4_route_update(struct bgp
*bgp
,
626 struct bgp_evpn_es
*es
, struct prefix_evpn
*p
)
629 int route_changed
= 0;
630 afi_t afi
= AFI_L2VPN
;
631 safi_t safi
= SAFI_EVPN
;
633 struct attr
*attr_new
= NULL
;
634 struct bgp_dest
*dest
= NULL
;
635 struct bgp_path_info
*pi
= NULL
;
637 memset(&attr
, 0, sizeof(struct attr
));
639 /* Build path-attribute for this route. */
640 bgp_attr_default_set(&attr
, BGP_ORIGIN_IGP
);
641 attr
.nexthop
= es
->originator_ip
;
642 attr
.mp_nexthop_global_in
= es
->originator_ip
;
643 attr
.mp_nexthop_len
= BGP_ATTR_NHLEN_IPV4
;
645 /* Set up extended community. */
646 bgp_evpn_type4_route_extcomm_build(es
, &attr
);
648 /* First, create (or fetch) route node within the ESI. */
649 /* NOTE: There is no RD here. */
650 dest
= bgp_node_get(es
->route_table
, (struct prefix
*)p
);
652 /* Create or update route entry. */
653 ret
= bgp_evpn_mh_route_update(bgp
, es
, NULL
, afi
, safi
, dest
, &attr
, 1,
654 &pi
, &route_changed
);
658 "%u ERROR: Failed to updated ES route ESI: %s VTEP %pI4",
659 bgp
->vrf_id
, es
->esi_str
, &es
->originator_ip
);
664 /* Perform route selection;
665 * this is just to set the flags correctly
666 * as local route in the ES always wins.
668 bgp_evpn_es_route_select_install(bgp
, es
, dest
);
669 bgp_dest_unlock_node(dest
);
671 /* If this is a new route or some attribute has changed, export the
672 * route to the global table. The route will be advertised to peers
673 * from there. Note that this table is a 2-level tree (RD-level +
674 * Prefix-level) similar to L3VPN routes.
677 struct bgp_path_info
*global_pi
;
679 dest
= bgp_global_evpn_node_get(bgp
->rib
[afi
][safi
], afi
, safi
,
681 bgp_evpn_mh_route_update(bgp
, es
, NULL
, afi
, safi
, dest
,
682 attr_new
, 1, &global_pi
,
685 /* Schedule for processing and unlock node. */
686 bgp_process(bgp
, dest
, afi
, safi
);
687 bgp_dest_unlock_node(dest
);
690 /* Unintern temporary. */
691 aspath_unintern(&attr
.aspath
);
695 /* Delete local type-4 route */
696 static int bgp_evpn_type4_route_delete(struct bgp
*bgp
,
697 struct bgp_evpn_es
*es
, struct prefix_evpn
*p
)
699 return bgp_evpn_mh_route_delete(bgp
, es
, NULL
/* l2vni */, p
);
702 /* Process remote/received EVPN type-4 route (advertise or withdraw) */
703 int bgp_evpn_type4_route_process(struct peer
*peer
, afi_t afi
, safi_t safi
,
704 struct attr
*attr
, uint8_t *pfx
, int psize
,
710 struct in_addr vtep_ip
;
711 struct prefix_rd prd
;
712 struct prefix_evpn p
;
714 /* Type-4 route should be either 23 or 35 bytes
715 * RD (8), ESI (10), ip-len (1), ip (4 or 16)
717 if (psize
!= BGP_EVPN_TYPE4_V4_PSIZE
&&
718 psize
!= BGP_EVPN_TYPE4_V6_PSIZE
) {
719 flog_err(EC_BGP_EVPN_ROUTE_INVALID
,
720 "%u:%s - Rx EVPN Type-4 NLRI with invalid length %d",
721 peer
->bgp
->vrf_id
, peer
->host
, psize
);
726 prd
.family
= AF_UNSPEC
;
728 memcpy(&prd
.val
, pfx
, RD_BYTES
);
732 memcpy(&esi
, pfx
, ESI_BYTES
);
738 if (ipaddr_len
== IPV4_MAX_BITLEN
) {
739 memcpy(&vtep_ip
, pfx
, IPV4_MAX_BYTELEN
);
742 EC_BGP_EVPN_ROUTE_INVALID
,
743 "%u:%s - Rx EVPN Type-4 NLRI with unsupported IP address length %d",
744 peer
->bgp
->vrf_id
, peer
->host
, ipaddr_len
);
748 build_evpn_type4_prefix(&p
, &esi
, vtep_ip
);
749 /* Process the route. */
751 ret
= bgp_update(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
752 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
753 &prd
, NULL
, 0, 0, NULL
);
755 ret
= bgp_withdraw(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
756 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
757 &prd
, NULL
, 0, NULL
);
762 /* Check if a prefix belongs to the local ES */
763 static bool bgp_evpn_type4_prefix_match(struct prefix_evpn
*p
,
764 struct bgp_evpn_es
*es
)
766 return (p
->prefix
.route_type
== BGP_EVPN_ES_ROUTE
) &&
767 !memcmp(&p
->prefix
.es_addr
.esi
, &es
->esi
, sizeof(esi_t
));
770 /* Import remote ESRs on local ethernet segment add */
771 static int bgp_evpn_type4_remote_routes_import(struct bgp
*bgp
,
772 struct bgp_evpn_es
*es
, bool install
)
777 struct bgp_dest
*rd_dest
, *dest
;
778 struct bgp_table
*table
;
779 struct bgp_path_info
*pi
;
784 /* Walk entire global routing table and evaluate routes which could be
785 * imported into this Ethernet Segment.
787 for (rd_dest
= bgp_table_top(bgp
->rib
[afi
][safi
]); rd_dest
;
788 rd_dest
= bgp_route_next(rd_dest
)) {
789 table
= bgp_dest_get_bgp_table_info(rd_dest
);
793 for (dest
= bgp_table_top(table
); dest
;
794 dest
= bgp_route_next(dest
)) {
795 struct prefix_evpn
*evp
=
796 (struct prefix_evpn
*)bgp_dest_get_prefix(dest
);
798 for (pi
= bgp_dest_get_bgp_path_info(dest
); pi
;
801 * Consider "valid" remote routes applicable for
804 if (!(CHECK_FLAG(pi
->flags
, BGP_PATH_VALID
)
805 && pi
->type
== ZEBRA_ROUTE_BGP
806 && pi
->sub_type
== BGP_ROUTE_NORMAL
))
809 if (!bgp_evpn_type4_prefix_match(evp
, es
))
813 ret
= bgp_evpn_es_route_install(
816 ret
= bgp_evpn_es_route_uninstall(
822 "Failed to %s EVPN %pFX route in ESI %s",
827 bgp_dest_unlock_node(rd_dest
);
828 bgp_dest_unlock_node(dest
);
837 /*****************************************************************************
838 * Ethernet Auto Discovery (EAD/Type-1) route handling
839 * There are two types of EAD routes -
840 * 1. EAD-per-ES - Key: {ESI, ET=0xffffffff}
841 * 2. EAD-per-EVI - Key: {ESI, ET=0}
844 /* Extended communities associated with EAD-per-ES */
845 static void bgp_evpn_type1_es_route_extcomm_build(struct bgp_evpn_es
*es
,
848 struct ecommunity ecom_encap
;
849 struct ecommunity ecom_esi_label
;
850 struct ecommunity_val eval
;
851 struct ecommunity_val eval_esi_label
;
852 bgp_encap_types tnl_type
;
853 struct listnode
*evi_node
, *rt_node
;
854 struct ecommunity
*ecom
;
855 struct bgp_evpn_es_evi
*es_evi
;
858 tnl_type
= BGP_ENCAP_TYPE_VXLAN
;
859 memset(&ecom_encap
, 0, sizeof(ecom_encap
));
860 encode_encap_extcomm(tnl_type
, &eval
);
862 ecom_encap
.unit_size
= ECOMMUNITY_SIZE
;
863 ecom_encap
.val
= (uint8_t *)eval
.val
;
864 attr
->ecommunity
= ecommunity_dup(&ecom_encap
);
867 encode_esi_label_extcomm(&eval_esi_label
,
868 false /*single_active*/);
869 ecom_esi_label
.size
= 1;
870 ecom_esi_label
.unit_size
= ECOMMUNITY_SIZE
;
871 ecom_esi_label
.val
= (uint8_t *)eval_esi_label
.val
;
873 ecommunity_merge(attr
->ecommunity
, &ecom_esi_label
);
875 /* Add export RTs for all L2-VNIs associated with this ES */
876 /* XXX - suppress EAD-ES advertisment if there are no EVIs associated
879 for (ALL_LIST_ELEMENTS_RO(es
->es_evi_list
,
881 if (!CHECK_FLAG(es_evi
->flags
, BGP_EVPNES_EVI_LOCAL
))
883 for (ALL_LIST_ELEMENTS_RO(es_evi
->vpn
->export_rtl
,
885 attr
->ecommunity
= ecommunity_merge(attr
->ecommunity
,
889 attr
->flag
|= ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
);
892 /* Extended communities associated with EAD-per-EVI */
893 static void bgp_evpn_type1_evi_route_extcomm_build(struct bgp_evpn_es
*es
,
894 struct bgpevpn
*vpn
, struct attr
*attr
)
896 struct ecommunity ecom_encap
;
897 struct ecommunity_val eval
;
898 bgp_encap_types tnl_type
;
899 struct listnode
*rt_node
;
900 struct ecommunity
*ecom
;
903 tnl_type
= BGP_ENCAP_TYPE_VXLAN
;
904 memset(&ecom_encap
, 0, sizeof(ecom_encap
));
905 encode_encap_extcomm(tnl_type
, &eval
);
907 ecom_encap
.unit_size
= ECOMMUNITY_SIZE
;
908 ecom_encap
.val
= (uint8_t *)eval
.val
;
909 attr
->ecommunity
= ecommunity_dup(&ecom_encap
);
911 /* Add export RTs for the L2-VNI */
912 for (ALL_LIST_ELEMENTS_RO(vpn
->export_rtl
, rt_node
, ecom
))
913 attr
->ecommunity
= ecommunity_merge(attr
->ecommunity
, ecom
);
915 attr
->flag
|= ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
);
918 /* Update EVPN EAD (type-1) route -
919 * vpn - valid for EAD-EVI routes and NULL for EAD-ES routes
921 static int bgp_evpn_type1_route_update(struct bgp
*bgp
,
922 struct bgp_evpn_es
*es
, struct bgpevpn
*vpn
,
923 struct prefix_evpn
*p
)
926 afi_t afi
= AFI_L2VPN
;
927 safi_t safi
= SAFI_EVPN
;
929 struct attr
*attr_new
= NULL
;
930 struct bgp_dest
*dest
= NULL
;
931 struct bgp_path_info
*pi
= NULL
;
932 int route_changed
= 0;
933 struct prefix_rd
*global_rd
;
935 memset(&attr
, 0, sizeof(struct attr
));
937 /* Build path-attribute for this route. */
938 bgp_attr_default_set(&attr
, BGP_ORIGIN_IGP
);
939 attr
.nexthop
= es
->originator_ip
;
940 attr
.mp_nexthop_global_in
= es
->originator_ip
;
941 attr
.mp_nexthop_len
= BGP_ATTR_NHLEN_IPV4
;
944 /* EAD-EVI route update */
946 vni2label(vpn
->vni
, &(attr
.label
));
948 /* Set up extended community */
949 bgp_evpn_type1_evi_route_extcomm_build(es
, vpn
, &attr
);
951 /* First, create (or fetch) route node within the VNI. */
952 dest
= bgp_node_get(vpn
->route_table
, (struct prefix
*)p
);
954 /* Create or update route entry. */
955 ret
= bgp_evpn_mh_route_update(bgp
, es
, vpn
, afi
, safi
, dest
,
956 &attr
, 1, &pi
, &route_changed
);
960 "%u Failed to update EAD-EVI route ESI: %s VNI %u VTEP %pI4",
961 bgp
->vrf_id
, es
->esi_str
, vpn
->vni
,
963 global_rd
= &vpn
->prd
;
965 /* EAD-ES route update */
966 /* MPLS label is 0 for EAD-ES route */
968 /* Set up extended community */
969 bgp_evpn_type1_es_route_extcomm_build(es
, &attr
);
971 /* First, create (or fetch) route node within the ES. */
972 /* NOTE: There is no RD here. */
973 /* XXX: fragment ID must be included as a part of the prefix. */
974 dest
= bgp_node_get(es
->route_table
, (struct prefix
*)p
);
976 /* Create or update route entry. */
977 ret
= bgp_evpn_mh_route_update(bgp
, es
, vpn
, afi
, safi
, dest
,
978 &attr
, 1, &pi
, &route_changed
);
982 "%u ERROR: Failed to updated EAD-EVI route ESI: %s VTEP %pI4",
983 bgp
->vrf_id
, es
->esi_str
, &es
->originator_ip
);
985 global_rd
= &es
->prd
;
992 /* Perform route selection;
993 * this is just to set the flags correctly as local route in
994 * the ES always wins.
996 evpn_route_select_install(bgp
, vpn
, dest
);
997 bgp_dest_unlock_node(dest
);
999 /* If this is a new route or some attribute has changed, export the
1000 * route to the global table. The route will be advertised to peers
1001 * from there. Note that this table is a 2-level tree (RD-level +
1002 * Prefix-level) similar to L3VPN routes.
1004 if (route_changed
) {
1005 struct bgp_path_info
*global_pi
;
1007 dest
= bgp_global_evpn_node_get(bgp
->rib
[afi
][safi
], afi
, safi
,
1009 bgp_evpn_mh_route_update(bgp
, es
, vpn
, afi
, safi
, dest
,
1010 attr_new
, 1, &global_pi
,
1013 /* Schedule for processing and unlock node. */
1014 bgp_process(bgp
, dest
, afi
, safi
);
1015 bgp_dest_unlock_node(dest
);
1018 /* Unintern temporary. */
1019 aspath_unintern(&attr
.aspath
);
1024 * This function is called when the export RT for a VNI changes.
1025 * Update all type-1 local routes for this VNI from VNI/ES tables and the global
1026 * table and advertise these routes to peers.
1029 void update_type1_routes_for_evi(struct bgp
*bgp
, struct bgpevpn
*vpn
)
1031 struct prefix_evpn p
;
1032 struct bgp_evpn_es
*es
;
1033 struct bgp_evpn_es_evi
*es_evi
;
1034 struct bgp_evpn_es_evi
*es_evi_next
;
1036 RB_FOREACH_SAFE(es_evi
, bgp_es_evi_rb_head
,
1037 &vpn
->es_evi_rb_tree
, es_evi_next
) {
1041 if (CHECK_FLAG(es
->flags
, BGP_EVPNES_OPER_UP
)) {
1042 build_evpn_type1_prefix(&p
, BGP_EVPN_AD_ES_ETH_TAG
,
1043 &es
->esi
, es
->originator_ip
);
1044 if (bgp_evpn_type1_route_update(bgp
, es
, NULL
, &p
))
1045 flog_err(EC_BGP_EVPN_ROUTE_CREATE
,
1046 "%u: EAD-ES route update failure for ESI %s VNI %u",
1047 bgp
->vrf_id
, es
->esi_str
,
1051 /* Update EAD-EVI */
1052 if (CHECK_FLAG(es
->flags
, BGP_EVPNES_ADV_EVI
)) {
1053 build_evpn_type1_prefix(&p
, BGP_EVPN_AD_EVI_ETH_TAG
,
1054 &es
->esi
, es
->originator_ip
);
1055 if (bgp_evpn_type1_route_update(bgp
, es
, es_evi
->vpn
,
1057 flog_err(EC_BGP_EVPN_ROUTE_DELETE
,
1058 "%u: EAD-EVI route update failure for ESI %s VNI %u",
1059 bgp
->vrf_id
, es
->esi_str
,
1065 /* Delete local Type-1 route */
1066 static int bgp_evpn_type1_es_route_delete(struct bgp
*bgp
,
1067 struct bgp_evpn_es
*es
, struct prefix_evpn
*p
)
1069 return bgp_evpn_mh_route_delete(bgp
, es
, NULL
/* l2vni */, p
);
1072 static int bgp_evpn_type1_evi_route_delete(struct bgp
*bgp
,
1073 struct bgp_evpn_es
*es
, struct bgpevpn
*vpn
,
1074 struct prefix_evpn
*p
)
1076 return bgp_evpn_mh_route_delete(bgp
, es
, vpn
, p
);
1079 /* Generate EAD-EVI for all VNIs */
1080 static void bgp_evpn_local_type1_evi_route_add(struct bgp
*bgp
,
1081 struct bgp_evpn_es
*es
)
1083 struct listnode
*evi_node
;
1084 struct prefix_evpn p
;
1085 struct bgp_evpn_es_evi
*es_evi
;
1087 /* EAD-per-EVI routes have been suppressed */
1088 if (!bgp_mh_info
->ead_evi_tx
)
1091 if (CHECK_FLAG(es
->flags
, BGP_EVPNES_ADV_EVI
))
1092 /* EAD-EVI route add for this ES is already done */
1095 SET_FLAG(es
->flags
, BGP_EVPNES_ADV_EVI
);
1096 build_evpn_type1_prefix(&p
, BGP_EVPN_AD_EVI_ETH_TAG
,
1097 &es
->esi
, es
->originator_ip
);
1099 for (ALL_LIST_ELEMENTS_RO(es
->es_evi_list
, evi_node
, es_evi
)) {
1100 if (!CHECK_FLAG(es_evi
->flags
, BGP_EVPNES_EVI_LOCAL
))
1102 if (bgp_evpn_type1_route_update(bgp
, es
, es_evi
->vpn
, &p
))
1103 flog_err(EC_BGP_EVPN_ROUTE_CREATE
,
1104 "%u: Type4 route creation failure for ESI %s",
1105 bgp
->vrf_id
, es
->esi_str
);
1110 * Withdraw EAD-EVI for all VNIs
1112 static void bgp_evpn_local_type1_evi_route_del(struct bgp
*bgp
,
1113 struct bgp_evpn_es
*es
)
1115 struct listnode
*evi_node
;
1116 struct prefix_evpn p
;
1117 struct bgp_evpn_es_evi
*es_evi
;
1119 /* Delete and withdraw locally learnt EAD-EVI route */
1120 if (!CHECK_FLAG(es
->flags
, BGP_EVPNES_ADV_EVI
))
1121 /* EAD-EVI route has not been advertised for this ES */
1124 UNSET_FLAG(es
->flags
, BGP_EVPNES_ADV_EVI
);
1125 build_evpn_type1_prefix(&p
, BGP_EVPN_AD_EVI_ETH_TAG
,
1126 &es
->esi
, es
->originator_ip
);
1127 for (ALL_LIST_ELEMENTS_RO(es
->es_evi_list
, evi_node
, es_evi
)) {
1128 if (!CHECK_FLAG(es_evi
->flags
, BGP_EVPNES_EVI_LOCAL
))
1130 if (bgp_evpn_mh_route_delete(bgp
, es
, es_evi
->vpn
, &p
))
1131 flog_err(EC_BGP_EVPN_ROUTE_CREATE
,
1132 "%u: Type4 route creation failure for ESI %s",
1133 bgp
->vrf_id
, es
->esi_str
);
1138 * Process received EVPN type-1 route (advertise or withdraw).
1140 int bgp_evpn_type1_route_process(struct peer
*peer
, afi_t afi
, safi_t safi
,
1141 struct attr
*attr
, uint8_t *pfx
, int psize
,
1142 uint32_t addpath_id
)
1145 struct prefix_rd prd
;
1149 struct in_addr vtep_ip
;
1150 struct prefix_evpn p
;
1152 if (psize
!= BGP_EVPN_TYPE1_PSIZE
) {
1153 flog_err(EC_BGP_EVPN_ROUTE_INVALID
,
1154 "%u:%s - Rx EVPN Type-1 NLRI with invalid length %d",
1155 peer
->bgp
->vrf_id
, peer
->host
, psize
);
1159 /* Make prefix_rd */
1160 prd
.family
= AF_UNSPEC
;
1162 memcpy(&prd
.val
, pfx
, RD_BYTES
);
1166 memcpy(&esi
, pfx
, ESI_BYTES
);
1169 /* Copy Ethernet Tag */
1170 memcpy(ð_tag
, pfx
, EVPN_ETH_TAG_BYTES
);
1171 eth_tag
= ntohl(eth_tag
);
1172 pfx
+= EVPN_ETH_TAG_BYTES
;
1174 memcpy(&label
, pfx
, BGP_LABEL_BYTES
);
1176 /* EAD route prefix doesn't include the nexthop in the global
1179 vtep_ip
.s_addr
= INADDR_ANY
;
1180 build_evpn_type1_prefix(&p
, eth_tag
, &esi
, vtep_ip
);
1181 /* Process the route. */
1183 ret
= bgp_update(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
1184 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
1185 &prd
, NULL
, 0, 0, NULL
);
1187 ret
= bgp_withdraw(peer
, (struct prefix
*)&p
, addpath_id
, attr
,
1188 afi
, safi
, ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
1189 &prd
, NULL
, 0, NULL
);
1194 /*****************************************************************************/
1195 /* Ethernet Segment Management
1196 * 1. Ethernet Segment is a collection of links attached to the same
1197 * server (MHD) or switch (MHN)
1198 * 2. An Ethernet Segment can span multiple PEs and is identified by the
1200 * 3. Local ESs are configured in zebra and sent to BGP
1201 * 4. Remote ESs are created by BGP when one or more ES-EVIs reference it i.e.
1202 * created on first reference and release on last de-reference
1203 * 5. An ES can be both local and remote. Infact most local ESs are expected
1204 * to have an ES peer.
1207 /* A list of remote VTEPs is maintained for each ES. This list includes -
1208 * 1. VTEPs for which we have imported the ESR i.e. ES-peers
1209 * 2. VTEPs that have an "active" ES-EVI VTEP i.e. EAD-per-ES and EAD-per-EVI
1210 * have been imported into one or more VNIs
1212 static int bgp_evpn_es_vtep_cmp(void *p1
, void *p2
)
1214 const struct bgp_evpn_es_vtep
*es_vtep1
= p1
;
1215 const struct bgp_evpn_es_vtep
*es_vtep2
= p2
;
1217 return es_vtep1
->vtep_ip
.s_addr
- es_vtep2
->vtep_ip
.s_addr
;
1220 static struct bgp_evpn_es_vtep
*bgp_evpn_es_vtep_new(struct bgp_evpn_es
*es
,
1221 struct in_addr vtep_ip
)
1223 struct bgp_evpn_es_vtep
*es_vtep
;
1225 es_vtep
= XCALLOC(MTYPE_BGP_EVPN_ES_VTEP
, sizeof(*es_vtep
));
1228 es_vtep
->vtep_ip
.s_addr
= vtep_ip
.s_addr
;
1229 inet_ntop(AF_INET
, &es_vtep
->vtep_ip
, es_vtep
->vtep_str
,
1230 sizeof(es_vtep
->vtep_str
));
1231 listnode_init(&es_vtep
->es_listnode
, es_vtep
);
1232 listnode_add_sort(es
->es_vtep_list
, &es_vtep
->es_listnode
);
1237 static void bgp_evpn_es_vtep_free(struct bgp_evpn_es_vtep
*es_vtep
)
1239 struct bgp_evpn_es
*es
= es_vtep
->es
;
1241 if (CHECK_FLAG(es_vtep
->flags
, BGP_EVPNES_VTEP_ESR
) ||
1243 /* as long as there is some reference we can't free it */
1246 list_delete_node(es
->es_vtep_list
, &es_vtep
->es_listnode
);
1247 XFREE(MTYPE_BGP_EVPN_ES_VTEP
, es_vtep
);
1250 /* check if VTEP is already part of the list */
1251 static struct bgp_evpn_es_vtep
*bgp_evpn_es_vtep_find(struct bgp_evpn_es
*es
,
1252 struct in_addr vtep_ip
)
1254 struct listnode
*node
= NULL
;
1255 struct bgp_evpn_es_vtep
*es_vtep
;
1257 for (ALL_LIST_ELEMENTS_RO(es
->es_vtep_list
, node
, es_vtep
)) {
1258 if (es_vtep
->vtep_ip
.s_addr
== vtep_ip
.s_addr
)
1264 /* Send the remote ES to zebra for NHG programming */
1265 static int bgp_zebra_send_remote_es_vtep(struct bgp
*bgp
,
1266 struct bgp_evpn_es_vtep
*es_vtep
, bool add
)
1268 struct bgp_evpn_es
*es
= es_vtep
->es
;
1273 if (!zclient
|| zclient
->sock
< 0)
1276 /* Don't try to register if Zebra doesn't know of this instance. */
1277 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp
)) {
1278 if (BGP_DEBUG(zebra
, ZEBRA
))
1279 zlog_debug("No zebra instance, not installing remote es %s",
1284 if (es_vtep
->flags
& BGP_EVPNES_VTEP_ESR
)
1285 flags
|= ZAPI_ES_VTEP_FLAG_ESR_RXED
;
1290 zclient_create_header(s
,
1291 add
? ZEBRA_REMOTE_ES_VTEP_ADD
: ZEBRA_REMOTE_ES_VTEP_DEL
,
1293 stream_put(s
, &es
->esi
, sizeof(esi_t
));
1294 stream_put_ipv4(s
, es_vtep
->vtep_ip
.s_addr
);
1296 stream_putl(s
, flags
);
1297 stream_putc(s
, es_vtep
->df_alg
);
1298 stream_putw(s
, es_vtep
->df_pref
);
1301 stream_putw_at(s
, 0, stream_get_endp(s
));
1303 if (BGP_DEBUG(evpn_mh
, EVPN_MH_ES
))
1304 zlog_debug("Tx %s Remote ESI %s VTEP %pI4", add
? "ADD" : "DEL",
1305 es
->esi_str
, &es_vtep
->vtep_ip
);
1307 frrtrace(3, frr_bgp
, evpn_mh_vtep_zsend
, add
, es
, es_vtep
);
1309 return zclient_send_message(zclient
);
1312 static void bgp_evpn_es_vtep_re_eval_active(struct bgp
*bgp
,
1313 struct bgp_evpn_es_vtep
*es_vtep
,
1319 old_active
= CHECK_FLAG(es_vtep
->flags
, BGP_EVPNES_VTEP_ACTIVE
);
1320 /* currently we need an active EVI reference to use the VTEP as
1321 * a nexthop. this may change...
1323 if (es_vtep
->evi_cnt
)
1324 SET_FLAG(es_vtep
->flags
, BGP_EVPNES_VTEP_ACTIVE
);
1326 UNSET_FLAG(es_vtep
->flags
, BGP_EVPNES_VTEP_ACTIVE
);
1328 new_active
= CHECK_FLAG(es_vtep
->flags
, BGP_EVPNES_VTEP_ACTIVE
);
1330 if ((old_active
!= new_active
) || (new_active
&& param_change
)) {
1332 if (BGP_DEBUG(evpn_mh
, EVPN_MH_ES
))
1333 zlog_debug("es %s vtep %pI4 %s df %u/%u",
1334 es_vtep
->es
->esi_str
, &es_vtep
->vtep_ip
,
1335 new_active
? "active" : "inactive",
1336 es_vtep
->df_alg
, es_vtep
->df_pref
);
1338 /* send remote ES to zebra */
1339 bgp_zebra_send_remote_es_vtep(bgp
, es_vtep
, new_active
);
1341 /* The NHG is updated first for efficient failover handling.
1342 * Note the NHG can be de-activated while there are bgp
1343 * routes referencing it. Zebra is capable of handling that
1344 * elegantly by holding the NHG till all routes using it are
1347 bgp_evpn_l3nhg_update_on_vtep_chg(es_vtep
->es
);
1348 /* queue up the es for background consistency checks */
1349 bgp_evpn_es_cons_checks_pend_add(es_vtep
->es
);
1353 static struct bgp_evpn_es_vtep
*bgp_evpn_es_vtep_add(struct bgp
*bgp
,
1354 struct bgp_evpn_es
*es
,
1355 struct in_addr vtep_ip
,
1356 bool esr
, uint8_t df_alg
,
1359 struct bgp_evpn_es_vtep
*es_vtep
;
1360 bool param_change
= false;
1362 es_vtep
= bgp_evpn_es_vtep_find(es
, vtep_ip
);
1365 es_vtep
= bgp_evpn_es_vtep_new(es
, vtep_ip
);
1367 if (BGP_DEBUG(evpn_mh
, EVPN_MH_ES
))
1368 zlog_debug("es %s vtep %pI4 add %s df %u/%u",
1369 es_vtep
->es
->esi_str
, &es_vtep
->vtep_ip
,
1370 esr
? "esr" : "ead", df_alg
, df_pref
);
1373 SET_FLAG(es_vtep
->flags
, BGP_EVPNES_VTEP_ESR
);
1374 if ((es_vtep
->df_pref
!= df_pref
)
1375 || (es_vtep
->df_alg
!= df_alg
)) {
1376 param_change
= true;
1377 es_vtep
->df_pref
= df_pref
;
1378 es_vtep
->df_alg
= df_alg
;
1384 bgp_evpn_es_vtep_re_eval_active(bgp
, es_vtep
, param_change
);
1389 static void bgp_evpn_es_vtep_do_del(struct bgp
*bgp
,
1390 struct bgp_evpn_es_vtep
*es_vtep
, bool esr
)
1392 bool param_change
= false;
1394 if (BGP_DEBUG(evpn_mh
, EVPN_MH_ES
))
1395 zlog_debug("es %s vtep %pI4 del %s", es_vtep
->es
->esi_str
,
1396 &es_vtep
->vtep_ip
, esr
? "esr" : "ead");
1398 UNSET_FLAG(es_vtep
->flags
, BGP_EVPNES_VTEP_ESR
);
1399 if (es_vtep
->df_pref
|| es_vtep
->df_alg
) {
1400 param_change
= true;
1401 es_vtep
->df_pref
= 0;
1402 es_vtep
->df_alg
= 0;
1405 if (es_vtep
->evi_cnt
)
1409 bgp_evpn_es_vtep_re_eval_active(bgp
, es_vtep
, param_change
);
1410 bgp_evpn_es_vtep_free(es_vtep
);
1413 static void bgp_evpn_es_vtep_del(struct bgp
*bgp
,
1414 struct bgp_evpn_es
*es
, struct in_addr vtep_ip
, bool esr
)
1416 struct bgp_evpn_es_vtep
*es_vtep
;
1418 es_vtep
= bgp_evpn_es_vtep_find(es
, vtep_ip
);
1420 bgp_evpn_es_vtep_do_del(bgp
, es_vtep
, esr
);
1423 /********************** ES MAC-IP paths *************************************
1424 * 1. Local MAC-IP routes in the VNI routing table are linked to the
1425 * destination ES (macip_evi_path_list) for efficient updates on ES oper
1427 * 2. Non-local MAC-IP routes in the global routing table are linked to
1428 * the detination for efficient updates on -
1429 * a. VTEP add/del - this results in a L3NHG update.
1430 * b. ES-VRF add/del - this may result in the host route being migrated to
1431 * L3NHG or vice versa (flat multipath list).
1432 ****************************************************************************/
1433 static void bgp_evpn_path_es_info_free(struct bgp_path_es_info
*es_info
)
1435 bgp_evpn_path_es_unlink(es_info
);
1436 XFREE(MTYPE_BGP_EVPN_PATH_ES_INFO
, es_info
);
1439 void bgp_evpn_path_mh_info_free(struct bgp_path_mh_info
*mh_info
)
1441 if (mh_info
->es_info
)
1442 bgp_evpn_path_es_info_free(mh_info
->es_info
);
1443 if (mh_info
->nh_info
)
1444 bgp_evpn_path_nh_info_free(mh_info
->nh_info
);
1445 XFREE(MTYPE_BGP_EVPN_PATH_MH_INFO
, mh_info
);
1448 static struct bgp_path_es_info
*
1449 bgp_evpn_path_es_info_new(struct bgp_path_info
*pi
, vni_t vni
)
1451 struct bgp_path_info_extra
*e
;
1452 struct bgp_path_mh_info
*mh_info
;
1453 struct bgp_path_es_info
*es_info
;
1455 e
= bgp_path_info_extra_get(pi
);
1457 /* If mh_info doesn't exist allocate it */
1458 mh_info
= e
->mh_info
;
1460 e
->mh_info
= mh_info
= XCALLOC(MTYPE_BGP_EVPN_PATH_MH_INFO
,
1461 sizeof(struct bgp_path_mh_info
));
1463 /* If es_info doesn't exist allocate it */
1464 es_info
= mh_info
->es_info
;
1466 mh_info
->es_info
= es_info
=
1467 XCALLOC(MTYPE_BGP_EVPN_PATH_ES_INFO
,
1468 sizeof(struct bgp_path_es_info
));
1476 static void bgp_evpn_path_es_unlink(struct bgp_path_es_info
*es_info
)
1478 struct bgp_evpn_es
*es
= es_info
->es
;
1479 struct bgp_path_info
*pi
;
1485 if (BGP_DEBUG(evpn_mh
, EVPN_MH_RT
))
1486 zlog_debug("vni %u path %pFX unlinked from es %s", es_info
->vni
,
1487 &pi
->net
->p
, es
->esi_str
);
1490 list_delete_node(es
->macip_evi_path_list
,
1491 &es_info
->es_listnode
);
1493 list_delete_node(es
->macip_global_path_list
,
1494 &es_info
->es_listnode
);
1498 /* if there are no other references against the ES it
1501 bgp_evpn_es_free(es
, __func__
);
1503 /* Note we don't free the path es_info on unlink; it will be freed up
1504 * along with the path.
1508 void bgp_evpn_path_es_link(struct bgp_path_info
*pi
, vni_t vni
, esi_t
*esi
)
1510 struct bgp_path_es_info
*es_info
;
1511 struct bgp_evpn_es
*es
;
1512 struct bgp
*bgp_evpn
;
1514 es_info
= (pi
->extra
&& pi
->extra
->mh_info
)
1515 ? pi
->extra
->mh_info
->es_info
1517 /* if the esi is zero just unlink the path from the old es */
1518 if (!esi
|| !memcmp(esi
, zero_esi
, sizeof(*esi
))) {
1520 bgp_evpn_path_es_unlink(es_info
);
1524 bgp_evpn
= bgp_get_evpn();
1528 /* setup es_info against the path if it doesn't aleady exist */
1530 es_info
= bgp_evpn_path_es_info_new(pi
, vni
);
1532 /* find-create ES */
1533 es
= bgp_evpn_es_find(esi
);
1535 es
= bgp_evpn_es_new(bgp_evpn
, esi
);
1538 if (es_info
->es
== es
)
1541 /* unlink old ES if any */
1542 bgp_evpn_path_es_unlink(es_info
);
1544 if (BGP_DEBUG(evpn_mh
, EVPN_MH_RT
))
1545 zlog_debug("vni %u path %pFX linked to es %s", vni
, &pi
->net
->p
,
1548 /* link mac-ip path to the new destination ES */
1550 listnode_init(&es_info
->es_listnode
, es_info
);
1552 listnode_add(es
->macip_evi_path_list
, &es_info
->es_listnode
);
1554 listnode_add(es
->macip_global_path_list
, &es_info
->es_listnode
);
1557 static bool bgp_evpn_is_macip_path(struct bgp_path_info
*pi
)
1559 struct prefix_evpn
*evp
;
1561 /* Only MAC-IP routes need to be linked (MAC-only routes can be
1562 * skipped) as these lists are maintained for managing
1563 * host routes in the tenant VRF
1565 evp
= (struct prefix_evpn
*)&pi
->net
->p
;
1566 return is_evpn_prefix_ipaddr_v4(evp
) || is_evpn_prefix_ipaddr_v6(evp
);
1569 /* When a remote ES is added to a VRF, routes using that as
1570 * a destination need to be migrated to a L3NHG or viceversa.
1571 * This is done indirectly by re-attempting an install of the
1572 * route in the associated VRFs. As a part of the VRF install use
1573 * of l3 NHG is evaluated and this results in the
1574 * attr.es_flag ATTR_ES_USE_L3_NHG being set or cleared.
1577 bgp_evpn_es_path_update_on_es_vrf_chg(struct bgp_evpn_es_vrf
*es_vrf
,
1580 struct listnode
*node
;
1581 struct bgp_path_es_info
*es_info
;
1582 struct bgp_path_info
*pi
;
1583 struct bgp_evpn_es
*es
= es_vrf
->es
;
1585 if (!bgp_mh_info
->host_routes_use_l3nhg
)
1588 if (BGP_DEBUG(evpn_mh
, EVPN_MH_RT
))
1589 zlog_debug("update paths linked to es %s on es-vrf %s %s",
1590 es
->esi_str
, es_vrf
->bgp_vrf
->name
, reason
);
1592 for (ALL_LIST_ELEMENTS_RO(es
->macip_global_path_list
, node
, es_info
)) {
1595 if (!bgp_evpn_is_macip_path(pi
))
1598 if (BGP_DEBUG(evpn_mh
, EVPN_MH_RT
))
1600 "update path %pFX linked to es %s on vrf chg",
1601 &pi
->net
->p
, es
->esi_str
);
1602 bgp_evpn_route_entry_install_if_vrf_match(es_vrf
->bgp_vrf
, pi
,
1607 /* compare ES-IDs for the global ES RB tree */
1608 static int bgp_es_rb_cmp(const struct bgp_evpn_es
*es1
,
1609 const struct bgp_evpn_es
*es2
)
1611 return memcmp(&es1
->esi
, &es2
->esi
, ESI_BYTES
);
1613 RB_GENERATE(bgp_es_rb_head
, bgp_evpn_es
, rb_node
, bgp_es_rb_cmp
);
1615 struct bgp_evpn_es
*bgp_evpn_es_find(const esi_t
*esi
)
1617 struct bgp_evpn_es tmp
;
1619 memcpy(&tmp
.esi
, esi
, sizeof(esi_t
));
1620 return RB_FIND(bgp_es_rb_head
, &bgp_mh_info
->es_rb_tree
, &tmp
);
1623 static struct bgp_evpn_es
*bgp_evpn_es_new(struct bgp
*bgp
, const esi_t
*esi
)
1625 struct bgp_evpn_es
*es
;
1630 es
= XCALLOC(MTYPE_BGP_EVPN_ES
, sizeof(struct bgp_evpn_es
));
1633 memcpy(&es
->esi
, esi
, sizeof(esi_t
));
1635 /* Initialise the VTEP list */
1636 es
->es_vtep_list
= list_new();
1637 listset_app_node_mem(es
->es_vtep_list
);
1638 es
->es_vtep_list
->cmp
= bgp_evpn_es_vtep_cmp
;
1640 esi_to_str(&es
->esi
, es
->esi_str
, sizeof(es
->esi_str
));
1642 /* Initialize the ES routing table */
1643 es
->route_table
= bgp_table_init(bgp
, AFI_L2VPN
, SAFI_EVPN
);
1645 /* Add to rb_tree */
1646 if (RB_INSERT(bgp_es_rb_head
, &bgp_mh_info
->es_rb_tree
, es
)) {
1647 XFREE(MTYPE_BGP_EVPN_ES
, es
);
1651 /* Initialise the ES-EVI list */
1652 es
->es_evi_list
= list_new();
1653 listset_app_node_mem(es
->es_evi_list
);
1655 /* Initialise the ES-VRF list used for L3NHG management */
1656 es
->es_vrf_list
= list_new();
1657 listset_app_node_mem(es
->es_vrf_list
);
1659 /* Initialise the route list used for efficient event handling */
1660 es
->macip_evi_path_list
= list_new();
1661 listset_app_node_mem(es
->macip_evi_path_list
);
1662 es
->macip_global_path_list
= list_new();
1663 listset_app_node_mem(es
->macip_global_path_list
);
1665 QOBJ_REG(es
, bgp_evpn_es
);
1670 /* Free a given ES -
1671 * This just frees appropriate memory, caller should have taken other
1674 static void bgp_evpn_es_free(struct bgp_evpn_es
*es
, const char *caller
)
1676 if ((es
->flags
& (BGP_EVPNES_LOCAL
| BGP_EVPNES_REMOTE
))
1677 || listcount(es
->macip_evi_path_list
)
1678 || listcount(es
->macip_global_path_list
))
1681 if (BGP_DEBUG(evpn_mh
, EVPN_MH_ES
))
1682 zlog_debug("%s: es %s free", caller
, es
->esi_str
);
1684 /* cleanup resources maintained against the ES */
1685 list_delete(&es
->es_evi_list
);
1686 list_delete(&es
->es_vrf_list
);
1687 list_delete(&es
->es_vtep_list
);
1688 list_delete(&es
->macip_evi_path_list
);
1689 list_delete(&es
->macip_global_path_list
);
1690 bgp_table_unlock(es
->route_table
);
1692 /* remove the entry from various databases */
1693 RB_REMOVE(bgp_es_rb_head
, &bgp_mh_info
->es_rb_tree
, es
);
1694 bgp_evpn_es_cons_checks_pend_del(es
);
1697 XFREE(MTYPE_BGP_EVPN_ES
, es
);
1700 static inline bool bgp_evpn_is_es_local_and_non_bypass(struct bgp_evpn_es
*es
)
1702 return (es
->flags
& BGP_EVPNES_LOCAL
)
1703 && !(es
->flags
& BGP_EVPNES_BYPASS
);
1706 /* init local info associated with the ES */
1707 static void bgp_evpn_es_local_info_set(struct bgp
*bgp
, struct bgp_evpn_es
*es
)
1709 char buf
[BGP_EVPN_PREFIX_RD_LEN
];
1713 if (CHECK_FLAG(es
->flags
, BGP_EVPNES_LOCAL
))
1716 old_is_local
= bgp_evpn_is_es_local_and_non_bypass(es
);
1717 SET_FLAG(es
->flags
, BGP_EVPNES_LOCAL
);
1719 listnode_init(&es
->es_listnode
, es
);
1720 listnode_add(bgp_mh_info
->local_es_list
, &es
->es_listnode
);
1722 /* auto derive RD for this es */
1723 bf_assign_index(bm
->rd_idspace
, es
->rd_id
);
1724 es
->prd
.family
= AF_UNSPEC
;
1725 es
->prd
.prefixlen
= 64;
1726 snprintfrr(buf
, sizeof(buf
), "%pI4:%hu", &bgp
->router_id
, es
->rd_id
);
1727 (void)str2prefix_rd(buf
, &es
->prd
);
1729 is_local
= bgp_evpn_is_es_local_and_non_bypass(es
);
1730 if (old_is_local
!= is_local
)
1731 bgp_evpn_mac_update_on_es_local_chg(es
, is_local
);
1734 /* clear any local info associated with the ES */
1735 static void bgp_evpn_es_local_info_clear(struct bgp_evpn_es
*es
, bool finish
)
1740 if (!CHECK_FLAG(es
->flags
, BGP_EVPNES_LOCAL
))
1743 old_is_local
= bgp_evpn_is_es_local_and_non_bypass(es
);
1744 UNSET_FLAG(es
->flags
, BGP_EVPNES_LOCAL
);
1746 is_local
= bgp_evpn_is_es_local_and_non_bypass(es
);
1747 if (!finish
&& (old_is_local
!= is_local
))
1748 bgp_evpn_mac_update_on_es_local_chg(es
, is_local
);
1750 /* remove from the ES local list */
1751 list_delete_node(bgp_mh_info
->local_es_list
, &es
->es_listnode
);
1753 bf_release_index(bm
->rd_idspace
, es
->rd_id
);
1755 bgp_evpn_es_free(es
, __func__
);
1758 /* eval remote info associated with the ES */
1759 static void bgp_evpn_es_remote_info_re_eval(struct bgp_evpn_es
*es
)
1761 if (es
->remote_es_evi_cnt
) {
1762 SET_FLAG(es
->flags
, BGP_EVPNES_REMOTE
);
1764 if (CHECK_FLAG(es
->flags
, BGP_EVPNES_REMOTE
)) {
1765 UNSET_FLAG(es
->flags
, BGP_EVPNES_REMOTE
);
1766 bgp_evpn_es_free(es
, __func__
);
1771 /* If ES is present and local it needs to be active/oper-up for
1774 bool bgp_evpn_es_add_l3_ecomm_ok(esi_t
*esi
)
1776 struct bgp_evpn_es
*es
;
1778 if (!esi
|| !bgp_mh_info
->suppress_l3_ecomm_on_inactive_es
)
1781 es
= bgp_evpn_es_find(esi
);
1783 return (!es
|| !(es
->flags
& BGP_EVPNES_LOCAL
)
1784 || bgp_evpn_local_es_is_active(es
));
1787 static bool bgp_evpn_is_valid_local_path(struct bgp_path_info
*pi
)
1789 return (CHECK_FLAG(pi
->flags
, BGP_PATH_VALID
)
1790 && pi
->type
== ZEBRA_ROUTE_BGP
1791 && pi
->sub_type
== BGP_ROUTE_STATIC
);
1794 /* Update all local MAC-IP routes in the VNI routing table associated
1795 * with the ES. When the ES is down the routes are advertised without
1798 static void bgp_evpn_mac_update_on_es_oper_chg(struct bgp_evpn_es
*es
)
1800 struct listnode
*node
;
1801 struct bgp_path_es_info
*es_info
;
1802 struct bgp_path_info
*pi
;
1804 struct bgpevpn
*vpn
;
1806 if (!bgp_mh_info
->suppress_l3_ecomm_on_inactive_es
)
1809 if (BGP_DEBUG(evpn_mh
, EVPN_MH_ES
))
1810 zlog_debug("update paths linked to es %s on oper chg",
1813 bgp
= bgp_get_evpn();
1814 for (ALL_LIST_ELEMENTS_RO(es
->macip_evi_path_list
, node
, es_info
)) {
1817 if (!bgp_evpn_is_valid_local_path(pi
))
1820 if (!bgp_evpn_is_macip_path(pi
))
1823 vpn
= bgp_evpn_lookup_vni(bgp
, es_info
->vni
);
1827 if (BGP_DEBUG(evpn_mh
, EVPN_MH_RT
))
1829 "update path %d %pFX linked to es %s on oper chg",
1830 es_info
->vni
, &pi
->net
->p
, es
->esi_str
);
1832 bgp_evpn_update_type2_route_entry(bgp
, vpn
, pi
->net
, pi
,
1837 static bool bgp_evpn_is_valid_bgp_path(struct bgp_path_info
*pi
)
1839 return (CHECK_FLAG(pi
->flags
, BGP_PATH_VALID
)
1840 && pi
->type
== ZEBRA_ROUTE_BGP
1841 && pi
->sub_type
== BGP_ROUTE_NORMAL
);
1844 /* If an ES is no longer local (or becomes local) we need to re-install
1845 * paths using that ES as destination. This is needed as the criteria
1846 * for best path selection has changed.
1848 static void bgp_evpn_mac_update_on_es_local_chg(struct bgp_evpn_es
*es
,
1851 struct listnode
*node
;
1852 struct bgp_path_es_info
*es_info
;
1853 struct bgp_path_info
*pi
;
1855 struct attr
*attr_new
;
1856 struct attr attr_tmp
;
1858 if (BGP_DEBUG(evpn_mh
, EVPN_MH_ES
))
1859 zlog_debug("update paths linked to es %s on chg to %s",
1860 es
->esi_str
, is_local
? "local" : "non-local");
1862 for (ALL_LIST_ELEMENTS_RO(es
->macip_global_path_list
, node
, es_info
)) {
1865 /* Consider "valid" remote routes */
1866 if (!bgp_evpn_is_valid_bgp_path(pi
))
1872 tmp_local
= !!(pi
->attr
->es_flags
& ATTR_ES_IS_LOCAL
);
1873 if (tmp_local
== is_local
)
1876 if (BGP_DEBUG(evpn_mh
, EVPN_MH_RT
))
1878 "update path %pFX linked to es %s on chg to %s",
1879 &pi
->net
->p
, es
->esi_str
,
1880 is_local
? "local" : "non-local");
1882 attr_tmp
= *pi
->attr
;
1884 attr_tmp
.es_flags
|= ATTR_ES_IS_LOCAL
;
1886 attr_tmp
.es_flags
&= ~ATTR_ES_IS_LOCAL
;
1887 attr_new
= bgp_attr_intern(&attr_tmp
);
1888 bgp_attr_unintern(&pi
->attr
);
1889 pi
->attr
= attr_new
;
1890 bgp_evpn_import_type2_route(pi
, 1);
1894 static void bgp_evpn_local_es_deactivate(struct bgp
*bgp
,
1895 struct bgp_evpn_es
*es
)
1897 struct prefix_evpn p
;
1901 /* Delete and withdraw locally learnt ES route */
1902 build_evpn_type4_prefix(&p
, &es
->esi
, es
->originator_ip
);
1903 ret
= bgp_evpn_type4_route_delete(bgp
, es
, &p
);
1905 flog_err(EC_BGP_EVPN_ROUTE_DELETE
,
1906 "%u failed to delete type-4 route for ESI %s",
1907 bgp
->vrf_id
, es
->esi_str
);
1910 /* withdraw EAD-EVI */
1911 if (!bgp_mh_info
->ead_evi_adv_for_down_links
)
1912 bgp_evpn_local_type1_evi_route_del(bgp
, es
);
1914 /* withdraw EAD-ES */
1915 build_evpn_type1_prefix(&p
, BGP_EVPN_AD_ES_ETH_TAG
,
1916 &es
->esi
, es
->originator_ip
);
1917 ret
= bgp_evpn_type1_es_route_delete(bgp
, es
, &p
);
1919 flog_err(EC_BGP_EVPN_ROUTE_DELETE
,
1920 "%u failed to delete type-1 route for ESI %s",
1921 bgp
->vrf_id
, es
->esi_str
);
1924 bgp_evpn_mac_update_on_es_oper_chg(es
);
1927 /* Process ES link oper-down by withdrawing ES-EAD and ESR */
1928 static void bgp_evpn_local_es_down(struct bgp
*bgp
, struct bgp_evpn_es
*es
)
1932 if (!CHECK_FLAG(es
->flags
, BGP_EVPNES_OPER_UP
))
1935 old_active
= bgp_evpn_local_es_is_active(es
);
1936 UNSET_FLAG(es
->flags
, BGP_EVPNES_OPER_UP
);
1938 if (BGP_DEBUG(evpn_mh
, EVPN_MH_ES
))
1939 zlog_debug("local es %s down", es
->esi_str
);
1942 bgp_evpn_local_es_deactivate(bgp
, es
);
1945 static void bgp_evpn_local_es_activate(struct bgp
*bgp
, struct bgp_evpn_es
*es
,
1946 bool regen_ead
, bool regen_esr
)
1948 struct prefix_evpn p
;
1951 if (BGP_DEBUG(evpn_mh
, EVPN_MH_ES
))
1952 zlog_debug("local es %s generate ESR", es
->esi_str
);
1954 build_evpn_type4_prefix(&p
, &es
->esi
, es
->originator_ip
);
1955 if (bgp_evpn_type4_route_update(bgp
, es
, &p
))
1956 flog_err(EC_BGP_EVPN_ROUTE_CREATE
,
1957 "%u: Type4 route creation failure for ESI %s",
1958 bgp
->vrf_id
, es
->esi_str
);
1962 if (BGP_DEBUG(evpn_mh
, EVPN_MH_ES
))
1963 zlog_debug("local es %s generate EAD", es
->esi_str
);
1964 /* generate EAD-EVI */
1965 bgp_evpn_local_type1_evi_route_add(bgp
, es
);
1967 /* generate EAD-ES */
1968 build_evpn_type1_prefix(&p
, BGP_EVPN_AD_ES_ETH_TAG
, &es
->esi
,
1970 (void)bgp_evpn_type1_route_update(bgp
, es
, NULL
, &p
);
1973 bgp_evpn_mac_update_on_es_oper_chg(es
);
1976 /* Process ES link oper-up by generating ES-EAD and ESR */
1977 static void bgp_evpn_local_es_up(struct bgp
*bgp
, struct bgp_evpn_es
*es
,
1980 bool regen_ead
= false;
1981 bool active
= false;
1983 if (!CHECK_FLAG(es
->flags
, BGP_EVPNES_OPER_UP
)) {
1984 if (BGP_DEBUG(evpn_mh
, EVPN_MH_ES
))
1985 zlog_debug("local es %s up", es
->esi_str
);
1987 SET_FLAG(es
->flags
, BGP_EVPNES_OPER_UP
);
1992 active
= bgp_evpn_local_es_is_active(es
);
1993 if (active
&& (regen_ead
|| regen_esr
))
1994 bgp_evpn_local_es_activate(bgp
, es
, regen_ead
, regen_esr
);
1997 /* If an ethernet segment is in LACP bypass we cannot advertise
1998 * reachability to it i.e. EAD-per-ES and ESR is not advertised in
2000 * PS: EAD-per-EVI will continue to be advertised
2002 static void bgp_evpn_local_es_bypass_update(struct bgp
*bgp
,
2003 struct bgp_evpn_es
*es
, bool bypass
)
2005 bool old_bypass
= !!(es
->flags
& BGP_EVPNES_BYPASS
);
2011 if (bypass
== old_bypass
)
2014 old_active
= bgp_evpn_local_es_is_active(es
);
2015 old_is_local
= bgp_evpn_is_es_local_and_non_bypass(es
);
2017 SET_FLAG(es
->flags
, BGP_EVPNES_BYPASS
);
2019 UNSET_FLAG(es
->flags
, BGP_EVPNES_BYPASS
);
2021 if (BGP_DEBUG(evpn_mh
, EVPN_MH_ES
))
2022 zlog_debug("local es %s bypass %s", es
->esi_str
,
2023 bypass
? "set" : "clear");
2025 new_active
= bgp_evpn_local_es_is_active(es
);
2026 if (old_active
!= new_active
) {
2028 bgp_evpn_local_es_activate(bgp
, es
, true, true);
2030 bgp_evpn_local_es_deactivate(bgp
, es
);
2033 is_local
= bgp_evpn_is_es_local_and_non_bypass(es
);
2034 if (old_is_local
!= is_local
)
2035 bgp_evpn_mac_update_on_es_local_chg(es
, is_local
);
2038 static void bgp_evpn_local_es_do_del(struct bgp
*bgp
, struct bgp_evpn_es
*es
)
2040 struct bgp_evpn_es_evi
*es_evi
;
2041 struct listnode
*evi_node
, *evi_next_node
;
2043 if (BGP_DEBUG(evpn_mh
, EVPN_MH_ES
))
2044 zlog_debug("del local es %s", es
->esi_str
);
2046 /* Delete all local EVPN ES routes from ESI table
2047 * and schedule for processing (to withdraw from peers))
2049 bgp_evpn_es_route_del_all(bgp
, es
);
2051 /* release all local ES EVIs associated with the ES */
2052 for (ALL_LIST_ELEMENTS(es
->es_evi_list
, evi_node
,
2053 evi_next_node
, es_evi
)) {
2054 bgp_evpn_local_es_evi_do_del(es_evi
);
2057 /* Clear local info associated with the ES and free it up if there is
2058 * no remote reference
2060 bgp_evpn_es_local_info_clear(es
, false);
2063 bool bgp_evpn_is_esi_local_and_non_bypass(esi_t
*esi
)
2065 struct bgp_evpn_es
*es
= NULL
;
2067 /* Lookup ESI hash - should exist. */
2068 es
= bgp_evpn_es_find(esi
);
2070 return es
&& bgp_evpn_is_es_local_and_non_bypass(es
);
2073 int bgp_evpn_local_es_del(struct bgp
*bgp
, esi_t
*esi
)
2075 struct bgp_evpn_es
*es
= NULL
;
2077 /* Lookup ESI hash - should exist. */
2078 es
= bgp_evpn_es_find(esi
);
2080 flog_warn(EC_BGP_EVPN_ESI
, "%u: ES missing at local ES DEL",
2085 bgp_evpn_local_es_do_del(bgp
, es
);
2089 /* Handle device to ES id association. Results in the creation of a local
2092 int bgp_evpn_local_es_add(struct bgp
*bgp
, esi_t
*esi
,
2093 struct in_addr originator_ip
, bool oper_up
,
2094 uint16_t df_pref
, bool bypass
)
2096 char buf
[ESI_STR_LEN
];
2097 struct bgp_evpn_es
*es
;
2099 bool regen_esr
= false;
2101 /* create the new es */
2102 es
= bgp_evpn_es_find(esi
);
2104 if (CHECK_FLAG(es
->flags
, BGP_EVPNES_LOCAL
))
2107 es
= bgp_evpn_es_new(bgp
, esi
);
2109 flog_err(EC_BGP_ES_CREATE
,
2110 "%u: Failed to allocate ES entry for ESI %s - at Local ES Add",
2111 bgp
->vrf_id
, esi_to_str(esi
, buf
, sizeof(buf
)));
2116 if (BGP_DEBUG(evpn_mh
, EVPN_MH_ES
))
2117 zlog_debug("add local es %s orig-ip %pI4 df_pref %u %s",
2118 es
->esi_str
, &originator_ip
, df_pref
,
2119 bypass
? "bypass" : "");
2121 es
->originator_ip
= originator_ip
;
2122 if (df_pref
!= es
->df_pref
) {
2123 es
->df_pref
= df_pref
;
2126 bgp_evpn_es_local_info_set(bgp
, es
);
2128 /* import all remote Type-4 routes in the ES table */
2130 bgp_evpn_type4_remote_routes_import(bgp
, es
,
2131 true /* install */);
2133 /* create and advertise EAD-EVI routes for the ES -
2134 * XXX - till an ES-EVI reference is created there is really nothing to
2137 if (bgp_mh_info
->ead_evi_adv_for_down_links
)
2138 bgp_evpn_local_type1_evi_route_add(bgp
, es
);
2140 bgp_evpn_local_es_bypass_update(bgp
, es
, bypass
);
2142 /* If the ES link is operationally up generate EAD-ES. EAD-EVI
2143 * can be generated even if the link is inactive.
2146 bgp_evpn_local_es_up(bgp
, es
, regen_esr
);
2148 bgp_evpn_local_es_down(bgp
, es
);
2153 static char *bgp_evpn_es_vteps_str(char *vtep_str
, struct bgp_evpn_es
*es
,
2154 uint8_t vtep_str_size
)
2156 char vtep_flag_str
[BGP_EVPN_FLAG_STR_SZ
];
2157 struct listnode
*node
;
2158 struct bgp_evpn_es_vtep
*es_vtep
;
2160 char ip_buf
[INET6_ADDRSTRLEN
];
2163 for (ALL_LIST_ELEMENTS_RO(es
->es_vtep_list
, node
, es_vtep
)) {
2164 vtep_flag_str
[0] = '\0';
2166 if (es_vtep
->flags
& BGP_EVPNES_VTEP_ESR
)
2167 strlcat(vtep_flag_str
, "E", sizeof(vtep_flag_str
));
2168 if (es_vtep
->flags
& BGP_EVPNES_VTEP_ACTIVE
)
2169 strlcat(vtep_flag_str
, "A", sizeof(vtep_flag_str
));
2171 if (!strlen(vtep_flag_str
))
2172 strlcat(vtep_flag_str
, "-", sizeof(vtep_flag_str
));
2176 strlcat(vtep_str
, ",", vtep_str_size
);
2178 inet_ntop(AF_INET
, &es_vtep
->vtep_ip
, ip_buf
,
2181 strlcat(vtep_str
, "(", vtep_str_size
);
2182 strlcat(vtep_str
, vtep_flag_str
, vtep_str_size
);
2183 strlcat(vtep_str
, ")", vtep_str_size
);
2189 static void bgp_evpn_es_json_vtep_fill(json_object
*json_vteps
,
2190 struct bgp_evpn_es_vtep
*es_vtep
)
2192 json_object
*json_vtep_entry
;
2193 json_object
*json_flags
;
2195 json_vtep_entry
= json_object_new_object();
2197 json_object_string_addf(json_vtep_entry
, "vtep_ip", "%pI4",
2199 if (es_vtep
->flags
& (BGP_EVPNES_VTEP_ESR
|
2200 BGP_EVPNES_VTEP_ACTIVE
)) {
2201 json_flags
= json_object_new_array();
2202 if (es_vtep
->flags
& BGP_EVPNES_VTEP_ESR
)
2203 json_array_string_add(json_flags
, "esr");
2204 if (es_vtep
->flags
& BGP_EVPNES_VTEP_ACTIVE
)
2205 json_array_string_add(json_flags
, "active");
2206 json_object_object_add(json_vtep_entry
, "flags", json_flags
);
2207 if (es_vtep
->flags
& BGP_EVPNES_VTEP_ESR
) {
2208 json_object_int_add(json_vtep_entry
, "dfPreference",
2210 json_object_int_add(json_vtep_entry
, "dfAlgorithm",
2215 json_object_array_add(json_vteps
,
2219 static void bgp_evpn_es_vteps_show_detail(struct vty
*vty
,
2220 struct bgp_evpn_es
*es
)
2222 char vtep_flag_str
[BGP_EVPN_FLAG_STR_SZ
];
2223 struct listnode
*node
;
2224 struct bgp_evpn_es_vtep
*es_vtep
;
2225 char alg_buf
[EVPN_DF_ALG_STR_LEN
];
2227 for (ALL_LIST_ELEMENTS_RO(es
->es_vtep_list
, node
, es_vtep
)) {
2228 vtep_flag_str
[0] = '\0';
2229 if (es_vtep
->flags
& BGP_EVPNES_VTEP_ESR
)
2230 strlcat(vtep_flag_str
, "E", sizeof(vtep_flag_str
));
2231 if (es_vtep
->flags
& BGP_EVPNES_VTEP_ACTIVE
)
2232 strlcat(vtep_flag_str
, "A", sizeof(vtep_flag_str
));
2234 if (!strlen(vtep_flag_str
))
2235 strlcat(vtep_flag_str
, "-", sizeof(vtep_flag_str
));
2237 vty_out(vty
, " %pI4 flags: %s", &es_vtep
->vtep_ip
,
2240 if (es_vtep
->flags
& BGP_EVPNES_VTEP_ESR
)
2241 vty_out(vty
, " df_alg: %s df_pref: %u\n",
2242 evpn_es_df_alg2str(es_vtep
->df_alg
, alg_buf
,
2250 static void bgp_evpn_es_show_entry(struct vty
*vty
,
2251 struct bgp_evpn_es
*es
, json_object
*json
)
2253 char buf1
[RD_ADDRSTRLEN
];
2254 struct listnode
*node
;
2255 struct bgp_evpn_es_vtep
*es_vtep
;
2258 json_object
*json_vteps
;
2259 json_object
*json_types
;
2261 json_object_string_add(json
, "esi", es
->esi_str
);
2262 json_object_string_add(json
, "rd",
2263 prefix_rd2str(&es
->prd
, buf1
,
2266 if (es
->flags
& (BGP_EVPNES_LOCAL
| BGP_EVPNES_REMOTE
)) {
2267 json_types
= json_object_new_array();
2268 if (es
->flags
& BGP_EVPNES_LOCAL
)
2269 json_array_string_add(json_types
, "local");
2270 if (es
->flags
& BGP_EVPNES_REMOTE
)
2271 json_array_string_add(json_types
, "remote");
2272 json_object_object_add(json
, "type", json_types
);
2275 if (listcount(es
->es_vtep_list
)) {
2276 json_vteps
= json_object_new_array();
2277 for (ALL_LIST_ELEMENTS_RO(es
->es_vtep_list
,
2279 bgp_evpn_es_json_vtep_fill(json_vteps
, es_vtep
);
2281 json_object_object_add(json
, "vteps", json_vteps
);
2283 json_object_int_add(json
, "vniCount",
2284 listcount(es
->es_evi_list
));
2287 char vtep_str
[ES_VTEP_LIST_STR_SZ
+ BGP_EVPN_VTEPS_FLAG_STR_SZ
];
2290 if (es
->flags
& BGP_EVPNES_BYPASS
)
2291 strlcat(type_str
, "B", sizeof(type_str
));
2292 if (es
->flags
& BGP_EVPNES_LOCAL
)
2293 strlcat(type_str
, "L", sizeof(type_str
));
2294 if (es
->flags
& BGP_EVPNES_REMOTE
)
2295 strlcat(type_str
, "R", sizeof(type_str
));
2296 if (es
->inconsistencies
)
2297 strlcat(type_str
, "I", sizeof(type_str
));
2299 bgp_evpn_es_vteps_str(vtep_str
, es
, sizeof(vtep_str
));
2301 if (es
->flags
& BGP_EVPNES_LOCAL
)
2302 prefix_rd2str(&es
->prd
, buf1
, sizeof(buf1
));
2304 strlcpy(buf1
, "-", sizeof(buf1
));
2306 vty_out(vty
, "%-30s %-5s %-21s %-8d %s\n",
2307 es
->esi_str
, type_str
, buf1
,
2308 listcount(es
->es_evi_list
), vtep_str
);
2312 static void bgp_evpn_es_show_entry_detail(struct vty
*vty
,
2313 struct bgp_evpn_es
*es
, json_object
*json
)
2316 json_object
*json_flags
;
2317 json_object
*json_incons
;
2318 json_object
*json_vteps
;
2319 struct listnode
*node
;
2320 struct bgp_evpn_es_vtep
*es_vtep
;
2322 /* Add the "brief" info first */
2323 bgp_evpn_es_show_entry(vty
, es
, json
);
2325 & (BGP_EVPNES_OPER_UP
| BGP_EVPNES_ADV_EVI
2326 | BGP_EVPNES_BYPASS
)) {
2327 json_flags
= json_object_new_array();
2328 if (es
->flags
& BGP_EVPNES_OPER_UP
)
2329 json_array_string_add(json_flags
, "up");
2330 if (es
->flags
& BGP_EVPNES_ADV_EVI
)
2331 json_array_string_add(json_flags
,
2333 if (es
->flags
& BGP_EVPNES_BYPASS
)
2334 json_array_string_add(json_flags
, "bypass");
2335 json_object_object_add(json
, "flags", json_flags
);
2337 json_object_string_addf(json
, "originator_ip", "%pI4",
2338 &es
->originator_ip
);
2339 json_object_int_add(json
, "remoteVniCount",
2340 es
->remote_es_evi_cnt
);
2341 json_object_int_add(json
, "vrfCount",
2342 listcount(es
->es_vrf_list
));
2343 json_object_int_add(json
, "macipPathCount",
2344 listcount(es
->macip_evi_path_list
));
2345 json_object_int_add(json
, "macipGlobalPathCount",
2346 listcount(es
->macip_global_path_list
));
2347 json_object_int_add(json
, "inconsistentVniVtepCount",
2348 es
->incons_evi_vtep_cnt
);
2349 if (listcount(es
->es_vtep_list
)) {
2350 json_vteps
= json_object_new_array();
2351 for (ALL_LIST_ELEMENTS_RO(es
->es_vtep_list
, node
,
2353 bgp_evpn_es_json_vtep_fill(json_vteps
, es_vtep
);
2355 json_object_object_add(json
, "vteps", json_vteps
);
2357 if (es
->inconsistencies
) {
2358 json_incons
= json_object_new_array();
2359 if (es
->inconsistencies
& BGP_EVPNES_INCONS_VTEP_LIST
)
2360 json_array_string_add(json_incons
,
2361 "vni-vtep-mismatch");
2362 json_object_object_add(json
, "inconsistencies",
2366 char incons_str
[BGP_EVPNES_INCONS_STR_SZ
];
2368 char buf1
[RD_ADDRSTRLEN
];
2371 if (es
->flags
& BGP_EVPNES_LOCAL
)
2372 strlcat(type_str
, "L", sizeof(type_str
));
2373 if (es
->flags
& BGP_EVPNES_REMOTE
)
2374 strlcat(type_str
, "R", sizeof(type_str
));
2376 if (es
->flags
& BGP_EVPNES_LOCAL
)
2377 prefix_rd2str(&es
->prd
, buf1
, sizeof(buf1
));
2379 strlcpy(buf1
, "-", sizeof(buf1
));
2381 vty_out(vty
, "ESI: %s\n", es
->esi_str
);
2382 vty_out(vty
, " Type: %s\n", type_str
);
2383 vty_out(vty
, " RD: %s\n", buf1
);
2384 vty_out(vty
, " Originator-IP: %pI4\n", &es
->originator_ip
);
2385 if (es
->flags
& BGP_EVPNES_LOCAL
)
2386 vty_out(vty
, " Local ES DF preference: %u\n",
2388 if (es
->flags
& BGP_EVPNES_BYPASS
)
2389 vty_out(vty
, " LACP bypass: on\n");
2390 vty_out(vty
, " VNI Count: %d\n", listcount(es
->es_evi_list
));
2391 vty_out(vty
, " Remote VNI Count: %d\n",
2392 es
->remote_es_evi_cnt
);
2393 vty_out(vty
, " VRF Count: %d\n", listcount(es
->es_vrf_list
));
2394 vty_out(vty
, " MACIP EVI Path Count: %d\n",
2395 listcount(es
->macip_evi_path_list
));
2396 vty_out(vty
, " MACIP Global Path Count: %d\n",
2397 listcount(es
->macip_global_path_list
));
2398 vty_out(vty
, " Inconsistent VNI VTEP Count: %d\n",
2399 es
->incons_evi_vtep_cnt
);
2400 if (es
->inconsistencies
) {
2401 incons_str
[0] = '\0';
2402 if (es
->inconsistencies
& BGP_EVPNES_INCONS_VTEP_LIST
)
2403 strlcat(incons_str
, "vni-vtep-mismatch",
2404 sizeof(incons_str
));
2406 strlcpy(incons_str
, "-", sizeof(incons_str
));
2408 vty_out(vty
, " Inconsistencies: %s\n",
2410 if (listcount(es
->es_vtep_list
)) {
2411 vty_out(vty
, " VTEPs:\n");
2412 bgp_evpn_es_vteps_show_detail(vty
, es
);
2418 /* Display all ESs */
2419 void bgp_evpn_es_show(struct vty
*vty
, bool uj
, bool detail
)
2421 struct bgp_evpn_es
*es
;
2422 json_object
*json_array
= NULL
;
2423 json_object
*json
= NULL
;
2426 /* create an array of ESs */
2427 json_array
= json_object_new_array();
2431 "ES Flags: B - bypass, L local, R remote, I inconsistent\n");
2433 "VTEP Flags: E ESR/Type-4, A active nexthop\n");
2435 "%-30s %-5s %-21s %-8s %s\n",
2436 "ESI", "Flags", "RD", "#VNIs", "VTEPs");
2440 RB_FOREACH(es
, bgp_es_rb_head
, &bgp_mh_info
->es_rb_tree
) {
2442 /* create a separate json object for each ES */
2443 json
= json_object_new_object();
2445 bgp_evpn_es_show_entry_detail(vty
, es
, json
);
2447 bgp_evpn_es_show_entry(vty
, es
, json
);
2448 /* add ES to the json array */
2450 json_object_array_add(json_array
, json
);
2453 /* print the array of json-ESs */
2455 vty_out(vty
, "%s\n", json_object_to_json_string_ext(
2456 json_array
, JSON_C_TO_STRING_PRETTY
));
2457 json_object_free(json_array
);
2461 /* Display specific ES */
2462 void bgp_evpn_es_show_esi(struct vty
*vty
, esi_t
*esi
, bool uj
)
2464 struct bgp_evpn_es
*es
;
2465 json_object
*json
= NULL
;
2468 json
= json_object_new_object();
2470 es
= bgp_evpn_es_find(esi
);
2472 bgp_evpn_es_show_entry_detail(vty
, es
, json
);
2475 vty_out(vty
, "ESI not found\n");
2479 vty_out(vty
, "%s\n", json_object_to_json_string_ext(
2480 json
, JSON_C_TO_STRING_PRETTY
));
2481 json_object_free(json
);
2485 /*****************************************************************************/
2486 /* Ethernet Segment to VRF association -
2487 * 1. Each ES-EVI entry is associated with a tenant VRF. This associaton
2488 * triggers the creation of an ES-VRF entry.
2489 * 2. The ES-VRF entry is maintained for the purpose of L3-NHG creation
2490 * 3. Type-2/MAC-IP routes are imported into a tenant VRF and programmed as
2491 * a /32 or host route entry in the dataplane. If the destination of
2492 * the host route is a remote-ES the route is programmed with the
2493 * corresponding (keyed in by {vrf,ES-id}) L3-NHG.
2494 * 4. The reason for this indirection (route->L3-NHG, L3-NHG->list-of-VTEPs)
2495 * is to avoid route updates to the dplane when a remote-ES link flaps i.e.
2496 * instead of updating all the dependent routes the NHG's contents are updated.
2497 * This reduces the amount of datplane updates (nhg updates vs. route updates)
2498 * allowing for a faster failover.
2500 * XXX - can the L3 SVI index change without change in vpn->bgp_vrf
2501 * association? If yes we need to handle that by updating all the L3 NHGs
2504 /******************************** L3 NHG management *************************/
2505 static void bgp_evpn_l3nhg_zebra_add_v4_or_v6(struct bgp_evpn_es_vrf
*es_vrf
,
2508 uint32_t nhg_id
= v4_nhg
? es_vrf
->nhg_id
: es_vrf
->v6_nhg_id
;
2509 struct bgp_evpn_es
*es
= es_vrf
->es
;
2510 struct listnode
*node
;
2511 struct bgp_evpn_es_vtep
*es_vtep
;
2513 struct zapi_nexthop
*api_nh
;
2514 struct zapi_nhg api_nhg
= {};
2516 /* Skip installation of L3-NHG if host routes used */
2520 if (BGP_DEBUG(evpn_mh
, EVPN_MH_ES
))
2521 zlog_debug("es %s vrf %u %s nhg %u to zebra", es
->esi_str
,
2522 es_vrf
->bgp_vrf
->vrf_id
,
2523 v4_nhg
? "v4_nhg" : "v6_nhg", nhg_id
);
2525 frrtrace(4, frr_bgp
, evpn_mh_nhg_zsend
, true, v4_nhg
, nhg_id
, es_vrf
);
2527 /* only the gateway ip changes for each NH. rest of the params
2530 memset(&nh
, 0, sizeof(nh
));
2531 nh
.vrf_id
= es_vrf
->bgp_vrf
->vrf_id
;
2532 nh
.flags
= NEXTHOP_FLAG_ONLINK
;
2533 nh
.ifindex
= es_vrf
->bgp_vrf
->l3vni_svi_ifindex
;
2536 v4_nhg
? NEXTHOP_TYPE_IPV4_IFINDEX
: NEXTHOP_TYPE_IPV6_IFINDEX
;
2538 api_nhg
.id
= nhg_id
;
2539 for (ALL_LIST_ELEMENTS_RO(es
->es_vtep_list
, node
, es_vtep
)) {
2540 if (!CHECK_FLAG(es_vtep
->flags
, BGP_EVPNES_VTEP_ACTIVE
))
2543 /* Don't overrun the zapi buffer. */
2544 if (api_nhg
.nexthop_num
== MULTIPATH_NUM
)
2547 /* overwrite the gw */
2549 nh
.gate
.ipv4
= es_vtep
->vtep_ip
;
2551 ipv4_to_ipv4_mapped_ipv6(&nh
.gate
.ipv6
,
2554 /* convert to zapi format */
2555 api_nh
= &api_nhg
.nexthops
[api_nhg
.nexthop_num
];
2556 zapi_nexthop_from_nexthop(api_nh
, &nh
);
2558 ++api_nhg
.nexthop_num
;
2559 if (BGP_DEBUG(evpn_mh
, EVPN_MH_ES
))
2560 zlog_debug("nhg %u vtep %pI4 l3-svi %d", api_nhg
.id
,
2562 es_vrf
->bgp_vrf
->l3vni_svi_ifindex
);
2564 frrtrace(3, frr_bgp
, evpn_mh_nh_zsend
, nhg_id
, es_vtep
, es_vrf
);
2567 if (!api_nhg
.nexthop_num
)
2570 zclient_nhg_send(zclient
, ZEBRA_NHG_ADD
, &api_nhg
);
2573 static bool bgp_evpn_l3nhg_zebra_ok(struct bgp_evpn_es_vrf
*es_vrf
)
2575 if (!bgp_mh_info
->host_routes_use_l3nhg
&& !bgp_mh_info
->install_l3nhg
)
2579 if (!zclient
|| zclient
->sock
< 0)
2585 static void bgp_evpn_l3nhg_zebra_add(struct bgp_evpn_es_vrf
*es_vrf
)
2587 if (!bgp_evpn_l3nhg_zebra_ok(es_vrf
))
2590 bgp_evpn_l3nhg_zebra_add_v4_or_v6(es_vrf
, true /*v4_nhg*/);
2591 bgp_evpn_l3nhg_zebra_add_v4_or_v6(es_vrf
, false /*v4_nhg*/);
2594 static void bgp_evpn_l3nhg_zebra_del_v4_or_v6(struct bgp_evpn_es_vrf
*es_vrf
,
2597 struct zapi_nhg api_nhg
= {};
2599 api_nhg
.id
= v4_nhg
? es_vrf
->nhg_id
: es_vrf
->v6_nhg_id
;
2601 /* Skip installation of L3-NHG if host routes used */
2605 if (BGP_DEBUG(evpn_mh
, EVPN_MH_ES
))
2606 zlog_debug("es %s vrf %u %s nhg %u to zebra",
2607 es_vrf
->es
->esi_str
, es_vrf
->bgp_vrf
->vrf_id
,
2608 v4_nhg
? "v4_nhg" : "v6_nhg", api_nhg
.id
);
2611 frrtrace(4, frr_bgp
, evpn_mh_nhg_zsend
, false, v4_nhg
, api_nhg
.id
,
2614 zclient_nhg_send(zclient
, ZEBRA_NHG_DEL
, &api_nhg
);
2617 static void bgp_evpn_l3nhg_zebra_del(struct bgp_evpn_es_vrf
*es_vrf
)
2619 if (!bgp_evpn_l3nhg_zebra_ok(es_vrf
))
2622 bgp_evpn_l3nhg_zebra_del_v4_or_v6(es_vrf
, true /*v4_nhg*/);
2623 bgp_evpn_l3nhg_zebra_del_v4_or_v6(es_vrf
, false /*v4_nhg*/);
2626 static void bgp_evpn_l3nhg_deactivate(struct bgp_evpn_es_vrf
*es_vrf
)
2628 if (!(es_vrf
->flags
& BGP_EVPNES_VRF_NHG_ACTIVE
))
2631 if (BGP_DEBUG(evpn_mh
, EVPN_MH_ES
))
2632 zlog_debug("es %s vrf %u nhg %u de-activate",
2633 es_vrf
->es
->esi_str
, es_vrf
->bgp_vrf
->vrf_id
,
2635 bgp_evpn_l3nhg_zebra_del(es_vrf
);
2636 es_vrf
->flags
&= ~BGP_EVPNES_VRF_NHG_ACTIVE
;
2637 /* MAC-IPs can now be installed via the L3NHG */
2638 bgp_evpn_es_path_update_on_es_vrf_chg(es_vrf
, "l3nhg-deactivate");
2641 static void bgp_evpn_l3nhg_activate(struct bgp_evpn_es_vrf
*es_vrf
, bool update
)
2643 if (!bgp_evpn_es_get_active_vtep_cnt(es_vrf
->es
)) {
2644 bgp_evpn_l3nhg_deactivate(es_vrf
);
2648 if (es_vrf
->flags
& BGP_EVPNES_VRF_NHG_ACTIVE
) {
2652 if (BGP_DEBUG(evpn_mh
, EVPN_MH_ES
))
2653 zlog_debug("es %s vrf %u nhg %u activate",
2654 es_vrf
->es
->esi_str
, es_vrf
->bgp_vrf
->vrf_id
,
2656 es_vrf
->flags
|= BGP_EVPNES_VRF_NHG_ACTIVE
;
2657 /* MAC-IPs can now be installed via the L3NHG */
2658 bgp_evpn_es_path_update_on_es_vrf_chg(es_vrf
, "l3nhg_activate");
2661 bgp_evpn_l3nhg_zebra_add(es_vrf
);
2664 /* when a VTEP is activated or de-activated against an ES associated
2665 * VRFs' NHG needs to be updated
2667 static void bgp_evpn_l3nhg_update_on_vtep_chg(struct bgp_evpn_es
*es
)
2669 struct bgp_evpn_es_vrf
*es_vrf
;
2670 struct listnode
*es_vrf_node
;
2672 if (BGP_DEBUG(evpn_mh
, EVPN_MH_ES
))
2673 zlog_debug("es %s nhg update on vtep chg", es
->esi_str
);
2675 for (ALL_LIST_ELEMENTS_RO(es
->es_vrf_list
, es_vrf_node
, es_vrf
))
2676 bgp_evpn_l3nhg_activate(es_vrf
, true /* update */);
2679 /* compare ES-IDs for the ES-VRF RB tree maintained per-VRF */
2680 static int bgp_es_vrf_rb_cmp(const struct bgp_evpn_es_vrf
*es_vrf1
,
2681 const struct bgp_evpn_es_vrf
*es_vrf2
)
2683 return memcmp(&es_vrf1
->es
->esi
, &es_vrf2
->es
->esi
, ESI_BYTES
);
2685 RB_GENERATE(bgp_es_vrf_rb_head
, bgp_evpn_es_vrf
, rb_node
, bgp_es_vrf_rb_cmp
);
2687 /* Initialize the ES tables maintained per-tenant vrf */
2688 void bgp_evpn_vrf_es_init(struct bgp
*bgp_vrf
)
2690 /* Initialize the ES-VRF RB tree */
2691 RB_INIT(bgp_es_vrf_rb_head
, &bgp_vrf
->es_vrf_rb_tree
);
2694 /* find the ES-VRF in the per-VRF RB tree */
2695 static struct bgp_evpn_es_vrf
*bgp_evpn_es_vrf_find(struct bgp_evpn_es
*es
,
2696 struct bgp
*bgp_vrf
)
2698 struct bgp_evpn_es_vrf es_vrf
;
2702 return RB_FIND(bgp_es_vrf_rb_head
, &bgp_vrf
->es_vrf_rb_tree
, &es_vrf
);
2705 /* allocate a new ES-VRF and setup L3NHG for it */
2706 static struct bgp_evpn_es_vrf
*bgp_evpn_es_vrf_create(struct bgp_evpn_es
*es
,
2707 struct bgp
*bgp_vrf
)
2709 struct bgp_evpn_es_vrf
*es_vrf
;
2711 es_vrf
= XCALLOC(MTYPE_BGP_EVPN_ES_VRF
, sizeof(*es_vrf
));
2714 es_vrf
->bgp_vrf
= bgp_vrf
;
2716 /* insert into the VRF-ESI rb tree */
2717 if (RB_INSERT(bgp_es_vrf_rb_head
, &bgp_vrf
->es_vrf_rb_tree
, es_vrf
)) {
2718 XFREE(MTYPE_BGP_EVPN_ES_VRF
, es_vrf
);
2722 /* add to the ES's VRF list */
2723 listnode_init(&es_vrf
->es_listnode
, es_vrf
);
2724 listnode_add(es
->es_vrf_list
, &es_vrf
->es_listnode
);
2726 /* setup the L3 NHG id for the ES */
2727 es_vrf
->nhg_id
= bgp_l3nhg_id_alloc();
2728 es_vrf
->v6_nhg_id
= bgp_l3nhg_id_alloc();
2730 if (BGP_DEBUG(evpn_mh
, EVPN_MH_ES
))
2731 zlog_debug("es %s vrf %u nhg %u v6_nhg %d create", es
->esi_str
,
2732 bgp_vrf
->vrf_id
, es_vrf
->nhg_id
, es_vrf
->v6_nhg_id
);
2733 bgp_evpn_l3nhg_activate(es_vrf
, false /* update */);
2735 /* update paths in the VRF that may already be associated with
2736 * this destination ES
2738 bgp_evpn_es_path_update_on_es_vrf_chg(es_vrf
, "es-vrf-create");
2743 /* remove the L3-NHG associated with the ES-VRF and free it */
2744 static void bgp_evpn_es_vrf_delete(struct bgp_evpn_es_vrf
*es_vrf
)
2746 struct bgp_evpn_es
*es
= es_vrf
->es
;
2747 struct bgp
*bgp_vrf
= es_vrf
->bgp_vrf
;
2749 if (BGP_DEBUG(evpn_mh
, EVPN_MH_ES
))
2750 zlog_debug("es %s vrf %u nhg %u delete", es
->esi_str
,
2751 bgp_vrf
->vrf_id
, es_vrf
->nhg_id
);
2753 /* Remove the NHG resources */
2754 bgp_evpn_l3nhg_deactivate(es_vrf
);
2756 bgp_l3nhg_id_free(es_vrf
->nhg_id
);
2758 if (es_vrf
->v6_nhg_id
)
2759 bgp_l3nhg_id_free(es_vrf
->v6_nhg_id
);
2760 es_vrf
->v6_nhg_id
= 0;
2762 /* remove from the ES's VRF list */
2763 list_delete_node(es
->es_vrf_list
, &es_vrf
->es_listnode
);
2765 /* remove from the VRF-ESI rb tree */
2766 RB_REMOVE(bgp_es_vrf_rb_head
, &bgp_vrf
->es_vrf_rb_tree
, es_vrf
);
2768 /* update paths in the VRF that may already be associated with
2769 * this destination ES
2771 bgp_evpn_es_path_update_on_es_vrf_chg(es_vrf
, "es-vrf-delete");
2773 XFREE(MTYPE_BGP_EVPN_ES_VRF
, es_vrf
);
2776 /* deref and delete if there are no references */
2777 void bgp_evpn_es_vrf_deref(struct bgp_evpn_es_evi
*es_evi
)
2779 struct bgp_evpn_es_vrf
*es_vrf
= es_evi
->es_vrf
;
2784 if (BGP_DEBUG(evpn_mh
, EVPN_MH_ES
))
2785 zlog_debug("es-evi %s vni %u vrf %u de-ref",
2786 es_evi
->es
->esi_str
, es_evi
->vpn
->vni
,
2787 es_vrf
->bgp_vrf
->vrf_id
);
2789 es_evi
->es_vrf
= NULL
;
2790 if (es_vrf
->ref_cnt
)
2793 if (!es_vrf
->ref_cnt
)
2794 bgp_evpn_es_vrf_delete(es_vrf
);
2797 /* find or create and reference */
2798 void bgp_evpn_es_vrf_ref(struct bgp_evpn_es_evi
*es_evi
, struct bgp
*bgp_vrf
)
2800 struct bgp_evpn_es
*es
= es_evi
->es
;
2801 struct bgp_evpn_es_vrf
*es_vrf
= es_evi
->es_vrf
;
2802 struct bgp
*old_bgp_vrf
= NULL
;
2805 old_bgp_vrf
= es_vrf
->bgp_vrf
;
2807 if (old_bgp_vrf
== bgp_vrf
)
2810 /* deref the old ES-VRF */
2811 bgp_evpn_es_vrf_deref(es_evi
);
2816 if (BGP_DEBUG(evpn_mh
, EVPN_MH_ES
))
2817 zlog_debug("es-evi %s vni %u vrf %u ref", es_evi
->es
->esi_str
,
2818 es_evi
->vpn
->vni
, bgp_vrf
->vrf_id
);
2820 /* find-create the new ES-VRF */
2821 es_vrf
= bgp_evpn_es_vrf_find(es
, bgp_vrf
);
2823 es_vrf
= bgp_evpn_es_vrf_create(es
, bgp_vrf
);
2827 es_evi
->es_vrf
= es_vrf
;
2831 /* When the L2-VNI is associated with a L3-VNI/VRF update all the
2832 * associated ES-EVI entries
2834 void bgp_evpn_es_evi_vrf_deref(struct bgpevpn
*vpn
)
2836 struct bgp_evpn_es_evi
*es_evi
;
2838 if (BGP_DEBUG(evpn_mh
, EVPN_MH_ES
))
2839 zlog_debug("es-vrf de-ref for vni %u", vpn
->vni
);
2841 RB_FOREACH (es_evi
, bgp_es_evi_rb_head
, &vpn
->es_evi_rb_tree
)
2842 bgp_evpn_es_vrf_deref(es_evi
);
2844 void bgp_evpn_es_evi_vrf_ref(struct bgpevpn
*vpn
)
2846 struct bgp_evpn_es_evi
*es_evi
;
2848 if (BGP_DEBUG(evpn_mh
, EVPN_MH_ES
))
2849 zlog_debug("es-vrf ref for vni %u", vpn
->vni
);
2851 RB_FOREACH (es_evi
, bgp_es_evi_rb_head
, &vpn
->es_evi_rb_tree
)
2852 bgp_evpn_es_vrf_ref(es_evi
, vpn
->bgp_vrf
);
2855 /* 1. If ES-VRF is not present install the host route with the exploded/flat
2857 * 2. If ES-VRF is present -
2858 * - if L3NHG has not been activated for the ES-VRF (this could be because
2859 * all the PEs attached to the VRF are down) do not install the route
2861 * - if L3NHG has been activated install the route via that L3NHG
2863 void bgp_evpn_es_vrf_use_nhg(struct bgp
*bgp_vrf
, esi_t
*esi
, bool *use_l3nhg
,
2864 bool *is_l3nhg_active
,
2865 struct bgp_evpn_es_vrf
**es_vrf_p
)
2867 struct bgp_evpn_es
*es
;
2868 struct bgp_evpn_es_vrf
*es_vrf
;
2870 if (!bgp_mh_info
->host_routes_use_l3nhg
)
2873 es
= bgp_evpn_es_find(esi
);
2877 es_vrf
= bgp_evpn_es_vrf_find(es
, bgp_vrf
);
2882 if (es_vrf
->flags
& BGP_EVPNES_VRF_NHG_ACTIVE
)
2883 *is_l3nhg_active
= true;
2888 /* returns false if legacy-exploded mp needs to be used for route install */
2889 bool bgp_evpn_path_es_use_nhg(struct bgp
*bgp_vrf
, struct bgp_path_info
*pi
,
2893 struct bgp_evpn_es_vrf
*es_vrf
= NULL
;
2894 struct bgp_path_info
*parent_pi
;
2895 struct bgp_node
*rn
;
2896 struct prefix_evpn
*evp
;
2897 struct bgp_path_info
*mpinfo
;
2898 bool use_l3nhg
= false;
2899 bool is_l3nhg_active
= false;
2903 /* we don't support NHG for routes leaked from another VRF yet */
2904 if (pi
->extra
&& pi
->extra
->bgp_orig
)
2907 parent_pi
= get_route_parent_evpn(pi
);
2911 rn
= parent_pi
->net
;
2915 evp
= (struct prefix_evpn
*)&rn
->p
;
2916 if (evp
->prefix
.route_type
!= BGP_EVPN_MAC_IP_ROUTE
)
2919 /* non-es path, use legacy-exploded multipath */
2920 esi
= bgp_evpn_attr_get_esi(parent_pi
->attr
);
2921 if (!memcmp(esi
, zero_esi
, sizeof(*esi
)))
2924 bgp_evpn_es_vrf_use_nhg(bgp_vrf
, esi
, &use_l3nhg
, &is_l3nhg_active
,
2927 /* L3NHG support is disabled, use legacy-exploded multipath */
2931 /* if the NHG has not been installed we cannot install the route yet,
2932 * return a 0-NHG to indicate that
2934 if (!is_l3nhg_active
)
2937 /* this needs to be set the v6NHG if v6route */
2938 if (is_evpn_prefix_ipaddr_v6(evp
))
2939 *nhg_p
= es_vrf
->v6_nhg_id
;
2941 *nhg_p
= es_vrf
->nhg_id
;
2943 for (mpinfo
= bgp_path_info_mpath_next(pi
); mpinfo
;
2944 mpinfo
= bgp_path_info_mpath_next(mpinfo
)) {
2945 /* if any of the paths have a different ESI we can't use
2946 * the NHG associated with the ES. fallback to legacy-exploded
2949 if (memcmp(esi
, bgp_evpn_attr_get_esi(mpinfo
->attr
),
2957 static void bgp_evpn_es_vrf_show_entry(struct vty
*vty
,
2958 struct bgp_evpn_es_vrf
*es_vrf
,
2961 struct bgp_evpn_es
*es
= es_vrf
->es
;
2962 struct bgp
*bgp_vrf
= es_vrf
->bgp_vrf
;
2965 json_object
*json_types
;
2967 json_object_string_add(json
, "esi", es
->esi_str
);
2968 json_object_string_add(json
, "vrf", bgp_vrf
->name
);
2970 if (es_vrf
->flags
& (BGP_EVPNES_VRF_NHG_ACTIVE
)) {
2971 json_types
= json_object_new_array();
2972 if (es_vrf
->flags
& BGP_EVPNES_VRF_NHG_ACTIVE
)
2973 json_array_string_add(json_types
, "active");
2974 json_object_object_add(json
, "flags", json_types
);
2977 json_object_int_add(json
, "ipv4NHG", es_vrf
->nhg_id
);
2978 json_object_int_add(json
, "ipv6NHG", es_vrf
->v6_nhg_id
);
2979 json_object_int_add(json
, "refCount", es_vrf
->ref_cnt
);
2983 flags_str
[0] = '\0';
2984 if (es_vrf
->flags
& BGP_EVPNES_VRF_NHG_ACTIVE
)
2985 strlcat(flags_str
, "A", sizeof(flags_str
));
2987 vty_out(vty
, "%-30s %-15s %-5s %-8u %-8u %u\n", es
->esi_str
,
2988 bgp_vrf
->name
, flags_str
, es_vrf
->nhg_id
,
2989 es_vrf
->v6_nhg_id
, es_vrf
->ref_cnt
);
2993 static void bgp_evpn_es_vrf_show_es(struct vty
*vty
, json_object
*json_array
,
2994 struct bgp_evpn_es
*es
)
2996 json_object
*json
= NULL
;
2997 struct listnode
*es_vrf_node
;
2998 struct bgp_evpn_es_vrf
*es_vrf
;
3000 for (ALL_LIST_ELEMENTS_RO(es
->es_vrf_list
, es_vrf_node
, es_vrf
)) {
3001 /* create a separate json object for each ES-VRF */
3003 json
= json_object_new_object();
3004 bgp_evpn_es_vrf_show_entry(vty
, es_vrf
, json
);
3005 /* add ES-VRF to the json array */
3007 json_object_array_add(json_array
, json
);
3011 /* Display all ES VRFs */
3012 void bgp_evpn_es_vrf_show(struct vty
*vty
, bool uj
, struct bgp_evpn_es
*es
)
3014 json_object
*json_array
= NULL
;
3017 /* create an array of ESs */
3018 json_array
= json_object_new_array();
3020 vty_out(vty
, "ES-VRF Flags: A Active\n");
3021 vty_out(vty
, "%-30s %-15s %-5s %-8s %-8s %s\n", "ESI", "VRF",
3022 "Flags", "IPv4-NHG", "IPv6-NHG", "Ref");
3026 bgp_evpn_es_vrf_show_es(vty
, json_array
, es
);
3028 RB_FOREACH (es
, bgp_es_rb_head
, &bgp_mh_info
->es_rb_tree
)
3029 bgp_evpn_es_vrf_show_es(vty
, json_array
, es
);
3032 /* print the array of json-ESs */
3034 vty_out(vty
, "%s\n",
3035 json_object_to_json_string_ext(
3036 json_array
, JSON_C_TO_STRING_PRETTY
));
3037 json_object_free(json_array
);
3041 /* Display specific ES VRF */
3042 void bgp_evpn_es_vrf_show_esi(struct vty
*vty
, esi_t
*esi
, bool uj
)
3044 struct bgp_evpn_es
*es
;
3046 es
= bgp_evpn_es_find(esi
);
3048 bgp_evpn_es_vrf_show(vty
, uj
, es
);
3051 vty_out(vty
, "ESI not found\n");
3055 /*****************************************************************************/
3056 /* Ethernet Segment to EVI association -
3057 * 1. The ES-EVI entry is maintained as a RB tree per L2-VNI
3058 * (bgpevpn->es_evi_rb_tree).
3059 * 2. Each local ES-EVI entry is rxed from zebra and then used by BGP to
3060 * advertises an EAD-EVI (Type-1 EVPN) route
3061 * 3. The remote ES-EVI is created when a bgp_evpn_es_evi_vtep references
3065 /* A list of remote VTEPs is maintained for each ES-EVI. This list includes -
3066 * 1. VTEPs for which we have imported the EAD-per-ES Type1 route
3067 * 2. VTEPs for which we have imported the EAD-per-EVI Type1 route
3068 * VTEPs for which both routes have been rxed are activated. Activation
3069 * creates a NHG in the parent ES.
3071 static int bgp_evpn_es_evi_vtep_cmp(void *p1
, void *p2
)
3073 const struct bgp_evpn_es_evi_vtep
*evi_vtep1
= p1
;
3074 const struct bgp_evpn_es_evi_vtep
*evi_vtep2
= p2
;
3076 return evi_vtep1
->vtep_ip
.s_addr
- evi_vtep2
->vtep_ip
.s_addr
;
3079 static struct bgp_evpn_es_evi_vtep
*bgp_evpn_es_evi_vtep_new(
3080 struct bgp_evpn_es_evi
*es_evi
, struct in_addr vtep_ip
)
3082 struct bgp_evpn_es_evi_vtep
*evi_vtep
;
3084 evi_vtep
= XCALLOC(MTYPE_BGP_EVPN_ES_EVI_VTEP
, sizeof(*evi_vtep
));
3086 evi_vtep
->es_evi
= es_evi
;
3087 evi_vtep
->vtep_ip
.s_addr
= vtep_ip
.s_addr
;
3088 listnode_init(&evi_vtep
->es_evi_listnode
, evi_vtep
);
3089 listnode_add_sort(es_evi
->es_evi_vtep_list
, &evi_vtep
->es_evi_listnode
);
3094 static void bgp_evpn_es_evi_vtep_free(struct bgp_evpn_es_evi_vtep
*evi_vtep
)
3096 struct bgp_evpn_es_evi
*es_evi
= evi_vtep
->es_evi
;
3098 if (evi_vtep
->flags
& (BGP_EVPN_EVI_VTEP_EAD
))
3099 /* as long as there is some reference we can't free it */
3102 list_delete_node(es_evi
->es_evi_vtep_list
, &evi_vtep
->es_evi_listnode
);
3103 XFREE(MTYPE_BGP_EVPN_ES_EVI_VTEP
, evi_vtep
);
3106 /* check if VTEP is already part of the list */
3107 static struct bgp_evpn_es_evi_vtep
*bgp_evpn_es_evi_vtep_find(
3108 struct bgp_evpn_es_evi
*es_evi
, struct in_addr vtep_ip
)
3110 struct listnode
*node
= NULL
;
3111 struct bgp_evpn_es_evi_vtep
*evi_vtep
;
3113 for (ALL_LIST_ELEMENTS_RO(es_evi
->es_evi_vtep_list
, node
, evi_vtep
)) {
3114 if (evi_vtep
->vtep_ip
.s_addr
== vtep_ip
.s_addr
)
3120 /* A VTEP can be added as "active" attach to an ES if EAD-per-ES and
3121 * EAD-per-EVI routes are rxed from it.
3123 static void bgp_evpn_es_evi_vtep_re_eval_active(struct bgp
*bgp
,
3124 struct bgp_evpn_es_evi_vtep
*evi_vtep
)
3128 uint32_t ead_activity_flags
;
3130 old_active
= CHECK_FLAG(evi_vtep
->flags
, BGP_EVPN_EVI_VTEP_ACTIVE
);
3132 if (bgp_mh_info
->ead_evi_rx
)
3133 /* Both EAD-per-ES and EAD-per-EVI routes must be rxed from a PE
3134 * before it can be activated.
3136 ead_activity_flags
= BGP_EVPN_EVI_VTEP_EAD
;
3138 /* EAD-per-ES is sufficent to activate the PE */
3139 ead_activity_flags
= BGP_EVPN_EVI_VTEP_EAD_PER_ES
;
3141 if ((evi_vtep
->flags
& ead_activity_flags
) == ead_activity_flags
)
3142 SET_FLAG(evi_vtep
->flags
, BGP_EVPN_EVI_VTEP_ACTIVE
);
3144 UNSET_FLAG(evi_vtep
->flags
, BGP_EVPN_EVI_VTEP_ACTIVE
);
3146 new_active
= CHECK_FLAG(evi_vtep
->flags
, BGP_EVPN_EVI_VTEP_ACTIVE
);
3148 if (old_active
== new_active
)
3151 if (BGP_DEBUG(evpn_mh
, EVPN_MH_ES
))
3152 zlog_debug("es %s evi %u vtep %pI4 %s",
3153 evi_vtep
->es_evi
->es
->esi_str
,
3154 evi_vtep
->es_evi
->vpn
->vni
, &evi_vtep
->vtep_ip
,
3155 new_active
? "active" : "inactive");
3157 /* add VTEP to parent es */
3159 struct bgp_evpn_es_vtep
*es_vtep
;
3161 es_vtep
= bgp_evpn_es_vtep_add(bgp
, evi_vtep
->es_evi
->es
,
3162 evi_vtep
->vtep_ip
, false /*esr*/,
3164 evi_vtep
->es_vtep
= es_vtep
;
3166 if (evi_vtep
->es_vtep
) {
3167 bgp_evpn_es_vtep_do_del(bgp
, evi_vtep
->es_vtep
,
3169 evi_vtep
->es_vtep
= NULL
;
3172 /* queue up the parent es for background consistency checks */
3173 bgp_evpn_es_cons_checks_pend_add(evi_vtep
->es_evi
->es
);
3176 static void bgp_evpn_es_evi_vtep_add(struct bgp
*bgp
,
3177 struct bgp_evpn_es_evi
*es_evi
, struct in_addr vtep_ip
,
3180 struct bgp_evpn_es_evi_vtep
*evi_vtep
;
3182 evi_vtep
= bgp_evpn_es_evi_vtep_find(es_evi
, vtep_ip
);
3185 evi_vtep
= bgp_evpn_es_evi_vtep_new(es_evi
, vtep_ip
);
3187 if (BGP_DEBUG(evpn_mh
, EVPN_MH_ES
))
3188 zlog_debug("add es %s evi %u vtep %pI4 %s",
3189 evi_vtep
->es_evi
->es
->esi_str
,
3190 evi_vtep
->es_evi
->vpn
->vni
, &evi_vtep
->vtep_ip
,
3191 ead_es
? "ead_es" : "ead_evi");
3194 SET_FLAG(evi_vtep
->flags
, BGP_EVPN_EVI_VTEP_EAD_PER_ES
);
3196 SET_FLAG(evi_vtep
->flags
, BGP_EVPN_EVI_VTEP_EAD_PER_EVI
);
3198 bgp_evpn_es_evi_vtep_re_eval_active(bgp
, evi_vtep
);
3201 static void bgp_evpn_es_evi_vtep_del(struct bgp
*bgp
,
3202 struct bgp_evpn_es_evi
*es_evi
, struct in_addr vtep_ip
,
3205 struct bgp_evpn_es_evi_vtep
*evi_vtep
;
3207 evi_vtep
= bgp_evpn_es_evi_vtep_find(es_evi
, vtep_ip
);
3211 if (BGP_DEBUG(evpn_mh
, EVPN_MH_ES
))
3212 zlog_debug("del es %s evi %u vtep %pI4 %s",
3213 evi_vtep
->es_evi
->es
->esi_str
,
3214 evi_vtep
->es_evi
->vpn
->vni
, &evi_vtep
->vtep_ip
,
3215 ead_es
? "ead_es" : "ead_evi");
3218 UNSET_FLAG(evi_vtep
->flags
, BGP_EVPN_EVI_VTEP_EAD_PER_ES
);
3220 UNSET_FLAG(evi_vtep
->flags
, BGP_EVPN_EVI_VTEP_EAD_PER_EVI
);
3222 bgp_evpn_es_evi_vtep_re_eval_active(bgp
, evi_vtep
);
3223 bgp_evpn_es_evi_vtep_free(evi_vtep
);
3226 /* compare ES-IDs for the ES-EVI RB tree maintained per-VNI */
3227 static int bgp_es_evi_rb_cmp(const struct bgp_evpn_es_evi
*es_evi1
,
3228 const struct bgp_evpn_es_evi
*es_evi2
)
3230 return memcmp(&es_evi1
->es
->esi
, &es_evi2
->es
->esi
, ESI_BYTES
);
3232 RB_GENERATE(bgp_es_evi_rb_head
, bgp_evpn_es_evi
, rb_node
, bgp_es_evi_rb_cmp
);
3234 /* find the ES-EVI in the per-L2-VNI RB tree */
3235 static struct bgp_evpn_es_evi
*bgp_evpn_es_evi_find(struct bgp_evpn_es
*es
,
3236 struct bgpevpn
*vpn
)
3238 struct bgp_evpn_es_evi es_evi
;
3242 return RB_FIND(bgp_es_evi_rb_head
, &vpn
->es_evi_rb_tree
, &es_evi
);
3245 /* allocate a new ES-EVI and insert it into the per-L2-VNI and per-ES
3248 static struct bgp_evpn_es_evi
*bgp_evpn_es_evi_new(struct bgp_evpn_es
*es
,
3249 struct bgpevpn
*vpn
)
3251 struct bgp_evpn_es_evi
*es_evi
;
3253 es_evi
= XCALLOC(MTYPE_BGP_EVPN_ES_EVI
, sizeof(*es_evi
));
3258 /* Initialise the VTEP list */
3259 es_evi
->es_evi_vtep_list
= list_new();
3260 listset_app_node_mem(es_evi
->es_evi_vtep_list
);
3261 es_evi
->es_evi_vtep_list
->cmp
= bgp_evpn_es_evi_vtep_cmp
;
3263 /* insert into the VNI-ESI rb tree */
3264 if (RB_INSERT(bgp_es_evi_rb_head
, &vpn
->es_evi_rb_tree
, es_evi
)) {
3265 XFREE(MTYPE_BGP_EVPN_ES_EVI
, es_evi
);
3269 /* add to the ES's VNI list */
3270 listnode_init(&es_evi
->es_listnode
, es_evi
);
3271 listnode_add(es
->es_evi_list
, &es_evi
->es_listnode
);
3273 bgp_evpn_es_vrf_ref(es_evi
, vpn
->bgp_vrf
);
3278 /* remove the ES-EVI from the per-L2-VNI and per-ES tables and free
3281 static struct bgp_evpn_es_evi
*
3282 bgp_evpn_es_evi_free(struct bgp_evpn_es_evi
*es_evi
)
3284 struct bgp_evpn_es
*es
= es_evi
->es
;
3285 struct bgpevpn
*vpn
= es_evi
->vpn
;
3287 /* cannot free the element as long as there is a local or remote
3290 if (es_evi
->flags
& (BGP_EVPNES_EVI_LOCAL
| BGP_EVPNES_EVI_REMOTE
))
3293 bgp_evpn_es_vrf_deref(es_evi
);
3295 /* remove from the ES's VNI list */
3296 list_delete_node(es
->es_evi_list
, &es_evi
->es_listnode
);
3298 /* remove from the VNI-ESI rb tree */
3299 RB_REMOVE(bgp_es_evi_rb_head
, &vpn
->es_evi_rb_tree
, es_evi
);
3301 /* free the VTEP list */
3302 list_delete(&es_evi
->es_evi_vtep_list
);
3304 /* remove from the VNI-ESI rb tree */
3305 XFREE(MTYPE_BGP_EVPN_ES_EVI
, es_evi
);
3310 /* init local info associated with the ES-EVI */
3311 static void bgp_evpn_es_evi_local_info_set(struct bgp_evpn_es_evi
*es_evi
)
3313 struct bgpevpn
*vpn
= es_evi
->vpn
;
3315 if (CHECK_FLAG(es_evi
->flags
, BGP_EVPNES_EVI_LOCAL
))
3318 SET_FLAG(es_evi
->flags
, BGP_EVPNES_EVI_LOCAL
);
3319 listnode_init(&es_evi
->l2vni_listnode
, es_evi
);
3320 listnode_add(vpn
->local_es_evi_list
, &es_evi
->l2vni_listnode
);
3323 /* clear any local info associated with the ES-EVI */
3324 static struct bgp_evpn_es_evi
*
3325 bgp_evpn_es_evi_local_info_clear(struct bgp_evpn_es_evi
*es_evi
)
3327 struct bgpevpn
*vpn
= es_evi
->vpn
;
3329 UNSET_FLAG(es_evi
->flags
, BGP_EVPNES_EVI_LOCAL
);
3330 list_delete_node(vpn
->local_es_evi_list
, &es_evi
->l2vni_listnode
);
3332 return bgp_evpn_es_evi_free(es_evi
);
3335 /* eval remote info associated with the ES */
3336 static void bgp_evpn_es_evi_remote_info_re_eval(struct bgp_evpn_es_evi
*es_evi
)
3338 struct bgp_evpn_es
*es
= es_evi
->es
;
3340 /* if there are remote VTEPs the ES-EVI is classified as "remote" */
3341 if (listcount(es_evi
->es_evi_vtep_list
)) {
3342 if (!CHECK_FLAG(es_evi
->flags
, BGP_EVPNES_EVI_REMOTE
)) {
3343 SET_FLAG(es_evi
->flags
, BGP_EVPNES_EVI_REMOTE
);
3344 ++es
->remote_es_evi_cnt
;
3345 /* set remote on the parent es */
3346 bgp_evpn_es_remote_info_re_eval(es
);
3349 if (CHECK_FLAG(es_evi
->flags
, BGP_EVPNES_EVI_REMOTE
)) {
3350 UNSET_FLAG(es_evi
->flags
, BGP_EVPNES_EVI_REMOTE
);
3351 if (es
->remote_es_evi_cnt
)
3352 --es
->remote_es_evi_cnt
;
3353 bgp_evpn_es_evi_free(es_evi
);
3354 /* check if "remote" can be cleared from the
3357 bgp_evpn_es_remote_info_re_eval(es
);
3362 static struct bgp_evpn_es_evi
*
3363 bgp_evpn_local_es_evi_do_del(struct bgp_evpn_es_evi
*es_evi
)
3365 struct prefix_evpn p
;
3366 struct bgp_evpn_es
*es
= es_evi
->es
;
3369 if (!CHECK_FLAG(es_evi
->flags
, BGP_EVPNES_EVI_LOCAL
))
3372 if (BGP_DEBUG(evpn_mh
, EVPN_MH_ES
))
3373 zlog_debug("del local es %s evi %u",
3374 es_evi
->es
->esi_str
,
3377 bgp
= bgp_get_evpn();
3380 /* update EAD-ES with new list of VNIs */
3381 if (bgp_evpn_local_es_is_active(es
)) {
3382 build_evpn_type1_prefix(&p
, BGP_EVPN_AD_ES_ETH_TAG
,
3383 &es
->esi
, es
->originator_ip
);
3384 if (bgp_evpn_type1_route_update(bgp
, es
, NULL
, &p
))
3385 flog_err(EC_BGP_EVPN_ROUTE_CREATE
,
3386 "%u: EAD-ES route update failure for ESI %s VNI %u",
3387 bgp
->vrf_id
, es
->esi_str
,
3391 /* withdraw and delete EAD-EVI */
3392 if (CHECK_FLAG(es
->flags
, BGP_EVPNES_ADV_EVI
)) {
3393 build_evpn_type1_prefix(&p
, BGP_EVPN_AD_EVI_ETH_TAG
,
3394 &es
->esi
, es
->originator_ip
);
3395 if (bgp_evpn_type1_evi_route_delete(bgp
,
3396 es
, es_evi
->vpn
, &p
))
3397 flog_err(EC_BGP_EVPN_ROUTE_DELETE
,
3398 "%u: EAD-EVI route deletion failure for ESI %s VNI %u",
3399 bgp
->vrf_id
, es
->esi_str
,
3404 return bgp_evpn_es_evi_local_info_clear(es_evi
);
3407 int bgp_evpn_local_es_evi_del(struct bgp
*bgp
, esi_t
*esi
, vni_t vni
)
3409 struct bgpevpn
*vpn
;
3410 struct bgp_evpn_es
*es
;
3411 struct bgp_evpn_es_evi
*es_evi
;
3412 char buf
[ESI_STR_LEN
];
3414 es
= bgp_evpn_es_find(esi
);
3418 "%u: Failed to deref VNI %d from ESI %s; ES not present",
3420 esi_to_str(esi
, buf
, sizeof(buf
)));
3424 vpn
= bgp_evpn_lookup_vni(bgp
, vni
);
3428 "%u: Failed to deref VNI %d from ESI %s; VNI not present",
3429 bgp
->vrf_id
, vni
, es
->esi_str
);
3433 es_evi
= bgp_evpn_es_evi_find(es
, vpn
);
3437 "%u: Failed to deref VNI %d from ESI %s; ES-VNI not present",
3438 bgp
->vrf_id
, vni
, es
->esi_str
);
3442 bgp_evpn_local_es_evi_do_del(es_evi
);
3446 /* Create ES-EVI and advertise the corresponding EAD routes */
3447 int bgp_evpn_local_es_evi_add(struct bgp
*bgp
, esi_t
*esi
, vni_t vni
)
3449 struct bgpevpn
*vpn
;
3450 struct prefix_evpn p
;
3451 struct bgp_evpn_es
*es
;
3452 struct bgp_evpn_es_evi
*es_evi
;
3453 char buf
[ESI_STR_LEN
];
3455 es
= bgp_evpn_es_find(esi
);
3459 "%u: Failed to associate VNI %d with ESI %s; ES not present",
3461 esi_to_str(esi
, buf
, sizeof(buf
)));
3465 vpn
= bgp_evpn_lookup_vni(bgp
, vni
);
3469 "%u: Failed to associate VNI %d with ESI %s; VNI not present",
3470 bgp
->vrf_id
, vni
, es
->esi_str
);
3474 if (BGP_DEBUG(evpn_mh
, EVPN_MH_ES
))
3475 zlog_debug("add local es %s evi %u",
3478 es_evi
= bgp_evpn_es_evi_find(es
, vpn
);
3481 if (CHECK_FLAG(es_evi
->flags
, BGP_EVPNES_EVI_LOCAL
))
3485 es_evi
= bgp_evpn_es_evi_new(es
, vpn
);
3490 bgp_evpn_es_evi_local_info_set(es_evi
);
3492 /* generate an EAD-EVI for this new VNI */
3493 if (CHECK_FLAG(es
->flags
, BGP_EVPNES_ADV_EVI
)) {
3494 build_evpn_type1_prefix(&p
, BGP_EVPN_AD_EVI_ETH_TAG
, &es
->esi
,
3496 if (bgp_evpn_type1_route_update(bgp
, es
, vpn
, &p
))
3497 flog_err(EC_BGP_EVPN_ROUTE_CREATE
,
3498 "%u: EAD-EVI route creation failure for ESI %s VNI %u",
3499 bgp
->vrf_id
, es
->esi_str
, vni
);
3503 build_evpn_type1_prefix(&p
, BGP_EVPN_AD_ES_ETH_TAG
,
3504 &es
->esi
, es
->originator_ip
);
3505 if (bgp_evpn_local_es_is_active(es
)) {
3506 if (bgp_evpn_type1_route_update(bgp
, es
, NULL
, &p
))
3507 flog_err(EC_BGP_EVPN_ROUTE_CREATE
,
3508 "%u: EAD-ES route creation failure for ESI %s VNI %u",
3509 bgp
->vrf_id
, es
->esi_str
, vni
);
3515 /* Add remote ES-EVI entry. This is actually the remote VTEP add and the
3516 * ES-EVI is implicity created on first VTEP's reference.
3518 int bgp_evpn_remote_es_evi_add(struct bgp
*bgp
, struct bgpevpn
*vpn
,
3519 const struct prefix_evpn
*p
)
3521 char buf
[ESI_STR_LEN
];
3522 struct bgp_evpn_es
*es
;
3523 struct bgp_evpn_es_evi
*es_evi
;
3525 const esi_t
*esi
= &p
->prefix
.ead_addr
.esi
;
3528 /* local EAD-ES need not be sent back to zebra */
3531 if (BGP_DEBUG(evpn_mh
, EVPN_MH_ES
))
3532 zlog_debug("add remote %s es %s evi %u vtep %pI4",
3533 p
->prefix
.ead_addr
.eth_tag
? "ead-es" : "ead-evi",
3534 esi_to_str(esi
, buf
, sizeof(buf
)), vpn
->vni
,
3535 &p
->prefix
.ead_addr
.ip
.ipaddr_v4
);
3537 es
= bgp_evpn_es_find(esi
);
3539 es
= bgp_evpn_es_new(bgp
, esi
);
3541 flog_err(EC_BGP_ES_CREATE
,
3542 "%u: Failed to allocate ES entry for ESI %s - at remote ES Add",
3543 bgp
->vrf_id
, esi_to_str(esi
, buf
, sizeof(buf
)));
3548 es_evi
= bgp_evpn_es_evi_find(es
, vpn
);
3550 es_evi
= bgp_evpn_es_evi_new(es
, vpn
);
3552 bgp_evpn_es_free(es
, __func__
);
3557 ead_es
= !!p
->prefix
.ead_addr
.eth_tag
;
3558 bgp_evpn_es_evi_vtep_add(bgp
, es_evi
, p
->prefix
.ead_addr
.ip
.ipaddr_v4
,
3561 bgp_evpn_es_evi_remote_info_re_eval(es_evi
);
3565 /* A remote VTEP has withdrawn. The es-evi-vtep will be deleted and the
3566 * parent es-evi freed up implicitly in last VTEP's deref.
3568 int bgp_evpn_remote_es_evi_del(struct bgp
*bgp
, struct bgpevpn
*vpn
,
3569 const struct prefix_evpn
*p
)
3571 char buf
[ESI_STR_LEN
];
3572 struct bgp_evpn_es
*es
;
3573 struct bgp_evpn_es_evi
*es_evi
;
3577 /* local EAD-ES need not be sent back to zebra */
3580 if (BGP_DEBUG(evpn_mh
, EVPN_MH_ES
))
3582 "del remote %s es %s evi %u vtep %pI4",
3583 p
->prefix
.ead_addr
.eth_tag
? "ead-es" : "ead-evi",
3584 esi_to_str(&p
->prefix
.ead_addr
.esi
, buf
, sizeof(buf
)),
3585 vpn
->vni
, &p
->prefix
.ead_addr
.ip
.ipaddr_v4
);
3587 es
= bgp_evpn_es_find(&p
->prefix
.ead_addr
.esi
);
3589 if (BGP_DEBUG(evpn_mh
, EVPN_MH_ES
))
3590 zlog_debug("del remote %s es %s evi %u vtep %pI4, NO es",
3591 p
->prefix
.ead_addr
.eth_tag
? "ead-es"
3593 esi_to_str(&p
->prefix
.ead_addr
.esi
, buf
,
3596 &p
->prefix
.ead_addr
.ip
.ipaddr_v4
);
3599 es_evi
= bgp_evpn_es_evi_find(es
, vpn
);
3601 if (BGP_DEBUG(evpn_mh
, EVPN_MH_ES
))
3603 "del remote %s es %s evi %u vtep %pI4, NO es-evi",
3604 p
->prefix
.ead_addr
.eth_tag
? "ead-es"
3606 esi_to_str(&p
->prefix
.ead_addr
.esi
, buf
,
3609 &p
->prefix
.ead_addr
.ip
.ipaddr_v4
);
3613 ead_es
= !!p
->prefix
.ead_addr
.eth_tag
;
3614 bgp_evpn_es_evi_vtep_del(bgp
, es_evi
, p
->prefix
.ead_addr
.ip
.ipaddr_v4
,
3616 bgp_evpn_es_evi_remote_info_re_eval(es_evi
);
3620 /* If a VNI is being deleted we need to force del all remote VTEPs */
3621 static void bgp_evpn_remote_es_evi_flush(struct bgp_evpn_es_evi
*es_evi
)
3623 struct listnode
*node
= NULL
;
3624 struct listnode
*nnode
= NULL
;
3625 struct bgp_evpn_es_evi_vtep
*evi_vtep
;
3628 bgp
= bgp_get_evpn();
3632 /* delete all VTEPs */
3633 for (ALL_LIST_ELEMENTS(es_evi
->es_evi_vtep_list
, node
, nnode
,
3635 evi_vtep
->flags
&= ~(BGP_EVPN_EVI_VTEP_EAD_PER_ES
3636 | BGP_EVPN_EVI_VTEP_EAD_PER_EVI
);
3637 bgp_evpn_es_evi_vtep_re_eval_active(bgp
, evi_vtep
);
3638 bgp_evpn_es_evi_vtep_free(evi_vtep
);
3640 /* delete the EVI */
3641 bgp_evpn_es_evi_remote_info_re_eval(es_evi
);
3644 /* Initialize the ES tables maintained per-L2_VNI */
3645 void bgp_evpn_vni_es_init(struct bgpevpn
*vpn
)
3647 /* Initialize the ES-EVI RB tree */
3648 RB_INIT(bgp_es_evi_rb_head
, &vpn
->es_evi_rb_tree
);
3650 /* Initialize the local list maintained for quick walks by type */
3651 vpn
->local_es_evi_list
= list_new();
3652 listset_app_node_mem(vpn
->local_es_evi_list
);
3655 /* Cleanup the ES info maintained per-L2_VNI */
3656 void bgp_evpn_vni_es_cleanup(struct bgpevpn
*vpn
)
3658 struct bgp_evpn_es_evi
*es_evi
;
3659 struct bgp_evpn_es_evi
*es_evi_next
;
3661 RB_FOREACH_SAFE(es_evi
, bgp_es_evi_rb_head
,
3662 &vpn
->es_evi_rb_tree
, es_evi_next
) {
3663 es_evi
= bgp_evpn_local_es_evi_do_del(es_evi
);
3665 bgp_evpn_remote_es_evi_flush(es_evi
);
3668 list_delete(&vpn
->local_es_evi_list
);
3671 static char *bgp_evpn_es_evi_vteps_str(char *vtep_str
,
3672 struct bgp_evpn_es_evi
*es_evi
,
3673 uint8_t vtep_str_size
)
3675 char vtep_flag_str
[BGP_EVPN_FLAG_STR_SZ
];
3676 struct listnode
*node
;
3677 struct bgp_evpn_es_evi_vtep
*evi_vtep
;
3679 char ip_buf
[INET6_ADDRSTRLEN
];
3682 for (ALL_LIST_ELEMENTS_RO(es_evi
->es_evi_vtep_list
, node
, evi_vtep
)) {
3683 vtep_flag_str
[0] = '\0';
3684 if (evi_vtep
->flags
& BGP_EVPN_EVI_VTEP_EAD_PER_ES
)
3685 strlcat(vtep_flag_str
, "E", sizeof(vtep_flag_str
));
3686 if (evi_vtep
->flags
& BGP_EVPN_EVI_VTEP_EAD_PER_EVI
)
3687 strlcat(vtep_flag_str
, "V", sizeof(vtep_flag_str
));
3689 if (!strnlen(vtep_flag_str
, sizeof(vtep_flag_str
)))
3690 strlcpy(vtep_flag_str
, "-", sizeof(vtep_flag_str
));
3694 strlcat(vtep_str
, ",", vtep_str_size
);
3696 inet_ntop(AF_INET
, &evi_vtep
->vtep_ip
, ip_buf
,
3699 strlcat(vtep_str
, "(", vtep_str_size
);
3700 strlcat(vtep_str
, vtep_flag_str
, vtep_str_size
);
3701 strlcat(vtep_str
, ")", vtep_str_size
);
3707 static void bgp_evpn_es_evi_json_vtep_fill(json_object
*json_vteps
,
3708 struct bgp_evpn_es_evi_vtep
*evi_vtep
)
3710 json_object
*json_vtep_entry
;
3711 json_object
*json_flags
;
3713 json_vtep_entry
= json_object_new_object();
3715 json_object_string_addf(json_vtep_entry
, "vtep_ip", "%pI4",
3716 &evi_vtep
->vtep_ip
);
3717 if (evi_vtep
->flags
& (BGP_EVPN_EVI_VTEP_EAD_PER_ES
|
3718 BGP_EVPN_EVI_VTEP_EAD_PER_EVI
)) {
3719 json_flags
= json_object_new_array();
3720 if (evi_vtep
->flags
& BGP_EVPN_EVI_VTEP_EAD_PER_ES
)
3721 json_array_string_add(json_flags
, "ead-per-es");
3722 if (evi_vtep
->flags
& BGP_EVPN_EVI_VTEP_EAD_PER_EVI
)
3723 json_array_string_add(json_flags
, "ead-per-evi");
3724 json_object_object_add(json_vtep_entry
,
3725 "flags", json_flags
);
3728 json_object_array_add(json_vteps
,
3732 static void bgp_evpn_es_evi_show_entry(struct vty
*vty
,
3733 struct bgp_evpn_es_evi
*es_evi
, json_object
*json
)
3735 struct listnode
*node
;
3736 struct bgp_evpn_es_evi_vtep
*evi_vtep
;
3739 json_object
*json_vteps
;
3740 json_object
*json_types
;
3742 json_object_string_add(json
, "esi", es_evi
->es
->esi_str
);
3743 json_object_int_add(json
, "vni", es_evi
->vpn
->vni
);
3745 if (es_evi
->flags
& (BGP_EVPNES_EVI_LOCAL
|
3746 BGP_EVPNES_EVI_REMOTE
)) {
3747 json_types
= json_object_new_array();
3748 if (es_evi
->flags
& BGP_EVPNES_EVI_LOCAL
)
3749 json_array_string_add(json_types
, "local");
3750 if (es_evi
->flags
& BGP_EVPNES_EVI_REMOTE
)
3751 json_array_string_add(json_types
, "remote");
3752 json_object_object_add(json
, "type", json_types
);
3755 if (listcount(es_evi
->es_evi_vtep_list
)) {
3756 json_vteps
= json_object_new_array();
3757 for (ALL_LIST_ELEMENTS_RO(es_evi
->es_evi_vtep_list
,
3759 bgp_evpn_es_evi_json_vtep_fill(json_vteps
,
3762 json_object_object_add(json
, "vteps", json_vteps
);
3766 char vtep_str
[ES_VTEP_LIST_STR_SZ
+ BGP_EVPN_VTEPS_FLAG_STR_SZ
];
3769 if (es_evi
->flags
& BGP_EVPNES_EVI_LOCAL
)
3770 strlcat(type_str
, "L", sizeof(type_str
));
3771 if (es_evi
->flags
& BGP_EVPNES_EVI_REMOTE
)
3772 strlcat(type_str
, "R", sizeof(type_str
));
3773 if (es_evi
->flags
& BGP_EVPNES_EVI_INCONS_VTEP_LIST
)
3774 strlcat(type_str
, "I", sizeof(type_str
));
3776 bgp_evpn_es_evi_vteps_str(vtep_str
, es_evi
, sizeof(vtep_str
));
3778 vty_out(vty
, "%-8d %-30s %-5s %s\n",
3779 es_evi
->vpn
->vni
, es_evi
->es
->esi_str
,
3780 type_str
, vtep_str
);
3784 static void bgp_evpn_es_evi_show_entry_detail(struct vty
*vty
,
3785 struct bgp_evpn_es_evi
*es_evi
, json_object
*json
)
3788 json_object
*json_flags
;
3790 /* Add the "brief" info first */
3791 bgp_evpn_es_evi_show_entry(vty
, es_evi
, json
);
3792 if (es_evi
->flags
& BGP_EVPNES_EVI_INCONS_VTEP_LIST
) {
3793 json_flags
= json_object_new_array();
3794 json_array_string_add(json_flags
, "es-vtep-mismatch");
3795 json_object_object_add(json
, "flags", json_flags
);
3798 char vtep_str
[ES_VTEP_LIST_STR_SZ
+ BGP_EVPN_VTEPS_FLAG_STR_SZ
];
3802 if (es_evi
->flags
& BGP_EVPNES_EVI_LOCAL
)
3803 strlcat(type_str
, "L", sizeof(type_str
));
3804 if (es_evi
->flags
& BGP_EVPNES_EVI_REMOTE
)
3805 strlcat(type_str
, "R", sizeof(type_str
));
3807 bgp_evpn_es_evi_vteps_str(vtep_str
, es_evi
, sizeof(vtep_str
));
3808 if (!strlen(vtep_str
))
3809 strlcpy(vtep_str
, "-", sizeof(type_str
));
3811 vty_out(vty
, "VNI: %d ESI: %s\n",
3812 es_evi
->vpn
->vni
, es_evi
->es
->esi_str
);
3813 vty_out(vty
, " Type: %s\n", type_str
);
3814 vty_out(vty
, " Inconsistencies: %s\n",
3815 (es_evi
->flags
& BGP_EVPNES_EVI_INCONS_VTEP_LIST
) ?
3816 "es-vtep-mismatch":"-");
3817 vty_out(vty
, " VTEPs: %s\n", vtep_str
);
3822 static void bgp_evpn_es_evi_show_one_vni(struct bgpevpn
*vpn
, struct vty
*vty
,
3823 json_object
*json_array
, bool detail
)
3825 struct bgp_evpn_es_evi
*es_evi
;
3826 json_object
*json
= NULL
;
3828 RB_FOREACH(es_evi
, bgp_es_evi_rb_head
, &vpn
->es_evi_rb_tree
) {
3830 /* create a separate json object for each ES */
3831 json
= json_object_new_object();
3833 bgp_evpn_es_evi_show_entry_detail(vty
, es_evi
, json
);
3835 bgp_evpn_es_evi_show_entry(vty
, es_evi
, json
);
3836 /* add ES to the json array */
3838 json_object_array_add(json_array
, json
);
3842 struct es_evi_show_ctx
{
3848 static void bgp_evpn_es_evi_show_one_vni_hash_cb(struct hash_bucket
*bucket
,
3851 struct bgpevpn
*vpn
= (struct bgpevpn
*)bucket
->data
;
3852 struct es_evi_show_ctx
*wctx
= (struct es_evi_show_ctx
*)ctxt
;
3854 bgp_evpn_es_evi_show_one_vni(vpn
, wctx
->vty
, wctx
->json
, wctx
->detail
);
3857 /* Display all ES EVIs */
3858 void bgp_evpn_es_evi_show(struct vty
*vty
, bool uj
, bool detail
)
3860 json_object
*json_array
= NULL
;
3861 struct es_evi_show_ctx wctx
;
3865 /* create an array of ES-EVIs */
3866 json_array
= json_object_new_array();
3870 wctx
.json
= json_array
;
3871 wctx
.detail
= detail
;
3873 bgp
= bgp_get_evpn();
3875 if (!json_array
&& !detail
) {
3876 vty_out(vty
, "Flags: L local, R remote, I inconsistent\n");
3877 vty_out(vty
, "VTEP-Flags: E EAD-per-ES, V EAD-per-EVI\n");
3878 vty_out(vty
, "%-8s %-30s %-5s %s\n",
3879 "VNI", "ESI", "Flags", "VTEPs");
3883 hash_iterate(bgp
->vnihash
,
3884 (void (*)(struct hash_bucket
*,
3885 void *))bgp_evpn_es_evi_show_one_vni_hash_cb
,
3888 vty_out(vty
, "%s\n", json_object_to_json_string_ext(
3889 json_array
, JSON_C_TO_STRING_PRETTY
));
3890 json_object_free(json_array
);
3894 /* Display specific ES EVI */
3895 void bgp_evpn_es_evi_show_vni(struct vty
*vty
, vni_t vni
,
3896 bool uj
, bool detail
)
3898 struct bgpevpn
*vpn
= NULL
;
3899 json_object
*json_array
= NULL
;
3903 /* create an array of ES-EVIs */
3904 json_array
= json_object_new_array();
3907 bgp
= bgp_get_evpn();
3909 vpn
= bgp_evpn_lookup_vni(bgp
, vni
);
3912 if (!json_array
&& !detail
) {
3913 vty_out(vty
, "Flags: L local, R remote, I inconsistent\n");
3914 vty_out(vty
, "VTEP-Flags: E EAD-per-ES, V EAD-per-EVI\n");
3915 vty_out(vty
, "%-8s %-30s %-5s %s\n",
3916 "VNI", "ESI", "Flags", "VTEPs");
3919 bgp_evpn_es_evi_show_one_vni(vpn
, vty
, json_array
, detail
);
3922 vty_out(vty
, "VNI not found\n");
3926 vty_out(vty
, "%s\n", json_object_to_json_string_ext(
3927 json_array
, JSON_C_TO_STRING_PRETTY
));
3928 json_object_free(json_array
);
3932 /*****************************************************************************
3933 * Ethernet Segment Consistency checks
3934 * Consistency checking is done to detect misconfig or mis-cabling. When
3935 * an inconsistency is detected it is simply logged (and displayed via
3936 * show commands) at this point. A more drastic action can be executed (based
3937 * on user config) in the future.
3939 static void bgp_evpn_es_cons_checks_timer_start(void)
3941 if (!bgp_mh_info
->consistency_checking
|| bgp_mh_info
->t_cons_check
)
3944 if (BGP_DEBUG(evpn_mh
, EVPN_MH_ES
))
3945 zlog_debug("periodic consistency checking started");
3947 thread_add_timer(bm
->master
, bgp_evpn_run_consistency_checks
, NULL
,
3948 BGP_EVPN_CONS_CHECK_INTERVAL
,
3949 &bgp_mh_info
->t_cons_check
);
3952 /* queue up the es for background consistency checks */
3953 static void bgp_evpn_es_cons_checks_pend_add(struct bgp_evpn_es
*es
)
3955 if (!bgp_mh_info
->consistency_checking
)
3956 /* consistency checking is not enabled */
3959 if (CHECK_FLAG(es
->flags
, BGP_EVPNES_CONS_CHECK_PEND
))
3960 /* already queued for consistency checking */
3963 /* start the periodic timer for consistency checks if it is not
3964 * already running */
3965 bgp_evpn_es_cons_checks_timer_start();
3967 SET_FLAG(es
->flags
, BGP_EVPNES_CONS_CHECK_PEND
);
3968 listnode_init(&es
->pend_es_listnode
, es
);
3969 listnode_add_after(bgp_mh_info
->pend_es_list
,
3970 listtail_unchecked(bgp_mh_info
->pend_es_list
),
3971 &es
->pend_es_listnode
);
3974 /* pull the ES from the consistency check list */
3975 static void bgp_evpn_es_cons_checks_pend_del(struct bgp_evpn_es
*es
)
3977 if (!CHECK_FLAG(es
->flags
, BGP_EVPNES_CONS_CHECK_PEND
))
3980 UNSET_FLAG(es
->flags
, BGP_EVPNES_CONS_CHECK_PEND
);
3981 list_delete_node(bgp_mh_info
->pend_es_list
,
3982 &es
->pend_es_listnode
);
3985 /* Number of active VTEPs associated with the ES-per-EVI */
3986 static uint32_t bgp_evpn_es_evi_get_active_vtep_cnt(
3987 struct bgp_evpn_es_evi
*es_evi
)
3989 struct bgp_evpn_es_evi_vtep
*evi_vtep
;
3990 struct listnode
*node
;
3991 uint32_t vtep_cnt
= 0;
3993 for (ALL_LIST_ELEMENTS_RO(es_evi
->es_evi_vtep_list
, node
, evi_vtep
)) {
3994 if (CHECK_FLAG(evi_vtep
->flags
, BGP_EVPN_EVI_VTEP_ACTIVE
))
4001 /* Number of active VTEPs associated with the ES */
4002 static uint32_t bgp_evpn_es_get_active_vtep_cnt(struct bgp_evpn_es
*es
)
4004 struct listnode
*node
;
4005 uint32_t vtep_cnt
= 0;
4006 struct bgp_evpn_es_vtep
*es_vtep
;
4008 for (ALL_LIST_ELEMENTS_RO(es
->es_vtep_list
, node
, es_vtep
)) {
4009 if (CHECK_FLAG(es_vtep
->flags
, BGP_EVPNES_VTEP_ACTIVE
))
4016 static struct bgp_evpn_es_vtep
*bgp_evpn_es_get_next_active_vtep(
4017 struct bgp_evpn_es
*es
, struct bgp_evpn_es_vtep
*es_vtep
)
4019 struct listnode
*node
;
4020 struct bgp_evpn_es_vtep
*next_es_vtep
;
4023 node
= listnextnode_unchecked(&es_vtep
->es_listnode
);
4025 node
= listhead(es
->es_vtep_list
);
4027 for (; node
; node
= listnextnode_unchecked(node
)) {
4028 next_es_vtep
= listgetdata(node
);
4029 if (CHECK_FLAG(next_es_vtep
->flags
, BGP_EVPNES_VTEP_ACTIVE
))
4030 return next_es_vtep
;
4036 static struct bgp_evpn_es_evi_vtep
*bgp_evpn_es_evi_get_next_active_vtep(
4037 struct bgp_evpn_es_evi
*es_evi
,
4038 struct bgp_evpn_es_evi_vtep
*evi_vtep
)
4040 struct listnode
*node
;
4041 struct bgp_evpn_es_evi_vtep
*next_evi_vtep
;
4044 node
= listnextnode_unchecked(&evi_vtep
->es_evi_listnode
);
4046 node
= listhead(es_evi
->es_evi_vtep_list
);
4048 for (; node
; node
= listnextnode_unchecked(node
)) {
4049 next_evi_vtep
= listgetdata(node
);
4050 if (CHECK_FLAG(next_evi_vtep
->flags
, BGP_EVPN_EVI_VTEP_ACTIVE
))
4051 return next_evi_vtep
;
4057 static void bgp_evpn_es_evi_set_inconsistent(struct bgp_evpn_es_evi
*es_evi
)
4059 if (!CHECK_FLAG(es_evi
->flags
, BGP_EVPNES_EVI_INCONS_VTEP_LIST
)) {
4060 if (BGP_DEBUG(evpn_mh
, EVPN_MH_ES
))
4061 zlog_debug("inconsistency detected - es %s evi %u vtep list mismatch",
4062 es_evi
->es
->esi_str
,
4064 SET_FLAG(es_evi
->flags
, BGP_EVPNES_EVI_INCONS_VTEP_LIST
);
4066 /* update parent ES with the incosistency setting */
4067 if (!es_evi
->es
->incons_evi_vtep_cnt
&&
4068 BGP_DEBUG(evpn_mh
, EVPN_MH_ES
))
4069 zlog_debug("inconsistency detected - es %s vtep list mismatch",
4070 es_evi
->es
->esi_str
);
4071 ++es_evi
->es
->incons_evi_vtep_cnt
;
4072 SET_FLAG(es_evi
->es
->inconsistencies
,
4073 BGP_EVPNES_INCONS_VTEP_LIST
);
4077 static uint32_t bgp_evpn_es_run_consistency_checks(struct bgp_evpn_es
*es
)
4080 int es_active_vtep_cnt
;
4081 int evi_active_vtep_cnt
;
4082 struct bgp_evpn_es_evi
*es_evi
;
4083 struct listnode
*evi_node
;
4084 struct bgp_evpn_es_vtep
*es_vtep
;
4085 struct bgp_evpn_es_evi_vtep
*evi_vtep
;
4087 /* reset the inconsistencies and re-evaluate */
4088 es
->incons_evi_vtep_cnt
= 0;
4089 es
->inconsistencies
= 0;
4091 es_active_vtep_cnt
= bgp_evpn_es_get_active_vtep_cnt(es
);
4092 for (ALL_LIST_ELEMENTS_RO(es
->es_evi_list
,
4093 evi_node
, es_evi
)) {
4096 /* reset the inconsistencies on the EVI and re-evaluate*/
4097 UNSET_FLAG(es_evi
->flags
, BGP_EVPNES_EVI_INCONS_VTEP_LIST
);
4099 evi_active_vtep_cnt
=
4100 bgp_evpn_es_evi_get_active_vtep_cnt(es_evi
);
4101 if (es_active_vtep_cnt
!= evi_active_vtep_cnt
) {
4102 bgp_evpn_es_evi_set_inconsistent(es_evi
);
4106 if (!es_active_vtep_cnt
)
4111 while ((es_vtep
= bgp_evpn_es_get_next_active_vtep(
4113 evi_vtep
= bgp_evpn_es_evi_get_next_active_vtep(es_evi
,
4116 bgp_evpn_es_evi_set_inconsistent(es_evi
);
4119 if (es_vtep
->vtep_ip
.s_addr
!=
4120 evi_vtep
->vtep_ip
.s_addr
) {
4121 /* inconsistency detected; set it and move
4124 bgp_evpn_es_evi_set_inconsistent(es_evi
);
4133 static int bgp_evpn_run_consistency_checks(struct thread
*t
)
4137 struct listnode
*node
;
4138 struct listnode
*nextnode
;
4139 struct bgp_evpn_es
*es
;
4141 for (ALL_LIST_ELEMENTS(bgp_mh_info
->pend_es_list
,
4142 node
, nextnode
, es
)) {
4145 /* run consistency checks on the ES and remove it from the
4148 proc_cnt
+= bgp_evpn_es_run_consistency_checks(es
);
4149 bgp_evpn_es_cons_checks_pend_del(es
);
4154 /* restart the timer */
4155 thread_add_timer(bm
->master
, bgp_evpn_run_consistency_checks
, NULL
,
4156 BGP_EVPN_CONS_CHECK_INTERVAL
,
4157 &bgp_mh_info
->t_cons_check
);
4162 /*****************************************************************************
4163 * EVPN-Nexthop and RMAC management: nexthops associated with Type-2 routes
4164 * that have an ES as destination are consolidated by BGP into a per-VRF
4165 * nh->rmac mapping which is sent to zebra. Zebra installs the nexthop
4166 * as a remote neigh/fdb entry with a dummy (type-1) prefix referencing it.
4168 * This handling is needed because Type-2 routes with ES as dest use NHG
4169 * that is setup using EAD routes (i.e. such NHGs do not include the
4171 ****************************************************************************/
4172 static void bgp_evpn_nh_zebra_update_send(struct bgp_evpn_nh
*nh
, bool add
)
4175 struct bgp
*bgp_vrf
= nh
->bgp_vrf
;
4178 if (!zclient
|| zclient
->sock
< 0)
4181 /* Don't try to register if Zebra doesn't know of this instance. */
4182 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp_vrf
)) {
4183 if (BGP_DEBUG(zebra
, ZEBRA
))
4184 zlog_debug("No zebra instance, not %s remote nh %s",
4185 add
? "adding" : "deleting", nh
->nh_str
);
4192 zclient_create_header(
4193 s
, add
? ZEBRA_EVPN_REMOTE_NH_ADD
: ZEBRA_EVPN_REMOTE_NH_DEL
,
4195 stream_putl(s
, bgp_vrf
->vrf_id
);
4196 stream_put(s
, &nh
->ip
, sizeof(nh
->ip
));
4198 stream_put(s
, &nh
->rmac
, sizeof(nh
->rmac
));
4200 stream_putw_at(s
, 0, stream_get_endp(s
));
4202 if (BGP_DEBUG(evpn_mh
, EVPN_MH_ES
)) {
4204 zlog_debug("evpn vrf %s nh %s rmac %pEA add to zebra",
4205 nh
->bgp_vrf
->name
, nh
->nh_str
, &nh
->rmac
);
4206 else if (BGP_DEBUG(evpn_mh
, EVPN_MH_ES
))
4207 zlog_debug("evpn vrf %s nh %s del to zebra",
4208 nh
->bgp_vrf
->name
, nh
->nh_str
);
4211 frrtrace(2, frr_bgp
, evpn_mh_nh_rmac_zsend
, add
, nh
);
4213 zclient_send_message(zclient
);
4216 static void bgp_evpn_nh_zebra_update(struct bgp_evpn_nh
*nh
, bool add
)
4218 if (add
&& !is_zero_mac(&nh
->rmac
)) {
4219 nh
->flags
|= BGP_EVPN_NH_READY_FOR_ZEBRA
;
4220 bgp_evpn_nh_zebra_update_send(nh
, true);
4222 if (!(nh
->flags
& BGP_EVPN_NH_READY_FOR_ZEBRA
))
4224 nh
->flags
&= ~BGP_EVPN_NH_READY_FOR_ZEBRA
;
4225 bgp_evpn_nh_zebra_update_send(nh
, false);
4229 static void *bgp_evpn_nh_alloc(void *p
)
4231 struct bgp_evpn_nh
*tmp_n
= p
;
4232 struct bgp_evpn_nh
*n
;
4234 n
= XCALLOC(MTYPE_BGP_EVPN_NH
, sizeof(struct bgp_evpn_nh
));
4240 static struct bgp_evpn_nh
*bgp_evpn_nh_find(struct bgp
*bgp_vrf
,
4243 struct bgp_evpn_nh tmp
;
4244 struct bgp_evpn_nh
*n
;
4246 memset(&tmp
, 0, sizeof(tmp
));
4247 memcpy(&tmp
.ip
, ip
, sizeof(struct ipaddr
));
4248 n
= hash_lookup(bgp_vrf
->evpn_nh_table
, &tmp
);
4253 /* Add nexthop entry - implicitly created on first path reference */
4254 static struct bgp_evpn_nh
*bgp_evpn_nh_add(struct bgp
*bgp_vrf
,
4256 struct bgp_path_info
*pi
)
4258 struct bgp_evpn_nh tmp_n
;
4259 struct bgp_evpn_nh
*n
= NULL
;
4261 memset(&tmp_n
, 0, sizeof(struct bgp_evpn_nh
));
4262 memcpy(&tmp_n
.ip
, ip
, sizeof(struct ipaddr
));
4263 n
= hash_get(bgp_vrf
->evpn_nh_table
, &tmp_n
, bgp_evpn_nh_alloc
);
4264 ipaddr2str(ip
, n
->nh_str
, sizeof(n
->nh_str
));
4265 n
->bgp_vrf
= bgp_vrf
;
4267 n
->pi_list
= list_new();
4268 listset_app_node_mem(n
->pi_list
);
4270 /* Setup ref_pi when the nh is created */
4271 if (CHECK_FLAG(pi
->flags
, BGP_PATH_VALID
) && pi
->attr
) {
4273 memcpy(&n
->rmac
, &pi
->attr
->rmac
, ETH_ALEN
);
4276 if (BGP_DEBUG(evpn_mh
, EVPN_MH_ES
))
4277 zlog_debug("evpn vrf %s nh %s rmac %pEA add", n
->bgp_vrf
->name
,
4278 n
->nh_str
, &n
->rmac
);
4279 bgp_evpn_nh_zebra_update(n
, true);
4283 /* Delete nexthop entry if there are no paths referencing it */
4284 static void bgp_evpn_nh_del(struct bgp_evpn_nh
*n
)
4286 struct bgp_evpn_nh
*tmp_n
;
4287 struct bgp
*bgp_vrf
= n
->bgp_vrf
;
4289 if (listcount(n
->pi_list
))
4292 if (BGP_DEBUG(evpn_mh
, EVPN_MH_ES
))
4293 zlog_debug("evpn vrf %s nh %s del to zebra", bgp_vrf
->name
,
4296 bgp_evpn_nh_zebra_update(n
, false);
4297 list_delete(&n
->pi_list
);
4298 tmp_n
= hash_release(bgp_vrf
->evpn_nh_table
, n
);
4299 XFREE(MTYPE_BGP_EVPN_NH
, tmp_n
);
4302 static unsigned int bgp_evpn_nh_hash_keymake(const void *p
)
4304 const struct bgp_evpn_nh
*n
= p
;
4305 const struct ipaddr
*ip
= &n
->ip
;
4307 if (IS_IPADDR_V4(ip
))
4308 return jhash_1word(ip
->ipaddr_v4
.s_addr
, 0);
4310 return jhash2(ip
->ipaddr_v6
.s6_addr32
,
4311 array_size(ip
->ipaddr_v6
.s6_addr32
), 0);
4314 static bool bgp_evpn_nh_cmp(const void *p1
, const void *p2
)
4316 const struct bgp_evpn_nh
*n1
= p1
;
4317 const struct bgp_evpn_nh
*n2
= p2
;
4319 if (n1
== NULL
&& n2
== NULL
)
4322 if (n1
== NULL
|| n2
== NULL
)
4325 return (memcmp(&n1
->ip
, &n2
->ip
, sizeof(struct ipaddr
)) == 0);
4328 void bgp_evpn_nh_init(struct bgp
*bgp_vrf
)
4330 if (BGP_DEBUG(evpn_mh
, EVPN_MH_ES
))
4331 zlog_debug("evpn vrf %s nh init", bgp_vrf
->name
);
4332 bgp_vrf
->evpn_nh_table
= hash_create(
4333 bgp_evpn_nh_hash_keymake
, bgp_evpn_nh_cmp
, "BGP EVPN NH table");
4336 static void bgp_evpn_nh_flush_entry(struct bgp_evpn_nh
*nh
)
4338 struct listnode
*node
;
4339 struct listnode
*nnode
;
4340 struct bgp_path_evpn_nh_info
*nh_info
;
4342 if (BGP_DEBUG(evpn_mh
, EVPN_MH_ES
))
4343 zlog_debug("evpn vrf %s nh %s flush", nh
->bgp_vrf
->name
,
4346 /* force flush paths */
4347 for (ALL_LIST_ELEMENTS(nh
->pi_list
, node
, nnode
, nh_info
))
4348 bgp_evpn_path_nh_del(nh
->bgp_vrf
, nh_info
->pi
);
4351 static void bgp_evpn_nh_flush_cb(struct hash_bucket
*bucket
, void *ctxt
)
4353 struct bgp_evpn_nh
*nh
= (struct bgp_evpn_nh
*)bucket
->data
;
4355 bgp_evpn_nh_flush_entry(nh
);
4358 void bgp_evpn_nh_finish(struct bgp
*bgp_vrf
)
4360 if (BGP_DEBUG(evpn_mh
, EVPN_MH_ES
))
4361 zlog_debug("evpn vrf %s nh finish", bgp_vrf
->name
);
4363 bgp_vrf
->evpn_nh_table
,
4364 (void (*)(struct hash_bucket
*, void *))bgp_evpn_nh_flush_cb
,
4366 hash_free(bgp_vrf
->evpn_nh_table
);
4367 bgp_vrf
->evpn_nh_table
= NULL
;
4370 static void bgp_evpn_nh_update_ref_pi(struct bgp_evpn_nh
*nh
)
4372 struct listnode
*node
;
4373 struct bgp_path_info
*pi
;
4374 struct bgp_path_evpn_nh_info
*nh_info
;
4379 for (ALL_LIST_ELEMENTS_RO(nh
->pi_list
, node
, nh_info
)) {
4381 if (!CHECK_FLAG(pi
->flags
, BGP_PATH_VALID
) || !pi
->attr
)
4384 if (BGP_DEBUG(evpn_mh
, EVPN_MH_ES
))
4385 zlog_debug("evpn vrf %s nh %s ref_pi update",
4386 nh
->bgp_vrf
->name
, nh
->nh_str
);
4388 /* If we have a new pi copy rmac from it and update
4389 * zebra if the new rmac is different
4391 if (memcmp(&nh
->rmac
, &nh
->ref_pi
->attr
->rmac
, ETH_ALEN
)) {
4392 memcpy(&nh
->rmac
, &nh
->ref_pi
->attr
->rmac
, ETH_ALEN
);
4393 bgp_evpn_nh_zebra_update(nh
, true);
4399 static void bgp_evpn_nh_clear_ref_pi(struct bgp_evpn_nh
*nh
,
4400 struct bgp_path_info
*pi
)
4402 if (nh
->ref_pi
!= pi
)
4405 if (BGP_DEBUG(evpn_mh
, EVPN_MH_ES
))
4406 zlog_debug("evpn vrf %s nh %s ref_pi clear", nh
->bgp_vrf
->name
,
4409 /* try to find another ref_pi */
4410 bgp_evpn_nh_update_ref_pi(nh
);
4411 /* couldn't find one - clear the old rmac and notify zebra */
4413 memset(&nh
->rmac
, 0, ETH_ALEN
);
4414 bgp_evpn_nh_zebra_update(nh
, true);
4418 static void bgp_evpn_path_nh_info_free(struct bgp_path_evpn_nh_info
*nh_info
)
4420 bgp_evpn_path_nh_unlink(nh_info
);
4421 XFREE(MTYPE_BGP_EVPN_PATH_NH_INFO
, nh_info
);
4424 static struct bgp_path_evpn_nh_info
*
4425 bgp_evpn_path_nh_info_new(struct bgp_path_info
*pi
)
4427 struct bgp_path_info_extra
*e
;
4428 struct bgp_path_mh_info
*mh_info
;
4429 struct bgp_path_evpn_nh_info
*nh_info
;
4431 e
= bgp_path_info_extra_get(pi
);
4433 /* If mh_info doesn't exist allocate it */
4434 mh_info
= e
->mh_info
;
4436 e
->mh_info
= mh_info
= XCALLOC(MTYPE_BGP_EVPN_PATH_MH_INFO
,
4437 sizeof(struct bgp_path_mh_info
));
4439 /* If nh_info doesn't exist allocate it */
4440 nh_info
= mh_info
->nh_info
;
4442 mh_info
->nh_info
= nh_info
=
4443 XCALLOC(MTYPE_BGP_EVPN_PATH_NH_INFO
,
4444 sizeof(struct bgp_path_evpn_nh_info
));
4451 static void bgp_evpn_path_nh_unlink(struct bgp_path_evpn_nh_info
*nh_info
)
4453 struct bgp_evpn_nh
*nh
= nh_info
->nh
;
4454 struct bgp_path_info
*pi
;
4455 char prefix_buf
[PREFIX_STRLEN
];
4461 if (BGP_DEBUG(evpn_mh
, EVPN_MH_RT
))
4462 zlog_debug("path %s unlinked from nh %s %s",
4463 pi
->net
? prefix2str(&pi
->net
->p
, prefix_buf
,
4466 nh
->bgp_vrf
->name
, nh
->nh_str
);
4468 list_delete_node(nh
->pi_list
, &nh_info
->nh_listnode
);
4472 /* check if the ref_pi need to be updated */
4473 bgp_evpn_nh_clear_ref_pi(nh
, pi
);
4475 /* if there are no other references against the nh it
4478 bgp_evpn_nh_del(nh
);
4480 /* Note we don't free the path nh_info on unlink; it will be freed up
4481 * along with the path.
4485 static void bgp_evpn_path_nh_link(struct bgp
*bgp_vrf
, struct bgp_path_info
*pi
)
4487 struct bgp_path_evpn_nh_info
*nh_info
;
4488 struct bgp_evpn_nh
*nh
;
4491 /* EVPN nexthop setup in bgp has been turned off */
4492 if (!bgp_mh_info
->bgp_evpn_nh_setup
)
4495 if (!bgp_vrf
->evpn_nh_table
) {
4496 if (BGP_DEBUG(evpn_mh
, EVPN_MH_RT
))
4497 zlog_debug("path %pFX linked to vrf %s failed",
4498 &pi
->net
->p
, bgp_vrf
->name
);
4502 nh_info
= (pi
->extra
&& pi
->extra
->mh_info
)
4503 ? pi
->extra
->mh_info
->nh_info
4506 /* if NHG is not being used for this path we don't need to manage the
4507 * nexthops in bgp (they are managed by zebra instead)
4509 if (!(pi
->attr
->es_flags
& ATTR_ES_L3_NHG_USE
)) {
4511 bgp_evpn_path_nh_unlink(nh_info
);
4515 /* setup nh_info against the path if it doesn't aleady exist */
4517 nh_info
= bgp_evpn_path_nh_info_new(pi
);
4519 /* find-create nh */
4520 memset(&ip
, 0, sizeof(ip
));
4521 if (pi
->net
->p
.family
== AF_INET6
) {
4523 memcpy(&ip
.ipaddr_v6
, &pi
->attr
->mp_nexthop_global
,
4524 sizeof(ip
.ipaddr_v6
));
4527 memcpy(&ip
.ipaddr_v4
, &pi
->attr
->nexthop
, sizeof(ip
.ipaddr_v4
));
4530 nh
= bgp_evpn_nh_find(bgp_vrf
, &ip
);
4532 nh
= bgp_evpn_nh_add(bgp_vrf
, &ip
, pi
);
4535 if (nh_info
->nh
== nh
) {
4536 /* Check if any of the paths are now valid */
4537 bgp_evpn_nh_update_ref_pi(nh
);
4541 /* unlink old nh if any */
4542 bgp_evpn_path_nh_unlink(nh_info
);
4544 if (BGP_DEBUG(evpn_mh
, EVPN_MH_RT
))
4545 zlog_debug("path %pFX linked to nh %s %s", &pi
->net
->p
,
4546 nh
->bgp_vrf
->name
, nh
->nh_str
);
4548 /* link mac-ip path to the new nh */
4550 listnode_init(&nh_info
->nh_listnode
, nh_info
);
4551 listnode_add(nh
->pi_list
, &nh_info
->nh_listnode
);
4552 /* If a new valid path got linked to the nh see if can get the rmac
4555 bgp_evpn_nh_update_ref_pi(nh
);
4556 if (BGP_DEBUG(evpn_mh
, EVPN_MH_ES
)) {
4559 "path %pFX linked to nh %s %s with no valid pi",
4560 &pi
->net
->p
, nh
->bgp_vrf
->name
, nh
->nh_str
);
4564 void bgp_evpn_path_nh_del(struct bgp
*bgp_vrf
, struct bgp_path_info
*pi
)
4566 struct bgp_path_evpn_nh_info
*nh_info
;
4568 nh_info
= (pi
->extra
&& pi
->extra
->mh_info
)
4569 ? pi
->extra
->mh_info
->nh_info
4575 bgp_evpn_path_nh_unlink(nh_info
);
4578 void bgp_evpn_path_nh_add(struct bgp
*bgp_vrf
, struct bgp_path_info
*pi
)
4580 bgp_evpn_path_nh_link(bgp_vrf
, pi
);
4583 static void bgp_evpn_nh_show_entry(struct bgp_evpn_nh
*nh
, struct vty
*vty
,
4584 json_object
*json_array
)
4586 json_object
*json
= NULL
;
4587 char mac_buf
[ETHER_ADDR_STRLEN
];
4588 char prefix_buf
[PREFIX_STRLEN
];
4591 /* create a separate json object for each ES */
4592 json
= json_object_new_object();
4594 prefix_mac2str(&nh
->rmac
, mac_buf
, sizeof(mac_buf
));
4595 if (nh
->ref_pi
&& nh
->ref_pi
->net
)
4596 prefix2str(&nh
->ref_pi
->net
->p
, prefix_buf
, sizeof(prefix_buf
));
4598 prefix_buf
[0] = '\0';
4600 json_object_string_add(json
, "vrf", nh
->bgp_vrf
->name
);
4601 json_object_string_add(json
, "ip", nh
->nh_str
);
4602 json_object_string_add(json
, "rmac", mac_buf
);
4603 json_object_string_add(json
, "basePath", prefix_buf
);
4604 json_object_int_add(json
, "pathCount", listcount(nh
->pi_list
));
4606 vty_out(vty
, "%-15s %-15s %-17s %-10d %s\n", nh
->bgp_vrf
->name
,
4607 nh
->nh_str
, mac_buf
, listcount(nh
->pi_list
),
4611 /* add ES to the json array */
4613 json_object_array_add(json_array
, json
);
4616 struct nh_show_ctx
{
4621 static void bgp_evpn_nh_show_hash_cb(struct hash_bucket
*bucket
, void *ctxt
)
4623 struct bgp_evpn_nh
*nh
= (struct bgp_evpn_nh
*)bucket
->data
;
4624 struct nh_show_ctx
*wctx
= (struct nh_show_ctx
*)ctxt
;
4626 bgp_evpn_nh_show_entry(nh
, wctx
->vty
, wctx
->json
);
4629 /* Display all evpn nexthops */
4630 void bgp_evpn_nh_show(struct vty
*vty
, bool uj
)
4632 json_object
*json_array
= NULL
;
4633 struct bgp
*bgp_vrf
;
4634 struct listnode
*node
;
4635 struct nh_show_ctx wctx
;
4638 /* create an array of nexthops */
4639 json_array
= json_object_new_array();
4641 vty_out(vty
, "%-15s %-15s %-17s %-10s %s\n", "VRF", "IP",
4642 "RMAC", "#Paths", "Base Path");
4646 wctx
.json
= json_array
;
4648 /* walk through all vrfs */
4649 for (ALL_LIST_ELEMENTS_RO(bm
->bgp
, node
, bgp_vrf
)) {
4650 hash_iterate(bgp_vrf
->evpn_nh_table
,
4651 (void (*)(struct hash_bucket
*,
4652 void *))bgp_evpn_nh_show_hash_cb
,
4656 /* print the array of json-ESs */
4658 vty_out(vty
, "%s\n",
4659 json_object_to_json_string_ext(
4660 json_array
, JSON_C_TO_STRING_PRETTY
));
4661 json_object_free(json_array
);
4665 /*****************************************************************************/
4666 void bgp_evpn_mh_init(void)
4668 bm
->mh_info
= XCALLOC(MTYPE_BGP_EVPN_MH_INFO
, sizeof(*bm
->mh_info
));
4670 /* setup ES tables */
4671 RB_INIT(bgp_es_rb_head
, &bgp_mh_info
->es_rb_tree
);
4673 bgp_mh_info
->local_es_list
= list_new();
4674 listset_app_node_mem(bgp_mh_info
->local_es_list
);
4675 /* list of ESs with pending processing */
4676 bgp_mh_info
->pend_es_list
= list_new();
4677 listset_app_node_mem(bgp_mh_info
->pend_es_list
);
4679 bgp_mh_info
->ead_evi_rx
= BGP_EVPN_MH_EAD_EVI_RX_DEF
;
4680 bgp_mh_info
->ead_evi_tx
= BGP_EVPN_MH_EAD_EVI_TX_DEF
;
4682 /* config knobs - XXX add cli to control it */
4683 bgp_mh_info
->ead_evi_adv_for_down_links
= true;
4684 bgp_mh_info
->consistency_checking
= true;
4685 bgp_mh_info
->install_l3nhg
= false;
4686 bgp_mh_info
->host_routes_use_l3nhg
= BGP_EVPN_MH_USE_ES_L3NHG_DEF
;
4687 bgp_mh_info
->suppress_l3_ecomm_on_inactive_es
= true;
4688 bgp_mh_info
->bgp_evpn_nh_setup
= true;
4690 memset(&zero_esi_buf
, 0, sizeof(esi_t
));
4693 void bgp_evpn_mh_finish(void)
4695 struct bgp_evpn_es
*es
;
4696 struct bgp_evpn_es
*es_next
;
4698 if (BGP_DEBUG(evpn_mh
, EVPN_MH_RT
))
4699 zlog_debug("evpn mh finish");
4701 RB_FOREACH_SAFE (es
, bgp_es_rb_head
, &bgp_mh_info
->es_rb_tree
,
4703 bgp_evpn_es_local_info_clear(es
, true);
4705 if (bgp_mh_info
->t_cons_check
)
4706 thread_cancel(&bgp_mh_info
->t_cons_check
);
4707 list_delete(&bgp_mh_info
->local_es_list
);
4708 list_delete(&bgp_mh_info
->pend_es_list
);
4710 XFREE(MTYPE_BGP_EVPN_MH_INFO
, bgp_mh_info
);
4713 /* This function is called when disable-ead-evi-rx knob flaps */
4714 void bgp_evpn_switch_ead_evi_rx(void)
4717 struct bgp_evpn_es
*es
;
4718 struct bgp_evpn_es_evi
*es_evi
;
4719 struct listnode
*evi_node
= NULL
;
4720 struct listnode
*evi_next
= NULL
;
4721 struct bgp_evpn_es_evi_vtep
*vtep
;
4722 struct listnode
*vtep_node
= NULL
;
4723 struct listnode
*vtep_next
= NULL
;
4725 bgp
= bgp_get_evpn();
4730 * Process all the remote es_evi_vteps and reevaluate if the es_evi_vtep
4733 RB_FOREACH(es
, bgp_es_rb_head
, &bgp_mh_info
->es_rb_tree
) {
4734 if (!CHECK_FLAG(es
->flags
, BGP_EVPNES_REMOTE
))
4737 for (ALL_LIST_ELEMENTS(es
->es_evi_list
, evi_node
, evi_next
,
4739 if (!CHECK_FLAG(es_evi
->flags
, BGP_EVPNES_EVI_REMOTE
))
4742 for (ALL_LIST_ELEMENTS(es_evi
->es_evi_vtep_list
,
4743 vtep_node
, vtep_next
, vtep
))
4744 bgp_evpn_es_evi_vtep_re_eval_active(bgp
, vtep
);