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