]> git.proxmox.com Git - mirror_frr.git/blame - bgpd/bgp_evpn_mh.c
Merge pull request #13060 from opensourcerouting/feature/allow_peering_with_127.0.0.1
[mirror_frr.git] / bgpd / bgp_evpn_mh.c
CommitLineData
acddc0ed 1// SPDX-License-Identifier: GPL-2.0-or-later
185fb14a
AK
2/* EVPN Multihoming procedures
3 *
4 * Copyright (C) 2019 Cumulus Networks, Inc.
c44ab6f1 5 * Anuradha Karuppiah
185fb14a 6 *
185fb14a
AK
7 */
8
9#include <zebra.h>
10
11#include "command.h"
12#include "filter.h"
13#include "prefix.h"
14#include "log.h"
15#include "memory.h"
16#include "stream.h"
17#include "hash.h"
18#include "jhash.h"
19#include "zclient.h"
20
23d0a753
DA
21#include "lib/printfrr.h"
22
185fb14a
AK
23#include "bgpd/bgp_attr_evpn.h"
24#include "bgpd/bgpd.h"
25#include "bgpd/bgp_table.h"
26#include "bgpd/bgp_route.h"
27#include "bgpd/bgp_attr.h"
28#include "bgpd/bgp_mplsvpn.h"
29#include "bgpd/bgp_evpn.h"
30#include "bgpd/bgp_evpn_private.h"
31#include "bgpd/bgp_evpn_mh.h"
32#include "bgpd/bgp_ecommunity.h"
33#include "bgpd/bgp_encap_types.h"
34#include "bgpd/bgp_debug.h"
35#include "bgpd/bgp_errors.h"
36#include "bgpd/bgp_aspath.h"
37#include "bgpd/bgp_zebra.h"
38#include "bgpd/bgp_addpath.h"
c44ab6f1 39#include "bgpd/bgp_label.h"
c589d847 40#include "bgpd/bgp_nht.h"
6348981a 41#include "bgpd/bgp_mpath.h"
23aa35ad 42#include "bgpd/bgp_trace.h"
c44ab6f1
AK
43
44static void bgp_evpn_local_es_down(struct bgp *bgp,
45 struct bgp_evpn_es *es);
46static void bgp_evpn_local_type1_evi_route_del(struct bgp *bgp,
47 struct bgp_evpn_es *es);
48static struct bgp_evpn_es_vtep *bgp_evpn_es_vtep_add(struct bgp *bgp,
74e2bd89
AK
49 struct bgp_evpn_es *es,
50 struct in_addr vtep_ip,
51 bool esr, uint8_t df_alg,
52 uint16_t df_pref);
c44ab6f1
AK
53static void bgp_evpn_es_vtep_del(struct bgp *bgp,
54 struct bgp_evpn_es *es, struct in_addr vtep_ip, bool esr);
55static void bgp_evpn_es_cons_checks_pend_add(struct bgp_evpn_es *es);
56static void bgp_evpn_es_cons_checks_pend_del(struct bgp_evpn_es *es);
333ad64b
AK
57static struct bgp_evpn_es_evi *
58bgp_evpn_local_es_evi_do_del(struct bgp_evpn_es_evi *es_evi);
c589d847
AK
59static uint32_t bgp_evpn_es_get_active_vtep_cnt(struct bgp_evpn_es *es);
60static void bgp_evpn_l3nhg_update_on_vtep_chg(struct bgp_evpn_es *es);
26c03e43
AK
61static struct bgp_evpn_es *bgp_evpn_es_new(struct bgp *bgp, const esi_t *esi);
62static void bgp_evpn_es_free(struct bgp_evpn_es *es, const char *caller);
60605cbc 63static void bgp_evpn_path_es_unlink(struct bgp_path_es_info *es_info);
74efb822
AK
64static void bgp_evpn_mac_update_on_es_local_chg(struct bgp_evpn_es *es,
65 bool is_local);
c44ab6f1
AK
66
67esi_t zero_esi_buf, *zero_esi = &zero_esi_buf;
e6685141 68static void bgp_evpn_run_consistency_checks(struct event *t);
090efa2f
AK
69static void bgp_evpn_path_nh_info_free(struct bgp_path_evpn_nh_info *nh_info);
70static void bgp_evpn_path_nh_unlink(struct bgp_path_evpn_nh_info *nh_info);
c44ab6f1
AK
71
72/******************************************************************************
73 * per-ES (Ethernet Segment) routing table
74 *
75 * Following routes are added to the ES's routing table -
76 * 1. Local and remote ESR (Type-4)
77 * 2. Local EAD-per-ES (Type-1).
78 *
79 * Key for these routes is {ESI, VTEP-IP} so the path selection is practically
80 * a no-op i.e. all paths lead to same VTEP-IP (i.e. result in the same VTEP
81 * being added to same ES).
82 *
83 * Note the following routes go into the VNI routing table (instead of the
84 * ES routing table) -
85 * 1. Remote EAD-per-ES
86 * 2. Local and remote EAD-per-EVI
185fb14a 87 */
185fb14a 88
c44ab6f1
AK
89/* Calculate the best path for a multi-homing (Type-1 or Type-4) route
90 * installed in the ES's routing table.
185fb14a 91 */
c44ab6f1 92static int bgp_evpn_es_route_select_install(struct bgp *bgp,
09319b4e
DS
93 struct bgp_evpn_es *es,
94 struct bgp_dest *dest)
185fb14a
AK
95{
96 int ret = 0;
97 afi_t afi = AFI_L2VPN;
98 safi_t safi = SAFI_EVPN;
99 struct bgp_path_info *old_select; /* old best */
100 struct bgp_path_info *new_select; /* new best */
101 struct bgp_path_info_pair old_and_new;
102
103 /* Compute the best path. */
09319b4e
DS
104 bgp_best_selection(bgp, dest, &bgp->maxpaths[afi][safi], &old_and_new,
105 afi, safi);
185fb14a
AK
106 old_select = old_and_new.old;
107 new_select = old_and_new.new;
108
109 /*
110 * If the best path hasn't changed - see if something needs to be
111 * updated
112 */
113 if (old_select && old_select == new_select
09319b4e
DS
114 && old_select->type == ZEBRA_ROUTE_BGP
115 && old_select->sub_type == BGP_ROUTE_IMPORTED
116 && !CHECK_FLAG(dest->flags, BGP_NODE_USER_CLEAR)
117 && !CHECK_FLAG(old_select->flags, BGP_PATH_ATTR_CHANGED)
118 && !bgp_addpath_is_addpath_used(&bgp->tx_addpath, afi, safi)) {
185fb14a 119 if (bgp_zebra_has_route_changed(old_select)) {
74e2bd89
AK
120 bgp_evpn_es_vtep_add(bgp, es, old_select->attr->nexthop,
121 true /*esr*/,
122 old_select->attr->df_alg,
123 old_select->attr->df_pref);
185fb14a
AK
124 }
125 UNSET_FLAG(old_select->flags, BGP_PATH_MULTIPATH_CHG);
09319b4e 126 bgp_zebra_clear_route_change_flags(dest);
185fb14a
AK
127 return ret;
128 }
129
130 /* If the user did a "clear" this flag will be set */
09319b4e 131 UNSET_FLAG(dest->flags, BGP_NODE_USER_CLEAR);
185fb14a 132
c44ab6f1 133 /* bestpath has changed; update relevant fields and install or uninstall
185fb14a
AK
134 * into the zebra RIB.
135 */
136 if (old_select || new_select)
09319b4e 137 bgp_bump_version(dest);
185fb14a
AK
138
139 if (old_select)
09319b4e 140 bgp_path_info_unset_flag(dest, old_select, BGP_PATH_SELECTED);
185fb14a 141 if (new_select) {
09319b4e
DS
142 bgp_path_info_set_flag(dest, new_select, BGP_PATH_SELECTED);
143 bgp_path_info_unset_flag(dest, new_select,
144 BGP_PATH_ATTR_CHANGED);
185fb14a
AK
145 UNSET_FLAG(new_select->flags, BGP_PATH_MULTIPATH_CHG);
146 }
147
148 if (new_select && new_select->type == ZEBRA_ROUTE_BGP
c44ab6f1 149 && new_select->sub_type == BGP_ROUTE_IMPORTED) {
74e2bd89
AK
150 bgp_evpn_es_vtep_add(bgp, es, new_select->attr->nexthop,
151 true /*esr */, new_select->attr->df_alg,
152 new_select->attr->df_pref);
185fb14a
AK
153 } else {
154 if (old_select && old_select->type == ZEBRA_ROUTE_BGP
c44ab6f1
AK
155 && old_select->sub_type == BGP_ROUTE_IMPORTED)
156 bgp_evpn_es_vtep_del(
157 bgp, es, old_select->attr->nexthop,
158 true /*esr*/);
185fb14a
AK
159 }
160
161 /* Clear any route change flags. */
09319b4e 162 bgp_zebra_clear_route_change_flags(dest);
185fb14a
AK
163
164 /* Reap old select bgp_path_info, if it has been removed */
165 if (old_select && CHECK_FLAG(old_select->flags, BGP_PATH_REMOVED))
09319b4e 166 bgp_path_info_reap(dest, old_select);
185fb14a
AK
167
168 return ret;
169}
170
c44ab6f1
AK
171/* Install Type-1/Type-4 route entry in the per-ES routing table */
172static int bgp_evpn_es_route_install(struct bgp *bgp,
173 struct bgp_evpn_es *es, struct prefix_evpn *p,
174 struct bgp_path_info *parent_pi)
175{
176 int ret = 0;
09319b4e 177 struct bgp_dest *dest = NULL;
c44ab6f1
AK
178 struct bgp_path_info *pi = NULL;
179 struct attr *attr_new = NULL;
180
181 /* Create (or fetch) route within the VNI.
182 * NOTE: There is no RD here.
183 */
09319b4e 184 dest = bgp_node_get(es->route_table, (struct prefix *)p);
c44ab6f1
AK
185
186 /* Check if route entry is already present. */
09319b4e 187 for (pi = bgp_dest_get_bgp_path_info(dest); pi; pi = pi->next)
671ec576 188 if (pi->extra &&
189 (struct bgp_path_info *)pi->extra->parent == parent_pi)
c44ab6f1
AK
190 break;
191
192 if (!pi) {
193 /* Add (or update) attribute to hash. */
194 attr_new = bgp_attr_intern(parent_pi->attr);
195
196 /* Create new route with its attribute. */
197 pi = info_make(parent_pi->type, BGP_ROUTE_IMPORTED, 0,
09319b4e 198 parent_pi->peer, attr_new, dest);
c44ab6f1
AK
199 SET_FLAG(pi->flags, BGP_PATH_VALID);
200 bgp_path_info_extra_get(pi);
201 pi->extra->parent = bgp_path_info_lock(parent_pi);
09319b4e
DS
202 bgp_dest_lock_node((struct bgp_dest *)parent_pi->net);
203 bgp_path_info_add(dest, pi);
c44ab6f1
AK
204 } else {
205 if (attrhash_cmp(pi->attr, parent_pi->attr)
206 && !CHECK_FLAG(pi->flags, BGP_PATH_REMOVED)) {
09319b4e 207 bgp_dest_unlock_node(dest);
c44ab6f1
AK
208 return 0;
209 }
210 /* The attribute has changed. */
211 /* Add (or update) attribute to hash. */
212 attr_new = bgp_attr_intern(parent_pi->attr);
213
214 /* Restore route, if needed. */
215 if (CHECK_FLAG(pi->flags, BGP_PATH_REMOVED))
09319b4e 216 bgp_path_info_restore(dest, pi);
c44ab6f1
AK
217
218 /* Mark if nexthop has changed. */
219 if (!IPV4_ADDR_SAME(&pi->attr->nexthop, &attr_new->nexthop))
220 SET_FLAG(pi->flags, BGP_PATH_IGP_CHANGED);
221
222 /* Unintern existing, set to new. */
223 bgp_attr_unintern(&pi->attr);
224 pi->attr = attr_new;
083ec940 225 pi->uptime = monotime(NULL);
c44ab6f1
AK
226 }
227
228 /* Perform route selection and update zebra, if required. */
09319b4e 229 ret = bgp_evpn_es_route_select_install(bgp, es, dest);
c44ab6f1 230
09319b4e 231 bgp_dest_unlock_node(dest);
c44ab6f1
AK
232
233 return ret;
234}
235
236/* Uninstall Type-1/Type-4 route entry from the ES routing table */
237static int bgp_evpn_es_route_uninstall(struct bgp *bgp, struct bgp_evpn_es *es,
238 struct prefix_evpn *p, struct bgp_path_info *parent_pi)
239{
240 int ret;
09319b4e 241 struct bgp_dest *dest;
c44ab6f1
AK
242 struct bgp_path_info *pi;
243
244 if (!es->route_table)
245 return 0;
246
247 /* Locate route within the ESI.
248 * NOTE: There is no RD here.
249 */
09319b4e
DS
250 dest = bgp_node_lookup(es->route_table, (struct prefix *)p);
251 if (!dest)
c44ab6f1
AK
252 return 0;
253
254 /* Find matching route entry. */
09319b4e 255 for (pi = bgp_dest_get_bgp_path_info(dest); pi; pi = pi->next)
c44ab6f1
AK
256 if (pi->extra
257 && (struct bgp_path_info *)pi->extra->parent ==
258 parent_pi)
259 break;
260
e71ad4b6
DA
261 if (!pi) {
262 bgp_dest_unlock_node(dest);
c44ab6f1 263 return 0;
e71ad4b6 264 }
c44ab6f1
AK
265
266 /* Mark entry for deletion */
09319b4e 267 bgp_path_info_delete(dest, pi);
c44ab6f1
AK
268
269 /* Perform route selection and update zebra, if required. */
09319b4e 270 ret = bgp_evpn_es_route_select_install(bgp, es, dest);
c44ab6f1
AK
271
272 /* Unlock route node. */
09319b4e 273 bgp_dest_unlock_node(dest);
c44ab6f1
AK
274
275 return ret;
276}
277
fff7545a 278/* Install or unistall a Type-4 route in the per-ES routing table */
c44ab6f1
AK
279int bgp_evpn_es_route_install_uninstall(struct bgp *bgp, struct bgp_evpn_es *es,
280 afi_t afi, safi_t safi, struct prefix_evpn *evp,
281 struct bgp_path_info *pi, int install)
282{
283 int ret = 0;
284
285 if (install)
286 ret = bgp_evpn_es_route_install(bgp, es, evp, pi);
287 else
288 ret = bgp_evpn_es_route_uninstall(bgp, es, evp, pi);
289
290 if (ret) {
291 flog_err(
292 EC_BGP_EVPN_FAIL,
293 "%u: Failed to %s EVPN %s route in ESI %s",
294 bgp->vrf_id,
295 install ? "install" : "uninstall",
296 "ES", es->esi_str);
297 return ret;
298 }
299 return 0;
300}
301
302/* Delete (and withdraw) local routes for specified ES from global and ES table.
303 * Also remove all remote routes from the per ES table. Invoked when ES
304 * is deleted.
185fb14a 305 */
c44ab6f1
AK
306static void bgp_evpn_es_route_del_all(struct bgp *bgp, struct bgp_evpn_es *es)
307{
09319b4e 308 struct bgp_dest *dest;
c44ab6f1
AK
309 struct bgp_path_info *pi, *nextpi;
310
311 /* de-activate the ES */
312 bgp_evpn_local_es_down(bgp, es);
313 bgp_evpn_local_type1_evi_route_del(bgp, es);
314
315 /* Walk this ES's routing table and delete all routes. */
09319b4e
DS
316 for (dest = bgp_table_top(es->route_table); dest;
317 dest = bgp_route_next(dest)) {
318 for (pi = bgp_dest_get_bgp_path_info(dest);
319 (pi != NULL) && (nextpi = pi->next, 1); pi = nextpi) {
320 bgp_path_info_delete(dest, pi);
321 bgp_path_info_reap(dest, pi);
c44ab6f1
AK
322 }
323 }
324}
325
326/*****************************************************************************
327 * Base APIs for creating MH routes (Type-1 or Type-4) on local ethernet
328 * segment updates.
329 */
330
331/* create or update local EVPN type1/type4 route entry.
332 *
333 * This could be in -
334 * the ES table if ESR/EAD-ES (or)
335 * the VNI table if EAD-EVI (or)
336 * the global table if ESR/EAD-ES/EAD-EVI
337 *
338 * Note: vpn is applicable only to EAD-EVI routes (NULL for EAD-ES and
339 * ESR).
340 */
d60f63f0
AD
341int bgp_evpn_mh_route_update(struct bgp *bgp, struct bgp_evpn_es *es,
342 struct bgpevpn *vpn, afi_t afi, safi_t safi,
e0a79881 343 struct bgp_dest *dest, struct attr *attr,
d60f63f0 344 struct bgp_path_info **ri, int *route_changed)
185fb14a 345{
185fb14a
AK
346 struct bgp_path_info *tmp_pi = NULL;
347 struct bgp_path_info *local_pi = NULL; /* local route entry if any */
348 struct bgp_path_info *remote_pi = NULL; /* remote route entry if any */
349 struct attr *attr_new = NULL;
c44ab6f1 350 struct prefix_evpn *evp;
185fb14a
AK
351
352 *ri = NULL;
752eed47 353 evp = (struct prefix_evpn *)bgp_dest_get_prefix(dest);
c44ab6f1 354 *route_changed = 1;
185fb14a
AK
355
356 /* locate the local and remote entries if any */
09319b4e 357 for (tmp_pi = bgp_dest_get_bgp_path_info(dest); tmp_pi;
185fb14a
AK
358 tmp_pi = tmp_pi->next) {
359 if (tmp_pi->peer == bgp->peer_self
c44ab6f1
AK
360 && tmp_pi->type == ZEBRA_ROUTE_BGP
361 && tmp_pi->sub_type == BGP_ROUTE_STATIC)
185fb14a
AK
362 local_pi = tmp_pi;
363 if (tmp_pi->type == ZEBRA_ROUTE_BGP
c44ab6f1
AK
364 && tmp_pi->sub_type == BGP_ROUTE_IMPORTED
365 && CHECK_FLAG(tmp_pi->flags, BGP_PATH_VALID))
185fb14a
AK
366 remote_pi = tmp_pi;
367 }
368
fff7545a 369 /* we don't expect to see a remote_pi at this point as
c44ab6f1
AK
370 * an ES route has {esi, vtep_ip} as the key in the ES-rt-table
371 * in the VNI-rt-table.
185fb14a
AK
372 */
373 if (remote_pi) {
374 flog_err(
23d0a753 375 EC_BGP_ES_INVALID,
46281b21 376 "%u ERROR: local es route for ESI: %s vtep %pI4 also learnt from remote",
d60f63f0 377 bgp->vrf_id, es ? es->esi_str : "Null",
46281b21 378 es ? &es->originator_ip : NULL);
185fb14a
AK
379 return -1;
380 }
381
185fb14a
AK
382 /* create or update the entry */
383 if (!local_pi) {
384
385 /* Add or update attribute to hash */
386 attr_new = bgp_attr_intern(attr);
387
388 /* Create new route with its attribute. */
389 tmp_pi = info_make(ZEBRA_ROUTE_BGP, BGP_ROUTE_STATIC, 0,
09319b4e 390 bgp->peer_self, attr_new, dest);
185fb14a
AK
391 SET_FLAG(tmp_pi->flags, BGP_PATH_VALID);
392
c44ab6f1
AK
393 if (evp->prefix.route_type == BGP_EVPN_AD_ROUTE) {
394 bgp_path_info_extra_get(tmp_pi);
395 tmp_pi->extra->num_labels = 1;
396 if (vpn)
397 vni2label(vpn->vni, &tmp_pi->extra->label[0]);
398 else
399 tmp_pi->extra->label[0] = 0;
400 }
401
185fb14a 402 /* add the newly created path to the route-node */
09319b4e 403 bgp_path_info_add(dest, tmp_pi);
185fb14a
AK
404 } else {
405 tmp_pi = local_pi;
406 if (attrhash_cmp(tmp_pi->attr, attr)
c44ab6f1 407 && !CHECK_FLAG(tmp_pi->flags, BGP_PATH_REMOVED))
185fb14a
AK
408 *route_changed = 0;
409 else {
410 /* The attribute has changed.
411 * Add (or update) attribute to hash.
412 */
413 attr_new = bgp_attr_intern(attr);
09319b4e
DS
414 bgp_path_info_set_flag(dest, tmp_pi,
415 BGP_PATH_ATTR_CHANGED);
185fb14a
AK
416
417 /* Restore route, if needed. */
418 if (CHECK_FLAG(tmp_pi->flags, BGP_PATH_REMOVED))
09319b4e 419 bgp_path_info_restore(dest, tmp_pi);
185fb14a
AK
420
421 /* Unintern existing, set to new. */
422 bgp_attr_unintern(&tmp_pi->attr);
423 tmp_pi->attr = attr_new;
083ec940 424 tmp_pi->uptime = monotime(NULL);
185fb14a
AK
425 }
426 }
427
c44ab6f1
AK
428 if (*route_changed) {
429 if (BGP_DEBUG(evpn_mh, EVPN_MH_RT))
23d0a753
DA
430 zlog_debug(
431 "local ES %s vni %u route-type %s nexthop %pI4 updated",
d60f63f0 432 es ? es->esi_str : "Null", vpn ? vpn->vni : 0,
23d0a753
DA
433 evp->prefix.route_type == BGP_EVPN_ES_ROUTE
434 ? "esr"
435 : (vpn ? "ead-evi" : "ead-es"),
436 &attr->mp_nexthop_global_in);
c44ab6f1
AK
437 }
438
185fb14a
AK
439 /* Return back the route entry. */
440 *ri = tmp_pi;
441 return 0;
442}
443
c44ab6f1
AK
444/* Delete local EVPN ESR (type-4) and EAD (type-1) route
445 *
446 * Note: vpn is applicable only to EAD-EVI routes (NULL for EAD-ES and
447 * ESR).
448 */
449static int bgp_evpn_mh_route_delete(struct bgp *bgp, struct bgp_evpn_es *es,
7b0db0e4
AK
450 struct bgpevpn *vpn,
451 struct bgp_evpn_es_frag *es_frag,
452 struct prefix_evpn *p)
185fb14a 453{
185fb14a
AK
454 afi_t afi = AFI_L2VPN;
455 safi_t safi = SAFI_EVPN;
c44ab6f1 456 struct bgp_path_info *pi;
09319b4e
DS
457 struct bgp_dest *dest = NULL; /* dest in esi table */
458 struct bgp_dest *global_dest = NULL; /* dest in global table */
c44ab6f1
AK
459 struct bgp_table *rt_table;
460 struct prefix_rd *prd;
185fb14a 461
c44ab6f1 462 if (vpn) {
852d9f97 463 rt_table = vpn->ip_table;
c44ab6f1
AK
464 prd = &vpn->prd;
465 } else {
466 rt_table = es->route_table;
7b0db0e4 467 prd = &es_frag->prd;
c44ab6f1 468 }
185fb14a 469
c44ab6f1
AK
470 /* First, locate the route node within the ESI or VNI.
471 * If it doesn't exist, ther is nothing to do.
472 * Note: there is no RD here.
473 */
09319b4e
DS
474 dest = bgp_node_lookup(rt_table, (struct prefix *)p);
475 if (!dest)
c44ab6f1 476 return 0;
185fb14a 477
c44ab6f1 478 if (BGP_DEBUG(evpn_mh, EVPN_MH_RT))
23d0a753
DA
479 zlog_debug(
480 "local ES %s vni %u route-type %s nexthop %pI4 delete",
481 es->esi_str, vpn ? vpn->vni : 0,
482 p->prefix.route_type == BGP_EVPN_ES_ROUTE
483 ? "esr"
484 : (vpn ? "ead-evi" : "ead-es"),
485 &es->originator_ip);
185fb14a 486
c44ab6f1
AK
487 /* Next, locate route node in the global EVPN routing table.
488 * Note that this table is a 2-level tree (RD-level + Prefix-level)
489 */
0da34e49
DA
490 global_dest = bgp_evpn_global_node_lookup(bgp->rib[afi][safi], safi, p,
491 prd, NULL);
09319b4e 492 if (global_dest) {
185fb14a 493
c44ab6f1 494 /* Delete route entry in the global EVPN table. */
09319b4e 495 delete_evpn_route_entry(bgp, afi, safi, global_dest, &pi);
185fb14a
AK
496
497 /* Schedule for processing - withdraws to peers happen from
498 * this table.
499 */
500 if (pi)
09319b4e
DS
501 bgp_process(bgp, global_dest, afi, safi);
502 bgp_dest_unlock_node(global_dest);
185fb14a
AK
503 }
504
505 /*
c44ab6f1 506 * Delete route entry in the ESI or VNI routing table.
185fb14a
AK
507 * This can just be removed.
508 */
09319b4e 509 delete_evpn_route_entry(bgp, afi, safi, dest, &pi);
185fb14a 510 if (pi)
09319b4e
DS
511 bgp_path_info_reap(dest, pi);
512 bgp_dest_unlock_node(dest);
185fb14a
AK
513 return 0;
514}
515
d60f63f0
AD
516/*
517 * This function is called when the VNI RD changes.
518 * Delete all EAD/EVI local routes for this VNI from the global routing table.
519 * These routes are scheduled for withdraw from peers.
520 */
521int delete_global_ead_evi_routes(struct bgp *bgp, struct bgpevpn *vpn)
522{
523 afi_t afi;
524 safi_t safi;
525 struct bgp_dest *rdrn, *rn;
526 struct bgp_table *table;
527 struct bgp_path_info *pi;
528
529 afi = AFI_L2VPN;
530 safi = SAFI_EVPN;
531
532 /* Find the RD node for the VNI in the global table */
533 rdrn = bgp_node_lookup(bgp->rib[afi][safi], (struct prefix *)&vpn->prd);
534 if (rdrn && bgp_dest_has_bgp_path_info_data(rdrn)) {
535 table = bgp_dest_get_bgp_table_info(rdrn);
536
537 /*
538 * Iterate over all the routes in this table and delete EAD/EVI
539 * routes
540 */
541 for (rn = bgp_table_top(table); rn; rn = bgp_route_next(rn)) {
542 struct prefix_evpn *evp = (struct prefix_evpn *)&rn->p;
543
544 if (evp->prefix.route_type != BGP_EVPN_AD_ROUTE)
545 continue;
546
547 delete_evpn_route_entry(bgp, afi, safi, rn, &pi);
548 if (pi)
549 bgp_process(bgp, rn, afi, safi);
550 }
551 }
552
553 /* Unlock RD node. */
554 if (rdrn)
555 bgp_dest_unlock_node(rdrn);
556
557 return 0;
558}
559
c44ab6f1
AK
560/*****************************************************************************
561 * Ethernet Segment (Type-4) Routes
74e2bd89
AK
562 * ESRs are used for DF election. Currently service-carving described in
563 * RFC 7432 is NOT supported. Instead preference based DF election is
564 * used by default.
565 * Reference: draft-ietf-bess-evpn-pref-df
185fb14a 566 */
c44ab6f1
AK
567/* Build extended community for EVPN ES (type-4) route */
568static void bgp_evpn_type4_route_extcomm_build(struct bgp_evpn_es *es,
569 struct attr *attr)
185fb14a 570{
c44ab6f1
AK
571 struct ecommunity ecom_encap;
572 struct ecommunity ecom_es_rt;
74e2bd89 573 struct ecommunity ecom_df;
c44ab6f1
AK
574 struct ecommunity_val eval;
575 struct ecommunity_val eval_es_rt;
74e2bd89 576 struct ecommunity_val eval_df;
c44ab6f1
AK
577 bgp_encap_types tnl_type;
578 struct ethaddr mac;
185fb14a 579
c44ab6f1
AK
580 /* Encap */
581 tnl_type = BGP_ENCAP_TYPE_VXLAN;
582 memset(&ecom_encap, 0, sizeof(ecom_encap));
583 encode_encap_extcomm(tnl_type, &eval);
584 ecom_encap.size = 1;
34540b0d 585 ecom_encap.unit_size = ECOMMUNITY_SIZE;
c44ab6f1 586 ecom_encap.val = (uint8_t *)eval.val;
b53e67a3 587 bgp_attr_set_ecommunity(attr, ecommunity_dup(&ecom_encap));
185fb14a 588
c44ab6f1 589 /* ES import RT */
6006b807 590 memset(&mac, 0, sizeof(mac));
c44ab6f1
AK
591 memset(&ecom_es_rt, 0, sizeof(ecom_es_rt));
592 es_get_system_mac(&es->esi, &mac);
593 encode_es_rt_extcomm(&eval_es_rt, &mac);
594 ecom_es_rt.size = 1;
34540b0d 595 ecom_es_rt.unit_size = ECOMMUNITY_SIZE;
c44ab6f1 596 ecom_es_rt.val = (uint8_t *)eval_es_rt.val;
b53e67a3
DA
597 bgp_attr_set_ecommunity(
598 attr,
599 ecommunity_merge(bgp_attr_get_ecommunity(attr), &ecom_es_rt));
185fb14a 600
74e2bd89
AK
601 /* DF election extended community */
602 memset(&ecom_df, 0, sizeof(ecom_df));
603 encode_df_elect_extcomm(&eval_df, es->df_pref);
604 ecom_df.size = 1;
605 ecom_df.val = (uint8_t *)eval_df.val;
b53e67a3
DA
606 bgp_attr_set_ecommunity(
607 attr,
608 ecommunity_merge(bgp_attr_get_ecommunity(attr), &ecom_df));
185fb14a
AK
609}
610
c44ab6f1
AK
611/* Create or update local type-4 route */
612static int bgp_evpn_type4_route_update(struct bgp *bgp,
613 struct bgp_evpn_es *es, struct prefix_evpn *p)
185fb14a
AK
614{
615 int ret = 0;
c44ab6f1
AK
616 int route_changed = 0;
617 afi_t afi = AFI_L2VPN;
618 safi_t safi = SAFI_EVPN;
619 struct attr attr;
620 struct attr *attr_new = NULL;
09319b4e 621 struct bgp_dest *dest = NULL;
185fb14a 622 struct bgp_path_info *pi = NULL;
185fb14a 623
6006b807 624 memset(&attr, 0, sizeof(attr));
c44ab6f1
AK
625
626 /* Build path-attribute for this route. */
0f05ea43 627 bgp_attr_default_set(&attr, bgp, BGP_ORIGIN_IGP);
c44ab6f1
AK
628 attr.nexthop = es->originator_ip;
629 attr.mp_nexthop_global_in = es->originator_ip;
630 attr.mp_nexthop_len = BGP_ATTR_NHLEN_IPV4;
631
632 /* Set up extended community. */
633 bgp_evpn_type4_route_extcomm_build(es, &attr);
634
635 /* First, create (or fetch) route node within the ESI. */
636 /* NOTE: There is no RD here. */
09319b4e 637 dest = bgp_node_get(es->route_table, (struct prefix *)p);
185fb14a 638
c44ab6f1 639 /* Create or update route entry. */
e0a79881 640 ret = bgp_evpn_mh_route_update(bgp, es, NULL, afi, safi, dest, &attr,
09319b4e 641 &pi, &route_changed);
ec779825 642 if (ret != 0)
23d0a753
DA
643 flog_err(
644 EC_BGP_ES_INVALID,
645 "%u ERROR: Failed to updated ES route ESI: %s VTEP %pI4",
646 bgp->vrf_id, es->esi_str, &es->originator_ip);
185fb14a 647
c44ab6f1
AK
648 assert(pi);
649 attr_new = pi->attr;
185fb14a 650
c44ab6f1
AK
651 /* Perform route selection;
652 * this is just to set the flags correctly
653 * as local route in the ES always wins.
654 */
09319b4e
DS
655 bgp_evpn_es_route_select_install(bgp, es, dest);
656 bgp_dest_unlock_node(dest);
185fb14a 657
c44ab6f1
AK
658 /* If this is a new route or some attribute has changed, export the
659 * route to the global table. The route will be advertised to peers
660 * from there. Note that this table is a 2-level tree (RD-level +
661 * Prefix-level) similar to L3VPN routes.
662 */
663 if (route_changed) {
664 struct bgp_path_info *global_pi;
185fb14a 665
34c7f35f
SW
666 dest = bgp_evpn_global_node_get(bgp->rib[afi][safi], afi, safi,
667 p, &es->es_base_frag->prd,
668 NULL);
09319b4e 669 bgp_evpn_mh_route_update(bgp, es, NULL, afi, safi, dest,
e0a79881 670 attr_new, &global_pi, &route_changed);
185fb14a 671
c44ab6f1 672 /* Schedule for processing and unlock node. */
09319b4e
DS
673 bgp_process(bgp, dest, afi, safi);
674 bgp_dest_unlock_node(dest);
185fb14a
AK
675 }
676
c44ab6f1
AK
677 /* Unintern temporary. */
678 aspath_unintern(&attr.aspath);
679 return 0;
185fb14a
AK
680}
681
c44ab6f1
AK
682/* Delete local type-4 route */
683static int bgp_evpn_type4_route_delete(struct bgp *bgp,
684 struct bgp_evpn_es *es, struct prefix_evpn *p)
185fb14a 685{
7b0db0e4
AK
686 if (!es->es_base_frag)
687 return -1;
688
689 return bgp_evpn_mh_route_delete(bgp, es, NULL /* l2vni */,
690 es->es_base_frag, p);
c44ab6f1 691}
185fb14a 692
c44ab6f1
AK
693/* Process remote/received EVPN type-4 route (advertise or withdraw) */
694int bgp_evpn_type4_route_process(struct peer *peer, afi_t afi, safi_t safi,
695 struct attr *attr, uint8_t *pfx, int psize,
696 uint32_t addpath_id)
697{
c44ab6f1
AK
698 esi_t esi;
699 uint8_t ipaddr_len;
700 struct in_addr vtep_ip;
701 struct prefix_rd prd;
702 struct prefix_evpn p;
185fb14a 703
c44ab6f1
AK
704 /* Type-4 route should be either 23 or 35 bytes
705 * RD (8), ESI (10), ip-len (1), ip (4 or 16)
185fb14a 706 */
c44ab6f1
AK
707 if (psize != BGP_EVPN_TYPE4_V4_PSIZE &&
708 psize != BGP_EVPN_TYPE4_V6_PSIZE) {
709 flog_err(EC_BGP_EVPN_ROUTE_INVALID,
710 "%u:%s - Rx EVPN Type-4 NLRI with invalid length %d",
711 peer->bgp->vrf_id, peer->host, psize);
712 return -1;
713 }
185fb14a 714
c44ab6f1
AK
715 /* Make prefix_rd */
716 prd.family = AF_UNSPEC;
717 prd.prefixlen = 64;
718 memcpy(&prd.val, pfx, RD_BYTES);
719 pfx += RD_BYTES;
185fb14a 720
c44ab6f1
AK
721 /* get the ESI */
722 memcpy(&esi, pfx, ESI_BYTES);
723 pfx += ESI_BYTES;
185fb14a 724
185fb14a 725
c44ab6f1
AK
726 /* Get the IP. */
727 ipaddr_len = *pfx++;
728 if (ipaddr_len == IPV4_MAX_BITLEN) {
729 memcpy(&vtep_ip, pfx, IPV4_MAX_BYTELEN);
730 } else {
731 flog_err(
732 EC_BGP_EVPN_ROUTE_INVALID,
733 "%u:%s - Rx EVPN Type-4 NLRI with unsupported IP address length %d",
734 peer->bgp->vrf_id, peer->host, ipaddr_len);
735 return -1;
736 }
185fb14a 737
c44ab6f1
AK
738 build_evpn_type4_prefix(&p, &esi, vtep_ip);
739 /* Process the route. */
740 if (attr) {
367b458c
DS
741 bgp_update(peer, (struct prefix *)&p, addpath_id, attr, afi,
742 safi, ZEBRA_ROUTE_BGP, BGP_ROUTE_NORMAL, &prd, NULL,
743 0, 0, NULL);
c44ab6f1 744 } else {
bf0c6163
DA
745 bgp_withdraw(peer, (struct prefix *)&p, addpath_id, afi, safi,
746 ZEBRA_ROUTE_BGP, BGP_ROUTE_NORMAL, &prd, NULL, 0,
747 NULL);
c44ab6f1 748 }
367b458c 749 return 0;
185fb14a
AK
750}
751
c44ab6f1
AK
752/* Check if a prefix belongs to the local ES */
753static bool bgp_evpn_type4_prefix_match(struct prefix_evpn *p,
754 struct bgp_evpn_es *es)
185fb14a 755{
c44ab6f1
AK
756 return (p->prefix.route_type == BGP_EVPN_ES_ROUTE) &&
757 !memcmp(&p->prefix.es_addr.esi, &es->esi, sizeof(esi_t));
185fb14a
AK
758}
759
c44ab6f1
AK
760/* Import remote ESRs on local ethernet segment add */
761static int bgp_evpn_type4_remote_routes_import(struct bgp *bgp,
762 struct bgp_evpn_es *es, bool install)
185fb14a
AK
763{
764 int ret;
765 afi_t afi;
766 safi_t safi;
09319b4e 767 struct bgp_dest *rd_dest, *dest;
185fb14a
AK
768 struct bgp_table *table;
769 struct bgp_path_info *pi;
770
771 afi = AFI_L2VPN;
772 safi = SAFI_EVPN;
773
c44ab6f1
AK
774 /* Walk entire global routing table and evaluate routes which could be
775 * imported into this Ethernet Segment.
185fb14a 776 */
09319b4e
DS
777 for (rd_dest = bgp_table_top(bgp->rib[afi][safi]); rd_dest;
778 rd_dest = bgp_route_next(rd_dest)) {
779 table = bgp_dest_get_bgp_table_info(rd_dest);
185fb14a
AK
780 if (!table)
781 continue;
782
09319b4e
DS
783 for (dest = bgp_table_top(table); dest;
784 dest = bgp_route_next(dest)) {
785 struct prefix_evpn *evp =
752eed47 786 (struct prefix_evpn *)bgp_dest_get_prefix(dest);
185fb14a 787
09319b4e
DS
788 for (pi = bgp_dest_get_bgp_path_info(dest); pi;
789 pi = pi->next) {
185fb14a
AK
790 /*
791 * Consider "valid" remote routes applicable for
792 * this ES.
793 */
794 if (!(CHECK_FLAG(pi->flags, BGP_PATH_VALID)
c44ab6f1
AK
795 && pi->type == ZEBRA_ROUTE_BGP
796 && pi->sub_type == BGP_ROUTE_NORMAL))
185fb14a
AK
797 continue;
798
c44ab6f1 799 if (!bgp_evpn_type4_prefix_match(evp, es))
185fb14a
AK
800 continue;
801
802 if (install)
c44ab6f1
AK
803 ret = bgp_evpn_es_route_install(
804 bgp, es, evp, pi);
185fb14a 805 else
c44ab6f1
AK
806 ret = bgp_evpn_es_route_uninstall(
807 bgp, es, evp, pi);
185fb14a
AK
808
809 if (ret) {
810 flog_err(
2dbe669b
DA
811 EC_BGP_EVPN_FAIL,
812 "Failed to %s EVPN %pFX route in ESI %s",
813 install ? "install"
185fb14a 814 : "uninstall",
2dbe669b 815 evp, es->esi_str);
dc52bece
DS
816
817 bgp_dest_unlock_node(rd_dest);
818 bgp_dest_unlock_node(dest);
185fb14a
AK
819 return ret;
820 }
821 }
822 }
823 }
824 return 0;
825}
826
c44ab6f1
AK
827/*****************************************************************************
828 * Ethernet Auto Discovery (EAD/Type-1) route handling
829 * There are two types of EAD routes -
830 * 1. EAD-per-ES - Key: {ESI, ET=0xffffffff}
831 * 2. EAD-per-EVI - Key: {ESI, ET=0}
185fb14a 832 */
185fb14a 833
c44ab6f1 834/* Extended communities associated with EAD-per-ES */
7b0db0e4
AK
835static void
836bgp_evpn_type1_es_route_extcomm_build(struct bgp_evpn_es_frag *es_frag,
837 struct attr *attr)
185fb14a 838{
c44ab6f1
AK
839 struct ecommunity ecom_encap;
840 struct ecommunity ecom_esi_label;
841 struct ecommunity_val eval;
842 struct ecommunity_val eval_esi_label;
843 bgp_encap_types tnl_type;
844 struct listnode *evi_node, *rt_node;
845 struct ecommunity *ecom;
846 struct bgp_evpn_es_evi *es_evi;
185fb14a 847
c44ab6f1
AK
848 /* Encap */
849 tnl_type = BGP_ENCAP_TYPE_VXLAN;
850 memset(&ecom_encap, 0, sizeof(ecom_encap));
851 encode_encap_extcomm(tnl_type, &eval);
852 ecom_encap.size = 1;
34540b0d 853 ecom_encap.unit_size = ECOMMUNITY_SIZE;
c44ab6f1 854 ecom_encap.val = (uint8_t *)eval.val;
b53e67a3 855 bgp_attr_set_ecommunity(attr, ecommunity_dup(&ecom_encap));
185fb14a 856
c44ab6f1
AK
857 /* ESI label */
858 encode_esi_label_extcomm(&eval_esi_label,
859 false /*single_active*/);
860 ecom_esi_label.size = 1;
34540b0d 861 ecom_esi_label.unit_size = ECOMMUNITY_SIZE;
c44ab6f1 862 ecom_esi_label.val = (uint8_t *)eval_esi_label.val;
b53e67a3
DA
863 bgp_attr_set_ecommunity(attr,
864 ecommunity_merge(bgp_attr_get_ecommunity(attr),
865 &ecom_esi_label));
c44ab6f1
AK
866
867 /* Add export RTs for all L2-VNIs associated with this ES */
868 /* XXX - suppress EAD-ES advertisment if there are no EVIs associated
869 * with it.
870 */
f4a5218d
AK
871 if (listcount(bgp_mh_info->ead_es_export_rtl)) {
872 for (ALL_LIST_ELEMENTS_RO(bgp_mh_info->ead_es_export_rtl,
873 rt_node, ecom))
b53e67a3 874 bgp_attr_set_ecommunity(
f4a5218d
AK
875 attr, ecommunity_merge(attr->ecommunity, ecom));
876 } else {
7b0db0e4
AK
877 for (ALL_LIST_ELEMENTS_RO(es_frag->es_evi_frag_list, evi_node,
878 es_evi)) {
f4a5218d
AK
879 if (!CHECK_FLAG(es_evi->flags, BGP_EVPNES_EVI_LOCAL))
880 continue;
881 for (ALL_LIST_ELEMENTS_RO(es_evi->vpn->export_rtl,
882 rt_node, ecom))
883 bgp_attr_set_ecommunity(
884 attr, ecommunity_merge(attr->ecommunity,
885 ecom));
886 }
c44ab6f1 887 }
c44ab6f1
AK
888}
889
890/* Extended communities associated with EAD-per-EVI */
891static void bgp_evpn_type1_evi_route_extcomm_build(struct bgp_evpn_es *es,
892 struct bgpevpn *vpn, struct attr *attr)
893{
894 struct ecommunity ecom_encap;
895 struct ecommunity_val eval;
896 bgp_encap_types tnl_type;
897 struct listnode *rt_node;
898 struct ecommunity *ecom;
899
900 /* Encap */
901 tnl_type = BGP_ENCAP_TYPE_VXLAN;
902 memset(&ecom_encap, 0, sizeof(ecom_encap));
903 encode_encap_extcomm(tnl_type, &eval);
904 ecom_encap.size = 1;
7659ad68 905 ecom_encap.unit_size = ECOMMUNITY_SIZE;
c44ab6f1 906 ecom_encap.val = (uint8_t *)eval.val;
b53e67a3 907 bgp_attr_set_ecommunity(attr, ecommunity_dup(&ecom_encap));
c44ab6f1
AK
908
909 /* Add export RTs for the L2-VNI */
910 for (ALL_LIST_ELEMENTS_RO(vpn->export_rtl, rt_node, ecom))
b53e67a3
DA
911 bgp_attr_set_ecommunity(
912 attr,
913 ecommunity_merge(bgp_attr_get_ecommunity(attr), ecom));
c44ab6f1
AK
914}
915
916/* Update EVPN EAD (type-1) route -
917 * vpn - valid for EAD-EVI routes and NULL for EAD-ES routes
918 */
7b0db0e4
AK
919static int bgp_evpn_type1_route_update(struct bgp *bgp, struct bgp_evpn_es *es,
920 struct bgpevpn *vpn,
921 struct bgp_evpn_es_frag *es_frag,
922 struct prefix_evpn *p)
c44ab6f1
AK
923{
924 int ret = 0;
925 afi_t afi = AFI_L2VPN;
926 safi_t safi = SAFI_EVPN;
927 struct attr attr;
928 struct attr *attr_new = NULL;
09319b4e 929 struct bgp_dest *dest = NULL;
c44ab6f1
AK
930 struct bgp_path_info *pi = NULL;
931 int route_changed = 0;
932 struct prefix_rd *global_rd;
933
6006b807 934 memset(&attr, 0, sizeof(attr));
c44ab6f1
AK
935
936 /* Build path-attribute for this route. */
0f05ea43 937 bgp_attr_default_set(&attr, bgp, BGP_ORIGIN_IGP);
c44ab6f1
AK
938 attr.nexthop = es->originator_ip;
939 attr.mp_nexthop_global_in = es->originator_ip;
940 attr.mp_nexthop_len = BGP_ATTR_NHLEN_IPV4;
941
942 if (vpn) {
943 /* EAD-EVI route update */
944 /* MPLS label */
945 vni2label(vpn->vni, &(attr.label));
946
947 /* Set up extended community */
948 bgp_evpn_type1_evi_route_extcomm_build(es, vpn, &attr);
949
950 /* First, create (or fetch) route node within the VNI. */
852d9f97 951 dest = bgp_node_get(vpn->ip_table, (struct prefix *)p);
c44ab6f1
AK
952
953 /* Create or update route entry. */
09319b4e 954 ret = bgp_evpn_mh_route_update(bgp, es, vpn, afi, safi, dest,
e0a79881 955 &attr, &pi, &route_changed);
ec779825 956 if (ret != 0)
23d0a753
DA
957 flog_err(
958 EC_BGP_ES_INVALID,
959 "%u Failed to update EAD-EVI route ESI: %s VNI %u VTEP %pI4",
960 bgp->vrf_id, es->esi_str, vpn->vni,
961 &es->originator_ip);
c44ab6f1
AK
962 global_rd = &vpn->prd;
963 } else {
964 /* EAD-ES route update */
965 /* MPLS label is 0 for EAD-ES route */
966
967 /* Set up extended community */
7b0db0e4 968 bgp_evpn_type1_es_route_extcomm_build(es_frag, &attr);
c44ab6f1
AK
969
970 /* First, create (or fetch) route node within the ES. */
971 /* NOTE: There is no RD here. */
972 /* XXX: fragment ID must be included as a part of the prefix. */
09319b4e 973 dest = bgp_node_get(es->route_table, (struct prefix *)p);
c44ab6f1
AK
974
975 /* Create or update route entry. */
09319b4e 976 ret = bgp_evpn_mh_route_update(bgp, es, vpn, afi, safi, dest,
e0a79881 977 &attr, &pi, &route_changed);
c44ab6f1 978 if (ret != 0) {
23d0a753
DA
979 flog_err(
980 EC_BGP_ES_INVALID,
b0b9a2fe 981 "%u ERROR: Failed to updated EAD-ES route ESI: %s VTEP %pI4",
23d0a753 982 bgp->vrf_id, es->esi_str, &es->originator_ip);
c44ab6f1 983 }
7b0db0e4 984 global_rd = &es_frag->prd;
c44ab6f1
AK
985 }
986
987
988 assert(pi);
989 attr_new = pi->attr;
990
991 /* Perform route selection;
992 * this is just to set the flags correctly as local route in
993 * the ES always wins.
994 */
09319b4e
DS
995 evpn_route_select_install(bgp, vpn, dest);
996 bgp_dest_unlock_node(dest);
c44ab6f1
AK
997
998 /* If this is a new route or some attribute has changed, export the
999 * route to the global table. The route will be advertised to peers
1000 * from there. Note that this table is a 2-level tree (RD-level +
1001 * Prefix-level) similar to L3VPN routes.
1002 */
1003 if (route_changed) {
1004 struct bgp_path_info *global_pi;
1005
34c7f35f
SW
1006 dest = bgp_evpn_global_node_get(bgp->rib[afi][safi], afi, safi,
1007 p, global_rd, NULL);
09319b4e 1008 bgp_evpn_mh_route_update(bgp, es, vpn, afi, safi, dest,
e0a79881 1009 attr_new, &global_pi, &route_changed);
c44ab6f1
AK
1010
1011 /* Schedule for processing and unlock node. */
09319b4e
DS
1012 bgp_process(bgp, dest, afi, safi);
1013 bgp_dest_unlock_node(dest);
c44ab6f1
AK
1014 }
1015
1016 /* Unintern temporary. */
1017 aspath_unintern(&attr.aspath);
1018 return 0;
1019}
1020
9c49ac74
AD
1021/*
1022 * This function is called when the export RT for a VNI changes.
1023 * Update all type-1 local routes for this VNI from VNI/ES tables and the global
1024 * table and advertise these routes to peers.
1025 */
1026
7b0db0e4
AK
1027static void bgp_evpn_ead_es_route_update(struct bgp *bgp,
1028 struct bgp_evpn_es *es)
1029{
1030 struct listnode *node;
1031 struct bgp_evpn_es_frag *es_frag;
1032 struct prefix_evpn p;
1033
1034 build_evpn_type1_prefix(&p, BGP_EVPN_AD_ES_ETH_TAG, &es->esi,
1035 es->originator_ip);
1036 for (ALL_LIST_ELEMENTS_RO(es->es_frag_list, node, es_frag)) {
1037 if (!listcount(es_frag->es_evi_frag_list))
1038 continue;
1039
1040 p.prefix.ead_addr.frag_id = es_frag->rd_id;
1041 if (bgp_evpn_type1_route_update(bgp, es, NULL, es_frag, &p))
1042 flog_err(
1043 EC_BGP_EVPN_ROUTE_CREATE,
1044 "EAD-ES route creation failure for ESI %s frag %u",
1045 es->esi_str, es_frag->rd_id);
1046 }
1047}
1048
1049static void bgp_evpn_ead_evi_route_update(struct bgp *bgp,
1050 struct bgp_evpn_es *es,
1051 struct bgpevpn *vpn,
1052 struct prefix_evpn *p)
1053{
1054 if (bgp_evpn_type1_route_update(bgp, es, vpn, NULL, p))
1055 flog_err(EC_BGP_EVPN_ROUTE_CREATE,
1056 "EAD-EVI route creation failure for ESI %s VNI %u",
1057 es->esi_str, vpn->vni);
1058}
1059
9c49ac74
AD
1060void update_type1_routes_for_evi(struct bgp *bgp, struct bgpevpn *vpn)
1061{
1062 struct prefix_evpn p;
1063 struct bgp_evpn_es *es;
1064 struct bgp_evpn_es_evi *es_evi;
9c49ac74 1065
7b0db0e4
AK
1066
1067 RB_FOREACH (es_evi, bgp_es_evi_rb_head, &vpn->es_evi_rb_tree) {
9c49ac74
AD
1068 es = es_evi->es;
1069
7b0db0e4
AK
1070 if (es_evi->vpn != vpn)
1071 continue;
1072
9c49ac74 1073 /* Update EAD-ES */
7b0db0e4 1074 bgp_evpn_ead_es_route_update(bgp, es);
9c49ac74
AD
1075
1076 /* Update EAD-EVI */
1077 if (CHECK_FLAG(es->flags, BGP_EVPNES_ADV_EVI)) {
1078 build_evpn_type1_prefix(&p, BGP_EVPN_AD_EVI_ETH_TAG,
1079 &es->esi, es->originator_ip);
7b0db0e4 1080 bgp_evpn_ead_evi_route_update(bgp, es, vpn, &p);
9c49ac74
AD
1081 }
1082 }
1083}
1084
c44ab6f1 1085/* Delete local Type-1 route */
7b0db0e4
AK
1086static void bgp_evpn_ead_es_route_delete(struct bgp *bgp,
1087 struct bgp_evpn_es *es)
c44ab6f1 1088{
7b0db0e4
AK
1089 struct listnode *node;
1090 struct bgp_evpn_es_frag *es_frag;
1091 struct prefix_evpn p;
1092
1093 build_evpn_type1_prefix(&p, BGP_EVPN_AD_ES_ETH_TAG, &es->esi,
1094 es->originator_ip);
1095 for (ALL_LIST_ELEMENTS_RO(es->es_frag_list, node, es_frag)) {
1096 p.prefix.ead_addr.frag_id = es_frag->rd_id;
1097 bgp_evpn_mh_route_delete(bgp, es, NULL, es_frag, &p);
1098 }
c44ab6f1
AK
1099}
1100
7b0db0e4
AK
1101static int bgp_evpn_ead_evi_route_delete(struct bgp *bgp,
1102 struct bgp_evpn_es *es,
1103 struct bgpevpn *vpn,
1104 struct prefix_evpn *p)
c44ab6f1 1105{
7b0db0e4 1106 return bgp_evpn_mh_route_delete(bgp, es, vpn, NULL, p);
c44ab6f1
AK
1107}
1108
1109/* Generate EAD-EVI for all VNIs */
1110static void bgp_evpn_local_type1_evi_route_add(struct bgp *bgp,
1111 struct bgp_evpn_es *es)
1112{
1113 struct listnode *evi_node;
1114 struct prefix_evpn p;
1115 struct bgp_evpn_es_evi *es_evi;
1116
fe8293c3
AK
1117 /* EAD-per-EVI routes have been suppressed */
1118 if (!bgp_mh_info->ead_evi_tx)
1119 return;
1120
c44ab6f1
AK
1121 if (CHECK_FLAG(es->flags, BGP_EVPNES_ADV_EVI))
1122 /* EAD-EVI route add for this ES is already done */
1123 return;
1124
1125 SET_FLAG(es->flags, BGP_EVPNES_ADV_EVI);
1126 build_evpn_type1_prefix(&p, BGP_EVPN_AD_EVI_ETH_TAG,
1127 &es->esi, es->originator_ip);
1128
1129 for (ALL_LIST_ELEMENTS_RO(es->es_evi_list, evi_node, es_evi)) {
1130 if (!CHECK_FLAG(es_evi->flags, BGP_EVPNES_EVI_LOCAL))
1131 continue;
7b0db0e4 1132 bgp_evpn_ead_evi_route_update(bgp, es, es_evi->vpn, &p);
c44ab6f1
AK
1133 }
1134}
1135
1136/*
1137 * Withdraw EAD-EVI for all VNIs
1138 */
1139static void bgp_evpn_local_type1_evi_route_del(struct bgp *bgp,
1140 struct bgp_evpn_es *es)
1141{
1142 struct listnode *evi_node;
1143 struct prefix_evpn p;
1144 struct bgp_evpn_es_evi *es_evi;
1145
1146 /* Delete and withdraw locally learnt EAD-EVI route */
1147 if (!CHECK_FLAG(es->flags, BGP_EVPNES_ADV_EVI))
1148 /* EAD-EVI route has not been advertised for this ES */
1149 return;
1150
1151 UNSET_FLAG(es->flags, BGP_EVPNES_ADV_EVI);
1152 build_evpn_type1_prefix(&p, BGP_EVPN_AD_EVI_ETH_TAG,
1153 &es->esi, es->originator_ip);
1154 for (ALL_LIST_ELEMENTS_RO(es->es_evi_list, evi_node, es_evi)) {
1155 if (!CHECK_FLAG(es_evi->flags, BGP_EVPNES_EVI_LOCAL))
1156 continue;
7b0db0e4 1157 if (bgp_evpn_mh_route_delete(bgp, es, es_evi->vpn, NULL, &p))
c44ab6f1
AK
1158 flog_err(EC_BGP_EVPN_ROUTE_CREATE,
1159 "%u: Type4 route creation failure for ESI %s",
1160 bgp->vrf_id, es->esi_str);
1161 }
1162}
185fb14a
AK
1163
1164/*
c44ab6f1 1165 * Process received EVPN type-1 route (advertise or withdraw).
185fb14a 1166 */
c44ab6f1
AK
1167int bgp_evpn_type1_route_process(struct peer *peer, afi_t afi, safi_t safi,
1168 struct attr *attr, uint8_t *pfx, int psize,
1169 uint32_t addpath_id)
185fb14a 1170{
c44ab6f1 1171 struct prefix_rd prd;
185fb14a 1172 esi_t esi;
c44ab6f1
AK
1173 uint32_t eth_tag;
1174 mpls_label_t label;
185fb14a 1175 struct in_addr vtep_ip;
185fb14a
AK
1176 struct prefix_evpn p;
1177
c44ab6f1 1178 if (psize != BGP_EVPN_TYPE1_PSIZE) {
185fb14a 1179 flog_err(EC_BGP_EVPN_ROUTE_INVALID,
c44ab6f1
AK
1180 "%u:%s - Rx EVPN Type-1 NLRI with invalid length %d",
1181 peer->bgp->vrf_id, peer->host, psize);
185fb14a
AK
1182 return -1;
1183 }
1184
1185 /* Make prefix_rd */
1186 prd.family = AF_UNSPEC;
1187 prd.prefixlen = 64;
c44ab6f1
AK
1188 memcpy(&prd.val, pfx, RD_BYTES);
1189 pfx += RD_BYTES;
185fb14a
AK
1190
1191 /* get the ESI */
1192 memcpy(&esi, pfx, ESI_BYTES);
1193 pfx += ESI_BYTES;
1194
c44ab6f1
AK
1195 /* Copy Ethernet Tag */
1196 memcpy(&eth_tag, pfx, EVPN_ETH_TAG_BYTES);
1197 eth_tag = ntohl(eth_tag);
1198 pfx += EVPN_ETH_TAG_BYTES;
185fb14a 1199
c44ab6f1 1200 memcpy(&label, pfx, BGP_LABEL_BYTES);
185fb14a 1201
c44ab6f1
AK
1202 /* EAD route prefix doesn't include the nexthop in the global
1203 * table
1204 */
3a6290bd 1205 vtep_ip.s_addr = INADDR_ANY;
c44ab6f1 1206 build_evpn_type1_prefix(&p, eth_tag, &esi, vtep_ip);
185fb14a
AK
1207 /* Process the route. */
1208 if (attr) {
367b458c
DS
1209 bgp_update(peer, (struct prefix *)&p, addpath_id, attr, afi,
1210 safi, ZEBRA_ROUTE_BGP, BGP_ROUTE_NORMAL, &prd, NULL,
1211 0, 0, NULL);
185fb14a 1212 } else {
bf0c6163
DA
1213 bgp_withdraw(peer, (struct prefix *)&p, addpath_id, afi, safi,
1214 ZEBRA_ROUTE_BGP, BGP_ROUTE_NORMAL, &prd, NULL, 0,
1215 NULL);
185fb14a 1216 }
367b458c 1217 return 0;
185fb14a
AK
1218}
1219
f4a5218d
AK
1220void bgp_evpn_mh_config_ead_export_rt(struct bgp *bgp,
1221 struct ecommunity *ecomcfg, bool del)
1222{
1223 struct listnode *node, *nnode, *node_to_del;
1224 struct ecommunity *ecom;
f4a5218d
AK
1225 struct bgp_evpn_es *es;
1226
1227 if (del) {
1228 if (ecomcfg == NULL) {
1229 /* Reset to default and process all routes. */
1230 for (ALL_LIST_ELEMENTS(bgp_mh_info->ead_es_export_rtl,
1231 node, nnode, ecom)) {
1232 ecommunity_free(&ecom);
1233 list_delete_node(bgp_mh_info->ead_es_export_rtl,
1234 node);
1235 }
1236 }
1237
1238 /* Delete a specific export RT */
1239 else {
1240 node_to_del = NULL;
1241
1242 for (ALL_LIST_ELEMENTS(bgp_mh_info->ead_es_export_rtl,
1243 node, nnode, ecom)) {
1244 if (ecommunity_match(ecom, ecomcfg)) {
1245 ecommunity_free(&ecom);
1246 node_to_del = node;
1247 break;
1248 }
1249 }
1250
e2d3f916 1251 assert(node_to_del);
1252 list_delete_node(bgp_mh_info->ead_es_export_rtl,
1253 node_to_del);
f4a5218d
AK
1254 }
1255 } else {
1256 listnode_add_sort(bgp_mh_info->ead_es_export_rtl, ecomcfg);
1257 }
1258
1259 if (BGP_DEBUG(evpn_mh, EVPN_MH_RT))
1260 zlog_debug("local ES del/re-add EAD route on export RT change");
1261 /*
1262 * walk through all active ESs withdraw the old EAD and
1263 * generate a new one
1264 */
1265 RB_FOREACH (es, bgp_es_rb_head, &bgp_mh_info->es_rb_tree) {
1266 if (!bgp_evpn_is_es_local(es) ||
1267 !bgp_evpn_local_es_is_active(es))
1268 continue;
1269
f4a5218d
AK
1270 if (BGP_DEBUG(evpn_mh, EVPN_MH_RT))
1271 zlog_debug(
1272 "local ES %s del/re-add EAD route on export RT change",
1273 es->esi_str);
1274
1275 /*
1276 * withdraw EAD-ES. XXX - this should technically not be
1277 * needed; can be removed after testing
1278 */
7b0db0e4 1279 bgp_evpn_ead_es_route_delete(bgp, es);
f4a5218d
AK
1280
1281 /* generate EAD-ES */
7b0db0e4 1282 bgp_evpn_ead_es_route_update(bgp, es);
f4a5218d
AK
1283 }
1284}
1285
c44ab6f1
AK
1286/*****************************************************************************/
1287/* Ethernet Segment Management
1288 * 1. Ethernet Segment is a collection of links attached to the same
1289 * server (MHD) or switch (MHN)
1290 * 2. An Ethernet Segment can span multiple PEs and is identified by the
1291 * 10-byte ES-ID.
1292 * 3. Local ESs are configured in zebra and sent to BGP
1293 * 4. Remote ESs are created by BGP when one or more ES-EVIs reference it i.e.
1294 * created on first reference and release on last de-reference
1295 * 5. An ES can be both local and remote. Infact most local ESs are expected
1296 * to have an ES peer.
1297 */
1298
1299/* A list of remote VTEPs is maintained for each ES. This list includes -
1300 * 1. VTEPs for which we have imported the ESR i.e. ES-peers
1301 * 2. VTEPs that have an "active" ES-EVI VTEP i.e. EAD-per-ES and EAD-per-EVI
1302 * have been imported into one or more VNIs
185fb14a 1303 */
c44ab6f1
AK
1304static int bgp_evpn_es_vtep_cmp(void *p1, void *p2)
1305{
1306 const struct bgp_evpn_es_vtep *es_vtep1 = p1;
1307 const struct bgp_evpn_es_vtep *es_vtep2 = p2;
1308
1309 return es_vtep1->vtep_ip.s_addr - es_vtep2->vtep_ip.s_addr;
1310}
1311
1312static struct bgp_evpn_es_vtep *bgp_evpn_es_vtep_new(struct bgp_evpn_es *es,
1313 struct in_addr vtep_ip)
1314{
1315 struct bgp_evpn_es_vtep *es_vtep;
1316
1317 es_vtep = XCALLOC(MTYPE_BGP_EVPN_ES_VTEP, sizeof(*es_vtep));
1318
1319 es_vtep->es = es;
1320 es_vtep->vtep_ip.s_addr = vtep_ip.s_addr;
23aa35ad
AK
1321 inet_ntop(AF_INET, &es_vtep->vtep_ip, es_vtep->vtep_str,
1322 sizeof(es_vtep->vtep_str));
c44ab6f1
AK
1323 listnode_init(&es_vtep->es_listnode, es_vtep);
1324 listnode_add_sort(es->es_vtep_list, &es_vtep->es_listnode);
1325
1326 return es_vtep;
1327}
1328
1329static void bgp_evpn_es_vtep_free(struct bgp_evpn_es_vtep *es_vtep)
1330{
1331 struct bgp_evpn_es *es = es_vtep->es;
1332
1333 if (CHECK_FLAG(es_vtep->flags, BGP_EVPNES_VTEP_ESR) ||
1334 es_vtep->evi_cnt)
1335 /* as long as there is some reference we can't free it */
1336 return;
1337
1338 list_delete_node(es->es_vtep_list, &es_vtep->es_listnode);
1339 XFREE(MTYPE_BGP_EVPN_ES_VTEP, es_vtep);
1340}
1341
1342/* check if VTEP is already part of the list */
1343static struct bgp_evpn_es_vtep *bgp_evpn_es_vtep_find(struct bgp_evpn_es *es,
1344 struct in_addr vtep_ip)
1345{
1346 struct listnode *node = NULL;
1347 struct bgp_evpn_es_vtep *es_vtep;
1348
1349 for (ALL_LIST_ELEMENTS_RO(es->es_vtep_list, node, es_vtep)) {
1350 if (es_vtep->vtep_ip.s_addr == vtep_ip.s_addr)
1351 return es_vtep;
1352 }
1353 return NULL;
1354}
1355
1356/* Send the remote ES to zebra for NHG programming */
1357static int bgp_zebra_send_remote_es_vtep(struct bgp *bgp,
1358 struct bgp_evpn_es_vtep *es_vtep, bool add)
1359{
1360 struct bgp_evpn_es *es = es_vtep->es;
1361 struct stream *s;
74e2bd89 1362 uint32_t flags = 0;
c44ab6f1
AK
1363
1364 /* Check socket. */
1365 if (!zclient || zclient->sock < 0)
1366 return 0;
1367
1368 /* Don't try to register if Zebra doesn't know of this instance. */
1369 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp)) {
1370 if (BGP_DEBUG(zebra, ZEBRA))
1371 zlog_debug("No zebra instance, not installing remote es %s",
1372 es->esi_str);
1373 return 0;
1374 }
1375
74e2bd89
AK
1376 if (es_vtep->flags & BGP_EVPNES_VTEP_ESR)
1377 flags |= ZAPI_ES_VTEP_FLAG_ESR_RXED;
1378
c44ab6f1
AK
1379 s = zclient->obuf;
1380 stream_reset(s);
1381
1382 zclient_create_header(s,
1383 add ? ZEBRA_REMOTE_ES_VTEP_ADD : ZEBRA_REMOTE_ES_VTEP_DEL,
1384 bgp->vrf_id);
1385 stream_put(s, &es->esi, sizeof(esi_t));
1386 stream_put_ipv4(s, es_vtep->vtep_ip.s_addr);
74e2bd89
AK
1387 if (add) {
1388 stream_putl(s, flags);
1389 stream_putc(s, es_vtep->df_alg);
1390 stream_putw(s, es_vtep->df_pref);
1391 }
c44ab6f1
AK
1392
1393 stream_putw_at(s, 0, stream_get_endp(s));
1394
1395 if (BGP_DEBUG(evpn_mh, EVPN_MH_ES))
23d0a753
DA
1396 zlog_debug("Tx %s Remote ESI %s VTEP %pI4", add ? "ADD" : "DEL",
1397 es->esi_str, &es_vtep->vtep_ip);
c44ab6f1 1398
23aa35ad
AK
1399 frrtrace(3, frr_bgp, evpn_mh_vtep_zsend, add, es, es_vtep);
1400
c44ab6f1
AK
1401 return zclient_send_message(zclient);
1402}
1403
1404static void bgp_evpn_es_vtep_re_eval_active(struct bgp *bgp,
74e2bd89
AK
1405 struct bgp_evpn_es_vtep *es_vtep,
1406 bool param_change)
c44ab6f1
AK
1407{
1408 bool old_active;
1409 bool new_active;
1410
d4980edf 1411 old_active = CHECK_FLAG(es_vtep->flags, BGP_EVPNES_VTEP_ACTIVE);
c44ab6f1
AK
1412 /* currently we need an active EVI reference to use the VTEP as
1413 * a nexthop. this may change...
1414 */
1415 if (es_vtep->evi_cnt)
1416 SET_FLAG(es_vtep->flags, BGP_EVPNES_VTEP_ACTIVE);
1417 else
1418 UNSET_FLAG(es_vtep->flags, BGP_EVPNES_VTEP_ACTIVE);
1419
d4980edf 1420 new_active = CHECK_FLAG(es_vtep->flags, BGP_EVPNES_VTEP_ACTIVE);
c44ab6f1 1421
74e2bd89 1422 if ((old_active != new_active) || (new_active && param_change)) {
c44ab6f1 1423
74e2bd89
AK
1424 if (BGP_DEBUG(evpn_mh, EVPN_MH_ES))
1425 zlog_debug("es %s vtep %pI4 %s df %u/%u",
1426 es_vtep->es->esi_str, &es_vtep->vtep_ip,
1427 new_active ? "active" : "inactive",
1428 es_vtep->df_alg, es_vtep->df_pref);
c44ab6f1 1429
74e2bd89
AK
1430 /* send remote ES to zebra */
1431 bgp_zebra_send_remote_es_vtep(bgp, es_vtep, new_active);
c44ab6f1 1432
8bcb09a1
AK
1433 /* The NHG is updated first for efficient failover handling.
1434 * Note the NHG can be de-activated while there are bgp
1435 * routes referencing it. Zebra is capable of handling that
1436 * elegantly by holding the NHG till all routes using it are
1437 * removed.
26c03e43 1438 */
8bcb09a1 1439 bgp_evpn_l3nhg_update_on_vtep_chg(es_vtep->es);
74e2bd89
AK
1440 /* queue up the es for background consistency checks */
1441 bgp_evpn_es_cons_checks_pend_add(es_vtep->es);
1442 }
c44ab6f1
AK
1443}
1444
1445static struct bgp_evpn_es_vtep *bgp_evpn_es_vtep_add(struct bgp *bgp,
74e2bd89
AK
1446 struct bgp_evpn_es *es,
1447 struct in_addr vtep_ip,
1448 bool esr, uint8_t df_alg,
1449 uint16_t df_pref)
c44ab6f1
AK
1450{
1451 struct bgp_evpn_es_vtep *es_vtep;
74e2bd89 1452 bool param_change = false;
c44ab6f1
AK
1453
1454 es_vtep = bgp_evpn_es_vtep_find(es, vtep_ip);
1455
1456 if (!es_vtep)
1457 es_vtep = bgp_evpn_es_vtep_new(es, vtep_ip);
1458
1459 if (BGP_DEBUG(evpn_mh, EVPN_MH_ES))
74e2bd89
AK
1460 zlog_debug("es %s vtep %pI4 add %s df %u/%u",
1461 es_vtep->es->esi_str, &es_vtep->vtep_ip,
1462 esr ? "esr" : "ead", df_alg, df_pref);
c44ab6f1 1463
74e2bd89 1464 if (esr) {
c44ab6f1 1465 SET_FLAG(es_vtep->flags, BGP_EVPNES_VTEP_ESR);
74e2bd89
AK
1466 if ((es_vtep->df_pref != df_pref)
1467 || (es_vtep->df_alg != df_alg)) {
1468 param_change = true;
1469 es_vtep->df_pref = df_pref;
1470 es_vtep->df_alg = df_alg;
1471 }
1472 } else {
c44ab6f1 1473 ++es_vtep->evi_cnt;
74e2bd89 1474 }
c44ab6f1 1475
74e2bd89 1476 bgp_evpn_es_vtep_re_eval_active(bgp, es_vtep, param_change);
c44ab6f1
AK
1477
1478 return es_vtep;
1479}
1480
1481static void bgp_evpn_es_vtep_do_del(struct bgp *bgp,
1482 struct bgp_evpn_es_vtep *es_vtep, bool esr)
1483{
74e2bd89
AK
1484 bool param_change = false;
1485
c44ab6f1 1486 if (BGP_DEBUG(evpn_mh, EVPN_MH_ES))
23d0a753
DA
1487 zlog_debug("es %s vtep %pI4 del %s", es_vtep->es->esi_str,
1488 &es_vtep->vtep_ip, esr ? "esr" : "ead");
c44ab6f1
AK
1489 if (esr) {
1490 UNSET_FLAG(es_vtep->flags, BGP_EVPNES_VTEP_ESR);
74e2bd89
AK
1491 if (es_vtep->df_pref || es_vtep->df_alg) {
1492 param_change = true;
1493 es_vtep->df_pref = 0;
1494 es_vtep->df_alg = 0;
1495 }
c44ab6f1
AK
1496 } else {
1497 if (es_vtep->evi_cnt)
1498 --es_vtep->evi_cnt;
1499 }
1500
74e2bd89 1501 bgp_evpn_es_vtep_re_eval_active(bgp, es_vtep, param_change);
c44ab6f1
AK
1502 bgp_evpn_es_vtep_free(es_vtep);
1503}
1504
1505static void bgp_evpn_es_vtep_del(struct bgp *bgp,
1506 struct bgp_evpn_es *es, struct in_addr vtep_ip, bool esr)
1507{
1508 struct bgp_evpn_es_vtep *es_vtep;
1509
1510 es_vtep = bgp_evpn_es_vtep_find(es, vtep_ip);
1511 if (es_vtep)
1512 bgp_evpn_es_vtep_do_del(bgp, es_vtep, esr);
1513}
1514
26c03e43 1515/********************** ES MAC-IP paths *************************************
58bff4d1
AK
1516 * 1. Local MAC-IP routes in the VNI routing table are linked to the
1517 * destination ES (macip_evi_path_list) for efficient updates on ES oper
1518 * state changes.
1519 * 2. Non-local MAC-IP routes in the global routing table are linked to
1520 * the detination for efficient updates on -
1521 * a. VTEP add/del - this results in a L3NHG update.
1522 * b. ES-VRF add/del - this may result in the host route being migrated to
1523 * L3NHG or vice versa (flat multipath list).
26c03e43 1524 ****************************************************************************/
60605cbc 1525static void bgp_evpn_path_es_info_free(struct bgp_path_es_info *es_info)
26c03e43
AK
1526{
1527 bgp_evpn_path_es_unlink(es_info);
1528 XFREE(MTYPE_BGP_EVPN_PATH_ES_INFO, es_info);
1529}
1530
60605cbc
AK
1531void bgp_evpn_path_mh_info_free(struct bgp_path_mh_info *mh_info)
1532{
1533 if (mh_info->es_info)
1534 bgp_evpn_path_es_info_free(mh_info->es_info);
090efa2f
AK
1535 if (mh_info->nh_info)
1536 bgp_evpn_path_nh_info_free(mh_info->nh_info);
60605cbc
AK
1537 XFREE(MTYPE_BGP_EVPN_PATH_MH_INFO, mh_info);
1538}
1539
26c03e43
AK
1540static struct bgp_path_es_info *
1541bgp_evpn_path_es_info_new(struct bgp_path_info *pi, vni_t vni)
1542{
1543 struct bgp_path_info_extra *e;
60605cbc
AK
1544 struct bgp_path_mh_info *mh_info;
1545 struct bgp_path_es_info *es_info;
26c03e43
AK
1546
1547 e = bgp_path_info_extra_get(pi);
1548
60605cbc
AK
1549 /* If mh_info doesn't exist allocate it */
1550 mh_info = e->mh_info;
1551 if (!mh_info)
1552 e->mh_info = mh_info = XCALLOC(MTYPE_BGP_EVPN_PATH_MH_INFO,
1553 sizeof(struct bgp_path_mh_info));
1554
26c03e43 1555 /* If es_info doesn't exist allocate it */
60605cbc
AK
1556 es_info = mh_info->es_info;
1557 if (!es_info) {
1558 mh_info->es_info = es_info =
1559 XCALLOC(MTYPE_BGP_EVPN_PATH_ES_INFO,
1560 sizeof(struct bgp_path_es_info));
1561 es_info->vni = vni;
1562 es_info->pi = pi;
26c03e43
AK
1563 }
1564
60605cbc 1565 return es_info;
26c03e43
AK
1566}
1567
60605cbc 1568static void bgp_evpn_path_es_unlink(struct bgp_path_es_info *es_info)
26c03e43
AK
1569{
1570 struct bgp_evpn_es *es = es_info->es;
1571 struct bgp_path_info *pi;
26c03e43
AK
1572
1573 if (!es)
1574 return;
1575
1576 pi = es_info->pi;
1577 if (BGP_DEBUG(evpn_mh, EVPN_MH_RT))
229587fb
AK
1578 zlog_debug("vni %u path %pFX unlinked from es %s", es_info->vni,
1579 &pi->net->p, es->esi_str);
26c03e43 1580
58bff4d1
AK
1581 if (es_info->vni)
1582 list_delete_node(es->macip_evi_path_list,
1583 &es_info->es_listnode);
1584 else
1585 list_delete_node(es->macip_global_path_list,
1586 &es_info->es_listnode);
74efb822 1587
26c03e43
AK
1588 es_info->es = NULL;
1589
1590 /* if there are no other references against the ES it
1591 * needs to be freed
1592 */
1593 bgp_evpn_es_free(es, __func__);
1594
1595 /* Note we don't free the path es_info on unlink; it will be freed up
1596 * along with the path.
1597 */
1598}
1599
1600void bgp_evpn_path_es_link(struct bgp_path_info *pi, vni_t vni, esi_t *esi)
1601{
1602 struct bgp_path_es_info *es_info;
1603 struct bgp_evpn_es *es;
58bff4d1 1604 struct bgp *bgp_evpn;
26c03e43 1605
60605cbc
AK
1606 es_info = (pi->extra && pi->extra->mh_info)
1607 ? pi->extra->mh_info->es_info
1608 : NULL;
26c03e43
AK
1609 /* if the esi is zero just unlink the path from the old es */
1610 if (!esi || !memcmp(esi, zero_esi, sizeof(*esi))) {
1611 if (es_info)
1612 bgp_evpn_path_es_unlink(es_info);
1613 return;
1614 }
1615
58bff4d1 1616 bgp_evpn = bgp_get_evpn();
26c03e43
AK
1617 if (!bgp_evpn)
1618 return;
1619
1620 /* setup es_info against the path if it doesn't aleady exist */
1621 if (!es_info)
1622 es_info = bgp_evpn_path_es_info_new(pi, vni);
1623
1624 /* find-create ES */
1625 es = bgp_evpn_es_find(esi);
1626 if (!es)
f61fbf21 1627 es = bgp_evpn_es_new(bgp_evpn, esi);
26c03e43
AK
1628
1629 /* dup check */
1630 if (es_info->es == es)
1631 return;
1632
1633 /* unlink old ES if any */
1634 bgp_evpn_path_es_unlink(es_info);
1635
1636 if (BGP_DEBUG(evpn_mh, EVPN_MH_RT))
229587fb
AK
1637 zlog_debug("vni %u path %pFX linked to es %s", vni, &pi->net->p,
1638 es->esi_str);
26c03e43
AK
1639
1640 /* link mac-ip path to the new destination ES */
1641 es_info->es = es;
1642 listnode_init(&es_info->es_listnode, es_info);
58bff4d1
AK
1643 if (es_info->vni)
1644 listnode_add(es->macip_evi_path_list, &es_info->es_listnode);
1645 else
1646 listnode_add(es->macip_global_path_list, &es_info->es_listnode);
26c03e43
AK
1647}
1648
74efb822
AK
1649static bool bgp_evpn_is_macip_path(struct bgp_path_info *pi)
1650{
1651 struct prefix_evpn *evp;
1652
1653 /* Only MAC-IP routes need to be linked (MAC-only routes can be
1654 * skipped) as these lists are maintained for managing
1655 * host routes in the tenant VRF
1656 */
1657 evp = (struct prefix_evpn *)&pi->net->p;
1658 return is_evpn_prefix_ipaddr_v4(evp) || is_evpn_prefix_ipaddr_v6(evp);
1659}
1660
58bff4d1
AK
1661/* When a remote ES is added to a VRF, routes using that as
1662 * a destination need to be migrated to a L3NHG or viceversa.
1663 * This is done indirectly by re-attempting an install of the
1664 * route in the associated VRFs. As a part of the VRF install use
1665 * of l3 NHG is evaluated and this results in the
b3ba2118 1666 * attr.es_flag ATTR_ES_L3_NHG_USE being set or cleared.
36dd4574
AK
1667 */
1668static void
1669bgp_evpn_es_path_update_on_es_vrf_chg(struct bgp_evpn_es_vrf *es_vrf,
58bff4d1 1670 const char *reason)
26c03e43
AK
1671{
1672 struct listnode *node;
1673 struct bgp_path_es_info *es_info;
1674 struct bgp_path_info *pi;
58bff4d1 1675 struct bgp_evpn_es *es = es_vrf->es;
26c03e43 1676
36dd4574
AK
1677 if (!bgp_mh_info->host_routes_use_l3nhg)
1678 return;
1679
26c03e43 1680 if (BGP_DEBUG(evpn_mh, EVPN_MH_RT))
58bff4d1 1681 zlog_debug("update paths linked to es %s on es-vrf %s %s",
dc9ca53f 1682 es->esi_str, es_vrf->bgp_vrf->name_pretty, reason);
26c03e43 1683
58bff4d1 1684 for (ALL_LIST_ELEMENTS_RO(es->macip_global_path_list, node, es_info)) {
26c03e43 1685 pi = es_info->pi;
26c03e43 1686
74efb822
AK
1687 if (!bgp_evpn_is_macip_path(pi))
1688 continue;
1689
26c03e43 1690 if (BGP_DEBUG(evpn_mh, EVPN_MH_RT))
2eef4f20 1691 zlog_debug(
58bff4d1
AK
1692 "update path %pFX linked to es %s on vrf chg",
1693 &pi->net->p, es->esi_str);
1694 bgp_evpn_route_entry_install_if_vrf_match(es_vrf->bgp_vrf, pi,
1695 1);
26c03e43
AK
1696 }
1697}
1698
7b0db0e4
AK
1699static void bgp_evpn_es_frag_free(struct bgp_evpn_es_frag *es_frag)
1700{
1701 struct bgp_evpn_es *es = es_frag->es;
1702
1703 if (es->es_base_frag == es_frag)
1704 es->es_base_frag = NULL;
1705
1706 if (BGP_DEBUG(evpn_mh, EVPN_MH_ES))
1707 zlog_debug("es %s frag %u free", es->esi_str, es_frag->rd_id);
1708 list_delete_node(es->es_frag_list, &es_frag->es_listnode);
1709
1710 /* EVIs that are advertised using the info in this fragment */
1711 list_delete(&es_frag->es_evi_frag_list);
1712
1713 bf_release_index(bm->rd_idspace, es_frag->rd_id);
1714
1715
1716 XFREE(MTYPE_BGP_EVPN_ES_FRAG, es_frag);
1717}
1718
1719static void bgp_evpn_es_frag_free_unused(struct bgp_evpn_es_frag *es_frag)
1720{
1721 if ((es_frag->es->es_base_frag == es_frag) ||
1722 listcount(es_frag->es_evi_frag_list))
1723 return;
1724
1725 bgp_evpn_es_frag_free(es_frag);
1726}
1727
1728static void bgp_evpn_es_frag_free_all(struct bgp_evpn_es *es)
1729{
1730 struct listnode *node;
1731 struct listnode *nnode;
1732 struct bgp_evpn_es_frag *es_frag;
1733
1734 for (ALL_LIST_ELEMENTS(es->es_frag_list, node, nnode, es_frag))
1735 bgp_evpn_es_frag_free(es_frag);
1736}
1737
1738static struct bgp_evpn_es_frag *bgp_evpn_es_frag_new(struct bgp_evpn_es *es)
1739{
1740 struct bgp_evpn_es_frag *es_frag;
1741 char buf[BGP_EVPN_PREFIX_RD_LEN];
1742 struct bgp *bgp;
1743
1744 es_frag = XCALLOC(MTYPE_BGP_EVPN_ES_FRAG, sizeof(*es_frag));
1745 bf_assign_index(bm->rd_idspace, es_frag->rd_id);
1746 es_frag->prd.family = AF_UNSPEC;
1747 es_frag->prd.prefixlen = 64;
1748 bgp = bgp_get_evpn();
1749 snprintfrr(buf, sizeof(buf), "%pI4:%hu", &bgp->router_id,
1750 es_frag->rd_id);
1751 (void)str2prefix_rd(buf, &es_frag->prd);
1752
1753 /* EVIs that are advertised using the info in this fragment */
1754 es_frag->es_evi_frag_list = list_new();
1755 listset_app_node_mem(es_frag->es_evi_frag_list);
1756
1757 /* Link the fragment to the parent ES */
1758 es_frag->es = es;
1759 listnode_init(&es_frag->es_listnode, es_frag);
1760 listnode_add(es->es_frag_list, &es_frag->es_listnode);
1761
1762 if (BGP_DEBUG(evpn_mh, EVPN_MH_ES))
1763 zlog_debug("es %s frag %u new", es->esi_str, es_frag->rd_id);
1764 return es_frag;
1765}
1766
1767static struct bgp_evpn_es_frag *
1768bgp_evpn_es_find_frag_with_space(struct bgp_evpn_es *es)
1769{
1770 struct listnode *node;
1771 struct bgp_evpn_es_frag *es_frag;
1772
1773 for (ALL_LIST_ELEMENTS_RO(es->es_frag_list, node, es_frag)) {
bb37eabe
AK
1774 if (listcount(es_frag->es_evi_frag_list) <
1775 bgp_mh_info->evi_per_es_frag)
7b0db0e4
AK
1776 return es_frag;
1777 }
1778
1779 /* No frags where found with space; allocate a new one */
1780 return bgp_evpn_es_frag_new(es);
1781}
1782
1783/* Link the ES-EVI to one of the ES fragments */
1784static void bgp_evpn_es_frag_evi_add(struct bgp_evpn_es_evi *es_evi)
1785{
1786 struct bgp_evpn_es_frag *es_frag;
1787 struct bgp_evpn_es *es = es_evi->es;
1788
1789 if (es_evi->es_frag ||
1790 !(CHECK_FLAG(es_evi->flags, BGP_EVPNES_EVI_LOCAL)))
1791 return;
1792
1793 es_frag = bgp_evpn_es_find_frag_with_space(es);
1794
1795 es_evi->es_frag = es_frag;
1796 listnode_init(&es_evi->es_frag_listnode, es_evi);
1797 listnode_add(es_frag->es_evi_frag_list, &es_evi->es_frag_listnode);
1798
1799 if (BGP_DEBUG(evpn_mh, EVPN_MH_ES))
1800 zlog_debug("es %s vni %d linked to frag %u", es->esi_str,
1801 es_evi->vpn->vni, es_frag->rd_id);
1802}
1803
1804/* UnLink the ES-EVI from the ES fragment */
1805static void bgp_evpn_es_frag_evi_del(struct bgp_evpn_es_evi *es_evi,
1806 bool send_ead_del_if_empty)
1807{
1808 struct bgp_evpn_es_frag *es_frag = es_evi->es_frag;
1809 struct prefix_evpn p;
1810 struct bgp_evpn_es *es;
1811 struct bgp *bgp;
1812
1813 if (!es_frag)
1814 return;
1815
1816 es = es_frag->es;
1817 es_evi->es_frag = NULL;
1818 if (BGP_DEBUG(evpn_mh, EVPN_MH_ES))
1819 zlog_debug("es %s vni %d unlinked from frag %u", es->esi_str,
1820 es_evi->vpn->vni, es_frag->rd_id);
1821
1822 list_delete_node(es_frag->es_evi_frag_list, &es_evi->es_frag_listnode);
1823
1824 /*
1825 * if there are no other EVIs on the fragment deleted the EAD-ES for
1826 * the fragment
1827 */
1828 if (send_ead_del_if_empty && !listcount(es_frag->es_evi_frag_list)) {
1829 bgp = bgp_get_evpn();
1830
1831 if (BGP_DEBUG(evpn_mh, EVPN_MH_ES))
1832 zlog_debug("es %s frag %u ead-es route delete",
1833 es->esi_str, es_frag->rd_id);
1834 build_evpn_type1_prefix(&p, BGP_EVPN_AD_ES_ETH_TAG, &es->esi,
1835 es->originator_ip);
1836 p.prefix.ead_addr.frag_id = es_frag->rd_id;
1837 bgp_evpn_mh_route_delete(bgp, es, NULL, es_frag, &p);
1838 }
1839
1840 /* We don't attempt to coalesce frags that may not be full. Instead we
1841 * only free up the frag when it is completely empty.
1842 */
1843 bgp_evpn_es_frag_free_unused(es_frag);
1844}
1845
1846/* Link the ES-EVIs to one of the ES fragments */
1847static void bgp_evpn_es_frag_evi_update_all(struct bgp_evpn_es *es, bool add)
1848{
1849 struct listnode *node;
1850 struct bgp_evpn_es_evi *es_evi;
1851
1852 for (ALL_LIST_ELEMENTS_RO(es->es_evi_list, node, es_evi)) {
1853 if (add)
1854 bgp_evpn_es_frag_evi_add(es_evi);
1855 else
1856 bgp_evpn_es_frag_evi_del(es_evi, false);
1857 }
1858}
1859
c44ab6f1
AK
1860/* compare ES-IDs for the global ES RB tree */
1861static int bgp_es_rb_cmp(const struct bgp_evpn_es *es1,
1862 const struct bgp_evpn_es *es2)
1863{
1864 return memcmp(&es1->esi, &es2->esi, ESI_BYTES);
1865}
1866RB_GENERATE(bgp_es_rb_head, bgp_evpn_es, rb_node, bgp_es_rb_cmp);
1867
1868struct bgp_evpn_es *bgp_evpn_es_find(const esi_t *esi)
185fb14a 1869{
c44ab6f1 1870 struct bgp_evpn_es tmp;
185fb14a 1871
185fb14a 1872 memcpy(&tmp.esi, esi, sizeof(esi_t));
c44ab6f1 1873 return RB_FIND(bgp_es_rb_head, &bgp_mh_info->es_rb_tree, &tmp);
185fb14a
AK
1874}
1875
c44ab6f1 1876static struct bgp_evpn_es *bgp_evpn_es_new(struct bgp *bgp, const esi_t *esi)
185fb14a 1877{
c44ab6f1 1878 struct bgp_evpn_es *es;
185fb14a 1879
c44ab6f1 1880 es = XCALLOC(MTYPE_BGP_EVPN_ES, sizeof(struct bgp_evpn_es));
185fb14a 1881
c44ab6f1 1882 /* set the ESI */
185fb14a 1883 memcpy(&es->esi, esi, sizeof(esi_t));
185fb14a
AK
1884
1885 /* Initialise the VTEP list */
c44ab6f1
AK
1886 es->es_vtep_list = list_new();
1887 listset_app_node_mem(es->es_vtep_list);
1888 es->es_vtep_list->cmp = bgp_evpn_es_vtep_cmp;
185fb14a 1889
c44ab6f1 1890 esi_to_str(&es->esi, es->esi_str, sizeof(es->esi_str));
185fb14a 1891
c44ab6f1 1892 /* Initialize the ES routing table */
185fb14a
AK
1893 es->route_table = bgp_table_init(bgp, AFI_L2VPN, SAFI_EVPN);
1894
c44ab6f1 1895 /* Add to rb_tree */
92809558 1896 RB_INSERT(bgp_es_rb_head, &bgp_mh_info->es_rb_tree, es);
185fb14a 1897
c44ab6f1
AK
1898 /* Initialise the ES-EVI list */
1899 es->es_evi_list = list_new();
1900 listset_app_node_mem(es->es_evi_list);
1901
c589d847
AK
1902 /* Initialise the ES-VRF list used for L3NHG management */
1903 es->es_vrf_list = list_new();
1904 listset_app_node_mem(es->es_vrf_list);
1905
26c03e43 1906 /* Initialise the route list used for efficient event handling */
d656e0ae
AK
1907 es->macip_evi_path_list = list_new();
1908 listset_app_node_mem(es->macip_evi_path_list);
58bff4d1
AK
1909 es->macip_global_path_list = list_new();
1910 listset_app_node_mem(es->macip_global_path_list);
7b0db0e4
AK
1911 es->es_frag_list = list_new();
1912 listset_app_node_mem(es->es_frag_list);
26c03e43 1913
c44ab6f1
AK
1914 QOBJ_REG(es, bgp_evpn_es);
1915
185fb14a
AK
1916 return es;
1917}
1918
c44ab6f1 1919/* Free a given ES -
185fb14a
AK
1920 * This just frees appropriate memory, caller should have taken other
1921 * needed actions.
1922 */
45a859f1 1923static void bgp_evpn_es_free(struct bgp_evpn_es *es, const char *caller)
185fb14a 1924{
26c03e43 1925 if ((es->flags & (BGP_EVPNES_LOCAL | BGP_EVPNES_REMOTE))
58bff4d1
AK
1926 || listcount(es->macip_evi_path_list)
1927 || listcount(es->macip_global_path_list))
c44ab6f1
AK
1928 return;
1929
45a859f1
AK
1930 if (BGP_DEBUG(evpn_mh, EVPN_MH_ES))
1931 zlog_debug("%s: es %s free", caller, es->esi_str);
1932
c44ab6f1
AK
1933 /* cleanup resources maintained against the ES */
1934 list_delete(&es->es_evi_list);
c589d847 1935 list_delete(&es->es_vrf_list);
c44ab6f1 1936 list_delete(&es->es_vtep_list);
d656e0ae 1937 list_delete(&es->macip_evi_path_list);
58bff4d1 1938 list_delete(&es->macip_global_path_list);
7b0db0e4 1939 list_delete(&es->es_frag_list);
185fb14a 1940 bgp_table_unlock(es->route_table);
c44ab6f1
AK
1941
1942 /* remove the entry from various databases */
1943 RB_REMOVE(bgp_es_rb_head, &bgp_mh_info->es_rb_tree, es);
1944 bgp_evpn_es_cons_checks_pend_del(es);
1945
185fb14a
AK
1946 QOBJ_UNREG(es);
1947 XFREE(MTYPE_BGP_EVPN_ES, es);
1948}
1949
74efb822
AK
1950static inline bool bgp_evpn_is_es_local_and_non_bypass(struct bgp_evpn_es *es)
1951{
1952 return (es->flags & BGP_EVPNES_LOCAL)
1953 && !(es->flags & BGP_EVPNES_BYPASS);
1954}
1955
c44ab6f1
AK
1956/* init local info associated with the ES */
1957static void bgp_evpn_es_local_info_set(struct bgp *bgp, struct bgp_evpn_es *es)
185fb14a 1958{
74efb822
AK
1959 bool old_is_local;
1960 bool is_local;
185fb14a 1961
c44ab6f1
AK
1962 if (CHECK_FLAG(es->flags, BGP_EVPNES_LOCAL))
1963 return;
185fb14a 1964
74efb822 1965 old_is_local = bgp_evpn_is_es_local_and_non_bypass(es);
c44ab6f1 1966 SET_FLAG(es->flags, BGP_EVPNES_LOCAL);
74efb822 1967
c44ab6f1
AK
1968 listnode_init(&es->es_listnode, es);
1969 listnode_add(bgp_mh_info->local_es_list, &es->es_listnode);
185fb14a 1970
7b0db0e4
AK
1971 /* setup the first ES fragment; more fragments may be allocated based
1972 * on the the number of EVI entries
1973 */
1974 es->es_base_frag = bgp_evpn_es_frag_new(es);
1975 /* distribute ES-EVIs to one or more ES fragments */
1976 bgp_evpn_es_frag_evi_update_all(es, true);
74efb822
AK
1977
1978 is_local = bgp_evpn_is_es_local_and_non_bypass(es);
1979 if (old_is_local != is_local)
1980 bgp_evpn_mac_update_on_es_local_chg(es, is_local);
c44ab6f1
AK
1981}
1982
1983/* clear any local info associated with the ES */
d57e0c05 1984static void bgp_evpn_es_local_info_clear(struct bgp_evpn_es *es, bool finish)
c44ab6f1 1985{
74efb822
AK
1986 bool old_is_local;
1987 bool is_local;
1988
c44ab6f1
AK
1989 if (!CHECK_FLAG(es->flags, BGP_EVPNES_LOCAL))
1990 return;
185fb14a 1991
7b0db0e4
AK
1992 /* clear the es frag references and free them up */
1993 bgp_evpn_es_frag_evi_update_all(es, false);
1994 es->es_base_frag = NULL;
1995 bgp_evpn_es_frag_free_all(es);
1996
74efb822 1997 old_is_local = bgp_evpn_is_es_local_and_non_bypass(es);
c44ab6f1 1998 UNSET_FLAG(es->flags, BGP_EVPNES_LOCAL);
185fb14a 1999
74efb822 2000 is_local = bgp_evpn_is_es_local_and_non_bypass(es);
d57e0c05 2001 if (!finish && (old_is_local != is_local))
74efb822
AK
2002 bgp_evpn_mac_update_on_es_local_chg(es, is_local);
2003
c44ab6f1
AK
2004 /* remove from the ES local list */
2005 list_delete_node(bgp_mh_info->local_es_list, &es->es_listnode);
2006
45a859f1 2007 bgp_evpn_es_free(es, __func__);
185fb14a
AK
2008}
2009
c44ab6f1
AK
2010/* eval remote info associated with the ES */
2011static void bgp_evpn_es_remote_info_re_eval(struct bgp_evpn_es *es)
2012{
2013 if (es->remote_es_evi_cnt) {
2014 SET_FLAG(es->flags, BGP_EVPNES_REMOTE);
2015 } else {
2016 if (CHECK_FLAG(es->flags, BGP_EVPNES_REMOTE)) {
2017 UNSET_FLAG(es->flags, BGP_EVPNES_REMOTE);
45a859f1 2018 bgp_evpn_es_free(es, __func__);
c44ab6f1
AK
2019 }
2020 }
2021}
2022
70524092
AK
2023/* If ES is present and local it needs to be active/oper-up for
2024 * including L3 EC
2025 */
2026bool bgp_evpn_es_add_l3_ecomm_ok(esi_t *esi)
74be8313 2027{
70524092
AK
2028 struct bgp_evpn_es *es;
2029
2030 if (!esi || !bgp_mh_info->suppress_l3_ecomm_on_inactive_es)
2031 return true;
2032
2033 es = bgp_evpn_es_find(esi);
2034
2035 return (!es || !(es->flags & BGP_EVPNES_LOCAL)
2036 || bgp_evpn_local_es_is_active(es));
2037}
2038
74efb822
AK
2039static bool bgp_evpn_is_valid_local_path(struct bgp_path_info *pi)
2040{
2041 return (CHECK_FLAG(pi->flags, BGP_PATH_VALID)
2042 && pi->type == ZEBRA_ROUTE_BGP
2043 && pi->sub_type == BGP_ROUTE_STATIC);
2044}
2045
d656e0ae
AK
2046/* Update all local MAC-IP routes in the VNI routing table associated
2047 * with the ES. When the ES is down the routes are advertised without
2048 * the L3 extcomm
70524092
AK
2049 */
2050static void bgp_evpn_mac_update_on_es_oper_chg(struct bgp_evpn_es *es)
2051{
2052 struct listnode *node;
2053 struct bgp_path_es_info *es_info;
2054 struct bgp_path_info *pi;
70524092
AK
2055 struct bgp *bgp;
2056 struct bgpevpn *vpn;
2057
2058 if (!bgp_mh_info->suppress_l3_ecomm_on_inactive_es)
2059 return;
2060
2061 if (BGP_DEBUG(evpn_mh, EVPN_MH_ES))
2062 zlog_debug("update paths linked to es %s on oper chg",
2063 es->esi_str);
2064
2065 bgp = bgp_get_evpn();
d656e0ae 2066 for (ALL_LIST_ELEMENTS_RO(es->macip_evi_path_list, node, es_info)) {
70524092 2067 pi = es_info->pi;
74efb822
AK
2068
2069 if (!bgp_evpn_is_valid_local_path(pi))
70524092
AK
2070 continue;
2071
74efb822 2072 if (!bgp_evpn_is_macip_path(pi))
70524092
AK
2073 continue;
2074
2075 vpn = bgp_evpn_lookup_vni(bgp, es_info->vni);
2076 if (!vpn)
2077 continue;
2078
2079 if (BGP_DEBUG(evpn_mh, EVPN_MH_RT))
58bff4d1 2080 zlog_debug(
70545a29
AK
2081 "update path %d %pFX linked to es %s on oper chg",
2082 es_info->vni, &pi->net->p, es->esi_str);
70524092
AK
2083
2084 bgp_evpn_update_type2_route_entry(bgp, vpn, pi->net, pi,
2085 __func__);
2086 }
74be8313
AK
2087}
2088
74efb822
AK
2089static bool bgp_evpn_is_valid_bgp_path(struct bgp_path_info *pi)
2090{
2091 return (CHECK_FLAG(pi->flags, BGP_PATH_VALID)
2092 && pi->type == ZEBRA_ROUTE_BGP
2093 && pi->sub_type == BGP_ROUTE_NORMAL);
2094}
2095
2096/* If an ES is no longer local (or becomes local) we need to re-install
2097 * paths using that ES as destination. This is needed as the criteria
2098 * for best path selection has changed.
2099 */
2100static void bgp_evpn_mac_update_on_es_local_chg(struct bgp_evpn_es *es,
2101 bool is_local)
2102{
2103 struct listnode *node;
2104 struct bgp_path_es_info *es_info;
2105 struct bgp_path_info *pi;
74efb822
AK
2106 bool tmp_local;
2107 struct attr *attr_new;
2108 struct attr attr_tmp;
2109
2110 if (BGP_DEBUG(evpn_mh, EVPN_MH_ES))
2111 zlog_debug("update paths linked to es %s on chg to %s",
2112 es->esi_str, is_local ? "local" : "non-local");
2113
2114 for (ALL_LIST_ELEMENTS_RO(es->macip_global_path_list, node, es_info)) {
2115 pi = es_info->pi;
2116
2117 /* Consider "valid" remote routes */
2118 if (!bgp_evpn_is_valid_bgp_path(pi))
2119 continue;
2120
2121 if (!pi->attr)
2122 continue;
2123
2124 tmp_local = !!(pi->attr->es_flags & ATTR_ES_IS_LOCAL);
2125 if (tmp_local == is_local)
2126 continue;
2127
2128 if (BGP_DEBUG(evpn_mh, EVPN_MH_RT))
2129 zlog_debug(
70545a29
AK
2130 "update path %pFX linked to es %s on chg to %s",
2131 &pi->net->p, es->esi_str,
2132 is_local ? "local" : "non-local");
74efb822
AK
2133
2134 attr_tmp = *pi->attr;
2135 if (is_local)
2136 attr_tmp.es_flags |= ATTR_ES_IS_LOCAL;
2137 else
2138 attr_tmp.es_flags &= ~ATTR_ES_IS_LOCAL;
2139 attr_new = bgp_attr_intern(&attr_tmp);
2140 bgp_attr_unintern(&pi->attr);
2141 pi->attr = attr_new;
2142 bgp_evpn_import_type2_route(pi, 1);
2143 }
2144}
2145
74be8313
AK
2146static void bgp_evpn_local_es_deactivate(struct bgp *bgp,
2147 struct bgp_evpn_es *es)
185fb14a 2148{
185fb14a 2149 struct prefix_evpn p;
c44ab6f1 2150 int ret;
185fb14a 2151
c44ab6f1
AK
2152 /* withdraw ESR */
2153 /* Delete and withdraw locally learnt ES route */
2154 build_evpn_type4_prefix(&p, &es->esi, es->originator_ip);
2155 ret = bgp_evpn_type4_route_delete(bgp, es, &p);
2156 if (ret) {
2157 flog_err(EC_BGP_EVPN_ROUTE_DELETE,
2158 "%u failed to delete type-4 route for ESI %s",
2159 bgp->vrf_id, es->esi_str);
185fb14a
AK
2160 }
2161
c44ab6f1
AK
2162 /* withdraw EAD-EVI */
2163 if (!bgp_mh_info->ead_evi_adv_for_down_links)
2164 bgp_evpn_local_type1_evi_route_del(bgp, es);
185fb14a 2165
c44ab6f1 2166 /* withdraw EAD-ES */
7b0db0e4 2167 bgp_evpn_ead_es_route_delete(bgp, es);
70524092
AK
2168
2169 bgp_evpn_mac_update_on_es_oper_chg(es);
c44ab6f1
AK
2170}
2171
74be8313
AK
2172/* Process ES link oper-down by withdrawing ES-EAD and ESR */
2173static void bgp_evpn_local_es_down(struct bgp *bgp, struct bgp_evpn_es *es)
c44ab6f1 2174{
74be8313 2175 bool old_active;
c44ab6f1 2176
74be8313
AK
2177 if (!CHECK_FLAG(es->flags, BGP_EVPNES_OPER_UP))
2178 return;
c44ab6f1 2179
74be8313
AK
2180 old_active = bgp_evpn_local_es_is_active(es);
2181 UNSET_FLAG(es->flags, BGP_EVPNES_OPER_UP);
2182
2183 if (BGP_DEBUG(evpn_mh, EVPN_MH_ES))
2184 zlog_debug("local es %s down", es->esi_str);
2185
2186 if (old_active)
2187 bgp_evpn_local_es_deactivate(bgp, es);
2188}
2189
2190static void bgp_evpn_local_es_activate(struct bgp *bgp, struct bgp_evpn_es *es,
2191 bool regen_ead, bool regen_esr)
2192{
2193 struct prefix_evpn p;
c44ab6f1 2194
74e2bd89
AK
2195 if (regen_esr) {
2196 if (BGP_DEBUG(evpn_mh, EVPN_MH_ES))
2197 zlog_debug("local es %s generate ESR", es->esi_str);
2198 /* generate ESR */
2199 build_evpn_type4_prefix(&p, &es->esi, es->originator_ip);
2200 if (bgp_evpn_type4_route_update(bgp, es, &p))
2201 flog_err(EC_BGP_EVPN_ROUTE_CREATE,
2202 "%u: Type4 route creation failure for ESI %s",
2203 bgp->vrf_id, es->esi_str);
2204 }
c44ab6f1 2205
74e2bd89
AK
2206 if (regen_ead) {
2207 if (BGP_DEBUG(evpn_mh, EVPN_MH_ES))
2208 zlog_debug("local es %s generate EAD", es->esi_str);
2209 /* generate EAD-EVI */
2210 bgp_evpn_local_type1_evi_route_add(bgp, es);
c44ab6f1 2211
74e2bd89 2212 /* generate EAD-ES */
7b0db0e4 2213 bgp_evpn_ead_es_route_update(bgp, es);
74e2bd89 2214 }
70524092
AK
2215
2216 bgp_evpn_mac_update_on_es_oper_chg(es);
c44ab6f1
AK
2217}
2218
74be8313
AK
2219/* Process ES link oper-up by generating ES-EAD and ESR */
2220static void bgp_evpn_local_es_up(struct bgp *bgp, struct bgp_evpn_es *es,
2221 bool regen_esr)
2222{
2223 bool regen_ead = false;
2224 bool active = false;
2225
2226 if (!CHECK_FLAG(es->flags, BGP_EVPNES_OPER_UP)) {
2227 if (BGP_DEBUG(evpn_mh, EVPN_MH_ES))
2228 zlog_debug("local es %s up", es->esi_str);
2229
2230 SET_FLAG(es->flags, BGP_EVPNES_OPER_UP);
2231 regen_esr = true;
2232 regen_ead = true;
2233 }
2234
2235 active = bgp_evpn_local_es_is_active(es);
2236 if (active && (regen_ead || regen_esr))
2237 bgp_evpn_local_es_activate(bgp, es, regen_ead, regen_esr);
2238}
2239
2240/* If an ethernet segment is in LACP bypass we cannot advertise
2241 * reachability to it i.e. EAD-per-ES and ESR is not advertised in
2242 * bypass state.
2243 * PS: EAD-per-EVI will continue to be advertised
2244 */
2245static void bgp_evpn_local_es_bypass_update(struct bgp *bgp,
2246 struct bgp_evpn_es *es, bool bypass)
2247{
2248 bool old_bypass = !!(es->flags & BGP_EVPNES_BYPASS);
2249 bool old_active;
2250 bool new_active;
74efb822
AK
2251 bool old_is_local;
2252 bool is_local;
74be8313
AK
2253
2254 if (bypass == old_bypass)
2255 return;
2256
2257 old_active = bgp_evpn_local_es_is_active(es);
74efb822 2258 old_is_local = bgp_evpn_is_es_local_and_non_bypass(es);
74be8313
AK
2259 if (bypass)
2260 SET_FLAG(es->flags, BGP_EVPNES_BYPASS);
2261 else
2262 UNSET_FLAG(es->flags, BGP_EVPNES_BYPASS);
2263
2264 if (BGP_DEBUG(evpn_mh, EVPN_MH_ES))
2265 zlog_debug("local es %s bypass %s", es->esi_str,
2266 bypass ? "set" : "clear");
2267
2268 new_active = bgp_evpn_local_es_is_active(es);
2269 if (old_active != new_active) {
2270 if (new_active)
2271 bgp_evpn_local_es_activate(bgp, es, true, true);
2272 else
2273 bgp_evpn_local_es_deactivate(bgp, es);
2274 }
74efb822
AK
2275
2276 is_local = bgp_evpn_is_es_local_and_non_bypass(es);
2277 if (old_is_local != is_local)
2278 bgp_evpn_mac_update_on_es_local_chg(es, is_local);
74be8313
AK
2279}
2280
c44ab6f1
AK
2281static void bgp_evpn_local_es_do_del(struct bgp *bgp, struct bgp_evpn_es *es)
2282{
2283 struct bgp_evpn_es_evi *es_evi;
2284 struct listnode *evi_node, *evi_next_node;
2285
2286 if (BGP_DEBUG(evpn_mh, EVPN_MH_ES))
2287 zlog_debug("del local es %s", es->esi_str);
2288
2289 /* Delete all local EVPN ES routes from ESI table
2290 * and schedule for processing (to withdraw from peers))
2291 */
2292 bgp_evpn_es_route_del_all(bgp, es);
2293
2294 /* release all local ES EVIs associated with the ES */
2295 for (ALL_LIST_ELEMENTS(es->es_evi_list, evi_node,
2296 evi_next_node, es_evi)) {
2297 bgp_evpn_local_es_evi_do_del(es_evi);
2298 }
2299
2300 /* Clear local info associated with the ES and free it up if there is
2301 * no remote reference
2302 */
d57e0c05 2303 bgp_evpn_es_local_info_clear(es, false);
c44ab6f1
AK
2304}
2305
74efb822 2306bool bgp_evpn_is_esi_local_and_non_bypass(esi_t *esi)
7904e9fd
AK
2307{
2308 struct bgp_evpn_es *es = NULL;
2309
2310 /* Lookup ESI hash - should exist. */
2311 es = bgp_evpn_es_find(esi);
74efb822
AK
2312
2313 return es && bgp_evpn_is_es_local_and_non_bypass(es);
7904e9fd
AK
2314}
2315
c44ab6f1
AK
2316int bgp_evpn_local_es_del(struct bgp *bgp, esi_t *esi)
2317{
2318 struct bgp_evpn_es *es = NULL;
2319
2320 /* Lookup ESI hash - should exist. */
2321 es = bgp_evpn_es_find(esi);
2322 if (!es) {
eaf80288
DA
2323 flog_warn(EC_BGP_EVPN_ESI, "%u: ES missing at local ES DEL",
2324 bgp->vrf_id);
c44ab6f1
AK
2325 return -1;
2326 }
2327
2328 bgp_evpn_local_es_do_del(bgp, es);
2329 return 0;
2330}
2331
2332/* Handle device to ES id association. Results in the creation of a local
2333 * ES.
2334 */
2335int bgp_evpn_local_es_add(struct bgp *bgp, esi_t *esi,
74e2bd89 2336 struct in_addr originator_ip, bool oper_up,
74be8313 2337 uint16_t df_pref, bool bypass)
c44ab6f1 2338{
c44ab6f1
AK
2339 struct bgp_evpn_es *es;
2340 bool new_es = true;
74e2bd89 2341 bool regen_esr = false;
c44ab6f1
AK
2342
2343 /* create the new es */
2344 es = bgp_evpn_es_find(esi);
2345 if (es) {
2346 if (CHECK_FLAG(es->flags, BGP_EVPNES_LOCAL))
2347 new_es = false;
573eeb2b 2348 } else
c44ab6f1 2349 es = bgp_evpn_es_new(bgp, esi);
c44ab6f1
AK
2350
2351 if (BGP_DEBUG(evpn_mh, EVPN_MH_ES))
74be8313
AK
2352 zlog_debug("add local es %s orig-ip %pI4 df_pref %u %s",
2353 es->esi_str, &originator_ip, df_pref,
2354 bypass ? "bypass" : "");
c44ab6f1
AK
2355
2356 es->originator_ip = originator_ip;
74e2bd89
AK
2357 if (df_pref != es->df_pref) {
2358 es->df_pref = df_pref;
2359 regen_esr = true;
2360 }
c44ab6f1
AK
2361 bgp_evpn_es_local_info_set(bgp, es);
2362
2363 /* import all remote Type-4 routes in the ES table */
2364 if (new_es)
2365 bgp_evpn_type4_remote_routes_import(bgp, es,
2366 true /* install */);
2367
2368 /* create and advertise EAD-EVI routes for the ES -
2369 * XXX - till an ES-EVI reference is created there is really nothing to
2370 * advertise
2371 */
2372 if (bgp_mh_info->ead_evi_adv_for_down_links)
2373 bgp_evpn_local_type1_evi_route_add(bgp, es);
2374
74be8313
AK
2375 bgp_evpn_local_es_bypass_update(bgp, es, bypass);
2376
c44ab6f1
AK
2377 /* If the ES link is operationally up generate EAD-ES. EAD-EVI
2378 * can be generated even if the link is inactive.
2379 */
2380 if (oper_up)
74e2bd89 2381 bgp_evpn_local_es_up(bgp, es, regen_esr);
c44ab6f1
AK
2382 else
2383 bgp_evpn_local_es_down(bgp, es);
2384
2385 return 0;
2386}
2387
7b0db0e4
AK
2388static void bgp_evpn_es_json_frag_fill(json_object *json_frags,
2389 struct bgp_evpn_es *es)
2390{
2391 json_object *json_frag;
7b0db0e4
AK
2392 struct listnode *node;
2393 struct bgp_evpn_es_frag *es_frag;
2394
2395 for (ALL_LIST_ELEMENTS_RO(es->es_frag_list, node, es_frag)) {
2396 json_frag = json_object_new_object();
2397
4a8cd6ad
PG
2398 json_object_string_addf(json_frag, "rd", "%pRDP",
2399 &es_frag->prd);
7b0db0e4
AK
2400 json_object_int_add(json_frag, "eviCount",
2401 listcount(es_frag->es_evi_frag_list));
2402
2403 json_object_array_add(json_frags, json_frag);
2404 }
2405}
2406
2407static void bgp_evpn_es_frag_show_detail(struct vty *vty,
2408 struct bgp_evpn_es *es)
2409{
2410 struct listnode *node;
7b0db0e4
AK
2411 struct bgp_evpn_es_frag *es_frag;
2412
2413 for (ALL_LIST_ELEMENTS_RO(es->es_frag_list, node, es_frag)) {
4a8cd6ad 2414 vty_out(vty, " %pRDP EVIs: %d\n", &es_frag->prd,
7b0db0e4
AK
2415 listcount(es_frag->es_evi_frag_list));
2416 }
2417}
2418
9e0c2fd1
AK
2419static char *bgp_evpn_es_vteps_str(char *vtep_str, struct bgp_evpn_es *es,
2420 uint8_t vtep_str_size)
c44ab6f1
AK
2421{
2422 char vtep_flag_str[BGP_EVPN_FLAG_STR_SZ];
2423 struct listnode *node;
2424 struct bgp_evpn_es_vtep *es_vtep;
2425 bool first = true;
07380148 2426 char ip_buf[INET_ADDRSTRLEN];
c44ab6f1
AK
2427
2428 vtep_str[0] = '\0';
2429 for (ALL_LIST_ELEMENTS_RO(es->es_vtep_list, node, es_vtep)) {
2430 vtep_flag_str[0] = '\0';
23d0a753 2431
c44ab6f1 2432 if (es_vtep->flags & BGP_EVPNES_VTEP_ESR)
9e0c2fd1 2433 strlcat(vtep_flag_str, "E", sizeof(vtep_flag_str));
c44ab6f1 2434 if (es_vtep->flags & BGP_EVPNES_VTEP_ACTIVE)
9e0c2fd1 2435 strlcat(vtep_flag_str, "A", sizeof(vtep_flag_str));
c44ab6f1
AK
2436
2437 if (!strlen(vtep_flag_str))
9e0c2fd1
AK
2438 strlcat(vtep_flag_str, "-", sizeof(vtep_flag_str));
2439 if (first)
c44ab6f1 2440 first = false;
9e0c2fd1
AK
2441 else
2442 strlcat(vtep_str, ",", vtep_str_size);
23d0a753 2443 strlcat(vtep_str,
ec779825
AK
2444 inet_ntop(AF_INET, &es_vtep->vtep_ip, ip_buf,
2445 sizeof(ip_buf)),
23d0a753 2446 vtep_str_size);
9e0c2fd1
AK
2447 strlcat(vtep_str, "(", vtep_str_size);
2448 strlcat(vtep_str, vtep_flag_str, vtep_str_size);
2449 strlcat(vtep_str, ")", vtep_str_size);
c44ab6f1
AK
2450 }
2451
2452 return vtep_str;
2453}
2454
c44ab6f1
AK
2455static void bgp_evpn_es_json_vtep_fill(json_object *json_vteps,
2456 struct bgp_evpn_es_vtep *es_vtep)
2457{
2458 json_object *json_vtep_entry;
2459 json_object *json_flags;
2460
2461 json_vtep_entry = json_object_new_object();
2462
c949c771
DA
2463 json_object_string_addf(json_vtep_entry, "vtep_ip", "%pI4",
2464 &es_vtep->vtep_ip);
c44ab6f1
AK
2465 if (es_vtep->flags & (BGP_EVPNES_VTEP_ESR |
2466 BGP_EVPNES_VTEP_ACTIVE)) {
2467 json_flags = json_object_new_array();
2468 if (es_vtep->flags & BGP_EVPNES_VTEP_ESR)
2469 json_array_string_add(json_flags, "esr");
2470 if (es_vtep->flags & BGP_EVPNES_VTEP_ACTIVE)
2471 json_array_string_add(json_flags, "active");
2472 json_object_object_add(json_vtep_entry, "flags", json_flags);
74e2bd89
AK
2473 if (es_vtep->flags & BGP_EVPNES_VTEP_ESR) {
2474 json_object_int_add(json_vtep_entry, "dfPreference",
2475 es_vtep->df_pref);
2476 json_object_int_add(json_vtep_entry, "dfAlgorithm",
2477 es_vtep->df_pref);
2478 }
c44ab6f1
AK
2479 }
2480
2481 json_object_array_add(json_vteps,
2482 json_vtep_entry);
2483}
2484
74e2bd89
AK
2485static void bgp_evpn_es_vteps_show_detail(struct vty *vty,
2486 struct bgp_evpn_es *es)
2487{
2488 char vtep_flag_str[BGP_EVPN_FLAG_STR_SZ];
2489 struct listnode *node;
2490 struct bgp_evpn_es_vtep *es_vtep;
2491 char alg_buf[EVPN_DF_ALG_STR_LEN];
2492
2493 for (ALL_LIST_ELEMENTS_RO(es->es_vtep_list, node, es_vtep)) {
2494 vtep_flag_str[0] = '\0';
2495 if (es_vtep->flags & BGP_EVPNES_VTEP_ESR)
2496 strlcat(vtep_flag_str, "E", sizeof(vtep_flag_str));
2497 if (es_vtep->flags & BGP_EVPNES_VTEP_ACTIVE)
2498 strlcat(vtep_flag_str, "A", sizeof(vtep_flag_str));
2499
2500 if (!strlen(vtep_flag_str))
2501 strlcat(vtep_flag_str, "-", sizeof(vtep_flag_str));
2502
ec779825 2503 vty_out(vty, " %pI4 flags: %s", &es_vtep->vtep_ip,
74e2bd89
AK
2504 vtep_flag_str);
2505
2506 if (es_vtep->flags & BGP_EVPNES_VTEP_ESR)
2507 vty_out(vty, " df_alg: %s df_pref: %u\n",
2508 evpn_es_df_alg2str(es_vtep->df_alg, alg_buf,
2509 sizeof(alg_buf)),
2510 es_vtep->df_pref);
2511 else
2512 vty_out(vty, "\n");
2513 }
2514}
2515
c44ab6f1
AK
2516static void bgp_evpn_es_show_entry(struct vty *vty,
2517 struct bgp_evpn_es *es, json_object *json)
2518{
c44ab6f1
AK
2519 struct listnode *node;
2520 struct bgp_evpn_es_vtep *es_vtep;
2521
2522 if (json) {
2523 json_object *json_vteps;
2524 json_object *json_types;
2525
2526 json_object_string_add(json, "esi", es->esi_str);
7b0db0e4 2527 if (es->es_base_frag)
4a8cd6ad 2528 json_object_string_addf(json, "rd", "%pRDP",
c4f64ea9 2529 &es->es_base_frag->prd);
c44ab6f1
AK
2530
2531 if (es->flags & (BGP_EVPNES_LOCAL | BGP_EVPNES_REMOTE)) {
2532 json_types = json_object_new_array();
2533 if (es->flags & BGP_EVPNES_LOCAL)
2534 json_array_string_add(json_types, "local");
2535 if (es->flags & BGP_EVPNES_REMOTE)
2536 json_array_string_add(json_types, "remote");
2537 json_object_object_add(json, "type", json_types);
2538 }
2539
2540 if (listcount(es->es_vtep_list)) {
2541 json_vteps = json_object_new_array();
2542 for (ALL_LIST_ELEMENTS_RO(es->es_vtep_list,
2543 node, es_vtep)) {
2544 bgp_evpn_es_json_vtep_fill(json_vteps, es_vtep);
2545 }
2546 json_object_object_add(json, "vteps", json_vteps);
2547 }
2548 json_object_int_add(json, "vniCount",
2549 listcount(es->es_evi_list));
2550 } else {
2551 char type_str[4];
2552 char vtep_str[ES_VTEP_LIST_STR_SZ + BGP_EVPN_VTEPS_FLAG_STR_SZ];
2553
2554 type_str[0] = '\0';
74be8313
AK
2555 if (es->flags & BGP_EVPNES_BYPASS)
2556 strlcat(type_str, "B", sizeof(type_str));
c44ab6f1 2557 if (es->flags & BGP_EVPNES_LOCAL)
9e0c2fd1 2558 strlcat(type_str, "L", sizeof(type_str));
c44ab6f1 2559 if (es->flags & BGP_EVPNES_REMOTE)
9e0c2fd1 2560 strlcat(type_str, "R", sizeof(type_str));
c44ab6f1 2561 if (es->inconsistencies)
9e0c2fd1 2562 strlcat(type_str, "I", sizeof(type_str));
c44ab6f1 2563
9e0c2fd1 2564 bgp_evpn_es_vteps_str(vtep_str, es, sizeof(vtep_str));
c44ab6f1 2565
4a8cd6ad 2566 vty_out(vty, "%-30s %-5s %-21pRDP %-8d %s\n", es->esi_str,
3059f5c9
DS
2567 type_str,
2568 es->es_base_frag ? &es->es_base_frag->prd : NULL,
c4f64ea9 2569 listcount(es->es_evi_list), vtep_str);
c44ab6f1
AK
2570 }
2571}
2572
2573static void bgp_evpn_es_show_entry_detail(struct vty *vty,
2574 struct bgp_evpn_es *es, json_object *json)
2575{
2576 if (json) {
2577 json_object *json_flags;
2578 json_object *json_incons;
74e2bd89 2579 json_object *json_vteps;
7b0db0e4 2580 json_object *json_frags;
74e2bd89
AK
2581 struct listnode *node;
2582 struct bgp_evpn_es_vtep *es_vtep;
c44ab6f1
AK
2583
2584 /* Add the "brief" info first */
2585 bgp_evpn_es_show_entry(vty, es, json);
74be8313
AK
2586 if (es->flags
2587 & (BGP_EVPNES_OPER_UP | BGP_EVPNES_ADV_EVI
2588 | BGP_EVPNES_BYPASS)) {
c44ab6f1
AK
2589 json_flags = json_object_new_array();
2590 if (es->flags & BGP_EVPNES_OPER_UP)
2591 json_array_string_add(json_flags, "up");
2592 if (es->flags & BGP_EVPNES_ADV_EVI)
2593 json_array_string_add(json_flags,
2594 "advertiseEVI");
74be8313
AK
2595 if (es->flags & BGP_EVPNES_BYPASS)
2596 json_array_string_add(json_flags, "bypass");
c44ab6f1
AK
2597 json_object_object_add(json, "flags", json_flags);
2598 }
c949c771
DA
2599 json_object_string_addf(json, "originator_ip", "%pI4",
2600 &es->originator_ip);
c44ab6f1
AK
2601 json_object_int_add(json, "remoteVniCount",
2602 es->remote_es_evi_cnt);
229587fb
AK
2603 json_object_int_add(json, "vrfCount",
2604 listcount(es->es_vrf_list));
2605 json_object_int_add(json, "macipPathCount",
d656e0ae 2606 listcount(es->macip_evi_path_list));
58bff4d1
AK
2607 json_object_int_add(json, "macipGlobalPathCount",
2608 listcount(es->macip_global_path_list));
c44ab6f1
AK
2609 json_object_int_add(json, "inconsistentVniVtepCount",
2610 es->incons_evi_vtep_cnt);
74e2bd89
AK
2611 if (listcount(es->es_vtep_list)) {
2612 json_vteps = json_object_new_array();
2613 for (ALL_LIST_ELEMENTS_RO(es->es_vtep_list, node,
2614 es_vtep)) {
2615 bgp_evpn_es_json_vtep_fill(json_vteps, es_vtep);
2616 }
2617 json_object_object_add(json, "vteps", json_vteps);
2618 }
7b0db0e4
AK
2619 if (listcount(es->es_frag_list)) {
2620 json_frags = json_object_new_array();
2621 bgp_evpn_es_json_frag_fill(json_frags, es);
2622 json_object_object_add(json, "fragments", json_frags);
2623 }
c44ab6f1
AK
2624 if (es->inconsistencies) {
2625 json_incons = json_object_new_array();
2626 if (es->inconsistencies & BGP_EVPNES_INCONS_VTEP_LIST)
2627 json_array_string_add(json_incons,
2628 "vni-vtep-mismatch");
2629 json_object_object_add(json, "inconsistencies",
2630 json_incons);
2631 }
2632 } else {
2633 char incons_str[BGP_EVPNES_INCONS_STR_SZ];
2634 char type_str[4];
c44ab6f1
AK
2635
2636 type_str[0] = '\0';
2637 if (es->flags & BGP_EVPNES_LOCAL)
9e0c2fd1 2638 strlcat(type_str, "L", sizeof(type_str));
c44ab6f1 2639 if (es->flags & BGP_EVPNES_REMOTE)
9e0c2fd1 2640 strlcat(type_str, "R", sizeof(type_str));
c44ab6f1 2641
c44ab6f1
AK
2642 vty_out(vty, "ESI: %s\n", es->esi_str);
2643 vty_out(vty, " Type: %s\n", type_str);
3059f5c9
DS
2644 vty_out(vty, " RD: %pRDP\n",
2645 es->es_base_frag ? &es->es_base_frag->prd : NULL);
23d0a753 2646 vty_out(vty, " Originator-IP: %pI4\n", &es->originator_ip);
74e2bd89
AK
2647 if (es->flags & BGP_EVPNES_LOCAL)
2648 vty_out(vty, " Local ES DF preference: %u\n",
2649 es->df_pref);
74be8313
AK
2650 if (es->flags & BGP_EVPNES_BYPASS)
2651 vty_out(vty, " LACP bypass: on\n");
c44ab6f1
AK
2652 vty_out(vty, " VNI Count: %d\n", listcount(es->es_evi_list));
2653 vty_out(vty, " Remote VNI Count: %d\n",
2654 es->remote_es_evi_cnt);
229587fb 2655 vty_out(vty, " VRF Count: %d\n", listcount(es->es_vrf_list));
58bff4d1 2656 vty_out(vty, " MACIP EVI Path Count: %d\n",
d656e0ae 2657 listcount(es->macip_evi_path_list));
58bff4d1
AK
2658 vty_out(vty, " MACIP Global Path Count: %d\n",
2659 listcount(es->macip_global_path_list));
c44ab6f1
AK
2660 vty_out(vty, " Inconsistent VNI VTEP Count: %d\n",
2661 es->incons_evi_vtep_cnt);
2662 if (es->inconsistencies) {
2663 incons_str[0] = '\0';
2664 if (es->inconsistencies & BGP_EVPNES_INCONS_VTEP_LIST)
9e0c2fd1
AK
2665 strlcat(incons_str, "vni-vtep-mismatch",
2666 sizeof(incons_str));
c44ab6f1 2667 } else {
9e0c2fd1 2668 strlcpy(incons_str, "-", sizeof(incons_str));
c44ab6f1
AK
2669 }
2670 vty_out(vty, " Inconsistencies: %s\n",
2671 incons_str);
7b0db0e4
AK
2672 if (listcount(es->es_frag_list)) {
2673 vty_out(vty, " Fragments:\n");
2674 bgp_evpn_es_frag_show_detail(vty, es);
2675 }
74e2bd89
AK
2676 if (listcount(es->es_vtep_list)) {
2677 vty_out(vty, " VTEPs:\n");
2678 bgp_evpn_es_vteps_show_detail(vty, es);
2679 }
c44ab6f1
AK
2680 vty_out(vty, "\n");
2681 }
2682}
2683
2684/* Display all ESs */
2685void bgp_evpn_es_show(struct vty *vty, bool uj, bool detail)
2686{
2687 struct bgp_evpn_es *es;
9c7edc03 2688 json_object *json_array = NULL;
c44ab6f1
AK
2689 json_object *json = NULL;
2690
2691 if (uj) {
2692 /* create an array of ESs */
2693 json_array = json_object_new_array();
2694 } else {
2695 if (!detail) {
2696 vty_out(vty,
74be8313 2697 "ES Flags: B - bypass, L local, R remote, I inconsistent\n");
c44ab6f1
AK
2698 vty_out(vty,
2699 "VTEP Flags: E ESR/Type-4, A active nexthop\n");
2700 vty_out(vty,
2701 "%-30s %-5s %-21s %-8s %s\n",
2702 "ESI", "Flags", "RD", "#VNIs", "VTEPs");
2703 }
2704 }
2705
2706 RB_FOREACH(es, bgp_es_rb_head, &bgp_mh_info->es_rb_tree) {
2707 if (uj)
2708 /* create a separate json object for each ES */
2709 json = json_object_new_object();
2710 if (detail)
2711 bgp_evpn_es_show_entry_detail(vty, es, json);
2712 else
2713 bgp_evpn_es_show_entry(vty, es, json);
2714 /* add ES to the json array */
2715 if (uj)
2716 json_object_array_add(json_array, json);
2717 }
2718
2719 /* print the array of json-ESs */
c48349e3 2720 if (uj)
75eeda93 2721 vty_json(vty, json_array);
c44ab6f1
AK
2722}
2723
2724/* Display specific ES */
2725void bgp_evpn_es_show_esi(struct vty *vty, esi_t *esi, bool uj)
2726{
2727 struct bgp_evpn_es *es;
2728 json_object *json = NULL;
2729
2730 if (uj)
2731 json = json_object_new_object();
2732
2733 es = bgp_evpn_es_find(esi);
2734 if (es) {
2735 bgp_evpn_es_show_entry_detail(vty, es, json);
2736 } else {
2737 if (!uj)
2738 vty_out(vty, "ESI not found\n");
2739 }
2740
c48349e3 2741 if (uj)
75eeda93 2742 vty_json(vty, json);
c44ab6f1
AK
2743}
2744
c589d847
AK
2745/*****************************************************************************/
2746/* Ethernet Segment to VRF association -
2747 * 1. Each ES-EVI entry is associated with a tenant VRF. This associaton
2748 * triggers the creation of an ES-VRF entry.
2749 * 2. The ES-VRF entry is maintained for the purpose of L3-NHG creation
2750 * 3. Type-2/MAC-IP routes are imported into a tenant VRF and programmed as
2751 * a /32 or host route entry in the dataplane. If the destination of
2752 * the host route is a remote-ES the route is programmed with the
2753 * corresponding (keyed in by {vrf,ES-id}) L3-NHG.
2754 * 4. The reason for this indirection (route->L3-NHG, L3-NHG->list-of-VTEPs)
2755 * is to avoid route updates to the dplane when a remote-ES link flaps i.e.
2756 * instead of updating all the dependent routes the NHG's contents are updated.
2757 * This reduces the amount of datplane updates (nhg updates vs. route updates)
2758 * allowing for a faster failover.
2759 *
2760 * XXX - can the L3 SVI index change without change in vpn->bgp_vrf
2761 * association? If yes we need to handle that by updating all the L3 NHGs
2762 * in that VRF.
2763 */
2764/******************************** L3 NHG management *************************/
8bcb09a1
AK
2765static void bgp_evpn_l3nhg_zebra_add_v4_or_v6(struct bgp_evpn_es_vrf *es_vrf,
2766 bool v4_nhg)
c589d847 2767{
8bcb09a1
AK
2768 uint32_t nhg_id = v4_nhg ? es_vrf->nhg_id : es_vrf->v6_nhg_id;
2769 struct bgp_evpn_es *es = es_vrf->es;
c589d847
AK
2770 struct listnode *node;
2771 struct bgp_evpn_es_vtep *es_vtep;
8bcb09a1
AK
2772 struct nexthop nh;
2773 struct zapi_nexthop *api_nh;
2774 struct zapi_nhg api_nhg = {};
2775
2776 /* Skip installation of L3-NHG if host routes used */
2777 if (!nhg_id)
2778 return;
c589d847
AK
2779
2780 if (BGP_DEBUG(evpn_mh, EVPN_MH_ES))
8bcb09a1
AK
2781 zlog_debug("es %s vrf %u %s nhg %u to zebra", es->esi_str,
2782 es_vrf->bgp_vrf->vrf_id,
2783 v4_nhg ? "v4_nhg" : "v6_nhg", nhg_id);
2784
23aa35ad
AK
2785 frrtrace(4, frr_bgp, evpn_mh_nhg_zsend, true, v4_nhg, nhg_id, es_vrf);
2786
8bcb09a1
AK
2787 /* only the gateway ip changes for each NH. rest of the params
2788 * are constant
2789 */
2790 memset(&nh, 0, sizeof(nh));
2791 nh.vrf_id = es_vrf->bgp_vrf->vrf_id;
2792 nh.flags = NEXTHOP_FLAG_ONLINK;
2793 nh.ifindex = es_vrf->bgp_vrf->l3vni_svi_ifindex;
2794 nh.weight = 1;
2795 nh.type =
2796 v4_nhg ? NEXTHOP_TYPE_IPV4_IFINDEX : NEXTHOP_TYPE_IPV6_IFINDEX;
2797
2798 api_nhg.id = nhg_id;
c589d847 2799 for (ALL_LIST_ELEMENTS_RO(es->es_vtep_list, node, es_vtep)) {
8bcb09a1
AK
2800 if (!CHECK_FLAG(es_vtep->flags, BGP_EVPNES_VTEP_ACTIVE))
2801 continue;
2802
719b209c
MS
2803 /* Don't overrun the zapi buffer. */
2804 if (api_nhg.nexthop_num == MULTIPATH_NUM)
2805 break;
2806
8bcb09a1
AK
2807 /* overwrite the gw */
2808 if (v4_nhg)
2809 nh.gate.ipv4 = es_vtep->vtep_ip;
2810 else
2811 ipv4_to_ipv4_mapped_ipv6(&nh.gate.ipv6,
2812 es_vtep->vtep_ip);
2813
2814 /* convert to zapi format */
2815 api_nh = &api_nhg.nexthops[api_nhg.nexthop_num];
2816 zapi_nexthop_from_nexthop(api_nh, &nh);
2817
2818 ++api_nhg.nexthop_num;
2819 if (BGP_DEBUG(evpn_mh, EVPN_MH_ES))
2820 zlog_debug("nhg %u vtep %pI4 l3-svi %d", api_nhg.id,
2821 &es_vtep->vtep_ip,
2822 es_vrf->bgp_vrf->l3vni_svi_ifindex);
23aa35ad
AK
2823
2824 frrtrace(3, frr_bgp, evpn_mh_nh_zsend, nhg_id, es_vtep, es_vrf);
c589d847
AK
2825 }
2826
8bcb09a1
AK
2827 if (!api_nhg.nexthop_num)
2828 return;
2829
8bcb09a1 2830 zclient_nhg_send(zclient, ZEBRA_NHG_ADD, &api_nhg);
c589d847
AK
2831}
2832
8bcb09a1
AK
2833static bool bgp_evpn_l3nhg_zebra_ok(struct bgp_evpn_es_vrf *es_vrf)
2834{
c2b7d418 2835 if (!bgp_mh_info->host_routes_use_l3nhg)
8bcb09a1
AK
2836 return false;
2837
2838 /* Check socket. */
2839 if (!zclient || zclient->sock < 0)
2840 return false;
2841
2842 return true;
2843}
2844
2845static void bgp_evpn_l3nhg_zebra_add(struct bgp_evpn_es_vrf *es_vrf)
c589d847 2846{
8bcb09a1
AK
2847 if (!bgp_evpn_l3nhg_zebra_ok(es_vrf))
2848 return;
2849
2850 bgp_evpn_l3nhg_zebra_add_v4_or_v6(es_vrf, true /*v4_nhg*/);
2851 bgp_evpn_l3nhg_zebra_add_v4_or_v6(es_vrf, false /*v4_nhg*/);
2852}
2853
2854static void bgp_evpn_l3nhg_zebra_del_v4_or_v6(struct bgp_evpn_es_vrf *es_vrf,
2855 bool v4_nhg)
2856{
2857 struct zapi_nhg api_nhg = {};
2858
2859 api_nhg.id = v4_nhg ? es_vrf->nhg_id : es_vrf->v6_nhg_id;
2860
2861 /* Skip installation of L3-NHG if host routes used */
2862 if (!api_nhg.id)
2863 return;
2864
c589d847 2865 if (BGP_DEBUG(evpn_mh, EVPN_MH_ES))
8bcb09a1 2866 zlog_debug("es %s vrf %u %s nhg %u to zebra",
c589d847 2867 es_vrf->es->esi_str, es_vrf->bgp_vrf->vrf_id,
8bcb09a1
AK
2868 v4_nhg ? "v4_nhg" : "v6_nhg", api_nhg.id);
2869
23aa35ad
AK
2870
2871 frrtrace(4, frr_bgp, evpn_mh_nhg_zsend, false, v4_nhg, api_nhg.id,
2872 es_vrf);
2873
8bcb09a1
AK
2874 zclient_nhg_send(zclient, ZEBRA_NHG_DEL, &api_nhg);
2875}
c589d847 2876
8bcb09a1
AK
2877static void bgp_evpn_l3nhg_zebra_del(struct bgp_evpn_es_vrf *es_vrf)
2878{
2879 if (!bgp_evpn_l3nhg_zebra_ok(es_vrf))
2880 return;
2881
2882 bgp_evpn_l3nhg_zebra_del_v4_or_v6(es_vrf, true /*v4_nhg*/);
2883 bgp_evpn_l3nhg_zebra_del_v4_or_v6(es_vrf, false /*v4_nhg*/);
c589d847
AK
2884}
2885
2886static void bgp_evpn_l3nhg_deactivate(struct bgp_evpn_es_vrf *es_vrf)
2887{
2888 if (!(es_vrf->flags & BGP_EVPNES_VRF_NHG_ACTIVE))
2889 return;
2890
2891 if (BGP_DEBUG(evpn_mh, EVPN_MH_ES))
8bcb09a1 2892 zlog_debug("es %s vrf %u nhg %u de-activate",
c589d847
AK
2893 es_vrf->es->esi_str, es_vrf->bgp_vrf->vrf_id,
2894 es_vrf->nhg_id);
2895 bgp_evpn_l3nhg_zebra_del(es_vrf);
2896 es_vrf->flags &= ~BGP_EVPNES_VRF_NHG_ACTIVE;
58bff4d1
AK
2897 /* MAC-IPs can now be installed via the L3NHG */
2898 bgp_evpn_es_path_update_on_es_vrf_chg(es_vrf, "l3nhg-deactivate");
c589d847
AK
2899}
2900
2901static void bgp_evpn_l3nhg_activate(struct bgp_evpn_es_vrf *es_vrf, bool update)
2902{
2903 if (!bgp_evpn_es_get_active_vtep_cnt(es_vrf->es)) {
2904 bgp_evpn_l3nhg_deactivate(es_vrf);
2905 return;
2906 }
2907
2908 if (es_vrf->flags & BGP_EVPNES_VRF_NHG_ACTIVE) {
2909 if (!update)
2910 return;
2911 } else {
2912 if (BGP_DEBUG(evpn_mh, EVPN_MH_ES))
8bcb09a1 2913 zlog_debug("es %s vrf %u nhg %u activate",
c589d847
AK
2914 es_vrf->es->esi_str, es_vrf->bgp_vrf->vrf_id,
2915 es_vrf->nhg_id);
2916 es_vrf->flags |= BGP_EVPNES_VRF_NHG_ACTIVE;
58bff4d1
AK
2917 /* MAC-IPs can now be installed via the L3NHG */
2918 bgp_evpn_es_path_update_on_es_vrf_chg(es_vrf, "l3nhg_activate");
c589d847
AK
2919 }
2920
2921 bgp_evpn_l3nhg_zebra_add(es_vrf);
2922}
2923
2924/* when a VTEP is activated or de-activated against an ES associated
2925 * VRFs' NHG needs to be updated
2926 */
2927static void bgp_evpn_l3nhg_update_on_vtep_chg(struct bgp_evpn_es *es)
2928{
2929 struct bgp_evpn_es_vrf *es_vrf;
2930 struct listnode *es_vrf_node;
2931
2932 if (BGP_DEBUG(evpn_mh, EVPN_MH_ES))
2933 zlog_debug("es %s nhg update on vtep chg", es->esi_str);
2934
2935 for (ALL_LIST_ELEMENTS_RO(es->es_vrf_list, es_vrf_node, es_vrf))
2936 bgp_evpn_l3nhg_activate(es_vrf, true /* update */);
2937}
2938
2939/* compare ES-IDs for the ES-VRF RB tree maintained per-VRF */
2940static int bgp_es_vrf_rb_cmp(const struct bgp_evpn_es_vrf *es_vrf1,
2941 const struct bgp_evpn_es_vrf *es_vrf2)
2942{
2943 return memcmp(&es_vrf1->es->esi, &es_vrf2->es->esi, ESI_BYTES);
2944}
2945RB_GENERATE(bgp_es_vrf_rb_head, bgp_evpn_es_vrf, rb_node, bgp_es_vrf_rb_cmp);
2946
2947/* Initialize the ES tables maintained per-tenant vrf */
2948void bgp_evpn_vrf_es_init(struct bgp *bgp_vrf)
2949{
2950 /* Initialize the ES-VRF RB tree */
2951 RB_INIT(bgp_es_vrf_rb_head, &bgp_vrf->es_vrf_rb_tree);
2952}
2953
2954/* find the ES-VRF in the per-VRF RB tree */
2955static struct bgp_evpn_es_vrf *bgp_evpn_es_vrf_find(struct bgp_evpn_es *es,
2956 struct bgp *bgp_vrf)
2957{
2958 struct bgp_evpn_es_vrf es_vrf;
2959
2960 es_vrf.es = es;
2961
2962 return RB_FIND(bgp_es_vrf_rb_head, &bgp_vrf->es_vrf_rb_tree, &es_vrf);
2963}
2964
2965/* allocate a new ES-VRF and setup L3NHG for it */
2966static struct bgp_evpn_es_vrf *bgp_evpn_es_vrf_create(struct bgp_evpn_es *es,
2967 struct bgp *bgp_vrf)
2968{
2969 struct bgp_evpn_es_vrf *es_vrf;
2970
2971 es_vrf = XCALLOC(MTYPE_BGP_EVPN_ES_VRF, sizeof(*es_vrf));
2972
2973 es_vrf->es = es;
2974 es_vrf->bgp_vrf = bgp_vrf;
2975
2976 /* insert into the VRF-ESI rb tree */
92809558 2977 RB_INSERT(bgp_es_vrf_rb_head, &bgp_vrf->es_vrf_rb_tree, es_vrf);
c589d847
AK
2978
2979 /* add to the ES's VRF list */
2980 listnode_init(&es_vrf->es_listnode, es_vrf);
2981 listnode_add(es->es_vrf_list, &es_vrf->es_listnode);
2982
2983 /* setup the L3 NHG id for the ES */
2984 es_vrf->nhg_id = bgp_l3nhg_id_alloc();
8bcb09a1
AK
2985 es_vrf->v6_nhg_id = bgp_l3nhg_id_alloc();
2986
c589d847 2987 if (BGP_DEBUG(evpn_mh, EVPN_MH_ES))
8bcb09a1
AK
2988 zlog_debug("es %s vrf %u nhg %u v6_nhg %d create", es->esi_str,
2989 bgp_vrf->vrf_id, es_vrf->nhg_id, es_vrf->v6_nhg_id);
c589d847
AK
2990 bgp_evpn_l3nhg_activate(es_vrf, false /* update */);
2991
36dd4574
AK
2992 /* update paths in the VRF that may already be associated with
2993 * this destination ES
2994 */
58bff4d1 2995 bgp_evpn_es_path_update_on_es_vrf_chg(es_vrf, "es-vrf-create");
36dd4574 2996
c589d847
AK
2997 return es_vrf;
2998}
2999
3000/* remove the L3-NHG associated with the ES-VRF and free it */
3001static void bgp_evpn_es_vrf_delete(struct bgp_evpn_es_vrf *es_vrf)
3002{
3003 struct bgp_evpn_es *es = es_vrf->es;
3004 struct bgp *bgp_vrf = es_vrf->bgp_vrf;
3005
3006 if (BGP_DEBUG(evpn_mh, EVPN_MH_ES))
8bcb09a1 3007 zlog_debug("es %s vrf %u nhg %u delete", es->esi_str,
c589d847
AK
3008 bgp_vrf->vrf_id, es_vrf->nhg_id);
3009
3010 /* Remove the NHG resources */
3011 bgp_evpn_l3nhg_deactivate(es_vrf);
3012 if (es_vrf->nhg_id)
3013 bgp_l3nhg_id_free(es_vrf->nhg_id);
3014 es_vrf->nhg_id = 0;
8bcb09a1
AK
3015 if (es_vrf->v6_nhg_id)
3016 bgp_l3nhg_id_free(es_vrf->v6_nhg_id);
3017 es_vrf->v6_nhg_id = 0;
c589d847
AK
3018
3019 /* remove from the ES's VRF list */
3020 list_delete_node(es->es_vrf_list, &es_vrf->es_listnode);
3021
3022 /* remove from the VRF-ESI rb tree */
3023 RB_REMOVE(bgp_es_vrf_rb_head, &bgp_vrf->es_vrf_rb_tree, es_vrf);
3024
d656e0ae
AK
3025 /* update paths in the VRF that may already be associated with
3026 * this destination ES
3027 */
58bff4d1 3028 bgp_evpn_es_path_update_on_es_vrf_chg(es_vrf, "es-vrf-delete");
d656e0ae 3029
c589d847
AK
3030 XFREE(MTYPE_BGP_EVPN_ES_VRF, es_vrf);
3031}
3032
3033/* deref and delete if there are no references */
3034void bgp_evpn_es_vrf_deref(struct bgp_evpn_es_evi *es_evi)
3035{
3036 struct bgp_evpn_es_vrf *es_vrf = es_evi->es_vrf;
3037
3038 if (!es_vrf)
3039 return;
3040
3041 if (BGP_DEBUG(evpn_mh, EVPN_MH_ES))
3042 zlog_debug("es-evi %s vni %u vrf %u de-ref",
3043 es_evi->es->esi_str, es_evi->vpn->vni,
3044 es_vrf->bgp_vrf->vrf_id);
3045
3046 es_evi->es_vrf = NULL;
3047 if (es_vrf->ref_cnt)
3048 --es_vrf->ref_cnt;
3049
3050 if (!es_vrf->ref_cnt)
3051 bgp_evpn_es_vrf_delete(es_vrf);
3052}
3053
3054/* find or create and reference */
3055void bgp_evpn_es_vrf_ref(struct bgp_evpn_es_evi *es_evi, struct bgp *bgp_vrf)
3056{
3057 struct bgp_evpn_es *es = es_evi->es;
3058 struct bgp_evpn_es_vrf *es_vrf = es_evi->es_vrf;
3059 struct bgp *old_bgp_vrf = NULL;
3060
3061 if (es_vrf)
3062 old_bgp_vrf = es_vrf->bgp_vrf;
3063
3064 if (old_bgp_vrf == bgp_vrf)
3065 return;
3066
3067 /* deref the old ES-VRF */
3068 bgp_evpn_es_vrf_deref(es_evi);
3069
3070 if (!bgp_vrf)
3071 return;
3072
3073 if (BGP_DEBUG(evpn_mh, EVPN_MH_ES))
3074 zlog_debug("es-evi %s vni %u vrf %u ref", es_evi->es->esi_str,
3075 es_evi->vpn->vni, bgp_vrf->vrf_id);
3076
3077 /* find-create the new ES-VRF */
3078 es_vrf = bgp_evpn_es_vrf_find(es, bgp_vrf);
3079 if (!es_vrf)
3080 es_vrf = bgp_evpn_es_vrf_create(es, bgp_vrf);
c589d847
AK
3081
3082 es_evi->es_vrf = es_vrf;
3083 ++es_vrf->ref_cnt;
3084}
3085
3086/* When the L2-VNI is associated with a L3-VNI/VRF update all the
3087 * associated ES-EVI entries
3088 */
3089void bgp_evpn_es_evi_vrf_deref(struct bgpevpn *vpn)
3090{
3091 struct bgp_evpn_es_evi *es_evi;
3092
3093 if (BGP_DEBUG(evpn_mh, EVPN_MH_ES))
3094 zlog_debug("es-vrf de-ref for vni %u", vpn->vni);
3095
3096 RB_FOREACH (es_evi, bgp_es_evi_rb_head, &vpn->es_evi_rb_tree)
3097 bgp_evpn_es_vrf_deref(es_evi);
3098}
3099void bgp_evpn_es_evi_vrf_ref(struct bgpevpn *vpn)
3100{
3101 struct bgp_evpn_es_evi *es_evi;
3102
3103 if (BGP_DEBUG(evpn_mh, EVPN_MH_ES))
3104 zlog_debug("es-vrf ref for vni %u", vpn->vni);
3105
3106 RB_FOREACH (es_evi, bgp_es_evi_rb_head, &vpn->es_evi_rb_tree)
3107 bgp_evpn_es_vrf_ref(es_evi, vpn->bgp_vrf);
3108}
3109
58bff4d1
AK
3110/* 1. If ES-VRF is not present install the host route with the exploded/flat
3111 * multi-path list.
3112 * 2. If ES-VRF is present -
3113 * - if L3NHG has not been activated for the ES-VRF (this could be because
3114 * all the PEs attached to the VRF are down) do not install the route
3115 * in zebra.
3116 * - if L3NHG has been activated install the route via that L3NHG
3117 */
3118void bgp_evpn_es_vrf_use_nhg(struct bgp *bgp_vrf, esi_t *esi, bool *use_l3nhg,
3119 bool *is_l3nhg_active,
3120 struct bgp_evpn_es_vrf **es_vrf_p)
36dd4574
AK
3121{
3122 struct bgp_evpn_es *es;
3123 struct bgp_evpn_es_vrf *es_vrf;
3124
3125 if (!bgp_mh_info->host_routes_use_l3nhg)
58bff4d1 3126 return;
36dd4574
AK
3127
3128 es = bgp_evpn_es_find(esi);
3129 if (!es)
58bff4d1 3130 return;
36dd4574
AK
3131
3132 es_vrf = bgp_evpn_es_vrf_find(es, bgp_vrf);
3133 if (!es_vrf)
58bff4d1
AK
3134 return;
3135
3136 *use_l3nhg = true;
3137 if (es_vrf->flags & BGP_EVPNES_VRF_NHG_ACTIVE)
3138 *is_l3nhg_active = true;
36dd4574
AK
3139 if (es_vrf_p)
3140 *es_vrf_p = es_vrf;
36dd4574
AK
3141}
3142
6348981a
AK
3143/* returns false if legacy-exploded mp needs to be used for route install */
3144bool bgp_evpn_path_es_use_nhg(struct bgp *bgp_vrf, struct bgp_path_info *pi,
3145 uint32_t *nhg_p)
3146{
3147 esi_t *esi;
36dd4574 3148 struct bgp_evpn_es_vrf *es_vrf = NULL;
6348981a
AK
3149 struct bgp_path_info *parent_pi;
3150 struct bgp_node *rn;
3151 struct prefix_evpn *evp;
3152 struct bgp_path_info *mpinfo;
58bff4d1
AK
3153 bool use_l3nhg = false;
3154 bool is_l3nhg_active = false;
6348981a
AK
3155
3156 *nhg_p = 0;
3157
9f570418
AK
3158 /* we don't support NHG for routes leaked from another VRF yet */
3159 if (pi->extra && pi->extra->bgp_orig)
3160 return false;
3161
6348981a
AK
3162 parent_pi = get_route_parent_evpn(pi);
3163 if (!parent_pi)
3164 return false;
3165
3166 rn = parent_pi->net;
3167 if (!rn)
3168 return false;
3169
3170 evp = (struct prefix_evpn *)&rn->p;
3171 if (evp->prefix.route_type != BGP_EVPN_MAC_IP_ROUTE)
3172 return false;
3173
6348981a
AK
3174 /* non-es path, use legacy-exploded multipath */
3175 esi = bgp_evpn_attr_get_esi(parent_pi->attr);
3176 if (!memcmp(esi, zero_esi, sizeof(*esi)))
3177 return false;
3178
43853c9c
SW
3179 /* we don't support NHG for d-vni yet */
3180 if (bgp_evpn_mpath_has_dvni(bgp_vrf, pi))
3181 return false;
3182
58bff4d1
AK
3183 bgp_evpn_es_vrf_use_nhg(bgp_vrf, esi, &use_l3nhg, &is_l3nhg_active,
3184 &es_vrf);
3185
36dd4574 3186 /* L3NHG support is disabled, use legacy-exploded multipath */
58bff4d1 3187 if (!use_l3nhg)
36dd4574
AK
3188 return false;
3189
3190 /* if the NHG has not been installed we cannot install the route yet,
3191 * return a 0-NHG to indicate that
6348981a 3192 */
58bff4d1 3193 if (!is_l3nhg_active)
6348981a
AK
3194 return true;
3195
3196 /* this needs to be set the v6NHG if v6route */
8bcb09a1 3197 if (is_evpn_prefix_ipaddr_v6(evp))
6348981a
AK
3198 *nhg_p = es_vrf->v6_nhg_id;
3199 else
3200 *nhg_p = es_vrf->nhg_id;
3201
3202 for (mpinfo = bgp_path_info_mpath_next(pi); mpinfo;
3203 mpinfo = bgp_path_info_mpath_next(mpinfo)) {
36dd4574 3204 /* if any of the paths have a different ESI we can't use
6348981a
AK
3205 * the NHG associated with the ES. fallback to legacy-exploded
3206 * multipath
3207 */
3208 if (memcmp(esi, bgp_evpn_attr_get_esi(mpinfo->attr),
3209 sizeof(*esi)))
3210 return false;
3211 }
3212
3213 return true;
3214}
3215
229587fb
AK
3216static void bgp_evpn_es_vrf_show_entry(struct vty *vty,
3217 struct bgp_evpn_es_vrf *es_vrf,
3218 json_object *json)
3219{
3220 struct bgp_evpn_es *es = es_vrf->es;
3221 struct bgp *bgp_vrf = es_vrf->bgp_vrf;
3222
3223 if (json) {
3224 json_object *json_types;
3225
3226 json_object_string_add(json, "esi", es->esi_str);
dc9ca53f 3227 json_object_string_add(json, "vrf", bgp_vrf->name_pretty);
229587fb
AK
3228
3229 if (es_vrf->flags & (BGP_EVPNES_VRF_NHG_ACTIVE)) {
3230 json_types = json_object_new_array();
3231 if (es_vrf->flags & BGP_EVPNES_VRF_NHG_ACTIVE)
3232 json_array_string_add(json_types, "active");
3233 json_object_object_add(json, "flags", json_types);
3234 }
3235
3236 json_object_int_add(json, "ipv4NHG", es_vrf->nhg_id);
3237 json_object_int_add(json, "ipv6NHG", es_vrf->v6_nhg_id);
3238 json_object_int_add(json, "refCount", es_vrf->ref_cnt);
3239 } else {
3240 char flags_str[4];
3241
3242 flags_str[0] = '\0';
3243 if (es_vrf->flags & BGP_EVPNES_VRF_NHG_ACTIVE)
3244 strlcat(flags_str, "A", sizeof(flags_str));
3245
3246 vty_out(vty, "%-30s %-15s %-5s %-8u %-8u %u\n", es->esi_str,
dc9ca53f 3247 bgp_vrf->name_pretty, flags_str, es_vrf->nhg_id,
229587fb
AK
3248 es_vrf->v6_nhg_id, es_vrf->ref_cnt);
3249 }
3250}
3251
3252static void bgp_evpn_es_vrf_show_es(struct vty *vty, json_object *json_array,
3253 struct bgp_evpn_es *es)
3254{
3255 json_object *json = NULL;
3256 struct listnode *es_vrf_node;
3257 struct bgp_evpn_es_vrf *es_vrf;
3258
3259 for (ALL_LIST_ELEMENTS_RO(es->es_vrf_list, es_vrf_node, es_vrf)) {
3260 /* create a separate json object for each ES-VRF */
3261 if (json_array)
3262 json = json_object_new_object();
3263 bgp_evpn_es_vrf_show_entry(vty, es_vrf, json);
3264 /* add ES-VRF to the json array */
3265 if (json_array)
3266 json_object_array_add(json_array, json);
3267 }
3268}
3269
3270/* Display all ES VRFs */
3271void bgp_evpn_es_vrf_show(struct vty *vty, bool uj, struct bgp_evpn_es *es)
3272{
3273 json_object *json_array = NULL;
3274
3275 if (uj) {
3276 /* create an array of ESs */
3277 json_array = json_object_new_array();
3278 } else {
3279 vty_out(vty, "ES-VRF Flags: A Active\n");
3280 vty_out(vty, "%-30s %-15s %-5s %-8s %-8s %s\n", "ESI", "VRF",
3281 "Flags", "IPv4-NHG", "IPv6-NHG", "Ref");
3282 }
3283
3284 if (es) {
3285 bgp_evpn_es_vrf_show_es(vty, json_array, es);
3286 } else {
3287 RB_FOREACH (es, bgp_es_rb_head, &bgp_mh_info->es_rb_tree)
3288 bgp_evpn_es_vrf_show_es(vty, json_array, es);
3289 }
3290
3291 /* print the array of json-ESs */
c48349e3 3292 if (uj)
75eeda93 3293 vty_json(vty, json_array);
229587fb
AK
3294}
3295
3296/* Display specific ES VRF */
3297void bgp_evpn_es_vrf_show_esi(struct vty *vty, esi_t *esi, bool uj)
3298{
3299 struct bgp_evpn_es *es;
3300
3301 es = bgp_evpn_es_find(esi);
3302 if (es) {
3303 bgp_evpn_es_vrf_show(vty, uj, es);
3304 } else {
3305 if (!uj)
3306 vty_out(vty, "ESI not found\n");
3307 }
3308}
3309
c44ab6f1
AK
3310/*****************************************************************************/
3311/* Ethernet Segment to EVI association -
3312 * 1. The ES-EVI entry is maintained as a RB tree per L2-VNI
3313 * (bgpevpn->es_evi_rb_tree).
3314 * 2. Each local ES-EVI entry is rxed from zebra and then used by BGP to
3315 * advertises an EAD-EVI (Type-1 EVPN) route
3316 * 3. The remote ES-EVI is created when a bgp_evpn_es_evi_vtep references
3317 * it.
3318 */
3319
3320/* A list of remote VTEPs is maintained for each ES-EVI. This list includes -
3321 * 1. VTEPs for which we have imported the EAD-per-ES Type1 route
3322 * 2. VTEPs for which we have imported the EAD-per-EVI Type1 route
3323 * VTEPs for which both routes have been rxed are activated. Activation
3324 * creates a NHG in the parent ES.
3325 */
3326static int bgp_evpn_es_evi_vtep_cmp(void *p1, void *p2)
3327{
3328 const struct bgp_evpn_es_evi_vtep *evi_vtep1 = p1;
3329 const struct bgp_evpn_es_evi_vtep *evi_vtep2 = p2;
3330
3331 return evi_vtep1->vtep_ip.s_addr - evi_vtep2->vtep_ip.s_addr;
3332}
3333
3334static struct bgp_evpn_es_evi_vtep *bgp_evpn_es_evi_vtep_new(
3335 struct bgp_evpn_es_evi *es_evi, struct in_addr vtep_ip)
3336{
3337 struct bgp_evpn_es_evi_vtep *evi_vtep;
3338
3339 evi_vtep = XCALLOC(MTYPE_BGP_EVPN_ES_EVI_VTEP, sizeof(*evi_vtep));
3340
3341 evi_vtep->es_evi = es_evi;
3342 evi_vtep->vtep_ip.s_addr = vtep_ip.s_addr;
3343 listnode_init(&evi_vtep->es_evi_listnode, evi_vtep);
3344 listnode_add_sort(es_evi->es_evi_vtep_list, &evi_vtep->es_evi_listnode);
3345
3346 return evi_vtep;
3347}
3348
3349static void bgp_evpn_es_evi_vtep_free(struct bgp_evpn_es_evi_vtep *evi_vtep)
3350{
3351 struct bgp_evpn_es_evi *es_evi = evi_vtep->es_evi;
3352
3353 if (evi_vtep->flags & (BGP_EVPN_EVI_VTEP_EAD))
3354 /* as long as there is some reference we can't free it */
3355 return;
3356
3357 list_delete_node(es_evi->es_evi_vtep_list, &evi_vtep->es_evi_listnode);
3358 XFREE(MTYPE_BGP_EVPN_ES_EVI_VTEP, evi_vtep);
3359}
3360
3361/* check if VTEP is already part of the list */
3362static struct bgp_evpn_es_evi_vtep *bgp_evpn_es_evi_vtep_find(
3363 struct bgp_evpn_es_evi *es_evi, struct in_addr vtep_ip)
3364{
3365 struct listnode *node = NULL;
3366 struct bgp_evpn_es_evi_vtep *evi_vtep;
3367
3368 for (ALL_LIST_ELEMENTS_RO(es_evi->es_evi_vtep_list, node, evi_vtep)) {
3369 if (evi_vtep->vtep_ip.s_addr == vtep_ip.s_addr)
3370 return evi_vtep;
3371 }
3372 return NULL;
3373}
3374
3375/* A VTEP can be added as "active" attach to an ES if EAD-per-ES and
3376 * EAD-per-EVI routes are rxed from it.
3377 */
3378static void bgp_evpn_es_evi_vtep_re_eval_active(struct bgp *bgp,
3379 struct bgp_evpn_es_evi_vtep *evi_vtep)
3380{
3381 bool old_active;
3382 bool new_active;
fe8293c3 3383 uint32_t ead_activity_flags;
c44ab6f1 3384
d4980edf 3385 old_active = CHECK_FLAG(evi_vtep->flags, BGP_EVPN_EVI_VTEP_ACTIVE);
c44ab6f1 3386
fe8293c3
AK
3387 if (bgp_mh_info->ead_evi_rx)
3388 /* Both EAD-per-ES and EAD-per-EVI routes must be rxed from a PE
3389 * before it can be activated.
3390 */
3391 ead_activity_flags = BGP_EVPN_EVI_VTEP_EAD;
3392 else
3393 /* EAD-per-ES is sufficent to activate the PE */
3394 ead_activity_flags = BGP_EVPN_EVI_VTEP_EAD_PER_ES;
3395
3396 if ((evi_vtep->flags & ead_activity_flags) == ead_activity_flags)
c44ab6f1
AK
3397 SET_FLAG(evi_vtep->flags, BGP_EVPN_EVI_VTEP_ACTIVE);
3398 else
3399 UNSET_FLAG(evi_vtep->flags, BGP_EVPN_EVI_VTEP_ACTIVE);
3400
d4980edf 3401 new_active = CHECK_FLAG(evi_vtep->flags, BGP_EVPN_EVI_VTEP_ACTIVE);
c44ab6f1
AK
3402
3403 if (old_active == new_active)
3404 return;
3405
3406 if (BGP_DEBUG(evpn_mh, EVPN_MH_ES))
23d0a753
DA
3407 zlog_debug("es %s evi %u vtep %pI4 %s",
3408 evi_vtep->es_evi->es->esi_str,
3409 evi_vtep->es_evi->vpn->vni, &evi_vtep->vtep_ip,
3410 new_active ? "active" : "inactive");
c44ab6f1
AK
3411
3412 /* add VTEP to parent es */
bce8bf58 3413 if (new_active)
3414 evi_vtep->es_vtep = bgp_evpn_es_vtep_add(
3415 bgp, evi_vtep->es_evi->es, evi_vtep->vtep_ip,
3416 false /*esr*/, 0, 0);
3417 else {
c44ab6f1
AK
3418 if (evi_vtep->es_vtep) {
3419 bgp_evpn_es_vtep_do_del(bgp, evi_vtep->es_vtep,
3420 false /*esr*/);
3421 evi_vtep->es_vtep = NULL;
3422 }
3423 }
3424 /* queue up the parent es for background consistency checks */
3425 bgp_evpn_es_cons_checks_pend_add(evi_vtep->es_evi->es);
3426}
3427
3428static void bgp_evpn_es_evi_vtep_add(struct bgp *bgp,
3429 struct bgp_evpn_es_evi *es_evi, struct in_addr vtep_ip,
3430 bool ead_es)
3431{
3432 struct bgp_evpn_es_evi_vtep *evi_vtep;
3433
3434 evi_vtep = bgp_evpn_es_evi_vtep_find(es_evi, vtep_ip);
3435
3436 if (!evi_vtep)
3437 evi_vtep = bgp_evpn_es_evi_vtep_new(es_evi, vtep_ip);
3438
3439 if (BGP_DEBUG(evpn_mh, EVPN_MH_ES))
23d0a753
DA
3440 zlog_debug("add es %s evi %u vtep %pI4 %s",
3441 evi_vtep->es_evi->es->esi_str,
3442 evi_vtep->es_evi->vpn->vni, &evi_vtep->vtep_ip,
3443 ead_es ? "ead_es" : "ead_evi");
c44ab6f1
AK
3444
3445 if (ead_es)
3446 SET_FLAG(evi_vtep->flags, BGP_EVPN_EVI_VTEP_EAD_PER_ES);
3447 else
3448 SET_FLAG(evi_vtep->flags, BGP_EVPN_EVI_VTEP_EAD_PER_EVI);
3449
3450 bgp_evpn_es_evi_vtep_re_eval_active(bgp, evi_vtep);
3451}
3452
3453static void bgp_evpn_es_evi_vtep_del(struct bgp *bgp,
3454 struct bgp_evpn_es_evi *es_evi, struct in_addr vtep_ip,
3455 bool ead_es)
3456{
3457 struct bgp_evpn_es_evi_vtep *evi_vtep;
3458
3459 evi_vtep = bgp_evpn_es_evi_vtep_find(es_evi, vtep_ip);
3460 if (!evi_vtep)
3461 return;
3462
3463 if (BGP_DEBUG(evpn_mh, EVPN_MH_ES))
23d0a753
DA
3464 zlog_debug("del es %s evi %u vtep %pI4 %s",
3465 evi_vtep->es_evi->es->esi_str,
3466 evi_vtep->es_evi->vpn->vni, &evi_vtep->vtep_ip,
3467 ead_es ? "ead_es" : "ead_evi");
c44ab6f1
AK
3468
3469 if (ead_es)
3470 UNSET_FLAG(evi_vtep->flags, BGP_EVPN_EVI_VTEP_EAD_PER_ES);
3471 else
3472 UNSET_FLAG(evi_vtep->flags, BGP_EVPN_EVI_VTEP_EAD_PER_EVI);
3473
3474 bgp_evpn_es_evi_vtep_re_eval_active(bgp, evi_vtep);
3475 bgp_evpn_es_evi_vtep_free(evi_vtep);
3476}
3477
3478/* compare ES-IDs for the ES-EVI RB tree maintained per-VNI */
3479static int bgp_es_evi_rb_cmp(const struct bgp_evpn_es_evi *es_evi1,
3480 const struct bgp_evpn_es_evi *es_evi2)
3481{
3482 return memcmp(&es_evi1->es->esi, &es_evi2->es->esi, ESI_BYTES);
3483}
3484RB_GENERATE(bgp_es_evi_rb_head, bgp_evpn_es_evi, rb_node, bgp_es_evi_rb_cmp);
3485
3486/* find the ES-EVI in the per-L2-VNI RB tree */
3487static struct bgp_evpn_es_evi *bgp_evpn_es_evi_find(struct bgp_evpn_es *es,
3488 struct bgpevpn *vpn)
3489{
3490 struct bgp_evpn_es_evi es_evi;
3491
3492 es_evi.es = es;
3493
3494 return RB_FIND(bgp_es_evi_rb_head, &vpn->es_evi_rb_tree, &es_evi);
3495}
3496
3497/* allocate a new ES-EVI and insert it into the per-L2-VNI and per-ES
3498 * tables.
3499 */
3500static struct bgp_evpn_es_evi *bgp_evpn_es_evi_new(struct bgp_evpn_es *es,
3501 struct bgpevpn *vpn)
3502{
3503 struct bgp_evpn_es_evi *es_evi;
3504
3505 es_evi = XCALLOC(MTYPE_BGP_EVPN_ES_EVI, sizeof(*es_evi));
3506
3507 es_evi->es = es;
3508 es_evi->vpn = vpn;
3509
3510 /* Initialise the VTEP list */
3511 es_evi->es_evi_vtep_list = list_new();
3512 listset_app_node_mem(es_evi->es_evi_vtep_list);
3513 es_evi->es_evi_vtep_list->cmp = bgp_evpn_es_evi_vtep_cmp;
3514
3515 /* insert into the VNI-ESI rb tree */
92809558 3516 RB_INSERT(bgp_es_evi_rb_head, &vpn->es_evi_rb_tree, es_evi);
c44ab6f1
AK
3517
3518 /* add to the ES's VNI list */
3519 listnode_init(&es_evi->es_listnode, es_evi);
3520 listnode_add(es->es_evi_list, &es_evi->es_listnode);
3521
c589d847
AK
3522 bgp_evpn_es_vrf_ref(es_evi, vpn->bgp_vrf);
3523
c44ab6f1
AK
3524 return es_evi;
3525}
3526
3527/* remove the ES-EVI from the per-L2-VNI and per-ES tables and free
3528 * up the memory.
3529 */
333ad64b
AK
3530static struct bgp_evpn_es_evi *
3531bgp_evpn_es_evi_free(struct bgp_evpn_es_evi *es_evi)
c44ab6f1
AK
3532{
3533 struct bgp_evpn_es *es = es_evi->es;
3534 struct bgpevpn *vpn = es_evi->vpn;
3535
3536 /* cannot free the element as long as there is a local or remote
3537 * reference
3538 */
3539 if (es_evi->flags & (BGP_EVPNES_EVI_LOCAL | BGP_EVPNES_EVI_REMOTE))
333ad64b 3540 return es_evi;
7b0db0e4 3541 bgp_evpn_es_frag_evi_del(es_evi, false);
c589d847
AK
3542 bgp_evpn_es_vrf_deref(es_evi);
3543
c44ab6f1
AK
3544 /* remove from the ES's VNI list */
3545 list_delete_node(es->es_evi_list, &es_evi->es_listnode);
3546
3547 /* remove from the VNI-ESI rb tree */
3548 RB_REMOVE(bgp_es_evi_rb_head, &vpn->es_evi_rb_tree, es_evi);
3549
3550 /* free the VTEP list */
3551 list_delete(&es_evi->es_evi_vtep_list);
3552
3553 /* remove from the VNI-ESI rb tree */
3554 XFREE(MTYPE_BGP_EVPN_ES_EVI, es_evi);
333ad64b
AK
3555
3556 return NULL;
c44ab6f1
AK
3557}
3558
3559/* init local info associated with the ES-EVI */
3560static void bgp_evpn_es_evi_local_info_set(struct bgp_evpn_es_evi *es_evi)
3561{
3562 struct bgpevpn *vpn = es_evi->vpn;
3563
3564 if (CHECK_FLAG(es_evi->flags, BGP_EVPNES_EVI_LOCAL))
3565 return;
3566
3567 SET_FLAG(es_evi->flags, BGP_EVPNES_EVI_LOCAL);
3568 listnode_init(&es_evi->l2vni_listnode, es_evi);
3569 listnode_add(vpn->local_es_evi_list, &es_evi->l2vni_listnode);
7b0db0e4 3570 bgp_evpn_es_frag_evi_add(es_evi);
c44ab6f1
AK
3571}
3572
3573/* clear any local info associated with the ES-EVI */
333ad64b
AK
3574static struct bgp_evpn_es_evi *
3575bgp_evpn_es_evi_local_info_clear(struct bgp_evpn_es_evi *es_evi)
c44ab6f1
AK
3576{
3577 struct bgpevpn *vpn = es_evi->vpn;
3578
c44ab6f1
AK
3579 UNSET_FLAG(es_evi->flags, BGP_EVPNES_EVI_LOCAL);
3580 list_delete_node(vpn->local_es_evi_list, &es_evi->l2vni_listnode);
3581
333ad64b 3582 return bgp_evpn_es_evi_free(es_evi);
c44ab6f1
AK
3583}
3584
3585/* eval remote info associated with the ES */
3586static void bgp_evpn_es_evi_remote_info_re_eval(struct bgp_evpn_es_evi *es_evi)
3587{
3588 struct bgp_evpn_es *es = es_evi->es;
3589
3590 /* if there are remote VTEPs the ES-EVI is classified as "remote" */
3591 if (listcount(es_evi->es_evi_vtep_list)) {
3592 if (!CHECK_FLAG(es_evi->flags, BGP_EVPNES_EVI_REMOTE)) {
3593 SET_FLAG(es_evi->flags, BGP_EVPNES_EVI_REMOTE);
3594 ++es->remote_es_evi_cnt;
3595 /* set remote on the parent es */
3596 bgp_evpn_es_remote_info_re_eval(es);
3597 }
3598 } else {
3599 if (CHECK_FLAG(es_evi->flags, BGP_EVPNES_EVI_REMOTE)) {
3600 UNSET_FLAG(es_evi->flags, BGP_EVPNES_EVI_REMOTE);
3601 if (es->remote_es_evi_cnt)
3602 --es->remote_es_evi_cnt;
3603 bgp_evpn_es_evi_free(es_evi);
3604 /* check if "remote" can be cleared from the
3605 * parent es.
3606 */
3607 bgp_evpn_es_remote_info_re_eval(es);
3608 }
3609 }
3610}
3611
333ad64b
AK
3612static struct bgp_evpn_es_evi *
3613bgp_evpn_local_es_evi_do_del(struct bgp_evpn_es_evi *es_evi)
c44ab6f1
AK
3614{
3615 struct prefix_evpn p;
3616 struct bgp_evpn_es *es = es_evi->es;
3617 struct bgp *bgp;
3618
3619 if (!CHECK_FLAG(es_evi->flags, BGP_EVPNES_EVI_LOCAL))
333ad64b 3620 return es_evi;
c44ab6f1
AK
3621
3622 if (BGP_DEBUG(evpn_mh, EVPN_MH_ES))
3623 zlog_debug("del local es %s evi %u",
3624 es_evi->es->esi_str,
3625 es_evi->vpn->vni);
3626
3627 bgp = bgp_get_evpn();
3628
7b0db0e4
AK
3629 /* remove the es_evi from the es_frag before sending the update */
3630 bgp_evpn_es_frag_evi_del(es_evi, true);
c44ab6f1
AK
3631 if (bgp) {
3632 /* update EAD-ES with new list of VNIs */
7b0db0e4
AK
3633 if (bgp_evpn_local_es_is_active(es))
3634 bgp_evpn_ead_es_route_update(bgp, es);
c44ab6f1
AK
3635
3636 /* withdraw and delete EAD-EVI */
3637 if (CHECK_FLAG(es->flags, BGP_EVPNES_ADV_EVI)) {
3638 build_evpn_type1_prefix(&p, BGP_EVPN_AD_EVI_ETH_TAG,
3639 &es->esi, es->originator_ip);
7b0db0e4
AK
3640 if (bgp_evpn_ead_evi_route_delete(bgp, es, es_evi->vpn,
3641 &p))
c44ab6f1
AK
3642 flog_err(EC_BGP_EVPN_ROUTE_DELETE,
3643 "%u: EAD-EVI route deletion failure for ESI %s VNI %u",
3644 bgp->vrf_id, es->esi_str,
3645 es_evi->vpn->vni);
3646 }
3647 }
3648
333ad64b 3649 return bgp_evpn_es_evi_local_info_clear(es_evi);
c44ab6f1
AK
3650}
3651
3652int bgp_evpn_local_es_evi_del(struct bgp *bgp, esi_t *esi, vni_t vni)
3653{
3654 struct bgpevpn *vpn;
3655 struct bgp_evpn_es *es;
3656 struct bgp_evpn_es_evi *es_evi;
3657 char buf[ESI_STR_LEN];
3658
3659 es = bgp_evpn_es_find(esi);
3660 if (!es) {
3661 flog_err(
3662 EC_BGP_ES_CREATE,
3663 "%u: Failed to deref VNI %d from ESI %s; ES not present",
3664 bgp->vrf_id, vni,
3665 esi_to_str(esi, buf, sizeof(buf)));
3666 return -1;
3667 }
3668
3669 vpn = bgp_evpn_lookup_vni(bgp, vni);
3670 if (!vpn) {
3671 flog_err(
3672 EC_BGP_ES_CREATE,
3673 "%u: Failed to deref VNI %d from ESI %s; VNI not present",
3674 bgp->vrf_id, vni, es->esi_str);
3675 return -1;
3676 }
3677
3678 es_evi = bgp_evpn_es_evi_find(es, vpn);
3679 if (!es_evi) {
3680 flog_err(
3681 EC_BGP_ES_CREATE,
3682 "%u: Failed to deref VNI %d from ESI %s; ES-VNI not present",
3683 bgp->vrf_id, vni, es->esi_str);
3684 return -1;
3685 }
3686
3687 bgp_evpn_local_es_evi_do_del(es_evi);
3688 return 0;
3689}
3690
3691/* Create ES-EVI and advertise the corresponding EAD routes */
3692int bgp_evpn_local_es_evi_add(struct bgp *bgp, esi_t *esi, vni_t vni)
3693{
3694 struct bgpevpn *vpn;
3695 struct prefix_evpn p;
3696 struct bgp_evpn_es *es;
3697 struct bgp_evpn_es_evi *es_evi;
3698 char buf[ESI_STR_LEN];
3699
3700 es = bgp_evpn_es_find(esi);
3701 if (!es) {
3702 flog_err(
3703 EC_BGP_ES_CREATE,
3704 "%u: Failed to associate VNI %d with ESI %s; ES not present",
3705 bgp->vrf_id, vni,
3706 esi_to_str(esi, buf, sizeof(buf)));
3707 return -1;
3708 }
3709
3710 vpn = bgp_evpn_lookup_vni(bgp, vni);
3711 if (!vpn) {
3712 flog_err(
3713 EC_BGP_ES_CREATE,
3714 "%u: Failed to associate VNI %d with ESI %s; VNI not present",
3715 bgp->vrf_id, vni, es->esi_str);
3716 return -1;
3717 }
3718
3719 if (BGP_DEBUG(evpn_mh, EVPN_MH_ES))
3720 zlog_debug("add local es %s evi %u",
3721 es->esi_str, vni);
3722
3723 es_evi = bgp_evpn_es_evi_find(es, vpn);
3724
3725 if (es_evi) {
3726 if (CHECK_FLAG(es_evi->flags, BGP_EVPNES_EVI_LOCAL))
3727 /* dup */
3728 return 0;
49540e50 3729 } else
c44ab6f1 3730 es_evi = bgp_evpn_es_evi_new(es, vpn);
c44ab6f1
AK
3731
3732 bgp_evpn_es_evi_local_info_set(es_evi);
3733
3734 /* generate an EAD-EVI for this new VNI */
c44ab6f1 3735 if (CHECK_FLAG(es->flags, BGP_EVPNES_ADV_EVI)) {
fe8293c3
AK
3736 build_evpn_type1_prefix(&p, BGP_EVPN_AD_EVI_ETH_TAG, &es->esi,
3737 es->originator_ip);
7b0db0e4 3738 bgp_evpn_ead_evi_route_update(bgp, es, vpn, &p);
c44ab6f1
AK
3739 }
3740
3741 /* update EAD-ES */
7b0db0e4
AK
3742 if (bgp_evpn_local_es_is_active(es))
3743 bgp_evpn_ead_es_route_update(bgp, es);
c44ab6f1
AK
3744
3745 return 0;
3746}
3747
3748/* Add remote ES-EVI entry. This is actually the remote VTEP add and the
3749 * ES-EVI is implicity created on first VTEP's reference.
3750 */
3751int bgp_evpn_remote_es_evi_add(struct bgp *bgp, struct bgpevpn *vpn,
3752 const struct prefix_evpn *p)
3753{
3754 char buf[ESI_STR_LEN];
3755 struct bgp_evpn_es *es;
3756 struct bgp_evpn_es_evi *es_evi;
3757 bool ead_es;
3758 const esi_t *esi = &p->prefix.ead_addr.esi;
3759
3760 if (!vpn)
3761 /* local EAD-ES need not be sent back to zebra */
3762 return 0;
3763
3764 if (BGP_DEBUG(evpn_mh, EVPN_MH_ES))
23d0a753
DA
3765 zlog_debug("add remote %s es %s evi %u vtep %pI4",
3766 p->prefix.ead_addr.eth_tag ? "ead-es" : "ead-evi",
3767 esi_to_str(esi, buf, sizeof(buf)), vpn->vni,
3768 &p->prefix.ead_addr.ip.ipaddr_v4);
c44ab6f1
AK
3769
3770 es = bgp_evpn_es_find(esi);
573eeb2b 3771 if (!es)
c44ab6f1 3772 es = bgp_evpn_es_new(bgp, esi);
c44ab6f1
AK
3773
3774 es_evi = bgp_evpn_es_evi_find(es, vpn);
49540e50 3775 if (!es_evi)
c44ab6f1 3776 es_evi = bgp_evpn_es_evi_new(es, vpn);
c44ab6f1
AK
3777
3778 ead_es = !!p->prefix.ead_addr.eth_tag;
3779 bgp_evpn_es_evi_vtep_add(bgp, es_evi, p->prefix.ead_addr.ip.ipaddr_v4,
3780 ead_es);
3781
3782 bgp_evpn_es_evi_remote_info_re_eval(es_evi);
3783 return 0;
3784}
3785
3786/* A remote VTEP has withdrawn. The es-evi-vtep will be deleted and the
3787 * parent es-evi freed up implicitly in last VTEP's deref.
3788 */
3789int bgp_evpn_remote_es_evi_del(struct bgp *bgp, struct bgpevpn *vpn,
3790 const struct prefix_evpn *p)
3791{
3792 char buf[ESI_STR_LEN];
3793 struct bgp_evpn_es *es;
3794 struct bgp_evpn_es_evi *es_evi;
3795 bool ead_es;
3796
3797 if (!vpn)
3798 /* local EAD-ES need not be sent back to zebra */
3799 return 0;
3800
3801 if (BGP_DEBUG(evpn_mh, EVPN_MH_ES))
23d0a753
DA
3802 zlog_debug(
3803 "del remote %s es %s evi %u vtep %pI4",
3804 p->prefix.ead_addr.eth_tag ? "ead-es" : "ead-evi",
3805 esi_to_str(&p->prefix.ead_addr.esi, buf, sizeof(buf)),
3806 vpn->vni, &p->prefix.ead_addr.ip.ipaddr_v4);
c44ab6f1
AK
3807
3808 es = bgp_evpn_es_find(&p->prefix.ead_addr.esi);
2eef4f20
AK
3809 if (!es) {
3810 if (BGP_DEBUG(evpn_mh, EVPN_MH_ES))
671ec576 3811 zlog_debug(
3812 "del remote %s es %s evi %u vtep %pI4, NO es",
3813 p->prefix.ead_addr.eth_tag ? "ead-es"
3814 : "ead-evi",
3815 esi_to_str(&p->prefix.ead_addr.esi, buf,
3816 sizeof(buf)),
3817 vpn->vni, &p->prefix.ead_addr.ip.ipaddr_v4);
c44ab6f1 3818 return 0;
2eef4f20 3819 }
c44ab6f1 3820 es_evi = bgp_evpn_es_evi_find(es, vpn);
2eef4f20
AK
3821 if (!es_evi) {
3822 if (BGP_DEBUG(evpn_mh, EVPN_MH_ES))
3823 zlog_debug(
3824 "del remote %s es %s evi %u vtep %pI4, NO es-evi",
3825 p->prefix.ead_addr.eth_tag ? "ead-es"
3826 : "ead-evi",
3827 esi_to_str(&p->prefix.ead_addr.esi, buf,
3828 sizeof(buf)),
3829 vpn->vni,
3830 &p->prefix.ead_addr.ip.ipaddr_v4);
c44ab6f1 3831 return 0;
2eef4f20 3832 }
c44ab6f1
AK
3833
3834 ead_es = !!p->prefix.ead_addr.eth_tag;
3835 bgp_evpn_es_evi_vtep_del(bgp, es_evi, p->prefix.ead_addr.ip.ipaddr_v4,
3836 ead_es);
3837 bgp_evpn_es_evi_remote_info_re_eval(es_evi);
3838 return 0;
3839}
3840
333ad64b
AK
3841/* If a VNI is being deleted we need to force del all remote VTEPs */
3842static void bgp_evpn_remote_es_evi_flush(struct bgp_evpn_es_evi *es_evi)
3843{
3844 struct listnode *node = NULL;
3845 struct listnode *nnode = NULL;
3846 struct bgp_evpn_es_evi_vtep *evi_vtep;
3847 struct bgp *bgp;
3848
3849 bgp = bgp_get_evpn();
3850 if (!bgp)
3851 return;
3852
3853 /* delete all VTEPs */
3854 for (ALL_LIST_ELEMENTS(es_evi->es_evi_vtep_list, node, nnode,
3855 evi_vtep)) {
3856 evi_vtep->flags &= ~(BGP_EVPN_EVI_VTEP_EAD_PER_ES
3857 | BGP_EVPN_EVI_VTEP_EAD_PER_EVI);
3858 bgp_evpn_es_evi_vtep_re_eval_active(bgp, evi_vtep);
3859 bgp_evpn_es_evi_vtep_free(evi_vtep);
3860 }
3861 /* delete the EVI */
3862 bgp_evpn_es_evi_remote_info_re_eval(es_evi);
3863}
3864
c44ab6f1
AK
3865/* Initialize the ES tables maintained per-L2_VNI */
3866void bgp_evpn_vni_es_init(struct bgpevpn *vpn)
3867{
3868 /* Initialize the ES-EVI RB tree */
3869 RB_INIT(bgp_es_evi_rb_head, &vpn->es_evi_rb_tree);
3870
3871 /* Initialize the local list maintained for quick walks by type */
3872 vpn->local_es_evi_list = list_new();
3873 listset_app_node_mem(vpn->local_es_evi_list);
3874}
3875
3876/* Cleanup the ES info maintained per-L2_VNI */
3877void bgp_evpn_vni_es_cleanup(struct bgpevpn *vpn)
3878{
3879 struct bgp_evpn_es_evi *es_evi;
3880 struct bgp_evpn_es_evi *es_evi_next;
3881
3882 RB_FOREACH_SAFE(es_evi, bgp_es_evi_rb_head,
3883 &vpn->es_evi_rb_tree, es_evi_next) {
333ad64b
AK
3884 es_evi = bgp_evpn_local_es_evi_do_del(es_evi);
3885 if (es_evi)
3886 bgp_evpn_remote_es_evi_flush(es_evi);
c44ab6f1
AK
3887 }
3888
3889 list_delete(&vpn->local_es_evi_list);
3890}
3891
3892static char *bgp_evpn_es_evi_vteps_str(char *vtep_str,
9e0c2fd1
AK
3893 struct bgp_evpn_es_evi *es_evi,
3894 uint8_t vtep_str_size)
c44ab6f1
AK
3895{
3896 char vtep_flag_str[BGP_EVPN_FLAG_STR_SZ];
3897 struct listnode *node;
3898 struct bgp_evpn_es_evi_vtep *evi_vtep;
3899 bool first = true;
07380148 3900 char ip_buf[INET_ADDRSTRLEN];
c44ab6f1
AK
3901
3902 vtep_str[0] = '\0';
3903 for (ALL_LIST_ELEMENTS_RO(es_evi->es_evi_vtep_list, node, evi_vtep)) {
3904 vtep_flag_str[0] = '\0';
3905 if (evi_vtep->flags & BGP_EVPN_EVI_VTEP_EAD_PER_ES)
9e0c2fd1 3906 strlcat(vtep_flag_str, "E", sizeof(vtep_flag_str));
c44ab6f1 3907 if (evi_vtep->flags & BGP_EVPN_EVI_VTEP_EAD_PER_EVI)
9e0c2fd1 3908 strlcat(vtep_flag_str, "V", sizeof(vtep_flag_str));
c44ab6f1 3909
9e0c2fd1
AK
3910 if (!strnlen(vtep_flag_str, sizeof(vtep_flag_str)))
3911 strlcpy(vtep_flag_str, "-", sizeof(vtep_flag_str));
3912 if (first)
c44ab6f1 3913 first = false;
9e0c2fd1
AK
3914 else
3915 strlcat(vtep_str, ",", vtep_str_size);
23d0a753 3916 strlcat(vtep_str,
ec779825
AK
3917 inet_ntop(AF_INET, &evi_vtep->vtep_ip, ip_buf,
3918 sizeof(ip_buf)),
23d0a753 3919 vtep_str_size);
9e0c2fd1
AK
3920 strlcat(vtep_str, "(", vtep_str_size);
3921 strlcat(vtep_str, vtep_flag_str, vtep_str_size);
3922 strlcat(vtep_str, ")", vtep_str_size);
c44ab6f1
AK
3923 }
3924
3925 return vtep_str;
3926}
3927
3928static void bgp_evpn_es_evi_json_vtep_fill(json_object *json_vteps,
3929 struct bgp_evpn_es_evi_vtep *evi_vtep)
3930{
3931 json_object *json_vtep_entry;
3932 json_object *json_flags;
3933
3934 json_vtep_entry = json_object_new_object();
3935
c949c771
DA
3936 json_object_string_addf(json_vtep_entry, "vtep_ip", "%pI4",
3937 &evi_vtep->vtep_ip);
c44ab6f1
AK
3938 if (evi_vtep->flags & (BGP_EVPN_EVI_VTEP_EAD_PER_ES |
3939 BGP_EVPN_EVI_VTEP_EAD_PER_EVI)) {
3940 json_flags = json_object_new_array();
3941 if (evi_vtep->flags & BGP_EVPN_EVI_VTEP_EAD_PER_ES)
3942 json_array_string_add(json_flags, "ead-per-es");
3943 if (evi_vtep->flags & BGP_EVPN_EVI_VTEP_EAD_PER_EVI)
b37ff319 3944 json_array_string_add(json_flags, "ead-per-evi");
c44ab6f1
AK
3945 json_object_object_add(json_vtep_entry,
3946 "flags", json_flags);
3947 }
3948
3949 json_object_array_add(json_vteps,
3950 json_vtep_entry);
3951}
3952
3953static void bgp_evpn_es_evi_show_entry(struct vty *vty,
3954 struct bgp_evpn_es_evi *es_evi, json_object *json)
3955{
3956 struct listnode *node;
3957 struct bgp_evpn_es_evi_vtep *evi_vtep;
3958
3959 if (json) {
3960 json_object *json_vteps;
3961 json_object *json_types;
3962
3963 json_object_string_add(json, "esi", es_evi->es->esi_str);
f7b60a3a
PG
3964 if (es_evi->vpn)
3965 json_object_int_add(json, "vni", es_evi->vpn->vni);
c44ab6f1
AK
3966
3967 if (es_evi->flags & (BGP_EVPNES_EVI_LOCAL |
3968 BGP_EVPNES_EVI_REMOTE)) {
3969 json_types = json_object_new_array();
3970 if (es_evi->flags & BGP_EVPNES_EVI_LOCAL)
3971 json_array_string_add(json_types, "local");
3972 if (es_evi->flags & BGP_EVPNES_EVI_REMOTE)
3973 json_array_string_add(json_types, "remote");
3974 json_object_object_add(json, "type", json_types);
3975 }
3976
3977 if (listcount(es_evi->es_evi_vtep_list)) {
3978 json_vteps = json_object_new_array();
3979 for (ALL_LIST_ELEMENTS_RO(es_evi->es_evi_vtep_list,
3980 node, evi_vtep)) {
3981 bgp_evpn_es_evi_json_vtep_fill(json_vteps,
3982 evi_vtep);
3983 }
3984 json_object_object_add(json, "vteps", json_vteps);
3985 }
3986 } else {
3987 char type_str[4];
3988 char vtep_str[ES_VTEP_LIST_STR_SZ + BGP_EVPN_VTEPS_FLAG_STR_SZ];
3989
3990 type_str[0] = '\0';
3991 if (es_evi->flags & BGP_EVPNES_EVI_LOCAL)
9e0c2fd1 3992 strlcat(type_str, "L", sizeof(type_str));
c44ab6f1 3993 if (es_evi->flags & BGP_EVPNES_EVI_REMOTE)
9e0c2fd1 3994 strlcat(type_str, "R", sizeof(type_str));
c44ab6f1 3995 if (es_evi->flags & BGP_EVPNES_EVI_INCONS_VTEP_LIST)
9e0c2fd1 3996 strlcat(type_str, "I", sizeof(type_str));
c44ab6f1 3997
9e0c2fd1 3998 bgp_evpn_es_evi_vteps_str(vtep_str, es_evi, sizeof(vtep_str));
c44ab6f1
AK
3999
4000 vty_out(vty, "%-8d %-30s %-5s %s\n",
4001 es_evi->vpn->vni, es_evi->es->esi_str,
4002 type_str, vtep_str);
4003 }
4004}
4005
4006static void bgp_evpn_es_evi_show_entry_detail(struct vty *vty,
4007 struct bgp_evpn_es_evi *es_evi, json_object *json)
4008{
4a8cd6ad
PG
4009 enum asnotation_mode mode;
4010
4011 mode = bgp_get_asnotation(es_evi->vpn->bgp_vrf);
4012
c44ab6f1
AK
4013 if (json) {
4014 json_object *json_flags;
4015
4016 /* Add the "brief" info first */
4017 bgp_evpn_es_evi_show_entry(vty, es_evi, json);
7b0db0e4 4018 if (es_evi->es_frag)
4a8cd6ad
PG
4019 json_object_string_addf(json, "esFragmentRd",
4020 BGP_RD_AS_FORMAT(mode),
c4f64ea9 4021 &es_evi->es_frag->prd);
c44ab6f1
AK
4022 if (es_evi->flags & BGP_EVPNES_EVI_INCONS_VTEP_LIST) {
4023 json_flags = json_object_new_array();
4024 json_array_string_add(json_flags, "es-vtep-mismatch");
4025 json_object_object_add(json, "flags", json_flags);
4026 }
4027 } else {
4028 char vtep_str[ES_VTEP_LIST_STR_SZ + BGP_EVPN_VTEPS_FLAG_STR_SZ];
4029 char type_str[4];
4030
4031 type_str[0] = '\0';
4032 if (es_evi->flags & BGP_EVPNES_EVI_LOCAL)
9e0c2fd1 4033 strlcat(type_str, "L", sizeof(type_str));
c44ab6f1 4034 if (es_evi->flags & BGP_EVPNES_EVI_REMOTE)
9e0c2fd1 4035 strlcat(type_str, "R", sizeof(type_str));
c44ab6f1 4036
9e0c2fd1 4037 bgp_evpn_es_evi_vteps_str(vtep_str, es_evi, sizeof(vtep_str));
c44ab6f1 4038 if (!strlen(vtep_str))
9e0c2fd1 4039 strlcpy(vtep_str, "-", sizeof(type_str));
c44ab6f1
AK
4040
4041 vty_out(vty, "VNI: %d ESI: %s\n",
4042 es_evi->vpn->vni, es_evi->es->esi_str);
4043 vty_out(vty, " Type: %s\n", type_str);
4a8cd6ad
PG
4044 if (es_evi->es_frag) {
4045 vty_out(vty, " ES fragment RD: ");
4046 vty_out(vty, BGP_RD_AS_FORMAT(mode),
c4f64ea9 4047 &es_evi->es_frag->prd);
4a8cd6ad
PG
4048 vty_out(vty, "\n");
4049 }
c44ab6f1
AK
4050 vty_out(vty, " Inconsistencies: %s\n",
4051 (es_evi->flags & BGP_EVPNES_EVI_INCONS_VTEP_LIST) ?
4052 "es-vtep-mismatch":"-");
4053 vty_out(vty, " VTEPs: %s\n", vtep_str);
4054 vty_out(vty, "\n");
4055 }
4056}
4057
4058static void bgp_evpn_es_evi_show_one_vni(struct bgpevpn *vpn, struct vty *vty,
4059 json_object *json_array, bool detail)
4060{
4061 struct bgp_evpn_es_evi *es_evi;
4062 json_object *json = NULL;
4063
4064 RB_FOREACH(es_evi, bgp_es_evi_rb_head, &vpn->es_evi_rb_tree) {
4065 if (json_array)
4066 /* create a separate json object for each ES */
4067 json = json_object_new_object();
4068 if (detail)
4069 bgp_evpn_es_evi_show_entry_detail(vty, es_evi, json);
4070 else
4071 bgp_evpn_es_evi_show_entry(vty, es_evi, json);
4072 /* add ES to the json array */
4073 if (json_array)
4074 json_object_array_add(json_array, json);
4075 }
4076}
4077
4078struct es_evi_show_ctx {
4079 struct vty *vty;
4080 json_object *json;
4081 int detail;
4082};
4083
4084static void bgp_evpn_es_evi_show_one_vni_hash_cb(struct hash_bucket *bucket,
4085 void *ctxt)
4086{
4087 struct bgpevpn *vpn = (struct bgpevpn *)bucket->data;
4088 struct es_evi_show_ctx *wctx = (struct es_evi_show_ctx *)ctxt;
4089
4090 bgp_evpn_es_evi_show_one_vni(vpn, wctx->vty, wctx->json, wctx->detail);
4091}
4092
4093/* Display all ES EVIs */
4094void bgp_evpn_es_evi_show(struct vty *vty, bool uj, bool detail)
4095{
4096 json_object *json_array = NULL;
4097 struct es_evi_show_ctx wctx;
4098 struct bgp *bgp;
4099
4100 if (uj) {
4101 /* create an array of ES-EVIs */
4102 json_array = json_object_new_array();
4103 }
4104
4105 wctx.vty = vty;
4106 wctx.json = json_array;
4107 wctx.detail = detail;
4108
4109 bgp = bgp_get_evpn();
4110
4111 if (!json_array && !detail) {
4112 vty_out(vty, "Flags: L local, R remote, I inconsistent\n");
4113 vty_out(vty, "VTEP-Flags: E EAD-per-ES, V EAD-per-EVI\n");
4114 vty_out(vty, "%-8s %-30s %-5s %s\n",
4115 "VNI", "ESI", "Flags", "VTEPs");
4116 }
4117
4118 if (bgp)
4119 hash_iterate(bgp->vnihash,
4120 (void (*)(struct hash_bucket *,
4121 void *))bgp_evpn_es_evi_show_one_vni_hash_cb,
4122 &wctx);
c48349e3 4123 if (uj)
75eeda93 4124 vty_json(vty, json_array);
c44ab6f1
AK
4125}
4126
4127/* Display specific ES EVI */
4128void bgp_evpn_es_evi_show_vni(struct vty *vty, vni_t vni,
4129 bool uj, bool detail)
4130{
4131 struct bgpevpn *vpn = NULL;
4132 json_object *json_array = NULL;
4133 struct bgp *bgp;
4134
4135 if (uj) {
4136 /* create an array of ES-EVIs */
4137 json_array = json_object_new_array();
4138 }
4139
4140 bgp = bgp_get_evpn();
4141 if (bgp)
4142 vpn = bgp_evpn_lookup_vni(bgp, vni);
4143
4144 if (vpn) {
4145 if (!json_array && !detail) {
4146 vty_out(vty, "Flags: L local, R remote, I inconsistent\n");
4147 vty_out(vty, "VTEP-Flags: E EAD-per-ES, V EAD-per-EVI\n");
4148 vty_out(vty, "%-8s %-30s %-5s %s\n",
4149 "VNI", "ESI", "Flags", "VTEPs");
4150 }
4151
4152 bgp_evpn_es_evi_show_one_vni(vpn, vty, json_array, detail);
4153 } else {
4154 if (!uj)
4155 vty_out(vty, "VNI not found\n");
4156 }
4157
c48349e3 4158 if (uj)
75eeda93 4159 vty_json(vty, json_array);
c44ab6f1
AK
4160}
4161
4162/*****************************************************************************
4163 * Ethernet Segment Consistency checks
4164 * Consistency checking is done to detect misconfig or mis-cabling. When
4165 * an inconsistency is detected it is simply logged (and displayed via
4166 * show commands) at this point. A more drastic action can be executed (based
4167 * on user config) in the future.
4168 */
8fc2ffb3
AK
4169static void bgp_evpn_es_cons_checks_timer_start(void)
4170{
4171 if (!bgp_mh_info->consistency_checking || bgp_mh_info->t_cons_check)
4172 return;
4173
4174 if (BGP_DEBUG(evpn_mh, EVPN_MH_ES))
4175 zlog_debug("periodic consistency checking started");
4176
907a2395
DS
4177 event_add_timer(bm->master, bgp_evpn_run_consistency_checks, NULL,
4178 BGP_EVPN_CONS_CHECK_INTERVAL,
4179 &bgp_mh_info->t_cons_check);
8fc2ffb3
AK
4180}
4181
c44ab6f1
AK
4182/* queue up the es for background consistency checks */
4183static void bgp_evpn_es_cons_checks_pend_add(struct bgp_evpn_es *es)
4184{
4185 if (!bgp_mh_info->consistency_checking)
4186 /* consistency checking is not enabled */
4187 return;
4188
4189 if (CHECK_FLAG(es->flags, BGP_EVPNES_CONS_CHECK_PEND))
4190 /* already queued for consistency checking */
4191 return;
4192
8fc2ffb3
AK
4193 /* start the periodic timer for consistency checks if it is not
4194 * already running */
4195 bgp_evpn_es_cons_checks_timer_start();
4196
c44ab6f1
AK
4197 SET_FLAG(es->flags, BGP_EVPNES_CONS_CHECK_PEND);
4198 listnode_init(&es->pend_es_listnode, es);
4199 listnode_add_after(bgp_mh_info->pend_es_list,
4200 listtail_unchecked(bgp_mh_info->pend_es_list),
4201 &es->pend_es_listnode);
4202}
4203
4204/* pull the ES from the consistency check list */
4205static void bgp_evpn_es_cons_checks_pend_del(struct bgp_evpn_es *es)
4206{
4207 if (!CHECK_FLAG(es->flags, BGP_EVPNES_CONS_CHECK_PEND))
4208 return;
4209
4210 UNSET_FLAG(es->flags, BGP_EVPNES_CONS_CHECK_PEND);
4211 list_delete_node(bgp_mh_info->pend_es_list,
4212 &es->pend_es_listnode);
4213}
4214
4215/* Number of active VTEPs associated with the ES-per-EVI */
4216static uint32_t bgp_evpn_es_evi_get_active_vtep_cnt(
4217 struct bgp_evpn_es_evi *es_evi)
4218{
4219 struct bgp_evpn_es_evi_vtep *evi_vtep;
4220 struct listnode *node;
4221 uint32_t vtep_cnt = 0;
4222
4223 for (ALL_LIST_ELEMENTS_RO(es_evi->es_evi_vtep_list, node, evi_vtep)) {
4224 if (CHECK_FLAG(evi_vtep->flags, BGP_EVPN_EVI_VTEP_ACTIVE))
4225 ++vtep_cnt;
4226 }
4227
4228 return vtep_cnt;
4229}
4230
4231/* Number of active VTEPs associated with the ES */
4232static uint32_t bgp_evpn_es_get_active_vtep_cnt(struct bgp_evpn_es *es)
4233{
4234 struct listnode *node;
4235 uint32_t vtep_cnt = 0;
4236 struct bgp_evpn_es_vtep *es_vtep;
4237
4238 for (ALL_LIST_ELEMENTS_RO(es->es_vtep_list, node, es_vtep)) {
4239 if (CHECK_FLAG(es_vtep->flags, BGP_EVPNES_VTEP_ACTIVE))
4240 ++vtep_cnt;
4241 }
4242
4243 return vtep_cnt;
4244}
4245
4246static struct bgp_evpn_es_vtep *bgp_evpn_es_get_next_active_vtep(
4247 struct bgp_evpn_es *es, struct bgp_evpn_es_vtep *es_vtep)
4248{
4249 struct listnode *node;
4250 struct bgp_evpn_es_vtep *next_es_vtep;
4251
4252 if (es_vtep)
4253 node = listnextnode_unchecked(&es_vtep->es_listnode);
4254 else
4255 node = listhead(es->es_vtep_list);
4256
4257 for (; node; node = listnextnode_unchecked(node)) {
4258 next_es_vtep = listgetdata(node);
4259 if (CHECK_FLAG(next_es_vtep->flags, BGP_EVPNES_VTEP_ACTIVE))
4260 return next_es_vtep;
4261 }
4262
4263 return NULL;
4264}
4265
4266static struct bgp_evpn_es_evi_vtep *bgp_evpn_es_evi_get_next_active_vtep(
4267 struct bgp_evpn_es_evi *es_evi,
4268 struct bgp_evpn_es_evi_vtep *evi_vtep)
4269{
4270 struct listnode *node;
4271 struct bgp_evpn_es_evi_vtep *next_evi_vtep;
4272
4273 if (evi_vtep)
4274 node = listnextnode_unchecked(&evi_vtep->es_evi_listnode);
4275 else
4276 node = listhead(es_evi->es_evi_vtep_list);
4277
4278 for (; node; node = listnextnode_unchecked(node)) {
4279 next_evi_vtep = listgetdata(node);
4280 if (CHECK_FLAG(next_evi_vtep->flags, BGP_EVPN_EVI_VTEP_ACTIVE))
4281 return next_evi_vtep;
4282 }
4283
4284 return NULL;
4285}
4286
4287static void bgp_evpn_es_evi_set_inconsistent(struct bgp_evpn_es_evi *es_evi)
4288{
4289 if (!CHECK_FLAG(es_evi->flags, BGP_EVPNES_EVI_INCONS_VTEP_LIST)) {
4290 if (BGP_DEBUG(evpn_mh, EVPN_MH_ES))
4291 zlog_debug("inconsistency detected - es %s evi %u vtep list mismatch",
4292 es_evi->es->esi_str,
4293 es_evi->vpn->vni);
4294 SET_FLAG(es_evi->flags, BGP_EVPNES_EVI_INCONS_VTEP_LIST);
4295
4296 /* update parent ES with the incosistency setting */
4297 if (!es_evi->es->incons_evi_vtep_cnt &&
4298 BGP_DEBUG(evpn_mh, EVPN_MH_ES))
4299 zlog_debug("inconsistency detected - es %s vtep list mismatch",
4300 es_evi->es->esi_str);
4301 ++es_evi->es->incons_evi_vtep_cnt;
4302 SET_FLAG(es_evi->es->inconsistencies,
4303 BGP_EVPNES_INCONS_VTEP_LIST);
4304 }
4305}
4306
4307static uint32_t bgp_evpn_es_run_consistency_checks(struct bgp_evpn_es *es)
4308{
4309 int proc_cnt = 0;
4310 int es_active_vtep_cnt;
4311 int evi_active_vtep_cnt;
4312 struct bgp_evpn_es_evi *es_evi;
4313 struct listnode *evi_node;
4314 struct bgp_evpn_es_vtep *es_vtep;
4315 struct bgp_evpn_es_evi_vtep *evi_vtep;
4316
4317 /* reset the inconsistencies and re-evaluate */
4318 es->incons_evi_vtep_cnt = 0;
4319 es->inconsistencies = 0;
4320
4321 es_active_vtep_cnt = bgp_evpn_es_get_active_vtep_cnt(es);
4322 for (ALL_LIST_ELEMENTS_RO(es->es_evi_list,
4323 evi_node, es_evi)) {
4324 ++proc_cnt;
4325
4326 /* reset the inconsistencies on the EVI and re-evaluate*/
4327 UNSET_FLAG(es_evi->flags, BGP_EVPNES_EVI_INCONS_VTEP_LIST);
4328
4329 evi_active_vtep_cnt =
4330 bgp_evpn_es_evi_get_active_vtep_cnt(es_evi);
4331 if (es_active_vtep_cnt != evi_active_vtep_cnt) {
4332 bgp_evpn_es_evi_set_inconsistent(es_evi);
4333 continue;
4334 }
4335
4336 if (!es_active_vtep_cnt)
4337 continue;
4338
4339 es_vtep = NULL;
4340 evi_vtep = NULL;
4341 while ((es_vtep = bgp_evpn_es_get_next_active_vtep(
4342 es, es_vtep))) {
4343 evi_vtep = bgp_evpn_es_evi_get_next_active_vtep(es_evi,
4344 evi_vtep);
4345 if (!evi_vtep) {
4346 bgp_evpn_es_evi_set_inconsistent(es_evi);
4347 break;
4348 }
4349 if (es_vtep->vtep_ip.s_addr !=
4350 evi_vtep->vtep_ip.s_addr) {
4351 /* inconsistency detected; set it and move
4352 * to the next evi
4353 */
4354 bgp_evpn_es_evi_set_inconsistent(es_evi);
4355 break;
4356 }
4357 }
4358 }
4359
4360 return proc_cnt;
4361}
4362
e6685141 4363static void bgp_evpn_run_consistency_checks(struct event *t)
c44ab6f1
AK
4364{
4365 int proc_cnt = 0;
c44ab6f1
AK
4366 struct listnode *node;
4367 struct listnode *nextnode;
4368 struct bgp_evpn_es *es;
4369
4370 for (ALL_LIST_ELEMENTS(bgp_mh_info->pend_es_list,
4371 node, nextnode, es)) {
c44ab6f1
AK
4372 ++proc_cnt;
4373 /* run consistency checks on the ES and remove it from the
4374 * pending list
4375 */
4376 proc_cnt += bgp_evpn_es_run_consistency_checks(es);
4377 bgp_evpn_es_cons_checks_pend_del(es);
4378 if (proc_cnt > 500)
4379 break;
4380 }
4381
4382 /* restart the timer */
907a2395 4383 event_add_timer(bm->master, bgp_evpn_run_consistency_checks, NULL,
c44ab6f1
AK
4384 BGP_EVPN_CONS_CHECK_INTERVAL,
4385 &bgp_mh_info->t_cons_check);
c44ab6f1
AK
4386}
4387
090efa2f
AK
4388/*****************************************************************************
4389 * EVPN-Nexthop and RMAC management: nexthops associated with Type-2 routes
4390 * that have an ES as destination are consolidated by BGP into a per-VRF
4391 * nh->rmac mapping which is sent to zebra. Zebra installs the nexthop
4392 * as a remote neigh/fdb entry with a dummy (type-1) prefix referencing it.
4393 *
4394 * This handling is needed because Type-2 routes with ES as dest use NHG
4395 * that is setup using EAD routes (i.e. such NHGs do not include the
4396 * RMAC info).
4397 ****************************************************************************/
4398static void bgp_evpn_nh_zebra_update_send(struct bgp_evpn_nh *nh, bool add)
4399{
4400 struct stream *s;
4401 struct bgp *bgp_vrf = nh->bgp_vrf;
4402
4403 /* Check socket. */
4404 if (!zclient || zclient->sock < 0)
4405 return;
4406
4407 /* Don't try to register if Zebra doesn't know of this instance. */
4408 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp_vrf)) {
4409 if (BGP_DEBUG(zebra, ZEBRA))
4410 zlog_debug("No zebra instance, not %s remote nh %s",
4411 add ? "adding" : "deleting", nh->nh_str);
4412 return;
4413 }
4414
4415 s = zclient->obuf;
4416 stream_reset(s);
4417
4418 zclient_create_header(
4419 s, add ? ZEBRA_EVPN_REMOTE_NH_ADD : ZEBRA_EVPN_REMOTE_NH_DEL,
4420 bgp_vrf->vrf_id);
4421 stream_putl(s, bgp_vrf->vrf_id);
4422 stream_put(s, &nh->ip, sizeof(nh->ip));
4423 if (add)
4424 stream_put(s, &nh->rmac, sizeof(nh->rmac));
4425
4426 stream_putw_at(s, 0, stream_get_endp(s));
4427
4428 if (BGP_DEBUG(evpn_mh, EVPN_MH_ES)) {
4429 if (add)
4430 zlog_debug("evpn vrf %s nh %s rmac %pEA add to zebra",
dc9ca53f
DS
4431 nh->bgp_vrf->name_pretty, nh->nh_str,
4432 &nh->rmac);
090efa2f
AK
4433 else if (BGP_DEBUG(evpn_mh, EVPN_MH_ES))
4434 zlog_debug("evpn vrf %s nh %s del to zebra",
dc9ca53f 4435 nh->bgp_vrf->name_pretty, nh->nh_str);
090efa2f
AK
4436 }
4437
23aa35ad
AK
4438 frrtrace(2, frr_bgp, evpn_mh_nh_rmac_zsend, add, nh);
4439
090efa2f
AK
4440 zclient_send_message(zclient);
4441}
4442
4443static void bgp_evpn_nh_zebra_update(struct bgp_evpn_nh *nh, bool add)
4444{
4445 if (add && !is_zero_mac(&nh->rmac)) {
4446 nh->flags |= BGP_EVPN_NH_READY_FOR_ZEBRA;
4447 bgp_evpn_nh_zebra_update_send(nh, true);
4448 } else {
4449 if (!(nh->flags & BGP_EVPN_NH_READY_FOR_ZEBRA))
4450 return;
4451 nh->flags &= ~BGP_EVPN_NH_READY_FOR_ZEBRA;
4452 bgp_evpn_nh_zebra_update_send(nh, false);
4453 }
4454}
4455
4456static void *bgp_evpn_nh_alloc(void *p)
4457{
4458 struct bgp_evpn_nh *tmp_n = p;
4459 struct bgp_evpn_nh *n;
4460
4461 n = XCALLOC(MTYPE_BGP_EVPN_NH, sizeof(struct bgp_evpn_nh));
4462 *n = *tmp_n;
4463
4464 return ((void *)n);
4465}
4466
4467static struct bgp_evpn_nh *bgp_evpn_nh_find(struct bgp *bgp_vrf,
4468 struct ipaddr *ip)
4469{
4470 struct bgp_evpn_nh tmp;
4471 struct bgp_evpn_nh *n;
4472
4473 memset(&tmp, 0, sizeof(tmp));
4474 memcpy(&tmp.ip, ip, sizeof(struct ipaddr));
4475 n = hash_lookup(bgp_vrf->evpn_nh_table, &tmp);
4476
4477 return n;
4478}
4479
4480/* Add nexthop entry - implicitly created on first path reference */
4481static struct bgp_evpn_nh *bgp_evpn_nh_add(struct bgp *bgp_vrf,
4482 struct ipaddr *ip,
4483 struct bgp_path_info *pi)
4484{
4485 struct bgp_evpn_nh tmp_n;
4486 struct bgp_evpn_nh *n = NULL;
4487
6006b807 4488 memset(&tmp_n, 0, sizeof(tmp_n));
090efa2f
AK
4489 memcpy(&tmp_n.ip, ip, sizeof(struct ipaddr));
4490 n = hash_get(bgp_vrf->evpn_nh_table, &tmp_n, bgp_evpn_nh_alloc);
4491 ipaddr2str(ip, n->nh_str, sizeof(n->nh_str));
4492 n->bgp_vrf = bgp_vrf;
4493
4494 n->pi_list = list_new();
4495 listset_app_node_mem(n->pi_list);
4496
4497 /* Setup ref_pi when the nh is created */
4498 if (CHECK_FLAG(pi->flags, BGP_PATH_VALID) && pi->attr) {
4499 n->ref_pi = pi;
4500 memcpy(&n->rmac, &pi->attr->rmac, ETH_ALEN);
4501 }
4502
4503 if (BGP_DEBUG(evpn_mh, EVPN_MH_ES))
dc9ca53f
DS
4504 zlog_debug("evpn vrf %s nh %s rmac %pEA add",
4505 n->bgp_vrf->name_pretty, n->nh_str, &n->rmac);
090efa2f
AK
4506 bgp_evpn_nh_zebra_update(n, true);
4507 return n;
4508}
4509
4510/* Delete nexthop entry if there are no paths referencing it */
4511static void bgp_evpn_nh_del(struct bgp_evpn_nh *n)
4512{
4513 struct bgp_evpn_nh *tmp_n;
4514 struct bgp *bgp_vrf = n->bgp_vrf;
4515
4516 if (listcount(n->pi_list))
4517 return;
4518
4519 if (BGP_DEBUG(evpn_mh, EVPN_MH_ES))
dc9ca53f
DS
4520 zlog_debug("evpn vrf %s nh %s del to zebra",
4521 bgp_vrf->name_pretty, n->nh_str);
090efa2f
AK
4522
4523 bgp_evpn_nh_zebra_update(n, false);
4524 list_delete(&n->pi_list);
4525 tmp_n = hash_release(bgp_vrf->evpn_nh_table, n);
4526 XFREE(MTYPE_BGP_EVPN_NH, tmp_n);
4527}
4528
3727e359
DA
4529static void hash_evpn_nh_free(struct bgp_evpn_nh *ben)
4530{
4531 XFREE(MTYPE_BGP_EVPN_NH, ben);
4532}
4533
090efa2f
AK
4534static unsigned int bgp_evpn_nh_hash_keymake(const void *p)
4535{
4536 const struct bgp_evpn_nh *n = p;
4537 const struct ipaddr *ip = &n->ip;
4538
4539 if (IS_IPADDR_V4(ip))
4540 return jhash_1word(ip->ipaddr_v4.s_addr, 0);
4541
4542 return jhash2(ip->ipaddr_v6.s6_addr32,
4543 array_size(ip->ipaddr_v6.s6_addr32), 0);
4544}
4545
4546static bool bgp_evpn_nh_cmp(const void *p1, const void *p2)
4547{
4548 const struct bgp_evpn_nh *n1 = p1;
4549 const struct bgp_evpn_nh *n2 = p2;
4550
4551 if (n1 == NULL && n2 == NULL)
4552 return true;
4553
4554 if (n1 == NULL || n2 == NULL)
4555 return false;
4556
60cda04d 4557 return (ipaddr_cmp(&n1->ip, &n2->ip) == 0);
090efa2f
AK
4558}
4559
4560void bgp_evpn_nh_init(struct bgp *bgp_vrf)
4561{
4562 if (BGP_DEBUG(evpn_mh, EVPN_MH_ES))
dc9ca53f 4563 zlog_debug("evpn vrf %s nh init", bgp_vrf->name_pretty);
090efa2f
AK
4564 bgp_vrf->evpn_nh_table = hash_create(
4565 bgp_evpn_nh_hash_keymake, bgp_evpn_nh_cmp, "BGP EVPN NH table");
4566}
4567
4568static void bgp_evpn_nh_flush_entry(struct bgp_evpn_nh *nh)
4569{
4570 struct listnode *node;
4571 struct listnode *nnode;
4572 struct bgp_path_evpn_nh_info *nh_info;
4573
4574 if (BGP_DEBUG(evpn_mh, EVPN_MH_ES))
dc9ca53f 4575 zlog_debug("evpn vrf %s nh %s flush", nh->bgp_vrf->name_pretty,
090efa2f
AK
4576 nh->nh_str);
4577
4578 /* force flush paths */
4579 for (ALL_LIST_ELEMENTS(nh->pi_list, node, nnode, nh_info))
4580 bgp_evpn_path_nh_del(nh->bgp_vrf, nh_info->pi);
4581}
4582
4583static void bgp_evpn_nh_flush_cb(struct hash_bucket *bucket, void *ctxt)
4584{
4585 struct bgp_evpn_nh *nh = (struct bgp_evpn_nh *)bucket->data;
4586
4587 bgp_evpn_nh_flush_entry(nh);
4588}
4589
4590void bgp_evpn_nh_finish(struct bgp *bgp_vrf)
4591{
4592 if (BGP_DEBUG(evpn_mh, EVPN_MH_ES))
dc9ca53f 4593 zlog_debug("evpn vrf %s nh finish", bgp_vrf->name_pretty);
090efa2f
AK
4594 hash_iterate(
4595 bgp_vrf->evpn_nh_table,
4596 (void (*)(struct hash_bucket *, void *))bgp_evpn_nh_flush_cb,
4597 NULL);
d8bc11a5
DS
4598 hash_clean_and_free(&bgp_vrf->evpn_nh_table,
4599 (void (*)(void *))hash_evpn_nh_free);
090efa2f
AK
4600}
4601
4602static void bgp_evpn_nh_update_ref_pi(struct bgp_evpn_nh *nh)
4603{
4604 struct listnode *node;
4605 struct bgp_path_info *pi;
4606 struct bgp_path_evpn_nh_info *nh_info;
4607
4608 if (nh->ref_pi)
4609 return;
4610
4611 for (ALL_LIST_ELEMENTS_RO(nh->pi_list, node, nh_info)) {
4612 pi = nh_info->pi;
4613 if (!CHECK_FLAG(pi->flags, BGP_PATH_VALID) || !pi->attr)
4614 continue;
4615
4616 if (BGP_DEBUG(evpn_mh, EVPN_MH_ES))
4617 zlog_debug("evpn vrf %s nh %s ref_pi update",
dc9ca53f 4618 nh->bgp_vrf->name_pretty, nh->nh_str);
090efa2f
AK
4619 nh->ref_pi = pi;
4620 /* If we have a new pi copy rmac from it and update
4621 * zebra if the new rmac is different
4622 */
4623 if (memcmp(&nh->rmac, &nh->ref_pi->attr->rmac, ETH_ALEN)) {
4624 memcpy(&nh->rmac, &nh->ref_pi->attr->rmac, ETH_ALEN);
4625 bgp_evpn_nh_zebra_update(nh, true);
4626 }
4627 break;
4628 }
4629}
4630
4631static void bgp_evpn_nh_clear_ref_pi(struct bgp_evpn_nh *nh,
4632 struct bgp_path_info *pi)
4633{
4634 if (nh->ref_pi != pi)
4635 return;
4636
4637 if (BGP_DEBUG(evpn_mh, EVPN_MH_ES))
dc9ca53f
DS
4638 zlog_debug("evpn vrf %s nh %s ref_pi clear",
4639 nh->bgp_vrf->name_pretty, nh->nh_str);
090efa2f
AK
4640 nh->ref_pi = NULL;
4641 /* try to find another ref_pi */
4642 bgp_evpn_nh_update_ref_pi(nh);
4643 /* couldn't find one - clear the old rmac and notify zebra */
4644 if (!nh->ref_pi) {
4645 memset(&nh->rmac, 0, ETH_ALEN);
4646 bgp_evpn_nh_zebra_update(nh, true);
4647 }
4648}
4649
4650static void bgp_evpn_path_nh_info_free(struct bgp_path_evpn_nh_info *nh_info)
4651{
4652 bgp_evpn_path_nh_unlink(nh_info);
4653 XFREE(MTYPE_BGP_EVPN_PATH_NH_INFO, nh_info);
4654}
4655
4656static struct bgp_path_evpn_nh_info *
4657bgp_evpn_path_nh_info_new(struct bgp_path_info *pi)
4658{
4659 struct bgp_path_info_extra *e;
4660 struct bgp_path_mh_info *mh_info;
4661 struct bgp_path_evpn_nh_info *nh_info;
4662
4663 e = bgp_path_info_extra_get(pi);
4664
4665 /* If mh_info doesn't exist allocate it */
4666 mh_info = e->mh_info;
4667 if (!mh_info)
4668 e->mh_info = mh_info = XCALLOC(MTYPE_BGP_EVPN_PATH_MH_INFO,
4669 sizeof(struct bgp_path_mh_info));
4670
4671 /* If nh_info doesn't exist allocate it */
4672 nh_info = mh_info->nh_info;
4673 if (!nh_info) {
4674 mh_info->nh_info = nh_info =
4675 XCALLOC(MTYPE_BGP_EVPN_PATH_NH_INFO,
4676 sizeof(struct bgp_path_evpn_nh_info));
4677 nh_info->pi = pi;
4678 }
4679
4680 return nh_info;
4681}
4682
4683static void bgp_evpn_path_nh_unlink(struct bgp_path_evpn_nh_info *nh_info)
4684{
4685 struct bgp_evpn_nh *nh = nh_info->nh;
4686 struct bgp_path_info *pi;
4687 char prefix_buf[PREFIX_STRLEN];
4688
4689 if (!nh)
4690 return;
4691
4692 pi = nh_info->pi;
4693 if (BGP_DEBUG(evpn_mh, EVPN_MH_RT))
4694 zlog_debug("path %s unlinked from nh %s %s",
4695 pi->net ? prefix2str(&pi->net->p, prefix_buf,
4696 sizeof(prefix_buf))
4697 : "",
dc9ca53f 4698 nh->bgp_vrf->name_pretty, nh->nh_str);
090efa2f
AK
4699
4700 list_delete_node(nh->pi_list, &nh_info->nh_listnode);
4701
4702 nh_info->nh = NULL;
4703
4704 /* check if the ref_pi need to be updated */
4705 bgp_evpn_nh_clear_ref_pi(nh, pi);
4706
4707 /* if there are no other references against the nh it
4708 * needs to be freed
4709 */
4710 bgp_evpn_nh_del(nh);
4711
4712 /* Note we don't free the path nh_info on unlink; it will be freed up
4713 * along with the path.
4714 */
4715}
4716
4717static void bgp_evpn_path_nh_link(struct bgp *bgp_vrf, struct bgp_path_info *pi)
4718{
4719 struct bgp_path_evpn_nh_info *nh_info;
4720 struct bgp_evpn_nh *nh;
090efa2f
AK
4721 struct ipaddr ip;
4722
4723 /* EVPN nexthop setup in bgp has been turned off */
4724 if (!bgp_mh_info->bgp_evpn_nh_setup)
4725 return;
4726
4727 if (!bgp_vrf->evpn_nh_table) {
4728 if (BGP_DEBUG(evpn_mh, EVPN_MH_RT))
70545a29 4729 zlog_debug("path %pFX linked to vrf %s failed",
dc9ca53f 4730 &pi->net->p, bgp_vrf->name_pretty);
090efa2f
AK
4731 return;
4732 }
4733
4734 nh_info = (pi->extra && pi->extra->mh_info)
4735 ? pi->extra->mh_info->nh_info
4736 : NULL;
4737
4738 /* if NHG is not being used for this path we don't need to manage the
4739 * nexthops in bgp (they are managed by zebra instead)
4740 */
4741 if (!(pi->attr->es_flags & ATTR_ES_L3_NHG_USE)) {
4742 if (nh_info)
4743 bgp_evpn_path_nh_unlink(nh_info);
4744 return;
4745 }
4746
4747 /* setup nh_info against the path if it doesn't aleady exist */
4748 if (!nh_info)
4749 nh_info = bgp_evpn_path_nh_info_new(pi);
4750
4751 /* find-create nh */
4752 memset(&ip, 0, sizeof(ip));
4753 if (pi->net->p.family == AF_INET6) {
4754 SET_IPADDR_V6(&ip);
4755 memcpy(&ip.ipaddr_v6, &pi->attr->mp_nexthop_global,
4756 sizeof(ip.ipaddr_v6));
4757 } else {
4758 SET_IPADDR_V4(&ip);
4759 memcpy(&ip.ipaddr_v4, &pi->attr->nexthop, sizeof(ip.ipaddr_v4));
4760 }
4761
4762 nh = bgp_evpn_nh_find(bgp_vrf, &ip);
4763 if (!nh)
4764 nh = bgp_evpn_nh_add(bgp_vrf, &ip, pi);
4765
4766 /* dup check */
4767 if (nh_info->nh == nh) {
4768 /* Check if any of the paths are now valid */
4769 bgp_evpn_nh_update_ref_pi(nh);
4770 return;
4771 }
4772
4773 /* unlink old nh if any */
4774 bgp_evpn_path_nh_unlink(nh_info);
4775
4776 if (BGP_DEBUG(evpn_mh, EVPN_MH_RT))
70545a29 4777 zlog_debug("path %pFX linked to nh %s %s", &pi->net->p,
dc9ca53f 4778 nh->bgp_vrf->name_pretty, nh->nh_str);
090efa2f
AK
4779
4780 /* link mac-ip path to the new nh */
4781 nh_info->nh = nh;
4782 listnode_init(&nh_info->nh_listnode, nh_info);
4783 listnode_add(nh->pi_list, &nh_info->nh_listnode);
4784 /* If a new valid path got linked to the nh see if can get the rmac
4785 * from it
4786 */
4787 bgp_evpn_nh_update_ref_pi(nh);
4788 if (BGP_DEBUG(evpn_mh, EVPN_MH_ES)) {
4789 if (!nh->ref_pi)
4790 zlog_debug(
70545a29 4791 "path %pFX linked to nh %s %s with no valid pi",
dc9ca53f
DS
4792 &pi->net->p, nh->bgp_vrf->name_pretty,
4793 nh->nh_str);
090efa2f
AK
4794 }
4795}
4796
4797void bgp_evpn_path_nh_del(struct bgp *bgp_vrf, struct bgp_path_info *pi)
4798{
4799 struct bgp_path_evpn_nh_info *nh_info;
4800
4801 nh_info = (pi->extra && pi->extra->mh_info)
4802 ? pi->extra->mh_info->nh_info
4803 : NULL;
4804
4805 if (!nh_info)
4806 return;
4807
4808 bgp_evpn_path_nh_unlink(nh_info);
4809}
4810
4811void bgp_evpn_path_nh_add(struct bgp *bgp_vrf, struct bgp_path_info *pi)
4812{
4813 bgp_evpn_path_nh_link(bgp_vrf, pi);
4814}
4815
4816static void bgp_evpn_nh_show_entry(struct bgp_evpn_nh *nh, struct vty *vty,
4817 json_object *json_array)
4818{
4819 json_object *json = NULL;
4820 char mac_buf[ETHER_ADDR_STRLEN];
4821 char prefix_buf[PREFIX_STRLEN];
4822
4823 if (json_array)
4824 /* create a separate json object for each ES */
4825 json = json_object_new_object();
4826
4827 prefix_mac2str(&nh->rmac, mac_buf, sizeof(mac_buf));
4828 if (nh->ref_pi && nh->ref_pi->net)
4829 prefix2str(&nh->ref_pi->net->p, prefix_buf, sizeof(prefix_buf));
4830 else
4831 prefix_buf[0] = '\0';
4832 if (json) {
dc9ca53f 4833 json_object_string_add(json, "vrf", nh->bgp_vrf->name_pretty);
090efa2f
AK
4834 json_object_string_add(json, "ip", nh->nh_str);
4835 json_object_string_add(json, "rmac", mac_buf);
4836 json_object_string_add(json, "basePath", prefix_buf);
4837 json_object_int_add(json, "pathCount", listcount(nh->pi_list));
4838 } else {
dc9ca53f
DS
4839 vty_out(vty, "%-15s %-15s %-17s %-10d %s\n",
4840 nh->bgp_vrf->name_pretty, nh->nh_str, mac_buf,
4841 listcount(nh->pi_list), prefix_buf);
090efa2f
AK
4842 }
4843
4844 /* add ES to the json array */
4845 if (json_array)
4846 json_object_array_add(json_array, json);
4847}
4848
4849struct nh_show_ctx {
4850 struct vty *vty;
4851 json_object *json;
4852};
4853
4854static void bgp_evpn_nh_show_hash_cb(struct hash_bucket *bucket, void *ctxt)
4855{
4856 struct bgp_evpn_nh *nh = (struct bgp_evpn_nh *)bucket->data;
4857 struct nh_show_ctx *wctx = (struct nh_show_ctx *)ctxt;
4858
4859 bgp_evpn_nh_show_entry(nh, wctx->vty, wctx->json);
4860}
4861
4862/* Display all evpn nexthops */
4863void bgp_evpn_nh_show(struct vty *vty, bool uj)
4864{
4865 json_object *json_array = NULL;
4866 struct bgp *bgp_vrf;
4867 struct listnode *node;
4868 struct nh_show_ctx wctx;
4869
4870 if (uj) {
4871 /* create an array of nexthops */
4872 json_array = json_object_new_array();
4873 } else {
4874 vty_out(vty, "%-15s %-15s %-17s %-10s %s\n", "VRF", "IP",
4875 "RMAC", "#Paths", "Base Path");
4876 }
4877
4878 wctx.vty = vty;
4879 wctx.json = json_array;
4880
4881 /* walk through all vrfs */
4882 for (ALL_LIST_ELEMENTS_RO(bm->bgp, node, bgp_vrf)) {
4883 hash_iterate(bgp_vrf->evpn_nh_table,
4884 (void (*)(struct hash_bucket *,
4885 void *))bgp_evpn_nh_show_hash_cb,
4886 &wctx);
4887 }
4888
4889 /* print the array of json-ESs */
c48349e3 4890 if (uj)
75eeda93 4891 vty_json(vty, json_array);
090efa2f
AK
4892}
4893
c44ab6f1
AK
4894/*****************************************************************************/
4895void bgp_evpn_mh_init(void)
4896{
4897 bm->mh_info = XCALLOC(MTYPE_BGP_EVPN_MH_INFO, sizeof(*bm->mh_info));
4898
4899 /* setup ES tables */
4900 RB_INIT(bgp_es_rb_head, &bgp_mh_info->es_rb_tree);
4901 /* local ES list */
4902 bgp_mh_info->local_es_list = list_new();
4903 listset_app_node_mem(bgp_mh_info->local_es_list);
4904 /* list of ESs with pending processing */
4905 bgp_mh_info->pend_es_list = list_new();
4906 listset_app_node_mem(bgp_mh_info->pend_es_list);
4907
fe8293c3
AK
4908 bgp_mh_info->ead_evi_rx = BGP_EVPN_MH_EAD_EVI_RX_DEF;
4909 bgp_mh_info->ead_evi_tx = BGP_EVPN_MH_EAD_EVI_TX_DEF;
f4a5218d
AK
4910 bgp_mh_info->ead_es_export_rtl = list_new();
4911 bgp_mh_info->ead_es_export_rtl->cmp =
4912 (int (*)(void *, void *))bgp_evpn_route_target_cmp;
4913 bgp_mh_info->ead_es_export_rtl->del = bgp_evpn_xxport_delete_ecomm;
fe8293c3 4914
c44ab6f1
AK
4915 /* config knobs - XXX add cli to control it */
4916 bgp_mh_info->ead_evi_adv_for_down_links = true;
4917 bgp_mh_info->consistency_checking = true;
2867823e 4918 bgp_mh_info->host_routes_use_l3nhg = BGP_EVPN_MH_USE_ES_L3NHG_DEF;
70524092 4919 bgp_mh_info->suppress_l3_ecomm_on_inactive_es = true;
090efa2f 4920 bgp_mh_info->bgp_evpn_nh_setup = true;
bb37eabe 4921 bgp_mh_info->evi_per_es_frag = BGP_EVPN_MAX_EVI_PER_ES_FRAG;
c44ab6f1 4922
c44ab6f1
AK
4923 memset(&zero_esi_buf, 0, sizeof(esi_t));
4924}
4925
4926void bgp_evpn_mh_finish(void)
4927{
4928 struct bgp_evpn_es *es;
4929 struct bgp_evpn_es *es_next;
c44ab6f1 4930
45a859f1
AK
4931 if (BGP_DEBUG(evpn_mh, EVPN_MH_RT))
4932 zlog_debug("evpn mh finish");
4933
4934 RB_FOREACH_SAFE (es, bgp_es_rb_head, &bgp_mh_info->es_rb_tree,
4935 es_next) {
d57e0c05 4936 bgp_evpn_es_local_info_clear(es, true);
c44ab6f1 4937 }
8fc2ffb3 4938 if (bgp_mh_info->t_cons_check)
e16d030c 4939 EVENT_OFF(bgp_mh_info->t_cons_check);
c44ab6f1
AK
4940 list_delete(&bgp_mh_info->local_es_list);
4941 list_delete(&bgp_mh_info->pend_es_list);
f4a5218d 4942 list_delete(&bgp_mh_info->ead_es_export_rtl);
c44ab6f1
AK
4943
4944 XFREE(MTYPE_BGP_EVPN_MH_INFO, bgp_mh_info);
4945}
acd1d9bc
AD
4946
4947/* This function is called when disable-ead-evi-rx knob flaps */
4948void bgp_evpn_switch_ead_evi_rx(void)
4949{
4950 struct bgp *bgp;
4951 struct bgp_evpn_es *es;
4952 struct bgp_evpn_es_evi *es_evi;
4953 struct listnode *evi_node = NULL;
4954 struct listnode *evi_next = NULL;
4955 struct bgp_evpn_es_evi_vtep *vtep;
4956 struct listnode *vtep_node = NULL;
4957 struct listnode *vtep_next = NULL;
4958
4959 bgp = bgp_get_evpn();
4960 if (!bgp)
4961 return;
4962
4963 /*
4964 * Process all the remote es_evi_vteps and reevaluate if the es_evi_vtep
4965 * is active.
4966 */
4967 RB_FOREACH(es, bgp_es_rb_head, &bgp_mh_info->es_rb_tree) {
4968 if (!CHECK_FLAG(es->flags, BGP_EVPNES_REMOTE))
4969 continue;
4970
4971 for (ALL_LIST_ELEMENTS(es->es_evi_list, evi_node, evi_next,
4972 es_evi)) {
4973 if (!CHECK_FLAG(es_evi->flags, BGP_EVPNES_EVI_REMOTE))
4974 continue;
4975
4976 for (ALL_LIST_ELEMENTS(es_evi->es_evi_vtep_list,
4977 vtep_node, vtep_next, vtep))
4978 bgp_evpn_es_evi_vtep_re_eval_active(bgp, vtep);
4979 }
4980 }
4981}