]>
Commit | Line | Data |
---|---|---|
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" | |
128ea8ab | 32 | #include "zclient.h" |
7ef5a232 PG |
33 | |
34 | #include "bgpd/bgp_attr_evpn.h" | |
35 | #include "bgpd/bgpd.h" | |
36 | #include "bgpd/bgp_table.h" | |
37 | #include "bgpd/bgp_route.h" | |
38 | #include "bgpd/bgp_attr.h" | |
39 | #include "bgpd/bgp_mplsvpn.h" | |
9bedbb1e | 40 | #include "bgpd/bgp_label.h" |
7ef5a232 | 41 | #include "bgpd/bgp_evpn.h" |
14c1a7bf | 42 | #include "bgpd/bgp_evpn_private.h" |
43 | #include "bgpd/bgp_ecommunity.h" | |
128ea8ab | 44 | #include "bgpd/bgp_encap_types.h" |
45 | #include "bgpd/bgp_debug.h" | |
14454c9f | 46 | #include "bgpd/bgp_errors.h" |
128ea8ab | 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 | */ | |
54 | extern struct zclient *zclient; | |
55 | ||
56 | DEFINE_QOBJ_TYPE(bgpevpn) | |
50f74cf1 | 57 | DEFINE_QOBJ_TYPE(evpnes) |
128ea8ab | 58 | |
59 | ||
60 | /* | |
61 | * Static function declarations | |
62 | */ | |
50f74cf1 | 63 | static void delete_evpn_route_entry(struct bgp *bgp, |
64 | afi_t afi, safi_t safi, | |
65 | struct bgp_node *rn, | |
d62a17ae | 66 | struct bgp_info **ri); |
67 | static int delete_all_vni_routes(struct bgp *bgp, struct bgpevpn *vpn); | |
14c1a7bf | 68 | |
69 | /* | |
70 | * Private functions. | |
71 | */ | |
72 | ||
50f74cf1 | 73 | /* compare two IPV4 VTEP IPs */ |
74 | static int evpn_vtep_ip_cmp(const void *p1, const void *p2) | |
75 | { | |
76 | const struct in_addr *ip1 = p1; | |
77 | const struct in_addr *ip2 = p2; | |
78 | ||
79 | if (!ip1 && !ip2) | |
80 | return 1; | |
81 | if (!ip1 || !ip2) | |
82 | return 0; | |
83 | return (ip1->s_addr == ip2->s_addr); | |
84 | } | |
85 | ||
86 | /* | |
87 | * Make hash key for ESI. | |
88 | */ | |
89 | static unsigned int esi_hash_keymake(void *p) | |
90 | { | |
91 | struct evpnes *pes = p; | |
92 | const void *pnt = (void *)pes->esi.val; | |
93 | ||
94 | return jhash(pnt, ESI_BYTES, 0xa5a5a55a); | |
95 | } | |
96 | ||
97 | /* | |
98 | * Compare two ESIs. | |
99 | */ | |
100 | static int esi_cmp(const void *p1, const void *p2) | |
101 | { | |
102 | const struct evpnes *pes1 = p1; | |
103 | const struct evpnes *pes2 = p2; | |
104 | ||
105 | if (pes1 == NULL && pes2 == NULL) | |
106 | return 1; | |
107 | ||
108 | if (pes1 == NULL || pes2 == NULL) | |
109 | return 0; | |
110 | ||
111 | return (memcmp(pes1->esi.val, pes2->esi.val, ESI_BYTES) == 0); | |
112 | } | |
113 | ||
14c1a7bf | 114 | /* |
115 | * Make vni hash key. | |
116 | */ | |
d62a17ae | 117 | static unsigned int vni_hash_key_make(void *p) |
14c1a7bf | 118 | { |
d62a17ae | 119 | struct bgpevpn *vpn = p; |
120 | return (jhash_1word(vpn->vni, 0)); | |
14c1a7bf | 121 | } |
122 | ||
123 | /* | |
124 | * Comparison function for vni hash | |
125 | */ | |
d62a17ae | 126 | static int vni_hash_cmp(const void *p1, const void *p2) |
14c1a7bf | 127 | { |
d62a17ae | 128 | const struct bgpevpn *vpn1 = p1; |
129 | const struct bgpevpn *vpn2 = p2; | |
14c1a7bf | 130 | |
d62a17ae | 131 | if (!vpn1 && !vpn2) |
132 | return 1; | |
133 | if (!vpn1 || !vpn2) | |
134 | return 0; | |
135 | return (vpn1->vni == vpn2->vni); | |
14c1a7bf | 136 | } |
137 | ||
10ebe1ab MK |
138 | /* |
139 | * Make vrf import route target hash key. | |
140 | */ | |
141 | static unsigned int vrf_import_rt_hash_key_make(void *p) | |
142 | { | |
143 | struct vrf_irt_node *irt = p; | |
144 | char *pnt = irt->rt.val; | |
5a1b3fb5 DS |
145 | |
146 | return jhash(pnt, 8, 0x5abc1234); | |
10ebe1ab MK |
147 | } |
148 | ||
149 | /* | |
150 | * Comparison function for vrf import rt hash | |
151 | */ | |
152 | static int vrf_import_rt_hash_cmp(const void *p1, const void *p2) | |
153 | { | |
154 | const struct vrf_irt_node *irt1 = p1; | |
155 | const struct vrf_irt_node *irt2 = p2; | |
156 | ||
157 | if (irt1 == NULL && irt2 == NULL) | |
158 | return 1; | |
159 | ||
160 | if (irt1 == NULL || irt2 == NULL) | |
161 | return 0; | |
162 | ||
163 | return (memcmp(irt1->rt.val, irt2->rt.val, ECOMMUNITY_SIZE) == 0); | |
164 | } | |
165 | ||
166 | /* | |
167 | * Create a new vrf import_rt in default instance | |
168 | */ | |
169 | static struct vrf_irt_node *vrf_import_rt_new(struct ecommunity_val *rt) | |
170 | { | |
171 | struct bgp *bgp_def = NULL; | |
172 | struct vrf_irt_node *irt; | |
173 | ||
174 | bgp_def = bgp_get_default(); | |
175 | if (!bgp_def) { | |
af4c2728 | 176 | flog_err(BGP_ERR_NO_DFLT, |
14454c9f | 177 | "vrf import rt new - def instance not created yet"); |
10ebe1ab MK |
178 | return NULL; |
179 | } | |
180 | ||
181 | irt = XCALLOC(MTYPE_BGP_EVPN_VRF_IMPORT_RT, | |
182 | sizeof(struct vrf_irt_node)); | |
183 | if (!irt) | |
184 | return NULL; | |
185 | ||
186 | irt->rt = *rt; | |
187 | irt->vrfs = list_new(); | |
188 | ||
189 | /* Add to hash */ | |
190 | if (!hash_get(bgp_def->vrf_import_rt_hash, irt, hash_alloc_intern)) { | |
191 | XFREE(MTYPE_BGP_EVPN_VRF_IMPORT_RT, irt); | |
192 | return NULL; | |
193 | } | |
194 | ||
195 | return irt; | |
196 | } | |
197 | ||
198 | /* | |
199 | * Free the vrf import rt node | |
200 | */ | |
201 | static void vrf_import_rt_free(struct vrf_irt_node *irt) | |
202 | { | |
203 | struct bgp *bgp_def = NULL; | |
204 | ||
205 | bgp_def = bgp_get_default(); | |
206 | if (!bgp_def) { | |
af4c2728 | 207 | flog_err(BGP_ERR_NO_DFLT, |
14454c9f | 208 | "vrf import rt free - def instance not created yet"); |
10ebe1ab MK |
209 | return; |
210 | } | |
211 | ||
212 | hash_release(bgp_def->vrf_import_rt_hash, irt); | |
5d9cbca2 | 213 | list_delete_and_null(&irt->vrfs); |
10ebe1ab MK |
214 | XFREE(MTYPE_BGP_EVPN_VRF_IMPORT_RT, irt); |
215 | } | |
216 | ||
217 | /* | |
218 | * Function to lookup Import RT node - used to map a RT to set of | |
219 | * VNIs importing routes with that RT. | |
220 | */ | |
221 | static struct vrf_irt_node *lookup_vrf_import_rt(struct ecommunity_val *rt) | |
222 | { | |
223 | struct bgp *bgp_def = NULL; | |
224 | struct vrf_irt_node *irt; | |
225 | struct vrf_irt_node tmp; | |
226 | ||
227 | bgp_def = bgp_get_default(); | |
228 | if (!bgp_def) { | |
af4c2728 | 229 | flog_err( |
14454c9f DS |
230 | BGP_ERR_NO_DFLT, |
231 | "vrf import rt lookup - def instance not created yet"); | |
10ebe1ab MK |
232 | return NULL; |
233 | } | |
234 | ||
235 | memset(&tmp, 0, sizeof(struct vrf_irt_node)); | |
236 | memcpy(&tmp.rt, rt, ECOMMUNITY_SIZE); | |
237 | irt = hash_lookup(bgp_def->vrf_import_rt_hash, &tmp); | |
238 | return irt; | |
239 | } | |
240 | ||
241 | /* | |
242 | * Is specified VRF present on the RT's list of "importing" VRFs? | |
243 | */ | |
996c9314 | 244 | static int is_vrf_present_in_irt_vrfs(struct list *vrfs, struct bgp *bgp_vrf) |
10ebe1ab MK |
245 | { |
246 | struct listnode *node = NULL, *nnode = NULL; | |
247 | struct bgp *tmp_bgp_vrf = NULL; | |
248 | ||
249 | for (ALL_LIST_ELEMENTS(vrfs, node, nnode, tmp_bgp_vrf)) { | |
250 | if (tmp_bgp_vrf == bgp_vrf) | |
251 | return 1; | |
252 | } | |
253 | return 0; | |
254 | } | |
255 | ||
14c1a7bf | 256 | /* |
257 | * Make import route target hash key. | |
258 | */ | |
d62a17ae | 259 | static unsigned int import_rt_hash_key_make(void *p) |
14c1a7bf | 260 | { |
d62a17ae | 261 | struct irt_node *irt = p; |
262 | char *pnt = irt->rt.val; | |
5a1b3fb5 DS |
263 | |
264 | return jhash(pnt, 8, 0xdeadbeef); | |
14c1a7bf | 265 | } |
266 | ||
267 | /* | |
268 | * Comparison function for import rt hash | |
269 | */ | |
d62a17ae | 270 | static int import_rt_hash_cmp(const void *p1, const void *p2) |
14c1a7bf | 271 | { |
d62a17ae | 272 | const struct irt_node *irt1 = p1; |
273 | const struct irt_node *irt2 = p2; | |
14c1a7bf | 274 | |
d62a17ae | 275 | if (irt1 == NULL && irt2 == NULL) |
276 | return 1; | |
14c1a7bf | 277 | |
d62a17ae | 278 | if (irt1 == NULL || irt2 == NULL) |
279 | return 0; | |
14c1a7bf | 280 | |
d62a17ae | 281 | return (memcmp(irt1->rt.val, irt2->rt.val, ECOMMUNITY_SIZE) == 0); |
14c1a7bf | 282 | } |
283 | ||
7724c0a1 | 284 | /* |
128ea8ab | 285 | * Create a new import_rt |
286 | */ | |
d62a17ae | 287 | static struct irt_node *import_rt_new(struct bgp *bgp, |
288 | struct ecommunity_val *rt) | |
128ea8ab | 289 | { |
d62a17ae | 290 | struct irt_node *irt; |
128ea8ab | 291 | |
d62a17ae | 292 | if (!bgp) |
293 | return NULL; | |
128ea8ab | 294 | |
d62a17ae | 295 | irt = XCALLOC(MTYPE_BGP_EVPN_IMPORT_RT, sizeof(struct irt_node)); |
296 | if (!irt) | |
297 | return NULL; | |
128ea8ab | 298 | |
d62a17ae | 299 | irt->rt = *rt; |
300 | irt->vnis = list_new(); | |
128ea8ab | 301 | |
d62a17ae | 302 | /* Add to hash */ |
303 | if (!hash_get(bgp->import_rt_hash, irt, hash_alloc_intern)) { | |
304 | XFREE(MTYPE_BGP_EVPN_IMPORT_RT, irt); | |
305 | return NULL; | |
306 | } | |
128ea8ab | 307 | |
d62a17ae | 308 | return irt; |
128ea8ab | 309 | } |
310 | ||
311 | /* | |
312 | * Free the import rt node | |
7724c0a1 | 313 | */ |
d62a17ae | 314 | static void import_rt_free(struct bgp *bgp, struct irt_node *irt) |
7724c0a1 | 315 | { |
d62a17ae | 316 | hash_release(bgp->import_rt_hash, irt); |
b1ab0dfe | 317 | list_delete_and_null(&irt->vnis); |
d62a17ae | 318 | XFREE(MTYPE_BGP_EVPN_IMPORT_RT, irt); |
7724c0a1 | 319 | } |
320 | ||
14c1a7bf | 321 | /* |
128ea8ab | 322 | * Function to lookup Import RT node - used to map a RT to set of |
323 | * VNIs importing routes with that RT. | |
324 | */ | |
d62a17ae | 325 | static struct irt_node *lookup_import_rt(struct bgp *bgp, |
326 | struct ecommunity_val *rt) | |
128ea8ab | 327 | { |
d62a17ae | 328 | struct irt_node *irt; |
329 | struct irt_node tmp; | |
128ea8ab | 330 | |
d62a17ae | 331 | memset(&tmp, 0, sizeof(struct irt_node)); |
332 | memcpy(&tmp.rt, rt, ECOMMUNITY_SIZE); | |
333 | irt = hash_lookup(bgp->import_rt_hash, &tmp); | |
334 | return irt; | |
128ea8ab | 335 | } |
336 | ||
337 | /* | |
338 | * Is specified VNI present on the RT's list of "importing" VNIs? | |
339 | */ | |
d62a17ae | 340 | static int is_vni_present_in_irt_vnis(struct list *vnis, struct bgpevpn *vpn) |
128ea8ab | 341 | { |
d62a17ae | 342 | struct listnode *node, *nnode; |
343 | struct bgpevpn *tmp_vpn; | |
128ea8ab | 344 | |
d62a17ae | 345 | for (ALL_LIST_ELEMENTS(vnis, node, nnode, tmp_vpn)) { |
346 | if (tmp_vpn == vpn) | |
347 | return 1; | |
348 | } | |
128ea8ab | 349 | |
d62a17ae | 350 | return 0; |
128ea8ab | 351 | } |
352 | ||
353 | /* | |
354 | * Compare Route Targets. | |
355 | */ | |
d62a17ae | 356 | static int evpn_route_target_cmp(struct ecommunity *ecom1, |
357 | struct ecommunity *ecom2) | |
128ea8ab | 358 | { |
d62a17ae | 359 | if (ecom1 && !ecom2) |
360 | return -1; | |
128ea8ab | 361 | |
d62a17ae | 362 | if (!ecom1 && ecom2) |
363 | return 1; | |
128ea8ab | 364 | |
d62a17ae | 365 | if (!ecom1 && !ecom2) |
366 | return 0; | |
128ea8ab | 367 | |
d62a17ae | 368 | if (ecom1->str && !ecom2->str) |
369 | return -1; | |
128ea8ab | 370 | |
d62a17ae | 371 | if (!ecom1->str && ecom2->str) |
372 | return 1; | |
128ea8ab | 373 | |
d62a17ae | 374 | if (!ecom1->str && !ecom2->str) |
375 | return 0; | |
128ea8ab | 376 | |
d62a17ae | 377 | return strcmp(ecom1->str, ecom2->str); |
128ea8ab | 378 | } |
379 | ||
987d8198 DS |
380 | static void evpn_xxport_delete_ecomm(void *val) |
381 | { | |
382 | struct ecommunity *ecomm = val; | |
383 | ecommunity_free(&ecomm); | |
384 | } | |
385 | ||
128ea8ab | 386 | /* |
387 | * Mask off global-admin field of specified extended community (RT), | |
388 | * just retain the local-admin field. | |
389 | */ | |
d62a17ae | 390 | static inline void mask_ecom_global_admin(struct ecommunity_val *dst, |
391 | struct ecommunity_val *src) | |
128ea8ab | 392 | { |
d7c0a89a | 393 | uint8_t type; |
128ea8ab | 394 | |
d62a17ae | 395 | type = src->val[0]; |
396 | dst->val[0] = 0; | |
397 | if (type == ECOMMUNITY_ENCODE_AS) { | |
398 | dst->val[2] = dst->val[3] = 0; | |
399 | } else if (type == ECOMMUNITY_ENCODE_AS4 | |
400 | || type == ECOMMUNITY_ENCODE_IP) { | |
401 | dst->val[2] = dst->val[3] = 0; | |
402 | dst->val[4] = dst->val[5] = 0; | |
403 | } | |
128ea8ab | 404 | } |
405 | ||
10ebe1ab MK |
406 | /* |
407 | * Map one RT to specified VRF. | |
408 | * bgp_vrf = BGP vrf instance | |
409 | */ | |
996c9314 | 410 | static void map_vrf_to_rt(struct bgp *bgp_vrf, struct ecommunity_val *eval) |
10ebe1ab MK |
411 | { |
412 | struct vrf_irt_node *irt = NULL; | |
413 | struct ecommunity_val eval_tmp; | |
414 | ||
415 | /* If using "automatic" RT, | |
416 | * we only care about the local-admin sub-field. | |
417 | * This is to facilitate using L3VNI(VRF-VNI) | |
418 | * as the RT for EBGP peering too. | |
419 | */ | |
420 | memcpy(&eval_tmp, eval, ECOMMUNITY_SIZE); | |
996c9314 | 421 | if (!CHECK_FLAG(bgp_vrf->vrf_flags, BGP_VRF_IMPORT_RT_CFGD)) |
10ebe1ab MK |
422 | mask_ecom_global_admin(&eval_tmp, eval); |
423 | ||
424 | irt = lookup_vrf_import_rt(&eval_tmp); | |
e066d6d0 DS |
425 | if (irt && is_vrf_present_in_irt_vrfs(irt->vrfs, bgp_vrf)) |
426 | /* Already mapped. */ | |
427 | return; | |
10ebe1ab | 428 | |
e066d6d0 | 429 | if (!irt) |
10ebe1ab | 430 | irt = vrf_import_rt_new(&eval_tmp); |
10ebe1ab MK |
431 | |
432 | /* Add VRF to the list for this RT. */ | |
433 | listnode_add(irt->vrfs, bgp_vrf); | |
434 | } | |
435 | ||
436 | /* | |
437 | * Unmap specified VRF from specified RT. If there are no other | |
438 | * VRFs for this RT, then the RT hash is deleted. | |
439 | * bgp_vrf: BGP VRF specific instance | |
440 | */ | |
996c9314 | 441 | static void unmap_vrf_from_rt(struct bgp *bgp_vrf, struct vrf_irt_node *irt) |
10ebe1ab MK |
442 | { |
443 | /* Delete VRF from list for this RT. */ | |
444 | listnode_delete(irt->vrfs, bgp_vrf); | |
445 | if (!listnode_head(irt->vrfs)) { | |
10ebe1ab MK |
446 | vrf_import_rt_free(irt); |
447 | } | |
448 | } | |
449 | ||
128ea8ab | 450 | /* |
451 | * Map one RT to specified VNI. | |
14c1a7bf | 452 | */ |
d62a17ae | 453 | static void map_vni_to_rt(struct bgp *bgp, struct bgpevpn *vpn, |
454 | struct ecommunity_val *eval) | |
128ea8ab | 455 | { |
d62a17ae | 456 | struct irt_node *irt; |
457 | struct ecommunity_val eval_tmp; | |
128ea8ab | 458 | |
d62a17ae | 459 | /* If using "automatic" RT, we only care about the local-admin |
460 | * sub-field. | |
461 | * This is to facilitate using VNI as the RT for EBGP peering too. | |
462 | */ | |
463 | memcpy(&eval_tmp, eval, ECOMMUNITY_SIZE); | |
464 | if (!is_import_rt_configured(vpn)) | |
465 | mask_ecom_global_admin(&eval_tmp, eval); | |
128ea8ab | 466 | |
d62a17ae | 467 | irt = lookup_import_rt(bgp, &eval_tmp); |
b1ab0dfe | 468 | if (irt) |
d62a17ae | 469 | if (is_vni_present_in_irt_vnis(irt->vnis, vpn)) |
470 | /* Already mapped. */ | |
471 | return; | |
128ea8ab | 472 | |
d62a17ae | 473 | if (!irt) { |
474 | irt = import_rt_new(bgp, &eval_tmp); | |
475 | assert(irt); | |
476 | } | |
128ea8ab | 477 | |
d62a17ae | 478 | /* Add VNI to the hash list for this RT. */ |
479 | listnode_add(irt->vnis, vpn); | |
128ea8ab | 480 | } |
481 | ||
482 | /* | |
483 | * Unmap specified VNI from specified RT. If there are no other | |
484 | * VNIs for this RT, then the RT hash is deleted. | |
485 | */ | |
d62a17ae | 486 | static void unmap_vni_from_rt(struct bgp *bgp, struct bgpevpn *vpn, |
487 | struct irt_node *irt) | |
14c1a7bf | 488 | { |
d62a17ae | 489 | /* Delete VNI from hash list for this RT. */ |
490 | listnode_delete(irt->vnis, vpn); | |
491 | if (!listnode_head(irt->vnis)) { | |
d62a17ae | 492 | import_rt_free(bgp, irt); |
493 | } | |
14c1a7bf | 494 | } |
495 | ||
128ea8ab | 496 | /* |
497 | * Create RT extended community automatically from passed information: | |
498 | * of the form AS:VNI. | |
499 | * NOTE: We use only the lower 16 bits of the AS. This is sufficient as | |
500 | * the need is to get a RT value that will be unique across different | |
501 | * VNIs but the same across routers (in the same AS) for a particular | |
502 | * VNI. | |
503 | */ | |
c581d8b0 | 504 | static void form_auto_rt(struct bgp *bgp, vni_t vni, struct list *rtl) |
128ea8ab | 505 | { |
d62a17ae | 506 | struct ecommunity_val eval; |
507 | struct ecommunity *ecomadd; | |
128ea8ab | 508 | |
bf1061d8 VB |
509 | if (bgp->advertise_autort_rfc8365) |
510 | vni |= EVPN_AUTORT_VXLAN; | |
c581d8b0 | 511 | encode_route_target_as((bgp->as & 0xFFFF), vni, &eval); |
128ea8ab | 512 | |
d62a17ae | 513 | ecomadd = ecommunity_new(); |
514 | ecommunity_add_val(ecomadd, &eval); | |
515 | listnode_add_sort(rtl, ecomadd); | |
128ea8ab | 516 | } |
14c1a7bf | 517 | |
518 | /* | |
128ea8ab | 519 | * Derive RD and RT for a VNI automatically. Invoked at the time of |
520 | * creation of a VNI. | |
521 | */ | |
d62a17ae | 522 | static void derive_rd_rt_for_vni(struct bgp *bgp, struct bgpevpn *vpn) |
128ea8ab | 523 | { |
d62a17ae | 524 | bgp_evpn_derive_auto_rd(bgp, vpn); |
525 | bgp_evpn_derive_auto_rt_import(bgp, vpn); | |
526 | bgp_evpn_derive_auto_rt_export(bgp, vpn); | |
128ea8ab | 527 | } |
528 | ||
1ec31309 | 529 | /* |
530 | * Convert nexthop (remote VTEP IP) into an IPv6 address. | |
531 | */ | |
532 | static void evpn_convert_nexthop_to_ipv6(struct attr *attr) | |
533 | { | |
534 | if (BGP_ATTR_NEXTHOP_AFI_IP6(attr)) | |
535 | return; | |
536 | ipv4_to_ipv4_mapped_ipv6(&attr->mp_nexthop_global, attr->nexthop); | |
537 | attr->mp_nexthop_len = IPV6_MAX_BYTELEN; | |
538 | } | |
539 | ||
128ea8ab | 540 | /* |
541 | * Add (update) or delete MACIP from zebra. | |
14c1a7bf | 542 | */ |
d62a17ae | 543 | static int bgp_zebra_send_remote_macip(struct bgp *bgp, struct bgpevpn *vpn, |
544 | struct prefix_evpn *p, | |
545 | struct in_addr remote_vtep_ip, int add, | |
d7c0a89a | 546 | uint8_t flags) |
d62a17ae | 547 | { |
548 | struct stream *s; | |
549 | int ipa_len; | |
550 | char buf1[ETHER_ADDR_STRLEN]; | |
551 | char buf2[INET6_ADDRSTRLEN]; | |
552 | char buf3[INET6_ADDRSTRLEN]; | |
553 | ||
554 | /* Check socket. */ | |
555 | if (!zclient || zclient->sock < 0) | |
556 | return 0; | |
557 | ||
558 | /* Don't try to register if Zebra doesn't know of this instance. */ | |
559 | if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp)) | |
560 | return 0; | |
561 | ||
562 | s = zclient->obuf; | |
563 | stream_reset(s); | |
564 | ||
996c9314 LB |
565 | zclient_create_header( |
566 | s, add ? ZEBRA_REMOTE_MACIP_ADD : ZEBRA_REMOTE_MACIP_DEL, | |
567 | bgp->vrf_id); | |
d62a17ae | 568 | stream_putl(s, vpn->vni); |
3714a385 | 569 | stream_put(s, &p->prefix.macip_addr.mac.octet, ETH_ALEN); /* Mac Addr */ |
d62a17ae | 570 | /* IP address length and IP address, if any. */ |
3714a385 | 571 | if (is_evpn_prefix_ipaddr_none(p)) |
d62a17ae | 572 | stream_putl(s, 0); |
573 | else { | |
3714a385 | 574 | ipa_len = is_evpn_prefix_ipaddr_v4(p) ? IPV4_MAX_BYTELEN |
d62a17ae | 575 | : IPV6_MAX_BYTELEN; |
576 | stream_putl(s, ipa_len); | |
3714a385 | 577 | stream_put(s, &p->prefix.macip_addr.ip.ip.addr, ipa_len); |
d62a17ae | 578 | } |
579 | stream_put_in_addr(s, &remote_vtep_ip); | |
580 | ||
ead40654 | 581 | /* TX flags - MAC sticky status and/or gateway mac */ |
d62a17ae | 582 | if (add) |
ead40654 | 583 | stream_putc(s, flags); |
d62a17ae | 584 | |
585 | stream_putw_at(s, 0, stream_get_endp(s)); | |
586 | ||
587 | if (bgp_debug_zebra(NULL)) | |
996c9314 LB |
588 | zlog_debug( |
589 | "Tx %s MACIP, VNI %u MAC %s IP %s (flags: 0x%x) remote VTEP %s", | |
590 | add ? "ADD" : "DEL", vpn->vni, | |
3714a385 | 591 | prefix_mac2str(&p->prefix.macip_addr.mac, |
592 | buf1, sizeof(buf1)), | |
593 | ipaddr2str(&p->prefix.macip_addr.ip, | |
594 | buf3, sizeof(buf3)), flags, | |
996c9314 LB |
595 | inet_ntop(AF_INET, &remote_vtep_ip, buf2, |
596 | sizeof(buf2))); | |
d62a17ae | 597 | |
598 | return zclient_send_message(zclient); | |
7ef5a232 | 599 | } |
b18825eb | 600 | |
128ea8ab | 601 | /* |
602 | * Add (update) or delete remote VTEP from zebra. | |
603 | */ | |
d62a17ae | 604 | static int bgp_zebra_send_remote_vtep(struct bgp *bgp, struct bgpevpn *vpn, |
605 | struct prefix_evpn *p, int add) | |
128ea8ab | 606 | { |
d62a17ae | 607 | struct stream *s; |
128ea8ab | 608 | |
d62a17ae | 609 | /* Check socket. */ |
610 | if (!zclient || zclient->sock < 0) | |
611 | return 0; | |
128ea8ab | 612 | |
d62a17ae | 613 | /* Don't try to register if Zebra doesn't know of this instance. */ |
614 | if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp)) | |
615 | return 0; | |
128ea8ab | 616 | |
d62a17ae | 617 | s = zclient->obuf; |
618 | stream_reset(s); | |
128ea8ab | 619 | |
996c9314 LB |
620 | zclient_create_header( |
621 | s, add ? ZEBRA_REMOTE_VTEP_ADD : ZEBRA_REMOTE_VTEP_DEL, | |
622 | bgp->vrf_id); | |
d62a17ae | 623 | stream_putl(s, vpn->vni); |
3714a385 | 624 | if (is_evpn_prefix_ipaddr_v4(p)) |
625 | stream_put_in_addr(s, &p->prefix.imet_addr.ip.ipaddr_v4); | |
626 | else if (is_evpn_prefix_ipaddr_v6(p)) { | |
af4c2728 | 627 | flog_err( |
14454c9f | 628 | BGP_ERR_VTEP_INVALID, |
d62a17ae | 629 | "Bad remote IP when trying to %s remote VTEP for VNI %u", |
630 | add ? "ADD" : "DEL", vpn->vni); | |
631 | return -1; | |
632 | } | |
128ea8ab | 633 | |
d62a17ae | 634 | stream_putw_at(s, 0, stream_get_endp(s)); |
128ea8ab | 635 | |
d62a17ae | 636 | if (bgp_debug_zebra(NULL)) |
637 | zlog_debug("Tx %s Remote VTEP, VNI %u remote VTEP %s", | |
638 | add ? "ADD" : "DEL", vpn->vni, | |
3714a385 | 639 | inet_ntoa(p->prefix.imet_addr.ip.ipaddr_v4)); |
128ea8ab | 640 | |
d62a17ae | 641 | return zclient_send_message(zclient); |
128ea8ab | 642 | } |
643 | ||
50f74cf1 | 644 | /* |
645 | * Build extended community for EVPN ES (type-4) route | |
646 | */ | |
647 | static void build_evpn_type4_route_extcomm(struct evpnes *es, | |
648 | struct attr *attr) | |
649 | { | |
650 | struct ecommunity ecom_encap; | |
651 | struct ecommunity ecom_es_rt; | |
652 | struct ecommunity_val eval; | |
653 | struct ecommunity_val eval_es_rt; | |
654 | bgp_encap_types tnl_type; | |
655 | struct ethaddr mac; | |
656 | ||
657 | /* Encap */ | |
658 | tnl_type = BGP_ENCAP_TYPE_VXLAN; | |
659 | memset(&ecom_encap, 0, sizeof(ecom_encap)); | |
660 | encode_encap_extcomm(tnl_type, &eval); | |
661 | ecom_encap.size = 1; | |
662 | ecom_encap.val = (uint8_t *)eval.val; | |
663 | attr->ecommunity = ecommunity_dup(&ecom_encap); | |
664 | ||
665 | /* ES import RT */ | |
666 | memset(&mac, 0, sizeof(struct ethaddr)); | |
667 | memset(&ecom_es_rt, 0, sizeof(ecom_es_rt)); | |
668 | es_get_system_mac(&es->esi, &mac); | |
669 | encode_es_rt_extcomm(&eval_es_rt, &mac); | |
670 | ecom_es_rt.size = 1; | |
671 | ecom_es_rt.val = (uint8_t *)eval_es_rt.val; | |
672 | attr->ecommunity = | |
673 | ecommunity_merge(attr->ecommunity, &ecom_es_rt); | |
674 | ||
675 | attr->flag |= ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES); | |
676 | } | |
677 | ||
342dd0c6 | 678 | /* |
679 | * Build extended communities for EVPN prefix route. | |
680 | */ | |
681 | static void build_evpn_type5_route_extcomm(struct bgp *bgp_vrf, | |
682 | struct attr *attr) | |
683 | { | |
684 | struct ecommunity ecom_encap; | |
685 | struct ecommunity ecom_rmac; | |
686 | struct ecommunity_val eval; | |
687 | struct ecommunity_val eval_rmac; | |
688 | bgp_encap_types tnl_type; | |
689 | struct listnode *node, *nnode; | |
690 | struct ecommunity *ecom; | |
691 | struct list *vrf_export_rtl = NULL; | |
692 | ||
693 | /* Encap */ | |
694 | tnl_type = BGP_ENCAP_TYPE_VXLAN; | |
695 | memset(&ecom_encap, 0, sizeof(ecom_encap)); | |
696 | encode_encap_extcomm(tnl_type, &eval); | |
697 | ecom_encap.size = 1; | |
d7c0a89a | 698 | ecom_encap.val = (uint8_t *)eval.val; |
342dd0c6 | 699 | |
700 | /* Add Encap */ | |
701 | attr->ecommunity = ecommunity_dup(&ecom_encap); | |
702 | ||
703 | /* Add the export RTs for L3VNI/VRF */ | |
704 | vrf_export_rtl = bgp_vrf->vrf_export_rtl; | |
1525e99f DS |
705 | for (ALL_LIST_ELEMENTS(vrf_export_rtl, node, nnode, ecom)) |
706 | attr->ecommunity = | |
707 | ecommunity_merge(attr->ecommunity, ecom); | |
342dd0c6 | 708 | |
709 | /* add the router mac extended community */ | |
710 | if (!is_zero_mac(&attr->rmac)) { | |
711 | memset(&ecom_rmac, 0, sizeof(ecom_rmac)); | |
712 | encode_rmac_extcomm(&eval_rmac, &attr->rmac); | |
713 | ecom_rmac.size = 1; | |
714 | ecom_rmac.val = (uint8_t *)eval_rmac.val; | |
996c9314 LB |
715 | attr->ecommunity = |
716 | ecommunity_merge(attr->ecommunity, &ecom_rmac); | |
342dd0c6 | 717 | } |
718 | ||
719 | attr->flag |= ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES); | |
720 | } | |
721 | ||
128ea8ab | 722 | /* |
1ec31309 | 723 | * Build extended communities for EVPN route. |
724 | * This function is applicable for type-2 and type-3 routes. The layer-2 RT | |
725 | * and ENCAP extended communities are applicable for all routes. | |
726 | * The default gateway extended community and MAC mobility (sticky) extended | |
727 | * community are added as needed based on passed settings - only for type-2 | |
728 | * routes. Likewise, the layer-3 RT and Router MAC extended communities are | |
729 | * added, if present, based on passed settings - only for non-link-local | |
730 | * type-2 routes. | |
128ea8ab | 731 | */ |
7ec156a9 | 732 | static void build_evpn_route_extcomm(struct bgpevpn *vpn, struct attr *attr, |
1ec31309 | 733 | int add_l3_ecomm) |
128ea8ab | 734 | { |
d62a17ae | 735 | struct ecommunity ecom_encap; |
736 | struct ecommunity ecom_sticky; | |
ead40654 | 737 | struct ecommunity ecom_default_gw; |
bc59a672 | 738 | struct ecommunity ecom_rmac; |
68e33151 | 739 | struct ecommunity ecom_na; |
d62a17ae | 740 | struct ecommunity_val eval; |
741 | struct ecommunity_val eval_sticky; | |
ead40654 | 742 | struct ecommunity_val eval_default_gw; |
bc59a672 | 743 | struct ecommunity_val eval_rmac; |
68e33151 CS |
744 | struct ecommunity_val eval_na; |
745 | ||
d62a17ae | 746 | bgp_encap_types tnl_type; |
747 | struct listnode *node, *nnode; | |
748 | struct ecommunity *ecom; | |
d7c0a89a | 749 | uint32_t seqnum; |
7a3e76f1 | 750 | struct list *vrf_export_rtl = NULL; |
128ea8ab | 751 | |
d62a17ae | 752 | /* Encap */ |
753 | tnl_type = BGP_ENCAP_TYPE_VXLAN; | |
754 | memset(&ecom_encap, 0, sizeof(ecom_encap)); | |
755 | encode_encap_extcomm(tnl_type, &eval); | |
756 | ecom_encap.size = 1; | |
d7c0a89a | 757 | ecom_encap.val = (uint8_t *)eval.val; |
128ea8ab | 758 | |
d62a17ae | 759 | /* Add Encap */ |
760 | attr->ecommunity = ecommunity_dup(&ecom_encap); | |
128ea8ab | 761 | |
7a3e76f1 | 762 | /* Add the export RTs for L2VNI */ |
d62a17ae | 763 | for (ALL_LIST_ELEMENTS(vpn->export_rtl, node, nnode, ecom)) |
764 | attr->ecommunity = ecommunity_merge(attr->ecommunity, ecom); | |
128ea8ab | 765 | |
1ec31309 | 766 | /* Add the export RTs for L3VNI if told to - caller determines |
767 | * when this should be done. | |
523cafc4 | 768 | */ |
1ec31309 | 769 | if (add_l3_ecomm) { |
7ec156a9 MK |
770 | vrf_export_rtl = bgpevpn_get_vrf_export_rtl(vpn); |
771 | if (vrf_export_rtl && !list_isempty(vrf_export_rtl)) { | |
772 | for (ALL_LIST_ELEMENTS(vrf_export_rtl, node, nnode, | |
773 | ecom)) | |
996c9314 LB |
774 | attr->ecommunity = ecommunity_merge( |
775 | attr->ecommunity, ecom); | |
7ec156a9 | 776 | } |
f1f8b53c | 777 | } |
7a3e76f1 | 778 | |
1ec31309 | 779 | /* Add MAC mobility (sticky) if needed. */ |
d62a17ae | 780 | if (attr->sticky) { |
781 | seqnum = 0; | |
782 | memset(&ecom_sticky, 0, sizeof(ecom_sticky)); | |
783 | encode_mac_mobility_extcomm(1, seqnum, &eval_sticky); | |
784 | ecom_sticky.size = 1; | |
d7c0a89a | 785 | ecom_sticky.val = (uint8_t *)eval_sticky.val; |
d62a17ae | 786 | attr->ecommunity = |
787 | ecommunity_merge(attr->ecommunity, &ecom_sticky); | |
788 | } | |
c85c03c7 | 789 | |
1ec31309 | 790 | /* Add RMAC, if told to. */ |
791 | if (add_l3_ecomm) { | |
bc59a672 MK |
792 | memset(&ecom_rmac, 0, sizeof(ecom_rmac)); |
793 | encode_rmac_extcomm(&eval_rmac, &attr->rmac); | |
794 | ecom_rmac.size = 1; | |
795 | ecom_rmac.val = (uint8_t *)eval_rmac.val; | |
996c9314 LB |
796 | attr->ecommunity = |
797 | ecommunity_merge(attr->ecommunity, &ecom_rmac); | |
bc59a672 MK |
798 | } |
799 | ||
1ec31309 | 800 | /* Add default gateway, if needed. */ |
ead40654 MK |
801 | if (attr->default_gw) { |
802 | memset(&ecom_default_gw, 0, sizeof(ecom_default_gw)); | |
803 | encode_default_gw_extcomm(&eval_default_gw); | |
804 | ecom_default_gw.size = 1; | |
805 | ecom_default_gw.val = (uint8_t *)eval_default_gw.val; | |
996c9314 LB |
806 | attr->ecommunity = |
807 | ecommunity_merge(attr->ecommunity, &ecom_default_gw); | |
ead40654 MK |
808 | } |
809 | ||
68e33151 CS |
810 | if (attr->router_flag) { |
811 | memset(&ecom_na, 0, sizeof(ecom_na)); | |
812 | encode_na_flag_extcomm(&eval_na, attr->router_flag); | |
813 | ecom_na.size = 1; | |
814 | ecom_na.val = (uint8_t *)eval_na.val; | |
815 | attr->ecommunity = ecommunity_merge(attr->ecommunity, | |
816 | &ecom_na); | |
817 | } | |
818 | ||
d62a17ae | 819 | attr->flag |= ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES); |
128ea8ab | 820 | } |
821 | ||
822 | /* | |
823 | * Add MAC mobility extended community to attribute. | |
824 | */ | |
d7c0a89a | 825 | static void add_mac_mobility_to_attr(uint32_t seq_num, struct attr *attr) |
d62a17ae | 826 | { |
827 | struct ecommunity ecom_tmp; | |
828 | struct ecommunity_val eval; | |
d7c0a89a | 829 | uint8_t *ecom_val_ptr; |
d62a17ae | 830 | int i; |
d7c0a89a | 831 | uint8_t *pnt; |
d62a17ae | 832 | int type = 0; |
833 | int sub_type = 0; | |
834 | ||
835 | /* Build MM */ | |
836 | encode_mac_mobility_extcomm(0, seq_num, &eval); | |
837 | ||
838 | /* Find current MM ecommunity */ | |
421bb26a | 839 | ecom_val_ptr = NULL; |
d62a17ae | 840 | |
841 | if (attr->ecommunity) { | |
842 | for (i = 0; i < attr->ecommunity->size; i++) { | |
843 | pnt = attr->ecommunity->val + (i * 8); | |
844 | type = *pnt++; | |
845 | sub_type = *pnt++; | |
846 | ||
847 | if (type == ECOMMUNITY_ENCODE_EVPN | |
848 | && sub_type | |
849 | == ECOMMUNITY_EVPN_SUBTYPE_MACMOBILITY) { | |
d7c0a89a QY |
850 | ecom_val_ptr = (uint8_t *)(attr->ecommunity->val |
851 | + (i * 8)); | |
d62a17ae | 852 | break; |
853 | } | |
854 | } | |
855 | } | |
856 | ||
857 | /* Update the existing MM ecommunity */ | |
421bb26a MK |
858 | if (ecom_val_ptr) { |
859 | memcpy(ecom_val_ptr, eval.val, sizeof(char) * ECOMMUNITY_SIZE); | |
d62a17ae | 860 | } |
861 | /* Add MM to existing */ | |
862 | else { | |
863 | memset(&ecom_tmp, 0, sizeof(ecom_tmp)); | |
864 | ecom_tmp.size = 1; | |
d7c0a89a | 865 | ecom_tmp.val = (uint8_t *)eval.val; |
d62a17ae | 866 | |
f9a78910 DS |
867 | if (attr->ecommunity) |
868 | attr->ecommunity = | |
869 | ecommunity_merge(attr->ecommunity, &ecom_tmp); | |
870 | else | |
871 | attr->ecommunity = ecommunity_dup(&ecom_tmp); | |
d62a17ae | 872 | } |
128ea8ab | 873 | } |
874 | ||
875 | /* Install EVPN route into zebra. */ | |
d62a17ae | 876 | static int evpn_zebra_install(struct bgp *bgp, struct bgpevpn *vpn, |
877 | struct prefix_evpn *p, | |
d7c0a89a | 878 | struct in_addr remote_vtep_ip, uint8_t flags) |
128ea8ab | 879 | { |
d62a17ae | 880 | int ret; |
128ea8ab | 881 | |
d62a17ae | 882 | if (p->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE) |
883 | ret = bgp_zebra_send_remote_macip(bgp, vpn, p, remote_vtep_ip, | |
ead40654 | 884 | 1, flags); |
d62a17ae | 885 | else |
886 | ret = bgp_zebra_send_remote_vtep(bgp, vpn, p, 1); | |
128ea8ab | 887 | |
d62a17ae | 888 | return ret; |
128ea8ab | 889 | } |
890 | ||
891 | /* Uninstall EVPN route from zebra. */ | |
d62a17ae | 892 | static int evpn_zebra_uninstall(struct bgp *bgp, struct bgpevpn *vpn, |
893 | struct prefix_evpn *p, | |
894 | struct in_addr remote_vtep_ip) | |
128ea8ab | 895 | { |
d62a17ae | 896 | int ret; |
128ea8ab | 897 | |
d62a17ae | 898 | if (p->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE) |
899 | ret = bgp_zebra_send_remote_macip(bgp, vpn, p, remote_vtep_ip, | |
900 | 0, 0); | |
901 | else | |
902 | ret = bgp_zebra_send_remote_vtep(bgp, vpn, p, 0); | |
128ea8ab | 903 | |
d62a17ae | 904 | return ret; |
128ea8ab | 905 | } |
906 | ||
907 | /* | |
908 | * Due to MAC mobility, the prior "local" best route has been supplanted | |
909 | * by a "remote" best route. The prior route has to be deleted and withdrawn | |
910 | * from peers. | |
911 | */ | |
d62a17ae | 912 | static void evpn_delete_old_local_route(struct bgp *bgp, struct bgpevpn *vpn, |
913 | struct bgp_node *rn, | |
914 | struct bgp_info *old_local) | |
128ea8ab | 915 | { |
d62a17ae | 916 | struct bgp_node *global_rn; |
917 | struct bgp_info *ri; | |
918 | afi_t afi = AFI_L2VPN; | |
919 | safi_t safi = SAFI_EVPN; | |
128ea8ab | 920 | |
d62a17ae | 921 | /* Locate route node in the global EVPN routing table. Note that |
922 | * this table is a 2-level tree (RD-level + Prefix-level) similar to | |
923 | * L3VPN routes. | |
924 | */ | |
925 | global_rn = bgp_afi_node_lookup(bgp->rib[afi][safi], afi, safi, | |
926 | (struct prefix *)&rn->p, &vpn->prd); | |
927 | if (global_rn) { | |
928 | /* Delete route entry in the global EVPN table. */ | |
50f74cf1 | 929 | delete_evpn_route_entry(bgp, afi, safi, global_rn, &ri); |
128ea8ab | 930 | |
d62a17ae | 931 | /* Schedule for processing - withdraws to peers happen from |
932 | * this table. | |
933 | */ | |
934 | if (ri) | |
935 | bgp_process(bgp, global_rn, afi, safi); | |
936 | bgp_unlock_node(global_rn); | |
937 | } | |
128ea8ab | 938 | |
d62a17ae | 939 | /* Delete route entry in the VNI route table, caller to remove. */ |
940 | bgp_info_delete(rn, old_local); | |
128ea8ab | 941 | } |
942 | ||
50f74cf1 | 943 | static struct in_addr *es_vtep_new(struct in_addr vtep) |
944 | { | |
945 | struct in_addr *ip; | |
946 | ||
947 | ip = XCALLOC(MTYPE_BGP_EVPN_ES_VTEP, sizeof(struct in_addr)); | |
948 | if (!ip) | |
949 | return NULL; | |
950 | ||
951 | ip->s_addr = vtep.s_addr; | |
952 | return ip; | |
953 | } | |
954 | ||
955 | static void es_vtep_free(struct in_addr *ip) | |
956 | { | |
957 | XFREE(MTYPE_BGP_EVPN_ES_VTEP, ip); | |
958 | } | |
959 | ||
960 | /* check if VTEP is already part of the list */ | |
961 | static int is_vtep_present_in_list(struct list *list, | |
962 | struct in_addr vtep) | |
963 | { | |
964 | struct listnode *node = NULL; | |
965 | struct in_addr *tmp; | |
966 | ||
967 | for (ALL_LIST_ELEMENTS_RO(list, node, tmp)) { | |
968 | if (tmp->s_addr == vtep.s_addr) | |
969 | return 1; | |
970 | } | |
971 | return 0; | |
972 | } | |
973 | ||
2bb9eff4 DS |
974 | /* |
975 | * Best path for ES route was changed, | |
976 | * update the list of VTEPs for this ES | |
977 | */ | |
50f74cf1 | 978 | static int evpn_es_install_vtep(struct bgp *bgp, |
979 | struct evpnes *es, | |
980 | struct prefix_evpn *p, | |
981 | struct in_addr rvtep) | |
982 | { | |
983 | struct in_addr *vtep_ip; | |
984 | ||
985 | if (is_vtep_present_in_list(es->vtep_list, rvtep)) | |
986 | return 0; | |
987 | ||
988 | ||
989 | vtep_ip = es_vtep_new(rvtep); | |
990 | if (vtep_ip) | |
991 | listnode_add_sort(es->vtep_list, vtep_ip); | |
992 | return 0; | |
993 | } | |
994 | ||
2bb9eff4 DS |
995 | /* |
996 | * Best path for ES route was changed, | |
997 | * update the list of VTEPs for this ES | |
998 | */ | |
50f74cf1 | 999 | static int evpn_es_uninstall_vtep(struct bgp *bgp, |
1000 | struct evpnes *es, | |
1001 | struct prefix_evpn *p, | |
1002 | struct in_addr rvtep) | |
1003 | { | |
1004 | struct listnode *node, *nnode, *node_to_del = NULL; | |
1005 | struct in_addr *tmp; | |
1006 | ||
1007 | for (ALL_LIST_ELEMENTS(es->vtep_list, node, nnode, tmp)) { | |
1008 | if (tmp->s_addr == rvtep.s_addr) { | |
1009 | es_vtep_free(tmp); | |
1010 | node_to_del = node; | |
1011 | } | |
1012 | } | |
1013 | ||
1014 | if (node_to_del) | |
1015 | list_delete_node(es->vtep_list, node_to_del); | |
1016 | ||
1017 | return 0; | |
1018 | } | |
1019 | ||
1020 | /* | |
1021 | * Calculate the best path for a ES(type-4) route. | |
1022 | */ | |
1023 | static int evpn_es_route_select_install(struct bgp *bgp, | |
1024 | struct evpnes *es, | |
1025 | struct bgp_node *rn) | |
1026 | { | |
1027 | int ret = 0; | |
1028 | afi_t afi = AFI_L2VPN; | |
1029 | safi_t safi = SAFI_EVPN; | |
1030 | struct bgp_info *old_select; /* old best */ | |
1031 | struct bgp_info *new_select; /* new best */ | |
1032 | struct bgp_info_pair old_and_new; | |
1033 | ||
1034 | /* Compute the best path. */ | |
1035 | bgp_best_selection(bgp, rn, &bgp->maxpaths[afi][safi], | |
1036 | &old_and_new, afi, safi); | |
1037 | old_select = old_and_new.old; | |
1038 | new_select = old_and_new.new; | |
1039 | ||
2bb9eff4 DS |
1040 | /* |
1041 | * If the best path hasn't changed - see if something needs to be | |
50f74cf1 | 1042 | * updated |
1043 | */ | |
1044 | if (old_select && old_select == new_select | |
1045 | && old_select->type == ZEBRA_ROUTE_BGP | |
1046 | && old_select->sub_type == BGP_ROUTE_IMPORTED | |
1047 | && !CHECK_FLAG(rn->flags, BGP_NODE_USER_CLEAR) | |
1048 | && !CHECK_FLAG(old_select->flags, BGP_INFO_ATTR_CHANGED) | |
1049 | && !bgp->addpath_tx_used[afi][safi]) { | |
1050 | if (bgp_zebra_has_route_changed(rn, old_select)) { | |
1051 | ret = evpn_es_install_vtep(bgp, es, | |
1052 | (struct prefix_evpn *)&rn->p, | |
1053 | old_select->attr->nexthop); | |
1054 | } | |
1055 | UNSET_FLAG(old_select->flags, BGP_INFO_MULTIPATH_CHG); | |
1056 | bgp_zebra_clear_route_change_flags(rn); | |
1057 | return ret; | |
1058 | } | |
1059 | ||
1060 | /* If the user did a "clear" this flag will be set */ | |
1061 | UNSET_FLAG(rn->flags, BGP_NODE_USER_CLEAR); | |
1062 | ||
2bb9eff4 DS |
1063 | /* |
1064 | * bestpath has changed; update relevant fields and install or uninstall | |
50f74cf1 | 1065 | * into the zebra RIB. |
1066 | */ | |
1067 | if (old_select || new_select) | |
1068 | bgp_bump_version(rn); | |
1069 | ||
1070 | if (old_select) | |
1071 | bgp_info_unset_flag(rn, old_select, BGP_INFO_SELECTED); | |
1072 | if (new_select) { | |
1073 | bgp_info_set_flag(rn, new_select, BGP_INFO_SELECTED); | |
1074 | bgp_info_unset_flag(rn, new_select, BGP_INFO_ATTR_CHANGED); | |
1075 | UNSET_FLAG(new_select->flags, BGP_INFO_MULTIPATH_CHG); | |
1076 | } | |
1077 | ||
1078 | if (new_select && new_select->type == ZEBRA_ROUTE_BGP | |
1079 | && new_select->sub_type == BGP_ROUTE_IMPORTED) { | |
1080 | ret = evpn_es_install_vtep(bgp, es, | |
1081 | (struct prefix_evpn *)&rn->p, | |
1082 | new_select->attr->nexthop); | |
1083 | } else { | |
1084 | if (old_select && old_select->type == ZEBRA_ROUTE_BGP | |
1085 | && old_select->sub_type == BGP_ROUTE_IMPORTED) | |
2bb9eff4 DS |
1086 | ret = evpn_es_uninstall_vtep( |
1087 | bgp, es, (struct prefix_evpn *)&rn->p, | |
1088 | old_select->attr->nexthop); | |
50f74cf1 | 1089 | } |
1090 | ||
1091 | /* Clear any route change flags. */ | |
1092 | bgp_zebra_clear_route_change_flags(rn); | |
1093 | ||
1094 | /* Reap old select bgp_info, if it has been removed */ | |
1095 | if (old_select && CHECK_FLAG(old_select->flags, BGP_INFO_REMOVED)) | |
1096 | bgp_info_reap(rn, old_select); | |
1097 | ||
1098 | return ret; | |
1099 | } | |
1100 | ||
128ea8ab | 1101 | /* |
1102 | * Calculate the best path for an EVPN route. Install/update best path in zebra, | |
1103 | * if appropriate. | |
1104 | */ | |
d62a17ae | 1105 | static int evpn_route_select_install(struct bgp *bgp, struct bgpevpn *vpn, |
1106 | struct bgp_node *rn) | |
1107 | { | |
1108 | struct bgp_info *old_select, *new_select; | |
1109 | struct bgp_info_pair old_and_new; | |
68e33151 | 1110 | struct prefix_evpn *evp; |
d62a17ae | 1111 | afi_t afi = AFI_L2VPN; |
1112 | safi_t safi = SAFI_EVPN; | |
1113 | int ret = 0; | |
d7c0a89a | 1114 | uint8_t flags = 0; |
d62a17ae | 1115 | |
1116 | /* Compute the best path. */ | |
1117 | bgp_best_selection(bgp, rn, &bgp->maxpaths[afi][safi], &old_and_new, | |
1118 | afi, safi); | |
1119 | old_select = old_and_new.old; | |
1120 | new_select = old_and_new.new; | |
1121 | ||
68e33151 | 1122 | evp = (struct prefix_evpn *)&rn->p; |
d62a17ae | 1123 | /* If the best path hasn't changed - see if there is still something to |
1124 | * update | |
1125 | * to zebra RIB. | |
1126 | */ | |
1127 | if (old_select && old_select == new_select | |
1128 | && old_select->type == ZEBRA_ROUTE_BGP | |
90f4f482 | 1129 | && old_select->sub_type == BGP_ROUTE_IMPORTED |
d62a17ae | 1130 | && !CHECK_FLAG(rn->flags, BGP_NODE_USER_CLEAR) |
1131 | && !CHECK_FLAG(old_select->flags, BGP_INFO_ATTR_CHANGED) | |
1132 | && !bgp->addpath_tx_used[afi][safi]) { | |
ead40654 MK |
1133 | if (bgp_zebra_has_route_changed(rn, old_select)) { |
1134 | if (old_select->attr->sticky) | |
1135 | SET_FLAG(flags, ZEBRA_MACIP_TYPE_STICKY); | |
1136 | if (old_select->attr->default_gw) | |
1137 | SET_FLAG(flags, ZEBRA_MACIP_TYPE_GW); | |
68e33151 CS |
1138 | if (is_evpn_prefix_ipaddr_v6(evp) && |
1139 | old_select->attr->router_flag) | |
1140 | SET_FLAG(flags, ZEBRA_MACIP_TYPE_ROUTER_FLAG); | |
1141 | ||
996c9314 LB |
1142 | ret = evpn_zebra_install( |
1143 | bgp, vpn, (struct prefix_evpn *)&rn->p, | |
1144 | old_select->attr->nexthop, flags); | |
ead40654 | 1145 | } |
d62a17ae | 1146 | UNSET_FLAG(old_select->flags, BGP_INFO_MULTIPATH_CHG); |
1147 | bgp_zebra_clear_route_change_flags(rn); | |
1148 | return ret; | |
1149 | } | |
1150 | ||
1151 | /* If the user did a "clear" this flag will be set */ | |
1152 | UNSET_FLAG(rn->flags, BGP_NODE_USER_CLEAR); | |
1153 | ||
1154 | /* bestpath has changed; update relevant fields and install or uninstall | |
1155 | * into the zebra RIB. | |
1156 | */ | |
1157 | if (old_select || new_select) | |
1158 | bgp_bump_version(rn); | |
1159 | ||
1160 | if (old_select) | |
1161 | bgp_info_unset_flag(rn, old_select, BGP_INFO_SELECTED); | |
1162 | if (new_select) { | |
1163 | bgp_info_set_flag(rn, new_select, BGP_INFO_SELECTED); | |
1164 | bgp_info_unset_flag(rn, new_select, BGP_INFO_ATTR_CHANGED); | |
1165 | UNSET_FLAG(new_select->flags, BGP_INFO_MULTIPATH_CHG); | |
1166 | } | |
1167 | ||
1168 | if (new_select && new_select->type == ZEBRA_ROUTE_BGP | |
90f4f482 | 1169 | && new_select->sub_type == BGP_ROUTE_IMPORTED) { |
ead40654 MK |
1170 | flags = 0; |
1171 | if (new_select->attr->sticky) | |
1172 | SET_FLAG(flags, ZEBRA_MACIP_TYPE_STICKY); | |
1173 | if (new_select->attr->default_gw) | |
1174 | SET_FLAG(flags, ZEBRA_MACIP_TYPE_GW); | |
68e33151 CS |
1175 | if (is_evpn_prefix_ipaddr_v6(evp) && |
1176 | new_select->attr->router_flag) | |
1177 | SET_FLAG(flags, ZEBRA_MACIP_TYPE_ROUTER_FLAG); | |
1178 | ||
d62a17ae | 1179 | ret = evpn_zebra_install(bgp, vpn, (struct prefix_evpn *)&rn->p, |
996c9314 | 1180 | new_select->attr->nexthop, flags); |
d62a17ae | 1181 | /* If an old best existed and it was a "local" route, the only |
1182 | * reason | |
1183 | * it would be supplanted is due to MAC mobility procedures. So, | |
1184 | * we | |
1185 | * need to do an implicit delete and withdraw that route from | |
1186 | * peers. | |
1187 | */ | |
1188 | if (old_select && old_select->peer == bgp->peer_self | |
1189 | && old_select->type == ZEBRA_ROUTE_BGP | |
1190 | && old_select->sub_type == BGP_ROUTE_STATIC) | |
1191 | evpn_delete_old_local_route(bgp, vpn, rn, old_select); | |
1192 | } else { | |
1193 | if (old_select && old_select->type == ZEBRA_ROUTE_BGP | |
90f4f482 | 1194 | && old_select->sub_type == BGP_ROUTE_IMPORTED) |
d62a17ae | 1195 | ret = evpn_zebra_uninstall(bgp, vpn, |
1196 | (struct prefix_evpn *)&rn->p, | |
1197 | old_select->attr->nexthop); | |
1198 | } | |
1199 | ||
1200 | /* Clear any route change flags. */ | |
1201 | bgp_zebra_clear_route_change_flags(rn); | |
1202 | ||
1203 | /* Reap old select bgp_info, if it has been removed */ | |
1204 | if (old_select && CHECK_FLAG(old_select->flags, BGP_INFO_REMOVED)) | |
1205 | bgp_info_reap(rn, old_select); | |
1206 | ||
1207 | return ret; | |
128ea8ab | 1208 | } |
1209 | ||
ead40654 MK |
1210 | /* |
1211 | * Return true if the local ri for this rn is of type gateway mac | |
1212 | */ | |
1213 | static int evpn_route_is_def_gw(struct bgp *bgp, struct bgp_node *rn) | |
1214 | { | |
996c9314 LB |
1215 | struct bgp_info *tmp_ri = NULL; |
1216 | struct bgp_info *local_ri = NULL; | |
ead40654 MK |
1217 | |
1218 | local_ri = NULL; | |
1219 | for (tmp_ri = rn->info; tmp_ri; tmp_ri = tmp_ri->next) { | |
1220 | if (tmp_ri->peer == bgp->peer_self | |
1221 | && tmp_ri->type == ZEBRA_ROUTE_BGP | |
1222 | && tmp_ri->sub_type == BGP_ROUTE_STATIC) | |
1223 | local_ri = tmp_ri; | |
1224 | } | |
1225 | ||
1226 | if (!local_ri) | |
1227 | return 0; | |
1228 | ||
1229 | return local_ri->attr->default_gw; | |
1230 | } | |
1231 | ||
c85c03c7 | 1232 | |
1233 | /* | |
1234 | * Return true if the local ri for this rn has sticky set | |
1235 | */ | |
d62a17ae | 1236 | static int evpn_route_is_sticky(struct bgp *bgp, struct bgp_node *rn) |
c85c03c7 | 1237 | { |
d62a17ae | 1238 | struct bgp_info *tmp_ri; |
1239 | struct bgp_info *local_ri; | |
c85c03c7 | 1240 | |
d62a17ae | 1241 | local_ri = NULL; |
1242 | for (tmp_ri = rn->info; tmp_ri; tmp_ri = tmp_ri->next) { | |
1243 | if (tmp_ri->peer == bgp->peer_self | |
1244 | && tmp_ri->type == ZEBRA_ROUTE_BGP | |
1245 | && tmp_ri->sub_type == BGP_ROUTE_STATIC) | |
1246 | local_ri = tmp_ri; | |
1247 | } | |
c85c03c7 | 1248 | |
d62a17ae | 1249 | if (!local_ri) |
1250 | return 0; | |
c85c03c7 | 1251 | |
d62a17ae | 1252 | return local_ri->attr->sticky; |
c85c03c7 | 1253 | } |
1254 | ||
50f74cf1 | 1255 | /* |
1256 | * create or update EVPN type4 route entry. | |
1257 | * This could be in the ES table or the global table. | |
1258 | * TODO: handle remote ES (type4) routes as well | |
1259 | */ | |
1260 | static int update_evpn_type4_route_entry(struct bgp *bgp, | |
1261 | struct evpnes *es, | |
1262 | afi_t afi, safi_t safi, | |
1263 | struct bgp_node *rn, | |
1264 | struct attr *attr, | |
1265 | int add, | |
1266 | struct bgp_info **ri, | |
1267 | int *route_changed) | |
1268 | { | |
1269 | char buf[ESI_STR_LEN]; | |
1270 | char buf1[INET6_ADDRSTRLEN]; | |
1271 | struct bgp_info *tmp_ri = NULL; | |
1272 | struct bgp_info *local_ri = NULL; /* local route entry if any */ | |
1273 | struct bgp_info *remote_ri = NULL; /* remote route entry if any */ | |
1274 | struct attr *attr_new = NULL; | |
1275 | struct prefix_evpn *evp = NULL; | |
1276 | ||
1277 | *ri = NULL; | |
1278 | *route_changed = 1; | |
1279 | evp = (struct prefix_evpn *)&rn->p; | |
1280 | ||
1281 | /* locate the local and remote entries if any */ | |
1282 | for (tmp_ri = rn->info; tmp_ri; tmp_ri = tmp_ri->next) { | |
1283 | if (tmp_ri->peer == bgp->peer_self && | |
1284 | tmp_ri->type == ZEBRA_ROUTE_BGP && | |
1285 | tmp_ri->sub_type == BGP_ROUTE_STATIC) | |
1286 | local_ri = tmp_ri; | |
1287 | if (tmp_ri->type == ZEBRA_ROUTE_BGP && | |
1288 | tmp_ri->sub_type == BGP_ROUTE_IMPORTED && | |
1289 | CHECK_FLAG(tmp_ri->flags, BGP_INFO_VALID)) | |
1290 | remote_ri = tmp_ri; | |
1291 | } | |
1292 | ||
1293 | /* we don't expect to see a remote_ri at this point. | |
1294 | * An ES route has esi + vtep_ip as the key, | |
1295 | * We shouldn't see the same route from any other vtep. | |
1296 | */ | |
1297 | if (remote_ri) { | |
af4c2728 | 1298 | flog_err( |
14454c9f DS |
1299 | BGP_ERR_ES_INVALID, |
1300 | "%u ERROR: local es route for ESI: %s Vtep %s also learnt from remote", | |
1301 | bgp->vrf_id, | |
1302 | esi_to_str(&evp->prefix.es_addr.esi, buf, sizeof(buf)), | |
1303 | ipaddr2str(&es->originator_ip, buf1, sizeof(buf1))); | |
50f74cf1 | 1304 | return -1; |
1305 | } | |
1306 | ||
1307 | if (!local_ri && !add) | |
1308 | return 0; | |
1309 | ||
1310 | /* create or update the entry */ | |
1311 | if (!local_ri) { | |
1312 | ||
1313 | /* Add or update attribute to hash */ | |
1314 | attr_new = bgp_attr_intern(attr); | |
1315 | ||
1316 | /* Create new route with its attribute. */ | |
1317 | tmp_ri = info_make(ZEBRA_ROUTE_BGP, BGP_ROUTE_STATIC, | |
1318 | 0, bgp->peer_self, attr_new, rn); | |
1319 | SET_FLAG(tmp_ri->flags, BGP_INFO_VALID); | |
1320 | ||
1321 | /* add the newly created path to the route-node */ | |
1322 | bgp_info_add(rn, tmp_ri); | |
1323 | } else { | |
1324 | tmp_ri = local_ri; | |
1325 | if (attrhash_cmp(tmp_ri->attr, attr) | |
1326 | && !CHECK_FLAG(tmp_ri->flags, BGP_INFO_REMOVED)) | |
1327 | *route_changed = 0; | |
1328 | else { | |
1329 | /* The attribute has changed. | |
1330 | * Add (or update) attribute to hash. */ | |
1331 | attr_new = bgp_attr_intern(attr); | |
1332 | bgp_info_set_flag(rn, tmp_ri, BGP_INFO_ATTR_CHANGED); | |
1333 | ||
1334 | /* Restore route, if needed. */ | |
1335 | if (CHECK_FLAG(tmp_ri->flags, BGP_INFO_REMOVED)) | |
1336 | bgp_info_restore(rn, tmp_ri); | |
1337 | ||
1338 | /* Unintern existing, set to new. */ | |
1339 | bgp_attr_unintern(&tmp_ri->attr); | |
1340 | tmp_ri->attr = attr_new; | |
1341 | tmp_ri->uptime = bgp_clock(); | |
1342 | } | |
1343 | } | |
1344 | ||
1345 | /* Return back the route entry. */ | |
1346 | *ri = tmp_ri; | |
1347 | return 0; | |
1348 | } | |
1349 | ||
1350 | /* update evpn es (type-4) route */ | |
1351 | static int update_evpn_type4_route(struct bgp *bgp, | |
1352 | struct evpnes *es, | |
1353 | struct prefix_evpn *p) | |
1354 | { | |
1355 | int ret = 0; | |
1356 | int route_changed = 0; | |
1357 | char buf[ESI_STR_LEN]; | |
1358 | char buf1[INET6_ADDRSTRLEN]; | |
1359 | afi_t afi = AFI_L2VPN; | |
1360 | safi_t safi = SAFI_EVPN; | |
1361 | struct attr attr; | |
1362 | struct attr *attr_new = NULL; | |
1363 | struct bgp_node *rn = NULL; | |
1364 | struct bgp_info *ri = NULL; | |
1365 | ||
1366 | memset(&attr, 0, sizeof(struct attr)); | |
1367 | ||
1368 | /* Build path-attribute for this route. */ | |
1369 | bgp_attr_default_set(&attr, BGP_ORIGIN_IGP); | |
1370 | attr.nexthop = es->originator_ip.ipaddr_v4; | |
1371 | attr.mp_nexthop_global_in = es->originator_ip.ipaddr_v4; | |
1372 | attr.mp_nexthop_len = BGP_ATTR_NHLEN_IPV4; | |
1373 | ||
1374 | /* Set up extended community. */ | |
1375 | build_evpn_type4_route_extcomm(es, &attr); | |
1376 | ||
1377 | /* First, create (or fetch) route node within the ESI. */ | |
1378 | /* NOTE: There is no RD here. */ | |
1379 | rn = bgp_node_get(es->route_table, (struct prefix *)p); | |
1380 | ||
1381 | /* Create or update route entry. */ | |
1382 | ret = update_evpn_type4_route_entry(bgp, es, afi, safi, rn, | |
1383 | &attr, 1, &ri, | |
1384 | &route_changed); | |
1385 | if (ret != 0) { | |
af4c2728 | 1386 | flog_err( |
14454c9f DS |
1387 | BGP_ERR_ES_INVALID, |
1388 | "%u ERROR: Failed to updated ES route ESI: %s VTEP %s", | |
1389 | bgp->vrf_id, | |
1390 | esi_to_str(&p->prefix.es_addr.esi, buf, sizeof(buf)), | |
1391 | ipaddr2str(&es->originator_ip, buf1, sizeof(buf1))); | |
50f74cf1 | 1392 | } |
1393 | ||
1394 | assert(ri); | |
1395 | attr_new = ri->attr; | |
1396 | ||
1397 | /* Perform route selection; | |
1398 | * this is just to set the flags correctly | |
1399 | * as local route in the ES always wins. | |
1400 | */ | |
1401 | evpn_es_route_select_install(bgp, es, rn); | |
1402 | bgp_unlock_node(rn); | |
1403 | ||
1404 | /* If this is a new route or some attribute has changed, export the | |
1405 | * route to the global table. The route will be advertised to peers | |
1406 | * from there. Note that this table is a 2-level tree (RD-level + | |
1407 | * Prefix-level) similar to L3VPN routes. | |
1408 | */ | |
1409 | if (route_changed) { | |
1410 | struct bgp_info *global_ri; | |
1411 | ||
1412 | rn = bgp_afi_node_get(bgp->rib[afi][safi], afi, safi, | |
1413 | (struct prefix *)p, &es->prd); | |
1414 | update_evpn_type4_route_entry(bgp, es, afi, safi, | |
1415 | rn, attr_new, | |
1416 | 1, &global_ri, | |
1417 | &route_changed); | |
1418 | ||
1419 | /* Schedule for processing and unlock node. */ | |
1420 | bgp_process(bgp, rn, afi, safi); | |
1421 | bgp_unlock_node(rn); | |
1422 | } | |
1423 | ||
1424 | /* Unintern temporary. */ | |
1425 | aspath_unintern(&attr.aspath); | |
1426 | return 0; | |
1427 | } | |
1428 | ||
342dd0c6 | 1429 | static int update_evpn_type5_route_entry(struct bgp *bgp_def, |
1430 | struct bgp *bgp_vrf, afi_t afi, | |
1431 | safi_t safi, struct bgp_node *rn, | |
5424b7ba | 1432 | struct attr *attr, int *route_changed) |
342dd0c6 | 1433 | { |
1434 | struct attr *attr_new = NULL; | |
1435 | struct bgp_info *ri = NULL; | |
1436 | mpls_label_t label = MPLS_INVALID_LABEL; | |
1437 | struct bgp_info *local_ri = NULL; | |
1438 | struct bgp_info *tmp_ri = NULL; | |
1439 | ||
5424b7ba | 1440 | *route_changed = 0; |
342dd0c6 | 1441 | /* locate the local route entry if any */ |
1442 | for (tmp_ri = rn->info; tmp_ri; tmp_ri = tmp_ri->next) { | |
1443 | if (tmp_ri->peer == bgp_def->peer_self | |
1444 | && tmp_ri->type == ZEBRA_ROUTE_BGP | |
1445 | && tmp_ri->sub_type == BGP_ROUTE_STATIC) | |
1446 | local_ri = tmp_ri; | |
1447 | } | |
1448 | ||
2bb9eff4 DS |
1449 | /* |
1450 | * create a new route entry if one doesnt exist. | |
1451 | * Otherwise see if route attr has changed | |
523cafc4 | 1452 | */ |
342dd0c6 | 1453 | if (!local_ri) { |
1454 | ||
5424b7ba MK |
1455 | /* route has changed as this is the first entry */ |
1456 | *route_changed = 1; | |
1457 | ||
342dd0c6 | 1458 | /* Add (or update) attribute to hash. */ |
1459 | attr_new = bgp_attr_intern(attr); | |
1460 | ||
1461 | /* create the route info from attribute */ | |
1462 | ri = info_make(ZEBRA_ROUTE_BGP, BGP_ROUTE_STATIC, 0, | |
1463 | bgp_def->peer_self, attr_new, rn); | |
1464 | SET_FLAG(ri->flags, BGP_INFO_VALID); | |
1465 | ||
b57ba6d2 | 1466 | /* Type-5 routes advertise the L3-VNI */ |
342dd0c6 | 1467 | bgp_info_extra_get(ri); |
1468 | vni2label(bgp_vrf->l3vni, &label); | |
b57ba6d2 MK |
1469 | memcpy(&ri->extra->label, &label, sizeof(label)); |
1470 | ri->extra->num_labels = 1; | |
342dd0c6 | 1471 | |
1472 | /* add the route entry to route node*/ | |
1473 | bgp_info_add(rn, ri); | |
1474 | } else { | |
1475 | ||
1476 | tmp_ri = local_ri; | |
1477 | if (!attrhash_cmp(tmp_ri->attr, attr)) { | |
5424b7ba MK |
1478 | |
1479 | /* attribute changed */ | |
1480 | *route_changed = 1; | |
1481 | ||
342dd0c6 | 1482 | /* The attribute has changed. */ |
1483 | /* Add (or update) attribute to hash. */ | |
1484 | attr_new = bgp_attr_intern(attr); | |
1485 | bgp_info_set_flag(rn, tmp_ri, BGP_INFO_ATTR_CHANGED); | |
1486 | ||
1487 | /* Restore route, if needed. */ | |
1488 | if (CHECK_FLAG(tmp_ri->flags, BGP_INFO_REMOVED)) | |
1489 | bgp_info_restore(rn, tmp_ri); | |
1490 | ||
1491 | /* Unintern existing, set to new. */ | |
1492 | bgp_attr_unintern(&tmp_ri->attr); | |
1493 | tmp_ri->attr = attr_new; | |
1494 | tmp_ri->uptime = bgp_clock(); | |
1495 | } | |
1496 | } | |
1497 | return 0; | |
1498 | } | |
1499 | ||
1500 | /* update evpn type-5 route entry */ | |
996c9314 LB |
1501 | static int update_evpn_type5_route(struct bgp *bgp_vrf, struct prefix_evpn *evp, |
1502 | struct attr *src_attr) | |
342dd0c6 | 1503 | { |
1504 | afi_t afi = AFI_L2VPN; | |
1505 | safi_t safi = SAFI_EVPN; | |
1506 | struct attr attr; | |
1507 | struct bgp_node *rn = NULL; | |
1508 | struct bgp *bgp_def = NULL; | |
5424b7ba | 1509 | int route_changed = 0; |
342dd0c6 | 1510 | |
1511 | bgp_def = bgp_get_default(); | |
1512 | if (!bgp_def) | |
faafdfa8 | 1513 | return 0; |
342dd0c6 | 1514 | |
2f69f6d3 | 1515 | /* Build path attribute for this route - use the source attr, if |
1516 | * present, else treat as locally originated. | |
1517 | */ | |
1518 | if (src_attr) | |
1519 | bgp_attr_dup(&attr, src_attr); | |
1520 | else { | |
1521 | memset(&attr, 0, sizeof(struct attr)); | |
1522 | bgp_attr_default_set(&attr, BGP_ORIGIN_IGP); | |
1523 | } | |
1524 | /* Set nexthop to ourselves and fill in the Router MAC. */ | |
342dd0c6 | 1525 | attr.nexthop = bgp_vrf->originator_ip; |
1526 | attr.mp_nexthop_global_in = bgp_vrf->originator_ip; | |
1527 | attr.mp_nexthop_len = BGP_ATTR_NHLEN_IPV4; | |
1528 | memcpy(&attr.rmac, &bgp_vrf->rmac, sizeof(struct ethaddr)); | |
1529 | ||
1530 | /* Setup RT and encap extended community */ | |
1531 | build_evpn_type5_route_extcomm(bgp_vrf, &attr); | |
1532 | ||
1533 | /* get the route node in global table */ | |
1534 | rn = bgp_afi_node_get(bgp_def->rib[afi][safi], afi, safi, | |
996c9314 | 1535 | (struct prefix *)evp, &bgp_vrf->vrf_prd); |
342dd0c6 | 1536 | assert(rn); |
1537 | ||
1538 | /* create or update the route entry within the route node */ | |
996c9314 LB |
1539 | update_evpn_type5_route_entry(bgp_def, bgp_vrf, afi, safi, rn, &attr, |
1540 | &route_changed); | |
342dd0c6 | 1541 | |
1542 | /* schedule for processing and unlock node */ | |
5424b7ba MK |
1543 | if (route_changed) { |
1544 | bgp_process(bgp_def, rn, afi, safi); | |
1545 | bgp_unlock_node(rn); | |
1546 | } | |
342dd0c6 | 1547 | |
1548 | /* uninten temporary */ | |
5ee65f6f | 1549 | if (!src_attr) |
1550 | aspath_unintern(&attr.aspath); | |
342dd0c6 | 1551 | return 0; |
1552 | } | |
1553 | ||
128ea8ab | 1554 | /* |
1555 | * Create or update EVPN route entry. This could be in the VNI route table | |
1556 | * or the global route table. | |
1557 | */ | |
d62a17ae | 1558 | static int update_evpn_route_entry(struct bgp *bgp, struct bgpevpn *vpn, |
1559 | afi_t afi, safi_t safi, struct bgp_node *rn, | |
1560 | struct attr *attr, int add, int vni_table, | |
d7c0a89a | 1561 | struct bgp_info **ri, uint8_t flags) |
d62a17ae | 1562 | { |
1563 | struct bgp_info *tmp_ri; | |
1564 | struct bgp_info *local_ri, *remote_ri; | |
1565 | struct attr *attr_new; | |
b57ba6d2 | 1566 | mpls_label_t label[BGP_MAX_LABELS]; |
d7c0a89a | 1567 | uint32_t num_labels = 1; |
d62a17ae | 1568 | int route_change = 1; |
d7c0a89a | 1569 | uint8_t sticky = 0; |
b57ba6d2 | 1570 | struct prefix_evpn *evp; |
d62a17ae | 1571 | |
1572 | *ri = NULL; | |
b57ba6d2 MK |
1573 | evp = (struct prefix_evpn *)&rn->p; |
1574 | memset(&label, 0, sizeof(label)); | |
d62a17ae | 1575 | |
1576 | /* See if this is an update of an existing route, or a new add. Also, | |
1577 | * identify if already known from remote, and if so, the one with the | |
1578 | * highest sequence number; this is only when adding to the VNI routing | |
1579 | * table. | |
1580 | */ | |
1581 | local_ri = remote_ri = NULL; | |
1582 | for (tmp_ri = rn->info; tmp_ri; tmp_ri = tmp_ri->next) { | |
1583 | if (tmp_ri->peer == bgp->peer_self | |
1584 | && tmp_ri->type == ZEBRA_ROUTE_BGP | |
1585 | && tmp_ri->sub_type == BGP_ROUTE_STATIC) | |
1586 | local_ri = tmp_ri; | |
1587 | if (vni_table) { | |
1588 | if (tmp_ri->type == ZEBRA_ROUTE_BGP | |
90f4f482 | 1589 | && tmp_ri->sub_type == BGP_ROUTE_IMPORTED |
d62a17ae | 1590 | && CHECK_FLAG(tmp_ri->flags, BGP_INFO_VALID)) { |
1591 | if (!remote_ri) | |
1592 | remote_ri = tmp_ri; | |
1593 | else if (mac_mobility_seqnum(tmp_ri->attr) | |
1594 | > mac_mobility_seqnum(remote_ri->attr)) | |
1595 | remote_ri = tmp_ri; | |
1596 | } | |
1597 | } | |
1598 | } | |
1599 | ||
1600 | /* If route doesn't exist already, create a new one, if told to. | |
1601 | * Otherwise act based on whether the attributes of the route have | |
1602 | * changed or not. | |
1603 | */ | |
1604 | if (!local_ri && !add) | |
1605 | return 0; | |
1606 | ||
1607 | if (!local_ri) { | |
1608 | /* When learnt locally for the first time but already known from | |
1609 | * remote, we have to initiate appropriate MAC mobility steps. | |
50f74cf1 | 1610 | * This is applicable when updating the VNI routing table. |
1a98c087 MK |
1611 | * We need to skip mobility steps for g/w macs (local mac on g/w |
1612 | * SVI) advertised in EVPN. | |
1613 | * This will ensure that local routes are preferred for g/w macs | |
d62a17ae | 1614 | */ |
ead40654 | 1615 | if (remote_ri && !CHECK_FLAG(flags, ZEBRA_MACIP_TYPE_GW)) { |
d7c0a89a | 1616 | uint32_t cur_seqnum; |
d62a17ae | 1617 | |
1618 | /* Add MM extended community to route. */ | |
1619 | cur_seqnum = mac_mobility_seqnum(remote_ri->attr); | |
1620 | add_mac_mobility_to_attr(cur_seqnum + 1, attr); | |
1621 | } | |
1622 | ||
1623 | /* Add (or update) attribute to hash. */ | |
1624 | attr_new = bgp_attr_intern(attr); | |
1625 | ||
1626 | /* Extract MAC mobility sequence number, if any. */ | |
1627 | attr_new->mm_seqnum = | |
1628 | bgp_attr_mac_mobility_seqnum(attr_new, &sticky); | |
1629 | attr_new->sticky = sticky; | |
1630 | ||
1631 | /* Create new route with its attribute. */ | |
1632 | tmp_ri = info_make(ZEBRA_ROUTE_BGP, BGP_ROUTE_STATIC, 0, | |
1633 | bgp->peer_self, attr_new, rn); | |
1634 | SET_FLAG(tmp_ri->flags, BGP_INFO_VALID); | |
1635 | bgp_info_extra_get(tmp_ri); | |
1636 | ||
1637 | /* The VNI goes into the 'label' field of the route */ | |
b57ba6d2 | 1638 | vni2label(vpn->vni, &label[0]); |
c48d9f5f MK |
1639 | |
1640 | /* Type-2 routes may carry a second VNI - the L3-VNI. | |
1641 | * Only attach second label if we are advertising two labels for | |
1642 | * type-2 routes. | |
1643 | */ | |
996c9314 LB |
1644 | if (evp->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE |
1645 | && CHECK_FLAG(vpn->flags, VNI_FLAG_USE_TWO_LABELS)) { | |
b57ba6d2 MK |
1646 | vni_t l3vni; |
1647 | ||
1648 | l3vni = bgpevpn_get_l3vni(vpn); | |
1649 | if (l3vni) { | |
1650 | vni2label(l3vni, &label[1]); | |
1651 | num_labels++; | |
1652 | } | |
1653 | } | |
d62a17ae | 1654 | |
b57ba6d2 MK |
1655 | memcpy(&tmp_ri->extra->label, label, sizeof(label)); |
1656 | tmp_ri->extra->num_labels = num_labels; | |
d62a17ae | 1657 | bgp_info_add(rn, tmp_ri); |
1658 | } else { | |
1659 | tmp_ri = local_ri; | |
1660 | if (attrhash_cmp(tmp_ri->attr, attr) | |
1661 | && !CHECK_FLAG(tmp_ri->flags, BGP_INFO_REMOVED)) | |
1662 | route_change = 0; | |
1663 | else { | |
c48d9f5f MK |
1664 | /* |
1665 | * The attributes have changed, type-2 routes needs to | |
1666 | * be advertised with right labels. | |
1667 | */ | |
1668 | vni2label(vpn->vni, &label[0]); | |
996c9314 LB |
1669 | if (evp->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE |
1670 | && CHECK_FLAG(vpn->flags, | |
1671 | VNI_FLAG_USE_TWO_LABELS)) { | |
c48d9f5f MK |
1672 | vni_t l3vni; |
1673 | ||
1674 | l3vni = bgpevpn_get_l3vni(vpn); | |
1675 | if (l3vni) { | |
1676 | vni2label(l3vni, &label[1]); | |
1677 | num_labels++; | |
1678 | } | |
1679 | } | |
1680 | memcpy(&tmp_ri->extra->label, label, sizeof(label)); | |
1681 | tmp_ri->extra->num_labels = num_labels; | |
1682 | ||
d62a17ae | 1683 | /* The attribute has changed. */ |
1684 | /* Add (or update) attribute to hash. */ | |
1685 | attr_new = bgp_attr_intern(attr); | |
1686 | bgp_info_set_flag(rn, tmp_ri, BGP_INFO_ATTR_CHANGED); | |
1687 | ||
1688 | /* Restore route, if needed. */ | |
1689 | if (CHECK_FLAG(tmp_ri->flags, BGP_INFO_REMOVED)) | |
1690 | bgp_info_restore(rn, tmp_ri); | |
1691 | ||
1692 | /* Unintern existing, set to new. */ | |
1693 | bgp_attr_unintern(&tmp_ri->attr); | |
1694 | tmp_ri->attr = attr_new; | |
1695 | tmp_ri->uptime = bgp_clock(); | |
1696 | } | |
1697 | } | |
1698 | ||
1699 | /* Return back the route entry. */ | |
1700 | *ri = tmp_ri; | |
1701 | return route_change; | |
128ea8ab | 1702 | } |
1703 | ||
1704 | /* | |
1705 | * Create or update EVPN route (of type based on prefix) for specified VNI | |
1706 | * and schedule for processing. | |
1707 | */ | |
d62a17ae | 1708 | static int update_evpn_route(struct bgp *bgp, struct bgpevpn *vpn, |
d7c0a89a | 1709 | struct prefix_evpn *p, uint8_t flags) |
128ea8ab | 1710 | { |
d62a17ae | 1711 | struct bgp_node *rn; |
1712 | struct attr attr; | |
1713 | struct attr *attr_new; | |
1ec31309 | 1714 | int add_l3_ecomm = 0; |
d62a17ae | 1715 | struct bgp_info *ri; |
1716 | afi_t afi = AFI_L2VPN; | |
1717 | safi_t safi = SAFI_EVPN; | |
1718 | int route_change; | |
128ea8ab | 1719 | |
d62a17ae | 1720 | memset(&attr, 0, sizeof(struct attr)); |
128ea8ab | 1721 | |
d62a17ae | 1722 | /* Build path-attribute for this route. */ |
1723 | bgp_attr_default_set(&attr, BGP_ORIGIN_IGP); | |
1724 | attr.nexthop = vpn->originator_ip; | |
1725 | attr.mp_nexthop_global_in = vpn->originator_ip; | |
1726 | attr.mp_nexthop_len = BGP_ATTR_NHLEN_IPV4; | |
317f1fe0 | 1727 | attr.sticky = CHECK_FLAG(flags, ZEBRA_MACIP_TYPE_STICKY) ? 1 : 0; |
ead40654 | 1728 | attr.default_gw = CHECK_FLAG(flags, ZEBRA_MACIP_TYPE_GW) ? 1 : 0; |
68e33151 CS |
1729 | attr.router_flag = CHECK_FLAG(flags, |
1730 | ZEBRA_MACIP_TYPE_ROUTER_FLAG) ? 1 : 0; | |
be41eb68 MK |
1731 | |
1732 | /* PMSI is only needed for type-3 routes */ | |
1733 | if (p->prefix.route_type == BGP_EVPN_IMET_ROUTE) | |
1734 | attr.flag |= ATTR_FLAG_BIT(BGP_ATTR_PMSI_TUNNEL); | |
1735 | ||
1ec31309 | 1736 | /* router mac is only needed for type-2 routes here. */ |
be41eb68 MK |
1737 | if (p->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE) |
1738 | bgpevpn_get_rmac(vpn, &attr.rmac); | |
a21bd7a3 | 1739 | vni2label(vpn->vni, &(attr.label)); |
128ea8ab | 1740 | |
1ec31309 | 1741 | /* Include L3 VNI related RTs and RMAC for type-2 routes, if they're |
1742 | * IPv4 or IPv6 global addresses and we're advertising L3VNI with | |
1743 | * these routes. | |
1744 | */ | |
1745 | if (p->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE && | |
3714a385 | 1746 | (is_evpn_prefix_ipaddr_v4(p) || |
1747 | !IN6_IS_ADDR_LINKLOCAL(&p->prefix.macip_addr.ip.ipaddr_v6)) && | |
148b548c | 1748 | CHECK_FLAG(vpn->flags, VNI_FLAG_USE_TWO_LABELS) && |
1749 | bgpevpn_get_l3vni(vpn)) | |
1ec31309 | 1750 | add_l3_ecomm = 1; |
1751 | ||
1752 | /* Set up extended community. */ | |
1753 | build_evpn_route_extcomm(vpn, &attr, add_l3_ecomm); | |
128ea8ab | 1754 | |
d62a17ae | 1755 | /* First, create (or fetch) route node within the VNI. */ |
1756 | /* NOTE: There is no RD here. */ | |
1757 | rn = bgp_node_get(vpn->route_table, (struct prefix *)p); | |
128ea8ab | 1758 | |
d62a17ae | 1759 | /* Create or update route entry. */ |
1760 | route_change = update_evpn_route_entry(bgp, vpn, afi, safi, rn, &attr, | |
1a98c087 | 1761 | 1, 1, &ri, flags); |
d62a17ae | 1762 | assert(ri); |
1763 | attr_new = ri->attr; | |
128ea8ab | 1764 | |
d62a17ae | 1765 | /* Perform route selection; this is just to set the flags correctly |
1766 | * as local route in the VNI always wins. | |
1767 | */ | |
1768 | evpn_route_select_install(bgp, vpn, rn); | |
1769 | bgp_unlock_node(rn); | |
128ea8ab | 1770 | |
d62a17ae | 1771 | /* If this is a new route or some attribute has changed, export the |
1772 | * route to the global table. The route will be advertised to peers | |
1773 | * from there. Note that this table is a 2-level tree (RD-level + | |
1774 | * Prefix-level) similar to L3VPN routes. | |
1775 | */ | |
1776 | if (route_change) { | |
1777 | struct bgp_info *global_ri; | |
128ea8ab | 1778 | |
d62a17ae | 1779 | rn = bgp_afi_node_get(bgp->rib[afi][safi], afi, safi, |
1780 | (struct prefix *)p, &vpn->prd); | |
1781 | update_evpn_route_entry(bgp, vpn, afi, safi, rn, attr_new, 1, 0, | |
1a98c087 | 1782 | &global_ri, flags); |
128ea8ab | 1783 | |
d62a17ae | 1784 | /* Schedule for processing and unlock node. */ |
1785 | bgp_process(bgp, rn, afi, safi); | |
1786 | bgp_unlock_node(rn); | |
1787 | } | |
128ea8ab | 1788 | |
d62a17ae | 1789 | /* Unintern temporary. */ |
1790 | aspath_unintern(&attr.aspath); | |
128ea8ab | 1791 | |
d62a17ae | 1792 | return 0; |
128ea8ab | 1793 | } |
1794 | ||
50f74cf1 | 1795 | /* |
1796 | * Delete EVPN route entry. | |
1797 | * The entry can be in ESI/VNI table or the global table. | |
1798 | */ | |
1799 | static void delete_evpn_route_entry(struct bgp *bgp, | |
1800 | afi_t afi, safi_t safi, | |
1801 | struct bgp_node *rn, | |
1802 | struct bgp_info **ri) | |
342dd0c6 | 1803 | { |
50f74cf1 | 1804 | struct bgp_info *tmp_ri; |
342dd0c6 | 1805 | |
1806 | *ri = NULL; | |
1807 | ||
50f74cf1 | 1808 | /* Now, find matching route. */ |
342dd0c6 | 1809 | for (tmp_ri = rn->info; tmp_ri; tmp_ri = tmp_ri->next) |
50f74cf1 | 1810 | if (tmp_ri->peer == bgp->peer_self |
342dd0c6 | 1811 | && tmp_ri->type == ZEBRA_ROUTE_BGP |
1812 | && tmp_ri->sub_type == BGP_ROUTE_STATIC) | |
1813 | break; | |
1814 | ||
1815 | *ri = tmp_ri; | |
1816 | ||
1817 | /* Mark route for delete. */ | |
1818 | if (tmp_ri) | |
1819 | bgp_info_delete(rn, tmp_ri); | |
1820 | } | |
1821 | ||
50f74cf1 | 1822 | |
1823 | ||
1824 | /* Delete EVPN ES (type-4) route */ | |
1825 | static int delete_evpn_type4_route(struct bgp *bgp, | |
1826 | struct evpnes *es, | |
1827 | struct prefix_evpn *p) | |
1828 | { | |
1829 | afi_t afi = AFI_L2VPN; | |
1830 | safi_t safi = SAFI_EVPN; | |
1831 | struct bgp_info *ri; | |
1832 | struct bgp_node *rn = NULL; /* rn in esi table */ | |
1833 | struct bgp_node *global_rn = NULL; /* rn in global table */ | |
1834 | ||
1835 | /* First, locate the route node within the ESI. | |
1836 | * If it doesn't exist, ther is nothing to do. | |
1837 | * Note: there is no RD here. | |
1838 | */ | |
1839 | rn = bgp_node_lookup(es->route_table, (struct prefix *)p); | |
2bb9eff4 | 1840 | if (!rn) |
50f74cf1 | 1841 | return 0; |
1842 | ||
1843 | /* Next, locate route node in the global EVPN routing table. | |
1844 | * Note that this table is a 2-level tree (RD-level + Prefix-level) | |
1845 | */ | |
1846 | global_rn = bgp_afi_node_lookup(bgp->rib[afi][safi], afi, safi, | |
1847 | (struct prefix *)p, &es->prd); | |
1848 | if (global_rn) { | |
1849 | ||
1850 | /* Delete route entry in the global EVPN table. */ | |
1851 | delete_evpn_route_entry(bgp, afi, safi, | |
1852 | global_rn, &ri); | |
1853 | ||
1854 | /* Schedule for processing - withdraws to peers happen from | |
1855 | * this table. | |
1856 | */ | |
1857 | if (ri) | |
1858 | bgp_process(bgp, global_rn, afi, safi); | |
1859 | bgp_unlock_node(global_rn); | |
1860 | } | |
1861 | ||
1862 | /* | |
1863 | * Delete route entry in the ESI route table. | |
1864 | * This can just be removed. | |
1865 | */ | |
1866 | delete_evpn_route_entry(bgp, afi, safi, rn, &ri); | |
1867 | if (ri) | |
1868 | bgp_info_reap(rn, ri); | |
1869 | bgp_unlock_node(rn); | |
1870 | return 0; | |
1871 | } | |
1872 | ||
342dd0c6 | 1873 | /* Delete EVPN type5 route */ |
996c9314 | 1874 | static int delete_evpn_type5_route(struct bgp *bgp_vrf, struct prefix_evpn *evp) |
342dd0c6 | 1875 | { |
1876 | afi_t afi = AFI_L2VPN; | |
1877 | safi_t safi = SAFI_EVPN; | |
1878 | struct bgp_node *rn = NULL; | |
1879 | struct bgp_info *ri = NULL; | |
1880 | struct bgp *bgp_def = NULL; /* default bgp instance */ | |
1881 | ||
1882 | bgp_def = bgp_get_default(); | |
1883 | if (!bgp_def) | |
faafdfa8 | 1884 | return 0; |
342dd0c6 | 1885 | |
1886 | /* locate the global route entry for this type-5 prefix */ | |
1887 | rn = bgp_afi_node_lookup(bgp_def->rib[afi][safi], afi, safi, | |
1888 | (struct prefix *)evp, &bgp_vrf->vrf_prd); | |
1889 | if (!rn) | |
1890 | return 0; | |
1891 | ||
50f74cf1 | 1892 | delete_evpn_route_entry(bgp_def, afi, safi, rn, &ri); |
342dd0c6 | 1893 | if (ri) |
1894 | bgp_process(bgp_def, rn, afi, safi); | |
1895 | bgp_unlock_node(rn); | |
1896 | return 0; | |
1897 | } | |
1898 | ||
128ea8ab | 1899 | /* |
1900 | * Delete EVPN route (of type based on prefix) for specified VNI and | |
1901 | * schedule for processing. | |
1902 | */ | |
d62a17ae | 1903 | static int delete_evpn_route(struct bgp *bgp, struct bgpevpn *vpn, |
1904 | struct prefix_evpn *p) | |
1905 | { | |
1906 | struct bgp_node *rn, *global_rn; | |
1907 | struct bgp_info *ri; | |
1908 | afi_t afi = AFI_L2VPN; | |
1909 | safi_t safi = SAFI_EVPN; | |
1910 | ||
1911 | /* First, locate the route node within the VNI. If it doesn't exist, | |
1912 | * there | |
1913 | * is nothing further to do. | |
1914 | */ | |
1915 | /* NOTE: There is no RD here. */ | |
1916 | rn = bgp_node_lookup(vpn->route_table, (struct prefix *)p); | |
1917 | if (!rn) | |
1918 | return 0; | |
1919 | ||
1920 | /* Next, locate route node in the global EVPN routing table. Note that | |
1921 | * this table is a 2-level tree (RD-level + Prefix-level) similar to | |
1922 | * L3VPN routes. | |
1923 | */ | |
1924 | global_rn = bgp_afi_node_lookup(bgp->rib[afi][safi], afi, safi, | |
1925 | (struct prefix *)p, &vpn->prd); | |
1926 | if (global_rn) { | |
1927 | /* Delete route entry in the global EVPN table. */ | |
50f74cf1 | 1928 | delete_evpn_route_entry(bgp, afi, safi, global_rn, &ri); |
d62a17ae | 1929 | |
1930 | /* Schedule for processing - withdraws to peers happen from | |
1931 | * this table. | |
1932 | */ | |
1933 | if (ri) | |
1934 | bgp_process(bgp, global_rn, afi, safi); | |
1935 | bgp_unlock_node(global_rn); | |
1936 | } | |
1937 | ||
1938 | /* Delete route entry in the VNI route table. This can just be removed. | |
1939 | */ | |
50f74cf1 | 1940 | delete_evpn_route_entry(bgp, afi, safi, rn, &ri); |
d62a17ae | 1941 | if (ri) |
1942 | bgp_info_reap(rn, ri); | |
1943 | bgp_unlock_node(rn); | |
1944 | ||
1945 | return 0; | |
128ea8ab | 1946 | } |
1947 | ||
1948 | /* | |
1949 | * Update all type-2 (MACIP) local routes for this VNI - these should also | |
1950 | * be scheduled for advertise to peers. | |
1951 | */ | |
d62a17ae | 1952 | static int update_all_type2_routes(struct bgp *bgp, struct bgpevpn *vpn) |
1953 | { | |
1954 | afi_t afi; | |
1955 | safi_t safi; | |
1956 | struct bgp_node *rn; | |
1957 | struct bgp_info *ri; | |
1958 | struct attr attr; | |
1959 | struct attr attr_sticky; | |
ead40654 | 1960 | struct attr attr_def_gw; |
1ec31309 | 1961 | struct attr attr_ip6_ll; |
d62a17ae | 1962 | struct attr *attr_new; |
1ec31309 | 1963 | int add_l3_ecomm = 0; |
d62a17ae | 1964 | |
1965 | afi = AFI_L2VPN; | |
1966 | safi = SAFI_EVPN; | |
1967 | memset(&attr, 0, sizeof(struct attr)); | |
1968 | memset(&attr_sticky, 0, sizeof(struct attr)); | |
ead40654 | 1969 | memset(&attr_def_gw, 0, sizeof(struct attr)); |
1ec31309 | 1970 | memset(&attr_ip6_ll, 0, sizeof(struct attr)); |
d62a17ae | 1971 | |
1ec31309 | 1972 | /* Build path-attribute - multiple type-2 routes for this VNI will share |
1973 | * the same path attribute, but we need separate structures for sticky | |
1974 | * MACs, default gateway and IPv6 link-local addresses (no L3 RT/RMAC). | |
d62a17ae | 1975 | */ |
1976 | bgp_attr_default_set(&attr, BGP_ORIGIN_IGP); | |
1977 | bgp_attr_default_set(&attr_sticky, BGP_ORIGIN_IGP); | |
ead40654 | 1978 | bgp_attr_default_set(&attr_def_gw, BGP_ORIGIN_IGP); |
d62a17ae | 1979 | attr.nexthop = vpn->originator_ip; |
1980 | attr.mp_nexthop_global_in = vpn->originator_ip; | |
1981 | attr.mp_nexthop_len = BGP_ATTR_NHLEN_IPV4; | |
bc59a672 | 1982 | bgpevpn_get_rmac(vpn, &attr.rmac); |
d62a17ae | 1983 | attr_sticky.nexthop = vpn->originator_ip; |
1984 | attr_sticky.mp_nexthop_global_in = vpn->originator_ip; | |
1985 | attr_sticky.mp_nexthop_len = BGP_ATTR_NHLEN_IPV4; | |
1986 | attr_sticky.sticky = 1; | |
bc59a672 | 1987 | bgpevpn_get_rmac(vpn, &attr_sticky.rmac); |
ead40654 MK |
1988 | attr_def_gw.nexthop = vpn->originator_ip; |
1989 | attr_def_gw.mp_nexthop_global_in = vpn->originator_ip; | |
1990 | attr_def_gw.mp_nexthop_len = BGP_ATTR_NHLEN_IPV4; | |
1991 | attr_def_gw.default_gw = 1; | |
1992 | bgpevpn_get_rmac(vpn, &attr_def_gw.rmac); | |
1ec31309 | 1993 | bgp_attr_default_set(&attr_ip6_ll, BGP_ORIGIN_IGP); |
1994 | attr_ip6_ll.nexthop = vpn->originator_ip; | |
1995 | attr_ip6_ll.mp_nexthop_global_in = vpn->originator_ip; | |
1996 | attr_ip6_ll.mp_nexthop_len = BGP_ATTR_NHLEN_IPV4; | |
1997 | ||
1998 | /* Add L3 VNI RTs and RMAC for non IPv6 link-local attributes if | |
1999 | * using L3 VNI for type-2 routes also. | |
2000 | */ | |
148b548c | 2001 | if (CHECK_FLAG(vpn->flags, VNI_FLAG_USE_TWO_LABELS) && |
2002 | bgpevpn_get_l3vni(vpn)) | |
1ec31309 | 2003 | add_l3_ecomm = 1; |
2004 | ||
2005 | build_evpn_route_extcomm(vpn, &attr, add_l3_ecomm); | |
2006 | build_evpn_route_extcomm(vpn, &attr_sticky, add_l3_ecomm); | |
2007 | build_evpn_route_extcomm(vpn, &attr_def_gw, add_l3_ecomm); | |
2008 | build_evpn_route_extcomm(vpn, &attr_ip6_ll, 0); | |
d62a17ae | 2009 | |
2010 | /* Walk this VNI's route table and update local type-2 routes. For any | |
2011 | * routes updated, update corresponding entry in the global table too. | |
2012 | */ | |
2013 | for (rn = bgp_table_top(vpn->route_table); rn; | |
2014 | rn = bgp_route_next(rn)) { | |
2015 | struct prefix_evpn *evp = (struct prefix_evpn *)&rn->p; | |
2016 | struct bgp_node *rd_rn; | |
2017 | struct bgp_info *global_ri; | |
2018 | ||
2019 | if (evp->prefix.route_type != BGP_EVPN_MAC_IP_ROUTE) | |
2020 | continue; | |
2021 | ||
3714a385 | 2022 | if (is_evpn_prefix_ipaddr_v6(evp) && |
2023 | IN6_IS_ADDR_LINKLOCAL(&evp->prefix.macip_addr.ip.ipaddr_v6)) | |
1ec31309 | 2024 | update_evpn_route_entry(bgp, vpn, afi, safi, rn, |
2025 | &attr_ip6_ll, 0, 1, &ri, 0); | |
2026 | else { | |
7ec156a9 MK |
2027 | if (evpn_route_is_sticky(bgp, rn)) |
2028 | update_evpn_route_entry(bgp, vpn, afi, safi, rn, | |
996c9314 LB |
2029 | &attr_sticky, 0, 1, &ri, |
2030 | 0); | |
68e33151 CS |
2031 | else if (evpn_route_is_def_gw(bgp, rn)) { |
2032 | if (is_evpn_prefix_ipaddr_v6(evp)) | |
2033 | attr_def_gw.router_flag = 1; | |
ead40654 | 2034 | update_evpn_route_entry(bgp, vpn, afi, safi, rn, |
996c9314 LB |
2035 | &attr_def_gw, 0, 1, &ri, |
2036 | 0); | |
68e33151 | 2037 | } else |
7ec156a9 MK |
2038 | update_evpn_route_entry(bgp, vpn, afi, safi, rn, |
2039 | &attr, 0, 1, &ri, 0); | |
7ec156a9 | 2040 | } |
d62a17ae | 2041 | |
2042 | /* If a local route exists for this prefix, we need to update | |
2043 | * the global routing table too. | |
2044 | */ | |
2045 | if (!ri) | |
2046 | continue; | |
2047 | ||
2048 | /* Perform route selection; this is just to set the flags | |
2049 | * correctly | |
2050 | * as local route in the VNI always wins. | |
2051 | */ | |
2052 | evpn_route_select_install(bgp, vpn, rn); | |
2053 | ||
2054 | attr_new = ri->attr; | |
2055 | ||
2056 | /* Update route in global routing table. */ | |
2057 | rd_rn = bgp_afi_node_get(bgp->rib[afi][safi], afi, safi, | |
2058 | (struct prefix *)evp, &vpn->prd); | |
2059 | assert(rd_rn); | |
2060 | update_evpn_route_entry(bgp, vpn, afi, safi, rd_rn, attr_new, 0, | |
1a98c087 | 2061 | 0, &global_ri, 0); |
d62a17ae | 2062 | |
2063 | /* Schedule for processing and unlock node. */ | |
2064 | bgp_process(bgp, rd_rn, afi, safi); | |
2065 | bgp_unlock_node(rd_rn); | |
2066 | } | |
2067 | ||
2068 | /* Unintern temporary. */ | |
2069 | aspath_unintern(&attr.aspath); | |
2070 | aspath_unintern(&attr_sticky.aspath); | |
ead40654 | 2071 | aspath_unintern(&attr_def_gw.aspath); |
1ec31309 | 2072 | aspath_unintern(&attr_ip6_ll.aspath); |
d62a17ae | 2073 | |
2074 | return 0; | |
128ea8ab | 2075 | } |
2076 | ||
2077 | /* | |
2078 | * Delete all type-2 (MACIP) local routes for this VNI - only from the | |
2079 | * global routing table. These are also scheduled for withdraw from peers. | |
2080 | */ | |
d62a17ae | 2081 | static int delete_global_type2_routes(struct bgp *bgp, struct bgpevpn *vpn) |
128ea8ab | 2082 | { |
d62a17ae | 2083 | afi_t afi; |
2084 | safi_t safi; | |
2085 | struct bgp_node *rdrn, *rn; | |
2086 | struct bgp_table *table; | |
2087 | struct bgp_info *ri; | |
128ea8ab | 2088 | |
d62a17ae | 2089 | afi = AFI_L2VPN; |
2090 | safi = SAFI_EVPN; | |
128ea8ab | 2091 | |
d62a17ae | 2092 | rdrn = bgp_node_lookup(bgp->rib[afi][safi], (struct prefix *)&vpn->prd); |
2093 | if (rdrn && rdrn->info) { | |
2094 | table = (struct bgp_table *)rdrn->info; | |
2095 | for (rn = bgp_table_top(table); rn; rn = bgp_route_next(rn)) { | |
2096 | struct prefix_evpn *evp = (struct prefix_evpn *)&rn->p; | |
128ea8ab | 2097 | |
d62a17ae | 2098 | if (evp->prefix.route_type != BGP_EVPN_MAC_IP_ROUTE) |
2099 | continue; | |
128ea8ab | 2100 | |
50f74cf1 | 2101 | delete_evpn_route_entry(bgp, afi, safi, rn, &ri); |
d62a17ae | 2102 | if (ri) |
2103 | bgp_process(bgp, rn, afi, safi); | |
2104 | } | |
2105 | } | |
128ea8ab | 2106 | |
d62a17ae | 2107 | /* Unlock RD node. */ |
2108 | if (rdrn) | |
2109 | bgp_unlock_node(rdrn); | |
128ea8ab | 2110 | |
d62a17ae | 2111 | return 0; |
128ea8ab | 2112 | } |
2113 | ||
2114 | /* | |
2115 | * Delete all type-2 (MACIP) local routes for this VNI - from the global | |
2116 | * table as well as the per-VNI route table. | |
2117 | */ | |
d62a17ae | 2118 | static int delete_all_type2_routes(struct bgp *bgp, struct bgpevpn *vpn) |
128ea8ab | 2119 | { |
d62a17ae | 2120 | afi_t afi; |
2121 | safi_t safi; | |
2122 | struct bgp_node *rn; | |
2123 | struct bgp_info *ri; | |
128ea8ab | 2124 | |
d62a17ae | 2125 | afi = AFI_L2VPN; |
2126 | safi = SAFI_EVPN; | |
128ea8ab | 2127 | |
d62a17ae | 2128 | /* First, walk the global route table for this VNI's type-2 local |
2129 | * routes. | |
2130 | * EVPN routes are a 2-level table, first get the RD table. | |
2131 | */ | |
2132 | delete_global_type2_routes(bgp, vpn); | |
128ea8ab | 2133 | |
d62a17ae | 2134 | /* Next, walk this VNI's route table and delete local type-2 routes. */ |
2135 | for (rn = bgp_table_top(vpn->route_table); rn; | |
2136 | rn = bgp_route_next(rn)) { | |
2137 | struct prefix_evpn *evp = (struct prefix_evpn *)&rn->p; | |
128ea8ab | 2138 | |
d62a17ae | 2139 | if (evp->prefix.route_type != BGP_EVPN_MAC_IP_ROUTE) |
2140 | continue; | |
128ea8ab | 2141 | |
50f74cf1 | 2142 | delete_evpn_route_entry(bgp, afi, safi, rn, &ri); |
128ea8ab | 2143 | |
d62a17ae | 2144 | /* Route entry in local table gets deleted immediately. */ |
2145 | if (ri) | |
2146 | bgp_info_reap(rn, ri); | |
2147 | } | |
128ea8ab | 2148 | |
d62a17ae | 2149 | return 0; |
128ea8ab | 2150 | } |
2151 | ||
50f74cf1 | 2152 | /* |
2153 | * Delete all routes in per ES route-table | |
2154 | */ | |
2155 | static int delete_all_es_routes(struct bgp *bgp, struct evpnes *es) | |
2156 | { | |
2157 | struct bgp_node *rn; | |
2158 | struct bgp_info *ri, *nextri; | |
2159 | ||
2160 | /* Walk this ES's route table and delete all routes. */ | |
2161 | for (rn = bgp_table_top(es->route_table); rn; | |
2162 | rn = bgp_route_next(rn)) { | |
2163 | for (ri = rn->info; (ri != NULL) && (nextri = ri->next, 1); | |
2164 | ri = nextri) { | |
2165 | bgp_info_delete(rn, ri); | |
2166 | bgp_info_reap(rn, ri); | |
2167 | } | |
2168 | } | |
2169 | ||
2170 | return 0; | |
2171 | } | |
2172 | ||
128ea8ab | 2173 | /* |
2174 | * Delete all routes in the per-VNI route table. | |
2175 | */ | |
d62a17ae | 2176 | static int delete_all_vni_routes(struct bgp *bgp, struct bgpevpn *vpn) |
128ea8ab | 2177 | { |
d62a17ae | 2178 | struct bgp_node *rn; |
2179 | struct bgp_info *ri, *nextri; | |
128ea8ab | 2180 | |
d62a17ae | 2181 | /* Walk this VNI's route table and delete all routes. */ |
2182 | for (rn = bgp_table_top(vpn->route_table); rn; | |
2183 | rn = bgp_route_next(rn)) { | |
2184 | for (ri = rn->info; (ri != NULL) && (nextri = ri->next, 1); | |
2185 | ri = nextri) { | |
2186 | bgp_info_delete(rn, ri); | |
2187 | bgp_info_reap(rn, ri); | |
2188 | } | |
2189 | } | |
128ea8ab | 2190 | |
d62a17ae | 2191 | return 0; |
128ea8ab | 2192 | } |
2193 | ||
2194 | /* | |
2195 | * Update (and advertise) local routes for a VNI. Invoked upon the VNI | |
2196 | * export RT getting modified or change to tunnel IP. Note that these | |
2197 | * situations need the route in the per-VNI table as well as the global | |
2198 | * table to be updated (as attributes change). | |
2199 | */ | |
d62a17ae | 2200 | static int update_routes_for_vni(struct bgp *bgp, struct bgpevpn *vpn) |
128ea8ab | 2201 | { |
d62a17ae | 2202 | int ret; |
2203 | struct prefix_evpn p; | |
128ea8ab | 2204 | |
d62a17ae | 2205 | /* Update and advertise the type-3 route (only one) followed by the |
2206 | * locally learnt type-2 routes (MACIP) - for this VNI. | |
2207 | */ | |
2208 | build_evpn_type3_prefix(&p, vpn->originator_ip); | |
2209 | ret = update_evpn_route(bgp, vpn, &p, 0); | |
2210 | if (ret) | |
2211 | return ret; | |
128ea8ab | 2212 | |
d62a17ae | 2213 | return update_all_type2_routes(bgp, vpn); |
128ea8ab | 2214 | } |
2215 | ||
50f74cf1 | 2216 | /* Delete (and withdraw) local routes for specified ES from global and ES table. |
2217 | * Also remove all other routes from the per ES table. | |
2218 | * Invoked when ES is deleted. | |
2219 | */ | |
2220 | static int delete_routes_for_es(struct bgp *bgp, struct evpnes *es) | |
2221 | { | |
2222 | int ret; | |
2223 | char buf[ESI_STR_LEN]; | |
2224 | struct prefix_evpn p; | |
2225 | ||
2226 | /* Delete and withdraw locally learnt ES route */ | |
2227 | build_evpn_type4_prefix(&p, &es->esi, es->originator_ip.ipaddr_v4); | |
2228 | ret = delete_evpn_type4_route(bgp, es, &p); | |
2229 | if (ret) { | |
af4c2728 | 2230 | flog_err(BGP_ERR_EVPN_ROUTE_DELETE, |
14454c9f DS |
2231 | "%u failed to delete type-4 route for ESI %s", |
2232 | bgp->vrf_id, esi_to_str(&es->esi, buf, sizeof(buf))); | |
50f74cf1 | 2233 | } |
2234 | ||
2235 | /* Delete all routes from per ES table */ | |
2236 | return delete_all_es_routes(bgp, es); | |
2237 | } | |
2238 | ||
128ea8ab | 2239 | /* |
2240 | * Delete (and withdraw) local routes for specified VNI from the global | |
2241 | * table and per-VNI table. After this, remove all other routes from | |
2242 | * the per-VNI table. Invoked upon the VNI being deleted or EVPN | |
2243 | * (advertise-all-vni) being disabled. | |
2244 | */ | |
d62a17ae | 2245 | static int delete_routes_for_vni(struct bgp *bgp, struct bgpevpn *vpn) |
128ea8ab | 2246 | { |
d62a17ae | 2247 | int ret; |
2248 | struct prefix_evpn p; | |
128ea8ab | 2249 | |
d62a17ae | 2250 | /* Delete and withdraw locally learnt type-2 routes (MACIP) |
2251 | * followed by type-3 routes (only one) - for this VNI. | |
2252 | */ | |
2253 | ret = delete_all_type2_routes(bgp, vpn); | |
2254 | if (ret) | |
2255 | return ret; | |
128ea8ab | 2256 | |
d62a17ae | 2257 | build_evpn_type3_prefix(&p, vpn->originator_ip); |
2258 | ret = delete_evpn_route(bgp, vpn, &p); | |
2259 | if (ret) | |
2260 | return ret; | |
128ea8ab | 2261 | |
d62a17ae | 2262 | /* Delete all routes from the per-VNI table. */ |
2263 | return delete_all_vni_routes(bgp, vpn); | |
128ea8ab | 2264 | } |
2265 | ||
2266 | /* | |
d1911c26 | 2267 | * There is a tunnel endpoint IP address change for this VNI, delete |
2268 | * prior type-3 route (if needed) and update. | |
2269 | * Note: Route re-advertisement happens elsewhere after other processing | |
2270 | * other changes. | |
128ea8ab | 2271 | */ |
d62a17ae | 2272 | static int handle_tunnel_ip_change(struct bgp *bgp, struct bgpevpn *vpn, |
2273 | struct in_addr originator_ip) | |
128ea8ab | 2274 | { |
d62a17ae | 2275 | struct prefix_evpn p; |
128ea8ab | 2276 | |
ddd16ed5 MK |
2277 | /* If VNI is not live, we only need to update the originator ip */ |
2278 | if (!is_vni_live(vpn)) { | |
2279 | vpn->originator_ip = originator_ip; | |
2280 | return 0; | |
2281 | } | |
2282 | ||
db0e1937 MK |
2283 | /* Update the tunnel-ip hash */ |
2284 | bgp_tip_del(bgp, &vpn->originator_ip); | |
2285 | bgp_tip_add(bgp, &originator_ip); | |
2286 | ||
2287 | /* filter routes as martian nexthop db has changed */ | |
2288 | bgp_filter_evpn_routes_upon_martian_nh_change(bgp); | |
2289 | ||
d62a17ae | 2290 | /* Need to withdraw type-3 route as the originator IP is part |
2291 | * of the key. | |
2292 | */ | |
2293 | build_evpn_type3_prefix(&p, vpn->originator_ip); | |
2294 | delete_evpn_route(bgp, vpn, &p); | |
128ea8ab | 2295 | |
d62a17ae | 2296 | /* Update the tunnel IP and re-advertise all routes for this VNI. */ |
2297 | vpn->originator_ip = originator_ip; | |
d1911c26 | 2298 | return 0; |
128ea8ab | 2299 | } |
2300 | ||
50f74cf1 | 2301 | /* Install EVPN route entry in ES */ |
2302 | static int install_evpn_route_entry_in_es(struct bgp *bgp, | |
2303 | struct evpnes *es, | |
2304 | struct prefix_evpn *p, | |
2305 | struct bgp_info *parent_ri) | |
2306 | { | |
2307 | int ret = 0; | |
2308 | struct bgp_node *rn = NULL; | |
2309 | struct bgp_info *ri = NULL; | |
2310 | struct attr *attr_new = NULL; | |
2311 | ||
2312 | /* Create (or fetch) route within the VNI. | |
2313 | * NOTE: There is no RD here. | |
2314 | */ | |
2315 | rn = bgp_node_get(es->route_table, (struct prefix *)p); | |
2316 | ||
2317 | /* Check if route entry is already present. */ | |
2318 | for (ri = rn->info; ri; ri = ri->next) | |
2319 | if (ri->extra && | |
2320 | (struct bgp_info *)ri->extra->parent == parent_ri) | |
2321 | break; | |
2322 | ||
2323 | if (!ri) { | |
2324 | /* Add (or update) attribute to hash. */ | |
2325 | attr_new = bgp_attr_intern(parent_ri->attr); | |
2326 | ||
2327 | /* Create new route with its attribute. */ | |
2328 | ri = info_make(parent_ri->type, BGP_ROUTE_IMPORTED, 0, | |
2329 | parent_ri->peer, attr_new, rn); | |
2330 | SET_FLAG(ri->flags, BGP_INFO_VALID); | |
2331 | bgp_info_extra_get(ri); | |
2332 | ri->extra->parent = parent_ri; | |
2333 | bgp_info_add(rn, ri); | |
2334 | } else { | |
2335 | if (attrhash_cmp(ri->attr, parent_ri->attr) | |
2336 | && !CHECK_FLAG(ri->flags, BGP_INFO_REMOVED)) { | |
2337 | bgp_unlock_node(rn); | |
2338 | return 0; | |
2339 | } | |
2340 | /* The attribute has changed. */ | |
2341 | /* Add (or update) attribute to hash. */ | |
2342 | attr_new = bgp_attr_intern(parent_ri->attr); | |
2343 | ||
2344 | /* Restore route, if needed. */ | |
2345 | if (CHECK_FLAG(ri->flags, BGP_INFO_REMOVED)) | |
2346 | bgp_info_restore(rn, ri); | |
2347 | ||
2348 | /* Mark if nexthop has changed. */ | |
2349 | if (!IPV4_ADDR_SAME(&ri->attr->nexthop, &attr_new->nexthop)) | |
2350 | SET_FLAG(ri->flags, BGP_INFO_IGP_CHANGED); | |
2351 | ||
2352 | /* Unintern existing, set to new. */ | |
2353 | bgp_attr_unintern(&ri->attr); | |
2354 | ri->attr = attr_new; | |
2355 | ri->uptime = bgp_clock(); | |
2356 | } | |
2357 | ||
2358 | /* Perform route selection and update zebra, if required. */ | |
2359 | ret = evpn_es_route_select_install(bgp, es, rn); | |
2360 | return ret; | |
2361 | } | |
2362 | ||
d3135ba3 | 2363 | /* |
2364 | * Install route entry into the VRF routing table and invoke route selection. | |
2365 | */ | |
2366 | static int install_evpn_route_entry_in_vrf(struct bgp *bgp_vrf, | |
2367 | struct prefix_evpn *evp, | |
2368 | struct bgp_info *parent_ri) | |
2369 | { | |
2370 | struct bgp_node *rn; | |
2371 | struct bgp_info *ri; | |
1ec31309 | 2372 | struct attr attr; |
d3135ba3 | 2373 | struct attr *attr_new; |
c4edf708 | 2374 | int ret = 0; |
d3135ba3 | 2375 | struct prefix p; |
2376 | struct prefix *pp = &p; | |
2377 | afi_t afi = 0; | |
2378 | safi_t safi = 0; | |
1eb88002 MK |
2379 | char buf[PREFIX_STRLEN]; |
2380 | char buf1[PREFIX_STRLEN]; | |
d3135ba3 | 2381 | |
2382 | memset(pp, 0, sizeof(struct prefix)); | |
3714a385 | 2383 | ip_prefix_from_evpn_prefix(evp, pp); |
d3135ba3 | 2384 | |
1eb88002 | 2385 | if (bgp_debug_zebra(NULL)) { |
996c9314 LB |
2386 | zlog_debug( |
2387 | "installing evpn prefix %s as ip prefix %s in vrf %s", | |
2388 | prefix2str(evp, buf, sizeof(buf)), | |
2389 | prefix2str(pp, buf1, sizeof(buf)), | |
2390 | vrf_id_to_name(bgp_vrf->vrf_id)); | |
1eb88002 MK |
2391 | } |
2392 | ||
d3135ba3 | 2393 | /* Create (or fetch) route within the VRF. */ |
2394 | /* NOTE: There is no RD here. */ | |
3714a385 | 2395 | if (is_evpn_prefix_ipaddr_v4(evp)) { |
d3135ba3 | 2396 | afi = AFI_IP; |
2397 | safi = SAFI_UNICAST; | |
2398 | rn = bgp_node_get(bgp_vrf->rib[afi][safi], pp); | |
3714a385 | 2399 | } else if (is_evpn_prefix_ipaddr_v6(evp)) { |
d3135ba3 | 2400 | afi = AFI_IP6; |
2401 | safi = SAFI_UNICAST; | |
2402 | rn = bgp_node_get(bgp_vrf->rib[afi][safi], pp); | |
2403 | } else | |
2404 | return 0; | |
2405 | ||
1ec31309 | 2406 | /* EVPN routes currently only support a IPv4 next hop which corresponds |
2407 | * to the remote VTEP. When importing into a VRF, if it is IPv6 host | |
450e362d | 2408 | * or prefix route, we have to convert the next hop to an IPv4-mapped |
2409 | * address for the rest of the code to flow through. In the case of IPv4, | |
2410 | * make sure to set the flag for next hop attribute. | |
1ec31309 | 2411 | */ |
2412 | bgp_attr_dup(&attr, parent_ri->attr); | |
2413 | if (afi == AFI_IP6) | |
2414 | evpn_convert_nexthop_to_ipv6(&attr); | |
450e362d | 2415 | else |
2416 | attr.flag |= ATTR_FLAG_BIT(BGP_ATTR_NEXT_HOP); | |
1ec31309 | 2417 | |
d3135ba3 | 2418 | /* Check if route entry is already present. */ |
2419 | for (ri = rn->info; ri; ri = ri->next) | |
2420 | if (ri->extra | |
2421 | && (struct bgp_info *)ri->extra->parent == parent_ri) | |
2422 | break; | |
2423 | ||
2424 | if (!ri) { | |
2425 | /* Add (or update) attribute to hash. */ | |
1ec31309 | 2426 | attr_new = bgp_attr_intern(&attr); |
d3135ba3 | 2427 | |
2428 | /* Create new route with its attribute. */ | |
90f4f482 | 2429 | ri = info_make(parent_ri->type, BGP_ROUTE_IMPORTED, 0, |
d3135ba3 | 2430 | parent_ri->peer, attr_new, rn); |
2431 | SET_FLAG(ri->flags, BGP_INFO_VALID); | |
2432 | bgp_info_extra_get(ri); | |
528cd74f | 2433 | ri->extra->parent = bgp_info_lock(parent_ri); |
7c82b312 | 2434 | bgp_lock_node((struct bgp_node *)parent_ri->net); |
b57ba6d2 | 2435 | if (parent_ri->extra) { |
d3135ba3 | 2436 | memcpy(&ri->extra->label, &parent_ri->extra->label, |
b57ba6d2 MK |
2437 | sizeof(ri->extra->label)); |
2438 | ri->extra->num_labels = parent_ri->extra->num_labels; | |
2439 | } | |
d3135ba3 | 2440 | bgp_info_add(rn, ri); |
2441 | } else { | |
1ec31309 | 2442 | if (attrhash_cmp(ri->attr, &attr) |
d3135ba3 | 2443 | && !CHECK_FLAG(ri->flags, BGP_INFO_REMOVED)) { |
2444 | bgp_unlock_node(rn); | |
2445 | return 0; | |
2446 | } | |
2447 | /* The attribute has changed. */ | |
2448 | /* Add (or update) attribute to hash. */ | |
1ec31309 | 2449 | attr_new = bgp_attr_intern(&attr); |
d3135ba3 | 2450 | |
2451 | /* Restore route, if needed. */ | |
2452 | if (CHECK_FLAG(ri->flags, BGP_INFO_REMOVED)) | |
2453 | bgp_info_restore(rn, ri); | |
2454 | ||
2455 | /* Mark if nexthop has changed. */ | |
1ec31309 | 2456 | if ((afi == AFI_IP && |
2457 | !IPV4_ADDR_SAME(&ri->attr->nexthop, &attr_new->nexthop)) || | |
2458 | (afi == AFI_IP6 && | |
2459 | !IPV6_ADDR_SAME(&ri->attr->mp_nexthop_global, | |
2460 | &attr_new->mp_nexthop_global))) | |
d3135ba3 | 2461 | SET_FLAG(ri->flags, BGP_INFO_IGP_CHANGED); |
2462 | ||
2463 | /* Unintern existing, set to new. */ | |
2464 | bgp_attr_unintern(&ri->attr); | |
2465 | ri->attr = attr_new; | |
2466 | ri->uptime = bgp_clock(); | |
2467 | } | |
2468 | ||
2469 | /* Perform route selection and update zebra, if required. */ | |
1eb88002 | 2470 | bgp_process(bgp_vrf, rn, afi, safi); |
d3135ba3 | 2471 | |
2472 | return ret; | |
2473 | } | |
2474 | ||
128ea8ab | 2475 | /* |
2476 | * Install route entry into the VNI routing table and invoke route selection. | |
2477 | */ | |
d62a17ae | 2478 | static int install_evpn_route_entry(struct bgp *bgp, struct bgpevpn *vpn, |
2479 | struct prefix_evpn *p, | |
2480 | struct bgp_info *parent_ri) | |
2481 | { | |
2482 | struct bgp_node *rn; | |
2483 | struct bgp_info *ri; | |
2484 | struct attr *attr_new; | |
2485 | int ret; | |
2486 | ||
2487 | /* Create (or fetch) route within the VNI. */ | |
2488 | /* NOTE: There is no RD here. */ | |
2489 | rn = bgp_node_get(vpn->route_table, (struct prefix *)p); | |
2490 | ||
2491 | /* Check if route entry is already present. */ | |
2492 | for (ri = rn->info; ri; ri = ri->next) | |
2493 | if (ri->extra | |
2494 | && (struct bgp_info *)ri->extra->parent == parent_ri) | |
2495 | break; | |
2496 | ||
2497 | if (!ri) { | |
2498 | /* Add (or update) attribute to hash. */ | |
2499 | attr_new = bgp_attr_intern(parent_ri->attr); | |
2500 | ||
2501 | /* Create new route with its attribute. */ | |
90f4f482 | 2502 | ri = info_make(parent_ri->type, BGP_ROUTE_IMPORTED, 0, |
d62a17ae | 2503 | parent_ri->peer, attr_new, rn); |
2504 | SET_FLAG(ri->flags, BGP_INFO_VALID); | |
2505 | bgp_info_extra_get(ri); | |
528cd74f | 2506 | ri->extra->parent = bgp_info_lock(parent_ri); |
7c82b312 | 2507 | bgp_lock_node((struct bgp_node *)parent_ri->net); |
b57ba6d2 | 2508 | if (parent_ri->extra) { |
d62a17ae | 2509 | memcpy(&ri->extra->label, &parent_ri->extra->label, |
b57ba6d2 MK |
2510 | sizeof(ri->extra->label)); |
2511 | ri->extra->num_labels = parent_ri->extra->num_labels; | |
2512 | } | |
d62a17ae | 2513 | bgp_info_add(rn, ri); |
2514 | } else { | |
2515 | if (attrhash_cmp(ri->attr, parent_ri->attr) | |
2516 | && !CHECK_FLAG(ri->flags, BGP_INFO_REMOVED)) { | |
2517 | bgp_unlock_node(rn); | |
2518 | return 0; | |
2519 | } | |
2520 | /* The attribute has changed. */ | |
2521 | /* Add (or update) attribute to hash. */ | |
2522 | attr_new = bgp_attr_intern(parent_ri->attr); | |
2523 | ||
50f74cf1 | 2524 | /* Restore route, if needed. */ |
2525 | if (CHECK_FLAG(ri->flags, BGP_INFO_REMOVED)) | |
2526 | bgp_info_restore(rn, ri); | |
2527 | ||
2528 | /* Mark if nexthop has changed. */ | |
2529 | if (!IPV4_ADDR_SAME(&ri->attr->nexthop, &attr_new->nexthop)) | |
2530 | SET_FLAG(ri->flags, BGP_INFO_IGP_CHANGED); | |
2531 | ||
2532 | /* Unintern existing, set to new. */ | |
2533 | bgp_attr_unintern(&ri->attr); | |
2534 | ri->attr = attr_new; | |
2535 | ri->uptime = bgp_clock(); | |
2536 | } | |
2537 | ||
2538 | /* Perform route selection and update zebra, if required. */ | |
2539 | ret = evpn_route_select_install(bgp, vpn, rn); | |
2540 | ||
2541 | return ret; | |
2542 | } | |
2543 | ||
2544 | /* Uninstall EVPN route entry from ES route table */ | |
2545 | static int uninstall_evpn_route_entry_in_es(struct bgp *bgp, | |
2546 | struct evpnes *es, | |
2547 | struct prefix_evpn *p, | |
2548 | struct bgp_info *parent_ri) | |
2549 | { | |
2550 | int ret; | |
2551 | struct bgp_node *rn; | |
2552 | struct bgp_info *ri; | |
2553 | ||
2554 | if (!es->route_table) | |
2555 | return 0; | |
2556 | ||
2557 | /* Locate route within the ESI. | |
2558 | * NOTE: There is no RD here. | |
2559 | */ | |
2560 | rn = bgp_node_lookup(es->route_table, (struct prefix *)p); | |
2561 | if (!rn) | |
2562 | return 0; | |
2563 | ||
2564 | /* Find matching route entry. */ | |
2565 | for (ri = rn->info; ri; ri = ri->next) | |
2566 | if (ri->extra && | |
2567 | (struct bgp_info *)ri->extra->parent == parent_ri) | |
2568 | break; | |
d62a17ae | 2569 | |
50f74cf1 | 2570 | if (!ri) |
2571 | return 0; | |
d62a17ae | 2572 | |
50f74cf1 | 2573 | /* Mark entry for deletion */ |
2574 | bgp_info_delete(rn, ri); | |
d62a17ae | 2575 | |
2576 | /* Perform route selection and update zebra, if required. */ | |
50f74cf1 | 2577 | ret = evpn_es_route_select_install(bgp, es, rn); |
2578 | ||
2579 | /* Unlock route node. */ | |
2580 | bgp_unlock_node(rn); | |
d62a17ae | 2581 | |
2582 | return ret; | |
128ea8ab | 2583 | } |
2584 | ||
d3135ba3 | 2585 | /* |
2586 | * Uninstall route entry from the VRF routing table and send message | |
2587 | * to zebra, if appropriate. | |
2588 | */ | |
2589 | static int uninstall_evpn_route_entry_in_vrf(struct bgp *bgp_vrf, | |
2590 | struct prefix_evpn *evp, | |
2591 | struct bgp_info *parent_ri) | |
2592 | { | |
2593 | struct bgp_node *rn; | |
2594 | struct bgp_info *ri; | |
c4edf708 | 2595 | int ret = 0; |
d3135ba3 | 2596 | struct prefix p; |
2597 | struct prefix *pp = &p; | |
2598 | afi_t afi = 0; | |
2599 | safi_t safi = 0; | |
1eb88002 MK |
2600 | char buf[PREFIX_STRLEN]; |
2601 | char buf1[PREFIX_STRLEN]; | |
d3135ba3 | 2602 | |
2603 | memset(pp, 0, sizeof(struct prefix)); | |
3714a385 | 2604 | ip_prefix_from_evpn_prefix(evp, pp); |
d3135ba3 | 2605 | |
1eb88002 | 2606 | if (bgp_debug_zebra(NULL)) { |
996c9314 LB |
2607 | zlog_debug( |
2608 | "uninstalling evpn prefix %s as ip prefix %s in vrf %s", | |
2609 | prefix2str(evp, buf, sizeof(buf)), | |
2610 | prefix2str(pp, buf1, sizeof(buf)), | |
2611 | vrf_id_to_name(bgp_vrf->vrf_id)); | |
1eb88002 MK |
2612 | } |
2613 | ||
d3135ba3 | 2614 | /* Locate route within the VRF. */ |
2615 | /* NOTE: There is no RD here. */ | |
3714a385 | 2616 | if (is_evpn_prefix_ipaddr_v4(evp)) { |
d3135ba3 | 2617 | afi = AFI_IP; |
2618 | safi = SAFI_UNICAST; | |
2619 | rn = bgp_node_lookup(bgp_vrf->rib[afi][safi], pp); | |
2620 | } else { | |
2621 | afi = AFI_IP6; | |
2622 | safi = SAFI_UNICAST; | |
2623 | rn = bgp_node_lookup(bgp_vrf->rib[afi][safi], pp); | |
2624 | } | |
2625 | ||
2626 | if (!rn) | |
2627 | return 0; | |
2628 | ||
2629 | /* Find matching route entry. */ | |
2630 | for (ri = rn->info; ri; ri = ri->next) | |
2631 | if (ri->extra | |
2632 | && (struct bgp_info *)ri->extra->parent == parent_ri) | |
2633 | break; | |
2634 | ||
2635 | if (!ri) | |
2636 | return 0; | |
2637 | ||
2638 | /* Mark entry for deletion */ | |
2639 | bgp_info_delete(rn, ri); | |
2640 | ||
2641 | /* Perform route selection and update zebra, if required. */ | |
1eb88002 | 2642 | bgp_process(bgp_vrf, rn, afi, safi); |
d3135ba3 | 2643 | |
2644 | /* Unlock route node. */ | |
2645 | bgp_unlock_node(rn); | |
2646 | ||
2647 | return ret; | |
2648 | } | |
2649 | ||
128ea8ab | 2650 | /* |
2651 | * Uninstall route entry from the VNI routing table and send message | |
2652 | * to zebra, if appropriate. | |
2653 | */ | |
d62a17ae | 2654 | static int uninstall_evpn_route_entry(struct bgp *bgp, struct bgpevpn *vpn, |
2655 | struct prefix_evpn *p, | |
2656 | struct bgp_info *parent_ri) | |
128ea8ab | 2657 | { |
d62a17ae | 2658 | struct bgp_node *rn; |
2659 | struct bgp_info *ri; | |
2660 | int ret; | |
128ea8ab | 2661 | |
d62a17ae | 2662 | /* Locate route within the VNI. */ |
2663 | /* NOTE: There is no RD here. */ | |
2664 | rn = bgp_node_lookup(vpn->route_table, (struct prefix *)p); | |
2665 | if (!rn) | |
2666 | return 0; | |
128ea8ab | 2667 | |
d62a17ae | 2668 | /* Find matching route entry. */ |
2669 | for (ri = rn->info; ri; ri = ri->next) | |
2670 | if (ri->extra | |
2671 | && (struct bgp_info *)ri->extra->parent == parent_ri) | |
2672 | break; | |
128ea8ab | 2673 | |
d62a17ae | 2674 | if (!ri) |
2675 | return 0; | |
128ea8ab | 2676 | |
d62a17ae | 2677 | /* Mark entry for deletion */ |
2678 | bgp_info_delete(rn, ri); | |
128ea8ab | 2679 | |
d62a17ae | 2680 | /* Perform route selection and update zebra, if required. */ |
2681 | ret = evpn_route_select_install(bgp, vpn, rn); | |
128ea8ab | 2682 | |
d62a17ae | 2683 | /* Unlock route node. */ |
2684 | bgp_unlock_node(rn); | |
128ea8ab | 2685 | |
d62a17ae | 2686 | return ret; |
128ea8ab | 2687 | } |
2688 | ||
50f74cf1 | 2689 | /* |
2690 | * Given a prefix, see if it belongs to ES. | |
2691 | */ | |
2692 | static int is_prefix_matching_for_es(struct prefix_evpn *p, | |
2693 | struct evpnes *es) | |
2694 | { | |
2695 | /* if not an ES route return false */ | |
2696 | if (p->prefix.route_type != BGP_EVPN_ES_ROUTE) | |
2697 | return 0; | |
2698 | ||
2699 | if (memcmp(&p->prefix.es_addr.esi, &es->esi, sizeof(esi_t)) == 0) | |
2700 | return 1; | |
2701 | ||
2702 | return 0; | |
2703 | } | |
2704 | ||
5ba238b7 MK |
2705 | /* |
2706 | * Given a route entry and a VRF, see if this route entry should be | |
2707 | * imported into the VRF i.e., RTs match. | |
2708 | */ | |
996c9314 | 2709 | static int is_route_matching_for_vrf(struct bgp *bgp_vrf, struct bgp_info *ri) |
5ba238b7 MK |
2710 | { |
2711 | struct attr *attr = ri->attr; | |
2712 | struct ecommunity *ecom; | |
2713 | int i; | |
2714 | ||
2715 | assert(attr); | |
2716 | /* Route should have valid RT to be even considered. */ | |
2717 | if (!(attr->flag & ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES))) | |
2718 | return 0; | |
2719 | ||
2720 | ecom = attr->ecommunity; | |
2721 | if (!ecom || !ecom->size) | |
2722 | return 0; | |
2723 | ||
2724 | /* For each extended community RT, see if it matches this VNI. If any RT | |
2725 | * matches, we're done. | |
2726 | */ | |
2727 | for (i = 0; i < ecom->size; i++) { | |
d7c0a89a QY |
2728 | uint8_t *pnt; |
2729 | uint8_t type, sub_type; | |
5ba238b7 MK |
2730 | struct ecommunity_val *eval; |
2731 | struct ecommunity_val eval_tmp; | |
2732 | struct vrf_irt_node *irt; | |
2733 | ||
2734 | /* Only deal with RTs */ | |
2735 | pnt = (ecom->val + (i * ECOMMUNITY_SIZE)); | |
2736 | eval = (struct ecommunity_val *)(ecom->val | |
2737 | + (i * ECOMMUNITY_SIZE)); | |
2738 | type = *pnt++; | |
2739 | sub_type = *pnt++; | |
2740 | if (sub_type != ECOMMUNITY_ROUTE_TARGET) | |
2741 | continue; | |
2742 | ||
2743 | /* See if this RT matches specified VNIs import RTs */ | |
2744 | irt = lookup_vrf_import_rt(eval); | |
5d9cbca2 | 2745 | if (irt) |
5ba238b7 MK |
2746 | if (is_vrf_present_in_irt_vrfs(irt->vrfs, bgp_vrf)) |
2747 | return 1; | |
2748 | ||
2749 | /* Also check for non-exact match. In this, we mask out the AS | |
2750 | * and | |
2751 | * only check on the local-admin sub-field. This is to | |
2752 | * facilitate using | |
2753 | * VNI as the RT for EBGP peering too. | |
2754 | */ | |
2755 | irt = NULL; | |
2756 | if (type == ECOMMUNITY_ENCODE_AS | |
2757 | || type == ECOMMUNITY_ENCODE_AS4 | |
2758 | || type == ECOMMUNITY_ENCODE_IP) { | |
2759 | memcpy(&eval_tmp, eval, ECOMMUNITY_SIZE); | |
2760 | mask_ecom_global_admin(&eval_tmp, eval); | |
2761 | irt = lookup_vrf_import_rt(&eval_tmp); | |
2762 | } | |
5d9cbca2 | 2763 | if (irt) |
5ba238b7 MK |
2764 | if (is_vrf_present_in_irt_vrfs(irt->vrfs, bgp_vrf)) |
2765 | return 1; | |
2766 | } | |
2767 | ||
2768 | return 0; | |
2769 | } | |
2770 | ||
128ea8ab | 2771 | /* |
2772 | * Given a route entry and a VNI, see if this route entry should be | |
2773 | * imported into the VNI i.e., RTs match. | |
2774 | */ | |
d62a17ae | 2775 | static int is_route_matching_for_vni(struct bgp *bgp, struct bgpevpn *vpn, |
2776 | struct bgp_info *ri) | |
2777 | { | |
2778 | struct attr *attr = ri->attr; | |
2779 | struct ecommunity *ecom; | |
2780 | int i; | |
2781 | ||
2782 | assert(attr); | |
2783 | /* Route should have valid RT to be even considered. */ | |
2784 | if (!(attr->flag & ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES))) | |
2785 | return 0; | |
2786 | ||
2787 | ecom = attr->ecommunity; | |
2788 | if (!ecom || !ecom->size) | |
2789 | return 0; | |
2790 | ||
2791 | /* For each extended community RT, see if it matches this VNI. If any RT | |
2792 | * matches, we're done. | |
2793 | */ | |
2794 | for (i = 0; i < ecom->size; i++) { | |
d7c0a89a QY |
2795 | uint8_t *pnt; |
2796 | uint8_t type, sub_type; | |
d62a17ae | 2797 | struct ecommunity_val *eval; |
2798 | struct ecommunity_val eval_tmp; | |
2799 | struct irt_node *irt; | |
2800 | ||
2801 | /* Only deal with RTs */ | |
2802 | pnt = (ecom->val + (i * ECOMMUNITY_SIZE)); | |
2803 | eval = (struct ecommunity_val *)(ecom->val | |
2804 | + (i * ECOMMUNITY_SIZE)); | |
2805 | type = *pnt++; | |
2806 | sub_type = *pnt++; | |
2807 | if (sub_type != ECOMMUNITY_ROUTE_TARGET) | |
2808 | continue; | |
2809 | ||
2810 | /* See if this RT matches specified VNIs import RTs */ | |
2811 | irt = lookup_import_rt(bgp, eval); | |
b1ab0dfe | 2812 | if (irt) |
d62a17ae | 2813 | if (is_vni_present_in_irt_vnis(irt->vnis, vpn)) |
2814 | return 1; | |
2815 | ||
2816 | /* Also check for non-exact match. In this, we mask out the AS | |
2817 | * and | |
2818 | * only check on the local-admin sub-field. This is to | |
2819 | * facilitate using | |
2820 | * VNI as the RT for EBGP peering too. | |
2821 | */ | |
2822 | irt = NULL; | |
2823 | if (type == ECOMMUNITY_ENCODE_AS | |
2824 | || type == ECOMMUNITY_ENCODE_AS4 | |
2825 | || type == ECOMMUNITY_ENCODE_IP) { | |
2826 | memcpy(&eval_tmp, eval, ECOMMUNITY_SIZE); | |
2827 | mask_ecom_global_admin(&eval_tmp, eval); | |
2828 | irt = lookup_import_rt(bgp, &eval_tmp); | |
2829 | } | |
b1ab0dfe | 2830 | if (irt) |
d62a17ae | 2831 | if (is_vni_present_in_irt_vnis(irt->vnis, vpn)) |
2832 | return 1; | |
2833 | } | |
2834 | ||
2835 | return 0; | |
128ea8ab | 2836 | } |
2837 | ||
50f74cf1 | 2838 | static int install_uninstall_routes_for_es(struct bgp *bgp, |
2839 | struct evpnes *es, | |
2840 | int install) | |
2841 | { | |
2842 | int ret; | |
2843 | afi_t afi; | |
2844 | safi_t safi; | |
2845 | char buf[PREFIX_STRLEN]; | |
2846 | char buf1[ESI_STR_LEN]; | |
2847 | struct bgp_node *rd_rn, *rn; | |
2848 | struct bgp_table *table; | |
2849 | struct bgp_info *ri; | |
2850 | ||
2851 | afi = AFI_L2VPN; | |
2852 | safi = SAFI_EVPN; | |
2853 | ||
2bb9eff4 DS |
2854 | /* |
2855 | * Walk entire global routing table and evaluate routes which could be | |
50f74cf1 | 2856 | * imported into this VRF. Note that we need to loop through all global |
2857 | * routes to determine which route matches the import rt on vrf | |
2858 | */ | |
2859 | for (rd_rn = bgp_table_top(bgp->rib[afi][safi]); rd_rn; | |
2860 | rd_rn = bgp_route_next(rd_rn)) { | |
2861 | table = (struct bgp_table *)(rd_rn->info); | |
2862 | if (!table) | |
2863 | continue; | |
2864 | ||
2865 | for (rn = bgp_table_top(table); rn; rn = bgp_route_next(rn)) { | |
2866 | struct prefix_evpn *evp = (struct prefix_evpn *)&rn->p; | |
2867 | ||
2868 | for (ri = rn->info; ri; ri = ri->next) { | |
2bb9eff4 DS |
2869 | /* |
2870 | * Consider "valid" remote routes applicable for | |
50f74cf1 | 2871 | * this ES. |
2872 | */ | |
2873 | if (!(CHECK_FLAG(ri->flags, BGP_INFO_VALID) | |
2874 | && ri->type == ZEBRA_ROUTE_BGP | |
2875 | && ri->sub_type == BGP_ROUTE_NORMAL)) | |
2876 | continue; | |
2877 | ||
2bb9eff4 DS |
2878 | if (!is_prefix_matching_for_es(evp, es)) |
2879 | continue; | |
50f74cf1 | 2880 | |
2bb9eff4 DS |
2881 | if (install) |
2882 | ret = install_evpn_route_entry_in_es( | |
2883 | bgp, es, evp, ri); | |
2884 | else | |
2885 | ret = uninstall_evpn_route_entry_in_es( | |
2886 | bgp, es, evp, ri); | |
2887 | ||
2888 | if (ret) { | |
af4c2728 | 2889 | flog_err( |
14454c9f | 2890 | BGP_ERR_EVPN_FAIL, |
2bb9eff4 DS |
2891 | "Failed to %s EVPN %s route in ESI %s", |
2892 | install ? "install" | |
2893 | : "uninstall", | |
2894 | prefix2str(evp, buf, | |
2895 | sizeof(buf)), | |
2896 | esi_to_str(&es->esi, buf1, | |
2897 | sizeof(buf1))); | |
2898 | return ret; | |
50f74cf1 | 2899 | } |
2900 | } | |
2901 | } | |
2902 | } | |
2903 | return 0; | |
2904 | } | |
2905 | ||
5ba238b7 MK |
2906 | /* |
2907 | * Install or uninstall mac-ip routes are appropriate for this | |
2908 | * particular VRF. | |
2909 | */ | |
996c9314 | 2910 | static int install_uninstall_routes_for_vrf(struct bgp *bgp_vrf, int install) |
5ba238b7 MK |
2911 | { |
2912 | afi_t afi; | |
2913 | safi_t safi; | |
2914 | struct bgp_node *rd_rn, *rn; | |
2915 | struct bgp_table *table; | |
2916 | struct bgp_info *ri; | |
2917 | int ret; | |
2918 | char buf[PREFIX_STRLEN]; | |
2919 | struct bgp *bgp_def = NULL; | |
2920 | ||
2921 | afi = AFI_L2VPN; | |
2922 | safi = SAFI_EVPN; | |
2923 | bgp_def = bgp_get_default(); | |
2924 | if (!bgp_def) | |
2925 | return -1; | |
2926 | ||
2927 | /* Walk entire global routing table and evaluate routes which could be | |
2928 | * imported into this VRF. Note that we need to loop through all global | |
2929 | * routes to determine which route matches the import rt on vrf | |
2930 | */ | |
2931 | for (rd_rn = bgp_table_top(bgp_def->rib[afi][safi]); rd_rn; | |
2932 | rd_rn = bgp_route_next(rd_rn)) { | |
2933 | table = (struct bgp_table *)(rd_rn->info); | |
2934 | if (!table) | |
2935 | continue; | |
2936 | ||
2937 | for (rn = bgp_table_top(table); rn; rn = bgp_route_next(rn)) { | |
2938 | struct prefix_evpn *evp = (struct prefix_evpn *)&rn->p; | |
2939 | ||
1eb88002 | 2940 | /* if not mac-ip route skip this route */ |
996c9314 LB |
2941 | if (!(evp->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE |
2942 | || evp->prefix.route_type | |
2943 | == BGP_EVPN_IP_PREFIX_ROUTE)) | |
5ba238b7 MK |
2944 | continue; |
2945 | ||
1eb88002 | 2946 | /* if not a mac+ip route skip this route */ |
3714a385 | 2947 | if (!(is_evpn_prefix_ipaddr_v4(evp) |
2948 | || is_evpn_prefix_ipaddr_v6(evp))) | |
1eb88002 MK |
2949 | continue; |
2950 | ||
5ba238b7 MK |
2951 | for (ri = rn->info; ri; ri = ri->next) { |
2952 | /* Consider "valid" remote routes applicable for | |
523cafc4 | 2953 | * this VRF. |
2954 | */ | |
5ba238b7 MK |
2955 | if (!(CHECK_FLAG(ri->flags, BGP_INFO_VALID) |
2956 | && ri->type == ZEBRA_ROUTE_BGP | |
2957 | && ri->sub_type == BGP_ROUTE_NORMAL)) | |
2958 | continue; | |
2959 | ||
2960 | if (is_route_matching_for_vrf(bgp_vrf, ri)) { | |
2961 | if (install) | |
996c9314 | 2962 | ret = install_evpn_route_entry_in_vrf( |
5ba238b7 MK |
2963 | bgp_vrf, evp, ri); |
2964 | else | |
996c9314 | 2965 | ret = uninstall_evpn_route_entry_in_vrf( |
5ba238b7 MK |
2966 | bgp_vrf, evp, ri); |
2967 | ||
2968 | if (ret) { | |
af4c2728 | 2969 | flog_err( |
14454c9f | 2970 | BGP_ERR_EVPN_FAIL, |
5ba238b7 MK |
2971 | "Failed to %s EVPN %s route in VRF %s", |
2972 | install ? "install" | |
2973 | : "uninstall", | |
2974 | prefix2str(evp, buf, | |
2975 | sizeof(buf)), | |
996c9314 LB |
2976 | vrf_id_to_name( |
2977 | bgp_vrf->vrf_id)); | |
5ba238b7 MK |
2978 | return ret; |
2979 | } | |
2980 | } | |
2981 | } | |
2982 | } | |
2983 | } | |
2984 | ||
2985 | return 0; | |
2986 | } | |
2987 | ||
128ea8ab | 2988 | /* |
2989 | * Install or uninstall routes of specified type that are appropriate for this | |
2990 | * particular VNI. | |
2991 | */ | |
d62a17ae | 2992 | static int install_uninstall_routes_for_vni(struct bgp *bgp, |
2993 | struct bgpevpn *vpn, | |
2994 | bgp_evpn_route_type rtype, | |
2995 | int install) | |
2996 | { | |
0291c246 MK |
2997 | afi_t afi; |
2998 | safi_t safi; | |
2999 | struct bgp_node *rd_rn, *rn; | |
3000 | struct bgp_table *table; | |
3001 | struct bgp_info *ri; | |
3002 | int ret; | |
d62a17ae | 3003 | |
3004 | afi = AFI_L2VPN; | |
3005 | safi = SAFI_EVPN; | |
3006 | ||
3007 | /* Walk entire global routing table and evaluate routes which could be | |
3008 | * imported into this VPN. Note that we cannot just look at the routes | |
3009 | * for | |
3010 | * the VNI's RD - remote routes applicable for this VNI could have any | |
3011 | * RD. | |
3012 | */ | |
3013 | /* EVPN routes are a 2-level table. */ | |
3014 | for (rd_rn = bgp_table_top(bgp->rib[afi][safi]); rd_rn; | |
3015 | rd_rn = bgp_route_next(rd_rn)) { | |
3016 | table = (struct bgp_table *)(rd_rn->info); | |
3017 | if (!table) | |
3018 | continue; | |
3019 | ||
3020 | for (rn = bgp_table_top(table); rn; rn = bgp_route_next(rn)) { | |
3021 | struct prefix_evpn *evp = (struct prefix_evpn *)&rn->p; | |
3022 | ||
3023 | if (evp->prefix.route_type != rtype) | |
3024 | continue; | |
3025 | ||
3026 | for (ri = rn->info; ri; ri = ri->next) { | |
3027 | /* Consider "valid" remote routes applicable for | |
3028 | * this VNI. */ | |
3029 | if (!(CHECK_FLAG(ri->flags, BGP_INFO_VALID) | |
3030 | && ri->type == ZEBRA_ROUTE_BGP | |
3031 | && ri->sub_type == BGP_ROUTE_NORMAL)) | |
3032 | continue; | |
3033 | ||
3034 | if (is_route_matching_for_vni(bgp, vpn, ri)) { | |
3035 | if (install) | |
3036 | ret = install_evpn_route_entry( | |
60466a63 | 3037 | bgp, vpn, evp, ri); |
d62a17ae | 3038 | else |
3039 | ret = uninstall_evpn_route_entry( | |
3040 | bgp, vpn, evp, ri); | |
3041 | ||
3042 | if (ret) { | |
af4c2728 | 3043 | flog_err( |
14454c9f | 3044 | BGP_ERR_EVPN_FAIL, |
d62a17ae | 3045 | "%u: Failed to %s EVPN %s route in VNI %u", |
3046 | bgp->vrf_id, | |
3047 | install ? "install" | |
3048 | : "uninstall", | |
3049 | rtype == BGP_EVPN_MAC_IP_ROUTE | |
3050 | ? "MACIP" | |
3051 | : "IMET", | |
3052 | vpn->vni); | |
3053 | return ret; | |
3054 | } | |
3055 | } | |
3056 | } | |
3057 | } | |
3058 | } | |
3059 | ||
3060 | return 0; | |
128ea8ab | 3061 | } |
3062 | ||
50f74cf1 | 3063 | /* Install any existing remote ES routes applicable for this ES into its routing |
3064 | * table. This is invoked when ES comes up. | |
3065 | */ | |
3066 | static int install_routes_for_es(struct bgp *bgp, struct evpnes *es) | |
3067 | { | |
3068 | return install_uninstall_routes_for_es(bgp, es, 1); | |
3069 | } | |
3070 | ||
3071 | ||
5ba238b7 | 3072 | /* Install any existing remote routes applicable for this VRF into VRF RIB. This |
523cafc4 | 3073 | * is invoked upon l3vni-add or l3vni import rt change |
3074 | */ | |
5ba238b7 MK |
3075 | static int install_routes_for_vrf(struct bgp *bgp_vrf) |
3076 | { | |
3077 | install_uninstall_routes_for_vrf(bgp_vrf, 1); | |
3078 | return 0; | |
3079 | } | |
3080 | ||
128ea8ab | 3081 | /* |
3082 | * Install any existing remote routes applicable for this VNI into its | |
3083 | * routing table. This is invoked when a VNI becomes "live" or its Import | |
3084 | * RT is changed. | |
3085 | */ | |
d62a17ae | 3086 | static int install_routes_for_vni(struct bgp *bgp, struct bgpevpn *vpn) |
128ea8ab | 3087 | { |
d62a17ae | 3088 | int ret; |
128ea8ab | 3089 | |
d62a17ae | 3090 | /* Install type-3 routes followed by type-2 routes - the ones applicable |
3091 | * for this VNI. | |
3092 | */ | |
3093 | ret = install_uninstall_routes_for_vni(bgp, vpn, BGP_EVPN_IMET_ROUTE, | |
3094 | 1); | |
3095 | if (ret) | |
3096 | return ret; | |
128ea8ab | 3097 | |
d62a17ae | 3098 | return install_uninstall_routes_for_vni(bgp, vpn, BGP_EVPN_MAC_IP_ROUTE, |
3099 | 1); | |
128ea8ab | 3100 | } |
3101 | ||
5ba238b7 MK |
3102 | /* uninstall routes from l3vni vrf. */ |
3103 | static int uninstall_routes_for_vrf(struct bgp *bgp_vrf) | |
3104 | { | |
3105 | install_uninstall_routes_for_vrf(bgp_vrf, 0); | |
3106 | return 0; | |
3107 | } | |
3108 | ||
90e60aa7 | 3109 | /* |
3110 | * Uninstall any existing remote routes for this VNI. One scenario in which | |
3111 | * this is invoked is upon an import RT change. | |
3112 | */ | |
d62a17ae | 3113 | static int uninstall_routes_for_vni(struct bgp *bgp, struct bgpevpn *vpn) |
90e60aa7 | 3114 | { |
d62a17ae | 3115 | int ret; |
90e60aa7 | 3116 | |
d62a17ae | 3117 | /* Uninstall type-2 routes followed by type-3 routes - the ones |
3118 | * applicable | |
3119 | * for this VNI. | |
3120 | */ | |
3121 | ret = install_uninstall_routes_for_vni(bgp, vpn, BGP_EVPN_MAC_IP_ROUTE, | |
3122 | 0); | |
3123 | if (ret) | |
3124 | return ret; | |
90e60aa7 | 3125 | |
d62a17ae | 3126 | return install_uninstall_routes_for_vni(bgp, vpn, BGP_EVPN_IMET_ROUTE, |
3127 | 0); | |
90e60aa7 | 3128 | } |
3129 | ||
50f74cf1 | 3130 | /* Install or unistall route in ES */ |
3131 | static int install_uninstall_route_in_es(struct bgp *bgp, struct evpnes *es, | |
3132 | afi_t afi, safi_t safi, | |
3133 | struct prefix_evpn *evp, | |
3134 | struct bgp_info *ri, | |
3135 | int install) | |
3136 | { | |
3137 | int ret = 0; | |
3138 | char buf[ESI_STR_LEN]; | |
3139 | ||
3140 | if (install) | |
3141 | ret = install_evpn_route_entry_in_es(bgp, es, evp, ri); | |
3142 | else | |
3143 | ret = uninstall_evpn_route_entry_in_es(bgp, es, evp, ri); | |
3144 | ||
3145 | if (ret) { | |
af4c2728 | 3146 | flog_err( |
14454c9f DS |
3147 | BGP_ERR_EVPN_FAIL, |
3148 | "%u: Failed to %s EVPN %s route in ESI %s", bgp->vrf_id, | |
3149 | install ? "install" : "uninstall", "ES", | |
3150 | esi_to_str(&evp->prefix.es_addr.esi, buf, sizeof(buf))); | |
50f74cf1 | 3151 | return ret; |
3152 | } | |
3153 | return 0; | |
3154 | } | |
3155 | ||
d3135ba3 | 3156 | /* |
3157 | * Install or uninstall route in matching VRFs (list). | |
3158 | */ | |
3159 | static int install_uninstall_route_in_vrfs(struct bgp *bgp_def, afi_t afi, | |
3160 | safi_t safi, struct prefix_evpn *evp, | |
3161 | struct bgp_info *ri, | |
3162 | struct list *vrfs, int install) | |
3163 | { | |
3164 | char buf[PREFIX2STR_BUFFER]; | |
3165 | struct bgp *bgp_vrf; | |
3166 | struct listnode *node, *nnode; | |
3167 | ||
90264d64 | 3168 | /* Only type-2/type-5 routes go into a VRF */ |
996c9314 LB |
3169 | if (!(evp->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE |
3170 | || evp->prefix.route_type == BGP_EVPN_IP_PREFIX_ROUTE)) | |
d3135ba3 | 3171 | return 0; |
3172 | ||
90264d64 | 3173 | /* if it is type-2 route and not a mac+ip route skip this route */ |
996c9314 | 3174 | if ((evp->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE) |
3714a385 | 3175 | && !(is_evpn_prefix_ipaddr_v4(evp) |
3176 | || is_evpn_prefix_ipaddr_v6(evp))) | |
30a30f57 MK |
3177 | return 0; |
3178 | ||
d3135ba3 | 3179 | for (ALL_LIST_ELEMENTS(vrfs, node, nnode, bgp_vrf)) { |
3180 | int ret; | |
3181 | ||
3182 | if (install) | |
996c9314 | 3183 | ret = install_evpn_route_entry_in_vrf(bgp_vrf, evp, ri); |
d3135ba3 | 3184 | else |
996c9314 LB |
3185 | ret = uninstall_evpn_route_entry_in_vrf(bgp_vrf, evp, |
3186 | ri); | |
d3135ba3 | 3187 | |
3188 | if (ret) { | |
af4c2728 | 3189 | flog_err(BGP_ERR_EVPN_FAIL, |
14454c9f DS |
3190 | "%u: Failed to %s prefix %s in VRF %s", |
3191 | bgp_def->vrf_id, | |
3192 | install ? "install" : "uninstall", | |
3193 | prefix2str(evp, buf, sizeof(buf)), | |
3194 | vrf_id_to_name(bgp_vrf->vrf_id)); | |
d3135ba3 | 3195 | return ret; |
3196 | } | |
3197 | } | |
3198 | ||
3199 | return 0; | |
3200 | } | |
3201 | ||
128ea8ab | 3202 | /* |
3203 | * Install or uninstall route in matching VNIs (list). | |
3204 | */ | |
d62a17ae | 3205 | static int install_uninstall_route_in_vnis(struct bgp *bgp, afi_t afi, |
3206 | safi_t safi, struct prefix_evpn *evp, | |
3207 | struct bgp_info *ri, | |
3208 | struct list *vnis, int install) | |
128ea8ab | 3209 | { |
d62a17ae | 3210 | struct bgpevpn *vpn; |
3211 | struct listnode *node, *nnode; | |
128ea8ab | 3212 | |
d62a17ae | 3213 | for (ALL_LIST_ELEMENTS(vnis, node, nnode, vpn)) { |
3214 | int ret; | |
128ea8ab | 3215 | |
d62a17ae | 3216 | if (!is_vni_live(vpn)) |
3217 | continue; | |
128ea8ab | 3218 | |
d62a17ae | 3219 | if (install) |
3220 | ret = install_evpn_route_entry(bgp, vpn, evp, ri); | |
3221 | else | |
3222 | ret = uninstall_evpn_route_entry(bgp, vpn, evp, ri); | |
128ea8ab | 3223 | |
d62a17ae | 3224 | if (ret) { |
af4c2728 | 3225 | flog_err( |
14454c9f DS |
3226 | BGP_ERR_EVPN_FAIL, |
3227 | "%u: Failed to %s EVPN %s route in VNI %u", | |
3228 | bgp->vrf_id, install ? "install" : "uninstall", | |
3229 | evp->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE | |
3230 | ? "MACIP" | |
3231 | : "IMET", | |
3232 | vpn->vni); | |
d62a17ae | 3233 | return ret; |
3234 | } | |
3235 | } | |
128ea8ab | 3236 | |
d62a17ae | 3237 | return 0; |
128ea8ab | 3238 | } |
3239 | ||
3240 | /* | |
50f74cf1 | 3241 | * Install or uninstall route for appropriate VNIs/ESIs. |
128ea8ab | 3242 | */ |
d62a17ae | 3243 | static int install_uninstall_evpn_route(struct bgp *bgp, afi_t afi, safi_t safi, |
3244 | struct prefix *p, struct bgp_info *ri, | |
3245 | int import) | |
3246 | { | |
3247 | struct prefix_evpn *evp = (struct prefix_evpn *)p; | |
3248 | struct attr *attr = ri->attr; | |
3249 | struct ecommunity *ecom; | |
3250 | int i; | |
3251 | ||
3252 | assert(attr); | |
3253 | ||
50f74cf1 | 3254 | /* Only type-2, type-3, type-4 and type-5 are supported currently */ |
d62a17ae | 3255 | if (!(evp->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE |
90264d64 | 3256 | || evp->prefix.route_type == BGP_EVPN_IMET_ROUTE |
50f74cf1 | 3257 | || evp->prefix.route_type == BGP_EVPN_ES_ROUTE |
90264d64 | 3258 | || evp->prefix.route_type == BGP_EVPN_IP_PREFIX_ROUTE)) |
d62a17ae | 3259 | return 0; |
3260 | ||
3261 | /* If we don't have Route Target, nothing much to do. */ | |
3262 | if (!(attr->flag & ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES))) | |
3263 | return 0; | |
3264 | ||
3265 | ecom = attr->ecommunity; | |
3266 | if (!ecom || !ecom->size) | |
3267 | return -1; | |
3268 | ||
50f74cf1 | 3269 | /* An EVPN route belongs to a VNI or a VRF or an ESI based on the RTs |
3270 | * attached to the route */ | |
d62a17ae | 3271 | for (i = 0; i < ecom->size; i++) { |
d7c0a89a QY |
3272 | uint8_t *pnt; |
3273 | uint8_t type, sub_type; | |
d62a17ae | 3274 | struct ecommunity_val *eval; |
3275 | struct ecommunity_val eval_tmp; | |
996c9314 | 3276 | struct irt_node *irt; /* import rt for l2vni */ |
d3135ba3 | 3277 | struct vrf_irt_node *vrf_irt; /* import rt for l3vni */ |
50f74cf1 | 3278 | struct evpnes *es; |
d62a17ae | 3279 | |
3280 | /* Only deal with RTs */ | |
3281 | pnt = (ecom->val + (i * ECOMMUNITY_SIZE)); | |
3282 | eval = (struct ecommunity_val *)(ecom->val | |
3283 | + (i * ECOMMUNITY_SIZE)); | |
3284 | type = *pnt++; | |
3285 | sub_type = *pnt++; | |
3286 | if (sub_type != ECOMMUNITY_ROUTE_TARGET) | |
3287 | continue; | |
3288 | ||
50f74cf1 | 3289 | /* |
3290 | * macip routes (type-2) are imported into VNI and VRF tables. | |
3291 | * IMET route is imported into VNI table. | |
3292 | * prefix routes are imported into VRF table. | |
523cafc4 | 3293 | */ |
50f74cf1 | 3294 | if (evp->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE || |
3295 | evp->prefix.route_type == BGP_EVPN_IMET_ROUTE || | |
3296 | evp->prefix.route_type == BGP_EVPN_IP_PREFIX_ROUTE) { | |
d62a17ae | 3297 | |
50f74cf1 | 3298 | irt = lookup_import_rt(bgp, eval); |
3299 | if (irt) | |
3300 | install_uninstall_route_in_vnis(bgp, afi, safi, | |
3301 | evp, ri, | |
3302 | irt->vnis, | |
3303 | import); | |
3304 | ||
3305 | vrf_irt = lookup_vrf_import_rt(eval); | |
3306 | if (vrf_irt) | |
3307 | install_uninstall_route_in_vrfs(bgp, afi, safi, | |
3308 | evp, ri, | |
3309 | vrf_irt->vrfs, | |
3310 | import); | |
3311 | ||
3312 | /* Also check for non-exact match. | |
3313 | * In this, we mask out the AS and | |
3314 | * only check on the local-admin sub-field. | |
3315 | * This is to facilitate using | |
3316 | * VNI as the RT for EBGP peering too. | |
3317 | */ | |
3318 | irt = NULL; | |
3319 | vrf_irt = NULL; | |
3320 | if (type == ECOMMUNITY_ENCODE_AS | |
3321 | || type == ECOMMUNITY_ENCODE_AS4 | |
3322 | || type == ECOMMUNITY_ENCODE_IP) { | |
3323 | memcpy(&eval_tmp, eval, ECOMMUNITY_SIZE); | |
3324 | mask_ecom_global_admin(&eval_tmp, eval); | |
3325 | irt = lookup_import_rt(bgp, &eval_tmp); | |
3326 | vrf_irt = lookup_vrf_import_rt(&eval_tmp); | |
3327 | } | |
3328 | ||
3329 | if (irt) | |
3330 | install_uninstall_route_in_vnis(bgp, afi, safi, | |
3331 | evp, ri, | |
3332 | irt->vnis, | |
3333 | import); | |
3334 | if (vrf_irt) | |
3335 | install_uninstall_route_in_vrfs(bgp, afi, safi, | |
3336 | evp, ri, | |
3337 | vrf_irt->vrfs, | |
3338 | import); | |
3339 | } | |
3340 | ||
3341 | /* es route is imported into the es table */ | |
3342 | if (evp->prefix.route_type == BGP_EVPN_ES_ROUTE) { | |
3343 | ||
3344 | /* we will match based on the entire esi to avoid | |
3345 | * imoort of an es route for esi2 into esi1 | |
3346 | */ | |
3347 | es = bgp_evpn_lookup_es(bgp, &evp->prefix.es_addr.esi); | |
3348 | if (es && is_es_local(es)) | |
3349 | install_uninstall_route_in_es(bgp, es, | |
3350 | afi, safi, | |
3351 | evp, ri, import); | |
d62a17ae | 3352 | } |
d62a17ae | 3353 | } |
3354 | ||
3355 | return 0; | |
128ea8ab | 3356 | } |
3357 | ||
2bb9eff4 DS |
3358 | /* |
3359 | * delete and withdraw all ipv4 and ipv6 routes in the vrf table as type-5 | |
3360 | * routes | |
3361 | */ | |
80b140af MK |
3362 | static void delete_withdraw_vrf_routes(struct bgp *bgp_vrf) |
3363 | { | |
3364 | /* delete all ipv4 routes and withdraw from peers */ | |
fdf19f06 MK |
3365 | if (advertise_type5_routes(bgp_vrf, AFI_IP)) |
3366 | bgp_evpn_withdraw_type5_routes(bgp_vrf, AFI_IP, SAFI_UNICAST); | |
80b140af MK |
3367 | |
3368 | /* delete all ipv6 routes and withdraw from peers */ | |
fdf19f06 MK |
3369 | if (advertise_type5_routes(bgp_vrf, AFI_IP6)) |
3370 | bgp_evpn_withdraw_type5_routes(bgp_vrf, AFI_IP6, SAFI_UNICAST); | |
80b140af MK |
3371 | } |
3372 | ||
2bb9eff4 DS |
3373 | /* |
3374 | * update and advertise all ipv4 and ipv6 routes in thr vrf table as type-5 | |
3375 | * routes | |
3376 | */ | |
80b140af MK |
3377 | static void update_advertise_vrf_routes(struct bgp *bgp_vrf) |
3378 | { | |
3379 | /* update all ipv4 routes */ | |
fdf19f06 MK |
3380 | if (advertise_type5_routes(bgp_vrf, AFI_IP)) |
3381 | bgp_evpn_advertise_type5_routes(bgp_vrf, AFI_IP, SAFI_UNICAST); | |
80b140af MK |
3382 | |
3383 | /* update all ipv6 routes */ | |
fdf19f06 MK |
3384 | if (advertise_type5_routes(bgp_vrf, AFI_IP6)) |
3385 | bgp_evpn_advertise_type5_routes(bgp_vrf, AFI_IP6, SAFI_UNICAST); | |
80b140af MK |
3386 | } |
3387 | ||
676f83b9 | 3388 | /* |
3389 | * update and advertise local routes for a VRF as type-5 routes. | |
3390 | * This is invoked upon RD change for a VRF. Note taht the processing is only | |
3391 | * done in the global route table using the routes which already exist in the | |
3392 | * VRF routing table | |
3393 | */ | |
80b140af | 3394 | static void update_router_id_vrf(struct bgp *bgp_vrf) |
676f83b9 | 3395 | { |
80b140af MK |
3396 | /* skip if the RD is configured */ |
3397 | if (is_vrf_rd_configured(bgp_vrf)) | |
3398 | return; | |
3399 | ||
3400 | /* derive the RD for the VRF based on new router-id */ | |
3401 | bgp_evpn_derive_auto_rd_for_vrf(bgp_vrf); | |
3402 | ||
3403 | /* update advertise ipv4|ipv6 routes as type-5 routes */ | |
3404 | update_advertise_vrf_routes(bgp_vrf); | |
676f83b9 | 3405 | } |
3406 | ||
3407 | /* | |
3408 | * Delete and withdraw all type-5 routes for the RD corresponding to VRF. | |
3409 | * This is invoked upon VRF RD change. The processing is done only from global | |
3410 | * table. | |
3411 | */ | |
80b140af | 3412 | static void withdraw_router_id_vrf(struct bgp *bgp_vrf) |
676f83b9 | 3413 | { |
80b140af MK |
3414 | /* skip if the RD is configured */ |
3415 | if (is_vrf_rd_configured(bgp_vrf)) | |
3416 | return; | |
3417 | ||
3418 | /* delete/withdraw ipv4|ipv6 routes as type-5 routes */ | |
3419 | delete_withdraw_vrf_routes(bgp_vrf); | |
676f83b9 | 3420 | } |
3421 | ||
90e60aa7 | 3422 | /* |
3423 | * Update and advertise local routes for a VNI. Invoked upon router-id | |
3424 | * change. Note that the processing is done only on the global route table | |
3425 | * using routes that already exist in the per-VNI table. | |
3426 | */ | |
d62a17ae | 3427 | static int update_advertise_vni_routes(struct bgp *bgp, struct bgpevpn *vpn) |
3428 | { | |
3429 | struct prefix_evpn p; | |
3430 | struct bgp_node *rn, *global_rn; | |
3431 | struct bgp_info *ri, *global_ri; | |
3432 | struct attr *attr; | |
3433 | afi_t afi = AFI_L2VPN; | |
3434 | safi_t safi = SAFI_EVPN; | |
3435 | ||
3436 | /* Locate type-3 route for VNI in the per-VNI table and use its | |
3437 | * attributes to create and advertise the type-3 route for this VNI | |
3438 | * in the global table. | |
3439 | */ | |
3440 | build_evpn_type3_prefix(&p, vpn->originator_ip); | |
3441 | rn = bgp_node_lookup(vpn->route_table, (struct prefix *)&p); | |
3442 | if (!rn) /* unexpected */ | |
3443 | return 0; | |
3444 | for (ri = rn->info; ri; ri = ri->next) | |
3445 | if (ri->peer == bgp->peer_self && ri->type == ZEBRA_ROUTE_BGP | |
3446 | && ri->sub_type == BGP_ROUTE_STATIC) | |
3447 | break; | |
3448 | if (!ri) /* unexpected */ | |
3449 | return 0; | |
3450 | attr = ri->attr; | |
3451 | ||
3452 | global_rn = bgp_afi_node_get(bgp->rib[afi][safi], afi, safi, | |
3453 | (struct prefix *)&p, &vpn->prd); | |
1a98c087 MK |
3454 | update_evpn_route_entry(bgp, vpn, afi, safi, global_rn, attr, 1, 0, &ri, |
3455 | 0); | |
d62a17ae | 3456 | |
3457 | /* Schedule for processing and unlock node. */ | |
3458 | bgp_process(bgp, global_rn, afi, safi); | |
3459 | bgp_unlock_node(global_rn); | |
3460 | ||
3461 | /* Now, walk this VNI's route table and use the route and its attribute | |
3462 | * to create and schedule route in global table. | |
3463 | */ | |
3464 | for (rn = bgp_table_top(vpn->route_table); rn; | |
3465 | rn = bgp_route_next(rn)) { | |
3466 | struct prefix_evpn *evp = (struct prefix_evpn *)&rn->p; | |
3467 | ||
3468 | /* Identify MAC-IP local routes. */ | |
3469 | if (evp->prefix.route_type != BGP_EVPN_MAC_IP_ROUTE) | |
3470 | continue; | |
3471 | ||
3472 | for (ri = rn->info; ri; ri = ri->next) | |
3473 | if (ri->peer == bgp->peer_self | |
3474 | && ri->type == ZEBRA_ROUTE_BGP | |
3475 | && ri->sub_type == BGP_ROUTE_STATIC) | |
3476 | break; | |
3477 | if (!ri) | |
3478 | continue; | |
3479 | ||
3480 | /* Create route in global routing table using this route entry's | |
3481 | * attribute. | |
3482 | */ | |
3483 | attr = ri->attr; | |
3484 | global_rn = bgp_afi_node_get(bgp->rib[afi][safi], afi, safi, | |
3485 | (struct prefix *)evp, &vpn->prd); | |
3486 | assert(global_rn); | |
3487 | update_evpn_route_entry(bgp, vpn, afi, safi, global_rn, attr, 1, | |
1a98c087 | 3488 | 0, &global_ri, 0); |
d62a17ae | 3489 | |
3490 | /* Schedule for processing and unlock node. */ | |
3491 | bgp_process(bgp, global_rn, afi, safi); | |
3492 | bgp_unlock_node(global_rn); | |
3493 | } | |
3494 | ||
3495 | return 0; | |
90e60aa7 | 3496 | } |
3497 | ||
3498 | /* | |
3499 | * Delete (and withdraw) local routes for a VNI - only from the global | |
3500 | * table. Invoked upon router-id change. | |
3501 | */ | |
d62a17ae | 3502 | static int delete_withdraw_vni_routes(struct bgp *bgp, struct bgpevpn *vpn) |
90e60aa7 | 3503 | { |
d62a17ae | 3504 | int ret; |
3505 | struct prefix_evpn p; | |
3506 | struct bgp_node *global_rn; | |
3507 | struct bgp_info *ri; | |
3508 | afi_t afi = AFI_L2VPN; | |
3509 | safi_t safi = SAFI_EVPN; | |
90e60aa7 | 3510 | |
d62a17ae | 3511 | /* Delete and withdraw locally learnt type-2 routes (MACIP) |
3512 | * for this VNI - from the global table. | |
3513 | */ | |
3514 | ret = delete_global_type2_routes(bgp, vpn); | |
3515 | if (ret) | |
3516 | return ret; | |
90e60aa7 | 3517 | |
d62a17ae | 3518 | /* Remove type-3 route for this VNI from global table. */ |
3519 | build_evpn_type3_prefix(&p, vpn->originator_ip); | |
3520 | global_rn = bgp_afi_node_lookup(bgp->rib[afi][safi], afi, safi, | |
3521 | (struct prefix *)&p, &vpn->prd); | |
3522 | if (global_rn) { | |
3523 | /* Delete route entry in the global EVPN table. */ | |
50f74cf1 | 3524 | delete_evpn_route_entry(bgp, afi, safi, global_rn, &ri); |
90e60aa7 | 3525 | |
d62a17ae | 3526 | /* Schedule for processing - withdraws to peers happen from |
3527 | * this table. | |
3528 | */ | |
3529 | if (ri) | |
3530 | bgp_process(bgp, global_rn, afi, safi); | |
3531 | bgp_unlock_node(global_rn); | |
3532 | } | |
90e60aa7 | 3533 | |
d62a17ae | 3534 | return 0; |
90e60aa7 | 3535 | } |
3536 | ||
2d48ee25 | 3537 | /* |
3538 | * Handle router-id change. Update and advertise local routes corresponding | |
3539 | * to this VNI from peers. Note that this is invoked after updating the | |
3540 | * router-id. The routes in the per-VNI table are used to create routes in | |
3541 | * the global table and schedule them. | |
3542 | */ | |
d62a17ae | 3543 | static void update_router_id_vni(struct hash_backet *backet, struct bgp *bgp) |
2d48ee25 | 3544 | { |
0a1a07cb | 3545 | struct bgpevpn *vpn = (struct bgpevpn *)backet->data; |
2d48ee25 | 3546 | |
d62a17ae | 3547 | /* Skip VNIs with configured RD. */ |
3548 | if (is_rd_configured(vpn)) | |
3549 | return; | |
2d48ee25 | 3550 | |
d62a17ae | 3551 | bgp_evpn_derive_auto_rd(bgp, vpn); |
3552 | update_advertise_vni_routes(bgp, vpn); | |
2d48ee25 | 3553 | } |
3554 | ||
3555 | /* | |
3556 | * Handle router-id change. Delete and withdraw local routes corresponding | |
3557 | * to this VNI from peers. Note that this is invoked prior to updating | |
3558 | * the router-id and is done only on the global route table, the routes | |
3559 | * are needed in the per-VNI table to re-advertise with new router id. | |
3560 | */ | |
d62a17ae | 3561 | static void withdraw_router_id_vni(struct hash_backet *backet, struct bgp *bgp) |
2d48ee25 | 3562 | { |
0a1a07cb | 3563 | struct bgpevpn *vpn = (struct bgpevpn *)backet->data; |
2d48ee25 | 3564 | |
d62a17ae | 3565 | /* Skip VNIs with configured RD. */ |
3566 | if (is_rd_configured(vpn)) | |
3567 | return; | |
2d48ee25 | 3568 | |
d62a17ae | 3569 | delete_withdraw_vni_routes(bgp, vpn); |
2d48ee25 | 3570 | } |
3571 | ||
128ea8ab | 3572 | /* |
3573 | * Process received EVPN type-2 route (advertise or withdraw). | |
3574 | */ | |
d62a17ae | 3575 | static int process_type2_route(struct peer *peer, afi_t afi, safi_t safi, |
d7c0a89a QY |
3576 | struct attr *attr, uint8_t *pfx, int psize, |
3577 | uint32_t addpath_id) | |
d62a17ae | 3578 | { |
3579 | struct prefix_rd prd; | |
3580 | struct prefix_evpn p; | |
554cd77a | 3581 | struct bgp_route_evpn evpn; |
d7c0a89a QY |
3582 | uint8_t ipaddr_len; |
3583 | uint8_t macaddr_len; | |
b57ba6d2 | 3584 | mpls_label_t label[BGP_MAX_LABELS]; /* holds the VNI(s) as in packet */ |
d7c0a89a | 3585 | uint32_t num_labels = 0; |
554cd77a | 3586 | uint32_t eth_tag; |
d62a17ae | 3587 | int ret; |
3588 | ||
3589 | /* Type-2 route should be either 33, 37 or 49 bytes or an | |
3590 | * additional 3 bytes if there is a second label (VNI): | |
3591 | * RD (8), ESI (10), Eth Tag (4), MAC Addr Len (1), | |
3592 | * MAC Addr (6), IP len (1), IP (0, 4 or 16), | |
3593 | * MPLS Lbl1 (3), MPLS Lbl2 (0 or 3) | |
3594 | */ | |
3595 | if (psize != 33 && psize != 37 && psize != 49 && psize != 36 | |
3596 | && psize != 40 && psize != 52) { | |
af4c2728 | 3597 | flog_err(BGP_ERR_EVPN_ROUTE_INVALID, |
14454c9f DS |
3598 | "%u:%s - Rx EVPN Type-2 NLRI with invalid length %d", |
3599 | peer->bgp->vrf_id, peer->host, psize); | |
d62a17ae | 3600 | return -1; |
3601 | } | |
3602 | ||
554cd77a VB |
3603 | memset(&evpn, 0, sizeof(evpn)); |
3604 | ||
d62a17ae | 3605 | /* Make prefix_rd */ |
3606 | prd.family = AF_UNSPEC; | |
3607 | prd.prefixlen = 64; | |
3608 | memcpy(&prd.val, pfx, 8); | |
3609 | pfx += 8; | |
3610 | ||
3611 | /* Make EVPN prefix. */ | |
3612 | memset(&p, 0, sizeof(struct prefix_evpn)); | |
b03b8898 | 3613 | p.family = AF_EVPN; |
50f74cf1 | 3614 | p.prefixlen = EVPN_ROUTE_PREFIXLEN; |
d62a17ae | 3615 | p.prefix.route_type = BGP_EVPN_MAC_IP_ROUTE; |
3616 | ||
554cd77a VB |
3617 | /* Copy Ethernet Seg Identifier */ |
3618 | memcpy(&evpn.eth_s_id.val, pfx, ESI_LEN); | |
3619 | pfx += ESI_LEN; | |
d62a17ae | 3620 | |
554cd77a VB |
3621 | /* Copy Ethernet Tag */ |
3622 | memcpy(ð_tag, pfx, 4); | |
3714a385 | 3623 | p.prefix.macip_addr.eth_tag = ntohl(eth_tag); |
d62a17ae | 3624 | pfx += 4; |
3625 | ||
3626 | /* Get the MAC Addr len */ | |
3627 | macaddr_len = *pfx++; | |
3628 | ||
3629 | /* Get the MAC Addr */ | |
28328ea9 | 3630 | if (macaddr_len == (ETH_ALEN * 8)) { |
3714a385 | 3631 | memcpy(&p.prefix.macip_addr.mac.octet, pfx, ETH_ALEN); |
28328ea9 | 3632 | pfx += ETH_ALEN; |
d62a17ae | 3633 | } else { |
af4c2728 | 3634 | flog_err( |
14454c9f | 3635 | BGP_ERR_EVPN_ROUTE_INVALID, |
d62a17ae | 3636 | "%u:%s - Rx EVPN Type-2 NLRI with unsupported MAC address length %d", |
3637 | peer->bgp->vrf_id, peer->host, macaddr_len); | |
3638 | return -1; | |
3639 | } | |
3640 | ||
3641 | ||
3642 | /* Get the IP. */ | |
3643 | ipaddr_len = *pfx++; | |
3644 | if (ipaddr_len != 0 && ipaddr_len != IPV4_MAX_BITLEN | |
3645 | && ipaddr_len != IPV6_MAX_BITLEN) { | |
af4c2728 | 3646 | flog_err( |
14454c9f | 3647 | BGP_ERR_EVPN_ROUTE_INVALID, |
d62a17ae | 3648 | "%u:%s - Rx EVPN Type-2 NLRI with unsupported IP address length %d", |
3649 | peer->bgp->vrf_id, peer->host, ipaddr_len); | |
3650 | return -1; | |
3651 | } | |
3652 | ||
3653 | if (ipaddr_len) { | |
3654 | ipaddr_len /= 8; /* Convert to bytes. */ | |
3714a385 | 3655 | p.prefix.macip_addr.ip.ipa_type = (ipaddr_len == IPV4_MAX_BYTELEN) |
d62a17ae | 3656 | ? IPADDR_V4 |
3657 | : IPADDR_V6; | |
3714a385 | 3658 | memcpy(&p.prefix.macip_addr.ip.ip.addr, pfx, ipaddr_len); |
d62a17ae | 3659 | } |
3660 | pfx += ipaddr_len; | |
3661 | ||
b57ba6d2 MK |
3662 | /* Get the VNI(s). Stored as bytes here. */ |
3663 | num_labels++; | |
3664 | memset(label, 0, sizeof(label)); | |
3665 | memcpy(&label[0], pfx, BGP_LABEL_BYTES); | |
1b817c78 | 3666 | pfx += BGP_LABEL_BYTES; |
b57ba6d2 MK |
3667 | psize -= (33 + ipaddr_len); |
3668 | /* Do we have a second VNI? */ | |
3669 | if (psize) { | |
3670 | num_labels++; | |
3671 | memcpy(&label[1], pfx, BGP_LABEL_BYTES); | |
6b11bd8d | 3672 | /* |
3673 | * If in future, we are required to access additional fields, | |
996c9314 LB |
3674 | * we MUST increment pfx by BGP_LABEL_BYTES in before reading |
3675 | * the next field | |
6b11bd8d | 3676 | */ |
b57ba6d2 | 3677 | } |
d62a17ae | 3678 | |
3679 | /* Process the route. */ | |
3680 | if (attr) | |
3681 | ret = bgp_update(peer, (struct prefix *)&p, addpath_id, attr, | |
3682 | afi, safi, ZEBRA_ROUTE_BGP, BGP_ROUTE_NORMAL, | |
554cd77a | 3683 | &prd, &label[0], num_labels, 0, &evpn); |
d62a17ae | 3684 | else |
3685 | ret = bgp_withdraw(peer, (struct prefix *)&p, addpath_id, attr, | |
3686 | afi, safi, ZEBRA_ROUTE_BGP, BGP_ROUTE_NORMAL, | |
554cd77a | 3687 | &prd, &label[0], num_labels, &evpn); |
d62a17ae | 3688 | return ret; |
128ea8ab | 3689 | } |
3690 | ||
3691 | /* | |
3692 | * Process received EVPN type-3 route (advertise or withdraw). | |
3693 | */ | |
d62a17ae | 3694 | static int process_type3_route(struct peer *peer, afi_t afi, safi_t safi, |
d7c0a89a QY |
3695 | struct attr *attr, uint8_t *pfx, int psize, |
3696 | uint32_t addpath_id) | |
d62a17ae | 3697 | { |
3698 | struct prefix_rd prd; | |
3699 | struct prefix_evpn p; | |
d7c0a89a | 3700 | uint8_t ipaddr_len; |
554cd77a | 3701 | uint32_t eth_tag; |
d62a17ae | 3702 | int ret; |
3703 | ||
3704 | /* Type-3 route should be either 17 or 29 bytes: RD (8), Eth Tag (4), | |
3705 | * IP len (1) and IP (4 or 16). | |
3706 | */ | |
3707 | if (psize != 17 && psize != 29) { | |
af4c2728 | 3708 | flog_err(BGP_ERR_EVPN_ROUTE_INVALID, |
14454c9f DS |
3709 | "%u:%s - Rx EVPN Type-3 NLRI with invalid length %d", |
3710 | peer->bgp->vrf_id, peer->host, psize); | |
d62a17ae | 3711 | return -1; |
3712 | } | |
3713 | ||
7fd077aa | 3714 | /* If PMSI is present, log if it is anything other than IR. |
3715 | * Note: We just simply ignore the values as it is not clear if | |
3716 | * doing anything else is better. | |
3717 | */ | |
3718 | if (attr && | |
3719 | (attr->flag & ATTR_FLAG_BIT(BGP_ATTR_PMSI_TUNNEL))) { | |
3720 | if (attr->pmsi_tnl_type != PMSI_TNLTYPE_INGR_REPL) { | |
d0be47e8 | 3721 | zlog_warn("%u:%s - Rx EVPN Type-3 NLRI with unsupported PTA %d", |
7fd077aa | 3722 | peer->bgp->vrf_id, peer->host, |
3723 | attr->pmsi_tnl_type); | |
3724 | } | |
3725 | } | |
3726 | ||
d62a17ae | 3727 | /* Make prefix_rd */ |
3728 | prd.family = AF_UNSPEC; | |
3729 | prd.prefixlen = 64; | |
3730 | memcpy(&prd.val, pfx, 8); | |
3731 | pfx += 8; | |
3732 | ||
3733 | /* Make EVPN prefix. */ | |
3734 | memset(&p, 0, sizeof(struct prefix_evpn)); | |
b03b8898 | 3735 | p.family = AF_EVPN; |
50f74cf1 | 3736 | p.prefixlen = EVPN_ROUTE_PREFIXLEN; |
d62a17ae | 3737 | p.prefix.route_type = BGP_EVPN_IMET_ROUTE; |
3738 | ||
554cd77a VB |
3739 | /* Copy Ethernet Tag */ |
3740 | memcpy(ð_tag, pfx, 4); | |
3714a385 | 3741 | p.prefix.imet_addr.eth_tag = ntohl(eth_tag); |
d62a17ae | 3742 | pfx += 4; |
3743 | ||
3744 | /* Get the IP. */ | |
3745 | ipaddr_len = *pfx++; | |
3746 | if (ipaddr_len == IPV4_MAX_BITLEN) { | |
3714a385 | 3747 | p.prefix.imet_addr.ip.ipa_type = IPADDR_V4; |
3748 | memcpy(&p.prefix.imet_addr.ip.ip.addr, pfx, IPV4_MAX_BYTELEN); | |
d62a17ae | 3749 | } else { |
af4c2728 | 3750 | flog_err( |
14454c9f | 3751 | BGP_ERR_EVPN_ROUTE_INVALID, |
d62a17ae | 3752 | "%u:%s - Rx EVPN Type-3 NLRI with unsupported IP address length %d", |
3753 | peer->bgp->vrf_id, peer->host, ipaddr_len); | |
3754 | return -1; | |
3755 | } | |
3756 | ||
3757 | /* Process the route. */ | |
3758 | if (attr) | |
3759 | ret = bgp_update(peer, (struct prefix *)&p, addpath_id, attr, | |
3760 | afi, safi, ZEBRA_ROUTE_BGP, BGP_ROUTE_NORMAL, | |
b57ba6d2 | 3761 | &prd, NULL, 0, 0, NULL); |
d62a17ae | 3762 | else |
3763 | ret = bgp_withdraw(peer, (struct prefix *)&p, addpath_id, attr, | |
3764 | afi, safi, ZEBRA_ROUTE_BGP, BGP_ROUTE_NORMAL, | |
b57ba6d2 | 3765 | &prd, NULL, 0, NULL); |
d62a17ae | 3766 | return ret; |
128ea8ab | 3767 | } |
3768 | ||
50f74cf1 | 3769 | /* |
3770 | * Process received EVPN type-4 route (advertise or withdraw). | |
3771 | */ | |
3772 | static int process_type4_route(struct peer *peer, afi_t afi, safi_t safi, | |
3773 | struct attr *attr, uint8_t *pfx, int psize, | |
3774 | uint32_t addpath_id) | |
3775 | { | |
3776 | int ret; | |
3777 | esi_t esi; | |
3778 | uint8_t ipaddr_len; | |
3779 | struct in_addr vtep_ip; | |
3780 | struct prefix_rd prd; | |
3781 | struct prefix_evpn p; | |
3782 | ||
3783 | /* Type-4 route should be either 23 or 35 bytes | |
3784 | * RD (8), ESI (10), ip-len (1), ip (4 or 16) | |
3785 | */ | |
3786 | if (psize != 23 && psize != 35) { | |
af4c2728 | 3787 | flog_err(BGP_ERR_EVPN_ROUTE_INVALID, |
14454c9f DS |
3788 | "%u:%s - Rx EVPN Type-4 NLRI with invalid length %d", |
3789 | peer->bgp->vrf_id, peer->host, psize); | |
50f74cf1 | 3790 | return -1; |
3791 | } | |
3792 | ||
3793 | /* Make prefix_rd */ | |
3794 | prd.family = AF_UNSPEC; | |
3795 | prd.prefixlen = 64; | |
3796 | memcpy(&prd.val, pfx, 8); | |
3797 | pfx += 8; | |
3798 | ||
3799 | /* get the ESI */ | |
3800 | memcpy(&esi, pfx, ESI_BYTES); | |
3801 | pfx += ESI_BYTES; | |
3802 | ||
3803 | ||
3804 | /* Get the IP. */ | |
3805 | ipaddr_len = *pfx++; | |
3806 | if (ipaddr_len == IPV4_MAX_BITLEN) { | |
3807 | memcpy(&vtep_ip, pfx, IPV4_MAX_BYTELEN); | |
3808 | } else { | |
af4c2728 | 3809 | flog_err( |
14454c9f | 3810 | BGP_ERR_EVPN_ROUTE_INVALID, |
50f74cf1 | 3811 | "%u:%s - Rx EVPN Type-4 NLRI with unsupported IP address length %d", |
3812 | peer->bgp->vrf_id, peer->host, ipaddr_len); | |
3813 | return -1; | |
3814 | } | |
3815 | ||
3816 | build_evpn_type4_prefix(&p, &esi, vtep_ip); | |
3817 | /* Process the route. */ | |
3818 | if (attr) { | |
3819 | ret = bgp_update(peer, (struct prefix *)&p, addpath_id, attr, | |
3820 | afi, safi, ZEBRA_ROUTE_BGP, BGP_ROUTE_NORMAL, | |
3821 | &prd, NULL, 0, 0, NULL); | |
2bb9eff4 | 3822 | } else { |
50f74cf1 | 3823 | ret = bgp_withdraw(peer, (struct prefix *)&p, addpath_id, attr, |
3824 | afi, safi, ZEBRA_ROUTE_BGP, BGP_ROUTE_NORMAL, | |
3825 | &prd, NULL, 0, NULL); | |
3826 | } | |
3827 | return ret; | |
3828 | } | |
3829 | ||
3830 | ||
128ea8ab | 3831 | /* |
3832 | * Process received EVPN type-5 route (advertise or withdraw). | |
3833 | */ | |
d62a17ae | 3834 | static int process_type5_route(struct peer *peer, afi_t afi, safi_t safi, |
d7c0a89a QY |
3835 | struct attr *attr, uint8_t *pfx, int psize, |
3836 | uint32_t addpath_id, int withdraw) | |
d62a17ae | 3837 | { |
3838 | struct prefix_rd prd; | |
3839 | struct prefix_evpn p; | |
3840 | struct bgp_route_evpn evpn; | |
d7c0a89a QY |
3841 | uint8_t ippfx_len; |
3842 | uint32_t eth_tag; | |
b57ba6d2 | 3843 | mpls_label_t label; /* holds the VNI as in the packet */ |
d62a17ae | 3844 | int ret; |
3845 | ||
3846 | /* Type-5 route should be 34 or 58 bytes: | |
3847 | * RD (8), ESI (10), Eth Tag (4), IP len (1), IP (4 or 16), | |
3848 | * GW (4 or 16) and VNI (3). | |
3849 | * Note that the IP and GW should both be IPv4 or both IPv6. | |
3850 | */ | |
3851 | if (psize != 34 && psize != 58) { | |
af4c2728 | 3852 | flog_err(BGP_ERR_EVPN_ROUTE_INVALID, |
14454c9f DS |
3853 | "%u:%s - Rx EVPN Type-5 NLRI with invalid length %d", |
3854 | peer->bgp->vrf_id, peer->host, psize); | |
d62a17ae | 3855 | return -1; |
3856 | } | |
3857 | ||
3858 | /* Make prefix_rd */ | |
3859 | prd.family = AF_UNSPEC; | |
3860 | prd.prefixlen = 64; | |
3861 | memcpy(&prd.val, pfx, 8); | |
3862 | pfx += 8; | |
3863 | ||
3864 | /* Make EVPN prefix. */ | |
3865 | memset(&p, 0, sizeof(struct prefix_evpn)); | |
b03b8898 | 3866 | p.family = AF_EVPN; |
50f74cf1 | 3867 | p.prefixlen = EVPN_ROUTE_PREFIXLEN; |
d62a17ae | 3868 | p.prefix.route_type = BGP_EVPN_IP_PREFIX_ROUTE; |
3869 | ||
3870 | /* Additional information outside of prefix - ESI and GW IP */ | |
3871 | memset(&evpn, 0, sizeof(evpn)); | |
3872 | ||
3873 | /* Fetch ESI */ | |
3874 | memcpy(&evpn.eth_s_id.val, pfx, 10); | |
3875 | pfx += 10; | |
3876 | ||
3877 | /* Fetch Ethernet Tag. */ | |
3878 | memcpy(ð_tag, pfx, 4); | |
3714a385 | 3879 | p.prefix.prefix_addr.eth_tag = ntohl(eth_tag); |
d62a17ae | 3880 | pfx += 4; |
3881 | ||
3882 | /* Fetch IP prefix length. */ | |
3883 | ippfx_len = *pfx++; | |
3884 | if (ippfx_len > IPV6_MAX_BITLEN) { | |
af4c2728 | 3885 | flog_err( |
14454c9f | 3886 | BGP_ERR_EVPN_ROUTE_INVALID, |
d62a17ae | 3887 | "%u:%s - Rx EVPN Type-5 NLRI with invalid IP Prefix length %d", |
3888 | peer->bgp->vrf_id, peer->host, ippfx_len); | |
3889 | return -1; | |
3890 | } | |
3714a385 | 3891 | p.prefix.prefix_addr.ip_prefix_length = ippfx_len; |
d62a17ae | 3892 | |
3893 | /* Determine IPv4 or IPv6 prefix */ | |
3894 | /* Since the address and GW are from the same family, this just becomes | |
3895 | * a simple check on the total size. | |
3896 | */ | |
3897 | if (psize == 34) { | |
3714a385 | 3898 | SET_IPADDR_V4(&p.prefix.prefix_addr.ip); |
3899 | memcpy(&p.prefix.prefix_addr.ip.ipaddr_v4, pfx, 4); | |
d62a17ae | 3900 | pfx += 4; |
3901 | memcpy(&evpn.gw_ip.ipv4, pfx, 4); | |
3902 | pfx += 4; | |
d62a17ae | 3903 | } else { |
3714a385 | 3904 | SET_IPADDR_V6(&p.prefix.prefix_addr.ip); |
3905 | memcpy(&p.prefix.prefix_addr.ip.ipaddr_v6, pfx, 16); | |
d62a17ae | 3906 | pfx += 16; |
3907 | memcpy(&evpn.gw_ip.ipv6, pfx, 16); | |
3908 | pfx += 16; | |
d62a17ae | 3909 | } |
3910 | ||
b57ba6d2 MK |
3911 | /* Get the VNI (in MPLS label field). Stored as bytes here. */ |
3912 | memset(&label, 0, sizeof(label)); | |
3913 | memcpy(&label, pfx, BGP_LABEL_BYTES); | |
6b11bd8d | 3914 | |
3915 | /* | |
3916 | * If in future, we are required to access additional fields, | |
996c9314 LB |
3917 | * we MUST increment pfx by BGP_LABEL_BYTES in before reading the next |
3918 | * field | |
6b11bd8d | 3919 | */ |
d62a17ae | 3920 | |
3921 | /* Process the route. */ | |
3922 | if (!withdraw) | |
3923 | ret = bgp_update(peer, (struct prefix *)&p, addpath_id, attr, | |
3924 | afi, safi, ZEBRA_ROUTE_BGP, BGP_ROUTE_NORMAL, | |
b57ba6d2 | 3925 | &prd, &label, 1, 0, &evpn); |
d62a17ae | 3926 | else |
3927 | ret = bgp_withdraw(peer, (struct prefix *)&p, addpath_id, attr, | |
3928 | afi, safi, ZEBRA_ROUTE_BGP, BGP_ROUTE_NORMAL, | |
b57ba6d2 | 3929 | &prd, &label, 1, &evpn); |
d62a17ae | 3930 | |
3931 | return ret; | |
3932 | } | |
3933 | ||
3934 | static void evpn_mpattr_encode_type5(struct stream *s, struct prefix *p, | |
996c9314 | 3935 | struct prefix_rd *prd, mpls_label_t *label, |
d7c0a89a | 3936 | uint32_t num_labels, struct attr *attr) |
d62a17ae | 3937 | { |
3938 | int len; | |
3939 | char temp[16]; | |
3940 | struct evpn_addr *p_evpn_p; | |
3941 | ||
3942 | memset(&temp, 0, 16); | |
b03b8898 | 3943 | if (p->family != AF_EVPN) |
d62a17ae | 3944 | return; |
3945 | p_evpn_p = &(p->u.prefix_evpn); | |
3946 | ||
e9fc2840 | 3947 | /* len denites the total len of IP and GW-IP in the route |
523cafc4 | 3948 | IP and GW-IP have to be both ipv4 or ipv6 |
3949 | */ | |
3714a385 | 3950 | if (IS_IPADDR_V4(&p_evpn_p->prefix_addr.ip)) |
e9fc2840 | 3951 | len = 8; /* IP and GWIP are both ipv4 */ |
d62a17ae | 3952 | else |
e9fc2840 | 3953 | len = 32; /* IP and GWIP are both ipv6 */ |
d62a17ae | 3954 | /* Prefix contains RD, ESI, EthTag, IP length, IP, GWIP and VNI */ |
3955 | stream_putc(s, 8 + 10 + 4 + 1 + len + 3); | |
3956 | stream_put(s, prd->val, 8); | |
0af35d90 | 3957 | if (attr) |
d62a17ae | 3958 | stream_put(s, &(attr->evpn_overlay.eth_s_id), 10); |
3959 | else | |
3960 | stream_put(s, &temp, 10); | |
3714a385 | 3961 | stream_putl(s, p_evpn_p->prefix_addr.eth_tag); |
3962 | stream_putc(s, p_evpn_p->prefix_addr.ip_prefix_length); | |
3963 | if (IS_IPADDR_V4(&p_evpn_p->prefix_addr.ip)) | |
3964 | stream_put_ipv4(s, p_evpn_p->prefix_addr.ip.ipaddr_v4.s_addr); | |
d62a17ae | 3965 | else |
3714a385 | 3966 | stream_put(s, &p_evpn_p->prefix_addr.ip.ipaddr_v6, 16); |
0af35d90 | 3967 | if (attr) { |
3714a385 | 3968 | if (IS_IPADDR_V4(&p_evpn_p->prefix_addr.ip)) |
d62a17ae | 3969 | stream_put_ipv4(s, |
3970 | attr->evpn_overlay.gw_ip.ipv4.s_addr); | |
3971 | else | |
3972 | stream_put(s, &(attr->evpn_overlay.gw_ip.ipv6), 16); | |
3973 | } else { | |
3714a385 | 3974 | if (IS_IPADDR_V4(&p_evpn_p->prefix_addr.ip)) |
d62a17ae | 3975 | stream_put_ipv4(s, 0); |
3976 | else | |
3977 | stream_put(s, &temp, 16); | |
3978 | } | |
3979 | ||
b57ba6d2 | 3980 | if (num_labels) |
d62a17ae | 3981 | stream_put(s, label, 3); |
3982 | else | |
3983 | stream_put3(s, 0); | |
128ea8ab | 3984 | } |
3985 | ||
3986 | /* | |
3987 | * Cleanup specific VNI upon EVPN (advertise-all-vni) being disabled. | |
3988 | */ | |
d62a17ae | 3989 | static void cleanup_vni_on_disable(struct hash_backet *backet, struct bgp *bgp) |
128ea8ab | 3990 | { |
d62a17ae | 3991 | struct bgpevpn *vpn = (struct bgpevpn *)backet->data; |
128ea8ab | 3992 | |
d62a17ae | 3993 | /* Remove EVPN routes and schedule for processing. */ |
3994 | delete_routes_for_vni(bgp, vpn); | |
128ea8ab | 3995 | |
d62a17ae | 3996 | /* Clear "live" flag and see if hash needs to be freed. */ |
3997 | UNSET_FLAG(vpn->flags, VNI_FLAG_LIVE); | |
3998 | if (!is_vni_configured(vpn)) | |
3999 | bgp_evpn_free(bgp, vpn); | |
128ea8ab | 4000 | } |
4001 | ||
4002 | /* | |
4003 | * Free a VNI entry; iterator function called during cleanup. | |
4004 | */ | |
d62a17ae | 4005 | static void free_vni_entry(struct hash_backet *backet, struct bgp *bgp) |
128ea8ab | 4006 | { |
0a1a07cb | 4007 | struct bgpevpn *vpn = (struct bgpevpn *)backet->data; |
128ea8ab | 4008 | |
d62a17ae | 4009 | delete_all_vni_routes(bgp, vpn); |
4010 | bgp_evpn_free(bgp, vpn); | |
128ea8ab | 4011 | } |
4012 | ||
c581d8b0 MK |
4013 | /* |
4014 | * Derive AUTO import RT for BGP VRF - L3VNI | |
4015 | */ | |
4016 | static void evpn_auto_rt_import_add_for_vrf(struct bgp *bgp_vrf) | |
4017 | { | |
10ebe1ab MK |
4018 | struct bgp *bgp_def = NULL; |
4019 | ||
c581d8b0 | 4020 | form_auto_rt(bgp_vrf, bgp_vrf->l3vni, bgp_vrf->vrf_import_rtl); |
10ebe1ab MK |
4021 | UNSET_FLAG(bgp_vrf->vrf_flags, BGP_VRF_IMPORT_RT_CFGD); |
4022 | ||
4023 | /* Map RT to VRF */ | |
4024 | bgp_def = bgp_get_default(); | |
4025 | if (!bgp_def) | |
4026 | return; | |
4027 | bgp_evpn_map_vrf_to_its_rts(bgp_vrf); | |
c581d8b0 MK |
4028 | } |
4029 | ||
4030 | /* | |
4031 | * Delete AUTO import RT from BGP VRF - L3VNI | |
4032 | */ | |
4033 | static void evpn_auto_rt_import_delete_for_vrf(struct bgp *bgp_vrf) | |
4034 | { | |
4035 | evpn_rt_delete_auto(bgp_vrf, bgp_vrf->l3vni, bgp_vrf->vrf_import_rtl); | |
4036 | } | |
4037 | ||
4038 | /* | |
4039 | * Derive AUTO export RT for BGP VRF - L3VNI | |
4040 | */ | |
4041 | static void evpn_auto_rt_export_add_for_vrf(struct bgp *bgp_vrf) | |
4042 | { | |
4043 | UNSET_FLAG(bgp_vrf->vrf_flags, BGP_VRF_EXPORT_RT_CFGD); | |
4044 | form_auto_rt(bgp_vrf, bgp_vrf->l3vni, bgp_vrf->vrf_export_rtl); | |
4045 | } | |
4046 | ||
4047 | /* | |
4048 | * Delete AUTO export RT from BGP VRF - L3VNI | |
4049 | */ | |
4050 | static void evpn_auto_rt_export_delete_for_vrf(struct bgp *bgp_vrf) | |
4051 | { | |
4052 | evpn_rt_delete_auto(bgp_vrf, bgp_vrf->l3vni, bgp_vrf->vrf_export_rtl); | |
4053 | } | |
128ea8ab | 4054 | |
f1f8b53c MK |
4055 | static void bgp_evpn_handle_export_rt_change_for_vrf(struct bgp *bgp_vrf) |
4056 | { | |
4057 | struct bgp *bgp_def = NULL; | |
4058 | struct listnode *node = NULL; | |
4059 | struct bgpevpn *vpn = NULL; | |
4060 | ||
4061 | bgp_def = bgp_get_default(); | |
4062 | if (!bgp_def) | |
4063 | return; | |
4064 | ||
4992b4ae MK |
4065 | /* update all type-5 routes */ |
4066 | update_advertise_vrf_routes(bgp_vrf); | |
4067 | ||
4068 | /* update all type-2 routes */ | |
f1f8b53c MK |
4069 | for (ALL_LIST_ELEMENTS_RO(bgp_vrf->l2vnis, node, vpn)) |
4070 | update_routes_for_vni(bgp_def, vpn); | |
4071 | } | |
4072 | ||
bf1061d8 VB |
4073 | /* |
4074 | * Handle autort change for a given VNI. | |
4075 | */ | |
4076 | static void update_autort_vni(struct hash_backet *backet, struct bgp *bgp) | |
4077 | { | |
4078 | struct bgpevpn *vpn = backet->data; | |
bf1061d8 | 4079 | |
bf1061d8 VB |
4080 | if (!is_import_rt_configured(vpn)) { |
4081 | if (is_vni_live(vpn)) | |
4082 | bgp_evpn_uninstall_routes(bgp, vpn); | |
4083 | bgp_evpn_unmap_vni_from_its_rts(bgp, vpn); | |
bf1061d8 VB |
4084 | list_delete_all_node(vpn->import_rtl); |
4085 | bgp_evpn_derive_auto_rt_import(bgp, vpn); | |
4086 | if (is_vni_live(vpn)) | |
4087 | bgp_evpn_install_routes(bgp, vpn); | |
4088 | } | |
4089 | if (!is_export_rt_configured(vpn)) { | |
bf1061d8 VB |
4090 | list_delete_all_node(vpn->export_rtl); |
4091 | bgp_evpn_derive_auto_rt_export(bgp, vpn); | |
4092 | if (is_vni_live(vpn)) | |
4093 | bgp_evpn_handle_export_rt_change(bgp, vpn); | |
4094 | } | |
4095 | } | |
4096 | ||
128ea8ab | 4097 | /* |
4098 | * Public functions. | |
4099 | */ | |
4100 | ||
5424b7ba | 4101 | /* withdraw type-5 route corresponding to ip prefix */ |
31310b25 | 4102 | void bgp_evpn_withdraw_type5_route(struct bgp *bgp_vrf, struct prefix *p, |
5424b7ba MK |
4103 | afi_t afi, safi_t safi) |
4104 | { | |
4105 | int ret = 0; | |
4106 | struct prefix_evpn evp; | |
4107 | char buf[PREFIX_STRLEN]; | |
4108 | ||
31310b25 | 4109 | build_type5_prefix_from_ip_prefix(&evp, p); |
5424b7ba MK |
4110 | ret = delete_evpn_type5_route(bgp_vrf, &evp); |
4111 | if (ret) { | |
af4c2728 | 4112 | flog_err( |
14454c9f | 4113 | BGP_ERR_EVPN_ROUTE_DELETE, |
996c9314 LB |
4114 | "%u failed to delete type-5 route for prefix %s in vrf %s", |
4115 | bgp_vrf->vrf_id, prefix2str(p, buf, sizeof(buf)), | |
4116 | vrf_id_to_name(bgp_vrf->vrf_id)); | |
5424b7ba MK |
4117 | } |
4118 | } | |
4119 | ||
342dd0c6 | 4120 | /* withdraw all type-5 routes for an address family */ |
996c9314 | 4121 | void bgp_evpn_withdraw_type5_routes(struct bgp *bgp_vrf, afi_t afi, safi_t safi) |
342dd0c6 | 4122 | { |
4123 | struct bgp_table *table = NULL; | |
4124 | struct bgp_node *rn = NULL; | |
25f2ca53 | 4125 | struct bgp_info *ri; |
342dd0c6 | 4126 | |
053905d2 | 4127 | table = bgp_vrf->rib[afi][safi]; |
25f2ca53 | 4128 | for (rn = bgp_table_top(table); rn; rn = bgp_route_next(rn)) { |
4129 | /* Only care about "selected" routes - non-imported. */ | |
4130 | /* TODO: Support for AddPath for EVPN. */ | |
4131 | for (ri = rn->info; ri; ri = ri->next) { | |
996c9314 LB |
4132 | if (CHECK_FLAG(ri->flags, BGP_INFO_SELECTED) |
4133 | && (!ri->extra || !ri->extra->parent)) { | |
25f2ca53 | 4134 | bgp_evpn_withdraw_type5_route(bgp_vrf, &rn->p, |
4135 | afi, safi); | |
4136 | break; | |
4137 | } | |
4138 | } | |
4139 | } | |
5424b7ba | 4140 | } |
342dd0c6 | 4141 | |
2f69f6d3 | 4142 | /* |
4143 | * Advertise IP prefix as type-5 route. The afi/safi and src_attr passed | |
4144 | * to this function correspond to those of the source IP prefix (best | |
4145 | * path in the case of the attr. In the case of a local prefix (when we | |
4146 | * are advertising local subnets), the src_attr will be NULL. | |
4147 | */ | |
31310b25 | 4148 | void bgp_evpn_advertise_type5_route(struct bgp *bgp_vrf, struct prefix *p, |
996c9314 LB |
4149 | struct attr *src_attr, afi_t afi, |
4150 | safi_t safi) | |
5424b7ba MK |
4151 | { |
4152 | int ret = 0; | |
4153 | struct prefix_evpn evp; | |
4154 | char buf[PREFIX_STRLEN]; | |
7c82b312 | 4155 | |
31310b25 | 4156 | build_type5_prefix_from_ip_prefix(&evp, p); |
2f69f6d3 | 4157 | ret = update_evpn_type5_route(bgp_vrf, &evp, src_attr); |
4158 | if (ret) | |
af4c2728 | 4159 | flog_err(BGP_ERR_EVPN_ROUTE_CREATE, |
14454c9f DS |
4160 | "%u: Failed to create type-5 route for prefix %s", |
4161 | bgp_vrf->vrf_id, prefix2str(p, buf, sizeof(buf))); | |
342dd0c6 | 4162 | } |
4163 | ||
2f69f6d3 | 4164 | /* Inject all prefixes of a particular address-family (currently, IPv4 or |
4165 | * IPv6 unicast) into EVPN as type-5 routes. This is invoked when the | |
4166 | * advertisement is enabled. | |
4167 | */ | |
996c9314 LB |
4168 | void bgp_evpn_advertise_type5_routes(struct bgp *bgp_vrf, afi_t afi, |
4169 | safi_t safi) | |
342dd0c6 | 4170 | { |
4171 | struct bgp_table *table = NULL; | |
4172 | struct bgp_node *rn = NULL; | |
2f69f6d3 | 4173 | struct bgp_info *ri; |
342dd0c6 | 4174 | |
053905d2 | 4175 | table = bgp_vrf->rib[afi][safi]; |
31310b25 | 4176 | for (rn = bgp_table_top(table); rn; rn = bgp_route_next(rn)) { |
2f69f6d3 | 4177 | /* Need to identify the "selected" route entry to use its |
25f2ca53 | 4178 | * attribute. Also, we only consider "non-imported" routes. |
2f69f6d3 | 4179 | * TODO: Support for AddPath for EVPN. |
4180 | */ | |
4181 | for (ri = rn->info; ri; ri = ri->next) { | |
996c9314 LB |
4182 | if (CHECK_FLAG(ri->flags, BGP_INFO_SELECTED) |
4183 | && (!ri->extra || !ri->extra->parent)) { | |
53c84f78 MK |
4184 | |
4185 | /* apply the route-map */ | |
4186 | if (bgp_vrf->adv_cmd_rmap[afi][safi].map) { | |
4187 | int ret = 0; | |
4188 | ||
996c9314 LB |
4189 | ret = route_map_apply( |
4190 | bgp_vrf->adv_cmd_rmap[afi][safi] | |
4191 | .map, | |
4192 | &rn->p, RMAP_BGP, ri); | |
53c84f78 MK |
4193 | if (ret == RMAP_DENYMATCH) |
4194 | continue; | |
4195 | } | |
996c9314 LB |
4196 | bgp_evpn_advertise_type5_route( |
4197 | bgp_vrf, &rn->p, ri->attr, afi, safi); | |
2f69f6d3 | 4198 | break; |
4199 | } | |
4200 | } | |
31310b25 | 4201 | } |
342dd0c6 | 4202 | } |
4203 | ||
996c9314 | 4204 | void evpn_rt_delete_auto(struct bgp *bgp, vni_t vni, struct list *rtl) |
c581d8b0 MK |
4205 | { |
4206 | struct listnode *node, *nnode, *node_to_del; | |
4207 | struct ecommunity *ecom, *ecom_auto; | |
4208 | struct ecommunity_val eval; | |
4209 | ||
bf1061d8 VB |
4210 | if (bgp->advertise_autort_rfc8365) |
4211 | vni |= EVPN_AUTORT_VXLAN; | |
c581d8b0 MK |
4212 | encode_route_target_as((bgp->as & 0xFFFF), vni, &eval); |
4213 | ||
4214 | ecom_auto = ecommunity_new(); | |
4215 | ecommunity_add_val(ecom_auto, &eval); | |
4216 | node_to_del = NULL; | |
4217 | ||
4218 | for (ALL_LIST_ELEMENTS(rtl, node, nnode, ecom)) { | |
4219 | if (ecommunity_match(ecom, ecom_auto)) { | |
4220 | ecommunity_free(&ecom); | |
4221 | node_to_del = node; | |
4222 | } | |
4223 | } | |
4224 | ||
4225 | if (node_to_del) | |
4226 | list_delete_node(rtl, node_to_del); | |
4227 | ||
4228 | ecommunity_free(&ecom_auto); | |
4229 | } | |
4230 | ||
4231 | void bgp_evpn_configure_import_rt_for_vrf(struct bgp *bgp_vrf, | |
10ebe1ab | 4232 | struct ecommunity *ecomadd) |
c581d8b0 | 4233 | { |
5ba238b7 MK |
4234 | /* uninstall routes from vrf */ |
4235 | uninstall_routes_for_vrf(bgp_vrf); | |
10ebe1ab MK |
4236 | |
4237 | /* Cleanup the RT to VRF mapping */ | |
4238 | bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf); | |
4239 | ||
c581d8b0 MK |
4240 | /* Remove auto generated RT */ |
4241 | evpn_auto_rt_import_delete_for_vrf(bgp_vrf); | |
4242 | ||
4243 | /* Add the newly configured RT to RT list */ | |
4244 | listnode_add_sort(bgp_vrf->vrf_import_rtl, ecomadd); | |
4245 | SET_FLAG(bgp_vrf->vrf_flags, BGP_VRF_IMPORT_RT_CFGD); | |
4246 | ||
10ebe1ab MK |
4247 | /* map VRF to its RTs */ |
4248 | bgp_evpn_map_vrf_to_its_rts(bgp_vrf); | |
4249 | ||
5ba238b7 MK |
4250 | /* install routes matching the new VRF */ |
4251 | install_routes_for_vrf(bgp_vrf); | |
c581d8b0 MK |
4252 | } |
4253 | ||
4254 | void bgp_evpn_unconfigure_import_rt_for_vrf(struct bgp *bgp_vrf, | |
4255 | struct ecommunity *ecomdel) | |
4256 | { | |
4257 | struct listnode *node = NULL, *nnode = NULL, *node_to_del = NULL; | |
4258 | struct ecommunity *ecom = NULL; | |
4259 | ||
5ba238b7 MK |
4260 | /* uninstall routes from vrf */ |
4261 | uninstall_routes_for_vrf(bgp_vrf); | |
10ebe1ab MK |
4262 | |
4263 | /* Cleanup the RT to VRF mapping */ | |
4264 | bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf); | |
4265 | ||
c581d8b0 MK |
4266 | /* remove the RT from the RT list */ |
4267 | for (ALL_LIST_ELEMENTS(bgp_vrf->vrf_import_rtl, node, nnode, ecom)) { | |
4268 | if (ecommunity_match(ecom, ecomdel)) { | |
4269 | ecommunity_free(&ecom); | |
4270 | node_to_del = node; | |
4271 | break; | |
4272 | } | |
4273 | } | |
4274 | ||
4275 | if (node_to_del) | |
4276 | list_delete_node(bgp_vrf->vrf_import_rtl, node_to_del); | |
4277 | ||
b3a4db3d | 4278 | assert(bgp_vrf->vrf_import_rtl); |
c581d8b0 | 4279 | /* fallback to auto import rt, if this was the last RT */ |
1230a82d | 4280 | if (bgp_vrf->vrf_import_rtl && list_isempty(bgp_vrf->vrf_import_rtl)) { |
c581d8b0 MK |
4281 | UNSET_FLAG(bgp_vrf->vrf_flags, BGP_VRF_IMPORT_RT_CFGD); |
4282 | evpn_auto_rt_import_add_for_vrf(bgp_vrf); | |
4283 | } | |
4284 | ||
10ebe1ab MK |
4285 | /* map VRFs to its RTs */ |
4286 | bgp_evpn_map_vrf_to_its_rts(bgp_vrf); | |
4287 | ||
5ba238b7 MK |
4288 | /* install routes matching this new RT */ |
4289 | install_routes_for_vrf(bgp_vrf); | |
c581d8b0 MK |
4290 | } |
4291 | ||
4292 | void bgp_evpn_configure_export_rt_for_vrf(struct bgp *bgp_vrf, | |
4293 | struct ecommunity *ecomadd) | |
4294 | { | |
4295 | /* remove auto-generated RT */ | |
4296 | evpn_auto_rt_export_delete_for_vrf(bgp_vrf); | |
4297 | ||
4298 | /* Add the new RT to the RT list */ | |
4299 | listnode_add_sort(bgp_vrf->vrf_export_rtl, ecomadd); | |
4300 | SET_FLAG(bgp_vrf->vrf_flags, BGP_VRF_EXPORT_RT_CFGD); | |
4301 | ||
f1f8b53c | 4302 | bgp_evpn_handle_export_rt_change_for_vrf(bgp_vrf); |
c581d8b0 MK |
4303 | } |
4304 | ||
4305 | void bgp_evpn_unconfigure_export_rt_for_vrf(struct bgp *bgp_vrf, | |
4306 | struct ecommunity *ecomdel) | |
4307 | { | |
4308 | struct listnode *node = NULL, *nnode = NULL, *node_to_del = NULL; | |
4309 | struct ecommunity *ecom = NULL; | |
4310 | ||
4311 | /* Remove the RT from the RT list */ | |
4312 | for (ALL_LIST_ELEMENTS(bgp_vrf->vrf_export_rtl, node, nnode, ecom)) { | |
4313 | if (ecommunity_match(ecom, ecomdel)) { | |
4314 | ecommunity_free(&ecom); | |
4315 | node_to_del = node; | |
4316 | break; | |
4317 | } | |
4318 | } | |
4319 | ||
4320 | if (node_to_del) | |
4321 | list_delete_node(bgp_vrf->vrf_export_rtl, node_to_del); | |
4322 | ||
1525e99f DS |
4323 | /* |
4324 | * Temporary assert to make SA happy. | |
4325 | * The ALL_LIST_ELEMENTS macro above has a NULL check | |
4326 | * which means that SA is going to complain about | |
4327 | * the list_isempty call, which doesn't NULL check. | |
4328 | * So until we get this situation cleaned up, here | |
4329 | * we are. | |
4330 | */ | |
4331 | assert(bgp_vrf->vrf_export_rtl); | |
4332 | ||
c581d8b0 | 4333 | /* fall back to auto-generated RT if this was the last RT */ |
1525e99f | 4334 | if (list_isempty(bgp_vrf->vrf_export_rtl)) { |
c581d8b0 MK |
4335 | UNSET_FLAG(bgp_vrf->vrf_flags, BGP_VRF_EXPORT_RT_CFGD); |
4336 | evpn_auto_rt_export_add_for_vrf(bgp_vrf); | |
4337 | } | |
4338 | ||
f1f8b53c | 4339 | bgp_evpn_handle_export_rt_change_for_vrf(bgp_vrf); |
c581d8b0 MK |
4340 | } |
4341 | ||
2d48ee25 | 4342 | /* |
4343 | * Handle change to BGP router id. This is invoked twice by the change | |
4344 | * handler, first before the router id has been changed and then after | |
4345 | * the router id has been changed. The first invocation will result in | |
676f83b9 | 4346 | * local routes for all VNIs/VRF being deleted and withdrawn and the next |
2d48ee25 | 4347 | * will result in the routes being re-advertised. |
4348 | */ | |
d62a17ae | 4349 | void bgp_evpn_handle_router_id_update(struct bgp *bgp, int withdraw) |
2d48ee25 | 4350 | { |
676f83b9 | 4351 | if (withdraw) { |
4352 | ||
4353 | /* delete and withdraw all the type-5 routes | |
523cafc4 | 4354 | stored in the global table for this vrf |
4355 | */ | |
80b140af | 4356 | withdraw_router_id_vrf(bgp); |
676f83b9 | 4357 | |
4358 | /* delete all the VNI routes (type-2/type-3) routes for all the | |
523cafc4 | 4359 | * L2-VNIs |
4360 | */ | |
d62a17ae | 4361 | hash_iterate(bgp->vnihash, |
4362 | (void (*)(struct hash_backet *, | |
4363 | void *))withdraw_router_id_vni, | |
4364 | bgp); | |
676f83b9 | 4365 | } else { |
4366 | ||
4367 | /* advertise all routes in the vrf as type-5 routes with the new | |
523cafc4 | 4368 | * RD |
4369 | */ | |
80b140af | 4370 | update_router_id_vrf(bgp); |
676f83b9 | 4371 | |
4372 | /* advertise all the VNI routes (type-2/type-3) routes with the | |
523cafc4 | 4373 | * new RD |
4374 | */ | |
d62a17ae | 4375 | hash_iterate(bgp->vnihash, |
4376 | (void (*)(struct hash_backet *, | |
4377 | void *))update_router_id_vni, | |
4378 | bgp); | |
676f83b9 | 4379 | } |
2d48ee25 | 4380 | } |
4381 | ||
bf1061d8 VB |
4382 | /* |
4383 | * Handle change to auto-RT algorithm - update and advertise local routes. | |
4384 | */ | |
4385 | void bgp_evpn_handle_autort_change(struct bgp *bgp) | |
4386 | { | |
4387 | hash_iterate(bgp->vnihash, | |
4388 | (void (*)(struct hash_backet *, | |
4389 | void*))update_autort_vni, | |
4390 | bgp); | |
4391 | } | |
4392 | ||
90e60aa7 | 4393 | /* |
4394 | * Handle change to export RT - update and advertise local routes. | |
4395 | */ | |
d62a17ae | 4396 | int bgp_evpn_handle_export_rt_change(struct bgp *bgp, struct bgpevpn *vpn) |
90e60aa7 | 4397 | { |
d62a17ae | 4398 | return update_routes_for_vni(bgp, vpn); |
90e60aa7 | 4399 | } |
4400 | ||
996c9314 | 4401 | void bgp_evpn_handle_vrf_rd_change(struct bgp *bgp_vrf, int withdraw) |
676f83b9 | 4402 | { |
4403 | if (withdraw) | |
4404 | delete_withdraw_vrf_routes(bgp_vrf); | |
4405 | else | |
4406 | update_advertise_vrf_routes(bgp_vrf); | |
4407 | } | |
4408 | ||
90e60aa7 | 4409 | /* |
4410 | * Handle change to RD. This is invoked twice by the change handler, | |
4411 | * first before the RD has been changed and then after the RD has | |
4412 | * been changed. The first invocation will result in local routes | |
4413 | * of this VNI being deleted and withdrawn and the next will result | |
4414 | * in the routes being re-advertised. | |
4415 | */ | |
d62a17ae | 4416 | void bgp_evpn_handle_rd_change(struct bgp *bgp, struct bgpevpn *vpn, |
4417 | int withdraw) | |
90e60aa7 | 4418 | { |
d62a17ae | 4419 | if (withdraw) |
4420 | delete_withdraw_vni_routes(bgp, vpn); | |
4421 | else | |
4422 | update_advertise_vni_routes(bgp, vpn); | |
90e60aa7 | 4423 | } |
4424 | ||
4425 | /* | |
4426 | * Install routes for this VNI. Invoked upon change to Import RT. | |
4427 | */ | |
d62a17ae | 4428 | int bgp_evpn_install_routes(struct bgp *bgp, struct bgpevpn *vpn) |
90e60aa7 | 4429 | { |
d62a17ae | 4430 | return install_routes_for_vni(bgp, vpn); |
90e60aa7 | 4431 | } |
4432 | ||
4433 | /* | |
4434 | * Uninstall all routes installed for this VNI. Invoked upon change | |
4435 | * to Import RT. | |
4436 | */ | |
d62a17ae | 4437 | int bgp_evpn_uninstall_routes(struct bgp *bgp, struct bgpevpn *vpn) |
90e60aa7 | 4438 | { |
d62a17ae | 4439 | return uninstall_routes_for_vni(bgp, vpn); |
90e60aa7 | 4440 | } |
4441 | ||
b16031a2 | 4442 | /* |
b57ba6d2 | 4443 | * TODO: Hardcoded for a maximum of 2 VNIs right now |
b16031a2 | 4444 | */ |
d7c0a89a | 4445 | char *bgp_evpn_label2str(mpls_label_t *label, uint32_t num_labels, char *buf, |
996c9314 | 4446 | int len) |
b16031a2 | 4447 | { |
b57ba6d2 | 4448 | vni_t vni1, vni2; |
b16031a2 | 4449 | |
b57ba6d2 MK |
4450 | vni1 = label2vni(label); |
4451 | if (num_labels == 2) { | |
996c9314 | 4452 | vni2 = label2vni(label + 1); |
b57ba6d2 MK |
4453 | snprintf(buf, len, "%u/%u", vni1, vni2); |
4454 | } else | |
4455 | snprintf(buf, len, "%u", vni1); | |
d62a17ae | 4456 | return buf; |
b16031a2 | 4457 | } |
4458 | ||
9c92b5f7 MK |
4459 | /* |
4460 | * Function to convert evpn route to json format. | |
4461 | * NOTE: We don't use prefix2str as the output here is a bit different. | |
4462 | */ | |
57f7feb6 | 4463 | void bgp_evpn_route2json(struct prefix_evpn *p, json_object *json) |
9c92b5f7 | 4464 | { |
b682f6de | 4465 | char buf1[ETHER_ADDR_STRLEN]; |
4466 | char buf2[PREFIX2STR_BUFFER]; | |
9c92b5f7 | 4467 | |
b682f6de | 4468 | if (!json) |
4469 | return; | |
9c92b5f7 | 4470 | |
dff8f48d | 4471 | if (p->prefix.route_type == BGP_EVPN_IMET_ROUTE) { |
b682f6de | 4472 | json_object_int_add(json, "routeType", p->prefix.route_type); |
3714a385 | 4473 | json_object_int_add(json, "ethTag", |
4474 | p->prefix.imet_addr.eth_tag); | |
57f7feb6 | 4475 | json_object_int_add(json, "ipLen", |
3714a385 | 4476 | is_evpn_prefix_ipaddr_v4(p) |
57f7feb6 MK |
4477 | ? IPV4_MAX_BITLEN |
4478 | : IPV6_MAX_BITLEN); | |
b682f6de | 4479 | json_object_string_add(json, "ip", |
3714a385 | 4480 | inet_ntoa(p->prefix.imet_addr.ip.ipaddr_v4)); |
57f7feb6 | 4481 | } else if (p->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE) { |
3714a385 | 4482 | if (is_evpn_prefix_ipaddr_none(p)) { |
57f7feb6 MK |
4483 | json_object_int_add(json, "routeType", |
4484 | p->prefix.route_type); | |
3714a385 | 4485 | json_object_int_add(json, "ethTag", |
4486 | p->prefix.macip_addr.eth_tag); | |
57f7feb6 MK |
4487 | json_object_int_add(json, "macLen", 8 * ETH_ALEN); |
4488 | json_object_string_add(json, "mac", | |
3714a385 | 4489 | prefix_mac2str(&p->prefix.macip_addr.mac, |
57f7feb6 MK |
4490 | buf1, |
4491 | sizeof(buf1))); | |
dff8f48d | 4492 | } else { |
d7c0a89a | 4493 | uint8_t family; |
dff8f48d | 4494 | |
3714a385 | 4495 | family = is_evpn_prefix_ipaddr_v4(p) ? AF_INET |
57f7feb6 | 4496 | : AF_INET6; |
dff8f48d | 4497 | |
b682f6de | 4498 | json_object_int_add(json, "routeType", |
57f7feb6 | 4499 | p->prefix.route_type); |
3714a385 | 4500 | json_object_int_add(json, "ethTag", |
4501 | p->prefix.macip_addr.eth_tag); | |
57f7feb6 | 4502 | json_object_int_add(json, "macLen", 8 * ETH_ALEN); |
b682f6de | 4503 | json_object_string_add(json, "mac", |
3714a385 | 4504 | prefix_mac2str(&p->prefix.macip_addr.mac, |
57f7feb6 MK |
4505 | buf1, |
4506 | sizeof(buf1))); | |
b682f6de | 4507 | json_object_int_add(json, "ipLen", |
3714a385 | 4508 | is_evpn_prefix_ipaddr_v4(p) |
57f7feb6 MK |
4509 | ? IPV4_MAX_BITLEN |
4510 | : IPV6_MAX_BITLEN); | |
4511 | json_object_string_add( | |
4512 | json, "ip", | |
3714a385 | 4513 | inet_ntop(family, |
4514 | &p->prefix.macip_addr.ip.ip.addr, | |
4515 | buf2, | |
57f7feb6 | 4516 | PREFIX2STR_BUFFER)); |
dff8f48d MK |
4517 | } |
4518 | } else { | |
4519 | /* Currently, this is to cater to other AF_ETHERNET code. */ | |
4520 | } | |
9c92b5f7 MK |
4521 | } |
4522 | ||
520d5d76 | 4523 | /* |
4524 | * Function to convert evpn route to string. | |
4525 | * NOTE: We don't use prefix2str as the output here is a bit different. | |
4526 | */ | |
d62a17ae | 4527 | char *bgp_evpn_route2str(struct prefix_evpn *p, char *buf, int len) |
4528 | { | |
4529 | char buf1[ETHER_ADDR_STRLEN]; | |
4530 | char buf2[PREFIX2STR_BUFFER]; | |
50f74cf1 | 4531 | char buf3[ESI_STR_LEN]; |
d62a17ae | 4532 | |
4533 | if (p->prefix.route_type == BGP_EVPN_IMET_ROUTE) { | |
554cd77a | 4534 | snprintf(buf, len, "[%d]:[%d]:[%d]:[%s]", p->prefix.route_type, |
3714a385 | 4535 | p->prefix.imet_addr.eth_tag, |
4536 | is_evpn_prefix_ipaddr_v4(p) ? IPV4_MAX_BITLEN | |
d62a17ae | 4537 | : IPV6_MAX_BITLEN, |
3714a385 | 4538 | inet_ntoa(p->prefix.imet_addr.ip.ipaddr_v4)); |
d62a17ae | 4539 | } else if (p->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE) { |
3714a385 | 4540 | if (is_evpn_prefix_ipaddr_none(p)) |
554cd77a VB |
4541 | snprintf(buf, len, "[%d]:[%d]:[%d]:[%s]", |
4542 | p->prefix.route_type, | |
3714a385 | 4543 | p->prefix.macip_addr.eth_tag, |
554cd77a | 4544 | 8 * ETH_ALEN, |
3714a385 | 4545 | prefix_mac2str(&p->prefix.macip_addr.mac, buf1, |
d62a17ae | 4546 | sizeof(buf1))); |
4547 | else { | |
d7c0a89a | 4548 | uint8_t family; |
d62a17ae | 4549 | |
3714a385 | 4550 | family = is_evpn_prefix_ipaddr_v4(p) ? AF_INET |
d62a17ae | 4551 | : AF_INET6; |
554cd77a VB |
4552 | snprintf(buf, len, "[%d]:[%d]:[%d]:[%s]:[%d]:[%s]", |
4553 | p->prefix.route_type, | |
3714a385 | 4554 | p->prefix.macip_addr.eth_tag, |
554cd77a | 4555 | 8 * ETH_ALEN, |
3714a385 | 4556 | prefix_mac2str(&p->prefix.macip_addr.mac, buf1, |
d62a17ae | 4557 | sizeof(buf1)), |
4558 | family == AF_INET ? IPV4_MAX_BITLEN | |
4559 | : IPV6_MAX_BITLEN, | |
3714a385 | 4560 | inet_ntop(family, |
4561 | &p->prefix.macip_addr.ip.ip.addr, | |
4562 | buf2, | |
d62a17ae | 4563 | PREFIX2STR_BUFFER)); |
4564 | } | |
342dd0c6 | 4565 | } else if (p->prefix.route_type == BGP_EVPN_IP_PREFIX_ROUTE) { |
554cd77a VB |
4566 | snprintf(buf, len, "[%d]:[%d]:[%d]:[%s]", |
4567 | p->prefix.route_type, | |
3714a385 | 4568 | p->prefix.prefix_addr.eth_tag, |
4569 | p->prefix.prefix_addr.ip_prefix_length, | |
4570 | is_evpn_prefix_ipaddr_v4(p) | |
4571 | ? inet_ntoa(p->prefix.prefix_addr.ip.ipaddr_v4) | |
4572 | : inet6_ntoa(p->prefix.prefix_addr.ip.ipaddr_v6)); | |
50f74cf1 | 4573 | } else if (p->prefix.route_type == BGP_EVPN_ES_ROUTE) { |
4574 | snprintf(buf, len, "[%d]:[%s]:[%d]:[%s]", | |
4575 | p->prefix.route_type, | |
4576 | esi_to_str(&p->prefix.es_addr.esi, buf3, sizeof(buf3)), | |
4577 | is_evpn_prefix_ipaddr_v4(p) ? IPV4_MAX_BITLEN | |
4578 | : IPV6_MAX_BITLEN, | |
4579 | inet_ntoa(p->prefix.es_addr.ip.ipaddr_v4)); | |
d62a17ae | 4580 | } else { |
b03b8898 | 4581 | /* For EVPN route types not supported yet. */ |
f9aa3e55 QY |
4582 | snprintf(buf, len, "(unsupported route type %d)", |
4583 | p->prefix.route_type); | |
d62a17ae | 4584 | } |
4585 | ||
4586 | return (buf); | |
520d5d76 | 4587 | } |
4588 | ||
128ea8ab | 4589 | /* |
4590 | * Encode EVPN prefix in Update (MP_REACH) | |
4591 | */ | |
d62a17ae | 4592 | void bgp_evpn_encode_prefix(struct stream *s, struct prefix *p, |
996c9314 | 4593 | struct prefix_rd *prd, mpls_label_t *label, |
d7c0a89a QY |
4594 | uint32_t num_labels, struct attr *attr, |
4595 | int addpath_encode, uint32_t addpath_tx_id) | |
d62a17ae | 4596 | { |
4597 | struct prefix_evpn *evp = (struct prefix_evpn *)p; | |
b57ba6d2 | 4598 | int len, ipa_len = 0; |
d62a17ae | 4599 | |
4600 | if (addpath_encode) | |
4601 | stream_putl(s, addpath_tx_id); | |
4602 | ||
4603 | /* Route type */ | |
4604 | stream_putc(s, evp->prefix.route_type); | |
4605 | ||
4606 | switch (evp->prefix.route_type) { | |
4607 | case BGP_EVPN_MAC_IP_ROUTE: | |
3714a385 | 4608 | if (is_evpn_prefix_ipaddr_v4(evp)) |
d62a17ae | 4609 | ipa_len = IPV4_MAX_BYTELEN; |
3714a385 | 4610 | else if (is_evpn_prefix_ipaddr_v6(evp)) |
d62a17ae | 4611 | ipa_len = IPV6_MAX_BYTELEN; |
b57ba6d2 MK |
4612 | /* RD, ESI, EthTag, MAC+len, IP len, [IP], 1 VNI */ |
4613 | len = 8 + 10 + 4 + 1 + 6 + 1 + ipa_len + 3; | |
4614 | if (ipa_len && num_labels > 1) /* There are 2 VNIs */ | |
4615 | len += 3; | |
4616 | stream_putc(s, len); | |
996c9314 | 4617 | stream_put(s, prd->val, 8); /* RD */ |
554cd77a VB |
4618 | if (attr) |
4619 | stream_put(s, &attr->evpn_overlay.eth_s_id, ESI_LEN); | |
4620 | else | |
4621 | stream_put(s, 0, 10); | |
3714a385 | 4622 | stream_putl(s, evp->prefix.macip_addr.eth_tag); /* Ethernet Tag ID */ |
28328ea9 | 4623 | stream_putc(s, 8 * ETH_ALEN); /* Mac Addr Len - bits */ |
3714a385 | 4624 | stream_put(s, evp->prefix.macip_addr.mac.octet, 6); /* Mac Addr */ |
4625 | stream_putc(s, 8 * ipa_len); /* IP address Length */ | |
4626 | if (ipa_len) /* IP */ | |
4627 | stream_put(s, &evp->prefix.macip_addr.ip.ip.addr, | |
4628 | ipa_len); | |
b57ba6d2 MK |
4629 | /* 1st label is the L2 VNI */ |
4630 | stream_put(s, label, BGP_LABEL_BYTES); | |
4631 | /* Include 2nd label (L3 VNI) if advertising MAC+IP */ | |
4632 | if (ipa_len && num_labels > 1) | |
996c9314 | 4633 | stream_put(s, label + 1, BGP_LABEL_BYTES); |
d62a17ae | 4634 | break; |
4635 | ||
4636 | case BGP_EVPN_IMET_ROUTE: | |
4637 | stream_putc(s, 17); // TODO: length - assumes IPv4 address | |
4638 | stream_put(s, prd->val, 8); /* RD */ | |
3714a385 | 4639 | stream_putl(s, evp->prefix.imet_addr.eth_tag); /* Ethernet Tag ID */ |
d62a17ae | 4640 | stream_putc(s, IPV4_MAX_BITLEN); /* IP address Length - bits */ |
4641 | /* Originating Router's IP Addr */ | |
3714a385 | 4642 | stream_put_in_addr(s, &evp->prefix.imet_addr.ip.ipaddr_v4); |
d62a17ae | 4643 | break; |
4644 | ||
50f74cf1 | 4645 | case BGP_EVPN_ES_ROUTE: |
4646 | stream_putc(s, 23); /* TODO: length: assumes ipv4 VTEP */ | |
4647 | stream_put(s, prd->val, 8); /* RD */ | |
4648 | stream_put(s, evp->prefix.es_addr.esi.val, 10); /* ESI */ | |
4649 | stream_putc(s, IPV4_MAX_BITLEN); /* IP address Length - bits */ | |
2bb9eff4 DS |
4650 | /* VTEP IP */ |
4651 | stream_put_in_addr(s, &evp->prefix.es_addr.ip.ipaddr_v4); | |
50f74cf1 | 4652 | break; |
4653 | ||
d62a17ae | 4654 | case BGP_EVPN_IP_PREFIX_ROUTE: |
4655 | /* TODO: AddPath support. */ | |
b57ba6d2 | 4656 | evpn_mpattr_encode_type5(s, p, prd, label, num_labels, attr); |
d62a17ae | 4657 | break; |
4658 | ||
4659 | default: | |
4660 | break; | |
4661 | } | |
4662 | } | |
4663 | ||
4664 | int bgp_nlri_parse_evpn(struct peer *peer, struct attr *attr, | |
4665 | struct bgp_nlri *packet, int withdraw) | |
4666 | { | |
d7c0a89a QY |
4667 | uint8_t *pnt; |
4668 | uint8_t *lim; | |
d62a17ae | 4669 | afi_t afi; |
4670 | safi_t safi; | |
d7c0a89a | 4671 | uint32_t addpath_id; |
d62a17ae | 4672 | int addpath_encoded; |
4673 | int psize = 0; | |
d7c0a89a | 4674 | uint8_t rtype; |
d62a17ae | 4675 | struct prefix p; |
4676 | ||
d62a17ae | 4677 | /* Start processing the NLRI - there may be multiple in the MP_REACH */ |
4678 | pnt = packet->nlri; | |
4679 | lim = pnt + packet->length; | |
4680 | afi = packet->afi; | |
4681 | safi = packet->safi; | |
4682 | addpath_id = 0; | |
4683 | ||
4684 | addpath_encoded = | |
4685 | (CHECK_FLAG(peer->af_cap[afi][safi], PEER_CAP_ADDPATH_AF_RX_ADV) | |
4686 | && CHECK_FLAG(peer->af_cap[afi][safi], | |
4687 | PEER_CAP_ADDPATH_AF_TX_RCV)); | |
4688 | ||
4689 | for (; pnt < lim; pnt += psize) { | |
4690 | /* Clear prefix structure. */ | |
4691 | memset(&p, 0, sizeof(struct prefix)); | |
4692 | ||
4693 | /* Deal with path-id if AddPath is supported. */ | |
4694 | if (addpath_encoded) { | |
4695 | /* When packet overflow occurs return immediately. */ | |
4696 | if (pnt + BGP_ADDPATH_ID_LEN > lim) | |
4697 | return -1; | |
4698 | ||
4699 | addpath_id = ntohl(*((uint32_t *)pnt)); | |
4700 | pnt += BGP_ADDPATH_ID_LEN; | |
4701 | } | |
4702 | ||
4703 | /* All EVPN NLRI types start with type and length. */ | |
4704 | if (pnt + 2 > lim) | |
4705 | return -1; | |
4706 | ||
4707 | rtype = *pnt++; | |
3f54c705 | 4708 | psize = *pnt++; |
d62a17ae | 4709 | |
4710 | /* When packet overflow occur return immediately. */ | |
4711 | if (pnt + psize > lim) | |
4712 | return -1; | |
4713 | ||
4714 | switch (rtype) { | |
4715 | case BGP_EVPN_MAC_IP_ROUTE: | |
4716 | if (process_type2_route(peer, afi, safi, | |
4717 | withdraw ? NULL : attr, pnt, | |
4718 | psize, addpath_id)) { | |
af4c2728 | 4719 | flog_err( |
14454c9f | 4720 | BGP_ERR_EVPN_FAIL, |
d62a17ae | 4721 | "%u:%s - Error in processing EVPN type-2 NLRI size %d", |
4722 | peer->bgp->vrf_id, peer->host, psize); | |
4723 | return -1; | |
4724 | } | |
4725 | break; | |
4726 | ||
4727 | case BGP_EVPN_IMET_ROUTE: | |
4728 | if (process_type3_route(peer, afi, safi, | |
4729 | withdraw ? NULL : attr, pnt, | |
4730 | psize, addpath_id)) { | |
af4c2728 | 4731 | flog_err( |
14454c9f | 4732 | BGP_ERR_PKT_PROCESS, |
d62a17ae | 4733 | "%u:%s - Error in processing EVPN type-3 NLRI size %d", |
4734 | peer->bgp->vrf_id, peer->host, psize); | |
4735 | return -1; | |
4736 | } | |
4737 | break; | |
4738 | ||
50f74cf1 | 4739 | case BGP_EVPN_ES_ROUTE: |
4740 | if (process_type4_route(peer, afi, safi, | |
4741 | withdraw ? NULL : attr, pnt, | |
4742 | psize, addpath_id)) { | |
af4c2728 | 4743 | flog_err( |
14454c9f | 4744 | BGP_ERR_PKT_PROCESS, |
50f74cf1 | 4745 | "%u:%s - Error in processing EVPN type-4 NLRI size %d", |
4746 | peer->bgp->vrf_id, peer->host, psize); | |
4747 | return -1; | |
4748 | } | |
4749 | break; | |
4750 | ||
d62a17ae | 4751 | case BGP_EVPN_IP_PREFIX_ROUTE: |
4752 | if (process_type5_route(peer, afi, safi, attr, pnt, | |
4753 | psize, addpath_id, withdraw)) { | |
af4c2728 | 4754 | flog_err( |
14454c9f | 4755 | BGP_ERR_PKT_PROCESS, |
d62a17ae | 4756 | "%u:%s - Error in processing EVPN type-5 NLRI size %d", |
4757 | peer->bgp->vrf_id, peer->host, psize); | |
4758 | return -1; | |
4759 | } | |
4760 | break; | |
4761 | ||
4762 | default: | |
4763 | break; | |
4764 | } | |
4765 | } | |
4766 | ||
4767 | /* Packet length consistency check. */ | |
4768 | if (pnt != lim) | |
4769 | return -1; | |
4770 | ||
4771 | return 0; | |
128ea8ab | 4772 | } |
4773 | ||
10ebe1ab MK |
4774 | /* |
4775 | * Map the RTs (configured or automatically derived) of a VRF to the VRF. | |
4776 | * The mapping will be used during route processing. | |
4777 | * bgp_def: default bgp instance | |
4778 | * bgp_vrf: specific bgp vrf instance on which RT is configured | |
4779 | */ | |
4780 | void bgp_evpn_map_vrf_to_its_rts(struct bgp *bgp_vrf) | |
4781 | { | |
4782 | int i = 0; | |
4783 | struct ecommunity_val *eval = NULL; | |
4784 | struct listnode *node = NULL, *nnode = NULL; | |
4785 | struct ecommunity *ecom = NULL; | |
4786 | ||
4787 | for (ALL_LIST_ELEMENTS(bgp_vrf->vrf_import_rtl, node, nnode, ecom)) { | |
4788 | for (i = 0; i < ecom->size; i++) { | |
4789 | eval = (struct ecommunity_val *)(ecom->val | |
4790 | + (i | |
4791 | * ECOMMUNITY_SIZE)); | |
4792 | map_vrf_to_rt(bgp_vrf, eval); | |
4793 | } | |
4794 | } | |
4795 | } | |
4796 | ||
4797 | /* | |
4798 | * Unmap the RTs (configured or automatically derived) of a VRF from the VRF. | |
4799 | */ | |
4800 | void bgp_evpn_unmap_vrf_from_its_rts(struct bgp *bgp_vrf) | |
4801 | { | |
4802 | int i; | |
4803 | struct ecommunity_val *eval; | |
4804 | struct listnode *node, *nnode; | |
4805 | struct ecommunity *ecom; | |
4806 | ||
4807 | for (ALL_LIST_ELEMENTS(bgp_vrf->vrf_import_rtl, node, nnode, ecom)) { | |
4808 | for (i = 0; i < ecom->size; i++) { | |
4809 | struct vrf_irt_node *irt; | |
4810 | struct ecommunity_val eval_tmp; | |
4811 | ||
4812 | eval = (struct ecommunity_val *)(ecom->val | |
4813 | + (i | |
4814 | * ECOMMUNITY_SIZE)); | |
4815 | /* If using "automatic" RT, we only care about the | |
4816 | * local-admin sub-field. | |
4817 | * This is to facilitate using VNI as the RT for EBGP | |
4818 | * peering too. | |
4819 | */ | |
4820 | memcpy(&eval_tmp, eval, ECOMMUNITY_SIZE); | |
4821 | if (!CHECK_FLAG(bgp_vrf->vrf_flags, | |
4822 | BGP_VRF_IMPORT_RT_CFGD)) | |
4823 | mask_ecom_global_admin(&eval_tmp, eval); | |
4824 | ||
4825 | irt = lookup_vrf_import_rt(&eval_tmp); | |
4826 | if (irt) | |
4827 | unmap_vrf_from_rt(bgp_vrf, irt); | |
4828 | } | |
4829 | } | |
4830 | } | |
4831 | ||
4832 | ||
128ea8ab | 4833 | /* |
4834 | * Map the RTs (configured or automatically derived) of a VNI to the VNI. | |
4835 | * The mapping will be used during route processing. | |
4836 | */ | |
d62a17ae | 4837 | void bgp_evpn_map_vni_to_its_rts(struct bgp *bgp, struct bgpevpn *vpn) |
128ea8ab | 4838 | { |
d62a17ae | 4839 | int i; |
4840 | struct ecommunity_val *eval; | |
4841 | struct listnode *node, *nnode; | |
4842 | struct ecommunity *ecom; | |
128ea8ab | 4843 | |
d62a17ae | 4844 | for (ALL_LIST_ELEMENTS(vpn->import_rtl, node, nnode, ecom)) { |
4845 | for (i = 0; i < ecom->size; i++) { | |
4846 | eval = (struct ecommunity_val *)(ecom->val | |
4847 | + (i | |
4848 | * ECOMMUNITY_SIZE)); | |
4849 | map_vni_to_rt(bgp, vpn, eval); | |
4850 | } | |
4851 | } | |
128ea8ab | 4852 | } |
4853 | ||
4854 | /* | |
4855 | * Unmap the RTs (configured or automatically derived) of a VNI from the VNI. | |
4856 | */ | |
d62a17ae | 4857 | void bgp_evpn_unmap_vni_from_its_rts(struct bgp *bgp, struct bgpevpn *vpn) |
128ea8ab | 4858 | { |
d62a17ae | 4859 | int i; |
4860 | struct ecommunity_val *eval; | |
4861 | struct listnode *node, *nnode; | |
4862 | struct ecommunity *ecom; | |
128ea8ab | 4863 | |
d62a17ae | 4864 | for (ALL_LIST_ELEMENTS(vpn->import_rtl, node, nnode, ecom)) { |
4865 | for (i = 0; i < ecom->size; i++) { | |
4866 | struct irt_node *irt; | |
4867 | struct ecommunity_val eval_tmp; | |
128ea8ab | 4868 | |
d62a17ae | 4869 | eval = (struct ecommunity_val *)(ecom->val |
4870 | + (i | |
4871 | * ECOMMUNITY_SIZE)); | |
4872 | /* If using "automatic" RT, we only care about the | |
4873 | * local-admin sub-field. | |
4874 | * This is to facilitate using VNI as the RT for EBGP | |
4875 | * peering too. | |
4876 | */ | |
4877 | memcpy(&eval_tmp, eval, ECOMMUNITY_SIZE); | |
4878 | if (!is_import_rt_configured(vpn)) | |
4879 | mask_ecom_global_admin(&eval_tmp, eval); | |
128ea8ab | 4880 | |
d62a17ae | 4881 | irt = lookup_import_rt(bgp, &eval_tmp); |
4882 | if (irt) | |
4883 | unmap_vni_from_rt(bgp, vpn, irt); | |
4884 | } | |
4885 | } | |
128ea8ab | 4886 | } |
4887 | ||
4888 | /* | |
4889 | * Derive Import RT automatically for VNI and map VNI to RT. | |
4890 | * The mapping will be used during route processing. | |
4891 | */ | |
d62a17ae | 4892 | void bgp_evpn_derive_auto_rt_import(struct bgp *bgp, struct bgpevpn *vpn) |
128ea8ab | 4893 | { |
c581d8b0 | 4894 | form_auto_rt(bgp, vpn->vni, vpn->import_rtl); |
d62a17ae | 4895 | UNSET_FLAG(vpn->flags, VNI_FLAG_IMPRT_CFGD); |
128ea8ab | 4896 | |
d62a17ae | 4897 | /* Map RT to VNI */ |
4898 | bgp_evpn_map_vni_to_its_rts(bgp, vpn); | |
128ea8ab | 4899 | } |
4900 | ||
4901 | /* | |
4902 | * Derive Export RT automatically for VNI. | |
4903 | */ | |
d62a17ae | 4904 | void bgp_evpn_derive_auto_rt_export(struct bgp *bgp, struct bgpevpn *vpn) |
128ea8ab | 4905 | { |
c581d8b0 | 4906 | form_auto_rt(bgp, vpn->vni, vpn->export_rtl); |
d62a17ae | 4907 | UNSET_FLAG(vpn->flags, VNI_FLAG_EXPRT_CFGD); |
128ea8ab | 4908 | } |
4909 | ||
676f83b9 | 4910 | /* |
4911 | * Derive RD automatically for VNI using passed information - it | |
4912 | * is of the form RouterId:unique-id-for-vni. | |
4913 | */ | |
4914 | void bgp_evpn_derive_auto_rd_for_vrf(struct bgp *bgp) | |
4915 | { | |
92708db6 | 4916 | form_auto_rd(bgp->router_id, bgp->vrf_rd_id, &bgp->vrf_prd); |
676f83b9 | 4917 | } |
4918 | ||
128ea8ab | 4919 | /* |
4920 | * Derive RD automatically for VNI using passed information - it | |
4921 | * is of the form RouterId:unique-id-for-vni. | |
4922 | */ | |
d62a17ae | 4923 | void bgp_evpn_derive_auto_rd(struct bgp *bgp, struct bgpevpn *vpn) |
128ea8ab | 4924 | { |
d62a17ae | 4925 | char buf[100]; |
128ea8ab | 4926 | |
d62a17ae | 4927 | vpn->prd.family = AF_UNSPEC; |
4928 | vpn->prd.prefixlen = 64; | |
4929 | sprintf(buf, "%s:%hu", inet_ntoa(bgp->router_id), vpn->rd_id); | |
cbb65f5e | 4930 | (void)str2prefix_rd(buf, &vpn->prd); |
d62a17ae | 4931 | UNSET_FLAG(vpn->flags, VNI_FLAG_RD_CFGD); |
128ea8ab | 4932 | } |
4933 | ||
4934 | /* | |
4935 | * Lookup VNI. | |
4936 | */ | |
d62a17ae | 4937 | struct bgpevpn *bgp_evpn_lookup_vni(struct bgp *bgp, vni_t vni) |
128ea8ab | 4938 | { |
d62a17ae | 4939 | struct bgpevpn *vpn; |
4940 | struct bgpevpn tmp; | |
128ea8ab | 4941 | |
d62a17ae | 4942 | memset(&tmp, 0, sizeof(struct bgpevpn)); |
4943 | tmp.vni = vni; | |
4944 | vpn = hash_lookup(bgp->vnihash, &tmp); | |
4945 | return vpn; | |
128ea8ab | 4946 | } |
4947 | ||
4948 | /* | |
4949 | * Create a new vpn - invoked upon configuration or zebra notification. | |
4950 | */ | |
d62a17ae | 4951 | struct bgpevpn *bgp_evpn_new(struct bgp *bgp, vni_t vni, |
29c53922 MK |
4952 | struct in_addr originator_ip, |
4953 | vrf_id_t tenant_vrf_id) | |
128ea8ab | 4954 | { |
d62a17ae | 4955 | struct bgpevpn *vpn; |
128ea8ab | 4956 | |
d62a17ae | 4957 | if (!bgp) |
4958 | return NULL; | |
128ea8ab | 4959 | |
d62a17ae | 4960 | vpn = XCALLOC(MTYPE_BGP_EVPN, sizeof(struct bgpevpn)); |
4961 | if (!vpn) | |
4962 | return NULL; | |
128ea8ab | 4963 | |
d62a17ae | 4964 | /* Set values - RD and RT set to defaults. */ |
4965 | vpn->vni = vni; | |
4966 | vpn->originator_ip = originator_ip; | |
29c53922 | 4967 | vpn->tenant_vrf_id = tenant_vrf_id; |
128ea8ab | 4968 | |
d62a17ae | 4969 | /* Initialize route-target import and export lists */ |
4970 | vpn->import_rtl = list_new(); | |
4971 | vpn->import_rtl->cmp = (int (*)(void *, void *))evpn_route_target_cmp; | |
987d8198 | 4972 | vpn->import_rtl->del = evpn_xxport_delete_ecomm; |
d62a17ae | 4973 | vpn->export_rtl = list_new(); |
4974 | vpn->export_rtl->cmp = (int (*)(void *, void *))evpn_route_target_cmp; | |
987d8198 | 4975 | vpn->export_rtl->del = evpn_xxport_delete_ecomm; |
e9eb5f63 | 4976 | bf_assign_index(bm->rd_idspace, vpn->rd_id); |
d62a17ae | 4977 | derive_rd_rt_for_vni(bgp, vpn); |
128ea8ab | 4978 | |
d62a17ae | 4979 | /* Initialize EVPN route table. */ |
960035b2 | 4980 | vpn->route_table = bgp_table_init(bgp, AFI_L2VPN, SAFI_EVPN); |
128ea8ab | 4981 | |
d62a17ae | 4982 | /* Add to hash */ |
4983 | if (!hash_get(bgp->vnihash, vpn, hash_alloc_intern)) { | |
4984 | XFREE(MTYPE_BGP_EVPN, vpn); | |
4985 | return NULL; | |
4986 | } | |
6a8657d0 MK |
4987 | |
4988 | /* add to l2vni list on corresponding vrf */ | |
4989 | bgpevpn_link_to_l3vni(vpn); | |
4990 | ||
d62a17ae | 4991 | QOBJ_REG(vpn, bgpevpn); |
4992 | return vpn; | |
128ea8ab | 4993 | } |
4994 | ||
4995 | /* | |
4996 | * Free a given VPN - called in multiple scenarios such as zebra | |
4997 | * notification, configuration being deleted, advertise-all-vni disabled etc. | |
4998 | * This just frees appropriate memory, caller should have taken other | |
4999 | * needed actions. | |
5000 | */ | |
d62a17ae | 5001 | void bgp_evpn_free(struct bgp *bgp, struct bgpevpn *vpn) |
128ea8ab | 5002 | { |
6a8657d0 | 5003 | bgpevpn_unlink_from_l3vni(vpn); |
d62a17ae | 5004 | bgp_table_unlock(vpn->route_table); |
5005 | bgp_evpn_unmap_vni_from_its_rts(bgp, vpn); | |
affe9e99 DS |
5006 | list_delete_and_null(&vpn->import_rtl); |
5007 | list_delete_and_null(&vpn->export_rtl); | |
e9eb5f63 | 5008 | bf_release_index(bm->rd_idspace, vpn->rd_id); |
d62a17ae | 5009 | hash_release(bgp->vnihash, vpn); |
5010 | QOBJ_UNREG(vpn); | |
5011 | XFREE(MTYPE_BGP_EVPN, vpn); | |
128ea8ab | 5012 | } |
5013 | ||
5014 | /* | |
50f74cf1 | 5015 | * Lookup local ES. |
5016 | */ | |
5017 | struct evpnes *bgp_evpn_lookup_es(struct bgp *bgp, esi_t *esi) | |
5018 | { | |
5019 | struct evpnes *es; | |
5020 | struct evpnes tmp; | |
5021 | ||
5022 | memset(&tmp, 0, sizeof(struct evpnes)); | |
5023 | memcpy(&tmp.esi, esi, sizeof(esi_t)); | |
5024 | es = hash_lookup(bgp->esihash, &tmp); | |
5025 | return es; | |
5026 | } | |
5027 | ||
5028 | /* | |
5029 | * Create a new local es - invoked upon zebra notification. | |
5030 | */ | |
5031 | struct evpnes *bgp_evpn_es_new(struct bgp *bgp, | |
5032 | esi_t *esi, | |
5033 | struct ipaddr *originator_ip) | |
5034 | { | |
5035 | char buf[100]; | |
5036 | struct evpnes *es; | |
5037 | ||
5038 | if (!bgp) | |
5039 | return NULL; | |
5040 | ||
5041 | es = XCALLOC(MTYPE_BGP_EVPN_ES, sizeof(struct evpnes)); | |
5042 | if (!es) | |
5043 | return NULL; | |
5044 | ||
5045 | /* set the ESI and originator_ip */ | |
5046 | memcpy(&es->esi, esi, sizeof(esi_t)); | |
5047 | memcpy(&es->originator_ip, originator_ip, sizeof(struct ipaddr)); | |
5048 | ||
5049 | /* Initialise the VTEP list */ | |
5050 | es->vtep_list = list_new(); | |
5051 | es->vtep_list->cmp = (int (*)(void *, void *))evpn_vtep_ip_cmp; | |
5052 | ||
5053 | /* auto derive RD for this es */ | |
5054 | bf_assign_index(bm->rd_idspace, es->rd_id); | |
5055 | es->prd.family = AF_UNSPEC; | |
5056 | es->prd.prefixlen = 64; | |
5057 | sprintf(buf, "%s:%hu", inet_ntoa(bgp->router_id), es->rd_id); | |
5058 | (void)str2prefix_rd(buf, &es->prd); | |
5059 | ||
5060 | /* Initialize the ES route table */ | |
5061 | es->route_table = bgp_table_init(bgp, AFI_L2VPN, SAFI_EVPN); | |
5062 | ||
5063 | /* Add to hash */ | |
5064 | if (!hash_get(bgp->esihash, es, hash_alloc_intern)) { | |
5065 | XFREE(MTYPE_BGP_EVPN_ES, es); | |
5066 | return NULL; | |
5067 | } | |
5068 | ||
5069 | QOBJ_REG(es, evpnes); | |
5070 | return es; | |
5071 | } | |
5072 | ||
5073 | /* | |
5074 | * Free a given ES - | |
5075 | * This just frees appropriate memory, caller should have taken other | |
5076 | * needed actions. | |
5077 | */ | |
5078 | void bgp_evpn_es_free(struct bgp *bgp, struct evpnes *es) | |
5079 | { | |
5080 | list_delete_and_null(&es->vtep_list); | |
5081 | bgp_table_unlock(es->route_table); | |
5082 | bf_release_index(bm->rd_idspace, es->rd_id); | |
5083 | hash_release(bgp->esihash, es); | |
5084 | QOBJ_UNREG(es); | |
5085 | XFREE(MTYPE_BGP_EVPN_ES, es); | |
5086 | } | |
5087 | ||
5088 | /* | |
5089 | * Import evpn route from global table to VNI/VRF/ESI. | |
128ea8ab | 5090 | */ |
d62a17ae | 5091 | int bgp_evpn_import_route(struct bgp *bgp, afi_t afi, safi_t safi, |
5092 | struct prefix *p, struct bgp_info *ri) | |
128ea8ab | 5093 | { |
d62a17ae | 5094 | return install_uninstall_evpn_route(bgp, afi, safi, p, ri, 1); |
128ea8ab | 5095 | } |
5096 | ||
5097 | /* | |
50f74cf1 | 5098 | * Unimport evpn route from VNI/VRF/ESI. |
128ea8ab | 5099 | */ |
d62a17ae | 5100 | int bgp_evpn_unimport_route(struct bgp *bgp, afi_t afi, safi_t safi, |
5101 | struct prefix *p, struct bgp_info *ri) | |
128ea8ab | 5102 | { |
d62a17ae | 5103 | return install_uninstall_evpn_route(bgp, afi, safi, p, ri, 0); |
128ea8ab | 5104 | } |
5105 | ||
db0e1937 MK |
5106 | /* filter routes which have martian next hops */ |
5107 | int bgp_filter_evpn_routes_upon_martian_nh_change(struct bgp *bgp) | |
5108 | { | |
0291c246 MK |
5109 | afi_t afi; |
5110 | safi_t safi; | |
5111 | struct bgp_node *rd_rn, *rn; | |
5112 | struct bgp_table *table; | |
5113 | struct bgp_info *ri; | |
db0e1937 MK |
5114 | |
5115 | afi = AFI_L2VPN; | |
5116 | safi = SAFI_EVPN; | |
5117 | ||
5118 | /* Walk entire global routing table and evaluate routes which could be | |
5119 | * imported into this VPN. Note that we cannot just look at the routes | |
5120 | * for the VNI's RD - | |
5121 | * remote routes applicable for this VNI could have any RD. | |
5122 | */ | |
5123 | /* EVPN routes are a 2-level table. */ | |
5124 | for (rd_rn = bgp_table_top(bgp->rib[afi][safi]); rd_rn; | |
5125 | rd_rn = bgp_route_next(rd_rn)) { | |
5126 | table = (struct bgp_table *)(rd_rn->info); | |
5127 | if (!table) | |
5128 | continue; | |
5129 | ||
5130 | for (rn = bgp_table_top(table); rn; rn = bgp_route_next(rn)) { | |
5131 | ||
5132 | for (ri = rn->info; ri; ri = ri->next) { | |
5133 | ||
5134 | /* Consider "valid" remote routes applicable for | |
5135 | * this VNI. */ | |
5136 | if (!(ri->type == ZEBRA_ROUTE_BGP | |
5137 | && ri->sub_type == BGP_ROUTE_NORMAL)) | |
5138 | continue; | |
5139 | ||
60466a63 | 5140 | if (bgp_nexthop_self(bgp, ri->attr->nexthop)) { |
db0e1937 MK |
5141 | |
5142 | char attr_str[BUFSIZ]; | |
5143 | char pbuf[PREFIX_STRLEN]; | |
5144 | ||
5145 | bgp_dump_attr(ri->attr, attr_str, | |
5146 | BUFSIZ); | |
5147 | ||
5148 | if (bgp_debug_update(ri->peer, &rn->p, | |
5149 | NULL, 1)) | |
5150 | zlog_debug( | |
b682f6de | 5151 | "%u: prefix %s with attr %s - DENIED due to martian or self nexthop", |
db0e1937 MK |
5152 | bgp->vrf_id, |
5153 | prefix2str( | |
60466a63 | 5154 | &rn->p, pbuf, |
db0e1937 MK |
5155 | sizeof(pbuf)), |
5156 | attr_str); | |
5157 | ||
5158 | bgp_evpn_unimport_route(bgp, afi, safi, | |
5159 | &rn->p, ri); | |
5160 | ||
60466a63 QY |
5161 | bgp_rib_remove(rn, ri, ri->peer, afi, |
5162 | safi); | |
db0e1937 | 5163 | } |
db0e1937 MK |
5164 | } |
5165 | } | |
5166 | } | |
5167 | ||
5168 | return 0; | |
5169 | } | |
5170 | ||
128ea8ab | 5171 | /* |
5172 | * Handle del of a local MACIP. | |
5173 | */ | |
d62a17ae | 5174 | int bgp_evpn_local_macip_del(struct bgp *bgp, vni_t vni, struct ethaddr *mac, |
5175 | struct ipaddr *ip) | |
128ea8ab | 5176 | { |
d62a17ae | 5177 | struct bgpevpn *vpn; |
5178 | struct prefix_evpn p; | |
128ea8ab | 5179 | |
d62a17ae | 5180 | /* Lookup VNI hash - should exist. */ |
5181 | vpn = bgp_evpn_lookup_vni(bgp, vni); | |
5182 | if (!vpn || !is_vni_live(vpn)) { | |
5183 | zlog_warn("%u: VNI hash entry for VNI %u %s at MACIP DEL", | |
5184 | bgp->vrf_id, vni, vpn ? "not live" : "not found"); | |
5185 | return -1; | |
5186 | } | |
128ea8ab | 5187 | |
d62a17ae | 5188 | /* Remove EVPN type-2 route and schedule for processing. */ |
5189 | build_evpn_type2_prefix(&p, mac, ip); | |
5190 | delete_evpn_route(bgp, vpn, &p); | |
128ea8ab | 5191 | |
d62a17ae | 5192 | return 0; |
128ea8ab | 5193 | } |
5194 | ||
5195 | /* | |
5196 | * Handle add of a local MACIP. | |
5197 | */ | |
d62a17ae | 5198 | int bgp_evpn_local_macip_add(struct bgp *bgp, vni_t vni, struct ethaddr *mac, |
d7c0a89a | 5199 | struct ipaddr *ip, uint8_t flags) |
128ea8ab | 5200 | { |
d62a17ae | 5201 | struct bgpevpn *vpn; |
5202 | struct prefix_evpn p; | |
128ea8ab | 5203 | |
d62a17ae | 5204 | /* Lookup VNI hash - should exist. */ |
5205 | vpn = bgp_evpn_lookup_vni(bgp, vni); | |
5206 | if (!vpn || !is_vni_live(vpn)) { | |
5207 | zlog_warn("%u: VNI hash entry for VNI %u %s at MACIP ADD", | |
5208 | bgp->vrf_id, vni, vpn ? "not live" : "not found"); | |
5209 | return -1; | |
5210 | } | |
128ea8ab | 5211 | |
d62a17ae | 5212 | /* Create EVPN type-2 route and schedule for processing. */ |
5213 | build_evpn_type2_prefix(&p, mac, ip); | |
1a98c087 | 5214 | if (update_evpn_route(bgp, vpn, &p, flags)) { |
d62a17ae | 5215 | char buf[ETHER_ADDR_STRLEN]; |
5216 | char buf2[INET6_ADDRSTRLEN]; | |
128ea8ab | 5217 | |
af4c2728 | 5218 | flog_err( |
14454c9f | 5219 | BGP_ERR_EVPN_ROUTE_CREATE, |
ead40654 | 5220 | "%u:Failed to create Type-2 route, VNI %u %s MAC %s IP %s (flags: 0x%x)", |
1a98c087 | 5221 | bgp->vrf_id, vpn->vni, |
996c9314 LB |
5222 | CHECK_FLAG(flags, ZEBRA_MACIP_TYPE_STICKY) |
5223 | ? "sticky gateway" | |
5224 | : "", | |
d62a17ae | 5225 | prefix_mac2str(mac, buf, sizeof(buf)), |
996c9314 | 5226 | ipaddr2str(ip, buf2, sizeof(buf2)), flags); |
d62a17ae | 5227 | return -1; |
5228 | } | |
128ea8ab | 5229 | |
d62a17ae | 5230 | return 0; |
128ea8ab | 5231 | } |
5232 | ||
6a8657d0 MK |
5233 | static void link_l2vni_hash_to_l3vni(struct hash_backet *backet, |
5234 | struct bgp *bgp_vrf) | |
5235 | { | |
0a1a07cb | 5236 | struct bgpevpn *vpn = (struct bgpevpn *)backet->data; |
6a8657d0 MK |
5237 | struct bgp *bgp_def = NULL; |
5238 | ||
5239 | bgp_def = bgp_get_default(); | |
5240 | assert(bgp_def); | |
5241 | ||
6a8657d0 MK |
5242 | if (vpn->tenant_vrf_id == bgp_vrf->vrf_id) |
5243 | bgpevpn_link_to_l3vni(vpn); | |
5244 | } | |
5245 | ||
996c9314 LB |
5246 | int bgp_evpn_local_l3vni_add(vni_t l3vni, vrf_id_t vrf_id, struct ethaddr *rmac, |
5247 | struct in_addr originator_ip, int filter) | |
fe1dc5a3 MK |
5248 | { |
5249 | struct bgp *bgp_vrf = NULL; /* bgp VRF instance */ | |
5250 | struct bgp *bgp_def = NULL; /* default bgp instance */ | |
f1f8b53c MK |
5251 | struct listnode *node = NULL; |
5252 | struct bgpevpn *vpn = NULL; | |
fe1dc5a3 MK |
5253 | as_t as = 0; |
5254 | ||
d846168d | 5255 | /* get the default instance - required to get the AS number for VRF |
523cafc4 | 5256 | * auto-creatio |
5257 | */ | |
fe1dc5a3 MK |
5258 | bgp_def = bgp_get_default(); |
5259 | if (!bgp_def) { | |
af4c2728 | 5260 | flog_err( |
14454c9f | 5261 | BGP_ERR_NO_DFLT, |
996c9314 LB |
5262 | "Cannot process L3VNI %u ADD - default BGP instance not yet created", |
5263 | l3vni); | |
fe1dc5a3 MK |
5264 | return -1; |
5265 | } | |
5266 | as = bgp_def->as; | |
5267 | ||
5268 | /* if the BGP vrf instance doesnt exist - create one */ | |
0b5131c9 | 5269 | bgp_vrf = bgp_lookup_by_name(vrf_id_to_name(vrf_id)); |
fe1dc5a3 MK |
5270 | if (!bgp_vrf) { |
5271 | ||
5272 | int ret = 0; | |
5273 | ||
5274 | ret = bgp_get(&bgp_vrf, &as, vrf_id_to_name(vrf_id), | |
5275 | BGP_INSTANCE_TYPE_VRF); | |
5276 | switch (ret) { | |
5277 | case BGP_ERR_MULTIPLE_INSTANCE_NOT_SET: | |
af4c2728 | 5278 | flog_err(BGP_ERR_MULTI_INSTANCE, |
14454c9f | 5279 | "'bgp multiple-instance' not present\n"); |
fe1dc5a3 MK |
5280 | return -1; |
5281 | case BGP_ERR_AS_MISMATCH: | |
af4c2728 | 5282 | flog_err(BGP_ERR_EVPN_AS_MISMATCH, |
14454c9f | 5283 | "BGP is already running; AS is %u\n", as); |
fe1dc5a3 MK |
5284 | return -1; |
5285 | case BGP_ERR_INSTANCE_MISMATCH: | |
af4c2728 | 5286 | flog_err(BGP_ERR_EVPN_INSTANCE_MISMATCH, |
14454c9f | 5287 | "BGP instance name and AS number mismatch\n"); |
fe1dc5a3 MK |
5288 | return -1; |
5289 | } | |
5290 | ||
5291 | /* mark as auto created */ | |
5292 | SET_FLAG(bgp_vrf->vrf_flags, BGP_VRF_AUTO); | |
5293 | } | |
5294 | ||
5295 | /* associate with l3vni */ | |
5296 | bgp_vrf->l3vni = l3vni; | |
5297 | ||
5298 | /* set the router mac - to be used in mac-ip routes for this vrf */ | |
5299 | memcpy(&bgp_vrf->rmac, rmac, sizeof(struct ethaddr)); | |
5300 | ||
b67a60d2 | 5301 | /* set the originator ip */ |
5302 | bgp_vrf->originator_ip = originator_ip; | |
5303 | ||
c48d9f5f MK |
5304 | /* set the right filter - are we using l3vni only for prefix routes? */ |
5305 | if (filter) | |
5306 | SET_FLAG(bgp_vrf->vrf_flags, BGP_VRF_L3VNI_PREFIX_ROUTES_ONLY); | |
5307 | ||
c581d8b0 MK |
5308 | /* auto derive RD/RT */ |
5309 | if (!CHECK_FLAG(bgp_vrf->vrf_flags, BGP_VRF_IMPORT_RT_CFGD)) | |
5310 | evpn_auto_rt_import_add_for_vrf(bgp_vrf); | |
5311 | if (!CHECK_FLAG(bgp_vrf->vrf_flags, BGP_VRF_EXPORT_RT_CFGD)) | |
5312 | evpn_auto_rt_export_add_for_vrf(bgp_vrf); | |
676f83b9 | 5313 | bgp_evpn_derive_auto_rd_for_vrf(bgp_vrf); |
fe1dc5a3 | 5314 | |
6a8657d0 MK |
5315 | /* link all corresponding l2vnis */ |
5316 | hash_iterate(bgp_def->vnihash, | |
996c9314 LB |
5317 | (void (*)(struct hash_backet *, |
5318 | void *))link_l2vni_hash_to_l3vni, | |
6a8657d0 MK |
5319 | bgp_vrf); |
5320 | ||
c48d9f5f MK |
5321 | /* Only update all corresponding type-2 routes if we are advertising two |
5322 | * labels along with type-2 routes | |
5323 | */ | |
5324 | if (!filter) | |
5325 | for (ALL_LIST_ELEMENTS_RO(bgp_vrf->l2vnis, node, vpn)) | |
5326 | update_routes_for_vni(bgp_def, vpn); | |
fe1dc5a3 | 5327 | |
06d2e8f3 MK |
5328 | /* advertise type-5 routes if needed */ |
5329 | update_advertise_vrf_routes(bgp_vrf); | |
5330 | ||
5ba238b7 MK |
5331 | /* install all remote routes belonging to this l3vni into correspondng |
5332 | * vrf */ | |
5333 | install_routes_for_vrf(bgp_vrf); | |
fe1dc5a3 MK |
5334 | |
5335 | return 0; | |
5336 | } | |
5337 | ||
996c9314 | 5338 | int bgp_evpn_local_l3vni_del(vni_t l3vni, vrf_id_t vrf_id) |
fe1dc5a3 MK |
5339 | { |
5340 | struct bgp *bgp_vrf = NULL; /* bgp vrf instance */ | |
f1f8b53c MK |
5341 | struct bgp *bgp_def = NULL; /* default bgp instance */ |
5342 | struct listnode *node = NULL; | |
18abc1eb | 5343 | struct listnode *next = NULL; |
f1f8b53c | 5344 | struct bgpevpn *vpn = NULL; |
fe1dc5a3 MK |
5345 | |
5346 | bgp_vrf = bgp_lookup_by_vrf_id(vrf_id); | |
5347 | if (!bgp_vrf) { | |
af4c2728 | 5348 | flog_err( |
14454c9f | 5349 | BGP_ERR_NO_DFLT, |
996c9314 LB |
5350 | "Cannot process L3VNI %u Del - Could not find BGP instance", |
5351 | l3vni); | |
fe1dc5a3 MK |
5352 | return -1; |
5353 | } | |
5354 | ||
f1f8b53c MK |
5355 | bgp_def = bgp_get_default(); |
5356 | if (!bgp_def) { | |
af4c2728 | 5357 | flog_err( |
14454c9f | 5358 | BGP_ERR_NO_DFLT, |
996c9314 LB |
5359 | "Cannot process L3VNI %u Del - Could not find default BGP instance", |
5360 | l3vni); | |
f1f8b53c MK |
5361 | return -1; |
5362 | } | |
5363 | ||
d846168d CS |
5364 | /* Remove remote routes from BGT VRF even if BGP_VRF_AUTO is configured, |
5365 | * bgp_delete would not remove/decrement bgp_info of the ip_prefix | |
5366 | * routes. This will uninstalling the routes from zebra and decremnt the | |
5367 | * bgp info count. | |
523cafc4 | 5368 | */ |
d846168d | 5369 | uninstall_routes_for_vrf(bgp_vrf); |
5ba238b7 | 5370 | |
06d2e8f3 MK |
5371 | /* delete/withdraw all type-5 routes */ |
5372 | delete_withdraw_vrf_routes(bgp_vrf); | |
5373 | ||
fe1dc5a3 MK |
5374 | /* remove the l3vni from vrf instance */ |
5375 | bgp_vrf->l3vni = 0; | |
5376 | ||
5377 | /* remove the Rmac from the BGP vrf */ | |
5378 | memset(&bgp_vrf->rmac, 0, sizeof(struct ethaddr)); | |
5379 | ||
c581d8b0 | 5380 | /* delete RD/RT */ |
1525e99f | 5381 | if (!list_isempty(bgp_vrf->vrf_import_rtl)) { |
10ebe1ab | 5382 | bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf); |
5ba238b7 | 5383 | list_delete_all_node(bgp_vrf->vrf_import_rtl); |
23a06e11 | 5384 | } |
1525e99f | 5385 | if (!list_isempty(bgp_vrf->vrf_export_rtl)) { |
5ba238b7 | 5386 | list_delete_all_node(bgp_vrf->vrf_export_rtl); |
23a06e11 | 5387 | } |
fe1dc5a3 | 5388 | |
f1f8b53c | 5389 | /* update all corresponding local mac-ip routes */ |
c48d9f5f MK |
5390 | if (!CHECK_FLAG(bgp_vrf->vrf_flags, BGP_VRF_L3VNI_PREFIX_ROUTES_ONLY)) { |
5391 | for (ALL_LIST_ELEMENTS_RO(bgp_vrf->l2vnis, node, vpn)) { | |
5392 | UNSET_FLAG(vpn->flags, VNI_FLAG_USE_TWO_LABELS); | |
5393 | update_routes_for_vni(bgp_def, vpn); | |
5394 | } | |
5395 | } | |
fe1dc5a3 | 5396 | |
18abc1eb | 5397 | /* If any L2VNIs point to this instance, unlink them. */ |
5398 | for (ALL_LIST_ELEMENTS(bgp_vrf->l2vnis, node, next, vpn)) | |
5399 | bgpevpn_unlink_from_l3vni(vpn); | |
5400 | ||
fe1dc5a3 MK |
5401 | /* Delete the instance if it was autocreated */ |
5402 | if (CHECK_FLAG(bgp_vrf->vrf_flags, BGP_VRF_AUTO)) | |
5403 | bgp_delete(bgp_vrf); | |
5404 | ||
5405 | return 0; | |
5406 | } | |
5407 | ||
128ea8ab | 5408 | /* |
5409 | * Handle del of a local VNI. | |
5410 | */ | |
d62a17ae | 5411 | int bgp_evpn_local_vni_del(struct bgp *bgp, vni_t vni) |
128ea8ab | 5412 | { |
d62a17ae | 5413 | struct bgpevpn *vpn; |
128ea8ab | 5414 | |
d62a17ae | 5415 | /* Locate VNI hash */ |
5416 | vpn = bgp_evpn_lookup_vni(bgp, vni); | |
5417 | if (!vpn) { | |
1e00627b | 5418 | if (bgp_debug_zebra(NULL)) |
3518f352 DS |
5419 | zlog_warn( |
5420 | "%u: VNI hash entry for VNI %u not found at DEL", | |
5421 | bgp->vrf_id, vni); | |
d62a17ae | 5422 | return 0; |
5423 | } | |
128ea8ab | 5424 | |
d62a17ae | 5425 | /* Remove all local EVPN routes and schedule for processing (to |
5426 | * withdraw from peers). | |
5427 | */ | |
5428 | delete_routes_for_vni(bgp, vpn); | |
128ea8ab | 5429 | |
db0e1937 MK |
5430 | /* |
5431 | * tunnel is no longer active, del tunnel ip address from tip_hash | |
5432 | */ | |
5433 | bgp_tip_del(bgp, &vpn->originator_ip); | |
5434 | ||
d62a17ae | 5435 | /* Clear "live" flag and see if hash needs to be freed. */ |
5436 | UNSET_FLAG(vpn->flags, VNI_FLAG_LIVE); | |
5437 | if (!is_vni_configured(vpn)) | |
5438 | bgp_evpn_free(bgp, vpn); | |
128ea8ab | 5439 | |
d62a17ae | 5440 | return 0; |
128ea8ab | 5441 | } |
5442 | ||
5443 | /* | |
d1911c26 | 5444 | * Handle add (or update) of a local VNI. The VNI changes we care |
5445 | * about are for the local-tunnel-ip and the (tenant) VRF. | |
128ea8ab | 5446 | */ |
d62a17ae | 5447 | int bgp_evpn_local_vni_add(struct bgp *bgp, vni_t vni, |
996c9314 | 5448 | struct in_addr originator_ip, vrf_id_t tenant_vrf_id) |
d62a17ae | 5449 | { |
5450 | struct bgpevpn *vpn; | |
5451 | struct prefix_evpn p; | |
5452 | ||
d62a17ae | 5453 | /* Lookup VNI. If present and no change, exit. */ |
5454 | vpn = bgp_evpn_lookup_vni(bgp, vni); | |
ddd16ed5 | 5455 | if (vpn) { |
29c53922 | 5456 | |
d1911c26 | 5457 | if (is_vni_live(vpn) |
5458 | && IPV4_ADDR_SAME(&vpn->originator_ip, &originator_ip) | |
5459 | && vpn->tenant_vrf_id == tenant_vrf_id) | |
5460 | /* Probably some other param has changed that we don't | |
5461 | * care about. */ | |
5462 | return 0; | |
5463 | ||
5464 | /* Update tenant_vrf_id if it has changed. */ | |
6a8657d0 MK |
5465 | if (vpn->tenant_vrf_id != tenant_vrf_id) { |
5466 | bgpevpn_unlink_from_l3vni(vpn); | |
29c53922 | 5467 | vpn->tenant_vrf_id = tenant_vrf_id; |
6a8657d0 MK |
5468 | bgpevpn_link_to_l3vni(vpn); |
5469 | } | |
29c53922 | 5470 | |
d1911c26 | 5471 | /* If tunnel endpoint IP has changed, update (and delete prior |
5472 | * type-3 route, if needed.) | |
5473 | */ | |
5474 | if (!IPV4_ADDR_SAME(&vpn->originator_ip, &originator_ip)) | |
5475 | handle_tunnel_ip_change(bgp, vpn, originator_ip); | |
d62a17ae | 5476 | |
d1911c26 | 5477 | /* Update all routes with new endpoint IP and/or export RT |
5478 | * for VRFs | |
5479 | */ | |
5480 | if (is_vni_live(vpn)) | |
5481 | update_routes_for_vni(bgp, vpn); | |
d62a17ae | 5482 | } |
5483 | ||
5484 | /* Create or update as appropriate. */ | |
5485 | if (!vpn) { | |
29c53922 | 5486 | vpn = bgp_evpn_new(bgp, vni, originator_ip, tenant_vrf_id); |
d62a17ae | 5487 | if (!vpn) { |
af4c2728 | 5488 | flog_err( |
14454c9f | 5489 | BGP_ERR_VNI, |
d62a17ae | 5490 | "%u: Failed to allocate VNI entry for VNI %u - at Add", |
5491 | bgp->vrf_id, vni); | |
5492 | return -1; | |
5493 | } | |
5494 | } | |
5495 | ||
db0e1937 | 5496 | /* if the VNI is live already, there is nothing more to do */ |
ddd16ed5 MK |
5497 | if (is_vni_live(vpn)) |
5498 | return 0; | |
5499 | ||
d62a17ae | 5500 | /* Mark as "live" */ |
5501 | SET_FLAG(vpn->flags, VNI_FLAG_LIVE); | |
5502 | ||
db0e1937 MK |
5503 | /* tunnel is now active, add tunnel-ip to db */ |
5504 | bgp_tip_add(bgp, &originator_ip); | |
5505 | ||
5506 | /* filter routes as nexthop database has changed */ | |
5507 | bgp_filter_evpn_routes_upon_martian_nh_change(bgp); | |
5508 | ||
d62a17ae | 5509 | /* Create EVPN type-3 route and schedule for processing. */ |
5510 | build_evpn_type3_prefix(&p, vpn->originator_ip); | |
5511 | if (update_evpn_route(bgp, vpn, &p, 0)) { | |
af4c2728 | 5512 | flog_err(BGP_ERR_EVPN_ROUTE_CREATE, |
14454c9f DS |
5513 | "%u: Type3 route creation failure for VNI %u", |
5514 | bgp->vrf_id, vni); | |
d62a17ae | 5515 | return -1; |
5516 | } | |
5517 | ||
5518 | /* If we have learnt and retained remote routes (VTEPs, MACs) for this | |
5519 | * VNI, | |
5520 | * install them. | |
5521 | */ | |
5522 | install_routes_for_vni(bgp, vpn); | |
5523 | ||
d7d97010 MK |
5524 | /* If we are advertising gateway mac-ip |
5525 | It needs to be conveyed again to zebra */ | |
5526 | bgp_zebra_advertise_gw_macip(bgp, vpn->advertise_gw_macip, vpn->vni); | |
5527 | ||
d62a17ae | 5528 | return 0; |
b18825eb | 5529 | } |
14c1a7bf | 5530 | |
50f74cf1 | 5531 | /* |
5532 | * bgp_evpn_local_es_del | |
5533 | */ | |
5534 | int bgp_evpn_local_es_del(struct bgp *bgp, | |
5535 | esi_t *esi, | |
5536 | struct ipaddr *originator_ip) | |
5537 | { | |
5538 | char buf[ESI_STR_LEN]; | |
5539 | struct evpnes *es = NULL; | |
5540 | ||
5541 | if (!bgp->esihash) { | |
af4c2728 | 5542 | flog_err(BGP_ERR_ES_CREATE, "%u: ESI hash not yet created", |
14454c9f | 5543 | bgp->vrf_id); |
50f74cf1 | 5544 | return -1; |
5545 | } | |
5546 | ||
5547 | /* Lookup ESI hash - should exist. */ | |
5548 | es = bgp_evpn_lookup_es(bgp, esi); | |
5549 | if (!es) { | |
5550 | zlog_warn("%u: ESI hash entry for ESI %s at Local ES DEL", | |
5551 | bgp->vrf_id, | |
5552 | esi_to_str(esi, buf, sizeof(buf))); | |
5553 | return -1; | |
5554 | } | |
5555 | ||
5556 | /* Delete all local EVPN ES routes from ESI table | |
2bb9eff4 | 5557 | * and schedule for processing (to withdraw from peers)) |
50f74cf1 | 5558 | */ |
5559 | delete_routes_for_es(bgp, es); | |
5560 | ||
5561 | /* free the hash entry */ | |
5562 | bgp_evpn_es_free(bgp, es); | |
5563 | ||
5564 | return 0; | |
5565 | } | |
5566 | ||
5567 | /* | |
5568 | * bgp_evpn_local_es_add | |
5569 | */ | |
5570 | int bgp_evpn_local_es_add(struct bgp *bgp, | |
5571 | esi_t *esi, | |
5572 | struct ipaddr *originator_ip) | |
5573 | { | |
5574 | char buf[ESI_STR_LEN]; | |
5575 | struct evpnes *es = NULL; | |
5576 | struct prefix_evpn p; | |
5577 | ||
5578 | if (!bgp->esihash) { | |
af4c2728 | 5579 | flog_err(BGP_ERR_ES_CREATE, "%u: ESI hash not yet created", |
14454c9f | 5580 | bgp->vrf_id); |
50f74cf1 | 5581 | return -1; |
5582 | } | |
5583 | ||
5584 | /* create the new es */ | |
2bb9eff4 | 5585 | es = bgp_evpn_lookup_es(bgp, esi); |
50f74cf1 | 5586 | if (!es) { |
5587 | es = bgp_evpn_es_new(bgp, esi, originator_ip); | |
5588 | if (!es) { | |
af4c2728 | 5589 | flog_err( |
14454c9f | 5590 | BGP_ERR_ES_CREATE, |
50f74cf1 | 5591 | "%u: Failed to allocate ES entry for ESI %s - at Local ES Add", |
5592 | bgp->vrf_id, esi_to_str(esi, buf, sizeof(buf))); | |
5593 | return -1; | |
5594 | } | |
5595 | } | |
5596 | UNSET_FLAG(es->flags, EVPNES_REMOTE); | |
5597 | SET_FLAG(es->flags, EVPNES_LOCAL); | |
5598 | ||
5599 | build_evpn_type4_prefix(&p, esi, originator_ip->ipaddr_v4); | |
5600 | if (update_evpn_type4_route(bgp, es, &p)) { | |
af4c2728 | 5601 | flog_err(BGP_ERR_EVPN_ROUTE_CREATE, |
14454c9f DS |
5602 | "%u: Type4 route creation failure for ESI %s", |
5603 | bgp->vrf_id, esi_to_str(esi, buf, sizeof(buf))); | |
50f74cf1 | 5604 | return -1; |
5605 | } | |
5606 | ||
5607 | /* import all remote ES routes in th ES table */ | |
5608 | install_routes_for_es(bgp, es); | |
5609 | ||
5610 | return 0; | |
5611 | } | |
5612 | ||
7724c0a1 | 5613 | /* |
5614 | * Cleanup EVPN information on disable - Need to delete and withdraw | |
5615 | * EVPN routes from peers. | |
5616 | */ | |
d62a17ae | 5617 | void bgp_evpn_cleanup_on_disable(struct bgp *bgp) |
7724c0a1 | 5618 | { |
9d303b37 DL |
5619 | hash_iterate(bgp->vnihash, (void (*)(struct hash_backet *, |
5620 | void *))cleanup_vni_on_disable, | |
5621 | bgp); | |
7724c0a1 | 5622 | } |
5623 | ||
14c1a7bf | 5624 | /* |
5625 | * Cleanup EVPN information - invoked at the time of bgpd exit or when the | |
5626 | * BGP instance (default) is being freed. | |
5627 | */ | |
d62a17ae | 5628 | void bgp_evpn_cleanup(struct bgp *bgp) |
14c1a7bf | 5629 | { |
1525e99f DS |
5630 | hash_iterate(bgp->vnihash, |
5631 | (void (*)(struct hash_backet *, void *))free_vni_entry, | |
5632 | bgp); | |
5633 | ||
5634 | hash_free(bgp->import_rt_hash); | |
d62a17ae | 5635 | bgp->import_rt_hash = NULL; |
1525e99f DS |
5636 | |
5637 | hash_free(bgp->vrf_import_rt_hash); | |
10ebe1ab | 5638 | bgp->vrf_import_rt_hash = NULL; |
1525e99f DS |
5639 | |
5640 | hash_free(bgp->vnihash); | |
d62a17ae | 5641 | bgp->vnihash = NULL; |
50f74cf1 | 5642 | if (bgp->esihash) |
5643 | hash_free(bgp->esihash); | |
5644 | bgp->esihash = NULL; | |
1525e99f DS |
5645 | |
5646 | list_delete_and_null(&bgp->vrf_import_rtl); | |
5647 | list_delete_and_null(&bgp->vrf_export_rtl); | |
5648 | list_delete_and_null(&bgp->l2vnis); | |
14c1a7bf | 5649 | } |
5650 | ||
5651 | /* | |
5652 | * Initialization for EVPN | |
5653 | * Create | |
5654 | * VNI hash table | |
5655 | * hash for RT to VNI | |
14c1a7bf | 5656 | */ |
d62a17ae | 5657 | void bgp_evpn_init(struct bgp *bgp) |
5658 | { | |
5659 | bgp->vnihash = | |
5660 | hash_create(vni_hash_key_make, vni_hash_cmp, "BGP VNI Hash"); | |
50f74cf1 | 5661 | bgp->esihash = |
5662 | hash_create(esi_hash_keymake, esi_cmp, | |
5663 | "BGP EVPN Local ESI Hash"); | |
d62a17ae | 5664 | bgp->import_rt_hash = |
5665 | hash_create(import_rt_hash_key_make, import_rt_hash_cmp, | |
5666 | "BGP Import RT Hash"); | |
10ebe1ab MK |
5667 | bgp->vrf_import_rt_hash = |
5668 | hash_create(vrf_import_rt_hash_key_make, vrf_import_rt_hash_cmp, | |
5669 | "BGP VRF Import RT Hash"); | |
c581d8b0 MK |
5670 | bgp->vrf_import_rtl = list_new(); |
5671 | bgp->vrf_import_rtl->cmp = | |
5672 | (int (*)(void *, void *))evpn_route_target_cmp; | |
987d8198 | 5673 | bgp->vrf_import_rtl->del = evpn_xxport_delete_ecomm; |
c581d8b0 MK |
5674 | bgp->vrf_export_rtl = list_new(); |
5675 | bgp->vrf_export_rtl->cmp = | |
5676 | (int (*)(void *, void *))evpn_route_target_cmp; | |
987d8198 | 5677 | bgp->vrf_export_rtl->del = evpn_xxport_delete_ecomm; |
6a8657d0 | 5678 | bgp->l2vnis = list_new(); |
996c9314 | 5679 | bgp->l2vnis->cmp = (int (*)(void *, void *))vni_hash_cmp; |
14c1a7bf | 5680 | } |
10ebe1ab MK |
5681 | |
5682 | void bgp_evpn_vrf_delete(struct bgp *bgp_vrf) | |
5683 | { | |
5684 | bgp_evpn_unmap_vrf_from_its_rts(bgp_vrf); | |
5685 | } |