]> git.proxmox.com Git - mirror_frr.git/blame - bgpd/bgp_evpn.c
bgpd: set vrf originator ip to kernels local-ip
[mirror_frr.git] / bgpd / bgp_evpn.c
CommitLineData
7ef5a232 1/* Ethernet-VPN Packet and vty Processing File
896014f4 2 * Copyright (C) 2016 6WIND
128ea8ab 3 * Copyright (C) 2017 Cumulus Networks, Inc.
896014f4 4 *
128ea8ab 5 * This file is part of FRR.
896014f4
DL
6 *
7 * FRRouting is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2, or (at your option) any
10 * later version.
11 *
12 * FRRouting is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; see the file COPYING; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 */
7ef5a232 21
7ef5a232
PG
22#include <zebra.h>
23
24#include "command.h"
25#include "filter.h"
26#include "prefix.h"
27#include "log.h"
28#include "memory.h"
29#include "stream.h"
14c1a7bf 30#include "hash.h"
31#include "jhash.h"
32#include "bitfield.h"
128ea8ab 33#include "zclient.h"
7ef5a232
PG
34
35#include "bgpd/bgp_attr_evpn.h"
36#include "bgpd/bgpd.h"
37#include "bgpd/bgp_table.h"
38#include "bgpd/bgp_route.h"
39#include "bgpd/bgp_attr.h"
40#include "bgpd/bgp_mplsvpn.h"
9bedbb1e 41#include "bgpd/bgp_label.h"
7ef5a232 42#include "bgpd/bgp_evpn.h"
14c1a7bf 43#include "bgpd/bgp_evpn_private.h"
44#include "bgpd/bgp_ecommunity.h"
128ea8ab 45#include "bgpd/bgp_encap_types.h"
46#include "bgpd/bgp_debug.h"
47#include "bgpd/bgp_aspath.h"
d7d97010 48#include "bgpd/bgp_zebra.h"
db0e1937 49#include "bgpd/bgp_nexthop.h"
128ea8ab 50
51/*
52 * Definitions and external declarations.
53 */
54extern struct zclient *zclient;
55
56DEFINE_QOBJ_TYPE(bgpevpn)
57
58
59/*
60 * Static function declarations
61 */
d62a17ae 62static void delete_evpn_route_entry(struct bgp *bgp, struct bgpevpn *vpn,
63 afi_t afi, safi_t safi, struct bgp_node *rn,
64 struct bgp_info **ri);
65static int delete_all_vni_routes(struct bgp *bgp, struct bgpevpn *vpn);
14c1a7bf 66
67/*
68 * Private functions.
69 */
70
71/*
72 * Make vni hash key.
73 */
d62a17ae 74static unsigned int vni_hash_key_make(void *p)
14c1a7bf 75{
d62a17ae 76 struct bgpevpn *vpn = p;
77 return (jhash_1word(vpn->vni, 0));
14c1a7bf 78}
79
80/*
81 * Comparison function for vni hash
82 */
d62a17ae 83static int vni_hash_cmp(const void *p1, const void *p2)
14c1a7bf 84{
d62a17ae 85 const struct bgpevpn *vpn1 = p1;
86 const struct bgpevpn *vpn2 = p2;
14c1a7bf 87
d62a17ae 88 if (!vpn1 && !vpn2)
89 return 1;
90 if (!vpn1 || !vpn2)
91 return 0;
92 return (vpn1->vni == vpn2->vni);
14c1a7bf 93}
94
10ebe1ab
MK
95/*
96 * Make vrf import route target hash key.
97 */
98static unsigned int vrf_import_rt_hash_key_make(void *p)
99{
100 struct vrf_irt_node *irt = p;
101 char *pnt = irt->rt.val;
102 unsigned int key = 0;
103 int c = 0;
104
105 key += pnt[c];
106 key += pnt[c + 1];
107 key += pnt[c + 2];
108 key += pnt[c + 3];
109 key += pnt[c + 4];
110 key += pnt[c + 5];
111 key += pnt[c + 6];
112 key += pnt[c + 7];
113
114 return key;
115}
116
117/*
118 * Comparison function for vrf import rt hash
119 */
120static int vrf_import_rt_hash_cmp(const void *p1, const void *p2)
121{
122 const struct vrf_irt_node *irt1 = p1;
123 const struct vrf_irt_node *irt2 = p2;
124
125 if (irt1 == NULL && irt2 == NULL)
126 return 1;
127
128 if (irt1 == NULL || irt2 == NULL)
129 return 0;
130
131 return (memcmp(irt1->rt.val, irt2->rt.val, ECOMMUNITY_SIZE) == 0);
132}
133
134/*
135 * Create a new vrf import_rt in default instance
136 */
137static struct vrf_irt_node *vrf_import_rt_new(struct ecommunity_val *rt)
138{
139 struct bgp *bgp_def = NULL;
140 struct vrf_irt_node *irt;
141
142 bgp_def = bgp_get_default();
143 if (!bgp_def) {
144 zlog_err("vrf import rt new - def instance not created yet");
145 return NULL;
146 }
147
148 irt = XCALLOC(MTYPE_BGP_EVPN_VRF_IMPORT_RT,
149 sizeof(struct vrf_irt_node));
150 if (!irt)
151 return NULL;
152
153 irt->rt = *rt;
154 irt->vrfs = list_new();
155
156 /* Add to hash */
157 if (!hash_get(bgp_def->vrf_import_rt_hash, irt, hash_alloc_intern)) {
158 XFREE(MTYPE_BGP_EVPN_VRF_IMPORT_RT, irt);
159 return NULL;
160 }
161
162 return irt;
163}
164
165/*
166 * Free the vrf import rt node
167 */
168static void vrf_import_rt_free(struct vrf_irt_node *irt)
169{
170 struct bgp *bgp_def = NULL;
171
172 bgp_def = bgp_get_default();
173 if (!bgp_def) {
174 zlog_err("vrf import rt free - def instance not created yet");
175 return;
176 }
177
178 hash_release(bgp_def->vrf_import_rt_hash, irt);
179 XFREE(MTYPE_BGP_EVPN_VRF_IMPORT_RT, irt);
180}
181
182/*
183 * Function to lookup Import RT node - used to map a RT to set of
184 * VNIs importing routes with that RT.
185 */
186static struct vrf_irt_node *lookup_vrf_import_rt(struct ecommunity_val *rt)
187{
188 struct bgp *bgp_def = NULL;
189 struct vrf_irt_node *irt;
190 struct vrf_irt_node tmp;
191
192 bgp_def = bgp_get_default();
193 if (!bgp_def) {
194 zlog_err("vrf import rt lookup - def instance not created yet");
195 return NULL;
196 }
197
198 memset(&tmp, 0, sizeof(struct vrf_irt_node));
199 memcpy(&tmp.rt, rt, ECOMMUNITY_SIZE);
200 irt = hash_lookup(bgp_def->vrf_import_rt_hash, &tmp);
201 return irt;
202}
203
204/*
205 * Is specified VRF present on the RT's list of "importing" VRFs?
206 */
207static int is_vrf_present_in_irt_vrfs(struct list *vrfs,
208 struct bgp *bgp_vrf)
209{
210 struct listnode *node = NULL, *nnode = NULL;
211 struct bgp *tmp_bgp_vrf = NULL;
212
213 for (ALL_LIST_ELEMENTS(vrfs, node, nnode, tmp_bgp_vrf)) {
214 if (tmp_bgp_vrf == bgp_vrf)
215 return 1;
216 }
217 return 0;
218}
219
14c1a7bf 220/*
221 * Make import route target hash key.
222 */
d62a17ae 223static unsigned int import_rt_hash_key_make(void *p)
14c1a7bf 224{
d62a17ae 225 struct irt_node *irt = p;
226 char *pnt = irt->rt.val;
227 unsigned int key = 0;
228 int c = 0;
14c1a7bf 229
d62a17ae 230 key += pnt[c];
231 key += pnt[c + 1];
232 key += pnt[c + 2];
233 key += pnt[c + 3];
234 key += pnt[c + 4];
235 key += pnt[c + 5];
236 key += pnt[c + 6];
237 key += pnt[c + 7];
14c1a7bf 238
d62a17ae 239 return (key);
14c1a7bf 240}
241
242/*
243 * Comparison function for import rt hash
244 */
d62a17ae 245static int import_rt_hash_cmp(const void *p1, const void *p2)
14c1a7bf 246{
d62a17ae 247 const struct irt_node *irt1 = p1;
248 const struct irt_node *irt2 = p2;
14c1a7bf 249
d62a17ae 250 if (irt1 == NULL && irt2 == NULL)
251 return 1;
14c1a7bf 252
d62a17ae 253 if (irt1 == NULL || irt2 == NULL)
254 return 0;
14c1a7bf 255
d62a17ae 256 return (memcmp(irt1->rt.val, irt2->rt.val, ECOMMUNITY_SIZE) == 0);
14c1a7bf 257}
258
7724c0a1 259/*
128ea8ab 260 * Create a new import_rt
261 */
d62a17ae 262static struct irt_node *import_rt_new(struct bgp *bgp,
263 struct ecommunity_val *rt)
128ea8ab 264{
d62a17ae 265 struct irt_node *irt;
128ea8ab 266
d62a17ae 267 if (!bgp)
268 return NULL;
128ea8ab 269
d62a17ae 270 irt = XCALLOC(MTYPE_BGP_EVPN_IMPORT_RT, sizeof(struct irt_node));
271 if (!irt)
272 return NULL;
128ea8ab 273
d62a17ae 274 irt->rt = *rt;
275 irt->vnis = list_new();
128ea8ab 276
d62a17ae 277 /* Add to hash */
278 if (!hash_get(bgp->import_rt_hash, irt, hash_alloc_intern)) {
279 XFREE(MTYPE_BGP_EVPN_IMPORT_RT, irt);
280 return NULL;
281 }
128ea8ab 282
d62a17ae 283 return irt;
128ea8ab 284}
285
286/*
287 * Free the import rt node
7724c0a1 288 */
d62a17ae 289static void import_rt_free(struct bgp *bgp, struct irt_node *irt)
7724c0a1 290{
d62a17ae 291 hash_release(bgp->import_rt_hash, irt);
292 XFREE(MTYPE_BGP_EVPN_IMPORT_RT, irt);
7724c0a1 293}
294
14c1a7bf 295/*
128ea8ab 296 * Function to lookup Import RT node - used to map a RT to set of
297 * VNIs importing routes with that RT.
298 */
d62a17ae 299static struct irt_node *lookup_import_rt(struct bgp *bgp,
300 struct ecommunity_val *rt)
128ea8ab 301{
d62a17ae 302 struct irt_node *irt;
303 struct irt_node tmp;
128ea8ab 304
d62a17ae 305 memset(&tmp, 0, sizeof(struct irt_node));
306 memcpy(&tmp.rt, rt, ECOMMUNITY_SIZE);
307 irt = hash_lookup(bgp->import_rt_hash, &tmp);
308 return irt;
128ea8ab 309}
310
311/*
312 * Is specified VNI present on the RT's list of "importing" VNIs?
313 */
d62a17ae 314static int is_vni_present_in_irt_vnis(struct list *vnis, struct bgpevpn *vpn)
128ea8ab 315{
d62a17ae 316 struct listnode *node, *nnode;
317 struct bgpevpn *tmp_vpn;
128ea8ab 318
d62a17ae 319 for (ALL_LIST_ELEMENTS(vnis, node, nnode, tmp_vpn)) {
320 if (tmp_vpn == vpn)
321 return 1;
322 }
128ea8ab 323
d62a17ae 324 return 0;
128ea8ab 325}
326
327/*
328 * Compare Route Targets.
329 */
d62a17ae 330static int evpn_route_target_cmp(struct ecommunity *ecom1,
331 struct ecommunity *ecom2)
128ea8ab 332{
d62a17ae 333 if (ecom1 && !ecom2)
334 return -1;
128ea8ab 335
d62a17ae 336 if (!ecom1 && ecom2)
337 return 1;
128ea8ab 338
d62a17ae 339 if (!ecom1 && !ecom2)
340 return 0;
128ea8ab 341
d62a17ae 342 if (ecom1->str && !ecom2->str)
343 return -1;
128ea8ab 344
d62a17ae 345 if (!ecom1->str && ecom2->str)
346 return 1;
128ea8ab 347
d62a17ae 348 if (!ecom1->str && !ecom2->str)
349 return 0;
128ea8ab 350
d62a17ae 351 return strcmp(ecom1->str, ecom2->str);
128ea8ab 352}
353
354/*
355 * Mask off global-admin field of specified extended community (RT),
356 * just retain the local-admin field.
357 */
d62a17ae 358static inline void mask_ecom_global_admin(struct ecommunity_val *dst,
359 struct ecommunity_val *src)
128ea8ab 360{
d62a17ae 361 u_char type;
128ea8ab 362
d62a17ae 363 type = src->val[0];
364 dst->val[0] = 0;
365 if (type == ECOMMUNITY_ENCODE_AS) {
366 dst->val[2] = dst->val[3] = 0;
367 } else if (type == ECOMMUNITY_ENCODE_AS4
368 || type == ECOMMUNITY_ENCODE_IP) {
369 dst->val[2] = dst->val[3] = 0;
370 dst->val[4] = dst->val[5] = 0;
371 }
128ea8ab 372}
373
10ebe1ab
MK
374/*
375 * Map one RT to specified VRF.
376 * bgp_vrf = BGP vrf instance
377 */
378static void map_vrf_to_rt(struct bgp *bgp_vrf,
379 struct ecommunity_val *eval)
380{
381 struct vrf_irt_node *irt = NULL;
382 struct ecommunity_val eval_tmp;
383
384 /* If using "automatic" RT,
385 * we only care about the local-admin sub-field.
386 * This is to facilitate using L3VNI(VRF-VNI)
387 * as the RT for EBGP peering too.
388 */
389 memcpy(&eval_tmp, eval, ECOMMUNITY_SIZE);
390 if (!CHECK_FLAG(bgp_vrf->vrf_flags,
391 BGP_VRF_IMPORT_RT_CFGD))
392 mask_ecom_global_admin(&eval_tmp, eval);
393
394 irt = lookup_vrf_import_rt(&eval_tmp);
395 if (irt && irt->vrfs)
396 if (is_vrf_present_in_irt_vrfs(irt->vrfs, bgp_vrf))
397 /* Already mapped. */
398 return;
399
400 if (!irt) {
401 irt = vrf_import_rt_new(&eval_tmp);
402 assert(irt);
403 }
404
405 /* Add VRF to the list for this RT. */
406 listnode_add(irt->vrfs, bgp_vrf);
407}
408
409/*
410 * Unmap specified VRF from specified RT. If there are no other
411 * VRFs for this RT, then the RT hash is deleted.
412 * bgp_vrf: BGP VRF specific instance
413 */
414static void unmap_vrf_from_rt(struct bgp *bgp_vrf,
415 struct vrf_irt_node *irt)
416{
417 /* Delete VRF from list for this RT. */
418 listnode_delete(irt->vrfs, bgp_vrf);
419 if (!listnode_head(irt->vrfs)) {
bb7a24ab 420 list_delete_and_null(&irt->vrfs);
10ebe1ab
MK
421 vrf_import_rt_free(irt);
422 }
423}
424
128ea8ab 425/*
426 * Map one RT to specified VNI.
14c1a7bf 427 */
d62a17ae 428static void map_vni_to_rt(struct bgp *bgp, struct bgpevpn *vpn,
429 struct ecommunity_val *eval)
128ea8ab 430{
d62a17ae 431 struct irt_node *irt;
432 struct ecommunity_val eval_tmp;
128ea8ab 433
d62a17ae 434 /* If using "automatic" RT, we only care about the local-admin
435 * sub-field.
436 * This is to facilitate using VNI as the RT for EBGP peering too.
437 */
438 memcpy(&eval_tmp, eval, ECOMMUNITY_SIZE);
439 if (!is_import_rt_configured(vpn))
440 mask_ecom_global_admin(&eval_tmp, eval);
128ea8ab 441
d62a17ae 442 irt = lookup_import_rt(bgp, &eval_tmp);
443 if (irt && irt->vnis)
444 if (is_vni_present_in_irt_vnis(irt->vnis, vpn))
445 /* Already mapped. */
446 return;
128ea8ab 447
d62a17ae 448 if (!irt) {
449 irt = import_rt_new(bgp, &eval_tmp);
450 assert(irt);
451 }
128ea8ab 452
d62a17ae 453 /* Add VNI to the hash list for this RT. */
454 listnode_add(irt->vnis, vpn);
128ea8ab 455}
456
457/*
458 * Unmap specified VNI from specified RT. If there are no other
459 * VNIs for this RT, then the RT hash is deleted.
460 */
d62a17ae 461static void unmap_vni_from_rt(struct bgp *bgp, struct bgpevpn *vpn,
462 struct irt_node *irt)
14c1a7bf 463{
d62a17ae 464 /* Delete VNI from hash list for this RT. */
465 listnode_delete(irt->vnis, vpn);
466 if (!listnode_head(irt->vnis)) {
acdf5e25 467 list_delete_and_null(&irt->vnis);
d62a17ae 468 import_rt_free(bgp, irt);
469 }
14c1a7bf 470}
471
128ea8ab 472/*
473 * Create RT extended community automatically from passed information:
474 * of the form AS:VNI.
475 * NOTE: We use only the lower 16 bits of the AS. This is sufficient as
476 * the need is to get a RT value that will be unique across different
477 * VNIs but the same across routers (in the same AS) for a particular
478 * VNI.
479 */
c581d8b0 480static void form_auto_rt(struct bgp *bgp, vni_t vni, struct list *rtl)
128ea8ab 481{
d62a17ae 482 struct ecommunity_val eval;
483 struct ecommunity *ecomadd;
128ea8ab 484
c581d8b0 485 encode_route_target_as((bgp->as & 0xFFFF), vni, &eval);
128ea8ab 486
d62a17ae 487 ecomadd = ecommunity_new();
488 ecommunity_add_val(ecomadd, &eval);
489 listnode_add_sort(rtl, ecomadd);
128ea8ab 490}
14c1a7bf 491
492/*
128ea8ab 493 * Derive RD and RT for a VNI automatically. Invoked at the time of
494 * creation of a VNI.
495 */
d62a17ae 496static void derive_rd_rt_for_vni(struct bgp *bgp, struct bgpevpn *vpn)
128ea8ab 497{
d62a17ae 498 bgp_evpn_derive_auto_rd(bgp, vpn);
499 bgp_evpn_derive_auto_rt_import(bgp, vpn);
500 bgp_evpn_derive_auto_rt_export(bgp, vpn);
128ea8ab 501}
502
503/*
504 * Add (update) or delete MACIP from zebra.
14c1a7bf 505 */
d62a17ae 506static int bgp_zebra_send_remote_macip(struct bgp *bgp, struct bgpevpn *vpn,
507 struct prefix_evpn *p,
508 struct in_addr remote_vtep_ip, int add,
509 u_char sticky)
510{
511 struct stream *s;
512 int ipa_len;
513 char buf1[ETHER_ADDR_STRLEN];
514 char buf2[INET6_ADDRSTRLEN];
515 char buf3[INET6_ADDRSTRLEN];
516
517 /* Check socket. */
518 if (!zclient || zclient->sock < 0)
519 return 0;
520
521 /* Don't try to register if Zebra doesn't know of this instance. */
522 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp))
523 return 0;
524
525 s = zclient->obuf;
526 stream_reset(s);
527
421bb26a
MK
528 zclient_create_header(s, add ? ZEBRA_REMOTE_MACIP_ADD
529 : ZEBRA_REMOTE_MACIP_DEL,
530 bgp->vrf_id);
d62a17ae 531 stream_putl(s, vpn->vni);
28328ea9 532 stream_put(s, &p->prefix.mac.octet, ETH_ALEN); /* Mac Addr */
d62a17ae 533 /* IP address length and IP address, if any. */
534 if (IS_EVPN_PREFIX_IPADDR_NONE(p))
535 stream_putl(s, 0);
536 else {
537 ipa_len = IS_EVPN_PREFIX_IPADDR_V4(p) ? IPV4_MAX_BYTELEN
538 : IPV6_MAX_BYTELEN;
539 stream_putl(s, ipa_len);
540 stream_put(s, &p->prefix.ip.ip.addr, ipa_len);
541 }
542 stream_put_in_addr(s, &remote_vtep_ip);
543
544 /* TX MAC sticky status */
545 if (add)
546 stream_putc(s, sticky);
547
548 stream_putw_at(s, 0, stream_get_endp(s));
549
550 if (bgp_debug_zebra(NULL))
551 zlog_debug("Tx %s MACIP, VNI %u %sMAC %s IP %s remote VTEP %s",
552 add ? "ADD" : "DEL", vpn->vni,
553 sticky ? "sticky " : "",
554 prefix_mac2str(&p->prefix.mac, buf1, sizeof(buf1)),
555 ipaddr2str(&p->prefix.ip, buf3, sizeof(buf3)),
556 inet_ntop(AF_INET, &remote_vtep_ip, buf2,
557 sizeof(buf2)));
558
559 return zclient_send_message(zclient);
7ef5a232 560}
b18825eb 561
128ea8ab 562/*
563 * Add (update) or delete remote VTEP from zebra.
564 */
d62a17ae 565static int bgp_zebra_send_remote_vtep(struct bgp *bgp, struct bgpevpn *vpn,
566 struct prefix_evpn *p, int add)
128ea8ab 567{
d62a17ae 568 struct stream *s;
128ea8ab 569
d62a17ae 570 /* Check socket. */
571 if (!zclient || zclient->sock < 0)
572 return 0;
128ea8ab 573
d62a17ae 574 /* Don't try to register if Zebra doesn't know of this instance. */
575 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp))
576 return 0;
128ea8ab 577
d62a17ae 578 s = zclient->obuf;
579 stream_reset(s);
128ea8ab 580
421bb26a
MK
581 zclient_create_header(s, add ? ZEBRA_REMOTE_VTEP_ADD
582 : ZEBRA_REMOTE_VTEP_DEL,
583 bgp->vrf_id);
d62a17ae 584 stream_putl(s, vpn->vni);
585 if (IS_EVPN_PREFIX_IPADDR_V4(p))
586 stream_put_in_addr(s, &p->prefix.ip.ipaddr_v4);
587 else if (IS_EVPN_PREFIX_IPADDR_V6(p)) {
588 zlog_err(
589 "Bad remote IP when trying to %s remote VTEP for VNI %u",
590 add ? "ADD" : "DEL", vpn->vni);
591 return -1;
592 }
128ea8ab 593
d62a17ae 594 stream_putw_at(s, 0, stream_get_endp(s));
128ea8ab 595
d62a17ae 596 if (bgp_debug_zebra(NULL))
597 zlog_debug("Tx %s Remote VTEP, VNI %u remote VTEP %s",
598 add ? "ADD" : "DEL", vpn->vni,
599 inet_ntoa(p->prefix.ip.ipaddr_v4));
128ea8ab 600
d62a17ae 601 return zclient_send_message(zclient);
128ea8ab 602}
603
604/*
605 * Build extended communities for EVPN route. RT and ENCAP are
606 * applicable to all routes.
7ec156a9
MK
607 * TODO: currently kernel doesnt support ipv6 routes with ipv4 nexthops.
608 * This means that we can't do symmetric routing for ipv6 hosts routes
609 * in the same way as ipv4 host routes.
610 * We wont attach l3-vni related RTs for ipv6 routes.
611 * For now, We will only adevrtise ipv4 host routes
612 * with L3-VNI related ext-comm.
128ea8ab 613 */
7ec156a9
MK
614static void build_evpn_route_extcomm(struct bgpevpn *vpn, struct attr *attr,
615 afi_t afi)
128ea8ab 616{
d62a17ae 617 struct ecommunity ecom_encap;
618 struct ecommunity ecom_sticky;
bc59a672 619 struct ecommunity ecom_rmac;
d62a17ae 620 struct ecommunity_val eval;
621 struct ecommunity_val eval_sticky;
bc59a672 622 struct ecommunity_val eval_rmac;
d62a17ae 623 bgp_encap_types tnl_type;
624 struct listnode *node, *nnode;
625 struct ecommunity *ecom;
626 u_int32_t seqnum;
7a3e76f1 627 struct list *vrf_export_rtl = NULL;
128ea8ab 628
d62a17ae 629 /* Encap */
630 tnl_type = BGP_ENCAP_TYPE_VXLAN;
631 memset(&ecom_encap, 0, sizeof(ecom_encap));
632 encode_encap_extcomm(tnl_type, &eval);
633 ecom_encap.size = 1;
634 ecom_encap.val = (u_int8_t *)eval.val;
128ea8ab 635
d62a17ae 636 /* Add Encap */
637 attr->ecommunity = ecommunity_dup(&ecom_encap);
128ea8ab 638
7a3e76f1 639 /* Add the export RTs for L2VNI */
d62a17ae 640 for (ALL_LIST_ELEMENTS(vpn->export_rtl, node, nnode, ecom))
641 attr->ecommunity = ecommunity_merge(attr->ecommunity, ecom);
128ea8ab 642
7ec156a9
MK
643 /* Add the export RTs for L3VNI - currently only supported for IPV4 host
644 * routes */
645 if (afi == AFI_IP) {
646 vrf_export_rtl = bgpevpn_get_vrf_export_rtl(vpn);
647 if (vrf_export_rtl && !list_isempty(vrf_export_rtl)) {
648 for (ALL_LIST_ELEMENTS(vrf_export_rtl, node, nnode,
649 ecom))
650 attr->ecommunity =
651 ecommunity_merge(attr->ecommunity,
652 ecom);
653 }
f1f8b53c 654 }
7a3e76f1 655
d62a17ae 656 if (attr->sticky) {
657 seqnum = 0;
658 memset(&ecom_sticky, 0, sizeof(ecom_sticky));
659 encode_mac_mobility_extcomm(1, seqnum, &eval_sticky);
660 ecom_sticky.size = 1;
661 ecom_sticky.val = (u_int8_t *)eval_sticky.val;
662 attr->ecommunity =
663 ecommunity_merge(attr->ecommunity, &ecom_sticky);
664 }
c85c03c7 665
7ec156a9 666 if (afi == AFI_IP && !is_zero_mac(&attr->rmac)) {
bc59a672
MK
667 memset(&ecom_rmac, 0, sizeof(ecom_rmac));
668 encode_rmac_extcomm(&eval_rmac, &attr->rmac);
669 ecom_rmac.size = 1;
670 ecom_rmac.val = (uint8_t *)eval_rmac.val;
671 attr->ecommunity = ecommunity_merge(attr->ecommunity,
672 &ecom_rmac);
673 }
674
d62a17ae 675 attr->flag |= ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES);
128ea8ab 676}
677
678/*
679 * Add MAC mobility extended community to attribute.
680 */
d62a17ae 681static void add_mac_mobility_to_attr(u_int32_t seq_num, struct attr *attr)
682{
683 struct ecommunity ecom_tmp;
684 struct ecommunity_val eval;
421bb26a 685 u_int8_t *ecom_val_ptr;
d62a17ae 686 int i;
687 u_int8_t *pnt;
688 int type = 0;
689 int sub_type = 0;
690
691 /* Build MM */
692 encode_mac_mobility_extcomm(0, seq_num, &eval);
693
694 /* Find current MM ecommunity */
421bb26a 695 ecom_val_ptr = NULL;
d62a17ae 696
697 if (attr->ecommunity) {
698 for (i = 0; i < attr->ecommunity->size; i++) {
699 pnt = attr->ecommunity->val + (i * 8);
700 type = *pnt++;
701 sub_type = *pnt++;
702
703 if (type == ECOMMUNITY_ENCODE_EVPN
704 && sub_type
705 == ECOMMUNITY_EVPN_SUBTYPE_MACMOBILITY) {
421bb26a
MK
706 ecom_val_ptr =
707 (u_int8_t *)(attr->ecommunity->val
708 + (i * 8));
d62a17ae 709 break;
710 }
711 }
712 }
713
714 /* Update the existing MM ecommunity */
421bb26a
MK
715 if (ecom_val_ptr) {
716 memcpy(ecom_val_ptr, eval.val, sizeof(char) * ECOMMUNITY_SIZE);
d62a17ae 717 }
718 /* Add MM to existing */
719 else {
720 memset(&ecom_tmp, 0, sizeof(ecom_tmp));
721 ecom_tmp.size = 1;
722 ecom_tmp.val = (u_int8_t *)eval.val;
723
724 attr->ecommunity =
725 ecommunity_merge(attr->ecommunity, &ecom_tmp);
726 }
128ea8ab 727}
728
729/* Install EVPN route into zebra. */
d62a17ae 730static int evpn_zebra_install(struct bgp *bgp, struct bgpevpn *vpn,
731 struct prefix_evpn *p,
732 struct in_addr remote_vtep_ip, u_char sticky)
128ea8ab 733{
d62a17ae 734 int ret;
128ea8ab 735
d62a17ae 736 if (p->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE)
737 ret = bgp_zebra_send_remote_macip(bgp, vpn, p, remote_vtep_ip,
738 1, sticky);
739 else
740 ret = bgp_zebra_send_remote_vtep(bgp, vpn, p, 1);
128ea8ab 741
d62a17ae 742 return ret;
128ea8ab 743}
744
745/* Uninstall EVPN route from zebra. */
d62a17ae 746static int evpn_zebra_uninstall(struct bgp *bgp, struct bgpevpn *vpn,
747 struct prefix_evpn *p,
748 struct in_addr remote_vtep_ip)
128ea8ab 749{
d62a17ae 750 int ret;
128ea8ab 751
d62a17ae 752 if (p->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE)
753 ret = bgp_zebra_send_remote_macip(bgp, vpn, p, remote_vtep_ip,
754 0, 0);
755 else
756 ret = bgp_zebra_send_remote_vtep(bgp, vpn, p, 0);
128ea8ab 757
d62a17ae 758 return ret;
128ea8ab 759}
760
761/*
762 * Due to MAC mobility, the prior "local" best route has been supplanted
763 * by a "remote" best route. The prior route has to be deleted and withdrawn
764 * from peers.
765 */
d62a17ae 766static void evpn_delete_old_local_route(struct bgp *bgp, struct bgpevpn *vpn,
767 struct bgp_node *rn,
768 struct bgp_info *old_local)
128ea8ab 769{
d62a17ae 770 struct bgp_node *global_rn;
771 struct bgp_info *ri;
772 afi_t afi = AFI_L2VPN;
773 safi_t safi = SAFI_EVPN;
128ea8ab 774
d62a17ae 775 /* Locate route node in the global EVPN routing table. Note that
776 * this table is a 2-level tree (RD-level + Prefix-level) similar to
777 * L3VPN routes.
778 */
779 global_rn = bgp_afi_node_lookup(bgp->rib[afi][safi], afi, safi,
780 (struct prefix *)&rn->p, &vpn->prd);
781 if (global_rn) {
782 /* Delete route entry in the global EVPN table. */
783 delete_evpn_route_entry(bgp, vpn, afi, safi, global_rn, &ri);
128ea8ab 784
d62a17ae 785 /* Schedule for processing - withdraws to peers happen from
786 * this table.
787 */
788 if (ri)
789 bgp_process(bgp, global_rn, afi, safi);
790 bgp_unlock_node(global_rn);
791 }
128ea8ab 792
d62a17ae 793 /* Delete route entry in the VNI route table, caller to remove. */
794 bgp_info_delete(rn, old_local);
128ea8ab 795}
796
797/*
798 * Calculate the best path for an EVPN route. Install/update best path in zebra,
799 * if appropriate.
800 */
d62a17ae 801static int evpn_route_select_install(struct bgp *bgp, struct bgpevpn *vpn,
802 struct bgp_node *rn)
803{
804 struct bgp_info *old_select, *new_select;
805 struct bgp_info_pair old_and_new;
806 afi_t afi = AFI_L2VPN;
807 safi_t safi = SAFI_EVPN;
808 int ret = 0;
809
810 /* Compute the best path. */
811 bgp_best_selection(bgp, rn, &bgp->maxpaths[afi][safi], &old_and_new,
812 afi, safi);
813 old_select = old_and_new.old;
814 new_select = old_and_new.new;
815
816 /* If the best path hasn't changed - see if there is still something to
817 * update
818 * to zebra RIB.
819 */
820 if (old_select && old_select == new_select
821 && old_select->type == ZEBRA_ROUTE_BGP
822 && old_select->sub_type == BGP_ROUTE_NORMAL
823 && !CHECK_FLAG(rn->flags, BGP_NODE_USER_CLEAR)
824 && !CHECK_FLAG(old_select->flags, BGP_INFO_ATTR_CHANGED)
825 && !bgp->addpath_tx_used[afi][safi]) {
826 if (bgp_zebra_has_route_changed(rn, old_select))
827 ret = evpn_zebra_install(bgp, vpn,
828 (struct prefix_evpn *)&rn->p,
829 old_select->attr->nexthop,
830 old_select->attr->sticky);
831 UNSET_FLAG(old_select->flags, BGP_INFO_MULTIPATH_CHG);
832 bgp_zebra_clear_route_change_flags(rn);
833 return ret;
834 }
835
836 /* If the user did a "clear" this flag will be set */
837 UNSET_FLAG(rn->flags, BGP_NODE_USER_CLEAR);
838
839 /* bestpath has changed; update relevant fields and install or uninstall
840 * into the zebra RIB.
841 */
842 if (old_select || new_select)
843 bgp_bump_version(rn);
844
845 if (old_select)
846 bgp_info_unset_flag(rn, old_select, BGP_INFO_SELECTED);
847 if (new_select) {
848 bgp_info_set_flag(rn, new_select, BGP_INFO_SELECTED);
849 bgp_info_unset_flag(rn, new_select, BGP_INFO_ATTR_CHANGED);
850 UNSET_FLAG(new_select->flags, BGP_INFO_MULTIPATH_CHG);
851 }
852
853 if (new_select && new_select->type == ZEBRA_ROUTE_BGP
854 && new_select->sub_type == BGP_ROUTE_NORMAL) {
855 ret = evpn_zebra_install(bgp, vpn, (struct prefix_evpn *)&rn->p,
856 new_select->attr->nexthop,
857 new_select->attr->sticky);
858 /* If an old best existed and it was a "local" route, the only
859 * reason
860 * it would be supplanted is due to MAC mobility procedures. So,
861 * we
862 * need to do an implicit delete and withdraw that route from
863 * peers.
864 */
865 if (old_select && old_select->peer == bgp->peer_self
866 && old_select->type == ZEBRA_ROUTE_BGP
867 && old_select->sub_type == BGP_ROUTE_STATIC)
868 evpn_delete_old_local_route(bgp, vpn, rn, old_select);
869 } else {
870 if (old_select && old_select->type == ZEBRA_ROUTE_BGP
871 && old_select->sub_type == BGP_ROUTE_NORMAL)
872 ret = evpn_zebra_uninstall(bgp, vpn,
873 (struct prefix_evpn *)&rn->p,
874 old_select->attr->nexthop);
875 }
876
877 /* Clear any route change flags. */
878 bgp_zebra_clear_route_change_flags(rn);
879
880 /* Reap old select bgp_info, if it has been removed */
881 if (old_select && CHECK_FLAG(old_select->flags, BGP_INFO_REMOVED))
882 bgp_info_reap(rn, old_select);
883
884 return ret;
128ea8ab 885}
886
c85c03c7 887
888/*
889 * Return true if the local ri for this rn has sticky set
890 */
d62a17ae 891static int evpn_route_is_sticky(struct bgp *bgp, struct bgp_node *rn)
c85c03c7 892{
d62a17ae 893 struct bgp_info *tmp_ri;
894 struct bgp_info *local_ri;
c85c03c7 895
d62a17ae 896 local_ri = NULL;
897 for (tmp_ri = rn->info; tmp_ri; tmp_ri = tmp_ri->next) {
898 if (tmp_ri->peer == bgp->peer_self
899 && tmp_ri->type == ZEBRA_ROUTE_BGP
900 && tmp_ri->sub_type == BGP_ROUTE_STATIC)
901 local_ri = tmp_ri;
902 }
c85c03c7 903
d62a17ae 904 if (!local_ri)
905 return 0;
c85c03c7 906
d62a17ae 907 return local_ri->attr->sticky;
c85c03c7 908}
909
128ea8ab 910/*
911 * Create or update EVPN route entry. This could be in the VNI route table
912 * or the global route table.
913 */
d62a17ae 914static int update_evpn_route_entry(struct bgp *bgp, struct bgpevpn *vpn,
915 afi_t afi, safi_t safi, struct bgp_node *rn,
916 struct attr *attr, int add, int vni_table,
1a98c087 917 struct bgp_info **ri, u_char flags)
d62a17ae 918{
919 struct bgp_info *tmp_ri;
920 struct bgp_info *local_ri, *remote_ri;
921 struct attr *attr_new;
922 mpls_label_t label = MPLS_INVALID_LABEL;
923 int route_change = 1;
924 u_char sticky = 0;
925
926 *ri = NULL;
927
928 /* See if this is an update of an existing route, or a new add. Also,
929 * identify if already known from remote, and if so, the one with the
930 * highest sequence number; this is only when adding to the VNI routing
931 * table.
932 */
933 local_ri = remote_ri = NULL;
934 for (tmp_ri = rn->info; tmp_ri; tmp_ri = tmp_ri->next) {
935 if (tmp_ri->peer == bgp->peer_self
936 && tmp_ri->type == ZEBRA_ROUTE_BGP
937 && tmp_ri->sub_type == BGP_ROUTE_STATIC)
938 local_ri = tmp_ri;
939 if (vni_table) {
940 if (tmp_ri->type == ZEBRA_ROUTE_BGP
941 && tmp_ri->sub_type == BGP_ROUTE_NORMAL
942 && CHECK_FLAG(tmp_ri->flags, BGP_INFO_VALID)) {
943 if (!remote_ri)
944 remote_ri = tmp_ri;
945 else if (mac_mobility_seqnum(tmp_ri->attr)
946 > mac_mobility_seqnum(remote_ri->attr))
947 remote_ri = tmp_ri;
948 }
949 }
950 }
951
952 /* If route doesn't exist already, create a new one, if told to.
953 * Otherwise act based on whether the attributes of the route have
954 * changed or not.
955 */
956 if (!local_ri && !add)
957 return 0;
958
959 if (!local_ri) {
960 /* When learnt locally for the first time but already known from
961 * remote, we have to initiate appropriate MAC mobility steps.
962 * This
963 * is applicable when updating the VNI routing table.
1a98c087
MK
964 * We need to skip mobility steps for g/w macs (local mac on g/w
965 * SVI) advertised in EVPN.
966 * This will ensure that local routes are preferred for g/w macs
d62a17ae 967 */
1a98c087 968 if (remote_ri && !CHECK_FLAG(flags, ZEBRA_MAC_TYPE_GW)) {
d62a17ae 969 u_int32_t cur_seqnum;
970
971 /* Add MM extended community to route. */
972 cur_seqnum = mac_mobility_seqnum(remote_ri->attr);
973 add_mac_mobility_to_attr(cur_seqnum + 1, attr);
974 }
975
976 /* Add (or update) attribute to hash. */
977 attr_new = bgp_attr_intern(attr);
978
979 /* Extract MAC mobility sequence number, if any. */
980 attr_new->mm_seqnum =
981 bgp_attr_mac_mobility_seqnum(attr_new, &sticky);
982 attr_new->sticky = sticky;
983
984 /* Create new route with its attribute. */
985 tmp_ri = info_make(ZEBRA_ROUTE_BGP, BGP_ROUTE_STATIC, 0,
986 bgp->peer_self, attr_new, rn);
987 SET_FLAG(tmp_ri->flags, BGP_INFO_VALID);
988 bgp_info_extra_get(tmp_ri);
989
990 /* The VNI goes into the 'label' field of the route */
991 vni2label(vpn->vni, &label);
992
993 memcpy(&tmp_ri->extra->label, &label, BGP_LABEL_BYTES);
994 bgp_info_add(rn, tmp_ri);
995 } else {
996 tmp_ri = local_ri;
997 if (attrhash_cmp(tmp_ri->attr, attr)
998 && !CHECK_FLAG(tmp_ri->flags, BGP_INFO_REMOVED))
999 route_change = 0;
1000 else {
1001 /* The attribute has changed. */
1002 /* Add (or update) attribute to hash. */
1003 attr_new = bgp_attr_intern(attr);
1004 bgp_info_set_flag(rn, tmp_ri, BGP_INFO_ATTR_CHANGED);
1005
1006 /* Restore route, if needed. */
1007 if (CHECK_FLAG(tmp_ri->flags, BGP_INFO_REMOVED))
1008 bgp_info_restore(rn, tmp_ri);
1009
1010 /* Unintern existing, set to new. */
1011 bgp_attr_unintern(&tmp_ri->attr);
1012 tmp_ri->attr = attr_new;
1013 tmp_ri->uptime = bgp_clock();
1014 }
1015 }
1016
1017 /* Return back the route entry. */
1018 *ri = tmp_ri;
1019 return route_change;
128ea8ab 1020}
1021
1022/*
1023 * Create or update EVPN route (of type based on prefix) for specified VNI
1024 * and schedule for processing.
1025 */
d62a17ae 1026static int update_evpn_route(struct bgp *bgp, struct bgpevpn *vpn,
1a98c087 1027 struct prefix_evpn *p, u_char flags)
128ea8ab 1028{
d62a17ae 1029 struct bgp_node *rn;
1030 struct attr attr;
1031 struct attr *attr_new;
1032 struct bgp_info *ri;
1033 afi_t afi = AFI_L2VPN;
1034 safi_t safi = SAFI_EVPN;
1035 int route_change;
128ea8ab 1036
d62a17ae 1037 memset(&attr, 0, sizeof(struct attr));
128ea8ab 1038
d62a17ae 1039 /* Build path-attribute for this route. */
1040 bgp_attr_default_set(&attr, BGP_ORIGIN_IGP);
1041 attr.nexthop = vpn->originator_ip;
1042 attr.mp_nexthop_global_in = vpn->originator_ip;
1043 attr.mp_nexthop_len = BGP_ATTR_NHLEN_IPV4;
1a98c087 1044 attr.sticky = CHECK_FLAG(flags, ZEBRA_MAC_TYPE_STICKY) ? 1 : 0;
bc59a672 1045 bgpevpn_get_rmac(vpn, &attr.rmac);
128ea8ab 1046
d62a17ae 1047 /* Set up RT and ENCAP extended community. */
7ec156a9
MK
1048 build_evpn_route_extcomm(vpn, &attr,
1049 IS_EVPN_PREFIX_IPADDR_V4(p) ?
1050 AFI_IP : AFI_IP6);
128ea8ab 1051
d62a17ae 1052 /* First, create (or fetch) route node within the VNI. */
1053 /* NOTE: There is no RD here. */
1054 rn = bgp_node_get(vpn->route_table, (struct prefix *)p);
128ea8ab 1055
d62a17ae 1056 /* Create or update route entry. */
1057 route_change = update_evpn_route_entry(bgp, vpn, afi, safi, rn, &attr,
1a98c087 1058 1, 1, &ri, flags);
d62a17ae 1059 assert(ri);
1060 attr_new = ri->attr;
128ea8ab 1061
d62a17ae 1062 /* Perform route selection; this is just to set the flags correctly
1063 * as local route in the VNI always wins.
1064 */
1065 evpn_route_select_install(bgp, vpn, rn);
1066 bgp_unlock_node(rn);
128ea8ab 1067
d62a17ae 1068 /* If this is a new route or some attribute has changed, export the
1069 * route to the global table. The route will be advertised to peers
1070 * from there. Note that this table is a 2-level tree (RD-level +
1071 * Prefix-level) similar to L3VPN routes.
1072 */
1073 if (route_change) {
1074 struct bgp_info *global_ri;
128ea8ab 1075
d62a17ae 1076 rn = bgp_afi_node_get(bgp->rib[afi][safi], afi, safi,
1077 (struct prefix *)p, &vpn->prd);
1078 update_evpn_route_entry(bgp, vpn, afi, safi, rn, attr_new, 1, 0,
1a98c087 1079 &global_ri, flags);
128ea8ab 1080
d62a17ae 1081 /* Schedule for processing and unlock node. */
1082 bgp_process(bgp, rn, afi, safi);
1083 bgp_unlock_node(rn);
1084 }
128ea8ab 1085
d62a17ae 1086 /* Unintern temporary. */
1087 aspath_unintern(&attr.aspath);
128ea8ab 1088
d62a17ae 1089 return 0;
128ea8ab 1090}
1091
1092/*
1093 * Delete EVPN route entry. This could be in the VNI route table
1094 * or the global route table.
1095 */
d62a17ae 1096static void delete_evpn_route_entry(struct bgp *bgp, struct bgpevpn *vpn,
1097 afi_t afi, safi_t safi, struct bgp_node *rn,
1098 struct bgp_info **ri)
128ea8ab 1099{
d62a17ae 1100 struct bgp_info *tmp_ri;
128ea8ab 1101
d62a17ae 1102 *ri = NULL;
128ea8ab 1103
d62a17ae 1104 /* Now, find matching route. */
1105 for (tmp_ri = rn->info; tmp_ri; tmp_ri = tmp_ri->next)
1106 if (tmp_ri->peer == bgp->peer_self
1107 && tmp_ri->type == ZEBRA_ROUTE_BGP
1108 && tmp_ri->sub_type == BGP_ROUTE_STATIC)
1109 break;
128ea8ab 1110
d62a17ae 1111 *ri = tmp_ri;
128ea8ab 1112
d62a17ae 1113 /* Mark route for delete. */
1114 if (tmp_ri)
1115 bgp_info_delete(rn, tmp_ri);
128ea8ab 1116}
1117
1118/*
1119 * Delete EVPN route (of type based on prefix) for specified VNI and
1120 * schedule for processing.
1121 */
d62a17ae 1122static int delete_evpn_route(struct bgp *bgp, struct bgpevpn *vpn,
1123 struct prefix_evpn *p)
1124{
1125 struct bgp_node *rn, *global_rn;
1126 struct bgp_info *ri;
1127 afi_t afi = AFI_L2VPN;
1128 safi_t safi = SAFI_EVPN;
1129
1130 /* First, locate the route node within the VNI. If it doesn't exist,
1131 * there
1132 * is nothing further to do.
1133 */
1134 /* NOTE: There is no RD here. */
1135 rn = bgp_node_lookup(vpn->route_table, (struct prefix *)p);
1136 if (!rn)
1137 return 0;
1138
1139 /* Next, locate route node in the global EVPN routing table. Note that
1140 * this table is a 2-level tree (RD-level + Prefix-level) similar to
1141 * L3VPN routes.
1142 */
1143 global_rn = bgp_afi_node_lookup(bgp->rib[afi][safi], afi, safi,
1144 (struct prefix *)p, &vpn->prd);
1145 if (global_rn) {
1146 /* Delete route entry in the global EVPN table. */
1147 delete_evpn_route_entry(bgp, vpn, afi, safi, global_rn, &ri);
1148
1149 /* Schedule for processing - withdraws to peers happen from
1150 * this table.
1151 */
1152 if (ri)
1153 bgp_process(bgp, global_rn, afi, safi);
1154 bgp_unlock_node(global_rn);
1155 }
1156
1157 /* Delete route entry in the VNI route table. This can just be removed.
1158 */
1159 delete_evpn_route_entry(bgp, vpn, afi, safi, rn, &ri);
1160 if (ri)
1161 bgp_info_reap(rn, ri);
1162 bgp_unlock_node(rn);
1163
1164 return 0;
128ea8ab 1165}
1166
1167/*
1168 * Update all type-2 (MACIP) local routes for this VNI - these should also
1169 * be scheduled for advertise to peers.
1170 */
d62a17ae 1171static int update_all_type2_routes(struct bgp *bgp, struct bgpevpn *vpn)
1172{
1173 afi_t afi;
1174 safi_t safi;
1175 struct bgp_node *rn;
1176 struct bgp_info *ri;
1177 struct attr attr;
1178 struct attr attr_sticky;
7ec156a9
MK
1179 struct attr attr_ip6;
1180 struct attr attr_sticky_ip6;
d62a17ae 1181 struct attr *attr_new;
1182
1183 afi = AFI_L2VPN;
1184 safi = SAFI_EVPN;
1185 memset(&attr, 0, sizeof(struct attr));
1186 memset(&attr_sticky, 0, sizeof(struct attr));
7ec156a9
MK
1187 memset(&attr_ip6, 0, sizeof(struct attr));
1188 memset(&attr_sticky_ip6, 0, sizeof(struct attr));
d62a17ae 1189
1190 /* Build path-attribute - all type-2 routes for this VNI will share the
1191 * same path attribute.
1192 */
1193 bgp_attr_default_set(&attr, BGP_ORIGIN_IGP);
1194 bgp_attr_default_set(&attr_sticky, BGP_ORIGIN_IGP);
1195 attr.nexthop = vpn->originator_ip;
1196 attr.mp_nexthop_global_in = vpn->originator_ip;
1197 attr.mp_nexthop_len = BGP_ATTR_NHLEN_IPV4;
bc59a672 1198 bgpevpn_get_rmac(vpn, &attr.rmac);
d62a17ae 1199 attr_sticky.nexthop = vpn->originator_ip;
1200 attr_sticky.mp_nexthop_global_in = vpn->originator_ip;
1201 attr_sticky.mp_nexthop_len = BGP_ATTR_NHLEN_IPV4;
1202 attr_sticky.sticky = 1;
bc59a672 1203 bgpevpn_get_rmac(vpn, &attr_sticky.rmac);
7ec156a9
MK
1204 bgp_attr_default_set(&attr_ip6, BGP_ORIGIN_IGP);
1205 bgp_attr_default_set(&attr_sticky_ip6, BGP_ORIGIN_IGP);
1206 attr_ip6.nexthop = vpn->originator_ip;
1207 attr_ip6.mp_nexthop_global_in = vpn->originator_ip;
1208 attr_ip6.mp_nexthop_len = BGP_ATTR_NHLEN_IPV4;
1209 bgpevpn_get_rmac(vpn, &attr_ip6.rmac);
1210 attr_sticky_ip6.nexthop = vpn->originator_ip;
1211 attr_sticky_ip6.mp_nexthop_global_in = vpn->originator_ip;
1212 attr_sticky_ip6.mp_nexthop_len = BGP_ATTR_NHLEN_IPV4;
1213 attr_sticky_ip6.sticky = 1;
1214 bgpevpn_get_rmac(vpn, &attr_sticky_ip6.rmac);
d62a17ae 1215
1216 /* Set up RT, ENCAP and sticky MAC extended community. */
7ec156a9
MK
1217 build_evpn_route_extcomm(vpn, &attr, AFI_IP);
1218 build_evpn_route_extcomm(vpn, &attr_sticky, AFI_IP);
1219 build_evpn_route_extcomm(vpn, &attr_ip6, AFI_IP6);
1220 build_evpn_route_extcomm(vpn, &attr_sticky_ip6, AFI_IP6);
d62a17ae 1221
1222 /* Walk this VNI's route table and update local type-2 routes. For any
1223 * routes updated, update corresponding entry in the global table too.
1224 */
1225 for (rn = bgp_table_top(vpn->route_table); rn;
1226 rn = bgp_route_next(rn)) {
1227 struct prefix_evpn *evp = (struct prefix_evpn *)&rn->p;
1228 struct bgp_node *rd_rn;
1229 struct bgp_info *global_ri;
1230
1231 if (evp->prefix.route_type != BGP_EVPN_MAC_IP_ROUTE)
1232 continue;
1233
7ec156a9
MK
1234 if (IS_EVPN_PREFIX_IPADDR_V4(evp)) {
1235 if (evpn_route_is_sticky(bgp, rn))
1236 update_evpn_route_entry(bgp, vpn, afi, safi, rn,
1237 &attr_sticky, 0, 1,
1238 &ri, 0);
1239 else
1240 update_evpn_route_entry(bgp, vpn, afi, safi, rn,
1241 &attr, 0, 1, &ri, 0);
1242 } else {
1243 if (evpn_route_is_sticky(bgp, rn))
1244 update_evpn_route_entry(bgp, vpn, afi, safi, rn,
1245 &attr_sticky_ip6, 0, 1,
1246 &ri, 0);
1247 else
1248 update_evpn_route_entry(bgp, vpn, afi, safi, rn,
1249 &attr_ip6, 0, 1,
1250 &ri, 0);
1251 }
d62a17ae 1252
1253 /* If a local route exists for this prefix, we need to update
1254 * the global routing table too.
1255 */
1256 if (!ri)
1257 continue;
1258
1259 /* Perform route selection; this is just to set the flags
1260 * correctly
1261 * as local route in the VNI always wins.
1262 */
1263 evpn_route_select_install(bgp, vpn, rn);
1264
1265 attr_new = ri->attr;
1266
1267 /* Update route in global routing table. */
1268 rd_rn = bgp_afi_node_get(bgp->rib[afi][safi], afi, safi,
1269 (struct prefix *)evp, &vpn->prd);
1270 assert(rd_rn);
1271 update_evpn_route_entry(bgp, vpn, afi, safi, rd_rn, attr_new, 0,
1a98c087 1272 0, &global_ri, 0);
d62a17ae 1273
1274 /* Schedule for processing and unlock node. */
1275 bgp_process(bgp, rd_rn, afi, safi);
1276 bgp_unlock_node(rd_rn);
1277 }
1278
1279 /* Unintern temporary. */
1280 aspath_unintern(&attr.aspath);
1281 aspath_unintern(&attr_sticky.aspath);
1282
1283 return 0;
128ea8ab 1284}
1285
1286/*
1287 * Delete all type-2 (MACIP) local routes for this VNI - only from the
1288 * global routing table. These are also scheduled for withdraw from peers.
1289 */
d62a17ae 1290static int delete_global_type2_routes(struct bgp *bgp, struct bgpevpn *vpn)
128ea8ab 1291{
d62a17ae 1292 afi_t afi;
1293 safi_t safi;
1294 struct bgp_node *rdrn, *rn;
1295 struct bgp_table *table;
1296 struct bgp_info *ri;
128ea8ab 1297
d62a17ae 1298 afi = AFI_L2VPN;
1299 safi = SAFI_EVPN;
128ea8ab 1300
d62a17ae 1301 rdrn = bgp_node_lookup(bgp->rib[afi][safi], (struct prefix *)&vpn->prd);
1302 if (rdrn && rdrn->info) {
1303 table = (struct bgp_table *)rdrn->info;
1304 for (rn = bgp_table_top(table); rn; rn = bgp_route_next(rn)) {
1305 struct prefix_evpn *evp = (struct prefix_evpn *)&rn->p;
128ea8ab 1306
d62a17ae 1307 if (evp->prefix.route_type != BGP_EVPN_MAC_IP_ROUTE)
1308 continue;
128ea8ab 1309
d62a17ae 1310 delete_evpn_route_entry(bgp, vpn, afi, safi, rn, &ri);
1311 if (ri)
1312 bgp_process(bgp, rn, afi, safi);
1313 }
1314 }
128ea8ab 1315
d62a17ae 1316 /* Unlock RD node. */
1317 if (rdrn)
1318 bgp_unlock_node(rdrn);
128ea8ab 1319
d62a17ae 1320 return 0;
128ea8ab 1321}
1322
1323/*
1324 * Delete all type-2 (MACIP) local routes for this VNI - from the global
1325 * table as well as the per-VNI route table.
1326 */
d62a17ae 1327static int delete_all_type2_routes(struct bgp *bgp, struct bgpevpn *vpn)
128ea8ab 1328{
d62a17ae 1329 afi_t afi;
1330 safi_t safi;
1331 struct bgp_node *rn;
1332 struct bgp_info *ri;
128ea8ab 1333
d62a17ae 1334 afi = AFI_L2VPN;
1335 safi = SAFI_EVPN;
128ea8ab 1336
d62a17ae 1337 /* First, walk the global route table for this VNI's type-2 local
1338 * routes.
1339 * EVPN routes are a 2-level table, first get the RD table.
1340 */
1341 delete_global_type2_routes(bgp, vpn);
128ea8ab 1342
d62a17ae 1343 /* Next, walk this VNI's route table and delete local type-2 routes. */
1344 for (rn = bgp_table_top(vpn->route_table); rn;
1345 rn = bgp_route_next(rn)) {
1346 struct prefix_evpn *evp = (struct prefix_evpn *)&rn->p;
128ea8ab 1347
d62a17ae 1348 if (evp->prefix.route_type != BGP_EVPN_MAC_IP_ROUTE)
1349 continue;
128ea8ab 1350
d62a17ae 1351 delete_evpn_route_entry(bgp, vpn, afi, safi, rn, &ri);
128ea8ab 1352
d62a17ae 1353 /* Route entry in local table gets deleted immediately. */
1354 if (ri)
1355 bgp_info_reap(rn, ri);
1356 }
128ea8ab 1357
d62a17ae 1358 return 0;
128ea8ab 1359}
1360
1361/*
1362 * Delete all routes in the per-VNI route table.
1363 */
d62a17ae 1364static int delete_all_vni_routes(struct bgp *bgp, struct bgpevpn *vpn)
128ea8ab 1365{
d62a17ae 1366 struct bgp_node *rn;
1367 struct bgp_info *ri, *nextri;
128ea8ab 1368
d62a17ae 1369 /* Walk this VNI's route table and delete all routes. */
1370 for (rn = bgp_table_top(vpn->route_table); rn;
1371 rn = bgp_route_next(rn)) {
1372 for (ri = rn->info; (ri != NULL) && (nextri = ri->next, 1);
1373 ri = nextri) {
1374 bgp_info_delete(rn, ri);
1375 bgp_info_reap(rn, ri);
1376 }
1377 }
128ea8ab 1378
d62a17ae 1379 return 0;
128ea8ab 1380}
1381
1382/*
1383 * Update (and advertise) local routes for a VNI. Invoked upon the VNI
1384 * export RT getting modified or change to tunnel IP. Note that these
1385 * situations need the route in the per-VNI table as well as the global
1386 * table to be updated (as attributes change).
1387 */
d62a17ae 1388static int update_routes_for_vni(struct bgp *bgp, struct bgpevpn *vpn)
128ea8ab 1389{
d62a17ae 1390 int ret;
1391 struct prefix_evpn p;
128ea8ab 1392
d62a17ae 1393 /* Update and advertise the type-3 route (only one) followed by the
1394 * locally learnt type-2 routes (MACIP) - for this VNI.
1395 */
1396 build_evpn_type3_prefix(&p, vpn->originator_ip);
1397 ret = update_evpn_route(bgp, vpn, &p, 0);
1398 if (ret)
1399 return ret;
128ea8ab 1400
d62a17ae 1401 return update_all_type2_routes(bgp, vpn);
128ea8ab 1402}
1403
1404/*
1405 * Delete (and withdraw) local routes for specified VNI from the global
1406 * table and per-VNI table. After this, remove all other routes from
1407 * the per-VNI table. Invoked upon the VNI being deleted or EVPN
1408 * (advertise-all-vni) being disabled.
1409 */
d62a17ae 1410static int delete_routes_for_vni(struct bgp *bgp, struct bgpevpn *vpn)
128ea8ab 1411{
d62a17ae 1412 int ret;
1413 struct prefix_evpn p;
128ea8ab 1414
d62a17ae 1415 /* Delete and withdraw locally learnt type-2 routes (MACIP)
1416 * followed by type-3 routes (only one) - for this VNI.
1417 */
1418 ret = delete_all_type2_routes(bgp, vpn);
1419 if (ret)
1420 return ret;
128ea8ab 1421
d62a17ae 1422 build_evpn_type3_prefix(&p, vpn->originator_ip);
1423 ret = delete_evpn_route(bgp, vpn, &p);
1424 if (ret)
1425 return ret;
128ea8ab 1426
d62a17ae 1427 /* Delete all routes from the per-VNI table. */
1428 return delete_all_vni_routes(bgp, vpn);
128ea8ab 1429}
1430
1431/*
1432 * There is a tunnel endpoint IP address change for this VNI,
1433 * need to re-advertise routes with the new nexthop.
1434 */
d62a17ae 1435static int handle_tunnel_ip_change(struct bgp *bgp, struct bgpevpn *vpn,
1436 struct in_addr originator_ip)
128ea8ab 1437{
d62a17ae 1438 struct prefix_evpn p;
128ea8ab 1439
ddd16ed5
MK
1440 /* If VNI is not live, we only need to update the originator ip */
1441 if (!is_vni_live(vpn)) {
1442 vpn->originator_ip = originator_ip;
1443 return 0;
1444 }
1445
db0e1937
MK
1446 /* Update the tunnel-ip hash */
1447 bgp_tip_del(bgp, &vpn->originator_ip);
1448 bgp_tip_add(bgp, &originator_ip);
1449
1450 /* filter routes as martian nexthop db has changed */
1451 bgp_filter_evpn_routes_upon_martian_nh_change(bgp);
1452
d62a17ae 1453 /* Need to withdraw type-3 route as the originator IP is part
1454 * of the key.
1455 */
1456 build_evpn_type3_prefix(&p, vpn->originator_ip);
1457 delete_evpn_route(bgp, vpn, &p);
128ea8ab 1458
d62a17ae 1459 /* Update the tunnel IP and re-advertise all routes for this VNI. */
1460 vpn->originator_ip = originator_ip;
1461 return update_routes_for_vni(bgp, vpn);
128ea8ab 1462}
1463
d3135ba3 1464/*
1465 * Install route entry into the VRF routing table and invoke route selection.
1466 */
1467static int install_evpn_route_entry_in_vrf(struct bgp *bgp_vrf,
1468 struct prefix_evpn *evp,
1469 struct bgp_info *parent_ri)
1470{
1471 struct bgp_node *rn;
1472 struct bgp_info *ri;
1473 struct attr *attr_new;
c4edf708 1474 int ret = 0;
d3135ba3 1475 struct prefix p;
1476 struct prefix *pp = &p;
1477 afi_t afi = 0;
1478 safi_t safi = 0;
1eb88002
MK
1479 char buf[PREFIX_STRLEN];
1480 char buf1[PREFIX_STRLEN];
d3135ba3 1481
1482 memset(pp, 0, sizeof(struct prefix));
1483 ip_prefix_from_type2_prefix(evp, pp);
1484
1eb88002
MK
1485 if (bgp_debug_zebra(NULL)) {
1486 zlog_debug("installing evpn prefix %s as ip prefix %s in vrf %s",
1487 prefix2str(evp, buf, sizeof(buf)),
1488 prefix2str(pp, buf1, sizeof(buf)),
1489 vrf_id_to_name(bgp_vrf->vrf_id));
1490 }
1491
d3135ba3 1492 /* Create (or fetch) route within the VRF. */
1493 /* NOTE: There is no RD here. */
1494 if (IS_EVPN_PREFIX_IPADDR_V4(evp)) {
1495 afi = AFI_IP;
1496 safi = SAFI_UNICAST;
1497 rn = bgp_node_get(bgp_vrf->rib[afi][safi], pp);
1498 } else if (IS_EVPN_PREFIX_IPADDR_V6(evp)) {
1499 afi = AFI_IP6;
1500 safi = SAFI_UNICAST;
1501 rn = bgp_node_get(bgp_vrf->rib[afi][safi], pp);
1502 } else
1503 return 0;
1504
1505 /* Check if route entry is already present. */
1506 for (ri = rn->info; ri; ri = ri->next)
1507 if (ri->extra
1508 && (struct bgp_info *)ri->extra->parent == parent_ri)
1509 break;
1510
1511 if (!ri) {
1512 /* Add (or update) attribute to hash. */
1513 attr_new = bgp_attr_intern(parent_ri->attr);
1514
1515 /* Create new route with its attribute. */
1516 ri = info_make(parent_ri->type, parent_ri->sub_type, 0,
1517 parent_ri->peer, attr_new, rn);
1518 SET_FLAG(ri->flags, BGP_INFO_VALID);
1519 bgp_info_extra_get(ri);
1520 ri->extra->parent = parent_ri;
1521 if (parent_ri->extra)
1522 memcpy(&ri->extra->label, &parent_ri->extra->label,
1523 BGP_LABEL_BYTES);
1524 bgp_info_add(rn, ri);
1525 } else {
1526 if (attrhash_cmp(ri->attr, parent_ri->attr)
1527 && !CHECK_FLAG(ri->flags, BGP_INFO_REMOVED)) {
1528 bgp_unlock_node(rn);
1529 return 0;
1530 }
1531 /* The attribute has changed. */
1532 /* Add (or update) attribute to hash. */
1533 attr_new = bgp_attr_intern(parent_ri->attr);
1534
1535 /* Restore route, if needed. */
1536 if (CHECK_FLAG(ri->flags, BGP_INFO_REMOVED))
1537 bgp_info_restore(rn, ri);
1538
1539 /* Mark if nexthop has changed. */
1540 if (!IPV4_ADDR_SAME(&ri->attr->nexthop, &attr_new->nexthop))
1541 SET_FLAG(ri->flags, BGP_INFO_IGP_CHANGED);
1542
1543 /* Unintern existing, set to new. */
1544 bgp_attr_unintern(&ri->attr);
1545 ri->attr = attr_new;
1546 ri->uptime = bgp_clock();
1547 }
1548
1549 /* Perform route selection and update zebra, if required. */
1eb88002 1550 bgp_process(bgp_vrf, rn, afi, safi);
d3135ba3 1551
1552 return ret;
1553}
1554
128ea8ab 1555/*
1556 * Install route entry into the VNI routing table and invoke route selection.
1557 */
d62a17ae 1558static int install_evpn_route_entry(struct bgp *bgp, struct bgpevpn *vpn,
1559 struct prefix_evpn *p,
1560 struct bgp_info *parent_ri)
1561{
1562 struct bgp_node *rn;
1563 struct bgp_info *ri;
1564 struct attr *attr_new;
1565 int ret;
1566
1567 /* Create (or fetch) route within the VNI. */
1568 /* NOTE: There is no RD here. */
1569 rn = bgp_node_get(vpn->route_table, (struct prefix *)p);
1570
1571 /* Check if route entry is already present. */
1572 for (ri = rn->info; ri; ri = ri->next)
1573 if (ri->extra
1574 && (struct bgp_info *)ri->extra->parent == parent_ri)
1575 break;
1576
1577 if (!ri) {
1578 /* Add (or update) attribute to hash. */
1579 attr_new = bgp_attr_intern(parent_ri->attr);
1580
1581 /* Create new route with its attribute. */
1582 ri = info_make(parent_ri->type, parent_ri->sub_type, 0,
1583 parent_ri->peer, attr_new, rn);
1584 SET_FLAG(ri->flags, BGP_INFO_VALID);
1585 bgp_info_extra_get(ri);
1586 ri->extra->parent = parent_ri;
1587 if (parent_ri->extra)
1588 memcpy(&ri->extra->label, &parent_ri->extra->label,
1589 BGP_LABEL_BYTES);
1590 bgp_info_add(rn, ri);
1591 } else {
1592 if (attrhash_cmp(ri->attr, parent_ri->attr)
1593 && !CHECK_FLAG(ri->flags, BGP_INFO_REMOVED)) {
1594 bgp_unlock_node(rn);
1595 return 0;
1596 }
1597 /* The attribute has changed. */
1598 /* Add (or update) attribute to hash. */
1599 attr_new = bgp_attr_intern(parent_ri->attr);
1600
1601 /* Restore route, if needed. */
1602 if (CHECK_FLAG(ri->flags, BGP_INFO_REMOVED))
1603 bgp_info_restore(rn, ri);
1604
1605 /* Mark if nexthop has changed. */
1606 if (!IPV4_ADDR_SAME(&ri->attr->nexthop, &attr_new->nexthop))
1607 SET_FLAG(ri->flags, BGP_INFO_IGP_CHANGED);
1608
1609 /* Unintern existing, set to new. */
1610 bgp_attr_unintern(&ri->attr);
1611 ri->attr = attr_new;
1612 ri->uptime = bgp_clock();
1613 }
1614
1615 /* Perform route selection and update zebra, if required. */
1616 ret = evpn_route_select_install(bgp, vpn, rn);
1617
1618 return ret;
128ea8ab 1619}
1620
d3135ba3 1621/*
1622 * Uninstall route entry from the VRF routing table and send message
1623 * to zebra, if appropriate.
1624 */
1625static int uninstall_evpn_route_entry_in_vrf(struct bgp *bgp_vrf,
1626 struct prefix_evpn *evp,
1627 struct bgp_info *parent_ri)
1628{
1629 struct bgp_node *rn;
1630 struct bgp_info *ri;
c4edf708 1631 int ret = 0;
d3135ba3 1632 struct prefix p;
1633 struct prefix *pp = &p;
1634 afi_t afi = 0;
1635 safi_t safi = 0;
1eb88002
MK
1636 char buf[PREFIX_STRLEN];
1637 char buf1[PREFIX_STRLEN];
d3135ba3 1638
1639 memset(pp, 0, sizeof(struct prefix));
1640 ip_prefix_from_type2_prefix(evp, pp);
1641
1eb88002 1642 if (bgp_debug_zebra(NULL)) {
30a30f57 1643 zlog_debug("uninstalling evpn prefix %s as ip prefix %s in vrf %s",
1eb88002
MK
1644 prefix2str(evp, buf, sizeof(buf)),
1645 prefix2str(pp, buf1, sizeof(buf)),
1646 vrf_id_to_name(bgp_vrf->vrf_id));
1647 }
1648
d3135ba3 1649 /* Locate route within the VRF. */
1650 /* NOTE: There is no RD here. */
1651 if (IS_EVPN_PREFIX_IPADDR_V4(evp)) {
1652 afi = AFI_IP;
1653 safi = SAFI_UNICAST;
1654 rn = bgp_node_lookup(bgp_vrf->rib[afi][safi], pp);
1655 } else {
1656 afi = AFI_IP6;
1657 safi = SAFI_UNICAST;
1658 rn = bgp_node_lookup(bgp_vrf->rib[afi][safi], pp);
1659 }
1660
1661 if (!rn)
1662 return 0;
1663
1664 /* Find matching route entry. */
1665 for (ri = rn->info; ri; ri = ri->next)
1666 if (ri->extra
1667 && (struct bgp_info *)ri->extra->parent == parent_ri)
1668 break;
1669
1670 if (!ri)
1671 return 0;
1672
1673 /* Mark entry for deletion */
1674 bgp_info_delete(rn, ri);
1675
1676 /* Perform route selection and update zebra, if required. */
1eb88002 1677 bgp_process(bgp_vrf, rn, afi, safi);
d3135ba3 1678
1679 /* Unlock route node. */
1680 bgp_unlock_node(rn);
1681
1682 return ret;
1683}
1684
128ea8ab 1685/*
1686 * Uninstall route entry from the VNI routing table and send message
1687 * to zebra, if appropriate.
1688 */
d62a17ae 1689static int uninstall_evpn_route_entry(struct bgp *bgp, struct bgpevpn *vpn,
1690 struct prefix_evpn *p,
1691 struct bgp_info *parent_ri)
128ea8ab 1692{
d62a17ae 1693 struct bgp_node *rn;
1694 struct bgp_info *ri;
1695 int ret;
128ea8ab 1696
d62a17ae 1697 /* Locate route within the VNI. */
1698 /* NOTE: There is no RD here. */
1699 rn = bgp_node_lookup(vpn->route_table, (struct prefix *)p);
1700 if (!rn)
1701 return 0;
128ea8ab 1702
d62a17ae 1703 /* Find matching route entry. */
1704 for (ri = rn->info; ri; ri = ri->next)
1705 if (ri->extra
1706 && (struct bgp_info *)ri->extra->parent == parent_ri)
1707 break;
128ea8ab 1708
d62a17ae 1709 if (!ri)
1710 return 0;
128ea8ab 1711
d62a17ae 1712 /* Mark entry for deletion */
1713 bgp_info_delete(rn, ri);
128ea8ab 1714
d62a17ae 1715 /* Perform route selection and update zebra, if required. */
1716 ret = evpn_route_select_install(bgp, vpn, rn);
128ea8ab 1717
d62a17ae 1718 /* Unlock route node. */
1719 bgp_unlock_node(rn);
128ea8ab 1720
d62a17ae 1721 return ret;
128ea8ab 1722}
1723
5ba238b7
MK
1724/*
1725 * Given a route entry and a VRF, see if this route entry should be
1726 * imported into the VRF i.e., RTs match.
1727 */
1728static int is_route_matching_for_vrf(struct bgp *bgp_vrf,
1729 struct bgp_info *ri)
1730{
1731 struct attr *attr = ri->attr;
1732 struct ecommunity *ecom;
1733 int i;
1734
1735 assert(attr);
1736 /* Route should have valid RT to be even considered. */
1737 if (!(attr->flag & ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES)))
1738 return 0;
1739
1740 ecom = attr->ecommunity;
1741 if (!ecom || !ecom->size)
1742 return 0;
1743
1744 /* For each extended community RT, see if it matches this VNI. If any RT
1745 * matches, we're done.
1746 */
1747 for (i = 0; i < ecom->size; i++) {
1748 u_char *pnt;
1749 u_char type, sub_type;
1750 struct ecommunity_val *eval;
1751 struct ecommunity_val eval_tmp;
1752 struct vrf_irt_node *irt;
1753
1754 /* Only deal with RTs */
1755 pnt = (ecom->val + (i * ECOMMUNITY_SIZE));
1756 eval = (struct ecommunity_val *)(ecom->val
1757 + (i * ECOMMUNITY_SIZE));
1758 type = *pnt++;
1759 sub_type = *pnt++;
1760 if (sub_type != ECOMMUNITY_ROUTE_TARGET)
1761 continue;
1762
1763 /* See if this RT matches specified VNIs import RTs */
1764 irt = lookup_vrf_import_rt(eval);
1765 if (irt && irt->vrfs)
1766 if (is_vrf_present_in_irt_vrfs(irt->vrfs, bgp_vrf))
1767 return 1;
1768
1769 /* Also check for non-exact match. In this, we mask out the AS
1770 * and
1771 * only check on the local-admin sub-field. This is to
1772 * facilitate using
1773 * VNI as the RT for EBGP peering too.
1774 */
1775 irt = NULL;
1776 if (type == ECOMMUNITY_ENCODE_AS
1777 || type == ECOMMUNITY_ENCODE_AS4
1778 || type == ECOMMUNITY_ENCODE_IP) {
1779 memcpy(&eval_tmp, eval, ECOMMUNITY_SIZE);
1780 mask_ecom_global_admin(&eval_tmp, eval);
1781 irt = lookup_vrf_import_rt(&eval_tmp);
1782 }
1783 if (irt && irt->vrfs)
1784 if (is_vrf_present_in_irt_vrfs(irt->vrfs, bgp_vrf))
1785 return 1;
1786 }
1787
1788 return 0;
1789}
1790
128ea8ab 1791/*
1792 * Given a route entry and a VNI, see if this route entry should be
1793 * imported into the VNI i.e., RTs match.
1794 */
d62a17ae 1795static int is_route_matching_for_vni(struct bgp *bgp, struct bgpevpn *vpn,
1796 struct bgp_info *ri)
1797{
1798 struct attr *attr = ri->attr;
1799 struct ecommunity *ecom;
1800 int i;
1801
1802 assert(attr);
1803 /* Route should have valid RT to be even considered. */
1804 if (!(attr->flag & ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES)))
1805 return 0;
1806
1807 ecom = attr->ecommunity;
1808 if (!ecom || !ecom->size)
1809 return 0;
1810
1811 /* For each extended community RT, see if it matches this VNI. If any RT
1812 * matches, we're done.
1813 */
1814 for (i = 0; i < ecom->size; i++) {
1815 u_char *pnt;
1816 u_char type, sub_type;
1817 struct ecommunity_val *eval;
1818 struct ecommunity_val eval_tmp;
1819 struct irt_node *irt;
1820
1821 /* Only deal with RTs */
1822 pnt = (ecom->val + (i * ECOMMUNITY_SIZE));
1823 eval = (struct ecommunity_val *)(ecom->val
1824 + (i * ECOMMUNITY_SIZE));
1825 type = *pnt++;
1826 sub_type = *pnt++;
1827 if (sub_type != ECOMMUNITY_ROUTE_TARGET)
1828 continue;
1829
1830 /* See if this RT matches specified VNIs import RTs */
1831 irt = lookup_import_rt(bgp, eval);
1832 if (irt && irt->vnis)
1833 if (is_vni_present_in_irt_vnis(irt->vnis, vpn))
1834 return 1;
1835
1836 /* Also check for non-exact match. In this, we mask out the AS
1837 * and
1838 * only check on the local-admin sub-field. This is to
1839 * facilitate using
1840 * VNI as the RT for EBGP peering too.
1841 */
1842 irt = NULL;
1843 if (type == ECOMMUNITY_ENCODE_AS
1844 || type == ECOMMUNITY_ENCODE_AS4
1845 || type == ECOMMUNITY_ENCODE_IP) {
1846 memcpy(&eval_tmp, eval, ECOMMUNITY_SIZE);
1847 mask_ecom_global_admin(&eval_tmp, eval);
1848 irt = lookup_import_rt(bgp, &eval_tmp);
1849 }
1850 if (irt && irt->vnis)
1851 if (is_vni_present_in_irt_vnis(irt->vnis, vpn))
1852 return 1;
1853 }
1854
1855 return 0;
128ea8ab 1856}
1857
5ba238b7
MK
1858/*
1859 * Install or uninstall mac-ip routes are appropriate for this
1860 * particular VRF.
1861 */
1862static int install_uninstall_routes_for_vrf(struct bgp *bgp_vrf,
1863 int install)
1864{
1865 afi_t afi;
1866 safi_t safi;
1867 struct bgp_node *rd_rn, *rn;
1868 struct bgp_table *table;
1869 struct bgp_info *ri;
1870 int ret;
1871 char buf[PREFIX_STRLEN];
1872 struct bgp *bgp_def = NULL;
1873
1874 afi = AFI_L2VPN;
1875 safi = SAFI_EVPN;
1876 bgp_def = bgp_get_default();
1877 if (!bgp_def)
1878 return -1;
1879
1880 /* Walk entire global routing table and evaluate routes which could be
1881 * imported into this VRF. Note that we need to loop through all global
1882 * routes to determine which route matches the import rt on vrf
1883 */
1884 for (rd_rn = bgp_table_top(bgp_def->rib[afi][safi]); rd_rn;
1885 rd_rn = bgp_route_next(rd_rn)) {
1886 table = (struct bgp_table *)(rd_rn->info);
1887 if (!table)
1888 continue;
1889
1890 for (rn = bgp_table_top(table); rn; rn = bgp_route_next(rn)) {
1891 struct prefix_evpn *evp = (struct prefix_evpn *)&rn->p;
1892
1eb88002 1893 /* if not mac-ip route skip this route */
5ba238b7
MK
1894 if (evp->prefix.route_type != BGP_EVPN_MAC_IP_ROUTE)
1895 continue;
1896
1eb88002
MK
1897 /* if not a mac+ip route skip this route */
1898 if (!(IS_EVPN_PREFIX_IPADDR_V4(evp) ||
1899 IS_EVPN_PREFIX_IPADDR_V6(evp)))
1900 continue;
1901
5ba238b7
MK
1902 for (ri = rn->info; ri; ri = ri->next) {
1903 /* Consider "valid" remote routes applicable for
1904 * this VRF. */
1905 if (!(CHECK_FLAG(ri->flags, BGP_INFO_VALID)
1906 && ri->type == ZEBRA_ROUTE_BGP
1907 && ri->sub_type == BGP_ROUTE_NORMAL))
1908 continue;
1909
1910 if (is_route_matching_for_vrf(bgp_vrf, ri)) {
1911 if (install)
1912 ret =
1913 install_evpn_route_entry_in_vrf(
1914 bgp_vrf, evp, ri);
1915 else
1916 ret =
1917 uninstall_evpn_route_entry_in_vrf(
1918 bgp_vrf, evp, ri);
1919
1920 if (ret) {
1921 zlog_err(
1922 "Failed to %s EVPN %s route in VRF %s",
1923 install ? "install"
1924 : "uninstall",
1925 prefix2str(evp, buf,
1926 sizeof(buf)),
1927 vrf_id_to_name(bgp_vrf->vrf_id));
1928 return ret;
1929 }
1930 }
1931 }
1932 }
1933 }
1934
1935 return 0;
1936}
1937
128ea8ab 1938/*
1939 * Install or uninstall routes of specified type that are appropriate for this
1940 * particular VNI.
1941 */
d62a17ae 1942static int install_uninstall_routes_for_vni(struct bgp *bgp,
1943 struct bgpevpn *vpn,
1944 bgp_evpn_route_type rtype,
1945 int install)
1946{
0291c246
MK
1947 afi_t afi;
1948 safi_t safi;
1949 struct bgp_node *rd_rn, *rn;
1950 struct bgp_table *table;
1951 struct bgp_info *ri;
1952 int ret;
d62a17ae 1953
1954 afi = AFI_L2VPN;
1955 safi = SAFI_EVPN;
1956
1957 /* Walk entire global routing table and evaluate routes which could be
1958 * imported into this VPN. Note that we cannot just look at the routes
1959 * for
1960 * the VNI's RD - remote routes applicable for this VNI could have any
1961 * RD.
1962 */
1963 /* EVPN routes are a 2-level table. */
1964 for (rd_rn = bgp_table_top(bgp->rib[afi][safi]); rd_rn;
1965 rd_rn = bgp_route_next(rd_rn)) {
1966 table = (struct bgp_table *)(rd_rn->info);
1967 if (!table)
1968 continue;
1969
1970 for (rn = bgp_table_top(table); rn; rn = bgp_route_next(rn)) {
1971 struct prefix_evpn *evp = (struct prefix_evpn *)&rn->p;
1972
1973 if (evp->prefix.route_type != rtype)
1974 continue;
1975
1976 for (ri = rn->info; ri; ri = ri->next) {
1977 /* Consider "valid" remote routes applicable for
1978 * this VNI. */
1979 if (!(CHECK_FLAG(ri->flags, BGP_INFO_VALID)
1980 && ri->type == ZEBRA_ROUTE_BGP
1981 && ri->sub_type == BGP_ROUTE_NORMAL))
1982 continue;
1983
1984 if (is_route_matching_for_vni(bgp, vpn, ri)) {
1985 if (install)
1986 ret = install_evpn_route_entry(
60466a63 1987 bgp, vpn, evp, ri);
d62a17ae 1988 else
1989 ret = uninstall_evpn_route_entry(
1990 bgp, vpn, evp, ri);
1991
1992 if (ret) {
1993 zlog_err(
1994 "%u: Failed to %s EVPN %s route in VNI %u",
1995 bgp->vrf_id,
1996 install ? "install"
1997 : "uninstall",
1998 rtype == BGP_EVPN_MAC_IP_ROUTE
1999 ? "MACIP"
2000 : "IMET",
2001 vpn->vni);
2002 return ret;
2003 }
2004 }
2005 }
2006 }
2007 }
2008
2009 return 0;
128ea8ab 2010}
2011
5ba238b7
MK
2012/* Install any existing remote routes applicable for this VRF into VRF RIB. This
2013 * is invoked upon l3vni-add or l3vni import rt change */
2014static int install_routes_for_vrf(struct bgp *bgp_vrf)
2015{
2016 install_uninstall_routes_for_vrf(bgp_vrf, 1);
2017 return 0;
2018}
2019
128ea8ab 2020/*
2021 * Install any existing remote routes applicable for this VNI into its
2022 * routing table. This is invoked when a VNI becomes "live" or its Import
2023 * RT is changed.
2024 */
d62a17ae 2025static int install_routes_for_vni(struct bgp *bgp, struct bgpevpn *vpn)
128ea8ab 2026{
d62a17ae 2027 int ret;
128ea8ab 2028
d62a17ae 2029 /* Install type-3 routes followed by type-2 routes - the ones applicable
2030 * for this VNI.
2031 */
2032 ret = install_uninstall_routes_for_vni(bgp, vpn, BGP_EVPN_IMET_ROUTE,
2033 1);
2034 if (ret)
2035 return ret;
128ea8ab 2036
d62a17ae 2037 return install_uninstall_routes_for_vni(bgp, vpn, BGP_EVPN_MAC_IP_ROUTE,
2038 1);
128ea8ab 2039}
2040
5ba238b7
MK
2041/* uninstall routes from l3vni vrf. */
2042static int uninstall_routes_for_vrf(struct bgp *bgp_vrf)
2043{
2044 install_uninstall_routes_for_vrf(bgp_vrf, 0);
2045 return 0;
2046}
2047
90e60aa7 2048/*
2049 * Uninstall any existing remote routes for this VNI. One scenario in which
2050 * this is invoked is upon an import RT change.
2051 */
d62a17ae 2052static int uninstall_routes_for_vni(struct bgp *bgp, struct bgpevpn *vpn)
90e60aa7 2053{
d62a17ae 2054 int ret;
90e60aa7 2055
d62a17ae 2056 /* Uninstall type-2 routes followed by type-3 routes - the ones
2057 * applicable
2058 * for this VNI.
2059 */
2060 ret = install_uninstall_routes_for_vni(bgp, vpn, BGP_EVPN_MAC_IP_ROUTE,
2061 0);
2062 if (ret)
2063 return ret;
90e60aa7 2064
d62a17ae 2065 return install_uninstall_routes_for_vni(bgp, vpn, BGP_EVPN_IMET_ROUTE,
2066 0);
90e60aa7 2067}
2068
d3135ba3 2069/*
2070 * Install or uninstall route in matching VRFs (list).
2071 */
2072static int install_uninstall_route_in_vrfs(struct bgp *bgp_def, afi_t afi,
2073 safi_t safi, struct prefix_evpn *evp,
2074 struct bgp_info *ri,
2075 struct list *vrfs, int install)
2076{
2077 char buf[PREFIX2STR_BUFFER];
2078 struct bgp *bgp_vrf;
2079 struct listnode *node, *nnode;
2080
2081 /* Only type-2 routes go into a VRF */
2082 if (!(evp->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE))
2083 return 0;
2084
30a30f57
MK
2085 /* if not a mac+ip route skip this route */
2086 if (!(IS_EVPN_PREFIX_IPADDR_V4(evp) ||
2087 IS_EVPN_PREFIX_IPADDR_V6(evp)))
2088 return 0;
2089
d3135ba3 2090 for (ALL_LIST_ELEMENTS(vrfs, node, nnode, bgp_vrf)) {
2091 int ret;
2092
2093 if (install)
2094 ret = install_evpn_route_entry_in_vrf(bgp_vrf,
2095 evp, ri);
2096 else
2097 ret = uninstall_evpn_route_entry_in_vrf(bgp_vrf,
2098 evp, ri);
2099
2100 if (ret) {
2101 zlog_err("%u: Failed to %s prefix %s in VRF %s",
2102 bgp_def->vrf_id,
2103 install ? "install" : "uninstall",
2104 prefix2str(evp, buf, sizeof(buf)),
2105 vrf_id_to_name(bgp_vrf->vrf_id));
2106 return ret;
2107 }
2108 }
2109
2110 return 0;
2111}
2112
128ea8ab 2113/*
2114 * Install or uninstall route in matching VNIs (list).
2115 */
d62a17ae 2116static int install_uninstall_route_in_vnis(struct bgp *bgp, afi_t afi,
2117 safi_t safi, struct prefix_evpn *evp,
2118 struct bgp_info *ri,
2119 struct list *vnis, int install)
128ea8ab 2120{
d62a17ae 2121 struct bgpevpn *vpn;
2122 struct listnode *node, *nnode;
128ea8ab 2123
d62a17ae 2124 for (ALL_LIST_ELEMENTS(vnis, node, nnode, vpn)) {
2125 int ret;
128ea8ab 2126
d62a17ae 2127 if (!is_vni_live(vpn))
2128 continue;
128ea8ab 2129
d62a17ae 2130 if (install)
2131 ret = install_evpn_route_entry(bgp, vpn, evp, ri);
2132 else
2133 ret = uninstall_evpn_route_entry(bgp, vpn, evp, ri);
128ea8ab 2134
d62a17ae 2135 if (ret) {
2136 zlog_err("%u: Failed to %s EVPN %s route in VNI %u",
2137 bgp->vrf_id, install ? "install" : "uninstall",
2138 evp->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE
2139 ? "MACIP"
2140 : "IMET",
2141 vpn->vni);
2142 return ret;
2143 }
2144 }
128ea8ab 2145
d62a17ae 2146 return 0;
128ea8ab 2147}
2148
2149/*
2150 * Install or uninstall route for appropriate VNIs.
2151 */
d62a17ae 2152static int install_uninstall_evpn_route(struct bgp *bgp, afi_t afi, safi_t safi,
2153 struct prefix *p, struct bgp_info *ri,
2154 int import)
2155{
2156 struct prefix_evpn *evp = (struct prefix_evpn *)p;
2157 struct attr *attr = ri->attr;
2158 struct ecommunity *ecom;
2159 int i;
2160
2161 assert(attr);
2162
2163 /* Only type-2 and type-3 routes go into a L2 VNI. */
2164 if (!(evp->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE
2165 || evp->prefix.route_type == BGP_EVPN_IMET_ROUTE))
2166 return 0;
2167
2168 /* If we don't have Route Target, nothing much to do. */
2169 if (!(attr->flag & ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES)))
2170 return 0;
2171
2172 ecom = attr->ecommunity;
2173 if (!ecom || !ecom->size)
2174 return -1;
2175
2176 /* For each extended community RT, see which VNIs match and import
2177 * the route into matching VNIs.
2178 */
2179 for (i = 0; i < ecom->size; i++) {
2180 u_char *pnt;
2181 u_char type, sub_type;
2182 struct ecommunity_val *eval;
2183 struct ecommunity_val eval_tmp;
d3135ba3 2184 struct irt_node *irt; /* import rt for l2vni */
2185 struct vrf_irt_node *vrf_irt; /* import rt for l3vni */
d62a17ae 2186
2187 /* Only deal with RTs */
2188 pnt = (ecom->val + (i * ECOMMUNITY_SIZE));
2189 eval = (struct ecommunity_val *)(ecom->val
2190 + (i * ECOMMUNITY_SIZE));
2191 type = *pnt++;
2192 sub_type = *pnt++;
2193 if (sub_type != ECOMMUNITY_ROUTE_TARGET)
2194 continue;
2195
d3135ba3 2196 /* Import route into matching l2-vnis */
d62a17ae 2197 irt = lookup_import_rt(bgp, eval);
2198 if (irt && irt->vnis)
2199 install_uninstall_route_in_vnis(bgp, afi, safi, evp, ri,
2200 irt->vnis, import);
2201
d3135ba3 2202 /* Import route into matching l3-vnis (vrfs) */
2203 vrf_irt = lookup_vrf_import_rt(eval);
2204 if (vrf_irt && vrf_irt->vrfs)
2205 install_uninstall_route_in_vrfs(bgp, afi, safi, evp, ri,
2206 vrf_irt->vrfs, import);
2207
d62a17ae 2208 /* Also check for non-exact match. In this, we mask out the AS
2209 * and
2210 * only check on the local-admin sub-field. This is to
2211 * facilitate using
2212 * VNI as the RT for EBGP peering too.
2213 */
2214 irt = NULL;
d3135ba3 2215 vrf_irt = NULL;
d62a17ae 2216 if (type == ECOMMUNITY_ENCODE_AS
2217 || type == ECOMMUNITY_ENCODE_AS4
2218 || type == ECOMMUNITY_ENCODE_IP) {
2219 memcpy(&eval_tmp, eval, ECOMMUNITY_SIZE);
2220 mask_ecom_global_admin(&eval_tmp, eval);
2221 irt = lookup_import_rt(bgp, &eval_tmp);
d3135ba3 2222 vrf_irt = lookup_vrf_import_rt(&eval_tmp);
d62a17ae 2223 }
2224 if (irt && irt->vnis)
2225 install_uninstall_route_in_vnis(bgp, afi, safi, evp, ri,
2226 irt->vnis, import);
d3135ba3 2227 if (vrf_irt && vrf_irt->vrfs)
2228 install_uninstall_route_in_vrfs(bgp, afi, safi, evp,
2229 ri, vrf_irt->vrfs,
2230 import);
d62a17ae 2231 }
2232
2233 return 0;
128ea8ab 2234}
2235
676f83b9 2236/*
2237 * update and advertise local routes for a VRF as type-5 routes.
2238 * This is invoked upon RD change for a VRF. Note taht the processing is only
2239 * done in the global route table using the routes which already exist in the
2240 * VRF routing table
2241 */
2242static int update_advertise_vrf_routes(struct bgp *bgp_vrf)
2243{
2244 return 0;
2245}
2246
2247/*
2248 * Delete and withdraw all type-5 routes for the RD corresponding to VRF.
2249 * This is invoked upon VRF RD change. The processing is done only from global
2250 * table.
2251 */
2252static int delete_withdraw_vrf_routes(struct bgp *bgp_vrf)
2253{
2254 return 0;
2255}
2256
90e60aa7 2257/*
2258 * Update and advertise local routes for a VNI. Invoked upon router-id
2259 * change. Note that the processing is done only on the global route table
2260 * using routes that already exist in the per-VNI table.
2261 */
d62a17ae 2262static int update_advertise_vni_routes(struct bgp *bgp, struct bgpevpn *vpn)
2263{
2264 struct prefix_evpn p;
2265 struct bgp_node *rn, *global_rn;
2266 struct bgp_info *ri, *global_ri;
2267 struct attr *attr;
2268 afi_t afi = AFI_L2VPN;
2269 safi_t safi = SAFI_EVPN;
2270
2271 /* Locate type-3 route for VNI in the per-VNI table and use its
2272 * attributes to create and advertise the type-3 route for this VNI
2273 * in the global table.
2274 */
2275 build_evpn_type3_prefix(&p, vpn->originator_ip);
2276 rn = bgp_node_lookup(vpn->route_table, (struct prefix *)&p);
2277 if (!rn) /* unexpected */
2278 return 0;
2279 for (ri = rn->info; ri; ri = ri->next)
2280 if (ri->peer == bgp->peer_self && ri->type == ZEBRA_ROUTE_BGP
2281 && ri->sub_type == BGP_ROUTE_STATIC)
2282 break;
2283 if (!ri) /* unexpected */
2284 return 0;
2285 attr = ri->attr;
2286
2287 global_rn = bgp_afi_node_get(bgp->rib[afi][safi], afi, safi,
2288 (struct prefix *)&p, &vpn->prd);
1a98c087
MK
2289 update_evpn_route_entry(bgp, vpn, afi, safi, global_rn, attr, 1, 0, &ri,
2290 0);
d62a17ae 2291
2292 /* Schedule for processing and unlock node. */
2293 bgp_process(bgp, global_rn, afi, safi);
2294 bgp_unlock_node(global_rn);
2295
2296 /* Now, walk this VNI's route table and use the route and its attribute
2297 * to create and schedule route in global table.
2298 */
2299 for (rn = bgp_table_top(vpn->route_table); rn;
2300 rn = bgp_route_next(rn)) {
2301 struct prefix_evpn *evp = (struct prefix_evpn *)&rn->p;
2302
2303 /* Identify MAC-IP local routes. */
2304 if (evp->prefix.route_type != BGP_EVPN_MAC_IP_ROUTE)
2305 continue;
2306
2307 for (ri = rn->info; ri; ri = ri->next)
2308 if (ri->peer == bgp->peer_self
2309 && ri->type == ZEBRA_ROUTE_BGP
2310 && ri->sub_type == BGP_ROUTE_STATIC)
2311 break;
2312 if (!ri)
2313 continue;
2314
2315 /* Create route in global routing table using this route entry's
2316 * attribute.
2317 */
2318 attr = ri->attr;
2319 global_rn = bgp_afi_node_get(bgp->rib[afi][safi], afi, safi,
2320 (struct prefix *)evp, &vpn->prd);
2321 assert(global_rn);
2322 update_evpn_route_entry(bgp, vpn, afi, safi, global_rn, attr, 1,
1a98c087 2323 0, &global_ri, 0);
d62a17ae 2324
2325 /* Schedule for processing and unlock node. */
2326 bgp_process(bgp, global_rn, afi, safi);
2327 bgp_unlock_node(global_rn);
2328 }
2329
2330 return 0;
90e60aa7 2331}
2332
2333/*
2334 * Delete (and withdraw) local routes for a VNI - only from the global
2335 * table. Invoked upon router-id change.
2336 */
d62a17ae 2337static int delete_withdraw_vni_routes(struct bgp *bgp, struct bgpevpn *vpn)
90e60aa7 2338{
d62a17ae 2339 int ret;
2340 struct prefix_evpn p;
2341 struct bgp_node *global_rn;
2342 struct bgp_info *ri;
2343 afi_t afi = AFI_L2VPN;
2344 safi_t safi = SAFI_EVPN;
90e60aa7 2345
d62a17ae 2346 /* Delete and withdraw locally learnt type-2 routes (MACIP)
2347 * for this VNI - from the global table.
2348 */
2349 ret = delete_global_type2_routes(bgp, vpn);
2350 if (ret)
2351 return ret;
90e60aa7 2352
d62a17ae 2353 /* Remove type-3 route for this VNI from global table. */
2354 build_evpn_type3_prefix(&p, vpn->originator_ip);
2355 global_rn = bgp_afi_node_lookup(bgp->rib[afi][safi], afi, safi,
2356 (struct prefix *)&p, &vpn->prd);
2357 if (global_rn) {
2358 /* Delete route entry in the global EVPN table. */
2359 delete_evpn_route_entry(bgp, vpn, afi, safi, global_rn, &ri);
90e60aa7 2360
d62a17ae 2361 /* Schedule for processing - withdraws to peers happen from
2362 * this table.
2363 */
2364 if (ri)
2365 bgp_process(bgp, global_rn, afi, safi);
2366 bgp_unlock_node(global_rn);
2367 }
90e60aa7 2368
d62a17ae 2369 return 0;
90e60aa7 2370}
2371
2d48ee25 2372/*
2373 * Handle router-id change. Update and advertise local routes corresponding
2374 * to this VNI from peers. Note that this is invoked after updating the
2375 * router-id. The routes in the per-VNI table are used to create routes in
2376 * the global table and schedule them.
2377 */
d62a17ae 2378static void update_router_id_vni(struct hash_backet *backet, struct bgp *bgp)
2d48ee25 2379{
d62a17ae 2380 struct bgpevpn *vpn;
2d48ee25 2381
d62a17ae 2382 vpn = (struct bgpevpn *)backet->data;
2d48ee25 2383
d62a17ae 2384 if (!vpn) {
2385 zlog_warn("%s: VNI hash entry for VNI not found", __FUNCTION__);
2386 return;
2387 }
2d48ee25 2388
d62a17ae 2389 /* Skip VNIs with configured RD. */
2390 if (is_rd_configured(vpn))
2391 return;
2d48ee25 2392
d62a17ae 2393 bgp_evpn_derive_auto_rd(bgp, vpn);
2394 update_advertise_vni_routes(bgp, vpn);
2d48ee25 2395}
2396
2397/*
2398 * Handle router-id change. Delete and withdraw local routes corresponding
2399 * to this VNI from peers. Note that this is invoked prior to updating
2400 * the router-id and is done only on the global route table, the routes
2401 * are needed in the per-VNI table to re-advertise with new router id.
2402 */
d62a17ae 2403static void withdraw_router_id_vni(struct hash_backet *backet, struct bgp *bgp)
2d48ee25 2404{
d62a17ae 2405 struct bgpevpn *vpn;
2d48ee25 2406
d62a17ae 2407 vpn = (struct bgpevpn *)backet->data;
2d48ee25 2408
d62a17ae 2409 if (!vpn) {
2410 zlog_warn("%s: VNI hash entry for VNI not found", __FUNCTION__);
2411 return;
2412 }
2d48ee25 2413
d62a17ae 2414 /* Skip VNIs with configured RD. */
2415 if (is_rd_configured(vpn))
2416 return;
2d48ee25 2417
d62a17ae 2418 delete_withdraw_vni_routes(bgp, vpn);
2d48ee25 2419}
2420
128ea8ab 2421/*
2422 * Process received EVPN type-2 route (advertise or withdraw).
2423 */
d62a17ae 2424static int process_type2_route(struct peer *peer, afi_t afi, safi_t safi,
2425 struct attr *attr, u_char *pfx, int psize,
2426 u_int32_t addpath_id)
2427{
2428 struct prefix_rd prd;
2429 struct prefix_evpn p;
2430 u_char ipaddr_len;
2431 u_char macaddr_len;
2432 mpls_label_t *label_pnt;
2433 int ret;
2434
2435 /* Type-2 route should be either 33, 37 or 49 bytes or an
2436 * additional 3 bytes if there is a second label (VNI):
2437 * RD (8), ESI (10), Eth Tag (4), MAC Addr Len (1),
2438 * MAC Addr (6), IP len (1), IP (0, 4 or 16),
2439 * MPLS Lbl1 (3), MPLS Lbl2 (0 or 3)
2440 */
2441 if (psize != 33 && psize != 37 && psize != 49 && psize != 36
2442 && psize != 40 && psize != 52) {
2443 zlog_err("%u:%s - Rx EVPN Type-2 NLRI with invalid length %d",
2444 peer->bgp->vrf_id, peer->host, psize);
2445 return -1;
2446 }
2447
2448 /* Make prefix_rd */
2449 prd.family = AF_UNSPEC;
2450 prd.prefixlen = 64;
2451 memcpy(&prd.val, pfx, 8);
2452 pfx += 8;
2453
2454 /* Make EVPN prefix. */
2455 memset(&p, 0, sizeof(struct prefix_evpn));
b03b8898 2456 p.family = AF_EVPN;
d62a17ae 2457 p.prefixlen = EVPN_TYPE_2_ROUTE_PREFIXLEN;
2458 p.prefix.route_type = BGP_EVPN_MAC_IP_ROUTE;
2459
2460 /* Skip over Ethernet Seg Identifier for now. */
2461 pfx += 10;
2462
2463 /* Skip over Ethernet Tag for now. */
2464 pfx += 4;
2465
2466 /* Get the MAC Addr len */
2467 macaddr_len = *pfx++;
2468
2469 /* Get the MAC Addr */
28328ea9
DS
2470 if (macaddr_len == (ETH_ALEN * 8)) {
2471 memcpy(&p.prefix.mac.octet, pfx, ETH_ALEN);
2472 pfx += ETH_ALEN;
d62a17ae 2473 } else {
2474 zlog_err(
2475 "%u:%s - Rx EVPN Type-2 NLRI with unsupported MAC address length %d",
2476 peer->bgp->vrf_id, peer->host, macaddr_len);
2477 return -1;
2478 }
2479
2480
2481 /* Get the IP. */
2482 ipaddr_len = *pfx++;
2483 if (ipaddr_len != 0 && ipaddr_len != IPV4_MAX_BITLEN
2484 && ipaddr_len != IPV6_MAX_BITLEN) {
2485 zlog_err(
2486 "%u:%s - Rx EVPN Type-2 NLRI with unsupported IP address length %d",
2487 peer->bgp->vrf_id, peer->host, ipaddr_len);
2488 return -1;
2489 }
2490
2491 if (ipaddr_len) {
2492 ipaddr_len /= 8; /* Convert to bytes. */
2493 p.prefix.ip.ipa_type = (ipaddr_len == IPV4_MAX_BYTELEN)
2494 ? IPADDR_V4
2495 : IPADDR_V6;
2496 memcpy(&p.prefix.ip.ip.addr, pfx, ipaddr_len);
2497 }
2498 pfx += ipaddr_len;
2499
2500 /* Get the VNI (in MPLS label field). */
2501 /* Note: We ignore the second VNI, if any. */
2502 label_pnt = (mpls_label_t *)pfx;
2503
2504 /* Process the route. */
2505 if (attr)
2506 ret = bgp_update(peer, (struct prefix *)&p, addpath_id, attr,
2507 afi, safi, ZEBRA_ROUTE_BGP, BGP_ROUTE_NORMAL,
2508 &prd, label_pnt, 0, NULL);
2509 else
2510 ret = bgp_withdraw(peer, (struct prefix *)&p, addpath_id, attr,
2511 afi, safi, ZEBRA_ROUTE_BGP, BGP_ROUTE_NORMAL,
2512 &prd, label_pnt, NULL);
2513 return ret;
128ea8ab 2514}
2515
2516/*
2517 * Process received EVPN type-3 route (advertise or withdraw).
2518 */
d62a17ae 2519static int process_type3_route(struct peer *peer, afi_t afi, safi_t safi,
2520 struct attr *attr, u_char *pfx, int psize,
2521 u_int32_t addpath_id)
2522{
2523 struct prefix_rd prd;
2524 struct prefix_evpn p;
2525 u_char ipaddr_len;
2526 int ret;
2527
2528 /* Type-3 route should be either 17 or 29 bytes: RD (8), Eth Tag (4),
2529 * IP len (1) and IP (4 or 16).
2530 */
2531 if (psize != 17 && psize != 29) {
2532 zlog_err("%u:%s - Rx EVPN Type-3 NLRI with invalid length %d",
2533 peer->bgp->vrf_id, peer->host, psize);
2534 return -1;
2535 }
2536
2537 /* Make prefix_rd */
2538 prd.family = AF_UNSPEC;
2539 prd.prefixlen = 64;
2540 memcpy(&prd.val, pfx, 8);
2541 pfx += 8;
2542
2543 /* Make EVPN prefix. */
2544 memset(&p, 0, sizeof(struct prefix_evpn));
b03b8898 2545 p.family = AF_EVPN;
d62a17ae 2546 p.prefixlen = EVPN_TYPE_3_ROUTE_PREFIXLEN;
2547 p.prefix.route_type = BGP_EVPN_IMET_ROUTE;
2548
2549 /* Skip over Ethernet Tag for now. */
2550 pfx += 4;
2551
2552 /* Get the IP. */
2553 ipaddr_len = *pfx++;
2554 if (ipaddr_len == IPV4_MAX_BITLEN) {
2555 p.prefix.ip.ipa_type = IPADDR_V4;
2556 memcpy(&p.prefix.ip.ip.addr, pfx, IPV4_MAX_BYTELEN);
2557 } else {
2558 zlog_err(
2559 "%u:%s - Rx EVPN Type-3 NLRI with unsupported IP address length %d",
2560 peer->bgp->vrf_id, peer->host, ipaddr_len);
2561 return -1;
2562 }
2563
2564 /* Process the route. */
2565 if (attr)
2566 ret = bgp_update(peer, (struct prefix *)&p, addpath_id, attr,
2567 afi, safi, ZEBRA_ROUTE_BGP, BGP_ROUTE_NORMAL,
2568 &prd, NULL, 0, NULL);
2569 else
2570 ret = bgp_withdraw(peer, (struct prefix *)&p, addpath_id, attr,
2571 afi, safi, ZEBRA_ROUTE_BGP, BGP_ROUTE_NORMAL,
2572 &prd, NULL, NULL);
2573 return ret;
128ea8ab 2574}
2575
2576/*
2577 * Process received EVPN type-5 route (advertise or withdraw).
2578 */
d62a17ae 2579static int process_type5_route(struct peer *peer, afi_t afi, safi_t safi,
2580 struct attr *attr, u_char *pfx, int psize,
2581 u_int32_t addpath_id, int withdraw)
2582{
2583 struct prefix_rd prd;
2584 struct prefix_evpn p;
2585 struct bgp_route_evpn evpn;
2586 u_char ippfx_len;
2587 u_int32_t eth_tag;
2588 mpls_label_t *label_pnt;
2589 int ret;
2590
2591 /* Type-5 route should be 34 or 58 bytes:
2592 * RD (8), ESI (10), Eth Tag (4), IP len (1), IP (4 or 16),
2593 * GW (4 or 16) and VNI (3).
2594 * Note that the IP and GW should both be IPv4 or both IPv6.
2595 */
2596 if (psize != 34 && psize != 58) {
2597 zlog_err("%u:%s - Rx EVPN Type-5 NLRI with invalid length %d",
2598 peer->bgp->vrf_id, peer->host, psize);
2599 return -1;
2600 }
2601
2602 /* Make prefix_rd */
2603 prd.family = AF_UNSPEC;
2604 prd.prefixlen = 64;
2605 memcpy(&prd.val, pfx, 8);
2606 pfx += 8;
2607
2608 /* Make EVPN prefix. */
2609 memset(&p, 0, sizeof(struct prefix_evpn));
b03b8898 2610 p.family = AF_EVPN;
d62a17ae 2611 p.prefix.route_type = BGP_EVPN_IP_PREFIX_ROUTE;
2612
2613 /* Additional information outside of prefix - ESI and GW IP */
2614 memset(&evpn, 0, sizeof(evpn));
2615
2616 /* Fetch ESI */
2617 memcpy(&evpn.eth_s_id.val, pfx, 10);
2618 pfx += 10;
2619
2620 /* Fetch Ethernet Tag. */
2621 memcpy(&eth_tag, pfx, 4);
2622 p.prefix.eth_tag = ntohl(eth_tag);
2623 pfx += 4;
2624
2625 /* Fetch IP prefix length. */
2626 ippfx_len = *pfx++;
2627 if (ippfx_len > IPV6_MAX_BITLEN) {
2628 zlog_err(
2629 "%u:%s - Rx EVPN Type-5 NLRI with invalid IP Prefix length %d",
2630 peer->bgp->vrf_id, peer->host, ippfx_len);
2631 return -1;
2632 }
2633 p.prefix.ip_prefix_length = ippfx_len;
2634
2635 /* Determine IPv4 or IPv6 prefix */
2636 /* Since the address and GW are from the same family, this just becomes
2637 * a simple check on the total size.
2638 */
2639 if (psize == 34) {
2640 SET_IPADDR_V4(&p.prefix.ip);
2641 memcpy(&p.prefix.ip.ipaddr_v4, pfx, 4);
2642 pfx += 4;
2643 memcpy(&evpn.gw_ip.ipv4, pfx, 4);
2644 pfx += 4;
2645 p.prefixlen = PREFIX_LEN_ROUTE_TYPE_5_IPV4;
2646 } else {
2647 SET_IPADDR_V6(&p.prefix.ip);
2648 memcpy(&p.prefix.ip.ipaddr_v6, pfx, 16);
2649 pfx += 16;
2650 memcpy(&evpn.gw_ip.ipv6, pfx, 16);
2651 pfx += 16;
2652 p.prefixlen = PREFIX_LEN_ROUTE_TYPE_5_IPV6;
2653 }
2654
2655 label_pnt = (mpls_label_t *)pfx;
2656
2657 /* Process the route. */
2658 if (!withdraw)
2659 ret = bgp_update(peer, (struct prefix *)&p, addpath_id, attr,
2660 afi, safi, ZEBRA_ROUTE_BGP, BGP_ROUTE_NORMAL,
2661 &prd, label_pnt, 0, &evpn);
2662 else
2663 ret = bgp_withdraw(peer, (struct prefix *)&p, addpath_id, attr,
2664 afi, safi, ZEBRA_ROUTE_BGP, BGP_ROUTE_NORMAL,
2665 &prd, label_pnt, &evpn);
2666
2667 return ret;
2668}
2669
2670static void evpn_mpattr_encode_type5(struct stream *s, struct prefix *p,
2671 struct prefix_rd *prd, mpls_label_t *label,
2672 struct attr *attr)
2673{
2674 int len;
2675 char temp[16];
2676 struct evpn_addr *p_evpn_p;
2677
2678 memset(&temp, 0, 16);
b03b8898 2679 if (p->family != AF_EVPN)
d62a17ae 2680 return;
2681 p_evpn_p = &(p->u.prefix_evpn);
2682
2683 if (IS_IPADDR_V4(&p_evpn_p->ip))
2684 len = 8; /* ipv4 */
2685 else
2686 len = 32; /* ipv6 */
d62a17ae 2687 /* Prefix contains RD, ESI, EthTag, IP length, IP, GWIP and VNI */
2688 stream_putc(s, 8 + 10 + 4 + 1 + len + 3);
2689 stream_put(s, prd->val, 8);
0af35d90 2690 if (attr)
d62a17ae 2691 stream_put(s, &(attr->evpn_overlay.eth_s_id), 10);
2692 else
2693 stream_put(s, &temp, 10);
2694 stream_putl(s, p_evpn_p->eth_tag);
2695 stream_putc(s, p_evpn_p->ip_prefix_length);
2696 if (IS_IPADDR_V4(&p_evpn_p->ip))
2697 stream_put_ipv4(s, p_evpn_p->ip.ipaddr_v4.s_addr);
2698 else
2699 stream_put(s, &p_evpn_p->ip.ipaddr_v6, 16);
0af35d90 2700 if (attr) {
d62a17ae 2701 if (IS_IPADDR_V4(&p_evpn_p->ip))
2702 stream_put_ipv4(s,
2703 attr->evpn_overlay.gw_ip.ipv4.s_addr);
2704 else
2705 stream_put(s, &(attr->evpn_overlay.gw_ip.ipv6), 16);
2706 } else {
2707 if (IS_IPADDR_V4(&p_evpn_p->ip))
2708 stream_put_ipv4(s, 0);
2709 else
2710 stream_put(s, &temp, 16);
2711 }
2712
2713 if (label)
2714 stream_put(s, label, 3);
2715 else
2716 stream_put3(s, 0);
128ea8ab 2717}
2718
2719/*
2720 * Cleanup specific VNI upon EVPN (advertise-all-vni) being disabled.
2721 */
d62a17ae 2722static void cleanup_vni_on_disable(struct hash_backet *backet, struct bgp *bgp)
128ea8ab 2723{
d62a17ae 2724 struct bgpevpn *vpn = (struct bgpevpn *)backet->data;
128ea8ab 2725
d62a17ae 2726 /* Remove EVPN routes and schedule for processing. */
2727 delete_routes_for_vni(bgp, vpn);
128ea8ab 2728
d62a17ae 2729 /* Clear "live" flag and see if hash needs to be freed. */
2730 UNSET_FLAG(vpn->flags, VNI_FLAG_LIVE);
2731 if (!is_vni_configured(vpn))
2732 bgp_evpn_free(bgp, vpn);
128ea8ab 2733}
2734
2735/*
2736 * Free a VNI entry; iterator function called during cleanup.
2737 */
d62a17ae 2738static void free_vni_entry(struct hash_backet *backet, struct bgp *bgp)
128ea8ab 2739{
d62a17ae 2740 struct bgpevpn *vpn;
128ea8ab 2741
d62a17ae 2742 vpn = (struct bgpevpn *)backet->data;
2743 delete_all_vni_routes(bgp, vpn);
2744 bgp_evpn_free(bgp, vpn);
128ea8ab 2745}
2746
c581d8b0
MK
2747/*
2748 * Derive AUTO import RT for BGP VRF - L3VNI
2749 */
2750static void evpn_auto_rt_import_add_for_vrf(struct bgp *bgp_vrf)
2751{
10ebe1ab
MK
2752 struct bgp *bgp_def = NULL;
2753
c581d8b0 2754 form_auto_rt(bgp_vrf, bgp_vrf->l3vni, bgp_vrf->vrf_import_rtl);
10ebe1ab
MK
2755 UNSET_FLAG(bgp_vrf->vrf_flags, BGP_VRF_IMPORT_RT_CFGD);
2756
2757 /* Map RT to VRF */
2758 bgp_def = bgp_get_default();
2759 if (!bgp_def)
2760 return;
2761 bgp_evpn_map_vrf_to_its_rts(bgp_vrf);
c581d8b0
MK
2762}
2763
2764/*
2765 * Delete AUTO import RT from BGP VRF - L3VNI
2766 */
2767static void evpn_auto_rt_import_delete_for_vrf(struct bgp *bgp_vrf)
2768{
2769 evpn_rt_delete_auto(bgp_vrf, bgp_vrf->l3vni, bgp_vrf->vrf_import_rtl);
2770}
2771
2772/*
2773 * Derive AUTO export RT for BGP VRF - L3VNI
2774 */
2775static void evpn_auto_rt_export_add_for_vrf(struct bgp *bgp_vrf)
2776{
2777 UNSET_FLAG(bgp_vrf->vrf_flags, BGP_VRF_EXPORT_RT_CFGD);
2778 form_auto_rt(bgp_vrf, bgp_vrf->l3vni, bgp_vrf->vrf_export_rtl);
2779}
2780
2781/*
2782 * Delete AUTO export RT from BGP VRF - L3VNI
2783 */
2784static void evpn_auto_rt_export_delete_for_vrf(struct bgp *bgp_vrf)
2785{
2786 evpn_rt_delete_auto(bgp_vrf, bgp_vrf->l3vni, bgp_vrf->vrf_export_rtl);
2787}
128ea8ab 2788
f1f8b53c
MK
2789static void bgp_evpn_handle_export_rt_change_for_vrf(struct bgp *bgp_vrf)
2790{
2791 struct bgp *bgp_def = NULL;
2792 struct listnode *node = NULL;
2793 struct bgpevpn *vpn = NULL;
2794
2795 bgp_def = bgp_get_default();
2796 if (!bgp_def)
2797 return;
2798
2799 for (ALL_LIST_ELEMENTS_RO(bgp_vrf->l2vnis, node, vpn))
2800 update_routes_for_vni(bgp_def, vpn);
2801}
2802
128ea8ab 2803/*
2804 * Public functions.
2805 */
2806
c581d8b0
MK
2807void evpn_rt_delete_auto(struct bgp *bgp, vni_t vni,
2808 struct list *rtl)
2809{
2810 struct listnode *node, *nnode, *node_to_del;
2811 struct ecommunity *ecom, *ecom_auto;
2812 struct ecommunity_val eval;
2813
2814 encode_route_target_as((bgp->as & 0xFFFF), vni, &eval);
2815
2816 ecom_auto = ecommunity_new();
2817 ecommunity_add_val(ecom_auto, &eval);
2818 node_to_del = NULL;
2819
2820 for (ALL_LIST_ELEMENTS(rtl, node, nnode, ecom)) {
2821 if (ecommunity_match(ecom, ecom_auto)) {
2822 ecommunity_free(&ecom);
2823 node_to_del = node;
2824 }
2825 }
2826
2827 if (node_to_del)
2828 list_delete_node(rtl, node_to_del);
2829
2830 ecommunity_free(&ecom_auto);
2831}
2832
2833void bgp_evpn_configure_import_rt_for_vrf(struct bgp *bgp_vrf,
10ebe1ab 2834 struct ecommunity *ecomadd)
c581d8b0 2835{
5ba238b7
MK
2836 /* uninstall routes from vrf */
2837 uninstall_routes_for_vrf(bgp_vrf);
10ebe1ab
MK
2838
2839 /* Cleanup the RT to VRF mapping */
2840 bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf);
2841
c581d8b0
MK
2842 /* Remove auto generated RT */
2843 evpn_auto_rt_import_delete_for_vrf(bgp_vrf);
2844
2845 /* Add the newly configured RT to RT list */
2846 listnode_add_sort(bgp_vrf->vrf_import_rtl, ecomadd);
2847 SET_FLAG(bgp_vrf->vrf_flags, BGP_VRF_IMPORT_RT_CFGD);
2848
10ebe1ab
MK
2849 /* map VRF to its RTs */
2850 bgp_evpn_map_vrf_to_its_rts(bgp_vrf);
2851
5ba238b7
MK
2852 /* install routes matching the new VRF */
2853 install_routes_for_vrf(bgp_vrf);
c581d8b0
MK
2854}
2855
2856void bgp_evpn_unconfigure_import_rt_for_vrf(struct bgp *bgp_vrf,
2857 struct ecommunity *ecomdel)
2858{
2859 struct listnode *node = NULL, *nnode = NULL, *node_to_del = NULL;
2860 struct ecommunity *ecom = NULL;
2861
5ba238b7
MK
2862 /* uninstall routes from vrf */
2863 uninstall_routes_for_vrf(bgp_vrf);
10ebe1ab
MK
2864
2865 /* Cleanup the RT to VRF mapping */
2866 bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf);
2867
c581d8b0
MK
2868 /* remove the RT from the RT list */
2869 for (ALL_LIST_ELEMENTS(bgp_vrf->vrf_import_rtl, node, nnode, ecom)) {
2870 if (ecommunity_match(ecom, ecomdel)) {
2871 ecommunity_free(&ecom);
2872 node_to_del = node;
2873 break;
2874 }
2875 }
2876
2877 if (node_to_del)
2878 list_delete_node(bgp_vrf->vrf_import_rtl, node_to_del);
2879
2880 /* fallback to auto import rt, if this was the last RT */
2881 if (list_isempty(bgp_vrf->vrf_import_rtl)) {
2882 UNSET_FLAG(bgp_vrf->vrf_flags, BGP_VRF_IMPORT_RT_CFGD);
2883 evpn_auto_rt_import_add_for_vrf(bgp_vrf);
2884 }
2885
10ebe1ab
MK
2886 /* map VRFs to its RTs */
2887 bgp_evpn_map_vrf_to_its_rts(bgp_vrf);
2888
5ba238b7
MK
2889 /* install routes matching this new RT */
2890 install_routes_for_vrf(bgp_vrf);
c581d8b0
MK
2891}
2892
2893void bgp_evpn_configure_export_rt_for_vrf(struct bgp *bgp_vrf,
2894 struct ecommunity *ecomadd)
2895{
2896 /* remove auto-generated RT */
2897 evpn_auto_rt_export_delete_for_vrf(bgp_vrf);
2898
2899 /* Add the new RT to the RT list */
2900 listnode_add_sort(bgp_vrf->vrf_export_rtl, ecomadd);
2901 SET_FLAG(bgp_vrf->vrf_flags, BGP_VRF_EXPORT_RT_CFGD);
2902
f1f8b53c
MK
2903 bgp_evpn_handle_export_rt_change_for_vrf(bgp_vrf);
2904
c581d8b0
MK
2905}
2906
2907void bgp_evpn_unconfigure_export_rt_for_vrf(struct bgp *bgp_vrf,
2908 struct ecommunity *ecomdel)
2909{
2910 struct listnode *node = NULL, *nnode = NULL, *node_to_del = NULL;
2911 struct ecommunity *ecom = NULL;
2912
2913 /* Remove the RT from the RT list */
2914 for (ALL_LIST_ELEMENTS(bgp_vrf->vrf_export_rtl, node, nnode, ecom)) {
2915 if (ecommunity_match(ecom, ecomdel)) {
2916 ecommunity_free(&ecom);
2917 node_to_del = node;
2918 break;
2919 }
2920 }
2921
2922 if (node_to_del)
2923 list_delete_node(bgp_vrf->vrf_export_rtl, node_to_del);
2924
2925 /* fall back to auto-generated RT if this was the last RT */
2926 if (list_isempty(bgp_vrf->vrf_export_rtl)) {
2927 UNSET_FLAG(bgp_vrf->vrf_flags, BGP_VRF_EXPORT_RT_CFGD);
2928 evpn_auto_rt_export_add_for_vrf(bgp_vrf);
2929 }
2930
f1f8b53c 2931 bgp_evpn_handle_export_rt_change_for_vrf(bgp_vrf);
c581d8b0
MK
2932}
2933
2d48ee25 2934/*
2935 * Handle change to BGP router id. This is invoked twice by the change
2936 * handler, first before the router id has been changed and then after
2937 * the router id has been changed. The first invocation will result in
676f83b9 2938 * local routes for all VNIs/VRF being deleted and withdrawn and the next
2d48ee25 2939 * will result in the routes being re-advertised.
2940 */
d62a17ae 2941void bgp_evpn_handle_router_id_update(struct bgp *bgp, int withdraw)
2d48ee25 2942{
676f83b9 2943 if (withdraw) {
2944
2945 /* delete and withdraw all the type-5 routes
2946 stored in the global table for this vrf */
2947 delete_withdraw_vrf_routes(bgp);
2948
2949 /* delete all the VNI routes (type-2/type-3) routes for all the
2950 * L2-VNIs */
d62a17ae 2951 hash_iterate(bgp->vnihash,
2952 (void (*)(struct hash_backet *,
2953 void *))withdraw_router_id_vni,
2954 bgp);
676f83b9 2955 } else {
2956
2957 /* advertise all routes in the vrf as type-5 routes with the new
2958 * RD */
2959 update_advertise_vrf_routes(bgp);
2960
2961 /* advertise all the VNI routes (type-2/type-3) routes with the
2962 * new RD*/
d62a17ae 2963 hash_iterate(bgp->vnihash,
2964 (void (*)(struct hash_backet *,
2965 void *))update_router_id_vni,
2966 bgp);
676f83b9 2967 }
2d48ee25 2968}
2969
90e60aa7 2970/*
2971 * Handle change to export RT - update and advertise local routes.
2972 */
d62a17ae 2973int bgp_evpn_handle_export_rt_change(struct bgp *bgp, struct bgpevpn *vpn)
90e60aa7 2974{
d62a17ae 2975 return update_routes_for_vni(bgp, vpn);
90e60aa7 2976}
2977
676f83b9 2978void bgp_evpn_handle_vrf_rd_change(struct bgp *bgp_vrf,
2979 int withdraw)
2980{
2981 if (withdraw)
2982 delete_withdraw_vrf_routes(bgp_vrf);
2983 else
2984 update_advertise_vrf_routes(bgp_vrf);
2985}
2986
90e60aa7 2987/*
2988 * Handle change to RD. This is invoked twice by the change handler,
2989 * first before the RD has been changed and then after the RD has
2990 * been changed. The first invocation will result in local routes
2991 * of this VNI being deleted and withdrawn and the next will result
2992 * in the routes being re-advertised.
2993 */
d62a17ae 2994void bgp_evpn_handle_rd_change(struct bgp *bgp, struct bgpevpn *vpn,
2995 int withdraw)
90e60aa7 2996{
d62a17ae 2997 if (withdraw)
2998 delete_withdraw_vni_routes(bgp, vpn);
2999 else
3000 update_advertise_vni_routes(bgp, vpn);
90e60aa7 3001}
3002
3003/*
3004 * Install routes for this VNI. Invoked upon change to Import RT.
3005 */
d62a17ae 3006int bgp_evpn_install_routes(struct bgp *bgp, struct bgpevpn *vpn)
90e60aa7 3007{
d62a17ae 3008 return install_routes_for_vni(bgp, vpn);
90e60aa7 3009}
3010
3011/*
3012 * Uninstall all routes installed for this VNI. Invoked upon change
3013 * to Import RT.
3014 */
d62a17ae 3015int bgp_evpn_uninstall_routes(struct bgp *bgp, struct bgpevpn *vpn)
90e60aa7 3016{
d62a17ae 3017 return uninstall_routes_for_vni(bgp, vpn);
90e60aa7 3018}
3019
b16031a2 3020/*
3021 * Function to display "tag" in route as a VNI.
3022 */
d62a17ae 3023char *bgp_evpn_label2str(mpls_label_t *label, char *buf, int len)
b16031a2 3024{
d62a17ae 3025 vni_t vni;
b16031a2 3026
d62a17ae 3027 vni = label2vni(label);
3028 snprintf(buf, len, "%u", vni);
3029 return buf;
b16031a2 3030}
3031
9c92b5f7
MK
3032/*
3033 * Function to convert evpn route to json format.
3034 * NOTE: We don't use prefix2str as the output here is a bit different.
3035 */
57f7feb6 3036void bgp_evpn_route2json(struct prefix_evpn *p, json_object *json)
9c92b5f7 3037{
b682f6de 3038 char buf1[ETHER_ADDR_STRLEN];
3039 char buf2[PREFIX2STR_BUFFER];
9c92b5f7 3040
b682f6de 3041 if (!json)
3042 return;
9c92b5f7 3043
dff8f48d 3044 if (p->prefix.route_type == BGP_EVPN_IMET_ROUTE) {
b682f6de 3045 json_object_int_add(json, "routeType", p->prefix.route_type);
3046 json_object_int_add(json, "ethTag", 0);
57f7feb6
MK
3047 json_object_int_add(json, "ipLen",
3048 IS_EVPN_PREFIX_IPADDR_V4(p)
3049 ? IPV4_MAX_BITLEN
3050 : IPV6_MAX_BITLEN);
b682f6de 3051 json_object_string_add(json, "ip",
57f7feb6
MK
3052 inet_ntoa(p->prefix.ip.ipaddr_v4));
3053 } else if (p->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE) {
dff8f48d 3054 if (IS_EVPN_PREFIX_IPADDR_NONE(p)) {
57f7feb6
MK
3055 json_object_int_add(json, "routeType",
3056 p->prefix.route_type);
3057 json_object_int_add(
3058 json, "esi",
3059 0); /* TODO: we don't support esi yet */
3060 json_object_int_add(json, "ethTag", 0);
3061 json_object_int_add(json, "macLen", 8 * ETH_ALEN);
3062 json_object_string_add(json, "mac",
3063 prefix_mac2str(&p->prefix.mac,
3064 buf1,
3065 sizeof(buf1)));
dff8f48d
MK
3066 } else {
3067 u_char family;
3068
57f7feb6
MK
3069 family = IS_EVPN_PREFIX_IPADDR_V4(p) ? AF_INET
3070 : AF_INET6;
dff8f48d 3071
b682f6de 3072 json_object_int_add(json, "routeType",
57f7feb6
MK
3073 p->prefix.route_type);
3074 json_object_int_add(
3075 json, "esi",
3076 0); /* TODO: we don't support esi yet */
b682f6de 3077 json_object_int_add(json, "ethTag", 0);
57f7feb6 3078 json_object_int_add(json, "macLen", 8 * ETH_ALEN);
b682f6de 3079 json_object_string_add(json, "mac",
57f7feb6
MK
3080 prefix_mac2str(&p->prefix.mac,
3081 buf1,
3082 sizeof(buf1)));
b682f6de 3083 json_object_int_add(json, "ipLen",
57f7feb6
MK
3084 IS_EVPN_PREFIX_IPADDR_V4(p)
3085 ? IPV4_MAX_BITLEN
3086 : IPV6_MAX_BITLEN);
3087 json_object_string_add(
3088 json, "ip",
3089 inet_ntop(family, &p->prefix.ip.ip.addr, buf2,
3090 PREFIX2STR_BUFFER));
dff8f48d
MK
3091 }
3092 } else {
3093 /* Currently, this is to cater to other AF_ETHERNET code. */
3094 }
9c92b5f7
MK
3095}
3096
520d5d76 3097/*
3098 * Function to convert evpn route to string.
3099 * NOTE: We don't use prefix2str as the output here is a bit different.
3100 */
d62a17ae 3101char *bgp_evpn_route2str(struct prefix_evpn *p, char *buf, int len)
3102{
3103 char buf1[ETHER_ADDR_STRLEN];
3104 char buf2[PREFIX2STR_BUFFER];
3105
3106 if (p->prefix.route_type == BGP_EVPN_IMET_ROUTE) {
3107 snprintf(buf, len, "[%d]:[0]:[%d]:[%s]", p->prefix.route_type,
3108 IS_EVPN_PREFIX_IPADDR_V4(p) ? IPV4_MAX_BITLEN
3109 : IPV6_MAX_BITLEN,
3110 inet_ntoa(p->prefix.ip.ipaddr_v4));
3111 } else if (p->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE) {
3112 if (IS_EVPN_PREFIX_IPADDR_NONE(p))
3113 snprintf(buf, len, "[%d]:[0]:[0]:[%d]:[%s]",
28328ea9 3114 p->prefix.route_type, 8 * ETH_ALEN,
d62a17ae 3115 prefix_mac2str(&p->prefix.mac, buf1,
3116 sizeof(buf1)));
3117 else {
3118 u_char family;
3119
3120 family = IS_EVPN_PREFIX_IPADDR_V4(p) ? AF_INET
3121 : AF_INET6;
3122 snprintf(buf, len, "[%d]:[0]:[0]:[%d]:[%s]:[%d]:[%s]",
28328ea9 3123 p->prefix.route_type, 8 * ETH_ALEN,
d62a17ae 3124 prefix_mac2str(&p->prefix.mac, buf1,
3125 sizeof(buf1)),
3126 family == AF_INET ? IPV4_MAX_BITLEN
3127 : IPV6_MAX_BITLEN,
3128 inet_ntop(family, &p->prefix.ip.ip.addr, buf2,
3129 PREFIX2STR_BUFFER));
3130 }
3131 } else {
b03b8898 3132 /* For EVPN route types not supported yet. */
f9aa3e55
QY
3133 snprintf(buf, len, "(unsupported route type %d)",
3134 p->prefix.route_type);
d62a17ae 3135 }
3136
3137 return (buf);
520d5d76 3138}
3139
128ea8ab 3140/*
3141 * Encode EVPN prefix in Update (MP_REACH)
3142 */
d62a17ae 3143void bgp_evpn_encode_prefix(struct stream *s, struct prefix *p,
3144 struct prefix_rd *prd, mpls_label_t *label,
3145 struct attr *attr, int addpath_encode,
3146 u_int32_t addpath_tx_id)
3147{
3148 struct prefix_evpn *evp = (struct prefix_evpn *)p;
3149 int ipa_len = 0;
3150
3151 if (addpath_encode)
3152 stream_putl(s, addpath_tx_id);
3153
3154 /* Route type */
3155 stream_putc(s, evp->prefix.route_type);
3156
3157 switch (evp->prefix.route_type) {
3158 case BGP_EVPN_MAC_IP_ROUTE:
3159 if (IS_EVPN_PREFIX_IPADDR_V4(evp))
3160 ipa_len = IPV4_MAX_BYTELEN;
3161 else if (IS_EVPN_PREFIX_IPADDR_V6(evp))
3162 ipa_len = IPV6_MAX_BYTELEN;
3163 stream_putc(s, 33 + ipa_len); // 1 VNI
3164 stream_put(s, prd->val, 8); /* RD */
3165 stream_put(s, 0, 10); /* ESI */
3166 stream_putl(s, 0); /* Ethernet Tag ID */
28328ea9 3167 stream_putc(s, 8 * ETH_ALEN); /* Mac Addr Len - bits */
d62a17ae 3168 stream_put(s, evp->prefix.mac.octet, 6); /* Mac Addr */
3169 stream_putc(s, 8 * ipa_len); /* IP address Length */
3170 if (ipa_len)
3171 stream_put(s, &evp->prefix.ip.ip.addr,
3172 ipa_len); /* IP */
3173 stream_put(s, label,
3174 BGP_LABEL_BYTES); /* VNI is contained in 'tag' */
3175 break;
3176
3177 case BGP_EVPN_IMET_ROUTE:
3178 stream_putc(s, 17); // TODO: length - assumes IPv4 address
3179 stream_put(s, prd->val, 8); /* RD */
3180 stream_putl(s, 0); /* Ethernet Tag ID */
3181 stream_putc(s, IPV4_MAX_BITLEN); /* IP address Length - bits */
3182 /* Originating Router's IP Addr */
3183 stream_put_in_addr(s, &evp->prefix.ip.ipaddr_v4);
3184 break;
3185
3186 case BGP_EVPN_IP_PREFIX_ROUTE:
3187 /* TODO: AddPath support. */
3188 evpn_mpattr_encode_type5(s, p, prd, label, attr);
3189 break;
3190
3191 default:
3192 break;
3193 }
3194}
3195
3196int bgp_nlri_parse_evpn(struct peer *peer, struct attr *attr,
3197 struct bgp_nlri *packet, int withdraw)
3198{
3199 u_char *pnt;
3200 u_char *lim;
3201 afi_t afi;
3202 safi_t safi;
3203 u_int32_t addpath_id;
3204 int addpath_encoded;
3205 int psize = 0;
3206 u_char rtype;
3207 u_char rlen;
3208 struct prefix p;
3209
3210 /* Check peer status. */
3211 if (peer->status != Established) {
3212 zlog_err("%u:%s - EVPN update received in state %d",
3213 peer->bgp->vrf_id, peer->host, peer->status);
3214 return -1;
3215 }
3216
3217 /* Start processing the NLRI - there may be multiple in the MP_REACH */
3218 pnt = packet->nlri;
3219 lim = pnt + packet->length;
3220 afi = packet->afi;
3221 safi = packet->safi;
3222 addpath_id = 0;
3223
3224 addpath_encoded =
3225 (CHECK_FLAG(peer->af_cap[afi][safi], PEER_CAP_ADDPATH_AF_RX_ADV)
3226 && CHECK_FLAG(peer->af_cap[afi][safi],
3227 PEER_CAP_ADDPATH_AF_TX_RCV));
3228
3229 for (; pnt < lim; pnt += psize) {
3230 /* Clear prefix structure. */
3231 memset(&p, 0, sizeof(struct prefix));
3232
3233 /* Deal with path-id if AddPath is supported. */
3234 if (addpath_encoded) {
3235 /* When packet overflow occurs return immediately. */
3236 if (pnt + BGP_ADDPATH_ID_LEN > lim)
3237 return -1;
3238
3239 addpath_id = ntohl(*((uint32_t *)pnt));
3240 pnt += BGP_ADDPATH_ID_LEN;
3241 }
3242
3243 /* All EVPN NLRI types start with type and length. */
3244 if (pnt + 2 > lim)
3245 return -1;
3246
3247 rtype = *pnt++;
3248 psize = rlen = *pnt++;
3249
3250 /* When packet overflow occur return immediately. */
3251 if (pnt + psize > lim)
3252 return -1;
3253
3254 switch (rtype) {
3255 case BGP_EVPN_MAC_IP_ROUTE:
3256 if (process_type2_route(peer, afi, safi,
3257 withdraw ? NULL : attr, pnt,
3258 psize, addpath_id)) {
3259 zlog_err(
3260 "%u:%s - Error in processing EVPN type-2 NLRI size %d",
3261 peer->bgp->vrf_id, peer->host, psize);
3262 return -1;
3263 }
3264 break;
3265
3266 case BGP_EVPN_IMET_ROUTE:
3267 if (process_type3_route(peer, afi, safi,
3268 withdraw ? NULL : attr, pnt,
3269 psize, addpath_id)) {
3270 zlog_err(
3271 "%u:%s - Error in processing EVPN type-3 NLRI size %d",
3272 peer->bgp->vrf_id, peer->host, psize);
3273 return -1;
3274 }
3275 break;
3276
3277 case BGP_EVPN_IP_PREFIX_ROUTE:
3278 if (process_type5_route(peer, afi, safi, attr, pnt,
3279 psize, addpath_id, withdraw)) {
3280 zlog_err(
3281 "%u:%s - Error in processing EVPN type-5 NLRI size %d",
3282 peer->bgp->vrf_id, peer->host, psize);
3283 return -1;
3284 }
3285 break;
3286
3287 default:
3288 break;
3289 }
3290 }
3291
3292 /* Packet length consistency check. */
3293 if (pnt != lim)
3294 return -1;
3295
3296 return 0;
128ea8ab 3297}
3298
10ebe1ab
MK
3299/*
3300 * Map the RTs (configured or automatically derived) of a VRF to the VRF.
3301 * The mapping will be used during route processing.
3302 * bgp_def: default bgp instance
3303 * bgp_vrf: specific bgp vrf instance on which RT is configured
3304 */
3305void bgp_evpn_map_vrf_to_its_rts(struct bgp *bgp_vrf)
3306{
3307 int i = 0;
3308 struct ecommunity_val *eval = NULL;
3309 struct listnode *node = NULL, *nnode = NULL;
3310 struct ecommunity *ecom = NULL;
3311
3312 for (ALL_LIST_ELEMENTS(bgp_vrf->vrf_import_rtl, node, nnode, ecom)) {
3313 for (i = 0; i < ecom->size; i++) {
3314 eval = (struct ecommunity_val *)(ecom->val
3315 + (i
3316 * ECOMMUNITY_SIZE));
3317 map_vrf_to_rt(bgp_vrf, eval);
3318 }
3319 }
3320}
3321
3322/*
3323 * Unmap the RTs (configured or automatically derived) of a VRF from the VRF.
3324 */
3325void bgp_evpn_unmap_vrf_from_its_rts(struct bgp *bgp_vrf)
3326{
3327 int i;
3328 struct ecommunity_val *eval;
3329 struct listnode *node, *nnode;
3330 struct ecommunity *ecom;
3331
3332 for (ALL_LIST_ELEMENTS(bgp_vrf->vrf_import_rtl, node, nnode, ecom)) {
3333 for (i = 0; i < ecom->size; i++) {
3334 struct vrf_irt_node *irt;
3335 struct ecommunity_val eval_tmp;
3336
3337 eval = (struct ecommunity_val *)(ecom->val
3338 + (i
3339 * ECOMMUNITY_SIZE));
3340 /* If using "automatic" RT, we only care about the
3341 * local-admin sub-field.
3342 * This is to facilitate using VNI as the RT for EBGP
3343 * peering too.
3344 */
3345 memcpy(&eval_tmp, eval, ECOMMUNITY_SIZE);
3346 if (!CHECK_FLAG(bgp_vrf->vrf_flags,
3347 BGP_VRF_IMPORT_RT_CFGD))
3348 mask_ecom_global_admin(&eval_tmp, eval);
3349
3350 irt = lookup_vrf_import_rt(&eval_tmp);
3351 if (irt)
3352 unmap_vrf_from_rt(bgp_vrf, irt);
3353 }
3354 }
3355}
3356
3357
128ea8ab 3358
3359/*
3360 * Map the RTs (configured or automatically derived) of a VNI to the VNI.
3361 * The mapping will be used during route processing.
3362 */
d62a17ae 3363void bgp_evpn_map_vni_to_its_rts(struct bgp *bgp, struct bgpevpn *vpn)
128ea8ab 3364{
d62a17ae 3365 int i;
3366 struct ecommunity_val *eval;
3367 struct listnode *node, *nnode;
3368 struct ecommunity *ecom;
128ea8ab 3369
d62a17ae 3370 for (ALL_LIST_ELEMENTS(vpn->import_rtl, node, nnode, ecom)) {
3371 for (i = 0; i < ecom->size; i++) {
3372 eval = (struct ecommunity_val *)(ecom->val
3373 + (i
3374 * ECOMMUNITY_SIZE));
3375 map_vni_to_rt(bgp, vpn, eval);
3376 }
3377 }
128ea8ab 3378}
3379
3380/*
3381 * Unmap the RTs (configured or automatically derived) of a VNI from the VNI.
3382 */
d62a17ae 3383void bgp_evpn_unmap_vni_from_its_rts(struct bgp *bgp, struct bgpevpn *vpn)
128ea8ab 3384{
d62a17ae 3385 int i;
3386 struct ecommunity_val *eval;
3387 struct listnode *node, *nnode;
3388 struct ecommunity *ecom;
128ea8ab 3389
d62a17ae 3390 for (ALL_LIST_ELEMENTS(vpn->import_rtl, node, nnode, ecom)) {
3391 for (i = 0; i < ecom->size; i++) {
3392 struct irt_node *irt;
3393 struct ecommunity_val eval_tmp;
128ea8ab 3394
d62a17ae 3395 eval = (struct ecommunity_val *)(ecom->val
3396 + (i
3397 * ECOMMUNITY_SIZE));
3398 /* If using "automatic" RT, we only care about the
3399 * local-admin sub-field.
3400 * This is to facilitate using VNI as the RT for EBGP
3401 * peering too.
3402 */
3403 memcpy(&eval_tmp, eval, ECOMMUNITY_SIZE);
3404 if (!is_import_rt_configured(vpn))
3405 mask_ecom_global_admin(&eval_tmp, eval);
128ea8ab 3406
d62a17ae 3407 irt = lookup_import_rt(bgp, &eval_tmp);
3408 if (irt)
3409 unmap_vni_from_rt(bgp, vpn, irt);
3410 }
3411 }
128ea8ab 3412}
3413
3414/*
3415 * Derive Import RT automatically for VNI and map VNI to RT.
3416 * The mapping will be used during route processing.
3417 */
d62a17ae 3418void bgp_evpn_derive_auto_rt_import(struct bgp *bgp, struct bgpevpn *vpn)
128ea8ab 3419{
c581d8b0 3420 form_auto_rt(bgp, vpn->vni, vpn->import_rtl);
d62a17ae 3421 UNSET_FLAG(vpn->flags, VNI_FLAG_IMPRT_CFGD);
128ea8ab 3422
d62a17ae 3423 /* Map RT to VNI */
3424 bgp_evpn_map_vni_to_its_rts(bgp, vpn);
128ea8ab 3425}
3426
3427/*
3428 * Derive Export RT automatically for VNI.
3429 */
d62a17ae 3430void bgp_evpn_derive_auto_rt_export(struct bgp *bgp, struct bgpevpn *vpn)
128ea8ab 3431{
c581d8b0 3432 form_auto_rt(bgp, vpn->vni, vpn->export_rtl);
d62a17ae 3433 UNSET_FLAG(vpn->flags, VNI_FLAG_EXPRT_CFGD);
128ea8ab 3434}
3435
676f83b9 3436/*
3437 * Derive RD automatically for VNI using passed information - it
3438 * is of the form RouterId:unique-id-for-vni.
3439 */
3440void bgp_evpn_derive_auto_rd_for_vrf(struct bgp *bgp)
3441{
3442 char buf[100];
3443
3444 bgp->vrf_prd.family = AF_UNSPEC;
3445 bgp->vrf_prd.prefixlen = 64;
3446 sprintf(buf, "%s:%hu", inet_ntoa(bgp->router_id), bgp->vrf_rd_id);
3447 str2prefix_rd(buf, &bgp->vrf_prd);
3448}
3449
128ea8ab 3450/*
3451 * Derive RD automatically for VNI using passed information - it
3452 * is of the form RouterId:unique-id-for-vni.
3453 */
d62a17ae 3454void bgp_evpn_derive_auto_rd(struct bgp *bgp, struct bgpevpn *vpn)
128ea8ab 3455{
d62a17ae 3456 char buf[100];
128ea8ab 3457
d62a17ae 3458 vpn->prd.family = AF_UNSPEC;
3459 vpn->prd.prefixlen = 64;
3460 sprintf(buf, "%s:%hu", inet_ntoa(bgp->router_id), vpn->rd_id);
cbb65f5e 3461 (void)str2prefix_rd(buf, &vpn->prd);
d62a17ae 3462 UNSET_FLAG(vpn->flags, VNI_FLAG_RD_CFGD);
128ea8ab 3463}
3464
3465/*
3466 * Lookup VNI.
3467 */
d62a17ae 3468struct bgpevpn *bgp_evpn_lookup_vni(struct bgp *bgp, vni_t vni)
128ea8ab 3469{
d62a17ae 3470 struct bgpevpn *vpn;
3471 struct bgpevpn tmp;
128ea8ab 3472
d62a17ae 3473 memset(&tmp, 0, sizeof(struct bgpevpn));
3474 tmp.vni = vni;
3475 vpn = hash_lookup(bgp->vnihash, &tmp);
3476 return vpn;
128ea8ab 3477}
3478
3479/*
3480 * Create a new vpn - invoked upon configuration or zebra notification.
3481 */
d62a17ae 3482struct bgpevpn *bgp_evpn_new(struct bgp *bgp, vni_t vni,
29c53922
MK
3483 struct in_addr originator_ip,
3484 vrf_id_t tenant_vrf_id)
128ea8ab 3485{
d62a17ae 3486 struct bgpevpn *vpn;
128ea8ab 3487
d62a17ae 3488 if (!bgp)
3489 return NULL;
128ea8ab 3490
d62a17ae 3491 vpn = XCALLOC(MTYPE_BGP_EVPN, sizeof(struct bgpevpn));
3492 if (!vpn)
3493 return NULL;
128ea8ab 3494
d62a17ae 3495 /* Set values - RD and RT set to defaults. */
3496 vpn->vni = vni;
3497 vpn->originator_ip = originator_ip;
29c53922 3498 vpn->tenant_vrf_id = tenant_vrf_id;
128ea8ab 3499
d62a17ae 3500 /* Initialize route-target import and export lists */
3501 vpn->import_rtl = list_new();
3502 vpn->import_rtl->cmp = (int (*)(void *, void *))evpn_route_target_cmp;
3503 vpn->export_rtl = list_new();
3504 vpn->export_rtl->cmp = (int (*)(void *, void *))evpn_route_target_cmp;
e9eb5f63 3505 bf_assign_index(bm->rd_idspace, vpn->rd_id);
d62a17ae 3506 derive_rd_rt_for_vni(bgp, vpn);
128ea8ab 3507
d62a17ae 3508 /* Initialize EVPN route table. */
3509 vpn->route_table = bgp_table_init(AFI_L2VPN, SAFI_EVPN);
128ea8ab 3510
d62a17ae 3511 /* Add to hash */
3512 if (!hash_get(bgp->vnihash, vpn, hash_alloc_intern)) {
3513 XFREE(MTYPE_BGP_EVPN, vpn);
3514 return NULL;
3515 }
6a8657d0
MK
3516
3517 /* add to l2vni list on corresponding vrf */
3518 bgpevpn_link_to_l3vni(vpn);
3519
d62a17ae 3520 QOBJ_REG(vpn, bgpevpn);
3521 return vpn;
128ea8ab 3522}
3523
3524/*
3525 * Free a given VPN - called in multiple scenarios such as zebra
3526 * notification, configuration being deleted, advertise-all-vni disabled etc.
3527 * This just frees appropriate memory, caller should have taken other
3528 * needed actions.
3529 */
d62a17ae 3530void bgp_evpn_free(struct bgp *bgp, struct bgpevpn *vpn)
128ea8ab 3531{
6a8657d0 3532 bgpevpn_unlink_from_l3vni(vpn);
d62a17ae 3533 bgp_table_unlock(vpn->route_table);
3534 bgp_evpn_unmap_vni_from_its_rts(bgp, vpn);
affe9e99
DS
3535 list_delete_and_null(&vpn->import_rtl);
3536 list_delete_and_null(&vpn->export_rtl);
e9eb5f63 3537 bf_release_index(bm->rd_idspace, vpn->rd_id);
d62a17ae 3538 hash_release(bgp->vnihash, vpn);
3539 QOBJ_UNREG(vpn);
3540 XFREE(MTYPE_BGP_EVPN, vpn);
128ea8ab 3541}
3542
3543/*
3544 * Import route into matching VNI(s).
3545 */
d62a17ae 3546int bgp_evpn_import_route(struct bgp *bgp, afi_t afi, safi_t safi,
3547 struct prefix *p, struct bgp_info *ri)
128ea8ab 3548{
d62a17ae 3549 return install_uninstall_evpn_route(bgp, afi, safi, p, ri, 1);
128ea8ab 3550}
3551
3552/*
3553 * Unimport route from matching VNI(s).
3554 */
d62a17ae 3555int bgp_evpn_unimport_route(struct bgp *bgp, afi_t afi, safi_t safi,
3556 struct prefix *p, struct bgp_info *ri)
128ea8ab 3557{
d62a17ae 3558 return install_uninstall_evpn_route(bgp, afi, safi, p, ri, 0);
128ea8ab 3559}
3560
db0e1937
MK
3561/* filter routes which have martian next hops */
3562int bgp_filter_evpn_routes_upon_martian_nh_change(struct bgp *bgp)
3563{
0291c246
MK
3564 afi_t afi;
3565 safi_t safi;
3566 struct bgp_node *rd_rn, *rn;
3567 struct bgp_table *table;
3568 struct bgp_info *ri;
db0e1937
MK
3569
3570 afi = AFI_L2VPN;
3571 safi = SAFI_EVPN;
3572
3573 /* Walk entire global routing table and evaluate routes which could be
3574 * imported into this VPN. Note that we cannot just look at the routes
3575 * for the VNI's RD -
3576 * remote routes applicable for this VNI could have any RD.
3577 */
3578 /* EVPN routes are a 2-level table. */
3579 for (rd_rn = bgp_table_top(bgp->rib[afi][safi]); rd_rn;
3580 rd_rn = bgp_route_next(rd_rn)) {
3581 table = (struct bgp_table *)(rd_rn->info);
3582 if (!table)
3583 continue;
3584
3585 for (rn = bgp_table_top(table); rn; rn = bgp_route_next(rn)) {
3586
3587 for (ri = rn->info; ri; ri = ri->next) {
3588
3589 /* Consider "valid" remote routes applicable for
3590 * this VNI. */
3591 if (!(ri->type == ZEBRA_ROUTE_BGP
3592 && ri->sub_type == BGP_ROUTE_NORMAL))
3593 continue;
3594
60466a63 3595 if (bgp_nexthop_self(bgp, ri->attr->nexthop)) {
db0e1937
MK
3596
3597 char attr_str[BUFSIZ];
3598 char pbuf[PREFIX_STRLEN];
3599
3600 bgp_dump_attr(ri->attr, attr_str,
3601 BUFSIZ);
3602
3603 if (bgp_debug_update(ri->peer, &rn->p,
3604 NULL, 1))
3605 zlog_debug(
b682f6de 3606 "%u: prefix %s with attr %s - DENIED due to martian or self nexthop",
db0e1937
MK
3607 bgp->vrf_id,
3608 prefix2str(
60466a63 3609 &rn->p, pbuf,
db0e1937
MK
3610 sizeof(pbuf)),
3611 attr_str);
3612
3613 bgp_evpn_unimport_route(bgp, afi, safi,
3614 &rn->p, ri);
3615
60466a63
QY
3616 bgp_rib_remove(rn, ri, ri->peer, afi,
3617 safi);
db0e1937 3618 }
db0e1937
MK
3619 }
3620 }
3621 }
3622
3623 return 0;
3624}
3625
128ea8ab 3626/*
3627 * Handle del of a local MACIP.
3628 */
d62a17ae 3629int bgp_evpn_local_macip_del(struct bgp *bgp, vni_t vni, struct ethaddr *mac,
3630 struct ipaddr *ip)
128ea8ab 3631{
d62a17ae 3632 struct bgpevpn *vpn;
3633 struct prefix_evpn p;
128ea8ab 3634
d62a17ae 3635 if (!bgp->vnihash) {
3636 zlog_err("%u: VNI hash not created", bgp->vrf_id);
3637 return -1;
3638 }
128ea8ab 3639
d62a17ae 3640 /* Lookup VNI hash - should exist. */
3641 vpn = bgp_evpn_lookup_vni(bgp, vni);
3642 if (!vpn || !is_vni_live(vpn)) {
3643 zlog_warn("%u: VNI hash entry for VNI %u %s at MACIP DEL",
3644 bgp->vrf_id, vni, vpn ? "not live" : "not found");
3645 return -1;
3646 }
128ea8ab 3647
d62a17ae 3648 /* Remove EVPN type-2 route and schedule for processing. */
3649 build_evpn_type2_prefix(&p, mac, ip);
3650 delete_evpn_route(bgp, vpn, &p);
128ea8ab 3651
d62a17ae 3652 return 0;
128ea8ab 3653}
3654
3655/*
3656 * Handle add of a local MACIP.
3657 */
d62a17ae 3658int bgp_evpn_local_macip_add(struct bgp *bgp, vni_t vni, struct ethaddr *mac,
1a98c087 3659 struct ipaddr *ip, u_char flags)
128ea8ab 3660{
d62a17ae 3661 struct bgpevpn *vpn;
3662 struct prefix_evpn p;
128ea8ab 3663
d62a17ae 3664 if (!bgp->vnihash) {
3665 zlog_err("%u: VNI hash not created", bgp->vrf_id);
3666 return -1;
3667 }
128ea8ab 3668
d62a17ae 3669 /* Lookup VNI hash - should exist. */
3670 vpn = bgp_evpn_lookup_vni(bgp, vni);
3671 if (!vpn || !is_vni_live(vpn)) {
3672 zlog_warn("%u: VNI hash entry for VNI %u %s at MACIP ADD",
3673 bgp->vrf_id, vni, vpn ? "not live" : "not found");
3674 return -1;
3675 }
128ea8ab 3676
d62a17ae 3677 /* Create EVPN type-2 route and schedule for processing. */
3678 build_evpn_type2_prefix(&p, mac, ip);
1a98c087 3679 if (update_evpn_route(bgp, vpn, &p, flags)) {
d62a17ae 3680 char buf[ETHER_ADDR_STRLEN];
3681 char buf2[INET6_ADDRSTRLEN];
128ea8ab 3682
d62a17ae 3683 zlog_err(
b34fd35d 3684 "%u:Failed to create Type-2 route, VNI %u %s MAC %s IP %s",
1a98c087 3685 bgp->vrf_id, vpn->vni,
b34fd35d 3686 CHECK_FLAG(flags, ZEBRA_MAC_TYPE_STICKY) ? "sticky gateway"
1a98c087 3687 : "",
d62a17ae 3688 prefix_mac2str(mac, buf, sizeof(buf)),
3689 ipaddr2str(ip, buf2, sizeof(buf2)));
3690 return -1;
3691 }
128ea8ab 3692
d62a17ae 3693 return 0;
128ea8ab 3694}
3695
6a8657d0
MK
3696static void link_l2vni_hash_to_l3vni(struct hash_backet *backet,
3697 struct bgp *bgp_vrf)
3698{
3699 struct bgpevpn *vpn = NULL;
3700 struct bgp *bgp_def = NULL;
3701
3702 bgp_def = bgp_get_default();
3703 assert(bgp_def);
3704
3705 vpn = (struct bgpevpn *)backet->data;
3706 if (vpn->tenant_vrf_id == bgp_vrf->vrf_id)
3707 bgpevpn_link_to_l3vni(vpn);
3708}
3709
fe1dc5a3
MK
3710int bgp_evpn_local_l3vni_add(vni_t l3vni,
3711 vrf_id_t vrf_id,
b67a60d2 3712 struct ethaddr *rmac,
3713 struct in_addr originator_ip)
fe1dc5a3
MK
3714{
3715 struct bgp *bgp_vrf = NULL; /* bgp VRF instance */
3716 struct bgp *bgp_def = NULL; /* default bgp instance */
f1f8b53c
MK
3717 struct listnode *node = NULL;
3718 struct bgpevpn *vpn = NULL;
fe1dc5a3
MK
3719 as_t as = 0;
3720
3721 /* get the default instamce - required to get the AS number for VRF
3722 * auto-creation*/
3723 bgp_def = bgp_get_default();
3724 if (!bgp_def) {
3725 zlog_err("Cannot process L3VNI %u ADD - default BGP instance not yet created",
3726 l3vni);
3727 return -1;
3728 }
3729 as = bgp_def->as;
3730
3731 /* if the BGP vrf instance doesnt exist - create one */
0b5131c9 3732 bgp_vrf = bgp_lookup_by_name(vrf_id_to_name(vrf_id));
fe1dc5a3
MK
3733 if (!bgp_vrf) {
3734
3735 int ret = 0;
3736
3737 ret = bgp_get(&bgp_vrf, &as, vrf_id_to_name(vrf_id),
3738 BGP_INSTANCE_TYPE_VRF);
3739 switch (ret) {
3740 case BGP_ERR_MULTIPLE_INSTANCE_NOT_SET:
3741 zlog_err("'bgp multiple-instance' not present\n");
3742 return -1;
3743 case BGP_ERR_AS_MISMATCH:
3744 zlog_err("BGP is already running; AS is %u\n", as);
3745 return -1;
3746 case BGP_ERR_INSTANCE_MISMATCH:
3747 zlog_err("BGP instance name and AS number mismatch\n");
3748 return -1;
3749 }
3750
3751 /* mark as auto created */
3752 SET_FLAG(bgp_vrf->vrf_flags, BGP_VRF_AUTO);
3753 }
3754
3755 /* associate with l3vni */
3756 bgp_vrf->l3vni = l3vni;
3757
3758 /* set the router mac - to be used in mac-ip routes for this vrf */
3759 memcpy(&bgp_vrf->rmac, rmac, sizeof(struct ethaddr));
3760
b67a60d2 3761 /* set the originator ip */
3762 bgp_vrf->originator_ip = originator_ip;
3763
c581d8b0
MK
3764 /* auto derive RD/RT */
3765 if (!CHECK_FLAG(bgp_vrf->vrf_flags, BGP_VRF_IMPORT_RT_CFGD))
3766 evpn_auto_rt_import_add_for_vrf(bgp_vrf);
3767 if (!CHECK_FLAG(bgp_vrf->vrf_flags, BGP_VRF_EXPORT_RT_CFGD))
3768 evpn_auto_rt_export_add_for_vrf(bgp_vrf);
676f83b9 3769 bgp_evpn_derive_auto_rd_for_vrf(bgp_vrf);
fe1dc5a3 3770
6a8657d0
MK
3771 /* link all corresponding l2vnis */
3772 hash_iterate(bgp_def->vnihash,
3773 (void (*)(struct hash_backet *, void *))
3774 link_l2vni_hash_to_l3vni,
3775 bgp_vrf);
3776
f1f8b53c
MK
3777 /* updates all corresponding local mac-ip routes */
3778 for (ALL_LIST_ELEMENTS_RO(bgp_vrf->l2vnis, node, vpn))
3779 update_routes_for_vni(bgp_def, vpn);
fe1dc5a3 3780
5ba238b7
MK
3781 /* install all remote routes belonging to this l3vni into correspondng
3782 * vrf */
3783 install_routes_for_vrf(bgp_vrf);
fe1dc5a3
MK
3784
3785 return 0;
3786}
3787
3788int bgp_evpn_local_l3vni_del(vni_t l3vni,
3789 vrf_id_t vrf_id)
3790{
3791 struct bgp *bgp_vrf = NULL; /* bgp vrf instance */
f1f8b53c
MK
3792 struct bgp *bgp_def = NULL; /* default bgp instance */
3793 struct listnode *node = NULL;
3794 struct bgpevpn *vpn = NULL;
fe1dc5a3
MK
3795
3796 bgp_vrf = bgp_lookup_by_vrf_id(vrf_id);
3797 if (!bgp_vrf) {
3798 zlog_err("Cannot process L3VNI %u Del - Could not find BGP instance",
3799 l3vni);
3800 return -1;
3801 }
3802
f1f8b53c
MK
3803 bgp_def = bgp_get_default();
3804 if (!bgp_def) {
3805 zlog_err("Cannot process L3VNI %u Del - Could not find default BGP instance",
3806 l3vni);
3807 return -1;
3808 }
3809
1eb88002
MK
3810 /* unimport remote routes from VRF, if it is AUTO vrf bgp_delete will
3811 * take care of uninstalling the routes from zebra */
3812 if (!CHECK_FLAG(bgp_vrf->vrf_flags, BGP_VRF_AUTO))
3813 uninstall_routes_for_vrf(bgp_vrf);
5ba238b7 3814
fe1dc5a3
MK
3815 /* remove the l3vni from vrf instance */
3816 bgp_vrf->l3vni = 0;
3817
3818 /* remove the Rmac from the BGP vrf */
3819 memset(&bgp_vrf->rmac, 0, sizeof(struct ethaddr));
3820
c581d8b0 3821 /* delete RD/RT */
23a06e11 3822 if (bgp_vrf->vrf_import_rtl && !list_isempty(bgp_vrf->vrf_import_rtl)) {
10ebe1ab 3823 bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf);
5ba238b7 3824 list_delete_all_node(bgp_vrf->vrf_import_rtl);
23a06e11
MK
3825 }
3826 if (bgp_vrf->vrf_export_rtl && !list_isempty(bgp_vrf->vrf_export_rtl)) {
5ba238b7 3827 list_delete_all_node(bgp_vrf->vrf_export_rtl);
23a06e11 3828 }
fe1dc5a3 3829
f1f8b53c
MK
3830 /* update all corresponding local mac-ip routes */
3831 for (ALL_LIST_ELEMENTS_RO(bgp_vrf->l2vnis, node, vpn))
3832 update_routes_for_vni(bgp_def, vpn);
fe1dc5a3 3833
fe1dc5a3
MK
3834
3835 /* Delete the instance if it was autocreated */
3836 if (CHECK_FLAG(bgp_vrf->vrf_flags, BGP_VRF_AUTO))
3837 bgp_delete(bgp_vrf);
3838
3839 return 0;
3840}
3841
128ea8ab 3842/*
3843 * Handle del of a local VNI.
3844 */
d62a17ae 3845int bgp_evpn_local_vni_del(struct bgp *bgp, vni_t vni)
128ea8ab 3846{
d62a17ae 3847 struct bgpevpn *vpn;
128ea8ab 3848
d62a17ae 3849 if (!bgp->vnihash) {
3850 zlog_err("%u: VNI hash not created", bgp->vrf_id);
3851 return -1;
3852 }
128ea8ab 3853
d62a17ae 3854 /* Locate VNI hash */
3855 vpn = bgp_evpn_lookup_vni(bgp, vni);
3856 if (!vpn) {
3857 zlog_warn("%u: VNI hash entry for VNI %u not found at DEL",
3858 bgp->vrf_id, vni);
3859 return 0;
3860 }
128ea8ab 3861
d62a17ae 3862 /* Remove all local EVPN routes and schedule for processing (to
3863 * withdraw from peers).
3864 */
3865 delete_routes_for_vni(bgp, vpn);
128ea8ab 3866
db0e1937
MK
3867 /*
3868 * tunnel is no longer active, del tunnel ip address from tip_hash
3869 */
3870 bgp_tip_del(bgp, &vpn->originator_ip);
3871
d62a17ae 3872 /* Clear "live" flag and see if hash needs to be freed. */
3873 UNSET_FLAG(vpn->flags, VNI_FLAG_LIVE);
3874 if (!is_vni_configured(vpn))
3875 bgp_evpn_free(bgp, vpn);
128ea8ab 3876
d62a17ae 3877 return 0;
128ea8ab 3878}
3879
3880/*
3881 * Handle add (or update) of a local VNI. The only VNI change we care
3882 * about is change to local-tunnel-ip.
3883 */
d62a17ae 3884int bgp_evpn_local_vni_add(struct bgp *bgp, vni_t vni,
29c53922
MK
3885 struct in_addr originator_ip,
3886 vrf_id_t tenant_vrf_id)
d62a17ae 3887{
3888 struct bgpevpn *vpn;
3889 struct prefix_evpn p;
3890
3891 if (!bgp->vnihash) {
3892 zlog_err("%u: VNI hash not created", bgp->vrf_id);
3893 return -1;
3894 }
3895
3896 /* Lookup VNI. If present and no change, exit. */
3897 vpn = bgp_evpn_lookup_vni(bgp, vni);
ddd16ed5 3898 if (vpn) {
29c53922
MK
3899
3900 /* update tenant_vrf_id if required */
6a8657d0
MK
3901 if (vpn->tenant_vrf_id != tenant_vrf_id) {
3902 bgpevpn_unlink_from_l3vni(vpn);
29c53922 3903 vpn->tenant_vrf_id = tenant_vrf_id;
6a8657d0 3904 bgpevpn_link_to_l3vni(vpn);
e92bd2a2
MK
3905
3906 /* update all routes with new export RT for VRFs */
3907 update_routes_for_vni(bgp, vpn);
6a8657d0 3908 }
29c53922 3909
2f1ac16a
MK
3910 if (is_vni_live(vpn)
3911 && IPV4_ADDR_SAME(&vpn->originator_ip, &originator_ip))
d62a17ae 3912 /* Probably some other param has changed that we don't
3913 * care about. */
3914 return 0;
3915
3916 /* Local tunnel endpoint IP address has changed */
ddd16ed5 3917 handle_tunnel_ip_change(bgp, vpn, originator_ip);
d62a17ae 3918 }
3919
3920 /* Create or update as appropriate. */
3921 if (!vpn) {
29c53922 3922 vpn = bgp_evpn_new(bgp, vni, originator_ip, tenant_vrf_id);
d62a17ae 3923 if (!vpn) {
3924 zlog_err(
3925 "%u: Failed to allocate VNI entry for VNI %u - at Add",
3926 bgp->vrf_id, vni);
3927 return -1;
3928 }
3929 }
3930
db0e1937 3931 /* if the VNI is live already, there is nothing more to do */
ddd16ed5
MK
3932 if (is_vni_live(vpn))
3933 return 0;
3934
d62a17ae 3935 /* Mark as "live" */
3936 SET_FLAG(vpn->flags, VNI_FLAG_LIVE);
3937
db0e1937
MK
3938 /* tunnel is now active, add tunnel-ip to db */
3939 bgp_tip_add(bgp, &originator_ip);
3940
3941 /* filter routes as nexthop database has changed */
3942 bgp_filter_evpn_routes_upon_martian_nh_change(bgp);
3943
d62a17ae 3944 /* Create EVPN type-3 route and schedule for processing. */
3945 build_evpn_type3_prefix(&p, vpn->originator_ip);
3946 if (update_evpn_route(bgp, vpn, &p, 0)) {
3947 zlog_err("%u: Type3 route creation failure for VNI %u",
3948 bgp->vrf_id, vni);
3949 return -1;
3950 }
3951
3952 /* If we have learnt and retained remote routes (VTEPs, MACs) for this
3953 * VNI,
3954 * install them.
3955 */
3956 install_routes_for_vni(bgp, vpn);
3957
d7d97010
MK
3958 /* If we are advertising gateway mac-ip
3959 It needs to be conveyed again to zebra */
3960 bgp_zebra_advertise_gw_macip(bgp, vpn->advertise_gw_macip, vpn->vni);
3961
d62a17ae 3962 return 0;
b18825eb 3963}
14c1a7bf 3964
7724c0a1 3965/*
3966 * Cleanup EVPN information on disable - Need to delete and withdraw
3967 * EVPN routes from peers.
3968 */
d62a17ae 3969void bgp_evpn_cleanup_on_disable(struct bgp *bgp)
7724c0a1 3970{
9d303b37
DL
3971 hash_iterate(bgp->vnihash, (void (*)(struct hash_backet *,
3972 void *))cleanup_vni_on_disable,
3973 bgp);
7724c0a1 3974}
3975
14c1a7bf 3976/*
3977 * Cleanup EVPN information - invoked at the time of bgpd exit or when the
3978 * BGP instance (default) is being freed.
3979 */
d62a17ae 3980void bgp_evpn_cleanup(struct bgp *bgp)
14c1a7bf 3981{
d62a17ae 3982 if (bgp->vnihash)
9d303b37
DL
3983 hash_iterate(bgp->vnihash, (void (*)(struct hash_backet *,
3984 void *))free_vni_entry,
3985 bgp);
d62a17ae 3986 if (bgp->import_rt_hash)
3987 hash_free(bgp->import_rt_hash);
3988 bgp->import_rt_hash = NULL;
10ebe1ab
MK
3989 if (bgp->vrf_import_rt_hash)
3990 hash_free(bgp->vrf_import_rt_hash);
3991 bgp->vrf_import_rt_hash = NULL;
d62a17ae 3992 if (bgp->vnihash)
3993 hash_free(bgp->vnihash);
3994 bgp->vnihash = NULL;
c581d8b0 3995 if (bgp->vrf_import_rtl)
bb7a24ab 3996 list_delete_and_null(&bgp->vrf_import_rtl);
c581d8b0
MK
3997 bgp->vrf_import_rtl = NULL;
3998 if (bgp->vrf_export_rtl)
bb7a24ab 3999 list_delete_and_null(&bgp->vrf_export_rtl);
c581d8b0 4000 bgp->vrf_export_rtl = NULL;
6a8657d0 4001 if (bgp->l2vnis)
bb7a24ab 4002 list_delete_and_null(&bgp->l2vnis);
6a8657d0 4003 bgp->l2vnis = NULL;
676f83b9 4004 bf_release_index(bm->rd_idspace, bgp->vrf_rd_id);
14c1a7bf 4005}
4006
4007/*
4008 * Initialization for EVPN
4009 * Create
4010 * VNI hash table
4011 * hash for RT to VNI
676f83b9 4012 * assign a unique rd id for auto derivation of vrf_prd
14c1a7bf 4013 */
d62a17ae 4014void bgp_evpn_init(struct bgp *bgp)
4015{
4016 bgp->vnihash =
4017 hash_create(vni_hash_key_make, vni_hash_cmp, "BGP VNI Hash");
4018 bgp->import_rt_hash =
4019 hash_create(import_rt_hash_key_make, import_rt_hash_cmp,
4020 "BGP Import RT Hash");
10ebe1ab
MK
4021 bgp->vrf_import_rt_hash =
4022 hash_create(vrf_import_rt_hash_key_make, vrf_import_rt_hash_cmp,
4023 "BGP VRF Import RT Hash");
c581d8b0
MK
4024 bgp->vrf_import_rtl = list_new();
4025 bgp->vrf_import_rtl->cmp =
4026 (int (*)(void *, void *))evpn_route_target_cmp;
4027
4028 bgp->vrf_export_rtl = list_new();
4029 bgp->vrf_export_rtl->cmp =
4030 (int (*)(void *, void *))evpn_route_target_cmp;
6a8657d0
MK
4031 bgp->l2vnis = list_new();
4032 bgp->l2vnis->cmp =
4033 (int (*)(void *, void *))vni_hash_cmp;
676f83b9 4034 bf_assign_index(bm->rd_idspace, bgp->vrf_rd_id);
4035
14c1a7bf 4036}
10ebe1ab
MK
4037
4038void bgp_evpn_vrf_delete(struct bgp *bgp_vrf)
4039{
4040 bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf);
4041}