]> git.proxmox.com Git - mirror_frr.git/blame - bgpd/bgp_evpn.c
Merge pull request #1592 from lihongguang/master
[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
95/*
96 * Make import route target hash key.
97 */
d62a17ae 98static unsigned int import_rt_hash_key_make(void *p)
14c1a7bf 99{
d62a17ae 100 struct irt_node *irt = p;
101 char *pnt = irt->rt.val;
102 unsigned int key = 0;
103 int c = 0;
14c1a7bf 104
d62a17ae 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];
14c1a7bf 113
d62a17ae 114 return (key);
14c1a7bf 115}
116
117/*
118 * Comparison function for import rt hash
119 */
d62a17ae 120static int import_rt_hash_cmp(const void *p1, const void *p2)
14c1a7bf 121{
d62a17ae 122 const struct irt_node *irt1 = p1;
123 const struct irt_node *irt2 = p2;
14c1a7bf 124
d62a17ae 125 if (irt1 == NULL && irt2 == NULL)
126 return 1;
14c1a7bf 127
d62a17ae 128 if (irt1 == NULL || irt2 == NULL)
129 return 0;
14c1a7bf 130
d62a17ae 131 return (memcmp(irt1->rt.val, irt2->rt.val, ECOMMUNITY_SIZE) == 0);
14c1a7bf 132}
133
7724c0a1 134/*
128ea8ab 135 * Create a new import_rt
136 */
d62a17ae 137static struct irt_node *import_rt_new(struct bgp *bgp,
138 struct ecommunity_val *rt)
128ea8ab 139{
d62a17ae 140 struct irt_node *irt;
128ea8ab 141
d62a17ae 142 if (!bgp)
143 return NULL;
128ea8ab 144
d62a17ae 145 irt = XCALLOC(MTYPE_BGP_EVPN_IMPORT_RT, sizeof(struct irt_node));
146 if (!irt)
147 return NULL;
128ea8ab 148
d62a17ae 149 irt->rt = *rt;
150 irt->vnis = list_new();
128ea8ab 151
d62a17ae 152 /* Add to hash */
153 if (!hash_get(bgp->import_rt_hash, irt, hash_alloc_intern)) {
154 XFREE(MTYPE_BGP_EVPN_IMPORT_RT, irt);
155 return NULL;
156 }
128ea8ab 157
d62a17ae 158 return irt;
128ea8ab 159}
160
161/*
162 * Free the import rt node
7724c0a1 163 */
d62a17ae 164static void import_rt_free(struct bgp *bgp, struct irt_node *irt)
7724c0a1 165{
d62a17ae 166 hash_release(bgp->import_rt_hash, irt);
167 XFREE(MTYPE_BGP_EVPN_IMPORT_RT, irt);
7724c0a1 168}
169
14c1a7bf 170/*
128ea8ab 171 * Function to lookup Import RT node - used to map a RT to set of
172 * VNIs importing routes with that RT.
173 */
d62a17ae 174static struct irt_node *lookup_import_rt(struct bgp *bgp,
175 struct ecommunity_val *rt)
128ea8ab 176{
d62a17ae 177 struct irt_node *irt;
178 struct irt_node tmp;
128ea8ab 179
d62a17ae 180 memset(&tmp, 0, sizeof(struct irt_node));
181 memcpy(&tmp.rt, rt, ECOMMUNITY_SIZE);
182 irt = hash_lookup(bgp->import_rt_hash, &tmp);
183 return irt;
128ea8ab 184}
185
186/*
187 * Is specified VNI present on the RT's list of "importing" VNIs?
188 */
d62a17ae 189static int is_vni_present_in_irt_vnis(struct list *vnis, struct bgpevpn *vpn)
128ea8ab 190{
d62a17ae 191 struct listnode *node, *nnode;
192 struct bgpevpn *tmp_vpn;
128ea8ab 193
d62a17ae 194 for (ALL_LIST_ELEMENTS(vnis, node, nnode, tmp_vpn)) {
195 if (tmp_vpn == vpn)
196 return 1;
197 }
128ea8ab 198
d62a17ae 199 return 0;
128ea8ab 200}
201
202/*
203 * Compare Route Targets.
204 */
d62a17ae 205static int evpn_route_target_cmp(struct ecommunity *ecom1,
206 struct ecommunity *ecom2)
128ea8ab 207{
d62a17ae 208 if (ecom1 && !ecom2)
209 return -1;
128ea8ab 210
d62a17ae 211 if (!ecom1 && ecom2)
212 return 1;
128ea8ab 213
d62a17ae 214 if (!ecom1 && !ecom2)
215 return 0;
128ea8ab 216
d62a17ae 217 if (ecom1->str && !ecom2->str)
218 return -1;
128ea8ab 219
d62a17ae 220 if (!ecom1->str && ecom2->str)
221 return 1;
128ea8ab 222
d62a17ae 223 if (!ecom1->str && !ecom2->str)
224 return 0;
128ea8ab 225
d62a17ae 226 return strcmp(ecom1->str, ecom2->str);
128ea8ab 227}
228
229/*
230 * Mask off global-admin field of specified extended community (RT),
231 * just retain the local-admin field.
232 */
d62a17ae 233static inline void mask_ecom_global_admin(struct ecommunity_val *dst,
234 struct ecommunity_val *src)
128ea8ab 235{
d62a17ae 236 u_char type;
128ea8ab 237
d62a17ae 238 type = src->val[0];
239 dst->val[0] = 0;
240 if (type == ECOMMUNITY_ENCODE_AS) {
241 dst->val[2] = dst->val[3] = 0;
242 } else if (type == ECOMMUNITY_ENCODE_AS4
243 || type == ECOMMUNITY_ENCODE_IP) {
244 dst->val[2] = dst->val[3] = 0;
245 dst->val[4] = dst->val[5] = 0;
246 }
128ea8ab 247}
248
249/*
250 * Map one RT to specified VNI.
14c1a7bf 251 */
d62a17ae 252static void map_vni_to_rt(struct bgp *bgp, struct bgpevpn *vpn,
253 struct ecommunity_val *eval)
128ea8ab 254{
d62a17ae 255 struct irt_node *irt;
256 struct ecommunity_val eval_tmp;
128ea8ab 257
d62a17ae 258 /* If using "automatic" RT, we only care about the local-admin
259 * sub-field.
260 * This is to facilitate using VNI as the RT for EBGP peering too.
261 */
262 memcpy(&eval_tmp, eval, ECOMMUNITY_SIZE);
263 if (!is_import_rt_configured(vpn))
264 mask_ecom_global_admin(&eval_tmp, eval);
128ea8ab 265
d62a17ae 266 irt = lookup_import_rt(bgp, &eval_tmp);
267 if (irt && irt->vnis)
268 if (is_vni_present_in_irt_vnis(irt->vnis, vpn))
269 /* Already mapped. */
270 return;
128ea8ab 271
d62a17ae 272 if (!irt) {
273 irt = import_rt_new(bgp, &eval_tmp);
274 assert(irt);
275 }
128ea8ab 276
d62a17ae 277 /* Add VNI to the hash list for this RT. */
278 listnode_add(irt->vnis, vpn);
128ea8ab 279}
280
281/*
282 * Unmap specified VNI from specified RT. If there are no other
283 * VNIs for this RT, then the RT hash is deleted.
284 */
d62a17ae 285static void unmap_vni_from_rt(struct bgp *bgp, struct bgpevpn *vpn,
286 struct irt_node *irt)
14c1a7bf 287{
d62a17ae 288 /* Delete VNI from hash list for this RT. */
289 listnode_delete(irt->vnis, vpn);
290 if (!listnode_head(irt->vnis)) {
acdf5e25 291 list_delete_and_null(&irt->vnis);
d62a17ae 292 import_rt_free(bgp, irt);
293 }
14c1a7bf 294}
295
128ea8ab 296/*
297 * Create RT extended community automatically from passed information:
298 * of the form AS:VNI.
299 * NOTE: We use only the lower 16 bits of the AS. This is sufficient as
300 * the need is to get a RT value that will be unique across different
301 * VNIs but the same across routers (in the same AS) for a particular
302 * VNI.
303 */
d62a17ae 304static void form_auto_rt(struct bgp *bgp, struct bgpevpn *vpn, struct list *rtl)
128ea8ab 305{
d62a17ae 306 struct ecommunity_val eval;
307 struct ecommunity *ecomadd;
128ea8ab 308
d62a17ae 309 encode_route_target_as((bgp->as & 0xFFFF), vpn->vni, &eval);
128ea8ab 310
d62a17ae 311 ecomadd = ecommunity_new();
312 ecommunity_add_val(ecomadd, &eval);
313 listnode_add_sort(rtl, ecomadd);
128ea8ab 314}
14c1a7bf 315
316/*
128ea8ab 317 * Derive RD and RT for a VNI automatically. Invoked at the time of
318 * creation of a VNI.
319 */
d62a17ae 320static void derive_rd_rt_for_vni(struct bgp *bgp, struct bgpevpn *vpn)
128ea8ab 321{
d62a17ae 322 bgp_evpn_derive_auto_rd(bgp, vpn);
323 bgp_evpn_derive_auto_rt_import(bgp, vpn);
324 bgp_evpn_derive_auto_rt_export(bgp, vpn);
128ea8ab 325}
326
327/*
328 * Add (update) or delete MACIP from zebra.
14c1a7bf 329 */
d62a17ae 330static int bgp_zebra_send_remote_macip(struct bgp *bgp, struct bgpevpn *vpn,
331 struct prefix_evpn *p,
332 struct in_addr remote_vtep_ip, int add,
333 u_char sticky)
334{
335 struct stream *s;
336 int ipa_len;
337 char buf1[ETHER_ADDR_STRLEN];
338 char buf2[INET6_ADDRSTRLEN];
339 char buf3[INET6_ADDRSTRLEN];
340
341 /* Check socket. */
342 if (!zclient || zclient->sock < 0)
343 return 0;
344
345 /* Don't try to register if Zebra doesn't know of this instance. */
346 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp))
347 return 0;
348
349 s = zclient->obuf;
350 stream_reset(s);
351
421bb26a
MK
352 zclient_create_header(s, add ? ZEBRA_REMOTE_MACIP_ADD
353 : ZEBRA_REMOTE_MACIP_DEL,
354 bgp->vrf_id);
d62a17ae 355 stream_putl(s, vpn->vni);
28328ea9 356 stream_put(s, &p->prefix.mac.octet, ETH_ALEN); /* Mac Addr */
d62a17ae 357 /* IP address length and IP address, if any. */
358 if (IS_EVPN_PREFIX_IPADDR_NONE(p))
359 stream_putl(s, 0);
360 else {
361 ipa_len = IS_EVPN_PREFIX_IPADDR_V4(p) ? IPV4_MAX_BYTELEN
362 : IPV6_MAX_BYTELEN;
363 stream_putl(s, ipa_len);
364 stream_put(s, &p->prefix.ip.ip.addr, ipa_len);
365 }
366 stream_put_in_addr(s, &remote_vtep_ip);
367
368 /* TX MAC sticky status */
369 if (add)
370 stream_putc(s, sticky);
371
372 stream_putw_at(s, 0, stream_get_endp(s));
373
374 if (bgp_debug_zebra(NULL))
375 zlog_debug("Tx %s MACIP, VNI %u %sMAC %s IP %s remote VTEP %s",
376 add ? "ADD" : "DEL", vpn->vni,
377 sticky ? "sticky " : "",
378 prefix_mac2str(&p->prefix.mac, buf1, sizeof(buf1)),
379 ipaddr2str(&p->prefix.ip, buf3, sizeof(buf3)),
380 inet_ntop(AF_INET, &remote_vtep_ip, buf2,
381 sizeof(buf2)));
382
383 return zclient_send_message(zclient);
7ef5a232 384}
b18825eb 385
128ea8ab 386/*
387 * Add (update) or delete remote VTEP from zebra.
388 */
d62a17ae 389static int bgp_zebra_send_remote_vtep(struct bgp *bgp, struct bgpevpn *vpn,
390 struct prefix_evpn *p, int add)
128ea8ab 391{
d62a17ae 392 struct stream *s;
128ea8ab 393
d62a17ae 394 /* Check socket. */
395 if (!zclient || zclient->sock < 0)
396 return 0;
128ea8ab 397
d62a17ae 398 /* Don't try to register if Zebra doesn't know of this instance. */
399 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp))
400 return 0;
128ea8ab 401
d62a17ae 402 s = zclient->obuf;
403 stream_reset(s);
128ea8ab 404
421bb26a
MK
405 zclient_create_header(s, add ? ZEBRA_REMOTE_VTEP_ADD
406 : ZEBRA_REMOTE_VTEP_DEL,
407 bgp->vrf_id);
d62a17ae 408 stream_putl(s, vpn->vni);
409 if (IS_EVPN_PREFIX_IPADDR_V4(p))
410 stream_put_in_addr(s, &p->prefix.ip.ipaddr_v4);
411 else if (IS_EVPN_PREFIX_IPADDR_V6(p)) {
412 zlog_err(
413 "Bad remote IP when trying to %s remote VTEP for VNI %u",
414 add ? "ADD" : "DEL", vpn->vni);
415 return -1;
416 }
128ea8ab 417
d62a17ae 418 stream_putw_at(s, 0, stream_get_endp(s));
128ea8ab 419
d62a17ae 420 if (bgp_debug_zebra(NULL))
421 zlog_debug("Tx %s Remote VTEP, VNI %u remote VTEP %s",
422 add ? "ADD" : "DEL", vpn->vni,
423 inet_ntoa(p->prefix.ip.ipaddr_v4));
128ea8ab 424
d62a17ae 425 return zclient_send_message(zclient);
128ea8ab 426}
427
428/*
429 * Build extended communities for EVPN route. RT and ENCAP are
430 * applicable to all routes.
431 */
d62a17ae 432static void build_evpn_route_extcomm(struct bgpevpn *vpn, struct attr *attr)
128ea8ab 433{
d62a17ae 434 struct ecommunity ecom_encap;
435 struct ecommunity ecom_sticky;
436 struct ecommunity_val eval;
437 struct ecommunity_val eval_sticky;
438 bgp_encap_types tnl_type;
439 struct listnode *node, *nnode;
440 struct ecommunity *ecom;
441 u_int32_t seqnum;
128ea8ab 442
d62a17ae 443 /* Encap */
444 tnl_type = BGP_ENCAP_TYPE_VXLAN;
445 memset(&ecom_encap, 0, sizeof(ecom_encap));
446 encode_encap_extcomm(tnl_type, &eval);
447 ecom_encap.size = 1;
448 ecom_encap.val = (u_int8_t *)eval.val;
128ea8ab 449
d62a17ae 450 /* Add Encap */
451 attr->ecommunity = ecommunity_dup(&ecom_encap);
128ea8ab 452
d62a17ae 453 /* Add the export RTs */
454 for (ALL_LIST_ELEMENTS(vpn->export_rtl, node, nnode, ecom))
455 attr->ecommunity = ecommunity_merge(attr->ecommunity, ecom);
128ea8ab 456
d62a17ae 457 if (attr->sticky) {
458 seqnum = 0;
459 memset(&ecom_sticky, 0, sizeof(ecom_sticky));
460 encode_mac_mobility_extcomm(1, seqnum, &eval_sticky);
461 ecom_sticky.size = 1;
462 ecom_sticky.val = (u_int8_t *)eval_sticky.val;
463 attr->ecommunity =
464 ecommunity_merge(attr->ecommunity, &ecom_sticky);
465 }
c85c03c7 466
d62a17ae 467 attr->flag |= ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES);
128ea8ab 468}
469
470/*
471 * Add MAC mobility extended community to attribute.
472 */
d62a17ae 473static void add_mac_mobility_to_attr(u_int32_t seq_num, struct attr *attr)
474{
475 struct ecommunity ecom_tmp;
476 struct ecommunity_val eval;
421bb26a 477 u_int8_t *ecom_val_ptr;
d62a17ae 478 int i;
479 u_int8_t *pnt;
480 int type = 0;
481 int sub_type = 0;
482
483 /* Build MM */
484 encode_mac_mobility_extcomm(0, seq_num, &eval);
485
486 /* Find current MM ecommunity */
421bb26a 487 ecom_val_ptr = NULL;
d62a17ae 488
489 if (attr->ecommunity) {
490 for (i = 0; i < attr->ecommunity->size; i++) {
491 pnt = attr->ecommunity->val + (i * 8);
492 type = *pnt++;
493 sub_type = *pnt++;
494
495 if (type == ECOMMUNITY_ENCODE_EVPN
496 && sub_type
497 == ECOMMUNITY_EVPN_SUBTYPE_MACMOBILITY) {
421bb26a
MK
498 ecom_val_ptr =
499 (u_int8_t *)(attr->ecommunity->val
500 + (i * 8));
d62a17ae 501 break;
502 }
503 }
504 }
505
506 /* Update the existing MM ecommunity */
421bb26a
MK
507 if (ecom_val_ptr) {
508 memcpy(ecom_val_ptr, eval.val, sizeof(char) * ECOMMUNITY_SIZE);
d62a17ae 509 }
510 /* Add MM to existing */
511 else {
512 memset(&ecom_tmp, 0, sizeof(ecom_tmp));
513 ecom_tmp.size = 1;
514 ecom_tmp.val = (u_int8_t *)eval.val;
515
516 attr->ecommunity =
517 ecommunity_merge(attr->ecommunity, &ecom_tmp);
518 }
128ea8ab 519}
520
521/* Install EVPN route into zebra. */
d62a17ae 522static int evpn_zebra_install(struct bgp *bgp, struct bgpevpn *vpn,
523 struct prefix_evpn *p,
524 struct in_addr remote_vtep_ip, u_char sticky)
128ea8ab 525{
d62a17ae 526 int ret;
128ea8ab 527
d62a17ae 528 if (p->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE)
529 ret = bgp_zebra_send_remote_macip(bgp, vpn, p, remote_vtep_ip,
530 1, sticky);
531 else
532 ret = bgp_zebra_send_remote_vtep(bgp, vpn, p, 1);
128ea8ab 533
d62a17ae 534 return ret;
128ea8ab 535}
536
537/* Uninstall EVPN route from zebra. */
d62a17ae 538static int evpn_zebra_uninstall(struct bgp *bgp, struct bgpevpn *vpn,
539 struct prefix_evpn *p,
540 struct in_addr remote_vtep_ip)
128ea8ab 541{
d62a17ae 542 int ret;
128ea8ab 543
d62a17ae 544 if (p->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE)
545 ret = bgp_zebra_send_remote_macip(bgp, vpn, p, remote_vtep_ip,
546 0, 0);
547 else
548 ret = bgp_zebra_send_remote_vtep(bgp, vpn, p, 0);
128ea8ab 549
d62a17ae 550 return ret;
128ea8ab 551}
552
553/*
554 * Due to MAC mobility, the prior "local" best route has been supplanted
555 * by a "remote" best route. The prior route has to be deleted and withdrawn
556 * from peers.
557 */
d62a17ae 558static void evpn_delete_old_local_route(struct bgp *bgp, struct bgpevpn *vpn,
559 struct bgp_node *rn,
560 struct bgp_info *old_local)
128ea8ab 561{
d62a17ae 562 struct bgp_node *global_rn;
563 struct bgp_info *ri;
564 afi_t afi = AFI_L2VPN;
565 safi_t safi = SAFI_EVPN;
128ea8ab 566
d62a17ae 567 /* Locate route node in the global EVPN routing table. Note that
568 * this table is a 2-level tree (RD-level + Prefix-level) similar to
569 * L3VPN routes.
570 */
571 global_rn = bgp_afi_node_lookup(bgp->rib[afi][safi], afi, safi,
572 (struct prefix *)&rn->p, &vpn->prd);
573 if (global_rn) {
574 /* Delete route entry in the global EVPN table. */
575 delete_evpn_route_entry(bgp, vpn, afi, safi, global_rn, &ri);
128ea8ab 576
d62a17ae 577 /* Schedule for processing - withdraws to peers happen from
578 * this table.
579 */
580 if (ri)
581 bgp_process(bgp, global_rn, afi, safi);
582 bgp_unlock_node(global_rn);
583 }
128ea8ab 584
d62a17ae 585 /* Delete route entry in the VNI route table, caller to remove. */
586 bgp_info_delete(rn, old_local);
128ea8ab 587}
588
589/*
590 * Calculate the best path for an EVPN route. Install/update best path in zebra,
591 * if appropriate.
592 */
d62a17ae 593static int evpn_route_select_install(struct bgp *bgp, struct bgpevpn *vpn,
594 struct bgp_node *rn)
595{
596 struct bgp_info *old_select, *new_select;
597 struct bgp_info_pair old_and_new;
598 afi_t afi = AFI_L2VPN;
599 safi_t safi = SAFI_EVPN;
600 int ret = 0;
601
602 /* Compute the best path. */
603 bgp_best_selection(bgp, rn, &bgp->maxpaths[afi][safi], &old_and_new,
604 afi, safi);
605 old_select = old_and_new.old;
606 new_select = old_and_new.new;
607
608 /* If the best path hasn't changed - see if there is still something to
609 * update
610 * to zebra RIB.
611 */
612 if (old_select && old_select == new_select
613 && old_select->type == ZEBRA_ROUTE_BGP
614 && old_select->sub_type == BGP_ROUTE_NORMAL
615 && !CHECK_FLAG(rn->flags, BGP_NODE_USER_CLEAR)
616 && !CHECK_FLAG(old_select->flags, BGP_INFO_ATTR_CHANGED)
617 && !bgp->addpath_tx_used[afi][safi]) {
618 if (bgp_zebra_has_route_changed(rn, old_select))
619 ret = evpn_zebra_install(bgp, vpn,
620 (struct prefix_evpn *)&rn->p,
621 old_select->attr->nexthop,
622 old_select->attr->sticky);
623 UNSET_FLAG(old_select->flags, BGP_INFO_MULTIPATH_CHG);
624 bgp_zebra_clear_route_change_flags(rn);
625 return ret;
626 }
627
628 /* If the user did a "clear" this flag will be set */
629 UNSET_FLAG(rn->flags, BGP_NODE_USER_CLEAR);
630
631 /* bestpath has changed; update relevant fields and install or uninstall
632 * into the zebra RIB.
633 */
634 if (old_select || new_select)
635 bgp_bump_version(rn);
636
637 if (old_select)
638 bgp_info_unset_flag(rn, old_select, BGP_INFO_SELECTED);
639 if (new_select) {
640 bgp_info_set_flag(rn, new_select, BGP_INFO_SELECTED);
641 bgp_info_unset_flag(rn, new_select, BGP_INFO_ATTR_CHANGED);
642 UNSET_FLAG(new_select->flags, BGP_INFO_MULTIPATH_CHG);
643 }
644
645 if (new_select && new_select->type == ZEBRA_ROUTE_BGP
646 && new_select->sub_type == BGP_ROUTE_NORMAL) {
647 ret = evpn_zebra_install(bgp, vpn, (struct prefix_evpn *)&rn->p,
648 new_select->attr->nexthop,
649 new_select->attr->sticky);
650 /* If an old best existed and it was a "local" route, the only
651 * reason
652 * it would be supplanted is due to MAC mobility procedures. So,
653 * we
654 * need to do an implicit delete and withdraw that route from
655 * peers.
656 */
657 if (old_select && old_select->peer == bgp->peer_self
658 && old_select->type == ZEBRA_ROUTE_BGP
659 && old_select->sub_type == BGP_ROUTE_STATIC)
660 evpn_delete_old_local_route(bgp, vpn, rn, old_select);
661 } else {
662 if (old_select && old_select->type == ZEBRA_ROUTE_BGP
663 && old_select->sub_type == BGP_ROUTE_NORMAL)
664 ret = evpn_zebra_uninstall(bgp, vpn,
665 (struct prefix_evpn *)&rn->p,
666 old_select->attr->nexthop);
667 }
668
669 /* Clear any route change flags. */
670 bgp_zebra_clear_route_change_flags(rn);
671
672 /* Reap old select bgp_info, if it has been removed */
673 if (old_select && CHECK_FLAG(old_select->flags, BGP_INFO_REMOVED))
674 bgp_info_reap(rn, old_select);
675
676 return ret;
128ea8ab 677}
678
c85c03c7 679
680/*
681 * Return true if the local ri for this rn has sticky set
682 */
d62a17ae 683static int evpn_route_is_sticky(struct bgp *bgp, struct bgp_node *rn)
c85c03c7 684{
d62a17ae 685 struct bgp_info *tmp_ri;
686 struct bgp_info *local_ri;
c85c03c7 687
d62a17ae 688 local_ri = NULL;
689 for (tmp_ri = rn->info; tmp_ri; tmp_ri = tmp_ri->next) {
690 if (tmp_ri->peer == bgp->peer_self
691 && tmp_ri->type == ZEBRA_ROUTE_BGP
692 && tmp_ri->sub_type == BGP_ROUTE_STATIC)
693 local_ri = tmp_ri;
694 }
c85c03c7 695
d62a17ae 696 if (!local_ri)
697 return 0;
c85c03c7 698
d62a17ae 699 return local_ri->attr->sticky;
c85c03c7 700}
701
128ea8ab 702/*
703 * Create or update EVPN route entry. This could be in the VNI route table
704 * or the global route table.
705 */
d62a17ae 706static int update_evpn_route_entry(struct bgp *bgp, struct bgpevpn *vpn,
707 afi_t afi, safi_t safi, struct bgp_node *rn,
708 struct attr *attr, int add, int vni_table,
1a98c087 709 struct bgp_info **ri, u_char flags)
d62a17ae 710{
711 struct bgp_info *tmp_ri;
712 struct bgp_info *local_ri, *remote_ri;
713 struct attr *attr_new;
714 mpls_label_t label = MPLS_INVALID_LABEL;
715 int route_change = 1;
716 u_char sticky = 0;
717
718 *ri = NULL;
719
720 /* See if this is an update of an existing route, or a new add. Also,
721 * identify if already known from remote, and if so, the one with the
722 * highest sequence number; this is only when adding to the VNI routing
723 * table.
724 */
725 local_ri = remote_ri = NULL;
726 for (tmp_ri = rn->info; tmp_ri; tmp_ri = tmp_ri->next) {
727 if (tmp_ri->peer == bgp->peer_self
728 && tmp_ri->type == ZEBRA_ROUTE_BGP
729 && tmp_ri->sub_type == BGP_ROUTE_STATIC)
730 local_ri = tmp_ri;
731 if (vni_table) {
732 if (tmp_ri->type == ZEBRA_ROUTE_BGP
733 && tmp_ri->sub_type == BGP_ROUTE_NORMAL
734 && CHECK_FLAG(tmp_ri->flags, BGP_INFO_VALID)) {
735 if (!remote_ri)
736 remote_ri = tmp_ri;
737 else if (mac_mobility_seqnum(tmp_ri->attr)
738 > mac_mobility_seqnum(remote_ri->attr))
739 remote_ri = tmp_ri;
740 }
741 }
742 }
743
744 /* If route doesn't exist already, create a new one, if told to.
745 * Otherwise act based on whether the attributes of the route have
746 * changed or not.
747 */
748 if (!local_ri && !add)
749 return 0;
750
751 if (!local_ri) {
752 /* When learnt locally for the first time but already known from
753 * remote, we have to initiate appropriate MAC mobility steps.
754 * This
755 * is applicable when updating the VNI routing table.
1a98c087
MK
756 * We need to skip mobility steps for g/w macs (local mac on g/w
757 * SVI) advertised in EVPN.
758 * This will ensure that local routes are preferred for g/w macs
d62a17ae 759 */
1a98c087 760 if (remote_ri && !CHECK_FLAG(flags, ZEBRA_MAC_TYPE_GW)) {
d62a17ae 761 u_int32_t cur_seqnum;
762
763 /* Add MM extended community to route. */
764 cur_seqnum = mac_mobility_seqnum(remote_ri->attr);
765 add_mac_mobility_to_attr(cur_seqnum + 1, attr);
766 }
767
768 /* Add (or update) attribute to hash. */
769 attr_new = bgp_attr_intern(attr);
770
771 /* Extract MAC mobility sequence number, if any. */
772 attr_new->mm_seqnum =
773 bgp_attr_mac_mobility_seqnum(attr_new, &sticky);
774 attr_new->sticky = sticky;
775
776 /* Create new route with its attribute. */
777 tmp_ri = info_make(ZEBRA_ROUTE_BGP, BGP_ROUTE_STATIC, 0,
778 bgp->peer_self, attr_new, rn);
779 SET_FLAG(tmp_ri->flags, BGP_INFO_VALID);
780 bgp_info_extra_get(tmp_ri);
781
782 /* The VNI goes into the 'label' field of the route */
783 vni2label(vpn->vni, &label);
784
785 memcpy(&tmp_ri->extra->label, &label, BGP_LABEL_BYTES);
786 bgp_info_add(rn, tmp_ri);
787 } else {
788 tmp_ri = local_ri;
789 if (attrhash_cmp(tmp_ri->attr, attr)
790 && !CHECK_FLAG(tmp_ri->flags, BGP_INFO_REMOVED))
791 route_change = 0;
792 else {
793 /* The attribute has changed. */
794 /* Add (or update) attribute to hash. */
795 attr_new = bgp_attr_intern(attr);
796 bgp_info_set_flag(rn, tmp_ri, BGP_INFO_ATTR_CHANGED);
797
798 /* Restore route, if needed. */
799 if (CHECK_FLAG(tmp_ri->flags, BGP_INFO_REMOVED))
800 bgp_info_restore(rn, tmp_ri);
801
802 /* Unintern existing, set to new. */
803 bgp_attr_unintern(&tmp_ri->attr);
804 tmp_ri->attr = attr_new;
805 tmp_ri->uptime = bgp_clock();
806 }
807 }
808
809 /* Return back the route entry. */
810 *ri = tmp_ri;
811 return route_change;
128ea8ab 812}
813
814/*
815 * Create or update EVPN route (of type based on prefix) for specified VNI
816 * and schedule for processing.
817 */
d62a17ae 818static int update_evpn_route(struct bgp *bgp, struct bgpevpn *vpn,
1a98c087 819 struct prefix_evpn *p, u_char flags)
128ea8ab 820{
d62a17ae 821 struct bgp_node *rn;
822 struct attr attr;
823 struct attr *attr_new;
824 struct bgp_info *ri;
825 afi_t afi = AFI_L2VPN;
826 safi_t safi = SAFI_EVPN;
827 int route_change;
128ea8ab 828
d62a17ae 829 memset(&attr, 0, sizeof(struct attr));
128ea8ab 830
d62a17ae 831 /* Build path-attribute for this route. */
832 bgp_attr_default_set(&attr, BGP_ORIGIN_IGP);
833 attr.nexthop = vpn->originator_ip;
834 attr.mp_nexthop_global_in = vpn->originator_ip;
835 attr.mp_nexthop_len = BGP_ATTR_NHLEN_IPV4;
1a98c087 836 attr.sticky = CHECK_FLAG(flags, ZEBRA_MAC_TYPE_STICKY) ? 1 : 0;
128ea8ab 837
d62a17ae 838 /* Set up RT and ENCAP extended community. */
839 build_evpn_route_extcomm(vpn, &attr);
128ea8ab 840
d62a17ae 841 /* First, create (or fetch) route node within the VNI. */
842 /* NOTE: There is no RD here. */
843 rn = bgp_node_get(vpn->route_table, (struct prefix *)p);
128ea8ab 844
d62a17ae 845 /* Create or update route entry. */
846 route_change = update_evpn_route_entry(bgp, vpn, afi, safi, rn, &attr,
1a98c087 847 1, 1, &ri, flags);
d62a17ae 848 assert(ri);
849 attr_new = ri->attr;
128ea8ab 850
d62a17ae 851 /* Perform route selection; this is just to set the flags correctly
852 * as local route in the VNI always wins.
853 */
854 evpn_route_select_install(bgp, vpn, rn);
855 bgp_unlock_node(rn);
128ea8ab 856
d62a17ae 857 /* If this is a new route or some attribute has changed, export the
858 * route to the global table. The route will be advertised to peers
859 * from there. Note that this table is a 2-level tree (RD-level +
860 * Prefix-level) similar to L3VPN routes.
861 */
862 if (route_change) {
863 struct bgp_info *global_ri;
128ea8ab 864
d62a17ae 865 rn = bgp_afi_node_get(bgp->rib[afi][safi], afi, safi,
866 (struct prefix *)p, &vpn->prd);
867 update_evpn_route_entry(bgp, vpn, afi, safi, rn, attr_new, 1, 0,
1a98c087 868 &global_ri, flags);
128ea8ab 869
d62a17ae 870 /* Schedule for processing and unlock node. */
871 bgp_process(bgp, rn, afi, safi);
872 bgp_unlock_node(rn);
873 }
128ea8ab 874
d62a17ae 875 /* Unintern temporary. */
876 aspath_unintern(&attr.aspath);
128ea8ab 877
d62a17ae 878 return 0;
128ea8ab 879}
880
881/*
882 * Delete EVPN route entry. This could be in the VNI route table
883 * or the global route table.
884 */
d62a17ae 885static void delete_evpn_route_entry(struct bgp *bgp, struct bgpevpn *vpn,
886 afi_t afi, safi_t safi, struct bgp_node *rn,
887 struct bgp_info **ri)
128ea8ab 888{
d62a17ae 889 struct bgp_info *tmp_ri;
128ea8ab 890
d62a17ae 891 *ri = NULL;
128ea8ab 892
d62a17ae 893 /* Now, find matching route. */
894 for (tmp_ri = rn->info; tmp_ri; tmp_ri = tmp_ri->next)
895 if (tmp_ri->peer == bgp->peer_self
896 && tmp_ri->type == ZEBRA_ROUTE_BGP
897 && tmp_ri->sub_type == BGP_ROUTE_STATIC)
898 break;
128ea8ab 899
d62a17ae 900 *ri = tmp_ri;
128ea8ab 901
d62a17ae 902 /* Mark route for delete. */
903 if (tmp_ri)
904 bgp_info_delete(rn, tmp_ri);
128ea8ab 905}
906
907/*
908 * Delete EVPN route (of type based on prefix) for specified VNI and
909 * schedule for processing.
910 */
d62a17ae 911static int delete_evpn_route(struct bgp *bgp, struct bgpevpn *vpn,
912 struct prefix_evpn *p)
913{
914 struct bgp_node *rn, *global_rn;
915 struct bgp_info *ri;
916 afi_t afi = AFI_L2VPN;
917 safi_t safi = SAFI_EVPN;
918
919 /* First, locate the route node within the VNI. If it doesn't exist,
920 * there
921 * is nothing further to do.
922 */
923 /* NOTE: There is no RD here. */
924 rn = bgp_node_lookup(vpn->route_table, (struct prefix *)p);
925 if (!rn)
926 return 0;
927
928 /* Next, locate route node in the global EVPN routing table. Note that
929 * this table is a 2-level tree (RD-level + Prefix-level) similar to
930 * L3VPN routes.
931 */
932 global_rn = bgp_afi_node_lookup(bgp->rib[afi][safi], afi, safi,
933 (struct prefix *)p, &vpn->prd);
934 if (global_rn) {
935 /* Delete route entry in the global EVPN table. */
936 delete_evpn_route_entry(bgp, vpn, afi, safi, global_rn, &ri);
937
938 /* Schedule for processing - withdraws to peers happen from
939 * this table.
940 */
941 if (ri)
942 bgp_process(bgp, global_rn, afi, safi);
943 bgp_unlock_node(global_rn);
944 }
945
946 /* Delete route entry in the VNI route table. This can just be removed.
947 */
948 delete_evpn_route_entry(bgp, vpn, afi, safi, rn, &ri);
949 if (ri)
950 bgp_info_reap(rn, ri);
951 bgp_unlock_node(rn);
952
953 return 0;
128ea8ab 954}
955
956/*
957 * Update all type-2 (MACIP) local routes for this VNI - these should also
958 * be scheduled for advertise to peers.
959 */
d62a17ae 960static int update_all_type2_routes(struct bgp *bgp, struct bgpevpn *vpn)
961{
962 afi_t afi;
963 safi_t safi;
964 struct bgp_node *rn;
965 struct bgp_info *ri;
966 struct attr attr;
967 struct attr attr_sticky;
968 struct attr *attr_new;
969
970 afi = AFI_L2VPN;
971 safi = SAFI_EVPN;
972 memset(&attr, 0, sizeof(struct attr));
973 memset(&attr_sticky, 0, sizeof(struct attr));
974
975 /* Build path-attribute - all type-2 routes for this VNI will share the
976 * same path attribute.
977 */
978 bgp_attr_default_set(&attr, BGP_ORIGIN_IGP);
979 bgp_attr_default_set(&attr_sticky, BGP_ORIGIN_IGP);
980 attr.nexthop = vpn->originator_ip;
981 attr.mp_nexthop_global_in = vpn->originator_ip;
982 attr.mp_nexthop_len = BGP_ATTR_NHLEN_IPV4;
983 attr_sticky.nexthop = vpn->originator_ip;
984 attr_sticky.mp_nexthop_global_in = vpn->originator_ip;
985 attr_sticky.mp_nexthop_len = BGP_ATTR_NHLEN_IPV4;
986 attr_sticky.sticky = 1;
987
988 /* Set up RT, ENCAP and sticky MAC extended community. */
989 build_evpn_route_extcomm(vpn, &attr);
990 build_evpn_route_extcomm(vpn, &attr_sticky);
991
992 /* Walk this VNI's route table and update local type-2 routes. For any
993 * routes updated, update corresponding entry in the global table too.
994 */
995 for (rn = bgp_table_top(vpn->route_table); rn;
996 rn = bgp_route_next(rn)) {
997 struct prefix_evpn *evp = (struct prefix_evpn *)&rn->p;
998 struct bgp_node *rd_rn;
999 struct bgp_info *global_ri;
1000
1001 if (evp->prefix.route_type != BGP_EVPN_MAC_IP_ROUTE)
1002 continue;
1003
1004 if (evpn_route_is_sticky(bgp, rn))
1005 update_evpn_route_entry(bgp, vpn, afi, safi, rn,
1a98c087 1006 &attr_sticky, 0, 1, &ri, 0);
d62a17ae 1007 else
1008 update_evpn_route_entry(bgp, vpn, afi, safi, rn, &attr,
1a98c087 1009 0, 1, &ri, 0);
d62a17ae 1010
1011 /* If a local route exists for this prefix, we need to update
1012 * the global routing table too.
1013 */
1014 if (!ri)
1015 continue;
1016
1017 /* Perform route selection; this is just to set the flags
1018 * correctly
1019 * as local route in the VNI always wins.
1020 */
1021 evpn_route_select_install(bgp, vpn, rn);
1022
1023 attr_new = ri->attr;
1024
1025 /* Update route in global routing table. */
1026 rd_rn = bgp_afi_node_get(bgp->rib[afi][safi], afi, safi,
1027 (struct prefix *)evp, &vpn->prd);
1028 assert(rd_rn);
1029 update_evpn_route_entry(bgp, vpn, afi, safi, rd_rn, attr_new, 0,
1a98c087 1030 0, &global_ri, 0);
d62a17ae 1031
1032 /* Schedule for processing and unlock node. */
1033 bgp_process(bgp, rd_rn, afi, safi);
1034 bgp_unlock_node(rd_rn);
1035 }
1036
1037 /* Unintern temporary. */
1038 aspath_unintern(&attr.aspath);
1039 aspath_unintern(&attr_sticky.aspath);
1040
1041 return 0;
128ea8ab 1042}
1043
1044/*
1045 * Delete all type-2 (MACIP) local routes for this VNI - only from the
1046 * global routing table. These are also scheduled for withdraw from peers.
1047 */
d62a17ae 1048static int delete_global_type2_routes(struct bgp *bgp, struct bgpevpn *vpn)
128ea8ab 1049{
d62a17ae 1050 afi_t afi;
1051 safi_t safi;
1052 struct bgp_node *rdrn, *rn;
1053 struct bgp_table *table;
1054 struct bgp_info *ri;
128ea8ab 1055
d62a17ae 1056 afi = AFI_L2VPN;
1057 safi = SAFI_EVPN;
128ea8ab 1058
d62a17ae 1059 rdrn = bgp_node_lookup(bgp->rib[afi][safi], (struct prefix *)&vpn->prd);
1060 if (rdrn && rdrn->info) {
1061 table = (struct bgp_table *)rdrn->info;
1062 for (rn = bgp_table_top(table); rn; rn = bgp_route_next(rn)) {
1063 struct prefix_evpn *evp = (struct prefix_evpn *)&rn->p;
128ea8ab 1064
d62a17ae 1065 if (evp->prefix.route_type != BGP_EVPN_MAC_IP_ROUTE)
1066 continue;
128ea8ab 1067
d62a17ae 1068 delete_evpn_route_entry(bgp, vpn, afi, safi, rn, &ri);
1069 if (ri)
1070 bgp_process(bgp, rn, afi, safi);
1071 }
1072 }
128ea8ab 1073
d62a17ae 1074 /* Unlock RD node. */
1075 if (rdrn)
1076 bgp_unlock_node(rdrn);
128ea8ab 1077
d62a17ae 1078 return 0;
128ea8ab 1079}
1080
1081/*
1082 * Delete all type-2 (MACIP) local routes for this VNI - from the global
1083 * table as well as the per-VNI route table.
1084 */
d62a17ae 1085static int delete_all_type2_routes(struct bgp *bgp, struct bgpevpn *vpn)
128ea8ab 1086{
d62a17ae 1087 afi_t afi;
1088 safi_t safi;
1089 struct bgp_node *rn;
1090 struct bgp_info *ri;
128ea8ab 1091
d62a17ae 1092 afi = AFI_L2VPN;
1093 safi = SAFI_EVPN;
128ea8ab 1094
d62a17ae 1095 /* First, walk the global route table for this VNI's type-2 local
1096 * routes.
1097 * EVPN routes are a 2-level table, first get the RD table.
1098 */
1099 delete_global_type2_routes(bgp, vpn);
128ea8ab 1100
d62a17ae 1101 /* Next, walk this VNI's route table and delete local type-2 routes. */
1102 for (rn = bgp_table_top(vpn->route_table); rn;
1103 rn = bgp_route_next(rn)) {
1104 struct prefix_evpn *evp = (struct prefix_evpn *)&rn->p;
128ea8ab 1105
d62a17ae 1106 if (evp->prefix.route_type != BGP_EVPN_MAC_IP_ROUTE)
1107 continue;
128ea8ab 1108
d62a17ae 1109 delete_evpn_route_entry(bgp, vpn, afi, safi, rn, &ri);
128ea8ab 1110
d62a17ae 1111 /* Route entry in local table gets deleted immediately. */
1112 if (ri)
1113 bgp_info_reap(rn, ri);
1114 }
128ea8ab 1115
d62a17ae 1116 return 0;
128ea8ab 1117}
1118
1119/*
1120 * Delete all routes in the per-VNI route table.
1121 */
d62a17ae 1122static int delete_all_vni_routes(struct bgp *bgp, struct bgpevpn *vpn)
128ea8ab 1123{
d62a17ae 1124 struct bgp_node *rn;
1125 struct bgp_info *ri, *nextri;
128ea8ab 1126
d62a17ae 1127 /* Walk this VNI's route table and delete all routes. */
1128 for (rn = bgp_table_top(vpn->route_table); rn;
1129 rn = bgp_route_next(rn)) {
1130 for (ri = rn->info; (ri != NULL) && (nextri = ri->next, 1);
1131 ri = nextri) {
1132 bgp_info_delete(rn, ri);
1133 bgp_info_reap(rn, ri);
1134 }
1135 }
128ea8ab 1136
d62a17ae 1137 return 0;
128ea8ab 1138}
1139
1140/*
1141 * Update (and advertise) local routes for a VNI. Invoked upon the VNI
1142 * export RT getting modified or change to tunnel IP. Note that these
1143 * situations need the route in the per-VNI table as well as the global
1144 * table to be updated (as attributes change).
1145 */
d62a17ae 1146static int update_routes_for_vni(struct bgp *bgp, struct bgpevpn *vpn)
128ea8ab 1147{
d62a17ae 1148 int ret;
1149 struct prefix_evpn p;
128ea8ab 1150
d62a17ae 1151 /* Update and advertise the type-3 route (only one) followed by the
1152 * locally learnt type-2 routes (MACIP) - for this VNI.
1153 */
1154 build_evpn_type3_prefix(&p, vpn->originator_ip);
1155 ret = update_evpn_route(bgp, vpn, &p, 0);
1156 if (ret)
1157 return ret;
128ea8ab 1158
d62a17ae 1159 return update_all_type2_routes(bgp, vpn);
128ea8ab 1160}
1161
1162/*
1163 * Delete (and withdraw) local routes for specified VNI from the global
1164 * table and per-VNI table. After this, remove all other routes from
1165 * the per-VNI table. Invoked upon the VNI being deleted or EVPN
1166 * (advertise-all-vni) being disabled.
1167 */
d62a17ae 1168static int delete_routes_for_vni(struct bgp *bgp, struct bgpevpn *vpn)
128ea8ab 1169{
d62a17ae 1170 int ret;
1171 struct prefix_evpn p;
128ea8ab 1172
d62a17ae 1173 /* Delete and withdraw locally learnt type-2 routes (MACIP)
1174 * followed by type-3 routes (only one) - for this VNI.
1175 */
1176 ret = delete_all_type2_routes(bgp, vpn);
1177 if (ret)
1178 return ret;
128ea8ab 1179
d62a17ae 1180 build_evpn_type3_prefix(&p, vpn->originator_ip);
1181 ret = delete_evpn_route(bgp, vpn, &p);
1182 if (ret)
1183 return ret;
128ea8ab 1184
d62a17ae 1185 /* Delete all routes from the per-VNI table. */
1186 return delete_all_vni_routes(bgp, vpn);
128ea8ab 1187}
1188
1189/*
1190 * There is a tunnel endpoint IP address change for this VNI,
1191 * need to re-advertise routes with the new nexthop.
1192 */
d62a17ae 1193static int handle_tunnel_ip_change(struct bgp *bgp, struct bgpevpn *vpn,
1194 struct in_addr originator_ip)
128ea8ab 1195{
d62a17ae 1196 struct prefix_evpn p;
128ea8ab 1197
ddd16ed5
MK
1198 /* If VNI is not live, we only need to update the originator ip */
1199 if (!is_vni_live(vpn)) {
1200 vpn->originator_ip = originator_ip;
1201 return 0;
1202 }
1203
db0e1937
MK
1204 /* Update the tunnel-ip hash */
1205 bgp_tip_del(bgp, &vpn->originator_ip);
1206 bgp_tip_add(bgp, &originator_ip);
1207
1208 /* filter routes as martian nexthop db has changed */
1209 bgp_filter_evpn_routes_upon_martian_nh_change(bgp);
1210
d62a17ae 1211 /* Need to withdraw type-3 route as the originator IP is part
1212 * of the key.
1213 */
1214 build_evpn_type3_prefix(&p, vpn->originator_ip);
1215 delete_evpn_route(bgp, vpn, &p);
128ea8ab 1216
d62a17ae 1217 /* Update the tunnel IP and re-advertise all routes for this VNI. */
1218 vpn->originator_ip = originator_ip;
1219 return update_routes_for_vni(bgp, vpn);
128ea8ab 1220}
1221
1222/*
1223 * Install route entry into the VNI routing table and invoke route selection.
1224 */
d62a17ae 1225static int install_evpn_route_entry(struct bgp *bgp, struct bgpevpn *vpn,
1226 struct prefix_evpn *p,
1227 struct bgp_info *parent_ri)
1228{
1229 struct bgp_node *rn;
1230 struct bgp_info *ri;
1231 struct attr *attr_new;
1232 int ret;
1233
1234 /* Create (or fetch) route within the VNI. */
1235 /* NOTE: There is no RD here. */
1236 rn = bgp_node_get(vpn->route_table, (struct prefix *)p);
1237
1238 /* Check if route entry is already present. */
1239 for (ri = rn->info; ri; ri = ri->next)
1240 if (ri->extra
1241 && (struct bgp_info *)ri->extra->parent == parent_ri)
1242 break;
1243
1244 if (!ri) {
1245 /* Add (or update) attribute to hash. */
1246 attr_new = bgp_attr_intern(parent_ri->attr);
1247
1248 /* Create new route with its attribute. */
1249 ri = info_make(parent_ri->type, parent_ri->sub_type, 0,
1250 parent_ri->peer, attr_new, rn);
1251 SET_FLAG(ri->flags, BGP_INFO_VALID);
1252 bgp_info_extra_get(ri);
1253 ri->extra->parent = parent_ri;
1254 if (parent_ri->extra)
1255 memcpy(&ri->extra->label, &parent_ri->extra->label,
1256 BGP_LABEL_BYTES);
1257 bgp_info_add(rn, ri);
1258 } else {
1259 if (attrhash_cmp(ri->attr, parent_ri->attr)
1260 && !CHECK_FLAG(ri->flags, BGP_INFO_REMOVED)) {
1261 bgp_unlock_node(rn);
1262 return 0;
1263 }
1264 /* The attribute has changed. */
1265 /* Add (or update) attribute to hash. */
1266 attr_new = bgp_attr_intern(parent_ri->attr);
1267
1268 /* Restore route, if needed. */
1269 if (CHECK_FLAG(ri->flags, BGP_INFO_REMOVED))
1270 bgp_info_restore(rn, ri);
1271
1272 /* Mark if nexthop has changed. */
1273 if (!IPV4_ADDR_SAME(&ri->attr->nexthop, &attr_new->nexthop))
1274 SET_FLAG(ri->flags, BGP_INFO_IGP_CHANGED);
1275
1276 /* Unintern existing, set to new. */
1277 bgp_attr_unintern(&ri->attr);
1278 ri->attr = attr_new;
1279 ri->uptime = bgp_clock();
1280 }
1281
1282 /* Perform route selection and update zebra, if required. */
1283 ret = evpn_route_select_install(bgp, vpn, rn);
1284
1285 return ret;
128ea8ab 1286}
1287
1288/*
1289 * Uninstall route entry from the VNI routing table and send message
1290 * to zebra, if appropriate.
1291 */
d62a17ae 1292static int uninstall_evpn_route_entry(struct bgp *bgp, struct bgpevpn *vpn,
1293 struct prefix_evpn *p,
1294 struct bgp_info *parent_ri)
128ea8ab 1295{
d62a17ae 1296 struct bgp_node *rn;
1297 struct bgp_info *ri;
1298 int ret;
128ea8ab 1299
d62a17ae 1300 /* Locate route within the VNI. */
1301 /* NOTE: There is no RD here. */
1302 rn = bgp_node_lookup(vpn->route_table, (struct prefix *)p);
1303 if (!rn)
1304 return 0;
128ea8ab 1305
d62a17ae 1306 /* Find matching route entry. */
1307 for (ri = rn->info; ri; ri = ri->next)
1308 if (ri->extra
1309 && (struct bgp_info *)ri->extra->parent == parent_ri)
1310 break;
128ea8ab 1311
d62a17ae 1312 if (!ri)
1313 return 0;
128ea8ab 1314
d62a17ae 1315 /* Mark entry for deletion */
1316 bgp_info_delete(rn, ri);
128ea8ab 1317
d62a17ae 1318 /* Perform route selection and update zebra, if required. */
1319 ret = evpn_route_select_install(bgp, vpn, rn);
128ea8ab 1320
d62a17ae 1321 /* Unlock route node. */
1322 bgp_unlock_node(rn);
128ea8ab 1323
d62a17ae 1324 return ret;
128ea8ab 1325}
1326
1327/*
1328 * Given a route entry and a VNI, see if this route entry should be
1329 * imported into the VNI i.e., RTs match.
1330 */
d62a17ae 1331static int is_route_matching_for_vni(struct bgp *bgp, struct bgpevpn *vpn,
1332 struct bgp_info *ri)
1333{
1334 struct attr *attr = ri->attr;
1335 struct ecommunity *ecom;
1336 int i;
1337
1338 assert(attr);
1339 /* Route should have valid RT to be even considered. */
1340 if (!(attr->flag & ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES)))
1341 return 0;
1342
1343 ecom = attr->ecommunity;
1344 if (!ecom || !ecom->size)
1345 return 0;
1346
1347 /* For each extended community RT, see if it matches this VNI. If any RT
1348 * matches, we're done.
1349 */
1350 for (i = 0; i < ecom->size; i++) {
1351 u_char *pnt;
1352 u_char type, sub_type;
1353 struct ecommunity_val *eval;
1354 struct ecommunity_val eval_tmp;
1355 struct irt_node *irt;
1356
1357 /* Only deal with RTs */
1358 pnt = (ecom->val + (i * ECOMMUNITY_SIZE));
1359 eval = (struct ecommunity_val *)(ecom->val
1360 + (i * ECOMMUNITY_SIZE));
1361 type = *pnt++;
1362 sub_type = *pnt++;
1363 if (sub_type != ECOMMUNITY_ROUTE_TARGET)
1364 continue;
1365
1366 /* See if this RT matches specified VNIs import RTs */
1367 irt = lookup_import_rt(bgp, eval);
1368 if (irt && irt->vnis)
1369 if (is_vni_present_in_irt_vnis(irt->vnis, vpn))
1370 return 1;
1371
1372 /* Also check for non-exact match. In this, we mask out the AS
1373 * and
1374 * only check on the local-admin sub-field. This is to
1375 * facilitate using
1376 * VNI as the RT for EBGP peering too.
1377 */
1378 irt = NULL;
1379 if (type == ECOMMUNITY_ENCODE_AS
1380 || type == ECOMMUNITY_ENCODE_AS4
1381 || type == ECOMMUNITY_ENCODE_IP) {
1382 memcpy(&eval_tmp, eval, ECOMMUNITY_SIZE);
1383 mask_ecom_global_admin(&eval_tmp, eval);
1384 irt = lookup_import_rt(bgp, &eval_tmp);
1385 }
1386 if (irt && irt->vnis)
1387 if (is_vni_present_in_irt_vnis(irt->vnis, vpn))
1388 return 1;
1389 }
1390
1391 return 0;
128ea8ab 1392}
1393
1394/*
1395 * Install or uninstall routes of specified type that are appropriate for this
1396 * particular VNI.
1397 */
d62a17ae 1398static int install_uninstall_routes_for_vni(struct bgp *bgp,
1399 struct bgpevpn *vpn,
1400 bgp_evpn_route_type rtype,
1401 int install)
1402{
0291c246
MK
1403 afi_t afi;
1404 safi_t safi;
1405 struct bgp_node *rd_rn, *rn;
1406 struct bgp_table *table;
1407 struct bgp_info *ri;
1408 int ret;
d62a17ae 1409
1410 afi = AFI_L2VPN;
1411 safi = SAFI_EVPN;
1412
1413 /* Walk entire global routing table and evaluate routes which could be
1414 * imported into this VPN. Note that we cannot just look at the routes
1415 * for
1416 * the VNI's RD - remote routes applicable for this VNI could have any
1417 * RD.
1418 */
1419 /* EVPN routes are a 2-level table. */
1420 for (rd_rn = bgp_table_top(bgp->rib[afi][safi]); rd_rn;
1421 rd_rn = bgp_route_next(rd_rn)) {
1422 table = (struct bgp_table *)(rd_rn->info);
1423 if (!table)
1424 continue;
1425
1426 for (rn = bgp_table_top(table); rn; rn = bgp_route_next(rn)) {
1427 struct prefix_evpn *evp = (struct prefix_evpn *)&rn->p;
1428
1429 if (evp->prefix.route_type != rtype)
1430 continue;
1431
1432 for (ri = rn->info; ri; ri = ri->next) {
1433 /* Consider "valid" remote routes applicable for
1434 * this VNI. */
1435 if (!(CHECK_FLAG(ri->flags, BGP_INFO_VALID)
1436 && ri->type == ZEBRA_ROUTE_BGP
1437 && ri->sub_type == BGP_ROUTE_NORMAL))
1438 continue;
1439
1440 if (is_route_matching_for_vni(bgp, vpn, ri)) {
1441 if (install)
1442 ret = install_evpn_route_entry(
60466a63 1443 bgp, vpn, evp, ri);
d62a17ae 1444 else
1445 ret = uninstall_evpn_route_entry(
1446 bgp, vpn, evp, ri);
1447
1448 if (ret) {
1449 zlog_err(
1450 "%u: Failed to %s EVPN %s route in VNI %u",
1451 bgp->vrf_id,
1452 install ? "install"
1453 : "uninstall",
1454 rtype == BGP_EVPN_MAC_IP_ROUTE
1455 ? "MACIP"
1456 : "IMET",
1457 vpn->vni);
1458 return ret;
1459 }
1460 }
1461 }
1462 }
1463 }
1464
1465 return 0;
128ea8ab 1466}
1467
1468/*
1469 * Install any existing remote routes applicable for this VNI into its
1470 * routing table. This is invoked when a VNI becomes "live" or its Import
1471 * RT is changed.
1472 */
d62a17ae 1473static int install_routes_for_vni(struct bgp *bgp, struct bgpevpn *vpn)
128ea8ab 1474{
d62a17ae 1475 int ret;
128ea8ab 1476
d62a17ae 1477 /* Install type-3 routes followed by type-2 routes - the ones applicable
1478 * for this VNI.
1479 */
1480 ret = install_uninstall_routes_for_vni(bgp, vpn, BGP_EVPN_IMET_ROUTE,
1481 1);
1482 if (ret)
1483 return ret;
128ea8ab 1484
d62a17ae 1485 return install_uninstall_routes_for_vni(bgp, vpn, BGP_EVPN_MAC_IP_ROUTE,
1486 1);
128ea8ab 1487}
1488
90e60aa7 1489/*
1490 * Uninstall any existing remote routes for this VNI. One scenario in which
1491 * this is invoked is upon an import RT change.
1492 */
d62a17ae 1493static int uninstall_routes_for_vni(struct bgp *bgp, struct bgpevpn *vpn)
90e60aa7 1494{
d62a17ae 1495 int ret;
90e60aa7 1496
d62a17ae 1497 /* Uninstall type-2 routes followed by type-3 routes - the ones
1498 * applicable
1499 * for this VNI.
1500 */
1501 ret = install_uninstall_routes_for_vni(bgp, vpn, BGP_EVPN_MAC_IP_ROUTE,
1502 0);
1503 if (ret)
1504 return ret;
90e60aa7 1505
d62a17ae 1506 return install_uninstall_routes_for_vni(bgp, vpn, BGP_EVPN_IMET_ROUTE,
1507 0);
90e60aa7 1508}
1509
128ea8ab 1510/*
1511 * Install or uninstall route in matching VNIs (list).
1512 */
d62a17ae 1513static int install_uninstall_route_in_vnis(struct bgp *bgp, afi_t afi,
1514 safi_t safi, struct prefix_evpn *evp,
1515 struct bgp_info *ri,
1516 struct list *vnis, int install)
128ea8ab 1517{
d62a17ae 1518 struct bgpevpn *vpn;
1519 struct listnode *node, *nnode;
128ea8ab 1520
d62a17ae 1521 for (ALL_LIST_ELEMENTS(vnis, node, nnode, vpn)) {
1522 int ret;
128ea8ab 1523
d62a17ae 1524 if (!is_vni_live(vpn))
1525 continue;
128ea8ab 1526
d62a17ae 1527 if (install)
1528 ret = install_evpn_route_entry(bgp, vpn, evp, ri);
1529 else
1530 ret = uninstall_evpn_route_entry(bgp, vpn, evp, ri);
128ea8ab 1531
d62a17ae 1532 if (ret) {
1533 zlog_err("%u: Failed to %s EVPN %s route in VNI %u",
1534 bgp->vrf_id, install ? "install" : "uninstall",
1535 evp->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE
1536 ? "MACIP"
1537 : "IMET",
1538 vpn->vni);
1539 return ret;
1540 }
1541 }
128ea8ab 1542
d62a17ae 1543 return 0;
128ea8ab 1544}
1545
1546/*
1547 * Install or uninstall route for appropriate VNIs.
1548 */
d62a17ae 1549static int install_uninstall_evpn_route(struct bgp *bgp, afi_t afi, safi_t safi,
1550 struct prefix *p, struct bgp_info *ri,
1551 int import)
1552{
1553 struct prefix_evpn *evp = (struct prefix_evpn *)p;
1554 struct attr *attr = ri->attr;
1555 struct ecommunity *ecom;
1556 int i;
1557
1558 assert(attr);
1559
1560 /* Only type-2 and type-3 routes go into a L2 VNI. */
1561 if (!(evp->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE
1562 || evp->prefix.route_type == BGP_EVPN_IMET_ROUTE))
1563 return 0;
1564
1565 /* If we don't have Route Target, nothing much to do. */
1566 if (!(attr->flag & ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES)))
1567 return 0;
1568
1569 ecom = attr->ecommunity;
1570 if (!ecom || !ecom->size)
1571 return -1;
1572
1573 /* For each extended community RT, see which VNIs match and import
1574 * the route into matching VNIs.
1575 */
1576 for (i = 0; i < ecom->size; i++) {
1577 u_char *pnt;
1578 u_char type, sub_type;
1579 struct ecommunity_val *eval;
1580 struct ecommunity_val eval_tmp;
1581 struct irt_node *irt;
1582
1583 /* Only deal with RTs */
1584 pnt = (ecom->val + (i * ECOMMUNITY_SIZE));
1585 eval = (struct ecommunity_val *)(ecom->val
1586 + (i * ECOMMUNITY_SIZE));
1587 type = *pnt++;
1588 sub_type = *pnt++;
1589 if (sub_type != ECOMMUNITY_ROUTE_TARGET)
1590 continue;
1591
1592 /* Are we interested in this RT? */
1593 irt = lookup_import_rt(bgp, eval);
1594 if (irt && irt->vnis)
1595 install_uninstall_route_in_vnis(bgp, afi, safi, evp, ri,
1596 irt->vnis, import);
1597
1598 /* Also check for non-exact match. In this, we mask out the AS
1599 * and
1600 * only check on the local-admin sub-field. This is to
1601 * facilitate using
1602 * VNI as the RT for EBGP peering too.
1603 */
1604 irt = NULL;
1605 if (type == ECOMMUNITY_ENCODE_AS
1606 || type == ECOMMUNITY_ENCODE_AS4
1607 || type == ECOMMUNITY_ENCODE_IP) {
1608 memcpy(&eval_tmp, eval, ECOMMUNITY_SIZE);
1609 mask_ecom_global_admin(&eval_tmp, eval);
1610 irt = lookup_import_rt(bgp, &eval_tmp);
1611 }
1612 if (irt && irt->vnis)
1613 install_uninstall_route_in_vnis(bgp, afi, safi, evp, ri,
1614 irt->vnis, import);
1615 }
1616
1617 return 0;
128ea8ab 1618}
1619
90e60aa7 1620/*
1621 * Update and advertise local routes for a VNI. Invoked upon router-id
1622 * change. Note that the processing is done only on the global route table
1623 * using routes that already exist in the per-VNI table.
1624 */
d62a17ae 1625static int update_advertise_vni_routes(struct bgp *bgp, struct bgpevpn *vpn)
1626{
1627 struct prefix_evpn p;
1628 struct bgp_node *rn, *global_rn;
1629 struct bgp_info *ri, *global_ri;
1630 struct attr *attr;
1631 afi_t afi = AFI_L2VPN;
1632 safi_t safi = SAFI_EVPN;
1633
1634 /* Locate type-3 route for VNI in the per-VNI table and use its
1635 * attributes to create and advertise the type-3 route for this VNI
1636 * in the global table.
1637 */
1638 build_evpn_type3_prefix(&p, vpn->originator_ip);
1639 rn = bgp_node_lookup(vpn->route_table, (struct prefix *)&p);
1640 if (!rn) /* unexpected */
1641 return 0;
1642 for (ri = rn->info; ri; ri = ri->next)
1643 if (ri->peer == bgp->peer_self && ri->type == ZEBRA_ROUTE_BGP
1644 && ri->sub_type == BGP_ROUTE_STATIC)
1645 break;
1646 if (!ri) /* unexpected */
1647 return 0;
1648 attr = ri->attr;
1649
1650 global_rn = bgp_afi_node_get(bgp->rib[afi][safi], afi, safi,
1651 (struct prefix *)&p, &vpn->prd);
1a98c087
MK
1652 update_evpn_route_entry(bgp, vpn, afi, safi, global_rn, attr, 1, 0, &ri,
1653 0);
d62a17ae 1654
1655 /* Schedule for processing and unlock node. */
1656 bgp_process(bgp, global_rn, afi, safi);
1657 bgp_unlock_node(global_rn);
1658
1659 /* Now, walk this VNI's route table and use the route and its attribute
1660 * to create and schedule route in global table.
1661 */
1662 for (rn = bgp_table_top(vpn->route_table); rn;
1663 rn = bgp_route_next(rn)) {
1664 struct prefix_evpn *evp = (struct prefix_evpn *)&rn->p;
1665
1666 /* Identify MAC-IP local routes. */
1667 if (evp->prefix.route_type != BGP_EVPN_MAC_IP_ROUTE)
1668 continue;
1669
1670 for (ri = rn->info; ri; ri = ri->next)
1671 if (ri->peer == bgp->peer_self
1672 && ri->type == ZEBRA_ROUTE_BGP
1673 && ri->sub_type == BGP_ROUTE_STATIC)
1674 break;
1675 if (!ri)
1676 continue;
1677
1678 /* Create route in global routing table using this route entry's
1679 * attribute.
1680 */
1681 attr = ri->attr;
1682 global_rn = bgp_afi_node_get(bgp->rib[afi][safi], afi, safi,
1683 (struct prefix *)evp, &vpn->prd);
1684 assert(global_rn);
1685 update_evpn_route_entry(bgp, vpn, afi, safi, global_rn, attr, 1,
1a98c087 1686 0, &global_ri, 0);
d62a17ae 1687
1688 /* Schedule for processing and unlock node. */
1689 bgp_process(bgp, global_rn, afi, safi);
1690 bgp_unlock_node(global_rn);
1691 }
1692
1693 return 0;
90e60aa7 1694}
1695
1696/*
1697 * Delete (and withdraw) local routes for a VNI - only from the global
1698 * table. Invoked upon router-id change.
1699 */
d62a17ae 1700static int delete_withdraw_vni_routes(struct bgp *bgp, struct bgpevpn *vpn)
90e60aa7 1701{
d62a17ae 1702 int ret;
1703 struct prefix_evpn p;
1704 struct bgp_node *global_rn;
1705 struct bgp_info *ri;
1706 afi_t afi = AFI_L2VPN;
1707 safi_t safi = SAFI_EVPN;
90e60aa7 1708
d62a17ae 1709 /* Delete and withdraw locally learnt type-2 routes (MACIP)
1710 * for this VNI - from the global table.
1711 */
1712 ret = delete_global_type2_routes(bgp, vpn);
1713 if (ret)
1714 return ret;
90e60aa7 1715
d62a17ae 1716 /* Remove type-3 route for this VNI from global table. */
1717 build_evpn_type3_prefix(&p, vpn->originator_ip);
1718 global_rn = bgp_afi_node_lookup(bgp->rib[afi][safi], afi, safi,
1719 (struct prefix *)&p, &vpn->prd);
1720 if (global_rn) {
1721 /* Delete route entry in the global EVPN table. */
1722 delete_evpn_route_entry(bgp, vpn, afi, safi, global_rn, &ri);
90e60aa7 1723
d62a17ae 1724 /* Schedule for processing - withdraws to peers happen from
1725 * this table.
1726 */
1727 if (ri)
1728 bgp_process(bgp, global_rn, afi, safi);
1729 bgp_unlock_node(global_rn);
1730 }
90e60aa7 1731
d62a17ae 1732 return 0;
90e60aa7 1733}
1734
2d48ee25 1735/*
1736 * Handle router-id change. Update and advertise local routes corresponding
1737 * to this VNI from peers. Note that this is invoked after updating the
1738 * router-id. The routes in the per-VNI table are used to create routes in
1739 * the global table and schedule them.
1740 */
d62a17ae 1741static void update_router_id_vni(struct hash_backet *backet, struct bgp *bgp)
2d48ee25 1742{
d62a17ae 1743 struct bgpevpn *vpn;
2d48ee25 1744
d62a17ae 1745 vpn = (struct bgpevpn *)backet->data;
2d48ee25 1746
d62a17ae 1747 if (!vpn) {
1748 zlog_warn("%s: VNI hash entry for VNI not found", __FUNCTION__);
1749 return;
1750 }
2d48ee25 1751
d62a17ae 1752 /* Skip VNIs with configured RD. */
1753 if (is_rd_configured(vpn))
1754 return;
2d48ee25 1755
d62a17ae 1756 bgp_evpn_derive_auto_rd(bgp, vpn);
1757 update_advertise_vni_routes(bgp, vpn);
2d48ee25 1758}
1759
1760/*
1761 * Handle router-id change. Delete and withdraw local routes corresponding
1762 * to this VNI from peers. Note that this is invoked prior to updating
1763 * the router-id and is done only on the global route table, the routes
1764 * are needed in the per-VNI table to re-advertise with new router id.
1765 */
d62a17ae 1766static void withdraw_router_id_vni(struct hash_backet *backet, struct bgp *bgp)
2d48ee25 1767{
d62a17ae 1768 struct bgpevpn *vpn;
2d48ee25 1769
d62a17ae 1770 vpn = (struct bgpevpn *)backet->data;
2d48ee25 1771
d62a17ae 1772 if (!vpn) {
1773 zlog_warn("%s: VNI hash entry for VNI not found", __FUNCTION__);
1774 return;
1775 }
2d48ee25 1776
d62a17ae 1777 /* Skip VNIs with configured RD. */
1778 if (is_rd_configured(vpn))
1779 return;
2d48ee25 1780
d62a17ae 1781 delete_withdraw_vni_routes(bgp, vpn);
2d48ee25 1782}
1783
128ea8ab 1784/*
1785 * Process received EVPN type-2 route (advertise or withdraw).
1786 */
d62a17ae 1787static int process_type2_route(struct peer *peer, afi_t afi, safi_t safi,
1788 struct attr *attr, u_char *pfx, int psize,
1789 u_int32_t addpath_id)
1790{
1791 struct prefix_rd prd;
1792 struct prefix_evpn p;
1793 u_char ipaddr_len;
1794 u_char macaddr_len;
1795 mpls_label_t *label_pnt;
1796 int ret;
1797
1798 /* Type-2 route should be either 33, 37 or 49 bytes or an
1799 * additional 3 bytes if there is a second label (VNI):
1800 * RD (8), ESI (10), Eth Tag (4), MAC Addr Len (1),
1801 * MAC Addr (6), IP len (1), IP (0, 4 or 16),
1802 * MPLS Lbl1 (3), MPLS Lbl2 (0 or 3)
1803 */
1804 if (psize != 33 && psize != 37 && psize != 49 && psize != 36
1805 && psize != 40 && psize != 52) {
1806 zlog_err("%u:%s - Rx EVPN Type-2 NLRI with invalid length %d",
1807 peer->bgp->vrf_id, peer->host, psize);
1808 return -1;
1809 }
1810
1811 /* Make prefix_rd */
1812 prd.family = AF_UNSPEC;
1813 prd.prefixlen = 64;
1814 memcpy(&prd.val, pfx, 8);
1815 pfx += 8;
1816
1817 /* Make EVPN prefix. */
1818 memset(&p, 0, sizeof(struct prefix_evpn));
b03b8898 1819 p.family = AF_EVPN;
d62a17ae 1820 p.prefixlen = EVPN_TYPE_2_ROUTE_PREFIXLEN;
1821 p.prefix.route_type = BGP_EVPN_MAC_IP_ROUTE;
1822
1823 /* Skip over Ethernet Seg Identifier for now. */
1824 pfx += 10;
1825
1826 /* Skip over Ethernet Tag for now. */
1827 pfx += 4;
1828
1829 /* Get the MAC Addr len */
1830 macaddr_len = *pfx++;
1831
1832 /* Get the MAC Addr */
28328ea9
DS
1833 if (macaddr_len == (ETH_ALEN * 8)) {
1834 memcpy(&p.prefix.mac.octet, pfx, ETH_ALEN);
1835 pfx += ETH_ALEN;
d62a17ae 1836 } else {
1837 zlog_err(
1838 "%u:%s - Rx EVPN Type-2 NLRI with unsupported MAC address length %d",
1839 peer->bgp->vrf_id, peer->host, macaddr_len);
1840 return -1;
1841 }
1842
1843
1844 /* Get the IP. */
1845 ipaddr_len = *pfx++;
1846 if (ipaddr_len != 0 && ipaddr_len != IPV4_MAX_BITLEN
1847 && ipaddr_len != IPV6_MAX_BITLEN) {
1848 zlog_err(
1849 "%u:%s - Rx EVPN Type-2 NLRI with unsupported IP address length %d",
1850 peer->bgp->vrf_id, peer->host, ipaddr_len);
1851 return -1;
1852 }
1853
1854 if (ipaddr_len) {
1855 ipaddr_len /= 8; /* Convert to bytes. */
1856 p.prefix.ip.ipa_type = (ipaddr_len == IPV4_MAX_BYTELEN)
1857 ? IPADDR_V4
1858 : IPADDR_V6;
1859 memcpy(&p.prefix.ip.ip.addr, pfx, ipaddr_len);
1860 }
1861 pfx += ipaddr_len;
1862
1863 /* Get the VNI (in MPLS label field). */
1864 /* Note: We ignore the second VNI, if any. */
1865 label_pnt = (mpls_label_t *)pfx;
1866
1867 /* Process the route. */
1868 if (attr)
1869 ret = bgp_update(peer, (struct prefix *)&p, addpath_id, attr,
1870 afi, safi, ZEBRA_ROUTE_BGP, BGP_ROUTE_NORMAL,
1871 &prd, label_pnt, 0, NULL);
1872 else
1873 ret = bgp_withdraw(peer, (struct prefix *)&p, addpath_id, attr,
1874 afi, safi, ZEBRA_ROUTE_BGP, BGP_ROUTE_NORMAL,
1875 &prd, label_pnt, NULL);
1876 return ret;
128ea8ab 1877}
1878
1879/*
1880 * Process received EVPN type-3 route (advertise or withdraw).
1881 */
d62a17ae 1882static int process_type3_route(struct peer *peer, afi_t afi, safi_t safi,
1883 struct attr *attr, u_char *pfx, int psize,
1884 u_int32_t addpath_id)
1885{
1886 struct prefix_rd prd;
1887 struct prefix_evpn p;
1888 u_char ipaddr_len;
1889 int ret;
1890
1891 /* Type-3 route should be either 17 or 29 bytes: RD (8), Eth Tag (4),
1892 * IP len (1) and IP (4 or 16).
1893 */
1894 if (psize != 17 && psize != 29) {
1895 zlog_err("%u:%s - Rx EVPN Type-3 NLRI with invalid length %d",
1896 peer->bgp->vrf_id, peer->host, psize);
1897 return -1;
1898 }
1899
1900 /* Make prefix_rd */
1901 prd.family = AF_UNSPEC;
1902 prd.prefixlen = 64;
1903 memcpy(&prd.val, pfx, 8);
1904 pfx += 8;
1905
1906 /* Make EVPN prefix. */
1907 memset(&p, 0, sizeof(struct prefix_evpn));
b03b8898 1908 p.family = AF_EVPN;
d62a17ae 1909 p.prefixlen = EVPN_TYPE_3_ROUTE_PREFIXLEN;
1910 p.prefix.route_type = BGP_EVPN_IMET_ROUTE;
1911
1912 /* Skip over Ethernet Tag for now. */
1913 pfx += 4;
1914
1915 /* Get the IP. */
1916 ipaddr_len = *pfx++;
1917 if (ipaddr_len == IPV4_MAX_BITLEN) {
1918 p.prefix.ip.ipa_type = IPADDR_V4;
1919 memcpy(&p.prefix.ip.ip.addr, pfx, IPV4_MAX_BYTELEN);
1920 } else {
1921 zlog_err(
1922 "%u:%s - Rx EVPN Type-3 NLRI with unsupported IP address length %d",
1923 peer->bgp->vrf_id, peer->host, ipaddr_len);
1924 return -1;
1925 }
1926
1927 /* Process the route. */
1928 if (attr)
1929 ret = bgp_update(peer, (struct prefix *)&p, addpath_id, attr,
1930 afi, safi, ZEBRA_ROUTE_BGP, BGP_ROUTE_NORMAL,
1931 &prd, NULL, 0, NULL);
1932 else
1933 ret = bgp_withdraw(peer, (struct prefix *)&p, addpath_id, attr,
1934 afi, safi, ZEBRA_ROUTE_BGP, BGP_ROUTE_NORMAL,
1935 &prd, NULL, NULL);
1936 return ret;
128ea8ab 1937}
1938
1939/*
1940 * Process received EVPN type-5 route (advertise or withdraw).
1941 */
d62a17ae 1942static int process_type5_route(struct peer *peer, afi_t afi, safi_t safi,
1943 struct attr *attr, u_char *pfx, int psize,
1944 u_int32_t addpath_id, int withdraw)
1945{
1946 struct prefix_rd prd;
1947 struct prefix_evpn p;
1948 struct bgp_route_evpn evpn;
1949 u_char ippfx_len;
1950 u_int32_t eth_tag;
1951 mpls_label_t *label_pnt;
1952 int ret;
1953
1954 /* Type-5 route should be 34 or 58 bytes:
1955 * RD (8), ESI (10), Eth Tag (4), IP len (1), IP (4 or 16),
1956 * GW (4 or 16) and VNI (3).
1957 * Note that the IP and GW should both be IPv4 or both IPv6.
1958 */
1959 if (psize != 34 && psize != 58) {
1960 zlog_err("%u:%s - Rx EVPN Type-5 NLRI with invalid length %d",
1961 peer->bgp->vrf_id, peer->host, psize);
1962 return -1;
1963 }
1964
1965 /* Make prefix_rd */
1966 prd.family = AF_UNSPEC;
1967 prd.prefixlen = 64;
1968 memcpy(&prd.val, pfx, 8);
1969 pfx += 8;
1970
1971 /* Make EVPN prefix. */
1972 memset(&p, 0, sizeof(struct prefix_evpn));
b03b8898 1973 p.family = AF_EVPN;
d62a17ae 1974 p.prefix.route_type = BGP_EVPN_IP_PREFIX_ROUTE;
1975
1976 /* Additional information outside of prefix - ESI and GW IP */
1977 memset(&evpn, 0, sizeof(evpn));
1978
1979 /* Fetch ESI */
1980 memcpy(&evpn.eth_s_id.val, pfx, 10);
1981 pfx += 10;
1982
1983 /* Fetch Ethernet Tag. */
1984 memcpy(&eth_tag, pfx, 4);
1985 p.prefix.eth_tag = ntohl(eth_tag);
1986 pfx += 4;
1987
1988 /* Fetch IP prefix length. */
1989 ippfx_len = *pfx++;
1990 if (ippfx_len > IPV6_MAX_BITLEN) {
1991 zlog_err(
1992 "%u:%s - Rx EVPN Type-5 NLRI with invalid IP Prefix length %d",
1993 peer->bgp->vrf_id, peer->host, ippfx_len);
1994 return -1;
1995 }
1996 p.prefix.ip_prefix_length = ippfx_len;
1997
1998 /* Determine IPv4 or IPv6 prefix */
1999 /* Since the address and GW are from the same family, this just becomes
2000 * a simple check on the total size.
2001 */
2002 if (psize == 34) {
2003 SET_IPADDR_V4(&p.prefix.ip);
2004 memcpy(&p.prefix.ip.ipaddr_v4, pfx, 4);
2005 pfx += 4;
2006 memcpy(&evpn.gw_ip.ipv4, pfx, 4);
2007 pfx += 4;
2008 p.prefixlen = PREFIX_LEN_ROUTE_TYPE_5_IPV4;
2009 } else {
2010 SET_IPADDR_V6(&p.prefix.ip);
2011 memcpy(&p.prefix.ip.ipaddr_v6, pfx, 16);
2012 pfx += 16;
2013 memcpy(&evpn.gw_ip.ipv6, pfx, 16);
2014 pfx += 16;
2015 p.prefixlen = PREFIX_LEN_ROUTE_TYPE_5_IPV6;
2016 }
2017
2018 label_pnt = (mpls_label_t *)pfx;
2019
2020 /* Process the route. */
2021 if (!withdraw)
2022 ret = bgp_update(peer, (struct prefix *)&p, addpath_id, attr,
2023 afi, safi, ZEBRA_ROUTE_BGP, BGP_ROUTE_NORMAL,
2024 &prd, label_pnt, 0, &evpn);
2025 else
2026 ret = bgp_withdraw(peer, (struct prefix *)&p, addpath_id, attr,
2027 afi, safi, ZEBRA_ROUTE_BGP, BGP_ROUTE_NORMAL,
2028 &prd, label_pnt, &evpn);
2029
2030 return ret;
2031}
2032
2033static void evpn_mpattr_encode_type5(struct stream *s, struct prefix *p,
2034 struct prefix_rd *prd, mpls_label_t *label,
2035 struct attr *attr)
2036{
2037 int len;
2038 char temp[16];
2039 struct evpn_addr *p_evpn_p;
2040
2041 memset(&temp, 0, 16);
b03b8898 2042 if (p->family != AF_EVPN)
d62a17ae 2043 return;
2044 p_evpn_p = &(p->u.prefix_evpn);
2045
2046 if (IS_IPADDR_V4(&p_evpn_p->ip))
2047 len = 8; /* ipv4 */
2048 else
2049 len = 32; /* ipv6 */
d62a17ae 2050 /* Prefix contains RD, ESI, EthTag, IP length, IP, GWIP and VNI */
2051 stream_putc(s, 8 + 10 + 4 + 1 + len + 3);
2052 stream_put(s, prd->val, 8);
0af35d90 2053 if (attr)
d62a17ae 2054 stream_put(s, &(attr->evpn_overlay.eth_s_id), 10);
2055 else
2056 stream_put(s, &temp, 10);
2057 stream_putl(s, p_evpn_p->eth_tag);
2058 stream_putc(s, p_evpn_p->ip_prefix_length);
2059 if (IS_IPADDR_V4(&p_evpn_p->ip))
2060 stream_put_ipv4(s, p_evpn_p->ip.ipaddr_v4.s_addr);
2061 else
2062 stream_put(s, &p_evpn_p->ip.ipaddr_v6, 16);
0af35d90 2063 if (attr) {
d62a17ae 2064 if (IS_IPADDR_V4(&p_evpn_p->ip))
2065 stream_put_ipv4(s,
2066 attr->evpn_overlay.gw_ip.ipv4.s_addr);
2067 else
2068 stream_put(s, &(attr->evpn_overlay.gw_ip.ipv6), 16);
2069 } else {
2070 if (IS_IPADDR_V4(&p_evpn_p->ip))
2071 stream_put_ipv4(s, 0);
2072 else
2073 stream_put(s, &temp, 16);
2074 }
2075
2076 if (label)
2077 stream_put(s, label, 3);
2078 else
2079 stream_put3(s, 0);
128ea8ab 2080}
2081
2082/*
2083 * Cleanup specific VNI upon EVPN (advertise-all-vni) being disabled.
2084 */
d62a17ae 2085static void cleanup_vni_on_disable(struct hash_backet *backet, struct bgp *bgp)
128ea8ab 2086{
d62a17ae 2087 struct bgpevpn *vpn = (struct bgpevpn *)backet->data;
128ea8ab 2088
d62a17ae 2089 /* Remove EVPN routes and schedule for processing. */
2090 delete_routes_for_vni(bgp, vpn);
128ea8ab 2091
d62a17ae 2092 /* Clear "live" flag and see if hash needs to be freed. */
2093 UNSET_FLAG(vpn->flags, VNI_FLAG_LIVE);
2094 if (!is_vni_configured(vpn))
2095 bgp_evpn_free(bgp, vpn);
128ea8ab 2096}
2097
2098/*
2099 * Free a VNI entry; iterator function called during cleanup.
2100 */
d62a17ae 2101static void free_vni_entry(struct hash_backet *backet, struct bgp *bgp)
128ea8ab 2102{
d62a17ae 2103 struct bgpevpn *vpn;
128ea8ab 2104
d62a17ae 2105 vpn = (struct bgpevpn *)backet->data;
2106 delete_all_vni_routes(bgp, vpn);
2107 bgp_evpn_free(bgp, vpn);
128ea8ab 2108}
2109
2110
2111/*
2112 * Public functions.
2113 */
2114
2d48ee25 2115/*
2116 * Handle change to BGP router id. This is invoked twice by the change
2117 * handler, first before the router id has been changed and then after
2118 * the router id has been changed. The first invocation will result in
2119 * local routes for all VNIs being deleted and withdrawn and the next
2120 * will result in the routes being re-advertised.
2121 */
d62a17ae 2122void bgp_evpn_handle_router_id_update(struct bgp *bgp, int withdraw)
2d48ee25 2123{
d62a17ae 2124 if (withdraw)
2125 hash_iterate(bgp->vnihash,
2126 (void (*)(struct hash_backet *,
2127 void *))withdraw_router_id_vni,
2128 bgp);
2129 else
2130 hash_iterate(bgp->vnihash,
2131 (void (*)(struct hash_backet *,
2132 void *))update_router_id_vni,
2133 bgp);
2d48ee25 2134}
2135
90e60aa7 2136/*
2137 * Handle change to export RT - update and advertise local routes.
2138 */
d62a17ae 2139int bgp_evpn_handle_export_rt_change(struct bgp *bgp, struct bgpevpn *vpn)
90e60aa7 2140{
d62a17ae 2141 return update_routes_for_vni(bgp, vpn);
90e60aa7 2142}
2143
2144/*
2145 * Handle change to RD. This is invoked twice by the change handler,
2146 * first before the RD has been changed and then after the RD has
2147 * been changed. The first invocation will result in local routes
2148 * of this VNI being deleted and withdrawn and the next will result
2149 * in the routes being re-advertised.
2150 */
d62a17ae 2151void bgp_evpn_handle_rd_change(struct bgp *bgp, struct bgpevpn *vpn,
2152 int withdraw)
90e60aa7 2153{
d62a17ae 2154 if (withdraw)
2155 delete_withdraw_vni_routes(bgp, vpn);
2156 else
2157 update_advertise_vni_routes(bgp, vpn);
90e60aa7 2158}
2159
2160/*
2161 * Install routes for this VNI. Invoked upon change to Import RT.
2162 */
d62a17ae 2163int bgp_evpn_install_routes(struct bgp *bgp, struct bgpevpn *vpn)
90e60aa7 2164{
d62a17ae 2165 return install_routes_for_vni(bgp, vpn);
90e60aa7 2166}
2167
2168/*
2169 * Uninstall all routes installed for this VNI. Invoked upon change
2170 * to Import RT.
2171 */
d62a17ae 2172int bgp_evpn_uninstall_routes(struct bgp *bgp, struct bgpevpn *vpn)
90e60aa7 2173{
d62a17ae 2174 return uninstall_routes_for_vni(bgp, vpn);
90e60aa7 2175}
2176
b16031a2 2177/*
2178 * Function to display "tag" in route as a VNI.
2179 */
d62a17ae 2180char *bgp_evpn_label2str(mpls_label_t *label, char *buf, int len)
b16031a2 2181{
d62a17ae 2182 vni_t vni;
b16031a2 2183
d62a17ae 2184 vni = label2vni(label);
2185 snprintf(buf, len, "%u", vni);
2186 return buf;
b16031a2 2187}
2188
9c92b5f7
MK
2189/*
2190 * Function to convert evpn route to json format.
2191 * NOTE: We don't use prefix2str as the output here is a bit different.
2192 */
57f7feb6 2193void bgp_evpn_route2json(struct prefix_evpn *p, json_object *json)
9c92b5f7 2194{
b682f6de 2195 char buf1[ETHER_ADDR_STRLEN];
2196 char buf2[PREFIX2STR_BUFFER];
9c92b5f7 2197
b682f6de 2198 if (!json)
2199 return;
9c92b5f7 2200
dff8f48d 2201 if (p->prefix.route_type == BGP_EVPN_IMET_ROUTE) {
b682f6de 2202 json_object_int_add(json, "routeType", p->prefix.route_type);
2203 json_object_int_add(json, "ethTag", 0);
57f7feb6
MK
2204 json_object_int_add(json, "ipLen",
2205 IS_EVPN_PREFIX_IPADDR_V4(p)
2206 ? IPV4_MAX_BITLEN
2207 : IPV6_MAX_BITLEN);
b682f6de 2208 json_object_string_add(json, "ip",
57f7feb6
MK
2209 inet_ntoa(p->prefix.ip.ipaddr_v4));
2210 } else if (p->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE) {
dff8f48d 2211 if (IS_EVPN_PREFIX_IPADDR_NONE(p)) {
57f7feb6
MK
2212 json_object_int_add(json, "routeType",
2213 p->prefix.route_type);
2214 json_object_int_add(
2215 json, "esi",
2216 0); /* TODO: we don't support esi yet */
2217 json_object_int_add(json, "ethTag", 0);
2218 json_object_int_add(json, "macLen", 8 * ETH_ALEN);
2219 json_object_string_add(json, "mac",
2220 prefix_mac2str(&p->prefix.mac,
2221 buf1,
2222 sizeof(buf1)));
dff8f48d
MK
2223 } else {
2224 u_char family;
2225
57f7feb6
MK
2226 family = IS_EVPN_PREFIX_IPADDR_V4(p) ? AF_INET
2227 : AF_INET6;
dff8f48d 2228
b682f6de 2229 json_object_int_add(json, "routeType",
57f7feb6
MK
2230 p->prefix.route_type);
2231 json_object_int_add(
2232 json, "esi",
2233 0); /* TODO: we don't support esi yet */
b682f6de 2234 json_object_int_add(json, "ethTag", 0);
57f7feb6 2235 json_object_int_add(json, "macLen", 8 * ETH_ALEN);
b682f6de 2236 json_object_string_add(json, "mac",
57f7feb6
MK
2237 prefix_mac2str(&p->prefix.mac,
2238 buf1,
2239 sizeof(buf1)));
b682f6de 2240 json_object_int_add(json, "ipLen",
57f7feb6
MK
2241 IS_EVPN_PREFIX_IPADDR_V4(p)
2242 ? IPV4_MAX_BITLEN
2243 : IPV6_MAX_BITLEN);
2244 json_object_string_add(
2245 json, "ip",
2246 inet_ntop(family, &p->prefix.ip.ip.addr, buf2,
2247 PREFIX2STR_BUFFER));
dff8f48d
MK
2248 }
2249 } else {
2250 /* Currently, this is to cater to other AF_ETHERNET code. */
2251 }
9c92b5f7
MK
2252}
2253
520d5d76 2254/*
2255 * Function to convert evpn route to string.
2256 * NOTE: We don't use prefix2str as the output here is a bit different.
2257 */
d62a17ae 2258char *bgp_evpn_route2str(struct prefix_evpn *p, char *buf, int len)
2259{
2260 char buf1[ETHER_ADDR_STRLEN];
2261 char buf2[PREFIX2STR_BUFFER];
2262
2263 if (p->prefix.route_type == BGP_EVPN_IMET_ROUTE) {
2264 snprintf(buf, len, "[%d]:[0]:[%d]:[%s]", p->prefix.route_type,
2265 IS_EVPN_PREFIX_IPADDR_V4(p) ? IPV4_MAX_BITLEN
2266 : IPV6_MAX_BITLEN,
2267 inet_ntoa(p->prefix.ip.ipaddr_v4));
2268 } else if (p->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE) {
2269 if (IS_EVPN_PREFIX_IPADDR_NONE(p))
2270 snprintf(buf, len, "[%d]:[0]:[0]:[%d]:[%s]",
28328ea9 2271 p->prefix.route_type, 8 * ETH_ALEN,
d62a17ae 2272 prefix_mac2str(&p->prefix.mac, buf1,
2273 sizeof(buf1)));
2274 else {
2275 u_char family;
2276
2277 family = IS_EVPN_PREFIX_IPADDR_V4(p) ? AF_INET
2278 : AF_INET6;
2279 snprintf(buf, len, "[%d]:[0]:[0]:[%d]:[%s]:[%d]:[%s]",
28328ea9 2280 p->prefix.route_type, 8 * ETH_ALEN,
d62a17ae 2281 prefix_mac2str(&p->prefix.mac, buf1,
2282 sizeof(buf1)),
2283 family == AF_INET ? IPV4_MAX_BITLEN
2284 : IPV6_MAX_BITLEN,
2285 inet_ntop(family, &p->prefix.ip.ip.addr, buf2,
2286 PREFIX2STR_BUFFER));
2287 }
2288 } else {
b03b8898 2289 /* For EVPN route types not supported yet. */
f9aa3e55
QY
2290 snprintf(buf, len, "(unsupported route type %d)",
2291 p->prefix.route_type);
d62a17ae 2292 }
2293
2294 return (buf);
520d5d76 2295}
2296
128ea8ab 2297/*
2298 * Encode EVPN prefix in Update (MP_REACH)
2299 */
d62a17ae 2300void bgp_evpn_encode_prefix(struct stream *s, struct prefix *p,
2301 struct prefix_rd *prd, mpls_label_t *label,
2302 struct attr *attr, int addpath_encode,
2303 u_int32_t addpath_tx_id)
2304{
2305 struct prefix_evpn *evp = (struct prefix_evpn *)p;
2306 int ipa_len = 0;
2307
2308 if (addpath_encode)
2309 stream_putl(s, addpath_tx_id);
2310
2311 /* Route type */
2312 stream_putc(s, evp->prefix.route_type);
2313
2314 switch (evp->prefix.route_type) {
2315 case BGP_EVPN_MAC_IP_ROUTE:
2316 if (IS_EVPN_PREFIX_IPADDR_V4(evp))
2317 ipa_len = IPV4_MAX_BYTELEN;
2318 else if (IS_EVPN_PREFIX_IPADDR_V6(evp))
2319 ipa_len = IPV6_MAX_BYTELEN;
2320 stream_putc(s, 33 + ipa_len); // 1 VNI
2321 stream_put(s, prd->val, 8); /* RD */
2322 stream_put(s, 0, 10); /* ESI */
2323 stream_putl(s, 0); /* Ethernet Tag ID */
28328ea9 2324 stream_putc(s, 8 * ETH_ALEN); /* Mac Addr Len - bits */
d62a17ae 2325 stream_put(s, evp->prefix.mac.octet, 6); /* Mac Addr */
2326 stream_putc(s, 8 * ipa_len); /* IP address Length */
2327 if (ipa_len)
2328 stream_put(s, &evp->prefix.ip.ip.addr,
2329 ipa_len); /* IP */
2330 stream_put(s, label,
2331 BGP_LABEL_BYTES); /* VNI is contained in 'tag' */
2332 break;
2333
2334 case BGP_EVPN_IMET_ROUTE:
2335 stream_putc(s, 17); // TODO: length - assumes IPv4 address
2336 stream_put(s, prd->val, 8); /* RD */
2337 stream_putl(s, 0); /* Ethernet Tag ID */
2338 stream_putc(s, IPV4_MAX_BITLEN); /* IP address Length - bits */
2339 /* Originating Router's IP Addr */
2340 stream_put_in_addr(s, &evp->prefix.ip.ipaddr_v4);
2341 break;
2342
2343 case BGP_EVPN_IP_PREFIX_ROUTE:
2344 /* TODO: AddPath support. */
2345 evpn_mpattr_encode_type5(s, p, prd, label, attr);
2346 break;
2347
2348 default:
2349 break;
2350 }
2351}
2352
2353int bgp_nlri_parse_evpn(struct peer *peer, struct attr *attr,
2354 struct bgp_nlri *packet, int withdraw)
2355{
2356 u_char *pnt;
2357 u_char *lim;
2358 afi_t afi;
2359 safi_t safi;
2360 u_int32_t addpath_id;
2361 int addpath_encoded;
2362 int psize = 0;
2363 u_char rtype;
2364 u_char rlen;
2365 struct prefix p;
2366
2367 /* Check peer status. */
2368 if (peer->status != Established) {
2369 zlog_err("%u:%s - EVPN update received in state %d",
2370 peer->bgp->vrf_id, peer->host, peer->status);
2371 return -1;
2372 }
2373
2374 /* Start processing the NLRI - there may be multiple in the MP_REACH */
2375 pnt = packet->nlri;
2376 lim = pnt + packet->length;
2377 afi = packet->afi;
2378 safi = packet->safi;
2379 addpath_id = 0;
2380
2381 addpath_encoded =
2382 (CHECK_FLAG(peer->af_cap[afi][safi], PEER_CAP_ADDPATH_AF_RX_ADV)
2383 && CHECK_FLAG(peer->af_cap[afi][safi],
2384 PEER_CAP_ADDPATH_AF_TX_RCV));
2385
2386 for (; pnt < lim; pnt += psize) {
2387 /* Clear prefix structure. */
2388 memset(&p, 0, sizeof(struct prefix));
2389
2390 /* Deal with path-id if AddPath is supported. */
2391 if (addpath_encoded) {
2392 /* When packet overflow occurs return immediately. */
2393 if (pnt + BGP_ADDPATH_ID_LEN > lim)
2394 return -1;
2395
2396 addpath_id = ntohl(*((uint32_t *)pnt));
2397 pnt += BGP_ADDPATH_ID_LEN;
2398 }
2399
2400 /* All EVPN NLRI types start with type and length. */
2401 if (pnt + 2 > lim)
2402 return -1;
2403
2404 rtype = *pnt++;
2405 psize = rlen = *pnt++;
2406
2407 /* When packet overflow occur return immediately. */
2408 if (pnt + psize > lim)
2409 return -1;
2410
2411 switch (rtype) {
2412 case BGP_EVPN_MAC_IP_ROUTE:
2413 if (process_type2_route(peer, afi, safi,
2414 withdraw ? NULL : attr, pnt,
2415 psize, addpath_id)) {
2416 zlog_err(
2417 "%u:%s - Error in processing EVPN type-2 NLRI size %d",
2418 peer->bgp->vrf_id, peer->host, psize);
2419 return -1;
2420 }
2421 break;
2422
2423 case BGP_EVPN_IMET_ROUTE:
2424 if (process_type3_route(peer, afi, safi,
2425 withdraw ? NULL : attr, pnt,
2426 psize, addpath_id)) {
2427 zlog_err(
2428 "%u:%s - Error in processing EVPN type-3 NLRI size %d",
2429 peer->bgp->vrf_id, peer->host, psize);
2430 return -1;
2431 }
2432 break;
2433
2434 case BGP_EVPN_IP_PREFIX_ROUTE:
2435 if (process_type5_route(peer, afi, safi, attr, pnt,
2436 psize, addpath_id, withdraw)) {
2437 zlog_err(
2438 "%u:%s - Error in processing EVPN type-5 NLRI size %d",
2439 peer->bgp->vrf_id, peer->host, psize);
2440 return -1;
2441 }
2442 break;
2443
2444 default:
2445 break;
2446 }
2447 }
2448
2449 /* Packet length consistency check. */
2450 if (pnt != lim)
2451 return -1;
2452
2453 return 0;
128ea8ab 2454}
2455
2456
2457/*
2458 * Map the RTs (configured or automatically derived) of a VNI to the VNI.
2459 * The mapping will be used during route processing.
2460 */
d62a17ae 2461void bgp_evpn_map_vni_to_its_rts(struct bgp *bgp, struct bgpevpn *vpn)
128ea8ab 2462{
d62a17ae 2463 int i;
2464 struct ecommunity_val *eval;
2465 struct listnode *node, *nnode;
2466 struct ecommunity *ecom;
128ea8ab 2467
d62a17ae 2468 for (ALL_LIST_ELEMENTS(vpn->import_rtl, node, nnode, ecom)) {
2469 for (i = 0; i < ecom->size; i++) {
2470 eval = (struct ecommunity_val *)(ecom->val
2471 + (i
2472 * ECOMMUNITY_SIZE));
2473 map_vni_to_rt(bgp, vpn, eval);
2474 }
2475 }
128ea8ab 2476}
2477
2478/*
2479 * Unmap the RTs (configured or automatically derived) of a VNI from the VNI.
2480 */
d62a17ae 2481void bgp_evpn_unmap_vni_from_its_rts(struct bgp *bgp, struct bgpevpn *vpn)
128ea8ab 2482{
d62a17ae 2483 int i;
2484 struct ecommunity_val *eval;
2485 struct listnode *node, *nnode;
2486 struct ecommunity *ecom;
128ea8ab 2487
d62a17ae 2488 for (ALL_LIST_ELEMENTS(vpn->import_rtl, node, nnode, ecom)) {
2489 for (i = 0; i < ecom->size; i++) {
2490 struct irt_node *irt;
2491 struct ecommunity_val eval_tmp;
128ea8ab 2492
d62a17ae 2493 eval = (struct ecommunity_val *)(ecom->val
2494 + (i
2495 * ECOMMUNITY_SIZE));
2496 /* If using "automatic" RT, we only care about the
2497 * local-admin sub-field.
2498 * This is to facilitate using VNI as the RT for EBGP
2499 * peering too.
2500 */
2501 memcpy(&eval_tmp, eval, ECOMMUNITY_SIZE);
2502 if (!is_import_rt_configured(vpn))
2503 mask_ecom_global_admin(&eval_tmp, eval);
128ea8ab 2504
d62a17ae 2505 irt = lookup_import_rt(bgp, &eval_tmp);
2506 if (irt)
2507 unmap_vni_from_rt(bgp, vpn, irt);
2508 }
2509 }
128ea8ab 2510}
2511
2512/*
2513 * Derive Import RT automatically for VNI and map VNI to RT.
2514 * The mapping will be used during route processing.
2515 */
d62a17ae 2516void bgp_evpn_derive_auto_rt_import(struct bgp *bgp, struct bgpevpn *vpn)
128ea8ab 2517{
d62a17ae 2518 form_auto_rt(bgp, vpn, vpn->import_rtl);
2519 UNSET_FLAG(vpn->flags, VNI_FLAG_IMPRT_CFGD);
128ea8ab 2520
d62a17ae 2521 /* Map RT to VNI */
2522 bgp_evpn_map_vni_to_its_rts(bgp, vpn);
128ea8ab 2523}
2524
2525/*
2526 * Derive Export RT automatically for VNI.
2527 */
d62a17ae 2528void bgp_evpn_derive_auto_rt_export(struct bgp *bgp, struct bgpevpn *vpn)
128ea8ab 2529{
d62a17ae 2530 form_auto_rt(bgp, vpn, vpn->export_rtl);
2531 UNSET_FLAG(vpn->flags, VNI_FLAG_EXPRT_CFGD);
128ea8ab 2532}
2533
2534/*
2535 * Derive RD automatically for VNI using passed information - it
2536 * is of the form RouterId:unique-id-for-vni.
2537 */
d62a17ae 2538void bgp_evpn_derive_auto_rd(struct bgp *bgp, struct bgpevpn *vpn)
128ea8ab 2539{
d62a17ae 2540 char buf[100];
128ea8ab 2541
d62a17ae 2542 vpn->prd.family = AF_UNSPEC;
2543 vpn->prd.prefixlen = 64;
2544 sprintf(buf, "%s:%hu", inet_ntoa(bgp->router_id), vpn->rd_id);
cbb65f5e 2545 (void)str2prefix_rd(buf, &vpn->prd);
d62a17ae 2546 UNSET_FLAG(vpn->flags, VNI_FLAG_RD_CFGD);
128ea8ab 2547}
2548
2549/*
2550 * Lookup VNI.
2551 */
d62a17ae 2552struct bgpevpn *bgp_evpn_lookup_vni(struct bgp *bgp, vni_t vni)
128ea8ab 2553{
d62a17ae 2554 struct bgpevpn *vpn;
2555 struct bgpevpn tmp;
128ea8ab 2556
d62a17ae 2557 memset(&tmp, 0, sizeof(struct bgpevpn));
2558 tmp.vni = vni;
2559 vpn = hash_lookup(bgp->vnihash, &tmp);
2560 return vpn;
128ea8ab 2561}
2562
2563/*
2564 * Create a new vpn - invoked upon configuration or zebra notification.
2565 */
d62a17ae 2566struct bgpevpn *bgp_evpn_new(struct bgp *bgp, vni_t vni,
2567 struct in_addr originator_ip)
128ea8ab 2568{
d62a17ae 2569 struct bgpevpn *vpn;
128ea8ab 2570
d62a17ae 2571 if (!bgp)
2572 return NULL;
128ea8ab 2573
d62a17ae 2574 vpn = XCALLOC(MTYPE_BGP_EVPN, sizeof(struct bgpevpn));
2575 if (!vpn)
2576 return NULL;
128ea8ab 2577
d62a17ae 2578 /* Set values - RD and RT set to defaults. */
2579 vpn->vni = vni;
2580 vpn->originator_ip = originator_ip;
128ea8ab 2581
d62a17ae 2582 /* Initialize route-target import and export lists */
2583 vpn->import_rtl = list_new();
2584 vpn->import_rtl->cmp = (int (*)(void *, void *))evpn_route_target_cmp;
2585 vpn->export_rtl = list_new();
2586 vpn->export_rtl->cmp = (int (*)(void *, void *))evpn_route_target_cmp;
2587 bf_assign_index(bgp->rd_idspace, vpn->rd_id);
2588 derive_rd_rt_for_vni(bgp, vpn);
128ea8ab 2589
d62a17ae 2590 /* Initialize EVPN route table. */
2591 vpn->route_table = bgp_table_init(AFI_L2VPN, SAFI_EVPN);
128ea8ab 2592
d62a17ae 2593 /* Add to hash */
2594 if (!hash_get(bgp->vnihash, vpn, hash_alloc_intern)) {
2595 XFREE(MTYPE_BGP_EVPN, vpn);
2596 return NULL;
2597 }
2598 QOBJ_REG(vpn, bgpevpn);
2599 return vpn;
128ea8ab 2600}
2601
2602/*
2603 * Free a given VPN - called in multiple scenarios such as zebra
2604 * notification, configuration being deleted, advertise-all-vni disabled etc.
2605 * This just frees appropriate memory, caller should have taken other
2606 * needed actions.
2607 */
d62a17ae 2608void bgp_evpn_free(struct bgp *bgp, struct bgpevpn *vpn)
128ea8ab 2609{
d62a17ae 2610 bgp_table_unlock(vpn->route_table);
2611 bgp_evpn_unmap_vni_from_its_rts(bgp, vpn);
affe9e99
DS
2612 list_delete_and_null(&vpn->import_rtl);
2613 list_delete_and_null(&vpn->export_rtl);
d62a17ae 2614 bf_release_index(bgp->rd_idspace, vpn->rd_id);
2615 hash_release(bgp->vnihash, vpn);
2616 QOBJ_UNREG(vpn);
2617 XFREE(MTYPE_BGP_EVPN, vpn);
128ea8ab 2618}
2619
2620/*
2621 * Import route into matching VNI(s).
2622 */
d62a17ae 2623int bgp_evpn_import_route(struct bgp *bgp, afi_t afi, safi_t safi,
2624 struct prefix *p, struct bgp_info *ri)
128ea8ab 2625{
d62a17ae 2626 return install_uninstall_evpn_route(bgp, afi, safi, p, ri, 1);
128ea8ab 2627}
2628
2629/*
2630 * Unimport route from matching VNI(s).
2631 */
d62a17ae 2632int bgp_evpn_unimport_route(struct bgp *bgp, afi_t afi, safi_t safi,
2633 struct prefix *p, struct bgp_info *ri)
128ea8ab 2634{
d62a17ae 2635 return install_uninstall_evpn_route(bgp, afi, safi, p, ri, 0);
128ea8ab 2636}
2637
db0e1937
MK
2638/* filter routes which have martian next hops */
2639int bgp_filter_evpn_routes_upon_martian_nh_change(struct bgp *bgp)
2640{
0291c246
MK
2641 afi_t afi;
2642 safi_t safi;
2643 struct bgp_node *rd_rn, *rn;
2644 struct bgp_table *table;
2645 struct bgp_info *ri;
db0e1937
MK
2646
2647 afi = AFI_L2VPN;
2648 safi = SAFI_EVPN;
2649
2650 /* Walk entire global routing table and evaluate routes which could be
2651 * imported into this VPN. Note that we cannot just look at the routes
2652 * for the VNI's RD -
2653 * remote routes applicable for this VNI could have any RD.
2654 */
2655 /* EVPN routes are a 2-level table. */
2656 for (rd_rn = bgp_table_top(bgp->rib[afi][safi]); rd_rn;
2657 rd_rn = bgp_route_next(rd_rn)) {
2658 table = (struct bgp_table *)(rd_rn->info);
2659 if (!table)
2660 continue;
2661
2662 for (rn = bgp_table_top(table); rn; rn = bgp_route_next(rn)) {
2663
2664 for (ri = rn->info; ri; ri = ri->next) {
2665
2666 /* Consider "valid" remote routes applicable for
2667 * this VNI. */
2668 if (!(ri->type == ZEBRA_ROUTE_BGP
2669 && ri->sub_type == BGP_ROUTE_NORMAL))
2670 continue;
2671
60466a63 2672 if (bgp_nexthop_self(bgp, ri->attr->nexthop)) {
db0e1937
MK
2673
2674 char attr_str[BUFSIZ];
2675 char pbuf[PREFIX_STRLEN];
2676
2677 bgp_dump_attr(ri->attr, attr_str,
2678 BUFSIZ);
2679
2680 if (bgp_debug_update(ri->peer, &rn->p,
2681 NULL, 1))
2682 zlog_debug(
b682f6de 2683 "%u: prefix %s with attr %s - DENIED due to martian or self nexthop",
db0e1937
MK
2684 bgp->vrf_id,
2685 prefix2str(
60466a63 2686 &rn->p, pbuf,
db0e1937
MK
2687 sizeof(pbuf)),
2688 attr_str);
2689
2690 bgp_evpn_unimport_route(bgp, afi, safi,
2691 &rn->p, ri);
2692
60466a63
QY
2693 bgp_rib_remove(rn, ri, ri->peer, afi,
2694 safi);
db0e1937 2695 }
db0e1937
MK
2696 }
2697 }
2698 }
2699
2700 return 0;
2701}
2702
128ea8ab 2703/*
2704 * Handle del of a local MACIP.
2705 */
d62a17ae 2706int bgp_evpn_local_macip_del(struct bgp *bgp, vni_t vni, struct ethaddr *mac,
2707 struct ipaddr *ip)
128ea8ab 2708{
d62a17ae 2709 struct bgpevpn *vpn;
2710 struct prefix_evpn p;
128ea8ab 2711
d62a17ae 2712 if (!bgp->vnihash) {
2713 zlog_err("%u: VNI hash not created", bgp->vrf_id);
2714 return -1;
2715 }
128ea8ab 2716
d62a17ae 2717 /* Lookup VNI hash - should exist. */
2718 vpn = bgp_evpn_lookup_vni(bgp, vni);
2719 if (!vpn || !is_vni_live(vpn)) {
2720 zlog_warn("%u: VNI hash entry for VNI %u %s at MACIP DEL",
2721 bgp->vrf_id, vni, vpn ? "not live" : "not found");
2722 return -1;
2723 }
128ea8ab 2724
d62a17ae 2725 /* Remove EVPN type-2 route and schedule for processing. */
2726 build_evpn_type2_prefix(&p, mac, ip);
2727 delete_evpn_route(bgp, vpn, &p);
128ea8ab 2728
d62a17ae 2729 return 0;
128ea8ab 2730}
2731
2732/*
2733 * Handle add of a local MACIP.
2734 */
d62a17ae 2735int bgp_evpn_local_macip_add(struct bgp *bgp, vni_t vni, struct ethaddr *mac,
1a98c087 2736 struct ipaddr *ip, u_char flags)
128ea8ab 2737{
d62a17ae 2738 struct bgpevpn *vpn;
2739 struct prefix_evpn p;
128ea8ab 2740
d62a17ae 2741 if (!bgp->vnihash) {
2742 zlog_err("%u: VNI hash not created", bgp->vrf_id);
2743 return -1;
2744 }
128ea8ab 2745
d62a17ae 2746 /* Lookup VNI hash - should exist. */
2747 vpn = bgp_evpn_lookup_vni(bgp, vni);
2748 if (!vpn || !is_vni_live(vpn)) {
2749 zlog_warn("%u: VNI hash entry for VNI %u %s at MACIP ADD",
2750 bgp->vrf_id, vni, vpn ? "not live" : "not found");
2751 return -1;
2752 }
128ea8ab 2753
d62a17ae 2754 /* Create EVPN type-2 route and schedule for processing. */
2755 build_evpn_type2_prefix(&p, mac, ip);
1a98c087 2756 if (update_evpn_route(bgp, vpn, &p, flags)) {
d62a17ae 2757 char buf[ETHER_ADDR_STRLEN];
2758 char buf2[INET6_ADDRSTRLEN];
128ea8ab 2759
d62a17ae 2760 zlog_err(
b34fd35d 2761 "%u:Failed to create Type-2 route, VNI %u %s MAC %s IP %s",
1a98c087 2762 bgp->vrf_id, vpn->vni,
b34fd35d 2763 CHECK_FLAG(flags, ZEBRA_MAC_TYPE_STICKY) ? "sticky gateway"
1a98c087 2764 : "",
d62a17ae 2765 prefix_mac2str(mac, buf, sizeof(buf)),
2766 ipaddr2str(ip, buf2, sizeof(buf2)));
2767 return -1;
2768 }
128ea8ab 2769
d62a17ae 2770 return 0;
128ea8ab 2771}
2772
2773/*
2774 * Handle del of a local VNI.
2775 */
d62a17ae 2776int bgp_evpn_local_vni_del(struct bgp *bgp, vni_t vni)
128ea8ab 2777{
d62a17ae 2778 struct bgpevpn *vpn;
128ea8ab 2779
d62a17ae 2780 if (!bgp->vnihash) {
2781 zlog_err("%u: VNI hash not created", bgp->vrf_id);
2782 return -1;
2783 }
128ea8ab 2784
d62a17ae 2785 /* Locate VNI hash */
2786 vpn = bgp_evpn_lookup_vni(bgp, vni);
2787 if (!vpn) {
2788 zlog_warn("%u: VNI hash entry for VNI %u not found at DEL",
2789 bgp->vrf_id, vni);
2790 return 0;
2791 }
128ea8ab 2792
d62a17ae 2793 /* Remove all local EVPN routes and schedule for processing (to
2794 * withdraw from peers).
2795 */
2796 delete_routes_for_vni(bgp, vpn);
128ea8ab 2797
db0e1937
MK
2798 /*
2799 * tunnel is no longer active, del tunnel ip address from tip_hash
2800 */
2801 bgp_tip_del(bgp, &vpn->originator_ip);
2802
d62a17ae 2803 /* Clear "live" flag and see if hash needs to be freed. */
2804 UNSET_FLAG(vpn->flags, VNI_FLAG_LIVE);
2805 if (!is_vni_configured(vpn))
2806 bgp_evpn_free(bgp, vpn);
128ea8ab 2807
d62a17ae 2808 return 0;
128ea8ab 2809}
2810
2811/*
2812 * Handle add (or update) of a local VNI. The only VNI change we care
2813 * about is change to local-tunnel-ip.
2814 */
d62a17ae 2815int bgp_evpn_local_vni_add(struct bgp *bgp, vni_t vni,
2816 struct in_addr originator_ip)
2817{
2818 struct bgpevpn *vpn;
2819 struct prefix_evpn p;
2820
2821 if (!bgp->vnihash) {
2822 zlog_err("%u: VNI hash not created", bgp->vrf_id);
2823 return -1;
2824 }
2825
2826 /* Lookup VNI. If present and no change, exit. */
2827 vpn = bgp_evpn_lookup_vni(bgp, vni);
ddd16ed5 2828 if (vpn) {
2f1ac16a
MK
2829 if (is_vni_live(vpn)
2830 && IPV4_ADDR_SAME(&vpn->originator_ip, &originator_ip))
d62a17ae 2831 /* Probably some other param has changed that we don't
2832 * care about. */
2833 return 0;
2834
2835 /* Local tunnel endpoint IP address has changed */
ddd16ed5 2836 handle_tunnel_ip_change(bgp, vpn, originator_ip);
d62a17ae 2837 }
2838
2839 /* Create or update as appropriate. */
2840 if (!vpn) {
2841 vpn = bgp_evpn_new(bgp, vni, originator_ip);
2842 if (!vpn) {
2843 zlog_err(
2844 "%u: Failed to allocate VNI entry for VNI %u - at Add",
2845 bgp->vrf_id, vni);
2846 return -1;
2847 }
2848 }
2849
db0e1937 2850 /* if the VNI is live already, there is nothing more to do */
ddd16ed5
MK
2851 if (is_vni_live(vpn))
2852 return 0;
2853
d62a17ae 2854 /* Mark as "live" */
2855 SET_FLAG(vpn->flags, VNI_FLAG_LIVE);
2856
db0e1937
MK
2857 /* tunnel is now active, add tunnel-ip to db */
2858 bgp_tip_add(bgp, &originator_ip);
2859
2860 /* filter routes as nexthop database has changed */
2861 bgp_filter_evpn_routes_upon_martian_nh_change(bgp);
2862
d62a17ae 2863 /* Create EVPN type-3 route and schedule for processing. */
2864 build_evpn_type3_prefix(&p, vpn->originator_ip);
2865 if (update_evpn_route(bgp, vpn, &p, 0)) {
2866 zlog_err("%u: Type3 route creation failure for VNI %u",
2867 bgp->vrf_id, vni);
2868 return -1;
2869 }
2870
2871 /* If we have learnt and retained remote routes (VTEPs, MACs) for this
2872 * VNI,
2873 * install them.
2874 */
2875 install_routes_for_vni(bgp, vpn);
2876
d7d97010
MK
2877 /* If we are advertising gateway mac-ip
2878 It needs to be conveyed again to zebra */
2879 bgp_zebra_advertise_gw_macip(bgp, vpn->advertise_gw_macip, vpn->vni);
2880
d62a17ae 2881 return 0;
b18825eb 2882}
14c1a7bf 2883
7724c0a1 2884/*
2885 * Cleanup EVPN information on disable - Need to delete and withdraw
2886 * EVPN routes from peers.
2887 */
d62a17ae 2888void bgp_evpn_cleanup_on_disable(struct bgp *bgp)
7724c0a1 2889{
9d303b37
DL
2890 hash_iterate(bgp->vnihash, (void (*)(struct hash_backet *,
2891 void *))cleanup_vni_on_disable,
2892 bgp);
7724c0a1 2893}
2894
14c1a7bf 2895/*
2896 * Cleanup EVPN information - invoked at the time of bgpd exit or when the
2897 * BGP instance (default) is being freed.
2898 */
d62a17ae 2899void bgp_evpn_cleanup(struct bgp *bgp)
14c1a7bf 2900{
d62a17ae 2901 if (bgp->vnihash)
9d303b37
DL
2902 hash_iterate(bgp->vnihash, (void (*)(struct hash_backet *,
2903 void *))free_vni_entry,
2904 bgp);
d62a17ae 2905 if (bgp->import_rt_hash)
2906 hash_free(bgp->import_rt_hash);
2907 bgp->import_rt_hash = NULL;
2908 if (bgp->vnihash)
2909 hash_free(bgp->vnihash);
2910 bgp->vnihash = NULL;
2911 bf_free(bgp->rd_idspace);
14c1a7bf 2912}
2913
2914/*
2915 * Initialization for EVPN
2916 * Create
2917 * VNI hash table
2918 * hash for RT to VNI
2919 * unique rd id space for auto derivation of RD for VNIs
2920 */
d62a17ae 2921void bgp_evpn_init(struct bgp *bgp)
2922{
2923 bgp->vnihash =
2924 hash_create(vni_hash_key_make, vni_hash_cmp, "BGP VNI Hash");
2925 bgp->import_rt_hash =
2926 hash_create(import_rt_hash_key_make, import_rt_hash_cmp,
2927 "BGP Import RT Hash");
2928 bf_init(bgp->rd_idspace, UINT16_MAX);
2929 /*assign 0th index in the bitfield, so that we start with id 1*/
2930 bf_assign_zero_index(bgp->rd_idspace);
14c1a7bf 2931}